File size: 19,394 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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
import OAI.Combinatorics.MatchingEntropy.RefinedVariation

namespace OAI

noncomputable section

open scoped BigOperators Topology
open Filter Module Matrix

universe uV uE uI uJ uA uB uC uW uF uD uι

namespace MatchingEntropyBounds.Refined

variable {V : Type uV} {E : Type uE} [Fintype V] [Fintype E] [DecidableEq V] [DecidableEq E]

open LooplessGraph

theorem pointwise_entropy (G : LooplessGraph V E) (m : ℕ)
    (hm : 0 < m) (hV : Fintype.card V = 2 * m) (hPM : Nonempty G.Matching)
    (x : E → ℝ) (hx : x ∈ G.polytope) :
    entropy x - (2 - 2 / (m : ℝ)) * complementEntropy x ≤ maxMatchingEntropy G x ∧
      maxMatchingEntropy G x ≤ entropy x := by
  obtain ⟨_⟩ := hPM
  exact ⟨refined_lower G m hm hV x hx, refined_upper G x hx⟩

omit [Fintype V] [DecidableEq V] in
theorem F_nonneg (G : LooplessGraph V E) {x : E → ℝ} (hx : x ∈ G.polytope) :
    0 ≤ entropy x := by
  exact Finset.sum_nonneg (fun e _ => Real.negMulLog_nonneg
    (G.coordinate_mem_Icc hx e).1 (G.coordinate_mem_Icc hx e).2)

omit [Fintype V] [DecidableEq V] in
theorem B_nonneg (G : LooplessGraph V E) {x : E → ℝ} (hx : x ∈ G.polytope) :
    0 ≤ complementEntropy x := by
  apply Finset.sum_nonneg
  intro e _
  exact Real.negMulLog_nonneg (sub_nonneg.mpr (G.coordinate_mem_Icc hx e).2)
    (by linarith [(G.coordinate_mem_Icc hx e).1])

theorem B_le_order (G : LooplessGraph V E) (m : ℕ) (hV : Fintype.card V = 2 * m)
    {x : E → ℝ} (hx : x ∈ G.polytope) : complementEntropy x ≤ m := by
  rw [← G.marginal_sum hV hx]
  apply Finset.sum_le_sum
  intro e _
  simpa only [sub_sub_cancel] using
    Real.negMulLog_le_one_sub_self (sub_nonneg.mpr (G.coordinate_mem_Icc hx e).2)

theorem entropy_defect (G : LooplessGraph V E) (m : ℕ)
    (hm : 0 < m) (hV : Fintype.card V = 2 * m) (hPM : Nonempty G.Matching)
    (x : E → ℝ) (hx : x ∈ G.polytope) :
    0 ≤ entropy x - maxMatchingEntropy G x ∧
      entropy x - maxMatchingEntropy G x ≤ min (entropy x) (2 * (m : ℝ) - 2) := by
  have hb := pointwise_entropy G m hm hV hPM x hx
  have hmR : (0 : ℝ) < m := by exact_mod_cast hm
  have hm1 : (1 : ℝ) ≤ m := by exact_mod_cast hm
  have hc : 0 ≤ 2 - 2 / (m : ℝ) := by
    have hd : 2 / (m : ℝ) ≤ 2 := (div_le_iff₀ hmR).mpr (by linarith)
    linarith
  have heq : (2 - 2 / (m : ℝ)) * m = 2 * (m : ℝ) - 2 := by field_simp
  have hB := mul_le_mul_of_nonneg_left (B_le_order G m hV hx) hc
  rw [heq] at hB
  refine ⟨sub_nonneg.mpr hb.2, le_min ?_ ?_⟩
  · linarith [maxMatchingEntropy_nonneg G hx]
  · linarith [hb.1]

section UniformLaw
variable {I : Type uI} [Fintype I]

def uniformLaw (I : Type uI) [Fintype I] : I → ℝ := fun _ => (Fintype.card I : ℝ)⁻¹

theorem uniformLaw_simplex [Nonempty I] : uniformLaw I ∈ probabilitySimplex I := by
  have hN : (Fintype.card I : ℝ) ≠ 0 := by exact_mod_cast Fintype.card_ne_zero
  refine ⟨fun _ => inv_nonneg.mpr (Nat.cast_nonneg _), ?_⟩
  simp [uniformLaw, hN]

theorem uniformLaw_entropy [Nonempty I] : entropy (uniformLaw I) = Real.log (Fintype.card I : ℝ) := by
  have hN : (Fintype.card I : ℝ) ≠ 0 := by exact_mod_cast Fintype.card_ne_zero
  simp only [entropy, uniformLaw, Real.negMulLog, Real.log_inv, neg_mul_neg,
    Finset.sum_const, Finset.card_univ, nsmul_eq_mul]
  field_simp
end UniformLaw

omit [Fintype V] [DecidableEq V] in
theorem uniformMatching_mean (G : LooplessGraph V E) (hPM : Nonempty G.Matching) :
    ∃ z ∈ G.polytope, maxMatchingEntropy G z = Real.log (Fintype.card G.Matching : ℝ) := by
  let := hPM
  have hp := uniformLaw_simplex (I := G.Matching)
  have hz : G.mean (uniformLaw G.Matching) ∈ G.polytope := G.mean_mem_polytope hp
  refine ⟨_, hz, le_antisymm (maxMatchingEntropy_le_log_card G hz) ?_⟩
  rw [← uniformLaw_entropy (I := G.Matching)]
  exact entropy_le_maxMatchingEntropy G ⟨hp, rfl⟩

omit [Fintype V] [DecidableEq V] [DecidableEq E] in
theorem continuous_binaryEntropy : Continuous (binaryEntropy (E := E)) := by
  exact continuous_entropy.add (continuous_entropy.comp (by fun_prop))

theorem optimized_entropy (G : LooplessGraph V E) (m : ℕ)
    (hm : 0 < m) (hV : Fintype.card V = 2 * m) (hPM : Nonempty G.Matching) :
    ∃ x ∈ G.polytope, ∃ y ∈ G.polytope,
      (∀ z ∈ G.polytope, entropy z ≤ entropy x) ∧
      (∀ z ∈ G.polytope, binaryEntropy z ≤ binaryEntropy y) ∧
      Real.log (Fintype.card G.Matching : ℝ) ≤ entropy x ∧
      entropy x ≤ Real.log (Fintype.card G.Matching : ℝ) + 2 * (m : ℝ) - 2 ∧
      Real.log (Fintype.card G.Matching : ℝ) ≤ binaryEntropy y ∧
      binaryEntropy y ≤ Real.log (Fintype.card G.Matching : ℝ) + 3 * (m : ℝ) - 2 := by
  obtain ⟨z, hz, heq⟩ := uniformMatching_mean G hPM
  have hn : G.polytope.Nonempty := ⟨z, hz⟩
  obtain ⟨x, hx, hmax⟩ := UpperSemicontinuousOn.exists_isMaxOn hn G.compact_polytope
    continuous_entropy.continuousOn.upperSemicontinuousOn
  obtain ⟨y, hy, hmaxb⟩ := UpperSemicontinuousOn.exists_isMaxOn hn G.compact_polytope
    continuous_binaryEntropy.continuousOn.upperSemicontinuousOn
  have hxdef := (entropy_defect G m hm hV hPM x hx).2.trans (min_le_right _ _)
  have hydef := (entropy_defect G m hm hV hPM y hy).2.trans (min_le_right _ _)
  have hxu := maxMatchingEntropy_le_log_card G hx
  have hyu := maxMatchingEntropy_le_log_card G hy
  have hzu := refined_upper G z hz
  have hzB := B_nonneg G hz
  have hyB := B_le_order G m hV hy
  have hzx : entropy z ≤ entropy x := hmax hz
  have hzy := hmaxb hz
  change entropy z + complementEntropy z ≤ entropy y + complementEntropy y at hzy
  refine ⟨x, hx, y, hy, hmax, hmaxb, ?_, ?_, ?_, ?_⟩
  · linarith
  · linarith
  · change _ ≤ entropy y + complementEntropy y
    linarith
  · change entropy y + complementEntropy y ≤ _
    linarith

def countBound (m k : ℕ) (N : ℝ) : Prop :=
  (k = 1 → 1 ≤ N) ∧
  (2 ≤ k → (k : ℝ)^m * (1 - 1 / (k : ℝ))^(2 * (m - 1) * (k - 1)) ≤ N ∧
    Real.exp (-2 * ((m : ℝ) - 1)) * (k : ℝ)^m ≤
      (k : ℝ)^m * (1 - 1 / (k : ℝ))^(2 * (m - 1) * (k - 1)) ∧
    (Real.exp (-2) * (k : ℝ))^m ≤ Real.exp (-2 * ((m : ℝ) - 1)) * (k : ℝ)^m)

omit [Fintype V] [DecidableEq V] in
theorem matching_nonempty_of_mem (G : LooplessGraph V E) {x : E → ℝ} (hx : x ∈ G.polytope) :
    Nonempty G.Matching := by
  obtain ⟨p, hp⟩ := (G.mem_polytope_iff x).mp hx
  by_contra h
  have : IsEmpty G.Matching := not_nonempty_iff.mp h
  simpa using hp.1.2

theorem entropy_uniform (G : LooplessGraph V E) {m k : ℕ}
    (hV : Fintype.card V = 2 * m) (hk : 2 ≤ k) {x : E → ℝ} (hx : x ∈ G.polytope)
    (hu : ∀ e, x e = 0 ∨ x e = 1 / (k : ℝ)) :
    entropy x = (m : ℝ) * Real.log (k : ℝ) ∧
    complementEntropy x = -(m : ℝ) * ((k : ℝ) - 1) * Real.log (1 - 1 / (k : ℝ)) := by
  have hkR : (0 : ℝ) < k := by exact_mod_cast (show 0 < k by omega)
  have ha (e : E) : Real.negMulLog (x e) = x e * Real.log (k : ℝ) := by
    rcases hu e with he | he
    · simp [he]
    · rw [he, one_div, Real.negMulLog, Real.log_inv]; ring
  have hb (e : E) : Real.negMulLog (1 - x e) =
      -((k : ℝ) - 1) * x e * Real.log (1 - 1 / (k : ℝ)) := by
    rcases hu e with he | he
    · simp [he]
    · rw [he, Real.negMulLog]; field_simp
  constructor
  · simp_rw [entropy, ha]
    rw [← Finset.sum_mul, G.marginal_sum hV hx]
  · simp_rw [complementEntropy, entropy, hb]
    rw [← Finset.sum_mul, ← Finset.mul_sum, G.marginal_sum hV hx]
    ring

omit [Fintype V] [Fintype E] [DecidableEq V] [DecidableEq E] in
theorem finiteCount_eq_exp {m k : ℕ} (hm : 0 < m) (hk : 2 ≤ k) :
    (k : ℝ)^m * (1 - 1 / (k : ℝ))^(2 * (m - 1) * (k - 1)) =
      Real.exp ((m : ℝ) * Real.log (k : ℝ) +
        2 * ((m : ℝ) - 1) * ((k : ℝ) - 1) * Real.log (1 - 1 / (k : ℝ))) := by
  have hkR : (1 : ℝ) < k := by exact_mod_cast hk
  have ht : 0 < 1 - 1 / (k : ℝ) := sub_pos.mpr ((div_lt_one (by linarith)).mpr hkR)
  have hc : 2 * ((m : ℝ) - 1) * ((k : ℝ) - 1) = ((2 * (m - 1) * (k - 1) : ℕ) : ℝ) := by
    rw [Nat.cast_mul, Nat.cast_mul, Nat.cast_sub (by omega), Nat.cast_sub (by omega)]
    norm_num
  rw [hc, Real.exp_add, Real.exp_nat_mul, Real.exp_nat_mul, Real.exp_log (by linarith), Real.exp_log ht]

omit [Fintype V] [Fintype E] [DecidableEq V] [DecidableEq E] in
theorem log_uniform_loss {k : ℕ} (hk : 2 ≤ k) :
    -1 ≤ ((k : ℝ) - 1) * Real.log (1 - 1 / (k : ℝ)) := by
  have hkR : (1 : ℝ) < k := by exact_mod_cast hk
  have hk0 : (0 : ℝ) < k := by linarith
  have ht : 0 < 1 - 1 / (k : ℝ) := sub_pos.mpr ((div_lt_one hk0).mpr hkR)
  have hb := Real.log_le_sub_one_of_pos (inv_pos.mpr ht)
  rw [Real.log_inv] at hb
  have hkm : 0 < (k : ℝ) - 1 := by linarith
  have heq : ((1 - 1 / (k : ℝ))⁻¹ - 1) * ((k : ℝ) - 1) = 1 := by
    field_simp; ring
  have hh := mul_le_mul_of_nonneg_right hb (by linarith : 0 ≤ (k : ℝ) - 1)
  rw [heq] at hh
  nlinarith

theorem refined_uniform_count (G : LooplessGraph V E) {m k : ℕ}
    (hm : 0 < m) (hV : Fintype.card V = 2 * m) {x : E → ℝ} (hx : x ∈ G.polytope)
    (hu : ∀ e, x e = 0 ∨ x e = 1 / (k : ℝ)) :
    countBound m k (Fintype.card G.Matching : ℝ) := by
  have hPM := matching_nonempty_of_mem G hx
  have hN : (0 : ℝ) < Fintype.card G.Matching := by
    exact_mod_cast Fintype.card_pos_iff.mpr hPM
  refine ⟨fun _ => by exact_mod_cast Fintype.card_pos_iff.mpr hPM, fun hk => ?_⟩
  have hk0 : (0 : ℝ) < k := by exact_mod_cast (show 0 < k by omega)
  have hm0 : (0 : ℝ) < m := by exact_mod_cast hm
  have hm1 : (1 : ℝ) ≤ m := by exact_mod_cast hm
  obtain ⟨hF, hB⟩ := entropy_uniform G hV hk hx hu
  have hl := (pointwise_entropy G m hm hV hPM x hx).1.trans (maxMatchingEntropy_le_log_card G hx)
  rw [hF, hB] at hl
  have hl' : (m : ℝ) * Real.log (k : ℝ) +
      2 * ((m : ℝ) - 1) * ((k : ℝ) - 1) * Real.log (1 - 1 / (k : ℝ)) ≤
      Real.log (Fintype.card G.Matching : ℝ) := by
    convert hl using 1
    first | rfl | (field_simp; ring)
  refine ⟨?_, ?_, ?_⟩
  · rw [finiteCount_eq_exp hm hk]
    simpa only [Real.exp_log hN] using Real.exp_le_exp.mpr hl'
  · rw [finiteCount_eq_exp hm hk]
    have heq : Real.exp (-2 * ((m : ℝ) - 1)) * (k : ℝ)^m =
        Real.exp (-2 * ((m : ℝ) - 1) + (m : ℝ) * Real.log (k : ℝ)) := by
      rw [Real.exp_add, Real.exp_nat_mul, Real.exp_log hk0]
    rw [heq]
    apply Real.exp_le_exp.mpr
    have hh := mul_le_mul_of_nonneg_left (log_uniform_loss hk) (by linarith : 0 ≤ 2 * ((m : ℝ) - 1))
    nlinarith
  · rw [mul_pow, ← Real.exp_nat_mul]
    apply mul_le_mul_of_nonneg_right _ (pow_nonneg hk0.le _)
    apply Real.exp_le_exp.mpr
    linarith

theorem structural_count (G : LooplessGraph V E) (m k : ℕ)
    (hm : 0 < m) (hV : Fintype.card V = 2 * m) (hk : 1 ≤ k)
    (M : Fin k → G.Matching)
    (hd : ∀ i j, i ≠ j → Disjoint (M i).val (M j).val) :
    countBound m k (Fintype.card G.Matching : ℝ) := by
  obtain ⟨x, hx, hu⟩ := selected_uniform_mean G hk M hd
  exact refined_uniform_count G hm hV hx hu

theorem regular_count (G : LooplessGraph V E) (m k : ℕ)
    (hm : 0 < m) (hV : Fintype.card V = 2 * m) (hk : 1 ≤ k)
    (hregular : ∀ v, (Finset.univ.filter (G.Incident v)).card = k)
    (hodd : ∀ S : Finset V, Odd S.card → k ≤ (G.cut S).card) :
    countBound m k (Fintype.card G.Matching : ℝ) := by
  exact refined_uniform_count G hm hV (regular_uniform_mem G hV hk hregular hodd) (fun _ => Or.inr rfl)

omit [Fintype V] [DecidableEq E] in

theorem degree_cut_count (G : LooplessGraph V E) {k : ℕ}
    (hreg : ∀ v, (Finset.univ.filter (G.Incident v)).card = k) (S : Finset V) :
    S.card * k = 2 * (Finset.univ.filter (fun e => G.left e ∈ S ∧ G.right e ∈ S)).card +
      (G.cut S).card := by
  classical
  have hp (e : E) : ((if G.left e ∈ S then (1 : ℕ) else 0) +
      (if G.right e ∈ S then 1 else 0)) =
      2 * (if G.left e ∈ S ∧ G.right e ∈ S then 1 else 0) +
        (if G.Crosses S e then 1 else 0) := by
    by_cases hl : G.left e ∈ S <;> by_cases hr : G.right e ∈ S <;> simp [Crosses, hl, hr]
  calc
    S.card * k = ∑ v ∈ S, (∑ e : E, if G.Incident v e then (1 : ℕ) else 0) := by
      simp [Finset.sum_boole, hreg, Finset.sum_const]
    _ = ∑ e : E, ∑ v ∈ S, if G.Incident v e then (1 : ℕ) else 0 := Finset.sum_comm
    _ = _ := by
      simp_rw [G.incidence_sum, hp]
      rw [Finset.sum_add_distrib, ← Finset.mul_sum]
      simp [Finset.sum_boole, cut]

omit [DecidableEq E] in

theorem connected_odd_cut (G : LooplessGraph V E) {m k : ℕ}
    (hV : Fintype.card V = 2 * m) (hk : 1 ≤ k)
    (hregular : ∀ v, (Finset.univ.filter (G.Incident v)).card = k)
    (hconn : ∀ S : Finset V, S.Nonempty → S ≠ Finset.univ → k - 1 ≤ (G.cut S).card)
    (S : Finset V) (hS : Odd S.card) : k ≤ (G.cut S).card := by
  have hsmod := Nat.odd_iff.mp hS
  have hn : S.Nonempty := Finset.card_pos.mp (by omega)
  have hp : S ≠ Finset.univ := by
    intro hh
    simp only [hh, Finset.card_univ, hV] at hsmod
    omega
  have hc := hconn S hn hp
  have heq := congrArg (fun a : ℕ => a % 2) (degree_cut_count G hregular S)
  rw [Nat.mul_mod S.card k 2, hsmod] at heq
  simp only [one_mul, Nat.mod_mod] at heq
  omega

theorem connected_regular_count (G : LooplessGraph V E) (m k : ℕ)
    (hm : 0 < m) (hV : Fintype.card V = 2 * m) (hk : 1 ≤ k)
    (hregular : ∀ v, (Finset.univ.filter (G.Incident v)).card = k)
    (hconn : ∀ S : Finset V, S.Nonempty → S ≠ Finset.univ → k - 1 ≤ (G.cut S).card) :
    countBound m k (Fintype.card G.Matching : ℝ) := by
  exact regular_count G m k hm hV hk hregular (connected_odd_cut G hV hk hregular hconn)

def weightedPartition (G : LooplessGraph V E) (β : E → ℝ) : ℝ :=
  ∑ M : G.Matching, (2 : ℝ) ^ (∑ e, β e * G.indicator M e)
def weightedObjective (β x : E → ℝ) : ℝ := entropy x / Real.log 2 + ∑ e, β e * x e

def gibbsLaw (G : LooplessGraph V E) (β : E → ℝ) (M : G.Matching) : ℝ :=
  (2 : ℝ) ^ (∑ e, β e * G.indicator M e) / weightedPartition G β

omit [Fintype V] [DecidableEq V] in
theorem weightedPartition_pos (G : LooplessGraph V E) (hPM : Nonempty G.Matching) (β : E → ℝ) :
    0 < weightedPartition G β := by
  let := hPM
  exact Finset.sum_pos (fun M _ => Real.rpow_pos_of_pos (by norm_num) _) Finset.univ_nonempty

omit [Fintype V] [DecidableEq V] in
theorem gibbsLaw_pos (G : LooplessGraph V E) (hPM : Nonempty G.Matching) (β : E → ℝ) (M : G.Matching) :
    0 < gibbsLaw G β M :=
  div_pos (Real.rpow_pos_of_pos (by norm_num) _) (weightedPartition_pos G hPM β)

omit [Fintype V] [DecidableEq V] in
theorem gibbsLaw_simplex (G : LooplessGraph V E) (hPM : Nonempty G.Matching) (β : E → ℝ) :
    gibbsLaw G β ∈ probabilitySimplex G.Matching := by
  refine ⟨fun M => (gibbsLaw_pos G hPM β M).le, ?_⟩
  simp only [gibbsLaw, ← Finset.sum_div]
  exact div_self (weightedPartition_pos G hPM β).ne'

omit [Fintype V] [DecidableEq V] in
theorem gibbs_cross_entropy (G : LooplessGraph V E) (hPM : Nonempty G.Matching) (β : E → ℝ)
    {p : G.Matching → ℝ} (hp : p ∈ probabilitySimplex G.Matching) :
    -(∑ M, p M * Real.log (gibbsLaw G β M)) =
      Real.log (weightedPartition G β) - Real.log 2 * (∑ e, β e * G.mean p e) := by
  have hl (M : G.Matching) : Real.log (gibbsLaw G β M) =
      (∑ e, β e * G.indicator M e) * Real.log 2 - Real.log (weightedPartition G β) := by
    rw [gibbsLaw, Real.log_div (ne_of_gt (Real.rpow_pos_of_pos (by norm_num) _))
      (weightedPartition_pos G hPM β).ne', Real.log_rpow (by norm_num)]
  simp_rw [hl, mul_sub, ← mul_assoc]
  rw [Finset.sum_sub_distrib, ← Finset.sum_mul, ← Finset.sum_mul, hp.2, one_mul, sum_mean_assoc]
  ring

omit [Fintype V] [DecidableEq V] in
theorem gibbs_law_bound (G : LooplessGraph V E) (hPM : Nonempty G.Matching) (β : E → ℝ)
    {p : G.Matching → ℝ} (hp : p ∈ probabilitySimplex G.Matching) :
    entropy p / Real.log 2 + (∑ e, β e * G.mean p e) ≤
      Real.log (weightedPartition G β) / Real.log 2 := by
  have h := entropy_le_crossEntropy (gibbsLaw_simplex G hPM β) hp
    (fun M hzero => ((gibbsLaw_pos G hPM β M).ne' hzero).elim)
  rw [gibbs_cross_entropy G hPM β hp] at h
  have hlog : 0 < Real.log 2 := Real.log_pos (by norm_num)
  apply (le_div_iff₀ hlog).mpr
  rw [add_mul, div_mul_cancel₀ _ hlog.ne']
  nlinarith

omit [Fintype V] [DecidableEq V] in
theorem gibbs_law_equality (G : LooplessGraph V E) (hPM : Nonempty G.Matching) (β : E → ℝ) :
    entropy (gibbsLaw G β) / Real.log 2 + (∑ e, β e * G.mean (gibbsLaw G β) e) =
      Real.log (weightedPartition G β) / Real.log 2 := by
  have h := gibbs_cross_entropy G hPM β (gibbsLaw_simplex G hPM β)
  rw [← entropy_eq] at h
  rw [h]
  have hlog : Real.log 2 ≠ 0 := (Real.log_pos (by norm_num : (1 : ℝ) < 2)).ne'
  field_simp
  ring

omit [Fintype V] [DecidableEq V] in

theorem gibbs_variational (G : LooplessGraph V E) (hPM : Nonempty G.Matching) (β : E → ℝ) :
    gibbsLaw G β ∈ probabilitySimplex G.Matching ∧
    maxMatchingEntropy G (G.mean (gibbsLaw G β)) / Real.log 2 +
        (∑ e, β e * G.mean (gibbsLaw G β) e) = Real.log (weightedPartition G β) / Real.log 2 ∧
    ∀ x ∈ G.polytope, maxMatchingEntropy G x / Real.log 2 + (∑ e, β e * x e) ≤
      Real.log (weightedPartition G β) / Real.log 2 := by
  have hp := gibbsLaw_simplex G hPM β
  have hx := G.mean_mem_polytope hp
  have hu : ∀ x ∈ G.polytope, maxMatchingEntropy G x / Real.log 2 + (∑ e, β e * x e) ≤
      Real.log (weightedPartition G β) / Real.log 2 := by
    intro x hx
    obtain ⟨p, hp, hopt⟩ := maxMatchingEntropy_attained G hx
    rw [← hopt, ← hp.2]
    exact gibbs_law_bound G hPM β hp.1
  refine ⟨hp, le_antisymm (hu _ hx) ?_, hu⟩
  rw [← gibbs_law_equality G hPM β]
  exact add_le_add (div_le_div_of_nonneg_right (entropy_le_maxMatchingEntropy G ⟨hp, rfl⟩)
    (Real.log_pos (by norm_num)).le) le_rfl

theorem weighted_count (G : LooplessGraph V E) (m : ℕ)
    (hm : 0 < m) (hV : Fintype.card V = 2 * m) (hPM : Nonempty G.Matching)
    (β : E → ℝ) :
    ∃ x ∈ G.polytope, (∀ y ∈ G.polytope, weightedObjective β y ≤ weightedObjective β x) ∧
      weightedObjective β x - (2 * (m : ℝ) - 2) / Real.log 2 ≤
        Real.log (weightedPartition G β) / Real.log 2 ∧
      Real.log (weightedPartition G β) / Real.log 2 ≤ weightedObjective β x := by
  obtain ⟨hp, heq, hu⟩ := gibbs_variational G hPM β
  have hz := G.mean_mem_polytope hp
  have hcont : Continuous (weightedObjective β) := by
    unfold weightedObjective
    exact (continuous_entropy.div_const _).add (by fun_prop)
  obtain ⟨x, hx, hmax⟩ := UpperSemicontinuousOn.exists_isMaxOn ⟨_, hz⟩ G.compact_polytope
    hcont.continuousOn.upperSemicontinuousOn
  have hlog : 0 < Real.log 2 := Real.log_pos (by norm_num)
  refine ⟨x, hx, hmax, ?_, ?_⟩
  · have hd := (entropy_defect G m hm hV hPM x hx).2.trans (min_le_right _ _)
    have hd' := div_le_div_of_nonneg_right hd hlog.le
    have huu := hu x hx
    dsimp only [weightedObjective]
    rw [sub_div] at hd'
    linarith
  · rw [← heq]
    apply le_trans _ (hmax hz)
    exact add_le_add (div_le_div_of_nonneg_right (refined_upper G _ hz) hlog.le) le_rfl

end MatchingEntropyBounds.Refined

end

end OAI