crflynn / fbm

Exact methods for simulating fractional Brownian motion and fractional Gaussian noise in python
MIT License
96 stars 29 forks source link

add manual adjustment of fgn #5

Closed OSliusarenko closed 5 years ago

OSliusarenko commented 5 years ago

When simulating fBm sometimes it is necessary to adjust one position, but maintaining the correct correlations and distribution before it and after it. After one generates f.fgn() now it is possible to adjust the random R at time T with a subsequent command f.fgn(adjust_rnd=[T, R]), which using the same set of gaussian randoms will adjust the fG random at the specified position and re-correlate all the following fG randoms, thus preserving the beginning part precisely as it was. This will work only with Hosking method since it is iterative and does not use FFT.

crflynn commented 5 years ago

You can pass your own gaussian noise increments directly to the _hosking method to return a fGn realization, and subsequently take the cumulative sum for a fBm sample. I would say this is preferable to adding additional checking logic on every incremental iteration in the generation method.

import numpy as np
from fbm.fbm import FBM

n = 32

# Using your notation
i_a = 3
F = 1

# Generate some gaussian noise and create a FBM instance using the same n
gn = np.random.normal(0.0, 1.0, n)
f = FBM(n, 0.7)

# Generate a fBm sample by taking the cumulative sum of fGn
s = np.insert(f._hosking(gn).cumsum(), [0], 0)
print(s)

# Alter one of the incremental values
gn[i_a] = F

# Generate a second sample with the altered Gaussian increments
s2 = np.insert(f._hosking(gn).cumsum(), [0], 0)
print(s2)
OSliusarenko commented 5 years ago

Thank you for the response! Yes, for the readability of the code you are right. For my applications, however, these insertions could be up to several thousands of times for a single trajectory of 10^5 points,in parallel and a lot of such trajectories, so I'll prefer to hide these lines. Moreover, if not to use private FBM class methods, the wrapper function will do almost the same functionality as my code, except for checking each step.