<jupyter_start><jupyter_text>Stock Market Price Prediction via Logistic Regression Author : <NAME>  TL;DR* Data: [Yahoo! Finance Market Data Downloader](https://pypi.org/project/yfinance/) * Methdology: using a very simple **logistic regression** to predict the next day's stock **movement**   Import Library<jupyter_code># Data Manipulation
import numpy as np
import pandas as pd
from pandas_datareader import data as pdr

# Finance 
import talib as ta
import yfinance as yf

# Machine Learning 
from sklearn import preprocessing
from sklearn.linear_model import LogisticRegression 
from sklearn import metrics 
from sklearn.model_selection import cross_val_score

# Plotting 
import matplotlib.pyplot as plt

# Utils 
import os<jupyter_output><empty_output><jupyter_text>Default Setting<jupyter_code>plt.rcParams['figure.figsize'] = [27.0, 9.0]
plt.rcParams['figure.dpi'] = 80
plt.rcParams['savefig.dpi'] = 100

plt.rcParams['xtick.labelsize'] = 18
plt.rcParams['ytick.labelsize'] = 18
plt.rcParams['font.size'] = 18
plt.rcParams['legend.fontsize'] = 'large'
plt.rcParams['figure.titlesize'] = 'medium'
STOCK_SYMBOL = 'GOOG'
START_DATE = '2010-01-01'
END_DATE = '2021-04-30'<jupyter_output><empty_output><jupyter_text>Import and Explore Data<jupyter_code>df = pdr.get_data_yahoo(STOCK_SYMBOL, START_DATE, END_DATE )
df = df.dropna()
df.reset_index(inplace=True)
df.head()
df.describe()
plt.plot(range(df.shape[0]),df['Close'], label= STOCK_SYMBOL)
plt.xticks(range(0,df.shape[0],28*3),df['Date'].loc[::28*3],rotation=60)
plt.xlabel('Date')
plt.ylabel('Close Price')
plt.legend(loc=0)
plt.show()<jupyter_output><empty_output><jupyter_text>Define Features<jupyter_code>df['Return_PCT'] = df['Close'].shift(1).pct_change().values
df['Return_PCT_MA'] = df['Return_PCT'].rolling(2).mean()
df['Activity_PCT'] = df['Volume'].shift(1).pct_change().values
df['Activity_PCT_MA'] = df['Activity_PCT'].rolling(2).mean()
df['Open-Close'] = (df['Open'].shift(1)-df['Close'].shift(2))/df['Close'].shift(2)
df = df.dropna()
df.head()
features = ['Return_PCT_MA', 'Activity_PCT_MA', 'Open-Close']
X = np.array(df[features])
df['delta_of_close_next_day'] = df['Adj Close'].shift(-1) - df['Adj Close']
y = np.where(df['delta_of_close_next_day'] > 0.0,1.0,-1.0)<jupyter_output><empty_output><jupyter_text>Split the Dataset for Training and Testing<jupyter_code>split = int(0.8*len(df))
X_train, X_test, y_train, y_test = X[:split], X[split:], y[:split], y[split:]<jupyter_output><empty_output><jupyter_text>Build Model<jupyter_code>model = LogisticRegression()
model = model.fit (X_train,y_train)<jupyter_output><empty_output><jupyter_text>Prediction of Next Day's Stock Movement via Logistic Regression<jupyter_code>probability = model.predict_proba(X_test)
predicted = model.predict(X_test)<jupyter_output><empty_output><jupyter_text>Evaluate Model  Confusion Matrix<jupyter_code>print(metrics.confusion_matrix(y_test, predicted))<jupyter_output>[[ 21 237]
 [ 20 288]]<jupyter_text>Classification Report<jupyter_code>print(metrics.classification_report(y_test, predicted))<jupyter_output>precision    recall  f1-score   support

        -1.0       0.51      0.08      0.14       258
         1.0       0.55      0.94      0.69       308

    accuracy                           0.55       566
   macro avg       0.53      0.51      0.42       566
weighted avg       0.53      0.55      0.44       566<jupyter_text>Cross-Validation<jupyter_code>cross_val = cross_val_score(LogisticRegression(), X, y, scoring='accuracy', cv=10)
print(cross_val)
print(cross_val.mean())<jupyter_output>[0.53710247 0.51943463 0.56537102 0.49469965 0.5335689  0.55123675
 0.55477032 0.55123675 0.52836879 0.52836879]
0.5364158083352129<jupyter_text>Create Traing Strategy Using the Model<jupyter_code>df_pred = pd.DataFrame()
df_pred['Date']= df[split:]['Date']
df_pred['Close']= df[split:]['Close']
df_pred['Predicted_Signal'] = model.predict(X_test)
df_pred['Actual_Daily_Returns'] = df[split:]['Close'].pct_change().values
df_pred['Cumulative_Actual_Daily_Returns'] = (1 + df_pred['Actual_Daily_Returns']).cumprod()-1
df_pred.tail()
df_pred['Strategy_Daily_Returns'] = df_pred['Actual_Daily_Returns']* df_pred['Predicted_Signal'].shift(1)
df_pred['Cumulative_Strategy_Daily_Returns'] = (1 + df_pred['Strategy_Daily_Returns']).cumprod()-1
df_pred.reset_index(inplace=True)
df_pred.tail()
plt.figure()
plt.plot(df_pred['Cumulative_Actual_Daily_Returns'], color='r', label = 'Actual Cumulative Returns')
plt.plot(df_pred['Cumulative_Strategy_Daily_Returns'], color='g', linestyle ='--', label = 'Strategy Cumulative Returns')
plt.xticks(range(0,len(df_pred),28*3),df_pred['Date'].loc[::28*3],rotation=60)
plt.xlabel('Date')
plt.legend()
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15235281705001055, 'fraction_numerical': 0.05296476049799536, 'mean_word_length': 6.464566929133858, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 44, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '611894', 'license': 'MIT', 'n_tokens_mistral': 1950, 'n_tokens_neox': 1769, 'n_words': 283, 'path': 'XCS229ii-Project/Notebook/stock_pred_logistic_regression.ipynb', 'repo_name': 'bearbearyu1223/Stanford-XCS-229-II', 'size': '4739'}