The brief
A portfolio manager holds a mid-cap stock and hedges its market risk with index futures. The hedge ratio is the stock's beta. The risk committee wants to know whether the stock really moves more than the market, or whether a beta of 1 would do.
The data vendor sends two years of weekly closes for the stock and the index. The stock was suspended for one week, so one of its closes is missing.
Your task
- Write
weekly_returns(prices): the simple returns of a list of closes, as a NumPy array one shorter than the list. A missing close (None) makes both returns that touch itNaN. RaiseValueErrorfor a close that is zero or negative, or for fewer than two closes. - Write
capm_beta(stock, market, rf): regress the stock's excess returns on the market's, , by least squares, using only the weeks where both returns are known.rfis one number or one rate per week. Return a dict withalpha,beta, their standard errorsse_alphaandse_beta, andn, the number of weeks used. - Write
beta_test(stock, market, rf, b=1.0, level=0.95): the t test of and a confidence interval for . Return a dict witht, the two-sidedp_value, and the ends of the interval,lowandhigh.
capm_beta and beta_test raise ValueError when the two series differ in length, or when fewer than three weeks have both returns. They also raise it when the market's excess return is the same every week. beta_test raises it when level is not strictly between 0 and 1.
The data
STOCK and INDEX hold 105 weekly closes, oldest first, with None for the stock's missing close. RF_WEEKLY is the risk-free rate per week: 4% a year divided by 52. The prices are synthetic.
Hint: Where to start
Case study 5.10.5 estimates a beta this way from monthly excess returns. Fit the line with a column of ones beside the market's excess returns, as in lesson 5.10.2.
Hint: Standard errors and the test
Lesson 5.10.3 gives , with for a line. The t statistic for is , with degrees of freedom.
Hint: The missing week
A return across the gap covers two weeks, so its variance is about twice the others'. Lesson 5.10.3 explains why changing variance makes the standard errors wrong. Drop both returns that touch the missing close.