introvoyz045's picture
Migrated from GitHub
4a56cd6 verified
Raw History Blame Contribute Delete
12 kB
import OAI.Combinatorics.PerfectMatching.Duality
namespace OAI
/-!
Derivatives of entropy and log-partition, and the nonlinear entropy barrier.
-/
noncomputable section
open scoped BigOperators Topology
open Filter
namespace MatchingEntropy
/-- First derivative of a finite exponential partition function. -/
theorem partition_hasDerivAt {ΞΉ : Type*} [Fintype ΞΉ] (p a : ΞΉ β†’ ℝ) (t : ℝ) :
HasDerivAt (partition p a) (partition (fun i => p i*a i) a t) t := by
apply HasDerivAt.fun_sum
intro i _
simpa only [one_mul, id_eq, mul_assoc, mul_comm, mul_left_comm] using
(((hasDerivAt_id t).mul_const (a i)).exp.const_mul (p i))
/-- The logarithmic partition derivative, with no infinite optimization
or interchange of limits: only finite sums. -/
theorem logPartition_hasDerivAt {ΞΉ : Type*} [Fintype ΞΉ] {p : ΞΉ β†’ ℝ}
(hp : p ∈ probabilitySimplex ΞΉ) (a : ΞΉ β†’ ℝ) (t : ℝ) :
HasDerivAt (fun t => Real.log (partition p a t))
(partition (fun i => p i*a i) a t / partition p a t) t :=
(partition_hasDerivAt p a t).log (partition_pos hp a t).ne'
/-- The second derivative of log-partition at the base law is variance. -/
theorem logPartition_second {ΞΉ : Type*} [Fintype ΞΉ] {p : ΞΉ β†’ ℝ}
(hp : p ∈ probabilitySimplex ΞΉ) (a : ΞΉ β†’ ℝ) :
HasDerivAt (fun t => partition (fun i => p i*a i) a t / partition p a t)
((βˆ‘ i, p i*(a i)^2) - (βˆ‘ i, p i*a i)^2) 0 := by
have hd := (partition_hasDerivAt (fun i => p i*a i) a 0).div
(partition_hasDerivAt p a 0) (partition_pos hp a 0).ne'
refine hd.congr_deriv ?_
simp only [partition, zero_mul, Real.exp_zero, mul_one, hp.2, one_pow, div_one]
congr 1
Β· apply Finset.sum_congr rfl
intro i _
ring
Β· ring
/-- The explicit first derivative along a supported affine marginal path. -/
def entropyLineD {ΞΉ : Type*} [Fintype ΞΉ] (x v : ΞΉ β†’ ℝ) (t : ℝ) : ℝ :=
-βˆ‘ i, v i * Real.log (x i+t*v i) - βˆ‘ i, v i
theorem entropy_line_hasDerivAt_at {ΞΉ : Type*} [Fintype ΞΉ] (x v : ΞΉ β†’ ℝ)
(t : ℝ) (h : βˆ€ i, x i+t*v i β‰  0 ∨ v i = 0) :
HasDerivAt (fun t : ℝ => entropy (fun i => x i+t*v i)) (entropyLineD x v t) t := by
have hd : βˆ€ i, HasDerivAt (fun t : ℝ => Real.negMulLog (x i+t*v i))
((-Real.log (x i+t*v i)-1)*v i) t := by
intro i
rcases h i with hi | hi
Β· simpa only [one_mul, Function.comp_def, id_eq] using
(Real.hasDerivAt_negMulLog hi).comp t
(((hasDerivAt_id t).mul_const (v i)).const_add (x i))
Β· simpa only [hi, mul_zero, add_zero] using (hasDerivAt_const t (Real.negMulLog (x i)))
have he : (βˆ‘ i, (-Real.log (x i+t*v i)-1)*v i) = entropyLineD x v t := by
simp_rw [sub_mul, one_mul, neg_mul, mul_comm (Real.log (x _+t*v _))]
rw [Finset.sum_sub_distrib, Finset.sum_neg_distrib]
rfl
rw [← he]
simpa only [entropy, Finset.sum_apply] using
(HasDerivAt.fun_sum (u := Finset.univ) (fun i _ => hd i))
theorem entropy_line_deriv_eventually {ΞΉ : Type*} [Fintype ΞΉ] (x v : ΞΉ β†’ ℝ)
(hsupp : βˆ€ i, x i = 0 β†’ v i = 0) :
deriv (fun t : ℝ => entropy (fun i => x i+t*v i)) =αΆ [𝓝 0] entropyLineD x v := by
have hh : βˆ€αΆ  t : ℝ in 𝓝 0, βˆ€ i, x i+t*v i β‰  0 ∨ v i = 0 := by
apply Filter.eventually_all.mpr
intro i
by_cases hi : x i = 0
Β· exact Filter.Eventually.of_forall (fun t => Or.inr (hsupp i hi))
Β· have hc : ContinuousAt (fun t : ℝ => x i+t*v i) 0 := by fun_prop
have hn := hc.eventually_ne (show x i+(0:ℝ)*v i β‰  0 by simpa using hi)
exact hn.mono (fun t ht => Or.inl ht)
filter_upwards [hh] with t ht
exact (entropy_line_hasDerivAt_at x v t ht).deriv
theorem entropyLineD_hasDerivAt {ΞΉ : Type*} [Fintype ΞΉ] (x v : ΞΉ β†’ ℝ)
(hsupp : βˆ€ i, x i = 0 β†’ v i = 0) :
HasDerivAt (entropyLineD x v) (-βˆ‘ i, (v i)^2/x i) 0 := by
have hd : βˆ€ i, HasDerivAt (fun t : ℝ => v i*Real.log (x i+t*v i))
((v i)^2/x i) 0 := by
intro i
by_cases hi : x i = 0
Β· simpa only [hi, hsupp i hi, zero_mul, zero_pow (by omega : 2 β‰  0), zero_div] using
(hasDerivAt_const (0:ℝ) (0:ℝ))
Β· have hh := ((((hasDerivAt_id (0:ℝ)).mul_const (v i)).const_add (x i)).log
(by simpa using hi)).const_mul (v i)
simpa only [id_eq, one_mul, zero_mul, add_zero, pow_two, mul_div_assoc] using hh
change HasDerivAt (fun t : ℝ => -(βˆ‘ i, v i*Real.log (x i+t*v i)) - βˆ‘ i, v i)
(-βˆ‘ i, (v i)^2/x i) 0
exact (HasDerivAt.fun_sum (u := Finset.univ) (fun i _ => hd i)).neg.sub_const (βˆ‘ i, v i)
theorem entropy_line_second {ΞΉ : Type*} [Fintype ΞΉ] (x v : ΞΉ β†’ ℝ)
(hsupp : βˆ€ i, x i = 0 β†’ v i = 0) :
HasDerivAt (deriv (fun t : ℝ => entropy (fun i => x i+t*v i)))
(-βˆ‘ i, (v i)^2/x i) 0 :=
(entropyLineD_hasDerivAt x v hsupp).congr_of_eventuallyEq
(entropy_line_deriv_eventually x v hsupp)
def entropyBarrier (m h : ℝ) : ℝ := h-8*m*(1-Real.exp (-h/m))
def entropyBarrierD (m h : ℝ) : ℝ := 1-8*Real.exp (-h/m)
theorem entropyBarrier_hasDerivAt {m : ℝ} (hm : m β‰  0) (h : ℝ) :
HasDerivAt (entropyBarrier m) (entropyBarrierD m h) h := by
have he := (((hasDerivAt_id h).neg.div_const m).exp.const_sub 1).const_mul (8*m)
have hd := (hasDerivAt_id h).sub he
refine hd.congr_deriv ?_
dsimp [entropyBarrierD]
field_simp
theorem entropyBarrierD_hasDerivAt (m h : ℝ) :
HasDerivAt (entropyBarrierD m) (8*Real.exp (-h/m)/m) h := by
have hd := (((hasDerivAt_id h).neg.div_const m).exp.const_mul 8).const_sub 1
refine hd.congr_deriv ?_
dsimp
ring
/-- Second derivative of the scalar barrier composed with any twice
explicitly differentiable path. -/
theorem barrier_second_path {m : ℝ} (hm : m β‰  0) {f fD : ℝ β†’ ℝ} {fDD : ℝ}
(hf : βˆ€αΆ  t in 𝓝 (0:ℝ), HasDerivAt f (fD t) t)
(hDD : HasDerivAt fD fDD 0) :
HasDerivAt (deriv (fun t => entropyBarrier m (f t)))
(entropyBarrierD m (f 0)*fDD +
(8*Real.exp (-f 0/m)/m)*(fD 0)^2) 0 := by
have h0 : HasDerivAt f (fD 0) 0 := hf.self_of_nhds
have he : deriv (fun t => entropyBarrier m (f t)) =αΆ [𝓝 (0:ℝ)]
(fun t => entropyBarrierD m (f t)*fD t) := by
filter_upwards [hf] with t ht
exact ((entropyBarrier_hasDerivAt hm (f t)).comp t ht).deriv
have hd := ((entropyBarrierD_hasDerivAt m (f 0)).comp 0 h0).mul hDD
have hd' : HasDerivAt (fun t => entropyBarrierD m (f t)*fD t)
(entropyBarrierD m (f 0)*fDD +
(8*Real.exp (-f 0/m)/m)*(fD 0)^2) 0 := by
refine hd.congr_deriv ?_
dsimp only [Function.comp_apply]
ring
exact hd'.congr_of_eventuallyEq he
/-- A direct second-derivative calculation for the affine-law comparison
path used below. -/
theorem second_deriv_quadratic_adjustment {f fD g gD : ℝ β†’ ℝ} {fDD gDD cβ‚€ c₁ v : ℝ}
(hf : βˆ€αΆ  t in 𝓝 (0:ℝ), HasDerivAt f (fD t) t)
(hg : βˆ€αΆ  t in 𝓝 (0:ℝ), HasDerivAt g (gD t) t)
(hfD : HasDerivAt fD fDD 0) (hgD : HasDerivAt gD gDD 0) :
HasDerivAt (deriv (fun t => f t+cβ‚€+t*c₁+t^2*v-g t))
(fDD+2*v-gDD) 0 := by
have hfirst : deriv (fun t => f t+cβ‚€+t*c₁+t^2*v-g t) =αΆ [𝓝 (0:ℝ)]
(fun t => fD t+c₁+2*t*v-gD t) := by
filter_upwards [hf, hg] with t hft hgt
have hd := (((hft.add_const cβ‚€).fun_add ((hasDerivAt_id t).mul_const c₁)).fun_add
(((hasDerivAt_id t).fun_pow 2).mul_const v)).fun_sub hgt
simpa only [Pi.add_apply, Pi.sub_apply, Pi.pow_apply, id_eq, one_mul,
Nat.cast_ofNat, Nat.reduceSub, pow_one, mul_one] using hd.deriv
have hd := ((hfD.add_const c₁).fun_add
(((hasDerivAt_id (0:ℝ)).const_mul 2).mul_const v)).fun_sub hgD
have hd' : HasDerivAt (fun t => fD t+c₁+2*t*v-gD t) (fDD+2*v-gDD) 0 := by
simpa only [Pi.add_apply, Pi.sub_apply, id_eq, mul_one] using hd
exact hd'.congr_of_eventuallyEq hfirst
/-- The necessary one-dimensional second-derivative test, also when the
second derivative is defined by Lean's totalized derivative. -/
theorem second_deriv_nonpos_of_local_max {f : ℝ β†’ ℝ} {x : ℝ}
(hm : IsLocalMax f x) (hc : ContinuousAt f x) : deriv (deriv f) x ≀ 0 := by
by_contra h
have hp : 0 < deriv (deriv f) x := lt_of_not_ge h
have hmin := isLocalMin_of_deriv_deriv_pos hp hm.deriv_eq_zero hc
have he : f =αΆ [𝓝 x] (fun _ => f x) := by
filter_upwards [hm, hmin] with y h₁ hβ‚‚
exact le_antisymm h₁ hβ‚‚
have hh := he.deriv.deriv_eq
simp only [deriv_const', deriv_const] at hh
linarith
/-- All first derivatives on a neighborhood, including boundary coordinates
whose admissible direction is zero. -/
theorem entropy_line_eventually_hasDerivAt {ΞΉ : Type*} [Fintype ΞΉ] (x v : ΞΉ β†’ ℝ)
(hsupp : βˆ€ i, x i = 0 β†’ v i = 0) :
βˆ€αΆ  t : ℝ in 𝓝 0,
HasDerivAt (fun t : ℝ => entropy (fun i => x i+t*v i)) (entropyLineD x v t) t := by
have hh : βˆ€αΆ  t : ℝ in 𝓝 0, βˆ€ i, x i+t*v i β‰  0 ∨ v i = 0 := by
apply Filter.eventually_all.mpr
intro i
by_cases hi : x i = 0
Β· exact Filter.Eventually.of_forall (fun t => Or.inr (hsupp i hi))
Β· have hc : ContinuousAt (fun t : ℝ => x i+t*v i) 0 := by fun_prop
have hn := hc.eventually_ne (show x i+(0:ℝ)*v i β‰  0 by simpa using hi)
exact hn.mono (fun t ht => Or.inl ht)
exact hh.mono (fun t ht => entropy_line_hasDerivAt_at x v t ht)
theorem entropy_barrier_line_second {ΞΉ : Type*} [Fintype ΞΉ]
(x v : ΞΉ β†’ ℝ) (hsupp : βˆ€ i, x i = 0 β†’ v i = 0) {m : ℝ} (_hm : m β‰  0) :
HasDerivAt (fun t => entropyBarrierD m (entropy (fun i => x i+t*v i))*entropyLineD x v t)
((8*Real.exp (-entropy x/m)-1)*(βˆ‘ i, (v i)^2/x i) +
(8*Real.exp (-entropy x/m)/m)*(entropyLineD x v 0)^2) 0 := by
have h0 := (entropy_line_eventually_hasDerivAt x v hsupp).self_of_nhds
have hd := ((entropyBarrierD_hasDerivAt m (entropy (fun i => x i+(0:ℝ)*v i))).comp 0 h0).mul
(entropyLineD_hasDerivAt x v hsupp)
refine hd.congr_deriv ?_
simp only [Function.comp_apply, zero_mul, add_zero, entropyBarrierD]
ring
/-- The exact second variation along affine laws, proved solely by ordinary
one-variable derivatives and the finite log-partition variance identity. -/
theorem entropy_second_variation {ΞΉ ΞΊ : Type*} [Fintype ΞΉ] [Fintype ΞΊ]
(x v : ΞΉ β†’ ℝ) (hsupp : βˆ€ i, x i = 0 β†’ v i = 0)
{p : ΞΊ β†’ ℝ} (hp : p ∈ probabilitySimplex ΞΊ) (a : ΞΊ β†’ ℝ)
(hcenter : βˆ‘ i, p i*a i = 0) {m : ℝ} (hm : m β‰  0) (cβ‚€ c₁ : ℝ)
(hlocal : IsLocalMax (fun t => entropyBarrier m (entropy (fun i => x i+t*v i)) +
cβ‚€+t*c₁+t^2*(βˆ‘ i, p i*(a i)^2)-Real.log (partition p a t)) 0) :
(8*Real.exp (-entropy x/m)-1)*(βˆ‘ i, (v i)^2/x i) +
(8*Real.exp (-entropy x/m)/m)*(entropyLineD x v 0)^2 +
(βˆ‘ i, p i*(a i)^2) ≀ 0 := by
have hf : βˆ€αΆ  t : ℝ in 𝓝 0, HasDerivAt
(fun t => entropyBarrier m (entropy (fun i => x i+t*v i)))
(entropyBarrierD m (entropy (fun i => x i+t*v i))*entropyLineD x v t) t := by
filter_upwards [entropy_line_eventually_hasDerivAt x v hsupp] with t ht
exact (entropyBarrier_hasDerivAt hm _).comp t ht
have hg : βˆ€αΆ  t : ℝ in 𝓝 0, HasDerivAt (fun t => Real.log (partition p a t))
(partition (fun i => p i*a i) a t / partition p a t) t :=
Filter.Eventually.of_forall (logPartition_hasDerivAt hp a)
have hd := second_deriv_quadratic_adjustment (cβ‚€ := cβ‚€) (c₁ := c₁)
(v := βˆ‘ i, p i*(a i)^2) hf hg (entropy_barrier_line_second x v hsupp hm)
(logPartition_second hp a)
have hc : ContinuousAt (fun t => entropyBarrier m (entropy (fun i => x i+t*v i)) +
cβ‚€+t*c₁+t^2*(βˆ‘ i, p i*(a i)^2)-Real.log (partition p a t)) 0 := by
apply ContinuousAt.sub
Β· apply ContinuousAt.add
Β· apply ContinuousAt.add
Β· exact hf.self_of_nhds.continuousAt.add_const cβ‚€
Β· fun_prop
Β· fun_prop
Β· exact hg.self_of_nhds.continuousAt
have hh := second_deriv_nonpos_of_local_max hlocal hc
rw [hd.deriv, hcenter] at hh
nlinarith
end MatchingEntropy
end
end OAI