def test_binomial_loglikelihood(self): n = 17 p = .9 binomial = Binomial(n, p) samples = binomial.sample(100) loglik0 = stats.binom.logpmf(samples, n, p) loglik1 = binomial.loglikelihood(samples) self.assertLess(max(abs(loglik0 - loglik1)), 1e-8)
def test_binomial_pickle(self): tmp_file = mkstemp()[1] p0 = Binomial(13, .76) with open(tmp_file, 'w') as handle: dump({'p': p0}, handle) with open(tmp_file) as handle: p1 = load(handle)['p'] x = p0.sample(100) self.assertLess(max(abs(p0.loglikelihood(x) - p1.loglikelihood(x))), 1e-6)
def test_binomial_sample(self): T = 1000 for p, N in zip(rand(10), randint(100, size=10)): binomial = Binomial(N, p) # observed and expected frequencies f_obs = histogram(binomial.sample(T).ravel(), arange(N + 2) - 0.5)[0] f_exp = stats.binom.pmf(arange(N + 1), N, p) * T # Pearson's chi-squared test p = stats.chisquare(f_obs, f_exp)[1] self.assertGreater(p, 0.00001)