<jupyter_start><jupyter_text>Formatting and Analysis =====================<jupyter_code>import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import sktime
data = pd.read_csv('/home/zone/Documents/advantage-investing/data/raw/Goog.csv')
data
data.dtypes
data.columns
data[:][:1]
for col in data.drop(columns=['Unnamed: 0']):
    if str(col) !=['Unnamed: 0']:
        print(str(col))
        data[col] = data[col].str.split('.').str[0]
for col in data.drop(columns=['Unnamed: 0']):
    print(str(col))
    data[col] = data[col].str.strip(',')
df.Close
df = data.replace(',','', regex=True)
df.Date = pd.to_datetime(df.Date)
df.Close = pd.to_numeric(df.Close)
df = df.set_index(df.Date,drop = True)
df = pd.DataFrame(df.Close)
df
plt.figure(figsize=(9,5))
plt.grid(True)
plt.xlabel('')
plt.ylabel('')

plt.yticks(np.arange(0, 3000, 100))
plt.plot(df['Close'])
plt.title('goog closing price')
plt.show()
plt.figure(figsize=(10,6))
df_close = df.Close
df_close.plot(style='k.')
plt.title('Scatter plot of closing price')
plt.show()<jupyter_output><empty_output><jupyter_text>Is the data stationary? Data points are often non-stationary or have means, variances, and covariances that change over time. Non-stationary behaviors like trends are unpredictable and produce unreliable forecasts<jupyter_code>from statsmodels.tsa.stattools import adfuller

def test_stationarity(timeseries):
    #Determing rolling statistics
    rolmean = timeseries.rolling(12).mean()
    rolstd = timeseries.rolling(12).std()
    #Plot rolling statistics:
    plt.plot(timeseries, color='yellow',label='Original')
    plt.plot(rolmean, color='red', label='Rolling Mean')
    plt.plot(rolstd, color='black', label = 'Rolling Std')
    plt.legend(loc='best')
    plt.title('Rolling Mean and Standard Deviation')
    plt.show(block=False)
    print("Results of dickey fuller test")
    adft = adfuller(timeseries,autolag='AIC')
    # output for dft will give us without defining what the values are.
    #hence we manually write what values does it explains using a for loop
    output = pd.Series(adft[0:4],index=['Test Statistics','p-value','No. of lags used','Number of observations used'])
    for key,values in adft[4].items():
        output['critical value (%s)'%key] =  values
    print(output)

test_stationarity(df.Close)<jupyter_output><empty_output><jupyter_text>The mean and std  changes over time so our data is non stationary and looking at the graph the data is showing a trend. First we'll create train and test sets then we'll fit an ARIMA model to this data. ( ARIMA models use differencing to extract stationary data  from our originally non stationary data)<jupyter_code>df_log = df_log[::-1]
train_data, test_data = df_log[3:int(len(df_log)*0.9)], df_log[int(len(df_log)*0.9):]
plt.figure(figsize=(10,6))
plt.grid(True)
plt.xlabel('Dates')
plt.ylabel('Closing Prices')
plt.plot(df_log, 'green', label='Train data')
plt.plot(test_data, 'blue', label='Test data')
plt.legend()
len(test_data)
from sktime.forecasting.all import *

fh = FocastingHorizon(test_data.index, is_relative=False)
forecaster = ThetaForecaster(sp=12)
forecaster.fit(train_data)
y_pred = forecaster.predict(fh)
smape_loss(test_data, y_pred)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14641648723469702, 'fraction_numerical': 0.011073515841279606, 'mean_word_length': 5.530120481927711, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, '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': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '232299', 'license': 'FTL', 'n_tokens_mistral': 1175, 'n_tokens_neox': 1106, 'n_words': 277, 'path': 'notebooks/.ipynb_checkpoints/1.0-xs-analysis-checkpoint.ipynb', 'repo_name': 'SinAustin/advantage-investing', 'size': '3251'}