-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathplots.py
More file actions
164 lines (134 loc) · 4.93 KB
/
plots.py
File metadata and controls
164 lines (134 loc) · 4.93 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
#!/usr/bin/env python3
"""
Author: Group 1
Date: 08.02.2022
This code is provided "As Is"
"""
# Third Party
from statistics import mode
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
import scipy.stats as stats
# Local
from data import obtain_BTC, obtain_BlackSholes, obtain_EvoGenoa, obtain_Gauss, obtain_SP500, obtain_SVJ
from Stats import Stats
from utils import save_figure
# Matplotlib parameters
plt.style.use("seaborn")
mpl.rcParams["figure.dpi"] = 250
def plot_log_returns(ts_stats: Stats, plot_title: str) -> None:
"""
This function takes a Stats object (created for the ABM course) and produces plots
of log-returns(also saves them locally)
Input:
ts_stats: An object containing data regarding a given asset-price time series
plot_title: the string that is to be displayed as the title of the output plot.
"""
# Define plot
fig, ax = plt.subplots(2, 1, figsize=(6, 8))
# Plot normalized log returns
ax[0].plot(
np.arange(0, ts_stats.nlr_mean.shape[0]),
ts_stats.nlr_mean,
linewidth=1,
color="black",
)
ax[0].set(xlabel="Time $(t)$", ylabel="Normalized Log Returns")
# Plot histogram of normalized log returns and store bins
hist, bins, _ = ax[1].hist(
ts_stats.nlr_mean[~np.isnan(ts_stats.nlr_mean)], bins="auto"
)
# Evaluate normal probability distribution function given normalized log return data and bins
norm_pdf = stats.norm.pdf(
bins,
np.nanmean(ts_stats.nlr_mean),
np.nanstd(ts_stats.nlr_mean),
)
# Overlay normal probability distribution function
ax[1].plot(bins, norm_pdf * hist.sum() / norm_pdf.sum(), color="black")
ax[1].set(xlabel="Log of returns", ylabel="Frequency")
# Plot embellishments
fig.suptitle(plot_title)
plt.tight_layout()
save_figure(plot_title, "/price_path")
def plot_time_series(ts_stats: Stats, plot_title: str) -> None:
"""
This function takes a Stats object (created for the ABM course) and produces plots
of the price path of an asset (also saves them locally)
Input:
ts_stats: An object containing data regarding a given asset-price time series
plot_title: the string that is to be displayed as the title of the output plot.
"""
# Make plots
fig, ax = plt.subplots(figsize=(6, 4))
# Plot price line
ax.plot(
np.arange(0, len(ts_stats.ts_mean)),
ts_stats.ts_mean,
linewidth=1,
color="black",
label="Mean Asset Price",
)
# Plot standard error overlay
ax.fill_between(
np.arange(0, len(ts_stats.ts_mean)),
ts_stats.ts_mean - ts_stats.std_error,
ts_stats.ts_mean + ts_stats.std_error,
alpha=0.5,
label="Standard Error",
)
# Plot embellishments
ax.set(xlabel="Time $(t)$", ylabel="Price")
ax.legend(loc="best")
fig.suptitle(plot_title)
plt.tight_layout()
save_figure(plot_title, "/price_path")
def __test__():
"""
When run directly 'python plots.py' this script runs some default cases to check functionality of this script
"""
# Gaussian
gauss = obtain_Gauss()
gauss_stats = Stats(gauss)
plot_time_series(gauss_stats, "Gaussian Price Path")
plot_log_returns(gauss_stats, "Gaussian Log Returns")
# BTC
btc = obtain_BTC()
btc_stats = Stats(btc)
plot_time_series(btc_stats, "BTC Price Path")
plot_log_returns(btc_stats, "BTC Log Returns")
# SP500
sp500 = obtain_SP500()
sp500_stats = Stats(sp500)
plot_time_series(sp500_stats, "SP500 Price Path")
plot_log_returns(sp500_stats, "SP500 Log Returns")
# Standard Black-Scholes Model (SP500)
bs = obtain_BlackSholes(mode="SP500")
bs_stats = Stats(bs)
plot_time_series(bs_stats, "Black-Scholes Price Path (SP500)")
plot_log_returns(bs_stats, "Black-Scholes Log Returns (SP500)")
# Standard Black-Scholes Model (BTC)
bs = obtain_BlackSholes(mode="BTC")
bs_stats = Stats(bs)
plot_time_series(bs_stats, "Black-Scholes Price Path (BTC)")
plot_log_returns(bs_stats, "Black-Scholes Log Returns (BTC)")
# Standard Black-Scholes Model (SP500)
svj = obtain_SVJ(mode="SP500")
svj_stats = Stats(svj)
plot_time_series(svj_stats, "Stochastic Volatility With Jumps Price Path (SP500)")
plot_log_returns(svj_stats, "Stochastic Volatility With Jumps Log Returns (SP500)")
# Standard Black-Scholes Model (BTC)
svj = obtain_SVJ(mode="BTC")
svj_stats = Stats(svj)
plot_time_series(svj_stats, "Stochastic Volatility With Jumps Price Path (BTC)")
plot_log_returns(svj_stats, "Stochastic Volatility With Jumps Log Returns (BTC)")
# Evolutionary Genoa Model
evo = obtain_EvoGenoa()
evo_stats = Stats(evo)
plot_time_series(evo_stats, "Evolutionary Genoa Price Path")
plot_log_returns(evo_stats, "Evolutionary Genoa Log Returns")
# Show Plots
# plt.show()
if __name__ == "__main__":
__test__()