Пример #1
0
    def marginal_likelihood(self,
                            name,
                            X,
                            y,
                            noise,
                            jitter=0.0,
                            is_observed=True,
                            **kwargs):
        R"""
        Returns the marginal likelihood distribution, given the input
        locations `X` and the data `y`.

        This is integral over the product of the GP prior and a normal likelihood.

        .. math::

           y \mid X,\theta \sim \int p(y \mid f,\, X,\, \theta) \, p(f \mid X,\, \theta) \, df

        Parameters
        ----------
        name: string
            Name of the random variable
        X: array-like
            Function input values.  If one-dimensional, must be a column
            vector with shape `(n, 1)`.
        y: array-like
            Data that is the sum of the function with the GP prior and Gaussian
            noise.  Must have shape `(n, )`.
        noise: scalar, Variable, or Covariance
            Standard deviation of the Gaussian noise.  Can also be a Covariance for
            non-white noise.
        jitter: scalar
            A small correction added to the diagonal of positive semi-definite
            covariance matrices to ensure numerical stability.  Default value is 0.0.
        **kwargs
            Extra keyword arguments that are passed to `MvNormal` distribution
            constructor.
        """

        if not isinstance(noise, Covariance):
            noise = pm.gp.cov.WhiteNoise(noise)
        mu, cov = self._build_marginal_likelihood(X, noise, jitter)
        self.X = X
        self.y = y
        self.noise = noise
        if is_observed:
            return pm.MvNormal(name, mu=mu, cov=cov, observed=y, **kwargs)
        else:
            warnings.warn(
                "The 'is_observed' argument has been deprecated.  If the GP is "
                "unobserved use gp.Latent instead.",
                FutureWarning,
            )
            return pm.MvNormal(name, mu=mu, cov=cov, **kwargs)
Пример #2
0
    def conditional(self, name, Xnew, **kwargs):
        """
        Returns the conditional distribution evaluated over new input
        locations `Xnew`.

        `Xnew` will be split by columns and fed to the relevant
        covariance functions based on their `input_dim`. For example, if
        `cov_func1`, `cov_func2`, and `cov_func3` have `input_dim` of 2,
        1, and 4, respectively, then `Xnew` must have 7 columns and a
        covariance between the prediction points

        .. code:: python

            cov_func(Xnew) = cov_func1(Xnew[:, :2]) * cov_func1(Xnew[:, 2:3]) * cov_func1(Xnew[:, 3:])

        The distribution returned by `conditional` does not have a
        Kronecker structure regardless of whether the input points lie
        on a full grid.  Therefore, `Xnew` does not need to have grid
        structure.

        Parameters
        ----------
        name: string
            Name of the random variable
        Xnew: array-like
            Function input values.  If one-dimensional, must be a column
            vector with shape `(n, 1)`.
        **kwargs
            Extra keyword arguments that are passed to `MvNormal` distribution
            constructor.
        """
        mu, cov = self._build_conditional(Xnew)
        shape = infer_shape(Xnew, kwargs.pop("shape", None))
        return pm.MvNormal(name, mu=mu, cov=cov, size=shape, **kwargs)
Пример #3
0
    def conditional(self, name, Xnew, pred_noise=False, given=None, **kwargs):
        R"""
        Returns the approximate conditional distribution of the GP evaluated over
        new input locations `Xnew`.

        Parameters
        ----------
        name: string
            Name of the random variable
        Xnew: array-like
            Function input values.  If one-dimensional, must be a column
            vector with shape `(n, 1)`.
        pred_noise: bool
            Whether or not observation noise is included in the conditional.
            Default is `False`.
        given: dict
            Can optionally take as key value pairs: `X`, `Xu`, `y`, `noise`,
            and `gp`.  See the section in the documentation on additive GP
            models in PyMC for more information.
        **kwargs
            Extra keyword arguments that are passed to `MvNormal` distribution
            constructor.
        """

        givens = self._get_given_vals(given)
        mu, cov = self._build_conditional(Xnew, pred_noise, False, *givens)
        shape = infer_shape(Xnew, kwargs.pop("shape", None))
        return pm.MvNormal(name, mu=mu, cov=cov, size=shape, **kwargs)
Пример #4
0
    def test_simultaneous_size_and_dims(self, with_dims_ellipsis):
        with pm.Model() as pmodel:
            x = pm.ConstantData("x", [1, 2, 3], dims="ddata")
            assert "ddata" in pmodel.dim_lengths

            # Size does not include support dims, so this test must use a dist with support dims.
            kwargs = dict(name="y", size=2, mu=at.ones((3, 4)), cov=at.eye(4))
            if with_dims_ellipsis:
                y = pm.MvNormal(**kwargs, dims=("dsize", ...))
                assert pmodel.RV_dims["y"] == ("dsize", None, None)
            else:
                y = pm.MvNormal(**kwargs, dims=("dsize", "ddata", "dsupport"))
                assert pmodel.RV_dims["y"] == ("dsize", "ddata", "dsupport")

            assert "dsize" in pmodel.dim_lengths
            assert y.eval().shape == (2, 3, 4)
Пример #5
0
    def conditional(self, name, Xnew, given=None, **kwargs):
        R"""
        Returns the conditional distribution evaluated over new input
        locations `Xnew`.

        Given a set of function values `f` that
        the GP prior was over, the conditional distribution over a
        set of new points, `f_*` is

        .. math::

           f_* \mid f, X, X_* \sim \mathcal{GP}\left(
               K(X_*, X) K(X, X)^{-1} f \,,
               K(X_*, X_*) - K(X_*, X) K(X, X)^{-1} K(X, X_*) \right)

        Parameters
        ----------
        name: string
            Name of the random variable
        Xnew: array-like
            Function input values.
        given: dict
            Can optionally take as key value pairs: `X`, `y`, `noise`,
            and `gp`.  See the section in the documentation on additive GP
            models in PyMC for more information.
        **kwargs
            Extra keyword arguments that are passed to `MvNormal` distribution
            constructor.
        """
        givens = self._get_given_vals(given)
        mu, cov = self._build_conditional(Xnew, *givens)
        shape = infer_shape(Xnew, kwargs.pop("shape", None))
        return pm.MvNormal(name, mu=mu, cov=cov, size=shape, **kwargs)
Пример #6
0
def model():
    # Priors
    sigma_y = pymc.Uniform('sigma_y', lower=0, upper=100)
    tau_y = pymc.Lambda('tau_y', lambda s=sigma_y: s**-2)

    xi = pymc.Uniform('xi', lower=0, upper=100, value=np.zeros(K))

    mu_raw = pymc.Normal('mu_raw', mu=0., tau=0.0001, value=np.zeros(K))
    Tau_B_raw = pymc.Wishart('Tau_B_raw', df, Tau=np.diag(np.ones(K)))
    B_raw = pymc.MvNormal('B_raw', mu_raw, Tau_B_raw, value=np.zeros((J, K)))

    # Model
    @pymc.deterministic(plot=True)
    def B(xi=xi, B_raw=B_raw):
        return xi * B_raw

    @pymc.deterministic
    def mu(xi=xi, mu_raw=mu_raw):
        return xi * mu_raw

    @pymc.deterministic(plot=False)
    def y_hat(B=B, X=X, i=index_c):
        return np.sum(B[i, ] * X, axis=1)

    # Likelihood
    @pymc.stochastic(observed=True)
    def y_i(value=y, mu=y_hat, tau=tau_y):
        return pymc.normal_like(value, mu, tau)

    return vars()
Пример #7
0
    def conditional(self, name, Xnew, pred_noise=False, given=None, jitter=0.0, **kwargs):
        R"""
        Returns the approximate conditional distribution of the GP evaluated over
        new input locations `Xnew`.

        Parameters
        ----------
        name: string
            Name of the random variable
        Xnew: array-like
            Function input values.  If one-dimensional, must be a column
            vector with shape `(n, 1)`.
        pred_noise: bool
            Whether or not observation noise is included in the conditional.
            Default is `False`.
        given: dict
            Can optionally take as key value pairs: `X`, `Xu`, `y`, `noise`,
            and `gp`.  See the section in the documentation on additive GP
            models in PyMC for more information.
        jitter: scalar
            A small correction added to the diagonal of positive semi-definite
            covariance matrices to ensure numerical stability.  For conditionals
            the default value is 0.0.
        **kwargs
            Extra keyword arguments that are passed to `MvNormal` distribution
            constructor.
        """

        givens = self._get_given_vals(given)
        mu, cov = self._build_conditional(Xnew, pred_noise, False, *givens, jitter)
        return pm.MvNormal(name, mu=mu, cov=cov, **kwargs)
Пример #8
0
    def test_mv_missing_data_model(self):
        data = ma.masked_values([[1, 2], [2, 2], [-1, 4], [2, -1], [-1, -1]],
                                value=-1)

        model = pm.Model()
        with model:
            mu = pm.Normal("mu", 0, 1, size=2)
            sd_dist = pm.HalfNormal.dist(1.0)
            chol, *_ = pm.LKJCholeskyCov("chol_cov",
                                         n=2,
                                         eta=1,
                                         sd_dist=sd_dist,
                                         compute_corr=True)
            y = pm.MvNormal("y", mu=mu, chol=chol, observed=data)
            inference_data = pm.sample(100,
                                       chains=2,
                                       return_inferencedata=True)

        # make sure that data is really missing
        assert isinstance(y.owner.op,
                          (AdvancedIncSubtensor, AdvancedIncSubtensor1))

        test_dict = {
            "posterior": ["mu", "chol_cov"],
            "observed_data": ["y"],
            "log_likelihood": ["y"],
        }
        fails = check_multiple_attrs(test_dict, inference_data)
        assert not fails
Пример #9
0
def test_get_batched_jittered_initial_points():
    with pm.Model() as model:
        x = pm.MvNormal("x",
                        mu=np.zeros(3),
                        cov=np.eye(3),
                        shape=(2, 3),
                        initval=np.zeros((2, 3)))

    # No jitter
    ips = _get_batched_jittered_initial_points(model=model,
                                               chains=1,
                                               random_seed=1,
                                               initvals=None,
                                               jitter=False)
    assert np.all(ips[0] == 0)

    # Single chain
    ips = _get_batched_jittered_initial_points(model=model,
                                               chains=1,
                                               random_seed=1,
                                               initvals=None)

    assert ips[0].shape == (2, 3)
    assert np.all(ips[0] != 0)

    # Multiple chains
    ips = _get_batched_jittered_initial_points(model=model,
                                               chains=2,
                                               random_seed=1,
                                               initvals=None)

    assert ips[0].shape == (2, 2, 3)
    assert np.all(ips[0][0] != ips[0][1])
Пример #10
0
def mv_prior_simple():
    n = 3
    noise = 0.1
    X = np.linspace(0, 1, n)[:, None]

    K = pm.gp.cov.ExpQuad(1, 1)(X).eval()
    L = np.linalg.cholesky(K)
    K_noise = K + noise * np.eye(n)
    obs = floatX_array([-0.1, 0.5, 1.1])

    # Posterior mean
    L_noise = np.linalg.cholesky(K_noise)
    alpha = np.linalg.solve(L_noise.T, np.linalg.solve(L_noise, obs))
    mu_post = np.dot(K.T, alpha)

    # Posterior standard deviation
    v = np.linalg.solve(L_noise, K)
    std_post = (K - np.dot(v.T, v)).diagonal()**0.5

    with pm.Model() as model:
        x = pm.Flat("x", size=n)
        x_obs = pm.MvNormal("x_obs", observed=obs, mu=x, cov=noise * np.eye(n))

    return model.compute_initial_point(), model, (K, L, mu_post, std_post,
                                                  noise)
Пример #11
0
 def _build_prior(self, name, X, reparameterize=True, jitter=JITTER_DEFAULT, **kwargs):
     mu = self.mean_func(X)
     cov = stabilize(self.cov_func(X), jitter)
     if reparameterize:
         size = infer_size(X, kwargs.pop("size", None))
         v = pm.Normal(name + "_rotated_", mu=0.0, sigma=1.0, size=size, **kwargs)
         f = pm.Deterministic(name, mu + cholesky(cov).dot(v))
     else:
         f = pm.MvNormal(name, mu=mu, cov=cov, **kwargs)
     return f
Пример #12
0
    def marginal_likelihood(self, name, X, y, noise, is_observed=True, **kwargs):
        R"""
        Returns the marginal likelihood distribution, given the input
        locations `X` and the data `y`.

        This is integral over the product of the GP prior and a normal likelihood.

        .. math::

           y \mid X,\theta \sim \int p(y \mid f,\, X,\, \theta) \, p(f \mid X,\, \theta) \, df

        Parameters
        ----------
        name: string
            Name of the random variable
        X: array-like
            Function input values.  If one-dimensional, must be a column
            vector with shape `(n, 1)`.
        y: array-like
            Data that is the sum of the function with the GP prior and Gaussian
            noise.  Must have shape `(n, )`.
        noise: scalar, Variable, or Covariance
            Standard deviation of the Gaussian noise.  Can also be a Covariance for
            non-white noise.
        is_observed: bool
            Whether to set `y` as an `observed` variable in the `model`.
            Default is `True`.
        **kwargs
            Extra keyword arguments that are passed to `MvNormal` distribution
            constructor.
        """

        if not isinstance(noise, Covariance):
            noise = pm.gp.cov.WhiteNoise(noise)
        mu, cov = self._build_marginal_likelihood(X, noise)
        self.X = X
        self.y = y
        self.noise = noise
        if is_observed:
            return pm.MvNormal(name, mu=mu, cov=cov, observed=y, **kwargs)
        else:
            shape = infer_shape(X, kwargs.pop("shape", None))
            return pm.MvNormal(name, mu=mu, cov=cov, size=shape, **kwargs)
Пример #13
0
def test_square():
    iw = pymc.Wishart("A", 2, np.eye(2))
    mnc = pymc.MvNormal(
        "v",
        np.zeros(2),
        iw,
        value=np.zeros(2),
        observed=True)

    M = pymc.MCMC([iw, mnc])
    M.sample(8, progress_bar=0)
Пример #14
0
def getModel():
    C = pm.Categorical('1-Cat', [0.2, 0.4, 0.1, 0.3])
    #@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.select([n == 0, n == 1, n == 2, n == 3],
                              [[-5, -5], [0, 0], [5, 5], [10, 10]]),
        doc='Pr[Norm|Cat]')
    N = pm.MvNormal('2-Norm_2D', mu=p_N, tau=np.eye(2, 2))
    #@UndefinedVariable
    #     N = pm.MvNormal('2-Norm', mu=p_N, tau=np.eye(2,2), observed=True, value=[2.5,2.5]); #@UndefinedVariable
    return pm.Model([C, N])
Пример #15
0
def mv_simple():
    mu = floatX_array([-0.1, 0.5, 1.1])
    p = floatX_array([[2.0, 0, 0], [0.05, 0.1, 0], [1.0, -0.05, 5.5]])
    tau = np.dot(p, p.T)
    with pm.Model() as model:
        pm.MvNormal(
            "x",
            at.constant(mu),
            tau=at.constant(tau),
            initval=floatX_array([0.1, 1.0, 0.8]),
        )
    H = tau
    C = np.linalg.inv(H)
    return model.compute_initial_point(), model, (mu, C)
Пример #16
0
def getModel():
    D = pm.Dirichlet('1-Dirichlet', theta=[2, 1, 2, 4])
    #@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', D)
    #@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.select([n == 0, n == 1, n == 2, n == 3],
                              [[-5, -5], [0, 0], [5, 5], [10, 10]]),
        doc='Pr[Norm|Cat]')
    N = pm.MvNormal('3-Norm_2D', mu=p_N, tau=np.eye(2, 2))
    #@UndefinedVariable
    #     N = pm.MvNormal('2-Norm_2D', mu=p_N, tau=np.eye(2,2), observed=True, value=[2.5,2.5]); #@UndefinedVariable
    return pm.Model([D, C, N])
Пример #17
0
    def conditional(self,
                    name,
                    Xnew,
                    pred_noise=False,
                    given=None,
                    jitter=0.0,
                    **kwargs):
        R"""
        Returns the conditional distribution evaluated over new input
        locations `Xnew`.

        Given a set of function values `f` that the GP prior was over, the
        conditional distribution over a set of new points, `f_*` is:

        .. math::

           f_* \mid f, X, X_* \sim \mathcal{GP}\left(
               K(X_*, X) [K(X, X) + K_{n}(X, X)]^{-1} f \,,
               K(X_*, X_*) - K(X_*, X) [K(X, X) + K_{n}(X, X)]^{-1} K(X, X_*) \right)

        Parameters
        ----------
        name: string
            Name of the random variable
        Xnew: array-like
            Function input values.  If one-dimensional, must be a column
            vector with shape `(n, 1)`.
        pred_noise: bool
            Whether or not observation noise is included in the conditional.
            Default is `False`.
        given: dict
            Can optionally take as key value pairs: `X`, `y`, `noise`,
            and `gp`.  See the section in the documentation on additive GP
            models in PyMC for more information.
        jitter: scalar
            A small correction added to the diagonal of positive semi-definite
            covariance matrices to ensure numerical stability.  For conditionals
            the default value is 0.0.
        **kwargs
            Extra keyword arguments that are passed to `MvNormal` distribution
            constructor.
        """

        givens = self._get_given_vals(given)
        mu, cov = self._build_conditional(Xnew, pred_noise, False, *givens,
                                          jitter)
        return pm.MvNormal(name, mu=mu, cov=cov, **kwargs)
Пример #18
0
def mv_simple():
    mu = np.array([-.1, .5, 1.1])
    p = np.array([[2., 0, 0], [.05, .1, 0], [1., -0.05, 5.5]])

    tau = np.dot(p, p.T)

    with pm.Model() as model:
        x = pm.MvNormal('x',
                        pm.constant(mu),
                        pm.constant(tau),
                        shape=3,
                        testval=np.array([.1, 1., .8]))

    H = tau
    C = np.linalg.inv(H)

    return model.test_point, model, (mu, C)
Пример #19
0
    def __init__(self,
                 predictions,
                 measurements,
                 uncertainties,
                 regularization_strength=1.0,
                 precision=None,
                 prior_pops=None):
        """Bayesian Energy Landscape Tilting with MultiVariate Normal Prior.

        Parameters
        ----------
        predictions : ndarray, shape = (num_frames, num_measurements)
            predictions[j, i] gives the ith observabled predicted at frame j
        measurements : ndarray, shape = (num_measurements)
            measurements[i] gives the ith experimental measurement
        uncertainties : ndarray, shape = (num_measurements)
            uncertainties[i] gives the uncertainty of the ith experiment
        regularization_strength : float
            How strongly to weight the MVN prior (e.g. lambda)
        precision : ndarray, optional, shape = (num_measurements, num_measurements)
            The precision matrix of the predicted observables.
        prior_pops : ndarray, optional, shape = (num_frames)
            Prior populations of each conformation.  If None, use uniform populations.
        """
        BELT.__init__(self,
                      predictions,
                      measurements,
                      uncertainties,
                      prior_pops=prior_pops)

        if precision == None:
            precision = np.cov(predictions.T)
            if precision.ndim == 0:
                precision = precision.reshape((1, 1))

        self.alpha = pymc.MvNormal("alpha",
                                   np.zeros(self.num_measurements),
                                   tau=precision * regularization_strength)
        self.initialize_variables()
Пример #20
0
def test_full_adapt_sampling(seed=289586):
    np.random.seed(seed)

    L = np.random.randn(5, 5)
    L[np.diag_indices_from(L)] = np.exp(L[np.diag_indices_from(L)])
    L[np.triu_indices_from(L, 1)] = 0.0

    with pymc.Model() as model:
        pymc.MvNormal("a", mu=np.zeros(len(L)), chol=L, size=len(L))

        initial_point = model.recompute_initial_point()
        initial_point_size = sum(initial_point[n.name].size
                                 for n in model.value_vars)

        pot = quadpotential.QuadPotentialFullAdapt(
            initial_point_size, np.zeros(initial_point_size))
        step = pymc.NUTS(model=model, potential=pot)
        pymc.sample(draws=10,
                    tune=1000,
                    random_seed=seed,
                    step=step,
                    cores=1,
                    chains=1)
Пример #21
0
    def conditional(self, name, Xnew, jitter=0.0, **kwargs):
        """
        Returns the conditional distribution evaluated over new input
        locations `Xnew`.

        `Xnew` will be split by columns and fed to the relevant
        covariance functions based on their `input_dim`. For example, if
        `cov_func1`, `cov_func2`, and `cov_func3` have `input_dim` of 2,
        1, and 4, respectively, then `Xnew` must have 7 columns and a
        covariance between the prediction points

        .. code:: python

            cov_func(Xnew) = cov_func1(Xnew[:, :2]) * cov_func1(Xnew[:, 2:3]) * cov_func1(Xnew[:, 3:])

        The distribution returned by `conditional` does not have a
        Kronecker structure regardless of whether the input points lie
        on a full grid.  Therefore, `Xnew` does not need to have grid
        structure.

        Parameters
        ----------
        name: string
            Name of the random variable
        Xnew: array-like
            Function input values.  If one-dimensional, must be a column
            vector with shape `(n, 1)`.
        jitter: scalar
            A small correction added to the diagonal of positive semi-definite
            covariance matrices to ensure numerical stability.  For conditionals
            the default value is 0.0.
        **kwargs
            Extra keyword arguments that are passed to `MvNormal` distribution
            constructor.
        """
        mu, cov = self._build_conditional(Xnew, jitter)
        return pm.MvNormal(name, mu=mu, cov=cov, **kwargs)
Пример #22
0
xmin = np.min(xy_meas[:, 0]) * 1.1
ymin = np.min(xy_meas[:, 1]) * 1.1
xmax = np.max(xy_meas[:, 0]) * 1.1
ymax = np.max(xy_meas[:, 1]) * 1.1

# x_nodes = np.array([pymc.Uniform('x_nodes_%i'%i, lower=xmin, upper=xmax) for i in range(Nnodes)])
# y_nodes = np.array([pymc.Uniform('y_nodes_%i'%i, lower=ymin, upper=ymax) for i in range(Nnodes)])

xy_nodes = np.empty(Nnodes, dtype=object)
for i in range(Nnodes):
    mu = XYnodes_heuristic[i, 0:2]
    Sigma = np.empty((2, 2))
    Sigma[0, :] = XYnodes_heuristic[i, 2:]
    Sigma[1, :] = Sigma[0, ::-1]  #symmetric
    Tau = np.linalg.inv(Sigma)
    xy_nodes[i] = pymc.MvNormal('xy_nodes_%i' % i, mu=mu, tau=Tau)

# @pymc.deterministic(plot=False)
# def xy_points(o=state_origin, d=state_dest, f=frac, x_n = x_nodes, y_n = y_nodes):
#     x_n_s = x_n #np.sort(x_n)
#     y_n_s = y_n #np.sort(y_n)
#     x = (x_n_s[d]-x_n_s[o])*f + x_n_s[o]
#     y = (y_n_s[d]-y_n_s[o])*f + y_n_s[o]
#     out = np.column_stack([x, y])
#     return out

Prows = np.empty(Nnodes, dtype=object)
for i in range(Nnodes):
    t = np.ones(Nnodes) * 10
    t[i] = 0.5
    Prows[i] = pymc.Dirichlet('Dir_%i' % i, theta=t)
Пример #23
0
        root = sqrt(Omega_Lambda + Omega_M * (1 + x)**3 +
                    (1 - Omega_Lambda - Omega_M) * (1 + x)**2)
        result = 1 / root
        return result

    for i in range(len(xdata)):
        z = xdata[i]
        D_L = ((2998 / h) * (1 + z)) * quad(Formula, 0, z)[0]
        mu_b[i] = 25 + 5 * log10(abs(D_L))
    return mu_b


# (We define the error as 2 std)
T = np.linalg.inv(
    my_cov)  # tau = precision matrix = inverse of covariance matrix
y = pymc.MvNormal('y', mu=y_model, tau=T, observed=True, value=ydata)
# package the full model in a dictionary
model1 = dict(h=h, Omega_Lambda=Omega_Lambda, Omega_M=Omega_M, y=y)
# run the basic MCMC:
S = pymc.MCMC(model1)
S.sample(iter=100000, burn=80000)
# extract the traces and plot the results
pymc_trace_unifo = [
    S.trace('h')[:],
    S.trace('Omega_Lambda')[:],
    S.trace('Omega_M')[:]
]

plot_MCMC_results(xdata, ydata, pymc_trace_unifo)
print()
print("h mean = {:.4f}".format(pymc_trace_unifo[0].mean()))
Пример #24
0
import pandas as pd
import pymc as pm

d = pd.read_csv("data/gp.csv")
d.shape

D = np.array([ np.abs(xi - d.x) for xi in d.x])
I = (D == 0).astype("double")

with pm.Model() as gp:
  nugget = pm.HalfCauchy("nugget", beta=5)
  sigma2 = pm.HalfCauchy("sigma2", beta=5)
  ls     = pm.HalfCauchy("ls",     beta=5)

  Sigma = I * nugget + sigma2 * np.exp(-0.5 * D**2 * ls**2)
  
  y = pm.MvNormal(
    "y", 
    mu=np.zeros(d.shape[0]), 
    cov=Sigma, observed=d.y
  )

with gp:
    post_nuts = pm.sample(
        return_inferencedata = True,
        chains = 2
    )
Пример #25
0
    def __init__(self, F, G, V, W, m_0, C_0, Y_vals = None):
        """
        D = DLM(F, G, V, W, m_0, C_0[, Y_vals])

        Returns special NormalSubmodel instance representing the dynamic
        linear model formed by F, G, V and W.

        Resulting probability model:

            theta[0] | m_0, C_0 ~ N(m_0, C_0)

            theta[t] | theta[t-1], G[t], W[t] ~ N(G[t] theta[t-1], W[t]), t = 1..T

            Y[t] | theta[t], F[t], V[t] ~ N(F[t] theta[t], V[t]), t = 0..T


        Arguments F, G, V should be dictionaries keyed by name of component.
            F[comp], G[comp], V[comp] should be lists.
                F[comp][t] should be the design vector of component 'comp' at time t.
                G[comp][t] should be the system matrix.

        Argument W should be either a number between 0 and 1 or a dictionary of lists
        like V.
            If a dictionary of lists, W[comp][t] should be the system covariance or
            variance at time t.
            If a scalar, W should be the discount factor for the DLM.

        Arguments V and Y_vals, if given, should be lists.
            V[t] should be the observation covariance or variance at time t.
            Y_vals[t] should give the value of output Y at time t.

        Arguments m_0 and C_0 should be dictionaries keyed by name of component.
            m_0[comp] should be the mean of theta[comp][0].
            C_0[comp] should be the covariance or variance of theta[comp][0].

        Note: if multiple components are correlated in W or V, they should be made into
        a single component.

        D.comp is a handle to a list.
            D.comp[t] is a Stochastic representing the value of system state 'theta'
            sliced according to component 'comp' at time t.

        D.theta is a dictionary of lists analogous to F, G, V and W.

        D.Y is a list. D.Y[t] is a Stochastic representing the value of the output
        'Y' at time t.
        """

        self.comps = F.keys()

        self.F = dict_to_recarray(F)
        self.G = dict_to_recarray(G)
        self.V = pymc.ListContainer(V)
        if np.isscalar(W):
            self.discount = True
            self.delta = W
        else:
            self.W = dict_to_recarray(W)
            self.discount = False
            self.delta = None
        if self.discount:
            raise NotImplemented, "Have yet to code up the discount factor."
        self.m_0 = dict_to_recarray(m_0)
        self.C_0 = dict_to_recarray(C_0)
        self.T = len(self.V)

        theta = {}
        theta_mean = {}

        Y_mean = []
        Y = []

        # ==============
        # = Make theta =
        # ==============
        for comp in self.comps:
            # Is diagonal the covariance or variance?
            if isinstance(self.W[comp][0], pymc.Variable):
                diag = isvector(self.W[comp][0].value)
            else:
                diag = isvector(self.W[comp][0])

            if diag:
                # Normal variates if diagonal.
                theta[comp] = [pymc.Normal('%s[0]'%comp, m_0[comp], C_0[comp])]
            else:
                # MV normal otherwise.
                theta[comp] = [pymc.MvNormal('%s[0]'%comp, m_0[comp], C_0[comp])]

            theta_mean[comp] = []

            for t in xrange(1,self.T):

                theta_mean[comp].append(pymc.LinearCombination('%s_mean[%i]'%(comp, t), [G[comp][t-1]], [theta[comp][t-1]]))

                if diag:
                    # Normal variates if diagonal.
                    theta[comp].append(pymc.Normal('%s[%i]'%(comp,t), theta_mean[comp][t-1], W[comp][t-1]))
                else:
                    # MV normal otherwise.
                    theta[comp].append(pymc.MvNormal('%s[%i]'%(comp,t), theta_mean[comp][t-1], W[comp][t-1]))


        self.theta = dict_to_recarray(theta)
        self.theta_mean = dict_to_recarray(theta_mean)


        # ==========
        # = Make Y =
        # ==========
        Y_diag = isvector(self.V.value[0])

        for t in xrange(self.T):
            x_coef = []
            y_coef = []

            for comp in self.comps:
                x_coef.append(self.F[comp][t])
                y_coef.append(theta[comp][t])

            Y_mean.append(pymc.LinearCombination('Y_mean[%i]'%t, x_coef, y_coef))
            if Y_diag:
                # Normal variates if diagonal.
                Y.append(pymc.Normal('Y[%i]'%t, Y_mean[t], V[t]))
            else:
                # MV normal otherwise.
                Y.append(pymc.MvNormal('Y[%i]'%t, Y_mean[t], V[t]))

            # If data provided, use it.
            if Y_vals is not None:
                Y[t].value = Y_vals[t]
                Y[t].observed = True

        self.Y_mean = pymc.Container(np.array(Y_mean))
        self.Y = pymc.Container(np.array(Y))

        # No sense creating a NormalSubmodel here... just stay a ListContainer.
        NormalSubmodel.__init__(self, [F,G,W,V,m_0,C_0,Y,theta,theta_mean,Y_mean])
Пример #26
0
        ans[i] = counties_dict[counties[i]]
    return ans

index_c = createCountyIndex(counties)

# Priors
sigma_y = pymc.Uniform('sigma_y', lower=0, upper=100)
tau_y = pymc.Lambda('tau_y', lambda s=sigma_y: s**-2)

xi = pymc.Uniform('xi', lower=0, upper=100, value=np.zeros(K))
mu_raw = pymc.Normal('mu_raw', mu=0., tau=0.0001,value=np.zeros(K))
Tau_B_raw = pymc.Wishart('Tau_B_raw', df, Tau=np.diag(np.ones(K)))
B_raw_m = np.ones( (J,K) )
B_raw = []
for i in range(J):
    B_raw.append(pymc.MvNormal('B_raw_%i' % i, mu_raw, Tau_B_raw))

@pymc.deterministic
def Sigma_B_raw(Tau_B_raw=Tau_B_raw):
    return np.linalg.inv(Tau_B_raw)

@pymc.deterministic
def rho_B(Sigma_B_raw=Sigma_B_raw):
    return Sigma_B_raw / np.sqrt(np.diag(Sigma_B_raw) * Sigma_B_raw)

@pymc.deterministic
def Sigma_B(xi=xi,Sigma_B_raw=Sigma_B_raw):
    return abs(xi) * np.sqrt(np.diag(Sigma_B_raw))

@pymc.deterministic(plot=True)
def B(xi=xi, B_raw=B_raw, B_raw_m=B_raw_m):
Пример #27
0
import pymc
import numpy as np
import logging

log = logging.getLogger('emissions')

log_2_pi = np.log(2 * np.pi)
invwishart = lambda nu, L: pymc.InverseWishart("invwishart", nu, L).random()
mvnormal = lambda mu, tau: pymc.MvNormal('mvnormal', mu, tau).random()
#invwishart_like = lambda x, nu, L: pymc.inverse_wishart_like(x,nu,L)


class Gaussian:
    def __init__(self, nu, Lambda, mu_0, kappa, mu, tau):
        self.nu = nu
        self.Lambda = Lambda
        self.mu_0 = mu_0  # mu_0 is the mean of the prior on the mean
        self.kappa = float(kappa)

        self.mu = mu  # mu is the current value of the mean for each state
        self.tau = tau  # tau is the current precision matrix for each state

        self.states = range(len(mu))
        self.K = len(self.states)

    def likelihood(self, state, obs):
        assert state in self.states, (state, self.states)
        return pymc.mv_normal_like(obs, self.mu[state], self.tau[state])

    def sample_obs(self, state):
        assert state in self.states, (state, self.states)
Пример #28
0
import utils
import pymc
from params import *  # noqa


if __name__ == '__main__':

    # load a dataset
    dataset = utils.load_dataset(DATASET_PATH)
    observed_xs = dataset[:, 0]
    observed_ys = dataset[:, 1]

    # plot_ground_truth()

    # define a prior for w, which is a multivariable gaussian
    ws = pymc.MvNormal('ws', mu=np.zeros(M), tau=ALPHA * np.eye(M), value=np.zeros(M))

    # calculate x^0, x^1, ..., x^{M-1}
    xs = np.empty((M, len(observed_xs)), dtype=np.float32)
    for i in range(M):
        xs[i] = np.power(observed_xs, i)
    assert(xs.shape == (M, len(observed_xs)))

    # define a deterministic function
    linear_regression = pymc.Lambda('linear_regression', lambda xs=xs, ws=ws: ws.dot(xs))

    # define a model likelihood
    y = pymc.Normal('y', mu=linear_regression, tau=TAU, value=observed_ys, observed=True)

    # make a model
    model = pymc.Model([y, ws])
Пример #29
0
# priors, fairly vague
prior_mean = data.mean(0)
sigma0 = np.diag([1., 1.])
prior_cov = np.cov(data.T)

# shared hyperparameter?
# theta_tau = pm.Wishart('theta_tau', n=4, Tau=L.inv(sigma0))

# df = pm.DiscreteUniform('df', 3, 50)

thetas = []
taus = []
for j in range(ncomps):
    # need a hyperparameter for degrees of freedom?
    tau = pm.Wishart('C_%d' % j, n=3, Tau=inv(prior_cov))
    theta = pm.MvNormal('theta_%d' % j, mu=prior_mean, tau=inv(2 * prior_cov))

    thetas.append(theta)
    taus.append(tau)

alpha0 = np.ones(3.) / 3
weights = pm.Dirichlet('weights', theta=alpha0)
# labels = pm.Categorical('labels', p=weights, size=len(data))

from pandas.util.testing import set_trace as st
import pdfs
import util


def mixture_loglike(data, thetas, covs, labels):