This brief section illustrates the use of stochastic short rate models for simulation and (risk-neutral) discounting. The class used is called stochastic_short_rate
.
First, the market environment. As a stochastic short rate model the square_root_diffusion
class is (currently) available. We therefore need to define the respective parameters for this class in the market environment.
In [1]:
from dx import *
In [2]:
me = market_environment(name='me', pricing_date=dt.datetime(2015, 1, 1))
me.add_constant('initial_value', 0.01)
me.add_constant('volatility', 0.1)
me.add_constant('kappa', 2.0)
me.add_constant('theta', 0.05)
me.add_constant('paths', 1000)
me.add_constant('frequency', 'M')
me.add_constant('starting_date', me.pricing_date)
me.add_constant('final_date', dt.datetime(2015, 12, 31))
me.add_curve('discount_curve', 0.0) # dummy
me.add_constant('currency', 0.0) # dummy
Second, the instantiation of the class.
In [3]:
ssr = stochastic_short_rate('sr', me)
The following is an example list
object containing datetime
objects.
In [4]:
time_list = [dt.datetime(2015, 1, 1),
dt.datetime(2015, 4, 1),
dt.datetime(2015, 6, 15),
dt.datetime(2015, 10, 21)]
The call of the method get_forward_reates()
yields the above time_list
object and the simulated forward rates. In this case, 10 simulations.
In [5]:
ssr.get_forward_rates(time_list, 10)
Out[5]:
Accordingly, the call of the get_discount_factors()
method yields simulated zero-coupon bond prices for the time grid.
In [6]:
ssr.get_discount_factors(time_list, 10)
Out[6]:
Let us value use the stochastic short rate model to simulate a geometric Brownian motion with stochastic short rate. Define the market environment as follows:
In [7]:
me.add_constant('initial_value', 36.)
me.add_constant('volatility', 0.2)
# time horizon for the simulation
me.add_constant('currency', 'EUR')
me.add_constant('frequency', 'M')
# monthly frequency; paramter accorind to pandas convention
me.add_constant('paths', 10)
# number of paths for simulation
Then add the stochastic_short_rate
object as discount curve.
In [8]:
me.add_curve('discount_curve', ssr)
Finally, instantiate the geometric_brownian_motion
object.
In [9]:
gbm = geometric_brownian_motion('gbm', me)
We get simulated instrument values as usual via the get_instrument_values()
method.
In [10]:
gbm.get_instrument_values()
Out[10]:
In [11]:
import matplotlib.pyplot as plt
import seaborn as sns; sns.set()
%matplotlib inline
In [12]:
# short rate paths
plt.figure(figsize=(10, 6))
plt.plot(ssr.process.instrument_values[:, :10]);
Copyright, License & Disclaimer
© Dr. Yves J. Hilpisch | The Python Quants GmbH
DX Analytics (the "dx library") is licensed under the GNU Affero General Public License version 3 or later (see http://www.gnu.org/licenses/).
DX Analytics comes with no representations or warranties, to the extent permitted by applicable law.
http://tpq.io | team@tpq.io | http://twitter.com/dyjh
Quant Platform | http://quant-platform.com
Derivatives Analytics with Python (Wiley Finance) | http://derivatives-analytics-with-python.com
Python for Finance (O'Reilly) | http://python-for-finance.com