Пример #1
0
 def est_covariance_mtx(self):
     """
     Returns an estimate of the covariance of the current posterior model
     distribution, given by the covariance of the current SMC approximation.
     
     :rtype: :class:`numpy.ndarray`, shape
         ``(n_modelparams, n_modelparams)``.
     :returns: An array containing the estimated covariance matrix.
     """
     return particle_covariance_mtx(
         self.particle_weights,
         self.particle_locations)
Пример #2
0
    def est_cluster_moments(self, cluster_opts=None):
        # TODO: document

        if cluster_opts is None:
            cluster_opts = {}

        for cluster_label, cluster_particles in particle_clusters(
                self.particle_locations, self.particle_weights,
                **cluster_opts):

            w = self.particle_weights[cluster_particles]
            l = self.particle_locations[cluster_particles]
            yield (
                cluster_label,
                sum(w),  # The zeroth moment is very useful here!
                u.particle_meanfn(w, l, lambda x: x),
                u.particle_covariance_mtx(w, l))
Пример #3
0
    def est_cluster_moments(self, cluster_opts=None):
        # TODO: document

        if cluster_opts is None:
            cluster_opts = {}

        for cluster_label, cluster_particles in particle_clusters(
                self.particle_locations, self.particle_weights,
                **cluster_opts
            ):

            w = self.particle_weights[cluster_particles]
            l = self.particle_locations[cluster_particles]
            yield (
                cluster_label,
                sum(w), # The zeroth moment is very useful here!
                u.particle_meanfn(w, l, lambda x: x),
                u.particle_covariance_mtx(w, l)
            )
Пример #4
0
    def est_cluster_covs(self, cluster_opts=None):
        # TODO: document

        cluster_moments = np.array(
            list(self.est_cluster_moments(cluster_opts=cluster_opts)),
            dtype=[
                ('label', 'int'),
                ('weight', 'float64'),
                ('mean', '{}float64'.format(self.n_rvs)),
                ('cov', '{0},{0}float64'.format(self.n_rvs)),
            ])

        ws = cluster_moments['weight'][:, np.newaxis, np.newaxis]

        within_cluster_var = np.sum(ws * cluster_moments['cov'], axis=0)
        between_cluster_var = u.particle_covariance_mtx(
            # Treat the cluster means as a new very small particle cloud.
            cluster_moments['weight'], cluster_moments['mean']
        )
        total_var = within_cluster_var + between_cluster_var

        return within_cluster_var, between_cluster_var, total_var
Пример #5
0
    def est_cluster_covs(self, cluster_opts=None):
        # TODO: document

        cluster_moments = np.array(
            list(self.est_cluster_moments(cluster_opts=cluster_opts)),
            dtype=[
                ('label', 'int'),
                ('weight', 'float64'),
                ('mean', '{}float64'.format(self.n_rvs)),
                ('cov', '{0},{0}float64'.format(self.n_rvs)),
            ])

        ws = cluster_moments['weight'][:, np.newaxis, np.newaxis]

        within_cluster_var = np.sum(ws * cluster_moments['cov'], axis=0)
        between_cluster_var = u.particle_covariance_mtx(
            # Treat the cluster means as a new very small particle cloud.
            cluster_moments['weight'],
            cluster_moments['mean'])
        total_var = within_cluster_var + between_cluster_var

        return within_cluster_var, between_cluster_var, total_var
Пример #6
0
    def est_covariance_mtx(self, corr=False):
        """
        Returns the full-rank covariance matrix of the current particle
        distribution.

        :param bool corr: If `True`, the covariance matrix is normalized
            by the outer product of the square root diagonal of the covariance matrix,
            i.e. the correlation matrix is returned instead.

        :rtype: :class:`numpy.ndarray`, shape
            ``(n_modelparams, n_modelparams)``.
        :returns: An array containing the estimated covariance matrix.
        """

        cov = u.particle_covariance_mtx(self.particle_weights,
                                        self.particle_locations)

        if corr:
            dstd = np.sqrt(np.diag(cov))
            cov /= (np.outer(dstd, dstd))

        return cov
Пример #7
0
    def est_covariance_mtx(self, corr=False):
        """
        Returns the full-rank covariance matrix of the current particle
        distribution.

        :param bool corr: If `True`, the covariance matrix is normalized
            by the outer product of the square root diagonal of the covariance matrix,
            i.e. the correlation matrix is returned instead.

        :rtype: :class:`numpy.ndarray`, shape
            ``(n_modelparams, n_modelparams)``.
        :returns: An array containing the estimated covariance matrix.
        """

        cov = u.particle_covariance_mtx(
            self.particle_weights,
            self.particle_locations)

        if corr:
            dstd = np.sqrt(np.diag(cov))
            cov /= (np.outer(dstd, dstd))

        return cov
 def calc_covariance_matrix(self):
     '''
         Calculates the covariance matrix
     '''
     return utils.particle_covariance_mtx(self.weights, self.points)