from dx import *

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

"""
Explanation: <img src="http://hilpisch.com/tpq_logo.png" alt="The Python Quants" width="45%" align="right" border="4">
Stochastic Short Rates
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.
The Modelling
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.
End of explanation
"""


ssr = stochastic_short_rate('sr', me)

"""
Explanation: Second, the instantiation of the class.
End of explanation
"""


time_list = [dt.datetime(2015, 1, 1),
             dt.datetime(2015, 4, 1),
             dt.datetime(2015, 6, 15),
             dt.datetime(2015, 10, 21)]

"""
Explanation: The following is an example list object containing datetime objects.
End of explanation
"""


ssr.get_forward_rates(time_list, 10)

"""
Explanation: The call of the method get_forward_reates() yields the above time_list object and the simulated forward rates. In this case, 10 simulations.
End of explanation
"""


ssr.get_discount_factors(time_list, 10)

"""
Explanation: Accordingly, the call of the get_discount_factors() method yields simulated zero-coupon bond prices for the time grid.
End of explanation
"""


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

"""
Explanation: Stochstic Drifts
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:
End of explanation
"""


me.add_curve('discount_curve', ssr)

"""
Explanation: Then add the stochastic_short_rate object as discount curve.
End of explanation
"""


gbm = geometric_brownian_motion('gbm', me)

"""
Explanation: Finally, instantiate the geometric_brownian_motion object.
End of explanation
"""


gbm.get_instrument_values()

"""
Explanation: We get simulated instrument values as usual via the get_instrument_values() method.
End of explanation
"""


import matplotlib.pyplot as plt
import seaborn as sns; sns.set()
%matplotlib inline

# short rate paths
plt.figure(figsize=(10, 6))
plt.plot(ssr.process.instrument_values[:, :10]);

"""
Explanation: Visualization of Simulated Stochastic Short Rate
End of explanation
"""


