Exemple #1
0
def test_lu_slogdet():
    K = array([[2.76405235, 0.40015721], [0.97873798, 3.2408932]])
    K = dot(K, K.T)

    LU = lu_factor(K)
    assert_allclose(lu_slogdet(LU), slogdet(K))

    random = RandomState(6)
    K = random.randn(3, 3)
    K = dot(K, K.T)

    LU = lu_factor(K)
    assert_allclose(lu_slogdet(LU), slogdet(K))
Exemple #2
0
    def _logdetK(self):
        from numpy_sugar.linalg import lu_slogdet

        terms = self._terms
        S = terms["S"]
        Lz = terms["Lz"]

        cov_logdet = lu_slogdet(Lz)[1]
        cov_logdet -= 2 * log(S).sum() * self.nsamples
        return cov_logdet
Exemple #3
0
    def lml(self):
        from numpy_sugar.linalg import lu_slogdet

        if self._cache["lml"] is not None:
            return self._cache["lml"]

        self._update()

        LU = self._posterior.LU()
        LQt = self._posterior.LQt()
        cov = self._posterior.cov
        Q = cov["QS"][0][0]
        S = cov["QS"][1]
        ttau = self._site.tau
        teta = self._site.eta
        ctau = self._cav["tau"]
        ceta = self._cav["eta"]
        m = self._posterior.mean

        TS = ttau + ctau

        s = cov["scale"]
        d = cov["delta"]
        A = self._posterior.A
        dif = 2 * A * teta - A * ttau * m

        lml = [
            -0.5 * lu_slogdet(LU)[1],
            -0.5 * sum(log(s * S)),
            +0.5 * sum(log(A)),
            # lml += 0.5 * sum(log(ttau)),
            +0.5 * dot(teta * A, dot(Q, dot(LQt, teta * A))) * (1 - d),
            -0.5 * dot(teta, teta / TS),
            +dot(m, A * teta) - 0.5 * dot(m, A * ttau * m),
            -0.5 * dot(m * A * ttau, dot(Q, dot(LQt, dif))) * (1 - d),
            +sum(self._moments["log_zeroth"]),
            +0.5 * sum(log(TS)),
            # lml -= 0.5 * sum(log(ttau)),
            -0.5 * sum(log(ctau)),
            +0.5 * dot(ceta / TS, ttau * ceta / ctau - 2 * teta),
            0.5 * s * d * sum(teta * A * teta),
        ]
        lml = fsum(lml)

        if not isfinite(lml):
            raise ValueError("LML should not be %f." % lml)

        self._cache["lml"] = lml

        return lml
Exemple #4
0
    def lml(self):
        from numpy_sugar.linalg import lu_slogdet

        if self._cache["lml"] is not None:
            return self._cache["lml"]

        self._update()

        LU = self._posterior.LU()
        Q, S = self._posterior.cov["QS"]
        Q = Q[0]
        ttau = self._site.tau
        teta = self._site.eta
        ctau = self._cav["tau"]
        ceta = self._cav["eta"]
        m = self._posterior.mean

        TS = ttau + ctau

        lml = [
            -0.5 * lu_slogdet(LU)[1],
            -0.5 * sum(log(S)),
            # lml += 0.5 * sum(log(ttau)),
            +0.5 * dot(teta, dot(Q, lu_solve(LU, dot(Q.T, teta)))),
            -0.5 * dot(teta, teta / TS),
            +dot(m, teta) - 0.5 * dot(m, ttau * m),
            -0.5 *
            dot(m * ttau, dot(Q, lu_solve(LU, dot(Q.T, 2 * teta - ttau * m)))),
            +sum(self._moments["log_zeroth"]),
            +0.5 * sum(log(TS)),
            # lml -= 0.5 * sum(log(ttau)),
            -0.5 * sum(log(ctau)),
            +0.5 * dot(ceta / TS, ttau * ceta / ctau - 2 * teta),
        ]
        lml = fsum(lml)

        if not isfinite(lml):
            raise ValueError("LML should not be %f." % lml)

        self._cache["lml"] = lml
        return lml
Exemple #5
0
    def _terms(self):
        from numpy_sugar.linalg import ddot, lu_slogdet, sum2diag

        if self._cache["terms"] is not None:
            return self._cache["terms"]

        L0 = self._cov.C0.L
        S, U = self._cov.C1.eigh()
        W = ddot(U, 1 / S) @ U.T
        S = 1 / sqrt(S)
        Y = self._Y
        A = self._mean.A

        WL0 = W @ L0
        YW = Y @ W
        WA = W @ A
        L0WA = L0.T @ WA

        Z = kron(L0.T @ WL0, self._GG)
        Z = sum2diag(Z, 1)
        Lz = lu_factor(Z, check_finite=False)

        # ­Юљ▓рхђRРЂ╗┬╣­Юљ▓ = vec(YW)рхђ­Юљ▓
        yRiy = (YW * self._Y).sum()
        # MрхђRРЂ╗┬╣M = AрхђWA РіЌ XрхђX
        MRiM = kron(A.T @ WA, self._XX)
        # XрхђRРЂ╗┬╣­Юљ▓ = vec(GрхђYWLРѓђ)
        XRiy = vec(self._GY @ WL0)
        # XрхђRРЂ╗┬╣M = (LРѓђрхђWA) РіЌ (GрхђX)
        XRiM = kron(L0WA, self._GX)
        # MрхђRРЂ╗┬╣­Юљ▓ = vec(XрхђYWA)
        MRiy = vec(self._XY @ WA)

        ZiXRiM = lu_solve(Lz, XRiM)
        ZiXRiy = lu_solve(Lz, XRiy)

        MRiXZiXRiy = ZiXRiM.T @ XRiy
        MRiXZiXRiM = XRiM.T @ ZiXRiM

        yKiy = yRiy - XRiy @ ZiXRiy
        MKiy = MRiy - MRiXZiXRiy
        H = MRiM - MRiXZiXRiM
        Lh = lu_factor(H, check_finite=False)
        b = lu_solve(Lh, MKiy)
        B = unvec(b, (self.ncovariates, -1))
        self._mean.B = B
        XRim = XRiM @ b

        ZiXRim = ZiXRiM @ b
        mRiy = b.T @ MRiy
        mRim = b.T @ MRiM @ b

        logdetK = lu_slogdet(Lz)[1]
        logdetK -= 2 * log(S).sum() * self.nsamples

        mKiy = mRiy - XRim.T @ ZiXRiy
        mKim = mRim - XRim.T @ ZiXRim

        self._cache["terms"] = {
            "logdetK": logdetK,
            "mKiy": mKiy,
            "mKim": mKim,
            "b": b,
            "Z": Z,
            "B": B,
            "Lz": Lz,
            "S": S,
            "W": W,
            "WA": WA,
            "YW": YW,
            "WL0": WL0,
            "yRiy": yRiy,
            "MRiM": MRiM,
            "XRiy": XRiy,
            "XRiM": XRiM,
            "ZiXRiM": ZiXRiM,
            "ZiXRiy": ZiXRiy,
            "ZiXRim": ZiXRim,
            "MRiy": MRiy,
            "mRim": mRim,
            "mRiy": mRiy,
            "XRim": XRim,
            "yKiy": yKiy,
            "H": H,
            "Lh": Lh,
            "MRiXZiXRiy": MRiXZiXRiy,
            "MRiXZiXRiM": MRiXZiXRiM,
        }
        return self._cache["terms"]