def test_VartimeCovarianceMatrix2():
    """Test whether the trapezoidal integrator returns the expected
    based on the analytically adjusted results of the regular one.
    """

    # sample
    x = numx.random.random((10000, 2))
    dt = numx.ones(x.shape[0] - 1, )
    # initialize the estimators
    cov = CovarianceMatrix(bias=True)
    uncov = VartimeCovarianceMatrix()
    # update the estimators
    cov.update(x)
    uncov.update(x, dt, time_dep=False)
    # quit estimating
    unC, unAvg, unTlen = uncov.fix(center=False)
    C, avg, tlen = cov.fix(center=False)

    assert_array_almost_equal(unC * unTlen,
                              C * tlen - numx.outer(x[0], x[0]) / 2. -
                              numx.outer(x[-1], x[-1]) / 2.,
                              decimal=10)
def test_QuadraticForm_extrema():
    # TODO: add some real test
    # check H with negligible linear term
    noise = 1e-8
    tol = 1e-6
    x = numx_rand.random((10,))
    H = numx.outer(x, x) + numx.eye(10)*0.1
    f = noise*numx_rand.random((10,))
    q = utils.QuadraticForm(H, f)
    xmax, xmin = q.get_extrema(utils.norm2(x), tol=tol)
    assert_array_almost_equal(x, xmax, 5)
    # check I + linear term
    H = numx.eye(10, dtype='d')
    f = x
    q = utils.QuadraticForm(H, f=f)
    xmax, xmin = q.get_extrema(utils.norm2(x), tol=tol)
    assert_array_almost_equal(f, xmax, 5)
Exemple #3
0
 def _train_one(self, pattern):
     pattern = mdp.utils.bool_to_sign(pattern)
     weights = numx.outer(pattern, pattern)
     self._weight_matrix += weights / float(self.input_dim)
     self._num_patterns += 1
Exemple #4
0
    def _stop_training(self, debug=False):
        # debug argument is ignored but needed by the base class
        super(NIPALSNode, self)._stop_training()
        self._adjust_output_dim()
        if self.desired_variance is not None:
            des_var = True
        else:
            des_var = False

        X = self.data
        conv = self.conv
        dtype = self.dtype
        mean = X.mean(axis=0)
        self.avg = mean
        max_it = self.max_it
        tlen = self.tlen

        # remove mean
        X -= mean
        var = X.var(axis=0).sum()
        self.total_variance = var
        exp_var = 0

        eigenv = numx.zeros((self.input_dim, self.input_dim), dtype=dtype)
        d = numx.zeros((self.input_dim,), dtype = dtype)
        for i in range(self.input_dim):
            it = 0
            # first score vector t is initialized to first column in X
            t = X[:, 0]
            # initialize difference
            diff = conv + 1
            while diff > conv:
                # increase iteration counter
                it += 1
                # Project X onto t to find corresponding loading p
                # and normalize loading vector p to length 1
                p = old_div(mult(X.T, t),mult(t, t))
                p /= sqrt(mult(p, p))

                # project X onto p to find corresponding score vector t_new
                t_new = mult(X, p)
                # difference between new and old score vector
                tdiff = t_new - t
                diff = (tdiff*tdiff).sum()
                t = t_new
                if it > max_it:
                    msg = ('PC#%d: no convergence after'
                           ' %d iterations.'% (i, max_it))
                    raise NodeException(msg)

            # store ith eigenvector in result matrix
            eigenv[i, :] = p
            # remove the estimated principal component from X
            D = numx.outer(t, p)
            X -= D
            D = mult(D, p)
            d[i] = old_div((D*D).sum(),(tlen-1))
            exp_var += old_div(d[i],var)
            if des_var and (exp_var >= self.desired_variance):
                self.output_dim = i + 1
                break

        self.d = d[:self.output_dim]
        self.v = eigenv[:self.output_dim, :].T
        self.explained_variance = exp_var
Exemple #5
0
    def _stop_training(self, debug=False):
        # debug argument is ignored but needed by the base class
        super(NIPALSNode, self)._stop_training()
        self._adjust_output_dim()
        if self.desired_variance is not None:
            des_var = True
        else:
            des_var = False

        X = self.data
        conv = self.conv
        dtype = self.dtype
        mean = X.mean(axis=0)
        self.avg = mean
        max_it = self.max_it
        tlen = self.tlen

        # remove mean
        X -= mean
        var = X.var(axis=0).sum()
        self.total_variance = var
        exp_var = 0

        eigenv = numx.zeros((self.input_dim, self.input_dim), dtype=dtype)
        d = numx.zeros((self.input_dim,), dtype = dtype)
        for i in range(self.input_dim):
            it = 0
            # first score vector t is initialized to first column in X
            t = X[:, 0]
            # initialize difference
            diff = conv + 1
            while diff > conv:
                # increase iteration counter
                it += 1
                # Project X onto t to find corresponding loading p
                # and normalize loading vector p to length 1
                p = old_div(mult(X.T, t),mult(t, t))
                p /= sqrt(mult(p, p))

                # project X onto p to find corresponding score vector t_new
                t_new = mult(X, p)
                # difference between new and old score vector
                tdiff = t_new - t
                diff = (tdiff*tdiff).sum()
                t = t_new
                if it > max_it:
                    msg = ('PC#%d: no convergence after'
                           ' %d iterations.'% (i, max_it))
                    raise NodeException(msg)

            # store ith eigenvector in result matrix
            eigenv[i, :] = p
            # remove the estimated principal component from X
            D = numx.outer(t, p)
            X -= D
            D = mult(D, p)
            d[i] = old_div((D*D).sum(),(tlen-1))
            exp_var += old_div(d[i],var)
            if des_var and (exp_var >= self.desired_variance):
                self.output_dim = i + 1
                break

        self.d = d[:self.output_dim]
        self.v = eigenv[:self.output_dim, :].T
        self.explained_variance = exp_var
Exemple #6
0
 def _train_one(self, pattern):
     pattern = mdp.utils.bool_to_sign(pattern)
     weights = numx.outer(pattern, pattern)
     self._weight_matrix += weights / float(self.input_dim)
     self._num_patterns += 1