File size: 12,013 Bytes
e5a97c6 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 | 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
|