Esempio n. 1
0
    def sample(self, n=1):
        if not type(n) is int:
            raise TypeError("Number of samples must be integer")

        if n <= 0:
            raise ValueError("Number of samples (%d) needs to be positive", n)

        # copy mean vector a couple of times
        samples = repmat(self.mu, n, 1)

        if self.flip_at_least_one is False:
            # indices to flip, evenly distributed and the change probability is Bernoulli
            change_inds = rand(n, self.dimension) < self.spread
        else:
            # sample number of changes from binomial(spread, d-1) to have at least one change
            num_changes = 1 + sum(rand(n, self.dimension - 1) < self.spread, 1)

            # randomly change that many indices
            change_inds = zeros((n, self.dimension), dtype=numpy.bool8)
            for i in range(n):
                change_inds[i, arange(num_changes[i])] = True
                change_inds[i] = change_inds[i, permutation(self.dimension)]

        # flip all chosen indices
        samples[change_inds] = mod(samples[change_inds] + 1, 2)

        return Sample(samples)
Esempio n. 2
0
    def sample(self, n=1):
        X = randn(n, 2)
        X[:, 0] = sqrt(self.V) * X[:, 0]
        X[:, 1] = X[:, 1] + self.bananicity * (X[:, 0]**2 - self.V)
        if self.dimension > 2:
            X = hstack((X, randn(n, self.dimension - 2)))

        return Sample(X)
Esempio n. 3
0
    def sample(self, n=1):
        if self.ell is None:
            V = randn(self.dimension, n)
        else:
            V = randn(self.ell, n)

        # map to our desired Gaussian and transpose to have row-wise vectors
        return Sample(self.L.dot(V).T + self.mu)
Esempio n. 4
0
 def sample(self, n=1):
     u = np.random.rand(n)
     rez = np.zeros([n])
     for ii in range(0, n):
         jj = 0
         while u[ii] > self.cdf[jj]:
             jj += 1
         rez[ii] = self.support[jj]
     return Sample(rez.astype(np.int32))
Esempio n. 5
0
    def sample(self, n=1):
        # sample angles
        theta = rand(n, 1) * 2 * pi

        # sample radius
        radius_sample = randn(n, 1) * sqrt(self.variance) + self.radius + \
            self.amplitude * cos(self.frequency * theta)

        # sample points
        X = hstack((cos(theta) * radius_sample, sin(theta) * radius_sample))

        # add noise
        if self.dimension > 2:
            X = hstack((X, randn(n, self.dimension - 2)))

        return Sample(X)
Esempio n. 6
0
    def sample(self, n=1):
        if n is not 1:
            raise ValueError("Only one sample supported for full conditionals")

        # random schedules need to re-create index block upon last index
        if self.current_idx == self.dimension:
            if self.schedule == "random_permutation":
                self.index_block = permutation(self.index_block)
            elif self.schedule == "random_repetition":
                self.index_block = randint(0, self.dimension, self.dimension)

        # update current index
        self.current_idx = mod(self.current_idx + 1, self.dimension)
        idx = self.index_block[self.current_idx]

        # update current state at scheduled index and return a copy
        self.current_state[idx] = self.sample_conditional(idx)

        return Sample(self.get_current_state_array())
Esempio n. 7
0
 def __init__(self, samples, which_component):
     Sample.__init__(self, samples)
     self.which_component = which_component
Esempio n. 8
0
    def init(self, start):
        assert (len(shape(start)) == 1)

        self.current_sample_object = Sample(start)
        start_2d = reshape(start, (1, len(start)))
        self.log_lik_current = self.distribution.log_pdf(start_2d)
 def __init__(self,samples,which_component):
     Sample.__init__(self,samples)
     self.which_component=which_component