Exemplo n.º 1
0
def sampling_total_degree_indices(N, d, k):

    """
    Chooses N random multi-indices (with the uniform probability law) from the
    set of d-variate multi-indices whose total degree is k and less

    Parameters
    ------
    param1: N
    Numebr of chosen random multi-indices
    param2: d
    dimension of variables
    param3L k
    total degree of variables

    Returns
    ------
    The output lambdas is an N x d matrix, with each row containing one of
    these multi-indices
    """
    lambdas = np.zeros((N, d))

    degrees = discrete_sampling(N, pdjk(d, k), np.arange(k+1)).T

    for i in range(1, d):
        for n in range(1, N+1):
            lambdas[n-1, i-1] = \
                discrete_sampling(1, pdjk(d-i, degrees[n-1]),
                                  np.arange(degrees[n-1], 0-1e-8, -1))

        degrees = degrees - lambdas[:, i-1]

    lambdas[:, d-1] = degrees

    return lambdas
Exemplo n.º 2
0
    def idist_mixture_sampling(self, M, Lambdas, weights=None,
                               fast_sampler=True):
        """
        Performs tensorial inverse transform sampling from an additive mixture
        of tensorial induced distributions, generating M samples

        The measure this samples from is the order-Lambdas induced measure,
        which is an additive mixture of tensorial measures

        Each tensorial measure is defined a row of Lambdas

        Parameters
        ------
        param1: M
        Number of samples to generate
        param2: Lambdas
        Sample from the order-Lambdas induced measure

        Returns
        ------
        """

        from UncertainSCI.utils.prob import discrete_sampling

        K, d = Lambdas.shape

        assert M > 0 and d == self.dim

        if weights is None:
            weights = np.ones(K)/K
        else:
            assert weights.size == K
            assert np.all(weights >= 0)
            weights = weights/np.sum(weights)

        ks = discrete_sampling(M, weights, np.arange(K, dtype=int))
        Lambdas = Lambdas[ks, :]

        if self.isotropic:

            fidistinv = getattr(self.polys1d, "fidistinv", None)
            if callable(fidistinv):
                has_fidistinv = True
            else:
                has_fidistinv = False

            if fast_sampler and has_fidistinv:
                idistinv = self.polys1d.fidistinv
            else:
                idistinv = self.polys1d.idistinv

            return idistinv(np.random.random([M, d]), Lambdas)

        else:
            x = np.zeros([M, d])
            for qd in range(self.dim):

                fidistinv = getattr(self.polys1d[qd], "fidistinv", None)
                if callable(fidistinv):
                    has_fidistinv = True
                else:
                    has_fidistinv = False

                if fast_sampler and has_fidistinv:
                    idistinv = self.polys1d[qd].fidistinv
                else:
                    idistinv = self.polys1d[qd].idistinv

                x[:, qd] = idistinv(np.random.random(M), Lambdas[:, qd])
            return x