<jupyter_start><jupyter_text>Run CAPM, APT, ARCH, GARCH, and EGARCH models on Texas Istrument Stock data and visualize and compare each model to each other. TXN<jupyter_code>import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import datetime
import seaborn as sns
import warnings
import yfinance as yf
import seaborn as sns; sns.set()
yf.pdr_override() 
%matplotlib inline
warnings.filterwarnings('ignore')
stocks = ['TXN','VBINX']
start = datetime.datetime(2000,1, 1)
end = datetime.datetime(2020, 1, 1)
stock_prices = yf.download(stocks, start=start, end = end, interval='3mo')
#Additional statistics from yfinance per ticker.
#Txn.info
#Txn.dividends
#Txn.major_holders
#Txn.recommendations
#Txn.quarterly_earnings
#Txn.earnings
#Txn.sustainability
#Txn.cashflow
stock_prices.head()
stock_prices=stock_prices['Close']
stock_prices.dropna(inplace=True)
stock_prices.describe()
stock_prices.head()
stocks = [stock_prices]
print(stocks)

k=1
for i in range(0, len(stock_prices.columns)):
    sns.set
    plt.figure(figsize=(15, 5))
    plt.subplot(2, 2, k)
    plt.plot(stock_prices[stock_prices.columns[i]])
    plt.title("{}".format(stock_prices.columns[i]))
    k+=1
    plt.tight_layout()
    #plt.legend()
    plt.show()
heat_map_corr=stock_prices.corr()
sns.heatmap(heat_map_corr, annot=True)
plt.title("Correlation Matrix")
plt.show()
returns = stock_prices.pct_change()
returns.dropna(inplace=True)
returns.head()
from fredapi import Fred
fred = Fred(api_key='<KEY>')
fred.search('risk free')
risk_free=fred.get_series('GS3M')
risk_free=risk_free['2000-01-01' : '2020-01-01']
rf=risk_free.pct_change()
plt.plot(risk_free)
plt.xlabel('Date')
plt.ylabel('%')
plt.title('3-Month Treasury Constant Maturity Rate')
plt.tight_layout()
plt.show()
stocks = ['TXN', 'VBINX']
for i in stocks:
     returns["excess_return_"+str(i)]=returns[i]-rf
#returns= returns - rf
#returns.dropna(inplace=True)
returns.head()
import statsmodels.api as sm
Y_TXN= returns['excess_return_TXN']
X=returns['excess_return_VBINX']

X =sm.add_constant(X, prepend=False)
CAPM_TXN=sm.OLS(Y_TXN, X)
CAPM_results_TXN = CAPM_TXN.fit()
################  Capital Allocation Pricing Model RESULTS #############
CAPM_results_TXN.summary()
fred.search('industrial')
ind=fred.get_series('IPMAN')
ind=ind['2000-01-01' : '2020-01-01']
ind.head()
log_ind=np.log(ind.iloc[1:])
fred.search('potential inflation')
inf=fred.get_series('CPIECOMEDU')
inf=inf['2000-01-01' : '2020-01-01']
inf_rate=inf.pct_change().dropna()
df_TXN.dropna()

df_TXN=pd.concat([returns.excess_return_TXN, inf_rate, log_ind, returns.excess_return_TXN], axis =1)
df_TXN=df_TXN.rename({0:'inf_rate',1: 'log_ind'}, axis='columns')
#df_TXN.dropna()
df_TXN.head().dropna()

y=df_TXN['excess_return_TXN']
X_TXN= df_TXN.iloc[:,1:]
X_TXN= sm.add_constant(X_TXN, prepend=False)
APT_TXN= sm.OLS(y, X_TXN)
APT_results_TXN = APT_TXN.fit()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16076949501889384, 'fraction_numerical': 0.02610786671246994, 'mean_word_length': 6.956284153005464, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '467415', 'license': 'MIT', 'n_tokens_mistral': 1257, 'n_tokens_neox': 1179, 'n_words': 210, 'path': 'Stock_Analysis.ipynb', 'repo_name': 'EdwardDantes/FinancialModeling', 'size': '2938'}