WRITE · TRACE · UNDERSTAND

Project: nested group validation with auditable outer predictions

Rows have unique id, string group, finite scalar x,y.

Read lesson: Project: nested group validation with auditable outer predictions →

Your experiment

Rows have unique id, string group, finite scalar x,y. Require at least three groups and positive finite penalties. Outer leave-one-group-out evaluation must exclude that group's rows from both tuning and fitting. For each penalty, inner leave-one-group-out validation over the remaining groups computes equal-fold mean MSE; choose (MSE,penalty) minimum. Fit centered one-feature ridge with slope=sum((x-mx)*(y-my))/(sum((x-mx)**2)+alpha), unpenalized intercept. Return oof mapping, row-weighted outer mse and folds with group, alpha, fit_ids, held_ids.

Python runs in your browser. Your draft stays on this device.

What to look for

Explain inner selection versus outer evaluation, group weighting and the final refit that would follow evaluation. Audit sample identities, not only aggregate scores. Replay follows the main orchestration function; helper computations execute and are checked separately.

Watch your code work

Ready to experiment

Write a solution or load the worked answer, then choose Trace & check.

Step through arrays, variables and training curves produced by your own Python.

About this algorithm

How Project: nested group validation with auditable outer predictions works

The idea

When you tune a penalty and report an error on the same held-out data, the reported error is too optimistic, because the tuning has already seen that data. Nested validation solves this with two loops. The outer loop holds out one group to measure error. The inner loop sees only the remaining groups and chooses the penalty.

nested_group_ridge(rows, alphas) splits by group instead of by row, so rows that belong together, such as one patient or one site, never sit on both sides of a split. For each outer group it tunes alpha by inner leave-one-group-out, refits on all remaining groups, and predicts the held-out rows. Every row gets exactly one out-of-fold prediction, and folds records which IDs were fitted and which were held.

How it works

  1. Check the rows: unique string IDs, string groups, finite x and y, at least three groups, positive penalties.
  2. For each outer group, set its rows aside and keep the others as remaining.
  3. For each alpha, hold out each remaining group in turn, fit on the rest, and average the fold errors with equal weight.
  4. Select the smallest (mse, alpha) pair, so ties go to the smaller penalty.
  5. Fit the one-feature ridge on all of remaining with that alpha and predict the outer group's rows into oof.
  6. Report the mean squared error over all rows and the folds audit of fit_ids and held_ids.

What it costs

Model fits
G·(G-1)·A + G
Per fit
O(n)
Memory
O(n)

With G groups and A penalties, every outer fold runs an inner search of G - 1 fits per penalty, so the cost grows with the square of the number of groups.

Where it is used

  • scikit-learn expresses this as cross_val_score wrapped around GridSearchCV, with LeaveOneGroupOut or GroupKFold as the splitters.
  • Medical prediction studies split by patient instead of by scan or visit, so one person's records never appear in both training and test data.
  • Brain imaging studies use leave-one-subject-out validation to estimate how a decoder performs on a person it has not seen.

Watch out for

  • Tuning alpha on all the data first and then cross-validating with the winner, which leaks every outer group into the selection.
  • Splitting by row when rows share a group, so the model is tested on near-duplicates of its training rows.
  • Reporting the best inner validation error as the result, when the outer error is the honest estimate and is usually higher.