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. 1.Addition ruleAgrees
    P(A)=0.5P(A) = 0.5, P(B)=0.4P(B) = 0.4, P(A∩B)=0.2P(A \cap B) = 0.2. Find P(A∪B)P(A \cup B) and P(A∣B)P(A \mid B).
    Independent answer
    0.5+0.4−0.2=0.70.5 + 0.4 - 0.2 = 0.7; 0.2/0.4=0.50.2/0.4 = 0.5.
    RatMath code
    [R`0.5 + 0.4 - 0.2`, R`0.2 / 0.4`]
    Output
    [7/10, 1/2]
  2. 2.Birthday problemAgrees
    In a group of 23 people, what is the probability that two share a birthday (365 equally likely days)?
    Independent answer
    1−365⋅364⋯34336523≈0.50731 - \dfrac{365\cdot364\cdots343}{365^{23}} \approx 0.5073.
    RatMath code
    R`1`.subtract(product(343, 365, (k) => R`${k}/365`))
    Output
    0.507297234323985407225417…
  3. 3.HypergeometricAgrees
    A box has 8 red and 12 blue balls. Five are drawn without replacement. Find P(exactly 2 red)P(\text{exactly 2 red}).
    Independent answer
    (82)(123)(205)=28⋅22015504=385969\dfrac{\binom82\binom{12}3}{\binom{20}5} = \dfrac{28\cdot220}{15504} = \dfrac{385}{969}.
    RatMath code
    stats.hypergeometric(20, 8, 5).pmf(2)
    Output
    385/969
  4. 4.PoissonAgrees
    Calls arrive at 3 per minute (Poisson). Find P(X=2)P(X = 2) and P(X≥1)P(X \ge 1) in one minute.
    Independent answer
    92e−3≈0.22404\tfrac92e^{-3} \approx 0.22404; 1−e−3≈0.950211 - e^{-3} \approx 0.95021.
    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. 5.GeometricAgrees
    A 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
    E=6E = 6; (5/6)3(1/6)=125/1296(5/6)^3(1/6) = 125/1296.
    RatMath code
    const G = stats.geometric(R`1/6`);
    [G.mean, G.pmf(4)]
    Output
    [6, 125/1296]
  6. 6.Sum of two diceAgrees
    Find the mean and variance of the sum of two fair dice.
    Independent answer
    Mean 7; variance 2⋅3512=3562\cdot\tfrac{35}{12} = \tfrac{35}{6}.
    RatMath code
    const S = stats.die().sumOf(2);
    [S.mean(), S.variance()]
    Output
    [7, 35/6]
  7. 7.A continuous densityAgrees
    f(x)=cx2f(x) = cx^2 on [0,2][0, 2]. Find cc, E[X]E[X] and Var⁡X\operatorname{Var} X.
    Independent answer
    c⋅83=1c\cdot\tfrac83 = 1, c=38c = \tfrac38. E[X]=38⋅4=32E[X] = \tfrac38\cdot4 = \tfrac32; E[X2]=38⋅325=125E[X^2] = \tfrac38\cdot\tfrac{32}{5} = \tfrac{12}{5}; Var⁡=125−94=320\operatorname{Var} = \tfrac{12}{5} - \tfrac94 = \tfrac{3}{20}.
    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]
    fromDensity proves x2≥0x^2 \ge 0, finds the mass 83\tfrac83 exactly, and keeps the moments exact because the density is a polynomial.
    Fix: ratmath/stats#8, summaries in meanInterval/varianceInterval; closed-form power and sampleSize; fromDensity, transform and orderStatistic
  8. 8.Exponential and memorylessnessAgrees
    A bulb's life is exponential with mean 10 years. Find P(X>15)P(X > 15), and P(X>25∣X>10)P(X > 25 \mid X > 10).
    Independent answer
    e−1.5≈0.22313e^{-1.5} \approx 0.22313 for 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. 9.Normal tailAgrees
    IQ is normal with mean 100 and SD 15. What fraction score above 130?
    Independent answer
    P(Z>2)≈0.02275P(Z > 2) \approx 0.02275.
    RatMath code
    stats.normal(100, 15).sf(130)
    Output
    0.0227501319481792072:0.02275013194817920721
  10. 10.Central limit theoremAgrees
    100 fair dice are rolled. Estimate P(sum≥370)P(\text{sum} \ge 370) with the normal approximation, and give the exact value.
    Independent answer
    Mean 350, SD 100⋅35/12≈17.08\sqrt{100\cdot35/12} \approx 17.08. With continuity correction P(Z≥19.5/17.08)≈0.1268P(Z \ge 19.5/17.08) \approx 0.1268. Exactly (by convolution in Python) 0.126948.
    RatMath code
    const S = stats.die().sumOf(100);
    R`1`.subtract(S.cdf(369))
    Output
    0.126947761041731454742050…
  11. 11.CovarianceAgrees
    (X,Y)(X, Y) takes (0,0),(0,1),(1,0),(1,1)(0,0), (0,1), (1,0), (1,1) with probabilities 0.4, 0.1, 0.2, 0.3. Find Cov⁡(X,Y)\operatorname{Cov}(X, Y).
    Independent answer
    E[X]=0.5E[X] = 0.5, E[Y]=0.4E[Y] = 0.4, E[XY]=0.3E[XY] = 0.3: 0.3−0.2=0.10.3 - 0.2 = 0.1.
    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)
    Output
    1/10
  12. 12.ConditioningAgrees
    Roll a die. If it is even, flip two coins; otherwise flip one. What is the expected number of heads?
    Independent answer
    12⋅1+12⋅12=34\tfrac12\cdot1 + \tfrac12\cdot\tfrac12 = \tfrac34.
    RatMath code
    stats.die().expect((d) => (d % 2 === 0 ? R`1` : R`1/2`))
    Output
    3/4
    Bug: expect(f) refused the Core value R`1` returned by f ("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's Integer
  13. 13.Bayes with urnsAgrees
    Urn 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
    12⋅3512⋅35+12⋅15=34\dfrac{\tfrac12\cdot\tfrac35}{\tfrac12\cdot\tfrac35 + \tfrac12\cdot\tfrac15} = \tfrac34.
    RatMath code
    stats.bayes({ one: "1/2", two: "1/2" }, { one: "3/5", two: "1/5" }).posterior.p("one")
    Output
    3/4
  14. 14.Stationary distributionAgrees
    Find the stationary distribution of the chain with transition matrix (0.90.10.50.5)\begin{pmatrix} 0.9 & 0.1 \\ 0.5 & 0.5 \end{pmatrix}.
    Independent answer
    0.1π1=0.5π20.1\pi_1 = 0.5\pi_2: π=(56,16)\pi = (\tfrac56, \tfrac16).
    RatMath code
    stats.markovChain([["0.9", "0.1"], ["0.5", "0.5"]]).stationary().distribution
    Output
    { "0": 5/6, "1": 1/6 }
  15. 15.Gambler's ruinAgrees
    A 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
    r=q/p=1.5r = q/p = 1.5: 1−1.531−1.510=2.37556.665≈0.04191\dfrac{1 - 1.5^3}{1 - 1.5^{10}} = \dfrac{2.375}{56.665} \approx 0.04191.
    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]
    Output
    2432/58025
  16. 16.Moment generating functionAgrees
    The MGF of a Poisson(λ\lambda) variable is M(t)=eλ(et−1)M(t) = e^{\lambda(e^t - 1)}. Use it to find E[X2]E[X^2].
    Independent answer
    M′′(0)=λ+λ2M''(0) = \lambda + \lambda^2.
    RatMath code
    const M = symbolic.sym("exp(l*(exp(t) - 1))");
    symbolic.expand(M.diff("t").diff("t").subs({ t: 0 }).simplify())
    Output
    l^2 + l
  17. 17.A transformed variableAgrees
    XX is uniform on (0,1)(0, 1) and Y=X2Y = X^2. Find the density of YY and P(Y≤14)P(Y \le \tfrac14).
    Independent answer
    FY(y)=yF_Y(y) = \sqrt y, so fY(y)=12yf_Y(y) = \tfrac{1}{2\sqrt y} on (0,1)(0, 1); P(Y≤14)=12P(Y \le \tfrac14) = \tfrac12.
    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]
    transform proves x2x^2 increasing on (0,1)(0, 1) and gives the density pointwise: fY(14)=1=121/4f_Y(\tfrac14) = 1 = \tfrac{1}{2\sqrt{1/4}}. The formula 12y\tfrac{1}{2\sqrt y} itself is not produced symbolically.
    Fix: ratmath/stats#8, summaries in meanInterval/varianceInterval; closed-form power and sampleSize; fromDensity, transform and orderStatistic
  18. 18.Order statisticsAgrees
    Find the expected maximum of three independent uniform (0,1)(0, 1) variables.
    Independent answer
    Fmax⁡(x)=x3F_{\max}(x) = x^3, E=∫013x3 dx=34E = \int_0^1 3x^3\,dx = \tfrac34.
    RatMath code
    stats.uniform(0, 1).orderStatistic(3, 3).mean
    Output
    3/4
    orderStatistic(3, 3) is the maximum of three draws, an exact polynomial density 3x23x^2 (Beta(3, 1)).
    Fix: ratmath/stats#8, summaries in meanInterval/varianceInterval; closed-form power and sampleSize; fromDensity, transform and orderStatistic
  19. 19.Coupon collectorAgrees
    How many rolls of a die are expected before every face has appeared?
    Independent answer
    6(1+12+⋯+16)=6⋅4920=14.76\left(1 + \tfrac12 + \cdots + \tfrac16\right) = 6\cdot\tfrac{49}{20} = 14.7.
    RatMath code
    sum(1, 6, (k) => R`6/${k}`)
    Output
    147/10
  20. 20.Matching problemAgrees
    Ten letters go into ten envelopes at random. Find the expected number in the right envelope and the probability that none is.
    Independent answer
    E=10⋅110=1E = 10\cdot\tfrac1{10} = 1. P=D10/10!=1334961/3628800≈0.367879P = D_{10}/10! = 1334961/3628800 \approx 0.367879.
    RatMath code
    [R`10 * 1/10`, new Rational(discrete.derangementCount(10), discrete.factorial(10))]
    Output
    [1, 16481/44800]