Fix swapped location indices in UnivariateMaternNuggetsStationary covariance#58
Open
fonzie42 wants to merge 1 commit into
Open
Fix swapped location indices in UnivariateMaternNuggetsStationary covariance#58fonzie42 wants to merge 1 commit into
fonzie42 wants to merge 1 commit into
Conversation
…getsStationary GenerateCovarianceMatrix passed (j0, i0) to CalculateDistance instead of (i0, j0), swapping which location set each loop index addresses. Every other kernel in the project passes (i0, j0). The defect is invisible for the symmetric train-train covariance matrix (Euclidean distance is symmetric, so d(loc[i], loc[j]) == d(loc[j], loc[i])) which is why data generation and MLE are unaffected. It does corrupt the rectangular cross-covariance matrix C12 assembled during prediction, where the two location sets (observed and missing) differ: entry (i, j) ends up using d(location1[j], location2[i]) instead of d(location1[i], location2[j]), producing the transposed matrix and therefore wrong kriging predictions. Add an isolated regression test that builds a 3x3 cross-covariance between two distinct location sets and checks every entry against an independently computed reference (Matern nu=0.5 reduces to sigma^2 * exp(-d/beta)). On the old code entry (0,1) returns exp(-1) instead of exp(-3); with the fix all entries match. The existing data-generation test for this kernel continues to pass.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
UnivariateMaternNuggetsStationary::GenerateCovarianceMatrixpasses the twolocation indices to
CalculateDistancein the wrong order,(j0, i0)insteadof
(i0, j0). Every other kernel in the project passes(i0, j0).Root cause
The distance helper signature is:
CalculateDistance(Locations &loc1, Locations &loc2, idxInto_loc1, idxInto_loc2, ...)In the generation loop,
i0walks the rows (indexingaLocation1) andj0walks the columns (indexing
aLocation2), so the correct call is(..., i0, j0, ...). The nuggets kernel instead calls(..., j0, i0, ...)inboth branches.
This is invisible for the symmetric train-train covariance matrix, because
the Euclidean distance is symmetric:
d(loc[i], loc[j]) == d(loc[j], loc[i]).That is why synthetic data generation and MLE are unaffected, and why the
existing kernel test (which only checks the symmetric matrix) never caught it.
It does corrupt the rectangular cross-covariance matrix
C12assembledduring prediction, where the two location sets (observed and missing) differ.
Entry
(i, j)ends up usingd(location1[j], location2[i])instead ofd(location1[i], location2[j]), the transposed matrix, producing wrongkriging predictions whenever this kernel is used for prediction.
Fix
(applied to both branches of
GenerateCovarianceMatrix).Test
Adds
TestUnivariateMaternNuggetsCrossCovariance.cpp: it builds a 3×3cross-covariance between two distinct location sets and checks every entry
against an independently computed reference. With
nu = 0.5the Matérncovariance reduces to the exponential covariance
sigma^2 * exp(-d / beta), sothe reference values are self-evident and do not depend on any internal index
convention.
(0,1)resolves toexp(-1) ≈ 0.36788(thetransposed pair, distance 1) instead of
exp(-3) ≈ 0.04979(distance 3), sothe test distinguishes the two index orders.
(data-generation / MLE) path does not depend on the index order.
Note
Companion fix: the R-interface prediction problem-size fix (#59). The
two are independent code changes (disjoint files); both are needed together for
fully correct end-to-end kriging predictions with the nuggets kernel.