-
Notifications
You must be signed in to change notification settings - Fork 0
/
0024.py
60 lines (50 loc) · 2.17 KB
/
0024.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
import numpy as np
import matplotlib.pyplot as plt
import pylab as pl
from constants import *
from matplotlib import rcParams as rcp
from matplotlib.cm import ScalarMappable
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/l2norm.npz")
returns, variance, deltavec, obj_val = nf["returns"], nf["variance"], nf["deltavec"], nf["obj_val"]
###### Plot 1 #################################################
fig, ax = plt.subplots(1,1,figsize=(7,2))
colors = pl.cm.hsv(deltavec)
sc = ax.scatter(variance, returns, marker="o",c=colors)
sm = ScalarMappable(cmap=pl.cm.hsv, norm=plt.Normalize(vmin=min(deltavec), vmax=max(deltavec)))
plt.colorbar(sm, ax=ax, label="$L_2$-Norm Constraint")
ax.set_xlabel("Portfolio Variance"), ax.set_ylabel("Portfolio Return")
plt.subplots_adjust(wspace=0.3, hspace=0.7)
fig.savefig("img/00241.pdf",bbox_inches="tight")
###### Plot 2: Plot direct correlations #################################################
fig, ax = plt.subplots(1,3,figsize=(9,4))
ax[0].plot(deltavec, returns, marker="o")
ax[0].set_ylabel("Portfolio Returns")
ax[0].set_xlabel("$L_2$-Norm Bound, $\delta$")
#
ax[1].plot(deltavec, variance, marker="o")
ax[1].set_xlabel("$L_2$-Norm Bound, $\delta$")
ax[1].set_ylabel("Portfolio Variance")
#
ax[2].plot(deltavec, obj_val, marker="o")
ax[2].set_xlabel("$L_2$-Norm Bound, $\delta$")
ax[2].set_ylabel("Objective Value")
#
plt.subplots_adjust(wspace=0.7, hspace=0.7)
fig.savefig("img/00242.pdf",bbox_inches="tight")
#######################
sr = returns/np.sqrt(variance)
fig, ax = plt.subplots(1,1,figsize=(7,2))
colors = pl.cm.hsv(returns/max(returns))
sc = ax.scatter(deltavec, 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 $\delta$")
ax.set_xlabel("$L_2$-norm Bound, $\delta$")
ax.set_ylabel("Sharpe Ratio")
plt.subplots_adjust(wspace=0.3, hspace=0.7)
fig.savefig("img/00243.pdf",bbox_inches="tight")
plt.show()