def test_rvs_backend_arithmetic(tf_seed): """Test backend arithmetic implemented by the `WithBackendArithmetic` class.""" x = pm.Normal("NormA", mu=0, sigma=1) y = pm.Normal("NormB", mu=1, sigma=2) assert x + y is not None assert x - y is not None assert x * y is not None # TODO test __matmul__ once random variables support shapes. # assert x @ y is not None assert x / y is not None assert x // y is not None assert x % y is not None assert x**y is not None assert -x is not None
def test_wrong_distribution_argument_in_list_fails(): with pytest.raises(TypeError, match=r"every element in 'distribution' "): pm.Mixture( "mix", p=[0.5, 0.5], distributions=[pm.Normal("comp1", loc=0.0, scale=1.0), "not a distribution"], )
def model_with_yields_and_other_assigns(): N = 10 N += 10 x = yield pm.Normal(loc=0, scale=1) s: str = "A test string." y = yield pm.HalfCauchy(scale=2) a, b, c = (1, 2, 3) z = yield pm.Binomial(total_count=10, probs=0.2)
def model(): x = yield pm.Normal( "x", np.zeros(n_obs, dtype="float32"), 1, observed=np.zeros(n_obs, dtype="float32"), conditionally_independent=True, reinterpreted_batch_ndims=1, ) beta = yield pm.Normal("beta", 0, 1, conditionally_independent=True) bias = yield pm.Normal("bias", 0, 1, conditionally_independent=True) mu = beta[..., None] * x + bias[..., None] yield pm.Normal("obs", mu, 1, observed=np.ones(n_obs, dtype="float32"), reinterpreted_batch_ndims=1)
def model(): mu = yield pm.Normal( "mu", tf.zeros(4), 1, conditionally_independent=True, reinterpreted_batch_ndims=1, ) scale = yield pm.HalfNormal("scale", 1, conditionally_independent=True) x = yield pm.Normal( "x", mu, scale[..., None], observed=observed, reinterpreted_batch_ndims=1, event_stack=5, )
def model(): beta = yield pm.Normal( "beta", tf.zeros((n_features, )), 1, conditionally_independent=True, reinterpreted_batch_ndims=1, ) bias = yield pm.Normal("bias", 0, 1, conditionally_independent=True) scale = yield pm.HalfNormal("scale", 1, conditionally_independent=True) mu = tf.linalg.matvec(regressors, beta) + bias[..., None] y = yield pm.Normal( "y", mu, scale[..., None], observed=observed, reinterpreted_batch_ndims=1, )
def model(): yield pm.Normal("x", 0, 1)
def test_as_sampling_state_failure_on_dangling_distribution(): st = pm.flow.executor.SamplingState( distributions={"bla": pm.Normal("bla", 0, 1)}) with pytest.raises(TypeError): st.as_sampling_state()
def test_initialized_distribution_cant_be_transformed_into_a_new_prior(): with pytest.raises(TypeError) as e: pm.Normal("m", 0, 1).prior("n") assert e.match("already not anonymous")
def unvectorized_model(): norm = yield pm.Normal("norm", 0, 1, batch_stack=norm_shape) determ = yield pm.Deterministic("determ", tf.reduce_max(norm)) output = yield pm.Normal("output", determ, 1, observed=observed)
def model(df): """Create I_0 with shape: number_of_countries*number_of_age_groups """ batch_shape = len(countries) * len(age_groups) I_0 = yield pm.HalfCauchy(loc=[10] * batch_shape, name="I_0") """ Create Contact number matrix with LKJ prior and shape: [ number_of_age_groups x number_of_age_groups ] x number_of_countries """ batch_shape = len(countries) C = yield pm.LKJ( dimension=len(age_groups), concentration=[2] * batch_shape, # eta name="Contact_matrix", ) """ Create Reproduction number matrix, should be diagonal matrix shape: number_of_age_groups x number_of_age_groups """ batch_shape = len(age_groups) # *time? # Create RV with shape number_of_age_groups and convert to diag matrix R = yield pm.Normal(loc=[2] * batch_shape, scale=2.5, name="R_age_groups") R_diag = tf.linalg.diag(R) """ Create generation interval RV see function documentation for more informations """ g = covid19_npis.model._construct_generation_interval_gamma() """ Get RV for new_cases from SIR model it should have the shape: (data_end-data_begin).days+fcast x number_of_countries*number_of_age_groups """ new_cases = covid19_npis.model.NewCasesModel(I_0=I_0, R=R_diag, g=g) # TODO """ Delay new cases via convolution """ # 1. Delay RV delay = yield pm.Normal(loc=4, scale=3, name="D", batch_shape=1) # TODO shape # 2. Do convolution https://www.tensorflow.org/probability/api_docs/python/tfp/experimental/nn/Convolution new_cases_delayed = yield tfp.experimental.nn.Convolution() # TODO # supprisingly df.to_numpy() gives us the right numpy array i.e. with shape [time,countries*age_groups] likelihood = pm.NegativeBinomial(new_cases, observed=df.to_numpy())
def model(): a = yield pm.Normal("a", 0, 1) b = yield pm.HalfNormal("b", 1) c = yield pm.Normal("c", loc=a, scale=b, plate=len(observed_value))
def model(): x = yield pm.Normal("x", 0, 1) det = yield pm.Deterministic("det", x) y = yield pm.Normal("det", det, 1) return y
def model(): x = yield pm.Normal("x", tf.ones(5), 1, reinterpreted_batch_ndims=1) y = yield pm.Normal("y", x, 1)
def model(): beta = yield pm.Normal("beta", tf.zeros((n_features, )), 1) bias = yield pm.Normal("bias", 0, 1) scale = yield pm.HalfNormal("scale", 1) mu = tf.linalg.matvec(regressors, beta) + bias y = yield pm.Normal("y", mu, scale, observed=observed)
def model_with_only_good_yields(): x = yield pm.Normal(loc=0, scale=1) y = yield pm.HalfCauchy(scale=2) z = yield pm.Binomial(total_count=10, probs=0.2)
def model_with_yield_tuple(): x, y = yield pm.Normal(loc=0, scale=1), pm.HalfCauchy(scale=2)
def model(): x = yield pm.Normal("x", 0, 1) yield pm.Deterministic(None, x)
def model(): x = yield pm.Normal("x", 0, 1) det = yield pm.Deterministic("x", x) return det
def model(): a = yield pm.Normal("a", 0, 1, conditionally_independent=True) b = yield pm.Normal("b", a, 1)
def model(observed): a = yield pm.Normal("a", 0, [1, 2], observed=observed)
def model(): mu = yield pm.Normal("mu", prior_mean, prior_sigma) ll = yield pm.Normal("ll", mu, known_sigma, observed=data)
def create_rvs(**kwargs): # AST parsers should just add name kwargs to pymc4 RVs assert "name" not in kwargs.keys() return pm.Normal(0, 1, name="inside_function")
def invdalid_model(): yield pm.HalfNormal("n", 1, transform=pm.distributions.transforms.Log()) yield pm.Normal("n", 0, 1)
def simple_model(): norm = yield pm.Normal("norm", 0, 1) return norm
def model(): x = yield pm.Normal("x", tf.ones(5), 1) y = yield pm.Normal("y", x, 1)
def simple_model_no_free_rvs(): norm = yield pm.Normal("norm", 0, 1, observed=1) return norm
def model(): sd = yield pm.HalfNormal("sd", 1.0) mu = yield pm.Deterministic("mu", tf.convert_to_tensor(1.0)) x = yield pm.Normal("x", mu, sd, observed=observed) y = yield pm.Normal("y", x, 1e-9) dy = yield pm.Deterministic("dy", 2 * y)
def nested_model(cond): x = yield pm.Normal("x", cond, 1) dx = yield pm.Deterministic("dx", x + 1) return dx
def class_model_method(self): norm = yield pm.Normal("n", 0, 1) return norm