.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/plot_batchBayesianLinearRegression.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_batchBayesianLinearRegression.py: Batch Bayesian linear regression ================================ Batch estimation the posterior of the weighs of the linear regression model. .. GENERATED FROM PYTHON SOURCE LINES 10-12 Import requirements ------------------- .. 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 data generation variables -------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 22-28 .. code-block:: Python n_samples = 20 a0 = -0.3 a1 = 0.5 likelihood_precision_coef = (1/0.2)**2 .. GENERATED FROM PYTHON SOURCE LINES 29-31 Generate data ------------- .. GENERATED FROM PYTHON SOURCE LINES 31-36 .. code-block:: Python x = np.random.uniform(low=-1, high=1, size=n_samples) y = a0 + a1 * x t = y + np.random.standard_normal(size=y.shape) * 1.0/likelihood_precision_coef .. GENERATED FROM PYTHON SOURCE LINES 37-39 Define plotting variables ------------------------- .. GENERATED FROM PYTHON SOURCE LINES 39-50 .. code-block:: Python n_post_samples = 6 marker_true = "cross" size_true = 10 color_true = "red" marker_data = "circle-open" size_data = 10 color_data = "blue" line_width_data = 5 x_dense = np.arange(-1.0, 1.0, 0.1) .. GENERATED FROM PYTHON SOURCE LINES 51-53 Plot generated data ------------------- .. GENERATED FROM PYTHON SOURCE LINES 53-71 .. code-block:: Python y_true = a0 + a1 * x_dense fig = go.Figure() trace_true = go.Scatter(x=x_dense, y=y_true, mode="lines", line_color="green", showlegend=False) fig.add_trace(trace_true) trace_data_points = go.Scatter(x=x, y=t, mode="markers", marker_symbol=marker_data, marker_size=size_data, marker_color=color_data, marker_line_width=line_width_data, showlegend=False, ) fig.add_trace(trace_data_points) fig.update_xaxes(title_text="x") fig.update_yaxes(title_text="y") .. raw:: html


.. GENERATED FROM PYTHON SOURCE LINES 72-74 Define estimation variables --------------------------- .. GENERATED FROM PYTHON SOURCE LINES 74-77 .. code-block:: Python prior_precision_coef = 2.0 .. GENERATED FROM PYTHON SOURCE LINES 78-80 Estimate posterior ------------------ .. GENERATED FROM PYTHON SOURCE LINES 80-87 .. code-block:: Python Phi = np.column_stack((np.ones(len(x)), x)) mN, SN = bayesianLinearRegression.batchWithSimplePrior( Phi=Phi, y=y, alpha=prior_precision_coef, beta=likelihood_precision_coef) .. GENERATED FROM PYTHON SOURCE LINES 88-90 Plot posterior pdf ------------------ .. GENERATED FROM PYTHON SOURCE LINES 90-116 .. code-block:: Python x_grid = np.linspace(-1, 1, 100) y_grid = np.linspace(-1, 1, 100) X_grid, Y_grid = np.meshgrid(x_grid, y_grid) pos = np.dstack((X_grid, Y_grid)) rv = scipy.stats.multivariate_normal(mN, SN) Z = rv.pdf(pos) fig = go.Figure() trace_post = go.Contour(x=x_grid, y=y_grid, z=Z, showscale=False) fig.add_trace(trace_post) trace_true_coef = go.Scatter(x=[a0], y=[a1], mode="markers", marker_symbol=marker_true, marker_size=size_true, marker_color=color_true, name="true mean") fig.add_trace(trace_true_coef) fig.add_vline(x=0, line_color="white") fig.add_hline(y=0, line_color="white") fig.update_layout(xaxis_title="Intercept", yaxis_title="Slope") fig .. raw:: html


.. GENERATED FROM PYTHON SOURCE LINES 117-119 Plot sampled regression lines ----------------------------- .. GENERATED FROM PYTHON SOURCE LINES 119-136 .. code-block:: Python fig = go.Figure() samples = rv.rvs(size=n_post_samples) for a_sample in samples: sample_intercept, sample_slope = a_sample sample_y = sample_intercept + sample_slope * x_dense trace = go.Scatter(x=x_dense, y=sample_y, mode="lines", line_color="red", showlegend=False) fig.add_trace(trace) fig.update_xaxes(title_text="x") fig.update_yaxes(title_text="y") fig.add_trace(trace_data_points) fig # sphinx_gallery_thumbnail_path = '_static/bblr.png' .. raw:: html


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