from dx import *

np.set_printoptions(precision=3)

"""
Explanation: <img src="http://hilpisch.com/tpq_logo.png" alt="The Python Quants" width="45%" align="right" border="4">
Framework Classes and Functions
This section explains the usage of some basic framework classes and functions of DX Analytics. Mainly some helper functions, the discounting classes and the market environment class used to store market data and other parameters/data needed to model, value and risk manage derivative instruments.
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: Helper Functions
There are two helper functions used regulary:

<code>get_year_deltas</code>: get a list of year deltas (decimal fractions) relative to first value in time_list
<code>sn_random_numbers</code>: get an array of standard normally distributed pseudo-random numbers

get_year_deltas
Suppose we have a list object containing a number of datetime objects.
End of explanation
"""


get_year_deltas(time_list)

"""
Explanation: Passing this object to the get_year_deltas functions yields a list of year fractions representing the time intervals between the dates given. This is sometimes used e.g. for discounting purposes.
End of explanation
"""


ran = sn_random_numbers((2, 3, 4), antithetic=True,
                        moment_matching=True, fixed_seed=False)

ran

"""
Explanation: sn_random_numbers
Monte Carlo simulation of course relies heavily an the use of random numbers. The function sn_random_numbers is a wrapper function around the pseudo-random number generator of the NumPy library. It implements antithetic variates and moment matching as generic variance reduction techniques. It also allows to fix the seed value for the random number generator. The shape parameter is a tuple object of three integers.
End of explanation
"""


ran.mean()

ran.std()

"""
Explanation: Using moment matching makes sure that the first and second moments match exactly 0 and 1, respectively.
End of explanation
"""


ran = sn_random_numbers((1, 3, 4), antithetic=True,
                        moment_matching=True, fixed_seed=False)

ran

"""
Explanation: Setting the first value of the shape parameter to 1 yields a two-dimensional ndarray object.
End of explanation
"""


r = constant_short_rate('r', 0.05)

r.name

r.short_rate

"""
Explanation: Discounting Classes
In the risk-neutral valuation of derivative instrumente, discounting payoffs is a major task. The following discounting classes are implemented:

constant_short_rate: fixed short rate
deterministic_yield: deterministic yiels/term structure

constant_short_rate
The constant_short_rate class represents the most simple case for risk-neutral discounting. A discounting object is defined by instatiating the class and providing a name and a decimal short rate value only.
End of explanation
"""


r.get_forward_rates(time_list)

"""
Explanation: The object has a method get_forward_rates to generate forward rates given, for instance, a list object of datetime objects.
End of explanation
"""


r.get_discount_factors(time_list)

"""
Explanation: Similarly, the method get_discount_factors returns discount factors for such a list object.
End of explanation
"""


r.get_discount_factors(np.array([0., 1., 1.5, 2.]),
                       dtobjects=False)

"""
Explanation: You can also pass, for instance, an ndarry object containing year fractions.
End of explanation
"""


yields = [(dt.datetime(2015, 1, 1), 0.02),
          (dt.datetime(2015, 3, 1), 0.03),
          (dt.datetime(2015, 10, 15), 0.035),
          (dt.datetime(2015, 12, 31), 0.04)]

y = deterministic_short_rate('y', yields)

y.name

y.yield_list

"""
Explanation: deterministic_short_rate
The deterministic_short_rate class allows to model an interest rate term structure. To this end, you need to pass a list object of datetime and yield pairs to the  class.
End of explanation
"""


y.get_interpolated_yields(time_list)

"""
Explanation: The method get_interpolated_yields implements an interpolation of the yield data and returns the interpolated yields given a list object of datetime objects.
End of explanation
"""


y.get_forward_rates(time_list)

y.get_discount_factors(time_list)

"""
Explanation: In similar fashion, the methods get_forward_rates and get_discount_factors return forward rates and discount factors, respcectively.
End of explanation
"""


me = market_environment(name='me', pricing_date=dt.datetime(2014, 1, 1))

"""
Explanation: Market Environment
The market_environment class is used to collect relevant data for the modeling, valuation and risk management of single derivatives instruments and portfolios composed of such instruments. A market_environment object stores:

constants: e.g. maturity date of option
lists: e.g. list of dates
curves: e.g. discounting objects

A market_environment object is instantiated by providing a name as a string object and the pricing date as a datetime object.
End of explanation
"""


me.add_constant('initial_value', 100.)

me.add_constant('volatility', 0.25)

"""
Explanation: Constants are added via the add_constant method and providing a key and the value.
End of explanation
"""


me.add_list('dates', time_list)

"""
Explanation: Lists of data are added via the  add_list method.
End of explanation
"""


me.add_curve('discount_curve_1', r)

me.add_curve('discount_curve_2', y)

"""
Explanation: The add_curve method does the same for curves.
End of explanation
"""


me.constants

me.lists

me.curves

"""
Explanation: The single data objects are stored in separate dictionary objects.
End of explanation
"""


me.get_constant('volatility')

me.get_list('dates')

me.get_curve('discount_curve_1')

"""
Explanation: Data is retrieved from a market_environment object via the get_constant, get_list and get_curve methods and providing the respective key.
End of explanation
"""


me.get_curve('discount_curve_2').get_discount_factors(time_list)

"""
Explanation: Retrieving, for instance, a discounting object you can in one step retrieve it and call a method on it.
End of explanation
"""


