forked from emoen/Machine-Learning-for-Asset-Managers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutility_calculations.py
74 lines (69 loc) · 2.86 KB
/
utility_calculations.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
# -*- coding: utf-8 -*-
import yfinance as yf
import numpy as np
import pandas as pd
''' This file contains utility functions that already has implementation
in numpy or pandas. Learning objective to write the implementation.
'''
def calculate_correlation(S, T=936, N=234):
""" Create covariance matrix
>>> import numpy as np
>>> n = 3
>>> T = 3
>>> S = np.array([[1,2,3],[6,4,2],[9,1,5]])
>>> M = np.mean(S, axis=1) # mean of row (over all T (columns))
>>> M
array([2, 4, 5])
>>> demeaned_S = S - M[:,None]
>>> print(demeaned_S)
[[-1 0 1]
[ 2 0 -2]
[ 4 -4 0]]
>>> demeaned_S= demeaned_S.astype('float32')
>>> covariance = np.dot(demeaned_S, demeaned_S.T) * (1.0/(n-1))
>>> print(covariance)
[[ 1. -2. -2.]
[-2. 4. 4.]
[-2. 4. 16.]]
>>> np.testing.assert_array_equal(covariance, np.cov(S))
>>> stds = np.std(S, axis=1, ddof=1)
>>> stds_m = np.outer(stds, stds)
>>> covariance = covariance.astype('float32')
>>> correlation = np.divide(covariance, stds_m)
>>> np.testing.assert_array_equal(correlation, np.corrcoef(S))
>>> print(correlation)
>>> print(correlation_from_covariance(covariance))
"""
#2.Average Price Of Stock
M = np.sum(S, axis=1)/T #sum along row
#3.Demeaning The Prices
de_meaned_S = S - M[:,None]
#4.Covariance Matrix
#Once we have the de-meaned price series, we establish the
#covariance of different stocks by multiplying the transpose of
#the de-meaned price series with itself and divide it by 'm'
covariance = (np.dot(de_meaned_S, de_meaned_S.T))/(N-1)
# The eigen-values of the covariance matrix is distributed like Marcenko-Pasture dist.
#any any eigenvalues outside distribution is signal else noise.
#Standard Model: Markowitz’ Curse
#The condition number of a covariance, correlation (or normal, thus diagonalizable) matrix is the absolute
#value of the ratio between its maximal and minimal (by moduli) eigenvalues. This number is lowest for a diagonal
#correlation matrix, which is its own inverse.
corr = correlation_from_covariance(covariance)
eigenvalue, eigenvector = np.linalg.eig(np.corrcoef(S))
eigenvalue = abs(eigenvalue)
condition_num = max(eigenvalue) - min(eigenvalue)
def correlation_from_covariance(covariance):
v = np.sqrt(np.diag(covariance))
outer_v = np.outer(v, v)
correlation = covariance / outer_v
correlation[covariance == 0] = 0
return correlation
if __name__ == '__main__':
N = 234
T = 936
StockPrice = np.loadtxt('csv/ol184.csv', delimiter=',')
portfolio_name = pd.read_csv('csv/ol_names.csv', delimiter=',',header=None)[0].tolist()
StockPrice = StockPrice[:,1:184]
returns = pd.DataFrame(StockPrice).pct_change().dropna(how="all")
calculate_correlation(returns.to_numpy(), T=936, N=234)