<jupyter_start><jupyter_text>Standard Linear Regression: The Market Model---An asset's Market Model (MM) is a time series regression of the the realized excess returns of the asset against the realized excess returns of the market. Excess returns are defined as returns in excess of some benchmark such as the risk-free rate on a US treasury bill or note. A security's MM is formulated as:**(R - F) = 𝛂 + 𝛃 (M - F) + 𝛔**Y = (R - F) is the outcome/dependent variable,  X = (M - F) is the predictor/independent variable, R is the realized return of a stock, F is the return on a risk-free asset (US treasury bill) , M is the realized return of a market portfolio (S&P 500), 𝛼 (alpha) is the expected stock-specific return, 𝛽 (beta) is the level of systematic risk exposure to the market, 𝛔 (sigma) is the unexpected stock-specific returnNote that an asset's MM is different from its Captial Asset Pricing Model (CAPM). Unlike the CAPM, an asset's MM has both an idiosyncratic risk term 𝛼 and an error term 𝛔 in its formulation. However, the systematic risk term beta of the asset is the same as the one calculated using the CAPM. The expected value of 𝛔 is zero. That is the reason it does not appear in the CAPM which formulates expected returns.According to the CAPM, which subscribes to the efficient market hypothesis, the intercept, or 𝛼, of an asset's MM has an expected value of zero. According to the CAPM, an asset's realized postive/(negative) 𝛼 shows that the market participants under priced/(overpiced) the security during the holding period.  Import your dataWe use the following free data sources to get the daily closing prices of various assets:Risk free rate: https://fred.stlouisfed.org/series/TB4WKStock and market data: https://finance.yahoo.com<jupyter_code>!pip install yfinance

import statsmodels.api as sm
import pandas_datareader.data as pdr
import yfinance as yf

import matplotlib.pyplot as plt
plt.style.use('seaborn')
from datetime import datetime
start = datetime(2017, 9, 14)
end = datetime(2021, 8, 31)

riskfree_rate = pdr.DataReader('TB4WK', 'fred', start, end)
market = yf.Ticker('SPY').history(start=start, end=end)
stock = yf.Ticker('AAPL').history(start=start, end=end)<jupyter_output><empty_output><jupyter_text>Explore your data<jupyter_code>riskfree_rate = pdr.DataReader('TB4WK', 'fred', start, end)
riskfree_scaling = 28 #Need to scale the risk free rate by its maturity to get the daily risk free rate
riskfree_rate = riskfree_rate['TB4WK'].dropna()/riskfree_scaling
riskfree_rate.plot(), plt.title('Daily Risk Free Rate'), plt.show()


market_return = market['Close'].pct_change(1)*100
market_return = market_return.dropna()
plt.hist(market_return), plt.title('Distribution of Market (SPY) Daily Returns'), plt.xlabel('Daily Percentage Returns'), plt.ylabel('Frequency'), plt.show()
print("Descriptive Stastics of the Market's daily percentage returns:\n{}".format(market_return.describe()))

stock_return = stock['Close'].pct_change(1)*100
stock_return = stock_return.dropna()
plt.hist(stock_return), plt.title('Distribution of Stock (AAPL) Daily Returns'), plt.xlabel('Daily Percentage Returns'), plt.ylabel('Frequency'), plt.show()
print("Descriptive stastics of Apple stock's percentage returns:\n{}".format(stock_return.describe()))<jupyter_output><empty_output><jupyter_text>Build your linear regression model<jupyter_code>#AAPL's Market Model
y = stock_return - riskfree_rate.mean()
x = market_return - riskfree_rate.mean()
plt.scatter(x,y)
x = sm.add_constant(x)
market_model = sm.OLS(y, x).fit()
plt.plot(x, x*market_model.params[1]+market_model.params[0])
plt.title('Market Model of AAPL'), plt.xlabel('SPY Daily Excess Returns'), plt.ylabel('AAPL Daily Excess Returns'), plt.show();
print("According to AAPL's Market Model, the security had a realized Alpha of {0}% and Beta of {1}".format(round(market_model.params[0],2), round(market_model.params[1],2)))<jupyter_output><empty_output><jupyter_text>Analyze your results<jupyter_code>print("The Market Model of AAPL is summarized below:\n{}".format(market_model.summary()));
plt.plot(market_model.resid);<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10628019323671498, 'fraction_numerical': 0.009420289855072464, 'mean_word_length': 5.9713804713804715, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '571480', 'license': 'MIT', 'n_tokens_mistral': 1374, 'n_tokens_neox': 1279, 'n_words': 465, 'path': 'Algorithmic Trading/2-Linear_Regression.ipynb', 'repo_name': 'krishnamanchikalapudi/examples.py', 'size': '4140'}