def test_sample_after_set_data(self): with pm.Model() as model: x = pm.MutableData("x", [1.0, 2.0, 3.0]) y = pm.MutableData("y", [1.0, 2.0, 3.0]) beta = pm.Normal("beta", 0, 10.0) pm.Normal("obs", beta * x, np.sqrt(1e-2), observed=y) pm.sample( 1000, tune=1000, chains=1, compute_convergence_checks=False, ) # Predict on new data. new_x = [5.0, 6.0, 9.0] new_y = [5.0, 6.0, 9.0] with model: pm.set_data(new_data={"x": new_x, "y": new_y}) new_idata = pm.sample( 1000, tune=1000, chains=1, compute_convergence_checks=False, ) pp_trace = pm.sample_posterior_predictive(new_idata) assert pp_trace.posterior_predictive["obs"].shape == (1, 1000, 3) np.testing.assert_allclose(new_y, pp_trace.posterior_predictive["obs"].mean( ("chain", "draw")), atol=1e-1)
def test_predictions_constant_data(self): with pm.Model(): x = pm.ConstantData("x", [1.0, 2.0, 3.0]) y = pm.MutableData("y", [1.0, 2.0, 3.0]) beta = pm.Normal("beta", 0, 1) obs = pm.Normal("obs", x * beta, 1, observed=y) # pylint: disable=unused-variable trace = pm.sample(100, tune=100, return_inferencedata=False) inference_data = to_inference_data(trace) test_dict = {"posterior": ["beta"], "observed_data": ["obs"], "constant_data": ["x"]} fails = check_multiple_attrs(test_dict, inference_data) assert not fails with pm.Model(): x = pm.MutableData("x", [1.0, 2.0]) y = pm.ConstantData("y", [1.0, 2.0]) beta = pm.Normal("beta", 0, 1) obs = pm.Normal("obs", x * beta, 1, observed=y) # pylint: disable=unused-variable predictive_trace = pm.sample_posterior_predictive( inference_data, return_inferencedata=False ) assert set(predictive_trace.keys()) == {"obs"} # this should be four chains of 100 samples # assert predictive_trace["obs"].shape == (400, 2) # but the shape seems to vary between pymc versions inference_data = predictions_to_inference_data(predictive_trace, posterior_trace=trace) test_dict = {"posterior": ["beta"], "~observed_data": ""} fails = check_multiple_attrs(test_dict, inference_data) assert not fails, "Posterior data not copied over as expected." test_dict = {"predictions": ["obs"]} fails = check_multiple_attrs(test_dict, inference_data) assert not fails, "Predictions not instantiated as expected." test_dict = {"predictions_constant_data": ["x"]} fails = check_multiple_attrs(test_dict, inference_data) assert not fails, "Predictions constant data not instantiated as expected."
def test_shared_data_as_index(self): """ Allow pm.Data to be used for index variables, i.e with integers as well as floats. See https://github.com/pymc-devs/pymc/issues/3813 """ with pm.Model() as model: index = pm.MutableData("index", [2, 0, 1, 0, 2]) y = pm.MutableData("y", [1.0, 2.0, 3.0, 2.0, 1.0]) alpha = pm.Normal("alpha", 0, 1.5, size=3) pm.Normal("obs", alpha[index], np.sqrt(1e-2), observed=y) prior_trace = pm.sample_prior_predictive(1000) idata = pm.sample( 1000, tune=1000, chains=1, compute_convergence_checks=False, ) # Predict on new data new_index = np.array([0, 1, 2]) new_y = [5.0, 6.0, 9.0] with model: pm.set_data(new_data={"index": new_index, "y": new_y}) pp_trace = pm.sample_posterior_predictive( idata, var_names=["alpha", "obs"]) assert prior_trace.prior["alpha"].shape == (1, 1000, 3) assert idata.posterior["alpha"].shape == (1, 1000, 3) assert pp_trace.posterior_predictive["alpha"].shape == (1, 1000, 3) assert pp_trace.posterior_predictive["obs"].shape == (1, 1000, 3)
def radon_model(): """Similar in shape to the Radon model""" n_homes = 919 counties = 85 uranium = np.random.normal(-0.1, 0.4, size=n_homes) xbar = np.random.normal(1, 0.1, size=n_homes) floor_measure = np.random.randint(0, 2, size=n_homes) d, r = divmod(919, 85) county = np.hstack((np.tile(np.arange(counties, dtype=int), d), np.arange(r))) with pm.Model() as model: sigma_a = pm.HalfCauchy("sigma_a", 5) gamma = pm.Normal("gamma", mu=0.0, sigma=1e5, shape=3) mu_a = pm.Deterministic( "mu_a", gamma[0] + gamma[1] * uranium + gamma[2] * xbar) eps_a = pm.Normal("eps_a", mu=0, sigma=sigma_a, shape=counties) a = pm.Deterministic("a", mu_a + eps_a[county]) b = pm.Normal("b", mu=0.0, sigma=1e15) sigma_y = pm.Uniform("sigma_y", lower=0, upper=100) # Anonymous SharedVariables don't show up floor_measure = aesara.shared(floor_measure) floor_measure_offset = pm.MutableData("floor_measure_offset", 1) y_hat = a + b * floor_measure + floor_measure_offset log_radon = pm.MutableData("log_radon", np.random.normal(1, 1, size=n_homes)) y_like = pm.Normal("y_like", mu=y_hat, sigma=sigma_y, observed=log_radon) compute_graph = { # variable_name : set of named parents in the graph "sigma_a": set(), "gamma": set(), "mu_a": {"gamma"}, "eps_a": {"sigma_a"}, "a": {"mu_a", "eps_a"}, "b": set(), "sigma_y": set(), "y_like": {"a", "b", "sigma_y", "floor_measure_offset"}, "floor_measure_offset": set(), # observed data don't have parents in the model graph, but are shown as decendants # of the model variables that the observations belong to: "log_radon": {"y_like"}, } plates = { "": {"b", "sigma_a", "sigma_y", "floor_measure_offset"}, "3": {"gamma"}, "85": {"eps_a"}, "919": {"a", "mu_a", "y_like", "log_radon"}, } return model, compute_graph, plates
def test_set_data_warns_on_resize_of_dims_defined_by_other_mutabledata(): with pm.Model() as pmodel: pm.MutableData("m1", [1, 2], dims="mutable") pm.MutableData("m2", [3, 4], dims="mutable") # Resizing the non-defining variable first gives a warning with pytest.warns(ShapeWarning, match="by another variable"): pmodel.set_data("m2", [4, 5, 6]) pmodel.set_data("m1", [1, 2, 3]) # Resizing the definint variable first is silent with warnings.catch_warnings(): warnings.simplefilter("error") pmodel.set_data("m1", [1, 2]) pmodel.set_data("m2", [3, 4])
def test_explicit_coords(self): N_rows = 5 N_cols = 7 data = np.random.uniform(size=(N_rows, N_cols)) coords = { "rows": [f"R{r+1}" for r in range(N_rows)], "columns": [f"C{c+1}" for c in range(N_cols)], } # pass coordinates explicitly, use numpy array in Data container with pm.Model(coords=coords) as pmodel: # Dims created from coords are constant by default assert isinstance(pmodel.dim_lengths["rows"], TensorConstant) assert isinstance(pmodel.dim_lengths["columns"], TensorConstant) pm.MutableData("observations", data, dims=("rows", "columns")) # new data with same (!) shape pm.set_data({"observations": data + 1}) # new data with same (!) shape and coords pm.set_data({"observations": data}, coords=coords) assert "rows" in pmodel.coords assert pmodel.coords["rows"] == ("R1", "R2", "R3", "R4", "R5") assert "rows" in pmodel.dim_lengths assert pmodel.dim_lengths["rows"].eval() == 5 assert "columns" in pmodel.coords assert pmodel.coords["columns"] == ("C1", "C2", "C3", "C4", "C5", "C6", "C7") assert pmodel.RV_dims == {"observations": ("rows", "columns")} assert "columns" in pmodel.dim_lengths assert pmodel.dim_lengths["columns"].eval() == 7
def model_with_dims(): with pm.Model( coords={"city": ["Aachen", "Maastricht", "London", "Bergheim"] }) as pmodel: economics = pm.Uniform("economics", lower=-1, upper=1, shape=(1, )) population = pm.HalfNormal("population", sigma=5, dims=("city")) time = pm.ConstantData("time", [2014, 2015, 2016], dims="year") n = pm.Deterministic("tax revenue", economics * population[None, :] * time[:, None], dims=("year", "city")) yobs = pm.MutableData("observed", np.ones((3, 4))) L = pm.Normal("L", n, observed=yobs) compute_graph = { "economics": set(), "population": set(), "time": set(), "tax revenue": {"economics", "population", "time"}, "L": {"tax revenue"}, "observed": {"L"}, } plates = { "1": {"economics"}, "city (4)": {"population"}, "year (3)": {"time"}, "year (3) x city (4)": {"tax revenue"}, "3 x 4": {"L", "observed"}, } return pmodel, compute_graph, plates
def test_sample_posterior_predictive_after_set_data_with_coords(self): y = np.array([1.0, 2.0, 3.0]) with pm.Model() as model: x = pm.MutableData("x", [1.0, 2.0, 3.0], dims="obs_id") beta = pm.Normal("beta", 0, 10.0) pm.Normal("obs", beta * x, np.sqrt(1e-2), observed=y, dims="obs_id") idata = pm.sample( 10, tune=100, chains=1, return_inferencedata=True, compute_convergence_checks=False, ) # Predict on new data. with model: x_test = [5, 6] pm.set_data(new_data={"x": x_test}, coords={"obs_id": ["a", "b"]}) pm.sample_posterior_predictive(idata, extend_inferencedata=True, predictions=True) assert idata.predictions["obs"].shape == (1, 10, 2) assert np.all( idata.predictions["obs_id"].values == np.array(["a", "b"])) np.testing.assert_allclose(x_test, idata.predictions["obs"].mean( ("chain", "draw")), atol=1e-1)
def test_set_data_indirect_resize_with_coords(): with pm.Model() as pmodel: pmodel.add_coord("mdim", ["A", "B"], mutable=True, length=2) pm.MutableData("mdata", [1, 2], dims="mdim") assert pmodel.coords["mdim"] == ("A", "B") # First resize the dimension. pmodel.set_dim("mdim", 3, ["A", "B", "C"]) assert pmodel.coords["mdim"] == ("A", "B", "C") # Then change the data. with warnings.catch_warnings(): warnings.simplefilter("error") pmodel.set_data("mdata", [1, 2, 3]) # Now the other way around. with warnings.catch_warnings(): warnings.simplefilter("error") pmodel.set_data("mdata", [1, 2, 3, 4], coords=dict(mdim=["A", "B", "C", "D"])) assert pmodel.coords["mdim"] == ("A", "B", "C", "D") # This time with incorrectly sized coord values with pytest.raises(ShapeError, match="new coordinate values"): pmodel.set_data("mdata", [1, 2], coords=dict(mdim=[1, 2, 3]))
def test_no_trace(self): with pm.Model() as model: x = pm.ConstantData("x", [1.0, 2.0, 3.0]) y = pm.MutableData("y", [1.0, 2.0, 3.0]) beta = pm.Normal("beta", 0, 1) obs = pm.Normal("obs", x * beta, 1, observed=y) # pylint: disable=unused-variable idata = pm.sample(100, tune=100) prior = pm.sample_prior_predictive(return_inferencedata=False) posterior_predictive = pm.sample_posterior_predictive(idata, return_inferencedata=False) # Only prior inference_data = to_inference_data(prior=prior, model=model) test_dict = {"prior": ["beta"], "prior_predictive": ["obs"]} fails = check_multiple_attrs(test_dict, inference_data) assert not fails # Only posterior_predictive inference_data = to_inference_data(posterior_predictive=posterior_predictive, model=model) test_dict = {"posterior_predictive": ["obs"]} fails = check_multiple_attrs(test_dict, inference_data) assert not fails # Prior and posterior_predictive but no trace inference_data = to_inference_data( prior=prior, posterior_predictive=posterior_predictive, model=model ) test_dict = { "prior": ["beta"], "prior_predictive": ["obs"], "posterior_predictive": ["obs"], } fails = check_multiple_attrs(test_dict, inference_data) assert not fails
def test_sample(self): x = np.random.normal(size=100) y = x + np.random.normal(scale=1e-2, size=100) x_pred = np.linspace(-3, 3, 200, dtype="float32") with pm.Model(): x_shared = pm.MutableData("x_shared", x) b = pm.Normal("b", 0.0, 10.0) pm.Normal("obs", b * x_shared, np.sqrt(1e-2), observed=y) prior_trace0 = pm.sample_prior_predictive(1000) idata = pm.sample(1000, tune=1000, chains=1) pp_trace0 = pm.sample_posterior_predictive(idata) x_shared.set_value(x_pred) prior_trace1 = pm.sample_prior_predictive(1000) pp_trace1 = pm.sample_posterior_predictive(idata) assert prior_trace0.prior["b"].shape == (1, 1000) assert prior_trace0.prior_predictive["obs"].shape == (1, 1000, 100) assert prior_trace1.prior_predictive["obs"].shape == (1, 1000, 200) assert pp_trace0.posterior_predictive["obs"].shape == (1, 1000, 100) np.testing.assert_allclose(x, pp_trace0.posterior_predictive["obs"].mean( ("chain", "draw")), atol=1e-1) assert pp_trace1.posterior_predictive["obs"].shape == (1, 1000, 200) np.testing.assert_allclose(x_pred, pp_trace1.posterior_predictive["obs"].mean( ("chain", "draw")), atol=1e-1)
def test_shapeerror_from_resize_immutable_dim_from_coords(): with pm.Model(coords={"immutable": [1, 2]}) as pmodel: assert isinstance(pmodel.dim_lengths["immutable"], TensorConstant) pm.MutableData("m", [1, 2], dims="immutable") # Data can be changed pmodel.set_data("m", [3, 4]) with pytest.raises(ShapeError, match="`TensorConstant` stores its length"): # But the length is linked to a TensorConstant pmodel.set_data("m", [1, 2, 3], coords=dict(immutable=[1, 2, 3]))
def test_add_coord_mutable_kwarg(): """ Checks resulting tensor type depending on mutable kwarg in add_coord. """ with pm.Model() as m: m.add_coord("fixed", values=[1], mutable=False) m.add_coord("mutable1", values=[1, 2], mutable=True) assert isinstance(m._dim_lengths["fixed"], TensorConstant) assert isinstance(m._dim_lengths["mutable1"], ScalarSharedVariable) pm.MutableData("mdata", np.ones((1, 2, 3)), dims=("fixed", "mutable1", "mutable2")) assert isinstance(m._dim_lengths["mutable2"], TensorVariable)
def test_valueerror_from_resize_without_coords_update(): """ Resizing a mutable dimension that had coords, without passing new coords raises a ValueError. """ with pm.Model() as pmodel: pmodel.add_coord("shared", [1, 2, 3], mutable=True) pm.MutableData("m", [1, 2, 3], dims=("shared")) with pytest.raises(ValueError, match="'m' variable already had 3"): # tries to resize m but without passing coords so raise ValueError pm.set_data({"m": [1, 2, 3, 4]})
def test_data_kwargs(self): strict_value = True allow_downcast_value = False with pm.Model(): data = pm.MutableData( "mdata", value=[[1.0], [2.0], [3.0]], strict=strict_value, allow_downcast=allow_downcast_value, ) assert data.container.strict is strict_value assert data.container.allow_downcast is allow_downcast_value
def test_can_resize_data_defined_size(self): with pm.Model() as pmodel: x = pm.MutableData("x", [[1, 2, 3, 4]], dims=("first", "second")) y = pm.Normal("y", mu=0, dims=("first", "second")) z = pm.Normal("z", mu=y, observed=np.ones((1, 4))) assert x.eval().shape == (1, 4) assert y.eval().shape == (1, 4) assert z.eval().shape == (1, 4) assert "first" in pmodel.dim_lengths assert "second" in pmodel.dim_lengths pmodel.set_data("x", [[1, 2], [3, 4], [5, 6]]) assert x.eval().shape == (3, 2) assert y.eval().shape == (3, 2) assert z.eval().shape == (3, 2)
def test_no_resize_of_implied_dimensions(self): with pm.Model() as pmodel: # Imply a dimension through RV params pm.Normal("n", mu=[1, 2, 3], dims="city") # _Use_ the dimension for a data variable inhabitants = pm.MutableData("inhabitants", [100, 200, 300], dims="city") # Attempting to re-size the dimension through the data variable would # cause shape problems in InferenceData conversion, because the RV remains (3,). with pytest.raises( ShapeError, match= "was initialized from 'n' which is not a shared variable"): pmodel.set_data("inhabitants", [1, 2, 3, 4])
def test_set_coords_through_pmdata(self): with pm.Model() as pmodel: pm.ConstantData("population", [100, 200], dims="city", coords={"city": ["Tinyvil", "Minitown"]}) pm.MutableData( "temperature", [[15, 20, 22, 17], [18, 22, 21, 12]], dims=("city", "season"), coords={"season": ["winter", "spring", "summer", "fall"]}, ) assert "city" in pmodel.coords assert "season" in pmodel.coords assert pmodel.coords["city"] == ("Tinyvil", "Minitown") assert pmodel.coords["season"] == ("winter", "spring", "summer", "fall")
def test_set_data_indirect_resize(): with pm.Model() as pmodel: pmodel.add_coord("mdim", mutable=True, length=2) pm.MutableData("mdata", [1, 2], dims="mdim") # First resize the dimension. pmodel.dim_lengths["mdim"].set_value(3) # Then change the data. with warnings.catch_warnings(): warnings.simplefilter("error") pmodel.set_data("mdata", [1, 2, 3]) # Now the other way around. with warnings.catch_warnings(): warnings.simplefilter("error") pmodel.set_data("mdata", [1, 2, 3, 4])
def test_constant_data(self, use_context): """Test constant_data group behaviour.""" with pm.Model() as model: x = pm.ConstantData("x", [1.0, 2.0, 3.0]) y = pm.MutableData("y", [1.0, 2.0, 3.0]) beta = pm.Normal("beta", 0, 1) obs = pm.Normal("obs", x * beta, 1, observed=y) # pylint: disable=unused-variable trace = pm.sample(100, chains=2, tune=100, return_inferencedata=False) if use_context: inference_data = to_inference_data(trace=trace) if not use_context: inference_data = to_inference_data(trace=trace, model=model) test_dict = {"posterior": ["beta"], "observed_data": ["obs"], "constant_data": ["x"]} fails = check_multiple_attrs(test_dict, inference_data) assert not fails assert inference_data.log_likelihood["obs"].shape == (2, 100, 3)
def test_eval_rv_shapes(self): with pm.Model(coords={ "city": ["Sydney", "Las Vegas", "Düsseldorf"], }) as pmodel: pm.MutableData("budget", [1, 2, 3, 4], dims="year") pm.Normal("untransformed", size=(1, 2)) pm.Uniform("transformed", size=(7, )) obs = pm.Uniform("observed", size=(3, ), observed=[0.1, 0.2, 0.3]) pm.LogNormal("lognorm", mu=at.log(obs)) pm.Normal("from_dims", dims=("city", "year")) shapes = pmodel.eval_rv_shapes() assert shapes["untransformed"] == (1, 2) assert shapes["transformed"] == (7, ) assert shapes["transformed_interval__"] == (7, ) assert shapes["lognorm"] == (3, ) assert shapes["lognorm_log__"] == (3, ) assert shapes["from_dims"] == (3, 4)
def test_shared_data_as_rv_input(self): """ Allow pm.Data to be used as input for other RVs. See https://github.com/pymc-devs/pymc/issues/3842 """ with pm.Model() as m: x = pm.MutableData("x", [1.0, 2.0, 3.0]) y = pm.Normal("y", mu=x, size=(2, 3)) assert y.eval().shape == (2, 3) idata = pm.sample( chains=1, tune=500, draws=550, return_inferencedata=True, compute_convergence_checks=False, ) samples = idata.posterior["y"] assert samples.shape == (1, 550, 2, 3) np.testing.assert_allclose(np.array([1.0, 2.0, 3.0]), x.get_value(), atol=1e-1) np.testing.assert_allclose(np.array([1.0, 2.0, 3.0]), samples.mean(("chain", "draw", "y_dim_0")), atol=1e-1) with m: pm.set_data({"x": np.array([2.0, 4.0, 6.0])}) assert y.eval().shape == (2, 3) idata = pm.sample( chains=1, tune=500, draws=620, return_inferencedata=True, compute_convergence_checks=False, ) samples = idata.posterior["y"] assert samples.shape == (1, 620, 2, 3) np.testing.assert_allclose(np.array([2.0, 4.0, 6.0]), x.get_value(), atol=1e-1) np.testing.assert_allclose(np.array([2.0, 4.0, 6.0]), samples.mean(("chain", "draw", "y_dim_0")), atol=1e-1)
def test_shapeerror_from_resize_immutable_dims(): """ Trying to resize an immutable dimension should raise a ShapeError. Even if the variable being updated is a SharedVariable and has other dimensions that are mutable. """ with pm.Model() as pmodel: a = pm.Normal("a", mu=[1, 2, 3], dims="fixed") m = pm.MutableData("m", [[1, 2, 3]], dims=("one", "fixed")) # This is fine because the "fixed" dim is not resized pm.set_data({"m": [[1, 2, 3], [3, 4, 5]]}) with pytest.raises(ShapeError, match="was initialized from 'a'"): # Can't work because the "fixed" dimension is linked to a constant shape: # Note that the new data tries to change both dimensions with pmodel: pm.set_data({"m": [[1, 2], [3, 4]]})
def test_symbolic_coords(self): """ In v4 dimensions can be created without passing coordinate values. Their lengths are then automatically linked to the corresponding Tensor dimension. """ with pm.Model() as pmodel: intensity = pm.MutableData("intensity", np.ones((2, 3)), dims=("row", "column")) assert "row" in pmodel.dim_lengths assert "column" in pmodel.dim_lengths assert isinstance(pmodel.dim_lengths["row"], TensorVariable) assert isinstance(pmodel.dim_lengths["column"], TensorVariable) assert pmodel.dim_lengths["row"].eval() == 2 assert pmodel.dim_lengths["column"].eval() == 3 intensity.set_value(floatX(np.ones((4, 5)))) assert pmodel.dim_lengths["row"].eval() == 4 assert pmodel.dim_lengths["column"].eval() == 5
def test_priors_separation(self, use_context): """Test model is enough to get prior, prior predictive and observed_data.""" with pm.Model() as model: x = pm.MutableData("x", [1.0, 2.0, 3.0]) y = pm.ConstantData("y", [1.0, 2.0, 3.0]) beta = pm.Normal("beta", 0, 1) obs = pm.Normal("obs", x * beta, 1, observed=y) # pylint: disable=unused-variable prior = pm.sample_prior_predictive(return_inferencedata=False) test_dict = { "prior": ["beta", "~obs"], "observed_data": ["obs"], "prior_predictive": ["obs"], } if use_context: with model: inference_data = to_inference_data(prior=prior) else: inference_data = to_inference_data(prior=prior, model=model) fails = check_multiple_attrs(test_dict, inference_data) assert not fails
def test_model_to_graphviz_for_model_with_data_container(self): with pm.Model() as model: x = pm.ConstantData("x", [1.0, 2.0, 3.0]) y = pm.MutableData("y", [1.0, 2.0, 3.0]) beta = pm.Normal("beta", 0, 10.0) obs_sigma = floatX(np.sqrt(1e-2)) pm.Normal("obs", beta * x, obs_sigma, observed=y) pm.sample( 1000, init=None, tune=1000, chains=1, compute_convergence_checks=False, ) for formatting in {"latex", "latex_with_params"}: with pytest.raises(ValueError, match="Unsupported formatting"): pm.model_to_graphviz(model, formatting=formatting) exp_without = [ 'x [label="x\n~\nConstantData" shape=box style="rounded, filled"]', 'y [label="x\n~\nMutableData" shape=box style="rounded, filled"]', 'beta [label="beta\n~\nNormal"]', 'obs [label="obs\n~\nNormal" style=filled]', ] exp_with = [ 'x [label="x\n~\nConstantData" shape=box style="rounded, filled"]', 'y [label="x\n~\nMutableData" shape=box style="rounded, filled"]', 'beta [label="beta\n~\nNormal(mu=0.0, sigma=10.0)"]', f'obs [label="obs\n~\nNormal(mu=f(f(beta), x), sigma={obs_sigma})" style=filled]', ] for formatting, expected_substrings in [ ("plain", exp_without), ("plain_with_params", exp_with), ]: g = pm.model_to_graphviz(model, formatting=formatting) # check formatting of RV nodes for expected in expected_substrings: assert expected in g.source
def test_sample_posterior_predictive_after_set_data(self): with pm.Model() as model: x = pm.MutableData("x", [1.0, 2.0, 3.0]) y = pm.ConstantData("y", [1.0, 2.0, 3.0]) beta = pm.Normal("beta", 0, 10.0) pm.Normal("obs", beta * x, np.sqrt(1e-2), observed=y) trace = pm.sample( 1000, tune=1000, chains=1, return_inferencedata=False, compute_convergence_checks=False, ) # Predict on new data. with model: x_test = [5, 6, 9] pm.set_data(new_data={"x": x_test}) y_test = pm.sample_posterior_predictive(trace) assert y_test.posterior_predictive["obs"].shape == (1, 1000, 3) np.testing.assert_allclose(x_test, y_test.posterior_predictive["obs"].mean( ("chain", "draw")), atol=1e-1)
def test_gaussianrandomwalk_inference(self): mu, sigma, steps = 2, 1, 1000 obs = np.concatenate([[0], np.random.normal(mu, sigma, size=steps)]).cumsum() with pm.Model(): _mu = pm.Uniform("mu", -10, 10) _sigma = pm.Uniform("sigma", 0, 10) obs_data = pm.MutableData("obs_data", obs) grw = GaussianRandomWalk("grw", _mu, _sigma, steps=steps, observed=obs_data) trace = pm.sample(chains=1) recovered_mu = trace.posterior["mu"].mean() recovered_sigma = trace.posterior["sigma"].mean() np.testing.assert_allclose([mu, sigma], [recovered_mu, recovered_sigma], atol=0.2)
def test_explicit_coords(self): N_rows = 5 N_cols = 7 data = np.random.uniform(size=(N_rows, N_cols)) coords = { "rows": [f"R{r+1}" for r in range(N_rows)], "columns": [f"C{c+1}" for c in range(N_cols)], } # pass coordinates explicitly, use numpy array in Data container with pm.Model(coords=coords) as pmodel: pm.MutableData("observations", data, dims=("rows", "columns")) assert "rows" in pmodel.coords assert pmodel.coords["rows"] == ("R1", "R2", "R3", "R4", "R5") assert "rows" in pmodel.dim_lengths assert isinstance(pmodel.dim_lengths["rows"], ScalarSharedVariable) assert pmodel.dim_lengths["rows"].eval() == 5 assert "columns" in pmodel.coords assert pmodel.coords["columns"] == ("C1", "C2", "C3", "C4", "C5", "C6", "C7") assert pmodel.RV_dims == {"observations": ("rows", "columns")} assert "columns" in pmodel.dim_lengths assert isinstance(pmodel.dim_lengths["columns"], ScalarSharedVariable) assert pmodel.dim_lengths["columns"].eval() == 7
def test_deterministic(self): data_values = np.array([0.5, 0.4, 5, 2]) with pm.Model() as model: X = pm.MutableData("X", data_values) pm.Normal("y", 0, 1, observed=X) model.compile_logp()(model.initial_point())