-
Notifications
You must be signed in to change notification settings - Fork 0
/
ml.py
219 lines (163 loc) · 7.02 KB
/
ml.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import math
import pandas as pd
import numpy as np
import datetime as dt
import matplotlib.pyplot as plt
from matplotlib import style
import seaborn as sns
from scipy.stats import randint as sp_randint
from sklearn.decomposition import PCA
style.use("ggplot")
# loading the data
df = pd.read_csv('DJIA_adjcloses.csv', parse_dates=True, index_col=0)
# Visualizing the dataframe
# print(df.head())
# Dropping 'Not a Number' columns for Dow Chemicals (DWDP) and Visa (V)
df.drop(['DWDP', 'V'], axis=1, inplace=True)
# Copying the dataframe to add features
data = pd.DataFrame(df.copy())
# Daily Returns
# Daily Log Returns (%)
# datareturns = np.log(data / data.shift(1))
# Daily Linear Returns (%)
datareturns = data.pct_change(1)
# Data Raw
data_raw = datareturns
data_raw.dropna(how='all', inplace=True)
# Normalizing the Log returns
data = (data_raw - data_raw.mean()) / data_raw.std()
# Getting rid of the NaN values.
data.dropna(how='any', inplace=True)
data_raw.dropna(how='any', inplace=True)
# Visualizing Log Returns for the DJIA
# plt.figure(figsize=(16, 5))
# plt.title("Dow Jones Industrial Average Linear Returns (%)")
# data.DJIA.plot()
# plt.grid(True);
# plt.legend()
# plt.show()
# Taking away the market benchmark DJIA
stock_tickers = data.columns.values[:-1]
n_tickers = len(stock_tickers)
# Dividing the dataset into training and testing sets
percentage = int(len(data) * 0.8)
X_train = data[:percentage]
X_test = data[percentage:]
X_train_raw = data_raw[:percentage]
X_test_raw = data_raw[percentage:]
# Applying Principle Component Analysis
# Creating covariance matrix and training data on PCA.
cov_matrix = X_train.loc[:,X_train.columns != 'DJIA'].cov()
pca = PCA()
pca.fit(cov_matrix)
def plotPCA(plot=False):
# Visualizing Variance against number of principal components.
cov_matrix_raw = X_train_raw.loc[:,X_train_raw.columns != 'DJIA'].cov()
var_threshold = 0.95
var_explained = np.cumsum(pca.explained_variance_ratio_)
num_comp = np.where(np.logical_not(var_explained < var_threshold))[0][0] + 1
if plot:
print('%d principal components explain %.2f%% of variance' %(num_comp, 100* var_threshold))
# PCA percent variance explained.
bar_width = 0.9
n_asset = stock_tickers.shape[0]
x_indx = np.arange(n_asset)
fig, ax = plt.subplots()
# Eigenvalues measured as percentage of explained variance.
rects = ax.bar(x_indx, pca.explained_variance_ratio_[:n_asset], bar_width)
ax.set_xticks(x_indx + bar_width / 2)
ax.set_xticklabels(list(range(n_asset)), rotation=45)
ax.set_title('Percent variance explained')
ax.set_ylabel('Explained Variance')
ax.set_xlabel('Principal Components')
plt.show()
plotPCA(plot=True)
projected = pca.fit_transform(cov_matrix)
pcs = pca.components_
def PCWeights():
'''
Principal Components (PC) weights for each 28 PCs
'''
weights = pd.DataFrame()
for i in range(len(pcs)):
weights["weights_{}".format(i)] = pcs[:, i] / sum(pcs[:, i])
weights = weights.values.T
return weights
weights = PCWeights()
portfolio = portfolio = pd.DataFrame()
def plotEigen(weights, plot=False, portfolio=portfolio):
portfolio = pd.DataFrame(data ={'weights': weights.squeeze()*100}, index = stock_tickers)
portfolio.sort_values(by=['weights'], ascending=False, inplace=True)
if plot:
print('Sum of weights of current eigen-portfolio: %.2f' % np.sum(portfolio))
portfolio.plot(title='Current Eigen-Portfolio Weights',
figsize=(12,6),
xticks=range(0, len(stock_tickers),1),
rot=45,
linewidth=3
)
plt.show()
return portfolio
# Weights are stored in arrays, where 0 is the first PC's weights.
plotEigen(weights=weights[4], plot=True)
# Sharpe Ratio
def sharpe_ratio(ts_returns, periods_per_year=252):
'''
Sharpe ratio is the average return earned in excess of the risk-free rate per unit of volatility or total risk.
It calculares the annualized return, annualized volatility, and annualized sharpe ratio.
ts_returns are returns of a signle eigen portfolio.
'''
n_years = ts_returns.shape[0]/periods_per_year
annualized_return = np.power(np.prod(1+ts_returns),(1/n_years))-1
annualized_vol = ts_returns.std() * np.sqrt(periods_per_year)
annualized_sharpe = annualized_return / annualized_vol
return annualized_return, annualized_vol, annualized_sharpe
def plotSharpe(eigen):
'''
Plots Principle components returns against real returns.
'''
eigen_portfolio_returns = np.dot(X_test_raw.loc[:, eigen.index], eigen / len(pcs))
eigen_portfolio_returns = pd.Series(eigen_portfolio_returns.squeeze(), index=X_test.index)
returns, vol, sharpe = sharpe_ratio(eigen_portfolio_returns)
print('Current Eigen-Portfolio:\nReturn = %.2f%%\nVolatility = %.2f%%\nSharpe = %.2f' % (returns*100, vol*100, sharpe))
year_frac = (eigen_portfolio_returns.index[-1] - eigen_portfolio_returns.index[0]).days / 252
df_plot = pd.DataFrame({'PC': eigen_portfolio_returns, 'DJIA': X_test_raw.loc[:, 'DJIA']}, index=X_test.index)
np.cumprod(df_plot + 1).plot(title='Returns of the market-cap weighted index vs. First eigen-portfolio',
figsize=(12,6), linewidth=3)
plt.show()
plotSharpe(eigen=plotEigen(weights=weights[4]))
def optimizedPortfolio():
n_portfolios = len(pcs)
annualized_ret = np.array([0.] * n_portfolios)
sharpe_metric = np.array([0.] * n_portfolios)
annualized_vol = np.array([0.] * n_portfolios)
highest_sharpe = 0
for i in range(n_portfolios):
pc_w = pcs[:, i] / sum(pcs[:, i])
eigen_prtfi = pd.DataFrame(data ={'weights': pc_w.squeeze()*100}, index = stock_tickers)
eigen_prtfi.sort_values(by=['weights'], ascending=False, inplace=True)
eigen_prti_returns = np.dot(X_test_raw.loc[:, eigen_prtfix.index], eigen_prtfix / n_portfolios)
eigen_prti_returns = pd.Series(eigen_prtix_returns.squeeze(), index=X_test.index)
er, vol, sharpe = sharpe_ratio(eigen_prti_returns)
annualized_ret[i] = er
annualized_vol[i] = vol
sharpe_metric[i] = sharpe
# find portfolio with the highest Sharpe ratio
highest_sharpe = np.nanargmax(sharpe_metric)
print('Eigen portfolio #%d with the highest Sharpe. Return %.2f%%, vol = %.2f%%, Sharpe = %.2f' %
(highest_sharpe,
annualized_ret[highest_sharpe]*100,
annualized_vol[highest_sharpe]*100,
sharpe_metric[highest_sharpe]))
fig, ax = plt.subplots()
fig.set_size_inches(12, 4)
ax.plot(sharpe_metric, linewidth=3)
ax.set_title('Sharpe ratio of eigen-portfolios')
ax.set_ylabel('Sharpe ratio')
ax.set_xlabel('Portfolios')
results = pd.DataFrame(data={'Return': annualized_ret, 'Vol': annualized_vol, 'Sharpe': sharpe_metric})
results.dropna(inplace=True)
results.sort_values(by=['Sharpe'], ascending=False, inplace=True)
print(results.head(10))
plt.show()
optimizedPortfolio()