Panel.FixedEffect
Fixed-effect projection algebra: the within transformation and its annihilator properties.
FixedEffect 2 core · 0 supporting This file defines the two-way fixed-effect subspace of panel arrays as the set of unit-plus-period additive functions. ★ H_twfe_eq★ const_mem_H_twfe
Two-Way Fixed Effects
This file defines the two-way fixed-effect subspace of panel arrays as the set of unit-plus-period additive functions. It connects the panel notation to the generic additive-span infrastructure and records basic membership and finite-dimensionality facts.
The two-way fixed-effect subspace H_twfe equals the generic two-axis additive span over units and periods.
Formal statement
Proof (Lean source)
For any constant c₀, the constant array c₀ belongs to the two-way fixed-effect subspace H_twfe.
Formal statement
Proof (Lean source)
IndicatorClosedForms 6 core · 7 supporting This file proves closed-form two-way fixed-effect residuals for structured indicator arrays, including unit-only, period-only, rectangular, cohort-period, diagonal, and triangular indicators. ★ H_twfe_orthogonal_iff★ tildeX_product_indicator_balanced★ tildeX_cell_indicator_cohortPeriod★ tildeX_diagonal_indicator_cohortPeriod★ tildeX_triangular_indicator_cohortPeriod
Indicator Residual Closed Forms
This file proves closed-form two-way fixed-effect residuals for structured
indicator arrays, including unit-only, period-only, rectangular, cohort-period,
diagonal, and triangular indicators. It also provides the exported
row-and-column orthogonality criterion H_twfe_orthogonal_iff, the finite
cohort law CohortLaw, and the generic cohort-period weighted panel
construction cohortPeriodCells, so downstream files can reuse the algebra
without importing a specialized regression instance.
For a panel weighting scheme and any panel array g, g is orthogonal, under the weighted inner product, to every member of the two-way fixed-effect subspace H_twfe exactly when it is orthogonal to every unit indicator and every period indicator.
Formal statement
Proof (Lean source)
For a subset of units S_I, a subset of periods S_T, and an evaluation cell r, under balanced (equal) weights, the two-way-fixed-effect residual of the product indicator 1{i ∈ S_I}·1{t ∈ S_T}, evaluated at r, factorizes into the product of the centered unit indicator and the centered period indicator.
Formal statement
Proof (Lean source)
Cohort law on a finite cohort index Fin C: probabilities π(g) ≥ 0 summing to one.
For a target cohort g₀, a target period t₀, and an evaluation cell r, the cohort-period-weighted two-way-fixed-effect residual of the cell indicator 1{g = g₀, t = t₀}, evaluated at r, equals the product of centered cohort and period indicators minus the corresponding cross terms fixed by the gauge normalization ∑_t β(t) = π(g₀).
Formal statement
Proof (Lean source)
For an evaluation cell r, the cohort-period-weighted two-way-fixed-effect residual of the diagonal (switch-on) indicator 1{g.val = t.val}, evaluated at r, equals the indicator minus the cohort-conditional mean, minus the period-conditional mean net of the grand mean.
Formal statement
Proof (Lean source)
For an evaluation cell r, the cohort-period-weighted two-way-fixed-effect residual of the triangular (continued-treatment) indicator 1{g.val < t.val}, evaluated at r, equals the indicator minus the cohort-conditional mean, minus the period-conditional mean net of the grand mean.
Formal statement
Proof (Lean source)
7 supporting declarations (lemmas, instances)
-
sum_centered_indicator_mem_reallemma — Centering the indicator of a finite subset by its uniform average makes its sum across the index set equal to zero.hypothesesα :S :Finset αconclusion(∑ x : α, ((if x ∈ S then (1 : ℝ) else 0) - (S.card : ℝ) / (Fintype.card α : ℝ))) = 0Proof (Lean source)
lemma sum_centered_indicator_mem_real {α : Type*} [Fintype α] [DecidableEq α] (S : Finset α) : (∑ x : α, ((if x ∈ S then (1 : ℝ) else 0) - (S.card : ℝ) / (Fintype.card α : ℝ))) = 0 := by rcases isEmpty_or_nonempty α with hα | hα · letI := hα simp · letI := hα rw [Finset.sum_sub_distrib, sum_indicator_mem_real] rw [Finset.sum_const, Finset.card_univ, nsmul_eq_mul] have hcard : (Fintype.card α : ℝ) ≠ 0 := by have : 0 < Fintype.card α := Fintype.card_pos exact_mod_cast this.ne' field_simp [hcard] ring -
tildeX_consttheorem — The TWFE residual of a constant array vanishes on every observed cell.hypothesesconclusionc.tildeX Cells.H_twfe (fun _ : I × T => k) r = 0Proof (Lean source)
theorem tildeX_const (c : Cells I T) (k : ℝ) (r : I × T) (hr : r ∈ c.observed) : c.tildeX Cells.H_twfe (fun _ : I × T => k) r = 0 := by -- PROOF: constants belong to `H_twfe` (`const_mem_H_twfe`), so by -- `residualize_self_of_mem` the residual vanishes on observed cells. exact c.residualize_self_of_mem Cells.H_twfe (Cells.const_mem_H_twfe k) r hr -
tildeX_unit_indicatortheorem — Unit indicator is absorbed by H_twfe: ind(i = i₀) lies in H_twfe (via a(i) := ind(i = i₀), b(t) := 0), so its residual vanishes on every observed cell. Independent of the weights.hypothesesconclusionc.tildeX Cells.H_twfe (fun s : I × T => if s.1 = i₀ then (1 : ℝ) else 0) r = 0Proof (Lean source)
theorem tildeX_unit_indicator (c : Cells I T) (i₀ : I) (r : I × T) (hr : r ∈ c.observed) : c.tildeX Cells.H_twfe (fun s : I × T => if s.1 = i₀ then (1 : ℝ) else 0) r = 0 := by apply c.residualize_self_of_mem Cells.H_twfe ?_ r hr exact ⟨fun i => (if i = i₀ then 1 else 0 : ℝ), fun _ => 0, by intro p; simp⟩ -
tildeX_period_indicatortheorem — Period indicator is absorbed by H_twfe: ind(t = t₀) lies in H_twfe (via a := 0, b(t) := ind(t = t₀)), so its residual vanishes on every observed cell. Independent of the weights.hypothesesconclusionc.tildeX Cells.H_twfe (fun s : I × T => if s.2 = t₀ then (1 : ℝ) else 0) r = 0Proof (Lean source)
theorem tildeX_period_indicator (c : Cells I T) (t₀ : T) (r : I × T) (hr : r ∈ c.observed) : c.tildeX Cells.H_twfe (fun s : I × T => if s.2 = t₀ then (1 : ℝ) else 0) r = 0 := by apply c.residualize_self_of_mem Cells.H_twfe ?_ r hr exact ⟨fun _ => 0, fun t => (if t = t₀ then 1 else 0 : ℝ), by intro p; simp⟩ -
cohort_sum_pi_centered_eq_zerolemma — A singleton indicator centered by the selected category's weight has a zero weighted average whenever the weights across categories sum to one.hypothesesconclusion(∑ g : α, w g * ((if g = g₀ then (1 : ℝ) else 0) - w g₀)) = 0Proof (Lean source)
lemma cohort_sum_pi_centered_eq_zero {α : Type*} [Fintype α] [DecidableEq α] (w : α → ℝ) (hw : ∑ g, w g = 1) (g₀ : α) : (∑ g : α, w g * ((if g = g₀ then (1 : ℝ) else 0) - w g₀)) = 0 := by rw [show (∑ g : α, w g * ((if g = g₀ then (1 : ℝ) else 0) - w g₀)) = (∑ g : α, w g * (if g = g₀ then (1 : ℝ) else 0)) - ∑ g : α, w g * w g₀ by rw [← Finset.sum_sub_distrib] refine Finset.sum_congr rfl ?_ intro g hg ring] rw [show (∑ x : α, w x * if x = g₀ then 1 else 0) = w g₀ by simpa [mul_comm] using (Fintype.sum_ite_eq (i := g₀) (f := fun g : α => w g)).symm] rw [show (∑ g : α, w g * w g₀) = (∑ g : α, w g) * w g₀ by rw [Finset.sum_mul]] rw [hw] ring -
cohortPeriod_ip_unit_eqlemma — In a cohort-by-period panel with cohort-period weights, the weighted inner product of any outcome array with a cohort's unit indicator equals that cohort's weighted sum of outcomes across periods.hypothesesProof (Lean source)
lemma cohortPeriod_ip_unit_eq (law : CohortLaw C) (hpi : ∀ g : Fin C, 0 < (law.pi g : ℝ)) (Y : V (Fin C) (Fin S)) (g₁ : Fin C) : (cohortPeriodCells law hpi).ip Y (fun r : Fin C × Fin S => if r.1 = g₁ then (1 : ℝ) else 0) = ((law.pi g₁ : ℝ) / (card (Fin S) : ℝ)) * ∑ t : Fin S, Y (g₁, t) := by unfold ip simp only [cohortPeriodCells, Finset.mem_univ, ite_mul, one_mul, zero_mul, mul_ite, mul_one, mul_zero, ↓reduceIte] rw [Fintype.sum_prod_type] rw [show (∑ x : Fin C, ∑ t : Fin S, if x = g₁ then (law.pi x : ℝ) / (card (Fin S) : ℝ) * Y (x, t) else 0) = ∑ t : Fin S, (law.pi g₁ : ℝ) / (card (Fin S) : ℝ) * Y (g₁, t) by simpa [eq_comm] using (Fintype.sum_ite_eq (i := g₁) (f := fun x : Fin C => ∑ t : Fin S, (law.pi x : ℝ) / (card (Fin S) : ℝ) * Y (x, t))).symm] rw [Finset.mul_sum] -
cohortPeriod_ip_period_eqlemma — In a cohort-by-period panel with cohort-period weights, the weighted inner product of any outcome array with a period indicator equals the cohort-weighted sum of outcomes in that period.hypothesesProof (Lean source)
lemma cohortPeriod_ip_period_eq (law : CohortLaw C) (hpi : ∀ g : Fin C, 0 < (law.pi g : ℝ)) (Y : V (Fin C) (Fin S)) (t₁ : Fin S) : (cohortPeriodCells law hpi).ip Y (fun r : Fin C × Fin S => if r.2 = t₁ then (1 : ℝ) else 0) = (1 / (card (Fin S) : ℝ)) * ∑ g : Fin C, (law.pi g : ℝ) * Y (g, t₁) := by unfold ip simp only [cohortPeriodCells, Finset.mem_univ, ite_mul, one_mul, zero_mul, mul_ite, mul_one, mul_zero, ↓reduceIte] rw [Fintype.sum_prod_type] rw [show (∑ g : Fin C, ∑ t : Fin S, if t = t₁ then (law.pi g : ℝ) / (card (Fin S) : ℝ) * Y (g, t) else 0) = ∑ g : Fin C, (law.pi g : ℝ) / (card (Fin S) : ℝ) * Y (g, t₁) by refine Finset.sum_congr rfl ?_ intro g _ simpa [eq_comm] using (Fintype.sum_ite_eq (i := t₁) (f := fun t : Fin S => (law.pi g : ℝ) / (card (Fin S) : ℝ) * Y (g, t))).symm] rw [Finset.mul_sum] refine Finset.sum_congr rfl ?_ intro g _ ring