Day 5: In-Class Exercises
Hypothesis Tests III (binomial, t-tests, chi-square), correlation and simple linear regression, and doing all of it in R. Work each part by hand first, then open the solution to check, and run the R command to confirm. Every number below was verified with scipy.
A vendor claims a trading signal is profitable more often than chance. In $n = 20$ independent trades the signal was correct on $14$. Test $H_0: p = 0.5$ against $H_1: p > 0.5$ at $\alpha = 0.05$.
- State the test statistic (number of successes) and the sample proportion $\hat{p}$.
- Compute the exact one-sided $p$-value $P(X \ge 14 \mid p = 0.5)$.
- State the decision and interpret it in plain words.
Show solution
(a) Successes $X = 14$, $\hat{p} = 14/20 = 0.70$. Under $H_0$, $X \sim \text{Binomial}(20, 0.5)$.
(b) $p\text{-value} = P(X \ge 14) = \sum_{k=14}^{20} \binom{20}{k} 0.5^{20} = 0.0577$.
(c) Since $0.0577 > 0.05$, fail to reject $H_0$. Despite a 70% hit rate, 20 trades are too few to rule out chance at the 5% level (a borderline result: it would be significant at $\alpha = 0.10$).
A loan portfolio is rated for a default rate of at most $5\%$. In a sample of $n = 80$ loans, $8$ defaulted. Test $H_0: p = 0.05$ against $H_1: p > 0.05$ at $\alpha = 0.05$.
- Compute $\hat{p}$.
- Compute the exact one-sided $p$-value $P(X \ge 8 \mid p = 0.05)$.
- Decision and interpretation for the risk team.
Show solution
(a) $\hat{p} = 8/80 = 0.10$, twice the rated rate.
(b) $X \sim \text{Binomial}(80, 0.05)$, $p\text{-value} = P(X \ge 8) = 0.0466$.
(c) Since $0.0466 < 0.05$, reject $H_0$: there is (just) significant evidence the true default rate exceeds 5%. Note how close this is to the threshold, see E3.
Reusing E2 ($n = 80$, $\hat{p} = 0.10$, $p_0 = 0.05$), some analysts use the normal approximation $z = \dfrac{\hat{p} - p_0}{\sqrt{p_0(1-p_0)/n}}$.
- Check the rule of thumb $n p_0 \ge 5$ and $n(1-p_0) \ge 5$. Is the approximation safe here?
- Compute $z$ and the approximate one-sided $p$-value.
- Compare with the exact $p$-value from E2 and state which you would report.
Show solution
(a) $n p_0 = 80 \times 0.05 = 4 < 5$. The success count is too small, so the normal approximation is not safe.
(b) $z = \dfrac{0.10 - 0.05}{\sqrt{0.05 \times 0.95 / 80}} = \dfrac{0.05}{0.02437} = 2.052$, giving approximate $p = 1 - \Phi(2.052) = 0.0201$.
(c) The approximation ($0.0201$) is much smaller than the exact value ($0.0466$); it overstates the significance near the boundary. With $np_0 < 5$, report the exact binomial test.
A fund targets a mean monthly return of $1.0\%$. Ten recent monthly returns (%) are:
$1.2,\ 0.8,\ 1.5,\ 0.9,\ 1.1,\ 1.4,\ 0.7,\ 1.3,\ 1.0,\ 1.6$
Test $H_0: \mu = 1.0$ vs $H_1: \mu \ne 1.0$ at $\alpha = 0.05$.
- Compute $\bar{x}$ and the sample sd $s$.
- Compute $t = \dfrac{\bar{x} - \mu_0}{s/\sqrt{n}}$ and its degrees of freedom.
- Decision, and a 95% confidence interval for $\mu$.
Show solution
(a) $\bar{x} = 1.150$, $s = 0.3028$ (with $n - 1 = 9$).
(b) $t = \dfrac{1.150 - 1.0}{0.3028/\sqrt{10}} = 1.567$ on $\text{df} = 9$. Two-sided $p = 0.1516$.
(c) Since $0.1516 > 0.05$, fail to reject $H_0$. 95% CI for $\mu$: $1.150 \pm 2.262 \times 0.0958 = [0.933,\ 1.367]$, which contains $1.0$, consistent with the decision.
A desk tweaks its execution algorithm and records the daily return (%) of 8 assets before and after the change:
| Asset | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
|---|---|---|---|---|---|---|---|---|
| Before | 4.1 | 5.2 | 3.8 | 6.0 | 4.5 | 5.5 | 3.9 | 4.8 |
| After | 4.6 | 5.0 | 4.5 | 6.4 | 5.2 | 5.7 | 4.6 | 5.3 |
Test whether the change improved returns ($H_0: \mu_d = 0$ vs $H_1: \mu_d \ne 0$, where $d = $ after $-$ before).
- Form the differences and compute $\bar{d}$ and $s_d$.
- Compute $t = \dfrac{\bar{d}}{s_d/\sqrt{n}}$, df, and the decision.
- Give a 95% CI for the mean improvement.
Show solution
(a) $d = 0.5, -0.2, 0.7, 0.4, 0.7, 0.2, 0.7, 0.5$. $\bar{d} = 0.4375$, $s_d = 0.3114$.
(b) $t = \dfrac{0.4375}{0.3114/\sqrt{8}} = 3.974$ on $\text{df} = 7$, $p = 0.0054$. Since $p < 0.05$, reject $H_0$: the change significantly improved returns.
(c) 95% CI for $\mu_d$: $0.4375 \pm 2.365 \times 0.1101 = [0.177,\ 0.698]$ (does not contain 0).
Two funds report monthly returns (%). Fund A: $8.2, 7.5, 9.1, 8.8, 7.9, 8.5$. Fund B: $6.9, 7.2, 6.5, 7.8, 6.7, 7.1$. Assuming equal variances, test $H_0: \mu_A = \mu_B$ vs $H_1: \mu_A \ne \mu_B$.
- Compute the group means and the pooled sd $s_p$.
- Compute $t = \dfrac{\bar{x}_A - \bar{x}_B}{s_p\sqrt{1/n_A + 1/n_B}}$, df, and the decision.
- When would you switch to Welch's test instead?
Show solution
(a) $\bar{x}_A = 8.333$, $\bar{x}_B = 7.033$. $s_p = \sqrt{\dfrac{(n_A-1)s_A^2 + (n_B-1)s_B^2}{n_A + n_B - 2}} = 0.526$.
(b) $t = \dfrac{8.333 - 7.033}{0.526\sqrt{1/6 + 1/6}} = 4.281$ on $\text{df} = 10$, $p = 0.0016$. Reject $H_0$: the funds' mean returns differ.
(c) Use Welch's test (var.equal = FALSE, the R default) when the two groups' variances differ noticeably, especially with unequal sample sizes.
For each scenario, name the single most appropriate Day-5 test (binomial, one-sample t, two-sample t, paired t, chi-square goodness-of-fit, or chi-square independence).
- Is a coin-flip-style "buy signal" correct more than 50% of the time over 30 trades?
- Does the mean daily return of one stock differ from zero?
- Do two independent groups of clients have the same mean portfolio size?
- Did clients' risk scores change after a tailored advisory session (same clients, before/after)?
- Are trade volumes spread evenly across the five weekdays?
- Is sector (Tech/Finance/Energy) associated with whether a stock rose or fell?
Show solution
(a) Binomial test. (b) One-sample t-test. (c) Two-sample (independent) t-test. (d) Paired t-test (same subjects measured twice). (e) Chi-square goodness-of-fit (one categorical variable vs a uniform expectation). (f) Chi-square test of independence (two categorical variables in a contingency table).
Over one week, the number of trades per weekday was Mon $22$, Tue $18$, Wed $20$, Thu $25$, Fri $15$ (total $100$). Test whether trades are uniformly distributed across the five days at $\alpha = 0.05$.
- State the expected count per day under $H_0$.
- Compute $\chi^2 = \sum \dfrac{(O_i - E_i)^2}{E_i}$ and its df.
- Decision and interpretation.
Show solution
(a) Under uniformity, $E_i = 100/5 = 20$ for each day.
(b) $\chi^2 = \dfrac{2^2 + 2^2 + 0^2 + 5^2 + 5^2}{20} = \dfrac{58}{20} = 2.900$ on $\text{df} = k - 1 = 4$. $p = 0.5747$.
(c) Since $0.5747 > 0.05$, fail to reject $H_0$: no evidence that trades depart from a uniform weekday pattern.
A sample of stocks is cross-classified by sector and by whether the price rose or fell on the day:
| Up | Down | |
|---|---|---|
| Tech | 30 | 10 |
| Finance | 20 | 20 |
| Energy | 15 | 25 |
Test $H_0$: sector and direction are independent, at $\alpha = 0.05$.
- Compute the expected count for the Tech/Up cell.
- State the degrees of freedom.
- Given $\chi^2 = 11.748$, state the decision and interpret.
Show solution
(a) Row total (Tech) $= 40$, column total (Up) $= 65$, grand total $= 120$. $E_{\text{Tech,Up}} = \dfrac{40 \times 65}{120} = 21.67$.
(b) $\text{df} = (r-1)(c-1) = (3-1)(2-1) = 2$.
(c) $\chi^2 = 11.748$ on 2 df gives $p = 0.0028 < 0.05$, so reject $H_0$: direction depends on sector (Tech skews up, Energy skews down).
The chi-square approximation relies on sufficiently large expected counts.
- State the common rule of thumb for expected cell counts.
- For E9, the smallest expected count is $18.33$. Is the assumption satisfied?
- What would you do if several expected counts were below the threshold?
Show solution
(a) All expected counts should be $\ge 5$ (and none $= 0$); some texts allow up to 20% of cells between 1 and 5.
(b) Yes: the minimum expected count ($18.33$) comfortably exceeds 5, so the chi-square approximation is reliable here.
(c) Combine sparse categories, collect more data, or use an exact alternative (Fisher's exact test for a contingency table).
Two assets' returns (%) over six periods:
| $x$ (A) | 2 | 4 | 6 | 8 | 10 | 12 |
|---|---|---|---|---|---|---|
| $y$ (B) | 3 | 5 | 4 | 8 | 9 | 11 |
- Compute the sample covariance $\text{Cov}(x,y) = \dfrac{1}{n-1}\sum (x_i-\bar{x})(y_i-\bar{y})$.
- Compute the Pearson (Bravais-Pearson) correlation $r = \dfrac{\text{Cov}(x,y)}{s_x s_y}$.
- Interpret the sign and strength.
Show solution
(a) $\bar{x} = 7$, $\bar{y} = 6.667$. $\text{Cov}(x,y) = 11.200$.
(b) $r = 0.9529$.
(c) Strong positive linear association: the two assets tend to move up and down together (relevant for diversification, high $r$ means little diversification benefit).
A risk factor $x$ and a loss measure $y$ are related by $y = x^2$:
| $x$ | 1 | 2 | 3 | 4 | 5 | 6 |
|---|---|---|---|---|---|---|
| $y$ | 1 | 4 | 9 | 16 | 25 | 36 |
- The Pearson correlation is $r = 0.9789$. Why is it not exactly 1 even though $y$ is a function of $x$?
- Compute the Spearman rank correlation $\rho$.
- Which coefficient better captures this relationship, and why?
Show solution
(a) Pearson measures linear association. The relationship is curved (quadratic), so $r < 1$ even though it is a perfect functional relationship.
(b) Both $x$ and $y$ are strictly increasing, so their ranks are identical: $\rho = 1.0000$.
(c) Spearman, because it measures monotone association via ranks and is therefore robust to nonlinearity (and to outliers).
Six $(x,y)$ observations: $(1,2), (2,5), (3,1), (4,4), (5,3)$, and one extreme point $(15,15)$.
- Why should you always plot the data before reporting a correlation?
- The Pearson correlation of all six points is $r = 0.9308$. After removing $(15,15)$ it is $r = 0.1000$. What does this tell you?
- What is a more robust alternative here?
Show solution
(a) A single high-leverage point can manufacture or destroy a correlation; a scatterplot reveals it instantly, a single number does not.
(b) The "strong" correlation is entirely an artifact of one outlier. The first five points are essentially uncorrelated ($r \approx 0.10$); the outlier alone drives $r$ up to $0.93$.
(c) Spearman's rank correlation (less sensitive to extreme values), after investigating whether the outlier is a data error or a genuine observation.
Market excess return $x$ (%) and a stock's excess return $y$ (%) over five periods:
| $x$ | -2 | -1 | 0 | 1 | 2 |
|---|---|---|---|---|---|
| $y$ | -3 | -1 | 0 | 2 | 4 |
Fit $y = a + b x$ by least squares (the slope $b$ is the stock's beta).
- Compute $S_{xx} = \sum (x_i-\bar{x})^2$ and $S_{xy} = \sum (x_i-\bar{x})(y_i-\bar{y})$.
- Compute the slope $b = S_{xy}/S_{xx}$ and intercept $a = \bar{y} - b\bar{x}$.
- Compute $R^2$ and interpret the slope.
Show solution
(a) $\bar{x} = 0$, $\bar{y} = 0.4$. $S_{xx} = 4+1+0+1+4 = 10$, $S_{xy} = 6+1+0+2+8 = 17$.
(b) $b = 17/10 = 1.700$, $a = 0.4 - 1.7(0) = 0.400$.
(c) $R^2 = 0.9897$: about 99% of the variation in the stock's excess return is explained by the market. The beta of $1.7$ means the stock is aggressive, it moves about 1.7% for each 1% market move.
After fitting a regression you plot residuals against fitted values. For each pattern, state what it indicates and a possible fix.
- A random, horizontal band of points around 0.
- A funnel shape (spread grows with the fitted value).
- A clear U-shape or curve.
Show solution
(a) Assumptions look fine: constant variance and linearity, no action needed.
(b) Heteroscedasticity (non-constant variance). Fix: transform $y$ (e.g. log), use weighted least squares, or robust standard errors.
(c) Nonlinearity, the straight-line model is misspecified. Fix: add a polynomial term, transform a variable, or use a nonlinear model.
A normal Q-Q plot of the residuals checks the normality assumption. Interpret each pattern.
- Points lie close to the 45-degree reference line.
- Points curve away below the line at the left end and above it at the right end (an S that bends out at both tails).
- A pronounced upward curve across the whole plot.
Show solution
(a) Residuals are approximately normal, the assumption holds.
(b) Heavy tails (more extreme values than a normal): both tails depart from the line. Inference based on normality may understate tail risk.
(c) Skewness: a systematic curve indicates the residual distribution is asymmetric (right-skew if it bends upward). Consider transforming $y$.
- What are the two group means and the estimated difference?
- At $\alpha = 0.05$, what is the decision?
- Why does the conclusion agree with the confidence interval?
Show solution
(a) Means $8.333$ and $7.033$; estimated difference $\approx 1.300$.
(b) $p = 0.00161 < 0.05$, so reject $H_0$: the means differ.
(c) The 95% CI for the difference $[0.6234,\ 1.9766]$ excludes 0, which is equivalent to rejecting $H_0$ at the 5% level.
- Write the fitted equation.
- Is the slope significant at $\alpha = 0.05$? Is the intercept?
- What fraction of the variation in $y$ does the model explain?
Show solution
(a) $\hat{y} = 0.400 + 1.700\,x$.
(b) Slope: $p = 0.00044 < 0.05$, significant. Intercept: $p = 0.0663 > 0.05$, not significant (consistent with a CAPM alpha indistinguishable from 0).
(c) $R^2 = 0.9897$, about 99% of the variation in $y$ is explained.
- How many rows and columns did the table have (from the df)?
- State the decision at $\alpha = 0.05$ and what it means for the two variables.
Show solution
(a) $\text{df} = (r-1)(c-1) = 2$. With 2 columns ($c = 2$), $r - 1 = 2$, so $r = 3$ rows: a $3 \times 2$ table.
(b) $p = 0.0028 < 0.05$, reject independence: the two categorical variables are associated.
- For the Pearson test, report the correlation and whether it is significant at $\alpha = 0.05$.
- The Spearman output shows $\rho = 1$. What does that say about the relationship?
Show solution
(a) $r = 0.953$, $p = 0.0033 < 0.05$, significant positive linear correlation.
(b) $\rho = 1$ means a perfect monotone relationship: as $x$ increases, $y$ always increases (here exactly, since $y = x^2$ is increasing over the sampled range).