def model() -> Model: KeanuRandom.set_default_random_seed(1) model = thermometers.model() model.thermometer_one.observe(22.0) model.thermometer_two.observe(20.0) return model
def test_iter_with_live_plot(net: BayesNet) -> None: KeanuRandom.set_default_random_seed(1) _, ax = plt.subplots(3, 1, squeeze=False) samples = generate_samples(net=net, sample_from=net.get_latent_vertices(), live_plot=True, refresh_every=5, ax=ax) for sample in islice(samples, 5): pass reorder_subplots(ax) assert len(ax) == 3 assert all(len(ax[i][0].get_lines()) == 1 for i in range(3)) assert np.allclose( ax[0][0].get_lines()[0].get_ydata(), [0.49147822, 0.49147822, 0.49147822, 0.20033212, 0.20033212]) assert np.allclose( ax[1][0].get_lines()[0].get_ydata(), [0.87268333, 1.10409369, 1.10409369, 1.10409369, 0.69098161]) assert np.allclose( ax[2][0].get_lines()[0].get_ydata(), [-14.46166855, -14.46166855, 0.32305686, 0.32305686, 0.32305686])
def test_sample_with_plot(net: BayesNet) -> None: KeanuRandom.set_default_random_seed(1) _, ax = plt.subplots(3, 1, squeeze=False) sample(net=net, sample_from=net.get_latent_vertices(), draws=5, plot=True, ax=ax) reorder_subplots(ax) assert len(ax) == 3 assert all(len(ax[i][0].get_lines()) == 1 for i in range(3)) assert all(len(ax[i][0].get_lines()[0].get_ydata()) == 5 for i in range(3))
def model() -> Model: KeanuRandom.set_default_random_seed(1) with Model() as m: m.a = Gaussian(0., 50.) m.b = Gaussian(0., 50.) m.c = m.a + m.b m.d = Gaussian(m.c, 1.) m.d.observe(20.0) return m
def test_iter_with_live_plot(net: BayesNet) -> None: KeanuRandom.set_default_random_seed(1) _, ax = plt.subplots(3, 1, squeeze=False) samples = generate_samples(net=net, sample_from=net.get_latent_vertices(), live_plot=True, refresh_every=5, ax=ax) for sample in islice(samples, 5): pass reorder_subplots(ax) assert len(ax) == 3 assert all(len(ax[i][0].get_lines()) == 1 for i in range(3)) assert all(len(ax[i][0].get_lines()[0].get_ydata() == 5) for i in range(3))
def test_coalmining() -> None: KeanuRandom.set_default_random_seed(1) coal_mining = CoalMining() model = coal_mining.model() model.disasters.observe(coal_mining.training_data()) net = BayesNet(model.switchpoint.get_connected_graph()) samples = sample(net=net, sample_from=net.get_latent_vertices(), draws=50000, drop=10000, down_sample_interval=5) vertex_samples: List[numpy_types] = samples["switchpoint"] vertex_samples_primitive: List[List[primitive_types]] = list(map( lambda a: a.tolist(), vertex_samples)) # because you can't concatenate 0-d arrays vertex_samples_concatentated: np.ndarray = np.array(vertex_samples_primitive) switch_year = np.argmax(np.bincount(vertex_samples_concatentated)) assert switch_year == 1890
def test_probe_for_non_zero_probability_from_bayes_net() -> None: gamma = Gamma(1., 1.) poisson = Poisson(gamma) net = BayesNet([poisson, gamma]) assert not gamma.has_value() assert not poisson.has_value() net.probe_for_non_zero_probability(100, KeanuRandom()) assert gamma.has_value() assert poisson.has_value()
def set_starting_state(model: Model) -> None: KeanuRandom.set_default_random_seed(1) model.temperature.set_value(model.temperature.sample()) model.thermometer_one.set_value(model.thermometer_one.sample()) model.thermometer_two.set_value(model.thermometer_two.sample())
def set_starting_state(model: Model) -> None: KeanuRandom.set_default_random_seed(1) model.uniform.set_value(model.uniform.sample())
def test_default_keanu_random() -> None: keanu_random = KeanuRandom() random = keanu_random.next_double() assert type(random) == float assert 0 <= random < 1
def test_seeded_keanu_random() -> None: keanu_random = KeanuRandom(1) random = keanu_random.next_double() assert type(random) == float assert random == 0.1129943035738381