4.9.22 · D2 · HinglishProbability Theory & Statistics

Visual walkthroughLinear regression — least squares, inference on coefficients

2,099 words10 min read↑ Read in English

4.9.22 · D2 · Maths › Probability Theory & Statistics › Linear regression — least squares, inference on coefficients


Step 1 — Dots aur guess

KYA. Hum kuch measured pairs se shuru karte hain: har observation ke liye ek horizontal number aur ek vertical number hota hai. Inhe plot karne se ek point ka cloud banta hai. Hume lagta hai ki yeh cloud secretly ek straight line follow karta hai, lekin noise ne har dot ko thoda hila diya hai.

KYUN. Ek straight line sabse simple possible "kahaani" hai: ek starting height aur ek steepness chuno, aur tumne sirf do numbers se infinitely many points describe kar diye. Pehle hum koi bhi line draw aur score kar sakein, tabhi best line dhundh sakte hain.

PICTURE. Neeche, black dots data hain. Blue line ek candidate hai — ek guess. Iske do knobs hain:

Figure — Linear regression — least squares, inference on coefficients

Step 2 — Miss measure karna: residual

KYA. Har dot ke liye, dot se line tak ek vertical segment giraao. Uski signed length hi residual hai:

Vertical kyun, perpendicular kyun nahi? Hum ko se predict karne ki koshish kar rahe hain. Ek diya hua leke, line humein ek predicted deti hai. Honest error woh gap hai jo hum predict karte hain — -direction mein. Perpendicular distance mein ki errors bhi aa jaati hain (jinhe hum known maante hain), jo score mein mix ho jaatein.

PICTURE. Orange segments misses hain. Line ke upar wale dots ka hai; neeche wale dots ka hai.

Figure — Linear regression — least squares, inference on coefficients

Step 3 — Misses ko square kyun karte hain

KYA. Hum ek line ko sum of squared residuals se score karte hain:

Squaring kyun, absolute value kyun nahi? Teen reasons, har ek visible:

  1. Squaring har miss ko positive bana deta hai, toh kuch cancel nahi hota.
  2. Squaring badi misses ko zyada punish karta hai — ki miss cost karti hai, ki miss cost karti hai. Line apne worst offences se sabse zyada khinchti hai.
  3. Squaring smooth hai: score curve mein koi sharp corner nahi, toh calculus (Step 5) bottom cleanly dhundh sakta hai. Absolute value mein kink hoti hai aur koi tidy formula nahi milta.

PICTURE. Length ka har orange segment ek orange square ban jaata hai jiska area hai. Total shaded area hi score hai. Least squares = total orange area ko shrink karo.

Figure — Linear regression — least squares, inference on coefficients
Squares kyun?
Yeh badi misses ko zyada punish karte hain, kabhi cancel nahi hote, aur smooth rehte hain toh calculus kaam karta hai.

Step 4 — Score ek bowl mein rehta hai

KYA. do knobs pe depend karta hai. Un do numbers ko ek location maano aur ko unke upar height ki tarah plot karo. Kyunki squares ka sum hai, woh surface ek smooth upward bowl (paraboloid) hai jiska exactly ek lowest point hai.

KYUN important hai. Ek bowl ka ek hi bottom hota hai, aur us bottom pe zameen har direction mein flat hoti hai. "Flat" ek aisi cheez hai jo calculus exactly pin down kar sakta hai. Yahi guarantee karta hai ek unique best line — koi doosra candidate nahi, koi ambiguity nahi.

PICTURE. plane ke upar bowl. Red dot minimum hai; wahan red tangent plane horizontal hai.

Figure — Linear regression — least squares, inference on coefficients

Step 5 — Bowl ki do slopes zero set karo

KYA. Hum ka derivative har knob ke respect mein lete hain aur dono ko zero set karte hain. Derivative poochta hai "agar main height ko thoda nudge karun, toh kya score badlega?"; bottom pe answer hona chahiye "nahi."

\;\Longrightarrow\; \sum_i (y_i - \hat\beta_0 - \hat\beta_1 x_i)=0$$ $-1$ chain rule hai: miss $e_i = y_i-\hat\beta_0-\hat\beta_1 x_i$ mein $-1$ ka badlaav hota hai jab $\hat\beta_0$ $1$ se badhta hai. Result seedha padho: **residuals ka sum zero hai.** $$\frac{\partial S}{\partial \hat\beta_1} = \sum_i 2\big(y_i - \hat\beta_0 - \hat\beta_1 x_i\big)\cdot(-x_i) = 0 \;\Longrightarrow\; \sum_i x_i(y_i - \hat\beta_0 - \hat\beta_1 x_i)=0$$ Yahan miss $-x_i$ se badlti hai, toh yeh result kehta hai **residuals, $x$ se weighted, ka sum zero hai.** **YEH DO EQUATIONS KYUN?** Yeh bowl ki do "flatness" conditions hain — har knob ke liye ek. Saath mein yeh **normal equations** hain aur yeh *hi* poora minimisation hain. **PICTURE.** Bowl ke do cross-sections: $\hat\beta_0$ ke along slice (jab $\hat\beta_1$ fixed ho) aur $\hat\beta_1$ ke along. Har slice ek parabola hai; red mark wahan hai jahan uska tangent flat ho. ![[deepdives/dd-maths-4.9.22-d2-s05.png]] > [!definition] Normal equations > $$\sum e_i = 0 \qquad\text{and}\qquad \sum x_i e_i = 0.$$ > Do constraints jo best-fit residuals ko *zaroori* follow karne padte hain. (Yahi do "used-up" degrees of freedom hain jo baad mein $n-2$ divisor force karte hain.) --- ## Step 6 — Intercept: line centre of mass se guzarti hai **KYA.** Pehli normal equation $\sum(y_i - \hat\beta_0 - \hat\beta_1 x_i)=0$ lo, aur $n$ se divide karo. $\bar x = \frac1n\sum x_i$ aur $\bar y = \frac1n\sum y_i$ use karke: $$\bar y - \hat\beta_0 - \hat\beta_1\bar x = 0 \;\Longrightarrow\; \boxed{\;\hat\beta_0 = \bar y - \hat\beta_1\bar x\;}$$ Padho isko: height knob fix ho jaata hai jab hum slope jaante hain, kyunki hum demand karte hain ki line average point se guzre. **KYUN.** Rearrange karne par, yeh kehta hai $\bar y = \hat\beta_0 + \hat\beta_1\bar x$ — yaani **point $(\bar x,\bar y)$ fitted line pe exactly hota hai.** Best line hamesha cloud ke *centre of mass* se guzarti hai. Toh humein $\hat\beta_0$ alag se nahi dhoondna; sirf tilt chahiye, aur line $(\bar x,\bar y)$ se pivot karti hai. **PICTURE.** Green cross $(\bar x,\bar y)$ hai. Jo bhi slope chunein, winning line us pivot ke around rotate karti hai. ![[deepdives/dd-maths-4.9.22-d2-s06.png]] Har least-squares line kis point se guzarti hai? ::: Centre of mass $(\bar x,\bar y)$ se. --- ## Step 7 — Slope: co-movement divided by spread **KYA.** Ab $\hat\beta_0 = \bar y - \hat\beta_1\bar x$ ko doosri normal equation mein substitute karo aur clean up karo. Jo trick ise tidy banati hai: sab kuch *means ke relative* measure karo. Centred coordinates $x_i-\bar x$ aur $y_i-\bar y$ likho, aur define karo $$S_{xx} = \sum_i (x_i-\bar x)^2 \quad(\text{how spread-out } x \text{ is}), \qquad S_{xy} = \sum_i (x_i-\bar x)(y_i-\bar y) \quad(\text{how } x,y \text{ move together}).$$ Algebra collapse hokar yeh banta hai $$\boxed{\;\hat\beta_1 = \frac{S_{xy}}{S_{xx}} = \frac{\sum_i (x_i-\bar x)(y_i-\bar y)}{\sum_i (x_i-\bar x)^2}\;}$$ **YEH SHAPE KYUN.** $S_{xy}$ bada aur positive hota hai jab centre ke *daayein* wale dots centre ke *upar* baithte hain (dono factors positive) aur *baayin* wale neeche baithte hain (dono negative → product phir bhi positive). Yahi upward tilt hai. $S_{xx}$ se divide karna ise *rise per unit run* mein rescale karta hai. Yeh exactly [[Covariance]] divided by variance of $x$ hai (times $n$'s jo cancel ho jaate hain), aur yeh seedha [[Correlation coefficient]] se link karta hai. **PICTURE.** $S_{xy}$ mein har dot ka contribution ek signed rectangle hai jiska width $(x_i-\bar x)$ aur height $(y_i-\bar y)$ hai, green centre se drawn. Blue rectangles "tilt up" vote karte hain, red "tilt down"; net result slope decide karta hai. ![[deepdives/dd-maths-4.9.22-d2-s07.png]] > [!formula] Poora result, assembled > $$\hat\beta_1 = \frac{S_{xy}}{S_{xx}}, \qquad \hat\beta_0 = \bar y - \hat\beta_1\bar x.$$ > Slope = co-movement ÷ spread; intercept = line ko mean point se pin karo. --- ## Step 8 — Degenerate case: agar $x$ kabhi move na kare? **KYA.** Maano har $x_i$ same value ke barabar hai (saare dots ek vertical column mein stack). Tab $x_i-\bar x = 0$ sabke liye, toh $S_{xx}=0$, aur slope formula ban jaata hai $S_{xy}/0$ — undefined. **KYUN yeh *hona chahiye* ki toote.** Koi horizontal spread na ho toh "run" maapne ke liye kuch nahi. Ek vertical column of dots se infinitely many lines guzar sakti hain; har ek mean $\bar y$ equally well predict karta hai. Genuinely *koi unique slope nahi* hai — maths correctly ek banana refuse karta hai. Step 4 ka bowl degenerate hokar ek flat-bottomed *trough* ban jaata hai: infinitely many minima. **PICTURE.** Baayin: healthy spread → ek clear best tilt. Daayein: zero spread → score slope direction mein flat hai, toh koi single answer nahi. ![[deepdives/dd-maths-4.9.22-d2-s08.png]] > [!mistake] "Zyada data hamesha undefined slope fix kar deta hai." > *Kyun sahi lagta hai:* aamtaur pe zyada dots = zyada certainty. **Fix:** agar saare $x$ ek hi value share karte hain, toh usi value pe *hazaar* aur dots add karne se $S_{xx}=0$ hi rehta hai. Tumhe *$x$ mein spread* chahiye, sirf *zyada $x$* nahi. Yahi lever-arm idea hai jo $\text{Var}(\hat\beta_1)=\sigma^2/S_{xx}$ > ko blow up karata hai jab $S_{xx}\to 0$. --- ## Ek-picture summary Har idea, ek single frame mein: dots, $(\bar x,\bar y)$ pe pivot, winning line jiska tilt $S_{xy}/S_{xx}$ hai, aur orange squares jinka total area humne minimum tak pohnchaaya. ![[deepdives/dd-maths-4.9.22-d2-s09.png]] > [!recall]- Poore walkthrough ki Feynman retelling > Kuch dots bikharao. Do knobs wali ek straight line khaincho: ek height aur ek tilt. Har dot ke liye, > seedha *upar ya neeche* line tak mapo — woh uski miss hai. Har miss ko ek square banao (taaki badi misses chillaayein aur kuch cancel na ho) aur saare squares add kar lo. Woh total do knobs ke upar ek bowl-shaped landscape hai, jiska ek lowest point hai. Bottom pe zameen dono taraf flat hoti hai — dono slopes zero set karo. Pehla flatness rule kehta hai misses cancel ho jaate hain, jo line ko average dot $(\bar x,\bar y)$ se guzarne pe majboor karta hai. Doosra rule, jab tum sab kuch us average point se maapte ho, tumhe tilt deta hai: har dot ka sideways offset uske up-down offset ke saath kitna align karta hai yeh add karo (woh $S_{xy}$ hai), aur divide karo ki dots sideways kitne spread hain (woh $S_{xx}$ hai). > Bada shared movement aur wide sideways spread tumhe ek confident tilt dete hain. Aur agar dots kabhi sideways spread hi na karein, toh koi run measure karne ke liye nahi — formula zero se divide karta hai aur honestly maanta hai ki slope pick nahi kar sakta. > [!mnemonic] Picture yaad rakho > **"Misses square karo, bowl khaali karo; mean pe pivot karo, co-move-over-spread se tilt karo."** --- **Dekho bhi:** [[Gauss–Markov Theorem]] (yeh line *best* kyun hai), [[Maximum Likelihood Estimation]] (Gaussian noise mein squares kyun), [[t-distribution]] & [[Hypothesis Testing]] (tilt pe trust karna), [[Multiple Regression]] (ek saath kai knobs).