Marx Max中文
Menu

OLS sampling

Open in Marx Max

Use a simulated model with true slope 2 to compare a single OLS estimate and repeated sampling at n=30 and n=300. Download executed Python code, simulated data, 1,000 slope estimates, figures and rerun instructions.

Simulate y=1+2x+error with independent standard-normal x and error, seed 42. Compare a single n=30 estimate with 500 independent samples each at n=30 and n=300, retaining the generating model, 1,000 slopes, data, summaries, actual logs and figures. Read README.md and download the Python package into reference/. Retain reference outputs, check available dependencies and direct this run into rerun/. Check numerical output and use a separate directory to change seed. Simulation quantiles are not confidence intervals for one regression and this teaching model does not establish real-world findings.
Left: simulated observations, true line and OLS fit. Right: slope distributions from 500 independent samples each at n=30 and n=300.
The left panel estimates one sample; the right panel describes estimates across 1,000 samples. They answer different questions.

Download the complete OLS teaching package (ZIP)

If the true slope is 2, why does one regression not return exactly 2? This teaching exercise uses entirely simulated data with a known truth to compare one estimate and repeated sampling. All figures and numbers come from the executed companion program, which you can rerun with another seed.

Read the econometrics teaching guide: OLS, coefficients and MCMC

What stays fixed, and what changes?

The generating model is y = 1 + 2x + ε. Both x and ε are independent standard normals, and observations are independent. The true intercept is 1 and slope is 2; the seed is 42. Draw 500 independent samples each at n=30 and n=300, displaying the first n=30 sample as a single OLS example. Smaller samples are not prefixes of the larger ones, and repeated regressions do not reuse one dataset.

One estimate and two sampling distributions

The single sample of 30 observations yields an intercept of about 1.116 and slope of about 1.854. Deviation from the truth alone does not establish a programming or model error. Check the program and then use repeated sampling to inspect variability.

Sample sizeRepetitionsMean slopeSD of slopesEmpirical 2.5%–97.5% quantiles
305001.9970.1921.625–2.393
3005002.0010.0591.889–2.115

In this run, both means are close to 2 and the n=300 distribution is narrower. The SD describes variability across sampled coefficients, not the standard error reported by one regression; empirical quantiles are not that regression’s 95% confidence interval. Not every larger sample must yield an estimate closer to the truth.

The exercise assumes independent observations, homoskedastic errors and zero conditional error mean. Larger samples do not automatically repair endogeneity, measurement error or misspecification, and simulation does not establish identification assumptions in real research.

Download and rerun

  1. Extract into a fresh practice directory and retain a reference copy. Follow the README to prepare Python, NumPy and Matplotlib. Installing dependencies accesses a package repository; analysis itself is offline.
  2. Run python analyze.py and check the completion marker, 30 single-sample rows, 1,000 slope rows and both summaries. The same parameters and dependency environment reproduce numbers; image bytes need not match.
  3. Change only the seed first, then increase repetitions, writing exercises to new directories. Compare individual estimates and distributions: more repetitions and more observations per sample are different changes.

The program only generates simulated data and overwrites named files under the package’s results/ by default. It was executed and rerun in a fresh directory on macOS with Python 3.11.11; Windows hardware is not covered. In Marx Max, use a separate project and discuss actual source and output. The package does not establish classroom learning effects or automatic model performance.

Return to the econometrics teaching guide · Continue to model comparison with actual Stata data