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.

Exercises: 20
Topics: 6
Solutions: verified
Block 1Binomial Test
E1Does a trading signal beat a coin flip?

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$.

  1. State the test statistic (number of successes) and the sample proportion $\hat{p}$.
  2. Compute the exact one-sided $p$-value $P(X \ge 14 \mid p = 0.5)$.
  3. 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$).

> binom.test(14, 20, p = 0.5, alternative = "greater") Exact binomial test number of successes = 14, number of trials = 20, p-value = 0.05766 alternative hypothesis: true probability of success is greater than 0.5 sample estimates: probability of success 0.7
E2Is the default rate above the rated 5%?

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$.

  1. Compute $\hat{p}$.
  2. Compute the exact one-sided $p$-value $P(X \ge 8 \mid p = 0.05)$.
  3. 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.

> binom.test(8, 80, p = 0.05, alternative = "greater") Exact binomial test number of successes = 8, number of trials = 80, p-value = 0.04655 alternative hypothesis: true probability of success is greater than 0.05
E3Exact test vs normal approximation

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}}$.

  1. Check the rule of thumb $n p_0 \ge 5$ and $n(1-p_0) \ge 5$. Is the approximation safe here?
  2. Compute $z$ and the approximate one-sided $p$-value.
  3. 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.

# exact is preferred; normal approx shown for contrast > phat <- 0.10; p0 <- 0.05; n <- 80 > z <- (phat - p0) / sqrt(p0 * (1 - p0) / n) > c(z = z, p_approx = 1 - pnorm(z)) z p_approx 2.051957 0.020084
Block 2t-Tests & Choosing the Right Test
E4One-sample t-test (small sample)

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$.

  1. Compute $\bar{x}$ and the sample sd $s$.
  2. Compute $t = \dfrac{\bar{x} - \mu_0}{s/\sqrt{n}}$ and its degrees of freedom.
  3. 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.

> x <- c(1.2,0.8,1.5,0.9,1.1,1.4,0.7,1.3,1.0,1.6) > t.test(x, mu = 1.0) One Sample t-test t = 1.5666, df = 9, p-value = 0.1516 95 percent confidence interval: 0.9334 1.3666 sample estimates: mean of x 1.15
E5Paired-difference t-test

A desk tweaks its execution algorithm and records the daily return (%) of 8 assets before and after the change:

Asset12345678
Before4.15.23.86.04.55.53.94.8
After4.65.04.56.45.25.74.65.3

Test whether the change improved returns ($H_0: \mu_d = 0$ vs $H_1: \mu_d \ne 0$, where $d = $ after $-$ before).

  1. Form the differences and compute $\bar{d}$ and $s_d$.
  2. Compute $t = \dfrac{\bar{d}}{s_d/\sqrt{n}}$, df, and the decision.
  3. 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).

> before <- c(4.1,5.2,3.8,6.0,4.5,5.5,3.9,4.8) > after <- c(4.6,5.0,4.5,6.4,5.2,5.7,4.6,5.3) > t.test(after, before, paired = TRUE) Paired t-test t = 3.9735, df = 7, p-value = 0.005378 95 percent confidence interval: 0.1771 0.6979 sample estimates: mean difference 0.4375
E6Two-sample t-test (equal variances)

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$.

  1. Compute the group means and the pooled sd $s_p$.
  2. Compute $t = \dfrac{\bar{x}_A - \bar{x}_B}{s_p\sqrt{1/n_A + 1/n_B}}$, df, and the decision.
  3. 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.

> A <- c(8.2,7.5,9.1,8.8,7.9,8.5); B <- c(6.9,7.2,6.5,7.8,6.7,7.1) > t.test(A, B, var.equal = TRUE) Two Sample t-test t = 4.2809, df = 10, p-value = 0.00161 95 percent confidence interval: 0.6234 1.9766 sample estimates: mean of x mean of y 8.333333 7.033333
E7Choose the right test

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).

  1. Is a coin-flip-style "buy signal" correct more than 50% of the time over 30 trades?
  2. Does the mean daily return of one stock differ from zero?
  3. Do two independent groups of clients have the same mean portfolio size?
  4. Did clients' risk scores change after a tailored advisory session (same clients, before/after)?
  5. Are trade volumes spread evenly across the five weekdays?
  6. 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).

Block 3Chi-Square Tests
E8Goodness-of-fit: are trades uniform across weekdays?

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$.

  1. State the expected count per day under $H_0$.
  2. Compute $\chi^2 = \sum \dfrac{(O_i - E_i)^2}{E_i}$ and its df.
  3. 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.

> obs <- c(22, 18, 20, 25, 15) > chisq.test(obs) # default expects equal proportions Chi-squared test for given probabilities X-squared = 2.9, df = 4, p-value = 0.5747
E9Independence: sector vs up/down

A sample of stocks is cross-classified by sector and by whether the price rose or fell on the day:

UpDown
Tech3010
Finance2020
Energy1525

Test $H_0$: sector and direction are independent, at $\alpha = 0.05$.

  1. Compute the expected count for the Tech/Up cell.
  2. State the degrees of freedom.
  3. 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).

> tbl <- matrix(c(30,10, 20,20, 15,25), nrow = 3, byrow = TRUE) > chisq.test(tbl) Pearson's Chi-squared test X-squared = 11.748, df = 2, p-value = 0.002808
E10Chi-square assumptions

The chi-square approximation relies on sufficiently large expected counts.

  1. State the common rule of thumb for expected cell counts.
  2. For E9, the smallest expected count is $18.33$. Is the assumption satisfied?
  3. 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).

Block 4Covariance, Pearson & Spearman
E11Covariance and Pearson correlation

Two assets' returns (%) over six periods:

$x$ (A)24681012
$y$ (B)3548911
  1. Compute the sample covariance $\text{Cov}(x,y) = \dfrac{1}{n-1}\sum (x_i-\bar{x})(y_i-\bar{y})$.
  2. Compute the Pearson (Bravais-Pearson) correlation $r = \dfrac{\text{Cov}(x,y)}{s_x s_y}$.
  3. 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).

> x <- c(2,4,6,8,10,12); y <- c(3,5,4,8,9,11) > cov(x, y) [1] 11.2 > cor(x, y) [1] 0.9528704
E12When Spearman beats Pearson

A risk factor $x$ and a loss measure $y$ are related by $y = x^2$:

$x$123456
$y$149162536
  1. The Pearson correlation is $r = 0.9789$. Why is it not exactly 1 even though $y$ is a function of $x$?
  2. Compute the Spearman rank correlation $\rho$.
  3. 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).

> x <- 1:6; y <- x^2 > cor(x, y) # Pearson [1] 0.9788935 > cor(x, y, method = "spearman") # Spearman [1] 1
E13Scatterplots and the outlier trap

Six $(x,y)$ observations: $(1,2), (2,5), (3,1), (4,4), (5,3)$, and one extreme point $(15,15)$.

  1. Why should you always plot the data before reporting a correlation?
  2. 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?
  3. 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.

> x <- c(1,2,3,4,5,15); y <- c(2,5,1,4,3,15) > cor(x, y) [1] 0.9308 > cor(x[1:5], y[1:5]) # drop the outlier [1] 0.1 > plot(x, y) # always look
Block 5Simple Linear Regression & Diagnostics
E14Fitting a CAPM-style line by hand

Market excess return $x$ (%) and a stock's excess return $y$ (%) over five periods:

$x$-2-1012
$y$-3-1024

Fit $y = a + b x$ by least squares (the slope $b$ is the stock's beta).

  1. Compute $S_{xx} = \sum (x_i-\bar{x})^2$ and $S_{xy} = \sum (x_i-\bar{x})(y_i-\bar{y})$.
  2. Compute the slope $b = S_{xy}/S_{xx}$ and intercept $a = \bar{y} - b\bar{x}$.
  3. 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.

> x <- c(-2,-1,0,1,2); y <- c(-3,-1,0,2,4) > coef(lm(y ~ x)) (Intercept) x 0.4 1.7 > summary(lm(y ~ x))$r.squared [1] 0.9897
E15Reading a residual plot

After fitting a regression you plot residuals against fitted values. For each pattern, state what it indicates and a possible fix.

  1. A random, horizontal band of points around 0.
  2. A funnel shape (spread grows with the fitted value).
  3. 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.

E16Reading a normal Q-Q plot

A normal Q-Q plot of the residuals checks the normality assumption. Interpret each pattern.

  1. Points lie close to the 45-degree reference line.
  2. 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).
  3. 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$.

Block 6Reading R Output
E17Interpret a t.test() printout
Two Sample t-test data: A and B t = 4.2809, df = 10, p-value = 0.00161 alternative hypothesis: true difference in means is not equal to 0 95 percent confidence interval: 0.6234 1.9766 sample estimates: mean of x mean of y 8.333333 7.033333
  1. What are the two group means and the estimated difference?
  2. At $\alpha = 0.05$, what is the decision?
  3. 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.

E18Interpret an lm() summary
Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) 0.4000 0.1414 2.828 0.06628 . x 1.7000 0.1000 17.000 0.00044 *** Residual standard error: 0.3162 on 3 degrees of freedom Multiple R-squared: 0.9897, Adjusted R-squared: 0.9863 F-statistic: 289 on 1 and 3 DF, p-value: 0.00044
  1. Write the fitted equation.
  2. Is the slope significant at $\alpha = 0.05$? Is the intercept?
  3. 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.

E19Interpret a chisq.test() printout
Pearson's Chi-squared test data: tbl X-squared = 11.748, df = 2, p-value = 0.002808
  1. How many rows and columns did the table have (from the df)?
  2. 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.

E20Interpret cor.test() output
> cor.test(x, y) # Pearson t = 6.2806, df = 4, p-value = 0.003278 cor 0.9528704 > cor.test(x, y, method = "spearman") # on the y = x^2 data S = 0, p-value = 0.002778 rho 1
  1. For the Pearson test, report the correlation and whether it is significant at $\alpha = 0.05$.
  2. 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).