Course Test · University: first two years

Mathematical statistics: 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, 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.Maximum likelihood: exponentialAgrees
    The waiting times 2, 3, 1, 4, 5 come from an exponential distribution with rate λ\lambda. Find the maximum likelihood estimate of λ\lambda.
    Independent answer
    ℓ(λ)=nln⁡λ−λ∑xi\ell(\lambda) = n\ln\lambda - \lambda\sum x_i, so λ^=n/∑xi=5/15=13\hat\lambda = n/\sum x_i = 5/15 = \tfrac13.
    RatMath code
    stats.fit([2, 3, 1, 4, 5], "exponential").params.rate
    Output
    1/3
  2. 2.Maximum likelihood: PoissonAgrees
    Counts 2, 0, 3, 1, 4 come from a Poisson distribution. Find the maximum likelihood estimate of its mean.
    Independent answer
    λ^=xˉ=10/5=2\hat\lambda = \bar x = 10/5 = 2.
    RatMath code
    stats.fit([2, 0, 3, 1, 4], "poisson").params.lambda
    Output
    2
  3. 3.Unbiased varianceAgrees
    Find the sample variance s2s^2 (divisor n−1n-1) of 3, 5, 7, 9, 11.
    Independent answer
    Deviations −4,−2,0,2,4-4,-2,0,2,4; ∑=40\sum = 40; s2=40/4=10s^2 = 40/4 = 10.
    RatMath code
    stats.variance([3, 5, 7, 9, 11])
    Output
    10
  4. 4.Two-sample t testAgrees
    Group A: 12, 15, 11, 14, 13. Group B: 16, 18, 15, 17, 19. Test equal means with the pooled two-sample tt test. Give tt, the degrees of freedom and the two-sided pp-value.
    Independent answer
    xˉA=13\bar x_A = 13, xˉB=17\bar x_B = 17, both s2=2.5s^2 = 2.5, pooled sp2=2.5s_p^2 = 2.5. t=−4/2.5(2/5)=−4t = -4/\sqrt{2.5(2/5)} = -4, 8 df, p≈0.00395p \approx 0.00395.
    RatMath code
    const r = stats.tTest([12, 15, 11, 14, 13], [16, 18, 15, 17, 19], { equalVariance: true });
    [r.statistic, r.df, r.pValue]
    Output
    [-4, 8, 0.0039497728034453258[1:2]]
  5. 5.Paired t testAgrees
    Blood pressure before: 200, 210, 190, 220, 205; after: 195, 200, 192, 210, 198. Test for a change with a paired tt test (two-sided).
    Independent answer
    Differences 5, 10, −2-2, 10, 7: dˉ=6\bar d = 6, sd=24.5s_d = \sqrt{24.5}. t=6/(24.5/5)≈2.7105t = 6/(\sqrt{24.5}/\sqrt5) \approx 2.7105, 4 df, p≈0.0535p \approx 0.0535: not significant at 5%.
    RatMath code
    const r = stats.tTest([200, 210, 190, 220, 205], [195, 200, 192, 210, 198], { paired: true });
    [r.statistic, r.pValue]
    Output
    [2.7105237087157537131[3:5], 0.05350829685091812719:0.0535082968509181272]
  6. 6.Interval for a varianceAgrees
    A sample of n=10n = 10 from a normal population has s2=4s^2 = 4. Give a 95% confidence interval for σ2\sigma^2.
    Independent answer
    (9⋅4χ0.975,92,9⋅4χ0.025,92)=(3619.023,362.7004)≈(1.8925,13.331)\left(\dfrac{9\cdot4}{\chi^2_{0.975,9}}, \dfrac{9\cdot4}{\chi^2_{0.025,9}}\right) = \left(\dfrac{36}{19.023}, \dfrac{36}{2.7004}\right) \approx (1.8925, 13.331).
    RatMath code
    const ci = stats.varianceInterval({ n: 10, variance: 4 });
    [ci.low, ci.high]
    Output
    [1328165573307087716352/701816258310290954905, 10625324586456701730816/797014304087522667905]
    varianceInterval takes nn and s2s^2 as a summary.
    Fix: ratmath/stats#8, summaries in meanInterval/varianceInterval; closed-form power and sampleSize; fromDensity, transform and orderStatistic
  7. 7.One-way ANOVAAgrees
    Three groups: {4,5,6}\{4,5,6\}, {6,7,8}\{6,7,8\}, {9,10,11}\{9,10,11\}. Compute the ANOVA FF statistic and its pp-value.
    Independent answer
    Means 5, 7, 10, grand mean 22/322/3. SSB=3(499+19+649)=38SS_B = 3(\tfrac{49}{9} + \tfrac19 + \tfrac{64}{9}) = 38, SSW=6SS_W = 6. F=(38/2)/(6/6)=19F = (38/2)/(6/6) = 19 on (2, 6) df, p≈0.00254p \approx 0.00254.
    RatMath code
    const r = stats.anova([[4, 5, 6], [6, 7, 8], [9, 10, 11]]);
    [r.statistic, r.pValue]
    Output
    [19, 0.0025356874530428249[4:5]]
  8. 8.Inference for a slopeAgrees
    For x=1,…,5x = 1,\dots,5 and y=2,4,5,4,6y = 2, 4, 5, 4, 6, find the least-squares slope, its standard error, the tt statistic and the two-sided pp-value for H0:β1=0H_0: \beta_1 = 0.
    Independent answer
    b1=8/10=0.8b_1 = 8/10 = 0.8. SSE=2.4SSE = 2.4, s2=0.8s^2 = 0.8, SE=0.8/10≈0.28284SE = \sqrt{0.8/10} \approx 0.28284, t≈2.8284t \approx 2.8284, 3 df, p≈0.0663p \approx 0.0663.
    RatMath code
    const m = stats.linearModel([1, 2, 3, 4, 5], [2, 4, 5, 4, 6]);
    const r = m.test(1);
    [m.coefficients[1], r.standardError, r.statistic, r.pValue]
    Output
    [4/5, 0.2828427124746190097[6:7], 2.8284271247461900976:2.82842712474619009761, 0.0662756027415242553[5:6]]
  9. 9.Coefficient of determinationAgrees
    For the same data, find R2R^2.
    Independent answer
    SST=5.2SST = 5.2, SSR=0.82⋅10=6.4SSR = 0.8^2\cdot10 = 6.4… more simply R2=1−SSE/SST=1−2.4/8.8=811R^2 = 1 - SSE/SST = 1 - 2.4/8.8 = \tfrac{8}{11}.
    RatMath code
    stats.linearModel([1, 2, 3, 4, 5], [2, 4, 5, 4, 6]).rSquared
    Output
    8/11
  10. 10.Multiple regressionAgrees
    Fit y=b0+b1x1+b2x2y = b_0 + b_1x_1 + b_2x_2 to x1=1,2,3,4,5x_1 = 1,2,3,4,5, x2=2,1,4,3,5x_2 = 2,1,4,3,5, y=3,4,8,8,11y = 3,4,8,8,11.
    Independent answer
    Solving the normal equations XTXb=XTyX^TXb = X^Ty gives b=(310,43,56)b = (\tfrac3{10}, \tfrac43, \tfrac56).
    RatMath code
    stats.linearModel([[1, 2], [2, 1], [3, 4], [4, 3], [5, 5]], [3, 4, 8, 8, 11]).coefficients
    Output
    [3/10, 4/3, 5/6]
  11. 11.Mann–Whitney testAgrees
    Compare {1.1,2.3,3.0}\{1.1, 2.3, 3.0\} with {3.5,4.1,5.2,6.0}\{3.5, 4.1, 5.2, 6.0\} by the Mann–Whitney (Wilcoxon rank-sum) test. Give UU and the exact two-sided pp-value.
    Independent answer
    Every value in the first sample is below every value in the second, so U=0U = 0. Only 1 of the (73)=35\binom73 = 35 arrangements is that extreme on each side: p=2/35≈0.0571p = 2/35 \approx 0.0571.
    RatMath code
    const r = stats.mannWhitneyTest([1.1, 2.3, 3.0], [3.5, 4.1, 5.2, 6.0]);
    [r.statistic, r.pValue]
    Output
    [0, 2/35]
  12. 12.Sign testAgrees
    Of 10 paired differences, 9 are positive and 1 negative. Find the two-sided sign-test pp-value.
    Independent answer
    2 P(X≥9)=2⋅10+11024=221024=11512≈0.02152\,P(X \ge 9) = 2\cdot\dfrac{10 + 1}{1024} = \dfrac{22}{1024} = \dfrac{11}{512} \approx 0.0215.
    RatMath code
    stats.signTest([1, 1, 1, 1, 1, 1, 1, 1, 1, -1]).pValue
    Output
    11/512
  13. 13.Fisher's exact testAgrees
    Find the two-sided Fisher exact pp-value for the table (3113)\begin{pmatrix}3&1\\1&3\end{pmatrix}.
    Independent answer
    Hypergeometric probabilities for the top-left cell 0,…,40,\dots,4: 170,1670,3670,1670,170\tfrac1{70},\tfrac{16}{70},\tfrac{36}{70},\tfrac{16}{70},\tfrac1{70}. Tables at most as likely as the observed one: p=3470=1735p = \tfrac{34}{70} = \tfrac{17}{35}.
    RatMath code
    stats.fisherTest([[3, 1], [1, 3]]).pValue
    Output
    17/35
  14. 14.Power of a z testAgrees
    Test H0:μ=100H_0: \mu = 100 against μ>100\mu > 100 with σ=15\sigma = 15, n=25n = 25, α=0.05\alpha = 0.05. What is the power when the true mean is 106?
    Independent answer
    Reject when xˉ>100+1.645⋅3=104.935\bar x > 100 + 1.645\cdot3 = 104.935. Power =P(Z>(104.935−106)/3)=P(Z>−0.355)≈0.6388= P(Z > (104.935 - 106)/3) = P(Z > -0.355) \approx 0.6388.
    RatMath code
    stats.power({ mu0: 100, mu: 106, sigma: 15, n: 25, alternative: "greater" }).power
    Output
    0.6387600313123350643[1:4]
    power is the closed form 1−Φ(z0.95−δ)1 - \Phi(z_{0.95} - \delta) with certified quantiles.
    Fix: ratmath/stats#8, summaries in meanInterval/varianceInterval; closed-form power and sampleSize; fromDensity, transform and orderStatistic
  15. 15.Sample sizeAgrees
    How large a sample gives a margin of error of at most 3 percentage points for a proportion at 95% confidence (use p=0.5p = 0.5)?
    Independent answer
    n≥(1.96/0.03)2⋅0.25=1067.1n \ge (1.96/0.03)^2 \cdot 0.25 = 1067.1, so n=1068n = 1068.
    RatMath code
    stats.sampleSize({ margin: "0.03", p: "1/2" }).n
    Output
    1068
    sampleSize encloses z2p(1−p)/m2=1067.1z^2 p(1-p)/m^2 = 1067.1 and rounds up once the enclosure settles it.
    Fix: ratmath/stats#8, summaries in meanInterval/varianceInterval; closed-form power and sampleSize; fromDensity, transform and orderStatistic
  16. 16.Conjugate priorAgrees
    With a uniform Beta(1, 1) prior on a success probability, 7 successes are seen in 10 trials. Give the posterior and its mean.
    Independent answer
    Beta(8, 4), with mean 8/12=238/12 = \tfrac23.
    RatMath code
    const post = stats.betaPrior(1, 1).update({ successes: 7, trials: 10 });
    [post.params, post.mean]
    Output
    [{ a: 8, b: 4 }, 2/3]
  17. 17.Kolmogorov–Smirnov statisticAgrees
    Find the Kolmogorov–Smirnov statistic DD for the sample 0.1, 0.4, 0.7 against the uniform distribution on [0,1][0, 1].
    Independent answer
    Compare F(x)=xF(x) = x with the step function: at 0.1 the gaps are 0.1 and 0.233, at 0.4 they are 0.267 and 0.067, at 0.7 they are 0.3 and 0.033. D=0.3D = 0.3.
    RatMath code
    stats.ksTest([0.1, 0.4, 0.7], stats.uniform(0, 1)).statistic
    Output
    3/10
  18. 18.Spearman's rank correlationAgrees
    Find Spearman's ρ\rho for x=1,2,3,4,5x = 1,2,3,4,5 and y=2,1,4,3,5y = 2,1,4,3,5.
    Independent answer
    Rank differences −1,1,−1,1,0-1, 1, -1, 1, 0, ∑d2=4\sum d^2 = 4: ρ=1−6⋅45⋅24=0.8\rho = 1 - \dfrac{6\cdot4}{5\cdot24} = 0.8.
    RatMath code
    stats.spearmanTest([1, 2, 3, 4, 5], [2, 1, 4, 3, 5]).statistic
    Output
    4/5
  19. 19.Kruskal–Wallis testAgrees
    Find the Kruskal–Wallis HH for {1,2,3}\{1,2,3\}, {4,5,6}\{4,5,6\}, {7,8,9}\{7,8,9\}.
    Independent answer
    Rank sums 6, 15, 24. H=129⋅10(36+225+5763)−3⋅10=12⋅27990−30=365H = \dfrac{12}{9\cdot10}\left(\dfrac{36 + 225 + 576}{3}\right) - 3\cdot10 = \dfrac{12\cdot279}{90} - 30 = \dfrac{36}{5}.
    RatMath code
    stats.kruskalWallisTest([[1, 2, 3], [4, 5, 6], [7, 8, 9]]).statistic
    Output
    36/5
  20. 20.Difference of proportionsAgrees
    In two samples of 100, 45 and 30 people agree. Give a 95% confidence interval for p1−p2p_1 - p_2.
    Independent answer
    0.15±1.960.45⋅0.55100+0.3⋅0.7100=0.15±1.96(0.06745)≈(0.0174,0.2826)0.15 \pm 1.96\sqrt{\tfrac{0.45\cdot0.55}{100} + \tfrac{0.3\cdot0.7}{100}} = 0.15 \pm 1.96(0.06745) \approx (0.0174, 0.2826).
    RatMath code
    const I = stats.proportionTest([45, 30], [100, 100]).interval();
    [I.low, I.high]
    Output
    [0.017430493307564781223097…, 0.282569506692435218776902…]