def test_FBM_fgn_adjust_rnd(n_good, hurst_good, length_good, fbm_method_good, adjust_rnd_good): f = FBM(n_good, hurst_good, length_good, fbm_method_good) fgn_sample = f.fgn() fgn_sample = f.fgn(adjust_rnd_good) assert isinstance(fgn_sample, np.ndarray) assert len(fgn_sample) == n_good
class FBM2D: def __init__(self, H, N, L): """ init of FBM2D parameters: H : Hurst index with 0 < H < 1 N : number of observations along both dimensions, constructed field will contain (N x N) points L : number of one dimensional fbm used in construction of 2D Fields """ if H <= 0 or H >= 1: raise ValueError("Hurst parameter must be in interval (0, 1).") self.H = H self.N = N self.L = L self.fbb_gen = FBM( n=int(1.5 * self.N), hurst=self.H, length=1.5) #init generator for fractional Brownian motion def fbs(self): """ returnes fractional Brownian surface (scalar fractional Brownian motion) in 2D """ #generate self.L bands with equidistant angles theta between band and x-axis theta_i = np.linspace(0, np.pi, self.L + 1)[:-1] #angles oneDfbm = np.zeros((self.L, int(1.5 * self.N) + 1)) for i in range(self.L): fGn = self.fbb_gen.fgn() oneDfbm[i, 1:] = np.sqrt( np.sqrt(np.pi) * math.gamma(1 + self.H) / math.gamma(1 / 2 + self.H)) * np.cumsum(fGn) # perform turbing band algortihm in fortran return f.turningband2d(oneDfbm, theta_i, self.N)
class FractionalBlackScholes(BlackScholes): def __init__(self, drift, volatility, hurst, nb_paths, nb_stocks, nb_dates, spot, maturity, dividend=0, **keywords): super(FractionalBlackScholes, self).__init__(drift, volatility, nb_paths, nb_stocks, nb_dates, spot, maturity, dividend, **keywords) self.drift = drift self.hurst = hurst self.fBM = FBM(n=nb_dates, hurst=self.hurst, length=maturity, method='hosking') def generate_one_path(self): """Returns a nparray (nb_stocks * nb_dates) with prices.""" path = np.empty((self.nb_stocks, self.nb_dates + 1)) fracBM_noise = np.empty((self.nb_stocks, self.nb_dates)) path[:, 0] = self.spot for stock in range(self.nb_stocks): fracBM_noise[stock, :] = self.fBM.fgn() for k in range(1, self.nb_dates + 1): previous_spots = path[:, k - 1] diffusion = self.diffusion_fct(previous_spots, (k) * self.dt) path[:, k] = (previous_spots + self.drift_fct(previous_spots, (k) * self.dt) * self.dt + np.multiply(diffusion, fracBM_noise[:, k - 1])) print("path", path) return path
from fbm import FBM import matplotlib.pyplot as plt f = FBM(n=100, hurst=0.9, length=1, method='daviesharte') # Generate a fBm realization fbm_sample = f.fbm() # Generate a fGn realization fgn_sample = f.fgn() # Get the times associated with the fBm t_values = f.times() plt.plot(t_values, fbm_sample) plt.show()
# flake8: noqa from fbm import FBM import matplotlib.pyplot as plt import time import math import numpy as np def h(s): # return 0.499*math.sin(t) + 0.5 # return 0.6 * t + 0.3 return 0.5 * np.exp(-8.0 * s**2) fbm_generator = FBM(2**8, 0.85) t = fbm_generator.times() fbm_realization = fbm_generator.fbm() fgn_realization = fbm_generator.fgn() h_t = np.array(h(fbm_realization)) plt.plot(t, fbm_realization) plt.plot(t, h_t) # plt.plot(t[0:-1], fgn_realization) plt.show()