Example #1
0
    def PreCalculate(self ):
    
  
        
        if not self.prepared:
            self.PrepareCalculation()
        
        if debug:print('Precalculate')
        #first pass - decrease the weight of the outliers

            
        Y    = np.copy(self.Y[self.valid])/self.norm
        Yerr = np.copy(self.Yerr.data[self.valid])/self.norm
        Yerr *= self.deriv_trans(Y)
        
        Yerr[self.Yerr.mask[self.valid]] = np.infty

        #transform and weight by uncertainty
        try:
            self.f = self.trans(Y)/Yerr 
        except:
            print('s fit error np.sum(Yerr == 0), np.sum(Yerr < 0), np.sum(~np.isfinite(self.trans(Y)))', np.sum(Yerr == 0), np.sum(Yerr < 0), np.sum(~np.isfinite(self.trans(Y))))
            raise

        self.V = sp.spdiags(1/Yerr,0, self.n_points,self.n_points,format='csr')*self.M
        self.V.eliminate_zeros()  
        self.VV = self.V.T*self.V #slow 

        
        vvtrace = self.VV.diagonal().sum()
        lam = np.exp(16)/self.DRDR.diagonal().sum()*vvtrace
        eta = np.exp(11)/self.DTDT.diagonal().sum()*vvtrace
    
        if self.nt_new == 1: eta = 0
        AA = self.VV+lam*self.DRDR+eta*self.DTDT
   
        if chol_inst:
            self.Factor = analyze(AA, ordering_method='colamd')
 
        self.corrected=True

        if self.robust_fit:
        
            if chol_inst:
                self.Factor.cholesky_inplace(AA)
            else:
                self.Factor = sp.linalg.factorized(AA)
                print('umfpack')
 
            g=np.squeeze(self.Factor(self.V.T*self.f))

            #make a more robust fit in one iteration
            dist = self.V*g-self.f  
            
            #Winsorizing M estimator, penalize outliers
            Yerr_corr = ((dist/3.)**2+1)**(1)  

            self.f /= Yerr_corr
            self.V = sp.spdiags(1/Yerr_corr,0, self.n_points,self.n_points,format='csr')*self.V
            self.VV = self.V.T*self.V
Example #2
0
    def __init__(self, matrix, AAt=False, ordering="AMD"):
        # Initiate a Cholesky factorization

        # Set sizes
        self.N = matrix.shape[0]

        if AAt:
            # Analyze matrix
            self._chol = cholmod.analyze_AAt(matrix,
                                             mode="auto",
                                             ordering_method="best",
                                             use_long=False)
            # Factor matrix
            self._chol = self._chol.cholesky_AAt(matrix)
        else:
            # Analyze matrix
            self._chol = cholmod.analyze(matrix,
                                         mode="auto",
                                         ordering_method="best",
                                         use_long=False)
            # Factor matrix
            self._chol = self._chol.cholesky(matrix)

        if np.any(self._chol.D() <= 0):
            raise SparseCholeskyAbstract.PositiveDefiniteException(
                "Matrix is not positive definite")
Example #3
0
 def lookup(self, N, A):
     print('lookup')
     index = np.dot(self.indexBasis, N)
     if index not in self.factorCache:
         print('miss')
         t = time.time()
         self.factorCache[index] = analyze(A)
         t = time.time() - t
         print('a', t)
     else:
         print('hit')
     cholAFactor = self.factorCache[index]
     t = time.time()
     cholAFactorReturn = cholAFactor.cholesky(A)
     t = time.time() - t
     print('c', t)
     return cholAFactorReturn
Example #4
0
    def _get_state_update(self):
        W, J, r = self._graph.get_linearization()

        Jt = J.T.tocsc()
        J = J.tocsc()

        # Decompose W such that W = U * U.T
        if self._sym_decomp_W is None:
            self._sym_decomp_W = analyze(W, mode='auto')

        chol_decomp_W = self._sym_decomp_W.cholesky(W)
        U = chol_decomp_W.L()
        JtU = Jt.dot(U)

        # A = J.T * W * J
        #   = J.T * U * U.T * J
        if self._sym_decomp_JtWJ is None:
            self._sym_decomp_JtWJ = analyze_AAt(JtU, mode='auto')

        chol_decomp_JtWJ = self._sym_decomp_JtWJ.cholesky_AAt(JtU)

        b = Jt.dot(W.dot(r))
        x = chol_decomp_JtWJ.solve_A(b)
        return x
Example #5
0
def test_cholesky_matrix_market():
    for problem in ("well1033", "illc1033", "well1850", "illc1850"):
        X = mm_matrix(problem)
        y = mm_matrix(problem + "_rhs1")
        answer = np.linalg.lstsq(X.todense(), y)[0]
        XtX = (X.T * X).tocsc()
        Xty = X.T * y
        for mode in ("auto", "simplicial", "supernodal"):
            assert_allclose(cholesky(XtX, mode=mode)(Xty), answer)
            assert_allclose(cholesky_AAt(X.T, mode=mode)(Xty), answer)
            assert_allclose(cholesky(XtX, mode=mode).solve_A(Xty), answer)
            assert_allclose(cholesky_AAt(X.T, mode=mode).solve_A(Xty), answer)

            f1 = analyze(XtX, mode=mode)
            f2 = f1.cholesky(XtX)
            assert_allclose(f2(Xty), answer)
            assert_raises(CholmodError, f1, Xty)
            assert_raises(CholmodError, f1.solve_A, Xty)
            assert_raises(CholmodError, f1.solve_LDLt, Xty)
            assert_raises(CholmodError, f1.solve_LD, Xty)
            assert_raises(CholmodError, f1.solve_DLt, Xty)
            assert_raises(CholmodError, f1.solve_L, Xty)
            assert_raises(CholmodError, f1.solve_D, Xty)
            assert_raises(CholmodError, f1.apply_P, Xty)
            assert_raises(CholmodError, f1.apply_Pt, Xty)
            f1.P()
            assert_raises(CholmodError, f1.L)
            assert_raises(CholmodError, f1.LD)
            assert_raises(CholmodError, f1.L_D)
            assert_raises(CholmodError, f1.L_D)
            f1.cholesky_inplace(XtX)
            assert_allclose(f1(Xty), answer)

            f3 = analyze_AAt(X.T, mode=mode)
            f4 = f3.cholesky(XtX)
            assert_allclose(f4(Xty), answer)
            assert_raises(CholmodError, f3, Xty)
            f3.cholesky_AAt_inplace(X.T)
            assert_allclose(f3(Xty), answer)

            print(problem, mode)
            for f in (f1, f2, f3, f4):
                pXtX = XtX.todense()[f.P()[:, np.newaxis],
                                     f.P()[np.newaxis, :]]
                assert_allclose(np.prod(f.D()), np.linalg.det(XtX.todense()))
                assert_allclose((f.L() * f.L().T).todense(), pXtX)
                L, D = f.L_D()
                assert_allclose((L * D * L.T).todense(), pXtX)

                b = np.arange(XtX.shape[0])[:, np.newaxis]
                assert_allclose(f.solve_A(b), np.dot(XtX.todense().I, b))
                assert_allclose(f(b), np.dot(XtX.todense().I, b))
                assert_allclose(f.solve_LDLt(b),
                                np.dot((L * D * L.T).todense().I, b))
                assert_allclose(f.solve_LD(b), np.dot((L * D).todense().I, b))
                assert_allclose(f.solve_DLt(b), np.dot((D * L.T).todense().I,
                                                       b))
                assert_allclose(f.solve_L(b), np.dot(L.todense().I, b))
                assert_allclose(f.solve_Lt(b), np.dot(L.T.todense().I, b))
                assert_allclose(f.solve_D(b), np.dot(D.todense().I, b))

                assert_allclose(f.apply_P(b), b[f.P(), :])
                assert_allclose(f.apply_P(b), b[f.P(), :])
                # Pt is the inverse of P, and argsort inverts permutation
                # vectors:
                assert_allclose(f.apply_Pt(b), b[np.argsort(f.P()), :])
                assert_allclose(f.apply_Pt(b), b[np.argsort(f.P()), :])
Example #6
0
def test_cholesky_matrix_market():
    for problem in ("well1033", "illc1033", "well1850", "illc1850"):
        X = mm_matrix(problem)
        y = mm_matrix(problem + "_rhs1")
        answer = np.linalg.lstsq(X.todense(), y)[0]
        XtX = (X.T * X).tocsc()
        Xty = X.T * y
        for mode in ("auto", "simplicial", "supernodal"):
            assert_allclose(cholesky(XtX, mode=mode)(Xty), answer)
            assert_allclose(cholesky_AAt(X.T, mode=mode)(Xty), answer)
            assert_allclose(cholesky(XtX, mode=mode).solve_A(Xty), answer)
            assert_allclose(cholesky_AAt(X.T, mode=mode).solve_A(Xty), answer)

            f1 = analyze(XtX, mode=mode)
            f2 = f1.cholesky(XtX)
            assert_allclose(f2(Xty), answer)
            assert_raises(CholmodError, f1, Xty)
            assert_raises(CholmodError, f1.solve_A, Xty)
            assert_raises(CholmodError, f1.solve_LDLt, Xty)
            assert_raises(CholmodError, f1.solve_LD, Xty)
            assert_raises(CholmodError, f1.solve_DLt, Xty)
            assert_raises(CholmodError, f1.solve_L, Xty)
            assert_raises(CholmodError, f1.solve_D, Xty)
            assert_raises(CholmodError, f1.apply_P, Xty)
            assert_raises(CholmodError, f1.apply_Pt, Xty)
            f1.P()
            assert_raises(CholmodError, f1.L)
            assert_raises(CholmodError, f1.LD)
            assert_raises(CholmodError, f1.L_D)
            assert_raises(CholmodError, f1.L_D)
            f1.cholesky_inplace(XtX)
            assert_allclose(f1(Xty), answer)

            f3 = analyze_AAt(X.T, mode=mode)
            f4 = f3.cholesky(XtX)
            assert_allclose(f4(Xty), answer)
            assert_raises(CholmodError, f3, Xty)
            f3.cholesky_AAt_inplace(X.T)
            assert_allclose(f3(Xty), answer)

            print(problem, mode)
            for f in (f1, f2, f3, f4):
                pXtX = XtX.todense()[f.P()[:, np.newaxis],
                                     f.P()[np.newaxis, :]]
                assert_allclose(np.prod(f.D()),
                                np.linalg.det(XtX.todense()))
                assert_allclose((f.L() * f.L().T).todense(),
                                pXtX)
                L, D = f.L_D()
                assert_allclose((L * D * L.T).todense(),
                                pXtX)

                b = np.arange(XtX.shape[0])[:, np.newaxis]
                assert_allclose(f.solve_A(b),
                                np.dot(XtX.todense().I, b))
                assert_allclose(f(b),
                                np.dot(XtX.todense().I, b))
                assert_allclose(f.solve_LDLt(b),
                                np.dot((L * D * L.T).todense().I, b))
                assert_allclose(f.solve_LD(b),
                                np.dot((L * D).todense().I, b))
                assert_allclose(f.solve_DLt(b),
                                np.dot((D * L.T).todense().I, b))
                assert_allclose(f.solve_L(b),
                                np.dot(L.todense().I, b))
                assert_allclose(f.solve_Lt(b),
                                np.dot(L.T.todense().I, b))
                assert_allclose(f.solve_D(b),
                                np.dot(D.todense().I, b))

                assert_allclose(f.apply_P(b), b[f.P(), :])
                assert_allclose(f.apply_P(b), b[f.P(), :])
                # Pt is the inverse of P, and argsort inverts permutation
                # vectors:
                assert_allclose(f.apply_Pt(b), b[np.argsort(f.P()), :])
                assert_allclose(f.apply_Pt(b), b[np.argsort(f.P()), :])
Example #7
0
    def PreCalculate(self):

        if not self.prepared:
            print 'not yet prepared!'
            self.PrepareCalculation()

        print 'Precalculate'
        #first pass - decrease the weight of the outliers
        lam = 5  #BUG

        Y = copy(self.Y[self.valid]) / self.norm
        Yerr = copy(self.Yerr.data[self.valid]) / self.norm
        Yerr *= self.deriv_trans(Y)

        Yerr[self.Yerr.mask[self.valid]] = infty

        #transform and weight by uncertainty
        self.f = self.trans(Y) / Yerr

        self.V = sp.spdiags(
            1 / Yerr, 0, self.n_points, self.n_points, format='csr') * self.M
        self.V.eliminate_zeros()  #possible issues with cholesky analyze?
        self.VV = self.V.T * self.V

        vvtrace = self.VV.diagonal().sum()
        lam = exp(16) / self.DRDR.diagonal().sum() * vvtrace
        eta = exp(11) / self.DTDT.diagonal().sum() * vvtrace
        AA = self.VV + lam * self.DRDR + eta * self.DTDT
        #print 'TRACE %.3e  %.3e'%( self.DRDR.diagonal().sum()/vvtrace,self.DTDT.diagonal().sum()/vvtrace)

        if chol_inst:
            self.Factor = analyze(AA)

        self.corrected = True

        if self.robust_fit:

            try:
                if chol_inst:
                    self.Factor.cholesky_inplace(AA)
                else:
                    self.Factor = sp.linalg.factorized(AA)
                    print 'umfpack'

            except Exception as e:
                print e
                lam = lam + 2
                self.Factor = sp.linalg.factorized(self.VV + 10**lam * self.DD)
                print 'umfpack'

            g = squeeze(self.Factor(self.V.T * self.f))

            #make a more robust fit in one iteration
            dist = self.V * g - self.f

            #Winsorizing M estimator, penalize outliers
            Yerr_corr = ((dist / 3)**2 + 1)**(1)

            self.f /= Yerr_corr
            self.V = sp.spdiags(
                1 / Yerr_corr, 0, self.n_points, self.n_points,
                format='csr') * self.V
            self.VV = self.V.T * self.V
Example #8
0
    def PreCalculate(self):

        if not self.prepared:
            self.PrepareCalculation()

        if debug:
            print('Precalculate')
            TT = time.time()

        #first pass - decrease the weight of the outliers
        #embed()

        Y = self.Y[self.valid] / self.norm
        Yerr = self.Yerr.data[self.valid] / self.norm
        Yerr *= self.deriv_trans(Y)

        Yerr[self.Yerr.mask[self.valid]] = np.infty

        #transform and weight by uncertainty
        try:
            self.f = self.trans(Y) / Yerr
        except:
            print(
                's fit error np.sum(Yerr == 0), np.sum(Yerr < 0), np.sum(~np.isfinite(self.trans(Y)))',
                np.sum(Yerr == 0), np.sum(Yerr < 0),
                np.sum(~np.isfinite(self.trans(Y))))
            raise

        self.V = sp.spdiags(
            1 / Yerr, 0, self.n_points, self.n_points, format='csr') * self.M
        self.V.eliminate_zeros()
        #VV is called "precision matrix {\displaystyle \mathbf {\Lambda } =\mathbf {\Sigma } ^{-1}}" https://en.wikipedia.org/wiki/Gaussian_process_approximations

        self.VV = self.V.T * self.V  #slow

        #heurestic method for estimate of lambda
        vvtrace = self.VV.diagonal().sum()
        lam = np.exp(16) / self.DRDR.diagonal().sum() * vvtrace
        dt_diag = self.DTDT.diagonal().sum()
        if dt_diag == 0: dt_diag = 1
        eta = np.exp(11) / dt_diag * vvtrace

        #/(self.dt/0.01)

        if self.nt_new == 1: eta = 0
        #DRDR -s 5-diagonal, DTDT is also 5 diagonal, VV 7 diagonal, AA is 9 diagonal
        AA = self.VV + lam * self.DRDR + eta * self.DTDT

        if chol_inst:
            #t=time.time()
            #TODO use METIS oly for line interated data and elm sync??
            self.Factor = analyze(
                AA, ordering_method='metis'
            )  #colamd and amd has troubles with large lower sparsity matrices
            #self.Factor = analyze(AA, ordering_method='colamd') #colamd and amd has troubles with large lower sparsity matrices

            #self.Factor.cholesky_inplace(AA)#BUG
            #print('analyze',time.time()-t)

        self.corrected = True

        if self.robust_fit:

            if chol_inst:
                self.Factor.cholesky_inplace(AA)
            else:
                self.Factor = sp.linalg.factorized(AA)
                print('umfpack')

            g = np.squeeze(self.Factor(self.V.T * self.f))

            #make a more robust fit in one iteration
            dist = self.V * g - self.f

            #Winsorizing M estimator, penalize outliers
            Yerr_corr = ((dist / 3.)**2 + 1)**(1)

            self.f /= Yerr_corr
            self.V = sp.spdiags(
                1 / Yerr_corr, 0, self.n_points, self.n_points,
                format='csr') * self.V
            self.VV = self.V.T * self.V

        if debug:
            print('Precalculate', time.time() - TT)