import JaxLean.Stdlib.Batch import Mathlib.Data.Real.Basic import Mathlib.Data.Fintype.BigOperators import Mathlib.Tactic

Exact finite probability, separate from any particular sampler or program. A random variable is a function on outcomes. Reusing that function preserves dependence; prod explicitly constructs independent sample spaces.

namespace JaxLeanopen scoped BigOperatorsstructure FiniteLaw (Ω : Type) [Fintype Ω] where mass : Ω → ℝ nonneg : ∀ ω, 0 ≤ mass ω total : ∑ ω, mass ω = 1namespace FiniteLawvariable {Ω Ξ : Type} [Fintype Ω] [Fintype Ξ]noncomputable def mean (p : FiniteLaw Ω) (X : Ω → ℝ) : ℝ := ∑ ω, p.mass ω * X ωnoncomputable def variance (p : FiniteLaw Ω) (X : Ω → ℝ) : ℝ := p.mean (fun ω => (X ω - p.mean X) ^ 2)noncomputable def covariance (p : FiniteLaw Ω) (X Y : Ω → ℝ) : ℝ := p.mean (fun ω => (X ω - p.mean X) * (Y ω - p.mean Y))@[simp] theorem mean_const (p : FiniteLaw Ω) (c : ℝ) : p.mean (fun _ => c) = c := Ω:Typeinst✝:Fintype Ωp:FiniteLaw Ωc:ℝ⊢ (p.mean fun x => c) = c All goals completed! 🐙theorem mean_add (p : FiniteLaw Ω) (X Y : Ω → ℝ) : p.mean (fun ω => X ω + Y ω) = p.mean X + p.mean Y := Ω:Typeinst✝:Fintype Ωp:FiniteLaw ΩX:Ω → ℝY:Ω → ℝ⊢ (p.mean fun ω => X ω + Y ω) = p.mean X + p.mean Y All goals completed! 🐙theorem mean_sub (p : FiniteLaw Ω) (X Y : Ω → ℝ) : p.mean (fun ω => X ω - Y ω) = p.mean X - p.mean Y := Ω:Typeinst✝:Fintype Ωp:FiniteLaw ΩX:Ω → ℝY:Ω → ℝ⊢ (p.mean fun ω => X ω - Y ω) = p.mean X - p.mean Y All goals completed! 🐙theorem mean_scale (p : FiniteLaw Ω) (a : ℝ) (X : Ω → ℝ) : p.mean (fun ω => a * X ω) = a * p.mean X := Ω:Typeinst✝:Fintype Ωp:FiniteLaw Ωa:ℝX:Ω → ℝ⊢ (p.mean fun ω => a * X ω) = a * p.mean X All goals completed! 🐙

Expectation is just a linear functional on a space of value functions.

noncomputable def meanLinear (p : FiniteLaw Ω) : (Ω → ℝ) →ₗ[ℝ] ℝ where toFun := p.mean map_add' := p.mean_add map_smul' := p.mean_scale

Reduction commutation is generic linear algebra, without independence.

Ω:Typeinst✝¹:Fintype Ωι:Typeinst✝:Fintype ιp:FiniteLaw ΩX:ι → Ω → ℝh:(fun ω => ∑ i, X i ω) = ∑ i, X i⊢ p.mean (∑ i, X i) = ∑ i, p.mean (X i) All goals completed! 🐙All goals completed! 🐙theorem variance_nonneg (p : FiniteLaw Ω) (X : Ω → ℝ) : 0 ≤ p.variance X := Ω:Typeinst✝:Fintype Ωp:FiniteLaw ΩX:Ω → ℝ⊢ 0 ≤ p.variance X All goals completed! 🐙

Useful after a nonlinear operation: retain the second moment, not just the mean.

Ω:Typeinst✝:Fintype Ωp:FiniteLaw ΩX:Ω → ℝh:(fun ω => (X ω - p.mean X) ^ 2) = fun ω => X ω ^ 2 - 2 * p.mean X * X ω + p.mean X ^ 2⊢ (p.mean fun ω => X ω ^ 2) - 2 * p.mean X * p.mean X + p.mean X ^ 2 = (p.mean fun ω => X ω ^ 2) - p.mean X ^ 2 All goals completed! 🐙All goals completed! 🐙

No independence assumption: reusing a draw contributes covariance.

All goals completed! 🐙

Independent draws use product probabilities. This is an explicit construction.

noncomputable def prod (p : FiniteLaw Ω) (q : FiniteLaw Ξ) : FiniteLaw (Ω × Ξ) where mass ω := p.mass ω.1 * q.mass ω.2 nonneg ω := mul_nonneg (p.nonneg _) (q.nonneg _) total := Ω:TypeΞ:Typeinst✝¹:Fintype Ωinst✝:Fintype Ξp:FiniteLaw Ωq:FiniteLaw Ξ⊢ ∑ ω, p.mass ω.1 * q.mass ω.2 = 1 All goals completed! 🐙@[simp] theorem mean_prod_left (p : FiniteLaw Ω) (q : FiniteLaw Ξ) (X : Ω → ℝ) : (p.prod q).mean (fun ω => X ω.1) = p.mean X := Ω:TypeΞ:Typeinst✝¹:Fintype Ωinst✝:Fintype Ξp:FiniteLaw Ωq:FiniteLaw ΞX:Ω → ℝ⊢ ((p.prod q).mean fun ω => X ω.1) = p.mean X All goals completed! 🐙@[simp] theorem mean_prod_right (p : FiniteLaw Ω) (q : FiniteLaw Ξ) (Y : Ξ → ℝ) : (p.prod q).mean (fun ω => Y ω.2) = q.mean Y := Ω:TypeΞ:Typeinst✝¹:Fintype Ωinst✝:Fintype Ξp:FiniteLaw Ωq:FiniteLaw ΞY:Ξ → ℝ⊢ ((p.prod q).mean fun ω => Y ω.2) = q.mean Y All goals completed! 🐙Ω:TypeΞ:Typeinst✝¹:Fintype Ωinst✝:Fintype Ξp:FiniteLaw Ωq:FiniteLaw ΞX:Ω → ℝY:Ξ → ℝ⊢ ∑ y, ∑ x, p.mass x * q.mass y * (X x * Y y) = ∑ x, ∑ i, p.mass i * X i * (q.mass x * Y x) Ω:TypeΞ:Typeinst✝¹:Fintype Ωinst✝:Fintype Ξp:FiniteLaw Ωq:FiniteLaw ΞX:Ω → ℝY:Ξ → ℝ⊢ ∀ x ∈ Finset.univ, ∑ x_1, p.mass x_1 * q.mass x * (X x_1 * Y x) = ∑ i, p.mass i * X i * (q.mass x * Y x) Ω:TypeΞ:Typeinst✝¹:Fintype Ωinst✝:Fintype Ξp:FiniteLaw Ωq:FiniteLaw ΞX:Ω → ℝY:Ξ → ℝω:Ξa✝:ω ∈ Finset.univ⊢ ∑ x, p.mass x * q.mass ω * (X x * Y ω) = ∑ i, p.mass i * X i * (q.mass ω * Y ω) Ω:TypeΞ:Typeinst✝¹:Fintype Ωinst✝:Fintype Ξp:FiniteLaw Ωq:FiniteLaw ΞX:Ω → ℝY:Ξ → ℝω:Ξa✝:ω ∈ Finset.univ⊢ ∀ x ∈ Finset.univ, p.mass x * q.mass ω * (X x * Y ω) = p.mass x * X x * (q.mass ω * Y ω) Ω:TypeΞ:Typeinst✝¹:Fintype Ωinst✝:Fintype Ξp:FiniteLaw Ωq:FiniteLaw ΞX:Ω → ℝY:Ξ → ℝω:Ξa✝¹:ω ∈ Finset.univξ:Ωa✝:ξ ∈ Finset.univ⊢ p.mass ξ * q.mass ω * (X ξ * Y ω) = p.mass ξ * X ξ * (q.mass ω * Y ω) All goals completed! 🐙Ω:TypeΞ:Typeinst✝¹:Fintype Ωinst✝:Fintype Ξp:FiniteLaw Ωq:FiniteLaw ΞX:Ω → ℝY:Ξ → ℝh:(fun ω => (X ω.1 + Y ω.2) ^ 2) = fun ω => X ω.1 ^ 2 + Y ω.2 ^ 2 + 2 * (X ω.1 * Y ω.2)⊢ ((p.mean fun ω => X ω ^ 2) + q.mean fun ω => Y ω ^ 2) + 2 * (p.mean X * q.mean Y) - (p.mean X + q.mean Y) ^ 2 = (p.mean fun ω => X ω ^ 2) - p.mean X ^ 2 + ((q.mean fun ω => Y ω ^ 2) - q.mean Y ^ 2) All goals completed! 🐙

Constant division scales variance by the squared divisor.

Ω:Typeinst✝:Fintype Ωp:FiniteLaw ΩX:Ω → ℝa:ℝh:(fun ω => X ω / a) = fun ω => a⁻¹ * X ω + 0⊢ a⁻¹ ^ 2 * p.variance X = p.variance X / a ^ 2 All goals completed! 🐙end FiniteLawend JaxLean