prob_matrix#
Series: lecture-python.myst
File:
lectures/prob_matrix.mdAudit date: 2026-08-26
Corpus snapshot:
e25fdf2345Categories audited: writing, math, code, figures, links, admonitions (JAX out of scope)
Overall score: 6.1 / 10
Priority: HIGH
Score breakdown#
Category |
Score |
One-line note |
|---|---|---|
Writing |
3/10 |
|
Math |
3/10 |
|
Code |
5.5/10 |
|
JAX |
out of scope |
JAX rules target |
Figures |
5/10 |
|
References |
N/A |
no citations in this lecture. |
Links |
10/10 |
no mechanical violations detected. |
Admonitions |
10/10 |
no mechanical violations detected. |
Issues#
Critical#
None found.
High severity#
[qe-code-001] (reviewer) — Follow PEP8 unless closer to mathematical notation. Count: 7. Lines: 748, 751, 926, 981, 1074, 1648, 1719. Example: 748 declares
class discrete_bijoint:- PEP8 asks for CapWords for class names and there is no mathematical reading ofdiscrete_bijointthat requires lower case. All six docstrings in that class use'''instead of"""(751, 757, 761, 774, 778, 799, 826), against PEP 257. 926 names the bivariate normal densityfunc, which says nothing, where the surrounding prose calls it \(f(x,y)\). 981-982 omit the space before the assignment operator,μ= np.array([0, 5])andσ= np.array(...)(E225). 1074 and 1080 omit the space after a comma,plt.plot(y,z)andnp.dot(y,z)(E231), where 1041 and 1058 have it. 1648, 1654, 1655, 1662 and 1663 put two spaces before=(F = np.array(...),E_X = xs @ μ,var_X = ..., E221). 1719-1720 pads a call argument with ten spaces to align two columns,ax.bar(z_vals - 0.2, h, 0.4, ...)(E241). And five lines carry trailing whitespace: 1708, 1710, 1823, 1825, with 1706-1711 also indenting the closing paren and theforclause of one comprehension at three different depths.[qe-fig-005] — Descriptive figure names for cross-referencing. Count: 10. Lines: 955, 965, 991, 1000, 1037, 1056, 1071, 1078, 1703, 1811. Example: code-cell figure without mystnb figure metadata.
[qe-math-003] — Use square brackets for matrix notation. Count: 15. Lines: 261, 391, 501, 599, 696, 711, 723, 733, 914, 917, …. Example: array used as matrix.
[qe-math-009] (reviewer) — Choose simplicity in mathematical notation. Count: 5. Lines: 89, 154, 487, 696, 1793. Example: three displays fake a table inside a matrix environment: 696-703, 711-717 and 723-729 use
\vdotsas column rules,\cdots\cdots\cdotsas row rules and\quadfor padding, and set the header cellsvar,var_1,var_2,x,yas italic math (712) - the lecture already importsprettytable(55) and uses it eleven times for exactly this display. The conditioning bar is spelled three ways:\vert(487, 1049, 1050),|(431, 440, 447, 464, 494) and\mid(423, 1015, 1585, 1746). The weak inequality is spelled three ways:\geqslant(154, 481),\geq(366, 402, 481) and\ge(324, 332, 556). \(\mathcal{G}\) is used for a single event at 89, 98 and 110 while plain \(A\) and \(B\) are used for events at 423-426 - the same object under two conventions, and the plain letters win on simplicity. And 1793 writes the likelihood \(\mathcal{L}(\theta \mid \text{data})\), the only script letter in the exercises, where plain \(L\) would do.[qe-math-010 (proposed)] — Blackboard \mathbb{P}, \mathbb{E}, \mathbb{V} with braces. Count: 81. Lines: 104, 110, 149, 174, 210, 213, 220, 257, 316, 332, …. Example: non-blackboard
\textrm{Prob}.[qe-math-014 (proposed)] (reviewer) — Braces {…} for events, parentheses (…) for sets. Count: 12. Lines: 104, 149, 210, 453, 607, 1296, 1353. Example: the lecture writes the same events both ways. Braces are used correctly in most of the file - \(\textrm{Prob}\{X\leq x\}\) (213), \(\{X\in B\}\) (220), \(\{X=i\}\) (257, 316), \(\{x_0=i_0,\ldots\}\) (174), \(\{X=i,Y=j\}\) (366), \(\{Y=j\vert X=i\}\) (487) - but twelve events are parenthesised: \(\textrm{Prob}(X \in A)\) at 104, 110 and 210, \({\rm Prob}(X = i)\) at 149 (the identical event that 257 and 316 write with braces), \(\textrm{Prob}(X=i)\) and \(\textrm{Prob}(Y=j)\) at 607-608 (the identical marginals that 380 and 384 write with braces), \(\text{Prob}(X = 0)\) through \(\text{Prob}(Y = 1)\) at 1296-1299, and \(\textrm{Prob}(X=i,Y=j)\) at 1353. Line 453 gets it exactly inverted, \(\textrm{Prob}({X=i})\) - the braces are typed inside the parentheses rather than instead of them. The one legitimate parenthesised argument in the file is \(\mu(\mathcal{G})\) at 89, a measure applied to a named set, which is what the rule reserves parentheses for.
[qe-writing-002] (reviewer) — Keep writing clear, concise, and valuable. Count: 5. Lines: 137, 613, 1250, 1279, 1290. Example: 137 is a 46-word bullet containing two sentences (“The idea that a population probability distribution is what we anticipate relative frequencies will be in a long sequence of i.i.d. draws. Here the following mathematical machinery makes precise what is meant by anticipated relative frequencies”). 613 is 37 words restating 611: “The code lets us check whether the ‘sampling’ distribution agrees with the ‘population’ distribution – confirming that the population distribution correctly tells us the relative frequencies that we should expect in a large sample”. 1278-1279 spells the argument list \(F_1(x_1), F_2(x_2),\dots,F_N(x_N)\) three times inside one 43-word sentence. 1246-1250 spends three consecutive paragraphs on one point (“our two proposed joint distributions have the same marginal distributions” / “But the joint distributions differ” / “Thus, multiple joint distributions \([f_{ij}]\) can have the same marginals”). And 1290 and 1303 are the same sentence thirteen lines apart - “for two given marginal distributions there can be more than one coupling” and “For these two random variables there can be more than one coupling” - with only a display between them.
[qe-writing-003] (reviewer) — Maintain logical flow. Count: 6. Lines: 235, 281, 299, 517, 607, 953. Example: 235 has the logic backwards: the bullet list is about a discrete-valued random variable, whose second item says “we replace a density with a probability mass function”, and the third then says “when a density exists, we replace integration with summation in formulas like
{eq}`eq:CDFfromdensity`” - the substitution is needed when a density does not exist. 281 introduces a term with no antecedent: “Such a ‘non-parametric’ distribution has as many ‘parameters’ as…” is the first appearance of the word non-parametric anywhere in the lecture. 295-307 is a thirteen-line digression - statistical models, sufficient statistics, information, “key tools that AI uses” (305), a Fisher-information link (307) - dropped between the parameters of a distribution (284-292) and the geometric-distribution example (311); 299 defines “A statistic is a nonlinear function of a data set”, which contradicts 135 (“A statistic defined as a function of a sequence of samples”) and is false as written, and “sufficient statistics as data summaries”, promised in the overview list at 34, never gets a section, an example or a line of code. 517-541 “Application: forecasting a time series” restates 468-513 with \(f_{ij}\) for \(\rho_{ij}\) and \(X(0),X(1)\) for \(X,Y\) and adds only the sentence at 541. The marginals are renamed three times - \(\mu_i,\nu_j\) at 380-384, \(u_i,v_j\) at 607-608, back to \(\mu_i,\nu_j\) at 1177-1178 - and 596 calls the display that follows it “The table below” when 598-603 is a matrix. And the 145 lines from 953 to 1097 are nine plotting cells with almost no prose between them: 991-998 and 1000-1007 sit adjacent with no sentence at all between the two histograms.[qe-writing-004] — Avoid unnecessary capitalization in narrative text. Count: 7. Lines: 78, 194, 196, 1626, 1738, 1784. Example: mid-sentence ‘Values’.
[qe-writing-005] (reviewer) — Use bold for definitions, italic for emphasis. Count: 7. Lines: 137, 196, 202, 953, 978, 1165, 1575. Example: bold is used for both jobs and italic for neither. The definitions are correctly bolded - probability space, probability measure, random variable (68), events (85), probability mass function (234), statistical model (295), coupling (1171), copula function (1262) and about twenty more - but bold then does the emphasis too: relative frequencies is re-bolded at 137 after its definition at 136 and anticipated relative frequencies with it; all at 202 (“is all that a probability distribution means”); rate at 196; the contrast population (953) against sample (978); unique at 1165; independent at 1686 after the definition at 546. The five exercise titles (1575, 1626, 1684, 1738, 1784) are bold-as-heading rather than definitions. Across 1844 lines the file contains exactly one italic - the book title Risk, Uncertainty, and Values at 78 - so no emphasis anywhere is italicised.
[qe-writing-008] — Remove excessive whitespace between words. Count: 64. Lines: 233, 234, 255, 258, 269, 274, 284, 286, 311, 319, …. Example: 2 spaces.
Medium severity#
[qe-code-002] — Use Unicode symbols for Greek letters in code. Count: 4. Lines: 835, 836, 837. Example: spelled-out
xi.[qe-code-003] — Package installation at lecture top. Count: 1. Lines: 43. Example: non-Anaconda import with no install cell: [‘matplotlib_inline’].
[qe-fig-001] — Do not set figure size unless necessary. Count: 2. Lines: 1551, 1830. Example: figsize=.
[qe-fig-006] — Lowercase axis labels. Count: 2. Lines: 1722, 1837. Example: axis label
Probability.[qe-fig-008] — Use lw=2 for line charts. Count: 2. Lines: 1041, 1074. Example: plot() without lw=.
[qe-math-011 (proposed)] — Distribution names in plain letters, not \mathcal / \mathbb. Count: 2. Lines: 1015, 1016. Example: decorated distribution
\mathbb{N}.[qe-math-015 (proposed)] (reviewer) — Lowercase for densities/PMFs, uppercase for CDFs. Count: 4. Lines: 213, 360, 599, 1258. Example: \(F\) carries two incompatible meanings inside one lecture. 213 defines it as the CDF, \(F_X(x) = \textrm{Prob}\{X\leq x\}\), and 224 repeats that, \(F(x)=\int_{-\infty}^x f(t)dt\), against the lowercase density \(f\) - which is exactly the case convention proposed qe-math-015 (proposed) asks for. Then 360 makes \(F_{I\times J}=[f_{ij}]\) the joint probability mass matrix, and that is the meaning used at 390, 599, 1580, 1601 and 1648 - an uppercase letter for a mass function whose own elements are the lowercase \(f_{ij}\). By 1258 the copula section needs the first meaning back (“their marginal distributions are \(F_1(x_1),\ldots,F_N(x_N)\)”, used as CDFs at 1265 and 1273), so a reader arriving there from 599 has to re-learn the symbol. Either the joint matrix or the CDF has to be renamed.
[qe-writing-001] — Use one sentence per paragraph. Count: 1. Lines: 1252. Example: 2 sentences in one paragraph.
[qe-writing-007] (reviewer) — Use visual elements to enhance understanding. Count: 4. Lines: 696, 955, 992, 1200. Example: the three objects that carry the lecture’s own thesis - “a matrix to represent a bivariate … distribution” (36) - are the worst-rendered things in the file: the hand-drawn
arraytables at 696-703, 711-717 and 723-729. Second, the 3-d joint density at 955-963 has no axis labels, no colorbar, no caption and no sentence after it, and the contour that follows at 965-976 mixesplt.ylabel('y')withax.set_zlabel('f')and then hides the x axis withax.set_xticks([]). Third, 991-998 overlays a fitted normal on the sample histogram but omitsdensity=Truefrom both calls, while the matching cell at 1000-1007 includes it in both, and neither figure has a legend distinguishing the sample from the fit - so the reader cannot tell which histogram is which, in the one place the lecture is checking a fit. Fourth, the whole coupling argument (1140-1250) is algebra with no picture: the two couplings at 1200-1206 and 1224-1231 differ in a structural zero, which a pair of 2x2 heatmaps would show instantly, and the discretized conditional plots at 1037-1043 and 1071-1076 never draw the analytic normal of 1015-1016 on top of the approximation they are meant to validate - that check is left to four printed numbers at 1091-1096.[qe-writing-009 (proposed)] — Write “IID” — not “i.i.d.” or “iid”. Count: 4. Lines: 134, 137, 167, 1788. Example: i.i.d…
Low severity#
None found.
Strengths#
Every population object computed by matrix algebra has a simulated counterpart printed beside it: the 2x2 joint at 599 against a million draws at 617-637, the population marginals at 607-608 against the
PrettyTablemarginals at 644-663, the population conditionals at 722-740 against the sampled conditionals at 665-691, and 742 closes the loop - “These population objects closely resemble the sample counterparts computed above”.The two couplings at 1200-1206 and 1224-1231 are chosen so the second carries a structural zero, and each is then verified twice - algebraically at 1211-1219 and 1236-1244, and by simulation at 1367-1431 and 1442-1505 - so the claim at 1250 that multiple joint distributions share one pair of marginals is demonstrated three ways rather than asserted.
The Gaussian-copula section is the best-built part of the file: 1517-1522 gives the construction as three numbered steps, 1534-1549 implements exactly those three steps with comments that name them (
# Apply normal CDF -> uniform marginals (the copula itself)), the cell carriesmystnbcaption and name metadata at 1527-1532 - the only cell in 1844 lines that does - and 1560-1561 prints both correlations so the reader can see the dependence survive the marginal transform.253-255 justifies an indexing choice most lectures leave silent - the support is \(0,1,\ldots,I-1\) “because of how this aligns nicely with Python’s index convention” - and the algebra and the array indices then agree for the rest of the lecture.
The five exercises at 1572-1841 each test something the lecture actually built, and in the lecture’s own notation: independence via the outer product of the marginal vectors (1587), covariance from the joint matrix (1630-1636), the convolution formula
{eq}against a million simulated dice rolls (1688-1692), Markov iterates against a stationary distribution solved as a least-squares system (1748-1752), and{eq}`eq:condprobbayes`applied to a three-point prior (1798) - all with worked solutions.The three
{note}s are used for what admonitions are for: 72-79 boxes the measure-theoretic material the lecture is deliberately setting aside and points at Hansen and Sargent for it, 640-642 names the inverse-CDF technique behind the cell above it and links the lecture that derives it, and 1020-1022 defers the bivariate-normal conditional formulas tomultivariate_normal.
Recommended actions#
Fix the backwards sentence at 235: the bullet is about discrete random variables, where no density exists, so “when a density exists, we replace integration with summation” should read “when a density does not exist”.
Resolve the \(F\) collision - \(F\) is the CDF at 213 and 224, the joint probability matrix at 360, 390, 599, 1580, 1601 and 1648, and a CDF again at 1258-1273 where the copula needs it. Renaming the joint matrix (the lecture already writes it as \([f_{ij}]\) at 599 and 1359) is the smaller change.
Put the events in braces at 104, 110, 149, 210, 607, 608, 1296-1299 and 1353, and un-invert 453, which writes
\textrm{Prob}({X=i})with the braces inside the parentheses. Do it in the same pass as the 81qe-math-010(proposed) sites: convert\textrm{Prob},{\rm Prob}and\text{Prob}to\mathbb{P},\mathrm{E}at 588 to\mathbb{E}, and the variance operator\mathbb{D}(579, 588, 1050) to\mathbb{V}.Replace the three hand-drawn
arraytables at 696-703, 711-717 and 723-729 with markdown tables orprettytableoutput -prettytableis already imported at 55 and used eleven times - and drop the\vdots/\cdots\cdots\cdotsrules and the italic-math header cells at 712.Give the continuous-bivariate figures the labels and the prose they need: axis labels and a caption on the 3-d surface at 955-963,
density=Trueon both calls at 992 and 996 so the fit and the sample are comparable, a legend on 991-998 and 1000-1007, and the analytic conditional normal of 1015-1016 drawn on top of the discretized conditionals at 1037-1043 and 1071-1076 instead of checked by the four printed numbers at 1091-1096.Either deliver “sufficient statistics as data summaries”, promised in the overview at 34, or cut the digression at 295-307 down to the definitions the lecture uses - and correct 299, since a statistic need not be nonlinear and 135 already defines one differently.
Fold “Application: forecasting a time series” (517-541) into the transition-matrix section at 468-513, which it restates with \(f_{ij}\) for \(\rho_{ij}\); and settle one name for the marginals rather than \(\mu_i,\nu_j\) (380) then \(u_i,v_j\) (607) then \(\mu_i,\nu_j\) again (1177).
Italicise the emphasis at 137, 196, 202, 953, 978, 1165 and 1686 and leave bold for the definitions; the file currently has one italic in 1844 lines.
Code cleanup: rename
discrete_bijoint(748) to CapWords andfunc(926) to name the density, switch the six'''docstrings to"""(751, 757, 761, 774, 778, 799, 826), and fix the spacing at 981-982 (E225), 1074 and 1080 (E231), 1648-1663 (E221) and 1719-1720 (E241) plus the trailing whitespace at 1708, 1710, 1823 and 1825.Sweep the mechanical remainder: the 64 double spaces (233, 234, 255, 258, 269, 274, 284, 286, 311, 319 and 54 more), the four “i.i.d.” spellings (134, 137, 167, 1788) - 167 writes “IID or iid” in the same sentence - the fifteen
array/matrixdisplays that should bebmatrix,\mathbb{N}for the normal at 1015-1016,mystnbmetadata for the ten un-named figure cells,lw=2at 1041 and 1074, and the two capitalised axis labels at 1722 and 1837.