-
Notifications
You must be signed in to change notification settings - Fork 105
/
utils.py
93 lines (70 loc) · 2.85 KB
/
utils.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
import numpy as np
import pandas as pd
import statsmodels.api as sm
from statsmodels import regression
import matplotlib
import os
import matplotlib.pylab as plt
current_cmap = matplotlib.cm.get_cmap()
current_cmap.set_bad(color='red')
def plot_results(benchmark_series,
target_series,
target_balances,
n_assets,
columns,
name2plot = '',
path2save = './',
base_name_series = 'series'):
# N = len(np.array(benchmark_series).cumsum())
N = len(np.array([item for sublist in benchmark_series for item in sublist]).cumsum())
if not os.path.exists(path2save):
os.makedirs(path2save)
for i in range(0, len(target_balances)):
current_range = np.arange(0, N)
current_ts = np.zeros(N)
current_ts2 = np.zeros(N)
ts_benchmark = np.array([item for sublist in benchmark_series[:i+1] for item in sublist]).cumsum()
ts_target = np.array([item for sublist in target_series[:i+1] for item in sublist]).cumsum()
t = len(ts_benchmark)
current_ts[:t] = ts_benchmark
current_ts2[:t] = ts_target
current_ts[current_ts == 0] = ts_benchmark[-1]
current_ts2[current_ts2 == 0] = ts_target[-1]
plt.figure(figsize = (12, 10))
plt.subplot(2, 1, 1)
plt.bar(np.arange(n_assets), target_balances[i], color = 'grey')
plt.xticks(np.arange(n_assets), columns, rotation='vertical')
plt.subplot(2, 1, 2)
plt.colormaps = current_cmap
plt.plot(current_range[:t], current_ts[:t], color = 'black', label = 'Benchmark')
plt.plot(current_range[:t], current_ts2[:t], color = 'red', label = name2plot)
plt.plot(current_range[t:], current_ts[t:], ls = '--', lw = .1, color = 'black')
plt.autoscale(False)
plt.ylim([-1, 1])
plt.legend()
plt.savefig(path2save + base_name_series + str(i) + '.jpg')
def portfolio(returns, weights):
weights = np.array(weights)
rets = returns.mean() * 252
covs = returns.cov() * 252
P_ret = np.sum(rets * weights)
P_vol = np.sqrt(np.dot(weights.T, np.dot(covs, weights)))
P_sharpe = P_ret / P_vol
return np.array([P_ret, P_vol, P_sharpe])
def sharpe(R):
r = np.diff(R)
sr = r.mean()/r.std() * np.sqrt(252)
return sr
import statsmodels.api as sm
from statsmodels import regression
def print_stats(result, benchmark):
sharpe_ratio = sharpe(np.array(result).cumsum())
returns = np.mean(np.array(result))
volatility = np.std(np.array(result))
X = benchmark
y = result
x = sm.add_constant(X)
model = regression.linear_model.OLS(y, x).fit()
alpha = model.params[0]
beta = model.params[1]
return np.round(np.array([returns, volatility, sharpe_ratio, alpha, beta]), 4).tolist()