Repository navigation
Expand file tree
/
Copy pathsimulation_class.py
More file actions
94 lines (87 loc) · 3.47 KB
/
Copy pathsimulation_class.py
File metadata and controls
94 lines (87 loc) · 3.47 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
84
85
86
87
88
89
90
91
92
93
94
#
# DX Library Simulation
# simulation_class.py
#
import numpy as np
import pandas as pd
class simulation_class(object):
''' Providing base methods for simulation classes.
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
=======
generate_time_grid :
returns time grid for simulation
get_instrument_values :
returns the current instrument values (array)
'''
def __init__(self, name, mar_env, corr):
try:
self.name = name
self.pricing_date = mar_env.pricing_date
self.initial_value = mar_env.get_constant('initial_value')
self.volatility = mar_env.get_constant('volatility')
self.final_date = mar_env.get_constant('final_date')
self.currency = mar_env.get_constant('currency')
self.frequency = mar_env.get_constant('frequency')
self.paths = mar_env.get_constant('paths')
self.discount_curve = mar_env.get_curve('discount_curve')
try:
# if time_grid in mar_env take this
# (for portfolio valuation)
self.time_grid = mar_env.get_list('time_grid')
except:
self.time_grid = None
try:
# if there are special dates, then add these
self.special_dates = mar_env.get_list('special_dates')
except:
self.special_dates = []
self.instrument_values = None
self.correlated = corr
if corr is True:
# only needed in a portfolio context when
# risk factors are correlated
self.cholesky_matrix = mar_env.get_list('cholesky_matrix')
self.rn_set = mar_env.get_list('rn_set')[self.name]
self.random_numbers = mar_env.get_list('random_numbers')
except:
print "Error parsing market environment."
def generate_time_grid(self):
start = self.pricing_date
end = self.final_date
# pandas date_range function
# freq = e.g. 'B' for Business Day,
# 'W' for Weekly, 'M' for Monthly
time_grid = pd.date_range(start=start, end=end,
freq=self.frequency).to_pydatetime()
time_grid = list(time_grid)
# enhance time_grid by start, end, and special_dates
if start not in time_grid:
time_grid.insert(0, start)
# insert start date if not in list
if end not in time_grid:
time_grid.append(end)
# insert end date if not in list
if len(self.special_dates) > 0:
# add all special dates
time_grid.extend(self.special_dates)
# delete duplicates
time_grid = list(set(time_grid))
# sort list
time_grid.sort()
self.time_grid = np.array(time_grid)
def get_instrument_values(self, fixed_seed=True):
if self.instrument_values is None:
# only initiate simulation if there are no instrument values
self.generate_paths(fixed_seed=fixed_seed, day_count=365.)
elif fixed_seed is False:
# also initiate resimulation when fixed_seed is False
self.generate_paths(fixed_seed=fixed_seed, day_count=365.)
return self.instrument_values