Course Test · University: first two years
Probability: RatMath answers
The same twenty exercises, answered with RatMath Core and the Shelf libraries only. The code below was run when this page was built; its output is shown as printed, and compared with the independent answer.
Agrees 20
Libraries used here: core, symbolic, discrete, stats. In this code each library is in scope under its own name (linalg.det, stats.tTest), and Core's names (R, Rational, …) are in scope directly.
- 1.Addition ruleAgrees, , . Find and .Independent answer; .RatMath code
[R`0.5 + 0.4 - 0.2`, R`0.2 / 0.4`]Output[7/10, 1/2]
- 2.Birthday problemAgreesIn a group of 23 people, what is the probability that two share a birthday (365 equally likely days)?Independent answer.RatMath code
R`1`.subtract(product(343, 365, (k) => R`${k}/365`))Output0.507297234323985407225417…
- 3.HypergeometricAgreesA box has 8 red and 12 blue balls. Five are drawn without replacement. Find .Independent answer.RatMath code
stats.hypergeometric(20, 8, 5).pmf(2)Output385/969
- 4.PoissonAgreesCalls arrive at 3 per minute (Poisson). Find and in one minute.Independent answer; .RatMath code
const X = stats.poisson(3); [X.pmf(2), R`1`.subtract(X.pmf(0).low ?? X.pmf(0))]Output[0.2240418076553877434:0.22404180765538774341, 0.950212931632136057020657…]
- 5.GeometricAgreesA die is rolled until the first 6. Find the expected number of rolls and the probability the first 6 is on roll 4.Independent answer; .RatMath code
const G = stats.geometric(R`1/6`); [G.mean, G.pmf(4)]Output[6, 125/1296]
- 6.Sum of two diceAgreesFind the mean and variance of the sum of two fair dice.Independent answerMean 7; variance .RatMath code
const S = stats.die().sumOf(2); [S.mean(), S.variance()]Output[7, 35/6]
- 7.A continuous densityAgreeson . Find , and .Independent answer, . ; ; .RatMath code
const X = stats.fromDensity("x^2", [0, 2], { normalize: true }); [X.constant, X.mean, X.variance]Output[3/8, 3/2, 3/20]
fromDensityproves , finds the mass exactly, and keeps the moments exact because the density is a polynomial.Fix: ratmath/stats#8, summaries inmeanInterval/varianceInterval; closed-formpowerandsampleSize;fromDensity,transformandorderStatistic - 8.Exponential and memorylessnessAgreesA bulb's life is exponential with mean 10 years. Find , and .Independent answerfor both (memorylessness).RatMath code
const X = stats.exponential(R`1/10`); [X.sf(15), X.sf(25).low.divide(X.sf(10).high)]Output[0.2231301601484298289[3:4], 0.223130160148429828933280…]
- 9.Normal tailAgreesIQ is normal with mean 100 and SD 15. What fraction score above 130?Independent answer.RatMath code
stats.normal(100, 15).sf(130)Output0.0227501319481792072:0.02275013194817920721
- 10.Central limit theoremAgrees100 fair dice are rolled. Estimate with the normal approximation, and give the exact value.Independent answerMean 350, SD . With continuity correction . Exactly (by convolution in Python) 0.126948.RatMath code
const S = stats.die().sumOf(100); R`1`.subtract(S.cdf(369))Output0.126947761041731454742050…
- 11.CovarianceAgreestakes with probabilities 0.4, 0.1, 0.2, 0.3. Find .Independent answer, , : .RatMath code
const J = stats.finite(new Map([[[0, 0], "0.4"], [[0, 1], "0.1"], [[1, 0], "0.2"], [[1, 1], "0.3"]])); J.covariance(([x]) => x, ([, y]) => y)Output1/10
- 12.ConditioningAgreesRoll a die. If it is even, flip two coins; otherwise flip one. What is the expected number of heads?Independent answer.RatMath code
stats.die().expect((d) => (d % 2 === 0 ? R`1` : R`1/2`))Output3/4
Bug:expect(f)refused the Core valueR`1`returned byf("mean needs numbers, but an outcome is 1"); it took plain numbers only.Fix: ratmath/stats#7, textbook quartiles (method: "halves","tukey"); finite distributions accept Core'sInteger - 13.Bayes with urnsAgreesUrn 1 has 3 red and 2 blue balls, urn 2 has 1 red and 4 blue. An urn is chosen at random and a red ball drawn. What is the probability it came from urn 1?Independent answer.RatMath code
stats.bayes({ one: "1/2", two: "1/2" }, { one: "3/5", two: "1/5" }).posterior.p("one")Output3/4
- 14.Stationary distributionAgreesFind the stationary distribution of the chain with transition matrix .Independent answer: .RatMath code
stats.markovChain([["0.9", "0.1"], ["0.5", "0.5"]]).stationary().distributionOutput{ "0": 5/6, "1": 1/6 } - 15.Gambler's ruinAgreesA gambler with \$3 bets \$1 at a time, winning each bet with probability 0.4, until reaching \$10 or \$0. What is the probability of reaching \$10?Independent answer: .RatMath code
const P = Array.from({ length: 11 }, (_, i) => Array.from({ length: 11 }, (_, j) => (i === 0 || i === 10 ? (i === j ? 1 : 0) : j === i + 1 ? "0.4" : j === i - 1 ? "0.6" : 0))); stats.markovChain(P).hitting(10).probability[3]Output2432/58025
- 16.Moment generating functionAgreesThe MGF of a Poisson() variable is . Use it to find .Independent answer.RatMath code
const M = symbolic.sym("exp(l*(exp(t) - 1))"); symbolic.expand(M.diff("t").diff("t").subs({ t: 0 }).simplify())Outputl^2 + l
- 17.A transformed variableAgreesis uniform on and . Find the density of and .Independent answer, so on ; .RatMath code
const Y = stats.uniform(0, 1).transform("x^2"); [Y.pdf(R`1/4`), Y.cdf(R`1/4`)]Output[1, 1/2]
transformproves increasing on and gives the density pointwise: . The formula itself is not produced symbolically.Fix: ratmath/stats#8, summaries inmeanInterval/varianceInterval; closed-formpowerandsampleSize;fromDensity,transformandorderStatistic - 18.Order statisticsAgreesFind the expected maximum of three independent uniform variables.Independent answer, .RatMath code
stats.uniform(0, 1).orderStatistic(3, 3).meanOutput3/4
orderStatistic(3, 3)is the maximum of three draws, an exact polynomial density (Beta(3, 1)).Fix: ratmath/stats#8, summaries inmeanInterval/varianceInterval; closed-formpowerandsampleSize;fromDensity,transformandorderStatistic - 19.Coupon collectorAgreesHow many rolls of a die are expected before every face has appeared?Independent answer.RatMath code
sum(1, 6, (k) => R`6/${k}`)Output147/10
- 20.Matching problemAgreesTen letters go into ten envelopes at random. Find the expected number in the right envelope and the probability that none is.Independent answer. .RatMath code
[R`10 * 1/10`, new Rational(discrete.derangementCount(10), discrete.factorial(10))]Output[1, 16481/44800]