def setUp(self): lst = [0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3] observed = make_pmf_from_list(lst) expected = copy.deepcopy(observed) pmf_probabilities = PmfProbabilityRange(0, 3) self.chi_squared_test = ChiSquaredTest( observed, expected, [pmf_probabilities.get_probabilities], ntrials=10)
def test_variance_pmf(self): lst = [4,4,4,4,4] pmf = make_pmf_from_list(lst) self.assertEqual(variance(pmf), 0)
def test_mean_pmf(self): lst = [4,4,4,4,4] pmf = make_pmf_from_list(lst) self.assertEqual(mean(pmf), 4)
def test_relative_mean_difference(self): lst = [1,1,1,1,1,1,1,1,1,1] pmf = make_pmf_from_list(lst) self.assertEqual(relative_mean_difference(pmf), 0.0)
def _simulate_probabilities(self): random_observed_sample = [self.expected_sample.random() for _ in xrange(len(self.expected_sample))] simulated_observed_sample = make_pmf_from_list(random_observed_sample, self.expected_sample.name) probs = [prob_func(simulated_observed_sample) for prob_func in self.prob_functions ] return [probs]
def test_make_pmf_from_list(self): lst = [1, 1, 2, 2, 3, 3, 4, 5, 5, 5, 5] self.assertEqual(len(make_pmf_from_list(lst)), 5)
def test_make_pmf_from_list(self): lst = [1,1,2,2,3,3,4,5,5,5,5] self.assertEqual(len(make_pmf_from_list(lst)), 5)
def __init__(self, low, high, steps): self.low = low self.high = high self.steps = steps hypos = [self.low + (self.high-self.low) * i / (self.steps-1.0) for i in range(self.steps)] self.pmf = make_pmf_from_list(hypos)
def test_variance_pmf(self): lst = [4, 4, 4, 4, 4] pmf = make_pmf_from_list(lst) self.assertEqual(variance(pmf), 0)
def test_mean_pmf(self): lst = [4, 4, 4, 4, 4] pmf = make_pmf_from_list(lst) self.assertEqual(mean(pmf), 4)
def test_relative_mean_difference(self): lst = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1] pmf = make_pmf_from_list(lst) self.assertEqual(relative_mean_difference(pmf), 0.0)
def test_get_posterior(self): pmf = make_pmf_from_list([1,1,1,1]) self.assertEqual(self.bayesian_est.get_posterior(), pmf)
def setUp(self): pmf = make_pmf_from_list([1,1,1,1]) self.bayesian_est = BayesianEstimator(pmf, [1,1,1,1],TestLikelihood())
def test_pmf_probability_range_get_probabilities(self): lst = [0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3] pmf = make_pmf_from_list(lst) pmf_probabilities = PmfProbabilityRange(0, 3) self.assertEqual(pmf_probabilities.get_probabilities(pmf), 0.75)
def test_zscores_pmf(self): lst = [4,4,4,4,4] pmf = make_pmf_from_list(lst) self.assertEqual(zscores(pmf), [0])
def test_zscores_pmf(self): lst = [4, 4, 4, 4, 4] pmf = make_pmf_from_list(lst) self.assertEqual(zscores(pmf), [0])
def __init__(self, prior, sample, likelihood): test_obj_instance(prior, Pmf) test_obj_instance(sample, list) test_obj_subclass(likelihood, Likelihood) self.prior = prior self.sample = sample self.likelihood = likelihood self.posterior = deepcopy(prior) self.posterior.name = 'posterior' self._estimate_posterior() def _estimate_posterior(self): for hypo in self.prior.get_dict().itervalues(): self.posterior.multiply( hypo, self.likelihood.get_likelihood(self.sample, hypo)) self.posterior.normalise() def get_posterior(self): return self.posterior if __name__ == '__main__': uniform = Uniform(0, 100, 100) uniform_dist = [uniform.generate() for _ in xrange(100)] prior = make_pmf_from_list(uniform_dist) sample = [10.0, 9.0] bayesian_est = BayesianEstimator(prior, sample, ConditionalExponetialLikelihood(0, 100)) print make_cdf_from_pmf(bayesian_est.get_posterior()).sort()
class BayesianEstimator(object): def __init__(self, prior, sample, likelihood): test_obj_instance(prior, Pmf) test_obj_instance(sample, list) test_obj_subclass(likelihood, Likelihood) self.prior = prior self.sample = sample self.likelihood = likelihood self.posterior = deepcopy(prior) self.posterior.name = 'posterior' self._estimate_posterior() def _estimate_posterior(self): for hypo in self.prior.get_dict().itervalues(): self.posterior.multiply(hypo, self.likelihood.get_likelihood(self.sample,hypo)) self.posterior.normalise() def get_posterior(self): return self.posterior if __name__ == '__main__': uniform = Uniform(0,100,100) uniform_dist = [uniform.generate() for _ in xrange(100)] prior = make_pmf_from_list(uniform_dist) sample = [10.0,9.0] bayesian_est = BayesianEstimator(prior, sample, ConditionalExponetialLikelihood(0,100)) print make_cdf_from_pmf(bayesian_est.get_posterior()).sort()