def pymc3_random_discrete(dist, paramdomains, valuedomain=Domain([0]), ref_rand=None, size=100000, alpha=0.05, fails=20): model = build_model(dist, valuedomain, paramdomains) domains = paramdomains.copy() for pt in product(domains): pt = Point(pt, model=model) p = alpha # Allow Chisq test to fail (i.e., the samples be different) # a certain number of times. f = fails while p <= alpha and f > 0: o = model.named_vars['value'].random(size=size, point=pt) e = ref_rand(size=size, **pt) o = np.atleast_1d(o).flatten() e = np.atleast_1d(e).flatten() observed = dict(zip(*np.unique(o, return_counts=True))) expected = dict(zip(*np.unique(e, return_counts=True))) for e in expected.keys(): expected[e] = (observed.get(e, 0), expected[e]) k = np.array([v for v in expected.values()]) if np.all(k[:, 0] == k[:, 1]): p = 1. else: _chi, p = st.chisquare(k[:, 0], k[:, 1]) f -= 1 assert p > alpha, str(pt)
def init_proposal_covariance(bij, vars, model, pop_size=1000): """ Create initial proposal covariance matrix based on random samples from the solution space. """ population_array = num.zeros((pop_size, bij.ordering.size)) for i in range(pop_size): point = Point({v.name: v.random() for v in vars}, model=model) population_array[i, :] = bij.map(point) return num.diag(population_array.var(0))
def test_errors(): _, model, _ = exponential_beta(2) with model: try: newstart = find_MAP(Point(x=[-.5, .01], y=[.5, 4.4])) except ValueError as e: msg = str(e) assert "x.logp" in msg, msg assert "x.value" not in msg, msg else: assert False, newstart
def pymc3_random(dist, paramdomains, ref_rand=None, valuedomain=Domain([0]), size=10000, alpha=0.05, fails=10): model = build_model(dist, valuedomain, paramdomains) domains = paramdomains.copy() for pt in product(domains): pt = Point(pt, model=model) p = alpha # Allow KS test to fail (i.e., the samples be different) # a certain number of times. Crude, but necessary. f = fails while p <= alpha and f > 0: s0 = model.named_vars['value'].random(size=size, point=pt) s1 = ref_rand(size=size, **pt) _, p = st.ks_2samp(np.atleast_1d(s0).flatten(), np.atleast_1d(s1).flatten()) f -= 1 assert p > alpha, str(pt)
def test_accuracy_normal(): _, model, (mu, _) = simple_model() with model: newstart = find_MAP(Point(x=[-10.5, 100.5])) close_to(newstart['x'], [mu, mu], 1e-5)
def test_accuracy_non_normal(): _, model, (mu, _) = non_normal(4) with model: newstart = find_MAP(Point(x=[.5, .01, .95, .99])) close_to(newstart['x'], mu, 1e-5)
def test_accuracy_non_normal(): _, model, (mu, _) = non_normal(4) with model: newstart = find_MAP(Point(x=[.5, .01, .95, .99])) close_to(newstart['x'], mu, select_by_precision(float64=1e-5, float32=1E-4))
def test_accuracy_normal(): _, model, (mu, _) = simple_model() with model: newstart = find_MAP(Point(x=[-10.5, 100.5])) close_to(newstart['x'], [mu, mu], select_by_precision(float64=1e-5, float32=1E-4))