def test_gaussian1d(): x = np.linspace(-6, 10, 1E3) means = np.array([-1.5, 0.0, 2.3]) sigmas = np.array([1, 0.25, 3.8]) weights = np.array([1, 1, 1]) gauss = GaussianMixture1D(means=means, sigmas=sigmas, weights=weights) y = gauss.pdf(x) dx = x[1] - x[0] integral = np.sum(y * dx) assert np.round(integral, 1) == 1
def test_gaussian1d(): epsilon = 1e-6 # some fudge factor for numeric integration x = np.linspace(-6, 10, 1E3) means = np.array([-1.5, 0.0, 2.3]) sigmas = np.array([1, 0.25, 3.8]) weights = np.array([1, 1, 1]) gauss = GaussianMixture1D(means=means, sigmas=sigmas, weights=weights) y = gauss.pdf(x) dx = x[1] - x[0] integral = np.sum(y * dx) assert integral + epsilon >= 1. # test that we are close to 1
def test_gaussian1d(): x = np.linspace(-6, 10, 1E3) means = np.array([-1.5, 0.0, 2.3]) sigmas = np.array([1, 0.25, 3.8]) weights = np.array([1, 1, 1]) gauss = GaussianMixture1D(means=means, sigmas=sigmas, weights=weights) y = gauss.pdf(x) # Check whether sampling works gauss.sample(10) dx = x[1] - x[0] integral = np.sum(y * dx) assert_allclose(integral, 1., atol=0.02)
import numpy as np from matplotlib import pyplot as plt from scipy.stats import norm from astroML.density_estimation import GaussianMixture1D #------------------------------------------------------------ # Generate the data mu1_in = 0 sigma1_in = 0.3 mu2_in = 1 sigma2_in = 1 ratio_in = 1.5 N = 200 np.random.seed(10) gm = GaussianMixture1D([mu1_in, mu2_in], [sigma1_in, sigma2_in], [ratio_in, 1]) x_sample = gm.sample(N) #------------------------------------------------------------ # Get the MLE fit for a single gaussian sample_mu = np.mean(x_sample) sample_std = np.std(x_sample, ddof=1) #------------------------------------------------------------ # Plot the sampled data ax = plt.axes() ax.hist(x_sample, 20, histtype='stepfilled', normed=True, fc='#CCCCCC') x = np.linspace(-2, 4, 1000) factor1 = ratio_in / (1. + ratio_in)