Lawfare Group → Knowledge Institute → Lawfare Academy

← Course syllabus · Related lesson

Shrink coefficients deliberately, and keep validation outside the fit

Current complete on-site edition 6

Required teaching and complete optional models are supplied on LawfareGroup.com. Publisher and primary links are optional provenance or further reading. Florida is the contemporary state-law default; federal rules and explicitly comparative accounting frameworks are distinguished. Independent subject review and observed human learner testing remain pending.

Cumulative regression and forecasting dossier, transfer models and 30-point rubric

Preserved original teaching integrated with complete current on-site preparation. Independent human subject review and observed learner testing remain pending.

Changing the objective changes what best means

Least squares chooses coefficients to minimize the sum of squared residuals in the training data. Ridge regression instead minimizes that sum plus a penalty on coefficient size. These are different optimization questions. Ridge can have a larger training residual sum while achieving a smaller penalized objective and, in some settings, better predictions. It does not promise lower error for every future case. The penalty trades fitting flexibility against coefficient stability under a chosen parameterization.

For one predictor with an unpenalized intercept, write the objective as Σ(yi−a−bxi)²+λb², with λ≥0. This is a sum-of-squares convention. Software that divides the residual sum by n uses a differently scaled penalty parameter, so a numerical λ cannot be copied across implementations without checking the objective. The intercept represents the response level and is left unpenalized in this chapter. Penalizing it would define another estimator.

Derive a shrinkage result you can reproduce

For any slope b, minimizing over a gives a=y-bar−b x-bar. Substitute centered data into the objective. Differentiating with respect to b yields −2Sxy+2bSxx+2λb=0, so b=Sxy/(Sxx+λ). The ridge slope is the least-squares slope multiplied by Sxx/(Sxx+λ), when Sxx is positive. At λ=0 it recovers least squares; as λ grows, the slope approaches zero and fitted values approach the training response mean.

Use x=[1,2,3,4,5] and y=[2,4,5,4,5], so x-bar=3, y-bar=4, Sxx=10 and Sxy=6. Least squares gives b=0.6 and a=2.2, with residual sum 2.4. At λ=10, ridge gives b=0.3 and a=3.1. Its residual sum is 3.3 and its penalty is 10×0.3²=0.9, for objective 4.2. The least-squares coefficients evaluated in that same penalized objective give 2.4+10×0.6²=6.0. Ridge wins the criterion it actually optimizes, while least squares wins unpenalized training fit.

Units are part of a coefficient penalty

Replace x by x'=10x while retaining λ=10. Then Sx'x'=1000 and Sx'y=60, giving a coefficient 60/1010≈0.059406 on x'. On the original x scale this is a slope of about 0.594059, much closer to least squares than the earlier ridge slope 0.3. The data relationship did not change; the penalty did. Coefficient magnitude depends on predictor units, so equal numerical penalties across unscaled predictors are not automatically equal substantive restrictions.

To reproduce the earlier fitted line under x'=10x, use λ'=100λ=1000; then b'=60/2000=0.03 and 10b'=0.3. With several predictors, centering and scaling based on the training data makes the chosen penalty interpretable across numerical units. It does not make all predictors equally reliable or remove confounding. Retain the exact fitted means and scales, and transform future cases using those stored training quantities.

Choose tuning parameters without borrowing the answer

Validation estimates how a modeling procedure performs beyond the data used to fit it. Scaling, imputation, feature selection and penalty selection belong inside each training fold. If a held-out fold contributes its mean or variance to scaling, the fitted pipeline has used information from that fold. The labels need not be explicitly copied for information leakage to occur. Fit the entire preprocessing-plus-model procedure within the training portion, then apply it unchanged to the held-out portion.

Temporal prediction needs an available-information ledger rather than arbitrary random folds. Use training histories that end before the validation targets, and keep target-derived predictors or revised future data out. If many λ values and model families are tried on the same validation set, selection can adapt to that set. A genuinely untouched final period, or an appropriately nested evaluation design, supports a stronger performance claim. The procedure being evaluated includes tuning, not merely the final coefficient vector.

At x=6, the two fitted values are 5.8 for least squares and 4.9 for ridge. If the next observed y is 6, their absolute errors are 0.2 and 1.1; if it is 4, the errors are 1.8 and 0.9. These two hypothetical outcomes illustrate why no optimizer can certify prospective superiority from the training criterion alone. They are not an empirical validation study. Also remember that x=6 lies outside this training range. Regularization can stabilize coefficients while leaving extrapolation assumptions and causal identification unresolved.

Correlated predictors and prediction claims

In a multiple-predictor model, highly correlated columns can make the least-squares coefficient allocation unstable even when fitted predictions change little. Ridge changes that allocation by imposing its penalty; it does not discover a uniquely causal importance score for each correlated predictor. To evaluate a business explanation, distinguish coefficient stability, predictive performance and the causal question. A good validation result answers only the predictive question under the evaluated information and population conditions.

Progressive independent practice

Write your analysis before opening each model. Then change the stated fact and revise the reasoning.

Guided

Calculate the ridge coefficients and both objective values at λ=10.

Compare the model reasoning

Ridge gives a=3.1 and b=.3. Its SSE is 3.3 and penalized objective 4.2. Least squares gives a=2.2 and b=.6, SSE 2.4 and penalized objective 6.0.

Changed conditions

The library objective is mean squared error plus λb².

Compare the revised analysis

Convert conventions: multiplying that objective by n gives SSE+nλb². A library λ of 2 with n=5 corresponds to λ=10 in this chapter’s sum convention.

Independent

Rescale x to 10x. Determine the penalty needed to preserve the fitted ridge line.

Compare the model reasoning

The slope on the new scale is one tenth of the original; preserving the penalty contribution requires λ-prime=100λ=1000. With λ-prime=10 the original-scale slope is about .594059 instead of .3.

Changed conditions

One feature is scaled using all rows before cross-validation.

Compare the revised analysis

Refit the scaler inside each training fold. The held-out rows must not contribute preprocessing parameters, even when their outcomes are not used.

Transfer

A tuned ridge model has excellent random-fold error for a next-month demand task. Specify the audit before deployment.

Compare the model reasoning

Check timestamps and predictor vintages, recreate ordered training/validation splits, fit every transformation and tuning step inside training histories, compare admissible benchmarks and retain an untouched evaluation period.

Changed conditions

A coefficient becomes stable and is advertised as the effect of an intervention.

Compare the revised analysis

Stability is not causal identification. Examine assignment, confounding and the intervention estimand separately from the prediction procedure.

Source readings and scope

scikit-learn: Ridge — Reference for the penalized objective. Derivation, numbers and decision cases are original.

scikit-learn: common pitfalls — Reference reading on training-only preprocessing and leakage; the evaluation scenarios are original Academy material.

Reproduce the worked cases

Download the Python 3 laboratory. Uses only the standard library and synthetic data. Run locally; it does not access accounts, a network service or production records. Assertions check the stated results.