def net() -> BayesNet: with Model() as m: m.gamma = Gamma(1., 1.) m.exp = Exponential(1.) m.cauchy = Cauchy(m.gamma, m.exp) return m.to_bayes_net()
def model() -> Model: with Model() as m: m.temperature = Uniform(0., 100.) m.thermometer_one = Gaussian(m.temperature, 1.0) m.thermometer_two = Gaussian(m.temperature, 1.0) return m
def model() -> Model: 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 tensor_net() -> BayesNet: with Model() as m: m.gamma = Gamma( np.array([1., 1., 1., 1.]).reshape((2, 2)), np.array([2., 2., 2., 2.]).reshape((2, 2))) m.exp = Exponential(np.array([1., 1., 1., 1.]).reshape((2, 2))) m.add = m.gamma + m.exp return m.to_bayes_net()
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_autocorr_returns_ndarray_of_correct_dtype() -> None: with Model() as m: m.uniform = Uniform(0, 1000) net = m.to_bayes_net() samples = sample(net=net, sample_from=net.iter_latent_vertices(), draws=10) valid_key = list(samples.keys())[0] sample_ = samples.get(valid_key) assert sample_ is not None autocorr = stats.autocorrelation(sample_) assert type(autocorr) == np.ndarray
def test_to_bayes_net_excludes_non_vertices() -> None: with Model() as m: m.not_a_vertex = 1 m.vertex = Gamma(0.5, 0.1) net = m.to_bayes_net() assert isinstance(net, BayesNet) net_vertex_ids = [vertex.get_id() for vertex in net.iter_latent_or_observed_vertices()] assert len(net_vertex_ids) == 1 assert m.vertex.get_id() in net_vertex_ids
def autocorrelation_example_nd(): with Model() as m: m.a = Gaussian(np.array([[20., 30.], [40., 60.]]), np.array([[1., 1.], [1., 1.]])) bayes_net = m.to_bayes_net() # %%SNIPPET_START%% PythonNdAutocorrelation algo = MetropolisHastingsSampler() posterior_samples = sample(net=bayes_net, sample_from=bayes_net.get_latent_vertices(), sampling_algorithm=algo, draws=100) vertex_samples = posterior_samples.get('a') ac = stats.autocorrelation(vertex_samples, (0, 1))
def model(self) -> Model: start_year, end_year = (self._data.index.min(), self._data.index.max()) with Model() as m: m.switchpoint = UniformInt(start_year, end_year + 1) m.early_rate = Exponential(1.0) m.late_rate = Exponential(1.0) m.years = np.array(self._data.index) m.rates = If(m.switchpoint > m.years, m.early_rate, m.late_rate) m.disasters = Poisson(m.rates) return m
def test_thermometers_example(): # %%SNIPPET_START%% PythonTwoThermometers with Model() as m: m.temperature = Uniform(20., 30.) m.first_thermometer = Gaussian(m.temperature, 2.5) m.second_thermometer = Gaussian(m.temperature, 5.) m.first_thermometer.observe(25.) m.second_thermometer.observe(30.) bayes_net = m.to_bayes_net() optimizer = GradientOptimizer(bayes_net) optimizer.max_a_posteriori() calculated_temperature = m.temperature.get_value()
def autocorrelation_example_scalar(): with Model() as m: m.a = Gaussian(20, 1.) m.b = Gaussian(20, 1.) m.c = Gaussian(m.a + m.b, 1.) m.c.observe(43.) m.a.set_value(20.) m.b.set_value(20.) bayes_net = m.to_bayes_net() # %%SNIPPET_START%% PythonScalarAutocorrelation algo = MetropolisHastingsSampler() posterior_samples = sample(net=bayes_net, sample_from=bayes_net.get_latent_vertices(), sampling_algorithm=algo, draws=100) vertex_samples = posterior_samples.get('a') ac = stats.autocorrelation(vertex_samples)
def test_autocorrelation_same_for_streaming_as_batch() -> None: with Model() as model: model.uniform = Uniform(0, 1000) net = model.to_bayes_net() draws = 15 set_starting_state(model) samples = sample(net=net, sample_from=net.get_latent_vertices(), algo="metropolis", draws=draws) set_starting_state(model) iter_samples = generate_samples(net=net, sample_from=net.get_latent_vertices(), algo="metropolis") samples_dataframe = pd.DataFrame() for next_sample in islice(iter_samples, draws): samples_dataframe = samples_dataframe.append(next_sample, ignore_index=True) for vertex_id in samples_dataframe: autocorr_streaming = stats.autocorrelation(list(samples_dataframe[vertex_id].values)) autocorr_batch = stats.autocorrelation(samples[vertex_id]) np.testing.assert_array_equal(autocorr_batch, autocorr_streaming)
def inference_example_metropolis(): # %%SNIPPET_START%% PythonMetropolisExample with Model() as m: m.a = Gaussian(20., 1.) m.b = Gaussian(20., 1.) m.c = Gaussian(m.a + m.b, 1.) m.c.observe(43.) m.a.set_value(20.) m.b.set_value(20.) bayes_net = m.to_bayes_net() posterior_samples = sample(net=bayes_net, sample_from=bayes_net.get_latent_vertices(), algo="metropolis", draws=100000) average_posterior_a = np.average(posterior_samples.get('a')) average_posterior_b = np.average(posterior_samples.get('b')) actual = average_posterior_a + average_posterior_b
def test_to_bayes_net() -> None: with Model() as m: m.mu = Exponential(1.) m.sigma = Gamma(0.5, 0.1) m.gaussian = Gaussian(m.mu, m.sigma) net = m.to_bayes_net() assert isinstance(net, BayesNet) net_vertex_ids = [ vertex.get_id() for vertex in net.get_latent_or_observed_vertices() ] assert len(net_vertex_ids) == 3 assert m.mu.get_id() in net_vertex_ids assert m.sigma.get_id() in net_vertex_ids assert m.gaussian.get_id() in net_vertex_ids
def inference_example_hmc_nuts(): with Model() as m: m.a = Gaussian(20., 1.) m.b = Gaussian(20., 1.) m.c = Gaussian(m.a + m.b, 1.) m.c.observe(43.) m.a.set_value(20.) m.b.set_value(20.) bayes_net = m.to_bayes_net() # %%SNIPPET_START%% PythonHamiltonianExample posterior_samples = sample(net=bayes_net, sample_from=bayes_net.get_latent_vertices(), algo="hamiltonian", draws=2000) # %%SNIPPET_END%% PythonHamiltonianExample # %%SNIPPET_START%% PythonNUTSExample posterior_samples = sample(net=bayes_net, sample_from=bayes_net.get_latent_vertices(), algo="NUTS", draws=2000)
def net() -> BayesNet: with Model() as m: m.gamma = Gamma(1., 1.) m.gaussian = Gaussian(0., m.gamma) return m.to_bayes_net()
def build_model(): with Model() as m: m.temperature = Uniform(20., 30.) m.first_thermometer = Gaussian(m.temperature, 2.5) m.second_thermometer = Gaussian(m.temperature, 5.) return m