Download data/lean/OAI/Computability/MinUncut/Games/Basic.lean from introvoyz045/math: direct link, hf CLI and curl.
- Browser
- Download file 29.5 kB
-
https://huggingface.co/datasets/introvoyz045/math/resolve/main/data/lean/OAI/Computability/MinUncut/Games/Basic.lean
- Command line
-
hf download hf://datasets/introvoyz045/math/data/lean/OAI/Computability/MinUncut/Games/Basic.lean
-
curl -L -o Basic.lean https://huggingface.co/datasets/introvoyz045/math/resolve/main/data/lean/OAI/Computability/MinUncut/Games/Basic.lean
29.5 kB
| import Mathlib | |
| import OAI.Computability.MinUncut.Games.OuterQuestions | |
| namespace OAI | |
| namespace MinUncutGames.Foundations.CorrelatedSampling | |
| def firstAccepted {Ο : Type*} (accept : Ο β Bool) : List Ο β Option Ο | |
| | [] => none | |
| | proposal :: rest => | |
| if accept proposal then some proposal else firstAccepted accept rest | |
| theorem first_union_common {Ο : Type*} (left right : Ο β Bool) | |
| (proposals : List Ο) (proposal : Ο) | |
| (hfirst : firstAccepted (fun s => left s || right s) proposals = some proposal) | |
| (hleft : left proposal = true) (hright : right proposal = true) : | |
| firstAccepted left proposals = some proposal β§ | |
| firstAccepted right proposals = some proposal := by | |
| induction proposals with | |
| | nil => simp [firstAccepted] at hfirst | |
| | cons head tail ih => | |
| by_cases hl : left head = true | |
| Β· have heq : head = proposal := by | |
| simpa [firstAccepted, hl] using hfirst | |
| subst head | |
| simp [firstAccepted, hleft, hright] | |
| Β· by_cases hr : right head = true | |
| Β· have heq : head = proposal := by | |
| simpa [firstAccepted, hl, hr] using hfirst | |
| subst head | |
| exact False.elim (hl hleft) | |
| Β· have htail : firstAccepted (fun s => left s || right s) tail = some proposal := by | |
| simpa [firstAccepted, hl, hr] using hfirst | |
| simpa [firstAccepted, hl, hr] using ih htail | |
| def thresholdSample {Ξ± : Type*} (mass : Ξ± β Nat) (proposals : List (Ξ± Γ Nat)) : | |
| Option Ξ± := | |
| (firstAccepted (fun s => decide (s.2 < mass s.1)) proposals).map Prod.fst | |
| theorem thresholdSample_agreement {Ξ± : Type*} (left right : Ξ± β Nat) | |
| (proposals : List (Ξ± Γ Nat)) (a : Ξ±) (height : Nat) | |
| (hfirst : firstAccepted | |
| (fun s => decide (s.2 < left s.1) || decide (s.2 < right s.1)) proposals = | |
| some (a, height)) | |
| (hcommon : height < min (left a) (right a)) : | |
| thresholdSample left proposals = some a β§ | |
| thresholdSample right proposals = some a := by | |
| have h := first_union_common | |
| (fun s : Ξ± Γ Nat => decide (s.2 < left s.1)) | |
| (fun s : Ξ± Γ Nat => decide (s.2 < right s.1)) proposals (a, height) hfirst | |
| (by simpa using (lt_min_iff.mp hcommon).1) | |
| (by simpa using (lt_min_iff.mp hcommon).2) | |
| simp [thresholdSample, h.1, h.2] | |
| noncomputable section | |
| variable {Ξ± : Type*} [Fintype Ξ±] | |
| def totalVariation (p q : Ξ± β β) : β := (β a, |p a - q a|) / 2 | |
| def overlapMass (p q : Ξ± β β) : β := β a, min (p a) (q a) | |
| def unionMass (p q : Ξ± β β) : β := β a, max (p a) (q a) | |
| theorem totalVariation_nonneg (p q : Ξ± β β) : 0 β€ totalVariation p q := by | |
| unfold totalVariation | |
| exact div_nonneg (Finset.sum_nonneg (fun _ _ => abs_nonneg _)) (by norm_num) | |
| theorem overlapMass_nonneg (p q : Ξ± β β) (hp : β a, 0 β€ p a) | |
| (hq : β a, 0 β€ q a) : 0 β€ overlapMass p q := by | |
| exact Finset.sum_nonneg (fun a _ => le_min (hp a) (hq a)) | |
| private theorem min_abs_identity (x y : β) : 2 * min x y + |x - y| = x + y := by | |
| rcases le_total x y with h | h | |
| Β· rw [min_eq_left h, abs_of_nonpos (sub_nonpos.mpr h)] | |
| ring | |
| Β· rw [min_eq_right h, abs_of_nonneg (sub_nonneg.mpr h)] | |
| ring | |
| private theorem max_abs_identity (x y : β) : 2 * max x y - |x - y| = x + y := by | |
| rcases le_total x y with h | h | |
| Β· rw [max_eq_right h, abs_of_nonpos (sub_nonpos.mpr h)] | |
| ring | |
| Β· rw [max_eq_left h, abs_of_nonneg (sub_nonneg.mpr h)] | |
| ring | |
| theorem overlapMass_eq (p q : Ξ± β β) (hp : β a, p a = 1) (hq : β a, q a = 1) : | |
| overlapMass p q = 1 - totalVariation p q := by | |
| have h := Finset.sum_congr (sβ := Finset.univ) (sβ := Finset.univ) rfl | |
| (fun a _ => min_abs_identity (p a) (q a)) | |
| simp only [Finset.sum_add_distrib, β Finset.mul_sum, hp, hq] at h | |
| unfold overlapMass totalVariation | |
| linarith | |
| theorem unionMass_eq (p q : Ξ± β β) (hp : β a, p a = 1) (hq : β a, q a = 1) : | |
| unionMass p q = 1 + totalVariation p q := by | |
| have h := Finset.sum_congr (sβ := Finset.univ) (sβ := Finset.univ) rfl | |
| (fun a _ => max_abs_identity (p a) (q a)) | |
| simp only [Finset.sum_sub_distrib, Finset.sum_add_distrib, β Finset.mul_sum, hp, hq] at h | |
| unfold unionMass totalVariation | |
| linarith | |
| theorem totalVariation_le_one (p q : Ξ± β β) (hp : β a, 0 β€ p a) | |
| (hq : β a, 0 β€ q a) (hpsum : β a, p a = 1) (hqsum : β a, q a = 1) : | |
| totalVariation p q β€ 1 := by | |
| have h := overlapMass_nonneg p q hp hq | |
| rw [overlapMass_eq p q hpsum hqsum] at h | |
| linarith | |
| theorem overlap_union_ratio (p q : Ξ± β β) | |
| (hp : β a, p a = 1) (hq : β a, q a = 1) : | |
| overlapMass p q / unionMass p q = | |
| (1 - totalVariation p q) / (1 + totalVariation p q) := by | |
| rw [overlapMass_eq p q hp hq, unionMass_eq p q hp hq] | |
| theorem overlap_union_ratio_lower_bound (p q : Ξ± β β) | |
| (hp : β a, p a = 1) (hq : β a, q a = 1) : | |
| 1 - 2 * totalVariation p q β€ overlapMass p q / unionMass p q := by | |
| rw [overlap_union_ratio p q hp hq] | |
| have htv := totalVariation_nonneg p q | |
| have hden : 0 < 1 + totalVariation p q := by linarith | |
| apply (le_div_iffβ hden).2 | |
| nlinarith [sq_nonneg (totalVariation p q)] | |
| theorem first_union_disagreement_upper_bound (p q : Ξ± β β) | |
| (hp : β a, p a = 1) (hq : β a, q a = 1) : | |
| 1 - overlapMass p q / unionMass p q β€ 2 * totalVariation p q := by | |
| linarith [overlap_union_ratio_lower_bound p q hp hq] | |
| variable {Ο : Type*} [Fintype Ο] | |
| def eventMass (w : Ο β β) (event : Ο β Bool) : β := | |
| β s, if event s then w s else 0 | |
| theorem eventMass_nonneg (w : Ο β β) (event : Ο β Bool) (hw : β s, 0 β€ w s) : | |
| 0 β€ eventMass w event := by | |
| apply Finset.sum_nonneg | |
| intro s _ | |
| split <;> simp_all | |
| theorem eventMass_complement (w : Ο β β) (event : Ο β Bool) | |
| (hw : β s, w s = 1) : | |
| eventMass w event + eventMass w (fun s => !(event s)) = 1 := by | |
| unfold eventMass | |
| rw [β Finset.sum_add_distrib] | |
| calc | |
| β s, ((if event s then w s else 0) + (if !event s then w s else 0)) = β s, w s := by | |
| apply Finset.sum_congr rfl | |
| intro s _ | |
| cases event s <;> simp | |
| _ = 1 := hw | |
| def geometricMass (r : β) : Nat β β | |
| | 0 => 0 | |
| | n + 1 => 1 + r * geometricMass r n | |
| theorem geometricMass_nonneg (r : β) (hr : 0 β€ r) (n : Nat) : | |
| 0 β€ geometricMass r n := by | |
| induction n with | |
| | zero => simp [geometricMass] | |
| | succ n ih => exact add_nonneg (by norm_num) (mul_nonneg hr ih) | |
| theorem geometricMass_identity (r : β) (n : Nat) : | |
| (1 - r) * geometricMass r n = 1 - r ^ n := by | |
| induction n with | |
| | zero => simp [geometricMass] | |
| | succ n ih => | |
| simp only [geometricMass, pow_succ] | |
| calc | |
| (1 - r) * (1 + r * geometricMass r n) = | |
| (1 - r) + r * ((1 - r) * geometricMass r n) := by ring | |
| _ = 1 - r ^ n * r := by rw [ih]; ring | |
| def firstHitWeight (w : Ο β β) (accept : Ο β Bool) : Nat β Option Ο β β | |
| | 0, none => 1 | |
| | 0, some _ => 0 | |
| | n + 1, none => eventMass w (fun s => !(accept s)) * firstHitWeight w accept n none | |
| | n + 1, some s => | |
| (if accept s then w s else 0) + | |
| eventMass w (fun s => !(accept s)) * firstHitWeight w accept n (some s) | |
| theorem firstHitWeight_nonneg (w : Ο β β) (accept : Ο β Bool) | |
| (hw : β s, 0 β€ w s) (n : Nat) (output : Option Ο) : | |
| 0 β€ firstHitWeight w accept n output := by | |
| induction n generalizing output with | |
| | zero => cases output <;> simp [firstHitWeight] | |
| | succ n ih => | |
| have hr := eventMass_nonneg w (fun s => !(accept s)) hw | |
| cases output with | |
| | none => exact mul_nonneg hr (ih none) | |
| | some s => | |
| apply add_nonneg | |
| Β· split <;> simp_all | |
| Β· exact mul_nonneg hr (ih (some s)) | |
| theorem firstHitWeight_none (w : Ο β β) (accept : Ο β Bool) (n : Nat) : | |
| firstHitWeight w accept n none = eventMass w (fun s => !(accept s)) ^ n := by | |
| induction n with | |
| | zero => simp [firstHitWeight] | |
| | succ n ih => simp [firstHitWeight, ih, pow_succ, mul_comm] | |
| theorem firstHitWeight_some (w : Ο β β) (accept : Ο β Bool) (n : Nat) (s : Ο) : | |
| firstHitWeight w accept n (some s) = | |
| (if accept s then w s else 0) * | |
| geometricMass (eventMass w (fun s => !(accept s))) n := by | |
| induction n with | |
| | zero => simp [firstHitWeight, geometricMass] | |
| | succ n ih => | |
| rw [firstHitWeight, geometricMass, ih] | |
| ring | |
| theorem firstHitWeight_normalized (w : Ο β β) (accept : Ο β Bool) | |
| (hw : β s, w s = 1) (n : Nat) : β output, firstHitWeight w accept n output = 1 := by | |
| rw [Fintype.sum_option, firstHitWeight_none] | |
| simp_rw [firstHitWeight_some] | |
| rw [β Finset.sum_mul] | |
| change eventMass w (fun s => !(accept s)) ^ n + | |
| eventMass w accept * geometricMass (eventMass w (fun s => !(accept s))) n = 1 | |
| have hc := eventMass_complement w accept hw | |
| have hg := geometricMass_identity (eventMass w (fun s => !(accept s))) n | |
| have ha : eventMass w accept = 1 - eventMass w (fun s => !(accept s)) := by linarith | |
| rw [ha, hg] | |
| ring | |
| def goodFirstMass (w : Ο β β) (accept good : Ο β Bool) (n : Nat) : β := | |
| β s, if good s then firstHitWeight w accept n (some s) else 0 | |
| theorem goodFirstMass_eq (w : Ο β β) (accept good : Ο β Bool) (n : Nat) : | |
| goodFirstMass w accept good n = | |
| eventMass w (fun s => accept s && good s) * | |
| geometricMass (eventMass w (fun s => !(accept s))) n := by | |
| unfold goodFirstMass eventMass | |
| rw [Finset.sum_mul] | |
| apply Finset.sum_congr rfl | |
| intro s _ | |
| rw [firstHitWeight_some] | |
| cases ha : accept s <;> cases hg : good s <;> simp [ha, hg, eventMass] | |
| theorem goodFirstMass_exact (w : Ο β β) (accept good : Ο β Bool) | |
| (hw : β s, w s = 1) (ha : eventMass w accept β 0) (n : Nat) : | |
| goodFirstMass w accept good n = | |
| eventMass w (fun s => accept s && good s) / eventMass w accept * | |
| (1 - eventMass w (fun s => !(accept s)) ^ n) := by | |
| rw [goodFirstMass_eq] | |
| have hc := eventMass_complement w accept hw | |
| have hg := geometricMass_identity (eventMass w (fun s => !(accept s))) n | |
| have hgeom : eventMass w accept * | |
| geometricMass (eventMass w (fun s => !(accept s))) n = | |
| 1 - eventMass w (fun s => !(accept s)) ^ n := by | |
| have heq : eventMass w accept = 1 - eventMass w (fun s => !(accept s)) := by linarith | |
| rw [heq, hg] | |
| rw [β hgeom] | |
| field_simp | |
| theorem commonMass_le_acceptMass (w : Ο β β) (accept good : Ο β Bool) | |
| (hw : β s, 0 β€ w s) : | |
| eventMass w (fun s => accept s && good s) β€ eventMass w accept := by | |
| apply Finset.sum_le_sum | |
| intro s _ | |
| cases ha : accept s <;> cases hg : good s <;> simp [ha, hg, hw s] | |
| theorem goodFirstMass_lower_bound (w : Ο β β) (accept good : Ο β Bool) | |
| (hw : β s, 0 β€ w s) (hw_sum : β s, w s = 1) | |
| (ha : 0 < eventMass w accept) (n : Nat) : | |
| eventMass w (fun s => accept s && good s) / eventMass w accept - | |
| eventMass w (fun s => !(accept s)) ^ n β€ goodFirstMass w accept good n := by | |
| rw [goodFirstMass_exact w accept good hw_sum (ne_of_gt ha) n] | |
| have hratio : eventMass w (fun s => accept s && good s) / eventMass w accept β€ 1 := by | |
| apply (div_le_one ha).2 | |
| exact commonMass_le_acceptMass w accept good hw | |
| have htail : 0 β€ eventMass w (fun s => !(accept s)) ^ n := | |
| pow_nonneg (eventMass_nonneg w (fun s => !(accept s)) hw) n | |
| nlinarith | |
| def traceAverage (w : Ο β β) : Nat β (List Ο β β) β β | |
| | 0, observable => observable [] | |
| | n + 1, observable => β s, w s * traceAverage w n (fun tail => observable (s :: tail)) | |
| theorem traceAverage_const (w : Ο β β) (hw : β s, w s = 1) (n : Nat) (c : β) : | |
| traceAverage w n (fun _ => c) = c := by | |
| induction n with | |
| | zero => rfl | |
| | succ n ih => simp [traceAverage, ih, β Finset.sum_mul, hw] | |
| theorem traceAverage_mono (w : Ο β β) (hw : β s, 0 β€ w s) (n : Nat) | |
| (f g : List Ο β β) (hfg : β xs, f xs β€ g xs) : | |
| traceAverage w n f β€ traceAverage w n g := by | |
| induction n generalizing f g with | |
| | zero => exact hfg [] | |
| | succ n ih => | |
| apply Finset.sum_le_sum | |
| intro s _ | |
| exact mul_le_mul_of_nonneg_left | |
| (ih (fun tail => f (s :: tail)) (fun tail => g (s :: tail)) | |
| (fun tail => hfg (s :: tail))) (hw s) | |
| theorem traceAverage_sum {ΞΉ : Type*} [Fintype ΞΉ] (w : Ο β β) (n : Nat) | |
| (f : ΞΉ β List Ο β β) : | |
| traceAverage w n (fun xs => β i, f i xs) = β i, traceAverage w n (f i) := by | |
| induction n generalizing f with | |
| | zero => rfl | |
| | succ n ih => | |
| simp only [traceAverage] | |
| simp_rw [ih, Finset.mul_sum] | |
| exact Finset.sum_comm | |
| variable [DecidableEq Ο] | |
| theorem firstAccepted_law (w : Ο β β) (accept : Ο β Bool) | |
| (hw : β s, w s = 1) (n : Nat) (output : Option Ο) : | |
| traceAverage w n (fun proposals => | |
| if firstAccepted accept proposals = output then 1 else 0) = | |
| firstHitWeight w accept n output := by | |
| classical | |
| induction n generalizing output with | |
| | zero => cases output <;> simp [traceAverage, firstAccepted, firstHitWeight] | |
| | succ n ih => | |
| have hstep : traceAverage w (n + 1) (fun proposals => | |
| if firstAccepted accept proposals = output then 1 else 0) = | |
| β s, w s * (if accept s then (if some s = output then 1 else 0) | |
| else firstHitWeight w accept n output) := by | |
| unfold traceAverage | |
| apply Finset.sum_congr rfl | |
| intro s _ | |
| congr 1 | |
| by_cases hs : accept s = true | |
| Β· simp [firstAccepted, hs, traceAverage_const w hw] | |
| Β· simpa [firstAccepted, hs] using ih output | |
| rw [hstep] | |
| cases output with | |
| | none => | |
| simp only [Option.some_ne_none, ite_false, firstHitWeight] | |
| rw [eventMass, Finset.sum_mul] | |
| apply Finset.sum_congr rfl | |
| intro s _ | |
| cases accept s <;> simp | |
| | some t => | |
| simp only [Option.some.injEq, firstHitWeight] | |
| calc | |
| β s, w s * (if accept s then (if s = t then 1 else 0) | |
| else firstHitWeight w accept n (some t)) = | |
| β s, ((if s = t then (if accept s then w s else 0) else 0) + | |
| (if !(accept s) then w s else 0) * firstHitWeight w accept n (some t)) := by | |
| apply Finset.sum_congr rfl | |
| intro s _ | |
| by_cases he : s = t | |
| Β· subst s | |
| by_cases hs : accept t = true <;> simp [hs] | |
| Β· by_cases hs : accept s = true <;> simp [hs, he] | |
| _ = (if accept t then w t else 0) + | |
| eventMass w (fun s => !(accept s)) * firstHitWeight w accept n (some t) := by | |
| rw [Finset.sum_add_distrib, β Finset.sum_mul] | |
| simp [eventMass] | |
| def goodFirstIndicator (accept good : Ο β Bool) (proposals : List Ο) : β := | |
| match firstAccepted accept proposals with | |
| | none => 0 | |
| | some s => if good s then 1 else 0 | |
| theorem goodFirstIndicator_expansion (accept good : Ο β Bool) (proposals : List Ο) : | |
| goodFirstIndicator accept good proposals = | |
| β s, if good s then (if firstAccepted accept proposals = some s then 1 else 0) else 0 := by | |
| classical | |
| cases h : firstAccepted accept proposals with | |
| | none => simp [goodFirstIndicator, h] | |
| | some t => | |
| simp only [goodFirstIndicator, h, Option.some.injEq] | |
| calc | |
| (if good t then (1 : β) else 0) = | |
| β s, if t = s then (if good s then (1 : β) else 0) else 0 := by simp | |
| _ = β s, if good s then (if t = s then (1 : β) else 0) else 0 := by | |
| apply Finset.sum_congr rfl | |
| intro s _ | |
| by_cases hg : good s = true <;> by_cases ht : t = s <;> simp [hg, ht] | |
| theorem goodFirstIndicator_law (w : Ο β β) (accept good : Ο β Bool) | |
| (hw : β s, w s = 1) (n : Nat) : | |
| traceAverage w n (goodFirstIndicator accept good) = goodFirstMass w accept good n := by | |
| classical | |
| change traceAverage w n (fun xs => goodFirstIndicator accept good xs) = _ | |
| simp_rw [goodFirstIndicator_expansion] | |
| rw [traceAverage_sum] | |
| unfold goodFirstMass | |
| apply Finset.sum_congr rfl | |
| intro s _ | |
| by_cases hg : good s = true | |
| Β· simpa [hg] using firstAccepted_law w accept hw n (some s) | |
| Β· simp [hg, traceAverage_const w hw] | |
| structure ThresholdInterval where | |
| lo : β | |
| hi : β | |
| valid : lo β€ hi | |
| def splitPoint (I : ThresholdInterval) (t : β) : β := max I.lo (min t I.hi) | |
| def splitLeft (I : ThresholdInterval) (t : β) : ThresholdInterval := | |
| β¨I.lo, splitPoint I t, le_max_left _ _β© | |
| def splitRight (I : ThresholdInterval) (t : β) : ThresholdInterval := | |
| β¨splitPoint I t, I.hi, max_le I.valid (min_le_right _ _)β© | |
| def cutAt (r : β) (I : ThresholdInterval) : Prop := I.hi β€ r β¨ r β€ I.lo | |
| def intervalCDF (r : β) (I : ThresholdInterval) : β := min r I.hi - min r I.lo | |
| theorem split_cdf (I : ThresholdInterval) (t r : β) : | |
| intervalCDF r (splitLeft I t) + intervalCDF r (splitRight I t) = intervalCDF r I := by | |
| unfold intervalCDF splitLeft splitRight | |
| ring | |
| theorem accepted_interval_mass (I : ThresholdInterval) (r : β) (hc : cutAt r I) : | |
| (if I.lo < r then I.hi - I.lo else 0) = intervalCDF r I := by | |
| by_cases hl : I.lo < r | |
| Β· have hu : I.hi β€ r := hc.resolve_right (not_le_of_gt hl) | |
| simp [hl, intervalCDF, min_eq_right hu, min_eq_right (le_of_lt hl)] | |
| Β· have hr : r β€ I.lo := le_of_not_gt hl | |
| simp [hl, intervalCDF, min_eq_left hr, min_eq_left (hr.trans I.valid)] | |
| theorem splitLeft_cut (I : ThresholdInterval) (t : β) : cutAt t (splitLeft I t) := by | |
| by_cases h : I.lo β€ t | |
| Β· exact Or.inl (max_le h (min_le_left _ _)) | |
| Β· exact Or.inr (le_of_lt (lt_of_not_ge h)) | |
| theorem splitRight_cut (I : ThresholdInterval) (t : β) : cutAt t (splitRight I t) := by | |
| by_cases h : t β€ I.hi | |
| Β· right | |
| change t β€ max I.lo (min t I.hi) | |
| rw [min_eq_left h] | |
| exact le_max_right _ _ | |
| Β· exact Or.inl (le_of_lt (lt_of_not_ge h)) | |
| theorem splitLeft_preserves_cut (I : ThresholdInterval) (t r : β) (h : cutAt r I) : | |
| cutAt r (splitLeft I t) := by | |
| rcases h with h | h | |
| Β· exact Or.inl ((max_le I.valid (min_le_right _ _)).trans h) | |
| Β· exact Or.inr h | |
| theorem splitRight_preserves_cut (I : ThresholdInterval) (t r : β) (h : cutAt r I) : | |
| cutAt r (splitRight I t) := by | |
| rcases h with h | h | |
| Β· exact Or.inl h | |
| Β· exact Or.inr (h.trans (le_max_left _ _)) | |
| def splitAll (t : β) : List ThresholdInterval β List ThresholdInterval | |
| | [] => [] | |
| | I :: rest => splitLeft I t :: splitRight I t :: splitAll t rest | |
| def unitThresholdInterval : ThresholdInterval := β¨0, 1, by norm_numβ© | |
| def thresholdPartition : List β β List ThresholdInterval | |
| | [] => [unitThresholdInterval] | |
| | t :: rest => splitAll t (thresholdPartition rest) | |
| def sumOnIntervals (f : ThresholdInterval β β) : List ThresholdInterval β β | |
| | [] => 0 | |
| | I :: rest => f I + sumOnIntervals f rest | |
| theorem sumOn_splitAll (f : ThresholdInterval β β) (t : β) | |
| (hf : β I, f (splitLeft I t) + f (splitRight I t) = f I) | |
| (intervals : List ThresholdInterval) : | |
| sumOnIntervals f (splitAll t intervals) = sumOnIntervals f intervals := by | |
| induction intervals with | |
| | nil => rfl | |
| | cons I rest ih => | |
| simp only [splitAll, sumOnIntervals, ih] | |
| rw [β add_assoc, hf] | |
| theorem partition_sum (f : ThresholdInterval β β) | |
| (hf : β t I, f (splitLeft I t) + f (splitRight I t) = f I) | |
| (thresholds : List β) : | |
| sumOnIntervals f (thresholdPartition thresholds) = f unitThresholdInterval := by | |
| induction thresholds with | |
| | nil => simp [thresholdPartition, sumOnIntervals] | |
| | cons t rest ih => | |
| rw [thresholdPartition, sumOn_splitAll f t (hf t), ih] | |
| theorem partition_total_mass (thresholds : List β) : | |
| sumOnIntervals (fun I => I.hi - I.lo) (thresholdPartition thresholds) = 1 := by | |
| have h := partition_sum (fun I => I.hi - I.lo) | |
| (fun t I => by simp only [splitLeft, splitRight]; ring) thresholds | |
| simpa [unitThresholdInterval] using h | |
| theorem partition_cdf (thresholds : List β) (r : β) (h0 : 0 β€ r) (h1 : r β€ 1) : | |
| sumOnIntervals (intervalCDF r) (thresholdPartition thresholds) = r := by | |
| have h := partition_sum (intervalCDF r) (fun t I => split_cdf I t r) thresholds | |
| simpa [intervalCDF, unitThresholdInterval, min_eq_left h1, min_eq_right h0] using h | |
| theorem splitAll_new_cuts (intervals : List ThresholdInterval) (t : β) : | |
| β I β splitAll t intervals, cutAt t I := by | |
| induction intervals with | |
| | nil => intro I h; simp [splitAll] at h | |
| | cons J rest ih => | |
| intro I h | |
| rcases List.mem_cons.mp h with h | h | |
| Β· subst I; exact splitLeft_cut J t | |
| Β· rcases List.mem_cons.mp h with h | h | |
| Β· subst I; exact splitRight_cut J t | |
| Β· exact ih I h | |
| theorem splitAll_preserves_cuts (intervals : List ThresholdInterval) (t r : β) : | |
| (β I β intervals, cutAt r I) β β I β splitAll t intervals, cutAt r I := by | |
| induction intervals with | |
| | nil => intro _ I h; simp [splitAll] at h | |
| | cons J rest ih => | |
| intro hc I h | |
| have hJ : cutAt r J := hc J (by simp) | |
| have hrest : β K β rest, cutAt r K := fun K hK => hc K (by simp [hK]) | |
| rcases List.mem_cons.mp h with h | h | |
| Β· subst I; exact splitLeft_preserves_cut J t r hJ | |
| Β· rcases List.mem_cons.mp h with h | h | |
| Β· subst I; exact splitRight_preserves_cut J t r hJ | |
| Β· exact ih hrest I h | |
| theorem partition_cuts (thresholds : List β) (r : β) : | |
| r β thresholds β β I β thresholdPartition thresholds, cutAt r I := by | |
| induction thresholds with | |
| | nil => intro hr; simp at hr | |
| | cons t rest ih => | |
| intro hr | |
| rcases List.mem_cons.mp hr with h | h | |
| Β· subst r; exact splitAll_new_cuts (thresholdPartition rest) t | |
| Β· exact splitAll_preserves_cuts (thresholdPartition rest) t r (ih h) | |
| theorem sumOnIntervals_congr (f g : ThresholdInterval β β) (intervals : List ThresholdInterval) : | |
| (β I β intervals, f I = g I) β sumOnIntervals f intervals = sumOnIntervals g intervals := by | |
| induction intervals with | |
| | nil => intro _; rfl | |
| | cons J rest ih => | |
| intro h | |
| have hJ := h J (by simp) | |
| have hr : β I β rest, f I = g I := fun I hI => h I (by simp [hI]) | |
| simp only [sumOnIntervals, hJ, ih hr] | |
| theorem partition_acceptance_mass (thresholds : List β) (r : β) | |
| (hr : r β thresholds) (h0 : 0 β€ r) (h1 : r β€ 1) : | |
| sumOnIntervals (fun I => if I.lo < r then I.hi - I.lo else 0) | |
| (thresholdPartition thresholds) = r := by | |
| calc | |
| _ = sumOnIntervals (intervalCDF r) (thresholdPartition thresholds) := | |
| sumOnIntervals_congr _ _ _ (fun I hI => | |
| accepted_interval_mass I r (partition_cuts thresholds r hr I hI)) | |
| _ = r := partition_cdf thresholds r h0 h1 | |
| theorem sum_interval_get (f : ThresholdInterval β β) (intervals : List ThresholdInterval) : | |
| (β i : Fin intervals.length, f intervals[i]) = sumOnIntervals f intervals := by | |
| induction intervals with | |
| | nil => simp [sumOnIntervals] | |
| | cons I rest ih => | |
| simpa [Fin.sum_univ_succ, sumOnIntervals] using congrArg (fun x : β => f I + x) ih | |
| variable {Ξ± : Type*} [Fintype Ξ±] | |
| private theorem sum_div_const {ΞΉ : Type*} [Fintype ΞΉ] (f : ΞΉ β β) (c : β) : | |
| (β i, f i) / c = β i, f i / c := by | |
| simp only [div_eq_mul_inv, Finset.sum_mul] | |
| abbrev RectangleSeed (thresholds : List β) (Ξ± : Type*) := | |
| Ξ± Γ Fin (thresholdPartition thresholds).length | |
| def rectangleWeight (thresholds : List β) (seed : RectangleSeed thresholds Ξ±) : β := | |
| ((thresholdPartition thresholds)[seed.2].hi - (thresholdPartition thresholds)[seed.2].lo) / | |
| (Fintype.card Ξ± : β) | |
| def rectangleAccept (thresholds : List β) (density : Ξ± β β) | |
| (seed : RectangleSeed thresholds Ξ±) : Bool := | |
| decide ((thresholdPartition thresholds)[seed.2].lo < density seed.1) | |
| theorem rectangleWeight_nonnegative (thresholds : List β) (seed : RectangleSeed thresholds Ξ±) : | |
| 0 β€ rectangleWeight thresholds seed := | |
| div_nonneg (sub_nonneg.mpr ((thresholdPartition thresholds)[seed.2].valid)) (by positivity) | |
| theorem rectangleWeight_normalized [Nonempty Ξ±] (thresholds : List β) : | |
| β seed : RectangleSeed thresholds Ξ±, rectangleWeight thresholds seed = 1 := by | |
| rw [Fintype.sum_prod_type] | |
| have hrow (a : Ξ±) : (β i, rectangleWeight thresholds (a, i)) = | |
| 1 / (Fintype.card Ξ± : β) := by | |
| unfold rectangleWeight | |
| rw [β sum_div_const, sum_interval_get (fun I => I.hi - I.lo), partition_total_mass] | |
| simp_rw [hrow] | |
| have hc : (Fintype.card Ξ± : β) β 0 := by exact_mod_cast Fintype.card_ne_zero | |
| simp [hc] | |
| def rectangleDistribution [Nonempty Ξ±] (thresholds : List β) : | |
| Games.FiniteDistribution (RectangleSeed thresholds Ξ±) where | |
| weight := rectangleWeight thresholds | |
| nonnegative := rectangleWeight_nonnegative thresholds | |
| normalized := rectangleWeight_normalized thresholds | |
| theorem rectangle_row_mass (thresholds : List β) (density : Ξ± β β) (a : Ξ±) | |
| (hm : density a β thresholds) (h0 : 0 β€ density a) (h1 : density a β€ 1) : | |
| (β i, if rectangleAccept thresholds density (a, i) then | |
| rectangleWeight thresholds (a, i) else 0) = density a / (Fintype.card Ξ± : β) := by | |
| calc | |
| _ = (β i : Fin (thresholdPartition thresholds).length, | |
| if (thresholdPartition thresholds)[i].lo < density a then | |
| (thresholdPartition thresholds)[i].hi - (thresholdPartition thresholds)[i].lo else 0) / | |
| (Fintype.card Ξ± : β) := by | |
| rw [sum_div_const] | |
| apply Finset.sum_congr rfl | |
| intro i _ | |
| simp [rectangleAccept, rectangleWeight, ite_div] | |
| _ = density a / (Fintype.card Ξ± : β) := by | |
| rw [sum_interval_get (fun I => if I.lo < density a then I.hi - I.lo else 0), | |
| partition_acceptance_mass thresholds (density a) hm h0 h1] | |
| theorem rectangle_acceptance_mass (thresholds : List β) (density : Ξ± β β) | |
| (hm : β a, density a β thresholds) (h0 : β a, 0 β€ density a) | |
| (h1 : β a, density a β€ 1) : | |
| eventMass (rectangleWeight thresholds) (rectangleAccept thresholds density) = | |
| (β a, density a) / (Fintype.card Ξ± : β) := by | |
| unfold eventMass | |
| rw [Fintype.sum_prod_type] | |
| simp_rw [rectangle_row_mass thresholds density _ (hm _) (h0 _) (h1 _)] | |
| rw [sum_div_const] | |
| theorem rectangle_label_mass [DecidableEq Ξ±] (thresholds : List β) (density : Ξ± β β) (a : Ξ±) | |
| (hm : β x, density x β thresholds) (h0 : β x, 0 β€ density x) | |
| (h1 : β x, density x β€ 1) : | |
| eventMass (rectangleWeight thresholds) | |
| (fun seed => rectangleAccept thresholds density seed && decide (seed.1 = a)) = | |
| density a / (Fintype.card Ξ± : β) := by | |
| unfold eventMass | |
| rw [Fintype.sum_prod_type] | |
| calc | |
| _ = β x : Ξ±, if x = a then density x / (Fintype.card Ξ± : β) else 0 := by | |
| apply Finset.sum_congr rfl | |
| intro x _ | |
| by_cases hx : x = a | |
| Β· simpa [hx] using rectangle_row_mass thresholds density x (hm x) (h0 x) (h1 x) | |
| Β· simp [hx] | |
| _ = density a / (Fintype.card Ξ± : β) := by simp | |
| omit [Fintype Ξ±] in theorem rectangle_and (thresholds : List β) (p q : Ξ± β β) : | |
| (fun seed => rectangleAccept thresholds p seed && rectangleAccept thresholds q seed) = | |
| rectangleAccept thresholds (fun a => min (p a) (q a)) := by | |
| funext seed | |
| simp [rectangleAccept] | |
| omit [Fintype Ξ±] in theorem rectangle_or (thresholds : List β) (p q : Ξ± β β) : | |
| (fun seed => rectangleAccept thresholds p seed || rectangleAccept thresholds q seed) = | |
| rectangleAccept thresholds (fun a => max (p a) (q a)) := by | |
| funext seed | |
| simp [rectangleAccept] | |
| theorem min_mem_thresholds (thresholds : List β) {x y : β} (hx : x β thresholds) | |
| (hy : y β thresholds) : min x y β thresholds := by | |
| by_cases h : x β€ y <;> simp [min_def, h, hx, hy] | |
| theorem max_mem_thresholds (thresholds : List β) {x y : β} (hx : x β thresholds) | |
| (hy : y β thresholds) : max x y β thresholds := by | |
| by_cases h : x β€ y <;> simp [max_def, h, hx, hy] | |
| theorem rectangle_union_mass (thresholds : List β) (p q : Ξ± β β) | |
| (hpm : β a, p a β thresholds) (hqm : β a, q a β thresholds) | |
| (hp0 : β a, 0 β€ p a) (_hq0 : β a, 0 β€ q a) | |
| (hp1 : β a, p a β€ 1) (hq1 : β a, q a β€ 1) : | |
| eventMass (rectangleWeight thresholds) | |
| (fun seed => rectangleAccept thresholds p seed || rectangleAccept thresholds q seed) = | |
| unionMass p q / (Fintype.card Ξ± : β) := by | |
| rw [rectangle_or] | |
| exact rectangle_acceptance_mass thresholds _ | |
| (fun a => max_mem_thresholds thresholds (hpm a) (hqm a)) | |
| (fun a => (hp0 a).trans (le_max_left _ _)) (fun a => max_le (hp1 a) (hq1 a)) | |
| theorem rectangle_common_label_mass [DecidableEq Ξ±] (thresholds : List β) | |
| (p q : Ξ± β β) (a : Ξ±) | |
| (hpm : β x, p x β thresholds) (hqm : β x, q x β thresholds) | |
| (hp0 : β x, 0 β€ p x) (hq0 : β x, 0 β€ q x) | |
| (hp1 : β x, p x β€ 1) (_hq1 : β x, q x β€ 1) : | |
| eventMass (rectangleWeight thresholds) | |
| (fun seed => (rectangleAccept thresholds p seed && rectangleAccept thresholds q seed) && | |
| decide (seed.1 = a)) = min (p a) (q a) / (Fintype.card Ξ± : β) := by | |
| have hand := congrFun (rectangle_and thresholds p q) | |
| simp_rw [hand] | |
| exact rectangle_label_mass thresholds _ a | |
| (fun x => min_mem_thresholds thresholds (hpm x) (hqm x)) | |
| (fun x => le_min (hp0 x) (hq0 x)) (fun x => (min_le_left _ _).trans (hp1 x)) | |
| end | |
| end MinUncutGames.Foundations.CorrelatedSampling | |
| end OAI | |