python-synthetica 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- python_synthetica-0.1.0.dist-info/LICENSE +28 -0
- python_synthetica-0.1.0.dist-info/METADATA +154 -0
- python_synthetica-0.1.0.dist-info/RECORD +10 -0
- python_synthetica-0.1.0.dist-info/WHEEL +4 -0
- synthetica/__init__.py +1 -0
- synthetica/base.py +325 -0
- synthetica/config.py +144 -0
- synthetica/decorators.py +23 -0
- synthetica/models.py +1170 -0
- synthetica/stats.py +92 -0
synthetica/models.py
ADDED
|
@@ -0,0 +1,1170 @@
|
|
|
1
|
+
import numpy as np
|
|
2
|
+
import pandas as pd
|
|
3
|
+
from scipy.stats import levy_stable, norm
|
|
4
|
+
|
|
5
|
+
from synthetica import BaseSynthetic
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class GeometricBrownianMotion(BaseSynthetic):
|
|
9
|
+
"""
|
|
10
|
+
A class for generating synthetic data using the Geometric Brownian Motion
|
|
11
|
+
(GBM) model.
|
|
12
|
+
|
|
13
|
+
GBM is commonly used for modeling asset prices and is a fundamental
|
|
14
|
+
component of the Black-Scholes options pricing formula.
|
|
15
|
+
|
|
16
|
+
Attributes
|
|
17
|
+
----------
|
|
18
|
+
length : int | pd.DatetimeIndex, optional
|
|
19
|
+
The length of the time series or a date index. Default is 252.
|
|
20
|
+
num_paths : int, optional
|
|
21
|
+
The number of paths to generate. Default is 1.
|
|
22
|
+
mean : float, optional
|
|
23
|
+
Mean (“centre”) of the distribution. Defaults to 0.
|
|
24
|
+
delta : float, optional
|
|
25
|
+
The time step size (e.g., 1/252 for daily data). Default is 1/252.
|
|
26
|
+
sigma : float, optional
|
|
27
|
+
The volatility of the stochastic process. Default is 0.125.
|
|
28
|
+
mu : float, optional
|
|
29
|
+
The expected return rate (drift) for the GBM. Default is 0.058.
|
|
30
|
+
freq : str, optional
|
|
31
|
+
The frequency of the data. Default is 'D'.
|
|
32
|
+
seed : int, optional
|
|
33
|
+
The random seed for reproducibility. Default is None.
|
|
34
|
+
|
|
35
|
+
Note
|
|
36
|
+
----
|
|
37
|
+
This method constructs a sequence of log returns which, when exponentiated,
|
|
38
|
+
produce a random Geometric Brownian Motion. GBM is the stochastic
|
|
39
|
+
process underlying the Black Scholes options pricing formula.
|
|
40
|
+
|
|
41
|
+
Example
|
|
42
|
+
-------
|
|
43
|
+
>>> gbm_model = GeometricBrownianMotion(
|
|
44
|
+
... length=252,
|
|
45
|
+
... num_paths=1,
|
|
46
|
+
... delta=1/252,
|
|
47
|
+
... sigma=0.125,
|
|
48
|
+
... mu=0.058
|
|
49
|
+
... )
|
|
50
|
+
>>> synthetic_data = gbm_model.transform()
|
|
51
|
+
"""
|
|
52
|
+
|
|
53
|
+
def __init__(
|
|
54
|
+
self,
|
|
55
|
+
length: int | pd.DatetimeIndex = 252,
|
|
56
|
+
num_paths: int = 1,
|
|
57
|
+
mean: float = 0,
|
|
58
|
+
delta: float = 1/252,
|
|
59
|
+
sigma: float = 0.125,
|
|
60
|
+
mu: float = 0.058,
|
|
61
|
+
freq: str = 'D',
|
|
62
|
+
seed: int = None
|
|
63
|
+
):
|
|
64
|
+
super().__init__(
|
|
65
|
+
length=length,
|
|
66
|
+
num_paths=num_paths,
|
|
67
|
+
mean=mean,
|
|
68
|
+
delta=delta,
|
|
69
|
+
sigma=sigma,
|
|
70
|
+
freq=freq,
|
|
71
|
+
seed=seed
|
|
72
|
+
)
|
|
73
|
+
self.mu = mu
|
|
74
|
+
|
|
75
|
+
def transform(self, matrix: pd.DataFrame | np.ndarray = None) -> pd.Series | pd.DataFrame:
|
|
76
|
+
"""
|
|
77
|
+
Generate synthetic Geometric Brownian Motion (GBM) data.
|
|
78
|
+
|
|
79
|
+
Parameters
|
|
80
|
+
----------
|
|
81
|
+
matrix : pd.DataFrame or np.array, optional
|
|
82
|
+
The matrix applied in Cholesky decomposition (optional).
|
|
83
|
+
|
|
84
|
+
Returns
|
|
85
|
+
-------
|
|
86
|
+
pd.Series | pd.DataFrame
|
|
87
|
+
Data containing synthetic GBM data.
|
|
88
|
+
"""
|
|
89
|
+
sigma_pow_mu_delta = (
|
|
90
|
+
self.mu - 0.5 * np.power(self.sigma, 2.0)
|
|
91
|
+
) * self.delta
|
|
92
|
+
|
|
93
|
+
noise = (
|
|
94
|
+
self.cholesky_transform(self.white_noise, matrix)
|
|
95
|
+
if matrix is not None
|
|
96
|
+
else self.white_noise
|
|
97
|
+
)
|
|
98
|
+
|
|
99
|
+
paths = np.array(noise) + sigma_pow_mu_delta
|
|
100
|
+
prices = self.to_prices(paths)
|
|
101
|
+
|
|
102
|
+
return self.to_pandas(prices)
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
class Heston(BaseSynthetic):
|
|
106
|
+
"""
|
|
107
|
+
A class for generating synthetic data using the Heston model.
|
|
108
|
+
|
|
109
|
+
This class generates synthetic data based on the Heston model, which is
|
|
110
|
+
commonly used for modeling stochastic volatility. It combines a stochastic
|
|
111
|
+
volatility process with geometric Brownian motion.
|
|
112
|
+
|
|
113
|
+
Attributes
|
|
114
|
+
----------
|
|
115
|
+
length : int, optional
|
|
116
|
+
The length of the time series. Default is 252.
|
|
117
|
+
num_paths : int, optional
|
|
118
|
+
The number of paths to generate. Default is 1.
|
|
119
|
+
mean : float, optional
|
|
120
|
+
Mean (“centre”) of the distribution. Defaults to 0.
|
|
121
|
+
delta : float, optional
|
|
122
|
+
The time step size (e.g., 1/252 for daily data). Default is 1/252.
|
|
123
|
+
sigma : float, optional
|
|
124
|
+
The volatility of the stochastic processes. Default is 0.125.
|
|
125
|
+
matrix : pd.DataFrame or np.array, optional
|
|
126
|
+
The matrix applied in Cholesky decomposition (optional).
|
|
127
|
+
start_value : float, optional
|
|
128
|
+
The starting value for the process. Default is 100.0.
|
|
129
|
+
rho : float, optional
|
|
130
|
+
The correlation coefficient between two Brownian motions. Default
|
|
131
|
+
is 0.7.
|
|
132
|
+
vol0 : float, optional
|
|
133
|
+
The initial volatility value. Default is 0.25^2.
|
|
134
|
+
rf : float, optional
|
|
135
|
+
The risk-free rate. Default is 0.02.
|
|
136
|
+
kappa : float, optional
|
|
137
|
+
The rate of mean reversion for the volatility. Default is 3.
|
|
138
|
+
theta : float, optional
|
|
139
|
+
The long-run average volatility. Default is 0.20^2.
|
|
140
|
+
nu : float, optional
|
|
141
|
+
The volatility of the volatility. Default is 0.6.
|
|
142
|
+
freq : str, optional
|
|
143
|
+
The frequency of the data. Default is 'D'.
|
|
144
|
+
seed : int, optional
|
|
145
|
+
The random seed for reproducibility. Default is None.
|
|
146
|
+
|
|
147
|
+
Example
|
|
148
|
+
-------
|
|
149
|
+
>>> heston_model = Heston(
|
|
150
|
+
... length=252,
|
|
151
|
+
... num_paths=1, d
|
|
152
|
+
... elta=1/252,
|
|
153
|
+
... sigma=0.125,
|
|
154
|
+
... start_value=100.0,
|
|
155
|
+
... rho=0.7,
|
|
156
|
+
... vol0=0.25^2,
|
|
157
|
+
... rf=0.02, kappa=3,
|
|
158
|
+
... theta=0.20^2,
|
|
159
|
+
... nu=0.6
|
|
160
|
+
... )
|
|
161
|
+
>>> synthetic_data = heston_model.transform()
|
|
162
|
+
"""
|
|
163
|
+
|
|
164
|
+
def __init__(
|
|
165
|
+
self,
|
|
166
|
+
length: int | pd.DatetimeIndex = 252,
|
|
167
|
+
num_paths: int = 1,
|
|
168
|
+
mean: float = 0,
|
|
169
|
+
delta: float = 1/252,
|
|
170
|
+
sigma: float = 0.125,
|
|
171
|
+
start_value: float = 100.0,
|
|
172
|
+
rho: float = 0.7,
|
|
173
|
+
vol0: float = 0.25**2,
|
|
174
|
+
rf: float = 0.02,
|
|
175
|
+
kappa: float = 3,
|
|
176
|
+
theta: float = 0.20**2,
|
|
177
|
+
nu: float = 0.6,
|
|
178
|
+
freq: str = 'D',
|
|
179
|
+
seed: int = None
|
|
180
|
+
):
|
|
181
|
+
super().__init__(
|
|
182
|
+
length=length,
|
|
183
|
+
num_paths=num_paths,
|
|
184
|
+
mean=mean,
|
|
185
|
+
delta=delta,
|
|
186
|
+
sigma=sigma,
|
|
187
|
+
freq=freq,
|
|
188
|
+
seed=seed
|
|
189
|
+
)
|
|
190
|
+
self.start_value = start_value
|
|
191
|
+
self.rho = rho
|
|
192
|
+
self.vol0 = vol0
|
|
193
|
+
self.rf = rf
|
|
194
|
+
self.kappa = kappa
|
|
195
|
+
self.theta = theta
|
|
196
|
+
self.nu = nu
|
|
197
|
+
|
|
198
|
+
def transform(self, matrix: pd.DataFrame | np.ndarray = None) -> pd.Series | pd.DataFrame:
|
|
199
|
+
"""
|
|
200
|
+
Generate synthetic Heston model data.
|
|
201
|
+
|
|
202
|
+
Parameters
|
|
203
|
+
----------
|
|
204
|
+
matrix : pd.DataFrame or np.array, optional
|
|
205
|
+
The matrix applied in Cholesky decomposition (optional).
|
|
206
|
+
|
|
207
|
+
Returns
|
|
208
|
+
-------
|
|
209
|
+
pd.Series | pd.DataFrame
|
|
210
|
+
Data containing synthetic Heston model data.
|
|
211
|
+
"""
|
|
212
|
+
bm_volatility = np.random.multivariate_normal(
|
|
213
|
+
mean=np.array([0, 0]),
|
|
214
|
+
cov=np.array([[1, self.rho], [self.rho, 1]]),
|
|
215
|
+
size=(self.length, self.num_paths)
|
|
216
|
+
)
|
|
217
|
+
|
|
218
|
+
if matrix is not None:
|
|
219
|
+
bm_volatility = self.cholesky_transform(bm_volatility, matrix)
|
|
220
|
+
|
|
221
|
+
# arrays for storing prices and variances
|
|
222
|
+
prices = np.full(
|
|
223
|
+
shape=((self.length+1, self.num_paths)),
|
|
224
|
+
fill_value=self.start_value,
|
|
225
|
+
dtype=np.float64
|
|
226
|
+
)
|
|
227
|
+
volatility = np.full(
|
|
228
|
+
shape=((self.length+1, self.num_paths)),
|
|
229
|
+
fill_value=self.vol0,
|
|
230
|
+
dtype=np.float64
|
|
231
|
+
)
|
|
232
|
+
|
|
233
|
+
for length in range(1, self.length + 1):
|
|
234
|
+
prices[length] = (
|
|
235
|
+
prices[length-1]
|
|
236
|
+
* np.exp(
|
|
237
|
+
(self.rf - 0.5 * volatility[length-1])
|
|
238
|
+
* self.delta
|
|
239
|
+
+ np.sqrt(volatility[length-1] * self.delta)
|
|
240
|
+
* bm_volatility[length-1, :, 0]
|
|
241
|
+
)
|
|
242
|
+
)
|
|
243
|
+
prev_vol = (
|
|
244
|
+
volatility[length-1] + self.kappa *
|
|
245
|
+
(self.theta - volatility[length-1]) *
|
|
246
|
+
self.delta + self.nu *
|
|
247
|
+
np.sqrt(volatility[length-1] * self.delta) *
|
|
248
|
+
bm_volatility[length-1, :, 1]
|
|
249
|
+
)
|
|
250
|
+
volatility[length] = np.maximum(prev_vol, 0)
|
|
251
|
+
|
|
252
|
+
return self.to_pandas(prices[1:,])
|
|
253
|
+
|
|
254
|
+
|
|
255
|
+
class Merton(BaseSynthetic):
|
|
256
|
+
"""
|
|
257
|
+
A class for generating synthetic data using the Merton model.
|
|
258
|
+
|
|
259
|
+
This class generates synthetic data based on the Merton model, which is
|
|
260
|
+
used to model asset prices in the presence of jumps. It combines a jump
|
|
261
|
+
diffusion process with geometric Brownian motion.
|
|
262
|
+
|
|
263
|
+
Attributes
|
|
264
|
+
----------
|
|
265
|
+
length : int, optional
|
|
266
|
+
The length of the time series. Default is 252.
|
|
267
|
+
num_paths : int, optional
|
|
268
|
+
The number of paths to generate. Default is 1.
|
|
269
|
+
mean : float, optional
|
|
270
|
+
Mean (“centre”) of the distribution. Defaults to 0.
|
|
271
|
+
delta : float, optional
|
|
272
|
+
The time step size (e.g., 1/252 for daily data). Default is 1/252.
|
|
273
|
+
sigma : float, optional
|
|
274
|
+
The volatility of the stochastic process. Default is 0.125.
|
|
275
|
+
lmbda : float, optional
|
|
276
|
+
The jump intensity parameter. Default is 0.00025.
|
|
277
|
+
var : float, optional
|
|
278
|
+
The variance of jump sizes. Default is 0.001.
|
|
279
|
+
mu : float, optional
|
|
280
|
+
The expected return rate (drift) for the Merton model. Default is 0.2.
|
|
281
|
+
freq : str, optional
|
|
282
|
+
The frequency of the data. Default is 'D'.
|
|
283
|
+
seed : int, optional
|
|
284
|
+
The random seed for reproducibility. Default is None.
|
|
285
|
+
|
|
286
|
+
Note
|
|
287
|
+
------
|
|
288
|
+
This method produces a sequence of Jump Sizes which represent a jump
|
|
289
|
+
diffusion process. These jumps are combined with a geometric brownian
|
|
290
|
+
motion (log returns) to produce the Merton model.
|
|
291
|
+
|
|
292
|
+
Example
|
|
293
|
+
-------
|
|
294
|
+
>>> merton_model = Merton(
|
|
295
|
+
... length=252,
|
|
296
|
+
... num_paths=1,
|
|
297
|
+
... delta=1/252,
|
|
298
|
+
... sigma=0.125,
|
|
299
|
+
... lmbda=0.00025,
|
|
300
|
+
... var=0.001,
|
|
301
|
+
... mu=0.2
|
|
302
|
+
... )
|
|
303
|
+
synthetic_data = merton_model.transform()
|
|
304
|
+
"""
|
|
305
|
+
|
|
306
|
+
def __init__(
|
|
307
|
+
self,
|
|
308
|
+
length: int | pd.DatetimeIndex = 252,
|
|
309
|
+
num_paths: int = 1,
|
|
310
|
+
mean: float = 0,
|
|
311
|
+
delta: float = 1/252,
|
|
312
|
+
sigma: float = 0.125,
|
|
313
|
+
lmbda: float = 0.00025,
|
|
314
|
+
var: float = 0.001,
|
|
315
|
+
mu: float = 0.2,
|
|
316
|
+
freq: str = 'D',
|
|
317
|
+
seed: int = None
|
|
318
|
+
):
|
|
319
|
+
super().__init__(
|
|
320
|
+
length=length,
|
|
321
|
+
num_paths=num_paths,
|
|
322
|
+
mean=mean,
|
|
323
|
+
delta=delta,
|
|
324
|
+
sigma=sigma,
|
|
325
|
+
freq=freq,
|
|
326
|
+
seed=seed
|
|
327
|
+
)
|
|
328
|
+
self.lmbda = lmbda
|
|
329
|
+
self.var = var
|
|
330
|
+
self.mu = mu
|
|
331
|
+
|
|
332
|
+
def transform(self, matrix: pd.DataFrame | np.ndarray = None) -> pd.Series | pd.DataFrame:
|
|
333
|
+
"""
|
|
334
|
+
Generate synthetic Merton model data.
|
|
335
|
+
|
|
336
|
+
Parameters
|
|
337
|
+
----------
|
|
338
|
+
matrix : pd.DataFrame or np.array, optional
|
|
339
|
+
The matrix applied in Cholesky decomposition (optional).
|
|
340
|
+
|
|
341
|
+
Returns
|
|
342
|
+
-------
|
|
343
|
+
pd.Series | pd.DataFrame
|
|
344
|
+
Data containing synthetic Merton model data.
|
|
345
|
+
"""
|
|
346
|
+
small_lmbda = - (1.0 / self.lmbda)
|
|
347
|
+
jump = np.zeros((self.length, self.num_paths), dtype=np.float64)
|
|
348
|
+
|
|
349
|
+
for path in range(self.num_paths):
|
|
350
|
+
s_n = time = 0
|
|
351
|
+
|
|
352
|
+
while s_n < self.length:
|
|
353
|
+
s_n += small_lmbda * np.log(np.random.uniform(0, 1))
|
|
354
|
+
|
|
355
|
+
for length in range(self.length):
|
|
356
|
+
if time * self.delta <= s_n * self.delta <= (length + 1) * self.delta:
|
|
357
|
+
rand = np.random.normal(self.mu, self.var)
|
|
358
|
+
jump[length, path] += rand
|
|
359
|
+
break
|
|
360
|
+
|
|
361
|
+
time += 1
|
|
362
|
+
|
|
363
|
+
noise = (
|
|
364
|
+
self.cholesky_transform(self.white_noise, matrix)
|
|
365
|
+
if matrix is not None
|
|
366
|
+
else self.white_noise
|
|
367
|
+
)
|
|
368
|
+
|
|
369
|
+
paths = np.add(jump, noise)
|
|
370
|
+
prices = self.to_prices(paths)
|
|
371
|
+
|
|
372
|
+
return self.to_pandas(prices)
|
|
373
|
+
|
|
374
|
+
|
|
375
|
+
class Poisson(BaseSynthetic):
|
|
376
|
+
"""
|
|
377
|
+
A class for generating synthetic data using the poisson (jump) process
|
|
378
|
+
model.
|
|
379
|
+
|
|
380
|
+
This class generates synthetic data using the poisson model, which
|
|
381
|
+
combines a jump diffusion process with a Geometric Brownian Motion (GBM).
|
|
382
|
+
|
|
383
|
+
Attributes
|
|
384
|
+
----------
|
|
385
|
+
length : int, optional
|
|
386
|
+
The length of the time series. Default is 252.
|
|
387
|
+
num_paths : int, optional
|
|
388
|
+
The number of paths to generate. Default is 1.
|
|
389
|
+
mean : float, optional
|
|
390
|
+
Mean (“centre”) of the distribution. Defaults to 0.
|
|
391
|
+
delta : float, optional
|
|
392
|
+
The time step size (e.g., 1/252 for daily data). Default is 1/252.
|
|
393
|
+
sigma : float, optional
|
|
394
|
+
The volatility of the GBM component. Default is 0.125.
|
|
395
|
+
lmbda : float, optional
|
|
396
|
+
The jump intensity parameter. Default is 0.00025.
|
|
397
|
+
var : float, optional
|
|
398
|
+
The jump size variance. Default is 0.001.
|
|
399
|
+
mu : float, optional
|
|
400
|
+
The mean of jump sizes. Default is 0.2.
|
|
401
|
+
freq : str, optional
|
|
402
|
+
The frequency of the data. Default is 'D'.
|
|
403
|
+
seed : int, optional
|
|
404
|
+
The random seed for reproducibility. Default is None.
|
|
405
|
+
|
|
406
|
+
Note
|
|
407
|
+
----
|
|
408
|
+
This method produces a sequence of jump sizes which represent a jump
|
|
409
|
+
diffusion process. These jumps are combined with a geometric brownian
|
|
410
|
+
motion (log returns). This model is an alternative to the Merton model.
|
|
411
|
+
|
|
412
|
+
Example
|
|
413
|
+
-------
|
|
414
|
+
>>> poisson = Poisson(
|
|
415
|
+
... length=252,
|
|
416
|
+
... num_paths=1,
|
|
417
|
+
... delta=1/252,
|
|
418
|
+
... sigma=0.125,
|
|
419
|
+
... lmbda=0.00025,
|
|
420
|
+
... var=0.001,
|
|
421
|
+
... mu=0.2
|
|
422
|
+
... )
|
|
423
|
+
synthetic_data = poisson.transform()
|
|
424
|
+
"""
|
|
425
|
+
|
|
426
|
+
def __init__(
|
|
427
|
+
self,
|
|
428
|
+
length: int | pd.DatetimeIndex = 252,
|
|
429
|
+
num_paths: int = 1,
|
|
430
|
+
mean: float = 0,
|
|
431
|
+
delta: float = 1/252,
|
|
432
|
+
sigma: float = 0.125,
|
|
433
|
+
lmbda: float = 0.00025,
|
|
434
|
+
var: float = 0.001,
|
|
435
|
+
mu: float = 0.2,
|
|
436
|
+
freq: str = 'D',
|
|
437
|
+
seed: int = None
|
|
438
|
+
):
|
|
439
|
+
super().__init__(
|
|
440
|
+
length=length,
|
|
441
|
+
num_paths=num_paths,
|
|
442
|
+
mean=mean,
|
|
443
|
+
delta=delta,
|
|
444
|
+
sigma=sigma,
|
|
445
|
+
freq=freq,
|
|
446
|
+
seed=seed
|
|
447
|
+
)
|
|
448
|
+
self.lmbda = lmbda
|
|
449
|
+
self.var = var
|
|
450
|
+
self.mu = mu
|
|
451
|
+
|
|
452
|
+
@property
|
|
453
|
+
def lambda_poisson(self) -> float:
|
|
454
|
+
return 2 * (1 / self.length)
|
|
455
|
+
|
|
456
|
+
@lambda_poisson.setter
|
|
457
|
+
def lambda_poisson(self, l: int | float = 2) -> float:
|
|
458
|
+
self.lambda_poisson = l * (1 / self.length)
|
|
459
|
+
|
|
460
|
+
def _inverse_poisson(self) -> int:
|
|
461
|
+
"""
|
|
462
|
+
Calculate the inverse of the Poisson cumulative distribution function.
|
|
463
|
+
|
|
464
|
+
Parameters:
|
|
465
|
+
----------
|
|
466
|
+
u : float
|
|
467
|
+
Random uniform variable.
|
|
468
|
+
lmbda : float
|
|
469
|
+
Lambda parameter for Poisson distribution.
|
|
470
|
+
|
|
471
|
+
Returns
|
|
472
|
+
-------
|
|
473
|
+
int
|
|
474
|
+
Inverse Poisson random variable.
|
|
475
|
+
"""
|
|
476
|
+
poisson = np.exp(-self.lambda_poisson)
|
|
477
|
+
F = poisson
|
|
478
|
+
k = 0
|
|
479
|
+
u = np.random.rand()
|
|
480
|
+
while u > F:
|
|
481
|
+
k = k + 1
|
|
482
|
+
poisson = poisson * (self.lambda_poisson / k)
|
|
483
|
+
F = F + poisson
|
|
484
|
+
|
|
485
|
+
return k
|
|
486
|
+
|
|
487
|
+
def transform(self, matrix: pd.DataFrame | np.ndarray = None) -> pd.Series | pd.DataFrame:
|
|
488
|
+
"""
|
|
489
|
+
Generate synthetic data using the Poisson model.
|
|
490
|
+
|
|
491
|
+
Parameters
|
|
492
|
+
----------
|
|
493
|
+
matrix : pd.DataFrame or np.array, optional
|
|
494
|
+
The matrix applied in Cholesky decomposition (optional).
|
|
495
|
+
|
|
496
|
+
Returns
|
|
497
|
+
-------
|
|
498
|
+
pd.Series | pd.DataFrame
|
|
499
|
+
Data containing synthetic data following the poisson process model.
|
|
500
|
+
"""
|
|
501
|
+
jump = np.zeros((self.length, self.num_paths))
|
|
502
|
+
for path in range(self.num_paths):
|
|
503
|
+
for length in range(self.length):
|
|
504
|
+
alea_poisson = self._inverse_poisson()
|
|
505
|
+
if alea_poisson != 0:
|
|
506
|
+
for j in range(0, alea_poisson):
|
|
507
|
+
jump[length, path] += norm.ppf(
|
|
508
|
+
np.random.rand(),
|
|
509
|
+
loc=self.mu,
|
|
510
|
+
scale=self.var
|
|
511
|
+
)
|
|
512
|
+
|
|
513
|
+
noise = (
|
|
514
|
+
self.cholesky_transform(self.white_noise, matrix)
|
|
515
|
+
if matrix is not None
|
|
516
|
+
else self.white_noise
|
|
517
|
+
)
|
|
518
|
+
|
|
519
|
+
paths = np.add(jump, noise)
|
|
520
|
+
prices = self.to_prices(paths)
|
|
521
|
+
|
|
522
|
+
return self.to_pandas(prices)
|
|
523
|
+
|
|
524
|
+
|
|
525
|
+
class LevyStable(BaseSynthetic):
|
|
526
|
+
"""
|
|
527
|
+
A class for generating synthetic data using the Levy Stable Process model.
|
|
528
|
+
|
|
529
|
+
This class generates synthetic data using the Levy Stable Process model,
|
|
530
|
+
which generalizes several distributions including left-skewed Levy, Levy,
|
|
531
|
+
Cauchy, and Normal distributions.
|
|
532
|
+
|
|
533
|
+
Attributes
|
|
534
|
+
----------
|
|
535
|
+
length : int, optional
|
|
536
|
+
The length of the time series. Default is 252.
|
|
537
|
+
num_paths : int, optional
|
|
538
|
+
The number of paths to generate. Default is 1.
|
|
539
|
+
mean : float, optional
|
|
540
|
+
Mean (“centre”) of the distribution. Defaults to 0.
|
|
541
|
+
delta : float, optional
|
|
542
|
+
The time step size (e.g., 1/252 for daily data). Default is 1/252.
|
|
543
|
+
sigma : float, optional
|
|
544
|
+
The scale parameter of the Levy Stable distribution. Default is 0.125.
|
|
545
|
+
alpha : float, optional
|
|
546
|
+
The alpha parameter of the Levy Stable distribution. Should be in the
|
|
547
|
+
range (0, 2].
|
|
548
|
+
Default is 1.68.
|
|
549
|
+
beta : float, optional
|
|
550
|
+
The beta parameter of the Levy Stable distribution. Should be in the
|
|
551
|
+
range [-1, 1].
|
|
552
|
+
Default is 0.01.
|
|
553
|
+
freq : str, optional
|
|
554
|
+
The frequency of the data. Default is 'D'.
|
|
555
|
+
seed : int, optional
|
|
556
|
+
The random seed for reproducibility. Default is None.
|
|
557
|
+
|
|
558
|
+
Note
|
|
559
|
+
----
|
|
560
|
+
This method returns a Levy stable process. levy_stable generalizes
|
|
561
|
+
several distributions: 0 < alpha <= 2 and -1 <= beta <= 1, such as:
|
|
562
|
+
|
|
563
|
+
| Alpha | Beta | Equivalent | Description |
|
|
564
|
+
|-------|------|------------|--------------------|
|
|
565
|
+
| 0.5 | -1 | levy_l | Left-skewed Levy |
|
|
566
|
+
| 0.5 | 1 | levy | Levy |
|
|
567
|
+
| 1 | 0 | cauchy | Cauchy |
|
|
568
|
+
| 2 | Any | norm | Normal |
|
|
569
|
+
|
|
570
|
+
Example
|
|
571
|
+
-------
|
|
572
|
+
>>> levy_stable_model = LevyStable(
|
|
573
|
+
... length=252,
|
|
574
|
+
... num_paths=1,
|
|
575
|
+
... delta=1/252,
|
|
576
|
+
... sigma=0.125,
|
|
577
|
+
... alpha=1.68,
|
|
578
|
+
... beta=0.01
|
|
579
|
+
... )
|
|
580
|
+
>>> synthetic_data = levy_stable_model.transform()
|
|
581
|
+
"""
|
|
582
|
+
|
|
583
|
+
def __init__(
|
|
584
|
+
self,
|
|
585
|
+
length: int | pd.DatetimeIndex = 252,
|
|
586
|
+
num_paths: int = 1,
|
|
587
|
+
mean: float = 0,
|
|
588
|
+
delta: float = 1/252,
|
|
589
|
+
sigma: float = 0.125,
|
|
590
|
+
alpha: float = 1.68,
|
|
591
|
+
beta: float = 0.01,
|
|
592
|
+
freq: str = 'D',
|
|
593
|
+
seed: int = None
|
|
594
|
+
):
|
|
595
|
+
super().__init__(
|
|
596
|
+
length=length,
|
|
597
|
+
num_paths=num_paths,
|
|
598
|
+
mean=mean,
|
|
599
|
+
delta=delta,
|
|
600
|
+
sigma=sigma,
|
|
601
|
+
freq=freq,
|
|
602
|
+
seed=seed
|
|
603
|
+
)
|
|
604
|
+
self.alpha = alpha
|
|
605
|
+
self.beta = beta
|
|
606
|
+
|
|
607
|
+
def transform(self, matrix: pd.DataFrame | np.ndarray = None) -> pd.Series | pd.DataFrame:
|
|
608
|
+
"""
|
|
609
|
+
Generate synthetic data using the Levy Stable Process model.
|
|
610
|
+
|
|
611
|
+
Parameters
|
|
612
|
+
----------
|
|
613
|
+
matrix : pd.DataFrame or np.array, optional
|
|
614
|
+
The matrix applied in Cholesky decomposition (optional).
|
|
615
|
+
|
|
616
|
+
Returns
|
|
617
|
+
-------
|
|
618
|
+
pd.Series | pd.DataFrame
|
|
619
|
+
Data containing synthetic data following the Levy Stable Process model.
|
|
620
|
+
"""
|
|
621
|
+
paths = levy_stable.rvs(
|
|
622
|
+
self.alpha,
|
|
623
|
+
self.beta,
|
|
624
|
+
loc=self.mean,
|
|
625
|
+
scale=np.sqrt(self.delta) * self.sigma,
|
|
626
|
+
size=(self.length, self.num_paths)
|
|
627
|
+
)
|
|
628
|
+
|
|
629
|
+
if matrix is not None:
|
|
630
|
+
paths = self.cholesky_transform(paths, matrix)
|
|
631
|
+
|
|
632
|
+
prices = self.to_prices(paths)
|
|
633
|
+
|
|
634
|
+
return self.to_pandas(prices)
|
|
635
|
+
|
|
636
|
+
|
|
637
|
+
class CIR(BaseSynthetic):
|
|
638
|
+
"""
|
|
639
|
+
A class for generating synthetic data using the Cox-Ingersoll-Ross (CIR)
|
|
640
|
+
model.
|
|
641
|
+
|
|
642
|
+
This class generates synthetic interest rate data based on the
|
|
643
|
+
Cox-Ingersoll-Ross (CIR) model.
|
|
644
|
+
|
|
645
|
+
The CIR model is a mean-reverting model commonly used for modeling interest
|
|
646
|
+
rates.
|
|
647
|
+
|
|
648
|
+
Attributes
|
|
649
|
+
----------
|
|
650
|
+
length : int, optional
|
|
651
|
+
The length of the time series. Default is 252.
|
|
652
|
+
num_paths : int, optional
|
|
653
|
+
The number of paths to generate. Default is 1.
|
|
654
|
+
mean : float, optional
|
|
655
|
+
Mean (“centre”) of the distribution. Defaults to 0.
|
|
656
|
+
delta : float, optional
|
|
657
|
+
The time step size (e.g., 1/252 for daily data). Default is 1/252.
|
|
658
|
+
sigma : float, optional
|
|
659
|
+
The volatility of the stochastic processes. Default is 0.125.
|
|
660
|
+
start_value : float, optional
|
|
661
|
+
The starting value. Default is 0.5.
|
|
662
|
+
kappa : float, optional
|
|
663
|
+
The rate of mean reversion for the CIR model. Default is 3.0.
|
|
664
|
+
mu : float, optional
|
|
665
|
+
The long-run average interest rate for the CIR model. Default is 0.5.
|
|
666
|
+
freq : str, optional
|
|
667
|
+
The frequency of the data. Default is 'D'.
|
|
668
|
+
seed : int, optional
|
|
669
|
+
The random seed for reproducibility. Default is None.
|
|
670
|
+
|
|
671
|
+
Note
|
|
672
|
+
----
|
|
673
|
+
This method returns the rate levels of a mean-reverting cox ingersoll
|
|
674
|
+
ross process. It is used to model interest rates as well as stochastic
|
|
675
|
+
volatility in the Heston model. Because the returns between the
|
|
676
|
+
underlying and the stochastic volatility should be correlated we pass a
|
|
677
|
+
correlated Brownian motion process into the method from which the
|
|
678
|
+
interest rate levels are constructed. The other correlated process is
|
|
679
|
+
used in the Heston model.
|
|
680
|
+
|
|
681
|
+
Example
|
|
682
|
+
--------
|
|
683
|
+
>>> cir_model = CIR(
|
|
684
|
+
... length=252,
|
|
685
|
+
... num_paths=1,
|
|
686
|
+
... delta=1/252,
|
|
687
|
+
... sigma=0.125,
|
|
688
|
+
... start_value=0.5,
|
|
689
|
+
... kappa=3.0,
|
|
690
|
+
... mu=0.5
|
|
691
|
+
... )
|
|
692
|
+
>>> synthetic_data = cir_model.transform()
|
|
693
|
+
"""
|
|
694
|
+
|
|
695
|
+
def __init__(
|
|
696
|
+
self,
|
|
697
|
+
length: int | pd.DatetimeIndex = 252,
|
|
698
|
+
num_paths: int = 1,
|
|
699
|
+
mean: float = 0,
|
|
700
|
+
delta: float = 1/252,
|
|
701
|
+
sigma: float = 0.125,
|
|
702
|
+
start_value: float = 0.5,
|
|
703
|
+
kappa: float = 6.0,
|
|
704
|
+
mu: float = 0.5,
|
|
705
|
+
freq: str = 'D',
|
|
706
|
+
seed: int = None
|
|
707
|
+
):
|
|
708
|
+
super().__init__(
|
|
709
|
+
length=length,
|
|
710
|
+
num_paths=num_paths,
|
|
711
|
+
mean=mean,
|
|
712
|
+
delta=delta,
|
|
713
|
+
sigma=sigma,
|
|
714
|
+
freq=freq,
|
|
715
|
+
seed=seed
|
|
716
|
+
)
|
|
717
|
+
self.start_value = start_value
|
|
718
|
+
self.kappa = kappa
|
|
719
|
+
self.mu = mu
|
|
720
|
+
|
|
721
|
+
def transform(self, matrix: pd.DataFrame | np.ndarray = None) -> pd.Series | pd.DataFrame:
|
|
722
|
+
"""
|
|
723
|
+
Generate synthetic interest rate data using the Cox-Ingersoll-Ross
|
|
724
|
+
(CIR) model.
|
|
725
|
+
|
|
726
|
+
Note
|
|
727
|
+
----
|
|
728
|
+
The main difference between this and the Ornstein Uhlenbeck model
|
|
729
|
+
is that we multiply the 'random' component by the square-root of
|
|
730
|
+
the previous level i.e. the process has level dependent interest
|
|
731
|
+
rates.
|
|
732
|
+
|
|
733
|
+
Parameters
|
|
734
|
+
----------
|
|
735
|
+
matrix : pd.DataFrame or np.array, optional
|
|
736
|
+
The matrix applied in Cholesky decomposition (optional).
|
|
737
|
+
|
|
738
|
+
Returns
|
|
739
|
+
-------
|
|
740
|
+
pd.Series | pd.DataFrame
|
|
741
|
+
Data containing synthetic interest rate data.
|
|
742
|
+
"""
|
|
743
|
+
paths = np.full((self.length+1, self.num_paths), self.start_value)
|
|
744
|
+
|
|
745
|
+
noise = (
|
|
746
|
+
self.cholesky_transform(self.white_noise, matrix)
|
|
747
|
+
if matrix is not None
|
|
748
|
+
else self.white_noise
|
|
749
|
+
)
|
|
750
|
+
|
|
751
|
+
for length in range(1, self.length + 1):
|
|
752
|
+
drift = (
|
|
753
|
+
self.kappa * (self.mu - paths[length-1]) * self.delta
|
|
754
|
+
)
|
|
755
|
+
deviation = (
|
|
756
|
+
np.sqrt(paths[length-1]) * noise[length-1]
|
|
757
|
+
)
|
|
758
|
+
paths[length] = paths[length-1] + drift + deviation
|
|
759
|
+
|
|
760
|
+
return self.to_pandas(paths[1:,])
|
|
761
|
+
|
|
762
|
+
|
|
763
|
+
class MeanReverting(BaseSynthetic):
|
|
764
|
+
"""
|
|
765
|
+
A class for generating synthetic data using a mean-reverting
|
|
766
|
+
Ornstein-Uhlenbeck model.
|
|
767
|
+
|
|
768
|
+
Note
|
|
769
|
+
----
|
|
770
|
+
This class generates synthetic data using a mean-reverting
|
|
771
|
+
Ornstein-Uhlenbeck model. The Ornstein-Uhlenbeck model is commonly used for
|
|
772
|
+
modeling mean-reverting processes.
|
|
773
|
+
|
|
774
|
+
Attributes
|
|
775
|
+
----------
|
|
776
|
+
length : int, optional
|
|
777
|
+
The length of the time series. Default is 252.
|
|
778
|
+
num_paths : int, optional
|
|
779
|
+
The number of paths to generate. Default is 1.
|
|
780
|
+
mean : float, optional
|
|
781
|
+
Mean (“centre”) of the distribution. Defaults to 0.
|
|
782
|
+
delta : float, optional
|
|
783
|
+
The time step size (e.g., 1/252 for daily data). Default is 1/252.
|
|
784
|
+
sigma : float, optional
|
|
785
|
+
The volatility of the stochastic processes. Default is 0.125.
|
|
786
|
+
start_value : float, optional
|
|
787
|
+
The starting value. Default is 0.5.
|
|
788
|
+
kappa : float, optional
|
|
789
|
+
The rate of mean reversion for the Ornstein-Uhlenbeck model. Default
|
|
790
|
+
is 3.0.
|
|
791
|
+
mu : float, optional
|
|
792
|
+
The long-run average interest rate for the Ornstein-Uhlenbeck model.
|
|
793
|
+
Default is 0.5.
|
|
794
|
+
freq : str, optional
|
|
795
|
+
The frequency of the data. Default is 'D'.
|
|
796
|
+
seed : int, optional
|
|
797
|
+
The random seed for reproducibility. Default is None.
|
|
798
|
+
|
|
799
|
+
Example
|
|
800
|
+
-------
|
|
801
|
+
>>> ou_model = MeanReverting(
|
|
802
|
+
... length=252,
|
|
803
|
+
... num_paths=1,
|
|
804
|
+
... delta=1/252,
|
|
805
|
+
... sigma=0.125,
|
|
806
|
+
... start_value=0.5,
|
|
807
|
+
... kappa=3.0,
|
|
808
|
+
... mu=0.5
|
|
809
|
+
... )
|
|
810
|
+
>>> synthetic_data = ou_model.transform()
|
|
811
|
+
"""
|
|
812
|
+
|
|
813
|
+
def __init__(
|
|
814
|
+
self,
|
|
815
|
+
length: int | pd.DatetimeIndex = 252,
|
|
816
|
+
num_paths: int = 1,
|
|
817
|
+
mean: float = 0,
|
|
818
|
+
delta: float = 1/252,
|
|
819
|
+
sigma: float = 0.125,
|
|
820
|
+
start_value: float = 0.5,
|
|
821
|
+
kappa: float = 6.0,
|
|
822
|
+
mu: float = 0.5,
|
|
823
|
+
freq: str = 'D',
|
|
824
|
+
seed: int = None
|
|
825
|
+
):
|
|
826
|
+
super().__init__(
|
|
827
|
+
length=length,
|
|
828
|
+
num_paths=num_paths,
|
|
829
|
+
mean=mean,
|
|
830
|
+
delta=delta,
|
|
831
|
+
sigma=sigma,
|
|
832
|
+
freq=freq,
|
|
833
|
+
seed=seed
|
|
834
|
+
)
|
|
835
|
+
self.start_value = start_value
|
|
836
|
+
self.kappa = kappa
|
|
837
|
+
self.mu = mu
|
|
838
|
+
|
|
839
|
+
def transform(self, matrix: pd.DataFrame | np.ndarray = None) -> pd.Series | pd.DataFrame:
|
|
840
|
+
"""
|
|
841
|
+
Generate synthetic mean-reverting data using the Ornstein-Uhlenbeck
|
|
842
|
+
model.
|
|
843
|
+
|
|
844
|
+
Parameters
|
|
845
|
+
----------
|
|
846
|
+
matrix : pd.DataFrame or np.array, optional
|
|
847
|
+
The matrix applied in Cholesky decomposition (optional).
|
|
848
|
+
|
|
849
|
+
Returns
|
|
850
|
+
-------
|
|
851
|
+
pd.Series | pd.DataFrame
|
|
852
|
+
Data containing synthetic mean-reverting data.
|
|
853
|
+
"""
|
|
854
|
+
paths = np.full(
|
|
855
|
+
(self.length+1, self.num_paths), self.start_value, dtype=np.float64
|
|
856
|
+
)
|
|
857
|
+
|
|
858
|
+
noise = (
|
|
859
|
+
self.cholesky_transform(self.white_noise, matrix)
|
|
860
|
+
if matrix is not None
|
|
861
|
+
else self.white_noise
|
|
862
|
+
)
|
|
863
|
+
|
|
864
|
+
for length in range(1, self.length + 1):
|
|
865
|
+
drift = (
|
|
866
|
+
self.kappa * (self.mu - paths[length-1]) * self.delta
|
|
867
|
+
)
|
|
868
|
+
deviation = noise[length - 1]
|
|
869
|
+
|
|
870
|
+
paths[length] = paths[length-1] + drift + deviation
|
|
871
|
+
|
|
872
|
+
return self.to_pandas(paths[1:,])
|
|
873
|
+
|
|
874
|
+
|
|
875
|
+
class AR(BaseSynthetic):
|
|
876
|
+
"""
|
|
877
|
+
A class for generating synthetic autoregressive (AR) model.
|
|
878
|
+
|
|
879
|
+
Generates time series with an autogressive lag defined by the number of
|
|
880
|
+
parameters.
|
|
881
|
+
|
|
882
|
+
Parameters:
|
|
883
|
+
----------
|
|
884
|
+
length : int, optional
|
|
885
|
+
The length of the time series. Default is 252.
|
|
886
|
+
num_paths : int, optional
|
|
887
|
+
The number of paths to generate. Default is 1.
|
|
888
|
+
mean : float, optional
|
|
889
|
+
Mean (“centre”) of the distribution. Defaults to 0.
|
|
890
|
+
delta : float, optional
|
|
891
|
+
The time step size (e.g., 1/252 for daily data). Default is 1/252.
|
|
892
|
+
sigma : float, optional
|
|
893
|
+
The volatility of the stochastic processes. Default is 0.125.
|
|
894
|
+
start_value : float, optional
|
|
895
|
+
The starting value. Default is 0.5.
|
|
896
|
+
ar : list of float, optional
|
|
897
|
+
Coefficients for the AR model. The number of coefficients defines the
|
|
898
|
+
order. E.g., [0.8, -0.2] for an AR(2) process. IF None, it defaults to
|
|
899
|
+
[0.8]. Dafaults to None.
|
|
900
|
+
freq : str, optional
|
|
901
|
+
The frequency of the data. Default is 'D'.
|
|
902
|
+
seed : int, optional
|
|
903
|
+
The random seed for reproducibility. Default is None.
|
|
904
|
+
|
|
905
|
+
"""
|
|
906
|
+
|
|
907
|
+
def __init__(
|
|
908
|
+
self,
|
|
909
|
+
length: int | pd.DatetimeIndex = 252,
|
|
910
|
+
num_paths: int = 1,
|
|
911
|
+
mean: float = 0,
|
|
912
|
+
delta: float = 1/252,
|
|
913
|
+
sigma: float = 0.125,
|
|
914
|
+
ar=None,
|
|
915
|
+
freq: str = 'D',
|
|
916
|
+
seed: int = None
|
|
917
|
+
):
|
|
918
|
+
super().__init__(
|
|
919
|
+
length=length,
|
|
920
|
+
num_paths=num_paths,
|
|
921
|
+
mean=mean,
|
|
922
|
+
delta=delta,
|
|
923
|
+
sigma=sigma,
|
|
924
|
+
freq=freq,
|
|
925
|
+
seed=seed
|
|
926
|
+
)
|
|
927
|
+
self.ar = ar or [0.8]
|
|
928
|
+
self.order = len(self.ar)
|
|
929
|
+
|
|
930
|
+
def transform(self, matrix: pd.DataFrame | np.ndarray = None) -> pd.Series | pd.DataFrame:
|
|
931
|
+
"""
|
|
932
|
+
Generate synthetic autoregressive (AR) data.
|
|
933
|
+
|
|
934
|
+
Parameters
|
|
935
|
+
----------
|
|
936
|
+
matrix : pd.DataFrame or np.array, optional
|
|
937
|
+
The matrix applied in Cholesky decomposition (optional).
|
|
938
|
+
|
|
939
|
+
Returns
|
|
940
|
+
-------
|
|
941
|
+
pd.Series | pd.DataFrame
|
|
942
|
+
Data containing synthetic autoregressive (AR) data.
|
|
943
|
+
|
|
944
|
+
"""
|
|
945
|
+
paths = np.zeros((self.length+1, self.num_paths), dtype=np.float64)
|
|
946
|
+
|
|
947
|
+
noise = (
|
|
948
|
+
self.cholesky_transform(self.white_noise, matrix)
|
|
949
|
+
if matrix is not None
|
|
950
|
+
else self.white_noise
|
|
951
|
+
)
|
|
952
|
+
|
|
953
|
+
for length in range(1, self.length + 1):
|
|
954
|
+
deviation = noise[length - 1]
|
|
955
|
+
paths[length] = (
|
|
956
|
+
sum([self.ar[j] * paths[length - j - 1] for j in range(self.order)]) + deviation
|
|
957
|
+
)
|
|
958
|
+
|
|
959
|
+
return self.to_pandas(paths[1:,])
|
|
960
|
+
|
|
961
|
+
|
|
962
|
+
class NARMA(BaseSynthetic):
|
|
963
|
+
"""
|
|
964
|
+
A class for generating synthetic non-linear Autoregressive Moving Average
|
|
965
|
+
model.
|
|
966
|
+
|
|
967
|
+
An n-th order signal generator of the NARMA class, as defined in the
|
|
968
|
+
Harvard University official documentation[^1]:
|
|
969
|
+
|
|
970
|
+
$$
|
|
971
|
+
y(k+1) = a_0 y(k) + a_1 y(k) \sum_{i=0}^{n-1} y(k-i) + a_2 u(k-(n-1)) u(k) + a_3
|
|
972
|
+
$$
|
|
973
|
+
|
|
974
|
+
where $u$ is generated from $U(0, 0.5)$.
|
|
975
|
+
|
|
976
|
+
[^1]: Harvard University [official documentation](http://ieeexplore.ieee.org.ezp-prod1.hul.harvard.edu/stamp/stamp.jsp?arnumber=846741)
|
|
977
|
+
|
|
978
|
+
|
|
979
|
+
Parameters
|
|
980
|
+
----------
|
|
981
|
+
length : int, optional
|
|
982
|
+
The length of the time series. Default is 252.
|
|
983
|
+
num_paths : int, optional
|
|
984
|
+
The number of paths to generate. Default is 1.
|
|
985
|
+
start_value : float, optional
|
|
986
|
+
The starting value. Default is 0.5.
|
|
987
|
+
n : int, optional
|
|
988
|
+
The order of the NARMA process. Default is 10.
|
|
989
|
+
a : list, optional
|
|
990
|
+
Coefficients for the NARMA model. When None, we use reference
|
|
991
|
+
coefficients [0.3, 0.05, 1.5, 0.1]. Default is None.
|
|
992
|
+
freq : str, optional
|
|
993
|
+
The frequency of the data. Default is 'D'.
|
|
994
|
+
seed : int, optional
|
|
995
|
+
The random seed for reproducibility. Default is None.
|
|
996
|
+
"""
|
|
997
|
+
|
|
998
|
+
def __init__(
|
|
999
|
+
self,
|
|
1000
|
+
length: int | pd.DatetimeIndex = 252,
|
|
1001
|
+
num_paths: int = 1,
|
|
1002
|
+
start_value: float = 0.5,
|
|
1003
|
+
n: int = 10,
|
|
1004
|
+
a: list | None = None,
|
|
1005
|
+
freq: str = 'D',
|
|
1006
|
+
seed: int = None
|
|
1007
|
+
):
|
|
1008
|
+
super().__init__(
|
|
1009
|
+
length=length,
|
|
1010
|
+
num_paths=num_paths,
|
|
1011
|
+
freq=freq,
|
|
1012
|
+
seed=seed
|
|
1013
|
+
)
|
|
1014
|
+
self.n = n
|
|
1015
|
+
self.a = [0.3, 0.05, 1.5, 0.1] or a
|
|
1016
|
+
self.start_value = start_value
|
|
1017
|
+
|
|
1018
|
+
if len(self.a) != 4:
|
|
1019
|
+
raise ValueError(
|
|
1020
|
+
"We need 4 Coefficients for the NARMA model. Number of coeff is "
|
|
1021
|
+
f"{len(self.a)}."
|
|
1022
|
+
)
|
|
1023
|
+
|
|
1024
|
+
def transform(self, matrix: pd.DataFrame | np.ndarray = None) -> pd.Series | pd.DataFrame:
|
|
1025
|
+
"""
|
|
1026
|
+
Generate synthetic Non-linear Autoregressive Moving Average (NARMA)
|
|
1027
|
+
data.
|
|
1028
|
+
|
|
1029
|
+
Parameters
|
|
1030
|
+
----------
|
|
1031
|
+
matrix : pd.DataFrame or np.array, optional
|
|
1032
|
+
The matrix applied in Cholesky decomposition (optional).
|
|
1033
|
+
|
|
1034
|
+
Returns
|
|
1035
|
+
-------
|
|
1036
|
+
pd.Series | pd.DataFrame
|
|
1037
|
+
Data containing non-linear Autoregressive Moving Average (NARMA) data.
|
|
1038
|
+
"""
|
|
1039
|
+
paths = np.full(
|
|
1040
|
+
(self.length+1, self.num_paths),
|
|
1041
|
+
self.start_value,
|
|
1042
|
+
dtype=np.float64
|
|
1043
|
+
)
|
|
1044
|
+
# Uniform data
|
|
1045
|
+
u = np.random.uniform(
|
|
1046
|
+
low=0,
|
|
1047
|
+
high=0.5,
|
|
1048
|
+
size=(self.length + 1, self.num_paths)
|
|
1049
|
+
)
|
|
1050
|
+
|
|
1051
|
+
for length in range(1, self.length + 1):
|
|
1052
|
+
paths[length] = (
|
|
1053
|
+
self.a[0] * paths[length - 1] +
|
|
1054
|
+
self.a[1] * paths[length - 1] * sum(paths[length - self.n:length]) +
|
|
1055
|
+
self.a[2] * u[length - self.n] * u[length] +
|
|
1056
|
+
self.a[3]
|
|
1057
|
+
)
|
|
1058
|
+
|
|
1059
|
+
if matrix is not None:
|
|
1060
|
+
paths = self.cholesky_transform(paths, matrix)
|
|
1061
|
+
|
|
1062
|
+
return self.to_pandas(paths[1:,])
|
|
1063
|
+
|
|
1064
|
+
|
|
1065
|
+
class Seasonal(BaseSynthetic):
|
|
1066
|
+
"""
|
|
1067
|
+
A class for generating seasonal patterns model. The seasonal pattern is
|
|
1068
|
+
generated using the sinusoidal function
|
|
1069
|
+
|
|
1070
|
+
Note
|
|
1071
|
+
----
|
|
1072
|
+
The data generation process compute: a sin(2π ft + p).
|
|
1073
|
+
where t is a time vector from 0 to length - 1, a is the random amplitude
|
|
1074
|
+
values with a normal distribution (mean=1, std=0.1), f is the random
|
|
1075
|
+
frequency values with a normal distribution (mean=0.1, std=0.01). We then
|
|
1076
|
+
adjust the mean and std based on the desired seasonality. Finally, p is the
|
|
1077
|
+
random phase shifts uniformly distributed between 0 and 2π.
|
|
1078
|
+
|
|
1079
|
+
Parameters
|
|
1080
|
+
----------
|
|
1081
|
+
length : int, optional
|
|
1082
|
+
The length of the time series. Default is 252.
|
|
1083
|
+
num_paths : int, optional
|
|
1084
|
+
The number of paths to generate. Default is 1.
|
|
1085
|
+
mean : float, optional
|
|
1086
|
+
Mean (“centre”) of the distribution. Defaults to 0.
|
|
1087
|
+
delta : float, optional
|
|
1088
|
+
The time step size (e.g., 1/252 for daily data). Default is 1/252.
|
|
1089
|
+
sigma : float, optional
|
|
1090
|
+
The volatility of the amplitude process. Default is 0.125.
|
|
1091
|
+
omega : float, optional
|
|
1092
|
+
How often the wave oscillates or repeats its cycle per unit time. It
|
|
1093
|
+
represents the rate of change of the phase of a sinusoidal waveform.
|
|
1094
|
+
Defaults to 1.0.
|
|
1095
|
+
phi : float, optional
|
|
1096
|
+
Represents the phase angle, the duration of one cycle of the wave or
|
|
1097
|
+
the variation of the frequency (omega)
|
|
1098
|
+
freq : str, optional
|
|
1099
|
+
The frequency of the data. Default is 'D'.
|
|
1100
|
+
seed : int, optional
|
|
1101
|
+
The random seed for reproducibility. Default is None.
|
|
1102
|
+
"""
|
|
1103
|
+
|
|
1104
|
+
def __init__(
|
|
1105
|
+
self,
|
|
1106
|
+
length: int | pd.DatetimeIndex = 252,
|
|
1107
|
+
num_paths: int = 1,
|
|
1108
|
+
mean: float = 0,
|
|
1109
|
+
delta: float = 1/252,
|
|
1110
|
+
sigma: float = 0.125,
|
|
1111
|
+
omega: float = 1.0,
|
|
1112
|
+
phi: float = 0.4,
|
|
1113
|
+
freq: str = 'D',
|
|
1114
|
+
seed: int = None
|
|
1115
|
+
):
|
|
1116
|
+
super().__init__(
|
|
1117
|
+
length=length,
|
|
1118
|
+
num_paths=num_paths,
|
|
1119
|
+
mean=mean,
|
|
1120
|
+
delta=delta,
|
|
1121
|
+
sigma=sigma,
|
|
1122
|
+
freq=freq,
|
|
1123
|
+
seed=seed
|
|
1124
|
+
)
|
|
1125
|
+
self.omega = omega
|
|
1126
|
+
self.phi = phi
|
|
1127
|
+
|
|
1128
|
+
def transform(self, matrix: pd.DataFrame | np.ndarray = None) -> pd.Series | pd.DataFrame:
|
|
1129
|
+
"""
|
|
1130
|
+
Generate synthetic seasonal patterns data.
|
|
1131
|
+
|
|
1132
|
+
Parameters
|
|
1133
|
+
----------
|
|
1134
|
+
matrix : pd.DataFrame or np.array, optional
|
|
1135
|
+
The matrix applied in Cholesky decomposition (optional).
|
|
1136
|
+
|
|
1137
|
+
Returns
|
|
1138
|
+
-------
|
|
1139
|
+
pd.Series | pd.DataFrame
|
|
1140
|
+
Data containing seasonal patterns.
|
|
1141
|
+
|
|
1142
|
+
"""
|
|
1143
|
+
# A time vector from 0 to length - 1.
|
|
1144
|
+
t = np.arange(self.length).reshape(-1, 1)
|
|
1145
|
+
|
|
1146
|
+
# Random amplitude values
|
|
1147
|
+
amplitude = (
|
|
1148
|
+
self.cholesky_transform(self.white_noise, matrix)
|
|
1149
|
+
if matrix is not None
|
|
1150
|
+
else self.white_noise
|
|
1151
|
+
)
|
|
1152
|
+
|
|
1153
|
+
# Random frequency values
|
|
1154
|
+
frequency = np.random.normal(
|
|
1155
|
+
loc=self.omega,
|
|
1156
|
+
scale=self.phi,
|
|
1157
|
+
size=(self.length, self.num_paths)
|
|
1158
|
+
)
|
|
1159
|
+
|
|
1160
|
+
# Random phase shifts uniformly distributed between 0 and 2pi.
|
|
1161
|
+
phase = np.random.uniform(
|
|
1162
|
+
low=0,
|
|
1163
|
+
high=2*np.pi,
|
|
1164
|
+
size=(self.length, self.num_paths)
|
|
1165
|
+
)
|
|
1166
|
+
|
|
1167
|
+
# Generate the seasonal patterns
|
|
1168
|
+
paths = amplitude * np.sin(2 * np.pi * frequency * t + phase)
|
|
1169
|
+
|
|
1170
|
+
return self.to_pandas(paths)
|