.. index:: pair: class; gpr::LaplaceINLA .. _doxid-classgpr_1_1_laplace_i_n_l_a: class gpr::LaplaceINLA ====================== .. toctree:: :hidden: enum_gpr_LaplaceINLA_Likelihood.rst Overview ~~~~~~~~ Laplace / INLA uncertainty quantifier over GP/TP hyperparameters. :ref:`More...` .. ref-code-block:: cpp :class: doxyrest-overview-code-block #include class LaplaceINLA { public: // typedefs typedef std::function`, :ref:`EigenMatrix`, Eigen::VectorXd>(const Eigen::VectorXd&theta)> :ref:`KernelBuilder`; typedef std::function :ref:`LogPrior`; // enums enum :ref:`Likelihood`; // methods void :ref:`setTheta0`(const Eigen::VectorXd& theta_star); void :ref:`setHessian`(const Eigen::MatrixXd& H); void :ref:`setHessian`(const Eigen::MatrixXd& H, const Eigen::MatrixXd& parameter_directions); void :target:`setLikelihood`(:ref:`Likelihood` lik); :ref:`Likelihood` :target:`likelihood`() const; void :ref:`setTPHyperprior`(double a, double b); void :ref:`setObservationLikelihood`(std::shared_ptr<:ref:`ObservationLikelihood`> ol); void :ref:`buildGrid`(double f0 = -1.0, bool include_corners = true); void :ref:`fit`(const Eigen::VectorXd& y, :ref:`KernelBuilder` kernel_builder, :ref:`LogPrior` log_prior); :ref:`INLAPrediction` :ref:`predict`(const Eigen::VectorXd& y, :ref:`KernelBuilder` kernel_builder) const; :ref:`INLAPrediction` :target:`predict`(const Eigen::VectorXd& y, :ref:`KernelBuilder` kb, :ref:`linalg::BackendContext`& workspace) const; const std::vector& :ref:`gridPoints`() const; const Eigen::VectorXd& :target:`gridWeights`() const; const Eigen::VectorXd& :target:`finalWeights`() const; int :target:`p`() const; int :target:`nGridPoints`() const; const Eigen::VectorXd& :ref:`theta0`() const; const Eigen::MatrixXd& :ref:`lScale`() const; Eigen::MatrixXd :ref:`hessianInverse`() const; Eigen::MatrixXd :ref:`precisionMatrix`() const; }; .. _details-classgpr_1_1_laplace_i_n_l_a: Detailed Documentation ~~~~~~~~~~~~~~~~~~~~~~ Laplace / INLA uncertainty quantifier over GP/TP hyperparameters. Usage: :ref:`LaplaceINLA ` inla; inla.setTheta0(theta_star); // MAP from SCG inla.setHessian(H); // p x p, from assembleExactHessian inla.buildGrid(); // 1 + 2p + 2^p CCD points inla.fit(y, kernel_builder, log_prior); :ref:`INLAPrediction ` p = inla.predict(y, kernel_builder); The :ref:`KernelBuilder ` callback signature takes the RAW hyperparameter vector theta directly (caller owns the log-base convention). This differs from :ref:`BayesianModelAveraging::KernelBuilder `, which takes (lengthscale, noise) in linear space; callers who want to share a kernel closure across :ref:`LaplaceINLA ` and BMA should write two adapters rather than rely on implicit coercion inside either class. LOAD-BEARING CONTRACT: the LAST element of theta is always log-noise (natural log). :ref:`LaplaceINLA ` extracts it internally for :ref:`FactorizedLikelihood::evaluateGP ` / evaluateTP. Typedefs -------- .. index:: pair: typedef; KernelBuilder .. _doxid-classgpr_1_1_laplace_i_n_l_a_1acea707419a21690ca0e3bcdc90a0ec10: .. ref-code-block:: cpp :class: doxyrest-title-code-block typedef std::function`, :ref:`EigenMatrix`, Eigen::VectorXd>(const Eigen::VectorXd&theta)> KernelBuilder Builds kernel matrices from the RAW hyperparameter vector theta. CONVENTION: the last element of theta is always log-noise (natural log). Callers own the log-base for the other components. Returns (K_XX, K_test_X, k_test_test_diag). .. index:: pair: typedef; LogPrior .. _doxid-classgpr_1_1_laplace_i_n_l_a_1a7371f7298a76c3959d7e33313a758661: .. ref-code-block:: cpp :class: doxyrest-title-code-block typedef std::function LogPrior Log prior over hyperparameters. Called at every grid point. Return 0.0 for a flat improper prior; any log density otherwise. Methods ------- .. index:: pair: function; setTheta0 .. _doxid-classgpr_1_1_laplace_i_n_l_a_1ab967b7464c8c87d3dfbd43d2608dfbdd: .. ref-code-block:: cpp :class: doxyrest-title-code-block void setTheta0(const Eigen::VectorXd& theta_star) MAP hyperparameters (log space if using the project's conventions). .. index:: pair: function; setHessian .. _doxid-classgpr_1_1_laplace_i_n_l_a_1a52973823502557843c085c9b7d0984a3: .. ref-code-block:: cpp :class: doxyrest-title-code-block void setHessian(const Eigen::MatrixXd& H) p x p observed-information Hessian at theta\*. The spectrum must contain at least one positive finite curvature direction; mixed-curvature weak directions are converted to a proper posterior precision by :ref:`setHessian() `. .. index:: pair: function; setHessian .. _doxid-classgpr_1_1_laplace_i_n_l_a_1a84cccf24650740f44b5817258f3872d0: .. ref-code-block:: cpp :class: doxyrest-title-code-block void setHessian(const Eigen::MatrixXd& H, const Eigen::MatrixXd& parameter_directions) Observed information in q free coordinates, with a full-column-rank p x q map from free increments to the ambient theta coordinates. .. index:: pair: function; setTPHyperprior .. _doxid-classgpr_1_1_laplace_i_n_l_a_1aa5c4c0d297df1e541206a44767293418: .. ref-code-block:: cpp :class: doxyrest-title-code-block void setTPHyperprior(double a, double b) TP hyperprior (only used when lik\_ == T_CLOSED_FORM). .. index:: pair: function; setObservationLikelihood .. _doxid-classgpr_1_1_laplace_i_n_l_a_1a23cda8b325523b87685c33fe4ecbe34b: .. ref-code-block:: cpp :class: doxyrest-title-code-block void setObservationLikelihood(std::shared_ptr<:ref:`ObservationLikelihood`> ol) Attach a non-Gaussian observation likelihood. Only consulted when lik\_ == NON_GAUSSIAN; the branch throws if this was never attached (fail fast rather than silently falling back to Gaussian). .. index:: pair: function; buildGrid .. _doxid-classgpr_1_1_laplace_i_n_l_a_1ae0400cce52b33dd4b07eda17912f66e4: .. ref-code-block:: cpp :class: doxyrest-title-code-block void buildGrid(double f0 = -1.0, bool include_corners = true) Build the CCD grid in theta-space from (theta\*, H). Places 1 center, 2p axial, and 2^p factorial-corner points at z = 0, +/- f0 \* e_i, and (+/- f0/sqrt(p), ..., +/- f0/sqrt(p)) in standardised z-space, then maps back to theta-space via theta_k = theta\* + U Lambda^{-1/2} z_k. Grid-rule weights w_k^grid are chosen so the rule integrates z (to zero) and z z^T (to identity) exactly against N(0, I). These match Rue et al. 2009 eq. 6.5 and the moments validated by test_ccd_grid_moments_p2 in python/tests/test_sympy_laplace.py. .. rubric:: Parameters: .. list-table:: :widths: 20 80 * - f0 - Radius in standardised space. Pass a negative value (default) to use Rue's default sqrt(p + 2). * - include_corners - Include the 2^p factorial corner points (default true). When false, only the 1 + 2p axial subset is built; the corner Cholesky factorisations that dominate per-call BMA / LAPLACE-CCD wall are skipped. Both rules preserve the Gaussian first and second moments; the corner points change higher mixed moments. At p = 4 the axial rule uses K = 9 points instead of K = 25, with weights normalized for those nine points. .. index:: pair: function; fit .. _doxid-classgpr_1_1_laplace_i_n_l_a_1a1359dec676e5dd186b3016bde978a75a: .. ref-code-block:: cpp :class: doxyrest-title-code-block void fit(const Eigen::VectorXd& y, :ref:`KernelBuilder` kernel_builder, :ref:`LogPrior` log_prior) Evaluate the per-grid-point correction rho_k = exp(-Delta phi_k) and build the final normalised weights. Needs the kernel builder so it can evaluate the true log p(y|theta_k) * log p(theta_k) at each grid point. The Rue correction ratio is rho_k = [ p(y|theta_k) \* p(theta_k) \* pi_G(theta\*) ] / [ p(y|theta\*) \* p(theta\*) \* pi_G(theta_k) ] where pi_G is the Laplace Gaussian surrogate N(theta\*, H^-1). In the linear-Gaussian limit phi is exactly quadratic and rho_k = 1 everywhere (see test_rho_k_is_one_on_quadratic_phi). .. rubric:: Parameters: .. list-table:: :widths: 20 80 * - y - Training targets. * - kernel_builder - Same callback as BMA uses. * - log_prior - Log prior over hyperparameters (pass a zero-returning lambda for a flat improper prior). .. index:: pair: function; predict .. _doxid-classgpr_1_1_laplace_i_n_l_a_1a108e6784856352b53c6c00e3d5518f39: .. ref-code-block:: cpp :class: doxyrest-title-code-block :ref:`INLAPrediction` predict(const Eigen::VectorXd& y, :ref:`KernelBuilder` kernel_builder) const Predictive moments at the test points embedded in the kernel builder's K_test_X / k_test_test outputs. .. rubric:: Returns: :ref:`INLAPrediction ` with law-of-total-variance applied to the CCD mixture over theta_k's posterior predictive. .. index:: pair: function; gridPoints .. _doxid-classgpr_1_1_laplace_i_n_l_a_1ae97d7a7822a98a51db4bc8191fbbfba5: .. ref-code-block:: cpp :class: doxyrest-title-code-block const std::vector& gridPoints() const Grid accessors (for testing). .. index:: pair: function; theta0 .. _doxid-classgpr_1_1_laplace_i_n_l_a_1a1fbea1dec4bb6a836f7ed6be702250b2: .. ref-code-block:: cpp :class: doxyrest-title-code-block const Eigen::VectorXd& theta0() const MAP hyperparameter point set by setTheta0. Used by SVGD initFromCCD. .. index:: pair: function; lScale .. _doxid-classgpr_1_1_laplace_i_n_l_a_1a12be3c943d41e0995364ef13f332eb5d: .. ref-code-block:: cpp :class: doxyrest-title-code-block const Eigen::MatrixXd& lScale() const Posterior-covariance square root L_scale = U \* Lambda^{-1/2} in z-space. The posterior covariance is H^{-1} = L_scale \* L_scale^T. Used by the 2nd-order TCOV correction in :ref:`GaussianProcessRegression::calculatePotentialLaplace `. .. index:: pair: function; hessianInverse .. _doxid-classgpr_1_1_laplace_i_n_l_a_1a57701deba9f9ae03a32cf72eac66db60: .. ref-code-block:: cpp :class: doxyrest-title-code-block Eigen::MatrixXd hessianInverse() const Ambient covariance of the posterior at theta\*. Tied directions have zero variance normal to their parameter subspace. Assembled as L_scale \* L_scale^T from the cached eigendecomposition; cheaper than calling inverse() and numerically stable because :ref:`setHessian() ` stores a strictly positive spectrum. Returns an empty matrix if setHessian was never called. .. index:: pair: function; precisionMatrix .. _doxid-classgpr_1_1_laplace_i_n_l_a_1a575020a6cdaa40ecca63d80fc635f805: .. ref-code-block:: cpp :class: doxyrest-title-code-block Eigen::MatrixXd precisionMatrix() const Effective observed-information precision used to map the CCD grid. This includes any precision-form regularization applied by :ref:`setHessian() `.