Documentation

CatCryptCore.NonUniform.Sample

Sampling from an arbitrary sub-distribution #

SPComp.sample samples uniformly from a finite nonempty type. sampleFrom d samples from an arbitrary SDistr, leaving the heap unchanged, so that non-uniform distributions — centered binomial, rejection-sampling output, biased coins, discrete Gaussians — are expressible as SPComp programs.

Main definitions #

Main results #

The combinator #

noncomputable def CatCrypt.NonUniform.sampleFrom {α : Type u_1} (d : Prob.SDistr α) :

Sample from an arbitrary sub-distribution, leaving the heap unchanged. The computation fails with probability d none.

Equations
Instances For
    @[simp]
    theorem CatCrypt.NonUniform.sampleFrom_apply {α : Type u_1} (d : Prob.SDistr α) (h : Core.Heap) :
    sampleFrom d h = d.bind fun (a : α) => Prob.SDistr.pure (a, h)

    Uniform sampling is the sampleFrom at the uniform distribution.

    sampleFrom agrees with the SDistr → SPComp lift already in Core.Code.

    Monadic laws #

    theorem CatCrypt.NonUniform.sampleFrom_bind_apply {α : Type u_1} {β : Type u_2} (d : Prob.SDistr α) (k : α → Core.SPComp β) (h : Core.Heap) :
    (sampleFrom d).bind k h = d.bind fun (a : α) => k a h

    Bind interaction: the sampled value is fed to the continuation at the unchanged heap, so the whole computation is a bind at the SDistr level.

    theorem CatCrypt.NonUniform.sampleFrom_hBind_apply {α β : Type} (d : Prob.SDistr α) (k : α → Core.SPComp β) (h : Core.Heap) :
    (sampleFrom d >>= k) h = d.bind fun (a : α) => k a h

    Operator form of sampleFrom_bind_apply.

    @[simp]

    Sampling from a point mass is returning.

    @[simp]

    Sampling from the everywhere-failing distribution is failure.

    theorem CatCrypt.NonUniform.sampleFrom_bind_sampleFrom {α : Type u_1} {β : Type u_2} (d : Prob.SDistr α) (f : α → Prob.SDistr β) :
    ((sampleFrom d).bind fun (a : α) => sampleFrom (f a)) = sampleFrom (d.bind f)

    Two successive samples are one sample from the bound distribution.

    sampleFrom d does not read or write the heap.

    Mass and losslessness #

    The failure probability of sampleFrom d is the failure probability of d.

    The mass of sampleFrom d is the mass of d.

    sampleFrom d is lossless exactly when d is a total distribution.

    A total distribution gives a lossless computation.

    Discarding a lossless sample #

    theorem CatCrypt.NonUniform.bind_const_of_mass_one {α : Type u_1} {β : Type u_2} {d : Prob.SDistr α} (hd : d.mass = 1) (e : Prob.SDistr β) :
    (d.bind fun (x : α) => e) = e

    Binding a total distribution to a constant continuation is that constant. Generalizes SDistr.uniform_bind_const from uniform to arbitrary mass-one distributions.

    theorem CatCrypt.NonUniform.dead_sampleFrom {α : Type u_1} {β : Type u_2} {d : Prob.SDistr α} (hd : d.mass = 1) (k : Core.SPComp β) :
    ((sampleFrom d).bind fun (x : α) => k) = k

    A lossless sample whose result is unused can be dropped. Generalizes dead_sample from uniform to arbitrary mass-one distributions.

    Total distributions from PMF #

    noncomputable def CatCrypt.NonUniform.ofPMF {α : Type u_1} (p : PMF α) :

    A PMF viewed as a never-failing SDistr.

    Equations
    Instances For
      @[simp]
      theorem CatCrypt.NonUniform.ofPMF_apply_some {α : Type u_1} (p : PMF α) (a : α) :
      (ofPMF p) (some a) = p a
      @[simp]
      theorem CatCrypt.NonUniform.ofPMF_apply_none {α : Type u_1} (p : PMF α) :
      (ofPMF p) none = 0
      @[simp]
      theorem CatCrypt.NonUniform.mass_ofPMF {α : Type u_1} (p : PMF α) :
      (ofPMF p).mass = 1

      SDistr.uniform is ofPMF of the uniform PMF.