In [ 1 ]: from dx import *
In [ 2 ]: me_gbm = market_environment(‘me_gbm’, dt.datetime( 2015 , 1 , 1 ))
In [ 3 ]: me_gbm.add_constant(‘initial_value’, 36.)
me_gbm.add_constant(‘volatility’, 0.2)
me_gbm.add_constant(‘final_date’, dt.datetime( 2015 , 12 , 31 ))
me_gbm.add_constant(‘currency’, ‘EUR’)
me_gbm.add_constant(‘frequency’, ‘M’)
# monthly frequency (respective month end)
me_gbm.add_constant(‘paths’, 10000 )
In [ 4 ]: csr = constant_short_rate(‘csr’, 0.05)
In [ 5 ]: me_gbm.add_curve(‘discount_curve’, csr)
Second, we instantiate a model simulation object:
In [ 6 ]: from dx_simulation import *
In [ 7 ]: gbm = geometric_brownian_motion(‘gbm’, me_gbm)
Third, we can work with the object. For example, let us generate and inspect the
time_grid. You will notice that we have 13 datetime objects in the time_grid array
object (all the month ends in the relevant year, plus the pricing_date):
In [ 8 ]: gbm.generate_time_grid()
In [ 9 ]: gbm.time_grid
Out[9]: array([datetime.datetime(2015, 1, 1, 0, 0),
datetime.datetime(2015, 1, 31, 0, 0),
datetime.datetime(2015, 2, 28, 0, 0),
datetime.datetime(2015, 3, 31, 0, 0),
datetime.datetime(2015, 4, 30, 0, 0),
datetime.datetime(2015, 5, 31, 0, 0),
datetime.datetime(2015, 6, 30, 0, 0),
datetime.datetime(2015, 7, 31, 0, 0),
datetime.datetime(2015, 8, 31, 0, 0),
datetime.datetime(2015, 9, 30, 0, 0),
datetime.datetime(2015, 10, 31, 0, 0),
datetime.datetime(2015, 11, 30, 0, 0),
datetime.datetime(2015, 12, 31, 0, 0)], dtype=object)
Next, we might ask for the simulated instrument values:
In [ 10 ]: %time paths_1 = gbm.get_instrument_values()
Out[10]: CPU times: user 10.7 ms, sys: 2.91 ms, total: 13.6 ms
Wall time: 12.8 ms
In [ 11 ]: paths_1
Out[11]: array([[ 36. , 36. , 36. , ..., 36. ,
36. , 36. ],
[ 37.37221481, 38.08890977, 34.37156575, ..., 36.22258915,
35.05503522, 39.63544014],
[ 39.45866146, 42.18817025, 32.38579992, ..., 34.80319951,
33.60600939, 37.62733874],
...,
[ 40.15717404, 33.16701733, 23.32556112, ..., 37.5619937 ,
29.89282508, 30.2202427 ],
[ 42.0974104 , 36.59006321, 21.70771374, ..., 35.70950512,
30.64670854, 30.45901309],
[ 43.33170027, 37.42993532, 23.8840177 , ..., 35.92624556,
27.87720187, 28.77424561]])
Let us generate instrument values for a higher volatility as well:
In [ 12 ]: gbm.update(volatility=0.5)
In [ 13 ]: %time paths_2 = gbm.get_instrument_values()
Out[13]: CPU times: user 9.78 ms, sys: 1.36 ms, total: 11.1 ms
Wall time: 10.2 ms
The difference in the two sets of paths is illustrated in Figure 16-1:
In [ 14 ]: import matplotlib.pyplot as plt
%matplotlib inline
plt.figure(figsize=( 8 , 4 ))
p1 = plt.plot(gbm.time_grid, paths_1[:, : 10 ], ‘b’)