While k-fold cross-validation randomly splits data, stratified cross-validation preserves the class distribution in each fold, and leave-one-out cross-validation (LOOCV) uses exactly one sample for testing. Both address specific weaknesses in standard k-fold CV.
Standard k-fold uses< n. What happens as k increases?
k = 2: Each fold has n/2 samples, train on 50% of data
k = 5: Each fold has n/5 samples, train on 80% of data
k = 10: Each fold has n/10 samples, train on 90% of data
k = n: Each fold has 1 sample, train on (n-1)/n ≈ 100% of data
LOOCV is the limit: maximum training data per iteration.
Standard k-fold: Train k models, each on kk−1⋅n samples
LOOCV: Train n models, each on n-1 samples
Ratio: LOOCV requires kn times more training than k-fold.
For k=10, n=1000: LOOCV requires 100× more computation.
Special case - Linear models: Closed-form shortcut exists. For ordinary least squares:
CVLOOCV=n1∑i=1n(1−hiiyi−y^i)2
where y^i is the prediction from the model trained on all n samples, and hii is the leverage (diagonal of the hat matrix).
Why this works? The leave-one-out prediction can be computed from the full model without refitting. Training once gives all n LOOCV errors. Computation cost drops from O(n) model fits to O(1) model fit.
Imagine you have a bag of 100 marbles: 70 red and 30 blue. You want to split them into 5 groups play a game.
Random split: You close your eyes and grab 20 marbles at a time. Sometimes you get 18red and 2 blue, sometimes 12 red and 8 blue. The groups are all different!
Stratified split: You separate the reds and blues first. Then take 14 red (70÷5) and 6 blue (30÷5) for each group. Now every group has exactly the same 14 red + 6 blue mix!
Why it matters: If you're testing how well your friend can guess marble colors, you want each test to be fair. With random splits, some tests are easier (more of one color) and some harder. With stratified splits, every test is equally hard because they all have the same mix.
LOOCV is even more extreme: Instead of 5 groups of 20 marbles, you make 100 groups—each group has 99 marbles for practice and 1 marble for testing. You test your friend 100 times! This takes forever but you learn the most about their guessing ability.
3.2.05-Handling-imbalanced-classes - Stratified CV is essential when classes are imbalanced
2.3.08-Sampling-methods - Stratification is a form of stratified sampling applied to CV
5.4.02-Time-series-cross-validation - Time series requires different CV; can't use LOOCV or random splits
#flashcards/ai-ml
What is the key difference between stratified CV and standard k-fold CV? :: Stratified CV preserves the class distribution in each fold by splitting each class separately then combining, while standard k-fold randomly splits all samples together which can create imbalanced folds
Why does LOOCV have lower bias than k-fold CV?
LOOCV trains on n-1 samples (almost all data), making the validated model nearly identical to the final model trained on all n samples, minimizing the gap between CV performance and true test performance
What is the computational cost ratio of LOOCV vs k-fold?
LOOCV requires n/k times more training than k-fold; for k=10 and n=1000, LOCV needs 100× more computation (1000 model fits vs 10)
Write the LOOCV error formula :: CVLOOCV=n1∑i=1nL(yi,f^(−i)(xi)) where f^(−i) is trained on all samples except i
When should you use stratified CV? :: When class imbalance exists and mini(pi)<k2 (smallest class proportion is less than 2/k), or when minority classes have fewer than 10 samples per fold
Why does LOOCV have high variance despite using all data?
The n training sets overlap by-2 samples, creating high correlation between error estimates; variance doesn't decrease with n because Var(eˉ)≈σ2 due to correlation
How does stratified CV ensure class balance?
It splits each class into k groups separately, then combines one group from each class to form each fold, guaranteeing each fold has proportional class representation
What is the special property of LOOCV for linear models?
For OLS, LOOCV can be computed from a single model fit using n1∑(1−hiiyi−y^i)2 where hii is leverage, reducing cost from O(n) to O(1) fits
When is LOOCV preferred over k-fold?
When dataset is very small (n < 50-100), model training is fast, and you need minimum bias; avoid when n > 1000 or training is expensive
What happens if you use random5-fold on a 95%/5% imbalanced dataset?
Some folds might get 100% majority class or very few minority samples, causing the model to learn biased patterns and producing unreliable validation metrics
Chalo ise simple tarike se samajhte hain. Jab hum normal k-fold cross-validation karte hain, toh data randomly split hota hai. Problem yeh hai ki agar aapke paas imbalanced data hai — jaise 90% negative aur sirf 10% positive samples — toh random split se koi fold mein shayad 100% negative hi aa jaayein aur positive samples bilkul na aayein. Aisa hone par model galat cheez seekhta hai aur aapke validation numbers reliable nahi rehte. Stratified cross-validation yahi problem solve karta hai — yeh ensure karta hai ki har fold mein original dataset waali hi class ratio maintain rahe. Matlab agar poore data mein 70% cats aur 30% dogs hain, toh har fold mein bhi 70:30 ratio hi milega.
Iska logic bhi seedha hai: pehle data ko class ke hisaab se alag karo, phir har class ko k barabar groups mein todo, aur phir har fold banane ke liye har class se ek-ek group utha ke combine kar do. Is tarah har fold mein saari classes proportional matra mein aa jaati hain. Doosra variant hai LOOCV (Leave-One-Out), jahan hum k ko extreme tak le jaate hain — agar n samples hain toh n folds banate hain, har baar n-1 samples pe train karke sirf 1 sample pe test. Isse maximum training data milta hai, lekin computational cost bhi utni hi zyaada ho jaati hai kyunki aapko n baar model train karna padta hai.
Yeh cheezein isliye important hain kyunki real-world data aksar imbalanced hota hai — medical diagnosis, fraud detection, rare disease prediction, in sabme minority class kam hoti hai lekin sabse important hoti hai. Agar aap stratification use nahi karenge toh aapka model evaluation galat picture dikhayega aur aapko lagega model achha kaam kar raha hai, jabki asal mein woh minority class ko pehchaan hi nahi pa raha. Isliye jab bhi imbalanced dataset ho, stratified CV default choice honi chahiye, aur jab data bahut kam ho tabhi LOOCV ka istemaal karein kyunki wahaan har ek sample keemti hota hai.