Skip to content

Commit

Permalink
Warn about conditioning only when necessary.
Browse files Browse the repository at this point in the history
Previously, sparse-ir would warn whenever one constructs a sampling
object with poorly chosen sampling points. However, users have used
this interface also for constructing evaluation objects, which then
resulted in a confusing warning.

This commit restricts the warning to the actually dangerous situation,
i.e., only when we use the sampling object to fit things.
  • Loading branch information
mwallerb committed Mar 24, 2024
1 parent d3c126d commit dd7c4c9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
6 changes: 6 additions & 0 deletions doc/sampling.rst
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,9 @@ Base classes

.. autoclass:: sparse_ir.sampling.DecomposedMatrix
:members:

.. autoclass:: sparse_ir.sampling.SplitDecomposedMatrix
:members:

.. autoclass:: sparse_ir.sampling.ConditioningWarning
:members:
21 changes: 13 additions & 8 deletions src/sparse_ir/sampling.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@ def evaluate(self, al, axis=None):

def fit(self, ax, axis=None):
"""Fit basis coefficients from the sparse sampling points"""
return self.matrix.lstsq(ax, axis)
matrix = self.matrix
if self.basis.is_well_conditioned and not (matrix.cond <= 1e8):
warn(f"Sampling matrix is poorly conditioned "
f"(kappa = {matrix.cond:.2g})", ConditioningWarning)

return matrix.lstsq(ax, axis)

@property
def cond(self):
Expand Down Expand Up @@ -68,9 +73,6 @@ def __init__(self, basis, sampling_points=None):
self._sampling_points = sampling_points
self._matrix = DecomposedMatrix(matrix)

if basis.is_well_conditioned and self._matrix.cond > 1e8:
warn(f"Sampling matrix is poorly conditioned "
f"(kappa = {self._matrix.cond:.2g})", ConditioningWarning)

@property
def basis(self): return self._basis
Expand Down Expand Up @@ -125,10 +127,6 @@ def __init__(self, basis, sampling_points=None, *, positive_only=False):
else:
self._matrix = DecomposedMatrix(matrix)

if basis.is_well_conditioned and self._matrix.cond > 1e8:
warn(f"Sampling matrix is poorly conditioned "
f"(kappa = {self._matrix.cond:.2g})", ConditioningWarning)

@property
def basis(self): return self._basis

Expand Down Expand Up @@ -314,6 +312,13 @@ def cond(self):


class ConditioningWarning(RuntimeWarning):
"""Warns about a poorly conditioned problem.
This warning is issued if the library detects a poorly conditioned fitting
problem. This essentially means there is a high degree of ambiguity in how
to choose the solution. One must therefore expect to lose significant
precision in the parameter values.
"""
pass


Expand Down

0 comments on commit dd7c4c9

Please sign in to comment.