GPRidge v1.0.0
GPR-accelerated saddle point searches on potential energy surfaces
☾
Toggle main menu visibility
Loading...
Searching...
No Matches
LowRankSolve.cpp
Go to the documentation of this file.
1
// SPDX-License-Identifier: MIT
2
// Copyright (c) gpridge developers
3
4
#include "
LowRankSolve.h
"
5
6
#include <Eigen/Cholesky>
7
#include <algorithm>
8
#include <cmath>
9
#include <vector>
10
11
namespace
gpr
{
12
13
void
LowRankCholesky::factorize
(
const
Eigen::MatrixXd& K,
double
rtol,
14
int
max_rank)
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
}
47
48
Eigen::VectorXd
LowRankCholesky::solveNoisy
(
const
Eigen::VectorXd&
y
,
49
double
sigma2)
const
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
}
61
62
double
LowRankCholesky::logDetNoisy
(
double
sigma2)
const
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
}
80
81
}
// namespace gpr
y
@ y
Definition
Enums.h:28
LowRankSolve.h
gpr::LowRankCholesky::factorize
void factorize(const Eigen::MatrixXd &K, double rtol=1e-12, int max_rank=0)
Definition
LowRankSolve.cpp:13
gpr::LowRankCholesky::solveNoisy
Eigen::VectorXd solveNoisy(const Eigen::VectorXd &y, double sigma2) const
Definition
LowRankSolve.cpp:48
gpr::LowRankCholesky::logDetNoisy
double logDetNoisy(double sigma2) const
Definition
LowRankSolve.cpp:62
gpr
Gaussian-process regression, kernels, priors, and the searches that run on the surrogate.
Definition
Macros.h:16
gpr
ml
gp
LowRankSolve.cpp
Generated by
1.17.0
Generated by
Doxygen 1.17.0
Analytics by
Antics
provided by
TurtleTech ehf