-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculate_objectives.py
106 lines (73 loc) · 2.86 KB
/
calculate_objectives.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
import numpy as np
from estimate_moments import EstimateReturnMoments
def CalculatePortfolioObjectives(w, R, method, alpha):
w = w.reshape(-1, 1)
if method.lower() == 'mad':
port = PortfolioMAD()
port.setScenarios(R)
elif method.lower() == 'cvar':
port = PortfolioCVaR()
port.setScenarios(R)
port.setProbabilityLevel(alpha)
else:
Semi = method.lower() == 'msv'
MU, SIGMA = EstimateReturnMoments(R, Semi)
port = Portfolio()
port.setAssetMoments(MU, SIGMA)
rsk = port.estimatePortRisk(w)
ret = port.estimatePortReturn(w)
return rsk, ret
class PortfolioCVaR:
def __init__(self):
self.scenarios = None
self.probability_level = None
def setScenarios(self, scenarios):
self.scenarios = scenarios
def setProbabilityLevel(self, probability_level):
self.probability_level = probability_level
def estimatePortRisk(self, w):
# Calculate the portfolio returns
port_returns = np.dot(self.scenarios, w)
# Calculate the VaR
var = np.percentile(port_returns, (1 - self.probability_level) * 100)
# Calculate the CVaR
cvar = np.mean(port_returns[port_returns <= var])
return cvar
def estimatePortReturn(self, w):
# Calculate the portfolio returns
port_returns = np.dot(self.scenarios, w)
# Calculate the mean return
mean_return = np.mean(port_returns)
return mean_return
class PortfolioMAD:
def __init__(self):
self.scenarios = None
def setScenarios(self, scenarios):
self.scenarios = scenarios
def estimatePortRisk(self, w):
# Calculate the portfolio returns
port_returns = np.dot(self.scenarios, w)
# Calculate the Mean Absolute Deviation (MAD)
mad = np.mean(np.abs(port_returns - np.mean(port_returns)))
return mad
def estimatePortReturn(self, w):
# Calculate the portfolio returns
port_returns = np.dot(self.scenarios, w)
# Calculate the mean return
mean_return = np.mean(port_returns)
return mean_return
class Portfolio:
def __init__(self):
self.asset_moments = None
def setAssetMoments(self, mu, sigma):
self.asset_moments = (mu, sigma)
def estimatePortRisk(self, w):
# Calculate the portfolio variance
port_var = np.dot(w.T, np.dot(self.asset_moments[1], w))
# Calculate the portfolio standard deviation
port_std = np.sqrt(port_var)
return port_std
def estimatePortReturn(self, w):
# Calculate the portfolio return
port_return = np.dot(w.T, self.asset_moments[0])
return port_return