import Mathlib
import FoML


open scoped Topology

open MeasureTheory ProbabilityTheory BigOperators ENNReal Real Set Filter Asymptotics

theorem extracted_formal_statement_11.{u_1} {Ω : Type u_1} [inst : MeasurableSpace Ω] (μ : Measure Ω)
  [inst_1 : IsProbabilityMeasure μ] {n : ℕ} {X : Fin n → Ω → ℝ} (hXm : ∀ (i : Fin n), Measurable (X i))
  (hXi : iIndepFun X μ) (hX_bdd : ∀ (i : Fin n), ∀ᵐ (ω : Ω) ∂μ, |X i ω| ≤ 1)
  (hμX : ∀ (i : Fin n), ∫ (x : Ω), X i x ∂μ = 0) (σ : ℝ) (hσ : ∀ (i : Fin n), σ ^ 2 = ∫ (x : Ω), (X i ^ 2) x ∂μ)
  (ε τ : ℝ) (hτ : τ ≥ 0)
  (h_chernoff :
    Integrable (fun ω => rexp (τ * ∑ i, X i ω)) μ →
      (μ {ω | ε ≤ ∑ i, X i ω}).toReal ≤ rexp (-τ * ε) * mgf (fun ω => ∑ i, X i ω) μ τ)
  (h_integ : Integrable (fun ω => rexp (τ * ∑ i, X i ω)) μ)
  (h_ysum_mgf : ∀ (t : ℝ), mgf (fun ω => ∑ i, X i ω) μ t = ∏ i, mgf (X i) μ t)
  (chernoff_bound : (μ {ω | ε ≤ ∑ i, X i ω}).toReal ≤ rexp (-(τ * ε)) * ∏ i, mgf (X i) μ τ)
  (h_original_goal :
    rexp (-(τ * ε)) * ∏ i, ∫ (x : Ω), (fun ω => rexp (τ * X i ω)) x ∂μ ≤
      rexp (-(τ * ε) + ↑n * σ ^ 2 * (rexp τ - 1 - τ))) :
  rexp (-(τ * ε)) * ∏ x, ∫ (x_1 : Ω), rexp (τ * X x x_1) ∂μ ≤ rexp (-(τ * ε) + ↑n * σ ^ 2 * (rexp τ - 1 - τ)) := sorry

Tactic state before:
Ω : Type u_1
inst✝¹ : MeasurableSpace Ω
μ : Measure Ω
inst✝ : IsProbabilityMeasure μ
n : ℕ
X : Fin n → Ω → ℝ
hXm : ∀ (i : Fin n), Measurable (X i)
hXi : iIndepFun X μ
hX_bdd : ∀ (i : Fin n), ∀ᵐ (ω : Ω) ∂μ, |X i ω| ≤ 1
hμX : ∀ (i : Fin n), ∫ (x : Ω), X i x ∂μ = 0
σ : ℝ
hσ : ∀ (i : Fin n), σ ^ 2 = ∫ (x : Ω), (X i ^ 2) x ∂μ
ε τ : ℝ
hτ : τ ≥ 0
h_chernoff :
  Integrable (fun ω => rexp (τ * ∑ i, X i ω)) μ →
    (μ {ω | ε ≤ ∑ i, X i ω}).toReal ≤ rexp (-τ * ε) * mgf (fun ω => ∑ i, X i ω) μ τ
h_integ : Integrable (fun ω => rexp (τ * ∑ i, X i ω)) μ
h_ysum_mgf : ∀ (t : ℝ), mgf (fun ω => ∑ i, X i ω) μ t = ∏ i, mgf (X i) μ t
chernoff_bound : (μ {ω | ε ≤ ∑ i, X i ω}).toReal ≤ rexp (-(τ * ε)) * ∏ i, mgf (X i) μ τ
⊢ rexp (-(τ * ε)) * ∏ i, ∫ (x : Ω), (fun ω => rexp (τ * X i ω)) x ∂μ ≤ rexp (-(τ * ε) + ↑n * σ ^ 2 * (rexp τ - 1 - τ))

Tactic:
simp only
    have h_single : ∏ i, ∫ (ω : Ω), rexp (τ * X i ω) ∂μ ≤ rexp (n * σ ^ 2 * (rexp (τ) - 1 - τ)) :=
  by
  have this : ∀ i, ∫ (ω : Ω), rexp (τ * X i ω) ∂μ ≤ rexp (σ ^ 2 * (rexp (τ) - 1 - τ)) :=
    by
    have h_exp_taylor : ∀ i, ∀ᵐ ω ∂μ, rexp (τ * X i ω) ≤ 1 + τ * X i ω + (X i ω) ^ 2 * (rexp (τ) - 1 - τ) :=
      by
      intro i
      filter_upwards [hX_bdd i] with ω hω
      let x := X i ω
      have h_x_abs_le_1 : |x| ≤ 1 := by
        unfold x
        exact hω
      have h_exp_bound : rexp (τ * x) ≤ 1 + τ * x + (x) ^ 2 * (rexp (τ) - 1 - τ) := by
        exact exp_bound x τ h_x_abs_le_1 hτ
      simp only [h_x_abs_le_1, hτ] at h_exp_bound
      unfold x at h_exp_bound
      exact h_exp_bound
    intro i
    have h_single_integ1 : Integrable (f := fun ω => rexp (τ * X i ω)) (μ := μ) := by
      exact single_integrable1 μ hXm hX_bdd τ hτ i
    have h_single_integ2 : Integrable (f := fun ω => 1 + τ * X i ω + (X i ω) ^ 2 * (rexp (τ) - 1 - τ)) (μ := μ) := by
      exact single_integrable2 μ hXm hX_bdd τ i
    have h_exp_int : ∫ (ω : Ω), rexp (τ * X i ω) ∂μ ≤ ∫ (ω : Ω), 1 + τ * X i ω + (X i ω) ^ 2 * (rexp (τ) - 1 - τ) ∂μ :=
      by
      apply integral_mono_ae h_single_integ1 h_single_integ2
      exact h_exp_taylor i
    have h_int_decomp :
      ∫ (ω : Ω), 1 + τ * X i ω + X i ω ^ 2 * (rexp τ - 1 - τ) ∂μ =
        (∫ (ω : Ω), 1 ∂μ) + (∫ (ω : Ω), τ * X i ω ∂μ) + (∫ (ω : Ω), X i ω ^ 2 * (rexp τ - 1 - τ) ∂μ) :=
      by
      have h_const : Integrable (fun _ ↦ (1 : ℝ)) μ := integrable_const 1
      have hX_int : Integrable (fun ω ↦ X i ω) μ := by
        constructor
        · exact (hXm i).aestronglyMeasurable
        · have h_bound : ∀ᵐ ω ∂μ, ‖X i ω‖ ≤ 1 :=
            by
            filter_upwards [hX_bdd i] with ω hω
            exact hω
          apply hasFiniteIntegral_of_bounded (C := 1)
          exact h_bound
      have h_mul' := Integrable.const_mul hX_int τ
      have := Integrable.add' h_const h_mul'
      rw [integral_add]; rw [integral_add]
      simp only [h_const]
      simp only [h_mul']
      simp [<- Pi.add_apply]
      simp only [h_mul']
      have h_int : Integrable (fun ω ↦ X i ω ^ 2) μ := by
        constructor
        · exact (Measurable.pow (hXm i) (by measurability)).aestronglyMeasurable
        · have h_bound : ∀ᵐ a ∂μ, X i a ^ 2 ≤ 1 :=
            by
            filter_upwards [hX_bdd i] with a ha
            rw [sq_le_one_iff_abs_le_one]
            exact ha
          have h_lbound : ∀ᵐ a ∂μ, X i a ^ 2 ≥ 0 :=
            by
            filter_upwards [hX_bdd i] with a ha
            positivity
          have h_box : ∀ᵐ a ∂μ, X i a ^ 2 ∈ Set.Icc 0 1 :=
            by
            filter_upwards [h_bound, h_lbound] with a ha hlb
            simp only [Set.mem_Icc]
            constructor
            · exact hlb
            · exact ha
          apply HasFiniteIntegral.of_mem_Icc (a := 0) (b := 1) h_box
      have h_mul'' := Integrable.mul_const h_int (rexp τ - 1 - τ)
      exact h_mul''
    have h_tmp : ∫ (ω : Ω), τ * X i ω ∂μ = τ * (∫ (ω : Ω), X i ω ∂μ) := by rw [integral_mul_left]
    rw [h_tmp] at h_int_decomp
    rw [hμX i] at h_int_decomp
    simp only [mul_zero, add_zero] at h_int_decomp
    rw [h_int_decomp] at h_exp_int
    suffices ∫ (ω : Ω), 1 ∂μ + ∫ (ω : Ω), X i ω ^ 2 * (rexp τ - 1 - τ) ∂μ ≤ rexp (σ ^ 2 * (rexp τ - 1 - τ)) by linarith
    have hmu : ∫ (ω : Ω), (1 : ℝ) ∂μ = 1 := by simp [ENNReal.ofReal_mul, norm_eq_abs, abs_sq]
    rw [hmu, integral_mul_right, add_comm]
    have hmu2 : (∫ (a : Ω), X i a ^ 2 ∂μ) * (rexp τ - 1 - τ) = σ ^ 2 * (rexp τ - 1 - τ) :=
      by
      by_cases h : rexp τ - 1 - τ = 0
      · rw [h]
        simp [mul_zero]
      · suffices (∫ (a : Ω), X i a ^ 2 ∂μ) = σ ^ 2 by simp [h]; exact this
        have this := (hσ i)
        simp [this]
    rw [hmu2]
    suffices 1 + σ ^ 2 * (rexp τ - 1 - τ) ≤ rexp (σ ^ 2 * (rexp τ - 1 - τ)) by linarith
    have this := add_one_le_exp (σ ^ 2 * (rexp τ - 1 - τ))
    simp only [add_comm] at this
    exact this
  have h_nonneg : ∀ i, 0 ≤ ∫ ω, rexp (τ * X i ω) ∂μ := by
    intro i
    exact integral_nonneg (fun ω => (exp_pos _).le)
  have h_n : n * σ ^ 2 * (rexp τ - 1 - τ) = ∑ i : Fin n, σ ^ 2 * (rexp τ - 1 - τ) := by
    simp only [Finset.sum_const, Finset.card_univ, Fintype.card_fin, nsmul_eq_mul, mul_assoc];
  simp only [h_n]
  rw [exp_sum]
  let s : Finset (Fin n) := Finset.univ
  apply Finset.prod_le_prod (s := s)
  · intro i _
    exact integral_nonneg (fun ω => (exp_pos _).le)
  · intro i _
    exact this i
    have h_single_plus :
  rexp (-(τ * ε)) * ∏ i, ∫ (ω : Ω), rexp (τ * X i ω) ∂μ ≤ rexp (-(τ * ε) + n * σ ^ 2 * (rexp τ - 1 - τ)) :=
  by
  have this : (rexp (-(τ * ε))) ≥ 0 := by positivity
  rw [exp_add]
  nlinarith [this, h_single]

Tactic state after:
[Ω : Type u_1
 inst✝¹ : MeasurableSpace Ω
 μ : Measure Ω
 inst✝ : IsProbabilityMeasure μ
 n : ℕ
 X : Fin n → Ω → ℝ
 hXm : ∀ (i : Fin n), Measurable (X i)
 hXi : iIndepFun X μ
 hX_bdd : ∀ (i : Fin n), ∀ᵐ (ω : Ω) ∂μ, |X i ω| ≤ 1
 hμX : ∀ (i : Fin n), ∫ (x : Ω), X i x ∂μ = 0
 σ : ℝ
 hσ : ∀ (i : Fin n), σ ^ 2 = ∫ (x : Ω), (X i ^ 2) x ∂μ
 ε τ : ℝ
 hτ : τ ≥ 0
 h_chernoff :
   Integrable (fun ω => rexp (τ * ∑ i, X i ω)) μ →
     (μ {ω | ε ≤ ∑ i, X i ω}).toReal ≤ rexp (-τ * ε) * mgf (fun ω => ∑ i, X i ω) μ τ
 h_integ : Integrable (fun ω => rexp (τ * ∑ i, X i ω)) μ
 h_ysum_mgf : ∀ (t : ℝ), mgf (fun ω => ∑ i, X i ω) μ t = ∏ i, mgf (X i) μ t
 chernoff_bound : (μ {ω | ε ≤ ∑ i, X i ω}).toReal ≤ rexp (-(τ * ε)) * ∏ i, mgf (X i) μ τ
 h_single : ∏ i, ∫ (ω : Ω), rexp (τ * X i ω) ∂μ ≤ rexp (↑n * σ ^ 2 * (rexp τ - 1 - τ))
 h_single_plus : rexp (-(τ * ε)) * ∏ i, ∫ (ω : Ω), rexp (τ * X i ω) ∂μ ≤ rexp (-(τ * ε) + ↑n * σ ^ 2 * (rexp τ - 1 - τ))
 ⊢ rexp (-(τ * ε)) * ∏ x, ∫ (x_1 : Ω), rexp (τ * X x x_1) ∂μ ≤ rexp (-(τ * ε) + ↑n * σ ^ 2 * (rexp τ - 1 - τ))]