.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/emulator/plot_scalargasp.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_emulator_plot_scalargasp.py: ScalarGaSP: GP emulation for a single-output function ===================================================== .. GENERATED FROM PYTHON SOURCE LINES 8-13 This example shows how to apply Gaussian process emulation to a single-output function using class :class:`.ScalarGaSP`. The task is to build a GP emulator for the function :math:`y = x * sin(x)` based on a few number of training data. .. GENERATED FROM PYTHON SOURCE LINES 16-17 First, import the class :class:`.ScalarGaSP` by .. GENERATED FROM PYTHON SOURCE LINES 18-21 .. code-block:: default from psimpy.emulator import ScalarGaSP .. GENERATED FROM PYTHON SOURCE LINES 22-26 Then, create an instance of :class:`.ScalarGaSP`. The parameter ``ndim`` (dimension of function input ``x``) must be specified. Optional parameters, such as ``method``, ``kernel_type``, etc., can be set up if desired. Here, we leave all the optional parameters to their default values. .. GENERATED FROM PYTHON SOURCE LINES 27-30 .. code-block:: default emulator = ScalarGaSP(ndim=1) .. GENERATED FROM PYTHON SOURCE LINES 31-34 Given training input points ``design`` and corresponding output values ``response``, the emulator can be trained using :py:meth:`.ScalarGaSP.train`. Below we train an emulator using :math:`8` selected points. .. GENERATED FROM PYTHON SOURCE LINES 35-47 .. code-block:: default import numpy as np def f(x): #return x + 3*np.sin(x/2) return x*np.sin(x) x = np.arange(2,10,1) y = f(x) emulator.train(design=x, response=y) .. rst-class:: sphx-glr-script-out .. code-block:: none The upper bounds of the range parameters are 1983.214 The initial values of range parameters are 39.66427 Start of the optimization 1 : The number of iterations is 10 The value of the marginal posterior function is -13.79116 Optimized range parameters are 2.237467 Optimized nugget parameter is 0 Convergence: TRUE The initial values of range parameters are 0.21875 Start of the optimization 2 : The number of iterations is 9 The value of the marginal posterior function is -13.79116 Optimized range parameters are 2.237467 Optimized nugget parameter is 0 Convergence: TRUE .. GENERATED FROM PYTHON SOURCE LINES 48-50 We can validate the performance of the trained emulator using the leave-one-out cross validation method :py:meth:`loo_validate()`. .. GENERATED FROM PYTHON SOURCE LINES 51-54 .. code-block:: default validation = emulator.loo_validate() .. GENERATED FROM PYTHON SOURCE LINES 55-57 Let's plot emulator predictions vs actual outputs. The error bar indicates the standard deviation. .. GENERATED FROM PYTHON SOURCE LINES 58-74 .. code-block:: default import matplotlib.pyplot as plt fig , ax = plt.subplots(figsize=(4,4)) ax.set_xlabel('Actual y') ax.set_ylabel('Emulator-predicted y') ax.set_xlim(np.min(y)-1,np.max(y)+1) ax.set_ylim(np.min(y)-1,np.max(y)+1) _ = ax.plot([np.min(y)-1,np.max(y)+1], [np.min(y)-1,np.max(y)+1]) _ = ax.errorbar(y, validation[:,0], validation[:,1], fmt='.', linestyle='', label='prediction and std') _ = plt.legend() plt.tight_layout() .. image-sg:: /auto_examples/emulator/images/sphx_glr_plot_scalargasp_001.png :alt: plot scalargasp :srcset: /auto_examples/emulator/images/sphx_glr_plot_scalargasp_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 75-80 With the trained emulator at our deposit, we can use the :py:meth:`.ScalarGaSP.predict()` method to make predictions at any arbitrary set of input points (``testing_input``). It should be noted that, ``testing_trend`` should be set according to ``trend`` used during emulator training. .. GENERATED FROM PYTHON SOURCE LINES 81-95 .. code-block:: default testing_input = np.arange(0,10,0.1) predictions = emulator.predict(testing_input) plt.plot(testing_input, predictions[:, 0], 'r-', label= "mean") plt.scatter(x, y, s=15, c='k', label="training data", zorder=3) plt.plot(testing_input, f(testing_input), 'k:', zorder=2, label="true function") plt.fill_between(testing_input, predictions[:, 1], predictions[:, 2], alpha=0.3, label="95% CI") plt.xlabel('x') plt.ylabel('emulator-predicted y') plt.xlim(testing_input[0], testing_input[-1]) _ = plt.legend() plt.tight_layout() .. image-sg:: /auto_examples/emulator/images/sphx_glr_plot_scalargasp_002.png :alt: plot scalargasp :srcset: /auto_examples/emulator/images/sphx_glr_plot_scalargasp_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 96-98 We can also draw any number of samples at ``testing_input``` using :py:meth:`.ScalarGaSP.sample()` method. .. GENERATED FROM PYTHON SOURCE LINES 99-117 .. code-block:: default nsamples = 5 samples = emulator.sample(testing_input, nsamples=nsamples) # sphinx_gallery_thumbnail_number = 3 for i in range(nsamples): plt.plot(testing_input, samples[:,i], '--', label=f'sample{i+1}') plt.scatter(x, y, s=15, c='k', label="training data", zorder=2) plt.plot(testing_input, f(testing_input), 'k:', zorder=2, label="true function") plt.fill_between(testing_input, predictions[:, 1], predictions[:, 2], alpha=0.3, label="95% CI") plt.xlabel('x') plt.ylabel('emulator-predicted y') plt.xlim(testing_input[0], testing_input[-1]) _ = plt.legend() plt.tight_layout() .. image-sg:: /auto_examples/emulator/images/sphx_glr_plot_scalargasp_003.png :alt: plot scalargasp :srcset: /auto_examples/emulator/images/sphx_glr_plot_scalargasp_003.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 118-124 .. tip:: Above example shows how to train a GP emulator based on noise-free training data, which is often the case of emulating a deterministic simulator. If you are dealing with noisy training data, you can - set the parameter ``nugget`` to a desired value, or - set ``nugget`` to :math:`0` and ``nugget_est`` to `True`, meaning that ``nugget`` is estimated from the noisy training data. .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.558 seconds) .. _sphx_glr_download_auto_examples_emulator_plot_scalargasp.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_scalargasp.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_scalargasp.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_