-
Notifications
You must be signed in to change notification settings - Fork 0
/
0027.py
69 lines (58 loc) · 2.25 KB
/
0027.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
import numpy as np
import matplotlib.pyplot as plt
from constants import *
## Parameters for Plotting
from matplotlib import rcParams as rcp
rcp['font.family'],rcp['font.serif'] = 'serif', ['Computer Modern Roman']
rcp['text.usetex'] = True
rcp['font.size'], rcp['axes.labelsize'],rcp['axes.titlesize'] = 16, 16,18
rcp['xtick.labelsize'],rcp['ytick.labelsize'] = 14, 14
nf = np.load("data/0027.npz")
returns = nf["returns"]
variance = nf["variance"]
epsil_vec = nf["epsil_vec"]
obj_val = nf["obj_val"]
fig, ax = plt.subplots(1,3, figsize=(10,5))
#
ax[0].plot(epsil_vec,returns,label="Obj. Value",marker="o") # objective function value
ax[0].set_xlabel("$\epsilon$")
ax[0].set_ylabel("Obj. Value")
#
ax[1].plot(epsil_vec,variance,label="Returns",marker="o")
ax[1].set_xlabel("$\epsilon$")
ax[1].set_ylabel("Expected Portfolio Return")
ax[1].set_title("Robust Optimizaton")
#
ax[2].plot(epsil_vec, obj_val,label="Variance",marker="o")
ax[2].set_xlabel("$\epsilon$")
ax[2].set_ylabel("Portfolio Variance")
#
plt.subplots_adjust(wspace=0.5, hspace=0.7)
fig.savefig('img/0027a.pdf', bbox_inches='tight')
# Efficient Frontier
# epsil_vec = epsil_vec[:60]
# returns = returns[:60]
# variance = variance[:60]
import pylab as pl
from matplotlib.cm import ScalarMappable
fig, ax = plt.subplots(1,1,figsize=(7,2))
colors = pl.cm.hsv(epsil_vec/max(epsil_vec))
sc = ax.scatter(variance, returns, marker="o",c=colors)
sm = ScalarMappable(cmap=pl.cm.hsv, norm=plt.Normalize(vmin=min(epsil_vec), vmax=max(epsil_vec)))
plt.colorbar(sm, ax=ax, label="$\epsilon$")
ax.set_xlabel("Portfolio Variance"), ax.set_ylabel("Portfolio Return")
plt.subplots_adjust(wspace=0.3, hspace=0.7)
fig.savefig("img/0027b.pdf",bbox_inches="tight")
######## Plot Sharpe Ratio ###########
sr = returns/np.sqrt(variance)
fig, ax = plt.subplots(1,1,figsize=(7,2))
colors = pl.cm.hsv(returns/max(returns))
sc = ax.scatter(epsil_vec, sr, marker='o', c=colors)
sm = ScalarMappable(cmap=pl.cm.hsv, norm=plt.Normalize(vmin=min(returns), vmax=max(returns)))
plt.colorbar(sm, ax=ax,label="Expected Return")
ax.set_title("Sharpe Ratio $S$ for Varying $\epsilon$")
ax.set_xlabel("Uncertainty Set Size, $\epsilon$")
ax.set_ylabel("Sharpe Ratio")
plt.subplots_adjust(wspace=0.3, hspace=0.7)
fig.savefig("img/0027c.pdf",bbox_inches="tight")
plt.show()