-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathDavies_Harte.py
49 lines (37 loc) · 1.72 KB
/
Davies_Harte.py
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
# -*- coding: utf-8 -*-
"""
Created on Fri Jul 3 15:26:14 2020
@author: Justin Yu, M.S. Financial Engineering, Stevens Institute of Technology
Implementation of Fractional Brownian Motion, Davies Harte Method
"""
import numpy as np
def davies_harte(T, N, H):
'''
Generates sample paths of fractional Brownian Motion using the Davies Harte method
args:
T: length of time (in years)
N: number of time steps within timeframe
H: Hurst parameter
'''
gamma = lambda k,H: 0.5*(np.abs(k-1)**(2*H) - 2*np.abs(k)**(2*H) + np.abs(k+1)**(2*H))
g = [gamma(k,H) for k in range(0,N)]; r = g + [0] + g[::-1][0:N-1]
# Step 1 (eigenvalues)
j = np.arange(0,2*N); k = 2*N-1
lk = np.fft.fft(r*np.exp(2*np.pi*complex(0,1)*k*j*(1/(2*N))))[::-1]
# Step 2 (get random variables)
Vj = np.zeros((2*N,2), dtype=np.complex);
Vj[0,0] = np.random.standard_normal(); Vj[N,0] = np.random.standard_normal()
for i in range(1,N):
Vj1 = np.random.standard_normal(); Vj2 = np.random.standard_normal()
Vj[i][0] = Vj1; Vj[i][1] = Vj2; Vj[2*N-i][0] = Vj1; Vj[2*N-i][1] = Vj2
# Step 3 (compute Z)
wk = np.zeros(2*N, dtype=np.complex)
wk[0] = np.sqrt((lk[0]/(2*N)))*Vj[0][0];
wk[1:N] = np.sqrt(lk[1:N]/(4*N))*((Vj[1:N].T[0]) + (complex(0,1)*Vj[1:N].T[1]))
wk[N] = np.sqrt((lk[0]/(2*N)))*Vj[N][0]
wk[N+1:2*N] = np.sqrt(lk[N+1:2*N]/(4*N))*(np.flip(Vj[1:N].T[0]) - (complex(0,1)*np.flip(Vj[1:N].T[1])))
Z = np.fft.fft(wk); fGn = Z[0:N]
fBm = np.cumsum(fGn)*(N**(-H))
fBm = (T**H)*(fBm)
path = np.array([0] + list(fBm))
return path