Ejemplo n.º 1
0
def test_types_vector_comparison():
    a = Vector([1.0, 2.0])
    b = Vector([1.0, 1.5])

    assert_((a > b)[1])
    assert_((a == b)[0])
    assert_((a != b)[1])

    b.value = asarray([1.0, 2.0])

    assert_(all(a == b))
Ejemplo n.º 2
0
    def __init__(self, dim):
        """
        Constructor.

        Parameters
        ----------
        dim : int
            Dimension d of the free-form covariance matrix.
        """
        from numpy_sugar import epsilon

        dim = int(dim)
        tsize = ((dim + 1) * dim) // 2
        self._L = zeros((dim, dim))
        self._tril1 = tril_indices_from(self._L, k=-1)
        self._diag = diag_indices_from(self._L)
        self._L[self._tril1] = 1
        self._L[self._diag] = 0
        self._epsilon = epsilon.small * 1000
        self._Lu = Vector(zeros(tsize))
        self._Lu.value[:tsize - dim] = 1
        n = self.L.shape[0]
        self._grad_Lu = zeros((n, n, self._Lu.shape[0]))
        Function.__init__(self, "FreeCov", Lu=self._Lu)
        bounds = [(-inf, +inf)] * (tsize - dim)
        bounds += [(log(epsilon.small * 1000), +11)] * dim
        self._Lu.bounds = bounds
        self._cache = {"eig": None}
        self.listen(self._parameters_update)
        self._nparams = tsize
Ejemplo n.º 3
0
def test_types_vector_fix():
    a = Vector([1.0, 2.0])

    assert_(not a.isfixed)

    a.fix()
    assert_(a.isfixed)
Ejemplo n.º 4
0
 def __init__(self, size):
     """
     Args:
         dim:        dimension of the free-form covariance
         jitter:     extent of diagonal offset which is added for numerical stability
                     (default value: 1e-4)
     """
     tsize = ((size + 1) * size) / 2
     tsize = int(tsize)
     self._L = zeros((size, size))
     self._tril = tril_indices_from(self._L)
     self._L[self._tril] = 1
     Function.__init__(self, Lu=Vector(ones(tsize)))
Ejemplo n.º 5
0
def test_types_vector_listen():
    a = Vector([1.0, 2.0])

    class Listener(object):
        def __init__(self):
            self.value = None

        def __call__(self):
            self.value = asarray([3.0, -1.0])

    l = Listener()
    a.listen(l)
    a.value = asarray([3.0, -1.0])
    assert_allclose(l.value, [3.0, -1.0])
Ejemplo n.º 6
0
    def __init__(self, n, m):
        """
        Constructor.

        Parameters
        ----------
        n : int
            Covariance dimension.
        m : int
            Upper limit of the covariance matrix rank.
        """
        self._L = ones((n, m))
        self._Lu = Vector(self._L.ravel())
        Function.__init__(self, "LRFreeFormCov", Lu=self._Lu)
Ejemplo n.º 7
0
    def __init__(self, X):
        """
        Constructor.

        Parameters
        ----------
        X : array_like
            Covariates X, from X𝜶.
        """
        X = asarray(X, float)
        m = X.shape[1]
        self._effsizes = Vector(zeros(m))
        self._effsizes.bounds = [(-200.0, +200)] * m
        self._X = X
        Function.__init__(self, "LinearMean", effsizes=self._effsizes)
Ejemplo n.º 8
0
    def __init__(self, A, X):
        """
        Constructor.

        Parameters
        ----------
        A : array_like
            p×p array.
        X : array_like
            n×c array.
        """
        self._A = asarray(A, float)
        self._X = asarray(X, float)
        vecB = zeros((X.shape[1], A.shape[0])).ravel()
        self._vecB = Vector(vecB)
        self._nparams = vecB.size
        Function.__init__(self, "KronMean", vecB=self._vecB)
Ejemplo n.º 9
0
    def __init__(self, y, lik, X, QS=None):
        y = ascontiguousarray(y, float)
        X = asarray(X, float)

        Function.__init__(
            self,
            "GLMM",
            beta=Vector(zeros(X.shape[1])),
            logscale=Scalar(0.0),
            logitdelta=Scalar(0.0),
        )

        if not isinstance(lik, (tuple, list)):
            lik = (lik, )

        self._lik = (lik[0].lower(), ) + tuple(
            ascontiguousarray(i) for i in lik[1:])
        self._y = check_outcome(y, self._lik)
        self._X = check_covariates(X)
        if QS is None:
            self._QS = economic_qs_zeros(self._y.shape[0])
        else:
            self._QS = check_economic_qs(QS)
            if self._y.shape[0] != self._QS[0][0].shape[0]:
                raise ValueError(
                    "Number of samples in outcome and covariance differ.")

        if self._y.shape[0] != self._X.shape[0]:
            raise ValueError(
                "Number of samples in outcome and covariates differ.")

        self._factr = 1e5
        self._pgtol = 1e-6
        self._verbose = False
        self.set_variable_bounds("logscale", (log(0.001), 6.0))

        self.set_variable_bounds("logitdelta", (-50, +15))

        if lik[0] == "probit":
            self.delta = 0.0
            self.fix("delta")
Ejemplo n.º 10
0
def test_types_vector_copy():
    a = Vector([1.0])
    b = a.copy()

    assert_(a is not b)
    assert_(a == b)
Ejemplo n.º 11
0
def test_types_modify_vector():
    a = Vector([1.0, 2.0])
    value = atleast_1d(a.value)
    value[0] = 2.0
    assert_allclose(a.value, value)
Ejemplo n.º 12
0
 def __init__(self):
     self._a = Vector([0, 0])
     self._b = Vector([0, 0])
     self._c = Scalar(1)
     super(Foo1, self).__init__("Foo1", a=self._a, b=self._b, c=self._c)
Ejemplo n.º 13
0
 def __init__(self, size):
     Function.__init__(self, effsizes=Vector(zeros(size)))