In :
# plot the probability density function for a mixture of two Gaussian distributions
import scipy.stats

means = [1, 2]
variances = [.05, .05]
proportions = [.3, .7]
x = linspace(0, 3, 3000)
y = proportions * scipy.stats.norm.pdf(x, loc=means, scale=sqrt(variances)) +\
proportions * scipy.stats.norm.pdf(x, loc=means, scale=sqrt(variances))
title('Mixture of 2 Gaussians with means = ' + str(means) + ' and variances = ' + str(variances))
plot(x, y)

Out:
[<matplotlib.lines.Line2D at 0x4e24350>] In :
# sample some points from the mixture
np.random.seed(123)
N = 150
n1 = N * proportions
n2 = N - n1
sampled_points = normal(means, sqrt(variances), n1)
sampled_points = np.append(sampled_points, normal(means, sqrt(variances), n2))

title('Histogram of ' + str(N) + ' points sampled from a mixture of 2 Gaussians')
hist(sampled_points, bins=10); In :
# kernel density estimation (page 123 in PRML (2.250))
def my_gaussian_kde(x, sampled_points, h):
return 1.0/len(sampled_points) * sum([1/sqrt(2*pi*h**2) * exp((-1/(2*h**2))*(x-p_i)**2) for p_i in sampled_points])

title('green = true density, blue = density estimated with a gaussian kernel and ' + str(len(sampled_points)) + ' points')
plot(x, [my_gaussian_kde(x_i, sampled_points, .1) for x_i in x], color="blue")
plot(x, y, color="green")

Out:
[<matplotlib.lines.Line2D at 0x4f43110>] In :
# small smoothing parameter (noisy estimation)
title('green = true density, blue = density estimated with a gaussian kernel and ' + str(len(sampled_points)) + ' points')
plot(x, [my_gaussian_kde(x_i, sampled_points, .05) for x_i in x], color="blue")
plot(x, y, color="green")

Out:
[<matplotlib.lines.Line2D at 0x4bf98d0>] In :
# big smoothing parameter (oversmoothing)
title('green = true density, blue = density estimated with a gaussian kernel and ' + str(len(sampled_points)) + ' points')
plot(x, [my_gaussian_kde(x_i, sampled_points, .2) for x_i in x], color="blue")
plot(x, y, color="green")

Out:
[<matplotlib.lines.Line2D at 0x4f66210>] 