class gpr::StableBasisND

Overview

#include <gpr/ml/gp/StableBasis.h>
 
class StableBasisND {
public:
    // structs
 
    struct AffineVariancePrior;
    struct BorderedCacheState;
    struct BorderedFactorCache;
    struct BorderedRmCache;
    struct SchurFloorReport;
    struct TrainingNoiseReport;
 
    // construction
 
    StableBasisND();
    StableBasisND(std::shared_ptr<linalg::BackendContext> context);
 
    // methods
 
    void setChopNoiseVariance(double sigma2);
    double chopNoiseVariance() const;
    double droppedEigenvalueMax() const;
    double covarianceComplementEigenvalue() const;
    double complementWeight(double sigma2) const;
    double covarianceComplementWeight(double sigma2) const;
    void recordDroppedSpectrum(const Eigen::VectorXd& S);
    void factorize(const Eigen::MatrixXd& X, const Eigen::VectorXd& eps, double alpha, int max_degree, bool gradient_enhanced, double sv_rtol = 1e-13);
    void factorizeGPCartesian(const Eigen::MatrixXd& X, const Eigen::VectorXd& eps, double alpha, double overall_scale, int max_degree, const std::vector<Eigen::MatrixXd>& jacobians, bool component_major = false, double sv_rtol = 1e-13, int sketch_size = 0, int n_power_iter = 2, double mode_prune_rtol = 0.0, double sv_abs_tol = 0.0);
    void factorizeGPCartesianAffine(const Eigen::MatrixXd& U, const Eigen::VectorXd& eps, double alpha, double overall_scale, int max_degree, const std::vector<Eigen::MatrixXd>& jac_tangent, const std::vector<Eigen::MatrixXd>& jac_normal, bool component_major = false, double sv_rtol = 1e-13, int sketch_size = 0, int n_power_iter = 2, double mode_prune_rtol = 0.0, double sv_abs_tol = 0.0);
    void factorizeKernel(const Eigen::MatrixXd& kernel, double sv_rtol = 1e-13, double sv_abs_tol = 0.0, double sv_chop_tol = 0.0);
    bool factorizeKernelLapackRrr(const Eigen::MatrixXd& kernel, double sv_rtol = 1e-13, double sv_abs_tol = 0.0, double sv_chop_tol = 0.0, bool allow_value_range = false);
    void factorizeNoisyKernel(const Eigen::MatrixXd& noisy_kernel, double sigma2);
    bool factorizeNoisyKernelLapackRrr(const Eigen::MatrixXd& noisy_kernel, double sigma2);
    void factorizeKernelRandomized(const Eigen::MatrixXd& kernel, int sketch_size, int n_power_iter = 2, double sv_rtol = 1e-13, double sv_abs_tol = 0.0, double sv_chop_tol = 0.0);
    void liftFromDeflatedSubspace(Eigen::Index full_rows, Eigen::MatrixXd lifted_psi, const detail::RigidDeflationState& deflation, double extra_assembly_error = 0.0);
    bool factorizeKernelAppend(const StableBasisND& old_basis, const Eigen::MatrixXd& cross_old_new, const Eigen::MatrixXd& new_kernel, double sv_rtol = 1e-13, double sv_abs_tol = 0.0, double sv_chop_tol = 0.0);
    Eigen::VectorXd solve(const Eigen::VectorXd& y) const;
    Eigen::VectorXd predictEnergy(const Eigen::MatrixXd& Xstar, const Eigen::VectorXd& b) const;
    Eigen::MatrixXd predictGradient(const Eigen::MatrixXd& Xstar, const Eigen::VectorXd& b) const;
    Eigen::VectorXd solveNoisy(const Eigen::VectorXd& y, double sigma2) const;
    Eigen::VectorXd noisySelfQuadratic(const Eigen::MatrixXd& rhs, double sigma2) const;
    Eigen::VectorXd noisyRangeSelfQuadratic(const Eigen::MatrixXd& rhs, double sigma2) const;
    Eigen::VectorXd stableRangePosteriorDiagonal(const Eigen::MatrixXd& rhs, const Eigen::VectorXd& prior, const Eigen::VectorXd& signal, double sigma2, bool check_variance = true) const;
    std::vector<BorderedVarianceInterval> noisyBorderedVarianceIntervals(const Eigen::MatrixXd& rhs, const Eigen::VectorXd& prior_diag, double sigma2) const;
    std::vector<BorderedVarianceInterval> noisyBorderedVarianceIntervals(const Eigen::MatrixXd& rhs, const Eigen::VectorXd& prior_diag, double sigma2, BorderedSchurSign sign) const;
    BorderedNoiseReport borderedNoiseReport(double sigma2) const;
    BorderedNoiseReport borderedNoiseReport(double sigma2, double output_variance_scale) const;
    BorderedNoiseReport borderedNoiseReport(double sigma2, double y_scale, double calibration) const;
    SchurFloorReport floorCertifiedSchurDiagonal(const Eigen::MatrixXd& cross, const Eigen::VectorXd& prior, double sigma2, Eigen::VectorXd& value, const std::vector<char>* skip = nullptr) const;
    double trainingRowLatentErrorBound(Eigen::Index row, double sigma2) const;
    Eigen::Index borderedKernelOrder() const;
    bool supportsBorderedVarianceInterval() const;
    Eigen::VectorXd noisyInverseDiagonal(double sigma2) const;
    Eigen::VectorXd noisyInverseDiagonalEntries(const Eigen::VectorXi& rows, double sigma2) const;
    double leaveOneOutNegativeLogPredictiveDensity(const Eigen::VectorXd& y, double sigma2) const;
    double computeVarianceCalibrationFactor(const Eigen::VectorXd& y, double sigma2) const;
    Eigen::VectorXd noisyInverseSandwichDiagonal(const Eigen::MatrixXd& dK, double sigma2) const;
    double leaveOneOutNegativeLogPredictiveDensityDirectionalDerivative(const Eigen::VectorXd& y, double sigma2, const Eigen::MatrixXd& dK) const;
    Eigen::MatrixXd noisyInverse(double sigma2) const;
    double leaveOneOutNegativeLogPredictiveDensityGrouped(const Eigen::VectorXd& y, double sigma2, const std::vector<std::vector<Eigen::Index>>& groups) const;
    double leaveOneOutNegativeLogPredictiveDensityGroupedDirectionalDerivative(const Eigen::VectorXd& y, double sigma2, const Eigen::MatrixXd& dK, const std::vector<std::vector<Eigen::Index>>& groups) const;
    Eigen::VectorXd posteriorMeanReducedCoeffs(const Eigen::VectorXd& y, double sigma2) const;
    StableBasisPosteriorWeights noisyPosteriorWeights(const Eigen::VectorXd& y, double sigma2) const;
    double logDetNoisy(double sigma2) const;
    Eigen::VectorXd posteriorMeanWeights(const Eigen::VectorXd& alpha) const;
    Eigen::VectorXd posteriorMeanEnergy(const Eigen::MatrixXd& Xstar, const Eigen::VectorXd& alpha) const;
    Eigen::MatrixXd posteriorMeanGradient(const Eigen::MatrixXd& Xstar, const Eigen::VectorXd& alpha) const;
    Eigen::VectorXd posteriorVarianceEnergy(const Eigen::MatrixXd& Xstar, double sigma2) const;
    Eigen::VectorXd posteriorVarianceDiagonal(const Eigen::MatrixXd& cross, const Eigen::VectorXd& prior, double sigma2) const;
    Eigen::MatrixXd posteriorCovariance(const Eigen::MatrixXd& cross, const Eigen::MatrixXd& prior, double sigma2) const;
    double posteriorVarianceAction(const Eigen::MatrixXd& cross, const Eigen::MatrixXd& prior, const Eigen::VectorXd& weights, double sigma2) const;
    Eigen::VectorXd predictEnergyAffine(const Eigen::MatrixXd& Ustar, const Eigen::MatrixXd& residuals, const Eigen::VectorXd& b) const;
    Eigen::VectorXd energyAffineDesignRow(const Eigen::MatrixXd& Ustar, const Eigen::MatrixXd& residuals) const;
    Eigen::MatrixXd predictGradientAffine(const Eigen::MatrixXd& Ustar, const Eigen::MatrixXd& residuals, const Eigen::MatrixXd& basis, const Eigen::VectorXd& b) const;
    Eigen::MatrixXd predictGradientHessianVectorAffine(const Eigen::MatrixXd& Ustar, const Eigen::MatrixXd& residuals, const Eigen::MatrixXd& basis, const Eigen::MatrixXd& tangent_directions, const Eigen::MatrixXd& residual_directions, const Eigen::VectorXd& b) const;
    Eigen::VectorXd posteriorVarianceEnergyAffine(const Eigen::MatrixXd& Ustar, const Eigen::MatrixXd& residuals, double sigma2) const;
    double conditionNumber() const;
    double conditionFlooredNoise(double sigma2) const;
    double varianceFlooredNoise(double sigma2) const;
    int rank() const;
    bool usedLapackRrrValueRange() const;
    const char* lastKernelFactorizeDriver() const;
    int lapackRrrFullSolvesForTesting() const;
    int lapackRrrExtensionsForTesting() const;
    bool usedLapackRrrValueRangeForTesting() const;
    bool usedFastKernelEigensolverForTesting() const;
    int borderedFactorBuildsForTesting() const;
    void assumeExtendedCertificationForTesting(bool available);
    void setRetainedSpectrumForTesting(Eigen::MatrixXd psi, Eigen::VectorXd singular_values);
    Eigen::MatrixXd energyDesignForTesting(const Eigen::MatrixXd& Xstar) const;
    Eigen::MatrixXd affineEnergyDesignForTesting(const Eigen::MatrixXd& Ustar, const Eigen::MatrixXd& residuals, Eigen::VectorXd& kself) const;
    Eigen::VectorXd rangeVarianceFromDesignForTesting(const Eigen::MatrixXd& A_E, double sigma2) const;
    const Eigen::MatrixXd& trainingDesignForTesting() const;
    void borderedEpsKForTesting(double& hi, double& lo, int& operations) const;
    const Eigen::MatrixXd& borderedKernelForTesting() const;
    double borderedAssemblyErrorForTesting() const;
    bool hasCrossCovariance() const;
    int numBasis() const;
    Eigen::Index numObs() const;
    const Eigen::MatrixXd& leftSingularVectors() const;
    const Eigen::VectorXd& singularValues() const;
    const Eigen::VectorXd& noisyEigenvalues() const;
    bool hasNoisyEigenvalues() const;
    bool permuteObservationRows(const Eigen::VectorXi& source_for_target);
    Eigen::MatrixXd reconstructKernel(bool truncated) const;
    void eigenTensorEpsDerivative(const Eigen::MatrixXd& X, int j, Eigen::MatrixXd& Phi, Eigen::MatrixXd& dPhi_eps) const;
    static std::size_t factorizationCount();
    static SchurFloorReport schurFloorTotals();
    static void resetSchurFloorTotals();
    static double floorLatentVariance(double latent, double error_bound, SchurFloorReport* report);
    static Eigen::Index standardChop(const Eigen::VectorXd& coeffs, double tol);
    static int autoMaxDegree(const Eigen::VectorXd& eps, double alpha, int dim, double target_tol, int max_degree_cap = 40);
    static bool gradedModeCountExceedsLimit(int dim, int max_degree, Eigen::Index limit);
    static int capMaxDegreeByModeCount(int dim, int requested_degree, Eigen::Index mode_limit);
    static Eigen::MatrixXd affineSpanBasis(const Eigen::MatrixXd& centered_X);
    static void projectToAffineSpan(const Eigen::MatrixXd& centered_X, const Eigen::MatrixXd& basis, Eigen::MatrixXd& coords, Eigen::MatrixXd& residuals);
    static double conditionFlooredSigma2ForTesting(const Eigen::VectorXd& singular_values, double sigma2, Eigen::Index nobs);
    static int designVarianceWorkingDigitsForTesting();
    static long long latentVarianceOperationDepthForTesting(Eigen::Index rank);
};

Detailed Documentation

Methods

void setChopNoiseVariance(double sigma2)

Likelihood variance (noise + jitter) the plateau chop must respect: a component with s^2 > chop_tol * sigma2 is never dropped, because its removal perturbs (K + sigma2 I)^-1 by s^2/sigma2 relative.

double droppedEigenvalueMax() const

Largest eigenvalue discarded by the last truncation (0 when none).

double covarianceComplementEigenvalue() const

Largest signal eigenvalue represented by the discarded covariance complement.

A complement below the eigensolver’s backward-error threshold is the null space of the retained model and therefore has eigenvalue zero.

double complementWeight(double sigma2) const

Weight of the complement (I - Psi Psi^T) in (K + sigma2 I)^-1: 1/(dropped_s2_max + sigma2), the lower end of the exact range.

double covarianceComplementWeight(double sigma2) const

Complement weight for posterior covariance: 1/(max(dropped_s2_max, n eps s2_max) + sigma2).

A dropped eigenvalue below the resolution n eps s2_max is recorded as zero, so complementWeight gives it 1/sigma2. That overshoots its exact weight 1/(s2 + sigma2) by s2/sigma2 relative, which is O(1) once sigma2 sits at the condition floor (itself ~ n eps s2_max). A joint covariance K - K* W K* with W above (K + sigma2 I)^-1 in any direction is indefinite, and the training-row identity sigma2 I - sigma2^2 W pairs a zero complement variance with a nonzero mixed entry. Bounding the dropped spectrum by the resolution keeps W <= (K + sigma2 I)^-1: every covariance block then conditions the prior Psi S^2 Psi^T + (1/w_c - sigma2)(I - Psi Psi^T) >= K on the same observations. The bound is fixed by the factorization, not by the eigensolver’s rounding, so it does not move between evaluations.

Eigen::VectorXd stableRangePosteriorDiagonal(const Eigen::MatrixXd& rhs, const Eigen::VectorXd& prior, const Eigen::VectorXd& signal, double sigma2, bool check_variance = true) const

Range-only posterior diagonal as a nonnegative eigenmode sum.

The value is prior_j - z_j^T diag(1/(S^2+sigma2)) z_j with z_j = Psi^T rhs_j, the contraction noisyRangeSelfQuadratic subtracts, evaluated as the unresolved tail prior_j - norm(S^-1 z_j)^2 plus the noise-damped terms sigma2/(S^2+sigma2) (S^-1 z_j)^2 in ExactSum. signal_j is the variance of the diagonal that entered the sum and sets the roundoff allowance of linalg::requireStablePosteriorVariance. sigma2 is floored through varianceFlooredNoise. A shape mismatch or a non-positive noise floor throws linalg::StableVarianceError. Callers that replace training-row channels with their latent identity defer the finite/nonnegative check until after that replacement.

std::vector<BorderedVarianceInterval> noisyBorderedVarianceIntervals(const Eigen::MatrixXd& rhs, const Eigen::VectorXd& prior_diag, double sigma2) const

Two-sided variational Schur variance intervals [lower, upper] for s_j = prior_diag(j) - rhs_j^T (K + sigma2 I)^(-1) rhs_j. Requires a stored kernel and a strictly positive certified minimum eigenvalue mu_min of M = K + sigma2 I at the condition-floored noise. The default entry point clamps a negative lower endpoint to zero when the upper endpoint is nonnegative and throws std::runtime_error when the upper endpoint is negative or M cannot be certified. sigma2 is floored through conditionFlooredNoise, the noise the mean, log-determinant and LOO routes invert, so M is the matrix the trained factor used. Endpoints are rounded outward to binary64.

std::vector<BorderedVarianceInterval> noisyBorderedVarianceIntervals(const Eigen::MatrixXd& rhs, const Eigen::VectorXd& prior_diag, double sigma2, BorderedSchurSign sign) const

Same enclosure. PreserveSigned keeps a negative Schur value.

BorderedNoiseReport borderedNoiseReport(double sigma2) const

Condition-floored noise for the requested sigma2, the certificate gap of the stored kernel factor and the noise the interval certifies. Builds (and caches) the factor, and throws BorderedVarianceError when it cannot be built.

BorderedNoiseReport borderedNoiseReport(double sigma2, double output_variance_scale) const

The same report in the units of an output frame scaled by output_variance_scale (a positive finite factor, y_scale^2). The certificate is decided after scaling, from the scaled endpoints: floored is rounded down and certificate_gap up to binary64, so certified holds exactly when the reported floored exceeds the reported gap. A unit gap that survives in training units can collapse under the multiply and is then not certified. requested is rounded down like floored, so floored >= requested still holds, and interval equals floored.

BorderedNoiseReport borderedNoiseReport(double sigma2, double y_scale, double calibration) const

The same report for the output frame y_scale^2 * calibration, with the scale kept in the working type: it is the outward-rounded enclosure [lower, upper] of y_scale^2 * calibration (varianceScaleEnclosure), not a binary64 y_scale * y_scale. floored and requested are rounded down through lower, certificate_gap up through upper, so the certificate is the strict comparison of the reported endpoints and agrees with the energy variance interval gate. Throws BorderedVarianceError when the enclosure is not positive and finite.

SchurFloorReport floorCertifiedSchurDiagonal(const Eigen::MatrixXd& cross, const Eigen::VectorXd& prior, double sigma2, Eigen::VectorXd& value, const std::vector<char>* skip = nullptr) const

value(j) is a computed Schur diagonal prior(j) - cross_j^T (K + sigma2 I)^(-1) cross_j. Nonnegative entries are untouched; no entry is left negative or NaN. A negative entry is replaced by the smallest nonnegative value of the outward-rounded bordered enclosure of that same quantity: 0 where the enclosure contains zero (lower <= 0 <= upper, counted as floored), and the lower endpoint where it is entirely positive (counted as raised). Where the enclosure is entirely negative the entry becomes +inf and counts as proven negative. A NaN or -inf entry, and a negative one without an enclosure, becomes +inf and counts as uncertified. skip, when given, has one flag per entry. A flagged entry has already been replaced by another certified formula (the latent training-row variance), so it is neither floored nor counted: the counts describe the entries that remain in value.

double trainingRowLatentErrorBound(Eigen::Index row, double sigma2) const

Upper bound on the binary64 rounding error of the training-row latent variance eta2 - eta2^2 q(row), q the diagonal of (K_r + eta2 I)^-1 assembled from the retained spectrum. It includes the departure of the retained row from unit norm, which the nonnegativity of the latent variance assumes. Infinite when it cannot be bounded.

bool supportsBorderedVarianceInterval() const

Whether this factorization retained the exact assembled kernel needed by noisyBorderedVarianceIntervals. Design-only factorizations are not eligible for certification.

Eigen::VectorXd posteriorVarianceDiagonal(const Eigen::MatrixXd& cross, const Eigen::VectorXd& prior, double sigma2) const

Cancellation-free posterior diagonal for arbitrary query channels.

Each entry is one ExactSum of the nonnegative unresolved tail and the noise-damped retained and resolved-complement eigenmode terms, at the noise varianceFlooredNoise(sigma2). A null complement leaves the query’s residual in the unresolved tail. An entry the rounded basis cannot represent as nonnegative is NaN; callers reject it with linalg::requireStablePosteriorVariance after any training-row identity is applied. A shape mismatch or a non-positive noise floor throws linalg::StableVarianceError.

Eigen::MatrixXd posteriorCovariance(const Eigen::MatrixXd& cross, const Eigen::MatrixXd& prior, double sigma2) const

Joint posterior covariance from the same exact eigenmode sums.

Off-diagonal entries share the diagonal’s unresolved-tail and noise-damped mode decomposition, so linear actions and covariance blocks describe one posterior. The diagonal is posteriorVarianceDiagonal and is not projected; stabilizePosteriorCovariance makes the matrix positive semidefinite once the caller has validated it. Throws linalg::StableVarianceError like posteriorVarianceDiagonal.

double posteriorVarianceAction(const Eigen::MatrixXd& cross, const Eigen::MatrixXd& prior, const Eigen::VectorXd& weights, double sigma2) const

Variance of a linear action on a joint query covariance, evaluated by the same nonnegative eigenmode decomposition.

Returns NaN when the rounded basis cannot represent the action as nonnegative and throws linalg::StableVarianceError on a shape mismatch or a non-positive noise floor.

double conditionFlooredNoise(double sigma2) const

Condition-floored likelihood noise for this basis’ retained spectrum: the same floor every mean/logdet/LOO/variance path applies, exposed so external covariance assembly inverts the SAME regularized operator.

double varianceFlooredNoise(double sigma2) const

Variance-specific likelihood-noise floor for the retained spectrum.

With the stable posterior variance on, the floor resolves one squared eigenvalue to eps*s2_max and does not grow with the observation count, because the stable variances are nonnegative eigenmode sums reduced exactly. Inverse, mean, log-determinant, LOO and the training-row identity keep conditionFlooredNoise, the n*eps floor of their rounded observation-space projector. With the option off this equals conditionFlooredNoise.

int lapackRrrFullSolvesForTesting() const

Full DSYEVR calls. A repeated kernel and a bordered extension of a cached spectrum do not increment this.

void assumeExtendedCertificationForTesting(bool available)

Force the extended-precision certificate off, or back on. On cannot succeed where the platform has neither an extended long double nor IEEE binary64 for the double-double fallback. Clears the factor cache.

void setRetainedSpectrumForTesting(Eigen::MatrixXd psi, Eigen::VectorXd singular_values)

Install a caller-built retained basis and spectrum, so a test can give the projection a basis whose columns have lost orthogonality. Only the range variance reductions, which read Psi_ and Sr_, are meaningful afterwards; every cached factorization is dropped.

Eigen::VectorXd rangeVarianceFromDesignForTesting(const Eigen::MatrixXd& A_E, double sigma2) const

The design variance of the rows of A_E at sigma2, and the retained training design it contracts against.

void borderedEpsKForTesting(double& hi, double& lo, int& operations) const

The bordered factor’s eps_K as hi + lo binary64 limbs, and the number of rounded operations the directed margin on it counts.

const Eigen::MatrixXd& borderedKernelForTesting() const

The exact stored kernel and its binary64 assembly error bound, the two inputs eps_K is derived from.

static std::size_t factorizationCount()

Process-wide count of kernel eigenfactorizations (full and append), the O(m^3) operations of this backend; read per outer iteration by the driver’s report line.

static SchurFloorReport schurFloorTotals()

Process-wide totals of SchurFloorReport over every call.

static double floorLatentVariance(double latent, double error_bound, SchurFloorReport* report)

Floors a computed latent variance. A nonnegative value, +inf included, is returned as is. A negative value whose error bound reaches 0 is roundoff and becomes 0. One whose bound stays negative is proven negative and becomes +inf. A NaN or -inf value, or a negative one whose bound is NaN, infinite or negative, has no certificate: it becomes +inf and counts as uncertified.

static double conditionFlooredSigma2ForTesting(const Eigen::VectorXd& singular_values, double sigma2, Eigen::Index nobs)

The condition floor of a descending singular spectrum, as conditionFlooredNoise computes it from the retained one.

static int designVarianceWorkingDigitsForTesting()

Significand digits of the working type the design-variance accumulation runs in.

static long long latentVarianceOperationDepthForTesting(Eigen::Index rank)

Operations a term of the training-row latent variance passes through, which trainingRowLatentErrorBound scales the unit roundoff by.