Ejemplo n.º 1
0
    def wafp_sampling(self, indices, oversampling=10, weights=None,
                      sampler='idist', K=None, fast_sampler=True):
        """
        Computes (indices.shape[0] + oversampling) points using the WAFP
        strategy. This requires forming K random samples; the input sampler
        determines which measure to take these random samples from.
        """

        M = indices.shape[0] + oversampling

        if K is None:
            K = M + 40
        else:
            K = max(K, M)

        if sampler == 'idist':
            x = self.idist_mixture_sampling(K, indices, weights=weights,
                                            fast_sampler=fast_sampler)
        else:
            raise ValueError('Unrecognized string "{0:s}" for input \
                             "sampler"'.format(sampler))

        V = self.eval(x, indices)

        # Precondition rows to have unit norm
        V = np.multiply(V.T, 1/np.sqrt(np.sum(V**2, axis=1))).T

        P = greedy_d_optimal(V, M)

        return x[P, :]
Ejemplo n.º 2
0
    def test_greedy_d_optimal_restart(self):
        """ Greedy D-optimal designs with restart. """

        N = int(200 * np.random.random_sample())
        M = N + int(100 * np.random.random_sample())
        c = np.random.random_sample()
        p = int(c * M + (1 - c) * N)

        A = np.random.randn(M, N) / np.sqrt(N)

        P = greedy_d_optimal(A, p)

        _, P2 = qr(A.T, pivoting=True, mode='r')

        temp = A[P2[:N], :]
        # G = np.dot(temp.T, temp)
        # Ginvwm = np.dot(np.linalg.inv(G), A[P2[N:], :].T)

        # Confirm greedy determinantal stuff
        for q in range(p - N):
            detvals = np.zeros(M - N - q)
            for ind in range(M - N - q):
                temp = A[np.append(P2[:(N + q)], P2[N + q + ind]), :]
                detvals[ind] = np.linalg.det(np.dot(temp.T, temp))

            maxind = np.argmax(detvals) + N + q

            P2[[maxind, N + q]] = P2[[N + q, maxind]]

        errstr = 'Failed for (N,M,p) = ({0:d}, {1:d}, {2:d})'.format(N, M, p)

        self.assertEqual(np.linalg.norm(P - P2[:p], ord=np.inf), 0, msg=errstr)
Ejemplo n.º 3
0
    def wafp_sampling_restart(self,
                              indices,
                              samples,
                              Nnewsamples,
                              weights=None,
                              sampler='idist',
                              fast_sampler=True):
        """
        Computes Nnewsamples from a "restarted" WAFP procedure, from which the
        input samples are already prescribed.
        """

        if Nnewsamples == 0:
            return samples

        Mold = samples.shape[0]
        V = self.eval(samples, indices)
        # Precondition rows to have unit norm
        V = np.multiply(V.T, 1 / np.sqrt(np.sum(V**2, axis=1))).T

        # We'll do this slowly since adaptivity is hard.
        # Generate Ncandidate new candidates, choose 1 new sample, repeat.
        Ncandidates = 100

        for q in range(Nnewsamples):

            if sampler == 'idist':
                x = self.idist_mixture_sampling(Ncandidates,
                                                indices,
                                                weights=weights,
                                                fast_sampler=fast_sampler)
            else:
                raise ValueError('Unrecognized string "{0:s}" for input \
                                  "sampler"'.format(sampler))

            temp = self.eval(x, indices)
            temp = np.multiply(temp.T, 1 / np.sqrt(np.sum(temp**2, axis=1))).T

            A = np.vstack((V, temp))
            P = greedy_d_optimal(A, Mold + q + 1, pstart=range(Mold + q))

            # Append the new sample to both V and samples
            V = np.vstack((V, A[P[-1], :]))
            samples = np.vstack((samples, x[P[-1] - Mold - q, :]))

        return samples