Exemplo n.º 1
0
 def rvs(self, size):
     # if self.df == np.inf:
     #     x = 1.
     # else:
     #     x = np.random.chisquare(self.df, size)/self.df
     # z = np.random.multivariate_normal(np.zeros(self.d), self.cov, (size,))
     # return self.loc + z/np.sqrt(x)[:,None]
     if isinstance(self.loc, list):
         assert size in self.loc.shape, "If loc is a list of vectors, it should have same length with size."
         return [stats.multivariate_t(df=self.df, loc=mu, shape=self.cov).rvs(size=1) for mu in self.loc]
     else:
         return stats.multivariate_t(df=self.df, loc=self.loc, shape=self.cov).rvs(size=size)
Exemplo n.º 2
0
    def simulate_data(self, p_sample, n_obs):
        """
        Returns a dataset given a sample from the prior.
        """

        D = p_sample.shape[0] // 2
        mu, sd = p_sample[:D], p_sample[D:]
        x = stats.multivariate_t(loc=mu, shape=np.diag(sd),
                                 df=self.df).rvs(n_obs)
        return x
Exemplo n.º 3
0
 def get_trajectories(self,
                      J,
                      H_grid,
                      parameter,
                      distr_name: str,
                      log_hubs=False,
                      quantile=float('nan'),
                      matrix='hard'):
     multivariate = False
     if distr_name == 'no_distr':
         distribution = sps.norm(loc=0, scale=0)
     elif distr_name == 'norm':
         distribution = sps.norm(loc=0, scale=parameter)
     elif distr_name == 'student':
         distribution = sps.t(df=parameter)
     elif distr_name == 'multivariate_norm':
         multivariate = True
         if matrix == 'simple':
             cov_matrix = (1 - parameter) * np.eye(
                 self.N) + parameter * np.ones((self.N, self.N))
             distribution = sps.multivariate_normal(cov=cov_matrix)
         else:
             distribution = sps.multivariate_normal(
                 cov=self.__get_correlation_matrix(parameter))
     elif distr_name == 'multivariate_student':
         multivariate = True
         distribution = sps.multivariate_t(
             df=3.5, shape=3 / 7 * self.__get_correlation_matrix(parameter))
     else:
         raise NotImplementedError('Unknown distribution')
     fractions_low_to_high, fractions_high_to_low = [], []
     self.sample_fields(distribution, multivariate)
     self.set_state(-np.ones(self.N))
     if not log_hubs:
         for H in H_grid:
             new_state = self.get_stable_state(H, J)
             fractions_low_to_high.append(np.mean(new_state + 1) / 2)
         for H in np.flip(H_grid):
             new_state = self.get_stable_state(H, J)
             fractions_high_to_low.append(np.mean(new_state + 1) / 2)
         return fractions_low_to_high, fractions_high_to_low
     hubs_low_to_high, hubs_high_to_low = [], []
     for H in H_grid:
         new_state, changed_hubs = self.get_stable_state(
             H, J, log_hubs, quantile)
         fractions_low_to_high.append(np.mean(new_state + 1) / 2)
         hubs_low_to_high.append(changed_hubs)
     for H in np.flip(H_grid):
         new_state, changed_hubs = self.get_stable_state(
             H, J, log_hubs, quantile)
         fractions_high_to_low.append(np.mean(new_state + 1) / 2)
         hubs_high_to_low.append(changed_hubs)
     return fractions_low_to_high, fractions_high_to_low, hubs_low_to_high, hubs_high_to_low
Exemplo n.º 4
0
def mv_student_neg_logpdf_packed(x, ndim, data, corr=None, verbose=False):
    if corr is not None:
        rho_mapped = map_rho(corr)
        x = np.insert(x, 1, rho_mapped)
    df, loc, scale = unpack_mv_t_args(x, ndim)
    try:
        dist = stats.multivariate_t(loc=loc, shape=scale, df=df)
    except (np.linalg.LinAlgError, ValueError) as e:
        if verbose:
            print(e, df, loc, scale)
        return np.inf
    return -dist.logpdf(data).sum()
Exemplo n.º 5
0
    def simulate_data(self, p_sample, n_obs):
        """
        Returns a single dataset given a sample from the prior.

        Parameters
        ----------
        p_sample: np.ndarray
            One set of parameter samples for one multivariate t distribution
        n_obs: int
            Number of observations
        """

        D = p_sample.shape[0] // 2
        mu, sd = p_sample[:D], p_sample[D:]
        x = stats.multivariate_t(loc=mu, shape=np.diag(sd),
                                 df=self.df).rvs(n_obs)
        return x
Exemplo n.º 6
0
def radial_student_t_integral(r_max, df, ndim):
    from scipy import integrate

    vec = np.zeros(ndim)
    vec[0] = 1
    if ndim == 1:
        dist = stats.t(df=df, loc=0, scale=1)

        def _integrand(r):
            return 2 * dist.pdf(r * vec)

    elif ndim == 2:
        dist = stats.multivariate_t(df=df, loc=np.zeros(ndim), shape=np.eye(ndim))

        def _integrand(r):
            return r * 2 * np.pi * dist.pdf(r * vec)

    else:
        raise NotImplementedError("Not implemented for ndim > 2")

    integral, err = integrate.quad(_integrand, a=0, b=r_max)
    return integral
Exemplo n.º 7
0
 def _kth_likelihood(self, k):
     return stats.multivariate_t(loc=self.means_[:,k], 
                                 shape=self.covariance_[k,:,:], 
                                 df=self.dofs_[k],
                                 allow_singular=True)
Exemplo n.º 8
0
 def logpdf(self, x):
     return stats.multivariate_t(df=self.df, loc=self.loc, shape=self.cov).logpdf(x=x)