Skip to content

CoxGAM with fixed smoothing rejects 8-17% of well-posed fits ("precision loss" from SciPy Newton-CG) #50

Backlog

Description

@BenSaefken

What happens

CoxGAM(...).fit(...) with fixed smoothing parameters (the default) raises

RuntimeError: Fixed-smoothing Cox coefficient fitting failed: Warning: Desired error not necessarily achieved due to precision loss.

on a share of ordinary data sets. Which data sets fail depends on the machine.

Reproduce

import warnings

import numpy as np
import pandas as pd
from nampy import CoxGAM
from nampy.survival import SurvivalTarget

warnings.simplefilter("ignore")
fails = 0
for seed in range(300):
    rng = np.random.default_rng(1000 + seed)
    n = 96
    data = pd.DataFrame({"x": rng.uniform(0, 1, n), "z": rng.uniform(0, 1, n)})
    hazard = np.exp(0.8 * np.sin(3 * data.x) + 0.5 * data.z)
    time = rng.exponential(1 / hazard)
    censor = rng.exponential(2.0, n)
    target = SurvivalTarget(np.minimum(time, censor), time <= censor)
    try:
        CoxGAM(formula="~x+z").fit(data[["x", "z"]], target)
    except RuntimeError as error:
        fails += "precision loss" in str(error)
print(fails, "of 300 fits rejected")

Linux x86-64, Python 3.12.3, NumPy 1.26.4, SciPy 1.16.3 (same counts with SciPy 1.17.1), at 7f7fafa:

formula tol rejected
~x+z default (1e-9) 48 / 300
~x+z 1e-6 30 / 300
~x+z 1e-8 37 / 300
~x+z 1e-10 50 / 300
~s(x, k=6)+z default (1e-9) 31 / 300
~s(x, k=6)+z 1e-6 24 / 300
~s(x, k=6)+z 1e-8 27 / 300
~s(x, k=6)+z 1e-10 34 / 300

The reference example docs/_examples/reference/statistical_reference/estimators/CoxGAM.py is one such case. It fails on this Linux machine (also at e770586, before #48) and on Windows (Python 3.12.16, NumPy 1.26.4, SciPy 1.17.1, see #49). On both machines it is the only one of the 410 reference examples that fails (checked at a82fcea, the head of #49).

Diagnosis

fit_cox (nampy/gam/fit/solvers/cox.py) calls scipy.optimize.minimize(method="Newton-CG") and raises when solved.success is false. Newton-CG accepts a step only if its line search measures a decrease of the objective, and it reports success only after an accepted update with L1 norm at most n * xtol.

Near the optimum a Newton step lowers the objective by about 0.5 * g' H^-1 g. For the reference example the returned point has max |gradient| 3.8e-08 and a Newton step of 7.3e-09, so the decrease is of order 1e-16, while one unit in the last place of the objective (261.666) is 5.7e-14. The computed objective at the full Newton step is in fact 5.7e-14 higher, although that step moves the point from 7.3e-09 to 3e-15 from the optimum. Acceptance therefore depends on the sign of a rounding error. SciPy returns status 2 after 4 iterations, and it does so for every tol from 1e-5 to 1e-12, because the last accepted update was still larger than n * xtol.

On the 600 default-tol fits above (both formulas):

  • 79 are rejected, although one or two plain Newton updates from the returned point reach the optimum to 6e-15 (max |gradient| 2e-14).
  • The 521 fits that SciPy reports as successful are up to 7.7e-09 from the optimum (max |gradient| 6.4e-08), which is looser than tol = 1e-9. A shortened line-search step can satisfy the update-norm rule without the iterate being that close.

Why it matters

  • A default CoxGAM fit fails on roughly every seventh ordinary data set, and on different data sets on different machines.
  • docs/scripts/check_reference_examples.py cannot complete where the reference example fails, so the reference-example records cannot be refreshed. Every PR that touches nampy/ then fails tests/docs/test_feature_reference.py::test_all_reference_examples_are_executed_and_have_output_shapes in Docs and CI. This currently blocks Add row-wise predictive scores and paired comparison intervals #49.

Possible direction

After status 2, apply the solver's own stopping rule to plain Newton updates: compute s = -H^-1 g; if ||s||_1 <= n * tol, apply it and stop; otherwise accept the update only if it reduces the gradient norm, and raise as before if it does not. The penalised negative partial log-likelihood is convex, so a point with a positive definite Hessian and a Newton update below the tolerance is the minimiser however the iteration got there. This is not a rescue of a non-converged fit: it replaces a convergence test that the objective can no longer resolve, in the spirit of d254854 for the general-family Newton solver.

In an experiment with this rule all 600 fits above are accepted, the 285 tests in tests/survival pass, all 407 reference examples pass on Linux, and the CoxGAM example reproduces its recorded preview. A draft PR with the rule and regression tests follows.

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions