Skip to content

New lecture: Fitting Distributions to Data - #814

Merged
jstac merged 2 commits into
mainfrom
fitting-distributions
Aug 3, 2026
Merged

New lecture: Fitting Distributions to Data#814
jstac merged 2 commits into
mainfrom
fitting-distributions

Conversation

@jstac

@jstac jstac commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

Adds the third lecture in the probability sequence, after prob_dist and observed_distributions, sitting immediately after the latter in the TOC.

It answers a question the series raises repeatedly but never addresses directly: given a data set, which distribution should we use to describe it? That splits into choosing a family and choosing parameters, and the lecture takes them in that order.

Sections

§ Content
The method of moments Stated generally, then applied to the normal, lognormal and gamma families. This names what prob_dist already did once, when it fitted a normal to the heights
Q-Q plots Defined from first principles, not introduced through a library call
The Kolmogorov-Smirnov statistic The largest vertical gap between ECDF and fitted CDF, drawn on the figure
Choosing between families Fit each by moments, take the smallest D
Count data A Poisson fit to goals per football match
When nothing fits The normal against Amazon returns

Q-Q plots get a proper definition

heavy_tails currently uses sm.qqplot(data, line='45') without ever saying what a Q-Q plot is. Here the construction is explicit — the $i$-th order statistic estimates the quantile of order $(i-0.5)/n$, so plotting it against the fitted quantile should give the 45° line — with sm.qqplot introduced afterwards as the shortcut. How to read a departure is stated as a rule: curvature means skew, an S-shape means both tails heavier than the fit allows.

A follow-up PR can slim heavy_tails to cite this instead of introducing the tool cold.

Model selection, with its limits

Fitting all three families to the house prices by moments and ranking by $D$ gives lognormal 0.053, gamma 0.070, normal 0.123. That agrees with the near-zero skewness of the log prices found in observed_distributions, so the reader reaches the same conclusion twice by different routes.

Three warnings accompany it: $D$ does not charge a family for having more parameters (so compare like with like, and beware nesting), it is insensitive in the tails, and the winner is only the best of the candidates tried.

The lecture stops short of the KS test and says why — that needs the null distribution of $D$, and our parameters came from the same data.

The failure case

Goals per match fit well ($\bar x = 2.83$, variance 2.78, frequencies sitting on the fitted PMF), so the machinery is shown working before it is shown failing.

Then the Amazon returns: an unremarkable $D$ but an obviously S-shaped Q-Q plot, because $D$ looks at the middle and the trouble is in the tails — which is what matters for asset returns, and hands off to heavy_tails. A note warns that $D$ is not comparable across samples of different size, since it shrinks with $n$ even when the fit is right.

Exercises

  1. Earthquakes — fit an exponential to the times between events. It fails: gaps up to 31 days against ~23 predicted, standard deviation 3.77 against a mean of 2.56 where the exponential requires them equal, and monthly counts with variance 1149 against mean 11.9. Aftershocks cluster, so arrivals are not independent, which ties back to the independence section of observed_distributions.
  2. Ages at death — fit a normal and read the failure. The points bend downwards from left skew, and the Q-Q plot also exposes the "100 and over" recording cap as a flat segment, which is worth seeing in its own right.

Data

epl_match_goals.csv and japan_earthquakes.csv come from QuantEcon/data-lectures#31; the heights, house price and age-at-death data are already published there. Executes end to end against the live URLs.

Follow-up

This lecture reads the Amazon returns via yfinance, so it needs an audit_annotations.yml entry in data-lectures, alongside the migration.yml repoint and consumers entries for the two new datasets. I will open that PR once this merges, as with #811.

🤖 Generated with Claude Code

Adds the third lecture in the probability sequence, after prob_dist and
observed_distributions, on choosing a distribution to describe a data set.
It sits immediately after observed_distributions in the TOC.

The lecture is organized around the two halves of that question — which
family, and which parameters:

- The method of moments, stated generally and applied to the normal,
  lognormal and gamma families. This names what prob_dist already did once
  when it fitted a normal to the height data.
- Q-Q plots, defined from first principles rather than introduced through a
  library call: the i-th order statistic estimates the quantile of order
  (i-0.5)/n, so plotting it against the fitted quantile should give the 45
  degree line. Then how to read a departure — curvature means skew, an
  S-shape means heavier tails than the fit allows.
- The Kolmogorov-Smirnov statistic as the largest vertical gap between the
  ECDF and the fitted CDF, drawn on the figure. It stops short of the test,
  and says why: that needs the null distribution of D, and our parameters
  came from the same data.
- Choosing between families by fitting each and taking the smallest D. For
  the house prices this ranks lognormal (0.053) over gamma (0.070) over
  normal (0.123), agreeing with the near-zero skewness of the log prices
  found in the previous lecture.
- Count data, fitting a Poisson to goals per football match, where the fit is
  good and the reason it should be is worth stating.
- A section on failure: the normal fit to Amazon returns has an unremarkable
  D but an obviously S-shaped Q-Q plot, because D looks at the middle of the
  distribution and the trouble is in the tails. That hands off to heavy_tails.

Three warnings accompany the model-selection method: D does not charge a
family for having more parameters, it is insensitive in the tails, and the
winner is only the best of the candidates tried.

Exercises fit an exponential to the times between Japanese earthquakes, which
fails because aftershocks cluster and the arrivals are therefore not
independent, and a normal to the Japanese age-at-death data, which fails
through left skew and also exposes the "100 and over" recording cap as a flat
segment in the Q-Q plot.

Data comes from QuantEcon/data-lectures#31.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@netlify

netlify Bot commented Aug 3, 2026

Copy link
Copy Markdown

Deploy Preview for taupe-gaufre-c4e660 ready!

Name Link
🔨 Latest commit 6e0c62c
🔍 Latest deploy log https://app.netlify.com/projects/taupe-gaufre-c4e660/deploys/6a70df4d948f9b0008820c4e
😎 Deploy Preview https://deploy-preview-814--taupe-gaufre-c4e660.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@github-actions

github-actions Bot commented Aug 3, 2026

Copy link
Copy Markdown

@github-actions
github-actions Bot temporarily deployed to pull request August 3, 2026 11:37 Inactive
@github-actions
github-actions Bot temporarily deployed to pull request August 3, 2026 11:37 Inactive
Terminology. The lecture asked the reader to choose a "family" without
saying what one is. It now introduces the parametric class -- a set of
distributions indexed by a small number of parameters -- in the outline, and
uses that term throughout. This also sharpens the warnings about comparing
classes of different dimension, where "family" was doing double duty for both
the set and its members.

What the lecture is about. The outline claimed we take parameter choice first
"since it turns out to be easier". Both questions are treated, the emphasis is
on choosing the class, and the difficulty claim is one the lecture never
supports -- the Student t section now contradicts it outright. It says instead
that the class is the main subject, that parameters get one technique with the
rest left to mle, and that we start there because a class must be fitted
before it can be judged.

"When nothing fits" was false. A Student t fits the Amazon returns well: the
KS distance falls from 0.068 to 0.041 fitting by moments, and to 0.022 by
maximum likelihood. The section is now "When the normal fails", and it turns
the failure into a method: the Q-Q plot says the tails are too heavy, that
points to a class with heavier tails, and the t delivers one. The fitted
value nu = 5.8 comes with a warning against leaning on it, since it is
estimated from the sample kurtosis, which is worst behaved exactly when the
tails are heavy -- concrete motivation for mle.

Plotting positions. The claim that the i-th order statistic estimates the
quantile of order (i-0.5)/n was asserted without support. It is now derived
from the ECDF, which steps up from (i-1)/n to i/n at that observation, so the
data supply an interval of orders rather than one, and the midpoint splits
the difference. A second reason is given: with i/n the largest observation
would be matched to the quantile of order 1, which is infinite for every
unbounded class here.

Figures. Per the manual, no ax.set_title: the eight body figures now carry
mystnb captions with fig: names, and the two inside solution directives are
left bare, since a caption there becomes a LaTeX float and breaks the PDF
build. Reference lines and CDF curves moved to lw=2.

The statsmodels Q-Q figure is dropped in favour of a note recording that
sm.qqplot exists, and why we build our own: the construction is worth
understanding, and our version takes any distribution, which the earthquake
exercise needs. statsmodels is no longer imported.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@github-actions
github-actions Bot temporarily deployed to pull request August 3, 2026 18:43 Inactive
@github-actions
github-actions Bot temporarily deployed to pull request August 3, 2026 18:43 Inactive
@jstac
jstac merged commit dc63dac into main Aug 3, 2026
7 checks passed
@jstac
jstac deleted the fitting-distributions branch August 3, 2026 18:49
@mmcky

mmcky commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

✅ Translation sync completed (zh-cn)

Target repo: QuantEcon/lecture-intro.zh-cn
Translation PR: QuantEcon/lecture-intro.zh-cn#279
Files synced (2):

  • lectures/fitting_distributions.md
  • lectures/_toc.yml

jstac added a commit that referenced this pull request Aug 3, 2026
fitting_distributions (#814) was reachable only from the table of contents and
from heavy_tails. Neither of the two lectures it builds on mentioned it, so a
reader working through the sequence in order had no pointer forward.

- prob_dist now introduces itself as the first of three and names both of the
  others, replacing a sentence that named only observed_distributions.
- observed_distributions points forward in its outline, and again at the place
  where it fits a normal to the Amazon returns by matching moments -- which is
  the method of moments, unnamed there, and the subject of the next lecture.
- mle gains its first cross-reference to another lecture. Its two steps for
  maximum likelihood, guess the distribution and estimate the parameters, are
  the two questions fitting_distributions is organized around, so it now says
  so. That closes a loop: fitting_distributions already sent readers to mle
  twice, once for a fuller treatment of parameter estimation and once for the
  Student t fit that the method of moments handles poorly.

Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants