.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/simulation/plot_simulation.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_examples_simulation_plot_simulation.py: Simulation of functional data ============================= .. GENERATED FROM PYTHON SOURCE LINES 6-17 .. code-block:: Python # Author: Steven Golovkine # License: MIT # Load packages import numpy as np from FDApy.representation import DenseArgvals from FDApy.simulation import KarhunenLoeve from FDApy.visualization import plot .. GENERATED FROM PYTHON SOURCE LINES 18-21 Two main features of functional data are noise and sparsity. The package provides methods to simulate noisy and sparse functional data. In this example, we will simulate noisy and sparse functional data using the Karhunen-Loève decomposition. .. GENERATED FROM PYTHON SOURCE LINES 21-33 .. code-block:: Python # Set general parameters rng = 42 n_obs = 10 # Parameters of the basis name = "bsplines" n_functions = 5 argvals = DenseArgvals({"input_dim_0": np.linspace(0, 1, 101)}) .. GENERATED FROM PYTHON SOURCE LINES 34-41 For one dimensional data ------------------------ We simulate :math:`N = 10` curves on the one-dimensional observation grid :math:`\{0, 0.01, 0.02, \cdots, 1\}`, based on the first :math:`K = 5` B-splines basis functions on :math:`[0, 1]` and the variance of the scores random variables equal to :math:`1`. .. GENERATED FROM PYTHON SOURCE LINES 41-48 .. code-block:: Python kl = KarhunenLoeve( basis_name=name, n_functions=n_functions, argvals=argvals, random_state=rng ) kl.new(n_obs=n_obs) _ = plot(kl.data) .. image-sg:: /auto_examples/simulation/images/sphx_glr_plot_simulation_001.png :alt: plot simulation :srcset: /auto_examples/simulation/images/sphx_glr_plot_simulation_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 49-55 **Adding noise** --- We can generates a noisy version of the functional data by adding i.i.d. realizations of the random variable :math:`\varepsilon \sim \mathcal{N}(0, \sigma^2)` to the observation. In this example, we set :math:`\sigma^2 = 0.05`. .. GENERATED FROM PYTHON SOURCE LINES 55-62 .. code-block:: Python # Add some noise to the simulation. kl.add_noise(0.05) # Plot the noisy simulations _ = plot(kl.noisy_data) .. image-sg:: /auto_examples/simulation/images/sphx_glr_plot_simulation_002.png :alt: plot simulation :srcset: /auto_examples/simulation/images/sphx_glr_plot_simulation_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 63-70 **Sparsification** --- We can generates a sparsified version of the functional data object by randomly removing a certain percentage of the sampling points. The percentage of retain samplings points can be supplied by the user. In this example, the retained number of observations will be different for each curve and be randomly drawn between :math:`0.45` and :math:`0.55` (percentage :math:`\pm` epsilon). .. GENERATED FROM PYTHON SOURCE LINES 70-77 .. code-block:: Python # Sparsify the data kl.sparsify(percentage=0.5, epsilon=0.05) _ = plot(kl.sparse_data) .. image-sg:: /auto_examples/simulation/images/sphx_glr_plot_simulation_003.png :alt: plot simulation :srcset: /auto_examples/simulation/images/sphx_glr_plot_simulation_003.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 78-85 For two dimensional data ------------------------ We simulate :math:`N = 1` image on the two-dimensional observation grid :math:`\{0, 0.01, 0.02, \cdots, 1\} \times \{0, 0.01, 0.02, \cdots, 1\}`, based on the tensor product of the first :math:`K = 25` B-splines basis functions on :math:`[0, 1] \times [0, 1]` and the variance of the scores random variables equal to :math:`1`. .. GENERATED FROM PYTHON SOURCE LINES 85-100 .. code-block:: Python # Parameters of the basis name = ("bsplines", "bsplines") n_functions = (5, 5) argvals = DenseArgvals( {"input_dim_0": np.linspace(0, 1, 101), "input_dim_1": np.linspace(0, 1, 101)} ) kl = KarhunenLoeve( basis_name=name, n_functions=n_functions, argvals=argvals, random_state=rng ) kl.new(n_obs=1) _ = plot(kl.data) .. image-sg:: /auto_examples/simulation/images/sphx_glr_plot_simulation_004.png :alt: plot simulation :srcset: /auto_examples/simulation/images/sphx_glr_plot_simulation_004.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 101-107 **Adding noise** --- We can generates a noisy version of the functional data by adding i.i.d. realizations of the random variable :math:`\varepsilon \sim \mathcal{N}(0, \sigma^2)` to the observation. In this example, we set :math:`\sigma^2 = 0.05`. .. GENERATED FROM PYTHON SOURCE LINES 107-114 .. code-block:: Python # Add some noise to the simulation. kl.add_noise(0.05) # Plot the noisy simulations _ = plot(kl.noisy_data) .. image-sg:: /auto_examples/simulation/images/sphx_glr_plot_simulation_005.png :alt: plot simulation :srcset: /auto_examples/simulation/images/sphx_glr_plot_simulation_005.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 115-118 **Sparsification** --- The sparsification is not implemented for two-dimensional (and higher) data. .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.584 seconds) .. _sphx_glr_download_auto_examples_simulation_plot_simulation.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_simulation.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_simulation.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_simulation.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_