introvoyz045's picture
Migrated from GitHub
4a56cd6 verified
Raw History Blame Contribute Delete
16.1 kB
import Mathlib
import OAI.Probability.LogConcave.OraclePrograms.TerminalMean
import OAI.Probability.LogConcave.Sampling.TerminalMean
namespace OAI
section
section
noncomputable section
open MeasureTheory
namespace LogConcaveSampling.OracleCompiler
inductive Expression (X : Type*) [MeasurableSpace X] (d : β„•) where
| noise (base : X β†’ Point d) (measurable_base : Measurable base) (noise : ℝ)
| add (a : ℝ) (center : Expression X d) (k q : β„•)
(child : Program (Point d Γ— (Fin k β†’ Point d)) d q (Point d))
(P : (Fin k β†’ Point d) β†’ (Fin k β†’ Point d)) (measurable_P : Measurable P)
(rest : Expression X d)
namespace Expression
variable {X : Type*} [MeasurableSpace X] {d : β„•}
@[reducible] def slots : Expression X d β†’ β„•
| .noise .. => 1
| .add _ C k _ _ _ _ T => C.slots + (k + T.slots)
@[reducible] def calls : Expression X d β†’ β„•
| .noise .. => 0
| .add _ C _ q _ _ _ T => C.calls + (q + T.calls)
def leftBlock {c k t : β„•} (g : Fin (c+(k+t)) β†’ Point d) : Fin c β†’ Point d :=
fun i => g (Fin.castAdd (k+t) i)
def middleBlock {c k t : β„•} (g : Fin (c+(k+t)) β†’ Point d) : Fin k β†’ Point d :=
fun i => g (Fin.natAdd c (Fin.castAdd t i))
def rightBlock {c k t : β„•} (g : Fin (c+(k+t)) β†’ Point d) : Fin t β†’ Point d :=
fun i => g (Fin.natAdd c (Fin.natAdd k i))
lemma measurable_leftBlock (c k t : β„•) : Measurable (leftBlock (d:=d) (c:=c) (k:=k) (t:=t)) := by
unfold leftBlock
fun_prop
lemma measurable_middleBlock (c k t : β„•) : Measurable (middleBlock (d:=d) (c:=c) (k:=k) (t:=t)) := by
unfold middleBlock
fun_prop
lemma measurable_rightBlock (c k t : β„•) : Measurable (rightBlock (d:=d) (c:=c) (k:=k) (t:=t)) := by
unfold rightBlock
fun_prop
def compile : (C : Expression X d) β†’
Program (X Γ— (Fin C.slots β†’ Point d)) d C.calls (Point d)
| .noise b hb n => Program.pure (fun z => b z.1+n β€’ z.2 0) (by fun_prop)
| .add a C k q M P hP T => by
let Ξ© := X Γ— (Fin (C.slots+(k+T.slots)) β†’ Point d)
let A : Program Ξ© d C.calls (Point d) := C.compile.seedMap
(fun z => (z.1,leftBlock z.2)) (by
exact measurable_fst.prodMk ((measurable_leftBlock _ _ _).comp measurable_snd))
let B : Program (Ξ© Γ— Point d) d q (Point d) := M.seedMap
(fun z => (z.2,P (middleBlock z.1.2))) (by
exact measurable_snd.prodMk (hP.comp ((measurable_middleBlock _ _ _).comp
(measurable_snd.comp measurable_fst))))
let D : Program ((Ξ© Γ— Point d) Γ— Point d) d T.calls (Point d) :=
(T.compile.seedMap (fun z => (z.1.1.1,rightBlock z.1.1.2)) (by
exact (measurable_fst.comp (measurable_fst.comp measurable_fst)).prodMk
((measurable_rightBlock _ _ _).comp (measurable_snd.comp
(measurable_fst.comp measurable_fst))))).map
(fun z => a β€’ z.1.2+z.2) (by fun_prop)
exact A.seq (B.seq D)
def eval (V : Point d β†’ ℝ) : (C : Expression X d) β†’ X β†’ (Fin C.slots β†’ Point d) β†’ Point d
| .noise b _ n, x, g => b x+n β€’ g 0
| .add a C _ _ M P _ T, x, g =>
a β€’ M.run V (C.eval V x (leftBlock g),P (middleBlock g))+
T.eval V x (rightBlock g)
theorem compile_run (C : Expression X d) (V : Point d β†’ ℝ)
(x : X) (g : Fin C.slots β†’ Point d) : C.compile.run V (x,g)=C.eval V x g := by
induction C with
| noise b hb n => rfl
| add a C k q M P hP T hC hT =>
simp only [compile,Program.seq_run,Program.seedMap_run,Program.map_run,eval]
rw [hC,hT]
theorem measurable_eval (C : Expression X d) (V : Point d β†’ ℝ)
(hV : Measurable (firstOrderReply V)) :
Measurable (fun z : X Γ— (Fin C.slots β†’ Point d) => C.eval V z.1 z.2) := by
have hr := C.compile.measurable_run V hV
convert hr using 1
funext z
exact (compile_run C V z.1 z.2).symm
end Expression
end LogConcaveSampling.OracleCompiler
end
end
section
noncomputable section
namespace LogConcaveSampling.OracleCompiler.Program
open MeasureTheory
variable {Ω O R Ξ : Type*} [MeasurableSpace Ω] [MeasurableSpace O]
[MeasurableSpace R] [MeasurableSpace Ξ] {d q r : β„•}
structure Equivariant (A : Program Ξ© d q O) (f : Ξ© β†’ Ξ©) (Ο„ : O β†’ O) : Prop where
query : βˆ€i Ο‰ h,A.query i (f Ο‰,h)=A.query i (Ο‰,h)
output : βˆ€Ο‰ h,A.output (f Ο‰,h)=Ο„ (A.output (Ο‰,h))
namespace Equivariant
variable {A : Program Ξ© d q O} {f : Ξ© β†’ Ξ©} {Ο„ : O β†’ O}
theorem history (hA : A.Equivariant f Ο„) (V : Point d β†’ ℝ) (Ο‰ : Ξ©) (n : β„•) :
A.history V (f Ο‰) n=A.history V Ο‰ n := by
induction n with
| zero => rfl
| succ n ih =>
simp only [Program.history,ih]
split_ifs with hn
Β· rw [hA.query]
Β· rfl
theorem run (hA : A.Equivariant f Ο„) (V : Point d β†’ ℝ) (Ο‰ : Ξ©) :
A.run V (f Ο‰)=Ο„ (A.run V Ο‰) := by
unfold Program.run
rw [hA.history,hA.output]
theorem executed_query (hA : A.Equivariant f Ο„) (V : Point d β†’ ℝ) (Ο‰ : Ξ©)
(i : Fin q) :
A.query i (f Ο‰,A.history V (f Ο‰) i.val)=A.query i (Ο‰,A.history V Ο‰ i.val) := by
rw [hA.history,hA.query]
theorem seq (hA : A.Equivariant f Ο„) {B : Program (Ξ© Γ— O) d r R} {Οƒ : R β†’ R}
(hB : B.Equivariant (fun z => (f z.1,Ο„ z.2)) Οƒ) : (A.seq B).Equivariant f Οƒ := by
constructor
Β· intro i Ο‰ h
refine Fin.addCases (fun _ => ?_) (fun j => ?_) i
Β· simp only [Program.seq,Fin.addCases_left]
exact hA.query _ _ _
Β· simp only [Program.seq,Fin.addCases_right,hA.output]
exact hB.query j (Ο‰,A.output (Ο‰,leftTrace h)) (rightTrace h)
Β· intro Ο‰ h
simp only [Program.seq,hA.output]
exact hB.output (Ο‰,A.output (Ο‰,leftTrace h)) (rightTrace h)
theorem seedMap (hA : A.Equivariant f Ο„) (g : Ξ β†’ Ξ©) (hg : Measurable g)
(f' : Ξ β†’ Ξ) (hf : βˆ€ΞΎ,g (f' ΞΎ)=f (g ΞΎ)) : (A.seedMap g hg).Equivariant f' Ο„ := by
constructor
Β· intro i ΞΎ h
simpa only [Program.seedMap,hf] using hA.query i (g ΞΎ) h
Β· intro ΞΎ h
simpa only [Program.seedMap,hf] using hA.output (g ΞΎ) h
theorem map (hA : A.Equivariant f Ο„) (g : Ξ© Γ— O β†’ R) (hg : Measurable g)
(Οƒ : R β†’ R) (he : βˆ€Ο‰ x,g (f Ο‰,Ο„ x)=Οƒ (g (Ο‰,x))) :
(A.map g hg).Equivariant f Οƒ := by
constructor
Β· exact hA.query
Β· intro Ο‰ h
simp only [Program.map,hA.output,he]
end Equivariant
lemma pure_equivariant (g : Ξ© β†’ O) (hg : Measurable g) (f : Ξ© β†’ Ξ©) (Ο„ : O β†’ O)
(he : βˆ€Ο‰,g (f Ο‰)=Ο„ (g Ο‰)) : (Program.pure (d:=d) g hg).Equivariant f Ο„ := by
constructor
Β· intro i
exact Fin.elim0 i
Β· intro Ο‰ h
exact he Ο‰
lemma oneQuery_equivariant (x : Ξ© β†’ Point d) (hx : Measurable x)
(g : Ξ© Γ— Reply d β†’ O) (hg : Measurable g) (f : Ξ© β†’ Ξ©) (Ο„ : O β†’ O)
(he : βˆ€Ο‰,x (f Ο‰)=x Ο‰) (hout : βˆ€Ο‰ a,g (f Ο‰,a)=Ο„ (g (Ο‰,a))) :
(Program.oneQuery x hx g hg).Equivariant f Ο„ := by
constructor
Β· intro i Ο‰ h
exact he Ο‰
Β· intro Ο‰ h
exact hout Ο‰ (h 0)
end LogConcaveSampling.OracleCompiler.Program
end
end
section
noncomputable section
namespace LogConcaveSampling.OracleCompiler.Expression
open MeasureTheory
variable {X : Type*} [MeasurableSpace X] {d : β„•}
def shiftSlots {n : β„•} (v : Fin n β†’ ℝ) (Ξ” : Point d) (g : Fin n β†’ Point d) : Fin n β†’ Point d :=
fun i => g i+v i β€’ Ξ”
def moveSeed {n : β„•} (f : X β†’ X) (v : Fin n β†’ ℝ) (Ξ” : Point d)
(z : X Γ— (Fin n β†’ Point d)) : X Γ— (Fin n β†’ Point d) := (f z.1,shiftSlots v Ξ” z.2)
lemma leftBlock_shift {c k t : β„•} (u : Fin c β†’ ℝ) (v : Fin k β†’ ℝ) (w : Fin t β†’ ℝ)
(Ξ” : Point d) (g : Fin (c+(k+t)) β†’ Point d) :
leftBlock (shiftSlots (Fin.append u (Fin.append v w)) Ξ” g)=shiftSlots u Ξ” (leftBlock g) := by
ext i
simp [leftBlock,shiftSlots]
lemma middleBlock_shift {c k t : β„•} (u : Fin c β†’ ℝ) (v : Fin k β†’ ℝ) (w : Fin t β†’ ℝ)
(Ξ” : Point d) (g : Fin (c+(k+t)) β†’ Point d) :
middleBlock (shiftSlots (Fin.append u (Fin.append v w)) Ξ” g)=shiftSlots v Ξ” (middleBlock g) := by
ext i
simp [middleBlock,shiftSlots]
lemma rightBlock_shift {c k t : β„•} (u : Fin c β†’ ℝ) (v : Fin k β†’ ℝ) (w : Fin t β†’ ℝ)
(Ξ” : Point d) (g : Fin (c+(k+t)) β†’ Point d) :
rightBlock (shiftSlots (Fin.append u (Fin.append v w)) Ξ” g)=shiftSlots w Ξ” (rightBlock g) := by
ext i
simp [rightBlock,shiftSlots]
lemma noise_equivariant (b : X β†’ Point d) (hb : Measurable b) (n c : ℝ)
(f : X β†’ X) (Ξ” : Point d) (he : βˆ€x,b (f x)=b x+c β€’ Ξ”) :
(noise b hb n).compile.Equivariant (moveSeed f (fun _ => 0) Ξ”) (fun y => y+c β€’ Ξ”) := by
apply Program.pure_equivariant
intro z
simp [moveSeed,shiftSlots,he]
abel
lemma add_equivariant (a : ℝ) (C T : Expression X d) {k q : β„•}
(M : Program (Point d Γ— (Fin k β†’ Point d)) d q (Point d))
(P : (Fin k β†’ Point d) β†’ (Fin k β†’ Point d)) (hP : Measurable P)
(f : X β†’ X) (Ξ” : Point d) (u : Fin C.slots β†’ ℝ) (v : Fin k β†’ ℝ)
(w : Fin T.slots β†’ ℝ) (c t : ℝ)
(hC : C.compile.Equivariant (moveSeed f u Ξ”) (fun y => y+c β€’ Ξ”))
(hM : (M.seedMap (fun z : Point d Γ— (Fin k β†’ Point d) => (z.1,P z.2))
(measurable_fst.prodMk (hP.comp measurable_snd))).Equivariant
(moveSeed (fun y => y+c β€’ Ξ”) v Ξ”) id)
(hT : T.compile.Equivariant (moveSeed f w Ξ”) (fun y => y+t β€’ Ξ”)) :
(add a C k q M P hP T).compile.Equivariant
(moveSeed f (Fin.append u (Fin.append v w)) Ξ”) (fun y => y+t β€’ Ξ”) := by
let Ξ© := X Γ— (Fin (C.slots+(k+T.slots)) β†’ Point d)
let F : Ξ© β†’ Ξ© := moveSeed f (Fin.append u (Fin.append v w)) Ξ”
let A : Program Ξ© d C.calls (Point d) := C.compile.seedMap
(fun z => (z.1,leftBlock z.2))
(measurable_fst.prodMk ((measurable_leftBlock _ _ _).comp measurable_snd))
have hA : A.Equivariant F (fun y => y+c β€’ Ξ”) := by
apply hC.seedMap
intro z
simp only [F,moveSeed,leftBlock_shift]
let B : Program (Ξ© Γ— Point d) d q (Point d) := M.seedMap
(fun z => (z.2,P (middleBlock z.1.2)))
(measurable_snd.prodMk (hP.comp ((measurable_middleBlock _ _ _).comp
(measurable_snd.comp measurable_fst))))
let G : (Ξ© Γ— Point d) β†’ (Ξ© Γ— Point d) := fun z => (F z.1,z.2+c β€’ Ξ”)
have hB : B.Equivariant G id := by
constructor
Β· intro i z h
have H := hM.query i (z.2,middleBlock z.1.2) h
simpa only [B,Program.seedMap,G,F,moveSeed,middleBlock_shift] using H
Β· intro z h
have H := hM.output (z.2,middleBlock z.1.2) h
simpa only [B,Program.seedMap,G,F,moveSeed,middleBlock_shift] using H
let D : Program ((Ξ© Γ— Point d) Γ— Point d) d T.calls (Point d) :=
(T.compile.seedMap (fun z => (z.1.1.1,rightBlock z.1.1.2))
((measurable_fst.comp (measurable_fst.comp measurable_fst)).prodMk
((measurable_rightBlock _ _ _).comp (measurable_snd.comp
(measurable_fst.comp measurable_fst))))).map
(fun z => a β€’ z.1.2+z.2) (by fun_prop)
have hD : D.Equivariant (fun z => (G z.1,id z.2)) (fun y => y+t β€’ Ξ”) := by
apply Program.Equivariant.map
Β· apply hT.seedMap
intro z
simp only [G,F,moveSeed,rightBlock_shift]
Β· intro z y
dsimp
abel
exact hA.seq (hB.seq hD)
end LogConcaveSampling.OracleCompiler.Expression
end
end
section
noncomputable section
namespace LogConcaveSampling.OracleCompiler.Expression
open MeasureTheory
open scoped NNReal
variable {X : Type*} [PseudoMetricSpace X] [MeasurableSpace X] {d : β„•}
def FixedSeedLip (C : Expression X d) (V : Point d β†’ ℝ) (K : ℝβ‰₯0) : Prop :=
βˆ€g,LipschitzWith K (fun x => C.eval V x g)
lemma noise_fixedSeed_lip (b : X β†’ Point d) (hb : Measurable b) (n : ℝ)
(V : Point d β†’ ℝ) {K : ℝβ‰₯0} (h : LipschitzWith K b) :
FixedSeedLip (noise b hb n) V K := by
intro g
apply LipschitzWith.of_dist_le_mul
intro x y
simpa only [eval,dist_add_right] using h.dist_le_mul x y
lemma add_fixedSeed_lip (a : ℝ) (C T : Expression X d) {k q : β„•}
(M : Program (Point d Γ— (Fin k β†’ Point d)) d q (Point d))
(P : (Fin k β†’ Point d) β†’ (Fin k β†’ Point d)) (hP : Measurable P)
(V : Point d β†’ ℝ) {K L J : ℝβ‰₯0}
(hC : FixedSeedLip C V K) (hT : FixedSeedLip T V J)
(hM : βˆ€g,LipschitzWith L (fun x => M.run V (x,g))) :
FixedSeedLip (add a C k q M P hP T) V (β€–aβ€–β‚Š*(L*K)+J) := by
intro g
have hN := (hM (P (middleBlock g))).comp (hC (leftBlock g))
have hS : LipschitzWith (β€–aβ€–β‚Š*(L*K)) (fun x =>
a β€’ M.run V (C.eval V x (leftBlock g),P (middleBlock g))) := by
apply LipschitzWith.of_dist_le_mul
intro x y
rw [dist_smulβ‚€]
exact (mul_le_mul_of_nonneg_left (hN.dist_le_mul x y) (norm_nonneg a)).trans_eq
(by simp only [NNReal.coe_mul,coe_nnnorm]; ring)
exact hS.add (hT (rightBlock g))
lemma FixedSeedLip.compiled {C : Expression X d} {V : Point d β†’ ℝ} {K : ℝβ‰₯0}
(h : FixedSeedLip C V K) (g : Fin C.slots β†’ Point d) :
LipschitzWith K (fun x => C.compile.run V (x,g)) := by
simpa only [compile_run] using h g
end LogConcaveSampling.OracleCompiler.Expression
namespace LogConcaveSampling.SeedCompiler
open OracleCompiler
open scoped NNReal
variable {d : β„•} {V : Point d β†’ ℝ} {K : ℝβ‰₯0}
lemma terminalMean_fixedSeed_lip (hV : LipschitzWith K (gradient V))
(r Οƒ : ℝ) (g : Fin 2 β†’ Point d) :
LipschitzWith K (fun x => (terminalMean d r Οƒ).run V (x,g)) := by
apply LipschitzWith.of_dist_le_mul
intro x y
simp only [terminalMean_run,dist_add_right]
simpa only [dist_add_right] using hV.dist_le_mul (x+r β€’ g 0) (y+r β€’ g 0)
lemma terminalSample_fixedSeed_lip (hV : LipschitzWith K (gradient V))
(r Ξ· : ℝ) (g : Fin 3 β†’ Point d) :
LipschitzWith (β€–rβ€–β‚Š*K) (fun x => (terminalSample d r Ξ·).run V (x,g)) := by
apply LipschitzWith.of_dist_le_mul
intro x y
simp only [terminalSample_run,dist_add_right,dist_sub_left,dist_smulβ‚€,
NNReal.coe_mul,coe_nnnorm]
have hh := hV.dist_le_mul (x+r β€’ g 0) (y+r β€’ g 0)
rw [dist_add_right] at hh
nlinarith [mul_le_mul_of_nonneg_left hh (norm_nonneg r)]
end LogConcaveSampling.SeedCompiler
end
end
section
noncomputable section
namespace LogConcaveSampling.OracleCompiler.Expression
open MeasureTheory
variable {E : Type*} [MeasurableSpace E]
lemma split_slots_preserving (ΞΌ : Measure E) [SigmaFinite ΞΌ] (c k : β„•) :
MeasurePreserving
(fun g : Fin (c+k) β†’ E =>
((fun i : Fin c => g (Fin.castAdd k i)),fun i : Fin k => g (Fin.natAdd c i)))
(Measure.pi fun _ => ΞΌ) ((Measure.pi fun _ : Fin c => ΞΌ).prod (Measure.pi fun _ : Fin k => ΞΌ)) := by
have H := (measurePreserving_sumPiEquivProdPi (fun _ : Fin c βŠ• Fin k => ΞΌ)).comp
(measurePreserving_piCongrLeft (fun _ : Fin (c+k) => ΞΌ) finSumFinEquiv).symm
convert H using 1
rfl
lemma three_blocks_preserving (ΞΌ : Measure E) [SigmaFinite ΞΌ] (c k t : β„•) :
MeasurePreserving
(fun g : Fin (c+(k+t)) β†’ E =>
((fun i : Fin c => g (Fin.castAdd (k+t) i)),
((fun i : Fin k => g (Fin.natAdd c (Fin.castAdd t i))),
fun i : Fin t => g (Fin.natAdd c (Fin.natAdd k i)))))
(Measure.pi fun _ => ΞΌ)
((Measure.pi fun _ : Fin c => ΞΌ).prod
((Measure.pi fun _ : Fin k => ΞΌ).prod (Measure.pi fun _ : Fin t => ΞΌ))) := by
exact ((MeasurePreserving.id (Measure.pi fun _ : Fin c => ΞΌ)).prod
(split_slots_preserving ΞΌ k t)).comp (split_slots_preserving ΞΌ c (k+t))
lemma three_blocks_with_anchor {A : Type*} [MeasurableSpace A] (Ξ½ : Measure A) [SFinite Ξ½]
(ΞΌ : Measure E) [SigmaFinite ΞΌ] (c k t : β„•) :
(Ξ½.prod (Measure.pi fun _ : Fin (c+(k+t)) => ΞΌ)).map
(fun z => (z.1,
((fun i : Fin c => z.2 (Fin.castAdd (k+t) i)),
((fun i : Fin k => z.2 (Fin.natAdd c (Fin.castAdd t i))),
fun i : Fin t => z.2 (Fin.natAdd c (Fin.natAdd k i))))))=
Ξ½.prod ((Measure.pi fun _ : Fin c => ΞΌ).prod
((Measure.pi fun _ : Fin k => ΞΌ).prod (Measure.pi fun _ : Fin t => ΞΌ))) := by
exact ((MeasurePreserving.id Ξ½).prod (three_blocks_preserving ΞΌ c k t)).map_eq
end LogConcaveSampling.OracleCompiler.Expression
end
end
end
end OAI