Download data/lean/OAI/Probability/Contingency/PreparedResidual.lean from introvoyz045/math: direct link, hf CLI and curl.
- Browser
- Download file 26.8 kB
-
https://huggingface.co/datasets/introvoyz045/math/resolve/main/data/lean/OAI/Probability/Contingency/PreparedResidual.lean
- Command line
-
hf download hf://datasets/introvoyz045/math/data/lean/OAI/Probability/Contingency/PreparedResidual.lean
-
curl -L -o PreparedResidual.lean https://huggingface.co/datasets/introvoyz045/math/resolve/main/data/lean/OAI/Probability/Contingency/PreparedResidual.lean
26.8 kB
| import Mathlib | |
| import OAI.Probability.Contingency.WeightedSampler | |
| namespace OAI | |
| /-! Prepared Residual. -/ | |
| noncomputable section | |
| open scoped BigOperators | |
| open Filter | |
| namespace Contingency.BitModel.StackCode.PaddedWeightedSampler | |
| noncomputable section | |
| def groupMass (N : β) (ws : List (List Bool)) (rest : List Bool) (j s : β) : β := | |
| ((Finset.univ.filter fun f : Fin sβBool => (run draw (encodeNat N++(ws.flatten++rest)) f).state=none β§ | |
| Contingency.IntegerDraw.scan (WeightedScan.indexedWeights ws 0) | |
| (BinarySubtract.value ((run draw (encodeNat N++(ws.flatten++rest)) f).memory.stack 5))=some j).card:β)/2^s | |
| theorem group_sum (N : β) (ws : List (List Bool)) (rest : List Bool) (j s : β) : | |
| groupMass N ws rest j s=β u : Fin (N+1), | |
| if Contingency.IntegerDraw.scan (WeightedScan.indexedWeights ws 0) u.val=some j then | |
| WeightedSampler.storedMass N (ws.flatten++rest) u s else 0 := by | |
| classical | |
| let input := encodeNat N++(ws.flatten++rest) | |
| have point (f : Fin sβBool) : | |
| (if (run draw input f).state=none β§ Contingency.IntegerDraw.scan (WeightedScan.indexedWeights ws 0) | |
| (BinarySubtract.value ((run draw input f).memory.stack 5))=some j then (1:β) else 0)= | |
| β u : Fin (N+1),if Contingency.IntegerDraw.scan (WeightedScan.indexedWeights ws 0) u.val=some j then | |
| (if (run draw input f).state=none β§ (run draw input f).memory.stack 5=(IntegerDraw.integerWord N u).reverse then (1:β) else 0) else 0 := by | |
| by_cases hh : (run draw input f).state=none | |
| Β· obtain β¨u,hu,h6,h7,hi,hoβ© := WeightedSampler.first_safe N (ws.flatten++rest) (List.ofFn f) hh | |
| change (run draw input f).memory.stack 5=(IntegerDraw.integerWord N u).reverse at hu | |
| have hv : BinarySubtract.value ((run draw input f).memory.stack 5)=u.val := by | |
| rw [hu,WeightedSampler.reverse_value,IntegerDraw.integerWord_value] | |
| rw [Finset.sum_eq_single u] | |
| Β· simp only [hh,true_and,hu,ite_true,WeightedSampler.reverse_value,IntegerDraw.integerWord_value] | |
| Β· intro v _ hne | |
| have hn : (run draw input f).memory.stack 5β (IntegerDraw.integerWord N v).reverse := by | |
| intro he | |
| have hvu : u.val=v.val := by | |
| have hh' := congrArg (fun bs => BinaryTrial.beValue bs.reverse) (hu.symm.trans he) | |
| simpa using hh' | |
| exact hne (Fin.ext hvu.symm) | |
| simp [hn] | |
| Β· simp | |
| Β· simp [hh] | |
| unfold groupMass | |
| simp only [Finset.card_filter,Nat.cast_sum,Nat.cast_ite,Nat.cast_one,Nat.cast_zero] | |
| change (β f : Fin sβBool,if (run draw input f).state=none β§ _ then (1:β) else 0)/2^s=_ | |
| rw [show (β f : Fin sβBool,if (run draw input f).state=none β§ Contingency.IntegerDraw.scan (WeightedScan.indexedWeights ws 0) (BinarySubtract.value ((run draw input f).memory.stack 5))=some j then (1:β) else 0)=_ from Finset.sum_congr rfl (fun f _ => point f)] | |
| rw [Finset.sum_comm,Finset.sum_div] | |
| apply Finset.sum_congr rfl | |
| intro u _ | |
| by_cases hs : Contingency.IntegerDraw.scan (WeightedScan.indexedWeights ws 0) u.val=some j | |
| Β· simp only [hs,ite_true,WeightedSampler.storedMass,Finset.card_filter,Nat.cast_sum,Nat.cast_ite,Nat.cast_one,Nat.cast_zero] | |
| rfl | |
| Β· simp [hs] | |
| theorem group_limit (N : β) (ws : List (List Bool)) (rest : List Bool) (j : β) | |
| (htotal : WeightedScan.weightTotal ws=N+1) : | |
| Tendsto (groupMass N ws rest j) atTop | |
| (nhds ((Contingency.IntegerDraw.weight (WeightedScan.indexedWeights ws 0) j:β)/(N+1:β))) := by | |
| classical | |
| have hc : (β u : Fin (N+1),if Contingency.IntegerDraw.scan (WeightedScan.indexedWeights ws 0) u.val=some j then (1:β) else 0)= | |
| Contingency.IntegerDraw.weight (WeightedScan.indexedWeights ws 0) j := by | |
| change (β u : Fin (N+1),(fun x : β => if Contingency.IntegerDraw.scan (WeightedScan.indexedWeights ws 0) x=some j then (1:β) else 0) u.val)=_ | |
| rw [Fin.sum_univ_eq_sum_range (fun x => if Contingency.IntegerDraw.scan (WeightedScan.indexedWeights ws 0) x=some j then (1:β) else 0) (N+1)] | |
| have ht : Contingency.IntegerDraw.total (WeightedScan.indexedWeights ws 0)=N+1 := | |
| (WeightedScan.indexed_total ws 0).trans htotal | |
| rw [β ht] | |
| exact Contingency.IntegerDraw.scan_count _ _ | |
| have hcr : (β u : Fin (N+1),if Contingency.IntegerDraw.scan (WeightedScan.indexedWeights ws 0) u.val=some j then (1:β) else 0)= | |
| (Contingency.IntegerDraw.weight (WeightedScan.indexedWeights ws 0) j:β) := by exact_mod_cast hc | |
| have he : (β u : Fin (N+1),if Contingency.IntegerDraw.scan (WeightedScan.indexedWeights ws 0) u.val=some j then | |
| (1/(N+1:β)) else 0)=(Contingency.IntegerDraw.weight (WeightedScan.indexedWeights ws 0) j:β)/(N+1:β) := by | |
| rw [β hcr,Finset.sum_div] | |
| apply Finset.sum_congr rfl | |
| intro u _ | |
| split_ifs <;> simp | |
| rw [β he] | |
| have hh := tendsto_finsetSum (Finset.univ : Finset (Fin (N+1))) (fun u _ => | |
| show Tendsto (fun s => if Contingency.IntegerDraw.scan (WeightedScan.indexedWeights ws 0) u.val=some j then | |
| WeightedSampler.storedMass N (ws.flatten++rest) u s else 0) atTop | |
| (nhds (if Contingency.IntegerDraw.scan (WeightedScan.indexedWeights ws 0) u.val=some j then 1/(N+1:β) else 0)) from by | |
| split_ifs | |
| Β· exact WeightedSampler.stored_uniform N (ws.flatten++rest) u | |
| Β· exact tendsto_const_nhds) | |
| have heq : groupMass N ws rest j=(fun s => β u : Fin (N+1),if Contingency.IntegerDraw.scan (WeightedScan.indexedWeights ws 0) u.val=some j then WeightedSampler.storedMass N (ws.flatten++rest) u s else 0) := by | |
| funext s | |
| exact group_sum N ws rest j s | |
| rw [heq] | |
| exact hh | |
| end | |
| end Contingency.BitModel.StackCode.PaddedWeightedSampler | |
| open scoped BigOperators | |
| open Filter | |
| namespace Contingency.BitModel.StackCode.PaddedWeightedSampler | |
| noncomputable section | |
| theorem mass_upper (N : β) (ws : List (List Bool)) (rest : List Bool) (j s : β) | |
| (hlen : β wβws,w.length=N.size+1) (htotal : WeightedScan.weightTotal ws=N+1) : | |
| mass program (encodeNat N++(ws.flatten++rest)) (List.replicate j false) s β€ groupMass N ws rest j s := by | |
| classical | |
| unfold mass groupMass | |
| apply div_le_div_of_nonneg_right _ (by positivity) | |
| exact_mod_cast Finset.card_le_card (show | |
| (Finset.univ.filter fun f : Fin sβBool => (run program (encodeNat N++(ws.flatten++rest)) f).state=none β§ | |
| (run program (encodeNat N++(ws.flatten++rest)) f).memory.outputRev.reverse=List.replicate j false) β | |
| (Finset.univ.filter fun f : Fin sβBool => (run draw (encodeNat N++(ws.flatten++rest)) f).state=none β§ | |
| Contingency.IntegerDraw.scan (WeightedScan.indexedWeights ws 0) | |
| (BinarySubtract.value ((run draw (encodeNat N++(ws.flatten++rest)) f).memory.stack 5))=some j) from by | |
| intro f hf | |
| simp only [Finset.mem_filter,Finset.mem_univ,true_and] at hf β’ | |
| obtain β¨k,u,hk,hscan,hu,hoβ© := halted_output N ws rest (List.ofFn f) hlen htotal hf.1 | |
| change (run program (encodeNat N++(ws.flatten++rest)) f).memory.outputRev=List.replicate k false at ho | |
| have hkj : k=j := by | |
| have he := congrArg List.length hf.2 | |
| simpa only [List.length_reverse,ho,List.length_replicate] using he | |
| subst k | |
| refine β¨first_returned (List.ofFn f) _ hf.1,?_β© | |
| change (run draw (encodeNat N++(ws.flatten++rest)) f).memory.stack 5=(IntegerDraw.integerWord N u).reverse at hu | |
| rw [hu,WeightedSampler.reverse_value,IntegerDraw.integerWord_value] | |
| exact hscan) | |
| theorem mass_lower (N : β) (ws : List (List Bool)) (rest : List Bool) (j s : β) | |
| (hlen : β wβws,w.length=N.size+1) (htotal : WeightedScan.weightTotal ws=N+1) : | |
| groupMass N ws rest j s β€ mass program (encodeNat N++(ws.flatten++rest)) (List.replicate j false) (s+delay N ws) := by | |
| classical | |
| let e : Fin sβFin (s+delay N ws) := Fin.castAdd (delay N ws) | |
| have hi : Function.Injective e := by | |
| intro i j h; exact Fin.ext (congrArg (fun k : Fin (s+delay N ws) => k.val) h) | |
| let p : (Fin sβBool)βProp := fun f => (run draw (encodeNat N++(ws.flatten++rest)) f).state=none β§ | |
| Contingency.IntegerDraw.scan (WeightedScan.indexedWeights ws 0) | |
| (BinarySubtract.value ((run draw (encodeNat N++(ws.flatten++rest)) f).memory.stack 5))=some j | |
| have he := CoordinateLaw.probability_restrict e hi p | |
| simp only [Fintype.card_fin] at he | |
| unfold groupMass mass | |
| change ((Finset.univ.filter p).card:β)/2^sβ€_ | |
| rw [β he] | |
| apply div_le_div_of_nonneg_right _ (by positivity) | |
| exact_mod_cast Finset.card_le_card (show | |
| (Finset.univ.filter fun f : Fin (s+delay N ws)βBool => p (fβe)) β | |
| (Finset.univ.filter fun f : Fin (s+delay N ws)βBool => (run program (encodeNat N++(ws.flatten++rest)) f).state=none β§ | |
| (run program (encodeNat N++(ws.flatten++rest)) f).memory.outputRev.reverse=List.replicate j false) from by | |
| intro f hf | |
| simp only [Finset.mem_filter,Finset.mem_univ,true_and] at hf β’ | |
| change (run draw (encodeNat N++(ws.flatten++rest)) (fβe)).state=none β§ _ at hf | |
| obtain β¨u,hu,h6,h7,hin,houtβ© := WeightedSampler.first_safe N (ws.flatten++rest) (List.ofFn (fβe)) hf.1 | |
| change (run draw (encodeNat N++(ws.flatten++rest)) (fβe)).memory.stack 5=(IntegerDraw.integerWord N u).reverse at hu | |
| have hs : Contingency.IntegerDraw.scan (WeightedScan.indexedWeights ws 0) u.val=some j := by | |
| have hg := hf.2 | |
| rw [hu,WeightedSampler.reverse_value,IntegerDraw.integerWord_value] at hg | |
| exact hg | |
| have ht : (List.ofFn f).take s=List.ofFn (fβe) := by | |
| rw [List.ofFn_add] | |
| simp only [List.take_append,List.length_ofFn,Nat.sub_self,List.take_zero,List.append_nil] | |
| rw [List.take_of_length_le (by simp)] | |
| congr 2 | |
| have hc := completes N ws rest (List.ofFn f) s j u hlen htotal | |
| ((List.length_ofFn (f:=f)).symm.le) | |
| (by rw [ht]; exact hf.1) (by rw [ht]; exact hu) hs | |
| refine β¨hc.1,?_β© | |
| have ho : (run program (encodeNat N++(ws.flatten++rest)) f).memory.outputRev=List.replicate j false := hc.2 | |
| rw [ho,List.reverse_replicate]) | |
| theorem stack_law (N : β) (ws : List (List Bool)) (rest : List Bool) (j : β) | |
| (hlen : β wβws,w.length=N.size+1) (htotal : WeightedScan.weightTotal ws=N+1) : | |
| Tendsto (mass program (encodeNat N++(ws.flatten++rest)) (List.replicate j false)) atTop | |
| (nhds ((Contingency.IntegerDraw.weight (WeightedScan.indexedWeights ws 0) j:β)/(N+1:β))) := by | |
| obtain β¨r,hrβ© := mass_limit_exists program (encodeNat N++(ws.flatten++rest)) (List.replicate j false) | |
| have hg := group_limit N ws rest j htotal | |
| have hu : rβ€(Contingency.IntegerDraw.weight (WeightedScan.indexedWeights ws 0) j:β)/(N+1:β) := | |
| le_of_tendsto_of_tendsto' hr hg (fun s => mass_upper N ws rest j s hlen htotal) | |
| have hl : (Contingency.IntegerDraw.weight (WeightedScan.indexedWeights ws 0) j:β)/(N+1:β)β€r := | |
| le_of_tendsto_of_tendsto' hg (hr.comp (tendsto_add_atTop_nat (delay N ws))) (fun s => mass_lower N ws rest j s hlen htotal) | |
| simpa only [le_antisymm hu hl] using hr | |
| theorem cached_selector (N : β) (ws : List (List Bool)) (rest : List Bool) | |
| (hlen : β wβws,w.length=N.size+1) (htotal : WeightedScan.weightTotal ws=N+1) : | |
| (β (t : β) (f : Fin tβBool),(machine.run (encodeNat N++(ws.flatten++rest)) f).state=none β | |
| β j<ws.length,(machine.run (encodeNat N++(ws.flatten++rest)) f).outputRev.reverse=List.replicate j false) β§ | |
| (β j,Tendsto (machine.mass (encodeNat N++(ws.flatten++rest)) (List.replicate j false)) atTop | |
| (nhds ((Contingency.IntegerDraw.weight (WeightedScan.indexedWeights ws 0) j:β)/(N+1:β)))) β§ | |
| Summable (machine.tail (encodeNat N++(ws.flatten++rest))) β§ | |
| Tendsto (machine.tail (encodeNat N++(ws.flatten++rest))) atTop (nhds 0) β§ | |
| (β' s,machine.tail (encodeNat N++(ws.flatten++rest)) s)β€70*(N.size:β)+42+2*delay N ws := by | |
| refine β¨?_,?_,charged_time N ws rest hlen htotalβ© | |
| Β· apply safe_compile program _ (fun out => β j<ws.length,out=List.replicate j false) | |
| intro t f hh | |
| obtain β¨j,u,hj,hs,hu,hoβ© := halted_output N ws rest (List.ofFn f) hlen htotal hh | |
| refine β¨j,hj,?_β© | |
| change (run program (encodeNat N++(ws.flatten++rest)) f).memory.outputRev=List.replicate j false at ho | |
| rw [ho,List.reverse_replicate] | |
| Β· intro j | |
| exact mass_limit_compile program _ _ _ (stack_law N ws rest j hlen htotal) | |
| end | |
| end Contingency.BitModel.StackCode.PaddedWeightedSampler | |
| open scoped BigOperators | |
| open Filter | |
| namespace Contingency.BitModel.StackCode.PaddedWeightedSampler | |
| noncomputable section | |
| def cacheMaximum (weights : List β) : β := weights.sum-1 | |
| def cacheWords (weights : List β) : List (List Bool) := | |
| weights.map (BinarySubtract.bits ((cacheMaximum weights).size+1)) | |
| def cacheInput (weights : List β) (rest : List Bool) : List Bool := | |
| encodeNat (cacheMaximum weights)++((cacheWords weights).flatten++rest) | |
| def integerEntries : List ββββList (βΓβ) | |
| | [],_ => [] | |
| | w::ws,i => (i,w)::integerEntries ws (i+1) | |
| theorem member_le_sum (weights : List β) (x : β) (hx : xβweights) : xβ€weights.sum := by | |
| induction weights with | |
| | nil => simp at hx | |
| | cons a as ih => | |
| simp only [List.mem_cons] at hx | |
| simp only [List.sum_cons] | |
| rcases hx with rfl|hx | |
| Β· omega | |
| Β· have hh := ih hx; omega | |
| theorem cache_width (weights : List β) (hp : 0<weights.sum) : | |
| β wβcacheWords weights,w.length=(cacheMaximum weights).size+1 := by | |
| intro w hw | |
| obtain β¨x,hx,rflβ© := List.mem_map.mp hw | |
| apply BinarySubtract.bits_length | |
| have hmax : cacheMaximum weights+1=weights.sum := by dsimp [cacheMaximum]; omega | |
| have hb : cacheMaximum weights<2^(cacheMaximum weights).size := Nat.size_le.mp (le_refl _) | |
| have hxsum := member_le_sum weights x hx | |
| rw [pow_succ] | |
| omega | |
| theorem cache_total (weights : List β) (hp : 0<weights.sum) : | |
| WeightedScan.weightTotal (cacheWords weights)=cacheMaximum weights+1 := by | |
| have hmax : cacheMaximum weights+1=weights.sum := by dsimp [cacheMaximum]; omega | |
| rw [hmax] | |
| unfold WeightedScan.weightTotal cacheWords | |
| simp only [List.map_map,Function.comp_def,BinarySubtract.bits_value] | |
| have he : (weights.map fun x => x)=weights := by induction weights <;> simp_all | |
| rw [he] | |
| theorem cache_entries (weights : List β) (i : β) : | |
| WeightedScan.indexedWeights (cacheWords weights) i=integerEntries weights i := by | |
| have hh (n : β) (as : List β) (j : β) : | |
| WeightedScan.indexedWeights (as.map (BinarySubtract.bits n)) j=integerEntries as j := by | |
| induction as generalizing j with | |
| | nil => rfl | |
| | cons a as ih => simp [WeightedScan.indexedWeights,integerEntries,ih] | |
| exact hh _ _ _ | |
| theorem integer_cache_sampler (weights : List β) (rest : List Bool) (hp : 0<weights.sum) : | |
| (β (t : β) (f : Fin tβBool),(machine.run (cacheInput weights rest) f).state=none β | |
| β j<weights.length,(machine.run (cacheInput weights rest) f).outputRev.reverse=List.replicate j false) β§ | |
| (β j,Tendsto (machine.mass (cacheInput weights rest) (List.replicate j false)) atTop | |
| (nhds ((Contingency.IntegerDraw.weight (integerEntries weights 0) j:β)/(weights.sum:β)))) β§ | |
| Summable (machine.tail (cacheInput weights rest)) β§ | |
| Tendsto (machine.tail (cacheInput weights rest)) atTop (nhds 0) β§ | |
| (β' s,machine.tail (cacheInput weights rest) s)β€ | |
| 78*((cacheMaximum weights).size:β)+58+10*((cacheMaximum weights).size+2)*weights.length := by | |
| have hc := cached_selector (cacheMaximum weights) (cacheWords weights) rest | |
| (cache_width weights hp) (cache_total weights hp) | |
| have hmax : cacheMaximum weights+1=weights.sum := by dsimp [cacheMaximum]; omega | |
| have hlen : (cacheWords weights).length=weights.length := by simp [cacheWords] | |
| have hreal : (cacheMaximum weights:β)+1=(weights.sum:β) := by exact_mod_cast hmax | |
| refine β¨?_,?_,hc.2.2.1,hc.2.2.2.1,?_β© | |
| Β· simpa only [cacheInput,hlen] using hc.1 | |
| Β· simpa only [cacheInput,cache_entries,hreal] using hc.2.1 | |
| Β· have ht := hc.2.2.2.2 | |
| dsimp only [delay] at ht | |
| rw [hlen] at ht | |
| push_cast at ht | |
| dsimp only [cacheInput] | |
| nlinarith | |
| end | |
| end Contingency.BitModel.StackCode.PaddedWeightedSampler | |
| open scoped BigOperators | |
| open Filter | |
| namespace Contingency.BitModel.StackCode.PaddedWeightedSampler | |
| noncomputable section | |
| theorem entries_below (ws : List β) (i j : β) (hj : j < i) : | |
| Contingency.IntegerDraw.weight (integerEntries ws i) j=0 := by | |
| induction ws generalizing i with | |
| | nil => rfl | |
| | cons a as ih => | |
| have hn : iβ j := by omega | |
| simp [integerEntries,Contingency.IntegerDraw.weight,hn] | |
| exact ih (i+1) (by omega) | |
| theorem entries_lookup (ws : List β) (i j : β) : | |
| Contingency.IntegerDraw.weight (integerEntries ws i) (i+j)=ws.getD j 0 := by | |
| induction ws generalizing i j with | |
| | nil => simp [integerEntries,Contingency.IntegerDraw.weight] | |
| | cons a as ih => | |
| cases j with | |
| | zero => | |
| have hz := entries_below as (i+1) i (by omega) | |
| simp only [Nat.add_zero,integerEntries,Contingency.IntegerDraw.weight,List.map_cons,List.sum_cons,ite_true] | |
| change a+Contingency.IntegerDraw.weight (integerEntries as (i+1)) i=_ | |
| rw [hz] | |
| simp | |
| | succ j => | |
| have hn : iβ i+(j+1) := by omega | |
| simp only [integerEntries,Contingency.IntegerDraw.weight,List.map_cons,List.sum_cons,hn,ite_false,Nat.zero_add] | |
| change Contingency.IntegerDraw.weight (integerEntries as (i+1)) (i+(j+1))=_ | |
| rw [show i+(j+1)=(i+1)+j by omega,ih] | |
| simp | |
| theorem finite_weights_law {M : β} (w : Fin Mββ) (hw : 0<β j,w j) (rest : List Bool) (j : Fin M) : | |
| Tendsto (machine.mass (cacheInput (List.ofFn w) rest) (List.replicate j.val false)) atTop | |
| (nhds ((w j:β)/(β a,w a:β))) := by | |
| have hp : 0<(List.ofFn w).sum := by simpa only [List.sum_ofFn] using hw | |
| have h := (integer_cache_sampler (List.ofFn w) rest hp).2.1 j.val | |
| have hl := entries_lookup (List.ofFn w) 0 j.val | |
| simp only [Nat.zero_add] at hl | |
| rw [hl,List.getD_eq_getElem _ _ (by simp)] at h | |
| simpa only [List.getElem_ofFn,List.sum_ofFn] using h | |
| end | |
| end Contingency.BitModel.StackCode.PaddedWeightedSampler | |
| namespace Contingency | |
| open BitModel.StackCode.PaddedWeightedSampler | |
| noncomputable section | |
| variable {m n : β} (r : Fin mββ) (c : Fin nββ) | |
| local instance preparedResidualVectorTablesFintype : Fintype (Tables r c) := Fintype.ofFinite _ | |
| variable (hrc : (β i,r i)=β j,c j) | |
| def residualVector : Fin (Fintype.card (Tables r c))ββ := | |
| fun j => arbitraryResidual r c hrc ((Fintype.equivFin (Tables r c)).symm j) | |
| theorem residualVector_sum : (β j,residualVector r c hrc j)=β X,arbitraryResidual r c hrc X := by | |
| unfold residualVector | |
| exact Equiv.sum_comp (Fintype.equivFin (Tables r c)).symm _ | |
| theorem residual_cache_law (rest : List Bool) (X : Tables r c) : | |
| Tendsto (machine.mass (cacheInput (List.ofFn (residualVector r c hrc)) rest) | |
| (List.replicate ((Fintype.equivFin (Tables r c)) X).val false)) atTop | |
| (nhds ((arbitraryResidual r c hrc X:β)/(Fintype.card (Tables r c)*2^(arbitraryDraw r c hrc (2*Parameters.D m n (β i,r i))).bits:β))) := by | |
| have hp : 0<β j,residualVector r c hrc j := by | |
| rw [residualVector_sum] | |
| exact arbitraryResidual_total_pos r c hrc | |
| have hh := BitModel.StackCode.PaddedWeightedSampler.finite_weights_law (residualVector r c hrc) hp rest | |
| ((Fintype.equivFin (Tables r c)) X) | |
| have hw : residualVector r c hrc ((Fintype.equivFin (Tables r c)) X)=arbitraryResidual r c hrc X := by | |
| simp [residualVector] | |
| rw [hw,residualVector_sum,(arbitraryResidual_data r c hrc).2] at hh | |
| simpa only [Nat.cast_mul,Nat.cast_pow,Nat.cast_ofNat] using hh | |
| end | |
| end Contingency | |
| open scoped BigOperators | |
| open Filter | |
| namespace Contingency.BitModel.StackCode.PaddedWeightedSampler | |
| noncomputable section | |
| theorem cache_maximum_size_le (weights : List β) (D R : β) | |
| (hp : 0 < (weights).sum) (hM : (weights).length β€ 2 ^ D) | |
| (hs : (weights).sum = (weights).length * 2 ^ R) : | |
| (cacheMaximum weights).size β€ D + R := by | |
| apply Nat.size_le.mpr | |
| have hsum : (weights).sum β€ 2 ^ (D + R) := by | |
| rw [hs, pow_add] | |
| exact Nat.mul_le_mul_right _ hM | |
| dsimp only [cacheMaximum] | |
| exact (Nat.sub_lt hp (by omega)).trans_le hsum | |
| theorem integer_cache_rare_time (weights : List β) (rest : List Bool) (D R : β) | |
| (hp : 0 < (weights).sum) (hM : (weights).length β€ 2 ^ D) | |
| (hs : (weights).sum = (weights).length * 2 ^ R) : | |
| (1 / (2 : β) ^ D) * (β' t, machine.tail (cacheInput weights rest) t) β€ | |
| (100 * (D + R + 1) : β) := by | |
| have ht := (integer_cache_sampler weights rest hp).2.2.2.2 | |
| have hsize := cache_maximum_size_le weights D R hp hM hs | |
| have hΞ΄0 : (0 : β) β€ 1 / 2 ^ D := by positivity | |
| have hΞ΄1 : (1 : β) / 2 ^ D β€ 1 := | |
| (div_le_one (by positivity)).mpr (one_le_powβ (by norm_num)) | |
| have hΞ΄M : (1 / (2 : β) ^ D) * (weights).length β€ 1 := by | |
| calc | |
| _ = ((weights).length : β) / 2 ^ D := by ring | |
| _ β€ 1 := (div_le_one (by positivity)).mpr (by exact_mod_cast hM) | |
| have hfirst := mul_le_mul_of_nonneg_right hΞ΄1 | |
| (show (0 : β) β€ 78 * (cacheMaximum weights).size + 58 by positivity) | |
| have hsecond := mul_le_mul_of_nonneg_left hΞ΄M | |
| (show (0 : β) β€ 10 * ((cacheMaximum weights).size + 2) by positivity) | |
| have hsizeR : ((cacheMaximum weights).size : β) β€ (D + R : β) := by | |
| exact_mod_cast hsize | |
| have hsum0 : (0 : β) β€ (D + R : β) := Nat.cast_nonneg _ | |
| calc | |
| _ β€ (1 / (2 : β) ^ D) * | |
| (78 * (cacheMaximum weights).size + 58 + | |
| 10 * ((cacheMaximum weights).size + 2) * (weights).length) := | |
| mul_le_mul_of_nonneg_left ht hΞ΄0 | |
| _ = (1 / (2 : β) ^ D) * (78 * (cacheMaximum weights).size + 58) + | |
| 10 * ((cacheMaximum weights).size + 2) * | |
| ((1 / (2 : β) ^ D) * (weights).length) := by ring | |
| _ β€ (78 * (cacheMaximum weights).size + 58) + | |
| 10 * ((cacheMaximum weights).size + 2) := by | |
| simpa only [one_mul, mul_one] using add_le_add hfirst hsecond | |
| _ β€ (100 * (D + R + 1) : β) := by | |
| simp only [Nat.cast_mul, Nat.cast_ofNat, Nat.cast_add, Nat.cast_one] at * | |
| nlinarith only [hsizeR, hsum0] | |
| end | |
| end Contingency.BitModel.StackCode.PaddedWeightedSampler | |
| namespace Contingency | |
| open BitModel.StackCode.PaddedWeightedSampler | |
| noncomputable section | |
| def residualSelectionBudget (s : β) : β := | |
| 100 * (26 ^ 2 * (s + 3) ^ 16 + | |
| (32 * (s + 2 * (26 ^ 2 * (s + 3) ^ 16) + 3) ^ 2) ^ 300 + 1) | |
| variable {m n : β} (r : Fin m β β) (c : Fin n β β) | |
| local instance preparedResidualBudgetTablesFintype : Fintype (Tables r c) := Fintype.ofFinite _ | |
| variable (hrc : (β i, r i) = β j, c j) | |
| local notation "D" => Parameters.D m n (β i, r i) | |
| local notation "p" => arbitraryDraw r c hrc (2 * D) | |
| local notation "weights" => List.ofFn (residualVector r c hrc) | |
| theorem residual_cache_rare_time (rest : List Bool) : | |
| (1 / (2 : β) ^ D) * (β' t, machine.tail (cacheInput weights rest) t) β€ | |
| (residualSelectionBudget (inputSize m n (β i, r i)) : β) := by | |
| have hp : 0 < (weights).sum := by | |
| rw [List.sum_ofFn, residualVector_sum] | |
| exact arbitraryResidual_total_pos r c hrc | |
| have hM : (weights).length β€ 2 ^ D := by | |
| simpa only [List.length_ofFn, Nat.card_eq_fintype_card] using | |
| Parameters.table_card_D_all r c hrc | |
| have hs : (weights).sum = (weights).length * 2 ^ (p).bits := by | |
| rw [List.sum_ofFn, residualVector_sum, (arbitraryResidual_data r c hrc).2, | |
| List.length_ofFn] | |
| have ht := integer_cache_rare_time weights rest D (p).bits hp hM hs | |
| have hD := Parameters.D_le_input m n (β i, r i) | |
| have hk : 1 β€ 2 * D := by | |
| have := Parameters.D_positive m n (β i, r i) | |
| omega | |
| have hR := arbitraryDraw_bits_input r c hrc (2 * D) hk | |
| have hbase : inputSize m n (β i, r i) + 2 * D + 3 β€ | |
| inputSize m n (β i, r i) + | |
| 2 * (26 ^ 2 * (inputSize m n (β i, r i) + 3) ^ 16) + 3 := by omega | |
| have hR' : (p).bits β€ | |
| (32 * (inputSize m n (β i, r i) + | |
| 2 * (26 ^ 2 * (inputSize m n (β i, r i) + 3) ^ 16) + 3) ^ 2) ^ 300 := by | |
| calc | |
| _ β€ (32 * (inputSize m n (β i, r i) + 2 * D + 3) ^ 2) ^ 300 := hR | |
| _ β€ _ := by gcongr | |
| refine ht.trans ?_ | |
| apply Nat.cast_le.mpr | |
| unfold residualSelectionBudget | |
| apply Nat.mul_le_mul_left | |
| omega | |
| theorem residual_cache_corrected_limit (rest : List Bool) (X : Tables r c) : | |
| Tendsto | |
| (fun t => (1 - 1 / (2 : β) ^ D) * (p).toLaw.mass X + | |
| (1 / (2 : β) ^ D) * | |
| machine.mass (cacheInput weights rest) | |
| (List.replicate ((Fintype.equivFin (Tables r c)) X).val false) t) | |
| atTop (nhds (1 / (Nat.card (Tables r c) : β))) := by | |
| classical | |
| let := tables_nonempty r c hrc | |
| have hM : Fintype.card (Tables r c) β€ 2 ^ D := by | |
| simpa only [Nat.card_eq_fintype_card] using Parameters.table_card_D_all r c hrc | |
| have hk : 1 β€ 2 * D := by | |
| have := Parameters.D_positive m n (β i, r i) | |
| omega | |
| have he := corrected_exact p D hM (arbitraryDraw_tv r c hrc (2 * D) hk) X | |
| change (1 - 1 / (2 : β) ^ D) * (p).toLaw.mass X + | |
| (1 / (2 : β) ^ D) * ((arbitraryResidual r c hrc X : β) / | |
| (Fintype.card (Tables r c) * 2 ^ (p).bits : β)) = | |
| 1 / (Fintype.card (Tables r c) : β) at he | |
| have hl := ((residual_cache_law r c hrc rest X).const_mul ((1 : β) / 2 ^ D)).const_add | |
| ((1 - 1 / (2 : β) ^ D) * (p).toLaw.mass X) | |
| simpa only [he, Nat.card_eq_fintype_card] using hl | |
| theorem prepared_residual_stage (rest : List Bool) : | |
| (β (t : β) (coins : Fin t β Bool), | |
| (machine.run (cacheInput weights rest) coins).state = none β | |
| β j < Fintype.card (Tables r c), | |
| (machine.run (cacheInput weights rest) coins).outputRev.reverse = | |
| List.replicate j false) β§ | |
| Summable (machine.tail (cacheInput weights rest)) β§ | |
| Tendsto (machine.tail (cacheInput weights rest)) atTop (nhds 0) β§ | |
| (1 / (2 : β) ^ D) * (β' t, machine.tail (cacheInput weights rest) t) β€ | |
| (residualSelectionBudget (inputSize m n (β i, r i)) : β) β§ | |
| (β X : Tables r c, | |
| Tendsto | |
| (fun t => (1 - 1 / (2 : β) ^ D) * (p).toLaw.mass X + | |
| (1 / (2 : β) ^ D) * | |
| machine.mass (cacheInput weights rest) | |
| (List.replicate ((Fintype.equivFin (Tables r c)) X).val false) t) | |
| atTop (nhds (1 / (Nat.card (Tables r c) : β)))) := by | |
| have hp : 0 < (weights).sum := by | |
| rw [List.sum_ofFn, residualVector_sum] | |
| exact arbitraryResidual_total_pos r c hrc | |
| have hc := integer_cache_sampler weights rest hp | |
| exact β¨by simpa only [List.length_ofFn] using hc.1, hc.2.2.1, hc.2.2.2.1, | |
| residual_cache_rare_time r c hrc rest, residual_cache_corrected_limit r c hrc restβ© | |
| end | |
| end Contingency | |
| end | |
| end OAI | |