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. .. code:: python import warnings; warnings.simplefilter('ignore') .. code:: python from dx import * .. code:: python np.set_printoptions(precision=3) Helper Functions ---------------- There are two helper functions used regulary: - get\_year\_deltas: get a list of year deltas (decimal fractions) relative to first value in time\_list - sn\_random\_numbers: 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. .. code:: python time_list = [dt.datetime(2015, 1, 1), dt.datetime(2015, 4, 1), dt.datetime(2015, 6, 15), dt.datetime(2015, 10, 21)] 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. .. code:: python get_year_deltas(time_list) .. parsed-literal:: array([ 0. , 0.247, 0.452, 0.803]) 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. .. code:: python ran = sn_random_numbers((2, 3, 4), antithetic=True, moment_matching=True, fixed_seed=False) .. code:: python ran .. parsed-literal:: array([[[-0.51 , 0.492, 0.51 , -0.492], [-1.184, 0.455, 1.184, -0.455], [-1.335, 0.757, 1.335, -0.757]], [[-0.601, -0.079, 0.601, 0.079], [-0.208, -0.499, 0.208, 0.499], [-2.612, -0.227, 2.612, 0.227]]]) Using moment matching makes sure that the first and second moments match exactly 0 and 1, respectively. .. code:: python ran.mean() .. parsed-literal:: 0.0 .. code:: python ran.std() .. parsed-literal:: 1.0 Setting the first value of the ``shape`` parameter to 1 yields a two-dimensional ``ndarray`` object. .. code:: python ran = sn_random_numbers((1, 3, 4), antithetic=True, moment_matching=True, fixed_seed=False) .. code:: python ran .. parsed-literal:: array([[ 1.562, -0.462, -1.562, 0.462], [ 0.382, -0.293, -0.382, 0.293], [-0.305, 1.739, 0.305, -1.739]]) 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. .. code:: python r = constant_short_rate('r', 0.05) .. code:: python r.name .. parsed-literal:: 'r' .. code:: python r.short_rate .. parsed-literal:: 0.05 The object has a method ``get_forward_rates`` to generate forward rates given, for instance, a ``list`` object of ``datetime`` objects. .. code:: python r.get_forward_rates(time_list) .. parsed-literal:: ([datetime.datetime(2015, 1, 1, 0, 0), datetime.datetime(2015, 4, 1, 0, 0), datetime.datetime(2015, 6, 15, 0, 0), datetime.datetime(2015, 10, 21, 0, 0)], array([ 0.05, 0.05, 0.05, 0.05])) Similarly, the method ``get_discount_factors`` returns discount factors for such a ``list`` object. .. code:: python r.get_discount_factors(time_list) .. parsed-literal:: ([datetime.datetime(2015, 1, 1, 0, 0), datetime.datetime(2015, 4, 1, 0, 0), datetime.datetime(2015, 6, 15, 0, 0), datetime.datetime(2015, 10, 21, 0, 0)], array([ 0.961, 0.978, 0.988, 1. ])) You can also pass, for instance, an ``ndarry`` object containing year fractions. .. code:: python r.get_discount_factors(np.array([0., 1., 1.5, 2.]), dtobjects=False) .. parsed-literal:: (array([ 0. , 1. , 1.5, 2. ]), array([ 0.905, 0.928, 0.951, 1. ])) 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. .. code:: python 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)] .. code:: python y = deterministic_short_rate('y', yields) .. code:: python y.name .. parsed-literal:: 'y' .. code:: python y.yield_list .. parsed-literal:: array([[datetime.datetime(2015, 1, 1, 0, 0), 0.02], [datetime.datetime(2015, 3, 1, 0, 0), 0.03], [datetime.datetime(2015, 10, 15, 0, 0), 0.035], [datetime.datetime(2015, 12, 31, 0, 0), 0.04]], dtype=object) The method ``get_interpolated_yields`` implements an interpolation of the yield data and returns the interpolated yields given a ``list`` object of ``datetime`` objects. .. code:: python y.get_interpolated_yields(time_list) .. parsed-literal:: array([[datetime.datetime(2015, 1, 1, 0, 0), 0.019999999999999993, 0.0840608597791612], [datetime.datetime(2015, 4, 1, 0, 0), 0.03283048934520345, 0.02532998361805569], [datetime.datetime(2015, 6, 15, 0, 0), 0.035133049718591186, 0.0007769642303797064], [datetime.datetime(2015, 10, 21, 0, 0), 0.03515012570984609, 0.010083939037494674]], dtype=object) In similar fashion, the methods ``get_forward_rates`` and ``get_discount_factors`` return forward rates and discount factors, respcectively. .. code:: python y.get_forward_rates(time_list) .. parsed-literal:: ([datetime.datetime(2015, 1, 1, 0, 0), datetime.datetime(2015, 4, 1, 0, 0), datetime.datetime(2015, 6, 15, 0, 0), datetime.datetime(2015, 10, 21, 0, 0)], array([0.019999999999999993, 0.039076238730477456, 0.0354842801241053, 0.04324490417008154], dtype=object)) .. code:: python y.get_discount_factors(time_list) .. parsed-literal:: ([datetime.datetime(2015, 1, 1, 0, 0), datetime.datetime(2015, 4, 1, 0, 0), datetime.datetime(2015, 6, 15, 0, 0), datetime.datetime(2015, 10, 21, 0, 0)], [0.97166103139227611, 0.97876383482361962, 0.98629027682763593, 1.0]) 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. .. code:: python me = market_environment(name='me', pricing_date=dt.datetime(2014, 1, 1)) Constants are added via the ``add_constant`` method and providing a key and the value. .. code:: python me.add_constant('initial_value', 100.) .. code:: python me.add_constant('volatility', 0.25) Lists of data are added via the ``add_list`` method. .. code:: python me.add_list('dates', time_list) The ``add_curve`` method does the same for curves. .. code:: python me.add_curve('discount_curve_1', r) .. code:: python me.add_curve('discount_curve_2', y) The single data objects are stored in separate ``dictionary`` objects. .. code:: python me.constants .. parsed-literal:: {'initial_value': 100.0, 'volatility': 0.25} .. code:: python me.lists .. parsed-literal:: {'dates': [datetime.datetime(2015, 1, 1, 0, 0), datetime.datetime(2015, 4, 1, 0, 0), datetime.datetime(2015, 6, 15, 0, 0), datetime.datetime(2015, 10, 21, 0, 0)]} .. code:: python me.curves .. parsed-literal:: {'discount_curve_1': , 'discount_curve_2': } Data is retrieved from a ``market_environment`` object via the ``get_constant``, ``get_list`` and ``get_curve`` methods and providing the respective key. .. code:: python me.get_constant('volatility') .. parsed-literal:: 0.25 .. code:: python me.get_list('dates') .. parsed-literal:: [datetime.datetime(2015, 1, 1, 0, 0), datetime.datetime(2015, 4, 1, 0, 0), datetime.datetime(2015, 6, 15, 0, 0), datetime.datetime(2015, 10, 21, 0, 0)] .. code:: python me.get_curve('discount_curve_1') .. parsed-literal:: Retrieving, for instance, a discounting object you can in one step retrieve it and call a method on it. .. code:: python me.get_curve('discount_curve_2').get_discount_factors(time_list) .. parsed-literal:: ([datetime.datetime(2015, 1, 1, 0, 0), datetime.datetime(2015, 4, 1, 0, 0), datetime.datetime(2015, 6, 15, 0, 0), datetime.datetime(2015, 10, 21, 0, 0)], [0.97166103139227611, 0.97876383482361962, 0.98629027682763593, 1.0]) **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