-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplotting.py
More file actions
169 lines (142 loc) · 6.21 KB
/
plotting.py
File metadata and controls
169 lines (142 loc) · 6.21 KB
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
# Import modules
import yfinance as yf
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
from datetime import datetime as dt
import datetime as dt1
import pandas_market_calendars as mcal
import seaborn as sns
import matplotlib.patches as mpatches
# Plot the simulation paths
def mcPathsPlot(n, T, St, aIndex, a, VaR, ES, ticker, model):
tempList=St.transpose().tolist()
tempList.sort(key=lambda ele: (ele[n]), reverse=True)
pathsWithinLevel = np.asarray(tempList[:aIndex]).T
np.mean(pathsWithinLevel, axis=1)
pathsBreachedLevel = np.asarray(tempList[aIndex:]).T
averagePaths = np.mean(np.asarray(tempList).T, axis=1)
averagePathsBreachedLevel = np.mean(pathsBreachedLevel, axis=1)
# Used for the x-axis of the plot
time = np.linspace(0,T,n+1)
# Plots
plt.plot(time, pathsWithinLevel, color='green', label = "Non-Breaching Paths")
plt.plot(time, pathsBreachedLevel, color='red', label = "VaR Breaching Paths")
# Axis Labels
plt.xlabel("Trading Days $(t)$")
plt.ylabel("Stock Price $(S_t)$")
plt.title(r"$\bf{" + ticker + "\ Stock\ Price\ Paths\ for\ " + model + "\ Simulation" + "}$" + "\n Mean Price = " + '\${:,.2f}'.format(St[-1,:].mean()) + ", $\mathregular{VaR_{" + str(a) + "}}$ =" + "\${:,.2f}".format(VaR) + ", $\mathregular{ES_{" + str(a) + "}}$ =" + "\${:,.2f}".format(ES))
# Legend
green_patch = mpatches.Patch(color = 'green', label = "Non-Breaching Paths")
red_patch = mpatches.Patch(color = 'red', label = "VaR Breaching Paths")
plt.legend(handles = [green_patch, red_patch])
plt.show()
# Plot the loss distribution
def lossDistPlot(a, M, VaR, ES, sortedLoss, ticker):
# Histogram Bins
df=pd.DataFrame(sortedLoss)
ax = sns.distplot(sortedLoss)
data_x, data_y = ax.lines[0].get_data()
# Plotting
plt.fill_between(sortedLoss, np.interp(sortedLoss, data_x, data_y), where = sortedLoss >= VaR, color='red', alpha=0.8)
#Labeling
plt.xlabel("Loss ($)")
plt.ylabel("Relative Frequency (%)")
plt.title(r"$\bf{" + ticker + "\ Loss\ Distribution" + "}$" + "\n Mean Loss = " + '\${:,.2f}'.format(sortedLoss.mean()) + ", $\mathregular{VaR_{" + str(a) + "}}$ =" + "\${:,.2f}".format(VaR) + ", $\mathregular{ES_{" + str(a) + "}}$ =" + "\${:,.2f}".format(ES))
plt.show()
# Model output in tabular form
def modelOutput(model, ticker, T, M, St, sortedLoss, a, VaR, ES, mu, sigma, lambda_r, mu_j, sigma_j, jumpThreshold):
fig = plt.figure(figsize = (6,4))
ax = fig.add_subplot(111)
if model == "GBM":
df = pd.DataFrame(["",
model,
ticker,
T,
M,
"",
"",
'\${:,.2f}'.format(St[-1,:].mean()),
'\${:,.2f}'.format(-sortedLoss.mean()),
"",
"",
"\${:,.2f}".format(VaR),
"\${:,.2f}".format(ES),
"",
"",
"{:.2f}%".format(mu*252*100),
"{:.2F}%".format(sigma*np.sqrt(252)*100)
],
index=["Model Information (User Inputted)",
" Model Used",
" Stock",
" Time Horizon (T) in Trading Days",
" Number of Simulations (M)",
"",
"Projected Price Information",
" Projected Price",
" Projected P&L",
"",
"Risk Metrics",
" $\mathregular{VaR_{" + str(a) + "}}$",
" $\mathregular{ES_{" + str(a) + "}}$",
"",
"Model Parameters",
" μ (Annualized)",
" σ (Annualized)"
])
elif model == "Jump":
df = pd.DataFrame(["",
model,
ticker,
T,
M,
'{:,.2f}%'.format(100*jumpThreshold),
"",
"",
'\${:,.2f}'.format(St[-1,:].mean()),
'\${:,.2f}'.format(-sortedLoss.mean()),
"",
"",
"\${:,.2f}".format(VaR),
"\${:,.2f}".format(ES),
"",
"",
"{:.2f}%".format(mu*252*100),
"{:.2f}%".format(sigma*np.sqrt(252)*100),
"{}".format(int(np.round(lambda_r * 252))),
"{:.2e}".format(mu_j),
"{:.2e}".format(sigma_j),
],
index=["Model Information (User Inputted)",
" Model Used",
" Stock",
" Time Horizon (T) in Trading Days",
" Number of Simulations (M)",
" Jump Threshold ($\mathregular{J_{T}}$)",
"",
"Projected Price Information",
" Projected Price",
" Projected P&L",
"",
"Risk Metrics",
" $\mathregular{VaR_{" + str(a) + "}}$",
" $\mathregular{ES_{" + str(a) + "}}$",
"",
"Model Parameters",
" μ (Annualized)",
" σ (Annualized)",
" λ (Number of Jumps Annually)",
" $\mathregular{μ_{j}}$",
" $\mathregular{σ_{j}}$",
])
table = ax.table(cellText=df.values, rowLabels=df.index, loc = "upper center", colWidths=[0.2]*len(df.columns))
table.auto_set_font_size(False)
table.set_fontsize(10)
if model == "Jump":
ax.set_title("Jump Diffusion Model Output", loc='left')
else:
ax.set_title("GBM Model Output", loc='left')
ax.axis("off")
fig.tight_layout()
plt.show()