Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/gstools/covmodel/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import numpy as np
from hankel import SymmetricFourierTransform as SFT
from scipy import special as sps
from scipy.optimize import root
from scipy.optimize import root_scalar

from gstools.tools.geometric import no_of_angles, set_angles, set_anis
from gstools.tools.misc import list_format
Expand Down Expand Up @@ -439,8 +439,12 @@ def percentile_scale(model, per=0.9):
def curve(x):
return 1.0 - model.correlation(x) - per

# take 'per * len_rescaled' as initial guess
return root(curve, per * model.len_rescaled)["x"][0]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a return abs(root(curve, per * model.len_rescaled)["x"][0]) would be sufficient as the correlation function is symmetric around 0.

# upper bound for bracket where curve(b) > 0
b = float(max(model.len_rescaled, 1e-5))
while curve(b) <= 0:
b *= 2.0

return root_scalar(curve, bracket=[0.0, b], method="brentq").root


def set_arg_bounds(model, check_args=True, **kwargs):
Expand Down
6 changes: 6 additions & 0 deletions tests/test_covmodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,12 @@ def test_covmodel_class(self):
self.assertRaises(ValueError, Gau_fix, latlon=True)
# check inputs
self.assertRaises(ValueError, model_std.percentile_scale, per=-1.0)
# check positive percentile scale for SuperSpherical across nu values
for nu in (0.5, 1.0, 2.0, 5.0, 10.0):
m = SuperSpherical(dim=2, nu=nu, len_scale=10.0)
ps = m.percentile_scale(0.9)
self.assertGreater(ps, 0.0)
self.assertAlmostEqual(m.correlation(ps), 0.1, places=5)
self.assertRaises(ValueError, Gaussian, anis=-1.0)
self.assertRaises(ValueError, Gaussian, len_scale=[1, -1])
self.assertRaises(ValueError, check_arg_in_bounds, model_std, "wrong")
Expand Down
Loading