Skip to content

fix(covmodel): ensure positive percentile_scale using bounded brentq - #419

Open
n0228a wants to merge 1 commit into
GeoStat-Framework:mainfrom
n0228a:percentile-fix
Open

fix(covmodel): ensure positive percentile_scale using bounded brentq#419
n0228a wants to merge 1 commit into
GeoStat-Framework:mainfrom
n0228a:percentile-fix

Conversation

@n0228a

@n0228a n0228a commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

fix(covmodel): ensure positive percentile_scale using bounded brentq

Problem

CovModel.percentile_scale(per) returned negative distance scales (e.g. -6.215 for SuperSpherical with nu >= 2). A percentile scale represents physical distance and must be positive ($r \ge 0$).

Cause

  • Correlation functions are even $\rho(-r) = \rho(r)$, making $1 - \rho(r) - \text{per} = 0$ symmetric with roots at $+r_0$ and $-r_0$.
  • percentile_scale used unconstrained scipy.optimize.root.
  • For models with sharp drop-offs (SuperSpherical with $\nu \ge 2$), flat gradients near $x_0 = 0.9 \cdot \ell$ caused the solver to overshoot across zero and converge onto the mirror root $-r_0$.

Fix

Replace scipy.optimize.root with scipy.optimize.root_scalar(method="brentq") bounded on $[0, b]$:

  • Constrains the search to $r \ge 0$.
  • Dynamically expands upper bound $b$ until $\text{curve}(b) > 0$.
  • Guarantees convergence without derivative sensitivity.

Verification

import gstools as gs
for nu in (0.5, 1.0, 2.0, 5.0):
    m = gs.SuperSpherical(dim=2, nu=nu, len_scale=10.0)
    print(nu, m.percentile_scale(0.9))
  • Before: nu=2.0 -> -6.215, nu=5.0 -> -4.575
  • After: nu=2.0 -> +6.215, nu=5.0 -> +4.575

Test suite: pytest tests/test_covmodel.py (7/7 passed).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant