-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRandomWalk.py
More file actions
328 lines (268 loc) · 14 KB
/
RandomWalk.py
File metadata and controls
328 lines (268 loc) · 14 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import norm, lognorm
from scipy.stats import ttest_1samp, chi2, norm
import scienceplots
plt.style.use(['science', 'notebook', 'grid'])
np.random.seed(27029)
class RandomWalk:
"""
Generates different types of Random Walks.
Parameters:
- Type: Type of process.
"A" - Standard Brownian Motion dx(t)=dW(t)
"B" - Arithmetic Brownian Motion dx(t)=adt+bdW(t)
"C" - Multiplicative Linear Process dx(t)=bx(t)dW(t)
"D" - Geometric Brownian Motion dx(t)=ax(t)dt+bx(t)dW(t)
"E" - Ornstein-Uhlenbeck Process dx(t)=-a(x(t)-mu)dt+bdW(t)
- x0: Initial value.
- N_traiettorie: Number of trajectories.
- Step_Temporali: Number of time steps.
- dt: Time step size.
- a: Drift coefficient.
- b: Instantaneous Standard Deviation/Volatility.
- mu: Stationary mean of the OU process.
"""
def __init__(self, Type="A", x0=1, N_traiettorie=100, Step_Temporali=500, dt=0.01, a=0.1, b=0.2, mu=0):
"""
Initializes a RandomWalk object with the specified parameters.
"""
self.Type = Type
self.x0 = x0
self.N_traiettorie = N_traiettorie
self.Step_Temporali = Step_Temporali
self.dt = dt
self.a = a
self.b = b
self.mu = mu
# Initialize main variables
self.time = np.linspace(dt, Step_Temporali * dt, Step_Temporali)
self.positions = np.zeros((N_traiettorie, Step_Temporali))
self.numerical_variance = np.zeros(Step_Temporali)
self.log_returns = np.zeros((N_traiettorie, Step_Temporali)) if Type in ["C", "D"] else None
self.theoretical_mean = None
self.theoretical_variance = None
def generate(self):
"""Generates the trajectories of the Random Walk."""
for i in range(self.Step_Temporali):
if self.Type == "A":
dx = np.random.normal(0, self.b * np.sqrt(self.dt), self.N_traiettorie)
self.positions[:, i] = self.positions[:, i-1] + dx if i > 0 else self.x0
elif self.Type == "B":
deterministic = self.a * self.dt
stochastic = np.random.normal(0, self.b * np.sqrt(self.dt), self.N_traiettorie)
dx = deterministic + stochastic
self.positions[:, i] = self.positions[:, i-1] + dx if i > 0 else self.x0
elif self.Type in ["C", "D"]:
drift = -0.5 * (self.b ** 2) * self.dt if self.Type == "C" else self.a * self.dt - 0.5 * (self.b ** 2) * self.dt
stochastic = self.b * np.random.normal(0, np.sqrt(self.dt), self.N_traiettorie)
dlnx = drift + stochastic
self.log_returns[:, i] = self.log_returns[:, i-1] + dlnx if i > 0 else np.log(self.x0)
self.positions[:, i] = np.exp(self.log_returns[:, i])
elif self.Type == "E":
mu = self.mu
deterministic = -self.a * (self.positions[:, i-1] - mu) * self.dt if i > 0 else 0
stochastic = np.random.normal(0, self.b * np.sqrt(self.dt), self.N_traiettorie)
dx = deterministic + stochastic
self.positions[:, i] = self.positions[:, i-1] + dx if i > 0 else self.x0
# Calculate numerical variance
self.numerical_variance[i] = np.var(self.positions[:, i])
# Calculate theoretical mean
self.calculate_theoretical_mean()
def calculate_theoretical_mean(self):
"""Calculates the theoretical mean and variance."""
if self.Type == "A":
self.theoretical_mean = np.full_like(self.time, self.x0)
self.theoretical_variance = (self.b ** 2) * self.time
elif self.Type == "B":
self.theoretical_mean = self.x0 + self.a * self.time
self.theoretical_variance = (self.b ** 2) * self.time
elif self.Type == "C":
if self.a != 0:
self.a = 0
self.theoretical_mean = self.x0 * np.exp(self.a * self.time)
self.theoretical_variance = (self.x0**2)*(np.exp((self.b**2)*self.time)-1)
elif self.Type == "D":
self.theoretical_mean = self.x0 * np.exp(self.a * self.time)
self.theoretical_variance = (self.x0**2)*np.exp(2*self.a*self.time)*(np.exp((self.b**2)*self.time)-1)
elif self.Type == "E":
mu = self.mu
self.theoretical_mean = mu + (self.x0 - mu) * np.exp(-self.a * self.time)
self.theoretical_variance = (self.b ** 2) / (2 * self.a) * (1 - np.exp(-2 * self.a * self.time))
def plot_mean_variance(self):
"""Plots empirical and theoretical mean/variance."""
ensemble_mean = np.mean(self.positions, axis=0)
plt.figure(figsize=(10, 5))
plt.plot(self.time, ensemble_mean, label='Empirical Mean', color='blue')
plt.plot(self.time, self.theoretical_mean, label='Theoretical Mean', color='red', linestyle='--')
# Add confidence bands
plt.fill_between(self.time, ensemble_mean - np.sqrt(self.numerical_variance),
ensemble_mean + np.sqrt(self.numerical_variance),
color='blue', alpha=0.2, label='Empirical ± std')
plt.fill_between(self.time, self.theoretical_mean - np.sqrt(self.theoretical_variance),
self.theoretical_mean + np.sqrt(self.theoretical_variance),
color='red', alpha=0.2, label='Theoretical ± std')
plt.xlabel('Time')
plt.ylabel('Mean and Variance')
plt.title(f'Mean and Variance ({self.Type})')
plt.legend(framealpha=0.5)
plt.show()
def plot_trajectories(self, n_shown=10):
"""Plots the trajectories."""
plt.figure(figsize=(10, 5))
for j in range(n_shown):
plt.plot(self.time, self.positions[j, :], lw=0.8, alpha=0.7)
plt.xlabel('Time')
plt.ylabel('$x(t)$')
plt.title(f'Trajectories with Confidence Band - Type {self.Type}')
plt.show()
def plot_log_returns(self):
"""Plots log-returns for Type C and D."""
if self.Type not in ["C", "D"]:
print("Log-returns are only defined for types C and D.")
return
log_mean_theoretical = (np.log(self.x0) - 0.5 * (self.b ** 2) * self.time) if self.Type == "C" else (np.log(self.x0) + self.a * self.time - 0.5 * (self.b ** 2) * self.time)
log_mean_empirical = np.mean(self.log_returns, axis=0)
log_variance_empirical = np.var(self.log_returns, axis=0)
log_variance_theoretical = (self.b ** 2) * self.time
plt.figure(figsize=(10, 5))
plt.plot(self.time, log_mean_empirical, label='Empirical Log-Mean', color='blue')
plt.plot(self.time, log_mean_theoretical, label='Theoretical Log-Mean', color='red', linestyle='--')
# Add confidence bands
plt.fill_between(self.time, log_mean_empirical - np.sqrt(log_variance_empirical),
log_mean_empirical + np.sqrt(log_variance_empirical),
color='blue', alpha=0.3, label='Empirical Log ± std')
plt.fill_between(self.time, log_mean_theoretical - np.sqrt(log_variance_theoretical),
log_mean_theoretical + np.sqrt(log_variance_theoretical),
color='red', alpha=0.2, label='Theoretical Log ± std')
plt.xlabel('Time')
plt.ylabel('$\ln(x(t))$')
plt.title(f'Log-Returns Mean and Variance ({self.Type})')
plt.legend(framealpha=0.5)
plt.show()
def asymptotic_distributions(self, t=None, bins=None):
"""
Compares the empirical distribution of prices (or log-prices for Type C and D) at time t (default last step)
with a Gaussian distribution having the same mean and variance.
Includes a statistical test for mean and variance.
Parameters:
- t: Time step to consider (default last step).
- bins: Number of bins for the histogram (default automatic).
"""
if t is None:
t = self.Step_Temporali - 1 # Last time step
# Determine which dataset to use (prices or log-prices)
if self.Type in ["C", "D"]:
# For Type C and D, work with the logarithm of the data
data_at_t = np.log(self.positions[:, t])
theoretical_mean = (
np.log(self.x0) - 0.5 * (self.b ** 2) * self.time[t]
if self.Type == "C"
else np.log(self.x0) + self.a * self.time[t] - 0.5 * (self.b ** 2) * self.time[t]
)
theoretical_variance = (self.b ** 2) * self.time[t]
else:
# For other types, use prices directly
data_at_t = self.positions[:, t]
theoretical_mean = self.theoretical_mean[t]
theoretical_variance = self.theoretical_variance[t]
# Compute empirical mean and variance
empirical_mean = np.mean(data_at_t)
empirical_variance = np.var(data_at_t, ddof=1) # Sample variance (ddof=1)
# Statistical test for the mean (t-test)
t_stat, p_value_mean = ttest_1samp(data_at_t, theoretical_mean)
# Statistical test for the variance (chi-squared test)
n = len(data_at_t)
chi2_stat = (n - 1) * empirical_variance / theoretical_variance
p_value_variance = 1 - chi2.cdf(chi2_stat, df=n - 1)
# Number of bins (automatic if bins=None)
if bins is None:
bins = int(np.sqrt(len(data_at_t))) # Square root rule
# Histogram of the empirical distribution
plt.hist(data_at_t, bins=bins, density=True, alpha=0.6, color="b", label="Empirical Distribution")
# Theoretical Gaussian distribution with the same mean and variance
x = np.linspace(min(data_at_t), max(data_at_t), 1000)
y_gaussian = norm.pdf(x, theoretical_mean, np.sqrt(theoretical_variance))
plt.plot(x, y_gaussian, 'r-', lw=2, label="Theoretical Gaussian Distribution")
# Title, labels, and legend
plt.title(f"Asymptotic Distributions at Time t={self.time[t]:.2f}")
plt.xlabel("$\\ln[x(t)]$" if self.Type in ["C", "D"] else "Price $x(t)$")
plt.ylabel("Probability Density")
# Add test results to the legend
legend_test = (
# f"Mean Test: p-value={p_value_mean:.3f}\n"
# f"Variance Test: p-value={p_value_variance:.3f}"
)
plt.legend(framealpha=0.5, loc="upper left", title=legend_test)
# Show the plot
plt.show()
def log_normal_distributions(self, t=None, N_bins=None):
"""
Compares the empirical distribution of prices with a theoretical log-normal distribution
at time t (default is the last one) for Types C and D.
Parameters:
- t: Time step to consider (default is the last one).
- bins: Number of bins for the histogram (default is automatic).
"""
if self.Type not in ["C", "D"]:
print("This method is defined only for Types C and D.")
return
if t is None:
t = self.Step_Temporali - 1 # Last time step
if N_bins is None:
N_bins = int(np.sqrt(len(self.positions[:, t]))) # Square root rule
# Empirical data
data_at_t = self.positions[:, t]
# Parameters for the theoretical log-normal distribution
mean_ln = (np.log(self.x0) - 0.5 * (self.b ** 2) * self.time[t] if self.Type == "C" else np.log(self.x0) + self.a * self.time[t] - 0.5 * (self.b ** 2) * self.time[t])
var_ln = (self.b ** 2) * self.time[t]
sigma_ln = np.sqrt(var_ln)
# Empirical mean and variance of the logarithm of the data
log_data = np.log(data_at_t)
empirical_mean_ln = np.mean(log_data)
empirical_var_ln = np.var(log_data, ddof=1) # Sample variance
# Statistical test for the mean (t-test)
t_stat_ln, p_value_ln_mean = ttest_1samp(log_data, mean_ln)
# Generate theoretical x values (logarithmic scale)
x_theoretical = np.logspace(np.log10(min(data_at_t)), np.log10(max(data_at_t)), 100)
#x_theoretical = np.logspace(0.01, np.log10(max(data_at_t)), 100)
y_theoretical = lognorm.pdf(x_theoretical, s=sigma_ln, scale=np.exp(mean_ln))
# Empirical histogram with logarithmic binning
bins = np.logspace(np.log10(min(data_at_t)), np.log10(max(data_at_t)), N_bins)
#bins = np.logspace(0.01, np.log10(max(data_at_t)), N_bins)
plt.hist(data_at_t, bins=bins, density=True, alpha=0.6, color='blue', label="Empirical Distribution")
# Plot the theoretical distribution
plt.plot(x_theoretical, y_theoretical, 'r-', label="Theoretical Distribution (Lognormal)")
# Plot settings
plt.xscale('log') # Logarithmic scale on the x-axis
plt.yscale('log') # Logarithmic scale on the y-axis
plt.xlabel("Value")
plt.ylabel("Density (log)")
plt.title(f"Lognormal Distribution (Type {self.Type}) at time t = {self.time[t]:.2f}")
# Add test results to the legend
test_legend = (
#f"Log Mean Test: p-value={p_value_ln_mean:.3f}\n"
#f"Log Variance Test: p-value={p_value_ln_var:.3f}"
)
plt.legend(framealpha=0.5, loc="upper right", title=test_legend)
#plt.grid(True, which="both", linestyle="--", linewidth=0.5)
plt.show()
# Creazione dell'oggetto RandomWalk
rw = RandomWalk(Type="A", x0=5, N_traiettorie=1000, Step_Temporali=1000, dt=0.01, a=0.1, b=1, mu=0)
#N.B: per C e D riduci di tanto gli step temporali!! tipo 500 (altrimenti per gli altri fai 10000)
# Generazione delle traiettorie
rw.generate()
# Plot dei risultati
rw.plot_mean_variance()
rw.plot_trajectories()
rw.plot_log_returns()
rw.asymptotic_distributions()
rw = RandomWalk(Type="C", x0=5, N_traiettorie=10000, Step_Temporali=450, dt=0.01, a=0.1, b=1, mu=0)
# Generazione delle traiettorie
rw.generate()
# Plot dei risultati
rw.plot_mean_variance()
rw.plot_trajectories()
rw.plot_log_returns()
rw.asymptotic_distributions()
rw.log_normal_distributions()