.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/inference/mle/regression_model.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_mle_regression_model.py: Regression model ============================================== Here a model is defined that is of the form .. math:: y=f(θ) + \epsilon where f consists in running :class:`.RunModel`. In particular, here :math:`f(θ)=θ_{0} x + θ_{1} x^{2}` is a regression model. .. GENERATED FROM PYTHON SOURCE LINES 18-19 Initially we have to import the necessary modules. .. GENERATED FROM PYTHON SOURCE LINES 22-32 .. code-block:: default import shutil import numpy as np from UQpy import PythonModel from UQpy.inference import ComputationalModel, MLE from UQpy.distributions import Normal from UQpy.inference import MinimizeOptimizer from UQpy.run_model.RunModel import RunModel .. GENERATED FROM PYTHON SOURCE LINES 33-34 First we generate synthetic data, and add some noise to it. .. GENERATED FROM PYTHON SOURCE LINES 37-56 .. code-block:: default # Generate data 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)) .. GENERATED FROM PYTHON SOURCE LINES 57-59 Then we create an instance of the Model class, using model_type='python', and we perform maximum likelihood estimation of the two parameters. .. GENERATED FROM PYTHON SOURCE LINES 62-69 .. code-block:: default candidate_model = ComputationalModel(n_parameters=2, runmodel_object=h_func, error_covariance=error_covariance) optimizer = MinimizeOptimizer(method='nelder-mead') ml_estimator = MLE(inference_model=candidate_model, data=data_3, n_optimizations=1) print('fitted parameters: theta_0={0:.3f} (true=1.), and theta_1={1:.3f} (true=2.)'.format( ml_estimator.mle[0], ml_estimator.mle[1])) .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.000 seconds) .. _sphx_glr_download_auto_examples_inference_mle_regression_model.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/mle/regression_model.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: regression_model.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: regression_model.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_