Path: blob/master/deprecated/scripts/bimodal_dist_plot.py
1192 views
# Bimodal distribution (mixture of two 1d Gaussians)1# Based on https://github.com/probml/pmtk3/blob/master/demos/bimodalDemo.m234import superimport56import numpy as np7import matplotlib.pyplot as plt8import pyprobml_utils as pml91011from scipy.stats import norm1213# Define two normal distrubutions and their corresponding weights.14mu = [0, 2]15sigma = [1, 0.05]16n = [norm(loc=mu[i], scale=sigma[i]) for i in range(2)]17w = [0.5, 0.5]1819# Define a set of x points for graphing.20xs = np.linspace(-2, 2*mu[1], 600)2122# Combine the two distributions by their weights, evaluated at the x points.23p = sum(w[i] * n[i].pdf(xs) for i in range(2))2425# Calculate the mean of the final distribution.26mean_p = np.mean(xs * p)2728# Plot the final distribution and its mean.29linewidth = 330plt.figure()31plt.plot(xs, p, 'black', linewidth=linewidth)32plt.vlines(mean_p, ymin=0, ymax=max(p), color='red', linewidth=linewidth)33pml.savefig('bimodalSpike.pdf')34plt.show()353637# Another example, with two modes38mu = [0, 2]39sigma = [0.5, 0.5]40n = [norm(loc=mu[i], scale=sigma[i]) for i in range(2)]41w = [0.5, 0.5]42xs = np.linspace(-2, 2*mu[1], 600)43p = sum(w[i] * n[i].pdf(xs) for i in range(2))4445plt.figure()46linewidth = 347plt.plot(xs, p, 'black', linewidth=linewidth)48pml.savefig('bimodalDistribution.pdf')49plt.show()505152