forked from joshvdb/Portfolio-Analytics
-
Notifications
You must be signed in to change notification settings - Fork 1
/
portfolio_analytics_gui.py
566 lines (434 loc) · 19 KB
/
portfolio_analytics_gui.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
import os
import io
import base64
import numpy as np
import pandas as pd
from numba import jit
import statsmodels.api as sm
from datetime import datetime
import matplotlib.pyplot as plt
from scipy import integrate, stats
from flask import Flask, send_file
@jit(nopython=True, cache=False)
def sma(matrix, interval):
"""
Function to implement a Simple Moving Average (SMA), optimized with Numba.
:param matrix: np.array([float])
:param interval: int
:return: np.array([float])
"""
# declare empty SMA numpy array
s = np.zeros((matrix.shape[0] - interval))
# calculate the value of each point in the Simple Moving Average array
for t in range(0, s.shape[0]):
s[t] = np.sum(matrix[t:t + interval])/interval
return s
@jit(nopython=True, cache=False)
def ema(matrix, alpha):
"""
Function to implement an Exponential Moving Average (EMA), optimized with Numba. The variable alpha represents the
degree of weighting decrease, a constant smoothing factor between 0 and 1. A higher alpha discounts older
observations faster.
:param matrix: np.array([float])
:param alpha: float
:return: np.array([float])
"""
# declare empty EMA numpy array
e = np.zeros(matrix.shape[0])
# set the value of the first element in the EMA array
e[0] = matrix[0]
# use the EMA formula to calculate the value of each point in the EMA array
for t in range(1, matrix.shape[0]):
e[t] = alpha*matrix[t] + (1 - alpha)*e[t - 1]
return e
@jit(nopython=True, cache=False)
def twap(high, low, open, close, interval):
"""
Function to implement a Time-Weighted Average Price (TWAP), optimized with Numba.
:param high: np.array([float])
:param low: np.array([float])
:param open: np.array([float])
:param close: np.array([float])
:param interval: int
:return: np.array([float])
"""
# calculate prices data for each day
prices = (high + low + open + close) / 4
# declare empty TWAP numpy array
p = np.zeros((prices.shape[0] - interval))
# calculate the value of each point in the TWAP array
for t in range(0, p.shape[0]):
p[t] = np.sum(prices[t:t + interval]) / interval
return p
@jit(nopython=True, cache=False)
def vwap(high, low, close, volumes, interval):
"""
Function to implement a Volume-Weighted Average Price (VWAP), optimized with Numba.
:param high: np.array([float])
:param low: np.array([float])
:param close: np.array([float])
:param volumes: np.array([float])
:param interval: int
:return: np.array([float])
"""
# calculate prices data for each day
prices = (high + low + close) / 3
# declare empty VWAP numpy array
p = np.zeros((prices.shape[0] - interval))
# calculate the value of each point in the VWAP array
for t in range(0, p.shape[0]):
p[t] = np.sum(prices[t:t + interval]*volumes[t:t + interval]) / np.sum(volumes[t:t + interval])
return p
def portfolio_returns(returns, weights):
"""
Calculate the total portfolio returns time-series array by multiplying the weights matrix (dimensions = 1*N) and the
portfolio matrix (dimensions = N*t), for N securities in the portfolio and t returns per security. This function
returns a 1*t matrix where each element in the matrix is the portfolio return for a given time.
:param returns: np.array([float])
:param weights: np.array([float])
:return: np.array([float])
"""
# the portfolio returns are given by the dot product of the weights matrix and the portfolio matrix
port_returns = np.dot(weights, returns)
return port_returns
def alpha_rf(port_returns, risk_free_rate, market_returns, b):
"""
Calculate the Alpha of the portfolio.
:param port_returns: np.array([float])
:param risk_free_rate: np.array([float])
:param market_returns: np.array([float])
:param b: float
:return: float
"""
# the portfolio Alpha is given by the below equation, as stated by the Capital Asset Pricing Model
alpha = np.mean(port_returns) - risk_free_rate + b*(np.mean(market_returns) - risk_free_rate)
return alpha
def portfolio_analytics(port_returns, market_returns):
"""
Perform a regression on the portfolio returns and benchmark returns to calculate the Alpha, beta, and R-squared of
the portfolio. This function will also return a numpy array containing the regression prediction values.
:param port_returns: np.array([float])
:param market_returns: np.array([float])
:return: float, float, float, np.array([float])
"""
# add the intercept to the model
x2 = sm.add_constant(market_returns)
# train the model
estimator = sm.OLS(port_returns, x2)
model = estimator.fit()
# get portfolio analytics
alpha, beta = model.params
r_squared = model.rsquared
regression = model.predict()
return alpha, beta, r_squared, regression
def portfolio_volatility(returns, weights):
"""
Calculate the total portfolio volatility (the variance of the historical returns of the portfolio) using a
covariance matrix.
:param returns: np.array([float])
:param weights: np.array([float])
:return: float
"""
# generate the transform of the 1D numpy weights array
w_T = np.array([[x] for x in weights])
# calculate the covariance matrix of the asset returns
covariance = np.cov(returns)
# calculate the portfolio volatility
port_volatility = np.dot(np.dot(weights, covariance), w_T)[0]
return port_volatility
def sharpe_ratio(port_returns, risk_free_rate, asset_returns, weights):
"""
Calculate the Sharpe ratio of the portfolio, based on the latest portfolio return value.
:param port_returns: np.array([float])
:param risk_free_rate: float
:param asset_returns: np.array([float])
:param weights: np.array([float])
:return: float
"""
# calculate the standard deviation of the returns of the portfolio
portfolio_standard_deviation = np.sqrt(portfolio_volatility(asset_returns, weights))
# calculate the Sharpe ratio of the portfolio
sr = (port_returns[-1] - risk_free_rate)/portfolio_standard_deviation
return sr
def treynor_ratio(port_returns, risk_free_rate, beta):
"""
Calculate the Treynor ratio of the portfolio, based on the latest portfolio return value.
:param port_returns: np.array([float])
:param risk_free_rate: float
:param beta: float
:return: float
"""
# calculate the Treynor ratio of the portfolio
return (port_returns[-1] - risk_free_rate)/beta
def tracking_error(port_returns, market_returns):
"""
Calculate the Tracking Error of the portfolio relative to the benchmark.
:param port_returns: np.array([float])
:param market_returns: np.array([float])
:return: float
"""
return np.std(port_returns - market_returns)
def risk_metrics(returns, weights, portfolio_vol, var_p, d):
"""
Calculate the Analytical VaR and Expected Shortfall for a portfolio, using both the Normal distribution and the
T-distribution.
:param returns: np.array([float]) - the historical portfolio returns
:param weights: np.array([float]) - the weights of the assets in the portfolio
:param portfolio_vol: float - the volatility of the portfolio
:param var_p: float - the value of the daily returns at which to take the Analytical VaR
:param d: int - the number of degrees of freedom to use
:return: float, float, float, float, float, float
"""
# calculate the standard deviation of the portfolio
sigma = np.sqrt(portfolio_vol)
# calculate the mean return of the portfolio
mu = np.sum(portfolio_returns(returns, weights))/returns.shape[1]
# integrate the Probability Density Function to find the Analytical Value at Risk for both Normal and t distributions
a_var = stats.norm(mu, sigma).cdf(var_p)
t_dist_a_var = stats.t(d).cdf(var_p)
# calculate the expected shortfall for each distribution - this is the expected loss (in % daily returns) for the
# portfolio in the worst a_var% of cases - it is effectively the mean of the values along the x-axis from
# -infinity% to a_var%
es = (stats.norm.pdf(stats.norm.ppf((1 - a_var))) * sigma)/(1 - a_var) - mu
t_dist_es = (stats.t(d).pdf(stats.t(d).ppf((1 - a_var))) * sigma * (d + (stats.t(d).ppf((1 - a_var)))**2))/((1 - a_var) * (d - 1)) - mu
return sigma, mu, a_var, t_dist_a_var, es, t_dist_es
def ci_specified_risk_metrics(returns, weights, portfolio_vol, ci, d):
"""
Calculate the Analytical VaR and Expected Shortfall for a portfolio, using both the Normal distribution and the
T-distribution.
:param returns: np.array([float]) - the historical portfolio returns
:param weights: np.array([float]) - the weights of the assets in the portfolio
:param portfolio_vol: float - the volatility of the portfolio
:param ci: float - the confidence interval at which to take the Analytical VaR
:param d: int - the number of degrees of freedom to use
:return: float, float, float, float, float, float
"""
# calculate the standard deviation of the portfolio
sigma = np.sqrt(portfolio_vol)
# calculate the mean return of the portfolio
mu = np.sum(portfolio_returns(returns, weights))/returns.shape[1]
# integrate the Probability Density Function to find the Analytical Value at Risk for both Normal and t distributions
var_level = stats.norm.ppf(ci, mu, sigma)
t_dist_var_level = stats.t(d).ppf(ci)
# calculate the expected shortfall for each distribution - this is the expected loss (in % daily returns) for the
# portfolio in the worst a_var% of cases - it is effectively the mean of the values along the x-axis from
# -infinity% to a_var%
es = (stats.norm.pdf(stats.norm.ppf((1 - ci))) * sigma)/(1 - ci) - mu
t_dist_es = (stats.t(d).pdf(stats.t(d).ppf((1 - ci))) * sigma * (d + (stats.t(d).ppf((1 - ci)))**2))/((1 - ci) * (d - 1)) - mu
return sigma, mu, var_level, t_dist_var_level, es, t_dist_es
def historical_var(port_returns, var_p):
"""
Calculate the Historical VaR for a portfolio.
:param port_returns: np.array([float])
:param var_p: float
:return: float
"""
# calculate the Historical VaR of the portfolio - check if the daily return value is less than a% - if it is, then
# it is counted in the Historical VaR calculation
relevant_returns = 0
port_returns = np.sort(port_returns)
for i in range(0, port_returns.shape[0]):
if port_returns[i] < var_p:
relevant_returns += 1
h_var = relevant_returns/port_returns.shape[0]
return h_var
def ci_historical_var(port_returns, ci):
"""
Calculate the Historical VaR for a portfolio.
:param port_returns: np.array([float])
:param ci: float
:return: float
"""
# calculate the Historical VaR of the portfolio based on the confidence interval
return np.quantile(np.sort(port_returns), ci)
def plot_analytical_var(var_p, sigma, mu, n, z, plot='Normal'):
"""
Plot the normal or t distribution of portfolio returns, showing the area under the curve that corresponds to the
Analytical VaR of the portfolio.
:param var_p: float - the value of the daily returns at which to take the Analytical VaR
:param sigma: float - the standard deviation
:param mu: float - the mean
:param n: int - the number of points to use in the plot
:param z: float - the number of standard deviations from the mean to use as the plot range
:param plot: str - (Normal or T-dist) - used to select the function to plot
:return:
"""
# set the plot range at z standard deviations from the mean in both the left and right directions
plot_range = z * sigma
# set the bottom value on the x-axis (of % daily returns)
bottom = mu - plot_range
# set the top value on the x-axis (of % daily returns)
top = mu + plot_range
# declare the numpy array of the range of x values for the normal distribution
x = np.linspace(bottom, top, n)
# calculate the index of the nearest daily return in x corresponding to a%
risk_range = (np.abs(x - var_p)).argmin()
# calculate the normal distribution pdf for plotting purposes
if plot == 'Normal':
pdf = stats.norm(mu, sigma).pdf(x)
else:
pdf = stats.t(sigma).pdf(x)
plot = 't-dist'
figure = plt.figure()
axis = figure.add_subplot(111)
axis.plot(x, pdf, linewidth=2, color='r', label='Distribution of Returns')
axis.fill_between(x[0:risk_range], pdf[0:risk_range], facecolor='blue', label='Analytical VaR')
axis.legend(loc='upper left')
axis.set_xlabel('Daily Returns')
axis.set_ylabel('Frequency')
axis.set_title('Frequency vs Daily Returns (' + plot + ')')
return figure
def plot_historical_var(port_returns, var_p, num_plot_points):
"""
Plot a histogram showing the distribution of portfolio returns - mark the cutoff point that corresponds to the
Historical VaR of the portfolio. The variable x is the historical distribution of returns of the portfolio, a is
the cutoff value, and the bins are the bins in which to stratify the historical returns.
:param port_returns: np.array([float])
:param var_p: float
:param num_plot_points: int
:return:
"""
# create a numpy array of the bins to use for plotting the Historical VaR, based on the maximum and minimum values
# of the portfolio returns, and the number of plot points to include
bins = np.linspace(np.sort(port_returns)[0], np.sort(port_returns)[-1], num_plot_points)
figure = plt.figure()
axis = figure.add_subplot(111)
axis.hist(np.sort(port_returns), bins, label='Distribution of Returns')
axis.axvline(x=var_p, ymin=0, color='r', label='Historical VaR cutoff value')
axis.legend(loc='upper left')
axis.set_xlabel('Daily Returns')
axis.set_ylabel('Frequency')
axis.set_title('Frequency vs Daily Returns')
return figure
def plot_historical_returns(port_returns, market_returns):
"""
Function to plot the historical portfolio returns.
:param port_returns: np.array([float])
:param market_returns: np.array([float])
:return:
"""
# define x-axis data points
x = np.linspace(0, port_returns.shape[0], port_returns.shape[0])
figure = plt.figure()
axis = figure.add_subplot(111)
axis.plot(x, port_returns, linewidth=1, color='b', label='Portfolio Returns')
axis.plot(x, market_returns, linewidth=1, color='r', label='Benchmark Returns')
axis.legend(loc='upper left')
axis.set_xlabel('Time (days)')
axis.set_ylabel('Daily Return')
axis.set_title('Daily Return vs Time')
return figure
def plot_returns_regression(port_returns, market_returns, regression):
"""
Function to plot the Returns Regression.
:param port_returns: np.array([float])
:param market_returns: np.array([float])
:param regression: np.array([float])
:return:
"""
figure = plt.figure()
axis = figure.add_subplot(111)
axis.scatter(market_returns, port_returns, marker='.', linewidth=1, color='b', label='Actual Returns')
axis.plot(market_returns, regression, linewidth=1, color='k', label='Returns Regression')
axis.legend(loc='upper left')
axis.set_xlabel('Benchmark Daily Return')
axis.set_ylabel('Portfolio Daily Return')
axis.set_title('Returns Regression')
return figure
def plot_equity_prices(ticker, prices):
"""
Function to plot the prices of a single equity.
:param ticker: str
:param prices: np.array([float])
:return:
"""
# define x-axis data points
x = np.linspace(0, prices.shape[0], prices.shape[0])
figure = plt.figure()
axis = figure.add_subplot(111)
axis.plot(x, prices[ticker], linewidth=1, color='b', label=ticker)
axis.legend(loc='upper left')
axis.set_xlabel('Time (days)')
axis.set_ylabel('Price')
axis.set_title('Price vs Time: ' + ticker)
return figure
def equity_price_analytics(ticker, high_prices, low_prices, open_prices, close_prices, volume, alpha, interval):
"""
Function to calculate the EMA, SMA, TWAP and VWAP of a single equity.
:param ticker: str
:param high_prices: pd.DataFrame([float])
:param low_prices: pd.DataFrame([float])
:param open_prices: pd.DataFrame([float])
:param close_prices: pd.DataFrame([float])
:param volume: pd.DataFrame([float])
:param alpha: float
:param interval: int
:return: np.array([float]), np.array([float]), np.array([float]), np.array([float]), np.array([float])
"""
# get price and volume data in numpy array form
high = np.array(high_prices[ticker])
low = np.array(low_prices[ticker])
open = np.array(open_prices[ticker])
close = np.array(close_prices[ticker])
volume = np.array(volume[ticker])
# calculate price analytics
e_ma = ema(close, alpha)
s_ma = sma(close, interval)
t_wap = twap(high, low, open, close, interval)
v_wap = vwap(high, low, close, volume, interval)
return e_ma, s_ma, t_wap, v_wap, close
def plot_equity_price_analytics(ticker, e_ma, s_ma, t_wap, v_wap, close_prices, interval):
"""
Function to plot the mean price, EMA, SMA, TWAP and VWAP of a single equity.
:param ticker: str
:param e_ma: np.array([float])
:param s_ma: np.array([float])
:param t_wap: np.array([float])
:param v_wap: np.array([float])
:param close_prices: np.array([float])
:param interval: int
:return:
"""
# define x-axis data points
x = np.linspace(0, close_prices.shape[0], close_prices.shape[0])
figure = plt.figure()
axis = figure.add_subplot(111)
axis.plot(x, close_prices, linewidth=1, color='k', label='Close Price')
axis.plot(x, e_ma, linewidth=1, color='r', label='EMA Price')
axis.plot(x[interval:], s_ma, linewidth=1, color='b', label='SMA Price')
axis.plot(x[interval:], t_wap, linewidth=1, color='g', label='TWAP Price')
axis.plot(x[interval:], v_wap, linewidth=1, color='m', label='VWAP Price')
axis.legend(loc='upper left')
axis.set_xlabel('Time (days)')
axis.set_ylabel('Price')
axis.set_title('Price vs Time: ' + ticker)
return figure
def plot_equity_returns(ticker, returns):
"""
Function to plot the returns of a single equity.
:param ticker: str
:param returns: np.array([float])
:return:
"""
# define x-axis data points
x = np.linspace(0, returns.shape[0], returns.shape[0])
figure = plt.figure()
axis = figure.add_subplot(111)
axis.plot(x, returns[ticker], linewidth=1, color='b', label=ticker)
axis.legend(loc='upper left')
axis.set_xlabel('Time (days)')
axis.set_ylabel('Daily Return')
axis.set_title('Return vs Time for: ' + ticker)
return figure
def get_data(data):
"""
Function to convert pandas DataFrames into numpy array's of shape (n*m), where n = the number of equities and m =
the number of days for which we have price or return data.
:param data: pd.DataFrame([float])
:return: np.array([float])
"""
np_data = np.array(data)
array = []
for i in range(0, np_data.shape[1]):
array.append(np_data[:, i])
return np.array(array)