learn_first_differences

source

learn_first_differences(
    n_units=150,
    n_periods=2,
    beta=2.0,
    unit_effect_corr=0.8,
    noise_sd=0.5,
    seed=0,
)

Show that first differencing removes the unit effect — matching the within estimator.

Simulates a balanced panel y_it = beta * x_it + alpha_i + e_it where the regressor x is correlated with each unit’s fixed effect alpha_i (so pooled OLS is biased). Differencing (Δy on Δx) cancels alpha_i; on a two-period panel the first- differences estimate equals the within (demeaning) estimate, and both recover beta.

Parameters

Name Type Description Default
n_units int Panel dimensions. With n_periods=2 first differences and the within estimator coincide exactly; for more periods they differ slightly in finite samples. 150
n_periods int Panel dimensions. With n_periods=2 first differences and the within estimator coincide exactly; for more periods they differ slightly in finite samples. 150
beta float True within-unit slope. 2.0
unit_effect_corr float Correlation between x and the unit effect (drives the pooled bias). 0.8
noise_sd float Idiosyncratic noise standard deviation. 0.5
seed int Random seed. 0

Returns

Name Type Description
SandboxResult df (pooled vs first differences vs within vs true slope), fig, summary and topic.

Examples

This sandbox simulates its own two-period panel, so the call needs no DataFrame:

import expdpy as ex

res = ex.learn_first_differences()
res.fig