.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/plot_predictive_distribution.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_predictive_distribution.py: Predictive distribution ======================= Calculation of the predictive istribution of Bayesian linear regression model. .. GENERATED FROM PYTHON SOURCE LINES 10-12 Import requirments ------------------ .. GENERATED FROM PYTHON SOURCE LINES 12-19 .. code-block:: Python import numpy as np import scipy.stats import plotly.graph_objects as go import bayesianLinearRegression .. GENERATED FROM PYTHON SOURCE LINES 20-22 Define a function to generate sinusoidal regression data -------------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 22-29 .. code-block:: Python def generateData(x, sigma=0.3): y = np.sin(2*np.pi*x) t = y + np.random.normal(loc=0, scale=sigma, size=len(y)) return y, t .. GENERATED FROM PYTHON SOURCE LINES 30-32 Define functions to generate the design matrix sinusoidal regression data ------------------------------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 32-60 .. code-block:: Python def getGaussianBasisFunctions(mus, sigma): M = len(mus) basis_functions = [None for m in range(M)] for m in range(M): basis_functions[m] = lambda x, mu=mus[m], sigma=sigma: \ np.exp(-(x-mu)**2/(2.0*sigma**2)) return basis_functions def buildGaussianDesignMatrixRow(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 buildGaussianDesignMatrix(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,:] = buildGaussianDesignMatrixRow(x=x[n], basis_functions=basis_functions) return design_matrix .. GENERATED FROM PYTHON SOURCE LINES 61-63 Generate train data ------------------- .. GENERATED FROM PYTHON SOURCE LINES 63-72 .. code-block:: Python N = 10 # N = 25 # N = 4 x = np.sort(np.random.uniform(size=N)) _, t = generateData(x=x) x_dense = np.linspace(0, 1, 1000) y_dense, _ = generateData(x=x_dense) .. GENERATED FROM PYTHON SOURCE LINES 73-75 Plot train data --------------- .. GENERATED FROM PYTHON SOURCE LINES 75-86 .. code-block:: Python fig = go.Figure() trace_true = go.Scatter(x=x_dense, y=y_dense, mode="lines", line_color="green") trace_data = go.Scatter(x=x, y=t, mode="markers", marker_color="blue") fig.add_trace(trace_true) fig.add_trace(trace_data) fig.update_layout(xaxis_title="independent variable", yaxis_title="dependent variable", showlegend=False) fig .. raw:: html


.. GENERATED FROM PYTHON SOURCE LINES 87-89 Set estimation parameters ------------------------- .. GENERATED FROM PYTHON SOURCE LINES 89-96 .. code-block:: Python bf_mus = np.arange(0.1, 1.0, 0.1) bf_sigma = 1.0/(N-1) prior_precision = 2.0 likelihood_precision = 25.0 N_new = 100 .. GENERATED FROM PYTHON SOURCE LINES 97-99 Get and plot the basis functions -------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 99-112 .. code-block:: Python basis_functions = getGaussianBasisFunctions(mus=bf_mus, sigma=bf_sigma) fig = go.Figure() for i in range(len(basis_functions)): basis_function_values = basis_functions[i](x_dense) trace = go.Scatter(x=x_dense, y=basis_function_values, mode="lines") fig.add_trace(trace) fig.update_layout(xaxis_title="x", yaxis_title=r"$\phi_i(x)$", showlegend=False) fig .. raw:: html


.. GENERATED FROM PYTHON SOURCE LINES 113-115 Build design matrix ------------------- .. GENERATED FROM PYTHON SOURCE LINES 115-118 .. code-block:: Python Phi = buildGaussianDesignMatrix(x=x, basis_functions=basis_functions) .. GENERATED FROM PYTHON SOURCE LINES 119-121 Estimate posterior distribution ------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 121-125 .. code-block:: Python mN, SN = bayesianLinearRegression.batchWithSimplePrior( Phi=Phi, y=t, alpha=prior_precision, beta=likelihood_precision) .. GENERATED FROM PYTHON SOURCE LINES 126-128 Estimate predictive distribution -------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 128-140 .. code-block:: Python new_x = np.sort(np.random.uniform(size=N_new)) true_mean = np.empty(shape=N_new, dtype=np.double) new_mean = np.empty(shape=N_new, dtype=np.double) new_var = np.empty(shape=N_new, dtype=np.double) for n in range(N_new): true_mean[n] = np.sin(2*np.pi*new_x[n]) phi = buildGaussianDesignMatrixRow(x=new_x[n], basis_functions=basis_functions) new_mean[n], new_var[n] = bayesianLinearRegression.predict( phi=phi, mn=mN, Sn=SN, beta=likelihood_precision) .. GENERATED FROM PYTHON SOURCE LINES 141-143 Plot predictive distribution ---------------------------- .. GENERATED FROM PYTHON SOURCE LINES 143-170 .. code-block:: Python new_mean_upper = new_mean + 1.96*np.sqrt(new_var) new_mean_lower = new_mean - 1.96*np.sqrt(new_var) fig = go.Figure() trace_true = go.Scatter(x=new_x, y=true_mean, mode="lines", line_color="green") trace_mean = go.Scatter(x=new_x, y=new_mean, mode="lines", line_color="red") trace_mean_cb = go.Scatter(x=np.concatenate((new_x, new_x[::-1])), y=np.concatenate((new_mean_upper, new_mean_lower[::-1])), fill="toself", fillcolor="rgba(255,0,0,0.3)", line=dict(color="rgba(255,255,255,0)"), hoverinfo="skip", showlegend=False, ) trace_data = go.Scatter(x=x, y=t, mode="markers", marker_color="blue", marker_symbol="circle-open", marker_size=10) fig.add_trace(trace_true) fig.add_trace(trace_mean) fig.add_trace(trace_mean_cb) fig.add_trace(trace_data) fig.update_layout(xaxis_title="independent variable", yaxis_title="dependent variable", showlegend=False) fig # sphinx_gallery_thumbnail_path = '_static/predictive_distribution.png' .. raw:: html


.. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.040 seconds) .. _sphx_glr_download_auto_examples_plot_predictive_distribution.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_predictive_distribution.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_predictive_distribution.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_predictive_distribution.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_