module

public import SubentropyCertificates.AncillaReplicatedFactor
public import Mathlib.Analysis.Convex.SpecificFunctions.Basic

/-! An integrable majorant for the centered, replicated determinant spectrum. -/
@[expose] public section
noncomputable section
open scoped BigOperators
namespace SubentropyCertificates.AncillaLimit

private theorem sum_le_product {I : Type*} (s : Finset I) (v : I → ℝ) (hv : ∀ i, 0 ≤ v i) :
    1 + ∑ i ∈ s, v i ≤ ∏ i ∈ s, (1 + v i) := by
  classical
  induction s using Finset.induction_on with
  | empty => simp
  | @insert i s hi ih =>
    rw [Finset.sum_insert hi, Finset.prod_insert hi]
    have hcross := mul_nonneg (hv i) (Finset.sum_nonneg (fun j (_ : j ∈ s) ↦ hv j))
    calc
      _ ≤ (1 + v i) * (1 + ∑ j ∈ s, v j) := by nlinarith
      _ ≤ _ := mul_le_mul_of_nonneg_left ih (by linarith [hv i])

private theorem scalar_envelope (t : ℝ) (ht : 0 < t) (n : ℕ) (hn : 2 ≤ n) :
    (1 + t) ^ 2 ≤ 4 * (1 + t / n) ^ n := by
  have hN : (2 : ℝ) ≤ n := by exact_mod_cast hn
  have hNpos : (0 : ℝ) < n := by linarith
  have hx : 0 < 1 + t / n := by positivity
  have hhalf := one_add_mul_self_le_rpow_one_add (s := t / n) (p := (n : ℝ) / 2)
    (by linarith [div_pos ht hNpos]) (by linarith)
  have heq : (n : ℝ) / 2 * (t / n) = t / 2 := by field_simp
  rw [heq] at hhalf
  have hpow : ((1 + t / n) ^ ((n : ℝ) / 2)) ^ (2 : ℕ) = (1 + t / n) ^ n := by
    rw [← Real.rpow_natCast, ← Real.rpow_mul hx.le]
    simp
  have hsq := pow_le_pow_left₀ (by linarith : 0 ≤ 1 + t / 2) hhalf 2
  rw [hpow] at hsq
  nlinarith

/-- For dimension at least two, the centered scaled factor is bounded by a single
integrable function, uniformly in the dimension and in the probability spectrum. -/
theorem replicatedFactor_centered_bound {I : Type*} [Fintype I] (w : I → ℝ)
    (hw : ∀ i, 0 ≤ w i) (hs : ∑ i, w i = 1) {k : ℕ} (hk : 1 ≤ k) {t : ℝ} (ht : 0 < t) :
    |((k + 1 : ℕ) : ℝ) * (replicatedFactor w k t - Real.exp (-t)) / t ^ 2| ≤
      4 / (1 + t) ^ 2 := by
  let n := k + 1
  let x := t / (n : ℝ)
  let P := ∏ i, (1 + t * w i / (n : ℝ))
  have hn : 2 ≤ n := by dsimp [n]; omega
  have hnpos : (0 : ℝ) < n := by exact_mod_cast (show 0 < n by omega)
  have hx : 0 < x := div_pos ht hnpos
  have hnx : (n : ℝ) * x = t := by dsimp [x]; field_simp
  have hv i : 0 ≤ t * w i / (n : ℝ) := by positivity [hw i]
  have hsum : ∑ i, t * w i / (n : ℝ) = x := by
    rw [← Finset.sum_div, ← Finset.mul_sum, hs, mul_one]
  have hprod : 1 + x ≤ P := by
    simpa only [P, hsum] using sum_le_product Finset.univ (fun i ↦ t * w i / (n : ℝ)) hv
  have hP : P ≤ Real.exp x := by
    rw [← hsum, Real.exp_sum]
    dsimp only [P]
    exact Finset.prod_le_prod₀ (fun i _ ↦ by linarith [hv i]) (fun i _ ↦ by
      simpa [add_comm] using Real.add_one_le_exp (t * w i / (n : ℝ)))
  have h1x : 0 < 1 + x := by linarith
  have hPpos : 0 < P := h1x.trans_le hprod
  have hlow : Real.exp (-t) ≤ replicatedFactor w k t := by
    change Real.exp (-t) ≤ P⁻¹ ^ n
    rw [← hnx, ← mul_neg, Real.exp_nat_mul, Real.exp_neg]
    exact pow_le_pow_left₀ (by positivity) (inv_anti₀ hPpos hP) _
  let F := (1 + x)⁻¹ ^ n
  have hF : replicatedFactor w k t ≤ F :=
    pow_le_pow_left₀ (by positivity : 0 ≤ P⁻¹) (inv_anti₀ h1x hprod) n
  have hFpos : 0 < F := by dsimp [F]; positivity
  let D := t - (n : ℝ) * Real.log (1 + x)
  have hid : F * Real.exp (-D) = Real.exp (-t) := by
    have hFexp : F = Real.exp (-((n : ℝ) * Real.log (1 + x))) := by
      dsimp [F]
      rw [Real.exp_neg, Real.exp_nat_mul, Real.exp_log h1x, inv_pow]
    rw [hFexp, ← Real.exp_add]
    congr 1
    dsimp [D]
    ring
  have hlog : x - Real.log (1 + x) ≤ x ^ 2 := by
    have hh := Real.one_sub_inv_le_log_of_pos h1x
    have hi : x - x ^ 2 ≤ 1 - (1 + x)⁻¹ := by
      calc
        _ ≤ x / (1 + x) := by
          apply (le_div_iff₀ h1x).2
          nlinarith [pow_nonneg hx.le 3]
        _ = _ := by field_simp; ring
    linarith
  have hD : (n : ℝ) * D ≤ t ^ 2 := by
    have h := mul_le_mul_of_nonneg_left hlog (sq_nonneg (n : ℝ))
    dsimp [D]
    rw [← hnx]
    nlinarith only [h]
  have hdiff : F - Real.exp (-t) ≤ F * D := by
    rw [← hid]
    nlinarith only [mul_le_mul_of_nonneg_left (Real.one_sub_le_exp_neg D) hFpos.le]
  have hb : (n : ℝ) * (replicatedFactor w k t - Real.exp (-t)) / t ^ 2 ≤ F := by
    apply (div_le_iff₀ (sq_pos_of_pos ht)).2
    calc
      _ ≤ (n : ℝ) * (F * D) := mul_le_mul_of_nonneg_left ((sub_le_sub_right hF _).trans hdiff) hnpos.le
      _ = F * ((n : ℝ) * D) := by ring
      _ ≤ _ := mul_le_mul_of_nonneg_left hD hFpos.le
  rw [abs_of_nonneg (div_nonneg (mul_nonneg hnpos.le (sub_nonneg.mpr hlow)) (sq_nonneg t))]
  apply hb.trans
  change (1 + x)⁻¹ ^ n ≤ _
  rw [inv_pow, inv_eq_one_div, div_le_div_iff₀ (pow_pos h1x n) (by positivity)]
  simpa only [one_mul] using scalar_envelope t ht n hn

end SubentropyCertificates.AncillaLimit

