Esempio n. 1
0
def test_types_scalar_fix():
    a = Scalar(1.0)

    assert_(not a.isfixed)

    a.fix()
    assert_(a.isfixed)
Esempio n. 2
0
class Foo1(Function):
    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)

    @property
    def a(self):
        return self._a.value

    @property
    def b(self):
        return self._b.value

    @property
    def c(self):
        return self._c.value

    def fix_c(self):
        self._c.fix()

    def unfix_c(self):
        self._c.unfix()

    @c.setter
    def c(self, v):
        self._c.value = v

    def value(self):
        a = self.a
        b = self.b
        c = self.c
        return (a @ b - 3 + a @ [1, 1] - b @ [1, 2] + 1 / c)**2

    def gradient(self):
        a = self.a
        b = self.b
        c = self.c
        v = a @ b - 3 + a @ [1, 1] - b @ [1, 2] + 1 / c
        da = 2 * v * array([b[0] + 1, b[1] + 1])
        db = 2 * v * array([a[0] - 1, a[1] - 2])
        dc = 2 * v * -1 / (c**2)
        return {"a": da, "b": db, "c": dc}

    def check_grad(self):
        return self._check_grad()
Esempio n. 3
0
    def __init__(self, y, X, QS=None, restricted=False):
        """
        Constructor.

        Parameters
        ----------
        y : array_like
            Outcome.
        X : array_like
            Covariates as a two-dimensional array.
        QS : tuple
            Economic eigendecompositon in form of ``((Q0, ), S0)`` of a
            covariance matrix ``K``.
        restricted : bool
            ``True`` for restricted maximum likelihood optimization; ``False``
            otherwise. Defaults to ``False``.
        """
        from numpy_sugar import is_all_finite

        logistic = Scalar(0.0)
        logistic.listen(self._delta_update)
        logistic.bounds = (-numbers.logmax, +numbers.logmax)
        Function.__init__(self, "LMM", logistic=logistic)
        self._logistic = logistic

        y = asarray(y, float).ravel()
        if not is_all_finite(y):
            raise ValueError("There are non-finite values in the outcome.")

        if len(y) == 0:
            raise ValueError("The outcome array is empty.")

        X = atleast_2d(asarray(X, float).T).T
        if not is_all_finite(X):
            raise ValueError("There are non-finite values in the covariates matrix.")

        self._optimal = {"beta": False, "scale": False}
        if QS is None:
            QS = economic_qs_zeros(len(y))
            self._B = B(QS[0][0], QS[1], 0.0, 1.0)
            self.delta = 1.0
            logistic.fix()
        else:
            self._B = B(QS[0][0], QS[1], 0.5, 0.5)
            self.delta = 0.5

        if QS[0][0].shape[0] != len(y):
            msg = "Sample size differs between outcome and covariance decomposition."
            raise ValueError(msg)

        if y.shape[0] != X.shape[0]:
            msg = "Sample size differs between outcome and covariates."
            raise ValueError(msg)

        self._y = y
        self._Q0 = QS[0][0]
        self._S0 = QS[1]
        self._Xsvd = SVD(X)
        self._tbeta = zeros(self._Xsvd.rank)
        self._scale = 1.0
        self._fix = {"beta": False, "scale": False}
        self._restricted = restricted