Sub topic 2
Comparing two means: independent and paired samples
This chapter assumes you know the mean, variance, standard deviation, z-scores, and the idea of drawing a sample from a population. It covers two-sample tests, variance tests, nonparametric alternatives, correlation, and simple linear regression.
Independent and paired samples
Use an independent-samples test when the two groups have no link, such as returns from two different managers. Use a paired test when each observation in one sample matches one in the other, such as the same fund measured before and after a fee change. For paired data, test the mean difference, dbar, with n - 1 degrees of freedom:
t = dbar / (s_d / sqrt(n))
Testing variances
The chi-square test checks one variance: chi2 = (n - 1) s^2 / sigma0^2, with n - 1 degrees of freedom. The F-test compares two variances: F = s1^2 / s2^2, with the larger sample variance in the numerator. It uses n1 - 1 and n2 - 1 degrees of freedom and assumes normal populations.
When to use nonparametric tests
Parametric tests assume a distribution, usually normal. When that assumption fails and the sample is small, use a nonparametric test. The Wilcoxon signed-rank test handles paired data, and the Mann-Whitney U test handles independent samples. Both compare ranks rather than means.
Covariance and correlation
Covariance measures how two variables move together. Correlation rescales covariance to lie between -1 and +1: r = cov(X, Y) / (sX sY).
Is the correlation different from zero?
Test H0: rho = 0 with t = r sqrt(n - 2) / sqrt(1 - r^2), using n - 2 degrees of freedom.
import numpy as np
from scipy import stats
a = np.array([5.1, 4.8, 6.0, 5.5, 5.9])
b = np.array([4.2, 4.9, 5.0, 4.4, 4.7])
t_ind, p_ind = stats.ttest_ind(a, b, equal_var=False)
t_rel, p_rel = stats.ttest_rel(a, b)
print(round(t_ind, 3), round(p_ind, 4))
print(round(t_rel, 3), round(p_rel, 4))
x = np.array([1.0, 2.0, 3.0, 4.0, 5.0])
y = np.array([2.1, 3.9, 6.2, 7.8, 10.1])
r = np.cov(x, y, ddof=1)[0, 1] / (x.std(ddof=1) * y.std(ddof=1))
print(round(r, 4), round(np.corrcoef(x, y)[0, 1], 4))The simple linear regression model
The model is Y = b0 + b1 X + e, where Y is the dependent variable, X is the independent variable, and e is the error term. Ordinary least squares chooses b0 and b1 to minimize the sum of squared residuals. The slope b1 is the change in Y for a one-unit change in X.
from scipy import stats
res = stats.linregress([1.0, 2.0, 3.0, 4.0, 5.0], [2.1, 3.9, 6.2, 7.8, 10.1])
print(round(res.slope, 3), round(res.intercept, 3), round(res.slope / res.stderr, 3))Exercise
A fund's monthly differences (after minus before) have mean 0.8 and standard deviation 1.2 over n = 16 months. Compute the paired t-statistic and decide at the 5% level. The two-tailed critical value with 15 degrees of freedom is 2.131.
Show answer
t = 0.8 / (1.2 / sqrt(16)) = 0.8 / 0.3 = 2.67. Since 2.67 > 2.131, reject H0: the mean difference is not zero.