Capital Asset Pricing Model
Expected stock return is \(r_f + \beta \times \overline{r_m - r_f}\)
- Get today’s \(r_f\) from FRED
- Get historical \(\overline{r_m - r_f}\) from French’s data library
- Estimate beta by running regression \[r - r_f = \alpha + \beta (r_m-r_f) + \varepsilon\]
Let’s estimate the beta first.
Data for regression
The regression is: \[r - r_f = \alpha + \beta (r_m-r_f) + \varepsilon\]
- CVX return \(r\) from Yahoo adjusted closing prices
- Risk-free rate \(r_f\) from French’s data library
- Market excess return \(r_m-r_f\) from French’s data library
Compute CVX returns
- Ask Julius to pip install yfinance==0.1.70 and import yfinance as yf
- Then (all instructions at once to avoid repeated downloads) ask Julius to:
- use yf.Ticker to get CVX closing prices (= adjusted closing prices) since 2010
- downsample to end-of-month and compute returns as 100 times percent change
- convert dates to pandas monthly period format
Get Fama-French factors
Ask Julius to use the pandas datareader get the monthly Fama-French factors since 2010 from French’s data library,
Prepare data for the regression
- Ask Julius to do an inner merge on the dates of the CVX returns and Fama-French factors.
- Ask Julius to keep only the last 120 rows.
- Ask Julius to add the excess CVX return to the dataframe by subtracting RF from the CVX return.
- Ask Julius to show the head and tail of the dataframe.
Run the regression
- Ask Julius to regress the excess CVX return on MKT-RF, including an intercept, and show a summary of the results.
- Ask Julius to assign the MKT-RF regression coefficient to a new variable called beta.
Estimate the market risk premium
- Ask Julius to use pandas datareader to get the annual Fama-French factors from French’s data library starting in 1926.
- Ask Julius to compute the mean of MKT-RF and assign it to a new variablecalled market_premium.
Get the current risk-free rate
- Ask Julius to use pandas datareader to get 1-month Treasury bill yields from FRED.
- Ask Julius to get the most recent 1-month Treasury bill yield and assign it to a new variable called rf.
CVX cost of equity capital
Ask Julius to compute rf + beta * market_premium.
Visualize the regression
- Ask Julius to produce a scatter plot with the excess CVX return on the y axis and MKT-RF on the x axis.
- Tip: we might need to give Julius the name of the dataframe containing the excess CVX return and MKT-RF.
- Look back at the code where Julius showed the head and tail of the dataframe to find the name.
- Ask Julius to include the regression line on the scatter plot.
Visualization tips
- We can ask Julius to set the style of the plot. I like the seaborn whitegrid style.
- There are other choices: seaborn plot styles
- If Julius is balky at including the regression line, ask it to create a seaborn regplot with ci=None.
- You can also specify the colors of the points and the line (the line could be a different color than the points).
Interactive plots
- Suppose we’re giving a presentation in which we show the regression plot.
- We anticipate there might be questions about some of the points - e.g., extreme values.
- We could tag them with annotations, but it creates clutter.
- It might be useful to be able to hover over points and have pop-ups with details.
- This requires html.
Use plotly
- Ask Julius to use the dataframe containing the CVX excess return and MKT-RF and to convert the dates to strings.
- Ask Julius to use plotly to create a scatter plot with a regression line, the CVX excess return on the y-axis, and MKT-RF on the x-axis.
- Ask Julius to include the date in the hover data.
- Ask Julius to save the figure as html.
See the results
- Download the html file.
- Open it from Windows Explorer or the Mac Finder (it probably won’t work opening it from your web browser).
- Try out different plotly templates - e.g., plotly_dark.
- More visualization coming next week.