Skip to content

Commit 501b93a

Browse files
authored
Merge pull request #264 from PyAutoLabs/feature/gaussian-kernel-pd-and-slow-skips
Guarantee GaussianKernel regularization matrix is PD
2 parents 03a3b57 + f1817af commit 501b93a

1 file changed

Lines changed: 16 additions & 1 deletion

File tree

autoarray/inversion/regularization/gaussian_kernel.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,4 +115,19 @@ def regularization_matrix_from(self, linear_obj: LinearObj, xp=np) -> np.ndarray
115115
xp=xp,
116116
)
117117

118-
return self.coefficient * xp.linalg.inv(covariance_matrix)
118+
regularization_matrix = self.coefficient * xp.linalg.inv(covariance_matrix)
119+
120+
# inv() loses exact symmetry and can introduce tiny negative eigenvalues
121+
# when the covariance matrix is near-singular (e.g. scale >> pixel
122+
# spacing). Symmetrise and add a trace-scaled diagonal jitter so the
123+
# downstream cholesky in log_det_regularization_matrix_term cannot
124+
# fail on floating-point noise.
125+
regularization_matrix = 0.5 * (regularization_matrix + regularization_matrix.T)
126+
N = regularization_matrix.shape[0]
127+
diag_mean = xp.mean(xp.diag(regularization_matrix))
128+
jitter = 1e-8 * xp.abs(diag_mean)
129+
regularization_matrix = regularization_matrix + xp.eye(
130+
N, dtype=regularization_matrix.dtype
131+
) * jitter
132+
133+
return regularization_matrix

0 commit comments

Comments
 (0)