Book Image

Python for Finance

By : Yuxing Yan
Book Image

Python for Finance

By: Yuxing Yan

Overview of this book

Table of Contents (20 chapters)
Python for Finance
About the Author
About the Reviewers

Linear regression and Capital Assets Pricing Model (CAPM)

According to the famous CAPM, the returns of a stock are linearly correlated with its market returns. Usually, we consider the relationship of the excess stock returns versus the excess market returns.

Here Ri is the stock i's return; is the slope (market risk); Rmkt is the market return and Rf is the risk-free rate. Eventually, the preceding equation could be rewritten as follows:

The following lines of code are an example of this:

>>>from scipy import stats
>>>stock_ret = [0.065, 0.0265, -0.0593, -0.001,0.0346]
>>>mkt_ret  = [0.055, -0.09, -0.041,0.045,0.022]
>>>beta, alpha, r_value, p_value, std_err =
>>>print beta, alpha 
0.507743187877  -0.00848190035246
>>>print "R-squared=", r_value**2
R-squared =0.147885662966
>>>print "p-value =", p_value