Пример #1
0
    def mean_theta(self) -> List[np.ndarray]:
        """Mean estimate of model parameters

        Posterior if model is fitted, otherwise prior.

        """
        return utils.listmap(np.array)(utils.unflatten(
            self.theta.get_moments()[0], self.formula.terms))
Пример #2
0
    def theta_marginal(self, i):
        """Extract marginal distribution for a specific term

        """
        mus = utils.unflatten(self.theta.get_moments()[0], self.formula.bases)
        covs = utils.extract_diag_blocks(utils.solve_covariance(self.theta),
                                         self.formula.bases)
        return bp.nodes.Gaussian(mu=mus[i],
                                 Lambda=bp.utils.linalg.inv(covs[i]))
Пример #3
0
    def theta_marginal(self, i: int) -> Gaussian:
        """Extract marginal distribution for a specific term

        """
        u = self.theta.get_moments()
        mus = utils.unflatten(u[0], self.formula.terms)
        covs = utils.extract_diag_blocks(utils.solve_covariance(u),
                                         self.formula.terms)
        return Gaussian(mu=mus[i], Lambda=np.linalg.inv(covs[i]))
Пример #4
0
    def theta_marginals(self):
        """Nodes for the basis specific marginal distributions

        """
        # TODO: Test that the marginal distributions are correct
        mus = utils.unflatten(self.theta.get_moments()[0], self.formula.bases)
        covs = utils.extract_diag_blocks(utils.solve_covariance(self.theta),
                                         self.formula.bases)
        return [
            bp.nodes.Gaussian(mu=mu, Lambda=bp.utils.linalg.inv(cov))
            for mu, cov in zip(mus, covs)
        ]
Пример #5
0
    def theta_marginals(self) -> List[Gaussian]:
        """Marginal distributions of model parameters

        """
        u = self.theta.get_moments()
        mus = utils.unflatten(u[0], self.formula.terms)
        covs = utils.extract_diag_blocks(utils.solve_covariance(u),
                                         self.formula.terms)
        return [
            Gaussian(mu=mu, Lambda=np.linalg.inv(cov))
            for (mu, cov) in zip(mus, covs)
        ]
Пример #6
0
 def mean_theta(self):
     return pipe(self.theta.get_moments()[0],
                 lambda x: utils.unflatten(x, self.formula.bases),
                 listmap(np.array))
Пример #7
0
def test_unflatten(x, y, expected):
    assert utils.unflatten(x, y) == expected
    return