5#ifndef LINE_API_PFQN_LAPLACEAPPROX_H
6#define LINE_API_PFQN_LAPLACEAPPROX_H
57std::vector<T> num_grad(
const std::function<T(
const std::vector<T>&)>& f,
const std::vector<T>& X,
59 std::vector<T> df(X.size());
60 for (std::size_t i = 0; i < X.size(); ++i) {
61 std::vector<T> x1 = X, x2 = X;
64 df[i] = T(T(f(x2) - f(x1)) / T(num_traits<T>::from_int(2) * h));
71Matrix<T> num_hess(
const std::function<T(
const std::vector<T>&)>& f,
const std::vector<T>& X,
73 const std::size_t n = X.size();
74 Matrix<T> H(n, n, num_traits<T>::from_int(0));
75 for (std::size_t i = 0; i < n; ++i) {
76 std::vector<T> x1 = X, x2 = X;
79 const std::vector<T> df1 = num_grad<T>(f, x1, h);
80 const std::vector<T> df2 = num_grad<T>(f, x2, h);
81 for (std::size_t j = 0; j < n; ++j)
82 H(i, j) = T(T(df2[j] - df1[j]) / T(num_traits<T>::from_int(2) * h));
107 const std::vector<T>& x0) {
109 "laplaceapprox requires transcendental arithmetic (log, sqrt, (2 pi)^{d/2})");
112 const std::size_t d = x0.size();
113 if (d == 0)
throw InputError(
"laplaceapprox: empty expansion point");
114 const std::function<T(
const std::vector<T>&)> logh = [&h](
const std::vector<T>& x) {
119 const double steps[3] = {1e-5, 1e-4, 1e-3};
123 for (
int k = 0; k < 3; ++k) {
126 for (std::size_t i = 0; i < d; ++i)
127 for (std::size_t j = 0; j < d; ++j) nH(i, j) = T(-H(i, j));
128 detnH = detail::pfqn_det(nH);
144 : T(h0 * sqrt(T(
num_pow_int(twopi,
static_cast<unsigned>(d)) / detnH)));
NumericError(const std::string &what)
The exception types the port throws.
Dense matrix and non-owning view.
LaplaceResult< T > laplaceapprox(const std::function< T(const std::vector< T > &)> &h, const std::vector< T > &x0)
Laplace approximation of a multidimensional integral around a given point.
T num_pow_int(const T &base, unsigned e)
Integer power, valid in any field (no transcendental requirement).
Number-type abstraction for the templated API port.
Shared scalar machinery for the integration / asymptotic members of the pfqn family (pfqn_le,...
Return value of laplaceapprox, mirroring [I, H, logI].
bool detNegative
true when det(-H) stayed negative (MATLAB warns)