.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/plot_truePosteriorForTwoDataPoint.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. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_plot_truePosteriorForTwoDataPoint.py: True and estimated posteriors ============================= The code below plots the true and estimated posteriors of a sample of two data points from the clutter problem. .. GENERATED FROM PYTHON SOURCE LINES 13-19 Implementation Details ---------------------- Below is the code for the EP functions used in this example: .. literalinclude:: /../../../src/ep/examples/clutter/core.py :language: python .. GENERATED FROM PYTHON SOURCE LINES 21-23 Import requirments ------------------ .. GENERATED FROM PYTHON SOURCE LINES 23-34 .. code-block:: Python import numpy as np from scipy.stats import multivariate_normal from scipy.stats import norm from IPython.display import display import plotly.graph_objects as go import ep.examples.clutter.utils import ep.examples.clutter.core import ep.examples.clutter.plot .. GENERATED FROM PYTHON SOURCE LINES 35-37 Generate two samples from the clutter model ------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 37-50 .. code-block:: Python theta = 3.0 a = 10.0 b = 100.0 w = 0.5 N = 2 x_min = -5.0 x_max = 10.0 x_dt = 0.01 samples_list = ep.examples.clutter.utils.sample(theta=theta, a=a, w=w, n_samples=N) samples = [np.array([sample]) for sample in samples_list] .. GENERATED FROM PYTHON SOURCE LINES 51-53 Compute true posterior ---------------------- .. GENERATED FROM PYTHON SOURCE LINES 53-110 .. code-block:: Python D = len(samples[0]) x1 = samples[0] x2 = samples[1] sigma2_SS = b / (1 + 2 * b) mu_SS = sigma2_SS * (x1 + x2) sigma2_SC = b / (1 + b) mu_SC = sigma2_SC * x1 sigma2_CS = sigma2_SC mu_CS = sigma2_CS * x2 sigma2_CC = b mu_CC = 0.0 pi_SS = ((1 - w)**2 * (1 / (2 * np.pi)**D) * (1 / (1 + 2 * b)**(D / 2)) * np.exp((b * np.linalg.norm(x1 + x2)**2 - (1 + 2 * b) * (np.linalg.norm(x1)**2 + np.linalg.norm(x2)**2)) / (2 * (1 + 2 * b)))) pi_SC = ((1 - w) * w * multivariate_normal(np.zeros(shape=D), a * np.eye(D)).pdf(x2) * multivariate_normal(np.zeros(shape=D), (b + 1) * np.eye(D)).pdf(x1)) pi_CS = (w * (1 - w) * multivariate_normal(np.zeros(shape=D), a * np.eye(D)).pdf(x1) * multivariate_normal(np.zeros(shape=D), (b + 1) * np.eye(D)).pdf(x2)) pi_CC = (w**2 * multivariate_normal(np.zeros(shape=D), a * np.eye(D)).pdf(x1) * multivariate_normal(np.zeros(shape=D), a * np.eye(D)).pdf(x2)) K = 1.0 / (pi_SS + pi_SC + pi_CS + pi_CC) def two_points_true_posterior(theta): answer = K * (pi_SS * multivariate_normal(mu_SS, sigma2_SS * np.eye(D)).pdf(theta) + pi_SC * multivariate_normal(mu_SC, sigma2_SC * np.eye(D)).pdf(theta) + pi_CS * multivariate_normal(mu_CS, sigma2_CS * np.eye(D)).pdf(theta) + pi_CC * multivariate_normal(mu_CC, sigma2_CC * np.eye(D)).pdf(theta)) return answer .. GENERATED FROM PYTHON SOURCE LINES 111-113 Expectation Propagation script ------------------------------ .. GENERATED FROM PYTHON SOURCE LINES 113-146 .. code-block:: Python num_iter = 20 D = len(samples[0]) m, v, m_f, v_f, s_f = ep.examples.clutter.core.init(b=b, D=D, N=N) log_evidences = [] snapshots = [] for iter_num in range(num_iter): for n in range(N): v_cn = ep.examples.clutter.core.get_cavity_var(v=v, v_fn=v_f[n]) m_cn = ep.examples.clutter.core.get_cavity_mean(m=m, m_fn=m_f[n], v_fn=v_f[n], v_cn=v_cn) Z_n = ep.examples.clutter.core.get_zeroth_moment(w=w, a=a, m_cn=m_cn, v_cn=v_cn, x_n=samples[n]) rho_n = ep.examples.clutter.core.get_site_strength(w=w, a=a, D=D, Z_n=Z_n, x_n=samples[n]) m = ep.examples.clutter.core.get_q_mean(m_cn=m_cn, v_cn=v_cn, rho_n=rho_n, x_n=samples[n]) v = ep.examples.clutter.core.get_q_var(m_cn=m_cn, v_cn=v_cn, rho_n=rho_n, x_n=samples[n]) v_f[n] = ep.examples.clutter.core.get_factor_var(v_cn=v_cn, v=v) m_f[n] = ep.examples.clutter.core.get_factor_mean(m_cn=m_cn, v_cn=v_cn, v_fn=v_f[n], m=m) s_f[n] = ep.examples.clutter.core.get_factor_scale(Z_n=Z_n, m_fn=m_f[n], v_fn=v_f[n], m_cn=m_cn, v_cn=v_cn) snapshots.append({ "iter": iter_num, "v_cn": v_cn, "m_cn": m_cn.copy(), "v": v, "m": m.copy(), "v_fn": v_f[n], "m_fn": m_f[n].copy(), }) log_evidence = ep.examples.clutter.core.get_log_evidence(m=m, v=v, m_f=m_f, v_f=v_f, s_f=s_f, b=b) log_evidences.append(log_evidence) .. GENERATED FROM PYTHON SOURCE LINES 147-149 Plot EP probability density functions after iteration 0 ------------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 149-163 .. code-block:: Python x_min = -10 x_max = 10 x_dt = 0.1 iter_num = 0 s = snapshots[iter_num] ep.examples.clutter.plot.plot_pdfs(theta=theta, m_cn=s["m_cn"], v_cn=s["v_cn"], m=s["m"], v=s["v"], m_fn=s["m_fn"], v_fn=s["v_fn"], samples=samples[:N], x_min=x_min, x_max=x_max, x_dt=x_dt, title=f"Iteration {iter_num}, Factor {N-1}", true_posterior_func=two_points_true_posterior) .. raw:: html


.. GENERATED FROM PYTHON SOURCE LINES 164-166 Plot EP probability density functions after iteration 3 ------------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 166-176 .. code-block:: Python iter_num = 3 s = snapshots[iter_num] ep.examples.clutter.plot.plot_pdfs(theta=theta, m_cn=s["m_cn"], v_cn=s["v_cn"], m=s["m"], v=s["v"], m_fn=s["m_fn"], v_fn=s["v_fn"], samples=samples[:N], x_min=x_min, x_max=x_max, x_dt=x_dt, title=f"Iteration {iter_num}, Factor {N-1}", true_posterior_func=two_points_true_posterior) .. raw:: html


.. GENERATED FROM PYTHON SOURCE LINES 177-179 Plot EP probability density functions after iteration 6 ------------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 179-189 .. code-block:: Python iter_num = 6 s = snapshots[iter_num] ep.examples.clutter.plot.plot_pdfs(theta=theta, m_cn=s["m_cn"], v_cn=s["v_cn"], m=s["m"], v=s["v"], m_fn=s["m_fn"], v_fn=s["v_fn"], samples=samples[:N], x_min=x_min, x_max=x_max, x_dt=x_dt, title=f"Iteration {iter_num}, Factor {N-1}", true_posterior_func=two_points_true_posterior) .. raw:: html


.. GENERATED FROM PYTHON SOURCE LINES 190-192 Plot EP probability density functions after iteration 9 ------------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 192-203 .. code-block:: Python iter_num = 9 s = snapshots[iter_num] ep.examples.clutter.plot.plot_pdfs(theta=theta, m_cn=s["m_cn"], v_cn=s["v_cn"], m=s["m"], v=s["v"], m_fn=s["m_fn"], v_fn=s["v_fn"], samples=samples[:N], x_min=x_min, x_max=x_max, x_dt=x_dt, title=f"Iteration {iter_num}, Factor {N-1}", true_posterior_func=two_points_true_posterior) .. raw:: html


.. GENERATED FROM PYTHON SOURCE LINES 204-206 Plot EP probability density functions after iteration 19 ------------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 206-216 .. code-block:: Python iter_num = 19 s = snapshots[iter_num] ep.examples.clutter.plot.plot_pdfs(theta=theta, m_cn=s["m_cn"], v_cn=s["v_cn"], m=s["m"], v=s["v"], m_fn=s["m_fn"], v_fn=s["v_fn"], samples=samples[:N], x_min=x_min, x_max=x_max, x_dt=x_dt, title=f"Iteration {iter_num}, Factor {N-1}", true_posterior_func=two_points_true_posterior) .. raw:: html


.. GENERATED FROM PYTHON SOURCE LINES 217-219 Plot log evidences ------------------ .. GENERATED FROM PYTHON SOURCE LINES 219-221 .. code-block:: Python ep.examples.clutter.plot.plot_log_evidences(log_evidences) .. raw:: html


.. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.157 seconds) .. _sphx_glr_download_auto_examples_plot_truePosteriorForTwoDataPoint.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_truePosteriorForTwoDataPoint.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_truePosteriorForTwoDataPoint.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_truePosteriorForTwoDataPoint.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_