Loading...
Searching...
No Matches
gpr::LowRankCholesky Class Reference

#include <gpr/ml/gp/LowRankSolve.h>

Public Member Functions

void factorize (const Eigen::MatrixXd &K, double rtol=1e-12, int max_rank=0)
int rank () const
Eigen::Index numObs () const
const Eigen::MatrixXd & factor () const
Eigen::VectorXd solveNoisy (const Eigen::VectorXd &y, double sigma2) const
double logDetNoisy (double sigma2) const

Detailed Description

Definition at line 31 of file LowRankSolve.h.

Member Function Documentation

◆ factor()

const Eigen::MatrixXd & gpr::LowRankCholesky::factor ( ) const
inline

Definition at line 46 of file LowRankSolve.h.

47 {
48 return L_;
49 } // nobs x rank

◆ factorize()

void gpr::LowRankCholesky::factorize ( const Eigen::MatrixXd & K,
double rtol = 1e-12,
int max_rank = 0 )

Definition at line 13 of file LowRankSolve.cpp.

15{
16 const Eigen::Index n = K.rows();
17 const Eigen::Index cap =
18 max_rank > 0 ? std::min<Eigen::Index>(max_rank, n) : n;
19
20 // Greedy pivoted partial Cholesky (Harbrecht-Peters-Schneider 2012). The
21 // residual diagonal d tracks K - L L^T's diagonal; each step pivots on its
22 // largest entry and appends one Cholesky column. Stopping when the residual
23 // trace falls below rtol * trace(K) drops exactly the tail eigenvalues that
24 // carry the ill-conditioning, so the kept factor is well-conditioned.
25 Eigen::VectorXd d = K.diagonal();
26 const double trace0 = d.sum();
27 const double stop = rtol * (trace0 > 0.0 ? trace0 : 1.0);
28
29 Eigen::MatrixXd L(n, cap);
30 Eigen::Index r = 0;
31 for (; r < cap; ++r) {
32 Eigen::Index j = 0;
33 const double dmax = d.maxCoeff(&j);
34 if (!(dmax > stop)) break;
35 // column j of the residual: K(:,j) - L(:,0:r) * L(j,0:r)^T
36 Eigen::VectorXd col = K.col(j);
37 if (r > 0)
38 col.noalias() -= L.leftCols(r) * L.row(j).head(r).transpose();
39 const double piv = std::sqrt(dmax);
40 L.col(r) = col / piv;
41 // d <- d - L(:,r)^2, clamped at 0 (round-off can push slightly < 0).
42 d.noalias() -= L.col(r).cwiseAbs2();
43 d = d.cwiseMax(0.0);
44 }
45 L_ = L.leftCols(r);
46}

◆ logDetNoisy()

double gpr::LowRankCholesky::logDetNoisy ( double sigma2) const

Definition at line 62 of file LowRankSolve.cpp.

63{
64 // Matrix determinant lemma:
65 // log det(L L^T + sigma2 I)
66 // = (nobs - r) log sigma2 + log det(sigma2 I_r + L^T L).
67 const Eigen::Index n = L_.rows();
68 const Eigen::Index r = L_.cols();
69 double ld = static_cast<double>(n - r) * std::log(sigma2);
70 if (r > 0) {
71 Eigen::MatrixXd M =
72 L_.transpose() * L_ + sigma2 * Eigen::MatrixXd::Identity(r, r);
73 const Eigen::LLT<Eigen::MatrixXd> llt(M);
74 // log det(M) = 2 sum log diag(chol(M)).
75 ld +=
76 2.0 * llt.matrixL().toDenseMatrix().diagonal().array().log().sum();
77 }
78 return ld;
79}

◆ numObs()

Eigen::Index gpr::LowRankCholesky::numObs ( ) const
inline

Definition at line 42 of file LowRankSolve.h.

43 {
44 return L_.rows();
45 }

◆ rank()

int gpr::LowRankCholesky::rank ( ) const
inline

Definition at line 38 of file LowRankSolve.h.

39 {
40 return static_cast<int>(L_.cols());
41 }

◆ solveNoisy()

Eigen::VectorXd gpr::LowRankCholesky::solveNoisy ( const Eigen::VectorXd & y,
double sigma2 ) const

Definition at line 48 of file LowRankSolve.cpp.

50{
51 // (L L^T + sigma2 I)^{-1} y
52 // = (1/sigma2) [ y - L (sigma2 I_r + L^T L)^{-1} (L^T y) ].
53 const Eigen::Index r = L_.cols();
54 if (r == 0) return y / sigma2;
55 Eigen::MatrixXd M =
56 L_.transpose() * L_ + sigma2 * Eigen::MatrixXd::Identity(r, r);
57 const Eigen::LLT<Eigen::MatrixXd> llt(M);
58 const Eigen::VectorXd inner = llt.solve(L_.transpose() * y); // r
59 return (y - L_ * inner) / sigma2;
60}
@ y
Definition Enums.h:28

The documentation for this class was generated from the following files: