File size: 20,461 Bytes
b317718
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import OAI.Probability.InvariantIsing.Arrays.NSpinTensorFlatMean
import OAI.Probability.InvariantIsing.Arrays.TensorProfiles
import OAI.Probability.InvariantIsing.Arrays.PerturbationWeights
import OAI.Probability.IsingPerceptron.VarianceDerivative

namespace OAI

/-! A selected actual spectral/tree monomial as an independent Gaussian factor. -/

noncomputable section

open MeasureTheory ProbabilityTheory IsingPerceptron
open scoped BigOperators NNReal

namespace InvariantIsing

private abbrev TensorGaussianKey {N m k : ℕ} (I : Fin m → Finset (Fin N))
    (degree : Fin k → Fin m → ℕ) := ℕ × List ChildLabel × SpinTensorIndex I degree

/-- Namespace zero contains the linear field; namespace `j+1` contains
precisely the `j`th spectral tensor at all ancestor levels. -/
def tensorNamespaceTag {N m k : ℕ} (I : Fin m → Finset (Fin N))
    (degree : Fin k → Fin m → ℕ) (q : TensorGaussianKey I degree) : ℕ :=
  match q.2.2 with
  | Sum.inl a => Nat.pair 0 (Encodable.encode (q.1, q.2.1, a))
  | Sum.inr a => Nat.pair (a.1 + 1) (Encodable.encode (q.1, q.2.1, a.2))

lemma tensorNamespaceTag_injective {N m k : ℕ} (I : Fin m → Finset (Fin N))
    (degree : Fin k → Fin m → ℕ) : Function.Injective (tensorNamespaceTag I degree) := by
  rintro ⟨i, p, a⟩ ⟨j, q, b⟩ h
  cases a with
  | inl a =>
    cases b with
    | inl b =>
      have he := Encodable.encode_inj.mp (Nat.pair_eq_pair.mp h).2
      cases he
      rfl
    | inr b =>
      have he := (Nat.pair_eq_pair.mp h).1
      omega
  | inr a =>
    cases b with
    | inl b =>
      have he := (Nat.pair_eq_pair.mp h).1
      omega
    | inr b =>
      rcases a with ⟨a, s⟩
      rcases b with ⟨b, t⟩
      have hp := Nat.pair_eq_pair.mp h
      have hab : a = b := Fin.ext (Nat.add_right_cancel hp.1)
      subst b
      have he := Encodable.encode_inj.mp hp.2
      cases he
      rfl

def tensorNamespacedCoefficients {N m k : ℕ} (U : Rotation N)
    (I : Fin m → Finset (Fin N)) (degree : Fin k → Fin m → ℕ) (amplitude : Fin k → ℝ)
    (n : ℕ) (v : Fin (n + 1) → SpinTensorIndex I degree → ℝ≥0)
    (x : Spin N × LabeledLeaf n) : ℕ →₀ ℝ :=
  featureCoefficients (fun i : Fin (n + 1) × SpinTensorIndex I degree =>
    tensorNamespaceTag I degree (i.1, (labeledAddress n x.2).take i.1, i.2))
    (fun i => (NNReal.sqrt (v i.1 i.2) : ℝ) * spinTensorFeature U I degree amplitude x.1 i.2)

/-- Injective Gaussian reindexing identifies the entire countable
spin/leaf field law with the flat tensor model. -/
theorem tensorNamespacedFields_law {N m k : ℕ} (U : Rotation N)
    (I : Fin m → Finset (Fin N)) (degree : Fin k → Fin m → ℕ) (amplitude : Fin k → ℝ)
    (n : ℕ) (v : Fin (n + 1) → SpinTensorIndex I degree → ℝ≥0) :
    gaussianCoordinates.map (fun g x => cylinderField (tensorLeafCoefficients U I degree amplitude n v x) g) =
      gaussianCoordinates.map (fun g x => cylinderField (tensorNamespacedCoefficients U I degree amplitude n v x) g) := by
  let Φ := fun (G : TensorGaussianKey I degree → ℝ) (x : Spin N × LabeledLeaf n) =>
    ∑ i : Fin (n + 1), ∑ a : SpinTensorIndex I degree,
      (NNReal.sqrt (v i a) : ℝ) * spinTensorFeature U I degree amplitude x.1 a *
        G (i, (labeledAddress n x.2).take i, a)
  have hmΦ : Measurable Φ := by
    apply Measurable.of_eval
    intro x
    exact Finset.measurable_sum _ fun i _ => Finset.measurable_sum _ fun a _ =>
      (measurable_pi_apply (i.1, (labeledAddress n x.2).take i, a)).const_mul _
  have he : (fun g x => cylinderField (tensorLeafCoefficients U I degree amplitude n v x) g) =
      Φ ∘ (fun g : ℕ → ℝ => fun q : TensorGaussianKey I degree => g (Encodable.encode q)) := by
    funext g x
    rw [tensorLeafCoefficients, cylinderField_feature, Fintype.sum_prod_type]
    rfl
  have hn : (fun g x => cylinderField (tensorNamespacedCoefficients U I degree amplitude n v x) g) =
      Φ ∘ (fun g : ℕ → ℝ => fun q => g (tensorNamespaceTag I degree q)) := by
    funext g x
    rw [tensorNamespacedCoefficients, cylinderField_feature, Fintype.sum_prod_type]
    rfl
  have hp := gaussian_pullback_measurePreserving
    (Encodable.encode : TensorGaussianKey I degree → ℕ) Encodable.encode_injective
  have hq := gaussian_pullback_measurePreserving
    (tensorNamespaceTag I degree) (tensorNamespaceTag_injective I degree)
  rw [he, hn, ← Measure.map_map hmΦ hp.measurable, ← Measure.map_map hmΦ hq.measurable,
    hp.map_eq, hq.map_eq]

private lemma sqrt_varianceIncrement {q : ℕ → ℝ} (hq : Monotone q) (h0 : 0 ≤ q 0) (i : ℕ) :
    (NNReal.sqrt (varianceIncrement q i) : ℝ) = pathAmplitude q i := by
  rw [Real.coe_sqrt, varianceIncrement_coe hq h0]
  rfl

def tensorLinearCoefficients {N : ℕ} (n : ℕ) (h : ℕ → ℝ)
    (x : Spin N × LabeledLeaf n) : ℕ →₀ ℝ :=
  treeFieldCoefficients n x.2 (fun i => pathAmplitude h i) (fun i => spinValue (x.1 i))

/-- The reindexed field is exactly the sum of the linear enrichment and
the displayed independent spectral/tree Gaussian monomials. -/
theorem tensorNamespacedField_expand {N m k : ℕ} (U : Rotation N)
    (I : Fin m → Finset (Fin N)) (degree : Fin k → Fin m → ℕ) (amplitude : Fin k → ℝ)
    (n : ℕ) (treeDegree : Fin k → ℕ) (h : ℕ → ℝ) (hh : Monotone h) (h0 : 0 ≤ h 0)
    (x : Spin N × LabeledLeaf n) (g : ℕ → ℝ) :
    cylinderField (tensorNamespacedCoefficients U I degree amplitude n
      (fun i => tensorPathProfile I degree n treeDegree h i) x) g =
      cylinderField (tensorLinearCoefficients n h x) (fun i => g (Nat.pair 0 i)) +
        ∑ j, amplitude j * cylinderField
          (jointSpectralMonomialCoefficients U I (degree j) n (treeDegree j) x)
          (fun i => g (Nat.pair (j + 1) i)) := by
  rw [tensorNamespacedCoefficients, cylinderField_feature, Fintype.sum_prod_type]
  simp_rw [Fintype.sum_sum_type, Fintype.sum_sigma]
  simp only [tensorPathProfile, tensorVarianceProfile, spinTensorFeature, tensorNamespaceTag]
  simp_rw [sqrt_varianceIncrement hh h0,
    sqrt_varianceIncrement (monomialPath_monotone n _) (monomialPath_nonneg n _ 0)]
  rw [Finset.sum_add_distrib]
  congr 1
  · rw [tensorLinearCoefficients, treeFieldCoefficients, cylinderField_feature, Fintype.sum_prod_type]
    rfl
  · rw [Finset.sum_comm]
    apply Finset.sum_congr rfl
    intro j _
    rw [jointSpectralMonomialCoefficients, treeFieldCoefficients, cylinderField_feature,
      Fintype.sum_prod_type, Finset.mul_sum]
    apply Finset.sum_congr rfl
    intro i _
    rw [Finset.mul_sum]
    apply Finset.sum_congr rfl
    intro a _
    simp only [treeFeatureTag]
    ring

lemma tensorNamespacedField_frozen {N m k : ℕ} (U : Rotation N)
    (I : Fin m → Finset (Fin N)) (degree : Fin k → Fin m → ℕ) (amplitude : Fin k → ℝ)
    (n : ℕ) (treeDegree : Fin k → ℕ) (h : ℕ → ℝ) (hh : Monotone h) (h0 : 0 ≤ h 0)
    (j : Fin k) (x : Spin N × LabeledLeaf n) (z : ℕ → ℝ) (g : OtherNamespaces (j + 1)) :
    cylinderField (tensorNamespacedCoefficients U I degree (Function.update amplitude j 0) n
      (fun i => tensorPathProfile I degree n treeDegree h i) x)
      (gaussianNamespaceJoin (j + 1) (z, g)) =
    cylinderField (tensorNamespacedCoefficients U I degree (Function.update amplitude j 0) n
      (fun i => tensorPathProfile I degree n treeDegree h i) x)
      (gaussianNamespaceJoin (j + 1) (0, g)) := by
  classical
  simp_rw [tensorNamespacedField_expand U I degree _ n treeDegree h hh h0]
  congr 1
  · congr 1
    funext i
    exact (gaussianNamespaceJoin_other (j + 1) z g ⟨0, by omega⟩ i).trans
      (gaussianNamespaceJoin_other (j + 1) 0 g ⟨0, by omega⟩ i).symm
  · apply Finset.sum_congr rfl
    intro a _
    by_cases ha : a = j
    · subst a
      simp
    · simp only [Function.update_of_ne ha]
      congr 1
      congr 1
      funext i
      have hv : (a : ℕ) + 1 ≠ (j : ℕ) + 1 := fun he => ha (Fin.ext (Nat.add_right_cancel he))
      exact (gaussianNamespaceJoin_other (j + 1) z g ⟨a + 1, hv⟩ i).trans
        (gaussianNamespaceJoin_other (j + 1) 0 g ⟨a + 1, hv⟩ i).symm

/-- Inserting the selected standard Gaussian family adds exactly its
actual amplitude times `jointSpectralMonomialCoefficients`. -/
theorem tensorNamespacedField_insert {N m k : ℕ} (U : Rotation N)
    (I : Fin m → Finset (Fin N)) (degree : Fin k → Fin m → ℕ) (amplitude : Fin k → ℝ)
    (n : ℕ) (treeDegree : Fin k → ℕ) (h : ℕ → ℝ) (hh : Monotone h) (h0 : 0 ≤ h 0)
    (j : Fin k) (x : Spin N × LabeledLeaf n) (z : ℕ → ℝ) (g : OtherNamespaces (j + 1)) :
    cylinderField (tensorNamespacedCoefficients U I degree amplitude n
      (fun i => tensorPathProfile I degree n treeDegree h i) x)
      (gaussianNamespaceJoin (j + 1) (z, g)) =
    cylinderField (tensorNamespacedCoefficients U I degree (Function.update amplitude j 0) n
      (fun i => tensorPathProfile I degree n treeDegree h i) x)
      (gaussianNamespaceJoin (j + 1) (0, g)) +
        amplitude j * cylinderField (jointSpectralMonomialCoefficients U I (degree j) n (treeDegree j) x) z := by
  classical
  rw [← tensorNamespacedField_frozen U I degree amplitude n treeDegree h hh h0 j x z g]
  simp_rw [tensorNamespacedField_expand U I degree _ n treeDegree h hh h0]
  let Y := fun a : Fin k => cylinderField
    (jointSpectralMonomialCoefficients U I (degree a) n (treeDegree a) x)
    (fun i => gaussianNamespaceJoin (j + 1) (z, g) (Nat.pair (a + 1) i))
  have he (a : Fin k) : amplitude a * Y a = Function.update amplitude j 0 a * Y a +
      if a = j then amplitude j * Y j else 0 := by
    by_cases ha : a = j
    · subst a; simp
    · simp [ha]
  change _ + ∑ a, amplitude a * Y a = _
  have hsum : (∑ a, amplitude a * Y a) =
      ∑ a, (Function.update amplitude j 0 a * Y a + if a = j then amplitude j * Y j else 0) :=
    Finset.sum_congr rfl (fun a _ => he a)
  rw [hsum, Finset.sum_add_distrib]
  simp only [Finset.sum_ite_eq', Finset.mem_univ, ite_true]
  have hY : Y j = cylinderField (jointSpectralMonomialCoefficients U I (degree j) n (treeDegree j) x) z := by
    simp only [Y, gaussianNamespaceJoin_selected]
  rw [hY]
  ring
lemma measurable_tensorNamespacedFields_joint {N m k : ℕ}
    (I : Fin m → Finset (Fin N)) (degree : Fin k → Fin m → ℕ) (amplitude : Fin k → ℝ)
    (n : ℕ) (v : Fin (n + 1) → SpinTensorIndex I degree → ℝ≥0)
    (x : Spin N × LabeledLeaf n) :
    Measurable (fun p : SpecialOrthogonal N × (ℕ → ℝ) =>
      cylinderField (tensorNamespacedCoefficients (specialRotation p.1) I degree amplitude n v x) p.2) := by
  simp only [tensorNamespacedCoefficients, cylinderField_feature]
  apply Finset.measurable_sum
  intro i _
  exact (((measurable_spinTensorFeature I degree amplitude x.1 i.2).comp measurable_fst).const_mul _).mul
    ((measurable_pi_apply (tensorNamespaceTag I degree (i.1.1, (labeledAddress n x.2).take i.1, i.2))).comp measurable_snd)

/-- The full finite-volume Gibbs-law distribution is preserved by the
Gaussian namespace reindexing, for each actual rotation and cascade prior. -/
theorem tensorNamespacedGibbs_law {N m k : ℕ} (eig : Fin N → ℝ) (U : Rotation N) (c : Fin N → ℝ)
    (I : Fin m → Finset (Fin N)) (degree : Fin k → Fin m → ℕ) (amplitude : Fin k → ℝ)
    (n : ℕ) (v : Fin (n + 1) → SpinTensorIndex I degree → ℝ≥0) (T : LabeledTree n) :
    let ν := labeledSpinReference n (uniformSpinPrior N : Measure (Spin N)) T
    let H := fun x : Spin N × LabeledLeaf n => rotatedEnergy eig U x.1 + fieldEnergy c x.1
    gaussianCoordinates.map (fun g => gibbsProbability ν
      (fun x => H x + cylinderField (tensorLeafCoefficients U I degree amplitude n v x) g)) =
    gaussianCoordinates.map (fun g => gibbsProbability ν
      (fun x => H x + cylinderField (tensorNamespacedCoefficients U I degree amplitude n v x) g)) := by
  intro ν H
  let Kern := fun F : (Spin N × LabeledLeaf n → ℝ) => gibbsProbability ν (fun x => H x + F x)
  have : ∀ F : (Spin N × LabeledLeaf n → ℝ), IsProbabilityMeasure ((fun _ => ν) F) :=
    fun _ => by
      change IsProbabilityMeasure (labeledSpinReference n (uniformSpinPrior N : Measure (Spin N)) T)
      infer_instance
  have hE : Measurable (fun p : (Spin N × LabeledLeaf n → ℝ) × (Spin N × LabeledLeaf n) => H p.2 + p.1 p.2) := by
    apply measurable_from_prod_countable_left
    intro x
    have hc : Measurable (fun _ : (Spin N × LabeledLeaf n → ℝ) => H x) := measurable_const
    exact hc.add (measurable_pi_apply x)
  have hmKern : Measurable Kern := measurable_gibbsProbability (ν := fun _ : (Spin N × LabeledLeaf n → ℝ) => ν)
    (H := fun p => H p.2 + p.1 p.2) measurable_const hE
  have hleft : Measurable (fun g x => cylinderField (tensorLeafCoefficients U I degree amplitude n v x) g) :=
    Measurable.of_eval (fun x => measurable_cylinderField _)
  have hright : Measurable (fun g x => cylinderField (tensorNamespacedCoefficients U I degree amplitude n v x) g) :=
    Measurable.of_eval (fun x => measurable_cylinderField _)
  change gaussianCoordinates.map (Kern ∘ (fun g x => cylinderField (tensorLeafCoefficients U I degree amplitude n v x) g)) =
    gaussianCoordinates.map (Kern ∘ (fun g x => cylinderField (tensorNamespacedCoefficients U I degree amplitude n v x) g))
  rw [← Measure.map_map hmKern hleft, ← Measure.map_map hmKern hright, tensorNamespacedFields_law]

abbrev TensorFrozenData (N n : ℕ) (j : ℕ) := (SpecialOrthogonal N × LabeledTree n) × OtherNamespaces (j + 1)

def tensorFrozenLaw {N : ℕ} (μ : Measure (SpecialOrthogonal N)) (n : ℕ) (b : ℕ → ℝ) (j : ℕ) :
    Measure (TensorFrozenData N n j) :=
  (μ.prod (labeledCascadeLaw n b : Measure (LabeledTree n))).prod (otherNamespacesLaw (j + 1))

instance tensorFrozenLaw_probability {N : ℕ} (μ : Measure (SpecialOrthogonal N)) [IsProbabilityMeasure μ]
    (n : ℕ) (b : ℕ → ℝ) (j : ℕ) : IsProbabilityMeasure (tensorFrozenLaw μ n b j) := by
  unfold tensorFrozenLaw
  infer_instance

def tensorFrozenBaseEnergy {N m k : ℕ} (eig c : Fin N → ℝ)
    (I : Fin m → Finset (Fin N)) (degree : Fin k → Fin m → ℕ) (amplitude : Fin k → ℝ)
    (n : ℕ) (treeDegree : Fin k → ℕ) (h : ℕ → ℝ) (j : Fin k)
    (ω : TensorFrozenData N n j) (x : Spin N × LabeledLeaf n) : ℝ :=
  rotatedEnergy eig (specialRotation ω.1.1) x.1 + fieldEnergy c x.1 +
    cylinderField (tensorNamespacedCoefficients (specialRotation ω.1.1) I degree
      (Function.update amplitude j 0) n (fun i => tensorPathProfile I degree n treeDegree h i) x)
      (gaussianNamespaceJoin (j + 1) (0, ω.2))

def tensorFrozenReference {N m k : ℕ} (eig c : Fin N → ℝ)
    (I : Fin m → Finset (Fin N)) (degree : Fin k → Fin m → ℕ) (amplitude : Fin k → ℝ)
    (n : ℕ) (treeDegree : Fin k → ℕ) (h : ℕ → ℝ) (j : Fin k)
    (ω : TensorFrozenData N n j) : Measure (Spin N × LabeledLeaf n) :=
  gibbsProbability (labeledSpinReference n (uniformSpinPrior N : Measure (Spin N)) ω.1.2)
    (tensorFrozenBaseEnergy eig c I degree amplitude n treeDegree h j ω)

instance tensorFrozenReference_probability {N m k : ℕ} (eig c : Fin N → ℝ)
    (I : Fin m → Finset (Fin N)) (degree : Fin k → Fin m → ℕ) (amplitude : Fin k → ℝ)
    (n : ℕ) (treeDegree : Fin k → ℕ) (h : ℕ → ℝ) (j : Fin k) (ω : TensorFrozenData N n j) :
    IsProbabilityMeasure (tensorFrozenReference eig c I degree amplitude n treeDegree h j ω) :=
  gibbsProbability_probability _ _

lemma measurable_tensorFrozenBaseEnergy {N m k : ℕ} (eig c : Fin N → ℝ)
    (I : Fin m → Finset (Fin N)) (degree : Fin k → Fin m → ℕ) (amplitude : Fin k → ℝ)
    (n : ℕ) (treeDegree : Fin k → ℕ) (h : ℕ → ℝ) (j : Fin k) :
    Measurable (Function.uncurry (tensorFrozenBaseEnergy eig c I degree amplitude n treeDegree h j)) := by
  change Measurable (fun p : TensorFrozenData N n j × (Spin N × LabeledLeaf n) =>
    tensorFrozenBaseEnergy eig c I degree amplitude n treeDegree h j p.1 p.2)
  apply measurable_from_prod_countable_left
  intro x
  change Measurable (fun ω : TensorFrozenData N n j =>
    rotatedEnergy eig (specialRotation ω.1.1) x.1 + fieldEnergy c x.1 +
      cylinderField (tensorNamespacedCoefficients (specialRotation ω.1.1) I degree
        (Function.update amplitude j 0) n (fun i => tensorPathProfile I degree n treeDegree h i) x)
        (gaussianNamespaceJoin (j + 1) (0, ω.2)))
  have hrot0 : Measurable (fun U : SpecialOrthogonal N => rotatedEnergy eig (specialRotation U) x.1) := by
    unfold rotatedEnergy
    exact (Finset.measurable_sum _ fun i _ =>
      ((measurable_specialRotation_eval (spinVector x.1) i).pow_const 2).const_mul (eig i)).const_mul (1 / 2 : ℝ)
  have hrot : Measurable (fun ω : TensorFrozenData N n j => rotatedEnergy eig (specialRotation ω.1.1) x.1) :=
    hrot0.comp measurable_fst.fst
  have hm : Measurable (fun ω : TensorFrozenData N n j =>
      (ω.1.1, gaussianNamespaceJoin (j + 1) (0, ω.2))) :=
    measurable_fst.fst.prodMk ((measurable_gaussianNamespaceJoin (j + 1)).comp
      (measurable_const.prodMk measurable_snd))
  let v : Fin (n + 1) → SpinTensorIndex I degree → ℝ≥0 :=
    fun i => tensorPathProfile I degree n treeDegree h i
  have hfield0 := measurable_tensorNamespacedFields_joint I degree
    (Function.update amplitude j 0) n v x
  have hfield : Measurable (fun ω : TensorFrozenData N n j =>
      cylinderField (tensorNamespacedCoefficients (specialRotation ω.1.1) I degree
        (Function.update amplitude j 0) n v x) (gaussianNamespaceJoin (j + 1) (0, ω.2))) := by
    convert hfield0.comp hm using 1
    rfl
  convert (hrot.add_const (fieldEnergy c x.1)).add hfield using 1

lemma measurable_tensorFrozenReference {N m k : ℕ} (eig c : Fin N → ℝ)
    (I : Fin m → Finset (Fin N)) (degree : Fin k → Fin m → ℕ) (amplitude : Fin k → ℝ)
    (n : ℕ) (treeDegree : Fin k → ℕ) (h : ℕ → ℝ) (j : Fin k) :
    Measurable (tensorFrozenReference eig c I degree amplitude n treeDegree h j) := by
  have : ∀ ω : TensorFrozenData N n j, IsProbabilityMeasure
      ((labeledSpinReference n (uniformSpinPrior N : Measure (Spin N)) ∘ (fun ω : TensorFrozenData N n j => ω.1.2)) ω) :=
    fun ω => by
      change IsProbabilityMeasure (labeledSpinReference n (uniformSpinPrior N : Measure (Spin N)) ω.1.2)
      infer_instance
  exact measurable_gibbsProbability
    (H := Function.uncurry (tensorFrozenBaseEnergy eig c I degree amplitude n treeDegree h j))
    ((measurable_labeledSpinReference_general n (uniformSpinPrior N : Measure (Spin N))).comp measurable_fst.snd)
    (measurable_tensorFrozenBaseEnergy eig c I degree amplitude n treeDegree h j)

theorem tensorFrozenEnergy_insert {N m k : ℕ} (eig c : Fin N → ℝ)
    (I : Fin m → Finset (Fin N)) (degree : Fin k → Fin m → ℕ) (amplitude : Fin k → ℝ)
    (n : ℕ) (treeDegree : Fin k → ℕ) (h : ℕ → ℝ) (hh : Monotone h) (h0 : 0 ≤ h 0)
    (j : Fin k) (ω : TensorFrozenData N n j) (z : ℕ → ℝ) (x : Spin N × LabeledLeaf n) :
    rotatedEnergy eig (specialRotation ω.1.1) x.1 + fieldEnergy c x.1 +
      cylinderField (tensorNamespacedCoefficients (specialRotation ω.1.1) I degree amplitude n
        (fun i => tensorPathProfile I degree n treeDegree h i) x)
        (gaussianNamespaceJoin (j + 1) (z, ω.2)) =
      tensorFrozenBaseEnergy eig c I degree amplitude n treeDegree h j ω x +
        amplitude j * cylinderField (jointSpectralMonomialCoefficients
          (specialRotation ω.1.1) I (degree j) n (treeDegree j) x) z := by
  rw [tensorNamespacedField_insert (specialRotation ω.1.1) I degree amplitude n treeDegree h hh h0 j x z ω.2]
  exact (add_assoc _ _ _).symm

/-- The selected field is a genuinely independent Gaussian product factor;
all remaining marks, the rotation, and the tree form external disorder. -/
theorem tensorFrozenInsertion_measurePreserving {N : ℕ} (μ : Measure (SpecialOrthogonal N))
    [IsProbabilityMeasure μ] (n : ℕ) (b : ℕ → ℝ) (j : ℕ) :
    MeasurePreserving (fun p : TensorFrozenData N n j × (ℕ → ℝ) =>
      (p.1.1, gaussianNamespaceJoin (j + 1) (p.2, p.1.2)))
      ((tensorFrozenLaw μ n b j).prod gaussianCoordinates)
      ((μ.prod (labeledCascadeLaw n b : Measure (LabeledTree n))).prod gaussianCoordinates) :=
  namespaceInsertion_preserving _ _

end InvariantIsing

end

end OAI