def test_lpdf_matrix_N_components(self): llval = GMM1_lpdf( [ [1.0, 0.0, 0.0], [0, 0, 1], [0, 0, 1000], ], [0.25, 0.25, .5], # weights [0.0, 1.0, 2.0], # mu [1.0, 2.0, 5.0], # sigma ) print llval assert llval.shape == (3, 3) a = (.25 / np.sqrt(2 * np.pi * 1.0**2) * np.exp(-.5 * (1.0)**2)) a += (.25 / np.sqrt(2 * np.pi * 2.0**2)) a += (.5 / np.sqrt(2 * np.pi * 5.0**2) * np.exp(-.5 * (1.0 / 5.0)**2)) assert np.allclose(llval[0, 0], np.log(a)) assert np.allclose(llval[1, 2], np.log(a)) # case x = 0.0 a = (.25 / np.sqrt(2 * np.pi * 1.0**2)) a += (.25 / np.sqrt(2 * np.pi * 2.0**2) * np.exp(-.5 * (1.0 / 2.0)**2)) a += (.5 / np.sqrt(2 * np.pi * 5.0**2) * np.exp(-.5 * (2.0 / 5.0)**2)) assert np.allclose(llval[0, 1], np.log(a)) assert np.allclose(llval[0, 2], np.log(a)) assert np.allclose(llval[1, 0], np.log(a)) assert np.allclose(llval[1, 1], np.log(a)) assert np.allclose(llval[2, 0], np.log(a)) assert np.allclose(llval[2, 1], np.log(a)) assert np.isfinite(llval[2, 2])
def work(self): self.worked = True kwargs = dict( weights=self.weights, mus=self.mus, sigmas=self.sigmas, low=self.low, high=self.high, q=self.q, ) samples = GMM1(rng=self.rng, size=(self.n_samples, ), **kwargs) samples = np.sort(samples) edges = samples[::self.samples_per_bin] #print samples pdf = np.exp(GMM1_lpdf(edges[:-1], **kwargs)) dx = edges[1:] - edges[:-1] y = 1 / dx / len(dx) if self.show: plt.scatter(edges[:-1], y) plt.plot(edges[:-1], pdf) plt.show() err = (pdf - y)**2 print np.max(err) print np.mean(err) print np.median(err) if not self.show: assert np.max(err) < .1 assert np.mean(err) < .01 assert np.median(err) < .01
def test_lpdf_vector_N_components(self): llval = GMM1_lpdf( [1.0, 0.0], # x [0.25, 0.25, 0.5], # weights [0.0, 1.0, 2.0], # mu [1.0, 2.0, 5.0], # sigma ) # case x = 1.0 a = 0.25 / np.sqrt(2 * np.pi * 1.0 ** 2) * np.exp(-0.5 * (1.0) ** 2) a += old_div(0.25, np.sqrt(2 * np.pi * 2.0 ** 2)) a += ( 0.5 / np.sqrt(2 * np.pi * 5.0 ** 2) * np.exp(-0.5 * (old_div(1.0, 5.0)) ** 2) ) assert llval.shape == (2,) assert np.allclose(llval[0], np.log(a)) # case x = 0.0 a = old_div(0.25, np.sqrt(2 * np.pi * 1.0 ** 2)) a += ( 0.25 / np.sqrt(2 * np.pi * 2.0 ** 2) * np.exp(-0.5 * (old_div(1.0, 2.0)) ** 2) ) a += ( 0.5 / np.sqrt(2 * np.pi * 5.0 ** 2) * np.exp(-0.5 * (old_div(2.0, 5.0)) ** 2) ) assert np.allclose(llval[1], np.log(a))
def test_lpdf_scalar_one_component(self): llval = GMM1_lpdf( 1.0, # x [1.], # weights [1.0], # mu [2.0], # sigma ) assert llval.shape == () assert np.allclose(llval, np.log(1.0 / np.sqrt(2 * np.pi * 2.0**2)))
def test_lpdf_scalar_N_components(self): llval = GMM1_lpdf( 1.0, # x [0.25, 0.25, .5], # weights [0.0, 1.0, 2.0], # mu [1.0, 2.0, 5.0], # sigma ) a = (.25 / np.sqrt(2 * np.pi * 1.0**2) * np.exp(-.5 * (1.0)**2)) a += (.25 / np.sqrt(2 * np.pi * 2.0**2)) a += (.5 / np.sqrt(2 * np.pi * 5.0**2) * np.exp(-.5 * (1.0 / 5.0)**2))
def test_lpdf_scalar_N_components(self): llval = GMM1_lpdf( 1.0, # x [0.25, 0.25, 0.5], # weights [0.0, 1.0, 2.0], # mu [1.0, 2.0, 5.0], # sigma ) print(llval) a = 0.25 / np.sqrt(2 * np.pi * 1.0**2) * np.exp(-0.5 * (1.0)**2) a += old_div(0.25, np.sqrt(2 * np.pi * 2.0**2)) a += (0.5 / np.sqrt(2 * np.pi * 5.0**2) * np.exp(-0.5 * (old_div(1.0, 5.0))**2))
def work(self, **kwargs): self.__dict__.update(kwargs) del kwargs self.worked = True gkwargs = dict( weights=self.weights, mus=self.mus, sigmas=self.sigmas, low=self.low, high=self.high, q=self.q, ) samples = GMM1(rng=self.rng, size=(self.n_samples, ), ** gkwargs) / self.q print 'drew', len(samples), 'samples' assert np.all(samples == samples.astype('int')) min_max = int(samples.min()), int(samples.max()) counts = np.bincount(samples.astype('int') - min_max[0]) print counts xcoords = np.arange(min_max[0], min_max[1] + 1) * self.q prob = np.exp(GMM1_lpdf(xcoords, **gkwargs)) assert counts.sum() == self.n_samples y = counts / float(self.n_samples) if self.show: import matplotlib.pyplot as plt plt.scatter(xcoords, y, c='r', label='empirical') plt.scatter(xcoords, prob, c='b', label='predicted') plt.legend() plt.title(str(self.show)) plt.show() err = (prob - y)**2 print np.max(err) print np.mean(err) print np.median(err) if self.show: raise nose.SkipTest() else: assert np.max(err) < .1 assert np.mean(err) < .01 assert np.median(err) < .01
def work(self, **kwargs): self.__dict__.update(kwargs) del kwargs self.worked = True gkwargs = dict( weights=self.weights, mus=self.mus, sigmas=self.sigmas, low=self.low, high=self.high, q=self.q, ) samples = old_div( GMM1(rng=self.rng, size=(self.n_samples, ), **gkwargs), self.q) print("drew", len(samples), "samples") assert np.all(samples == samples.astype("int")) min_max = int(samples.min()), int(samples.max()) counts = np.bincount(samples.astype("int") - min_max[0]) print(counts) xcoords = np.arange(min_max[0], min_max[1] + 1) * self.q prob = np.exp(GMM1_lpdf(xcoords, **gkwargs)) assert counts.sum() == self.n_samples y = old_div(counts, float(self.n_samples)) if self.show: plt.scatter(xcoords, y, c="r", label="empirical") plt.scatter(xcoords, prob, c="b", label="predicted") plt.legend() plt.title(str(self.show)) plt.show() err = (prob - y)**2 print(np.max(err)) print(np.mean(err)) print(np.median(err)) if self.show: raise nose.SkipTest() else: assert np.max(err) < 0.1 assert np.mean(err) < 0.01 assert np.median(err) < 0.01
def test_lpdf_scalar_one_component(self): # x # weights # mu # sigma llval = GMM1_lpdf(1.0, [1.0], [1.0], [2.0]) assert llval.shape == () assert np.allclose(llval, np.log(old_div(1.0, np.sqrt(2 * np.pi * 2.0**2))))