Exemple #1
0
    def __init__(self, G, dim, rank):
        """
        Constructor.

        Parameters
        ----------
        dim : int
            Dimension d for the square matrices C₀ and C₁.
        rank : int
            Maximum rank of the C₁ matrix.
        """

        self._cache: Dict[str, Any] = {"LhD": None}
        self._C0 = LRFreeFormCov(dim, rank)
        self._C0.name = "C₀"
        self._C1 = FreeFormCov(dim)
        self._C1.name = "C₁"
        G = atleast_2d(asarray(G, float))
        self._G = G

        self._Sxe = None
        self._Sx = None
        self._Lx = None
        self._LxG = None
        self._diag_LxGGLx = None
        self._Lxe = None
        self._LxGe = None
        self._diag_LxGGLxe = None

        Function.__init__(
            self, "Kron2SumCov", composite=[("C0", self._C0), ("C1", self._C1)]
        )
        self._C0.listen(self._parameters_update)
        self._C1.listen(self._parameters_update)
Exemple #2
0
    def __init__(self, y, lik, mean, cov):
        from liknorm import LikNormMachine

        if isinstance(y, tuple):
            n = len(y[0])
        else:
            n = len(y)
        Function.__init__(self, "ExpFamGP", composite=[mean, cov])

        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._mean = mean
        self._cov = cov
        self._ep = EP(n)
        self._ep.set_compute_moments(self.compute_moments)

        self._mean_value = None
        self._cov_value = None

        self._machine = LikNormMachine(lik[0], 500)
Exemple #3
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
Exemple #4
0
    def fix(self, var_name):
        r"""Prevent a variable to be adjusted.

        Parameters
        ----------
        var_name : str
            Variable name.
        """
        Function._fix(self, _to_internal_name(var_name))
Exemple #5
0
    def unfix(self, var_name):
        r"""Let a variable be adjusted.

        Parameters
        ----------
        var_name : str
            Variable name.
        """
        Function._unfix(self, _to_internal_name(var_name))
Exemple #6
0
    def __init__(self, covariances):
        """
        Constructor.

        Parameters
        ----------
        covariances : list
            List of covariance functions.
        """
        self._covariances = [c for c in covariances]
        Function.__init__(self, "SumCov", composite=self._covariances)
Exemple #7
0
    def __init__(self, means):
        """
        Constructor.

        Parameters
        ----------
        means : list
            List of mean functions.
        """
        self._means = [c for c in means]
        Function.__init__(self, "SumMean", composite=self._means)
Exemple #8
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)))
Exemple #9
0
    def __init__(self, X):
        """
        Constructor.

        Parameters
        ----------
        X : array_like
            Matrix X from K = s⋅XXᵀ.
        """
        self._logscale = Scalar(0.0)
        self._X = X
        Function.__init__(self, "LinearCov", logscale=self._logscale)
        self._logscale.bounds = (-20.0, +10)
Exemple #10
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)
Exemple #11
0
    def __init__(self, dim):
        """
        Constructor.

        Parameters
        ----------
        dim : int
            Matrix dimension, d.
        """
        self._dim = dim
        self._I = eye(dim)
        self._logscale = Scalar(0.0)
        Function.__init__(self, "EyeCov", logscale=self._logscale)
        self._logscale.bounds = (-20.0, +10)
Exemple #12
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)
Exemple #13
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)
Exemple #14
0
    def __init__(self, K0):
        """
        Constructor.

        Parameters
        ----------
        K0 : array_like
            A semi-definite positive matrix.
        """
        from numpy_sugar.linalg import check_symmetry

        self._logscale = Scalar(0.0)
        Function.__init__(self, "GivenCov", logscale=self._logscale)
        self._logscale.bounds = (-20.0, +10)
        if not check_symmetry(K0):
            raise ValueError(
                "The provided covariance-matrix is not symmetric.")
        self._K0 = K0
Exemple #15
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")
Exemple #16
0
 def __init__(self):
     Function.__init__(self, offset=Scalar(1.0))
Exemple #17
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
Exemple #18
0
    def __init__(self, Y, A, X, G, rank=1, restricted=False):
        """
        Constructor.

        Parameters
        ----------
        Y : (n, p) array_like
            Outcome matrix.
        A : (n, n) array_like
            Trait-by-trait design matrix.
        X : (n, c) array_like
            Covariates design matrix.
        G : (n, r) array_like
            Matrix G from the GGрхђ term.
        rank : optional, int
            Maximum rank of matrix CРѓђ. Defaults to ``1``.
        """
        from numpy_sugar import is_all_finite

        Y = asfortranarray(Y, float)
        yrank = matrix_rank(Y)
        if Y.shape[1] > yrank:
            warnings.warn(
                f"Y is not full column rank: rank(Y)={yrank}. " +
                "Convergence might be problematic.",
                UserWarning,
            )

        A = asarray(A, float)
        X = asarray(X, float)
        Xrank = matrix_rank(X)
        if X.shape[1] > Xrank:
            warnings.warn(
                f"X is not full column rank: rank(X)={Xrank}. " +
                "Convergence might be problematic.",
                UserWarning,
            )
        G = asarray(G, float).copy()
        self._G_norm = max(G.min(), G.max())
        G /= self._G_norm

        if not is_all_finite(Y):
            raise ValueError(
                "There are non-finite values in the outcome matrix.")

        if not is_all_finite(A):
            msg = "There are non-finite values in the trait-by-trait design matrix."
            raise ValueError(msg)

        if not is_all_finite(X):
            raise ValueError(
                "There are non-finite values in the covariates matrix.")

        if not is_all_finite(G):
            raise ValueError("There are non-finite values in the G matrix.")

        self._Y = Y
        self._cov = Kron2SumCov(G, Y.shape[1], rank)
        self._cov.listen(self._parameters_update)
        self._mean = KronMean(A, X)
        self._cache = {"terms": None}
        self._restricted = restricted
        composite = [("C0", self._cov.C0), ("C1", self._cov.C1)]
        Function.__init__(self, "Kron2Sum", composite=composite)

        nparams = self._mean.nparams + self._cov.nparams
        if nparams > Y.size:
            msg = "The number of parameters is larger than the outcome size."
            msg += " Convergence is expected to be problematic."
            warnings.warn(msg, UserWarning)
Exemple #19
0
 def __init__(self):
     Function.__init__(self, logscale=Scalar(0.0))
Exemple #20
0
 def __init__(self, size):
     Function.__init__(self, effsizes=Vector(zeros(size)))