.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/plot_models_evidences.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_plot_models_evidences.py: Model evidence ============== Calculation of the marginalized log likelihood for models of different polynomial order. .. GENERATED FROM PYTHON SOURCE LINES 11-13 Import requirments ------------------ .. GENERATED FROM PYTHON SOURCE LINES 13-19 .. code-block:: Python import numpy as np import plotly.graph_objects as go import bayesianLinearRegression .. GENERATED FROM PYTHON SOURCE LINES 20-22 Define functions to generate the design matrix sinusoidal regression data ------------------------------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 22-48 .. code-block:: Python def getPolynomialBasisFunctions(M): basis_functions = [None for m in range(M+1)] for m in range(M+1): basis_functions[m] = lambda x, m=m: x**m return basis_functions def buildDesignMatrixRow(x, basis_functions): M = len(basis_functions) design_matrix_row = np.empty(shape=M, dtype=np.double) for m in range(M): design_matrix_row[m] = basis_functions[m](x) return design_matrix_row def buildDesignMatrix(x, basis_functions): M = len(basis_functions) N = len(x) design_matrix = np.empty(shape=(N, M), dtype=np.double) for n in range(N): design_matrix[n, :] = buildDesignMatrixRow( x=x[n], basis_functions=basis_functions) return design_matrix .. GENERATED FROM PYTHON SOURCE LINES 49-51 Define a function to generate polynomial data --------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 51-61 .. code-block:: Python def generateData(x, sigma, coefs): basis_functions = getPolynomialBasisFunctions(M=len(coefs)-1) Phi = buildDesignMatrix(x=x, basis_functions=basis_functions) y = Phi @ coefs noise = np.random.normal(loc=0, scale=sigma, size=len(y)) t = y + noise return y, t .. GENERATED FROM PYTHON SOURCE LINES 62-64 Set estimation parameters ------------------------- .. GENERATED FROM PYTHON SOURCE LINES 64-68 .. code-block:: Python prior_precision = 10.0 likelihood_precision = 10.0 .. GENERATED FROM PYTHON SOURCE LINES 69-71 Generate data ------------- .. GENERATED FROM PYTHON SOURCE LINES 71-80 .. code-block:: Python N = 50 x = 1.0 + np.random.uniform(size=N) # we generate data with M+1=5 coefficients, so that the marginalized log # likelihood should attain its maximum at M=4 (see Figure at the bottom). _, y = generateData(x=x, sigma=1.0/likelihood_precision, coefs=np.array([-0.5, 0.5, -0.5, 0.5, -0.5])) .. GENERATED FROM PYTHON SOURCE LINES 81-83 Calculate model evindences -------------------------- .. GENERATED FROM PYTHON SOURCE LINES 83-95 .. code-block:: Python Ms = np.arange(10) log_evidences = [None for m in Ms] for M in Ms: basis_functions = getPolynomialBasisFunctions(M=M) Phi = buildDesignMatrix(x=x, basis_functions=basis_functions) mN, SN = bayesianLinearRegression.batchWithSimplePrior( Phi=Phi, y=y, alpha=prior_precision, beta=likelihood_precision) log_evidences[M] = bayesianLinearRegression.computeLogEvidence( Phi=Phi, y=y, mN=mN, SN=SN, alpha=prior_precision, beta=likelihood_precision) .. GENERATED FROM PYTHON SOURCE LINES 96-99 Plot models' log evidences (log evidence should maximize for M=4; see Generate data above) ----------------------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 99-110 .. code-block:: Python fig = go.Figure() trace = go.Scatter(x=Ms, y=log_evidences, mode="lines+markers", line=dict(color="blue")) fig.add_trace(trace) fig.update_layout(xaxis_title="M", yaxis_title=r"$\log p(\mathbf{y}|\alpha,\beta)$") fig # sphinx_gallery_thumbnail_path = '_static/model_evidence.png' .. raw:: html


.. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.746 seconds) .. _sphx_glr_download_auto_examples_plot_models_evidences.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/joacorapela/bayesianLinearRegression/gh-pages?filepath=notebooks/auto_examples/plot_models_evidences.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_models_evidences.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_models_evidences.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_