Skip to content

Diabetes linear-family fitted coefficients diverge from sklearn: Lasso 6.9%, Ridge 7e-5, OLS at the collinear S1/S2 index #472

Closed

Description

@godofecht

From the substantive-disparity survey. Three related rows on diabetes:

  • Lasso: coef_max_relative_diff = 0.069, divergent from index 0. L1 problems on correlated designs are not strictly convex, so different solvers can reach different coefficient vectors with equal objective. The decisive measurement, same method as the SMO diagnosis on Close the Digits KernelSVC quality gap and isolate the libsvm performance deficit #206: compute both sides' Lasso objective (0.5/m·||y-Xw||² + alpha·||w||₁, matching each side's convention) at each side's coefficients. Equal objectives → different-but-equal optimum, declare it. Flow's worse → convergence gap in the new Gram solver (perf: Gram-matrix coordinate descent for Lasso, 74x on the canonical diabetes row #427), fix it.
  • OLS: coef_first_divergent_index = 4. Diabetes features S1/S2 correlate at ~0.9, so coefficients on those coordinates are ill-conditioned: f32 QR vs sklearn's f64 SVD can legitimately disagree there while predictions match. Measure prediction agreement and the design's condition number; if predictions match within f32, declare as conditioning.
  • Ridge: coef_max_relative_diff = 6.9e-5 — small; likely solver-path (Flow QR+alpha·I vs sklearn cholesky). Quantify and declare or fix.

This is analysis-first: post the objective/conditioning numbers to this issue before changing any code. Only fix code if a genuine convergence or correctness gap emerges.

Activity

  1. godofecht commented on Aug 21, 2026

    @godofecht
    OwnerAuthor

    Analysis: all three rows are declare, none is a Flow defect

    Measured before touching code, per the instruction in the issue. No code changed.

    Method

    Both sides' fitted coefficients are already committed at 9 significant figures in
    benchmarks/flow_results_v2.txt and benchmarks/sklearn_results_v2.txt, which is
    enough to recover the f32 values exactly. Objectives, conditioning and predictions
    were evaluated externally in numpy f64 on the shared design.

    First I checked the design matrices are the same object on both sides. Flow's
    standard_scaler_fit computes mean and variance two-pass in f64 and rounds to f32;
    sklearn's StandardScaler on a float32 input keeps mean_ in f64 and returns f32.
    Reproducing Flow's scaler in numpy against sklearn's on the canonical diabetes split:

    max |Xtr_sklearn - Xtr_flow| = 0.0
    bitwise identical: True
    

    So every difference below is the solver, not the data.


    1. Lasso: Flow's objective is lower. sklearn stopped early.

    Convention check first. Flow's lasso_fit sets threshold = alpha * m and minimises
    0.5*||y - Xc*w||^2 + threshold*||w||_1, which is (1/(2m))*||y - Xw - b||^2 + alpha*||w||_1 after dividing through by m. That is sklearn's convention exactly.
    Alpha is not scaled differently.

    Objective at each side's coefficients, alpha = 0.1, m = 353:

    objective
    Flow 1450.491383726857
    sklearn (shipped row) 1450.491461124187
    f64 reference (sklearn CD, tol=1e-14, max_iter=1e6) 1450.491383726853
    J_flow - J_ref = 3.18e-12
    J_sk   - J_ref = 7.74e-05
    

    Flow sits 3.2e-12 above the true optimum, which is the f32 output-rounding floor.
    sklearn sits 7.7e-5 above it. Subgradient (KKT) optimality on the centred design
    gives the same ordering:

    max subgradient violation, Flow    : 1.29e-06
    max subgradient violation, sklearn : 1.71e-03
    max subgradient violation, reference: 3.06e-13
    

    The shipped sklearn call is Lasso(alpha=0.1, max_iter=1000) with default tol=1e-4.
    It reports n_iter_ = 246 and dual_gap_ = 0.599. sklearn's stopping rule compares
    the duality gap against tol * ||y||^2, and ||y||^2 here is 1.05e7, so the
    threshold is 1048.81. It stopped on its own criterion long before the optimum.

    The 6.9% is coefficient index 6, the smallest coefficient in the vector:

    index 6:  Flow 0.588628   sklearn 0.547944   true optimum 0.588628
    

    Flow is on the optimum. The relative metric is large because the denominator is small.
    Per-coefficient distance from the true optimum: Flow max 3.68e-06 relative, sklearn max
    6.91e-02 relative.

    I also reproduced Flow's Gram coordinate descent sweep for sweep to check #427's note
    that the 1e-16 tolerance never fires. It does not fire; all 1000 sweeps run, ending at
    max_change = 1.43e-09, which is the f64 floor for this problem. The budget is not
    wasted though: the float32-rounded coefficient vector stops moving at sweep 993. The
    objective is within 6e-09 of optimal by sweep 500 and within 1e-12 by sweep 1000.

    after     1 sweeps: max_change=4.43e+01  objective=1715.468216063112
    after   100 sweeps: max_change=2.53e-02  objective=1450.509028468972
    after   500 sweeps: max_change=1.55e-05  objective=1450.491383733015
    after  1000 sweeps: max_change=1.43e-09  objective=1450.491383726853
    

    Held-out predictions differ by at most 0.0268 absolute (4.39e-04 relative). Test r2 is
    0.45552983 for Flow and 0.45554140 for sklearn. sklearn's early stop scores 1.16e-05
    higher on the test split, which is noise on a 89-row split and not evidence of a better
    fit; its training objective is worse by 7.7e-05.

    Verdict: no convergence gap in the #427 Gram solver. Flow reaches the optimum;
    sklearn stops at its default tolerance. Nothing to fix.


    2. OLS: ill-conditioning, and the f32/f64 roles are the other way round

    Conditioning of the centred training design:

    singular values: 37.430 22.841 21.020 18.890 15.244 14.474 13.553 12.372 5.167 1.805
    condition number: 20.73
    corr(S1, S2) = corr(x4, x5) = 0.8911   (largest off-diagonal correlation)
    

    Coefficients against the exact f64 least-squares solution:

    index      Flow            sklearn         exact f64        Flow-exact      sklearn-exact
        0      1.753758070      1.753758790      1.753757918       1.518e-07       8.718e-07
        1    -11.511808400    -11.511811300    -11.511808591       1.908e-07      -2.709e-06
        2     25.607120500     25.607139600     25.607120758      -2.580e-07       1.884e-05
        3     16.828870800     16.828872700     16.828871549      -7.486e-07       1.151e-06
        4    -44.448856400    -44.448810600    -44.448854066      -2.334e-06       4.347e-05
        5     24.640955000     24.640918700     24.640955169      -1.688e-07      -3.647e-05
        6      7.676980020      7.676937580      7.676977463       2.557e-06      -3.988e-05
        7     13.138785400     13.138752900     13.138782705       2.695e-06      -2.981e-05
        8     35.161197700     35.161186200     35.161195946       1.754e-06      -9.746e-06
        9      2.351363660      2.351366520      2.351363683      -2.276e-08       2.837e-06
    

    Flow is closer to the exact solution at nine of ten indices; max relative distance
    3.33e-07 for Flow against 5.2e-06 for sklearn. Training residual sum of squares:

    Flow    1012598.044594052
    exact   1012598.044594050
    sklearn 1012598.044594295
    

    Held-out predictions agree to 6.68e-05 absolute, 1.19e-06 relative, roughly two f32
    ulps at that magnitude. Test r2 0.45260275 (Flow) against 0.45260270 (sklearn).

    The index pattern is the conditioning signature. Injecting f32-ulp jitter into X and
    refitting 200 times gives per-coefficient standard deviations that track the observed
    divergence closely:

    index                  0        1        2        3        4        5        6        7        8        9
    jitter std      5.4e-07  6.0e-07  7.3e-07  6.0e-07  2.13e-05 1.68e-05 9.4e-06  4.1e-06  8.3e-06  6.3e-07
    observed diff   7.2e-07  2.9e-06  1.9e-05  1.9e-06  4.58e-05 3.63e-05 4.24e-05 3.25e-05 1.15e-05 2.9e-06
    

    Indices 4 through 8 are the amplified directions. Index 4 is the first to cross the
    report's 1e-9 + 1e-6 * max(|a|,|b|) divergence threshold, which is why
    coef_first_divergent_index = 4.

    One correction to the issue text. It describes this as "f32 QR vs sklearn's f64 SVD".
    It is the reverse. linear_regression_fit centres in f64 and runs Householder QR on the
    centred f64 design, narrowing to f32 only when it writes weights. sklearn's
    LinearRegression passes the input dtype straight to scipy.linalg.lstsq, and the
    canonical harness feeds it float32, so sklearn's coef_ comes back float32 and its
    lstsq ran in single precision. Refitting sklearn locally on the identical float32
    input reproduces this: its f32 answer sits 7.02e-06 relative from its own f64 answer,
    and it differs from the committed sklearn run at the same magnitude, so this row's f32
    noise is BLAS- and machine-dependent on sklearn's side.

    Verdict: ill-conditioning amplifying sklearn's single-precision lstsq. Predictions
    agree within f32. Nothing to fix.


    3. Ridge: alpha convention matches. The 6.9e-5 is sklearn's f32 Cholesky.

    Convention checked explicitly, since this is where #430 started. ridge_fit builds
    Xc^T Xc from the centred design, adds alpha (not alpha * m) to the diagonal, and
    solves in f64. sklearn Ridge penalises alpha * ||w||^2 against the sum-of-squares
    loss. Running sklearn's Ridge(alpha=1.0) on the same design in f64 and comparing to
    Flow's centred normal equations solved in f64:

    max relative difference: 1.98e-14
    

    Identical problem. No convention mismatch.

    Against that exact f64 solution:

    index      Flow            sklearn        exact f64        Flow-exact      sklearn-exact
        0      1.807341810      1.807345150      1.807341718       9.220e-08       3.432e-06
        1    -11.448188800    -11.448195500    -11.448189025       2.254e-07      -6.475e-06
        2     25.732698400     25.732702300     25.732698188       2.122e-07       4.112e-06
        3     16.734298700     16.734304400     16.734299552      -8.520e-07       4.848e-06
        4    -34.671955100    -34.671527900    -34.671953363      -1.737e-06       4.255e-04
        5     17.053077700     17.052787800     17.053076663       1.037e-06      -2.889e-04
        6      3.369915960      3.369683980      3.369913939       2.021e-06      -2.300e-04
        7     11.764261200     11.764143000     11.764259282       1.918e-06      -1.163e-04
        8     31.378385500     31.378208200     31.378384648       8.523e-07      -1.764e-04
        9      2.458139180      2.458141800      2.458139212      -3.221e-08       2.588e-06
    

    Distance from exact, measured in f32 ulps:

    Flow    :   0.77   0.24   0.11   0.45   0.46   0.54   8.48   2.01   0.45   0.14
    sklearn :  28.8    6.8    2.2    2.5  111.5  151.4  964.5  121.9   92.5   10.9
    

    Flow is at or under one f32 ulp at eight of ten indices. The coef_max_relative_diff = 6.9e-5 is index 6, where sklearn's shipped f32 run is 964 ulps from the exact answer
    and Flow is 8.5. Ridge objective ||y - Xw - b||^2 + ||w||^2:

    Flow    1016924.416285071
    exact   1016924.416285069
    sklearn 1016924.416287021
    

    Held-out predictions differ by 2.15e-04 absolute, 2.86e-06 relative. Test r2 0.45414651
    (Flow) against 0.45414656 (sklearn).

    Verdict: neither f32 storage on Flow's side nor an alpha convention mismatch. It is
    sklearn's Cholesky running in float32 on a float32 input. Flow keeps the whole solve in
    f64 and narrows once at the end. Nothing to fix.


    Summary

    row reported metric cause Flow vs sklearn accuracy
    Lasso coef_max_relative_diff = 0.069 sklearn stops at tol=1e-4 after 246 iterations Flow objective lower by 7.74e-05; Flow is on the optimum to 3.2e-12
    LinearRegression coef_first_divergent_index = 4 ill-conditioned directions (corr(S1,S2) = 0.891) amplifying sklearn's f32 lstsq Flow 3.33e-07 from exact, sklearn 5.2e-06; predictions agree to 1.19e-06
    Ridge coef_max_relative_diff = 6.9e-5 sklearn's f32 Cholesky Flow under 1 f32 ulp at 8/10 indices, sklearn up to 964 ulps

    No genuine convergence or correctness gap. No PR. The canonical RESULT| records are
    untouched: LinearRegression r2 0.452602565, Ridge 0.454146445, Lasso 0.455529690.

    On how to record it: I did not build the KMeans-style residual_semantic_differences
    plumbing. That pattern needs a committed audit JSON plus a hardcoded branch in
    generate_disparity_report.py, and all three of these rows already pass their gates
    (parity verified for LinearRegression, approximately equivalent for Ridge and
    Lasso), so nothing is red and the plumbing would be documentation only. If a durable
    record is wanted, the cheaper form is a short note in benchmarks/README.md next to the
    existing model-state diagnostics section saying that these three
    model_state_diagnostics entries are sklearn-side precision and tolerance artefacts,
    with a link back here. Happy to do that as a small follow-up if you want it.

  2. added a commit that references this issue on Aug 21, 2026
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