Loading...
Searching...
No Matches
gpr::laplace Namespace Reference

Classes

class  PsisLaplace
struct  PsisLaplaceOptions
struct  PsisLaplaceState
class  ScopedKernelState
class  SliceSampler
struct  SliceSamplerOptions
struct  SliceSamplerState
class  SteinVariational
struct  SteinVariationalConfig
class  TCovSecondOrder

Functions

double logGaussianDensity (const Eigen::VectorXd &delta, const Eigen::MatrixXd &cov_chol)
std::vector< double > selfNormalisedWeights (const std::vector< double > &log_w)
double estimateParetoKHat (const std::vector< double > &weights_descending, int M_pareto)

Function Documentation

◆ estimateParetoKHat()

double gpr::laplace::estimateParetoKHat ( const std::vector< double > & weights_descending,
int M_pareto )

Hill-style estimator of the Pareto shape k_hat on the upper-tail of the weight distribution. weights_descending must be sorted high to low (caller responsibility). M_pareto is the number of upper-tail samples to use; Vehtari 2024 recommends M_pareto = min(K/5, 3*sqrt(K)) but for K = 16 that is 3, which is too few for the production code path and the diagnostic; we use min(K-1, max(K/5, 8)) to keep the diagnostic honest. Returns 0 if M_pareto < 2 or all weights are equal.

Definition at line 58 of file PsisLaplace.cpp.

60{
61 if (M_pareto < 2 ||
62 static_cast<std::size_t>(M_pareto) > weights_descending.size()) {
63 return 0.0;
64 }
65 // Hill estimator: k_hat = (1 / (M_pareto - 1))
66 // * sum_{i = 0}^{M_pareto - 2} log(w_i / w_{M_pareto
67 // - 1})
68 // where w_0 >= w_1 >= ... >= w_{M_pareto - 1} > 0.
69 const double threshold = weights_descending[M_pareto - 1];
70 if (threshold <= 0.0) return 0.0;
71 double acc = 0.0;
72 int count = 0;
73 for (int i = 0; i < M_pareto - 1; ++i) {
74 const double w_i = weights_descending[i];
75 if (w_i <= 0.0) continue;
76 const double ratio = w_i / threshold;
77 if (ratio <= 1.0) continue; // numeric tie at the threshold
78 acc += std::log(ratio);
79 ++count;
80 }
81 if (count == 0) return 0.0;
82 return acc / static_cast<double>(count);
83}

◆ logGaussianDensity()

double gpr::laplace::logGaussianDensity ( const Eigen::VectorXd & delta,
const Eigen::MatrixXd & cov_chol )

Log-density of N(0, Sigma) at z, given the Cholesky factor L of Sigma (Sigma = L L^T) and the offset (theta - theta_map). Returns -0.5 (theta - theta_map)^T Sigma^{-1} (theta - theta_map)

  • 0.5 log det Sigma - (p/2) log(2 pi). Computed via two triangular solves on L for the quadratic term and 2 * sum log diag(L) for the log-det.

Definition at line 21 of file PsisLaplace.cpp.

23{
24 const Eigen::Index p = delta.size();
25 if (p <= 0 || cov_chol.rows() != p || cov_chol.cols() != p) {
26 return std::numeric_limits<double>::quiet_NaN();
27 }
28 // Quadratic form: delta^T Sigma^{-1} delta = ||L^{-1} delta||^2
29 const Eigen::VectorXd z =
30 cov_chol.template triangularView<Eigen::Lower>().solve(delta);
31 const double quad = z.squaredNorm();
32 // log det Sigma = 2 sum log diag(L)
33 const double log_det = 2.0 * cov_chol.diagonal().array().log().sum();
34 constexpr double LOG_2PI =
35 1.8378770664093454835606594728112352797227949472756;
36 return -0.5 * quad - 0.5 * log_det - 0.5 * static_cast<double>(p) * LOG_2PI;
37}
@ z
Definition Enums.h:28

◆ selfNormalisedWeights()

std::vector< double > gpr::laplace::selfNormalisedWeights ( const std::vector< double > & log_w)

Self-normalised importance weights. Numerically stable: subtracts max(log_w) before exp to avoid overflow. Returns weights summing to 1. If log_w is empty, returns empty.

Definition at line 39 of file PsisLaplace.cpp.

40{
41 const std::size_t K = log_w.size();
42 if (K == 0) return {};
43 const double max_lw = *std::max_element(log_w.begin(), log_w.end());
44 if (!std::isfinite(max_lw)) return std::vector<double>(K, 0.0);
45 std::vector<double> w(K);
46 double sum = 0.0;
47 for (std::size_t k = 0; k < K; ++k) {
48 const double v = std::exp(log_w[k] - max_lw);
49 w[k] = std::isfinite(v) ? v : 0.0;
50 sum += w[k];
51 }
52 if (sum <= 0.0) return std::vector<double>(K, 0.0);
53 for (std::size_t k = 0; k < K; ++k)
54 w[k] /= sum;
55 return w;
56}