Ejemplo n.º 1
0
    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,
                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
Ejemplo n.º 2
0
    def test_ovewrite_model_coords_dims(self):
        """Check coords and dims from model object can be partially overwritten."""
        dim1 = ["a", "b"]
        new_dim1 = ["c", "d"]
        coords = {"dim1": dim1, "dim2": ["c1", "c2"]}
        x_data = np.arange(4).reshape((2, 2))
        y = x_data + np.random.normal(size=(2, 2))
        with pm.Model(coords=coords):
            x = pm.ConstantData("x", x_data, dims=("dim1", "dim2"))
            beta = pm.Normal("beta", 0, 1, dims="dim1")
            _ = pm.Normal("obs",
                          x * beta,
                          1,
                          observed=y,
                          dims=("dim1", "dim2"))
            trace = pm.sample(100, tune=100, return_inferencedata=False)
            idata1 = to_inference_data(trace)
            idata2 = to_inference_data(trace,
                                       coords={"dim1": new_dim1},
                                       dims={"beta": ["dim2"]})

        test_dict = {
            "posterior": ["beta"],
            "observed_data": ["obs"],
            "constant_data": ["x"]
        }
        fails1 = check_multiple_attrs(test_dict, idata1)
        assert not fails1
        fails2 = check_multiple_attrs(test_dict, idata2)
        assert not fails2
        assert "dim1" in list(idata1.posterior.beta.dims)
        assert "dim2" in list(idata2.posterior.beta.dims)
        assert np.all(idata1.constant_data.x.dim1.values == np.array(dim1))
        assert np.all(
            idata1.constant_data.x.dim2.values == np.array(["c1", "c2"]))
        assert np.all(idata2.constant_data.x.dim1.values == np.array(new_dim1))
        assert np.all(
            idata2.constant_data.x.dim2.values == np.array(["c1", "c2"]))
def Main():
    
    plot_initial_data(data)
    
    # and to combine it with the observations:
    observations = pm.Normal("obs", center_i, tau_i, value=data, observed=True)
    
    # below we create a model class
    model = pm.Model([p, assignment, observations, taus, centers, sigmas])
    
    mcmc = pm.MCMC(model)
    mcmc.sample(50_000)
    
    plot_mms(mcmc)
Ejemplo n.º 4
0
def getModel():
    B = pm.Beta('1-Beta', alpha=1.2, beta=5)
    #@UndefinedVariable
    #     p_B = pm.Lambda('p_Bern', lambda b=B: np.where(b==0, 0.9, 0.1), doc='Pr[Bern|Beta]');
    C = pm.Categorical('2-Cat', [1 - B, B])
    #@UndefinedVariable
    #     C = pm.Categorical('1-Cat', [0.2, 0.4, 0.1, 0.3], observed=True, value=3); #@UndefinedVariable
    p_N = pm.Lambda('p_Norm',
                    lambda n=C: np.where(n == 0, 0, 5),
                    doc='Pr[Norm|Cat]')
    N = pm.Normal('3-Norm', mu=p_N, tau=1)
    #@UndefinedVariable
    #     N = pm.Normal('2-Norm', mu=p_N, tau=1, observed=True, value=2.5); #@UndefinedVariable
    return pm.Model([B, C, N])
Ejemplo n.º 5
0
def run_Bernoulli():
    B = pm.Bernoulli('Bern', 0.4)
    # @UndefinedVariable
    p_N = pm.Lambda('p_Norm',
                    lambda B=B: np.where(B, -5, 5),
                    doc='Pr[Norm|Bern]')
    N = pm.Normal('Norm', mu=p_N, tau=1)
    # @UndefinedVariable
    model = pm.Model([B, N])
    mcmc = pm.MCMC(model)
    mcmc.sample(1000, progress_bar=True)
    print "B:", B.stats()["mean"], B.value
    print "N:", N.stats()["mean"], N.value
    plot_Samples(mcmc)
Ejemplo n.º 6
0
    def test_vector_observed(self):
        with pm.Model() as model:
            mu = pm.Normal("mu", mu=0, sigma=1)
            a = pm.Normal("a", mu=mu, sigma=1, observed=np.array([0.0, 1.0]))
            idata = pm.sample(idata_kwargs={"log_likelihood": False})

        with model:
            # test list input
            # ppc0 = pm.sample_posterior_predictive([model.initial_point], samples=10)
            # TODO: Assert something about the output
            # ppc = pm.sample_posterior_predictive(idata, samples=12, var_names=[])
            # assert len(ppc) == 0
            ppc = pm.sample_posterior_predictive(
                idata, return_inferencedata=False, samples=12, var_names=["a"]
            )
            assert "a" in ppc
            assert ppc["a"].shape == (12, 2)

            ppc = pm.sample_posterior_predictive(
                idata, return_inferencedata=False, samples=10, var_names=["a"], size=4
            )
            assert "a" in ppc
            assert ppc["a"].shape == (10, 4, 2)
Ejemplo n.º 7
0
def test_iterator():
    with pm.Model() as model:
        a = pm.Normal("a", shape=1)
        b = pm.HalfNormal("b")
        step1 = pm.NUTS([model.rvs_to_values[a]])
        step2 = pm.Metropolis([model.rvs_to_values[b]])

    step = pm.CompoundStep([step1, step2])

    start = {"a": floatX(np.array([1.0])), "b_log__": floatX(np.array(2.0))}
    sampler = ps.ParallelSampler(10, 10, 3, 2, [2, 3, 4], [start] * 3, step, 0, False)
    with sampler:
        for draw in sampler:
            pass
Ejemplo n.º 8
0
    def test_named_model(self):
        # Named models used to fail with Simulator because the arguments to the
        # random fn used to be passed by name. This is no longer true.
        # https://github.com/pymc-devs/pymc/pull/4365#issuecomment-761221146
        name = "NamedModel"
        with pm.Model(name=name):
            a = pm.Normal("a", mu=0, sigma=1)
            b = pm.HalfNormal("b", sigma=1)
            s = pm.Simulator("s", self.normal_sim, a, b, observed=self.data)

            trace = pm.sample_smc(draws=10, chains=2, return_inferencedata=False)
            assert f"{name}/a" in trace.varnames
            assert f"{name}/b" in trace.varnames
            assert f"{name}/b_log__" in trace.varnames
Ejemplo n.º 9
0
    def test_missing_data_model(self):
        # source pymc/pymc/tests/test_missing.py
        data = ma.masked_values([1, 2, -1, 4, -1], value=-1)
        model = pm.Model()
        with model:
            x = pm.Normal("x", 1, 1)
            y = pm.Normal("y", x, 1, observed=data)
            inference_data = pm.sample(100, chains=2, return_inferencedata=True)

        # make sure that data is really missing
        assert "y_missing" in model.named_vars

        test_dict = {
            "posterior": ["x", "y_missing"],
            "observed_data": ["y_observed"],
            "log_likelihood": ["y_observed"],
        }
        fails = check_multiple_attrs(test_dict, inference_data)
        assert not fails

        # The missing part of partial observed RVs is not included in log_likelihood
        # See https://github.com/pymc-devs/pymc/issues/5255
        assert inference_data.log_likelihood["y_observed"].shape == (2, 100, 3)
Ejemplo n.º 10
0
def model(x, f):
    # priors uniformes
    pente = pymc.Uniform('pente', -10., 3.)
    amp = pymc.Uniform('amp', 1., 200.)
    bias = pymc.Uniform('bias', 1, 150)

    # foncion logistique à approximer
    @pymc.deterministic(plot=False)
    def lgs(x=x, pente=pente, amp=amp, bias=bias):
        return amp / (1. + np.exp(pente * (x - bias)))

    #print("+++",f_error,f,x)
    y = pymc.Normal('y', mu=lgs, tau=1.0 / f_error**2, value=f, observed=True)
    return locals()
Ejemplo n.º 11
0
 def test_setattr_properly_works(self):
     with pm.Model() as model:
         pm.Normal("v1")
         assert len(model.value_vars) == 1
         with pm.Model("sub") as submodel:
             submodel.register_rv(pm.Normal.dist(), "v1")
             assert hasattr(submodel, "v1")
             assert len(submodel.value_vars) == 1
         assert len(model.value_vars) == 2
         with submodel:
             submodel.register_rv(pm.Normal.dist(), "v2")
             assert hasattr(submodel, "v2")
             assert len(submodel.value_vars) == 2
         assert len(model.value_vars) == 3
Ejemplo n.º 12
0
def model_with_imputations():
    """The example from https://github.com/pymc-devs/pymc/issues/4043"""
    x = np.random.randn(10) + 10.0
    x = np.concatenate([x, [np.nan], [np.nan]])
    x = np.ma.masked_array(x, np.isnan(x))

    with pm.Model() as model:
        a = pm.Normal("a")
        pm.Normal("L", a, 1.0, observed=x)

    compute_graph = {
        "a": set(),
        "L_missing": {"a"},
        "L_observed": {"a"},
        "L": {"L_missing", "L_observed"},
    }
    plates = {
        "": {"a"},
        "2": {"L_missing"},
        "10": {"L_observed"},
        "12": {"L"},
    }
    return model, compute_graph, plates
Ejemplo n.º 13
0
 def _build_prior(self, name, Xs, jitter, **kwargs):
     self.N = int(np.prod([len(X) for X in Xs]))
     mu = self.mean_func(cartesian(*Xs))
     chols = [
         cholesky(stabilize(cov(X), jitter))
         for cov, X in zip(self.cov_funcs, Xs)
     ]
     v = pm.Normal(name + "_rotated_",
                   mu=0.0,
                   sigma=1.0,
                   size=self.N,
                   **kwargs)
     f = pm.Deterministic(name, mu + at.flatten(kron_dot(chols, v)))
     return f
Ejemplo n.º 14
0
    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.Data("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 model(x_obs, y_obs):
    m = pm.Uniform('m', 0, 10, value=0.15)
    n = pm.Uniform('n', -10, 10, value=1.0)
    x_pred = pm.Normal('x_true', mu=x_obs,
                       tau=(x_error)**-2)  # this allows error in x_obs

    #Theoretical values
    @pm.deterministic(plot=False)
    def linearD(x_true=x_pred, m=m, n=n):
        return m * x_true + n

    @pm.deterministic
    def random_operation_on_observable(y_obs=y_obs):
        return y_obs + 0

    #likelihood
    y = pm.Normal('y',
                  mu=linearD,
                  tau=1.0 / y_error**2,
                  value=random_operation_on_observable.value,
                  observed=True)

    return locals()
Ejemplo n.º 16
0
    def test_observed_with_column_vector(self):
        """This test is related to https://github.com/pymc-devs/aesara/issues/390 which breaks
        broadcastability of column-vector RVs. This unexpected change in type can lead to
        incompatibilities during graph rewriting for model.logp evaluation.
        """
        with pm.Model() as model:
            # The `observed` is a broadcastable column vector
            obs = at.as_tensor_variable(
                np.ones((3, 1), dtype=aesara.config.floatX))
            assert obs.broadcastable == (False, True)

            # Both shapes describe broadcastable volumn vectors
            size64 = at.constant([3, 1], dtype="int64")
            # But the second shape is upcasted from an int32 vector
            cast64 = at.cast(at.constant([3, 1], dtype="int32"), dtype="int64")

            pm.Normal("size64", mu=0, sd=1, size=size64, observed=obs)
            pm.Normal("shape64", mu=0, sd=1, shape=size64, observed=obs)
            model.logp()

            pm.Normal("size_cast64", mu=0, sd=1, size=cast64, observed=obs)
            pm.Normal("shape_cast64", mu=0, sd=1, shape=cast64, observed=obs)
            model.logp()
Ejemplo n.º 17
0
def coef_estimate(matrix, params, iters):
    # using specified set of priors, create coefficients
    coefs, term_list = create_coefs(
        params=params,
        priors={coef: pymc.Normal(coef, 0, 0.001)
                for coef in params})

    @pymc.deterministic
    def probs(term_list=term_list):
        probs = 1 / (1 + np.exp(-1 * sum(term_list)))  # The logistic function
        probs[np.diag_indices_from(probs)] = 0
        # Manually cut off the top triangle:
        probs[np.triu_indices_from(probs)] = 0
        return probs

    # Fitting
    matrix[np.triu_indices_from(matrix)] = 0
    max_attempts = 10
    attempts = 0
    while attempts < max_attempts:
        try:
            outcome = pymc.Bernoulli("outcome",
                                     probs,
                                     value=matrix,
                                     observed=True)
            break
        except:
            print("Encountered zero probability error number", attempts,
                  ", trying again...")
            if attempts >= max_attempts:
                raise
            attempts += 1

    sim_outcome = pymc.Bernoulli("sim_outcome", probs)
    args = [outcome, sim_outcome, probs]
    # density_coef, density_term,
    # block_coef, block_term]
    for coef, info in coefs.items():
        # Add both coefficient and term for each coefficient
        for item in info:
            args.append(item)
    model = pymc.Model(args)
    mcmc = pymc.MCMC(model)
    mcmc.sample(iters, 1000, 50)  # approx. 30 seconds

    traces = diagnostics(coefs=coefs, mcmc=mcmc)

    goodness_of_fit(mcmc=mcmc)

    return {coef: np.mean(trace) for coef, trace in traces.items()}
Ejemplo n.º 18
0
    def test_errors_and_warnings(self):
        with pm.Model():
            A = pm.Normal("A")
            B = pm.Uniform("B")
            strace = pm.sampling.NDArray(vars=[A, B])
            strace.setup(10, 0)

            with pytest.raises(ValueError, match="from existing MultiTrace"):
                pm.sampling._choose_backend(trace=MultiTrace([strace]))

            strace.record({"A": 2, "B_interval__": 0.1})
            assert len(strace) == 1
            with pytest.raises(ValueError, match="Continuation of traces"):
                pm.sampling._choose_backend(trace=strace)
Ejemplo n.º 19
0
def simple_normal(bounded_prior=False):
    """Simple normal for testing MLE / MAP; probes issue #2482."""
    x0 = 10.0
    sd = 1.0
    a, b = (9, 12)  # bounds for uniform RV, need non-symmetric to reproduce issue

    with pm.Model(rng_seeder=2482) as model:
        if bounded_prior:
            mu_i = pm.Uniform("mu_i", a, b)
        else:
            mu_i = pm.Flat("mu_i")
        pm.Normal("X_obs", mu=mu_i, sigma=sd, observed=x0)

    return model.initial_point, model, None
Ejemplo n.º 20
0
 def __setup_mu(self):
     """Populates the self.mu list with RVs corresponding to mu param
     of the Logit-Normal distribution, one for each equivalence class.
     """
     # self.mu = pymc.Container([pymc.Uniform('mu_%s' % j,
     #                                     lower = -4,
     #                                     upper = 4,
     #                                     value = 0)
     #                         for j in xrange(0, self.num_equiv)])
     self.mu = pymc.Container([pymc.Normal('mu_%s' % j,
                                           mu=self.mu_star,
                                           tau=1.0 / (self.sigma_star**2),
                                           value=0.6)
                               for j in xrange(0, self.num_equiv)])
Ejemplo n.º 21
0
def test_tempered_logp_dlogp():
    with pm.Model() as model:
        pm.Normal("x")
        pm.Normal("y", observed=1)

    func = model.logp_dlogp_function()
    func.set_extra_values({})

    func_temp = model.logp_dlogp_function(tempered=True)
    func_temp.set_extra_values({})

    func_nograd = model.logp_dlogp_function(compute_grads=False)
    func_nograd.set_extra_values({})

    func_temp_nograd = model.logp_dlogp_function(tempered=True, compute_grads=False)
    func_temp_nograd.set_extra_values({})

    x = np.ones(1, dtype=func.dtype)
    assert func(x) == func_temp(x)
    assert func_nograd(x) == func(x)[0]
    assert func_temp_nograd(x) == func(x)[0]

    func_temp.set_weights(np.array([0.0], dtype=func.dtype))
    func_temp_nograd.set_weights(np.array([0.0], dtype=func.dtype))
    npt.assert_allclose(func(x)[0], 2 * func_temp(x)[0])
    npt.assert_allclose(func(x)[1], func_temp(x)[1])

    npt.assert_allclose(func_nograd(x), func(x)[0])
    npt.assert_allclose(func_temp_nograd(x), func_temp(x)[0])

    func_temp.set_weights(np.array([0.5], dtype=func.dtype))
    func_temp_nograd.set_weights(np.array([0.5], dtype=func.dtype))
    npt.assert_allclose(func(x)[0], 4 / 3 * func_temp(x)[0])
    npt.assert_allclose(func(x)[1], func_temp(x)[1])

    npt.assert_allclose(func_nograd(x), func(x)[0])
    npt.assert_allclose(func_temp_nograd(x), func_temp(x)[0])
Ejemplo n.º 22
0
    def create_nodes_for_normal_normal(self, add_shift, tau_0, mu_0,
                                       sigma_beta, sigma_y, true_mu, n_subjs,
                                       avg_samples):
        """ create the normal normal nodes"""

        mu = pm.Normal('mu', mu_0, tau_0)
        nodes = {'mu': mu}
        size = [None] * n_subjs
        x_values = [None] * n_subjs
        if add_shift:
            b = []
        else:
            b = None
        for i in range(n_subjs):
            size[i] = int(max(1, avg_samples + randn() * 10))
            if add_shift:
                x_values[i] = randn() * sigma_beta
                value = randn(size[i]) * sigma_y + true_mu + x_values[i]
                x = pm.Lambda('x%d' % i, lambda x=x_values[i]: x)
                y = pm.Normal('y%d' % i,
                              mu + x,
                              sigma_y**-2,
                              value=value,
                              observed=True)
                nodes['x%d' % i] = x
                b.append(x)
            else:
                value = randn(size[i]) * sigma_y + true_mu
                y = pm.Normal('y%d' % i,
                              mu,
                              sigma_y**-2,
                              value=value,
                              observed=True)

            nodes['y%d' % i] = y

        return nodes, size, x_values
def linear_setup(df, ind_cols, dep_col):
    '''
    Inputs: pandas Data Frame, list of independent features, outcome var
    Output: PyMC Model
    '''

    # Non-informative priors for parameters- intercept and error
    b0 = pm.Normal('b0', 0, 0.0001)
    err = pm.Normal('err', 0, 0.0001)

    # initialize NumPy arrays for b and x with same size as no of covariates
    b = np.empty(len(ind_cols), dtype=object)
    x = np.empty(len(ind_cols), dtype=object)

    # Non-informative priors for each coefficient
    for i in range(len(b)):
        b[i] = pm.Normal('b' + str(i + 1), 0, 0.0001)

    # Equating x with normal distribution for each data point
    for i, col in enumerate(ind_cols):
        x[i] = pm.Normal('x' + str(i + 1),
                         0,
                         1,
                         value=np.array(df[col]),
                         observed=True)

    # For deterministic equations, need to define the function in this format
    # .dot() for 2D array (i.e., matrix) multiplication since its multi-variable regression
    @pm.deterministic
    def y_pred(b0=b0, b=b, x=x):
        return b0 + b.dot(x)

    # Modeling observed y values
    y = pm.Normal('y', y_pred, err, value=np.array(df[dep_col]), observed=True)

    # Returning the required model
    return pm.Model([b0, pm.Container(b), err, pm.Container(x), y, y_pred])
Ejemplo n.º 24
0
def test_elbo_beta_kl(aux_total_size):
    mu0 = 1.5
    sigma = 1.0
    y_obs = np.array([1.6, 1.4])
    beta = len(y_obs) / float(aux_total_size)

    with aesara.config.change_flags(floatX="float64", warn_float64="ignore"):

        post_mu = np.array([1.88], dtype=aesara.config.floatX)
        post_sigma = np.array([1], dtype=aesara.config.floatX)

        with pm.Model():
            mu = pm.Normal("mu", mu=mu0, sigma=sigma)
            pm.Normal("y", mu=mu, sigma=1, observed=y_obs, total_size=aux_total_size)
            # Create variational gradient tensor
            mean_field_1 = MeanField()
            mean_field_1.scale_cost_to_minibatch = True
            mean_field_1.shared_params["mu"].set_value(post_mu)
            mean_field_1.shared_params["rho"].set_value(np.log(np.exp(post_sigma) - 1))

            with aesara.config.change_flags(compute_test_value="off"):
                elbo_via_total_size_scaled = -pm.operators.KL(mean_field_1)()(10000)

        with pm.Model():
            mu = pm.Normal("mu", mu=mu0, sigma=sigma)
            pm.Normal("y", mu=mu, sigma=1, observed=y_obs)
            # Create variational gradient tensor
            mean_field_3 = MeanField()
            mean_field_3.shared_params["mu"].set_value(post_mu)
            mean_field_3.shared_params["rho"].set_value(np.log(np.exp(post_sigma) - 1))

            with aesara.config.change_flags(compute_test_value="off"):
                elbo_via_beta_kl = -pm.operators.KL(mean_field_3, beta=beta)()(10000)

        np.testing.assert_allclose(
            elbo_via_total_size_scaled.eval(), elbo_via_beta_kl.eval(), rtol=0, atol=1e-1
        )
Ejemplo n.º 25
0
def model_gen():

    varlist = []

    stdev = pymc.TruncatedNormal('stdev',
                                 mu=400,
                                 tau=1.0 / (400**2),
                                 a=0,
                                 b=Inf)
    varlist.append(stdev)

    @pymc.deterministic
    def precision(stdev=stdev):
        return 1.0 / (stdev**2)

    fakeA = pymc.TruncatedNormal('a', mu=1, tau=1.0 / (50**2), a=0, b=Inf)
    b = pymc.Uniform('b', lower=.05, upper=2.0)
    a = fakeA * maxRe**(-b)
    z = pymc.Normal('zero', mu=0, tau=1.0 / (400**2))

    varlist.append(fakeA)
    varlist.append(a)
    varlist.append(b)
    varlist.append(z)

    @pymc.deterministic
    def nonlinear(Re=ReData, value=measured, a=a, b=b, z=z, observed=True):
        return (a * (ReData)**b) + z

    results = pymc.Normal('results',
                          mu=nonlinear,
                          tau=precision,
                          value=measured,
                          observed=True)
    varlist.append(results)

    return varlist
Ejemplo n.º 26
0
def linear_setup(df, ind_cols, dep_col):
    
    '''
    Inputs: pandas Data Frame, list of strings for the independent variables,
    single string for the dependent variable
    Output: PyMC Model
    '''
  
    # model our intercept and error term as above
    #  with pm.Model() as model:
    b0 = pm.Normal('b0', 0, 0.0001)
    err = pm.Uniform('err', 0, 500)
 
    # initialize a NumPy array to hold our betas
    # and our observed x values
    b = np.empty(len(ind_cols), dtype=object)
    x = np.empty(len(ind_cols), dtype=object)
 
    # loop through b, and make our ith beta
    # a normal random variable, as in the single variable case
    for i in range(len(b)):
        b[i] = pm.Normal('b' + str(i + 1), 0, 0.0001)
 
    # loop through x, and inform our model about the observed
    # x values that correspond to the ith position
    for i, col in enumerate(ind_cols):
        x[i] = pm.Normal('b' + str(i + 1), 0, 1, value=np.array(df[col]), observed=True)
 
    # as above, but use .dot() for 2D array (i.e., matrix) multiplication
    @pm.deterministic
    def y_pred(b0=b0, b=b, x=x):
        return b0 + b.dot(x)
 
    # finally, "model" our observed y values as above
    y = pm.Normal('y', y_pred, err, value=np.array(df[dep_col]), observed=True)
 
    return pm.Model([b0, pm.Container(b), err, pm.Container(x), y, y_pred])
Ejemplo n.º 27
0
def main():
    # Code to create artificial data
    N = 100
    X = 0.025 * np.random.randn(N)
    Y = 0.5 * X + 0.01 * np.random.randn(N)

    ls_coef_ = np.cov(X, Y)[0, 1] / np.var(X)
    ls_intercept = Y.mean() - ls_coef_ * X.mean()

    plt.scatter(X, Y, c="k")
    plt.xlabel("trading signal")
    plt.ylabel("returns")
    plt.title("Empirical returns vs trading signal")
    plt.plot(X, ls_coef_ * X + ls_intercept, label="Least-squares line")
    plt.xlim(X.min(), X.max())
    plt.ylim(Y.min(), Y.max())
    plt.legend(loc="upper left")
    plt.show()

    std = pm.Uniform("std", 0, 100, trace=False)

    @pm.deterministic
    def prec(U=std):
        return 1.0 / (U)**2

    beta = pm.Normal("beta", 0, 0.0001)
    alpha = pm.Normal("alpha", 0, 0.0001)

    @pm.deterministic
    def mean(X=X, alpha=alpha, beta=beta):
        return alpha + beta * X

    obs = pm.Normal("obs", mean, prec, value=Y, observed=True)
    mcmc = pm.MCMC([obs, beta, alpha, std, prec])

    mcmc.sample(100000, 80000)
    mcplot(mcmc)
Ejemplo n.º 28
0
    def likelihood_model2(self, nObs, yObs, Slope, Norm, Sig, dx, xp=14, deg=3):

        # (1) Calculate the expected Mass -> MCR
        # (2) Calculate the slope and scatter parameter in -> MOR
        # (3) Calculate Number Count
        # (4) Write the likelihood

        # alpha = 1.0 / Slope # First Order Approximation
        # sigma = Sig / Slope # First Order Approximation
        slope = Slope #pymc.Normal('slope', mu=Slope, tau=100.0, value=Slope, observed=False)
        norm = pymc.Normal('norm', mu=Norm, tau=100.0, value=Norm, observed=False)
        sig = pymc.Normal('sig', mu=Sig, tau=100.0, value=Sig, observed=False)

        # [beta_n, beta_n-1, beta_n-2, ...]
        beta = [pymc.Normal('beta_%i'%i, mu=0., tau=0.0001, value=0.0, observed=False) for i in range(deg+1)]

        @pymc.deterministic(plot=False)
        def mu(yObs=yObs, slope=slope, norm=norm):
            return slope * yObs + norm

        @pymc.deterministic(plot=False)
        def exp_n(beta=beta, mu=mu, deg=deg, slope=slope, sig=sig, dx=dx):
            # It returns the normalization and the first order approximation for the scatter
            # MF = A x exp(beta1 x mu)

            p = np.poly1d(beta)
            A = dx * np.exp(p(mu))

            c = [beta[j] * (deg - j) for j in range(deg)]
            p = np.poly1d(c)
            beta1 = p(mu)

            return A * slope * np.exp(- sig**2 * beta1)

        likelihood = pymc.Poisson('n_obs', mu=exp_n, value=nObs, observed=True)

        return locals()
Ejemplo n.º 29
0
class TestUtils:
    X = np.random.normal(0, 1, size=(2, 50)).T
    Y = np.random.normal(0, 1, size=50)

    with pm.Model() as model:
        mu = pm.BART("mu", X, Y, m=10)
        sigma = pm.HalfNormal("sigma", 1)
        y = pm.Normal("y", mu, sigma, observed=Y)
        idata = pm.sample(random_seed=3415)

    def test_predict(self):
        rng = RandomState(12345)
        pred_all = pm.bart.utils.predict(self.idata, rng, size=2)
        rng = RandomState(12345)
        pred_first = pm.bart.utils.predict(self.idata, rng, X_new=self.X[:10])

        assert_almost_equal(pred_first, pred_all[0, :10], decimal=4)
        assert pred_all.shape == (2, 50)
        assert pred_first.shape == (10, )

    @pytest.mark.parametrize(
        "kwargs",
        [
            {},
            {
                "kind": "pdp",
                "samples": 2,
                "xs_interval": "quantiles",
                "xs_values": [0.25, 0.5, 0.75],
            },
            {
                "kind": "ice",
                "instances": 2
            },
            {
                "var_idx": [0],
                "rug": False,
                "smooth": False,
                "color": "k"
            },
            {
                "grid": (1, 2),
                "sharey": "none",
                "alpha": 1
            },
        ],
    )
    def test_pdp(self, kwargs):
        pm.bart.utils.plot_dependence(self.idata, X=self.X, Y=self.Y, **kwargs)
Ejemplo n.º 30
0
 def stoCCD(c_exp, ccd_priors):
     # Stochastic variables (noise on CCDtools output)
     # The only assumption we make is that the RTD noise is below 10%
     noise_rho = pymc.Uniform('noise_rho', lower=-0.1, upper=0.1)
     noise_tau = pymc.Uniform('log_noise_tau', lower=-0.1, upper=0.1)
     noise_m = pymc.Uniform('log_noise_m', lower=-0.1, upper=0.1)
     
     # Deterministic variables of CCD
     @pymc.deterministic(plot=False) 
     def log_m_i(logm=ccd_priors['log_m'], dm=noise_m):
         # Chargeability array
         return logm + dm
     @pymc.deterministic(plot=False) 
     def log_tau_i(logt=ccd_priors['log_tau'], dt=noise_tau):
         # Tau logarithmic array
         return logt + dt
     @pymc.deterministic(plot=False) 
     def R0(R=ccd_priors['R0'], dR=noise_rho):
         # DC resistivity (normalized)
         return R + dR
     @pymc.deterministic(plot=False) 
     def cond(log_tau = log_tau_i):
         # Condition on log_tau to compute integrating parameters
         return (log_tau >= min(log_tau)+1)&(log_tau <= max(log_tau)-1)
     @pymc.deterministic(plot=False)
     def total_m(m=10**log_m_i[cond]):
         # Total chargeability
         return np.sum(m)
     @pymc.deterministic(plot=False)
     def log_half_tau(m_i=10**log_m_i[cond], log_tau=log_tau_i[cond]):
         # Tau 50
         return log_tau[np.where(np.cumsum(m_i)/np.sum(m_i) > 0.5)[0][0]]
     @pymc.deterministic(plot=False)
     def log_peak_tau(m_i=log_m_i, log_tau=log_tau_i):
         # Tau peaks
         peak_cond = np.r_[True, m_i[1:] > m_i[:-1]] & np.r_[m_i[:-1] > m_i[1:], True]
         return log_tau[peak_cond]
     @pymc.deterministic(plot=False)
     def log_mean_tau(m_i=10**log_m_i[cond], log_tau=log_tau_i[cond]):
         # Tau logarithmic average 
         return np.log10(np.exp(old_div(np.sum(m_i*np.log(10**log_tau)),np.sum(m_i))))
     @pymc.deterministic(plot=False)
     def zmod(R0=R0, m=10**log_m_i, tau=10**log_tau_i):
         Z = R0 * (1 - np.sum(m*(1 - 1.0/(1 + ((1j*w[:,np.newaxis]*tau)**c_exp))), axis=1))
         return np.array([Z.real, Z.imag])
     
     # Likelihood function
     obs = pymc.Normal('obs', mu=zmod, tau=1./(2*self.data["zn_err"]**2), value=self.data["zn"], size = (2, len(w)), observed=True)
     return locals()