Repository navigation
Expand file tree
/
Copy pathsquare_root_diffusion.py
More file actions
83 lines (73 loc) · 2.77 KB
/
Copy pathsquare_root_diffusion.py
File metadata and controls
83 lines (73 loc) · 2.77 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
#
# DX Library Simulation
# square_root_diffusion.py
#
import numpy as np
from sn_random_numbers import sn_random_numbers
from simulation_class import simulation_class
class square_root_diffusion(simulation_class):
''' Class to generate simulated paths based on
the Cox-Ingersoll-Ross (1985) square-root diffusion model.
Attributes
==========
name : string
name of the object
mar_env : instance of market_environment
market environment data for simulation
corr : Boolean
True if correlated with other model object
Methods
=======
update :
updates parameters
generate_paths :
returns Monte Carlo paths given the market environment
'''
def __init__(self, name, mar_env, corr=False):
super(square_root_diffusion, self).__init__(name, mar_env, corr)
try:
self.kappa = mar_env.get_constant('kappa')
self.theta = mar_env.get_constant('theta')
except:
print("Error parsing market environment.")
def update(self, initial_value=None, volatility=None, kappa=None,
theta=None, final_date=None):
if initial_value is not None:
self.initial_value = initial_value
if volatility is not None:
self.volatility = volatility
if kappa is not None:
self.kappa = kappa
if theta is not None:
self.theta = theta
if final_date is not None:
self.final_date = final_date
self.instrument_values = None
def generate_paths(self, fixed_seed=True, day_count=365.):
if self.time_grid is None:
self.generate_time_grid()
M = len(self.time_grid)
I = self.paths
paths = np.zeros((M, I))
paths_ = np.zeros_like(paths)
paths[0] = self.initial_value
paths_[0] = self.initial_value
if self.correlated is False:
rand = sn_random_numbers((1, M, I),
fixed_seed=fixed_seed)
else:
rand = self.random_numbers
for t in range(1, len(self.time_grid)):
dt = (self.time_grid[t] - self.time_grid[t - 1]).days / day_count
if self.correlated is False:
ran = rand[t]
else:
ran = np.dot(self.cholesky_matrix, rand[:, t, :])
ran = ran[self.rn_set]
# full truncation Euler discretization
paths_[t] = (paths_[t - 1] + self.kappa
* (self.theta - np.maximum(0, paths_[t - 1, :])) * dt
+ np.sqrt(np.maximum(0, paths_[t - 1, :]))
* self.volatility * np.sqrt(dt) * ran)
paths[t] = np.maximum(0, paths_[t])
self.instrument_values = paths