.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_ch9\ch9_lagrange.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_ch9_ch9_lagrange.py: ==================================== 9.05 Lagrange interpolation model ==================================== We reproduce here a Lagrange interpolation model, not shown in the book. Utilitary functions can be found next to this file. Here, we only define codpy-related functions. .. GENERATED FROM PYTHON SOURCE LINES 9-11 Necessary Imports ----------------------- .. GENERATED FROM PYTHON SOURCE LINES 11-31 .. code-block:: Python import os import sys import matplotlib.pyplot as plt import numpy as np from codpy.kernel import Sampler try: CURRENT_DIR = os.path.dirname(os.path.abspath(__file__)) except NameError: CURRENT_DIR = os.getcwd() data_path = os.path.join(CURRENT_DIR, "data") PARENT_DIR = os.path.abspath(os.path.join(CURRENT_DIR, '..')) sys.path.insert(0, PARENT_DIR) import utils.ch9.mapping as maps from utils.ch9.plot_utils import display_historical_vs_generated_distribution from utils.ch9.market_data import retrieve_market_data from utils.ch9.path_generation import generate_paths .. GENERATED FROM PYTHON SOURCE LINES 32-34 Parameter definition ------------------------ .. GENERATED FROM PYTHON SOURCE LINES 34-48 .. code-block:: Python def get_cdpres_param(): return { 'rescale_kernel':{'max': 2000, 'seed':None}, 'rescale': True, 'grid_projection': True, 'reproductibility' : False, 'date_format' : '%d/%m/%Y', 'begin_date':'01/06/2020', 'end_date':'01/06/2022', 'today_date':'01/06/2022', 'symbols' : ['AAPL','GOOGL','AMZN'], } .. GENERATED FROM PYTHON SOURCE LINES 49-51 Get the market data ------------------------ .. GENERATED FROM PYTHON SOURCE LINES 51-53 .. code-block:: Python params = retrieve_market_data() .. GENERATED FROM PYTHON SOURCE LINES 54-60 Defining the map ------------------------ The Lagrange interpolation mapping is defined as: $$L^{(2p)}(X)=(X^{-p+k},\ldots,X^{+p+k}) = \sum_{i=-p}^{p} \beta^i_{t^{k*}} X^{k-i},\quad k=p,\ldots,T_X-p$$ Where $t^{k*} = \frac{t^{k}+t^{k+1}}{2}$, and the coefficients $\beta^i_{t^{k*}}$ are retrieved as a $p$ Lagrange interpolation in time. We show an example of resampling of our historical dataset using this Lagrange interpolation with $p=10$. .. GENERATED FROM PYTHON SOURCE LINES 60-66 .. code-block:: Python p = 10 params['q'] = p params['map'] = maps.composition_map([maps.diff(params),maps.q_interpolate(params),maps.log_map,maps.remove_time()]) params = maps.apply_map(params) .. GENERATED FROM PYTHON SOURCE LINES 67-71 We define our sampler on the mapped data using codpy's Sampler ------------------------------------------------------------------------------------------------ You can define your own latent generator function, here we use a simple uniform distribution. But if not provided, a default one will be used by the Sampler class. .. GENERATED FROM PYTHON SOURCE LINES 71-76 .. code-block:: Python mapped_data = params['transform_h'].values generator = lambda n: np.random.uniform(size=(n, mapped_data.shape[1])) sampler = Sampler(mapped_data, latent_generator=generator) params['sampler'] = sampler .. GENERATED FROM PYTHON SOURCE LINES 77-79 We plot the original distribution vs the generated one ------------------------------------------------------------------------ .. GENERATED FROM PYTHON SOURCE LINES 79-83 .. code-block:: Python params = display_historical_vs_generated_distribution(params) params['graphic'](params) plt.show() .. image-sg:: /auto_ch9/images/sphx_glr_ch9_lagrange_001.png :alt: ch9 lagrange :srcset: /auto_ch9/images/sphx_glr_ch9_lagrange_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 84-88 Reproductibility test ------------------------ We regenerate the same path by generating from the latent representation We make sure we get the original data back. .. GENERATED FROM PYTHON SOURCE LINES 88-93 .. code-block:: Python params['reproductibility'] = True params = generate_paths(params) params['graphic'](params) plt.show() .. image-sg:: /auto_ch9/images/sphx_glr_ch9_lagrange_002.png :alt: ch9 lagrange :srcset: /auto_ch9/images/sphx_glr_ch9_lagrange_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 94-96 We now generate a new set of 10 paths ------------------------------------------------ .. GENERATED FROM PYTHON SOURCE LINES 96-101 .. code-block:: Python params['reproductibility'] = False params['Nz'] = 10 params = generate_paths(params) params['graphic'](params) plt.show() pass .. image-sg:: /auto_ch9/images/sphx_glr_ch9_lagrange_003.png :alt: ch9 lagrange :srcset: /auto_ch9/images/sphx_glr_ch9_lagrange_003.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 3.211 seconds) .. _sphx_glr_download_auto_ch9_ch9_lagrange.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: ch9_lagrange.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: ch9_lagrange.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: ch9_lagrange.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_