.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/inference/bayes_parameter_estimation/bayes_parameter_IS_regression.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 or to run this example in your browser via Binder .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_inference_bayes_parameter_estimation_bayes_parameter_IS_regression.py: Parameter estimation using Importance Sampling - Regression Model ============================================================================= Here a model is defined that is of the form .. math:: y=f(θ) + \epsilon where :math:`f` consists in running RunModel. In particular, here :math:`f(θ)= θ_{0} x + θ_{1} x^{2}` is a regression model. .. GENERATED FROM PYTHON SOURCE LINES 16-17 Initially we have to import the necessary modules. .. GENERATED FROM PYTHON SOURCE LINES 20-38 .. code-block:: default import shutil import numpy as np import matplotlib.pyplot as plt from UQpy import PythonModel from UQpy.sampling.ImportanceSampling import ImportanceSampling from UQpy.inference import BayesParameterEstimation, ComputationalModel from UQpy.run_model.RunModel import RunModel # required to run the quadratic model from sklearn.neighbors import KernelDensity # for the plots from UQpy.distributions import JointIndependent, Normal def pdf_from_kde(domain, samples1d): bandwidth = 1.06 * np.std(samples1d) * samples1d.size ** (-1 / 5) kde = KernelDensity(bandwidth=bandwidth).fit(samples1d.reshape((-1, 1))) log_dens = kde.score_samples(domain) return np.exp(log_dens) .. GENERATED FROM PYTHON SOURCE LINES 39-40 First we generate synthetic data, and add some noise to it. .. GENERATED FROM PYTHON SOURCE LINES 43-85 .. code-block:: default param_true = np.array([1.0, 2.0]).reshape((1, -1)) print('Shape of true parameter vector: {}'.format(param_true.shape)) model = PythonModel(model_script='local_pfn_models.py', model_object_name='model_quadratic', delete_files=True, var_names=['theta_0', 'theta_1']) h_func = RunModel(model=model) h_func.run(samples=param_true) # Add noise error_covariance = 1. data_clean = np.array(h_func.qoi_list[0]) noise = Normal(loc=0., scale=np.sqrt(error_covariance)).rvs(nsamples=50).reshape((50,)) data_3 = data_clean + noise print('Shape of data: {}'.format(data_3.shape)) inference_model = ComputationalModel(n_parameters=2, runmodel_object=h_func, error_covariance=error_covariance) sampling = ImportanceSampling(proposal=JointIndependent([Normal(scale=2, )] * 2)) bayes_estimator =\ BayesParameterEstimation(inference_model=inference_model, data=data_3, sampling_class=sampling, nsamples=5000) s = bayes_estimator.sampler.samples w = bayes_estimator.sampler.weights print(sum(w)) # print results fig, ax = plt.subplots(1, 2) for i in range(2): ax[i].hist(x=s[:, i], weights=None, density=True, range=(-4, 4), bins=20, color='blue', alpha=0.4, label='prior') ax[i].hist(x=s[:, i], weights=w, density=True, range=(-4, 4), bins=20, color='orange', alpha=0.7, label='posterior') ax[i].legend() ax[i].set_title('theta {}'.format(i + 1)) plt.show() .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.000 seconds) .. _sphx_glr_download_auto_examples_inference_bayes_parameter_estimation_bayes_parameter_IS_regression.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: binder-badge .. image:: images/binder_badge_logo.svg :target: https://mybinder.org/v2/gh/SURGroup/UQpy/master?urlpath=lab/tree/notebooks/auto_examples/inference/bayes_parameter_estimation/bayes_parameter_IS_regression.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: bayes_parameter_IS_regression.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: bayes_parameter_IS_regression.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_