Skip to content
Tutorials Center
My learning

Topics

Lesson 4 of 37 ยท Getting Started

Simulating Causal Data in Python

Set up a causal inference workflow in Python: generate data from a known causal story with NumPy and pandas, estimate effects and compare them with the truth.

Real data never tells you the true causal effect, so how do you know a method works? You simulate: write the causal story as code, generate data from it, run your method, and compare with the truth you put in. This lesson sets up the tools and workflow used throughout the course.

The tools

Everything in this course runs in your browser with the Python runner:

  • NumPy for random numbers and arrays.
  • pandas for tables (DataFrame).
  • SciPy for tests and distributions.

You rarely need anything else. Always create a generator with a seed so results are repeatable:

Python Seeded randomness

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

A data-generating process

A data-generating process (DGP) lists, in causal order, how each variable is created from its causes plus noise. Writing it down is the same as drawing the causal graph you will meet in causal DAGs.

Example: a company runs a training course for employees.

flowchart LR
  M["motivation"] --> T["training"]
  M --> S["salary growth"]
  T --> S

Motivated people sign up more often and also earn more growth. Training itself adds a fixed bonus.

Python Writing a data-generating process

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Here the code knows effect = 3.0 even though the data frame hides it. That is the advantage of simulation.

Compare estimators with the truth

Wrap the process in a function, then test an estimator many times. The gap between the average estimate and the true effect is the bias.

Python Estimating bias by repetition

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The naive average sits far above 3 because motivated people both train more and grow more. Adjusting for motivation recovers roughly 3. This is the template of every later experiment.

A tidy workflow

  1. State the question (see estimands).
  2. Draw or write the assumed causal story.
  3. Simulate data from it, including a known effect.
  4. Apply your estimator and compare with the truth over many seeds.
  5. Break the assumptions on purpose (add an unmeasured confounder) to see what fails.
  6. Only then apply the method to real data, and report which assumptions you rely on.

Make simulations deliberately simple. Two or three variables are enough to expose most causal problems. Complexity belongs in real data, not in your teaching tool.

Useful building blocks

  • Continuous variable: rng.normal(mean, sd, n)
  • Binary variable with probability p: rng.random(n) < p
  • Probability that depends on a variable: the logistic function 1 / (1 + np.exp(-z))
  • Count variable: rng.poisson(lam, n)
  • Effect heterogeneity: effect = base + slope * x

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why simulate data when studying causal methods?

    With a known effect, bias and error of any method can be measured directly.
  2. 2. What does fixing the seed of the random generator do?

    The same seed produces the same random numbers every run.
  3. 3. In the training example, why was the naive difference too large?

    Motivation is a confounder pushing both variables the same way.
  4. 4. A data-generating process is best described as:

    It states the assumed causal story in executable form.

Summary

  • Simulate from a written causal story, then test estimators against the known effect.
  • Seeds make experiments repeatable; repetition over seeds reveals bias.
  • A data-generating process is the executable version of a causal graph.
  • Break assumptions deliberately to learn what each method needs.

Next: Randomized Experiments