Royal Bank of Scotland stocks: Calculating Beta for Capital Asset Pricing Model

In finance, the capital asset pricing model (CAPM) is a model used to determine a theoretically appropriate required rate of return of an asset, to make decisions about adding assets to a well-diversified portfolio.

Along with such parameters as the expected return of the market and the expected return of a theoretical risk-free asset, the model takes into account the asset's sensitivity to non-diversifiable risk, often represented by the quantity beta (β).

The beta coefficient is a measure of the volatility, or systematic risk, of an individual stock in comparison to the volatility of the entire market.

Let's calculate the beta coefficient for the CAMP model.

In [2]:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import datetime

First, we shall go to Yahoo Finance and download The Royal Bank of Scotland Group plc (RBS) historical prices and the historical data on S&P 500 Index.
We are interested in monthly values in particular:

In [3]:
df_rbs = pd.read_csv(r'C:\Users\user-dns\Desktop\Disc R\Python Basics\RBS.csv', parse_dates = True, index_col = 'Date')
df_gspc = pd.read_csv(r'C:\Users\user-dns\Desktop\Disc R\Python Basics\^GSPC.csv', parse_dates = True, index_col = 'Date')
In [4]:
df_rbs.head()
Out[4]:
Open High Low Close Adj Close Volume
Date
2016-08-01 NaN NaN NaN NaN NaN NaN
2016-09-01 5.39 5.53 4.47 4.66 4.434402 35896200.0
2016-10-01 4.59 4.87 4.18 4.63 4.405854 41482400.0
2016-11-01 4.67 5.34 4.51 4.85 4.615203 39037600.0
2016-12-01 5.00 5.72 4.87 5.53 5.262284 35871100.0
In [5]:
df = pd.concat([df_rbs['Close'], df_gspc['Close']], axis = 1)
df.columns = ['RBS','^GSPC']
In [6]:
df.head()
Out[6]:
RBS ^GSPC
Date
2016-08-01 NaN NaN
2016-09-01 4.66 2168.270020
2016-10-01 4.63 2126.149902
2016-11-01 4.85 2198.810059
2016-12-01 5.53 2238.830078
In [7]:
df_returns = df.pct_change(1).dropna()
In [8]:
df_returns.head()
Out[8]:
RBS ^GSPC
Date
2016-10-01 -0.006438 -0.019426
2016-11-01 0.047516 0.034175
2016-12-01 0.140206 0.018201
2017-01-01 0.016275 0.017884
2017-02-01 0.046263 0.037198

By this point, we have selected the Close values of RBS and S&P 500, merged the two tables and calculated montly percentage change.
Let's plot now the monthly percentage change values to see how RBS prices and the S&P500 index related to each other historically:

In [9]:
plt.figure(figsize=(20,10))
df_returns['RBS'].plot()
df_returns['^GSPC'].plot()
plt.ylabel('Daily Return of The Royal Bank of Scotland Group plc (RBS) VS S&P500')
plt.show
Out[9]:
<function matplotlib.pyplot.show(*args, **kw)>
In [36]:
import seaborn as sns
X = df_returns['^GSPC']
y = df_returns['RBS']
sns.regplot(X, y, data = df_returns)
Out[36]:
<matplotlib.axes._subplots.AxesSubplot at 0x1db9ff75390>

Finally, we use OLS linear regression to find the beta coefficient:

In [34]:
from sklearn.linear_model import LinearRegression
lm = LinearRegression()
X = np.asanyarray(df_returns[['^GSPC']]) 
y = np.asanyarray(df_returns[['RBS']])
lm.fit (X, y)
print ('Beta coefficient: ', lm.coef_)
Beta coefficient:  [[1.03383276]]

An alternative way is to use Statsmodels library.
Along with the coefficient of relative volatility Beta, it has a detailed summary of the regression results:

In [35]:
import statsmodels.api as sm
X = sm.add_constant(X)
model = sm.OLS(y, X)
results = model.fit()
print(results.summary())
                            OLS Regression Results                            
==============================================================================
Dep. Variable:                      y   R-squared:                       0.280
Model:                            OLS   Adj. R-squared:                  0.258
Method:                 Least Squares   F-statistic:                     12.82
Date:                Tue, 13 Aug 2019   Prob (F-statistic):            0.00109
Time:                        12:54:01   Log-Likelihood:                 49.664
No. Observations:                  35   AIC:                            -95.33
Df Residuals:                      33   BIC:                            -92.22
Df Model:                           1                                         
Covariance Type:            nonrobust                                         
==============================================================================
                 coef    std err          t      P>|t|      [0.025      0.975]
------------------------------------------------------------------------------
const         -0.0048      0.011     -0.457      0.650      -0.026       0.017
x1             1.0338      0.289      3.581      0.001       0.446       1.621
==============================================================================
Omnibus:                        1.034   Durbin-Watson:                   1.660
Prob(Omnibus):                  0.596   Jarque-Bera (JB):                1.046
Skew:                           0.353   Prob(JB):                        0.593
Kurtosis:                       2.533   Cond. No.                         28.3
==============================================================================

Warnings:
[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.

In both cases we found the beta coefficient of RBS stocks to be equal to approx 1.03. Naturally, Beta of the market is 1. That means the Royal Bank of Scotland stocks deviate from the macro market only slightly and in general follow the S&P 500 Index

Let's see if the coefficient we got is close enough to the official one.
Yep, it's pretty close indeed:

In [30]:
from IPython.display import Image
Image('RBS.jpg', width=700, height=700)
Out[30]:
In [ ]: