Cross-Validation
Cross-validation estimates a model’s out-of-sample prediction error. It repeatedly splits the data into a training set for fitting the model and a validation set for scoring it. It then averages the validation errors. The in-sample error of a fitted model is biased downward, because the model has already seen the training data. Only error on data the model has not seen estimates how it will perform on new observations (generalization). Cross-validation is the standard tool for choosing tuning parameters. Examples are the ridge penalty , the polynomial degree and the depth of a tree. It is also the standard tool for comparing competing models and reporting an estimate of expected predictive accuracy.
The schemes differ in how they split the data:
- -fold cross-validation: partition the observations randomly into disjoint folds of roughly equal size. For each fold , train the model on the other folds and evaluate it on fold . Standard choices of are 5 or 10, both good compromises between computational cost and statistical reliability.
- Leave-one-out (LOO, ): train on all observations except one and repeat times. This gives an almost-unbiased but high-variance estimate of test error. For OLS regression it has the closed-form PRESS statistic , where is the leverage.
- Single train-test split (the holdout method): fit once and score only the held-out part. It is the cheapest scheme, but it trains on only part of the data and gives a noisy estimate. It is appropriate only when is large enough that variance is not a concern.
Two-fold cross-validation differs from a holdout split. It fits twice, swapping the roles of the two halves, and averages both scores. For -fold, the cross-validated error is
where is the fold containing observation and is the model fit on data excluding that fold. Common losses are squared error for regression and 0-1 loss for classification.Cross-Validate the Whole Pipeline happens inside the cross-validation loop. This includes every transformation, every variable selection and every hyperparameter tuning. Suppose an analyst selects the top predictors by their correlation with on the full dataset. Running -fold on the reduced set then gives CV scores that are far too optimistic. The validation fold must play no part in any choice about the model, including pre-processing choices.
Cross-validation works only if every step that touches
Intuition
The training error of a fitted model is not a reliable estimate of how it will perform on new data. A flexible model can drive training error to zero by memorizing the noise. Its predictions on new observations can still be useless. The right quantity to estimate is the expected prediction error on a fresh sample from the same distribution. Computing it directly requires data the model has not seen. Cross-validation creates such data by hiding part of the existing dataset from the fitting procedure. It scores the resulting model on the hidden part and averages across many such hide-and-score rounds.
Any single train-test split is wasteful, because it discards a portion of the data when fitting. It is also noisy, because the validation error depends on which observations happened to land in the test fold. Multiple folds reduce the noise by averaging. Rotating which fold is held out lets every observation contribute to both training and validation. Five-fold and ten-fold are the standard choices. With them each training set is most of the data, so the bias is small. The variance is still acceptable, and the cost is only model fits instead of for LOO. Cross-validation is a smoothed and de-randomized version of the holdout estimate.
The animation runs 5-fold cross-validation for ridge regression on 20 points. Each fold serves once as the validation set. The CV curve then picks by its minimum and by the one-SE rule.
\usepackage{tikz}
\usetikzlibrary{animations, arrows.meta}
\begin{document}
\definecolor{hot}{HTML}{D9483B}
\definecolor{cold}{HTML}{2F80ED}
\definecolor{mid}{HTML}{8A8A8A}
\newcommand{\pt}[1]{\filldraw[draw=black, fill=cold] (#1) circle (2.2pt);}
\newcommand{\pv}[1]{\filldraw[draw=black, fill=hot] (#1) circle (2.6pt);}
\newcommand{\pf}[1]{\filldraw[draw=black, fill=hot] (#1) circle (2.4pt);}
\newcommand{\pk}[1]{\fill (#1) circle (2.2pt);}
\begin{tikzpicture}[font=\small, >={Stealth[length=2.2mm]}]
\draw (0.00,-0.55) rectangle (6.80,3.45);
\draw[mid, dashed, thick] (0.30,1.45) -- (0.46,1.65) -- (0.61,1.84) -- (0.77,2.02) -- (0.92,2.18) -- (1.07,2.33) -- (1.23,2.46) -- (1.39,2.56) -- (1.54,2.64) -- (1.70,2.68) -- (1.85,2.70) -- (2.01,2.68) -- (2.16,2.64) -- (2.31,2.56) -- (2.47,2.46) -- (2.62,2.33) -- (2.78,2.18) -- (2.94,2.02) -- (3.09,1.84) -- (3.25,1.65) -- (3.40,1.45) -- (3.56,1.25) -- (3.71,1.06) -- (3.87,0.88) -- (4.02,0.72) -- (4.17,0.57) -- (4.33,0.44) -- (4.49,0.34) -- (4.64,0.26) -- (4.80,0.22) -- (4.95,0.20) -- (5.11,0.22) -- (5.26,0.26) -- (5.42,0.34) -- (5.57,0.44) -- (5.72,0.57) -- (5.88,0.72) -- (6.04,0.88) -- (6.19,1.06) -- (6.35,1.25) -- (6.50,1.45);
\pt{5.29,0.01}
\pt{5.31,0.49}
\pt{3.50,1.31}
\pt{2.07,2.45}
\pt{0.63,2.02}
\pt{2.68,2.60}
\pt{2.83,1.51}
\pt{0.58,1.70}
\pt{0.60,1.46}
\pt{6.49,1.38}
\pt{4.34,-0.06}
\pt{1.75,2.70}
\pt{3.00,1.93}
\pt{6.34,1.13}
\pt{5.87,0.31}
\pt{5.53,0.26}
\pt{2.73,1.82}
\pt{3.36,1.00}
\pt{4.50,0.41}
\pt{0.68,1.50}
\node at (0.68,4.97) {\footnotesize fold 1};
\node at (2.04,4.97) {\footnotesize fold 2};
\node at (3.40,4.97) {\footnotesize fold 3};
\node at (4.76,4.97) {\footnotesize fold 4};
\node at (6.12,4.97) {\footnotesize fold 5};
\node at (11.83,5.0) {validation MSE per fold, \ $\lambda = 0.1$};
\draw[->] (9.0,2.6) -- (14.87,2.6);
\draw[->] (9.0,2.6) -- (9.0,4.80);
\draw (8.92,3.25) -- (9.08,3.25) node[pos=0, left] {\footnotesize $0.05$};
\draw (8.92,3.90) -- (9.08,3.90) node[pos=0, left] {\footnotesize $0.1$};
\draw (8.92,4.55) -- (9.08,4.55) node[pos=0, left] {\footnotesize $0.15$};
\node[below] at (9.85,2.6) {\footnotesize 1};
\node[below] at (10.70,2.6) {\footnotesize 2};
\node[below] at (11.55,2.6) {\footnotesize 3};
\node[below] at (12.40,2.6) {\footnotesize 4};
\node[below] at (13.25,2.6) {\footnotesize 5};
\node[below] at (14.27,2.6) {\footnotesize mean};
\node at (12.00,1.75) {5-fold CV MSE $\pm$ 1 SE};
\draw[->] (9.0,-2.0) -- (15.50,-2.0);
\draw[->] (9.0,-2.0) -- (9.0,1.25);
\draw (8.92,-1.25) -- (9.08,-1.25) node[pos=0, left] {\footnotesize $0.1$};
\draw (8.92,-0.50) -- (9.08,-0.50) node[pos=0, left] {\footnotesize $0.2$};
\draw (8.92,0.25) -- (9.08,0.25) node[pos=0, left] {\footnotesize $0.3$};
\draw (8.92,1.00) -- (9.08,1.00) node[pos=0, left] {\footnotesize $0.4$};
\draw (9.30,-2.08) -- (9.30,-1.92) node[pos=0, below] {\footnotesize $10^{-5}$};
\draw (10.25,-2.08) -- (10.25,-1.92) node[pos=0, below] {\footnotesize $10^{-4}$};
\draw (11.20,-2.08) -- (11.20,-1.92) node[pos=0, below] {\footnotesize $10^{-3}$};
\draw (12.15,-2.08) -- (12.15,-1.92) node[pos=0, below] {\footnotesize $10^{-2}$};
\draw (13.10,-2.08) -- (13.10,-1.92) node[pos=0, below] {\footnotesize $10^{-1}$};
\draw (14.05,-2.08) -- (14.05,-1.92) node[pos=0, below] {\footnotesize $10^{0}$};
\draw (15.00,-2.08) -- (15.00,-1.92) node[pos=0, below] {\footnotesize $10^{1}$};
\node at (12.15,-2.85) {\footnotesize ridge penalty $\lambda$ (log scale)};
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 2.4s = "true", 30.55s = "true", repeats}}}]
\pf{9.85,3.03}
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 5.4s = "true", 30.55s = "true", repeats}}}]
\pf{10.70,4.08}
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 7.2s = "true", 30.55s = "true", repeats}}}]
\pf{11.55,3.14}
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 8.6s = "true", 30.55s = "true", repeats}}}]
\pf{12.40,4.44}
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 10s = "true", 30.55s = "true", repeats}}}]
\pf{13.25,2.74}
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 11.4s = "true", 30.55s = "true", repeats}}}]
\draw[thick] (14.27,3.16) -- (14.27,3.81); \draw[thick] (14.17,3.16) -- (14.37,3.16); \draw[thick] (14.17,3.81) -- (14.37,3.81);
\pk{14.27,3.48}
\node[above] at (14.27,3.83) {\footnotesize $0.068 \pm 0.025$};
\draw[dashed] (9.55,3.48) -- (14.07,3.48);
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 16.8s = "true", 30.55s = "true", repeats}}}]
\draw (9.30,-0.98) -- (9.30,0.71); \draw (9.22,-0.98) -- (9.38,-0.98); \draw (9.22,0.71) -- (9.38,0.71);
\pk{9.30,-0.14}
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 17.8s = "true", 30.55s = "true", repeats}}}]
\draw (9.78,-1.15) -- (9.78,-0.29); \draw (9.70,-1.15) -- (9.86,-1.15); \draw (9.70,-0.29) -- (9.86,-0.29);
\pk{9.78,-0.72}
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 18.25s = "true", 30.55s = "true", repeats}}}]
\draw (10.25,-1.33) -- (10.25,-0.79); \draw (10.17,-1.33) -- (10.33,-1.33); \draw (10.17,-0.79) -- (10.33,-0.79);
\pk{10.25,-1.06}
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 18.7s = "true", 30.55s = "true", repeats}}}]
\draw (10.73,-1.46) -- (10.73,-1.08); \draw (10.65,-1.46) -- (10.81,-1.46); \draw (10.65,-1.08) -- (10.81,-1.08);
\pk{10.73,-1.27}
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 19.15s = "true", 30.55s = "true", repeats}}}]
\draw (11.20,-1.52) -- (11.20,-1.27); \draw (11.12,-1.52) -- (11.28,-1.52); \draw (11.12,-1.27) -- (11.28,-1.27);
\pk{11.20,-1.39}
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 19.6s = "true", 30.55s = "true", repeats}}}]
\draw (11.68,-1.57) -- (11.68,-1.36); \draw (11.60,-1.57) -- (11.76,-1.57); \draw (11.60,-1.36) -- (11.76,-1.36);
\pk{11.68,-1.46}
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 20.05s = "true", 30.55s = "true", repeats}}}]
\draw (12.15,-1.62) -- (12.15,-1.37); \draw (12.07,-1.62) -- (12.23,-1.62); \draw (12.07,-1.37) -- (12.23,-1.37);
\pk{12.15,-1.50}
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 20.5s = "true", 30.55s = "true", repeats}}}]
\draw (12.62,-1.66) -- (12.62,-1.35); \draw (12.54,-1.66) -- (12.71,-1.66); \draw (12.54,-1.35) -- (12.71,-1.35);
\pk{12.62,-1.51}
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 20.95s = "true", 30.55s = "true", repeats}}}]
\draw (13.10,-1.68) -- (13.10,-1.30); \draw (13.02,-1.68) -- (13.18,-1.68); \draw (13.02,-1.30) -- (13.18,-1.30);
\pk{13.10,-1.49}
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 21.4s = "true", 30.55s = "true", repeats}}}]
\draw (13.57,-1.60) -- (13.57,-1.17); \draw (13.49,-1.60) -- (13.65,-1.60); \draw (13.49,-1.17) -- (13.65,-1.17);
\pk{13.57,-1.38}
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 21.85s = "true", 30.55s = "true", repeats}}}]
\draw (14.05,-1.41) -- (14.05,-0.86); \draw (13.97,-1.41) -- (14.13,-1.41); \draw (13.97,-0.86) -- (14.13,-0.86);
\pk{14.05,-1.13}
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 22.3s = "true", 30.55s = "true", repeats}}}]
\draw (14.53,-1.08) -- (14.53,-0.33); \draw (14.45,-1.08) -- (14.61,-1.08); \draw (14.45,-0.33) -- (14.61,-0.33);
\pk{14.53,-0.70}
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 22.75s = "true", 30.55s = "true", repeats}}}]
\draw (15.00,-0.40) -- (15.00,0.47); \draw (14.92,-0.40) -- (15.08,-0.40); \draw (14.92,0.47) -- (15.08,0.47);
\pk{15.00,0.04}
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 23.75s = "true", 30.55s = "true", repeats}}}]
\draw[mid, dashed, thick] (9.00,-1.35) -- (15.30,-1.35);
\node[anchor=south west] at (9.05,-1.99) {\footnotesize min $+$ 1 SE};
\draw[dashed] (12.62,-1.20) -- (12.62,1.00) node[above left, inner sep=1pt] {\footnotesize minimum};
\draw[thick] (12.62,-1.51) circle (4.5pt);
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "true", 2.4s = "false", 30.55s = "false", repeats}}}]
\fill[cold] (0.08,4.3) rectangle (1.28,4.7);
\fill[cold] (1.44,4.3) rectangle (2.64,4.7);
\fill[cold] (2.80,4.3) rectangle (4.00,4.7);
\fill[cold] (4.16,4.3) rectangle (5.36,4.7);
\fill[cold] (5.52,4.3) rectangle (6.72,4.7);
\node[anchor=west] at (-0.1,-3.4) {Shuffle the 20 samples and split them into $k = 5$ folds of 4 samples.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 2.4s = "true", 5.4s = "false", 30.55s = "false", repeats}}}]
\fill[hot] (0.08,4.3) rectangle (1.28,4.7);
\fill[cold] (1.44,4.3) rectangle (2.64,4.7);
\fill[cold] (2.80,4.3) rectangle (4.00,4.7);
\fill[cold] (4.16,4.3) rectangle (5.36,4.7);
\fill[cold] (5.52,4.3) rectangle (6.72,4.7);
\draw[very thick] (0.30,0.36) -- (0.41,0.91) -- (0.53,1.36) -- (0.64,1.73) -- (0.75,2.02) -- (0.86,2.25) -- (0.98,2.43) -- (1.09,2.55) -- (1.20,2.64) -- (1.31,2.69) -- (1.43,2.71) -- (1.54,2.71) -- (1.65,2.69) -- (1.77,2.64) -- (1.88,2.59) -- (1.99,2.52) -- (2.10,2.44) -- (2.22,2.36) -- (2.33,2.27) -- (2.44,2.17) -- (2.55,2.07) -- (2.67,1.97) -- (2.78,1.86) -- (2.89,1.75) -- (3.01,1.65) -- (3.12,1.54) -- (3.23,1.43) -- (3.34,1.32) -- (3.46,1.21) -- (3.57,1.11) -- (3.68,1.01) -- (3.79,0.91) -- (3.91,0.81) -- (4.02,0.72) -- (4.13,0.63) -- (4.25,0.55) -- (4.36,0.47) -- (4.47,0.40) -- (4.58,0.34) -- (4.70,0.28) -- (4.81,0.24) -- (4.92,0.20) -- (5.03,0.18) -- (5.15,0.17) -- (5.26,0.17) -- (5.37,0.19) -- (5.49,0.23) -- (5.60,0.28) -- (5.71,0.36) -- (5.82,0.45) -- (5.94,0.56) -- (6.05,0.69) -- (6.16,0.84) -- (6.27,1.01) -- (6.39,1.20) -- (6.50,1.39);
\draw[hot, very thick] (3.00,1.93) -- (3.00,1.65);
\draw[hot, very thick] (5.87,0.31) -- (5.87,0.49);
\draw[hot, very thick] (3.36,1.00) -- (3.36,1.31);
\draw[hot, very thick] (6.34,1.13) -- (6.34,1.12);
\pv{3.00,1.93}
\pv{5.87,0.31}
\pv{3.36,1.00}
\pv{6.34,1.13}
\node at (3.4,3.88) {\footnotesize fold 1 validates: \ MSE on its 4 red points $= 0.033$};
\node[anchor=west] at (-0.1,-3.4) {Fold 1 is held out. Fit ridge on the other 16 points; measure the error on the 4 held out.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 5.4s = "true", 7.2s = "false", 30.55s = "false", repeats}}}]
\fill[cold] (0.08,4.3) rectangle (1.28,4.7);
\fill[hot] (1.44,4.3) rectangle (2.64,4.7);
\fill[cold] (2.80,4.3) rectangle (4.00,4.7);
\fill[cold] (4.16,4.3) rectangle (5.36,4.7);
\fill[cold] (5.52,4.3) rectangle (6.72,4.7);
\draw[very thick] (0.30,0.14) -- (0.41,0.77) -- (0.53,1.27) -- (0.64,1.68) -- (0.75,1.99) -- (0.86,2.23) -- (0.98,2.41) -- (1.09,2.53) -- (1.20,2.62) -- (1.31,2.67) -- (1.43,2.69) -- (1.54,2.69) -- (1.65,2.67) -- (1.77,2.63) -- (1.88,2.58) -- (1.99,2.52) -- (2.10,2.45) -- (2.22,2.37) -- (2.33,2.29) -- (2.44,2.21) -- (2.55,2.12) -- (2.67,2.02) -- (2.78,1.93) -- (2.89,1.83) -- (3.01,1.74) -- (3.12,1.64) -- (3.23,1.55) -- (3.34,1.45) -- (3.46,1.35) -- (3.57,1.26) -- (3.68,1.17) -- (3.79,1.08) -- (3.91,0.99) -- (4.02,0.90) -- (4.13,0.82) -- (4.25,0.73) -- (4.36,0.66) -- (4.47,0.58) -- (4.58,0.51) -- (4.70,0.45) -- (4.81,0.39) -- (4.92,0.34) -- (5.03,0.30) -- (5.15,0.26) -- (5.26,0.24) -- (5.37,0.22) -- (5.49,0.23) -- (5.60,0.24) -- (5.71,0.28) -- (5.82,0.34) -- (5.94,0.43) -- (6.05,0.55) -- (6.16,0.70) -- (6.27,0.90) -- (6.39,1.15) -- (6.50,1.45);
\draw[hot, very thick] (2.83,1.51) -- (2.83,1.89);
\draw[hot, very thick] (4.34,-0.06) -- (4.34,0.67);
\draw[hot, very thick] (0.58,1.70) -- (0.58,1.48);
\draw[hot, very thick] (1.75,2.70) -- (1.75,2.64);
\pv{2.83,1.51}
\pv{4.34,-0.06}
\pv{0.58,1.70}
\pv{1.75,2.70}
\node at (3.4,3.88) {\footnotesize fold 2 validates: \ MSE on its 4 red points $= 0.114$};
\node[anchor=west] at (-0.1,-3.4) {Fold 2 is held out. Fit again on the other four folds.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 7.2s = "true", 8.6s = "false", 30.55s = "false", repeats}}}]
\fill[cold] (0.08,4.3) rectangle (1.28,4.7);
\fill[cold] (1.44,4.3) rectangle (2.64,4.7);
\fill[hot] (2.80,4.3) rectangle (4.00,4.7);
\fill[cold] (4.16,4.3) rectangle (5.36,4.7);
\fill[cold] (5.52,4.3) rectangle (6.72,4.7);
\draw[very thick] (0.30,0.03) -- (0.41,0.75) -- (0.53,1.32) -- (0.64,1.76) -- (0.75,2.11) -- (0.86,2.36) -- (0.98,2.55) -- (1.09,2.68) -- (1.20,2.76) -- (1.31,2.80) -- (1.43,2.82) -- (1.54,2.80) -- (1.65,2.76) -- (1.77,2.71) -- (1.88,2.64) -- (1.99,2.57) -- (2.10,2.48) -- (2.22,2.39) -- (2.33,2.29) -- (2.44,2.18) -- (2.55,2.08) -- (2.67,1.97) -- (2.78,1.86) -- (2.89,1.75) -- (3.01,1.63) -- (3.12,1.52) -- (3.23,1.41) -- (3.34,1.31) -- (3.46,1.20) -- (3.57,1.10) -- (3.68,0.99) -- (3.79,0.90) -- (3.91,0.80) -- (4.02,0.71) -- (4.13,0.63) -- (4.25,0.55) -- (4.36,0.47) -- (4.47,0.41) -- (4.58,0.35) -- (4.70,0.30) -- (4.81,0.25) -- (4.92,0.22) -- (5.03,0.19) -- (5.15,0.18) -- (5.26,0.18) -- (5.37,0.19) -- (5.49,0.22) -- (5.60,0.26) -- (5.71,0.32) -- (5.82,0.40) -- (5.94,0.50) -- (6.05,0.63) -- (6.16,0.78) -- (6.27,0.96) -- (6.39,1.17) -- (6.50,1.42);
\draw[hot, very thick] (5.29,0.01) -- (5.29,0.18);
\draw[hot, very thick] (5.53,0.26) -- (5.53,0.24);
\draw[hot, very thick] (0.63,2.02) -- (0.63,1.75);
\draw[hot, very thick] (0.68,1.50) -- (0.68,1.89);
\pv{5.29,0.01}
\pv{5.53,0.26}
\pv{0.63,2.02}
\pv{0.68,1.50}
\node at (3.4,3.88) {\footnotesize fold 3 validates: \ MSE on its 4 red points $= 0.041$};
\node[anchor=west] at (-0.1,-3.4) {Each sample is held out exactly once.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 8.6s = "true", 10s = "false", 30.55s = "false", repeats}}}]
\fill[cold] (0.08,4.3) rectangle (1.28,4.7);
\fill[cold] (1.44,4.3) rectangle (2.64,4.7);
\fill[cold] (2.80,4.3) rectangle (4.00,4.7);
\fill[hot] (4.16,4.3) rectangle (5.36,4.7);
\fill[cold] (5.52,4.3) rectangle (6.72,4.7);
\draw[very thick] (0.30,0.06) -- (0.41,0.76) -- (0.53,1.31) -- (0.64,1.74) -- (0.75,2.07) -- (0.86,2.32) -- (0.98,2.49) -- (1.09,2.61) -- (1.20,2.68) -- (1.31,2.71) -- (1.43,2.71) -- (1.54,2.69) -- (1.65,2.64) -- (1.77,2.58) -- (1.88,2.50) -- (1.99,2.42) -- (2.10,2.33) -- (2.22,2.23) -- (2.33,2.13) -- (2.44,2.02) -- (2.55,1.92) -- (2.67,1.81) -- (2.78,1.70) -- (2.89,1.59) -- (3.01,1.49) -- (3.12,1.38) -- (3.23,1.27) -- (3.34,1.17) -- (3.46,1.07) -- (3.57,0.97) -- (3.68,0.88) -- (3.79,0.78) -- (3.91,0.69) -- (4.02,0.61) -- (4.13,0.53) -- (4.25,0.45) -- (4.36,0.38) -- (4.47,0.31) -- (4.58,0.25) -- (4.70,0.20) -- (4.81,0.15) -- (4.92,0.12) -- (5.03,0.09) -- (5.15,0.08) -- (5.26,0.07) -- (5.37,0.09) -- (5.49,0.11) -- (5.60,0.16) -- (5.71,0.22) -- (5.82,0.31) -- (5.94,0.42) -- (6.05,0.56) -- (6.16,0.73) -- (6.27,0.93) -- (6.39,1.16) -- (6.50,1.44);
\draw[hot, very thick] (5.31,0.49) -- (5.31,0.08);
\draw[hot, very thick] (2.68,2.60) -- (2.68,1.80);
\draw[hot, very thick] (3.50,1.31) -- (3.50,1.04);
\draw[hot, very thick] (2.73,1.82) -- (2.73,1.75);
\pv{5.31,0.49}
\pv{2.68,2.60}
\pv{3.50,1.31}
\pv{2.73,1.82}
\node at (3.4,3.88) {\footnotesize fold 4 validates: \ MSE on its 4 red points $= 0.141$};
\node[anchor=west] at (-0.1,-3.4) {Each sample is held out exactly once.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 10s = "true", 11.4s = "false", 30.55s = "false", repeats}}}]
\fill[cold] (0.08,4.3) rectangle (1.28,4.7);
\fill[cold] (1.44,4.3) rectangle (2.64,4.7);
\fill[cold] (2.80,4.3) rectangle (4.00,4.7);
\fill[cold] (4.16,4.3) rectangle (5.36,4.7);
\fill[hot] (5.52,4.3) rectangle (6.72,4.7);
\draw[very thick] (0.30,0.67) -- (0.41,1.10) -- (0.53,1.47) -- (0.64,1.78) -- (0.75,2.04) -- (0.86,2.26) -- (0.98,2.42) -- (1.09,2.55) -- (1.20,2.64) -- (1.31,2.70) -- (1.43,2.73) -- (1.54,2.73) -- (1.65,2.71) -- (1.77,2.68) -- (1.88,2.63) -- (1.99,2.56) -- (2.10,2.48) -- (2.22,2.39) -- (2.33,2.30) -- (2.44,2.20) -- (2.55,2.09) -- (2.67,1.98) -- (2.78,1.87) -- (2.89,1.76) -- (3.01,1.65) -- (3.12,1.53) -- (3.23,1.42) -- (3.34,1.31) -- (3.46,1.20) -- (3.57,1.09) -- (3.68,0.98) -- (3.79,0.88) -- (3.91,0.78) -- (4.02,0.69) -- (4.13,0.60) -- (4.25,0.51) -- (4.36,0.44) -- (4.47,0.37) -- (4.58,0.30) -- (4.70,0.25) -- (4.81,0.20) -- (4.92,0.17) -- (5.03,0.14) -- (5.15,0.13) -- (5.26,0.13) -- (5.37,0.15) -- (5.49,0.18) -- (5.60,0.23) -- (5.71,0.30) -- (5.82,0.39) -- (5.94,0.50) -- (6.05,0.64) -- (6.16,0.80) -- (6.27,0.99) -- (6.39,1.21) -- (6.50,1.47);
\draw[hot, very thick] (0.60,1.46) -- (0.60,1.69);
\draw[hot, very thick] (4.50,0.41) -- (4.50,0.35);
\draw[hot, very thick] (6.49,1.38) -- (6.49,1.45);
\draw[hot, very thick] (2.07,2.45) -- (2.07,2.50);
\pv{0.60,1.46}
\pv{4.50,0.41}
\pv{6.49,1.38}
\pv{2.07,2.45}
\node at (3.4,3.88) {\footnotesize fold 5 validates: \ MSE on its 4 red points $= 0.010$};
\node[anchor=west] at (-0.1,-3.4) {Each sample is held out exactly once.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 11.4s = "true", 14.4s = "false", 30.55s = "false", repeats}}}]
\node at (3.4,3.88) {\footnotesize all 5 folds done: one validation MSE per fold};
\node[anchor=west] at (-0.1,-3.4) {The CV error for $\lambda = 0.1$ is the mean of the 5 fold errors, shown $\pm$ one standard error.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 14.4s = "true", 16.8s = "false", 30.55s = "false", repeats}}}]
\node at (3.4,3.88) {\footnotesize all 5 folds done: one validation MSE per fold};
\draw[->, thick] (14.27,2.15) to[bend left=20] (13.22,-1.34);
\draw (13.10,-1.68) -- (13.10,-1.30); \draw (13.02,-1.68) -- (13.18,-1.68); \draw (13.02,-1.30) -- (13.18,-1.30);
\pk{13.10,-1.49}
\node[anchor=west] at (-0.1,-3.4) {This mean and its standard error become one point of the CV curve.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 16.8s = "true", 17.8s = "false", 30.55s = "false", repeats}}}]
\fill[cold] (0.08,4.3) rectangle (1.28,4.7);
\fill[cold] (1.44,4.3) rectangle (2.64,4.7);
\fill[cold] (2.80,4.3) rectangle (4.00,4.7);
\fill[cold] (4.16,4.3) rectangle (5.36,4.7);
\fill[cold] (5.52,4.3) rectangle (6.72,4.7);
\draw[very thick] (0.30,3.36) -- (0.41,2.27) -- (0.53,1.78) -- (0.64,1.66) -- (0.75,1.75) -- (0.86,1.93) -- (0.98,2.13) -- (1.09,2.33) -- (1.20,2.48) -- (1.31,2.59) -- (1.43,2.65) -- (1.54,2.68) -- (1.65,2.68) -- (1.77,2.66) -- (1.88,2.62) -- (1.99,2.57) -- (2.10,2.51) -- (2.22,2.45) -- (2.33,2.38) -- (2.44,2.30) -- (2.55,2.21) -- (2.67,2.11) -- (2.78,1.99) -- (2.89,1.86) -- (3.01,1.72) -- (3.12,1.57) -- (3.23,1.41) -- (3.34,1.25) -- (3.46,1.08) -- (3.57,0.92) -- (3.68,0.77) -- (3.79,0.63) -- (3.91,0.50) -- (4.02,0.40) -- (4.13,0.32) -- (4.25,0.26) -- (4.36,0.22) -- (4.47,0.20) -- (4.58,0.19) -- (4.70,0.20) -- (4.81,0.21) -- (4.92,0.22) -- (5.03,0.24) -- (5.15,0.24) -- (5.26,0.25) -- (5.37,0.25) -- (5.49,0.24) -- (5.60,0.25) -- (5.71,0.27) -- (5.82,0.32) -- (5.94,0.41) -- (6.05,0.55) -- (6.16,0.73) -- (6.27,0.96) -- (6.39,1.20) -- (6.50,1.40);
\node at (3.4,3.88) {\footnotesize $\lambda = 10^{-5}$: \ CV MSE $= 0.249 \pm 0.113$};
\draw[thick] (9.30,-0.14) circle (4.5pt);
\node[anchor=west] at (-0.1,-3.4) {Repeat the 5 folds for every $\lambda$. Left: the fit to all 20 points with this $\lambda$.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 17.8s = "true", 18.25s = "false", 30.55s = "false", repeats}}}]
\fill[cold] (0.08,4.3) rectangle (1.28,4.7);
\fill[cold] (1.44,4.3) rectangle (2.64,4.7);
\fill[cold] (2.80,4.3) rectangle (4.00,4.7);
\fill[cold] (4.16,4.3) rectangle (5.36,4.7);
\fill[cold] (5.52,4.3) rectangle (6.72,4.7);
\draw[very thick] (0.30,2.95) -- (0.41,2.12) -- (0.53,1.75) -- (0.64,1.66) -- (0.75,1.73) -- (0.86,1.89) -- (0.98,2.06) -- (1.09,2.24) -- (1.20,2.38) -- (1.31,2.50) -- (1.43,2.58) -- (1.54,2.63) -- (1.65,2.65) -- (1.77,2.65) -- (1.88,2.62) -- (1.99,2.59) -- (2.10,2.54) -- (2.22,2.48) -- (2.33,2.40) -- (2.44,2.32) -- (2.55,2.22) -- (2.67,2.11) -- (2.78,1.99) -- (2.89,1.86) -- (3.01,1.71) -- (3.12,1.56) -- (3.23,1.40) -- (3.34,1.24) -- (3.46,1.08) -- (3.57,0.93) -- (3.68,0.78) -- (3.79,0.65) -- (3.91,0.53) -- (4.02,0.43) -- (4.13,0.34) -- (4.25,0.28) -- (4.36,0.23) -- (4.47,0.20) -- (4.58,0.19) -- (4.70,0.18) -- (4.81,0.19) -- (4.92,0.20) -- (5.03,0.21) -- (5.15,0.22) -- (5.26,0.23) -- (5.37,0.24) -- (5.49,0.25) -- (5.60,0.26) -- (5.71,0.30) -- (5.82,0.35) -- (5.94,0.44) -- (6.05,0.56) -- (6.16,0.73) -- (6.27,0.94) -- (6.39,1.18) -- (6.50,1.42);
\node at (3.4,3.88) {\footnotesize $\lambda = 10^{-4.5}$: \ CV MSE $= 0.170 \pm 0.057$};
\draw[thick] (9.78,-0.72) circle (4.5pt);
\node[anchor=west] at (-0.1,-3.4) {Repeat the 5 folds for every $\lambda$. Left: the fit to all 20 points with this $\lambda$.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 18.25s = "true", 18.7s = "false", 30.55s = "false", repeats}}}]
\fill[cold] (0.08,4.3) rectangle (1.28,4.7);
\fill[cold] (1.44,4.3) rectangle (2.64,4.7);
\fill[cold] (2.80,4.3) rectangle (4.00,4.7);
\fill[cold] (4.16,4.3) rectangle (5.36,4.7);
\fill[cold] (5.52,4.3) rectangle (6.72,4.7);
\draw[very thick] (0.30,2.52) -- (0.41,1.98) -- (0.53,1.73) -- (0.64,1.67) -- (0.75,1.72) -- (0.86,1.84) -- (0.98,1.99) -- (1.09,2.14) -- (1.20,2.28) -- (1.31,2.40) -- (1.43,2.50) -- (1.54,2.57) -- (1.65,2.61) -- (1.77,2.63) -- (1.88,2.63) -- (1.99,2.61) -- (2.10,2.56) -- (2.22,2.50) -- (2.33,2.43) -- (2.44,2.34) -- (2.55,2.23) -- (2.67,2.11) -- (2.78,1.98) -- (2.89,1.85) -- (3.01,1.70) -- (3.12,1.55) -- (3.23,1.39) -- (3.34,1.24) -- (3.46,1.09) -- (3.57,0.94) -- (3.68,0.80) -- (3.79,0.67) -- (3.91,0.56) -- (4.02,0.46) -- (4.13,0.37) -- (4.25,0.30) -- (4.36,0.25) -- (4.47,0.21) -- (4.58,0.18) -- (4.70,0.17) -- (4.81,0.16) -- (4.92,0.17) -- (5.03,0.18) -- (5.15,0.19) -- (5.26,0.21) -- (5.37,0.22) -- (5.49,0.25) -- (5.60,0.28) -- (5.71,0.32) -- (5.82,0.38) -- (5.94,0.46) -- (6.05,0.57) -- (6.16,0.72) -- (6.27,0.92) -- (6.39,1.15) -- (6.50,1.43);
\node at (3.4,3.88) {\footnotesize $\lambda = 10^{-4}$: \ CV MSE $= 0.125 \pm 0.036$};
\draw[thick] (10.25,-1.06) circle (4.5pt);
\node[anchor=west] at (-0.1,-3.4) {Repeat the 5 folds for every $\lambda$. Left: the fit to all 20 points with this $\lambda$.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 18.7s = "true", 19.15s = "false", 30.55s = "false", repeats}}}]
\fill[cold] (0.08,4.3) rectangle (1.28,4.7);
\fill[cold] (1.44,4.3) rectangle (2.64,4.7);
\fill[cold] (2.80,4.3) rectangle (4.00,4.7);
\fill[cold] (4.16,4.3) rectangle (5.36,4.7);
\fill[cold] (5.52,4.3) rectangle (6.72,4.7);
\draw[very thick] (0.30,2.29) -- (0.41,1.90) -- (0.53,1.71) -- (0.64,1.67) -- (0.75,1.71) -- (0.86,1.82) -- (0.98,1.95) -- (1.09,2.09) -- (1.20,2.23) -- (1.31,2.35) -- (1.43,2.46) -- (1.54,2.54) -- (1.65,2.60) -- (1.77,2.63) -- (1.88,2.63) -- (1.99,2.62) -- (2.10,2.58) -- (2.22,2.52) -- (2.33,2.44) -- (2.44,2.34) -- (2.55,2.23) -- (2.67,2.11) -- (2.78,1.98) -- (2.89,1.84) -- (3.01,1.69) -- (3.12,1.54) -- (3.23,1.39) -- (3.34,1.24) -- (3.46,1.09) -- (3.57,0.95) -- (3.68,0.82) -- (3.79,0.69) -- (3.91,0.58) -- (4.02,0.48) -- (4.13,0.39) -- (4.25,0.32) -- (4.36,0.26) -- (4.47,0.21) -- (4.58,0.18) -- (4.70,0.16) -- (4.81,0.15) -- (4.92,0.15) -- (5.03,0.15) -- (5.15,0.17) -- (5.26,0.19) -- (5.37,0.22) -- (5.49,0.25) -- (5.60,0.29) -- (5.71,0.34) -- (5.82,0.40) -- (5.94,0.48) -- (6.05,0.58) -- (6.16,0.72) -- (6.27,0.90) -- (6.39,1.14) -- (6.50,1.44);
\node at (3.4,3.88) {\footnotesize $\lambda = 10^{-3.5}$: \ CV MSE $= 0.097 \pm 0.025$};
\draw[thick] (10.73,-1.27) circle (4.5pt);
\node[anchor=west] at (-0.1,-3.4) {Repeat the 5 folds for every $\lambda$. Left: the fit to all 20 points with this $\lambda$.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 19.15s = "true", 19.6s = "false", 30.55s = "false", repeats}}}]
\fill[cold] (0.08,4.3) rectangle (1.28,4.7);
\fill[cold] (1.44,4.3) rectangle (2.64,4.7);
\fill[cold] (2.80,4.3) rectangle (4.00,4.7);
\fill[cold] (4.16,4.3) rectangle (5.36,4.7);
\fill[cold] (5.52,4.3) rectangle (6.72,4.7);
\draw[very thick] (0.30,2.21) -- (0.41,1.87) -- (0.53,1.71) -- (0.64,1.67) -- (0.75,1.71) -- (0.86,1.81) -- (0.98,1.94) -- (1.09,2.08) -- (1.20,2.22) -- (1.31,2.35) -- (1.43,2.45) -- (1.54,2.54) -- (1.65,2.60) -- (1.77,2.63) -- (1.88,2.64) -- (1.99,2.62) -- (2.10,2.58) -- (2.22,2.52) -- (2.33,2.44) -- (2.44,2.34) -- (2.55,2.23) -- (2.67,2.11) -- (2.78,1.97) -- (2.89,1.83) -- (3.01,1.69) -- (3.12,1.54) -- (3.23,1.39) -- (3.34,1.24) -- (3.46,1.10) -- (3.57,0.96) -- (3.68,0.83) -- (3.79,0.71) -- (3.91,0.60) -- (4.02,0.49) -- (4.13,0.41) -- (4.25,0.33) -- (4.36,0.26) -- (4.47,0.21) -- (4.58,0.18) -- (4.70,0.15) -- (4.81,0.14) -- (4.92,0.13) -- (5.03,0.14) -- (5.15,0.16) -- (5.26,0.18) -- (5.37,0.21) -- (5.49,0.25) -- (5.60,0.29) -- (5.71,0.35) -- (5.82,0.41) -- (5.94,0.49) -- (6.05,0.59) -- (6.16,0.73) -- (6.27,0.90) -- (6.39,1.13) -- (6.50,1.45);
\node at (3.4,3.88) {\footnotesize $\lambda = 10^{-3}$: \ CV MSE $= 0.081 \pm 0.017$};
\draw[thick] (11.20,-1.39) circle (4.5pt);
\node[anchor=west] at (-0.1,-3.4) {Repeat the 5 folds for every $\lambda$. Left: the fit to all 20 points with this $\lambda$.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 19.6s = "true", 20.05s = "false", 30.55s = "false", repeats}}}]
\fill[cold] (0.08,4.3) rectangle (1.28,4.7);
\fill[cold] (1.44,4.3) rectangle (2.64,4.7);
\fill[cold] (2.80,4.3) rectangle (4.00,4.7);
\fill[cold] (4.16,4.3) rectangle (5.36,4.7);
\fill[cold] (5.52,4.3) rectangle (6.72,4.7);
\draw[very thick] (0.30,2.10) -- (0.41,1.81) -- (0.53,1.69) -- (0.64,1.67) -- (0.75,1.73) -- (0.86,1.84) -- (0.98,1.98) -- (1.09,2.12) -- (1.20,2.25) -- (1.31,2.38) -- (1.43,2.48) -- (1.54,2.56) -- (1.65,2.61) -- (1.77,2.64) -- (1.88,2.64) -- (1.99,2.62) -- (2.10,2.58) -- (2.22,2.51) -- (2.33,2.43) -- (2.44,2.33) -- (2.55,2.22) -- (2.67,2.09) -- (2.78,1.96) -- (2.89,1.82) -- (3.01,1.68) -- (3.12,1.54) -- (3.23,1.39) -- (3.34,1.25) -- (3.46,1.11) -- (3.57,0.98) -- (3.68,0.85) -- (3.79,0.73) -- (3.91,0.62) -- (4.02,0.52) -- (4.13,0.43) -- (4.25,0.35) -- (4.36,0.28) -- (4.47,0.23) -- (4.58,0.19) -- (4.70,0.16) -- (4.81,0.14) -- (4.92,0.13) -- (5.03,0.13) -- (5.15,0.15) -- (5.26,0.17) -- (5.37,0.20) -- (5.49,0.24) -- (5.60,0.29) -- (5.71,0.35) -- (5.82,0.42) -- (5.94,0.50) -- (6.05,0.61) -- (6.16,0.74) -- (6.27,0.92) -- (6.39,1.14) -- (6.50,1.44);
\node at (3.4,3.88) {\footnotesize $\lambda = 10^{-2.5}$: \ CV MSE $= 0.072 \pm 0.014$};
\draw[thick] (11.68,-1.46) circle (4.5pt);
\node[anchor=west] at (-0.1,-3.4) {Repeat the 5 folds for every $\lambda$. Left: the fit to all 20 points with this $\lambda$.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 20.05s = "true", 20.5s = "false", 30.55s = "false", repeats}}}]
\fill[cold] (0.08,4.3) rectangle (1.28,4.7);
\fill[cold] (1.44,4.3) rectangle (2.64,4.7);
\fill[cold] (2.80,4.3) rectangle (4.00,4.7);
\fill[cold] (4.16,4.3) rectangle (5.36,4.7);
\fill[cold] (5.52,4.3) rectangle (6.72,4.7);
\draw[very thick] (0.30,1.73) -- (0.41,1.62) -- (0.53,1.61) -- (0.64,1.68) -- (0.75,1.80) -- (0.86,1.94) -- (0.98,2.08) -- (1.09,2.23) -- (1.20,2.35) -- (1.31,2.46) -- (1.43,2.55) -- (1.54,2.61) -- (1.65,2.65) -- (1.77,2.66) -- (1.88,2.65) -- (1.99,2.61) -- (2.10,2.56) -- (2.22,2.49) -- (2.33,2.40) -- (2.44,2.30) -- (2.55,2.19) -- (2.67,2.07) -- (2.78,1.94) -- (2.89,1.81) -- (3.01,1.67) -- (3.12,1.54) -- (3.23,1.40) -- (3.34,1.27) -- (3.46,1.14) -- (3.57,1.01) -- (3.68,0.89) -- (3.79,0.77) -- (3.91,0.66) -- (4.02,0.56) -- (4.13,0.47) -- (4.25,0.39) -- (4.36,0.32) -- (4.47,0.26) -- (4.58,0.21) -- (4.70,0.17) -- (4.81,0.14) -- (4.92,0.13) -- (5.03,0.12) -- (5.15,0.13) -- (5.26,0.15) -- (5.37,0.18) -- (5.49,0.22) -- (5.60,0.27) -- (5.71,0.34) -- (5.82,0.42) -- (5.94,0.52) -- (6.05,0.64) -- (6.16,0.78) -- (6.27,0.95) -- (6.39,1.16) -- (6.50,1.42);
\node at (3.4,3.88) {\footnotesize $\lambda = 10^{-2}$: \ CV MSE $= 0.067 \pm 0.017$};
\draw[thick] (12.15,-1.50) circle (4.5pt);
\node[anchor=west] at (-0.1,-3.4) {Repeat the 5 folds for every $\lambda$. Left: the fit to all 20 points with this $\lambda$.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 20.5s = "true", 20.95s = "false", 30.55s = "false", repeats}}}]
\fill[cold] (0.08,4.3) rectangle (1.28,4.7);
\fill[cold] (1.44,4.3) rectangle (2.64,4.7);
\fill[cold] (2.80,4.3) rectangle (4.00,4.7);
\fill[cold] (4.16,4.3) rectangle (5.36,4.7);
\fill[cold] (5.52,4.3) rectangle (6.72,4.7);
\draw[very thick] (0.30,1.02) -- (0.41,1.25) -- (0.53,1.48) -- (0.64,1.71) -- (0.75,1.92) -- (0.86,2.11) -- (0.98,2.27) -- (1.09,2.41) -- (1.20,2.52) -- (1.31,2.60) -- (1.43,2.65) -- (1.54,2.68) -- (1.65,2.69) -- (1.77,2.67) -- (1.88,2.64) -- (1.99,2.59) -- (2.10,2.52) -- (2.22,2.44) -- (2.33,2.35) -- (2.44,2.25) -- (2.55,2.14) -- (2.67,2.02) -- (2.78,1.91) -- (2.89,1.78) -- (3.01,1.66) -- (3.12,1.54) -- (3.23,1.41) -- (3.34,1.29) -- (3.46,1.17) -- (3.57,1.05) -- (3.68,0.94) -- (3.79,0.83) -- (3.91,0.73) -- (4.02,0.63) -- (4.13,0.54) -- (4.25,0.45) -- (4.36,0.38) -- (4.47,0.31) -- (4.58,0.25) -- (4.70,0.20) -- (4.81,0.17) -- (4.92,0.14) -- (5.03,0.12) -- (5.15,0.12) -- (5.26,0.13) -- (5.37,0.16) -- (5.49,0.20) -- (5.60,0.25) -- (5.71,0.33) -- (5.82,0.42) -- (5.94,0.53) -- (6.05,0.66) -- (6.16,0.81) -- (6.27,0.98) -- (6.39,1.18) -- (6.50,1.40);
\node at (3.4,3.88) {\footnotesize $\lambda = 10^{-1.5}$: \ CV MSE $= 0.066 \pm 0.021$};
\draw[thick] (12.62,-1.51) circle (4.5pt);
\node[anchor=west] at (-0.1,-3.4) {Repeat the 5 folds for every $\lambda$. Left: the fit to all 20 points with this $\lambda$.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 20.95s = "true", 21.4s = "false", 30.55s = "false", repeats}}}]
\fill[cold] (0.08,4.3) rectangle (1.28,4.7);
\fill[cold] (1.44,4.3) rectangle (2.64,4.7);
\fill[cold] (2.80,4.3) rectangle (4.00,4.7);
\fill[cold] (4.16,4.3) rectangle (5.36,4.7);
\fill[cold] (5.52,4.3) rectangle (6.72,4.7);
\draw[very thick] (0.30,0.33) -- (0.41,0.90) -- (0.53,1.36) -- (0.64,1.73) -- (0.75,2.03) -- (0.86,2.26) -- (0.98,2.44) -- (1.09,2.57) -- (1.20,2.66) -- (1.31,2.71) -- (1.43,2.73) -- (1.54,2.73) -- (1.65,2.70) -- (1.77,2.66) -- (1.88,2.60) -- (1.99,2.54) -- (2.10,2.46) -- (2.22,2.37) -- (2.33,2.28) -- (2.44,2.18) -- (2.55,2.07) -- (2.67,1.97) -- (2.78,1.86) -- (2.89,1.75) -- (3.01,1.64) -- (3.12,1.53) -- (3.23,1.42) -- (3.34,1.31) -- (3.46,1.21) -- (3.57,1.10) -- (3.68,1.00) -- (3.79,0.90) -- (3.91,0.80) -- (4.02,0.71) -- (4.13,0.62) -- (4.25,0.54) -- (4.36,0.46) -- (4.47,0.39) -- (4.58,0.33) -- (4.70,0.28) -- (4.81,0.23) -- (4.92,0.19) -- (5.03,0.16) -- (5.15,0.15) -- (5.26,0.15) -- (5.37,0.16) -- (5.49,0.19) -- (5.60,0.23) -- (5.71,0.30) -- (5.82,0.38) -- (5.94,0.49) -- (6.05,0.62) -- (6.16,0.77) -- (6.27,0.96) -- (6.39,1.17) -- (6.50,1.42);
\node at (3.4,3.88) {\footnotesize $\lambda = 10^{-1}$: \ CV MSE $= 0.068 \pm 0.025$};
\draw[thick] (13.10,-1.49) circle (4.5pt);
\node[anchor=west] at (-0.1,-3.4) {Repeat the 5 folds for every $\lambda$. Left: the fit to all 20 points with this $\lambda$.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 21.4s = "true", 21.85s = "false", 30.55s = "false", repeats}}}]
\fill[cold] (0.08,4.3) rectangle (1.28,4.7);
\fill[cold] (1.44,4.3) rectangle (2.64,4.7);
\fill[cold] (2.80,4.3) rectangle (4.00,4.7);
\fill[cold] (4.16,4.3) rectangle (5.36,4.7);
\fill[cold] (5.52,4.3) rectangle (6.72,4.7);
\draw[very thick] (0.30,0.09) -- (0.41,0.78) -- (0.53,1.33) -- (0.64,1.75) -- (0.75,2.07) -- (0.86,2.30) -- (0.98,2.46) -- (1.09,2.57) -- (1.20,2.64) -- (1.31,2.67) -- (1.43,2.67) -- (1.54,2.65) -- (1.65,2.61) -- (1.77,2.56) -- (1.88,2.49) -- (1.99,2.42) -- (2.10,2.34) -- (2.22,2.26) -- (2.33,2.17) -- (2.44,2.08) -- (2.55,1.99) -- (2.67,1.90) -- (2.78,1.81) -- (2.89,1.71) -- (3.01,1.62) -- (3.12,1.53) -- (3.23,1.43) -- (3.34,1.34) -- (3.46,1.25) -- (3.57,1.16) -- (3.68,1.08) -- (3.79,0.99) -- (3.91,0.91) -- (4.02,0.83) -- (4.13,0.75) -- (4.25,0.67) -- (4.36,0.60) -- (4.47,0.53) -- (4.58,0.47) -- (4.70,0.41) -- (4.81,0.35) -- (4.92,0.30) -- (5.03,0.26) -- (5.15,0.23) -- (5.26,0.21) -- (5.37,0.20) -- (5.49,0.20) -- (5.60,0.22) -- (5.71,0.26) -- (5.82,0.32) -- (5.94,0.40) -- (6.05,0.52) -- (6.16,0.68) -- (6.27,0.88) -- (6.39,1.14) -- (6.50,1.46);
\node at (3.4,3.88) {\footnotesize $\lambda = 10^{-0.5}$: \ CV MSE $= 0.082 \pm 0.029$};
\draw[thick] (13.57,-1.38) circle (4.5pt);
\node[anchor=west] at (-0.1,-3.4) {Repeat the 5 folds for every $\lambda$. Left: the fit to all 20 points with this $\lambda$.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 21.85s = "true", 22.3s = "false", 30.55s = "false", repeats}}}]
\fill[cold] (0.08,4.3) rectangle (1.28,4.7);
\fill[cold] (1.44,4.3) rectangle (2.64,4.7);
\fill[cold] (2.80,4.3) rectangle (4.00,4.7);
\fill[cold] (4.16,4.3) rectangle (5.36,4.7);
\fill[cold] (5.52,4.3) rectangle (6.72,4.7);
\draw[very thick] (0.30,0.45) -- (0.41,1.00) -- (0.53,1.43) -- (0.64,1.75) -- (0.75,1.99) -- (0.86,2.16) -- (0.98,2.28) -- (1.09,2.35) -- (1.20,2.39) -- (1.31,2.41) -- (1.43,2.40) -- (1.54,2.38) -- (1.65,2.34) -- (1.77,2.30) -- (1.88,2.25) -- (1.99,2.19) -- (2.10,2.13) -- (2.22,2.07) -- (2.33,2.01) -- (2.44,1.94) -- (2.55,1.87) -- (2.67,1.80) -- (2.78,1.73) -- (2.89,1.67) -- (3.01,1.60) -- (3.12,1.53) -- (3.23,1.46) -- (3.34,1.39) -- (3.46,1.32) -- (3.57,1.25) -- (3.68,1.18) -- (3.79,1.11) -- (3.91,1.05) -- (4.02,0.98) -- (4.13,0.91) -- (4.25,0.85) -- (4.36,0.78) -- (4.47,0.71) -- (4.58,0.65) -- (4.70,0.59) -- (4.81,0.53) -- (4.92,0.47) -- (5.03,0.42) -- (5.15,0.37) -- (5.26,0.32) -- (5.37,0.29) -- (5.49,0.26) -- (5.60,0.25) -- (5.71,0.26) -- (5.82,0.29) -- (5.94,0.35) -- (6.05,0.45) -- (6.16,0.60) -- (6.27,0.81) -- (6.39,1.09) -- (6.50,1.47);
\node at (3.4,3.88) {\footnotesize $\lambda = 10^{0}$: \ CV MSE $= 0.116 \pm 0.037$};
\draw[thick] (14.05,-1.13) circle (4.5pt);
\node[anchor=west] at (-0.1,-3.4) {Repeat the 5 folds for every $\lambda$. Left: the fit to all 20 points with this $\lambda$.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 22.3s = "true", 22.75s = "false", 30.55s = "false", repeats}}}]
\fill[cold] (0.08,4.3) rectangle (1.28,4.7);
\fill[cold] (1.44,4.3) rectangle (2.64,4.7);
\fill[cold] (2.80,4.3) rectangle (4.00,4.7);
\fill[cold] (4.16,4.3) rectangle (5.36,4.7);
\fill[cold] (5.52,4.3) rectangle (6.72,4.7);
\draw[very thick] (0.30,1.09) -- (0.41,1.38) -- (0.53,1.59) -- (0.64,1.75) -- (0.75,1.86) -- (0.86,1.94) -- (0.98,1.99) -- (1.09,2.02) -- (1.20,2.03) -- (1.31,2.03) -- (1.43,2.02) -- (1.54,2.01) -- (1.65,1.98) -- (1.77,1.95) -- (1.88,1.92) -- (1.99,1.89) -- (2.10,1.85) -- (2.22,1.82) -- (2.33,1.78) -- (2.44,1.74) -- (2.55,1.70) -- (2.67,1.66) -- (2.78,1.62) -- (2.89,1.57) -- (3.01,1.53) -- (3.12,1.49) -- (3.23,1.44) -- (3.34,1.40) -- (3.46,1.35) -- (3.57,1.31) -- (3.68,1.26) -- (3.79,1.21) -- (3.91,1.16) -- (4.02,1.12) -- (4.13,1.06) -- (4.25,1.01) -- (4.36,0.96) -- (4.47,0.91) -- (4.58,0.85) -- (4.70,0.80) -- (4.81,0.74) -- (4.92,0.69) -- (5.03,0.64) -- (5.15,0.58) -- (5.26,0.54) -- (5.37,0.49) -- (5.49,0.46) -- (5.60,0.43) -- (5.71,0.42) -- (5.82,0.42) -- (5.94,0.45) -- (6.05,0.52) -- (6.16,0.63) -- (6.27,0.80) -- (6.39,1.04) -- (6.50,1.37);
\node at (3.4,3.88) {\footnotesize $\lambda = 10^{0.5}$: \ CV MSE $= 0.173 \pm 0.050$};
\draw[thick] (14.53,-0.70) circle (4.5pt);
\node[anchor=west] at (-0.1,-3.4) {Repeat the 5 folds for every $\lambda$. Left: the fit to all 20 points with this $\lambda$.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 22.75s = "true", 23.75s = "false", 30.55s = "false", repeats}}}]
\fill[cold] (0.08,4.3) rectangle (1.28,4.7);
\fill[cold] (1.44,4.3) rectangle (2.64,4.7);
\fill[cold] (2.80,4.3) rectangle (4.00,4.7);
\fill[cold] (4.16,4.3) rectangle (5.36,4.7);
\fill[cold] (5.52,4.3) rectangle (6.72,4.7);
\draw[very thick] (0.30,1.59) -- (0.41,1.65) -- (0.53,1.69) -- (0.64,1.72) -- (0.75,1.74) -- (0.86,1.74) -- (0.98,1.74) -- (1.09,1.74) -- (1.20,1.73) -- (1.31,1.71) -- (1.43,1.70) -- (1.54,1.68) -- (1.65,1.67) -- (1.77,1.65) -- (1.88,1.63) -- (1.99,1.61) -- (2.10,1.59) -- (2.22,1.57) -- (2.33,1.55) -- (2.44,1.53) -- (2.55,1.51) -- (2.67,1.49) -- (2.78,1.47) -- (2.89,1.44) -- (3.01,1.42) -- (3.12,1.40) -- (3.23,1.38) -- (3.34,1.35) -- (3.46,1.33) -- (3.57,1.30) -- (3.68,1.27) -- (3.79,1.25) -- (3.91,1.22) -- (4.02,1.19) -- (4.13,1.16) -- (4.25,1.13) -- (4.36,1.10) -- (4.47,1.07) -- (4.58,1.03) -- (4.70,1.00) -- (4.81,0.96) -- (4.92,0.93) -- (5.03,0.89) -- (5.15,0.85) -- (5.26,0.82) -- (5.37,0.79) -- (5.49,0.76) -- (5.60,0.73) -- (5.71,0.71) -- (5.82,0.71) -- (5.94,0.71) -- (6.05,0.74) -- (6.16,0.79) -- (6.27,0.87) -- (6.39,0.99) -- (6.50,1.16);
\node at (3.4,3.88) {\footnotesize $\lambda = 10^{1}$: \ CV MSE $= 0.271 \pm 0.058$};
\draw[thick] (15.00,0.04) circle (4.5pt);
\node[anchor=west] at (-0.1,-3.4) {Repeat the 5 folds for every $\lambda$. Left: the fit to all 20 points with this $\lambda$.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 23.75s = "true", 26.75s = "false", 30.55s = "false", repeats}}}]
\fill[cold] (0.08,4.3) rectangle (1.28,4.7);
\fill[cold] (1.44,4.3) rectangle (2.64,4.7);
\fill[cold] (2.80,4.3) rectangle (4.00,4.7);
\fill[cold] (4.16,4.3) rectangle (5.36,4.7);
\fill[cold] (5.52,4.3) rectangle (6.72,4.7);
\draw[very thick] (0.30,1.02) -- (0.41,1.25) -- (0.53,1.48) -- (0.64,1.71) -- (0.75,1.92) -- (0.86,2.11) -- (0.98,2.27) -- (1.09,2.41) -- (1.20,2.52) -- (1.31,2.60) -- (1.43,2.65) -- (1.54,2.68) -- (1.65,2.69) -- (1.77,2.67) -- (1.88,2.64) -- (1.99,2.59) -- (2.10,2.52) -- (2.22,2.44) -- (2.33,2.35) -- (2.44,2.25) -- (2.55,2.14) -- (2.67,2.02) -- (2.78,1.91) -- (2.89,1.78) -- (3.01,1.66) -- (3.12,1.54) -- (3.23,1.41) -- (3.34,1.29) -- (3.46,1.17) -- (3.57,1.05) -- (3.68,0.94) -- (3.79,0.83) -- (3.91,0.73) -- (4.02,0.63) -- (4.13,0.54) -- (4.25,0.45) -- (4.36,0.38) -- (4.47,0.31) -- (4.58,0.25) -- (4.70,0.20) -- (4.81,0.17) -- (4.92,0.14) -- (5.03,0.12) -- (5.15,0.12) -- (5.26,0.13) -- (5.37,0.16) -- (5.49,0.20) -- (5.60,0.25) -- (5.71,0.33) -- (5.82,0.42) -- (5.94,0.53) -- (6.05,0.66) -- (6.16,0.81) -- (6.27,0.98) -- (6.39,1.18) -- (6.50,1.40);
\node at (3.4,3.88) {\footnotesize $\lambda = 0.032$: \ CV MSE $= 0.066 \pm 0.021$};
\node[anchor=west] at (-0.1,-3.4) {The minimum is at $\lambda = 0.032$. The gray dashed line sits one SE above it, at $0.087$.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 26.75s = "true", 30.55s = "false", 30.55s = "false", repeats}}}]
\fill[cold] (0.08,4.3) rectangle (1.28,4.7);
\fill[cold] (1.44,4.3) rectangle (2.64,4.7);
\fill[cold] (2.80,4.3) rectangle (4.00,4.7);
\fill[cold] (4.16,4.3) rectangle (5.36,4.7);
\fill[cold] (5.52,4.3) rectangle (6.72,4.7);
\draw[very thick] (0.30,0.09) -- (0.41,0.78) -- (0.53,1.33) -- (0.64,1.75) -- (0.75,2.07) -- (0.86,2.30) -- (0.98,2.46) -- (1.09,2.57) -- (1.20,2.64) -- (1.31,2.67) -- (1.43,2.67) -- (1.54,2.65) -- (1.65,2.61) -- (1.77,2.56) -- (1.88,2.49) -- (1.99,2.42) -- (2.10,2.34) -- (2.22,2.26) -- (2.33,2.17) -- (2.44,2.08) -- (2.55,1.99) -- (2.67,1.90) -- (2.78,1.81) -- (2.89,1.71) -- (3.01,1.62) -- (3.12,1.53) -- (3.23,1.43) -- (3.34,1.34) -- (3.46,1.25) -- (3.57,1.16) -- (3.68,1.08) -- (3.79,0.99) -- (3.91,0.91) -- (4.02,0.83) -- (4.13,0.75) -- (4.25,0.67) -- (4.36,0.60) -- (4.47,0.53) -- (4.58,0.47) -- (4.70,0.41) -- (4.81,0.35) -- (4.92,0.30) -- (5.03,0.26) -- (5.15,0.23) -- (5.26,0.21) -- (5.37,0.20) -- (5.49,0.20) -- (5.60,0.22) -- (5.71,0.26) -- (5.82,0.32) -- (5.94,0.40) -- (6.05,0.52) -- (6.16,0.68) -- (6.27,0.88) -- (6.39,1.14) -- (6.50,1.46);
\node at (3.4,3.88) {\footnotesize $\lambda = 0.32$: \ CV MSE $= 0.082 \pm 0.029$};
\draw[dashed] (13.57,-1.02) -- (13.57,1.00) node[above right, inner sep=1pt] {\footnotesize one-SE choice};
\draw[thick] (13.57,-1.38) circle (4.5pt);
\node[anchor=west] at (-0.1,-3.4) {The one-SE rule picks the largest $\lambda$ with CV MSE under the dashed line. Here $\lambda = 0.32$.};
\end{scope}
\draw (0.08,4.3) rectangle (0.38,4.7);
\draw (0.38,4.3) rectangle (0.68,4.7);
\draw (0.68,4.3) rectangle (0.98,4.7);
\draw (0.98,4.3) rectangle (1.28,4.7);
\draw (1.44,4.3) rectangle (1.74,4.7);
\draw (1.74,4.3) rectangle (2.04,4.7);
\draw (2.04,4.3) rectangle (2.34,4.7);
\draw (2.34,4.3) rectangle (2.64,4.7);
\draw (2.80,4.3) rectangle (3.10,4.7);
\draw (3.10,4.3) rectangle (3.40,4.7);
\draw (3.40,4.3) rectangle (3.70,4.7);
\draw (3.70,4.3) rectangle (4.00,4.7);
\draw (4.16,4.3) rectangle (4.46,4.7);
\draw (4.46,4.3) rectangle (4.76,4.7);
\draw (4.76,4.3) rectangle (5.06,4.7);
\draw (5.06,4.3) rectangle (5.36,4.7);
\draw (5.52,4.3) rectangle (5.82,4.7);
\draw (5.82,4.3) rectangle (6.12,4.7);
\draw (6.12,4.3) rectangle (6.42,4.7);
\draw (6.42,4.3) rectangle (6.72,4.7);
\node[anchor=north west, align=left] at (-0.1,-0.85) {\footnotesize blue: training folds, \ red: validation fold and its errors \\ \footnotesize black: ridge fit with degree-9 polynomial features \\ \footnotesize dashed gray: true curve $\sin 2\pi x$};
\end{tikzpicture}
\end{document}Properties
(Fold Standard Error and the One-SE Rule) The spread of the per-fold errors gives a rough standard error, which the one-SE rule uses to prefer simpler models.
Write for the average loss on fold . The mean of the equals when all folds have the same size. Otherwise it gives the points in smaller folds slightly more weight. Most implementations also report the standard error across folds, . It gives a rough sense of the uncertainty in the CV estimate. This formula treats the as independent. They are not, since each fold appears in of the training sets. The SE therefore understates the uncertainty. Bengio and Grandvalet (2004) showed that no unbiased estimator of the variance of -fold CV exists. For hyperparameter selection, a common choice is the one-standard-error rule. Among models whose CV error is within one SE of the minimum, it picks the simplest. This produces more parsimonious choices than minimizing CV error directly, with little loss in predictive accuracy.
(Leave-One-Out) For OLS and other linear smoothers, the leave-one-out residuals follow from a single fit.
Setting leaves a single observation out at a time. Each training set then has observations, nearly the full dataset. The bias of LOOCV as an estimator of expected test error is therefore very small. The variance can be large, because the models are fit on nearly identical training sets. Their errors are therefore highly correlated. The PRESS shortcut in the callout follows from the Sherman-Morrison formula for a rank-one update of . Deleting observation changes the fit so that its leave-one-out residual equals , where is the ordinary residual. Here is the -th diagonal entry of the hat matrix . A point with leverage near has a small ordinary residual and a large leave-one-out residual. The full fit has already moved toward it. The same identity holds for any linear smoother whose leave-one-out fit uses the same procedure. Ridge regression with a fixed is one example, with in place of . Generalized cross-validation (GCV) replaces each by the average . So leave-one-out CV for OLS needs only a single fit. For other models LOOCV requires fits, which is expensive when is large. The high-variance issue is real enough that 5- or 10-fold are usually preferred even when LOO is computationally feasible.
(Train-Test Split) A single split suffices when the held-out set alone has enough observations for a precise estimate.
Splitting the data once is the cheapest validation scheme. Typically 70–80% of the data go to training and 20–30% to a held-out test set. The model is fit only once, and the test-set error gives a single estimate of out-of-sample performance. The estimate has high variance, because it depends on which observations happened to land in the test set. With small this dependence dominates. When is in the hundreds, prefer -fold. When is in the millions, a single split is fine. A train-validation-test three-way split is then standard (see 2.3. Generalization). Train models on the training set and choose hyperparameters on the validation set. Report the final error on the test set, which no choice by the analyst has touched.
(Bias-Variance of CV) Different trade bias for variance.
With smaller , more data are held out per fold, so the training sets are smaller. The fitted models are then weaker than the model trained on all observations. CV error therefore overestimates the true test error, which is a bias. Larger holds out less data per fold, so each training set is closer in size to the full dataset. This reduces the bias. The per-fold errors, however, become more correlated, which increases variance. LOOCV () is the low-bias, high-variance extreme; 2-fold is the high-bias, low-variance extreme. The usual defaults of 5-fold and 10-fold reflect this trade-off. They are biased only slightly upward, since the training set is 80–90% of the full data. That is plenty for most models. Their variance is manageable, since averaging over 5–10 partly-correlated folds smooths the noise. These are the bias and variance of the CV estimate of test error. They differ from the bias and variance of the fitted model in 2.3.1. Bias-Variance Decomposition.
(Stratified K-Fold) Preserve class balance in classification.
For classification problems with imbalanced classes, plain random folds can produce a fold with no examples of a rare class. The model fit can then fail. Stratified -fold assigns observations to folds while preserving the class proportions in each fold. It is the right default for any classification problem. scikit-learn’s cross_val_score uses StratifiedKFold by default for classifiers. The same principle applies to any structured target. The fold assignment should respect time bins, geographic regions and grouping variables. Group -fold puts all records of one patient, user or site in the same fold. This matches the deployment-unit rule in 2.3. Generalization.
(Repeated K-Fold) Run the procedure several times with different random partitions.
A single -fold partition is one of many possible ways to split observations into folds. The resulting CV score depends on the random seed. Repeated -fold runs the whole procedure times with different random partitions and averages the results. This reduces the variance of the estimate at times the computational cost. For and the standard error is usually small enough that further repetition adds nothing useful. This is most worth doing when comparing closely-tied models or when reporting CV scores with confidence intervals.
(Nested CV for Hyperparameter Tuning) Tuning hyperparameters and estimating test error need two separate CV loops.
A common mistake is to use the same -fold to tune (or any hyperparameter) and to estimate test error. The hyperparameter has then been optimized on the held-out folds, so the resulting error estimate is biased downward. The fix is nested cross-validation, which uses two loops. An outer CV loop estimates test error. An inner CV loop, run separately on each outer training fold, selects hyperparameters. The outer fold is then completely untouched by the hyperparameter choice. Nested CV with requires 25 model fits per hyperparameter value tested. This is expensive, but it is the right method when an unbiased estimate of generalization error matters.
(Cross-Validation Fails on Dependent Data) Random folds break temporal or spatial dependence.
The mechanical splitting in -fold assumes observations are exchangeable. For time series this assumption is false. Shuffling observations across time mixes future data into the training set. The CV scores are then far too optimistic. For spatial data, randomly chosen test points are surrounded by training points in space, making prediction artificially easy. The right substitutes are rolling-origin (or expanding-window) CV for time series and spatial block CV for spatial data. Both keep the training and validation sets well separated in time or space, so they respect the dependence structure.
Examples
(Easy) Comparing Polynomial Degrees
Fit polynomials of degree to a dataset of observations, and select the best degree by 5-fold CV.
What does the CV curve show? because higher-degree polynomials can fit the training data more closely. The CV error has a U shape. Low degrees underfit, and both training and CV error are high. High degrees overfit, with tiny training error and large CV error. A middle degree minimizes CV. The exact minimum depends on the noise level and the smoothness of the true function. The CV curve also has a noticeable variance band, one SE per degree. The one-standard-error rule selects the smallest degree within one SE of the minimum. This more conservative model often generalizes as well as or better than the CV-minimum choice.
The training error decreases monotonically with
(Medium) Choosing the Ridge Penalty
Fit a ridge regression on observations, predictors, and select from a grid using 10-fold CV.
How does 10-fold CV choose ? Take each candidate in turn. Split the 200 observations into 10 folds. For each fold, fit ridge on the other 9 folds and record the squared prediction error on the held-out fold. Average across folds to get . Plot versus . The plot is typically U-shaped. Large over-shrinks and gives high bias. Small overfits and gives high variance. The minimum sits between the two regimes. Pick at the minimum. Alternatively, apply the one-standard-error rule and pick the largest within one SE of the minimum. This slightly more conservative choice often generalizes better. Refit ridge with on all 200 observations to get the final model. Scikit-learn's
RidgeCVwithcv=10runs the same search in one call.
(Hard) Selecting Genes Before Cross-Validation
An analyst has patients and gene measurements. She first selects the 100 genes most correlated with the outcome. She then runs 10-fold CV on a logistic regression of the outcome on those 100 genes. She reports a CV accuracy of 95%.
What went wrong, and how should the analyst fix it? full dataset, including observations that later become the validation fold. The selected genes are guaranteed to look predictive in every fold, because they were picked using all the data. The CV procedure then scores only the final logistic regression, fit on a hand-picked set of "good" predictors. The gene-selection step is never evaluated. Valid CV puts the gene-selection step itself inside the CV loop. For each fold, the analyst redoes the gene selection on the training data only. She then refits the logistic regression on those genes and evaluates it on the held-out fold. Done correctly, CV in this setup gives chance-level accuracy when the genes carry no real signal. In that case the selected gene panel is an artifact of multiple comparisons.
The gene-selection step used the
See Also
- 2.3. Generalization, for the training, validation and test roles and the leakage that cross-validation must avoid.
- 2.3.1. Bias-Variance Decomposition, for the tradeoff that a CV curve traces as model complexity varies.
- 3.2. Regularization, since cross-validation is the standard tool for choosing the regularization strength .
- 3.1a. Multiple Linear Regression, for the leverage values used in the PRESS shortcut for LOO.
- 3.3. Nonlinear Regression, where the same CV procedure applies, with refits required at each fold.
- 2.4.5 Time-Series Cross-Validation, the time-series variant that uses rolling-origin splits instead of random folds.
- 3.8.1 Bayesian Linear Regression, the Bayesian alternative to CV-based hyperparameter selection (marginal likelihood or evidence).