def test_tag_output(self): def model(): return ergo.normal(7, 1, name="x") samples = ergo.run(model, num_samples=2000) stats = samples.describe() assert 6 < stats["output"]["mean"] < 8
def test_rejection(): def model(): x = ergo.flip() y = ergo.flip() ergo.condition(x or y) return x == y samples = ergo.run(model, num_samples=1000) assert 266 < sum(samples["output"]) < 466
def test_mem_2(): """ Check that mem is cleared at the start of each run """ @ergo.mem def model(): return ergo.lognormal_from_interval(1, 10) samples = ergo.run(model, num_samples=100) assert samples["output"].unique().size == 100
def test_sampling(self): def model(): x = ergo.lognormal_from_interval(1, 10, name="x") y = ergo.beta_from_hits(1, 9, name="y") z = x * y ergo.tag(z, "z") samples = ergo.run(model, num_samples=2000) stats = samples.describe() assert 3.5 < stats["x"]["mean"] < 4.5 assert 0.1 < stats["y"]["mean"] < 0.3 assert 0.6 < stats["z"]["mean"] < 1.0
def test_nomem(): """ Without mem, different calls to foo() should differ sometimes """ def foo(): return ergo.lognormal_from_interval(1, 10) def model(): x = foo() y = foo() return x == y samples = ergo.run(model, num_samples=1000) assert sum(samples["output"]) != 1000
def test_mem(): """ With mem, different calls to foo() should always have the same value """ @ergo.mem def foo(): return ergo.lognormal_from_interval(1, 10) def model(): x = foo() y = foo() return x == y samples = ergo.run(model, num_samples=1000) assert sum(samples["output"]) == 1000
def test_foretold_sampling(self): foretold = ergo.Foretold() # https://www.foretold.io/c/f45577e4-f1b0-4bba-8cf6-63944e63d70c/m/cf86da3f-c257-4787-b526-3ef3cb670cb4 # Distribution is mm(10 to 20, 200 to 210), a mixture model with most mass split between # 10 - 20 and 200 - 210. dist = foretold.get_question("cf86da3f-c257-4787-b526-3ef3cb670cb4") assert dist.quantile(0.25) < 100 assert dist.quantile(0.75) > 100 num_samples = 20000 samples = ergo.run(lambda: ergo.tag(dist.sample_community(), "sample"), num_samples=num_samples) # Probability mass is split evenly between both modes of the distribution, so approximately half of the # samples should be lower than 100 assert np.count_nonzero(samples > 100) == pytest.approx( num_samples / 2, 0.1)
def plot_question(sampler, num_samples=200, bw=None, start_date=date.today()): samples = ergo.run(sampler, num_samples=num_samples) summarize_question_samples(samples) q = sampler.question q_samples = samples[sampler.__name__] if ( q.id == 4128 ): # Date question: Need to convert back to date from days (https://github.com/oughtinc/ergo/issues/144) q_samples = np.array([start_date + timedelta(s) for s in q_samples]) if bw is not None: q.show_prediction(samples=q_samples, show_community=True, percent_kept=0.9, bw=bw) else: q.show_prediction(samples=q_samples, show_community=True, percent_kept=0.9)