-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilter_sim.py
More file actions
100 lines (87 loc) · 2.15 KB
/
filter_sim.py
File metadata and controls
100 lines (87 loc) · 2.15 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
# %% Load libraries
from price_generator import generate_spread_path
import numpy as np
import matplotlib.pyplot as plt
from filter import Filter
# %% [markdown]
# # Simulate Calendar Spreads
# Considering 2 scenarios:
# 1. Rolling Futures
# 2. Not Rolling Futures
# %% Initialise variables
x0, a, l, sig, k, dt = 1.5, 0.35, 0.05, 0.9, 0.25, 1 / 252.0
c1, c2, c3, c4 = 1.2, 0.19, 0.1, -0.34
q = 0.12
n_steps = 252
Tn = [200 / 360.0, 100.0 / 360.0 * 3]
# %% Generate path
price_path = generate_spread_path(
x0,
a,
l,
sig,
k,
dt,
Tn,
c1,
c2,
c3,
c4,
q,
n_steps,
rolling=False,
seed=int(np.random.uniform(0, 10000)),
)
# # %% Plot
# plt.plot(price_path[0], label="Spot Price")
# plt.legend()
# plt.show()
# %% Create filter
our_filter = Filter(a, l, sig, q, k, c1, c2, c3, c4, dt)
pn1n1 = 0.1
results = []
xnn = np.zeros(len(price_path[0]) - 1)
vn = np.zeros(len(price_path[0]) - 1)
xnn[0] = price_path[0][0]
# %% generate fiter outputs
for i in range(1, (len(price_path[0]) - 1)):
yn, T1, T2, T1_new, T2_new = (
price_path[1][i],
price_path[2][0, i],
price_path[2][1, i],
price_path[2][0, i + 1],
price_path[2][1, i + 1],
)
xnn[i], pn1n1, vn[i - 1] = our_filter.one_step(
yn, price_path[0][i], pn1n1, T1, T2, T1_new, T2_new
)
# xnn[i + 1], pn1n1 = our_filter.one_step(yn, xnn[i], pn1n1, T1, T2)
results.append(pn1n1)
# %% Plot outputs
plt.plot(xnn, label="Prediction")
plt.plot(price_path[0], label="Actual")
plt.legend()
# %% Plot outputs,
plt.hist((xnn / price_path[0][:-1] - 1) * 100, bins=20, label="Errors")
plt.legend()
print("Mean:", np.round(np.mean(abs(xnn / price_path[0][:-1] - 1) * 100), 2))
# %% [markdown]
# # Test against previous day
plt.hist(
(price_path[0][:-1] / price_path[0][1:] - 1) * 100, bins=20, label="Errors"
)
plt.legend()
print(
"Mean:",
np.round(
np.mean(abs(price_path[0][:-1] / price_path[0][1:] - 1) * 100), 2
),
)
# %% [markdown]
# # Futures comparison
print(
"Mean:", np.round(np.mean(abs(vn / price_path[1][:-1]) * 100), 2),
)
plt.plot(vn / price_path[1][:-1], label="error")
plt.legend()
# %%