<jupyter_start><jupyter_text>Stock Linear Regression<jupyter_code># Library
import pandas as pd
import numpy as np

from sklearn.linear_model import LinearRegression
import math

import matplotlib.pyplot as plt
import seaborn as sns

import warnings
warnings.filterwarnings("ignore")

import fix_yahoo_finance as yf
yf.pdr_override()
stock_name = 'AMD'
start = '2010-01-01' 
end = '2018-09-27'
df = yf.download(stock_name, start, end)
df = df.reset_index()
X = df.index.values
y = df['Adj Close'].values
X.shape
X = X.reshape((2200,-1))
lr = LinearRegression(fit_intercept=True)
lr.fit(X, y)
df.data = df[['Open','High','Low','Volume']]
df.target = df['Adj Close']
## Degrees of freedom on dependent variable variance
df_v = X.shape[0] - 1
print('Degrees of freedom on variable:', df_v)
## Degrees of freedom of error variance
df_e = X.shape[0] - X.shape[1] - 1
print('Degrees of freedom of error:', df_e)<jupyter_output>Degrees of freedom on variable: 2199
Degrees of freedom of error: 2198<jupyter_text>Sum of Squared Errors (SSE) is a measurement of model's predictions from the actual values<jupyter_code>model = LinearRegression(fit_intercept=True)
model.fit(X, y)
squared_errors = (y - model.predict(X)) ** 2
sse = np.sum(squared_errors)
print('Sum of Squared Errors (SSE):', round(sse,4))<jupyter_output>Sum of Squared Errors (SSE): 39802.0693<jupyter_text>Total Sum of Squares (SST) is a measurement of the variance in the target variable.<jupyter_code>avg_y = np.mean(y)
squared_errors = (y - avg_y) ** 2
sst = np.sum(squared_errors)
print('Total Sum of Squares (SST):', round(sst,4))<jupyter_output>Total Sum of Squares (SST): 45736.2484<jupyter_text>R^2 is the measurement of the variances in dependent variable that is predictable from the indepdented variable. Is a goodness-of-fit measure for linear regression models. It measures the strength of the relationship between the model and the dependent variable. The scale between 0 to 100%.<jupyter_code>r_squared = 1 - (sse/sst)
print('R^2:', round(r_squared,4))<jupyter_output>R^2: 0.1297<jupyter_text>Adjusted R^2 is the measurement that increases if the new term improves the model more than would be expected by chance.<jupyter_code>adj_r = 1 - (sse/df_e) / (sst/df_v)
print('Adjusted R^2:', round(adj_r,4))
print('             Summary Key Metric              ')
print('__________________________________________')
print('Sum of Squared Errors (SSE): ', round(sse,4))
print('Total Sum of Squares (SST):  ', round(sst,4))
print('R^2:                         ', round(r_squared,4))
print('Adjusted R^2:                ', round(adj_r,4))
class Stats:
    
    def __init__(self, X, y, model):
        self.data = X
        self.target = y
        self.model = model
        ## degrees of freedom population dep. variable variance
        self._dft = X.shape[0] - 1   
        ## degrees of freedom population error variance
        self._dfe = X.shape[0] - X.shape[1] - 1  
    
    def sse(self):
        '''returns sum of squared errors (model vs actual)'''
        squared_errors = (self.target - self.model.predict(self.data)) ** 2
        return np.sum(squared_errors)
        
    def sst(self):
        '''returns total sum of squared errors (actual vs avg(actual))'''
        avg_y = np.mean(self.target)
        squared_errors = (self.target - avg_y) ** 2
        return np.sum(squared_errors)
    
    def r_squared(self):
        '''returns calculated value of r^2'''
        return 1 - self.sse()/self.sst()
    
    def adj_r_squared(self):
        '''returns calculated value of adjusted r^2'''
        return 1 - (self.sse()/self._dfe) / (self.sst()/self._dft)
def pretty_print_stats(stats_obj):
    '''returns report of statistics for a given model object'''
    items = ( ('sse:', stats_obj.sse()), ('sst:', stats_obj.sst()), 
             ('r^2:', stats_obj.r_squared()), ('adj_r^2:', stats_obj.adj_r_squared()) )
    for item in items:
        print('{0:8} {1:.4f}'.format(item[0], item[1]))
stats = Stats(X, y, lr)
pretty_print_stats(stats)
stock_name = 'AMD'
start = '2010-01-01' 
end = '2018-09-27'
df = yf.download(stock_name, start, end)
df = df.reset_index()
X = df.index.values
y = df['Adj Close'].values
x_reshape = X.reshape((2200,-1))
# Linear Model
linear = LinearRegression()
linear.fit(x_reshape, y)
# Non Linear Model
y_nonlinear = [x**3 + np.random.rand(1)*10 for x in range(2200)]
nonlinear = LinearRegression()
nonlinear.fit(x_reshape, y_nonlinear)
fig, axes = plt.subplots(1, 2, sharex=False, sharey=False)
fig.suptitle('[Residual Plots]')
fig.set_size_inches(15,8)
axes[0].plot(linear.predict(x_reshape), y-linear.predict(x_reshape), 'bo')
axes[0].axhline(y=0, color='k')
axes[0].grid()
axes[0].set_title('Linear')
axes[0].set_xlabel('predicted values')
axes[0].set_ylabel('residuals')
axes[1].plot(nonlinear.predict(x_reshape), y_nonlinear-nonlinear.predict(x_reshape), 'ro')
axes[1].axhline(y=0, color='k')
axes[1].grid()
axes[1].set_title('Non-Linear')
axes[1].set_xlabel('predicted values')
s1 = Stats(x_reshape, y_nonlinear, nonlinear)
pretty_print_stats(s1)
s1 = Stats(x_reshape, y, linear)
pretty_print_stats(s1)
residuals_linear = y - linear.predict(x_reshape)
residuals_nlinear = y_nonlinear - nonlinear.predict(x_reshape)

sns.distplot(residuals_linear);
plt.title('Linear')
sns.distplot(residuals_nlinear)
plt.title('Non-Linear')
from scipy.stats import normaltest
normaltest(residuals_linear)
normaltest(residuals_nlinear)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1397731430662276, 'fraction_numerical': 0.027442371020856202, 'mean_word_length': 4.4289970208540215, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '226244', 'license': 'MIT', 'n_tokens_mistral': 2094, 'n_tokens_neox': 1939, 'n_words': 484, 'path': 'Linear_Regression_Stock.ipynb', 'repo_name': 'sonukumarraj007/Deep-Learning-Machine-Learning-Stock', 'size': '5466'}