-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtraining.py
178 lines (135 loc) · 5.55 KB
/
training.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import pandas as pd
import numpy as np
import simfin as sf
from simfin.names import *
from sklearn.ensemble import RandomForestRegressor
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
from sklearn.tree import export_text
from sklearn.metrics import confusion_matrix
from sklearn.metrics import r2_score, accuracy_score
API_KEY = 'MbOGeJgi6qQjgYbb58oBVQDaObxEZzXg'
# SimFin data-directory.
sf.set_data_dir('~/simfin_data/')
# SimFin load API key or use free data.
sf.load_api_key('MbOGeJgi6qQjgYbb58oBVQDaObxEZzXg')
# We are interested in the US stock-market.
market = 'us'
# Add this date-offset to the fundamental data such as
# Income Statements etc., because the REPORT_DATE is not
# when it was actually made available to the public,
# which can be 1, 2 or even 3 months after the Report Date.
offset = pd.DateOffset(days=60)
# Refresh the fundamental datasets (Income Statements etc.)
# every 30 days.
refresh_days = 30
# Refresh the dataset with shareprices every 10 days.
refresh_days_shareprices = 10
hub = sf.StockHub(market=market, offset=offset,
refresh_days=refresh_days,
refresh_days_shareprices=refresh_days_shareprices)
df_fin_signals = hub.fin_signals(variant='daily')
df_growth_signals = hub.growth_signals(variant='daily')
df_val_signals = hub.val_signals(variant='daily')
# Combine the DataFrames.
dfs = [df_fin_signals, df_growth_signals, df_val_signals]
df_signals = pd.concat(dfs, axis=1)
# Remove all rows with only NaN values.
df = df_signals.dropna(how='all').reset_index(drop=True)
# List of the columns before removing any.
columns_before = df_signals.columns
# Threshold for the number of rows that must be NaN for each column.
thresh = 0.75 * len(df_signals.dropna(how='all'))
# Remove all columns which don't have sufficient data.
df_signals = df_signals.dropna(axis='columns', thresh=thresh)
# List of the columns after the removal.
columns_after = df_signals.columns
# Show the columns that were removed.
columns_before.difference(columns_after)
# Name of the new column for the returns.
TOTAL_RETURN_1_3Y = 'Total Return 1-3 Years'
# Calculate the mean log-returns for all 1-3 year periods.
df_returns_1_3y = \
hub.mean_log_returns(name=TOTAL_RETURN_1_3Y,
future=True, annualized=True,
min_years=1, max_years=3)
dfs = [df_signals, df_returns_1_3y]
df_sig_rets = pd.concat(dfs, axis=1)
# Clip the signals and returns at their 5% and 95% quantiles.
# We do not set them to NaN because it would remove too much data.
df_sig_rets = sf.winsorize(df_sig_rets)
# Remove all rows with missing values (NaN)
# because scikit-learn cannot handle that.
df_sig_rets = df_sig_rets.dropna(how='any')
# Remove all tickers which have less than 200 data-rows.
df_sig_rets = df_sig_rets.groupby(TICKER) \
.filter(lambda df: len(df)>200)
# List of all unique stock-tickers in the dataset.
tickers = df_sig_rets.reset_index()[TICKER].unique()
# Split the tickers into training- and test-sets.
tickers_train, tickers_test = \
train_test_split(tickers, train_size=0.8, random_state=1234)
df_train = df_sig_rets.loc[tickers_train]
df_test = df_sig_rets.loc[tickers_test]
# DataFrames with signals for training- and test-sets.
X_train = df_train.drop(columns=[TOTAL_RETURN_1_3Y])
X_test = df_test.drop(columns=[TOTAL_RETURN_1_3Y])
# DataFrames with stock-returns for training- and test-sets.
y_train = df_train[TOTAL_RETURN_1_3Y]
y_test = df_test[TOTAL_RETURN_1_3Y]
# List of signal names.
signal_names = X_train.columns.values
# List of signal names where spaces are replaced with _
signal_names_ = [s.replace(' ', '_') for s in signal_names]
# Column-name.
FEATURE_IMPORTANCE = 'Feature Importance'
def compare_feature_imp_corr(estimator):
"""
Return a DataFrame which compares the signals' Feature
Importance in the Machine Learning model, to the absolute
correlation of the signals and stock-returns.
:param estimator: Sklearn ensemble estimator.
:return: Pandas DataFrame.
"""
# Wrap the list of Feature Importance in a Pandas Series.
df_feat_imp = pd.Series(estimator.feature_importances_,
index=signal_names,
name=FEATURE_IMPORTANCE)
# Concatenate the DataFrames with Feature Importance
# and Return Correlation.
dfs = [df_feat_imp, df_corr_returns]
df_compare = pd.concat(dfs, axis=1, sort=True)
# Sort by Feature Importance.
df_compare.sort_values(by=FEATURE_IMPORTANCE,
ascending=False, inplace=True)
return df_compare
def print_tree(estimator, max_depth=6, **kwargs):
"""
Print the first Decision Tree from a Random Forest.
:param estimator: Sklearn ensemble estimator.
"""
s = export_text(estimator.estimators_[0],
max_depth=max_depth,
feature_names=signal_names_,
**kwargs)
print(s)
# Parameters for scikit-learn's Random Forest models.
model_args = \
{
# Random Forest parameters to adjust between
# over- and under-fitting.
'n_estimators': 100,
'max_depth': 15,
'min_samples_split': 100,
'min_samples_leaf': 10,
# Use all available CPU cores.
'n_jobs': -1,
# Set random seed to make the experiments repeatable.
'random_state': 1234,
}
# Create the estimator, but don't do any computations yet.
regr = RandomForestRegressor(**model_args)
# Fit the estimator to the training-data.
# This may take several minutes on a 4-core CPU.
_ = regr.fit(X=X_train, y=y_train)
print_tree(regr)