Esempio n. 1
0
def elemwise_logp(model, var):
    terms = []
    for v in model.basic_RVs:
        v_logp = logpt(v)
        if var in graph_inputs([v_logp]):
            terms.append(v_logp)
    return model.fn(at.add(*terms))
Esempio n. 2
0
    def check_vectortransform_elementwise_logp(self, model, vect_opt=0):
        x = model.free_RVs[0]
        x0 = x.tag.value_var
        assert (x.ndim - 1) == logpt(x).ndim

        pt = model.initial_point
        array = np.random.randn(*pt[x0.name].shape)
        transform = x0.tag.transform
        logp_nojac = logpt(x, transform.backward(x, array), transformed=False)

        jacob_det = transform.jacobian_det(x, aesara.shared(array))
        assert logpt(x).ndim == jacob_det.ndim

        # Hack to get relative tolerance
        a = logpt(x, array.astype(aesara.config.floatX), jacobian=False).eval()
        b = logp_nojac.eval()
        close_to(a, b, np.abs(0.5 * (a + b) * tol))
Esempio n. 3
0
    def check_transform_elementwise_logp(self, model):
        x = model.free_RVs[0]
        x0 = x.tag.value_var
        assert x.ndim == logpt(x).ndim

        pt = model.initial_point
        array = np.random.randn(*pt[x0.name].shape)
        transform = x0.tag.transform
        logp_notrans = logpt(x,
                             transform.backward(x, array),
                             transformed=False)

        jacob_det = transform.jacobian_det(x, aesara.shared(array))
        assert logpt(x).ndim == jacob_det.ndim

        v1 = logpt(x, array, jacobian=False).eval()
        v2 = logp_notrans.eval()
        close_to(v1, v2, tol)
Esempio n. 4
0
    def test_shared_scalar_as_rv_input(self):
        # See https://github.com/pymc-devs/pymc3/issues/3139
        with pm.Model() as m:
            shared_var = shared(5.0)
            v = pm.Normal("v", mu=shared_var, size=1)

        np.testing.assert_allclose(
            logpt(v, np.r_[5.0]).eval(),
            -0.91893853,
            rtol=1e-5,
        )

        shared_var.set_value(10.0)

        np.testing.assert_allclose(
            logpt(v, np.r_[10.0]).eval(),
            -0.91893853,
            rtol=1e-5,
        )
Esempio n. 5
0
    def _extract_log_likelihood(self, trace):
        """Compute log likelihood of each observation."""
        if self.trace is None:
            return None
        if self.model is None:
            return None

        if self.log_likelihood is True:
            cached = [(var, self.model.fn(logpt(var)))
                      for var in self.model.observed_RVs]
        else:
            cached = [(var, self.model.fn(logpt(var)))
                      for var in self.model.observed_RVs
                      if var.name in self.log_likelihood]
        log_likelihood_dict = _DefaultTrace(len(trace.chains))
        for var, log_like_fun in cached:
            for k, chain in enumerate(trace.chains):
                log_like_chain = [
                    self.log_likelihood_vals_point(point, var, log_like_fun)
                    for point in trace.points([chain])
                ]
                log_likelihood_dict.insert(var.name, np.stack(log_like_chain),
                                           k)
        return log_likelihood_dict.trace_dict