diff --git a/doc/pub/week36/html/._week36-bs000.html b/doc/pub/week36/html/._week36-bs000.html index c79950484..12be7d203 100644 --- a/doc/pub/week36/html/._week36-bs000.html +++ b/doc/pub/week36/html/._week36-bs000.html @@ -37,7 +37,6 @@ doconce format html week36.do.txt --html_style=bootstrap --pygments_html_style=d
  • Plans for week 36
  • -
  • Thursday September 8
  • -
  • Summary from last Week and discussion of SVD, Ridge and Lasso regression with examples
  • -
  • Linear Regression and the SVD
  • -
  • What does it mean?
  • -
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • -
  • Code for SVD and Inversion of Matrices
  • -
  • Inverse of Rectangular Matrix
  • -
  • Ridge and LASSO Regression
  • -
  • From OLS to Ridge and Lasso
  • -
  • Deriving the Ridge Regression Equations
  • -
  • Note on Scikit-Learn
  • -
  • Comparison with OLS
  • -
  • SVD analysis
  • -
  • Interpreting the Ridge results
  • -
  • More interpretations
  • -
  • Deriving the Lasso Regression Equations
  • -
  • Simple example to illustrate Ordinary Least Squares, Ridge and Lasso Regression
  • -
  • Ridge Regression
  • -
  • Lasso Regression
  • -
  • Yet another Example
  • -
  • The OLS case
  • -
  • The Ridge case
  • -
  • Writing the Cost Function
  • -
  • Lasso case
  • -
  • The first Case
  • -
  • Simple code for solving the above problem
  • -
  • With Lasso Regression
  • -
  • Another Example, now with a polynomial fit
  • -
  • To think about, first part
  • -
  • More thinking
  • -
  • Still thinking
  • -
  • What does centering (subtracting the mean values) mean mathematically?
  • -
  • Further Manipulations
  • -
  • Wrapping it up
  • -
  • Linear Regression code, Intercept handling first
  • -
  • Code Examples
  • -
  • Taking out the mean
  • -
  • Friday September 9
  • -
  • Linking the regression analysis with a statistical interpretation
  • -
  • Assumptions made
  • -
  • Expectation value and variance
  • -
  • Expectation value and variance for \( \boldsymbol{\beta} \)
  • -
  • Deriving OLS from a probability distribution
  • -
  • Independent and Identically Distrubuted (iid)
  • -
  • Maximum Likelihood Estimation (MLE)
  • -
  • A new Cost Function
  • -
  • More basic Statistics and Bayes' theorem
  • -
  • Marginal Probability
  • -
  • Conditional Probability
  • -
  • Bayes' Theorem
  • -
  • Interpretations of Bayes' Theorem
  • -
  • Example of Usage of Bayes' theorem
  • -
  • Doing it correctly
  • -
  • Bayes' Theorem and Ridge and Lasso Regression
  • -
  • Test Function for what happens with OLS, Ridge and Lasso
  • -
  • Invoking Bayes' theorem
  • -
  • Ridge and Bayes
  • -
  • Lasso and Bayes
  • -
  • Exercise 1: mean values and variances in linear regression
  • -
  • Exercise 2: Adding Ridge and Lasso Regression
  • +
  • Summary from last Week and discussion of SVD, Ridge and Lasso regression with examples
  • +
  • Linear Regression and the SVD
  • +
  • What does it mean?
  • +
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • +
  • Code for SVD and Inversion of Matrices
  • +
  • Inverse of Rectangular Matrix
  • +
  • Ridge and LASSO Regression
  • +
  • From OLS to Ridge and Lasso
  • +
  • Deriving the Ridge Regression Equations
  • +
  • Note on Scikit-Learn
  • +
  • Comparison with OLS
  • +
  • SVD analysis
  • +
  • Interpreting the Ridge results
  • +
  • More interpretations
  • +
  • Deriving the Lasso Regression Equations
  • +
  • Simple example to illustrate Ordinary Least Squares, Ridge and Lasso Regression
  • +
  • Ridge Regression
  • +
  • Lasso Regression
  • +
  • Yet another Example
  • +
  • The OLS case
  • +
  • The Ridge case
  • +
  • Writing the Cost Function
  • +
  • Lasso case
  • +
  • The first Case
  • +
  • Simple code for solving the above problem
  • +
  • With Lasso Regression
  • +
  • Another Example, now with a polynomial fit
  • +
  • To think about, first part
  • +
  • More thinking
  • +
  • Still thinking
  • +
  • What does centering (subtracting the mean values) mean mathematically?
  • +
  • Further Manipulations
  • +
  • Wrapping it up
  • +
  • Linear Regression code, Intercept handling first
  • +
  • Code Examples
  • +
  • Taking out the mean
  • +
  • Friday September 9
  • +
  • Linking the regression analysis with a statistical interpretation
  • +
  • Assumptions made
  • +
  • Expectation value and variance
  • +
  • Expectation value and variance for \( \boldsymbol{\beta} \)
  • +
  • Deriving OLS from a probability distribution
  • +
  • Independent and Identically Distrubuted (iid)
  • +
  • Maximum Likelihood Estimation (MLE)
  • +
  • A new Cost Function
  • +
  • More basic Statistics and Bayes' theorem
  • +
  • Marginal Probability
  • +
  • Conditional Probability
  • +
  • Bayes' Theorem
  • +
  • Interpretations of Bayes' Theorem
  • +
  • Example of Usage of Bayes' theorem
  • +
  • Doing it correctly
  • +
  • Bayes' Theorem and Ridge and Lasso Regression
  • +
  • Test Function for what happens with OLS, Ridge and Lasso
  • +
  • Invoking Bayes' theorem
  • +
  • Ridge and Bayes
  • +
  • Lasso and Bayes
  • +
  • Exercise 1: mean values and variances in linear regression
  • +
  • Exercise 2: Adding Ridge and Lasso Regression
  • @@ -315,7 +313,7 @@ MathJax.Hub.Config({
    -

    Sep 9, 2022

    +

    May 29, 2023


    @@ -340,7 +338,7 @@ MathJax.Hub.Config({
  • 9
  • 10
  • ...
  • -
  • 60
  • +
  • 59
  • »
  • @@ -354,7 +352,7 @@ MathJax.Hub.Config({ -->
    - © 1999-2022, Morten Hjorth-Jensen. Released under CC Attribution-NonCommercial 4.0 license + © 1999-2023, Morten Hjorth-Jensen. Released under CC Attribution-NonCommercial 4.0 license
    diff --git a/doc/pub/week36/html/._week36-bs001.html b/doc/pub/week36/html/._week36-bs001.html index 0f24a7bb5..d83f9eb68 100644 --- a/doc/pub/week36/html/._week36-bs001.html +++ b/doc/pub/week36/html/._week36-bs001.html @@ -37,7 +37,6 @@ doconce format html week36.do.txt --html_style=bootstrap --pygments_html_style=d
  • Plans for week 36
  • -
  • Thursday September 8
  • -
  • Summary from last Week and discussion of SVD, Ridge and Lasso regression with examples
  • -
  • Linear Regression and the SVD
  • -
  • What does it mean?
  • -
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • -
  • Code for SVD and Inversion of Matrices
  • -
  • Inverse of Rectangular Matrix
  • -
  • Ridge and LASSO Regression
  • -
  • From OLS to Ridge and Lasso
  • -
  • Deriving the Ridge Regression Equations
  • -
  • Note on Scikit-Learn
  • -
  • Comparison with OLS
  • -
  • SVD analysis
  • -
  • Interpreting the Ridge results
  • -
  • More interpretations
  • -
  • Deriving the Lasso Regression Equations
  • -
  • Simple example to illustrate Ordinary Least Squares, Ridge and Lasso Regression
  • -
  • Ridge Regression
  • -
  • Lasso Regression
  • -
  • Yet another Example
  • -
  • The OLS case
  • -
  • The Ridge case
  • -
  • Writing the Cost Function
  • -
  • Lasso case
  • -
  • The first Case
  • -
  • Simple code for solving the above problem
  • -
  • With Lasso Regression
  • -
  • Another Example, now with a polynomial fit
  • -
  • To think about, first part
  • -
  • More thinking
  • -
  • Still thinking
  • -
  • What does centering (subtracting the mean values) mean mathematically?
  • -
  • Further Manipulations
  • -
  • Wrapping it up
  • -
  • Linear Regression code, Intercept handling first
  • -
  • Code Examples
  • -
  • Taking out the mean
  • -
  • Friday September 9
  • -
  • Linking the regression analysis with a statistical interpretation
  • -
  • Assumptions made
  • -
  • Expectation value and variance
  • -
  • Expectation value and variance for \( \boldsymbol{\beta} \)
  • -
  • Deriving OLS from a probability distribution
  • -
  • Independent and Identically Distrubuted (iid)
  • -
  • Maximum Likelihood Estimation (MLE)
  • -
  • A new Cost Function
  • -
  • More basic Statistics and Bayes' theorem
  • -
  • Marginal Probability
  • -
  • Conditional Probability
  • -
  • Bayes' Theorem
  • -
  • Interpretations of Bayes' Theorem
  • -
  • Example of Usage of Bayes' theorem
  • -
  • Doing it correctly
  • -
  • Bayes' Theorem and Ridge and Lasso Regression
  • -
  • Test Function for what happens with OLS, Ridge and Lasso
  • -
  • Invoking Bayes' theorem
  • -
  • Ridge and Bayes
  • -
  • Lasso and Bayes
  • -
  • Exercise 1: mean values and variances in linear regression
  • -
  • Exercise 2: Adding Ridge and Lasso Regression
  • +
  • Summary from last Week and discussion of SVD, Ridge and Lasso regression with examples
  • +
  • Linear Regression and the SVD
  • +
  • What does it mean?
  • +
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • +
  • Code for SVD and Inversion of Matrices
  • +
  • Inverse of Rectangular Matrix
  • +
  • Ridge and LASSO Regression
  • +
  • From OLS to Ridge and Lasso
  • +
  • Deriving the Ridge Regression Equations
  • +
  • Note on Scikit-Learn
  • +
  • Comparison with OLS
  • +
  • SVD analysis
  • +
  • Interpreting the Ridge results
  • +
  • More interpretations
  • +
  • Deriving the Lasso Regression Equations
  • +
  • Simple example to illustrate Ordinary Least Squares, Ridge and Lasso Regression
  • +
  • Ridge Regression
  • +
  • Lasso Regression
  • +
  • Yet another Example
  • +
  • The OLS case
  • +
  • The Ridge case
  • +
  • Writing the Cost Function
  • +
  • Lasso case
  • +
  • The first Case
  • +
  • Simple code for solving the above problem
  • +
  • With Lasso Regression
  • +
  • Another Example, now with a polynomial fit
  • +
  • To think about, first part
  • +
  • More thinking
  • +
  • Still thinking
  • +
  • What does centering (subtracting the mean values) mean mathematically?
  • +
  • Further Manipulations
  • +
  • Wrapping it up
  • +
  • Linear Regression code, Intercept handling first
  • +
  • Code Examples
  • +
  • Taking out the mean
  • +
  • Friday September 9
  • +
  • Linking the regression analysis with a statistical interpretation
  • +
  • Assumptions made
  • +
  • Expectation value and variance
  • +
  • Expectation value and variance for \( \boldsymbol{\beta} \)
  • +
  • Deriving OLS from a probability distribution
  • +
  • Independent and Identically Distrubuted (iid)
  • +
  • Maximum Likelihood Estimation (MLE)
  • +
  • A new Cost Function
  • +
  • More basic Statistics and Bayes' theorem
  • +
  • Marginal Probability
  • +
  • Conditional Probability
  • +
  • Bayes' Theorem
  • +
  • Interpretations of Bayes' Theorem
  • +
  • Example of Usage of Bayes' theorem
  • +
  • Doing it correctly
  • +
  • Bayes' Theorem and Ridge and Lasso Regression
  • +
  • Test Function for what happens with OLS, Ridge and Lasso
  • +
  • Invoking Bayes' theorem
  • +
  • Ridge and Bayes
  • +
  • Lasso and Bayes
  • +
  • Exercise 1: mean values and variances in linear regression
  • +
  • Exercise 2: Adding Ridge and Lasso Regression
  • @@ -300,14 +298,8 @@ MathJax.Hub.Config({

    Plans for week 36

    Recommended Reading:

      @@ -331,7 +323,7 @@ MathJax.Hub.Config({
    1. 10
    2. 11
    3. ...
    4. -
    5. 60
    6. +
    7. 59
    8. »
    9. diff --git a/doc/pub/week36/html/._week36-bs002.html b/doc/pub/week36/html/._week36-bs002.html index ff32ce789..9e13c333e 100644 --- a/doc/pub/week36/html/._week36-bs002.html +++ b/doc/pub/week36/html/._week36-bs002.html @@ -37,7 +37,6 @@ doconce format html week36.do.txt --html_style=bootstrap --pygments_html_style=d
    10. Plans for week 36
    11. -
    12. Thursday September 8
    13. -
    14. Summary from last Week and discussion of SVD, Ridge and Lasso regression with examples
    15. -
    16. Linear Regression and the SVD
    17. -
    18. What does it mean?
    19. -
    20. And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
    21. -
    22. Code for SVD and Inversion of Matrices
    23. -
    24. Inverse of Rectangular Matrix
    25. -
    26. Ridge and LASSO Regression
    27. -
    28. From OLS to Ridge and Lasso
    29. -
    30. Deriving the Ridge Regression Equations
    31. -
    32. Note on Scikit-Learn
    33. -
    34. Comparison with OLS
    35. -
    36. SVD analysis
    37. -
    38. Interpreting the Ridge results
    39. -
    40. More interpretations
    41. -
    42. Deriving the Lasso Regression Equations
    43. -
    44. Simple example to illustrate Ordinary Least Squares, Ridge and Lasso Regression
    45. -
    46. Ridge Regression
    47. -
    48. Lasso Regression
    49. -
    50. Yet another Example
    51. -
    52. The OLS case
    53. -
    54. The Ridge case
    55. -
    56. Writing the Cost Function
    57. -
    58. Lasso case
    59. -
    60. The first Case
    61. -
    62. Simple code for solving the above problem
    63. -
    64. With Lasso Regression
    65. -
    66. Another Example, now with a polynomial fit
    67. -
    68. To think about, first part
    69. -
    70. More thinking
    71. -
    72. Still thinking
    73. -
    74. What does centering (subtracting the mean values) mean mathematically?
    75. -
    76. Further Manipulations
    77. -
    78. Wrapping it up
    79. -
    80. Linear Regression code, Intercept handling first
    81. -
    82. Code Examples
    83. -
    84. Taking out the mean
    85. -
    86. Friday September 9
    87. -
    88. Linking the regression analysis with a statistical interpretation
    89. -
    90. Assumptions made
    91. -
    92. Expectation value and variance
    93. -
    94. Expectation value and variance for \( \boldsymbol{\beta} \)
    95. -
    96. Deriving OLS from a probability distribution
    97. -
    98. Independent and Identically Distrubuted (iid)
    99. -
    100. Maximum Likelihood Estimation (MLE)
    101. -
    102. A new Cost Function
    103. -
    104. More basic Statistics and Bayes' theorem
    105. -
    106. Marginal Probability
    107. -
    108. Conditional Probability
    109. -
    110. Bayes' Theorem
    111. -
    112. Interpretations of Bayes' Theorem
    113. -
    114. Example of Usage of Bayes' theorem
    115. -
    116. Doing it correctly
    117. -
    118. Bayes' Theorem and Ridge and Lasso Regression
    119. -
    120. Test Function for what happens with OLS, Ridge and Lasso
    121. -
    122. Invoking Bayes' theorem
    123. -
    124. Ridge and Bayes
    125. -
    126. Lasso and Bayes
    127. -
    128. Exercise 1: mean values and variances in linear regression
    129. -
    130. Exercise 2: Adding Ridge and Lasso Regression
    131. +
    132. Summary from last Week and discussion of SVD, Ridge and Lasso regression with examples
    133. +
    134. Linear Regression and the SVD
    135. +
    136. What does it mean?
    137. +
    138. And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
    139. +
    140. Code for SVD and Inversion of Matrices
    141. +
    142. Inverse of Rectangular Matrix
    143. +
    144. Ridge and LASSO Regression
    145. +
    146. From OLS to Ridge and Lasso
    147. +
    148. Deriving the Ridge Regression Equations
    149. +
    150. Note on Scikit-Learn
    151. +
    152. Comparison with OLS
    153. +
    154. SVD analysis
    155. +
    156. Interpreting the Ridge results
    157. +
    158. More interpretations
    159. +
    160. Deriving the Lasso Regression Equations
    161. +
    162. Simple example to illustrate Ordinary Least Squares, Ridge and Lasso Regression
    163. +
    164. Ridge Regression
    165. +
    166. Lasso Regression
    167. +
    168. Yet another Example
    169. +
    170. The OLS case
    171. +
    172. The Ridge case
    173. +
    174. Writing the Cost Function
    175. +
    176. Lasso case
    177. +
    178. The first Case
    179. +
    180. Simple code for solving the above problem
    181. +
    182. With Lasso Regression
    183. +
    184. Another Example, now with a polynomial fit
    185. +
    186. To think about, first part
    187. +
    188. More thinking
    189. +
    190. Still thinking
    191. +
    192. What does centering (subtracting the mean values) mean mathematically?
    193. +
    194. Further Manipulations
    195. +
    196. Wrapping it up
    197. +
    198. Linear Regression code, Intercept handling first
    199. +
    200. Code Examples
    201. +
    202. Taking out the mean
    203. +
    204. Friday September 9
    205. +
    206. Linking the regression analysis with a statistical interpretation
    207. +
    208. Assumptions made
    209. +
    210. Expectation value and variance
    211. +
    212. Expectation value and variance for \( \boldsymbol{\beta} \)
    213. +
    214. Deriving OLS from a probability distribution
    215. +
    216. Independent and Identically Distrubuted (iid)
    217. +
    218. Maximum Likelihood Estimation (MLE)
    219. +
    220. A new Cost Function
    221. +
    222. More basic Statistics and Bayes' theorem
    223. +
    224. Marginal Probability
    225. +
    226. Conditional Probability
    227. +
    228. Bayes' Theorem
    229. +
    230. Interpretations of Bayes' Theorem
    231. +
    232. Example of Usage of Bayes' theorem
    233. +
    234. Doing it correctly
    235. +
    236. Bayes' Theorem and Ridge and Lasso Regression
    237. +
    238. Test Function for what happens with OLS, Ridge and Lasso
    239. +
    240. Invoking Bayes' theorem
    241. +
    242. Ridge and Bayes
    243. +
    244. Lasso and Bayes
    245. +
    246. Exercise 1: mean values and variances in linear regression
    247. +
    248. Exercise 2: Adding Ridge and Lasso Regression
    249. @@ -297,7 +295,7 @@ MathJax.Hub.Config({

       

       

       

      -

      Thursday September 8

      +

      Summary from last Week and discussion of SVD, Ridge and Lasso regression with examples

      @@ -316,7 +314,7 @@ MathJax.Hub.Config({

    250. 11
    251. 12
    252. ...
    253. -
    254. 60
    255. +
    256. 59
    257. »
    258. diff --git a/doc/pub/week36/html/._week36-bs003.html b/doc/pub/week36/html/._week36-bs003.html index 016405d5f..ff7746d93 100644 --- a/doc/pub/week36/html/._week36-bs003.html +++ b/doc/pub/week36/html/._week36-bs003.html @@ -37,7 +37,6 @@ doconce format html week36.do.txt --html_style=bootstrap --pygments_html_style=d
    259. Plans for week 36
    260. -
    261. Thursday September 8
    262. -
    263. Summary from last Week and discussion of SVD, Ridge and Lasso regression with examples
    264. -
    265. Linear Regression and the SVD
    266. -
    267. What does it mean?
    268. -
    269. And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
    270. -
    271. Code for SVD and Inversion of Matrices
    272. -
    273. Inverse of Rectangular Matrix
    274. -
    275. Ridge and LASSO Regression
    276. -
    277. From OLS to Ridge and Lasso
    278. -
    279. Deriving the Ridge Regression Equations
    280. -
    281. Note on Scikit-Learn
    282. -
    283. Comparison with OLS
    284. -
    285. SVD analysis
    286. -
    287. Interpreting the Ridge results
    288. -
    289. More interpretations
    290. -
    291. Deriving the Lasso Regression Equations
    292. -
    293. Simple example to illustrate Ordinary Least Squares, Ridge and Lasso Regression
    294. -
    295. Ridge Regression
    296. -
    297. Lasso Regression
    298. -
    299. Yet another Example
    300. -
    301. The OLS case
    302. -
    303. The Ridge case
    304. -
    305. Writing the Cost Function
    306. -
    307. Lasso case
    308. -
    309. The first Case
    310. -
    311. Simple code for solving the above problem
    312. -
    313. With Lasso Regression
    314. -
    315. Another Example, now with a polynomial fit
    316. -
    317. To think about, first part
    318. -
    319. More thinking
    320. -
    321. Still thinking
    322. -
    323. What does centering (subtracting the mean values) mean mathematically?
    324. -
    325. Further Manipulations
    326. -
    327. Wrapping it up
    328. -
    329. Linear Regression code, Intercept handling first
    330. -
    331. Code Examples
    332. -
    333. Taking out the mean
    334. -
    335. Friday September 9
    336. -
    337. Linking the regression analysis with a statistical interpretation
    338. -
    339. Assumptions made
    340. -
    341. Expectation value and variance
    342. -
    343. Expectation value and variance for \( \boldsymbol{\beta} \)
    344. -
    345. Deriving OLS from a probability distribution
    346. -
    347. Independent and Identically Distrubuted (iid)
    348. -
    349. Maximum Likelihood Estimation (MLE)
    350. -
    351. A new Cost Function
    352. -
    353. More basic Statistics and Bayes' theorem
    354. -
    355. Marginal Probability
    356. -
    357. Conditional Probability
    358. -
    359. Bayes' Theorem
    360. -
    361. Interpretations of Bayes' Theorem
    362. -
    363. Example of Usage of Bayes' theorem
    364. -
    365. Doing it correctly
    366. -
    367. Bayes' Theorem and Ridge and Lasso Regression
    368. -
    369. Test Function for what happens with OLS, Ridge and Lasso
    370. -
    371. Invoking Bayes' theorem
    372. -
    373. Ridge and Bayes
    374. -
    375. Lasso and Bayes
    376. -
    377. Exercise 1: mean values and variances in linear regression
    378. -
    379. Exercise 2: Adding Ridge and Lasso Regression
    380. +
    381. Summary from last Week and discussion of SVD, Ridge and Lasso regression with examples
    382. +
    383. Linear Regression and the SVD
    384. +
    385. What does it mean?
    386. +
    387. And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
    388. +
    389. Code for SVD and Inversion of Matrices
    390. +
    391. Inverse of Rectangular Matrix
    392. +
    393. Ridge and LASSO Regression
    394. +
    395. From OLS to Ridge and Lasso
    396. +
    397. Deriving the Ridge Regression Equations
    398. +
    399. Note on Scikit-Learn
    400. +
    401. Comparison with OLS
    402. +
    403. SVD analysis
    404. +
    405. Interpreting the Ridge results
    406. +
    407. More interpretations
    408. +
    409. Deriving the Lasso Regression Equations
    410. +
    411. Simple example to illustrate Ordinary Least Squares, Ridge and Lasso Regression
    412. +
    413. Ridge Regression
    414. +
    415. Lasso Regression
    416. +
    417. Yet another Example
    418. +
    419. The OLS case
    420. +
    421. The Ridge case
    422. +
    423. Writing the Cost Function
    424. +
    425. Lasso case
    426. +
    427. The first Case
    428. +
    429. Simple code for solving the above problem
    430. +
    431. With Lasso Regression
    432. +
    433. Another Example, now with a polynomial fit
    434. +
    435. To think about, first part
    436. +
    437. More thinking
    438. +
    439. Still thinking
    440. +
    441. What does centering (subtracting the mean values) mean mathematically?
    442. +
    443. Further Manipulations
    444. +
    445. Wrapping it up
    446. +
    447. Linear Regression code, Intercept handling first
    448. +
    449. Code Examples
    450. +
    451. Taking out the mean
    452. +
    453. Friday September 9
    454. +
    455. Linking the regression analysis with a statistical interpretation
    456. +
    457. Assumptions made
    458. +
    459. Expectation value and variance
    460. +
    461. Expectation value and variance for \( \boldsymbol{\beta} \)
    462. +
    463. Deriving OLS from a probability distribution
    464. +
    465. Independent and Identically Distrubuted (iid)
    466. +
    467. Maximum Likelihood Estimation (MLE)
    468. +
    469. A new Cost Function
    470. +
    471. More basic Statistics and Bayes' theorem
    472. +
    473. Marginal Probability
    474. +
    475. Conditional Probability
    476. +
    477. Bayes' Theorem
    478. +
    479. Interpretations of Bayes' Theorem
    480. +
    481. Example of Usage of Bayes' theorem
    482. +
    483. Doing it correctly
    484. +
    485. Bayes' Theorem and Ridge and Lasso Regression
    486. +
    487. Test Function for what happens with OLS, Ridge and Lasso
    488. +
    489. Invoking Bayes' theorem
    490. +
    491. Ridge and Bayes
    492. +
    493. Lasso and Bayes
    494. +
    495. Exercise 1: mean values and variances in linear regression
    496. +
    497. Exercise 2: Adding Ridge and Lasso Regression
    498. @@ -297,7 +295,39 @@ MathJax.Hub.Config({

       

       

       

      -

      Summary from last Week and discussion of SVD, Ridge and Lasso regression with examples

      +

      Linear Regression and the SVD

      + +

      We used the SVD to analyse the matrix to invert in ordinary lineat regression

      +$$ +\boldsymbol{X}^T\boldsymbol{X}=\boldsymbol{V}\boldsymbol{\Sigma}^T\boldsymbol{U}^T\boldsymbol{U}\boldsymbol{\Sigma}\boldsymbol{V}^T=\boldsymbol{V}\boldsymbol{\Sigma}^T\boldsymbol{\Sigma}\boldsymbol{V}^T. +$$ + +

      Since the matrices here have dimension \( p\times p \), with \( p \) corresponding to the singular values, we defined last week the matrix

      +$$ +\boldsymbol{\Sigma}^T\boldsymbol{\Sigma} = \begin{bmatrix} \tilde{\boldsymbol{\Sigma}} & \boldsymbol{0}\\ \end{bmatrix}\begin{bmatrix} \tilde{\boldsymbol{\Sigma}} \\ \boldsymbol{0}\end{bmatrix}, +$$ + +

      where the tilde-matrix \( \tilde{\boldsymbol{\Sigma}} \) is a matrix of dimension \( p\times p \) containing only the singular values \( \sigma_i \), that is

      + +$$ +\tilde{\boldsymbol{\Sigma}}=\begin{bmatrix} \sigma_0 & 0 & 0 & \dots & 0 & 0 \\ + 0 & \sigma_1 & 0 & \dots & 0 & 0 \\ + 0 & 0 & \sigma_2 & \dots & 0 & 0 \\ + 0 & 0 & 0 & \dots & \sigma_{p-2} & 0 \\ + 0 & 0 & 0 & \dots & 0 & \sigma_{p-1} \\ +\end{bmatrix}, +$$ + +

      meaning we can write

      +$$ +\boldsymbol{X}^T\boldsymbol{X}=\boldsymbol{V}\tilde{\boldsymbol{\Sigma}}^2\boldsymbol{V}^T. +$$ + +

      Multiplying from the right with \( \boldsymbol{V} \) (using the orthogonality of \( \boldsymbol{V} \)) we get

      +$$ +\left(\boldsymbol{X}^T\boldsymbol{X}\right)\boldsymbol{V}=\boldsymbol{V}\tilde{\boldsymbol{\Sigma}}^2. +$$ +

      @@ -317,7 +347,7 @@ MathJax.Hub.Config({

    499. 12
    500. 13
    501. ...
    502. -
    503. 60
    504. +
    505. 59
    506. »
    507. diff --git a/doc/pub/week36/html/._week36-bs004.html b/doc/pub/week36/html/._week36-bs004.html index 0bef54934..9ec432f70 100644 --- a/doc/pub/week36/html/._week36-bs004.html +++ b/doc/pub/week36/html/._week36-bs004.html @@ -37,7 +37,6 @@ doconce format html week36.do.txt --html_style=bootstrap --pygments_html_style=d
    508. Plans for week 36
    509. -
    510. Thursday September 8
    511. -
    512. Summary from last Week and discussion of SVD, Ridge and Lasso regression with examples
    513. -
    514. Linear Regression and the SVD
    515. -
    516. What does it mean?
    517. -
    518. And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
    519. -
    520. Code for SVD and Inversion of Matrices
    521. -
    522. Inverse of Rectangular Matrix
    523. -
    524. Ridge and LASSO Regression
    525. -
    526. From OLS to Ridge and Lasso
    527. -
    528. Deriving the Ridge Regression Equations
    529. -
    530. Note on Scikit-Learn
    531. -
    532. Comparison with OLS
    533. -
    534. SVD analysis
    535. -
    536. Interpreting the Ridge results
    537. -
    538. More interpretations
    539. -
    540. Deriving the Lasso Regression Equations
    541. -
    542. Simple example to illustrate Ordinary Least Squares, Ridge and Lasso Regression
    543. -
    544. Ridge Regression
    545. -
    546. Lasso Regression
    547. -
    548. Yet another Example
    549. -
    550. The OLS case
    551. -
    552. The Ridge case
    553. -
    554. Writing the Cost Function
    555. -
    556. Lasso case
    557. -
    558. The first Case
    559. -
    560. Simple code for solving the above problem
    561. -
    562. With Lasso Regression
    563. -
    564. Another Example, now with a polynomial fit
    565. -
    566. To think about, first part
    567. -
    568. More thinking
    569. -
    570. Still thinking
    571. -
    572. What does centering (subtracting the mean values) mean mathematically?
    573. -
    574. Further Manipulations
    575. -
    576. Wrapping it up
    577. -
    578. Linear Regression code, Intercept handling first
    579. -
    580. Code Examples
    581. -
    582. Taking out the mean
    583. -
    584. Friday September 9
    585. -
    586. Linking the regression analysis with a statistical interpretation
    587. -
    588. Assumptions made
    589. -
    590. Expectation value and variance
    591. -
    592. Expectation value and variance for \( \boldsymbol{\beta} \)
    593. -
    594. Deriving OLS from a probability distribution
    595. -
    596. Independent and Identically Distrubuted (iid)
    597. -
    598. Maximum Likelihood Estimation (MLE)
    599. -
    600. A new Cost Function
    601. -
    602. More basic Statistics and Bayes' theorem
    603. -
    604. Marginal Probability
    605. -
    606. Conditional Probability
    607. -
    608. Bayes' Theorem
    609. -
    610. Interpretations of Bayes' Theorem
    611. -
    612. Example of Usage of Bayes' theorem
    613. -
    614. Doing it correctly
    615. -
    616. Bayes' Theorem and Ridge and Lasso Regression
    617. -
    618. Test Function for what happens with OLS, Ridge and Lasso
    619. -
    620. Invoking Bayes' theorem
    621. -
    622. Ridge and Bayes
    623. -
    624. Lasso and Bayes
    625. -
    626. Exercise 1: mean values and variances in linear regression
    627. -
    628. Exercise 2: Adding Ridge and Lasso Regression
    629. +
    630. Summary from last Week and discussion of SVD, Ridge and Lasso regression with examples
    631. +
    632. Linear Regression and the SVD
    633. +
    634. What does it mean?
    635. +
    636. And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
    637. +
    638. Code for SVD and Inversion of Matrices
    639. +
    640. Inverse of Rectangular Matrix
    641. +
    642. Ridge and LASSO Regression
    643. +
    644. From OLS to Ridge and Lasso
    645. +
    646. Deriving the Ridge Regression Equations
    647. +
    648. Note on Scikit-Learn
    649. +
    650. Comparison with OLS
    651. +
    652. SVD analysis
    653. +
    654. Interpreting the Ridge results
    655. +
    656. More interpretations
    657. +
    658. Deriving the Lasso Regression Equations
    659. +
    660. Simple example to illustrate Ordinary Least Squares, Ridge and Lasso Regression
    661. +
    662. Ridge Regression
    663. +
    664. Lasso Regression
    665. +
    666. Yet another Example
    667. +
    668. The OLS case
    669. +
    670. The Ridge case
    671. +
    672. Writing the Cost Function
    673. +
    674. Lasso case
    675. +
    676. The first Case
    677. +
    678. Simple code for solving the above problem
    679. +
    680. With Lasso Regression
    681. +
    682. Another Example, now with a polynomial fit
    683. +
    684. To think about, first part
    685. +
    686. More thinking
    687. +
    688. Still thinking
    689. +
    690. What does centering (subtracting the mean values) mean mathematically?
    691. +
    692. Further Manipulations
    693. +
    694. Wrapping it up
    695. +
    696. Linear Regression code, Intercept handling first
    697. +
    698. Code Examples
    699. +
    700. Taking out the mean
    701. +
    702. Friday September 9
    703. +
    704. Linking the regression analysis with a statistical interpretation
    705. +
    706. Assumptions made
    707. +
    708. Expectation value and variance
    709. +
    710. Expectation value and variance for \( \boldsymbol{\beta} \)
    711. +
    712. Deriving OLS from a probability distribution
    713. +
    714. Independent and Identically Distrubuted (iid)
    715. +
    716. Maximum Likelihood Estimation (MLE)
    717. +
    718. A new Cost Function
    719. +
    720. More basic Statistics and Bayes' theorem
    721. +
    722. Marginal Probability
    723. +
    724. Conditional Probability
    725. +
    726. Bayes' Theorem
    727. +
    728. Interpretations of Bayes' Theorem
    729. +
    730. Example of Usage of Bayes' theorem
    731. +
    732. Doing it correctly
    733. +
    734. Bayes' Theorem and Ridge and Lasso Regression
    735. +
    736. Test Function for what happens with OLS, Ridge and Lasso
    737. +
    738. Invoking Bayes' theorem
    739. +
    740. Ridge and Bayes
    741. +
    742. Lasso and Bayes
    743. +
    744. Exercise 1: mean values and variances in linear regression
    745. +
    746. Exercise 2: Adding Ridge and Lasso Regression
    747. @@ -297,39 +295,45 @@ MathJax.Hub.Config({

       

       

       

      -

      Linear Regression and the SVD

      +

      What does it mean?

      -

      We used the SVD to analyse the matrix to invert in ordinary lineat regression

      -$$ -\boldsymbol{X}^T\boldsymbol{X}=\boldsymbol{V}\boldsymbol{\Sigma}^T\boldsymbol{U}^T\boldsymbol{U}\boldsymbol{\Sigma}\boldsymbol{V}^T=\boldsymbol{V}\boldsymbol{\Sigma}^T\boldsymbol{\Sigma}\boldsymbol{V}^T. -$$ - -

      Since the matrices here have dimension \( p\times p \), with \( p \) corresponding to the singular values, we defined last week the matrix

      -$$ -\boldsymbol{\Sigma}^T\boldsymbol{\Sigma} = \begin{bmatrix} \tilde{\boldsymbol{\Sigma}} & \boldsymbol{0}\\ \end{bmatrix}\begin{bmatrix} \tilde{\boldsymbol{\Sigma}} \\ \boldsymbol{0}\end{bmatrix}, -$$ - -

      where the tilde-matrix \( \tilde{\boldsymbol{\Sigma}} \) is a matrix of dimension \( p\times p \) containing only the singular values \( \sigma_i \), that is

      +

      This means the vectors \( \boldsymbol{v}_i \) of the orthogonal matrix \( \boldsymbol{V} \) +are the eigenvectors of the matrix \( \boldsymbol{X}^T\boldsymbol{X} \) with eigenvalues +given by the singular values squared, that is +

      $$ -\tilde{\boldsymbol{\Sigma}}=\begin{bmatrix} \sigma_0 & 0 & 0 & \dots & 0 & 0 \\ - 0 & \sigma_1 & 0 & \dots & 0 & 0 \\ - 0 & 0 & \sigma_2 & \dots & 0 & 0 \\ - 0 & 0 & 0 & \dots & \sigma_{p-2} & 0 \\ - 0 & 0 & 0 & \dots & 0 & \sigma_{p-1} \\ -\end{bmatrix}, +\left(\boldsymbol{X}^T\boldsymbol{X}\right)\boldsymbol{v}_i=\boldsymbol{v}_i\sigma_i^2. $$ -

      meaning we can write

      +

      In other words, each non-zero singular value of \( \boldsymbol{X} \) is a positive +square root of an eigenvalue of \( \boldsymbol{X}^T\boldsymbol{X} \). It means also that +the columns of \( \boldsymbol{V} \) are the eigenvectors of +\( \boldsymbol{X}^T\boldsymbol{X} \). Since we have ordered the singular values of +\( \boldsymbol{X} \) in a descending order, it means that the column vectors +\( \boldsymbol{v}_i \) are hierarchically ordered by how much correlation they +encode from the columns of \( \boldsymbol{X} \). +

      + +

      Note that these are also the eigenvectors and eigenvalues of the +Hessian matrix. +

      + +

      If we now recall the definition of the covariance matrix (not using +Bessel's correction) we have +

      + $$ -\boldsymbol{X}^T\boldsymbol{X}=\boldsymbol{V}\tilde{\boldsymbol{\Sigma}}^2\boldsymbol{V}^T. -$$ - -

      Multiplying from the right with \( \boldsymbol{V} \) (using the orthogonality of \( \boldsymbol{V} \)) we get

      -$$ -\left(\boldsymbol{X}^T\boldsymbol{X}\right)\boldsymbol{V}=\boldsymbol{V}\tilde{\boldsymbol{\Sigma}}^2. +\boldsymbol{C}[\boldsymbol{X}]=\frac{1}{n}\boldsymbol{X}^T\boldsymbol{X}, $$ +

      meaning that every squared non-singular value of \( \boldsymbol{X} \) divided by \( n \) ( +the number of samples) are the eigenvalues of the covariance +matrix. Every singular value of \( \boldsymbol{X} \) is thus a positive square +root of an eigenvalue of \( \boldsymbol{X}^T\boldsymbol{X} \). If the matrix \( \boldsymbol{X} \) is +self-adjoint, the singular values of \( \boldsymbol{X} \) are equal to the +absolute value of the eigenvalues of \( \boldsymbol{X} \). +

      @@ -350,7 +354,7 @@ $$

    748. 13
    749. 14
    750. ...
    751. -
    752. 60
    753. +
    754. 59
    755. »
    756. diff --git a/doc/pub/week36/html/._week36-bs005.html b/doc/pub/week36/html/._week36-bs005.html index e468cd6b6..395ad8925 100644 --- a/doc/pub/week36/html/._week36-bs005.html +++ b/doc/pub/week36/html/._week36-bs005.html @@ -37,7 +37,6 @@ doconce format html week36.do.txt --html_style=bootstrap --pygments_html_style=d
    757. Plans for week 36
    758. -
    759. Thursday September 8
    760. -
    761. Summary from last Week and discussion of SVD, Ridge and Lasso regression with examples
    762. -
    763. Linear Regression and the SVD
    764. -
    765. What does it mean?
    766. -
    767. And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
    768. -
    769. Code for SVD and Inversion of Matrices
    770. -
    771. Inverse of Rectangular Matrix
    772. -
    773. Ridge and LASSO Regression
    774. -
    775. From OLS to Ridge and Lasso
    776. -
    777. Deriving the Ridge Regression Equations
    778. -
    779. Note on Scikit-Learn
    780. -
    781. Comparison with OLS
    782. -
    783. SVD analysis
    784. -
    785. Interpreting the Ridge results
    786. -
    787. More interpretations
    788. -
    789. Deriving the Lasso Regression Equations
    790. -
    791. Simple example to illustrate Ordinary Least Squares, Ridge and Lasso Regression
    792. -
    793. Ridge Regression
    794. -
    795. Lasso Regression
    796. -
    797. Yet another Example
    798. -
    799. The OLS case
    800. -
    801. The Ridge case
    802. -
    803. Writing the Cost Function
    804. -
    805. Lasso case
    806. -
    807. The first Case
    808. -
    809. Simple code for solving the above problem
    810. -
    811. With Lasso Regression
    812. -
    813. Another Example, now with a polynomial fit
    814. -
    815. To think about, first part
    816. -
    817. More thinking
    818. -
    819. Still thinking
    820. -
    821. What does centering (subtracting the mean values) mean mathematically?
    822. -
    823. Further Manipulations
    824. -
    825. Wrapping it up
    826. -
    827. Linear Regression code, Intercept handling first
    828. -
    829. Code Examples
    830. -
    831. Taking out the mean
    832. -
    833. Friday September 9
    834. -
    835. Linking the regression analysis with a statistical interpretation
    836. -
    837. Assumptions made
    838. -
    839. Expectation value and variance
    840. -
    841. Expectation value and variance for \( \boldsymbol{\beta} \)
    842. -
    843. Deriving OLS from a probability distribution
    844. -
    845. Independent and Identically Distrubuted (iid)
    846. -
    847. Maximum Likelihood Estimation (MLE)
    848. -
    849. A new Cost Function
    850. -
    851. More basic Statistics and Bayes' theorem
    852. -
    853. Marginal Probability
    854. -
    855. Conditional Probability
    856. -
    857. Bayes' Theorem
    858. -
    859. Interpretations of Bayes' Theorem
    860. -
    861. Example of Usage of Bayes' theorem
    862. -
    863. Doing it correctly
    864. -
    865. Bayes' Theorem and Ridge and Lasso Regression
    866. -
    867. Test Function for what happens with OLS, Ridge and Lasso
    868. -
    869. Invoking Bayes' theorem
    870. -
    871. Ridge and Bayes
    872. -
    873. Lasso and Bayes
    874. -
    875. Exercise 1: mean values and variances in linear regression
    876. -
    877. Exercise 2: Adding Ridge and Lasso Regression
    878. +
    879. Summary from last Week and discussion of SVD, Ridge and Lasso regression with examples
    880. +
    881. Linear Regression and the SVD
    882. +
    883. What does it mean?
    884. +
    885. And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
    886. +
    887. Code for SVD and Inversion of Matrices
    888. +
    889. Inverse of Rectangular Matrix
    890. +
    891. Ridge and LASSO Regression
    892. +
    893. From OLS to Ridge and Lasso
    894. +
    895. Deriving the Ridge Regression Equations
    896. +
    897. Note on Scikit-Learn
    898. +
    899. Comparison with OLS
    900. +
    901. SVD analysis
    902. +
    903. Interpreting the Ridge results
    904. +
    905. More interpretations
    906. +
    907. Deriving the Lasso Regression Equations
    908. +
    909. Simple example to illustrate Ordinary Least Squares, Ridge and Lasso Regression
    910. +
    911. Ridge Regression
    912. +
    913. Lasso Regression
    914. +
    915. Yet another Example
    916. +
    917. The OLS case
    918. +
    919. The Ridge case
    920. +
    921. Writing the Cost Function
    922. +
    923. Lasso case
    924. +
    925. The first Case
    926. +
    927. Simple code for solving the above problem
    928. +
    929. With Lasso Regression
    930. +
    931. Another Example, now with a polynomial fit
    932. +
    933. To think about, first part
    934. +
    935. More thinking
    936. +
    937. Still thinking
    938. +
    939. What does centering (subtracting the mean values) mean mathematically?
    940. +
    941. Further Manipulations
    942. +
    943. Wrapping it up
    944. +
    945. Linear Regression code, Intercept handling first
    946. +
    947. Code Examples
    948. +
    949. Taking out the mean
    950. +
    951. Friday September 9
    952. +
    953. Linking the regression analysis with a statistical interpretation
    954. +
    955. Assumptions made
    956. +
    957. Expectation value and variance
    958. +
    959. Expectation value and variance for \( \boldsymbol{\beta} \)
    960. +
    961. Deriving OLS from a probability distribution
    962. +
    963. Independent and Identically Distrubuted (iid)
    964. +
    965. Maximum Likelihood Estimation (MLE)
    966. +
    967. A new Cost Function
    968. +
    969. More basic Statistics and Bayes' theorem
    970. +
    971. Marginal Probability
    972. +
    973. Conditional Probability
    974. +
    975. Bayes' Theorem
    976. +
    977. Interpretations of Bayes' Theorem
    978. +
    979. Example of Usage of Bayes' theorem
    980. +
    981. Doing it correctly
    982. +
    983. Bayes' Theorem and Ridge and Lasso Regression
    984. +
    985. Test Function for what happens with OLS, Ridge and Lasso
    986. +
    987. Invoking Bayes' theorem
    988. +
    989. Ridge and Bayes
    990. +
    991. Lasso and Bayes
    992. +
    993. Exercise 1: mean values and variances in linear regression
    994. +
    995. Exercise 2: Adding Ridge and Lasso Regression
    996. @@ -297,44 +295,38 @@ MathJax.Hub.Config({

       

       

       

      -

      What does it mean?

      +

      And finally \( \boldsymbol{X}\boldsymbol{X}^T \)

      -

      This means the vectors \( \boldsymbol{v}_i \) of the orthogonal matrix \( \boldsymbol{V} \) -are the eigenvectors of the matrix \( \boldsymbol{X}^T\boldsymbol{X} \) with eigenvalues -given by the singular values squared, that is -

      +

      For \( \boldsymbol{X}\boldsymbol{X}^T \) we found

      $$ -\left(\boldsymbol{X}^T\boldsymbol{X}\right)\boldsymbol{v}_i=\boldsymbol{v}_i\sigma_i^2. +\boldsymbol{X}\boldsymbol{X}^T=\boldsymbol{U}\boldsymbol{\Sigma}\boldsymbol{V}^T\boldsymbol{V}\boldsymbol{\Sigma}^T\boldsymbol{U}^T=\boldsymbol{U}\boldsymbol{\Sigma}^T\boldsymbol{\Sigma}\boldsymbol{U}^T. $$ -

      In other words, each non-zero singular value of \( \boldsymbol{X} \) is a positive -square root of an eigenvalue of \( \boldsymbol{X}^T\boldsymbol{X} \). It means also that -the columns of \( \boldsymbol{V} \) are the eigenvectors of -\( \boldsymbol{X}^T\boldsymbol{X} \). Since we have ordered the singular values of -\( \boldsymbol{X} \) in a descending order, it means that the column vectors -\( \boldsymbol{v}_i \) are hierarchically ordered by how much correlation they -encode from the columns of \( \boldsymbol{X} \). -

      - -

      Note that these are also the eigenvectors and eigenvalues of the -Hessian matrix. -

      - -

      If we now recall the definition of the covariance matrix (not using -Bessel's correction) we have -

      - +

      Since the matrices here have dimension \( n\times n \), we have

      $$ -\boldsymbol{C}[\boldsymbol{X}]=\frac{1}{n}\boldsymbol{X}^T\boldsymbol{X}, +\boldsymbol{\Sigma}\boldsymbol{\Sigma}^T = \begin{bmatrix} \tilde{\boldsymbol{\Sigma}} \\ \boldsymbol{0}\\ \end{bmatrix}\begin{bmatrix} \tilde{\boldsymbol{\Sigma}} \boldsymbol{0}\\ \end{bmatrix}=\begin{bmatrix} \tilde{\boldsymbol{\Sigma}} & \boldsymbol{0} \\ \boldsymbol{0} & \boldsymbol{0}\\ \end{bmatrix}, $$ -

      meaning that every squared non-singular value of \( \boldsymbol{X} \) divided by \( n \) ( -the number of samples) are the eigenvalues of the covariance -matrix. Every singular value of \( \boldsymbol{X} \) is thus a positive square -root of an eigenvalue of \( \boldsymbol{X}^T\boldsymbol{X} \). If the matrix \( \boldsymbol{X} \) is -self-adjoint, the singular values of \( \boldsymbol{X} \) are equal to the -absolute value of the eigenvalues of \( \boldsymbol{X} \). +

      leading to

      +$$ +\boldsymbol{X}\boldsymbol{X}^T=\boldsymbol{U}\begin{bmatrix} \tilde{\boldsymbol{\Sigma}} & \boldsymbol{0} \\ \boldsymbol{0} & \boldsymbol{0}\\ \end{bmatrix}\boldsymbol{U}^T. +$$ + +

      Multiplying with \( \boldsymbol{U} \) from the right gives us the eigenvalue problem

      +$$ +(\boldsymbol{X}\boldsymbol{X}^T)\boldsymbol{U}=\boldsymbol{U}\begin{bmatrix} \tilde{\boldsymbol{\Sigma}} & \boldsymbol{0} \\ \boldsymbol{0} & \boldsymbol{0}\\ \end{bmatrix}. +$$ + +

      It means that the eigenvalues of \( \boldsymbol{X}\boldsymbol{X}^T \) are again given by +the non-zero singular values plus now a series of zeros. The column +vectors of \( \boldsymbol{U} \) are the eigenvectors of \( \boldsymbol{X}\boldsymbol{X}^T \) and +measure how much correlations are contained in the rows of \( \boldsymbol{X} \). +

      + +

      Since we will mainly be interested in the correlations among the features +of our data (the columns of \( \boldsymbol{X} \), the quantity of interest for us are the non-zero singular +values and the column vectors of \( \boldsymbol{V} \).

      @@ -357,7 +349,7 @@ absolute value of the eigenvalues of \( \boldsymbol{X} \).

    997. 14
    998. 15
    999. ...
    1000. -
    1001. 60
    1002. +
    1003. 59
    1004. »
    1005. diff --git a/doc/pub/week36/html/._week36-bs006.html b/doc/pub/week36/html/._week36-bs006.html index 395a166a7..996408460 100644 --- a/doc/pub/week36/html/._week36-bs006.html +++ b/doc/pub/week36/html/._week36-bs006.html @@ -37,7 +37,6 @@ doconce format html week36.do.txt --html_style=bootstrap --pygments_html_style=d
    1006. Plans for week 36
    1007. -
    1008. Thursday September 8
    1009. -
    1010. Summary from last Week and discussion of SVD, Ridge and Lasso regression with examples
    1011. -
    1012. Linear Regression and the SVD
    1013. -
    1014. What does it mean?
    1015. -
    1016. And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
    1017. -
    1018. Code for SVD and Inversion of Matrices
    1019. -
    1020. Inverse of Rectangular Matrix
    1021. -
    1022. Ridge and LASSO Regression
    1023. -
    1024. From OLS to Ridge and Lasso
    1025. -
    1026. Deriving the Ridge Regression Equations
    1027. -
    1028. Note on Scikit-Learn
    1029. -
    1030. Comparison with OLS
    1031. -
    1032. SVD analysis
    1033. -
    1034. Interpreting the Ridge results
    1035. -
    1036. More interpretations
    1037. -
    1038. Deriving the Lasso Regression Equations
    1039. -
    1040. Simple example to illustrate Ordinary Least Squares, Ridge and Lasso Regression
    1041. -
    1042. Ridge Regression
    1043. -
    1044. Lasso Regression
    1045. -
    1046. Yet another Example
    1047. -
    1048. The OLS case
    1049. -
    1050. The Ridge case
    1051. -
    1052. Writing the Cost Function
    1053. -
    1054. Lasso case
    1055. -
    1056. The first Case
    1057. -
    1058. Simple code for solving the above problem
    1059. -
    1060. With Lasso Regression
    1061. -
    1062. Another Example, now with a polynomial fit
    1063. -
    1064. To think about, first part
    1065. -
    1066. More thinking
    1067. -
    1068. Still thinking
    1069. -
    1070. What does centering (subtracting the mean values) mean mathematically?
    1071. -
    1072. Further Manipulations
    1073. -
    1074. Wrapping it up
    1075. -
    1076. Linear Regression code, Intercept handling first
    1077. -
    1078. Code Examples
    1079. -
    1080. Taking out the mean
    1081. -
    1082. Friday September 9
    1083. -
    1084. Linking the regression analysis with a statistical interpretation
    1085. -
    1086. Assumptions made
    1087. -
    1088. Expectation value and variance
    1089. -
    1090. Expectation value and variance for \( \boldsymbol{\beta} \)
    1091. -
    1092. Deriving OLS from a probability distribution
    1093. -
    1094. Independent and Identically Distrubuted (iid)
    1095. -
    1096. Maximum Likelihood Estimation (MLE)
    1097. -
    1098. A new Cost Function
    1099. -
    1100. More basic Statistics and Bayes' theorem
    1101. -
    1102. Marginal Probability
    1103. -
    1104. Conditional Probability
    1105. -
    1106. Bayes' Theorem
    1107. -
    1108. Interpretations of Bayes' Theorem
    1109. -
    1110. Example of Usage of Bayes' theorem
    1111. -
    1112. Doing it correctly
    1113. -
    1114. Bayes' Theorem and Ridge and Lasso Regression
    1115. -
    1116. Test Function for what happens with OLS, Ridge and Lasso
    1117. -
    1118. Invoking Bayes' theorem
    1119. -
    1120. Ridge and Bayes
    1121. -
    1122. Lasso and Bayes
    1123. -
    1124. Exercise 1: mean values and variances in linear regression
    1125. -
    1126. Exercise 2: Adding Ridge and Lasso Regression
    1127. +
    1128. Summary from last Week and discussion of SVD, Ridge and Lasso regression with examples
    1129. +
    1130. Linear Regression and the SVD
    1131. +
    1132. What does it mean?
    1133. +
    1134. And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
    1135. +
    1136. Code for SVD and Inversion of Matrices
    1137. +
    1138. Inverse of Rectangular Matrix
    1139. +
    1140. Ridge and LASSO Regression
    1141. +
    1142. From OLS to Ridge and Lasso
    1143. +
    1144. Deriving the Ridge Regression Equations
    1145. +
    1146. Note on Scikit-Learn
    1147. +
    1148. Comparison with OLS
    1149. +
    1150. SVD analysis
    1151. +
    1152. Interpreting the Ridge results
    1153. +
    1154. More interpretations
    1155. +
    1156. Deriving the Lasso Regression Equations
    1157. +
    1158. Simple example to illustrate Ordinary Least Squares, Ridge and Lasso Regression
    1159. +
    1160. Ridge Regression
    1161. +
    1162. Lasso Regression
    1163. +
    1164. Yet another Example
    1165. +
    1166. The OLS case
    1167. +
    1168. The Ridge case
    1169. +
    1170. Writing the Cost Function
    1171. +
    1172. Lasso case
    1173. +
    1174. The first Case
    1175. +
    1176. Simple code for solving the above problem
    1177. +
    1178. With Lasso Regression
    1179. +
    1180. Another Example, now with a polynomial fit
    1181. +
    1182. To think about, first part
    1183. +
    1184. More thinking
    1185. +
    1186. Still thinking
    1187. +
    1188. What does centering (subtracting the mean values) mean mathematically?
    1189. +
    1190. Further Manipulations
    1191. +
    1192. Wrapping it up
    1193. +
    1194. Linear Regression code, Intercept handling first
    1195. +
    1196. Code Examples
    1197. +
    1198. Taking out the mean
    1199. +
    1200. Friday September 9
    1201. +
    1202. Linking the regression analysis with a statistical interpretation
    1203. +
    1204. Assumptions made
    1205. +
    1206. Expectation value and variance
    1207. +
    1208. Expectation value and variance for \( \boldsymbol{\beta} \)
    1209. +
    1210. Deriving OLS from a probability distribution
    1211. +
    1212. Independent and Identically Distrubuted (iid)
    1213. +
    1214. Maximum Likelihood Estimation (MLE)
    1215. +
    1216. A new Cost Function
    1217. +
    1218. More basic Statistics and Bayes' theorem
    1219. +
    1220. Marginal Probability
    1221. +
    1222. Conditional Probability
    1223. +
    1224. Bayes' Theorem
    1225. +
    1226. Interpretations of Bayes' Theorem
    1227. +
    1228. Example of Usage of Bayes' theorem
    1229. +
    1230. Doing it correctly
    1231. +
    1232. Bayes' Theorem and Ridge and Lasso Regression
    1233. +
    1234. Test Function for what happens with OLS, Ridge and Lasso
    1235. +
    1236. Invoking Bayes' theorem
    1237. +
    1238. Ridge and Bayes
    1239. +
    1240. Lasso and Bayes
    1241. +
    1242. Exercise 1: mean values and variances in linear regression
    1243. +
    1244. Exercise 2: Adding Ridge and Lasso Regression
    1245. @@ -297,39 +295,87 @@ MathJax.Hub.Config({

       

       

       

      -

      And finally \( \boldsymbol{X}\boldsymbol{X}^T \)

      +

      Code for SVD and Inversion of Matrices

      -

      For \( \boldsymbol{X}\boldsymbol{X}^T \) we found

      - -$$ -\boldsymbol{X}\boldsymbol{X}^T=\boldsymbol{U}\boldsymbol{\Sigma}\boldsymbol{V}^T\boldsymbol{V}\boldsymbol{\Sigma}^T\boldsymbol{U}^T=\boldsymbol{U}\boldsymbol{\Sigma}^T\boldsymbol{\Sigma}\boldsymbol{U}^T. -$$ - -

      Since the matrices here have dimension \( n\times n \), we have

      -$$ -\boldsymbol{\Sigma}\boldsymbol{\Sigma}^T = \begin{bmatrix} \tilde{\boldsymbol{\Sigma}} \\ \boldsymbol{0}\\ \end{bmatrix}\begin{bmatrix} \tilde{\boldsymbol{\Sigma}} \boldsymbol{0}\\ \end{bmatrix}=\begin{bmatrix} \tilde{\boldsymbol{\Sigma}} & \boldsymbol{0} \\ \boldsymbol{0} & \boldsymbol{0}\\ \end{bmatrix}, -$$ - -

      leading to

      -$$ -\boldsymbol{X}\boldsymbol{X}^T=\boldsymbol{U}\begin{bmatrix} \tilde{\boldsymbol{\Sigma}} & \boldsymbol{0} \\ \boldsymbol{0} & \boldsymbol{0}\\ \end{bmatrix}\boldsymbol{U}^T. -$$ - -

      Multiplying with \( \boldsymbol{U} \) from the right gives us the eigenvalue problem

      -$$ -(\boldsymbol{X}\boldsymbol{X}^T)\boldsymbol{U}=\boldsymbol{U}\begin{bmatrix} \tilde{\boldsymbol{\Sigma}} & \boldsymbol{0} \\ \boldsymbol{0} & \boldsymbol{0}\\ \end{bmatrix}. -$$ - -

      It means that the eigenvalues of \( \boldsymbol{X}\boldsymbol{X}^T \) are again given by -the non-zero singular values plus now a series of zeros. The column -vectors of \( \boldsymbol{U} \) are the eigenvectors of \( \boldsymbol{X}\boldsymbol{X}^T \) and -measure how much correlations are contained in the rows of \( \boldsymbol{X} \). +

      How do we use the SVD to invert a matrix \( \boldsymbol{X}^\boldsymbol{X} \) which is singular or near singular? +The simple answer is to use the linear algebra function for pseudoinvers, that is

      -

      Since we will mainly be interested in the correlations among the features -of our data (the columns of \( \boldsymbol{X} \), the quantity of interest for us are the non-zero singular -values and the column vectors of \( \boldsymbol{V} \). -

      + +
      +
      +
      +
      +
      +
      Ainv = np.linlag.pinv(A)
      +
      +
      +
      +
      +
      +
      +
      +
      +
      +
      +
      +
      +
      +
      + +

      Let us first look at a matrix which does not causes problems and write our own function where we just use the SVD.

      + + + +
      +
      +
      +
      +
      +
      import numpy as np
      +# SVD inversion
      +def SVDinv(A):
      +    ''' Takes as input a numpy matrix A and returns inv(A) based on singular value decomposition (SVD).
      +    SVD is numerically more stable than the inversion algorithms provided by
      +    numpy and scipy.linalg at the cost of being slower.
      +    '''
      +    U, s, VT = np.linalg.svd(A)
      +    print('test U')
      +    print( (np.transpose(U) @ U - U @np.transpose(U)))
      +    print('test VT')
      +    print( (np.transpose(VT) @ VT - VT @np.transpose(VT)))
      +
      +
      +    D = np.zeros((len(U),len(VT)))
      +    D = np.diag(s)
      +    UT = np.transpose(U); V = np.transpose(VT); invD = np.linalg.inv(D)
      +    return np.matmul(V,np.matmul(invD,UT))
      +
      +
      +#X = np.array([ [1.0, -1.0, 2.0], [1.0, 0.0, 1.0], [1.0, 2.0, -1.0], [1.0, 1.0, 0.0] ])
      +# Non-singular square matrix
      +X = np.array( [ [1,2,3],[2,4,5],[3,5,6]])
      +print(X)
      +A = np.transpose(X) @ X
      +# Brute force inversion
      +B = np.linalg.inv(A)  # here we could use np.linalg.pinv(A)
      +C = SVDinv(A)
      +print(np.abs(B-C))
      +
      +
      +
      +
      +
      +
      +
      +
      +
      +
      +
      +
      +
      +
      +

      @@ -352,7 +398,7 @@ values and the column vectors of \( \boldsymbol{V} \).

    1246. 15
    1247. 16
    1248. ...
    1249. -
    1250. 60
    1251. +
    1252. 59
    1253. »
    1254. diff --git a/doc/pub/week36/html/._week36-bs007.html b/doc/pub/week36/html/._week36-bs007.html index 055ec9aae..3e6dd57ec 100644 --- a/doc/pub/week36/html/._week36-bs007.html +++ b/doc/pub/week36/html/._week36-bs007.html @@ -37,7 +37,6 @@ doconce format html week36.do.txt --html_style=bootstrap --pygments_html_style=d
    1255. Plans for week 36
    1256. -
    1257. Thursday September 8
    1258. -
    1259. Summary from last Week and discussion of SVD, Ridge and Lasso regression with examples
    1260. -
    1261. Linear Regression and the SVD
    1262. -
    1263. What does it mean?
    1264. -
    1265. And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
    1266. -
    1267. Code for SVD and Inversion of Matrices
    1268. -
    1269. Inverse of Rectangular Matrix
    1270. -
    1271. Ridge and LASSO Regression
    1272. -
    1273. From OLS to Ridge and Lasso
    1274. -
    1275. Deriving the Ridge Regression Equations
    1276. -
    1277. Note on Scikit-Learn
    1278. -
    1279. Comparison with OLS
    1280. -
    1281. SVD analysis
    1282. -
    1283. Interpreting the Ridge results
    1284. -
    1285. More interpretations
    1286. -
    1287. Deriving the Lasso Regression Equations
    1288. -
    1289. Simple example to illustrate Ordinary Least Squares, Ridge and Lasso Regression
    1290. -
    1291. Ridge Regression
    1292. -
    1293. Lasso Regression
    1294. -
    1295. Yet another Example
    1296. -
    1297. The OLS case
    1298. -
    1299. The Ridge case
    1300. -
    1301. Writing the Cost Function
    1302. -
    1303. Lasso case
    1304. -
    1305. The first Case
    1306. -
    1307. Simple code for solving the above problem
    1308. -
    1309. With Lasso Regression
    1310. -
    1311. Another Example, now with a polynomial fit
    1312. -
    1313. To think about, first part
    1314. -
    1315. More thinking
    1316. -
    1317. Still thinking
    1318. -
    1319. What does centering (subtracting the mean values) mean mathematically?
    1320. -
    1321. Further Manipulations
    1322. -
    1323. Wrapping it up
    1324. -
    1325. Linear Regression code, Intercept handling first
    1326. -
    1327. Code Examples
    1328. -
    1329. Taking out the mean
    1330. -
    1331. Friday September 9
    1332. -
    1333. Linking the regression analysis with a statistical interpretation
    1334. -
    1335. Assumptions made
    1336. -
    1337. Expectation value and variance
    1338. -
    1339. Expectation value and variance for \( \boldsymbol{\beta} \)
    1340. -
    1341. Deriving OLS from a probability distribution
    1342. -
    1343. Independent and Identically Distrubuted (iid)
    1344. -
    1345. Maximum Likelihood Estimation (MLE)
    1346. -
    1347. A new Cost Function
    1348. -
    1349. More basic Statistics and Bayes' theorem
    1350. -
    1351. Marginal Probability
    1352. -
    1353. Conditional Probability
    1354. -
    1355. Bayes' Theorem
    1356. -
    1357. Interpretations of Bayes' Theorem
    1358. -
    1359. Example of Usage of Bayes' theorem
    1360. -
    1361. Doing it correctly
    1362. -
    1363. Bayes' Theorem and Ridge and Lasso Regression
    1364. -
    1365. Test Function for what happens with OLS, Ridge and Lasso
    1366. -
    1367. Invoking Bayes' theorem
    1368. -
    1369. Ridge and Bayes
    1370. -
    1371. Lasso and Bayes
    1372. -
    1373. Exercise 1: mean values and variances in linear regression
    1374. -
    1375. Exercise 2: Adding Ridge and Lasso Regression
    1376. +
    1377. Summary from last Week and discussion of SVD, Ridge and Lasso regression with examples
    1378. +
    1379. Linear Regression and the SVD
    1380. +
    1381. What does it mean?
    1382. +
    1383. And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
    1384. +
    1385. Code for SVD and Inversion of Matrices
    1386. +
    1387. Inverse of Rectangular Matrix
    1388. +
    1389. Ridge and LASSO Regression
    1390. +
    1391. From OLS to Ridge and Lasso
    1392. +
    1393. Deriving the Ridge Regression Equations
    1394. +
    1395. Note on Scikit-Learn
    1396. +
    1397. Comparison with OLS
    1398. +
    1399. SVD analysis
    1400. +
    1401. Interpreting the Ridge results
    1402. +
    1403. More interpretations
    1404. +
    1405. Deriving the Lasso Regression Equations
    1406. +
    1407. Simple example to illustrate Ordinary Least Squares, Ridge and Lasso Regression
    1408. +
    1409. Ridge Regression
    1410. +
    1411. Lasso Regression
    1412. +
    1413. Yet another Example
    1414. +
    1415. The OLS case
    1416. +
    1417. The Ridge case
    1418. +
    1419. Writing the Cost Function
    1420. +
    1421. Lasso case
    1422. +
    1423. The first Case
    1424. +
    1425. Simple code for solving the above problem
    1426. +
    1427. With Lasso Regression
    1428. +
    1429. Another Example, now with a polynomial fit
    1430. +
    1431. To think about, first part
    1432. +
    1433. More thinking
    1434. +
    1435. Still thinking
    1436. +
    1437. What does centering (subtracting the mean values) mean mathematically?
    1438. +
    1439. Further Manipulations
    1440. +
    1441. Wrapping it up
    1442. +
    1443. Linear Regression code, Intercept handling first
    1444. +
    1445. Code Examples
    1446. +
    1447. Taking out the mean
    1448. +
    1449. Friday September 9
    1450. +
    1451. Linking the regression analysis with a statistical interpretation
    1452. +
    1453. Assumptions made
    1454. +
    1455. Expectation value and variance
    1456. +
    1457. Expectation value and variance for \( \boldsymbol{\beta} \)
    1458. +
    1459. Deriving OLS from a probability distribution
    1460. +
    1461. Independent and Identically Distrubuted (iid)
    1462. +
    1463. Maximum Likelihood Estimation (MLE)
    1464. +
    1465. A new Cost Function
    1466. +
    1467. More basic Statistics and Bayes' theorem
    1468. +
    1469. Marginal Probability
    1470. +
    1471. Conditional Probability
    1472. +
    1473. Bayes' Theorem
    1474. +
    1475. Interpretations of Bayes' Theorem
    1476. +
    1477. Example of Usage of Bayes' theorem
    1478. +
    1479. Doing it correctly
    1480. +
    1481. Bayes' Theorem and Ridge and Lasso Regression
    1482. +
    1483. Test Function for what happens with OLS, Ridge and Lasso
    1484. +
    1485. Invoking Bayes' theorem
    1486. +
    1487. Ridge and Bayes
    1488. +
    1489. Lasso and Bayes
    1490. +
    1491. Exercise 1: mean values and variances in linear regression
    1492. +
    1493. Exercise 2: Adding Ridge and Lasso Regression
    1494. @@ -297,35 +295,24 @@ MathJax.Hub.Config({

       

       

       

      -

      Code for SVD and Inversion of Matrices

      +

      Inverse of Rectangular Matrix

      -

      How do we use the SVD to invert a matrix \( \boldsymbol{X}^\boldsymbol{X} \) which is singular or near singular? -The simple answer is to use the linear algebra function for pseudoinvers, that is +

      Although our matrix to invert \( \boldsymbol{X}^T\boldsymbol{X} \) is a square matrix, our matrix may be singular.

      + +

      The pseudoinverse is the generalization of the matrix inverse for square matrices to +rectangular matrices where the number of rows and columns are not equal.

      - -
      -
      -
      -
      -
      -
      Ainv = np.linlag.pinv(A)
      -
      -
      -
      -
      -
      -
      -
      -
      -
      -
      -
      -
      -
      -
      +

      It is also called the the Moore-Penrose Inverse after two independent discoverers of the method or the Generalized Inverse. +It is used for the calculation of the inverse for singular or near singular matrices and for rectangular matrices. +

      -

      Let us first look at a matrix which does not causes problems and write our own function where we just use the SVD.

      +

      Using the SVD we can obtain the pseudoinverse of a matrix \( \boldsymbol{A} \) (labeled here as \( \boldsymbol{A}_{\mathrm{PI}} \))

      +$$ +\boldsymbol{A}_{\mathrm{PI}}= \boldsymbol{V}\boldsymbol{D}_{\mathrm{PI}}\boldsymbol{U}^T, +$$ + +

      where \( \boldsymbol{D}_{\mathrm{PI}} \) can be calculated by creating a diagonal matrix from \( \boldsymbol{\Sigma} \) where we only keep the singular values (the non-zero values). The following code computes the pseudoinvers of the matrix based on the SVD.

      @@ -337,32 +324,26 @@ The simple answer is to use the linear algebra function for pseudoinvers, that i
      import numpy as np
       # SVD inversion
       def SVDinv(A):
      -    ''' Takes as input a numpy matrix A and returns inv(A) based on singular value decomposition (SVD).
      -    SVD is numerically more stable than the inversion algorithms provided by
      -    numpy and scipy.linalg at the cost of being slower.
      -    '''
           U, s, VT = np.linalg.svd(A)
      -    print('test U')
      -    print( (np.transpose(U) @ U - U @np.transpose(U)))
      -    print('test VT')
      -    print( (np.transpose(VT) @ VT - VT @np.transpose(VT)))
      +    # reciprocals of singular values of s
      +    d = 1.0 / s
      +    # create m x n D matrix
      +    D = np.zeros(A.shape)
      +    # populate D with n x n diagonal matrix
      +    D[:A.shape[1], :A.shape[1]] = np.diag(d)
      +    UT = np.transpose(U)
      +    V = np.transpose(VT)
      +    return np.matmul(V,np.matmul(D.T,UT))
       
       
      -    D = np.zeros((len(U),len(VT)))
      -    D = np.diag(s)
      -    UT = np.transpose(U); V = np.transpose(VT); invD = np.linalg.inv(D)
      -    return np.matmul(V,np.matmul(invD,UT))
      -
      -
      -#X = np.array([ [1.0, -1.0, 2.0], [1.0, 0.0, 1.0], [1.0, 2.0, -1.0], [1.0, 1.0, 0.0] ])
      -# Non-singular square matrix
      -X = np.array( [ [1,2,3],[2,4,5],[3,5,6]])
      -print(X)
      -A = np.transpose(X) @ X
      -# Brute force inversion
      -B = np.linalg.inv(A)  # here we could use np.linalg.pinv(A)
      +A = np.array([ [0.3, 0.4], [0.5, 0.6], [0.7, 0.8],[0.9, 1.0]])
      +print(A)
      +# Brute force inversion of super-collinear matrix
      +B = np.linalg.pinv(A)
      +print(B)
      +# Compare our own algorithm with pinv
       C = SVDinv(A)
      -print(np.abs(B-C))
      +print(np.abs(C-B))
       
      @@ -378,6 +359,7 @@ C = SVDinv(A) +

      As you can see from this example, our own decomposition based on the SVD agrees the pseudoinverse algorithm provided by Numpy.

      @@ -401,7 +383,7 @@ C = SVDinv(A)

    1495. 16
    1496. 17
    1497. ...
    1498. -
    1499. 60
    1500. +
    1501. 59
    1502. »
    1503. diff --git a/doc/pub/week36/html/._week36-bs008.html b/doc/pub/week36/html/._week36-bs008.html index a986fe0c1..ecd6ed89f 100644 --- a/doc/pub/week36/html/._week36-bs008.html +++ b/doc/pub/week36/html/._week36-bs008.html @@ -37,7 +37,6 @@ doconce format html week36.do.txt --html_style=bootstrap --pygments_html_style=d
    1504. Plans for week 36
    1505. -
    1506. Thursday September 8
    1507. -
    1508. Summary from last Week and discussion of SVD, Ridge and Lasso regression with examples
    1509. -
    1510. Linear Regression and the SVD
    1511. -
    1512. What does it mean?
    1513. -
    1514. And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
    1515. -
    1516. Code for SVD and Inversion of Matrices
    1517. -
    1518. Inverse of Rectangular Matrix
    1519. -
    1520. Ridge and LASSO Regression
    1521. -
    1522. From OLS to Ridge and Lasso
    1523. -
    1524. Deriving the Ridge Regression Equations
    1525. -
    1526. Note on Scikit-Learn
    1527. -
    1528. Comparison with OLS
    1529. -
    1530. SVD analysis
    1531. -
    1532. Interpreting the Ridge results
    1533. -
    1534. More interpretations
    1535. -
    1536. Deriving the Lasso Regression Equations
    1537. -
    1538. Simple example to illustrate Ordinary Least Squares, Ridge and Lasso Regression
    1539. -
    1540. Ridge Regression
    1541. -
    1542. Lasso Regression
    1543. -
    1544. Yet another Example
    1545. -
    1546. The OLS case
    1547. -
    1548. The Ridge case
    1549. -
    1550. Writing the Cost Function
    1551. -
    1552. Lasso case
    1553. -
    1554. The first Case
    1555. -
    1556. Simple code for solving the above problem
    1557. -
    1558. With Lasso Regression
    1559. -
    1560. Another Example, now with a polynomial fit
    1561. -
    1562. To think about, first part
    1563. -
    1564. More thinking
    1565. -
    1566. Still thinking
    1567. -
    1568. What does centering (subtracting the mean values) mean mathematically?
    1569. -
    1570. Further Manipulations
    1571. -
    1572. Wrapping it up
    1573. -
    1574. Linear Regression code, Intercept handling first
    1575. -
    1576. Code Examples
    1577. -
    1578. Taking out the mean
    1579. -
    1580. Friday September 9
    1581. -
    1582. Linking the regression analysis with a statistical interpretation
    1583. -
    1584. Assumptions made
    1585. -
    1586. Expectation value and variance
    1587. -
    1588. Expectation value and variance for \( \boldsymbol{\beta} \)
    1589. -
    1590. Deriving OLS from a probability distribution
    1591. -
    1592. Independent and Identically Distrubuted (iid)
    1593. -
    1594. Maximum Likelihood Estimation (MLE)
    1595. -
    1596. A new Cost Function
    1597. -
    1598. More basic Statistics and Bayes' theorem
    1599. -
    1600. Marginal Probability
    1601. -
    1602. Conditional Probability
    1603. -
    1604. Bayes' Theorem
    1605. -
    1606. Interpretations of Bayes' Theorem
    1607. -
    1608. Example of Usage of Bayes' theorem
    1609. -
    1610. Doing it correctly
    1611. -
    1612. Bayes' Theorem and Ridge and Lasso Regression
    1613. -
    1614. Test Function for what happens with OLS, Ridge and Lasso
    1615. -
    1616. Invoking Bayes' theorem
    1617. -
    1618. Ridge and Bayes
    1619. -
    1620. Lasso and Bayes
    1621. -
    1622. Exercise 1: mean values and variances in linear regression
    1623. -
    1624. Exercise 2: Adding Ridge and Lasso Regression
    1625. +
    1626. Summary from last Week and discussion of SVD, Ridge and Lasso regression with examples
    1627. +
    1628. Linear Regression and the SVD
    1629. +
    1630. What does it mean?
    1631. +
    1632. And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
    1633. +
    1634. Code for SVD and Inversion of Matrices
    1635. +
    1636. Inverse of Rectangular Matrix
    1637. +
    1638. Ridge and LASSO Regression
    1639. +
    1640. From OLS to Ridge and Lasso
    1641. +
    1642. Deriving the Ridge Regression Equations
    1643. +
    1644. Note on Scikit-Learn
    1645. +
    1646. Comparison with OLS
    1647. +
    1648. SVD analysis
    1649. +
    1650. Interpreting the Ridge results
    1651. +
    1652. More interpretations
    1653. +
    1654. Deriving the Lasso Regression Equations
    1655. +
    1656. Simple example to illustrate Ordinary Least Squares, Ridge and Lasso Regression
    1657. +
    1658. Ridge Regression
    1659. +
    1660. Lasso Regression
    1661. +
    1662. Yet another Example
    1663. +
    1664. The OLS case
    1665. +
    1666. The Ridge case
    1667. +
    1668. Writing the Cost Function
    1669. +
    1670. Lasso case
    1671. +
    1672. The first Case
    1673. +
    1674. Simple code for solving the above problem
    1675. +
    1676. With Lasso Regression
    1677. +
    1678. Another Example, now with a polynomial fit
    1679. +
    1680. To think about, first part
    1681. +
    1682. More thinking
    1683. +
    1684. Still thinking
    1685. +
    1686. What does centering (subtracting the mean values) mean mathematically?
    1687. +
    1688. Further Manipulations
    1689. +
    1690. Wrapping it up
    1691. +
    1692. Linear Regression code, Intercept handling first
    1693. +
    1694. Code Examples
    1695. +
    1696. Taking out the mean
    1697. +
    1698. Friday September 9
    1699. +
    1700. Linking the regression analysis with a statistical interpretation
    1701. +
    1702. Assumptions made
    1703. +
    1704. Expectation value and variance
    1705. +
    1706. Expectation value and variance for \( \boldsymbol{\beta} \)
    1707. +
    1708. Deriving OLS from a probability distribution
    1709. +
    1710. Independent and Identically Distrubuted (iid)
    1711. +
    1712. Maximum Likelihood Estimation (MLE)
    1713. +
    1714. A new Cost Function
    1715. +
    1716. More basic Statistics and Bayes' theorem
    1717. +
    1718. Marginal Probability
    1719. +
    1720. Conditional Probability
    1721. +
    1722. Bayes' Theorem
    1723. +
    1724. Interpretations of Bayes' Theorem
    1725. +
    1726. Example of Usage of Bayes' theorem
    1727. +
    1728. Doing it correctly
    1729. +
    1730. Bayes' Theorem and Ridge and Lasso Regression
    1731. +
    1732. Test Function for what happens with OLS, Ridge and Lasso
    1733. +
    1734. Invoking Bayes' theorem
    1735. +
    1736. Ridge and Bayes
    1737. +
    1738. Lasso and Bayes
    1739. +
    1740. Exercise 1: mean values and variances in linear regression
    1741. +
    1742. Exercise 2: Adding Ridge and Lasso Regression
    1743. @@ -297,71 +295,26 @@ MathJax.Hub.Config({

       

       

       

      -

      Inverse of Rectangular Matrix

      +

      Ridge and LASSO Regression

      -

      Although our matrix to invert \( \boldsymbol{X}^T\boldsymbol{X} \) is a square matrix, our matrix may be singular.

      - -

      The pseudoinverse is the generalization of the matrix inverse for square matrices to -rectangular matrices where the number of rows and columns are not equal. +

      Let us remind ourselves about the expression for the standard Mean Squared Error (MSE) which we used to define our cost function and the equations for the ordinary least squares (OLS) method, that is +our optimization problem is

      - -

      It is also called the the Moore-Penrose Inverse after two independent discoverers of the method or the Generalized Inverse. -It is used for the calculation of the inverse for singular or near singular matrices and for rectangular matrices. -

      - -

      Using the SVD we can obtain the pseudoinverse of a matrix \( \boldsymbol{A} \) (labeled here as \( \boldsymbol{A}_{\mathrm{PI}} \))

      $$ -\boldsymbol{A}_{\mathrm{PI}}= \boldsymbol{V}\boldsymbol{D}_{\mathrm{PI}}\boldsymbol{U}^T, +{\displaystyle \min_{\boldsymbol{\beta}\in {\mathbb{R}}^{p}}}\frac{1}{n}\left\{\left(\boldsymbol{y}-\boldsymbol{X}\boldsymbol{\beta}\right)^T\left(\boldsymbol{y}-\boldsymbol{X}\boldsymbol{\beta}\right)\right\}. $$ -

      where \( \boldsymbol{D}_{\mathrm{PI}} \) can be calculated by creating a diagonal matrix from \( \boldsymbol{\Sigma} \) where we only keep the singular values (the non-zero values). The following code computes the pseudoinvers of the matrix based on the SVD.

      +

      or we can state it as

      +$$ +{\displaystyle \min_{\boldsymbol{\beta}\in +{\mathbb{R}}^{p}}}\frac{1}{n}\sum_{i=0}^{n-1}\left(y_i-\tilde{y}_i\right)^2=\frac{1}{n}\vert\vert \boldsymbol{y}-\boldsymbol{X}\boldsymbol{\beta}\vert\vert_2^2, +$$ +

      where we have used the definition of a norm-2 vector, that is

      +$$ +\vert\vert \boldsymbol{x}\vert\vert_2 = \sqrt{\sum_i x_i^2}. +$$ - -
      -
      -
      -
      -
      -
      import numpy as np
      -# SVD inversion
      -def SVDinv(A):
      -    U, s, VT = np.linalg.svd(A)
      -    # reciprocals of singular values of s
      -    d = 1.0 / s
      -    # create m x n D matrix
      -    D = np.zeros(A.shape)
      -    # populate D with n x n diagonal matrix
      -    D[:A.shape[1], :A.shape[1]] = np.diag(d)
      -    UT = np.transpose(U)
      -    V = np.transpose(VT)
      -    return np.matmul(V,np.matmul(D.T,UT))
      -
      -
      -A = np.array([ [0.3, 0.4], [0.5, 0.6], [0.7, 0.8],[0.9, 1.0]])
      -print(A)
      -# Brute force inversion of super-collinear matrix
      -B = np.linalg.pinv(A)
      -print(B)
      -# Compare our own algorithm with pinv
      -C = SVDinv(A)
      -print(np.abs(C-B))
      -
      -
      -
      -
      -
      -
      -
      -
      -
      -
      -
      -
      -
      -
      - -

      As you can see from this example, our own decomposition based on the SVD agrees the pseudoinverse algorithm provided by Numpy.

      @@ -386,7 +339,7 @@ C = SVDinv(A)

    1744. 17
    1745. 18
    1746. ...
    1747. -
    1748. 60
    1749. +
    1750. 59
    1751. »
    1752. diff --git a/doc/pub/week36/html/._week36-bs009.html b/doc/pub/week36/html/._week36-bs009.html index 5ba117a00..561a90d58 100644 --- a/doc/pub/week36/html/._week36-bs009.html +++ b/doc/pub/week36/html/._week36-bs009.html @@ -37,7 +37,6 @@ doconce format html week36.do.txt --html_style=bootstrap --pygments_html_style=d
    1753. Plans for week 36
    1754. -
    1755. Thursday September 8
    1756. -
    1757. Summary from last Week and discussion of SVD, Ridge and Lasso regression with examples
    1758. -
    1759. Linear Regression and the SVD
    1760. -
    1761. What does it mean?
    1762. -
    1763. And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
    1764. -
    1765. Code for SVD and Inversion of Matrices
    1766. -
    1767. Inverse of Rectangular Matrix
    1768. -
    1769. Ridge and LASSO Regression
    1770. -
    1771. From OLS to Ridge and Lasso
    1772. -
    1773. Deriving the Ridge Regression Equations
    1774. -
    1775. Note on Scikit-Learn
    1776. -
    1777. Comparison with OLS
    1778. -
    1779. SVD analysis
    1780. -
    1781. Interpreting the Ridge results
    1782. -
    1783. More interpretations
    1784. -
    1785. Deriving the Lasso Regression Equations
    1786. -
    1787. Simple example to illustrate Ordinary Least Squares, Ridge and Lasso Regression
    1788. -
    1789. Ridge Regression
    1790. -
    1791. Lasso Regression
    1792. -
    1793. Yet another Example
    1794. -
    1795. The OLS case
    1796. -
    1797. The Ridge case
    1798. -
    1799. Writing the Cost Function
    1800. -
    1801. Lasso case
    1802. -
    1803. The first Case
    1804. -
    1805. Simple code for solving the above problem
    1806. -
    1807. With Lasso Regression
    1808. -
    1809. Another Example, now with a polynomial fit
    1810. -
    1811. To think about, first part
    1812. -
    1813. More thinking
    1814. -
    1815. Still thinking
    1816. -
    1817. What does centering (subtracting the mean values) mean mathematically?
    1818. -
    1819. Further Manipulations
    1820. -
    1821. Wrapping it up
    1822. -
    1823. Linear Regression code, Intercept handling first
    1824. -
    1825. Code Examples
    1826. -
    1827. Taking out the mean
    1828. -
    1829. Friday September 9
    1830. -
    1831. Linking the regression analysis with a statistical interpretation
    1832. -
    1833. Assumptions made
    1834. -
    1835. Expectation value and variance
    1836. -
    1837. Expectation value and variance for \( \boldsymbol{\beta} \)
    1838. -
    1839. Deriving OLS from a probability distribution
    1840. -
    1841. Independent and Identically Distrubuted (iid)
    1842. -
    1843. Maximum Likelihood Estimation (MLE)
    1844. -
    1845. A new Cost Function
    1846. -
    1847. More basic Statistics and Bayes' theorem
    1848. -
    1849. Marginal Probability
    1850. -
    1851. Conditional Probability
    1852. -
    1853. Bayes' Theorem
    1854. -
    1855. Interpretations of Bayes' Theorem
    1856. -
    1857. Example of Usage of Bayes' theorem
    1858. -
    1859. Doing it correctly
    1860. -
    1861. Bayes' Theorem and Ridge and Lasso Regression
    1862. -
    1863. Test Function for what happens with OLS, Ridge and Lasso
    1864. -
    1865. Invoking Bayes' theorem
    1866. -
    1867. Ridge and Bayes
    1868. -
    1869. Lasso and Bayes
    1870. -
    1871. Exercise 1: mean values and variances in linear regression
    1872. -
    1873. Exercise 2: Adding Ridge and Lasso Regression
    1874. +
    1875. Summary from last Week and discussion of SVD, Ridge and Lasso regression with examples
    1876. +
    1877. Linear Regression and the SVD
    1878. +
    1879. What does it mean?
    1880. +
    1881. And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
    1882. +
    1883. Code for SVD and Inversion of Matrices
    1884. +
    1885. Inverse of Rectangular Matrix
    1886. +
    1887. Ridge and LASSO Regression
    1888. +
    1889. From OLS to Ridge and Lasso
    1890. +
    1891. Deriving the Ridge Regression Equations
    1892. +
    1893. Note on Scikit-Learn
    1894. +
    1895. Comparison with OLS
    1896. +
    1897. SVD analysis
    1898. +
    1899. Interpreting the Ridge results
    1900. +
    1901. More interpretations
    1902. +
    1903. Deriving the Lasso Regression Equations
    1904. +
    1905. Simple example to illustrate Ordinary Least Squares, Ridge and Lasso Regression
    1906. +
    1907. Ridge Regression
    1908. +
    1909. Lasso Regression
    1910. +
    1911. Yet another Example
    1912. +
    1913. The OLS case
    1914. +
    1915. The Ridge case
    1916. +
    1917. Writing the Cost Function
    1918. +
    1919. Lasso case
    1920. +
    1921. The first Case
    1922. +
    1923. Simple code for solving the above problem
    1924. +
    1925. With Lasso Regression
    1926. +
    1927. Another Example, now with a polynomial fit
    1928. +
    1929. To think about, first part
    1930. +
    1931. More thinking
    1932. +
    1933. Still thinking
    1934. +
    1935. What does centering (subtracting the mean values) mean mathematically?
    1936. +
    1937. Further Manipulations
    1938. +
    1939. Wrapping it up
    1940. +
    1941. Linear Regression code, Intercept handling first
    1942. +
    1943. Code Examples
    1944. +
    1945. Taking out the mean
    1946. +
    1947. Friday September 9
    1948. +
    1949. Linking the regression analysis with a statistical interpretation
    1950. +
    1951. Assumptions made
    1952. +
    1953. Expectation value and variance
    1954. +
    1955. Expectation value and variance for \( \boldsymbol{\beta} \)
    1956. +
    1957. Deriving OLS from a probability distribution
    1958. +
    1959. Independent and Identically Distrubuted (iid)
    1960. +
    1961. Maximum Likelihood Estimation (MLE)
    1962. +
    1963. A new Cost Function
    1964. +
    1965. More basic Statistics and Bayes' theorem
    1966. +
    1967. Marginal Probability
    1968. +
    1969. Conditional Probability
    1970. +
    1971. Bayes' Theorem
    1972. +
    1973. Interpretations of Bayes' Theorem
    1974. +
    1975. Example of Usage of Bayes' theorem
    1976. +
    1977. Doing it correctly
    1978. +
    1979. Bayes' Theorem and Ridge and Lasso Regression
    1980. +
    1981. Test Function for what happens with OLS, Ridge and Lasso
    1982. +
    1983. Invoking Bayes' theorem
    1984. +
    1985. Ridge and Bayes
    1986. +
    1987. Lasso and Bayes
    1988. +
    1989. Exercise 1: mean values and variances in linear regression
    1990. +
    1991. Exercise 2: Adding Ridge and Lasso Regression
    1992. @@ -297,24 +295,39 @@ MathJax.Hub.Config({

       

       

       

      -

      Ridge and LASSO Regression

      +

      From OLS to Ridge and Lasso

      -

      Let us remind ourselves about the expression for the standard Mean Squared Error (MSE) which we used to define our cost function and the equations for the ordinary least squares (OLS) method, that is -our optimization problem is +

      By minimizing the above equation with respect to the parameters +\( \boldsymbol{\beta} \) we could then obtain an analytical expression for the +parameters \( \boldsymbol{\beta} \). We can add a regularization parameter \( \lambda \) by +defining a new cost function to be optimized, that is

      -$$ -{\displaystyle \min_{\boldsymbol{\beta}\in {\mathbb{R}}^{p}}}\frac{1}{n}\left\{\left(\boldsymbol{y}-\boldsymbol{X}\boldsymbol{\beta}\right)^T\left(\boldsymbol{y}-\boldsymbol{X}\boldsymbol{\beta}\right)\right\}. -$$ -

      or we can state it as

      $$ {\displaystyle \min_{\boldsymbol{\beta}\in -{\mathbb{R}}^{p}}}\frac{1}{n}\sum_{i=0}^{n-1}\left(y_i-\tilde{y}_i\right)^2=\frac{1}{n}\vert\vert \boldsymbol{y}-\boldsymbol{X}\boldsymbol{\beta}\vert\vert_2^2, +{\mathbb{R}}^{p}}}\frac{1}{n}\vert\vert \boldsymbol{y}-\boldsymbol{X}\boldsymbol{\beta}\vert\vert_2^2+\lambda\vert\vert \boldsymbol{\beta}\vert\vert_2^2 $$ -

      where we have used the definition of a norm-2 vector, that is

      +

      which leads to the Ridge regression minimization problem where we +require that \( \vert\vert \boldsymbol{\beta}\vert\vert_2^2\le t \), where \( t \) is +a finite number larger than zero. By defining +

      + $$ -\vert\vert \boldsymbol{x}\vert\vert_2 = \sqrt{\sum_i x_i^2}. +C(\boldsymbol{X},\boldsymbol{\beta})=\frac{1}{n}\vert\vert \boldsymbol{y}-\boldsymbol{X}\boldsymbol{\beta}\vert\vert_2^2+\lambda\vert\vert \boldsymbol{\beta}\vert\vert_1, +$$ + +

      we have a new optimization equation

      +$$ +{\displaystyle \min_{\boldsymbol{\beta}\in +{\mathbb{R}}^{p}}}\frac{1}{n}\vert\vert \boldsymbol{y}-\boldsymbol{X}\boldsymbol{\beta}\vert\vert_2^2+\lambda\vert\vert \boldsymbol{\beta}\vert\vert_1 +$$ + +

      which leads to Lasso regression. Lasso stands for least absolute shrinkage and selection operator.

      + +

      Here we have defined the norm-1 as

      +$$ +\vert\vert \boldsymbol{x}\vert\vert_1 = \sum_i \vert x_i\vert. $$ @@ -342,7 +355,7 @@ $$
    1993. 18
    1994. 19
    1995. ...
    1996. -
    1997. 60
    1998. +
    1999. 59
    2000. »
    2001. diff --git a/doc/pub/week36/html/._week36-bs010.html b/doc/pub/week36/html/._week36-bs010.html index 07b8419a6..7e6571fdf 100644 --- a/doc/pub/week36/html/._week36-bs010.html +++ b/doc/pub/week36/html/._week36-bs010.html @@ -37,7 +37,6 @@ doconce format html week36.do.txt --html_style=bootstrap --pygments_html_style=d
    2002. Plans for week 36
    2003. -
    2004. Thursday September 8
    2005. -
    2006. Summary from last Week and discussion of SVD, Ridge and Lasso regression with examples
    2007. -
    2008. Linear Regression and the SVD
    2009. -
    2010. What does it mean?
    2011. -
    2012. And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
    2013. -
    2014. Code for SVD and Inversion of Matrices
    2015. -
    2016. Inverse of Rectangular Matrix
    2017. -
    2018. Ridge and LASSO Regression
    2019. -
    2020. From OLS to Ridge and Lasso
    2021. -
    2022. Deriving the Ridge Regression Equations
    2023. -
    2024. Note on Scikit-Learn
    2025. -
    2026. Comparison with OLS
    2027. -
    2028. SVD analysis
    2029. -
    2030. Interpreting the Ridge results
    2031. -
    2032. More interpretations
    2033. -
    2034. Deriving the Lasso Regression Equations
    2035. -
    2036. Simple example to illustrate Ordinary Least Squares, Ridge and Lasso Regression
    2037. -
    2038. Ridge Regression
    2039. -
    2040. Lasso Regression
    2041. -
    2042. Yet another Example
    2043. -
    2044. The OLS case
    2045. -
    2046. The Ridge case
    2047. -
    2048. Writing the Cost Function
    2049. -
    2050. Lasso case
    2051. -
    2052. The first Case
    2053. -
    2054. Simple code for solving the above problem
    2055. -
    2056. With Lasso Regression
    2057. -
    2058. Another Example, now with a polynomial fit
    2059. -
    2060. To think about, first part
    2061. -
    2062. More thinking
    2063. -
    2064. Still thinking
    2065. -
    2066. What does centering (subtracting the mean values) mean mathematically?
    2067. -
    2068. Further Manipulations
    2069. -
    2070. Wrapping it up
    2071. -
    2072. Linear Regression code, Intercept handling first
    2073. -
    2074. Code Examples
    2075. -
    2076. Taking out the mean
    2077. -
    2078. Friday September 9
    2079. -
    2080. Linking the regression analysis with a statistical interpretation
    2081. -
    2082. Assumptions made
    2083. -
    2084. Expectation value and variance
    2085. -
    2086. Expectation value and variance for \( \boldsymbol{\beta} \)
    2087. -
    2088. Deriving OLS from a probability distribution
    2089. -
    2090. Independent and Identically Distrubuted (iid)
    2091. -
    2092. Maximum Likelihood Estimation (MLE)
    2093. -
    2094. A new Cost Function
    2095. -
    2096. More basic Statistics and Bayes' theorem
    2097. -
    2098. Marginal Probability
    2099. -
    2100. Conditional Probability
    2101. -
    2102. Bayes' Theorem
    2103. -
    2104. Interpretations of Bayes' Theorem
    2105. -
    2106. Example of Usage of Bayes' theorem
    2107. -
    2108. Doing it correctly
    2109. -
    2110. Bayes' Theorem and Ridge and Lasso Regression
    2111. -
    2112. Test Function for what happens with OLS, Ridge and Lasso
    2113. -
    2114. Invoking Bayes' theorem
    2115. -
    2116. Ridge and Bayes
    2117. -
    2118. Lasso and Bayes
    2119. -
    2120. Exercise 1: mean values and variances in linear regression
    2121. -
    2122. Exercise 2: Adding Ridge and Lasso Regression
    2123. +
    2124. Summary from last Week and discussion of SVD, Ridge and Lasso regression with examples
    2125. +
    2126. Linear Regression and the SVD
    2127. +
    2128. What does it mean?
    2129. +
    2130. And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
    2131. +
    2132. Code for SVD and Inversion of Matrices
    2133. +
    2134. Inverse of Rectangular Matrix
    2135. +
    2136. Ridge and LASSO Regression
    2137. +
    2138. From OLS to Ridge and Lasso
    2139. +
    2140. Deriving the Ridge Regression Equations
    2141. +
    2142. Note on Scikit-Learn
    2143. +
    2144. Comparison with OLS
    2145. +
    2146. SVD analysis
    2147. +
    2148. Interpreting the Ridge results
    2149. +
    2150. More interpretations
    2151. +
    2152. Deriving the Lasso Regression Equations
    2153. +
    2154. Simple example to illustrate Ordinary Least Squares, Ridge and Lasso Regression
    2155. +
    2156. Ridge Regression
    2157. +
    2158. Lasso Regression
    2159. +
    2160. Yet another Example
    2161. +
    2162. The OLS case
    2163. +
    2164. The Ridge case
    2165. +
    2166. Writing the Cost Function
    2167. +
    2168. Lasso case
    2169. +
    2170. The first Case
    2171. +
    2172. Simple code for solving the above problem
    2173. +
    2174. With Lasso Regression
    2175. +
    2176. Another Example, now with a polynomial fit
    2177. +
    2178. To think about, first part
    2179. +
    2180. More thinking
    2181. +
    2182. Still thinking
    2183. +
    2184. What does centering (subtracting the mean values) mean mathematically?
    2185. +
    2186. Further Manipulations
    2187. +
    2188. Wrapping it up
    2189. +
    2190. Linear Regression code, Intercept handling first
    2191. +
    2192. Code Examples
    2193. +
    2194. Taking out the mean
    2195. +
    2196. Friday September 9
    2197. +
    2198. Linking the regression analysis with a statistical interpretation
    2199. +
    2200. Assumptions made
    2201. +
    2202. Expectation value and variance
    2203. +
    2204. Expectation value and variance for \( \boldsymbol{\beta} \)
    2205. +
    2206. Deriving OLS from a probability distribution
    2207. +
    2208. Independent and Identically Distrubuted (iid)
    2209. +
    2210. Maximum Likelihood Estimation (MLE)
    2211. +
    2212. A new Cost Function
    2213. +
    2214. More basic Statistics and Bayes' theorem
    2215. +
    2216. Marginal Probability
    2217. +
    2218. Conditional Probability
    2219. +
    2220. Bayes' Theorem
    2221. +
    2222. Interpretations of Bayes' Theorem
    2223. +
    2224. Example of Usage of Bayes' theorem
    2225. +
    2226. Doing it correctly
    2227. +
    2228. Bayes' Theorem and Ridge and Lasso Regression
    2229. +
    2230. Test Function for what happens with OLS, Ridge and Lasso
    2231. +
    2232. Invoking Bayes' theorem
    2233. +
    2234. Ridge and Bayes
    2235. +
    2236. Lasso and Bayes
    2237. +
    2238. Exercise 1: mean values and variances in linear regression
    2239. +
    2240. Exercise 2: Adding Ridge and Lasso Regression
    2241. @@ -297,41 +295,31 @@ MathJax.Hub.Config({

       

       

       

      -

      From OLS to Ridge and Lasso

      +

      Deriving the Ridge Regression Equations

      -

      By minimizing the above equation with respect to the parameters -\( \boldsymbol{\beta} \) we could then obtain an analytical expression for the -parameters \( \boldsymbol{\beta} \). We can add a regularization parameter \( \lambda \) by -defining a new cost function to be optimized, that is +

      Using the matrix-vector expression for Ridge regression and dropping the parameter \( 1/n \) in front of the standard means squared error equation, we have

      + +$$ +C(\boldsymbol{X},\boldsymbol{\beta})=\left\{(\boldsymbol{y}-\boldsymbol{X}\boldsymbol{\beta})^T(\boldsymbol{y}-\boldsymbol{X}\boldsymbol{\beta})\right\}+\lambda\boldsymbol{\beta}^T\boldsymbol{\beta}, +$$ + +

      and +taking the derivatives with respect to \( \boldsymbol{\beta} \) we obtain then +a slightly modified matrix inversion problem which for finite values +of \( \lambda \) does not suffer from singularity problems. We obtain +the optimal parameters

      - $$ -{\displaystyle \min_{\boldsymbol{\beta}\in -{\mathbb{R}}^{p}}}\frac{1}{n}\vert\vert \boldsymbol{y}-\boldsymbol{X}\boldsymbol{\beta}\vert\vert_2^2+\lambda\vert\vert \boldsymbol{\beta}\vert\vert_2^2 +\hat{\boldsymbol{\beta}}_{\mathrm{Ridge}} = \left(\boldsymbol{X}^T\boldsymbol{X}+\lambda\boldsymbol{I}\right)^{-1}\boldsymbol{X}^T\boldsymbol{y}, $$ -

      which leads to the Ridge regression minimization problem where we -require that \( \vert\vert \boldsymbol{\beta}\vert\vert_2^2\le t \), where \( t \) is -a finite number larger than zero. By defining -

      +

      with \( \boldsymbol{I} \) being a \( p\times p \) identity matrix with the constraint that

      $$ -C(\boldsymbol{X},\boldsymbol{\beta})=\frac{1}{n}\vert\vert \boldsymbol{y}-\boldsymbol{X}\boldsymbol{\beta}\vert\vert_2^2+\lambda\vert\vert \boldsymbol{\beta}\vert\vert_1, -$$ - -

      we have a new optimization equation

      -$$ -{\displaystyle \min_{\boldsymbol{\beta}\in -{\mathbb{R}}^{p}}}\frac{1}{n}\vert\vert \boldsymbol{y}-\boldsymbol{X}\boldsymbol{\beta}\vert\vert_2^2+\lambda\vert\vert \boldsymbol{\beta}\vert\vert_1 -$$ - -

      which leads to Lasso regression. Lasso stands for least absolute shrinkage and selection operator.

      - -

      Here we have defined the norm-1 as

      -$$ -\vert\vert \boldsymbol{x}\vert\vert_1 = \sum_i \vert x_i\vert. +\sum_{i=0}^{p-1} \beta_i^2 \leq t, $$ +

      with \( t \) a finite positive number.

      @@ -358,7 +346,7 @@ $$

    2242. 19
    2243. 20
    2244. ...
    2245. -
    2246. 60
    2247. +
    2248. 59
    2249. »
    2250. diff --git a/doc/pub/week36/html/._week36-bs011.html b/doc/pub/week36/html/._week36-bs011.html index 7e97353b7..899897538 100644 --- a/doc/pub/week36/html/._week36-bs011.html +++ b/doc/pub/week36/html/._week36-bs011.html @@ -37,7 +37,6 @@ doconce format html week36.do.txt --html_style=bootstrap --pygments_html_style=d
    2251. Plans for week 36
    2252. -
    2253. Thursday September 8
    2254. -
    2255. Summary from last Week and discussion of SVD, Ridge and Lasso regression with examples
    2256. -
    2257. Linear Regression and the SVD
    2258. -
    2259. What does it mean?
    2260. -
    2261. And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
    2262. -
    2263. Code for SVD and Inversion of Matrices
    2264. -
    2265. Inverse of Rectangular Matrix
    2266. -
    2267. Ridge and LASSO Regression
    2268. -
    2269. From OLS to Ridge and Lasso
    2270. -
    2271. Deriving the Ridge Regression Equations
    2272. -
    2273. Note on Scikit-Learn
    2274. -
    2275. Comparison with OLS
    2276. -
    2277. SVD analysis
    2278. -
    2279. Interpreting the Ridge results
    2280. -
    2281. More interpretations
    2282. -
    2283. Deriving the Lasso Regression Equations
    2284. -
    2285. Simple example to illustrate Ordinary Least Squares, Ridge and Lasso Regression
    2286. -
    2287. Ridge Regression
    2288. -
    2289. Lasso Regression
    2290. -
    2291. Yet another Example
    2292. -
    2293. The OLS case
    2294. -
    2295. The Ridge case
    2296. -
    2297. Writing the Cost Function
    2298. -
    2299. Lasso case
    2300. -
    2301. The first Case
    2302. -
    2303. Simple code for solving the above problem
    2304. -
    2305. With Lasso Regression
    2306. -
    2307. Another Example, now with a polynomial fit
    2308. -
    2309. To think about, first part
    2310. -
    2311. More thinking
    2312. -
    2313. Still thinking
    2314. -
    2315. What does centering (subtracting the mean values) mean mathematically?
    2316. -
    2317. Further Manipulations
    2318. -
    2319. Wrapping it up
    2320. -
    2321. Linear Regression code, Intercept handling first
    2322. -
    2323. Code Examples
    2324. -
    2325. Taking out the mean
    2326. -
    2327. Friday September 9
    2328. -
    2329. Linking the regression analysis with a statistical interpretation
    2330. -
    2331. Assumptions made
    2332. -
    2333. Expectation value and variance
    2334. -
    2335. Expectation value and variance for \( \boldsymbol{\beta} \)
    2336. -
    2337. Deriving OLS from a probability distribution
    2338. -
    2339. Independent and Identically Distrubuted (iid)
    2340. -
    2341. Maximum Likelihood Estimation (MLE)
    2342. -
    2343. A new Cost Function
    2344. -
    2345. More basic Statistics and Bayes' theorem
    2346. -
    2347. Marginal Probability
    2348. -
    2349. Conditional Probability
    2350. -
    2351. Bayes' Theorem
    2352. -
    2353. Interpretations of Bayes' Theorem
    2354. -
    2355. Example of Usage of Bayes' theorem
    2356. -
    2357. Doing it correctly
    2358. -
    2359. Bayes' Theorem and Ridge and Lasso Regression
    2360. -
    2361. Test Function for what happens with OLS, Ridge and Lasso
    2362. -
    2363. Invoking Bayes' theorem
    2364. -
    2365. Ridge and Bayes
    2366. -
    2367. Lasso and Bayes
    2368. -
    2369. Exercise 1: mean values and variances in linear regression
    2370. -
    2371. Exercise 2: Adding Ridge and Lasso Regression
    2372. +
    2373. Summary from last Week and discussion of SVD, Ridge and Lasso regression with examples
    2374. +
    2375. Linear Regression and the SVD
    2376. +
    2377. What does it mean?
    2378. +
    2379. And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
    2380. +
    2381. Code for SVD and Inversion of Matrices
    2382. +
    2383. Inverse of Rectangular Matrix
    2384. +
    2385. Ridge and LASSO Regression
    2386. +
    2387. From OLS to Ridge and Lasso
    2388. +
    2389. Deriving the Ridge Regression Equations
    2390. +
    2391. Note on Scikit-Learn
    2392. +
    2393. Comparison with OLS
    2394. +
    2395. SVD analysis
    2396. +
    2397. Interpreting the Ridge results
    2398. +
    2399. More interpretations
    2400. +
    2401. Deriving the Lasso Regression Equations
    2402. +
    2403. Simple example to illustrate Ordinary Least Squares, Ridge and Lasso Regression
    2404. +
    2405. Ridge Regression
    2406. +
    2407. Lasso Regression
    2408. +
    2409. Yet another Example
    2410. +
    2411. The OLS case
    2412. +
    2413. The Ridge case
    2414. +
    2415. Writing the Cost Function
    2416. +
    2417. Lasso case
    2418. +
    2419. The first Case
    2420. +
    2421. Simple code for solving the above problem
    2422. +
    2423. With Lasso Regression
    2424. +
    2425. Another Example, now with a polynomial fit
    2426. +
    2427. To think about, first part
    2428. +
    2429. More thinking
    2430. +
    2431. Still thinking
    2432. +
    2433. What does centering (subtracting the mean values) mean mathematically?
    2434. +
    2435. Further Manipulations
    2436. +
    2437. Wrapping it up
    2438. +
    2439. Linear Regression code, Intercept handling first
    2440. +
    2441. Code Examples
    2442. +
    2443. Taking out the mean
    2444. +
    2445. Friday September 9
    2446. +
    2447. Linking the regression analysis with a statistical interpretation
    2448. +
    2449. Assumptions made
    2450. +
    2451. Expectation value and variance
    2452. +
    2453. Expectation value and variance for \( \boldsymbol{\beta} \)
    2454. +
    2455. Deriving OLS from a probability distribution
    2456. +
    2457. Independent and Identically Distrubuted (iid)
    2458. +
    2459. Maximum Likelihood Estimation (MLE)
    2460. +
    2461. A new Cost Function
    2462. +
    2463. More basic Statistics and Bayes' theorem
    2464. +
    2465. Marginal Probability
    2466. +
    2467. Conditional Probability
    2468. +
    2469. Bayes' Theorem
    2470. +
    2471. Interpretations of Bayes' Theorem
    2472. +
    2473. Example of Usage of Bayes' theorem
    2474. +
    2475. Doing it correctly
    2476. +
    2477. Bayes' Theorem and Ridge and Lasso Regression
    2478. +
    2479. Test Function for what happens with OLS, Ridge and Lasso
    2480. +
    2481. Invoking Bayes' theorem
    2482. +
    2483. Ridge and Bayes
    2484. +
    2485. Lasso and Bayes
    2486. +
    2487. Exercise 1: mean values and variances in linear regression
    2488. +
    2489. Exercise 2: Adding Ridge and Lasso Regression
    2490. @@ -297,31 +295,15 @@ MathJax.Hub.Config({

       

       

       

      -

      Deriving the Ridge Regression Equations

      +

      Note on Scikit-Learn

      -

      Using the matrix-vector expression for Ridge regression and dropping the parameter \( 1/n \) in front of the standard means squared error equation, we have

      +

      Note well that a library like Scikit-Learn does not include the \( 1/n \) factor in the expression for the mean-squared error. If you include it, the optimal parameter \( \beta \) becomes

      $$ -C(\boldsymbol{X},\boldsymbol{\beta})=\left\{(\boldsymbol{y}-\boldsymbol{X}\boldsymbol{\beta})^T(\boldsymbol{y}-\boldsymbol{X}\boldsymbol{\beta})\right\}+\lambda\boldsymbol{\beta}^T\boldsymbol{\beta}, +\hat{\boldsymbol{\beta}}_{\mathrm{Ridge}} = \left(\boldsymbol{X}^T\boldsymbol{X}+n\lambda\boldsymbol{I}\right)^{-1}\boldsymbol{X}^T\boldsymbol{y}. $$ -

      and -taking the derivatives with respect to \( \boldsymbol{\beta} \) we obtain then -a slightly modified matrix inversion problem which for finite values -of \( \lambda \) does not suffer from singularity problems. We obtain -the optimal parameters -

      -$$ -\hat{\boldsymbol{\beta}}_{\mathrm{Ridge}} = \left(\boldsymbol{X}^T\boldsymbol{X}+\lambda\boldsymbol{I}\right)^{-1}\boldsymbol{X}^T\boldsymbol{y}, -$$ - -

      with \( \boldsymbol{I} \) being a \( p\times p \) identity matrix with the constraint that

      - -$$ -\sum_{i=0}^{p-1} \beta_i^2 \leq t, -$$ - -

      with \( t \) a finite positive number.

      +

      In our codes where we compare our own codes with Scikit-Learn, we do thus not include the \( 1/n \) factor in the cost function.

      @@ -348,7 +330,7 @@ $$

    2491. 20
    2492. 21
    2493. ...
    2494. -
    2495. 60
    2496. +
    2497. 59
    2498. »
    2499. diff --git a/doc/pub/week36/html/._week36-bs012.html b/doc/pub/week36/html/._week36-bs012.html index 3698108e4..2b98d7995 100644 --- a/doc/pub/week36/html/._week36-bs012.html +++ b/doc/pub/week36/html/._week36-bs012.html @@ -37,7 +37,6 @@ doconce format html week36.do.txt --html_style=bootstrap --pygments_html_style=d
    2500. Plans for week 36
    2501. -
    2502. Thursday September 8
    2503. -
    2504. Summary from last Week and discussion of SVD, Ridge and Lasso regression with examples
    2505. -
    2506. Linear Regression and the SVD
    2507. -
    2508. What does it mean?
    2509. -
    2510. And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
    2511. -
    2512. Code for SVD and Inversion of Matrices
    2513. -
    2514. Inverse of Rectangular Matrix
    2515. -
    2516. Ridge and LASSO Regression
    2517. -
    2518. From OLS to Ridge and Lasso
    2519. -
    2520. Deriving the Ridge Regression Equations
    2521. -
    2522. Note on Scikit-Learn
    2523. -
    2524. Comparison with OLS
    2525. -
    2526. SVD analysis
    2527. -
    2528. Interpreting the Ridge results
    2529. -
    2530. More interpretations
    2531. -
    2532. Deriving the Lasso Regression Equations
    2533. -
    2534. Simple example to illustrate Ordinary Least Squares, Ridge and Lasso Regression
    2535. -
    2536. Ridge Regression
    2537. -
    2538. Lasso Regression
    2539. -
    2540. Yet another Example
    2541. -
    2542. The OLS case
    2543. -
    2544. The Ridge case
    2545. -
    2546. Writing the Cost Function
    2547. -
    2548. Lasso case
    2549. -
    2550. The first Case
    2551. -
    2552. Simple code for solving the above problem
    2553. -
    2554. With Lasso Regression
    2555. -
    2556. Another Example, now with a polynomial fit
    2557. -
    2558. To think about, first part
    2559. -
    2560. More thinking
    2561. -
    2562. Still thinking
    2563. -
    2564. What does centering (subtracting the mean values) mean mathematically?
    2565. -
    2566. Further Manipulations
    2567. -
    2568. Wrapping it up
    2569. -
    2570. Linear Regression code, Intercept handling first
    2571. -
    2572. Code Examples
    2573. -
    2574. Taking out the mean
    2575. -
    2576. Friday September 9
    2577. -
    2578. Linking the regression analysis with a statistical interpretation
    2579. -
    2580. Assumptions made
    2581. -
    2582. Expectation value and variance
    2583. -
    2584. Expectation value and variance for \( \boldsymbol{\beta} \)
    2585. -
    2586. Deriving OLS from a probability distribution
    2587. -
    2588. Independent and Identically Distrubuted (iid)
    2589. -
    2590. Maximum Likelihood Estimation (MLE)
    2591. -
    2592. A new Cost Function
    2593. -
    2594. More basic Statistics and Bayes' theorem
    2595. -
    2596. Marginal Probability
    2597. -
    2598. Conditional Probability
    2599. -
    2600. Bayes' Theorem
    2601. -
    2602. Interpretations of Bayes' Theorem
    2603. -
    2604. Example of Usage of Bayes' theorem
    2605. -
    2606. Doing it correctly
    2607. -
    2608. Bayes' Theorem and Ridge and Lasso Regression
    2609. -
    2610. Test Function for what happens with OLS, Ridge and Lasso
    2611. -
    2612. Invoking Bayes' theorem
    2613. -
    2614. Ridge and Bayes
    2615. -
    2616. Lasso and Bayes
    2617. -
    2618. Exercise 1: mean values and variances in linear regression
    2619. -
    2620. Exercise 2: Adding Ridge and Lasso Regression
    2621. +
    2622. Summary from last Week and discussion of SVD, Ridge and Lasso regression with examples
    2623. +
    2624. Linear Regression and the SVD
    2625. +
    2626. What does it mean?
    2627. +
    2628. And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
    2629. +
    2630. Code for SVD and Inversion of Matrices
    2631. +
    2632. Inverse of Rectangular Matrix
    2633. +
    2634. Ridge and LASSO Regression
    2635. +
    2636. From OLS to Ridge and Lasso
    2637. +
    2638. Deriving the Ridge Regression Equations
    2639. +
    2640. Note on Scikit-Learn
    2641. +
    2642. Comparison with OLS
    2643. +
    2644. SVD analysis
    2645. +
    2646. Interpreting the Ridge results
    2647. +
    2648. More interpretations
    2649. +
    2650. Deriving the Lasso Regression Equations
    2651. +
    2652. Simple example to illustrate Ordinary Least Squares, Ridge and Lasso Regression
    2653. +
    2654. Ridge Regression
    2655. +
    2656. Lasso Regression
    2657. +
    2658. Yet another Example
    2659. +
    2660. The OLS case
    2661. +
    2662. The Ridge case
    2663. +
    2664. Writing the Cost Function
    2665. +
    2666. Lasso case
    2667. +
    2668. The first Case
    2669. +
    2670. Simple code for solving the above problem
    2671. +
    2672. With Lasso Regression
    2673. +
    2674. Another Example, now with a polynomial fit
    2675. +
    2676. To think about, first part
    2677. +
    2678. More thinking
    2679. +
    2680. Still thinking
    2681. +
    2682. What does centering (subtracting the mean values) mean mathematically?
    2683. +
    2684. Further Manipulations
    2685. +
    2686. Wrapping it up
    2687. +
    2688. Linear Regression code, Intercept handling first
    2689. +
    2690. Code Examples
    2691. +
    2692. Taking out the mean
    2693. +
    2694. Friday September 9
    2695. +
    2696. Linking the regression analysis with a statistical interpretation
    2697. +
    2698. Assumptions made
    2699. +
    2700. Expectation value and variance
    2701. +
    2702. Expectation value and variance for \( \boldsymbol{\beta} \)
    2703. +
    2704. Deriving OLS from a probability distribution
    2705. +
    2706. Independent and Identically Distrubuted (iid)
    2707. +
    2708. Maximum Likelihood Estimation (MLE)
    2709. +
    2710. A new Cost Function
    2711. +
    2712. More basic Statistics and Bayes' theorem
    2713. +
    2714. Marginal Probability
    2715. +
    2716. Conditional Probability
    2717. +
    2718. Bayes' Theorem
    2719. +
    2720. Interpretations of Bayes' Theorem
    2721. +
    2722. Example of Usage of Bayes' theorem
    2723. +
    2724. Doing it correctly
    2725. +
    2726. Bayes' Theorem and Ridge and Lasso Regression
    2727. +
    2728. Test Function for what happens with OLS, Ridge and Lasso
    2729. +
    2730. Invoking Bayes' theorem
    2731. +
    2732. Ridge and Bayes
    2733. +
    2734. Lasso and Bayes
    2735. +
    2736. Exercise 1: mean values and variances in linear regression
    2737. +
    2738. Exercise 2: Adding Ridge and Lasso Regression
    2739. @@ -297,15 +295,20 @@ MathJax.Hub.Config({

       

       

       

      -

      Note on Scikit-Learn

      - -

      Note well that a library like Scikit-Learn does not include the \( 1/n \) factor in the expression for the mean-squared error. If you include it, the optimal parameter \( \beta \) becomes

      - +

      Comparison with OLS

      +

      When we compare this with the ordinary least squares result we have

      $$ -\hat{\boldsymbol{\beta}}_{\mathrm{Ridge}} = \left(\boldsymbol{X}^T\boldsymbol{X}+n\lambda\boldsymbol{I}\right)^{-1}\boldsymbol{X}^T\boldsymbol{y}. +\hat{\boldsymbol{\beta}}_{\mathrm{OLS}} = \left(\boldsymbol{X}^T\boldsymbol{X}\right)^{-1}\boldsymbol{X}^T\boldsymbol{y}, $$ -

      In our codes where we compare our own codes with Scikit-Learn, we do thus not include the \( 1/n \) factor in the cost function.

      +

      which can lead to singular matrices. However, with the SVD, we can always compute the inverse of the matrix \( \boldsymbol{X}^T\boldsymbol{X} \).

      + +

      We see that Ridge regression is nothing but the standard OLS with a +modified diagonal term added to \( \boldsymbol{X}^T\boldsymbol{X} \). The consequences, in +particular for our discussion of the bias-variance tradeoff are rather +interesting. We will see that for specific values of \( \lambda \), we may +even reduce the variance of the optimal parameters \( \boldsymbol{\beta} \). These topics and other related ones, will be discussed after the more linear algebra oriented analysis here. +

      @@ -332,7 +335,7 @@ $$

    2740. 21
    2741. 22
    2742. ...
    2743. -
    2744. 60
    2745. +
    2746. 59
    2747. »
    2748. diff --git a/doc/pub/week36/html/._week36-bs013.html b/doc/pub/week36/html/._week36-bs013.html index e97a461b5..66d60dfed 100644 --- a/doc/pub/week36/html/._week36-bs013.html +++ b/doc/pub/week36/html/._week36-bs013.html @@ -37,7 +37,6 @@ doconce format html week36.do.txt --html_style=bootstrap --pygments_html_style=d
    2749. Plans for week 36
    2750. -
    2751. Thursday September 8
    2752. -
    2753. Summary from last Week and discussion of SVD, Ridge and Lasso regression with examples
    2754. -
    2755. Linear Regression and the SVD
    2756. -
    2757. What does it mean?
    2758. -
    2759. And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
    2760. -
    2761. Code for SVD and Inversion of Matrices
    2762. -
    2763. Inverse of Rectangular Matrix
    2764. -
    2765. Ridge and LASSO Regression
    2766. -
    2767. From OLS to Ridge and Lasso
    2768. -
    2769. Deriving the Ridge Regression Equations
    2770. -
    2771. Note on Scikit-Learn
    2772. -
    2773. Comparison with OLS
    2774. -
    2775. SVD analysis
    2776. -
    2777. Interpreting the Ridge results
    2778. -
    2779. More interpretations
    2780. -
    2781. Deriving the Lasso Regression Equations
    2782. -
    2783. Simple example to illustrate Ordinary Least Squares, Ridge and Lasso Regression
    2784. -
    2785. Ridge Regression
    2786. -
    2787. Lasso Regression
    2788. -
    2789. Yet another Example
    2790. -
    2791. The OLS case
    2792. -
    2793. The Ridge case
    2794. -
    2795. Writing the Cost Function
    2796. -
    2797. Lasso case
    2798. -
    2799. The first Case
    2800. -
    2801. Simple code for solving the above problem
    2802. -
    2803. With Lasso Regression
    2804. -
    2805. Another Example, now with a polynomial fit
    2806. -
    2807. To think about, first part
    2808. -
    2809. More thinking
    2810. -
    2811. Still thinking
    2812. -
    2813. What does centering (subtracting the mean values) mean mathematically?
    2814. -
    2815. Further Manipulations
    2816. -
    2817. Wrapping it up
    2818. -
    2819. Linear Regression code, Intercept handling first
    2820. -
    2821. Code Examples
    2822. -
    2823. Taking out the mean
    2824. -
    2825. Friday September 9
    2826. -
    2827. Linking the regression analysis with a statistical interpretation
    2828. -
    2829. Assumptions made
    2830. -
    2831. Expectation value and variance
    2832. -
    2833. Expectation value and variance for \( \boldsymbol{\beta} \)
    2834. -
    2835. Deriving OLS from a probability distribution
    2836. -
    2837. Independent and Identically Distrubuted (iid)
    2838. -
    2839. Maximum Likelihood Estimation (MLE)
    2840. -
    2841. A new Cost Function
    2842. -
    2843. More basic Statistics and Bayes' theorem
    2844. -
    2845. Marginal Probability
    2846. -
    2847. Conditional Probability
    2848. -
    2849. Bayes' Theorem
    2850. -
    2851. Interpretations of Bayes' Theorem
    2852. -
    2853. Example of Usage of Bayes' theorem
    2854. -
    2855. Doing it correctly
    2856. -
    2857. Bayes' Theorem and Ridge and Lasso Regression
    2858. -
    2859. Test Function for what happens with OLS, Ridge and Lasso
    2860. -
    2861. Invoking Bayes' theorem
    2862. -
    2863. Ridge and Bayes
    2864. -
    2865. Lasso and Bayes
    2866. -
    2867. Exercise 1: mean values and variances in linear regression
    2868. -
    2869. Exercise 2: Adding Ridge and Lasso Regression
    2870. +
    2871. Summary from last Week and discussion of SVD, Ridge and Lasso regression with examples
    2872. +
    2873. Linear Regression and the SVD
    2874. +
    2875. What does it mean?
    2876. +
    2877. And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
    2878. +
    2879. Code for SVD and Inversion of Matrices
    2880. +
    2881. Inverse of Rectangular Matrix
    2882. +
    2883. Ridge and LASSO Regression
    2884. +
    2885. From OLS to Ridge and Lasso
    2886. +
    2887. Deriving the Ridge Regression Equations
    2888. +
    2889. Note on Scikit-Learn
    2890. +
    2891. Comparison with OLS
    2892. +
    2893. SVD analysis
    2894. +
    2895. Interpreting the Ridge results
    2896. +
    2897. More interpretations
    2898. +
    2899. Deriving the Lasso Regression Equations
    2900. +
    2901. Simple example to illustrate Ordinary Least Squares, Ridge and Lasso Regression
    2902. +
    2903. Ridge Regression
    2904. +
    2905. Lasso Regression
    2906. +
    2907. Yet another Example
    2908. +
    2909. The OLS case
    2910. +
    2911. The Ridge case
    2912. +
    2913. Writing the Cost Function
    2914. +
    2915. Lasso case
    2916. +
    2917. The first Case
    2918. +
    2919. Simple code for solving the above problem
    2920. +
    2921. With Lasso Regression
    2922. +
    2923. Another Example, now with a polynomial fit
    2924. +
    2925. To think about, first part
    2926. +
    2927. More thinking
    2928. +
    2929. Still thinking
    2930. +
    2931. What does centering (subtracting the mean values) mean mathematically?
    2932. +
    2933. Further Manipulations
    2934. +
    2935. Wrapping it up
    2936. +
    2937. Linear Regression code, Intercept handling first
    2938. +
    2939. Code Examples
    2940. +
    2941. Taking out the mean
    2942. +
    2943. Friday September 9
    2944. +
    2945. Linking the regression analysis with a statistical interpretation
    2946. +
    2947. Assumptions made
    2948. +
    2949. Expectation value and variance
    2950. +
    2951. Expectation value and variance for \( \boldsymbol{\beta} \)
    2952. +
    2953. Deriving OLS from a probability distribution
    2954. +
    2955. Independent and Identically Distrubuted (iid)
    2956. +
    2957. Maximum Likelihood Estimation (MLE)
    2958. +
    2959. A new Cost Function
    2960. +
    2961. More basic Statistics and Bayes' theorem
    2962. +
    2963. Marginal Probability
    2964. +
    2965. Conditional Probability
    2966. +
    2967. Bayes' Theorem
    2968. +
    2969. Interpretations of Bayes' Theorem
    2970. +
    2971. Example of Usage of Bayes' theorem
    2972. +
    2973. Doing it correctly
    2974. +
    2975. Bayes' Theorem and Ridge and Lasso Regression
    2976. +
    2977. Test Function for what happens with OLS, Ridge and Lasso
    2978. +
    2979. Invoking Bayes' theorem
    2980. +
    2981. Ridge and Bayes
    2982. +
    2983. Lasso and Bayes
    2984. +
    2985. Exercise 1: mean values and variances in linear regression
    2986. +
    2987. Exercise 2: Adding Ridge and Lasso Regression
    2988. @@ -297,20 +295,22 @@ MathJax.Hub.Config({

       

       

       

      -

      Comparison with OLS

      -

      When we compare this with the ordinary least squares result we have

      -$$ -\hat{\boldsymbol{\beta}}_{\mathrm{OLS}} = \left(\boldsymbol{X}^T\boldsymbol{X}\right)^{-1}\boldsymbol{X}^T\boldsymbol{y}, -$$ +

      SVD analysis

      -

      which can lead to singular matrices. However, with the SVD, we can always compute the inverse of the matrix \( \boldsymbol{X}^T\boldsymbol{X} \).

      - -

      We see that Ridge regression is nothing but the standard OLS with a -modified diagonal term added to \( \boldsymbol{X}^T\boldsymbol{X} \). The consequences, in -particular for our discussion of the bias-variance tradeoff are rather -interesting. We will see that for specific values of \( \lambda \), we may -even reduce the variance of the optimal parameters \( \boldsymbol{\beta} \). These topics and other related ones, will be discussed after the more linear algebra oriented analysis here. +

      Using our insights about the SVD of the design matrix \( \boldsymbol{X} \) +We have already analyzed the OLS solutions in terms of the eigenvectors (the columns) of the right singular value matrix \( \boldsymbol{U} \) as

      +$$ +\tilde{\boldsymbol{y}}_{\mathrm{OLS}}=\boldsymbol{X}\boldsymbol{\beta} =\boldsymbol{U}\boldsymbol{U}^T\boldsymbol{y}. +$$ + +

      For Ridge regression this becomes

      + +$$ +\tilde{\boldsymbol{y}}_{\mathrm{Ridge}}=\boldsymbol{X}\boldsymbol{\beta}_{\mathrm{Ridge}} = \boldsymbol{U\Sigma V^T}\left(\boldsymbol{V}\boldsymbol{\Sigma}^2\boldsymbol{V}^T+\lambda\boldsymbol{I} \right)^{-1}(\boldsymbol{U\Sigma V^T})^T\boldsymbol{y}=\sum_{j=0}^{p-1}\boldsymbol{u}_j\boldsymbol{u}_j^T\frac{\sigma_j^2}{\sigma_j^2+\lambda}\boldsymbol{y}, +$$ + +

      with the vectors \( \boldsymbol{u}_j \) being the columns of \( \boldsymbol{U} \) from the SVD of the matrix \( \boldsymbol{X} \).

      @@ -337,7 +337,7 @@ even reduce the variance of the optimal parameters \( \boldsymbol{\beta} \). The

    2989. 22
    2990. 23
    2991. ...
    2992. -
    2993. 60
    2994. +
    2995. 59
    2996. »
    2997. diff --git a/doc/pub/week36/html/._week36-bs014.html b/doc/pub/week36/html/._week36-bs014.html index 093030d9d..5b829507f 100644 --- a/doc/pub/week36/html/._week36-bs014.html +++ b/doc/pub/week36/html/._week36-bs014.html @@ -37,7 +37,6 @@ doconce format html week36.do.txt --html_style=bootstrap --pygments_html_style=d
    2998. Plans for week 36
    2999. -
    3000. Thursday September 8
    3001. -
    3002. Summary from last Week and discussion of SVD, Ridge and Lasso regression with examples
    3003. -
    3004. Linear Regression and the SVD
    3005. -
    3006. What does it mean?
    3007. -
    3008. And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
    3009. -
    3010. Code for SVD and Inversion of Matrices
    3011. -
    3012. Inverse of Rectangular Matrix
    3013. -
    3014. Ridge and LASSO Regression
    3015. -
    3016. From OLS to Ridge and Lasso
    3017. -
    3018. Deriving the Ridge Regression Equations
    3019. -
    3020. Note on Scikit-Learn
    3021. -
    3022. Comparison with OLS
    3023. -
    3024. SVD analysis
    3025. -
    3026. Interpreting the Ridge results
    3027. -
    3028. More interpretations
    3029. -
    3030. Deriving the Lasso Regression Equations
    3031. -
    3032. Simple example to illustrate Ordinary Least Squares, Ridge and Lasso Regression
    3033. -
    3034. Ridge Regression
    3035. -
    3036. Lasso Regression
    3037. -
    3038. Yet another Example
    3039. -
    3040. The OLS case
    3041. -
    3042. The Ridge case
    3043. -
    3044. Writing the Cost Function
    3045. -
    3046. Lasso case
    3047. -
    3048. The first Case
    3049. -
    3050. Simple code for solving the above problem
    3051. -
    3052. With Lasso Regression
    3053. -
    3054. Another Example, now with a polynomial fit
    3055. -
    3056. To think about, first part
    3057. -
    3058. More thinking
    3059. -
    3060. Still thinking
    3061. -
    3062. What does centering (subtracting the mean values) mean mathematically?
    3063. -
    3064. Further Manipulations
    3065. -
    3066. Wrapping it up
    3067. -
    3068. Linear Regression code, Intercept handling first
    3069. -
    3070. Code Examples
    3071. -
    3072. Taking out the mean
    3073. -
    3074. Friday September 9
    3075. -
    3076. Linking the regression analysis with a statistical interpretation
    3077. -
    3078. Assumptions made
    3079. -
    3080. Expectation value and variance
    3081. -
    3082. Expectation value and variance for \( \boldsymbol{\beta} \)
    3083. -
    3084. Deriving OLS from a probability distribution
    3085. -
    3086. Independent and Identically Distrubuted (iid)
    3087. -
    3088. Maximum Likelihood Estimation (MLE)
    3089. -
    3090. A new Cost Function
    3091. -
    3092. More basic Statistics and Bayes' theorem
    3093. -
    3094. Marginal Probability
    3095. -
    3096. Conditional Probability
    3097. -
    3098. Bayes' Theorem
    3099. -
    3100. Interpretations of Bayes' Theorem
    3101. -
    3102. Example of Usage of Bayes' theorem
    3103. -
    3104. Doing it correctly
    3105. -
    3106. Bayes' Theorem and Ridge and Lasso Regression
    3107. -
    3108. Test Function for what happens with OLS, Ridge and Lasso
    3109. -
    3110. Invoking Bayes' theorem
    3111. -
    3112. Ridge and Bayes
    3113. -
    3114. Lasso and Bayes
    3115. -
    3116. Exercise 1: mean values and variances in linear regression
    3117. -
    3118. Exercise 2: Adding Ridge and Lasso Regression
    3119. +
    3120. Summary from last Week and discussion of SVD, Ridge and Lasso regression with examples
    3121. +
    3122. Linear Regression and the SVD
    3123. +
    3124. What does it mean?
    3125. +
    3126. And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
    3127. +
    3128. Code for SVD and Inversion of Matrices
    3129. +
    3130. Inverse of Rectangular Matrix
    3131. +
    3132. Ridge and LASSO Regression
    3133. +
    3134. From OLS to Ridge and Lasso
    3135. +
    3136. Deriving the Ridge Regression Equations
    3137. +
    3138. Note on Scikit-Learn
    3139. +
    3140. Comparison with OLS
    3141. +
    3142. SVD analysis
    3143. +
    3144. Interpreting the Ridge results
    3145. +
    3146. More interpretations
    3147. +
    3148. Deriving the Lasso Regression Equations
    3149. +
    3150. Simple example to illustrate Ordinary Least Squares, Ridge and Lasso Regression
    3151. +
    3152. Ridge Regression
    3153. +
    3154. Lasso Regression
    3155. +
    3156. Yet another Example
    3157. +
    3158. The OLS case
    3159. +
    3160. The Ridge case
    3161. +
    3162. Writing the Cost Function
    3163. +
    3164. Lasso case
    3165. +
    3166. The first Case
    3167. +
    3168. Simple code for solving the above problem
    3169. +
    3170. With Lasso Regression
    3171. +
    3172. Another Example, now with a polynomial fit
    3173. +
    3174. To think about, first part
    3175. +
    3176. More thinking
    3177. +
    3178. Still thinking
    3179. +
    3180. What does centering (subtracting the mean values) mean mathematically?
    3181. +
    3182. Further Manipulations
    3183. +
    3184. Wrapping it up
    3185. +
    3186. Linear Regression code, Intercept handling first
    3187. +
    3188. Code Examples
    3189. +
    3190. Taking out the mean
    3191. +
    3192. Friday September 9
    3193. +
    3194. Linking the regression analysis with a statistical interpretation
    3195. +
    3196. Assumptions made
    3197. +
    3198. Expectation value and variance
    3199. +
    3200. Expectation value and variance for \( \boldsymbol{\beta} \)
    3201. +
    3202. Deriving OLS from a probability distribution
    3203. +
    3204. Independent and Identically Distrubuted (iid)
    3205. +
    3206. Maximum Likelihood Estimation (MLE)
    3207. +
    3208. A new Cost Function
    3209. +
    3210. More basic Statistics and Bayes' theorem
    3211. +
    3212. Marginal Probability
    3213. +
    3214. Conditional Probability
    3215. +
    3216. Bayes' Theorem
    3217. +
    3218. Interpretations of Bayes' Theorem
    3219. +
    3220. Example of Usage of Bayes' theorem
    3221. +
    3222. Doing it correctly
    3223. +
    3224. Bayes' Theorem and Ridge and Lasso Regression
    3225. +
    3226. Test Function for what happens with OLS, Ridge and Lasso
    3227. +
    3228. Invoking Bayes' theorem
    3229. +
    3230. Ridge and Bayes
    3231. +
    3232. Lasso and Bayes
    3233. +
    3234. Exercise 1: mean values and variances in linear regression
    3235. +
    3236. Exercise 2: Adding Ridge and Lasso Regression
    3237. @@ -297,22 +295,22 @@ MathJax.Hub.Config({

       

       

       

      -

      SVD analysis

      +

      Interpreting the Ridge results

      -

      Using our insights about the SVD of the design matrix \( \boldsymbol{X} \) -We have already analyzed the OLS solutions in terms of the eigenvectors (the columns) of the right singular value matrix \( \boldsymbol{U} \) as +

      Since \( \lambda \geq 0 \), it means that compared to OLS, we have

      + +$$ +\frac{\sigma_j^2}{\sigma_j^2+\lambda} \leq 1. +$$ + +

      Ridge regression finds the coordinates of \( \boldsymbol{y} \) with respect to the +orthonormal basis \( \boldsymbol{U} \), it then shrinks the coordinates by +\( \frac{\sigma_j^2}{\sigma_j^2+\lambda} \). Recall that the SVD has +eigenvalues ordered in a descending way, that is \( \sigma_i \geq +\sigma_{i+1} \).

      -$$ -\tilde{\boldsymbol{y}}_{\mathrm{OLS}}=\boldsymbol{X}\boldsymbol{\beta} =\boldsymbol{U}\boldsymbol{U}^T\boldsymbol{y}. -$$ -

      For Ridge regression this becomes

      - -$$ -\tilde{\boldsymbol{y}}_{\mathrm{Ridge}}=\boldsymbol{X}\boldsymbol{\beta}_{\mathrm{Ridge}} = \boldsymbol{U\Sigma V^T}\left(\boldsymbol{V}\boldsymbol{\Sigma}^2\boldsymbol{V}^T+\lambda\boldsymbol{I} \right)^{-1}(\boldsymbol{U\Sigma V^T})^T\boldsymbol{y}=\sum_{j=0}^{p-1}\boldsymbol{u}_j\boldsymbol{u}_j^T\frac{\sigma_j^2}{\sigma_j^2+\lambda}\boldsymbol{y}, -$$ - -

      with the vectors \( \boldsymbol{u}_j \) being the columns of \( \boldsymbol{U} \) from the SVD of the matrix \( \boldsymbol{X} \).

      +

      For small eigenvalues \( \sigma_i \) it means that their contributions become less important, a fact which can be used to reduce the number of degrees of freedom. More about this when we have covered the material on a statistical interpretation of various linear regression methods.

      @@ -339,7 +337,7 @@ $$

    3238. 23
    3239. 24
    3240. ...
    3241. -
    3242. 60
    3243. +
    3244. 59
    3245. »
    3246. diff --git a/doc/pub/week36/html/._week36-bs015.html b/doc/pub/week36/html/._week36-bs015.html index f7cd403cf..e4c997e23 100644 --- a/doc/pub/week36/html/._week36-bs015.html +++ b/doc/pub/week36/html/._week36-bs015.html @@ -37,7 +37,6 @@ doconce format html week36.do.txt --html_style=bootstrap --pygments_html_style=d
    3247. Plans for week 36
    3248. -
    3249. Thursday September 8
    3250. -
    3251. Summary from last Week and discussion of SVD, Ridge and Lasso regression with examples
    3252. -
    3253. Linear Regression and the SVD
    3254. -
    3255. What does it mean?
    3256. -
    3257. And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
    3258. -
    3259. Code for SVD and Inversion of Matrices
    3260. -
    3261. Inverse of Rectangular Matrix
    3262. -
    3263. Ridge and LASSO Regression
    3264. -
    3265. From OLS to Ridge and Lasso
    3266. -
    3267. Deriving the Ridge Regression Equations
    3268. -
    3269. Note on Scikit-Learn
    3270. -
    3271. Comparison with OLS
    3272. -
    3273. SVD analysis
    3274. -
    3275. Interpreting the Ridge results
    3276. -
    3277. More interpretations
    3278. -
    3279. Deriving the Lasso Regression Equations
    3280. -
    3281. Simple example to illustrate Ordinary Least Squares, Ridge and Lasso Regression
    3282. -
    3283. Ridge Regression
    3284. -
    3285. Lasso Regression
    3286. -
    3287. Yet another Example
    3288. -
    3289. The OLS case
    3290. -
    3291. The Ridge case
    3292. -
    3293. Writing the Cost Function
    3294. -
    3295. Lasso case
    3296. -
    3297. The first Case
    3298. -
    3299. Simple code for solving the above problem
    3300. -
    3301. With Lasso Regression
    3302. -
    3303. Another Example, now with a polynomial fit
    3304. -
    3305. To think about, first part
    3306. -
    3307. More thinking
    3308. -
    3309. Still thinking
    3310. -
    3311. What does centering (subtracting the mean values) mean mathematically?
    3312. -
    3313. Further Manipulations
    3314. -
    3315. Wrapping it up
    3316. -
    3317. Linear Regression code, Intercept handling first
    3318. -
    3319. Code Examples
    3320. -
    3321. Taking out the mean
    3322. -
    3323. Friday September 9
    3324. -
    3325. Linking the regression analysis with a statistical interpretation
    3326. -
    3327. Assumptions made
    3328. -
    3329. Expectation value and variance
    3330. -
    3331. Expectation value and variance for \( \boldsymbol{\beta} \)
    3332. -
    3333. Deriving OLS from a probability distribution
    3334. -
    3335. Independent and Identically Distrubuted (iid)
    3336. -
    3337. Maximum Likelihood Estimation (MLE)
    3338. -
    3339. A new Cost Function
    3340. -
    3341. More basic Statistics and Bayes' theorem
    3342. -
    3343. Marginal Probability
    3344. -
    3345. Conditional Probability
    3346. -
    3347. Bayes' Theorem
    3348. -
    3349. Interpretations of Bayes' Theorem
    3350. -
    3351. Example of Usage of Bayes' theorem
    3352. -
    3353. Doing it correctly
    3354. -
    3355. Bayes' Theorem and Ridge and Lasso Regression
    3356. -
    3357. Test Function for what happens with OLS, Ridge and Lasso
    3358. -
    3359. Invoking Bayes' theorem
    3360. -
    3361. Ridge and Bayes
    3362. -
    3363. Lasso and Bayes
    3364. -
    3365. Exercise 1: mean values and variances in linear regression
    3366. -
    3367. Exercise 2: Adding Ridge and Lasso Regression
    3368. +
    3369. Summary from last Week and discussion of SVD, Ridge and Lasso regression with examples
    3370. +
    3371. Linear Regression and the SVD
    3372. +
    3373. What does it mean?
    3374. +
    3375. And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
    3376. +
    3377. Code for SVD and Inversion of Matrices
    3378. +
    3379. Inverse of Rectangular Matrix
    3380. +
    3381. Ridge and LASSO Regression
    3382. +
    3383. From OLS to Ridge and Lasso
    3384. +
    3385. Deriving the Ridge Regression Equations
    3386. +
    3387. Note on Scikit-Learn
    3388. +
    3389. Comparison with OLS
    3390. +
    3391. SVD analysis
    3392. +
    3393. Interpreting the Ridge results
    3394. +
    3395. More interpretations
    3396. +
    3397. Deriving the Lasso Regression Equations
    3398. +
    3399. Simple example to illustrate Ordinary Least Squares, Ridge and Lasso Regression
    3400. +
    3401. Ridge Regression
    3402. +
    3403. Lasso Regression
    3404. +
    3405. Yet another Example
    3406. +
    3407. The OLS case
    3408. +
    3409. The Ridge case
    3410. +
    3411. Writing the Cost Function
    3412. +
    3413. Lasso case
    3414. +
    3415. The first Case
    3416. +
    3417. Simple code for solving the above problem
    3418. +
    3419. With Lasso Regression
    3420. +
    3421. Another Example, now with a polynomial fit
    3422. +
    3423. To think about, first part
    3424. +
    3425. More thinking
    3426. +
    3427. Still thinking
    3428. +
    3429. What does centering (subtracting the mean values) mean mathematically?
    3430. +
    3431. Further Manipulations
    3432. +
    3433. Wrapping it up
    3434. +
    3435. Linear Regression code, Intercept handling first
    3436. +
    3437. Code Examples
    3438. +
    3439. Taking out the mean
    3440. +
    3441. Friday September 9
    3442. +
    3443. Linking the regression analysis with a statistical interpretation
    3444. +
    3445. Assumptions made
    3446. +
    3447. Expectation value and variance
    3448. +
    3449. Expectation value and variance for \( \boldsymbol{\beta} \)
    3450. +
    3451. Deriving OLS from a probability distribution
    3452. +
    3453. Independent and Identically Distrubuted (iid)
    3454. +
    3455. Maximum Likelihood Estimation (MLE)
    3456. +
    3457. A new Cost Function
    3458. +
    3459. More basic Statistics and Bayes' theorem
    3460. +
    3461. Marginal Probability
    3462. +
    3463. Conditional Probability
    3464. +
    3465. Bayes' Theorem
    3466. +
    3467. Interpretations of Bayes' Theorem
    3468. +
    3469. Example of Usage of Bayes' theorem
    3470. +
    3471. Doing it correctly
    3472. +
    3473. Bayes' Theorem and Ridge and Lasso Regression
    3474. +
    3475. Test Function for what happens with OLS, Ridge and Lasso
    3476. +
    3477. Invoking Bayes' theorem
    3478. +
    3479. Ridge and Bayes
    3480. +
    3481. Lasso and Bayes
    3482. +
    3483. Exercise 1: mean values and variances in linear regression
    3484. +
    3485. Exercise 2: Adding Ridge and Lasso Regression
    3486. @@ -297,22 +295,35 @@ MathJax.Hub.Config({

       

       

       

      -

      Interpreting the Ridge results

      +

      More interpretations

      -

      Since \( \lambda \geq 0 \), it means that compared to OLS, we have

      +

      For the sake of simplicity, let us assume that the design matrix is orthonormal, that is

      $$ -\frac{\sigma_j^2}{\sigma_j^2+\lambda} \leq 1. +\boldsymbol{X}^T\boldsymbol{X}=(\boldsymbol{X}^T\boldsymbol{X})^{-1} =\boldsymbol{I}. $$ -

      Ridge regression finds the coordinates of \( \boldsymbol{y} \) with respect to the -orthonormal basis \( \boldsymbol{U} \), it then shrinks the coordinates by -\( \frac{\sigma_j^2}{\sigma_j^2+\lambda} \). Recall that the SVD has -eigenvalues ordered in a descending way, that is \( \sigma_i \geq -\sigma_{i+1} \). +

      In this case the standard OLS results in

      +$$ +\boldsymbol{\beta}^{\mathrm{OLS}} = \boldsymbol{X}^T\boldsymbol{y}=\sum_{i=0}^{n-1}\boldsymbol{u}_i\boldsymbol{u}_i^T\boldsymbol{y}, +$$ + +

      and

      + +$$ +\boldsymbol{\beta}^{\mathrm{Ridge}} = \left(\boldsymbol{I}+\lambda\boldsymbol{I}\right)^{-1}\boldsymbol{X}^T\boldsymbol{y}=\left(1+\lambda\right)^{-1}\boldsymbol{\beta}^{\mathrm{OLS}}, +$$ + +

      that is the Ridge estimator scales the OLS estimator by the inverse of a factor \( 1+\lambda \), and +the Ridge estimator converges to zero when the hyperparameter goes to +infinity.

      -

      For small eigenvalues \( \sigma_i \) it means that their contributions become less important, a fact which can be used to reduce the number of degrees of freedom. More about this when we have covered the material on a statistical interpretation of various linear regression methods.

      +

      We will come back to more interpreations after we have gone through some of the statistical analysis part.

      + +

      For more discussions of Ridge and Lasso regression, Wessel van Wieringen's article is highly recommended. +Similarly, Mehta et al's article is also recommended. +

      @@ -339,7 +350,7 @@ eigenvalues ordered in a descending way, that is \( \sigma_i \geq

    3487. 24
    3488. 25
    3489. ...
    3490. -
    3491. 60
    3492. +
    3493. 59
    3494. »
    3495. diff --git a/doc/pub/week36/html/._week36-bs016.html b/doc/pub/week36/html/._week36-bs016.html index ceee78588..e914af0f0 100644 --- a/doc/pub/week36/html/._week36-bs016.html +++ b/doc/pub/week36/html/._week36-bs016.html @@ -37,7 +37,6 @@ doconce format html week36.do.txt --html_style=bootstrap --pygments_html_style=d
    3496. Plans for week 36
    3497. -
    3498. Thursday September 8
    3499. -
    3500. Summary from last Week and discussion of SVD, Ridge and Lasso regression with examples
    3501. -
    3502. Linear Regression and the SVD
    3503. -
    3504. What does it mean?
    3505. -
    3506. And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
    3507. -
    3508. Code for SVD and Inversion of Matrices
    3509. -
    3510. Inverse of Rectangular Matrix
    3511. -
    3512. Ridge and LASSO Regression
    3513. -
    3514. From OLS to Ridge and Lasso
    3515. -
    3516. Deriving the Ridge Regression Equations
    3517. -
    3518. Note on Scikit-Learn
    3519. -
    3520. Comparison with OLS
    3521. -
    3522. SVD analysis
    3523. -
    3524. Interpreting the Ridge results
    3525. -
    3526. More interpretations
    3527. -
    3528. Deriving the Lasso Regression Equations
    3529. -
    3530. Simple example to illustrate Ordinary Least Squares, Ridge and Lasso Regression
    3531. -
    3532. Ridge Regression
    3533. -
    3534. Lasso Regression
    3535. -
    3536. Yet another Example
    3537. -
    3538. The OLS case
    3539. -
    3540. The Ridge case
    3541. -
    3542. Writing the Cost Function
    3543. -
    3544. Lasso case
    3545. -
    3546. The first Case
    3547. -
    3548. Simple code for solving the above problem
    3549. -
    3550. With Lasso Regression
    3551. -
    3552. Another Example, now with a polynomial fit
    3553. -
    3554. To think about, first part
    3555. -
    3556. More thinking
    3557. -
    3558. Still thinking
    3559. -
    3560. What does centering (subtracting the mean values) mean mathematically?
    3561. -
    3562. Further Manipulations
    3563. -
    3564. Wrapping it up
    3565. -
    3566. Linear Regression code, Intercept handling first
    3567. -
    3568. Code Examples
    3569. -
    3570. Taking out the mean
    3571. -
    3572. Friday September 9
    3573. -
    3574. Linking the regression analysis with a statistical interpretation
    3575. -
    3576. Assumptions made
    3577. -
    3578. Expectation value and variance
    3579. -
    3580. Expectation value and variance for \( \boldsymbol{\beta} \)
    3581. -
    3582. Deriving OLS from a probability distribution
    3583. -
    3584. Independent and Identically Distrubuted (iid)
    3585. -
    3586. Maximum Likelihood Estimation (MLE)
    3587. -
    3588. A new Cost Function
    3589. -
    3590. More basic Statistics and Bayes' theorem
    3591. -
    3592. Marginal Probability
    3593. -
    3594. Conditional Probability
    3595. -
    3596. Bayes' Theorem
    3597. -
    3598. Interpretations of Bayes' Theorem
    3599. -
    3600. Example of Usage of Bayes' theorem
    3601. -
    3602. Doing it correctly
    3603. -
    3604. Bayes' Theorem and Ridge and Lasso Regression
    3605. -
    3606. Test Function for what happens with OLS, Ridge and Lasso
    3607. -
    3608. Invoking Bayes' theorem
    3609. -
    3610. Ridge and Bayes
    3611. -
    3612. Lasso and Bayes
    3613. -
    3614. Exercise 1: mean values and variances in linear regression
    3615. -
    3616. Exercise 2: Adding Ridge and Lasso Regression
    3617. +
    3618. Summary from last Week and discussion of SVD, Ridge and Lasso regression with examples
    3619. +
    3620. Linear Regression and the SVD
    3621. +
    3622. What does it mean?
    3623. +
    3624. And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
    3625. +
    3626. Code for SVD and Inversion of Matrices
    3627. +
    3628. Inverse of Rectangular Matrix
    3629. +
    3630. Ridge and LASSO Regression
    3631. +
    3632. From OLS to Ridge and Lasso
    3633. +
    3634. Deriving the Ridge Regression Equations
    3635. +
    3636. Note on Scikit-Learn
    3637. +
    3638. Comparison with OLS
    3639. +
    3640. SVD analysis
    3641. +
    3642. Interpreting the Ridge results
    3643. +
    3644. More interpretations
    3645. +
    3646. Deriving the Lasso Regression Equations
    3647. +
    3648. Simple example to illustrate Ordinary Least Squares, Ridge and Lasso Regression
    3649. +
    3650. Ridge Regression
    3651. +
    3652. Lasso Regression
    3653. +
    3654. Yet another Example
    3655. +
    3656. The OLS case
    3657. +
    3658. The Ridge case
    3659. +
    3660. Writing the Cost Function
    3661. +
    3662. Lasso case
    3663. +
    3664. The first Case
    3665. +
    3666. Simple code for solving the above problem
    3667. +
    3668. With Lasso Regression
    3669. +
    3670. Another Example, now with a polynomial fit
    3671. +
    3672. To think about, first part
    3673. +
    3674. More thinking
    3675. +
    3676. Still thinking
    3677. +
    3678. What does centering (subtracting the mean values) mean mathematically?
    3679. +
    3680. Further Manipulations
    3681. +
    3682. Wrapping it up
    3683. +
    3684. Linear Regression code, Intercept handling first
    3685. +
    3686. Code Examples
    3687. +
    3688. Taking out the mean
    3689. +
    3690. Friday September 9
    3691. +
    3692. Linking the regression analysis with a statistical interpretation
    3693. +
    3694. Assumptions made
    3695. +
    3696. Expectation value and variance
    3697. +
    3698. Expectation value and variance for \( \boldsymbol{\beta} \)
    3699. +
    3700. Deriving OLS from a probability distribution
    3701. +
    3702. Independent and Identically Distrubuted (iid)
    3703. +
    3704. Maximum Likelihood Estimation (MLE)
    3705. +
    3706. A new Cost Function
    3707. +
    3708. More basic Statistics and Bayes' theorem
    3709. +
    3710. Marginal Probability
    3711. +
    3712. Conditional Probability
    3713. +
    3714. Bayes' Theorem
    3715. +
    3716. Interpretations of Bayes' Theorem
    3717. +
    3718. Example of Usage of Bayes' theorem
    3719. +
    3720. Doing it correctly
    3721. +
    3722. Bayes' Theorem and Ridge and Lasso Regression
    3723. +
    3724. Test Function for what happens with OLS, Ridge and Lasso
    3725. +
    3726. Invoking Bayes' theorem
    3727. +
    3728. Ridge and Bayes
    3729. +
    3730. Lasso and Bayes
    3731. +
    3732. Exercise 1: mean values and variances in linear regression
    3733. +
    3734. Exercise 2: Adding Ridge and Lasso Regression
    3735. @@ -297,35 +295,31 @@ MathJax.Hub.Config({

       

       

       

      -

      More interpretations

      +

      Deriving the Lasso Regression Equations

      -

      For the sake of simplicity, let us assume that the design matrix is orthonormal, that is

      +

      Using the matrix-vector expression for Lasso regression and dropping the parameter \( 1/n \) in front of the standard mean squared error equation, we have the following cost function

      $$ -\boldsymbol{X}^T\boldsymbol{X}=(\boldsymbol{X}^T\boldsymbol{X})^{-1} =\boldsymbol{I}. +C(\boldsymbol{X},\boldsymbol{\beta})=\left\{(\boldsymbol{y}-\boldsymbol{X}\boldsymbol{\beta})^T(\boldsymbol{y}-\boldsymbol{X}\boldsymbol{\beta})\right\}+\lambda\vert\vert\boldsymbol{\beta}\vert\vert_1, $$ -

      In this case the standard OLS results in

      +

      Taking the derivative with respect to \( \boldsymbol{\beta} \) and recalling that the derivative of the absolute value is (we drop the boldfaced vector symbol for simplicty)

      $$ -\boldsymbol{\beta}^{\mathrm{OLS}} = \boldsymbol{X}^T\boldsymbol{y}=\sum_{i=0}^{n-1}\boldsymbol{u}_i\boldsymbol{u}_i^T\boldsymbol{y}, +\frac{d \vert \beta\vert}{d \boldsymbol{\beta}}=\mathrm{sgn}(\boldsymbol{\beta})=\left\{\begin{array}{cc} 1 & \beta > 0 \\-1 & \beta < 0, \end{array}\right. $$ -

      and

      +

      we have that the derivative of the cost function is

      $$ -\boldsymbol{\beta}^{\mathrm{Ridge}} = \left(\boldsymbol{I}+\lambda\boldsymbol{I}\right)^{-1}\boldsymbol{X}^T\boldsymbol{y}=\left(1+\lambda\right)^{-1}\boldsymbol{\beta}^{\mathrm{OLS}}, +\frac{\partial C(\boldsymbol{X},\boldsymbol{\beta})}{\partial \boldsymbol{\beta}}=-2\boldsymbol{X}^T(\boldsymbol{y}-\boldsymbol{X}\boldsymbol{\beta})+\lambda sgn(\boldsymbol{\beta})=0, $$ -

      that is the Ridge estimator scales the OLS estimator by the inverse of a factor \( 1+\lambda \), and -the Ridge estimator converges to zero when the hyperparameter goes to -infinity. -

      +

      and reordering we have

      +$$ +\boldsymbol{X}^T\boldsymbol{X}\boldsymbol{\beta}+\lambda sgn(\boldsymbol{\beta})=2\boldsymbol{X}^T\boldsymbol{y}. +$$ -

      We will come back to more interpreations after we have gone through some of the statistical analysis part.

      - -

      For more discussions of Ridge and Lasso regression, Wessel van Wieringen's article is highly recommended. -Similarly, Mehta et al's article is also recommended. -

      +

      This equation does not lead to a nice analytical equation as in Ridge regression or ordinary least squares. This equation can however be solved by using standard convex optimization algorithms using for example the Python package CVXOPT. We will discuss this later.

      @@ -352,7 +346,7 @@ Similarly, Mehta et al

    3736. 25
    3737. 26
    3738. ...
    3739. -
    3740. 60
    3741. +
    3742. 59
    3743. »
    3744. diff --git a/doc/pub/week36/html/._week36-bs017.html b/doc/pub/week36/html/._week36-bs017.html index 0bfe5df46..43a007fee 100644 --- a/doc/pub/week36/html/._week36-bs017.html +++ b/doc/pub/week36/html/._week36-bs017.html @@ -37,7 +37,6 @@ doconce format html week36.do.txt --html_style=bootstrap --pygments_html_style=d
    3745. Plans for week 36
    3746. -
    3747. Thursday September 8
    3748. -
    3749. Summary from last Week and discussion of SVD, Ridge and Lasso regression with examples
    3750. -
    3751. Linear Regression and the SVD
    3752. -
    3753. What does it mean?
    3754. -
    3755. And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
    3756. -
    3757. Code for SVD and Inversion of Matrices
    3758. -
    3759. Inverse of Rectangular Matrix
    3760. -
    3761. Ridge and LASSO Regression
    3762. -
    3763. From OLS to Ridge and Lasso
    3764. -
    3765. Deriving the Ridge Regression Equations
    3766. -
    3767. Note on Scikit-Learn
    3768. -
    3769. Comparison with OLS
    3770. -
    3771. SVD analysis
    3772. -
    3773. Interpreting the Ridge results
    3774. -
    3775. More interpretations
    3776. -
    3777. Deriving the Lasso Regression Equations
    3778. -
    3779. Simple example to illustrate Ordinary Least Squares, Ridge and Lasso Regression
    3780. -
    3781. Ridge Regression
    3782. -
    3783. Lasso Regression
    3784. -
    3785. Yet another Example
    3786. -
    3787. The OLS case
    3788. -
    3789. The Ridge case
    3790. -
    3791. Writing the Cost Function
    3792. -
    3793. Lasso case
    3794. -
    3795. The first Case
    3796. -
    3797. Simple code for solving the above problem
    3798. -
    3799. With Lasso Regression
    3800. -
    3801. Another Example, now with a polynomial fit
    3802. -
    3803. To think about, first part
    3804. -
    3805. More thinking
    3806. -
    3807. Still thinking
    3808. -
    3809. What does centering (subtracting the mean values) mean mathematically?
    3810. -
    3811. Further Manipulations
    3812. -
    3813. Wrapping it up
    3814. -
    3815. Linear Regression code, Intercept handling first
    3816. -
    3817. Code Examples
    3818. -
    3819. Taking out the mean
    3820. -
    3821. Friday September 9
    3822. -
    3823. Linking the regression analysis with a statistical interpretation
    3824. -
    3825. Assumptions made
    3826. -
    3827. Expectation value and variance
    3828. -
    3829. Expectation value and variance for \( \boldsymbol{\beta} \)
    3830. -
    3831. Deriving OLS from a probability distribution
    3832. -
    3833. Independent and Identically Distrubuted (iid)
    3834. -
    3835. Maximum Likelihood Estimation (MLE)
    3836. -
    3837. A new Cost Function
    3838. -
    3839. More basic Statistics and Bayes' theorem
    3840. -
    3841. Marginal Probability
    3842. -
    3843. Conditional Probability
    3844. -
    3845. Bayes' Theorem
    3846. -
    3847. Interpretations of Bayes' Theorem
    3848. -
    3849. Example of Usage of Bayes' theorem
    3850. -
    3851. Doing it correctly
    3852. -
    3853. Bayes' Theorem and Ridge and Lasso Regression
    3854. -
    3855. Test Function for what happens with OLS, Ridge and Lasso
    3856. -
    3857. Invoking Bayes' theorem
    3858. -
    3859. Ridge and Bayes
    3860. -
    3861. Lasso and Bayes
    3862. -
    3863. Exercise 1: mean values and variances in linear regression
    3864. -
    3865. Exercise 2: Adding Ridge and Lasso Regression
    3866. +
    3867. Summary from last Week and discussion of SVD, Ridge and Lasso regression with examples
    3868. +
    3869. Linear Regression and the SVD
    3870. +
    3871. What does it mean?
    3872. +
    3873. And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
    3874. +
    3875. Code for SVD and Inversion of Matrices
    3876. +
    3877. Inverse of Rectangular Matrix
    3878. +
    3879. Ridge and LASSO Regression
    3880. +
    3881. From OLS to Ridge and Lasso
    3882. +
    3883. Deriving the Ridge Regression Equations
    3884. +
    3885. Note on Scikit-Learn
    3886. +
    3887. Comparison with OLS
    3888. +
    3889. SVD analysis
    3890. +
    3891. Interpreting the Ridge results
    3892. +
    3893. More interpretations
    3894. +
    3895. Deriving the Lasso Regression Equations
    3896. +
    3897. Simple example to illustrate Ordinary Least Squares, Ridge and Lasso Regression
    3898. +
    3899. Ridge Regression
    3900. +
    3901. Lasso Regression
    3902. +
    3903. Yet another Example
    3904. +
    3905. The OLS case
    3906. +
    3907. The Ridge case
    3908. +
    3909. Writing the Cost Function
    3910. +
    3911. Lasso case
    3912. +
    3913. The first Case
    3914. +
    3915. Simple code for solving the above problem
    3916. +
    3917. With Lasso Regression
    3918. +
    3919. Another Example, now with a polynomial fit
    3920. +
    3921. To think about, first part
    3922. +
    3923. More thinking
    3924. +
    3925. Still thinking
    3926. +
    3927. What does centering (subtracting the mean values) mean mathematically?
    3928. +
    3929. Further Manipulations
    3930. +
    3931. Wrapping it up
    3932. +
    3933. Linear Regression code, Intercept handling first
    3934. +
    3935. Code Examples
    3936. +
    3937. Taking out the mean
    3938. +
    3939. Friday September 9
    3940. +
    3941. Linking the regression analysis with a statistical interpretation
    3942. +
    3943. Assumptions made
    3944. +
    3945. Expectation value and variance
    3946. +
    3947. Expectation value and variance for \( \boldsymbol{\beta} \)
    3948. +
    3949. Deriving OLS from a probability distribution
    3950. +
    3951. Independent and Identically Distrubuted (iid)
    3952. +
    3953. Maximum Likelihood Estimation (MLE)
    3954. +
    3955. A new Cost Function
    3956. +
    3957. More basic Statistics and Bayes' theorem
    3958. +
    3959. Marginal Probability
    3960. +
    3961. Conditional Probability
    3962. +
    3963. Bayes' Theorem
    3964. +
    3965. Interpretations of Bayes' Theorem
    3966. +
    3967. Example of Usage of Bayes' theorem
    3968. +
    3969. Doing it correctly
    3970. +
    3971. Bayes' Theorem and Ridge and Lasso Regression
    3972. +
    3973. Test Function for what happens with OLS, Ridge and Lasso
    3974. +
    3975. Invoking Bayes' theorem
    3976. +
    3977. Ridge and Bayes
    3978. +
    3979. Lasso and Bayes
    3980. +
    3981. Exercise 1: mean values and variances in linear regression
    3982. +
    3983. Exercise 2: Adding Ridge and Lasso Regression
    3984. @@ -297,31 +295,22 @@ MathJax.Hub.Config({

       

       

       

      -

      Deriving the Lasso Regression Equations

      +

      Simple example to illustrate Ordinary Least Squares, Ridge and Lasso Regression

      -

      Using the matrix-vector expression for Lasso regression and dropping the parameter \( 1/n \) in front of the standard mean squared error equation, we have the following cost function

      +

      Let us assume that our design matrix is given by unit (identity) matrix, that is a square diagonal matrix with ones only along the +diagonal. In this case we have an equal number of rows and columns \( n=p \). +

      +

      Our model approximation is just \( \tilde{\boldsymbol{y}}=\boldsymbol{\beta} \) and the mean squared error and thereby the cost function for ordinary least sqquares (OLS) is then (we drop the term \( 1/n \))

      $$ -C(\boldsymbol{X},\boldsymbol{\beta})=\left\{(\boldsymbol{y}-\boldsymbol{X}\boldsymbol{\beta})^T(\boldsymbol{y}-\boldsymbol{X}\boldsymbol{\beta})\right\}+\lambda\vert\vert\boldsymbol{\beta}\vert\vert_1, +C(\boldsymbol{\beta})=\sum_{i=0}^{p-1}(y_i-\beta_i)^2, $$ -

      Taking the derivative with respect to \( \boldsymbol{\beta} \) and recalling that the derivative of the absolute value is (we drop the boldfaced vector symbol for simplicty)

      +

      and minimizing we have that

      $$ -\frac{d \vert \beta\vert}{d \boldsymbol{\beta}}=\mathrm{sgn}(\boldsymbol{\beta})=\left\{\begin{array}{cc} 1 & \beta > 0 \\-1 & \beta < 0, \end{array}\right. +\hat{\beta}_i^{\mathrm{OLS}} = y_i. $$ -

      we have that the derivative of the cost function is

      - -$$ -\frac{\partial C(\boldsymbol{X},\boldsymbol{\beta})}{\partial \boldsymbol{\beta}}=-2\boldsymbol{X}^T(\boldsymbol{y}-\boldsymbol{X}\boldsymbol{\beta})+\lambda sgn(\boldsymbol{\beta})=0, -$$ - -

      and reordering we have

      -$$ -\boldsymbol{X}^T\boldsymbol{X}\boldsymbol{\beta}+\lambda sgn(\boldsymbol{\beta})=2\boldsymbol{X}^T\boldsymbol{y}. -$$ - -

      This equation does not lead to a nice analytical equation as in Ridge regression or ordinary least squares. This equation can however be solved by using standard convex optimization algorithms using for example the Python package CVXOPT. We will discuss this later.

      @@ -348,7 +337,7 @@ $$

    3985. 26
    3986. 27
    3987. ...
    3988. -
    3989. 60
    3990. +
    3991. 59
    3992. »
    3993. diff --git a/doc/pub/week36/html/._week36-bs018.html b/doc/pub/week36/html/._week36-bs018.html index 7da57dcd9..12a02f185 100644 --- a/doc/pub/week36/html/._week36-bs018.html +++ b/doc/pub/week36/html/._week36-bs018.html @@ -37,7 +37,6 @@ doconce format html week36.do.txt --html_style=bootstrap --pygments_html_style=d
    3994. Plans for week 36
    3995. -
    3996. Thursday September 8
    3997. -
    3998. Summary from last Week and discussion of SVD, Ridge and Lasso regression with examples
    3999. -
    4000. Linear Regression and the SVD
    4001. -
    4002. What does it mean?
    4003. -
    4004. And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
    4005. -
    4006. Code for SVD and Inversion of Matrices
    4007. -
    4008. Inverse of Rectangular Matrix
    4009. -
    4010. Ridge and LASSO Regression
    4011. -
    4012. From OLS to Ridge and Lasso
    4013. -
    4014. Deriving the Ridge Regression Equations
    4015. -
    4016. Note on Scikit-Learn
    4017. -
    4018. Comparison with OLS
    4019. -
    4020. SVD analysis
    4021. -
    4022. Interpreting the Ridge results
    4023. -
    4024. More interpretations
    4025. -
    4026. Deriving the Lasso Regression Equations
    4027. -
    4028. Simple example to illustrate Ordinary Least Squares, Ridge and Lasso Regression
    4029. -
    4030. Ridge Regression
    4031. -
    4032. Lasso Regression
    4033. -
    4034. Yet another Example
    4035. -
    4036. The OLS case
    4037. -
    4038. The Ridge case
    4039. -
    4040. Writing the Cost Function
    4041. -
    4042. Lasso case
    4043. -
    4044. The first Case
    4045. -
    4046. Simple code for solving the above problem
    4047. -
    4048. With Lasso Regression
    4049. -
    4050. Another Example, now with a polynomial fit
    4051. -
    4052. To think about, first part
    4053. -
    4054. More thinking
    4055. -
    4056. Still thinking
    4057. -
    4058. What does centering (subtracting the mean values) mean mathematically?
    4059. -
    4060. Further Manipulations
    4061. -
    4062. Wrapping it up
    4063. -
    4064. Linear Regression code, Intercept handling first
    4065. -
    4066. Code Examples
    4067. -
    4068. Taking out the mean
    4069. -
    4070. Friday September 9
    4071. -
    4072. Linking the regression analysis with a statistical interpretation
    4073. -
    4074. Assumptions made
    4075. -
    4076. Expectation value and variance
    4077. -
    4078. Expectation value and variance for \( \boldsymbol{\beta} \)
    4079. -
    4080. Deriving OLS from a probability distribution
    4081. -
    4082. Independent and Identically Distrubuted (iid)
    4083. -
    4084. Maximum Likelihood Estimation (MLE)
    4085. -
    4086. A new Cost Function
    4087. -
    4088. More basic Statistics and Bayes' theorem
    4089. -
    4090. Marginal Probability
    4091. -
    4092. Conditional Probability
    4093. -
    4094. Bayes' Theorem
    4095. -
    4096. Interpretations of Bayes' Theorem
    4097. -
    4098. Example of Usage of Bayes' theorem
    4099. -
    4100. Doing it correctly
    4101. -
    4102. Bayes' Theorem and Ridge and Lasso Regression
    4103. -
    4104. Test Function for what happens with OLS, Ridge and Lasso
    4105. -
    4106. Invoking Bayes' theorem
    4107. -
    4108. Ridge and Bayes
    4109. -
    4110. Lasso and Bayes
    4111. -
    4112. Exercise 1: mean values and variances in linear regression
    4113. -
    4114. Exercise 2: Adding Ridge and Lasso Regression
    4115. +
    4116. Summary from last Week and discussion of SVD, Ridge and Lasso regression with examples
    4117. +
    4118. Linear Regression and the SVD
    4119. +
    4120. What does it mean?
    4121. +
    4122. And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
    4123. +
    4124. Code for SVD and Inversion of Matrices
    4125. +
    4126. Inverse of Rectangular Matrix
    4127. +
    4128. Ridge and LASSO Regression
    4129. +
    4130. From OLS to Ridge and Lasso
    4131. +
    4132. Deriving the Ridge Regression Equations
    4133. +
    4134. Note on Scikit-Learn
    4135. +
    4136. Comparison with OLS
    4137. +
    4138. SVD analysis
    4139. +
    4140. Interpreting the Ridge results
    4141. +
    4142. More interpretations
    4143. +
    4144. Deriving the Lasso Regression Equations
    4145. +
    4146. Simple example to illustrate Ordinary Least Squares, Ridge and Lasso Regression
    4147. +
    4148. Ridge Regression
    4149. +
    4150. Lasso Regression
    4151. +
    4152. Yet another Example
    4153. +
    4154. The OLS case
    4155. +
    4156. The Ridge case
    4157. +
    4158. Writing the Cost Function
    4159. +
    4160. Lasso case
    4161. +
    4162. The first Case
    4163. +
    4164. Simple code for solving the above problem
    4165. +
    4166. With Lasso Regression
    4167. +
    4168. Another Example, now with a polynomial fit
    4169. +
    4170. To think about, first part
    4171. +
    4172. More thinking
    4173. +
    4174. Still thinking
    4175. +
    4176. What does centering (subtracting the mean values) mean mathematically?
    4177. +
    4178. Further Manipulations
    4179. +
    4180. Wrapping it up
    4181. +
    4182. Linear Regression code, Intercept handling first
    4183. +
    4184. Code Examples
    4185. +
    4186. Taking out the mean
    4187. +
    4188. Friday September 9
    4189. +
    4190. Linking the regression analysis with a statistical interpretation
    4191. +
    4192. Assumptions made
    4193. +
    4194. Expectation value and variance
    4195. +
    4196. Expectation value and variance for \( \boldsymbol{\beta} \)
    4197. +
    4198. Deriving OLS from a probability distribution
    4199. +
    4200. Independent and Identically Distrubuted (iid)
    4201. +
    4202. Maximum Likelihood Estimation (MLE)
    4203. +
    4204. A new Cost Function
    4205. +
    4206. More basic Statistics and Bayes' theorem
    4207. +
    4208. Marginal Probability
    4209. +
    4210. Conditional Probability
    4211. +
    4212. Bayes' Theorem
    4213. +
    4214. Interpretations of Bayes' Theorem
    4215. +
    4216. Example of Usage of Bayes' theorem
    4217. +
    4218. Doing it correctly
    4219. +
    4220. Bayes' Theorem and Ridge and Lasso Regression
    4221. +
    4222. Test Function for what happens with OLS, Ridge and Lasso
    4223. +
    4224. Invoking Bayes' theorem
    4225. +
    4226. Ridge and Bayes
    4227. +
    4228. Lasso and Bayes
    4229. +
    4230. Exercise 1: mean values and variances in linear regression
    4231. +
    4232. Exercise 2: Adding Ridge and Lasso Regression
    4233. @@ -297,20 +295,16 @@ MathJax.Hub.Config({

       

       

       

      -

      Simple example to illustrate Ordinary Least Squares, Ridge and Lasso Regression

      +

      Ridge Regression

      -

      Let us assume that our design matrix is given by unit (identity) matrix, that is a square diagonal matrix with ones only along the -diagonal. In this case we have an equal number of rows and columns \( n=p \). -

      - -

      Our model approximation is just \( \tilde{\boldsymbol{y}}=\boldsymbol{\beta} \) and the mean squared error and thereby the cost function for ordinary least sqquares (OLS) is then (we drop the term \( 1/n \))

      +

      For Ridge regression our cost function is

      $$ -C(\boldsymbol{\beta})=\sum_{i=0}^{p-1}(y_i-\beta_i)^2, +C(\boldsymbol{\beta})=\sum_{i=0}^{p-1}(y_i-\beta_i)^2+\lambda\sum_{i=0}^{p-1}\beta_i^2, $$

      and minimizing we have that

      $$ -\hat{\beta}_i^{\mathrm{OLS}} = y_i. +\hat{\beta}_i^{\mathrm{Ridge}} = \frac{y_i}{1+\lambda}. $$ @@ -339,7 +333,7 @@ $$
    4234. 27
    4235. 28
    4236. ...
    4237. -
    4238. 60
    4239. +
    4240. 59
    4241. »
    4242. diff --git a/doc/pub/week36/html/._week36-bs019.html b/doc/pub/week36/html/._week36-bs019.html index e06dbdf14..3589e887f 100644 --- a/doc/pub/week36/html/._week36-bs019.html +++ b/doc/pub/week36/html/._week36-bs019.html @@ -37,7 +37,6 @@ doconce format html week36.do.txt --html_style=bootstrap --pygments_html_style=d
    4243. Plans for week 36
    4244. -
    4245. Thursday September 8
    4246. -
    4247. Summary from last Week and discussion of SVD, Ridge and Lasso regression with examples
    4248. -
    4249. Linear Regression and the SVD
    4250. -
    4251. What does it mean?
    4252. -
    4253. And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
    4254. -
    4255. Code for SVD and Inversion of Matrices
    4256. -
    4257. Inverse of Rectangular Matrix
    4258. -
    4259. Ridge and LASSO Regression
    4260. -
    4261. From OLS to Ridge and Lasso
    4262. -
    4263. Deriving the Ridge Regression Equations
    4264. -
    4265. Note on Scikit-Learn
    4266. -
    4267. Comparison with OLS
    4268. -
    4269. SVD analysis
    4270. -
    4271. Interpreting the Ridge results
    4272. -
    4273. More interpretations
    4274. -
    4275. Deriving the Lasso Regression Equations
    4276. -
    4277. Simple example to illustrate Ordinary Least Squares, Ridge and Lasso Regression
    4278. -
    4279. Ridge Regression
    4280. -
    4281. Lasso Regression
    4282. -
    4283. Yet another Example
    4284. -
    4285. The OLS case
    4286. -
    4287. The Ridge case
    4288. -
    4289. Writing the Cost Function
    4290. -
    4291. Lasso case
    4292. -
    4293. The first Case
    4294. -
    4295. Simple code for solving the above problem
    4296. -
    4297. With Lasso Regression
    4298. -
    4299. Another Example, now with a polynomial fit
    4300. -
    4301. To think about, first part
    4302. -
    4303. More thinking
    4304. -
    4305. Still thinking
    4306. -
    4307. What does centering (subtracting the mean values) mean mathematically?
    4308. -
    4309. Further Manipulations
    4310. -
    4311. Wrapping it up
    4312. -
    4313. Linear Regression code, Intercept handling first
    4314. -
    4315. Code Examples
    4316. -
    4317. Taking out the mean
    4318. -
    4319. Friday September 9
    4320. -
    4321. Linking the regression analysis with a statistical interpretation
    4322. -
    4323. Assumptions made
    4324. -
    4325. Expectation value and variance
    4326. -
    4327. Expectation value and variance for \( \boldsymbol{\beta} \)
    4328. -
    4329. Deriving OLS from a probability distribution
    4330. -
    4331. Independent and Identically Distrubuted (iid)
    4332. -
    4333. Maximum Likelihood Estimation (MLE)
    4334. -
    4335. A new Cost Function
    4336. -
    4337. More basic Statistics and Bayes' theorem
    4338. -
    4339. Marginal Probability
    4340. -
    4341. Conditional Probability
    4342. -
    4343. Bayes' Theorem
    4344. -
    4345. Interpretations of Bayes' Theorem
    4346. -
    4347. Example of Usage of Bayes' theorem
    4348. -
    4349. Doing it correctly
    4350. -
    4351. Bayes' Theorem and Ridge and Lasso Regression
    4352. -
    4353. Test Function for what happens with OLS, Ridge and Lasso
    4354. -
    4355. Invoking Bayes' theorem
    4356. -
    4357. Ridge and Bayes
    4358. -
    4359. Lasso and Bayes
    4360. -
    4361. Exercise 1: mean values and variances in linear regression
    4362. -
    4363. Exercise 2: Adding Ridge and Lasso Regression
    4364. +
    4365. Summary from last Week and discussion of SVD, Ridge and Lasso regression with examples
    4366. +
    4367. Linear Regression and the SVD
    4368. +
    4369. What does it mean?
    4370. +
    4371. And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
    4372. +
    4373. Code for SVD and Inversion of Matrices
    4374. +
    4375. Inverse of Rectangular Matrix
    4376. +
    4377. Ridge and LASSO Regression
    4378. +
    4379. From OLS to Ridge and Lasso
    4380. +
    4381. Deriving the Ridge Regression Equations
    4382. +
    4383. Note on Scikit-Learn
    4384. +
    4385. Comparison with OLS
    4386. +
    4387. SVD analysis
    4388. +
    4389. Interpreting the Ridge results
    4390. +
    4391. More interpretations
    4392. +
    4393. Deriving the Lasso Regression Equations
    4394. +
    4395. Simple example to illustrate Ordinary Least Squares, Ridge and Lasso Regression
    4396. +
    4397. Ridge Regression
    4398. +
    4399. Lasso Regression
    4400. +
    4401. Yet another Example
    4402. +
    4403. The OLS case
    4404. +
    4405. The Ridge case
    4406. +
    4407. Writing the Cost Function
    4408. +
    4409. Lasso case
    4410. +
    4411. The first Case
    4412. +
    4413. Simple code for solving the above problem
    4414. +
    4415. With Lasso Regression
    4416. +
    4417. Another Example, now with a polynomial fit
    4418. +
    4419. To think about, first part
    4420. +
    4421. More thinking
    4422. +
    4423. Still thinking
    4424. +
    4425. What does centering (subtracting the mean values) mean mathematically?
    4426. +
    4427. Further Manipulations
    4428. +
    4429. Wrapping it up
    4430. +
    4431. Linear Regression code, Intercept handling first
    4432. +
    4433. Code Examples
    4434. +
    4435. Taking out the mean
    4436. +
    4437. Friday September 9
    4438. +
    4439. Linking the regression analysis with a statistical interpretation
    4440. +
    4441. Assumptions made
    4442. +
    4443. Expectation value and variance
    4444. +
    4445. Expectation value and variance for \( \boldsymbol{\beta} \)
    4446. +
    4447. Deriving OLS from a probability distribution
    4448. +
    4449. Independent and Identically Distrubuted (iid)
    4450. +
    4451. Maximum Likelihood Estimation (MLE)
    4452. +
    4453. A new Cost Function
    4454. +
    4455. More basic Statistics and Bayes' theorem
    4456. +
    4457. Marginal Probability
    4458. +
    4459. Conditional Probability
    4460. +
    4461. Bayes' Theorem
    4462. +
    4463. Interpretations of Bayes' Theorem
    4464. +
    4465. Example of Usage of Bayes' theorem
    4466. +
    4467. Doing it correctly
    4468. +
    4469. Bayes' Theorem and Ridge and Lasso Regression
    4470. +
    4471. Test Function for what happens with OLS, Ridge and Lasso
    4472. +
    4473. Invoking Bayes' theorem
    4474. +
    4475. Ridge and Bayes
    4476. +
    4477. Lasso and Bayes
    4478. +
    4479. Exercise 1: mean values and variances in linear regression
    4480. +
    4481. Exercise 2: Adding Ridge and Lasso Regression
    4482. @@ -297,18 +295,26 @@ MathJax.Hub.Config({

       

       

       

      -

      Ridge Regression

      +

      Lasso Regression

      -

      For Ridge regression our cost function is

      +

      For Lasso regression our cost function is

      $$ -C(\boldsymbol{\beta})=\sum_{i=0}^{p-1}(y_i-\beta_i)^2+\lambda\sum_{i=0}^{p-1}\beta_i^2, +C(\boldsymbol{\beta})=\sum_{i=0}^{p-1}(y_i-\beta_i)^2+\lambda\sum_{i=0}^{p-1}\vert\beta_i\vert=\sum_{i=0}^{p-1}(y_i-\beta_i)^2+\lambda\sum_{i=0}^{p-1}\sqrt{\beta_i^2}, $$

      and minimizing we have that

      $$ -\hat{\beta}_i^{\mathrm{Ridge}} = \frac{y_i}{1+\lambda}. +-2\sum_{i=0}^{p-1}(y_i-\beta_i)+\lambda \sum_{i=0}^{p-1}\frac{(\beta_i)}{\vert\beta_i\vert}=0, $$ +

      which leads to

      +$$ +\hat{\boldsymbol{\beta}}_i^{\mathrm{Lasso}} = \left\{\begin{array}{ccc}y_i-\frac{\lambda}{2} &\mathrm{if} & y_i> \frac{\lambda}{2}\\ + y_i+\frac{\lambda}{2} &\mathrm{if} & y_i < -\frac{\lambda}{2}\\ + 0 &\mathrm{if} & \vert y_i\vert\le \frac{\lambda}{2}\end{array}\right.\\. +$$ + +

      Plotting these results (figure in handwritten notes for week 36) shows clearly that Lasso regression suppresses (sets to zero) values of \( \beta_i \) for specific values of \( \lambda \). Ridge regression reduces on the other hand the values of \( \beta_i \) as function of \( \lambda \).

      @@ -335,7 +341,7 @@ $$

    4483. 28
    4484. 29
    4485. ...
    4486. -
    4487. 60
    4488. +
    4489. 59
    4490. »
    4491. diff --git a/doc/pub/week36/html/._week36-bs020.html b/doc/pub/week36/html/._week36-bs020.html index 97357fbec..1158ac94e 100644 --- a/doc/pub/week36/html/._week36-bs020.html +++ b/doc/pub/week36/html/._week36-bs020.html @@ -37,7 +37,6 @@ doconce format html week36.do.txt --html_style=bootstrap --pygments_html_style=d
    4492. Plans for week 36
    4493. -
    4494. Thursday September 8
    4495. -
    4496. Summary from last Week and discussion of SVD, Ridge and Lasso regression with examples
    4497. -
    4498. Linear Regression and the SVD
    4499. -
    4500. What does it mean?
    4501. -
    4502. And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
    4503. -
    4504. Code for SVD and Inversion of Matrices
    4505. -
    4506. Inverse of Rectangular Matrix
    4507. -
    4508. Ridge and LASSO Regression
    4509. -
    4510. From OLS to Ridge and Lasso
    4511. -
    4512. Deriving the Ridge Regression Equations
    4513. -
    4514. Note on Scikit-Learn
    4515. -
    4516. Comparison with OLS
    4517. -
    4518. SVD analysis
    4519. -
    4520. Interpreting the Ridge results
    4521. -
    4522. More interpretations
    4523. -
    4524. Deriving the Lasso Regression Equations
    4525. -
    4526. Simple example to illustrate Ordinary Least Squares, Ridge and Lasso Regression
    4527. -
    4528. Ridge Regression
    4529. -
    4530. Lasso Regression
    4531. -
    4532. Yet another Example
    4533. -
    4534. The OLS case
    4535. -
    4536. The Ridge case
    4537. -
    4538. Writing the Cost Function
    4539. -
    4540. Lasso case
    4541. -
    4542. The first Case
    4543. -
    4544. Simple code for solving the above problem
    4545. -
    4546. With Lasso Regression
    4547. -
    4548. Another Example, now with a polynomial fit
    4549. -
    4550. To think about, first part
    4551. -
    4552. More thinking
    4553. -
    4554. Still thinking
    4555. -
    4556. What does centering (subtracting the mean values) mean mathematically?
    4557. -
    4558. Further Manipulations
    4559. -
    4560. Wrapping it up
    4561. -
    4562. Linear Regression code, Intercept handling first
    4563. -
    4564. Code Examples
    4565. -
    4566. Taking out the mean
    4567. -
    4568. Friday September 9
    4569. -
    4570. Linking the regression analysis with a statistical interpretation
    4571. -
    4572. Assumptions made
    4573. -
    4574. Expectation value and variance
    4575. -
    4576. Expectation value and variance for \( \boldsymbol{\beta} \)
    4577. -
    4578. Deriving OLS from a probability distribution
    4579. -
    4580. Independent and Identically Distrubuted (iid)
    4581. -
    4582. Maximum Likelihood Estimation (MLE)
    4583. -
    4584. A new Cost Function
    4585. -
    4586. More basic Statistics and Bayes' theorem
    4587. -
    4588. Marginal Probability
    4589. -
    4590. Conditional Probability
    4591. -
    4592. Bayes' Theorem
    4593. -
    4594. Interpretations of Bayes' Theorem
    4595. -
    4596. Example of Usage of Bayes' theorem
    4597. -
    4598. Doing it correctly
    4599. -
    4600. Bayes' Theorem and Ridge and Lasso Regression
    4601. -
    4602. Test Function for what happens with OLS, Ridge and Lasso
    4603. -
    4604. Invoking Bayes' theorem
    4605. -
    4606. Ridge and Bayes
    4607. -
    4608. Lasso and Bayes
    4609. -
    4610. Exercise 1: mean values and variances in linear regression
    4611. -
    4612. Exercise 2: Adding Ridge and Lasso Regression
    4613. +
    4614. Summary from last Week and discussion of SVD, Ridge and Lasso regression with examples
    4615. +
    4616. Linear Regression and the SVD
    4617. +
    4618. What does it mean?
    4619. +
    4620. And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
    4621. +
    4622. Code for SVD and Inversion of Matrices
    4623. +
    4624. Inverse of Rectangular Matrix
    4625. +
    4626. Ridge and LASSO Regression
    4627. +
    4628. From OLS to Ridge and Lasso
    4629. +
    4630. Deriving the Ridge Regression Equations
    4631. +
    4632. Note on Scikit-Learn
    4633. +
    4634. Comparison with OLS
    4635. +
    4636. SVD analysis
    4637. +
    4638. Interpreting the Ridge results
    4639. +
    4640. More interpretations
    4641. +
    4642. Deriving the Lasso Regression Equations
    4643. +
    4644. Simple example to illustrate Ordinary Least Squares, Ridge and Lasso Regression
    4645. +
    4646. Ridge Regression
    4647. +
    4648. Lasso Regression
    4649. +
    4650. Yet another Example
    4651. +
    4652. The OLS case
    4653. +
    4654. The Ridge case
    4655. +
    4656. Writing the Cost Function
    4657. +
    4658. Lasso case
    4659. +
    4660. The first Case
    4661. +
    4662. Simple code for solving the above problem
    4663. +
    4664. With Lasso Regression
    4665. +
    4666. Another Example, now with a polynomial fit
    4667. +
    4668. To think about, first part
    4669. +
    4670. More thinking
    4671. +
    4672. Still thinking
    4673. +
    4674. What does centering (subtracting the mean values) mean mathematically?
    4675. +
    4676. Further Manipulations
    4677. +
    4678. Wrapping it up
    4679. +
    4680. Linear Regression code, Intercept handling first
    4681. +
    4682. Code Examples
    4683. +
    4684. Taking out the mean
    4685. +
    4686. Friday September 9
    4687. +
    4688. Linking the regression analysis with a statistical interpretation
    4689. +
    4690. Assumptions made
    4691. +
    4692. Expectation value and variance
    4693. +
    4694. Expectation value and variance for \( \boldsymbol{\beta} \)
    4695. +
    4696. Deriving OLS from a probability distribution
    4697. +
    4698. Independent and Identically Distrubuted (iid)
    4699. +
    4700. Maximum Likelihood Estimation (MLE)
    4701. +
    4702. A new Cost Function
    4703. +
    4704. More basic Statistics and Bayes' theorem
    4705. +
    4706. Marginal Probability
    4707. +
    4708. Conditional Probability
    4709. +
    4710. Bayes' Theorem
    4711. +
    4712. Interpretations of Bayes' Theorem
    4713. +
    4714. Example of Usage of Bayes' theorem
    4715. +
    4716. Doing it correctly
    4717. +
    4718. Bayes' Theorem and Ridge and Lasso Regression
    4719. +
    4720. Test Function for what happens with OLS, Ridge and Lasso
    4721. +
    4722. Invoking Bayes' theorem
    4723. +
    4724. Ridge and Bayes
    4725. +
    4726. Lasso and Bayes
    4727. +
    4728. Exercise 1: mean values and variances in linear regression
    4729. +
    4730. Exercise 2: Adding Ridge and Lasso Regression
    4731. @@ -297,26 +295,20 @@ MathJax.Hub.Config({

       

       

       

      -

      Lasso Regression

      +

      Yet another Example

      + +

      Let us assume we have a data set with outputs/targets given by the vector

      -

      For Lasso regression our cost function is

      $$ -C(\boldsymbol{\beta})=\sum_{i=0}^{p-1}(y_i-\beta_i)^2+\lambda\sum_{i=0}^{p-1}\vert\beta_i\vert=\sum_{i=0}^{p-1}(y_i-\beta_i)^2+\lambda\sum_{i=0}^{p-1}\sqrt{\beta_i^2}, +\boldsymbol{y}=\begin{bmatrix}4 \\ 2 \\3\end{bmatrix}, $$ -

      and minimizing we have that

      +

      and our inputs as a \( 3\times 2 \) design matrix

      $$ --2\sum_{i=0}^{p-1}(y_i-\beta_i)+\lambda \sum_{i=0}^{p-1}\frac{(\beta_i)}{\vert\beta_i\vert}=0, +\boldsymbol{X}=\begin{bmatrix}2 & 0\\ 0 & 1 \\ 0 & 0\end{bmatrix}, $$ -

      which leads to

      -$$ -\hat{\boldsymbol{\beta}}_i^{\mathrm{Lasso}} = \left\{\begin{array}{ccc}y_i-\frac{\lambda}{2} &\mathrm{if} & y_i> \frac{\lambda}{2}\\ - y_i+\frac{\lambda}{2} &\mathrm{if} & y_i < -\frac{\lambda}{2}\\ - 0 &\mathrm{if} & \vert y_i\vert\le \frac{\lambda}{2}\end{array}\right.\\. -$$ - -

      Plotting these results (figure in handwritten notes for week 36) shows clearly that Lasso regression suppresses (sets to zero) values of \( \beta_i \) for specific values of \( \lambda \). Ridge regression reduces on the other hand the values of \( \beta_i \) as function of \( \lambda \).

      +

      meaning that we have two features and two unknown parameters \( \beta_0 \) and \( \beta_1 \) to be determined either by ordinary least squares, Ridge or Lasso regression.

      @@ -343,7 +335,7 @@ $$

    4732. 29
    4733. 30
    4734. ...
    4735. -
    4736. 60
    4737. +
    4738. 59
    4739. »
    4740. diff --git a/doc/pub/week36/html/._week36-bs021.html b/doc/pub/week36/html/._week36-bs021.html index 2d2c24864..ce416353a 100644 --- a/doc/pub/week36/html/._week36-bs021.html +++ b/doc/pub/week36/html/._week36-bs021.html @@ -37,7 +37,6 @@ doconce format html week36.do.txt --html_style=bootstrap --pygments_html_style=d
    4741. Plans for week 36
    4742. -
    4743. Thursday September 8
    4744. -
    4745. Summary from last Week and discussion of SVD, Ridge and Lasso regression with examples
    4746. -
    4747. Linear Regression and the SVD
    4748. -
    4749. What does it mean?
    4750. -
    4751. And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
    4752. -
    4753. Code for SVD and Inversion of Matrices
    4754. -
    4755. Inverse of Rectangular Matrix
    4756. -
    4757. Ridge and LASSO Regression
    4758. -
    4759. From OLS to Ridge and Lasso
    4760. -
    4761. Deriving the Ridge Regression Equations
    4762. -
    4763. Note on Scikit-Learn
    4764. -
    4765. Comparison with OLS
    4766. -
    4767. SVD analysis
    4768. -
    4769. Interpreting the Ridge results
    4770. -
    4771. More interpretations
    4772. -
    4773. Deriving the Lasso Regression Equations
    4774. -
    4775. Simple example to illustrate Ordinary Least Squares, Ridge and Lasso Regression
    4776. -
    4777. Ridge Regression
    4778. -
    4779. Lasso Regression
    4780. -
    4781. Yet another Example
    4782. -
    4783. The OLS case
    4784. -
    4785. The Ridge case
    4786. -
    4787. Writing the Cost Function
    4788. -
    4789. Lasso case
    4790. -
    4791. The first Case
    4792. -
    4793. Simple code for solving the above problem
    4794. -
    4795. With Lasso Regression
    4796. -
    4797. Another Example, now with a polynomial fit
    4798. -
    4799. To think about, first part
    4800. -
    4801. More thinking
    4802. -
    4803. Still thinking
    4804. -
    4805. What does centering (subtracting the mean values) mean mathematically?
    4806. -
    4807. Further Manipulations
    4808. -
    4809. Wrapping it up
    4810. -
    4811. Linear Regression code, Intercept handling first
    4812. -
    4813. Code Examples
    4814. -
    4815. Taking out the mean
    4816. -
    4817. Friday September 9
    4818. -
    4819. Linking the regression analysis with a statistical interpretation
    4820. -
    4821. Assumptions made
    4822. -
    4823. Expectation value and variance
    4824. -
    4825. Expectation value and variance for \( \boldsymbol{\beta} \)
    4826. -
    4827. Deriving OLS from a probability distribution
    4828. -
    4829. Independent and Identically Distrubuted (iid)
    4830. -
    4831. Maximum Likelihood Estimation (MLE)
    4832. -
    4833. A new Cost Function
    4834. -
    4835. More basic Statistics and Bayes' theorem
    4836. -
    4837. Marginal Probability
    4838. -
    4839. Conditional Probability
    4840. -
    4841. Bayes' Theorem
    4842. -
    4843. Interpretations of Bayes' Theorem
    4844. -
    4845. Example of Usage of Bayes' theorem
    4846. -
    4847. Doing it correctly
    4848. -
    4849. Bayes' Theorem and Ridge and Lasso Regression
    4850. -
    4851. Test Function for what happens with OLS, Ridge and Lasso
    4852. -
    4853. Invoking Bayes' theorem
    4854. -
    4855. Ridge and Bayes
    4856. -
    4857. Lasso and Bayes
    4858. -
    4859. Exercise 1: mean values and variances in linear regression
    4860. -
    4861. Exercise 2: Adding Ridge and Lasso Regression
    4862. +
    4863. Summary from last Week and discussion of SVD, Ridge and Lasso regression with examples
    4864. +
    4865. Linear Regression and the SVD
    4866. +
    4867. What does it mean?
    4868. +
    4869. And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
    4870. +
    4871. Code for SVD and Inversion of Matrices
    4872. +
    4873. Inverse of Rectangular Matrix
    4874. +
    4875. Ridge and LASSO Regression
    4876. +
    4877. From OLS to Ridge and Lasso
    4878. +
    4879. Deriving the Ridge Regression Equations
    4880. +
    4881. Note on Scikit-Learn
    4882. +
    4883. Comparison with OLS
    4884. +
    4885. SVD analysis
    4886. +
    4887. Interpreting the Ridge results
    4888. +
    4889. More interpretations
    4890. +
    4891. Deriving the Lasso Regression Equations
    4892. +
    4893. Simple example to illustrate Ordinary Least Squares, Ridge and Lasso Regression
    4894. +
    4895. Ridge Regression
    4896. +
    4897. Lasso Regression
    4898. +
    4899. Yet another Example
    4900. +
    4901. The OLS case
    4902. +
    4903. The Ridge case
    4904. +
    4905. Writing the Cost Function
    4906. +
    4907. Lasso case
    4908. +
    4909. The first Case
    4910. +
    4911. Simple code for solving the above problem
    4912. +
    4913. With Lasso Regression
    4914. +
    4915. Another Example, now with a polynomial fit
    4916. +
    4917. To think about, first part
    4918. +
    4919. More thinking
    4920. +
    4921. Still thinking
    4922. +
    4923. What does centering (subtracting the mean values) mean mathematically?
    4924. +
    4925. Further Manipulations
    4926. +
    4927. Wrapping it up
    4928. +
    4929. Linear Regression code, Intercept handling first
    4930. +
    4931. Code Examples
    4932. +
    4933. Taking out the mean
    4934. +
    4935. Friday September 9
    4936. +
    4937. Linking the regression analysis with a statistical interpretation
    4938. +
    4939. Assumptions made
    4940. +
    4941. Expectation value and variance
    4942. +
    4943. Expectation value and variance for \( \boldsymbol{\beta} \)
    4944. +
    4945. Deriving OLS from a probability distribution
    4946. +
    4947. Independent and Identically Distrubuted (iid)
    4948. +
    4949. Maximum Likelihood Estimation (MLE)
    4950. +
    4951. A new Cost Function
    4952. +
    4953. More basic Statistics and Bayes' theorem
    4954. +
    4955. Marginal Probability
    4956. +
    4957. Conditional Probability
    4958. +
    4959. Bayes' Theorem
    4960. +
    4961. Interpretations of Bayes' Theorem
    4962. +
    4963. Example of Usage of Bayes' theorem
    4964. +
    4965. Doing it correctly
    4966. +
    4967. Bayes' Theorem and Ridge and Lasso Regression
    4968. +
    4969. Test Function for what happens with OLS, Ridge and Lasso
    4970. +
    4971. Invoking Bayes' theorem
    4972. +
    4973. Ridge and Bayes
    4974. +
    4975. Lasso and Bayes
    4976. +
    4977. Exercise 1: mean values and variances in linear regression
    4978. +
    4979. Exercise 2: Adding Ridge and Lasso Regression
    4980. @@ -297,20 +295,21 @@ MathJax.Hub.Config({

       

       

       

      -

      Yet another Example

      +

      The OLS case

      -

      Let us assume we have a data set with outputs/targets given by the vector

      +

      For ordinary least squares (OLS) we know that the optimal solution is

      $$ -\boldsymbol{y}=\begin{bmatrix}4 \\ 2 \\3\end{bmatrix}, +\hat{\boldsymbol{\beta}}^{\mathrm{OLS}}=\left( \boldsymbol{X}^T\boldsymbol{X}\right)^{-1}\boldsymbol{X}^T\boldsymbol{y}. $$ -

      and our inputs as a \( 3\times 2 \) design matrix

      +

      Inserting the above values we obtain that

      + $$ -\boldsymbol{X}=\begin{bmatrix}2 & 0\\ 0 & 1 \\ 0 & 0\end{bmatrix}, +\hat{\boldsymbol{\beta}}^{\mathrm{OLS}}=\begin{bmatrix}2 \\ 2\end{bmatrix}, $$ -

      meaning that we have two features and two unknown parameters \( \beta_0 \) and \( \beta_1 \) to be determined either by ordinary least squares, Ridge or Lasso regression.

      +

      The code which implements this simpler case is presented after the discussion of Ridge and Lasso.

      @@ -337,7 +336,7 @@ $$

    4981. 30
    4982. 31
    4983. ...
    4984. -
    4985. 60
    4986. +
    4987. 59
    4988. »
    4989. diff --git a/doc/pub/week36/html/._week36-bs022.html b/doc/pub/week36/html/._week36-bs022.html index 0136ae958..f074e3484 100644 --- a/doc/pub/week36/html/._week36-bs022.html +++ b/doc/pub/week36/html/._week36-bs022.html @@ -37,7 +37,6 @@ doconce format html week36.do.txt --html_style=bootstrap --pygments_html_style=d
    4990. Plans for week 36
    4991. -
    4992. Thursday September 8
    4993. -
    4994. Summary from last Week and discussion of SVD, Ridge and Lasso regression with examples
    4995. -
    4996. Linear Regression and the SVD
    4997. -
    4998. What does it mean?
    4999. -
    5000. And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
    5001. -
    5002. Code for SVD and Inversion of Matrices
    5003. -
    5004. Inverse of Rectangular Matrix
    5005. -
    5006. Ridge and LASSO Regression
    5007. -
    5008. From OLS to Ridge and Lasso
    5009. -
    5010. Deriving the Ridge Regression Equations
    5011. -
    5012. Note on Scikit-Learn
    5013. -
    5014. Comparison with OLS
    5015. -
    5016. SVD analysis
    5017. -
    5018. Interpreting the Ridge results
    5019. -
    5020. More interpretations
    5021. -
    5022. Deriving the Lasso Regression Equations
    5023. -
    5024. Simple example to illustrate Ordinary Least Squares, Ridge and Lasso Regression
    5025. -
    5026. Ridge Regression
    5027. -
    5028. Lasso Regression
    5029. -
    5030. Yet another Example
    5031. -
    5032. The OLS case
    5033. -
    5034. The Ridge case
    5035. -
    5036. Writing the Cost Function
    5037. -
    5038. Lasso case
    5039. -
    5040. The first Case
    5041. -
    5042. Simple code for solving the above problem
    5043. -
    5044. With Lasso Regression
    5045. -
    5046. Another Example, now with a polynomial fit
    5047. -
    5048. To think about, first part
    5049. -
    5050. More thinking
    5051. -
    5052. Still thinking
    5053. -
    5054. What does centering (subtracting the mean values) mean mathematically?
    5055. -
    5056. Further Manipulations
    5057. -
    5058. Wrapping it up
    5059. -
    5060. Linear Regression code, Intercept handling first
    5061. -
    5062. Code Examples
    5063. -
    5064. Taking out the mean
    5065. -
    5066. Friday September 9
    5067. -
    5068. Linking the regression analysis with a statistical interpretation
    5069. -
    5070. Assumptions made
    5071. -
    5072. Expectation value and variance
    5073. -
    5074. Expectation value and variance for \( \boldsymbol{\beta} \)
    5075. -
    5076. Deriving OLS from a probability distribution
    5077. -
    5078. Independent and Identically Distrubuted (iid)
    5079. -
    5080. Maximum Likelihood Estimation (MLE)
    5081. -
    5082. A new Cost Function
    5083. -
    5084. More basic Statistics and Bayes' theorem
    5085. -
    5086. Marginal Probability
    5087. -
    5088. Conditional Probability
    5089. -
    5090. Bayes' Theorem
    5091. -
    5092. Interpretations of Bayes' Theorem
    5093. -
    5094. Example of Usage of Bayes' theorem
    5095. -
    5096. Doing it correctly
    5097. -
    5098. Bayes' Theorem and Ridge and Lasso Regression
    5099. -
    5100. Test Function for what happens with OLS, Ridge and Lasso
    5101. -
    5102. Invoking Bayes' theorem
    5103. -
    5104. Ridge and Bayes
    5105. -
    5106. Lasso and Bayes
    5107. -
    5108. Exercise 1: mean values and variances in linear regression
    5109. -
    5110. Exercise 2: Adding Ridge and Lasso Regression
    5111. +
    5112. Summary from last Week and discussion of SVD, Ridge and Lasso regression with examples
    5113. +
    5114. Linear Regression and the SVD
    5115. +
    5116. What does it mean?
    5117. +
    5118. And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
    5119. +
    5120. Code for SVD and Inversion of Matrices
    5121. +
    5122. Inverse of Rectangular Matrix
    5123. +
    5124. Ridge and LASSO Regression
    5125. +
    5126. From OLS to Ridge and Lasso
    5127. +
    5128. Deriving the Ridge Regression Equations
    5129. +
    5130. Note on Scikit-Learn
    5131. +
    5132. Comparison with OLS
    5133. +
    5134. SVD analysis
    5135. +
    5136. Interpreting the Ridge results
    5137. +
    5138. More interpretations
    5139. +
    5140. Deriving the Lasso Regression Equations
    5141. +
    5142. Simple example to illustrate Ordinary Least Squares, Ridge and Lasso Regression
    5143. +
    5144. Ridge Regression
    5145. +
    5146. Lasso Regression
    5147. +
    5148. Yet another Example
    5149. +
    5150. The OLS case
    5151. +
    5152. The Ridge case
    5153. +
    5154. Writing the Cost Function
    5155. +
    5156. Lasso case
    5157. +
    5158. The first Case
    5159. +
    5160. Simple code for solving the above problem
    5161. +
    5162. With Lasso Regression
    5163. +
    5164. Another Example, now with a polynomial fit
    5165. +
    5166. To think about, first part
    5167. +
    5168. More thinking
    5169. +
    5170. Still thinking
    5171. +
    5172. What does centering (subtracting the mean values) mean mathematically?
    5173. +
    5174. Further Manipulations
    5175. +
    5176. Wrapping it up
    5177. +
    5178. Linear Regression code, Intercept handling first
    5179. +
    5180. Code Examples
    5181. +
    5182. Taking out the mean
    5183. +
    5184. Friday September 9
    5185. +
    5186. Linking the regression analysis with a statistical interpretation
    5187. +
    5188. Assumptions made
    5189. +
    5190. Expectation value and variance
    5191. +
    5192. Expectation value and variance for \( \boldsymbol{\beta} \)
    5193. +
    5194. Deriving OLS from a probability distribution
    5195. +
    5196. Independent and Identically Distrubuted (iid)
    5197. +
    5198. Maximum Likelihood Estimation (MLE)
    5199. +
    5200. A new Cost Function
    5201. +
    5202. More basic Statistics and Bayes' theorem
    5203. +
    5204. Marginal Probability
    5205. +
    5206. Conditional Probability
    5207. +
    5208. Bayes' Theorem
    5209. +
    5210. Interpretations of Bayes' Theorem
    5211. +
    5212. Example of Usage of Bayes' theorem
    5213. +
    5214. Doing it correctly
    5215. +
    5216. Bayes' Theorem and Ridge and Lasso Regression
    5217. +
    5218. Test Function for what happens with OLS, Ridge and Lasso
    5219. +
    5220. Invoking Bayes' theorem
    5221. +
    5222. Ridge and Bayes
    5223. +
    5224. Lasso and Bayes
    5225. +
    5226. Exercise 1: mean values and variances in linear regression
    5227. +
    5228. Exercise 2: Adding Ridge and Lasso Regression
    5229. @@ -297,21 +295,25 @@ MathJax.Hub.Config({

       

       

       

      -

      The OLS case

      +

      The Ridge case

      -

      For ordinary least squares (OLS) we know that the optimal solution is

      +

      For Ridge regression we have

      $$ -\hat{\boldsymbol{\beta}}^{\mathrm{OLS}}=\left( \boldsymbol{X}^T\boldsymbol{X}\right)^{-1}\boldsymbol{X}^T\boldsymbol{y}. +\hat{\boldsymbol{\beta}}^{\mathrm{Ridge}}=\left( \boldsymbol{X}^T\boldsymbol{X}+\lambda\boldsymbol{I}\right)^{-1}\boldsymbol{X}^T\boldsymbol{y}. $$

      Inserting the above values we obtain that

      $$ -\hat{\boldsymbol{\beta}}^{\mathrm{OLS}}=\begin{bmatrix}2 \\ 2\end{bmatrix}, +\hat{\boldsymbol{\beta}}^{\mathrm{Ridge}}=\begin{bmatrix}\frac{8}{4+\lambda} \\ \frac{2}{1+\lambda}\end{bmatrix}, $$ -

      The code which implements this simpler case is presented after the discussion of Ridge and Lasso.

      +

      There is normally a constraint on the value of \( \vert\vert \boldsymbol{\beta}\vert\vert_2 \) via the parameter \( \lambda \). +Let us for simplicity assume that \( \beta_0^2+\beta_1^2=1 \) as constraint. This will allow us to find an expression for the optimal values of \( \beta \) and \( \lambda \). +

      + +

      To see this, let us write the cost function for Ridge regression.

      @@ -338,7 +340,7 @@ $$

    5230. 31
    5231. 32
    5232. ...
    5233. -
    5234. 60
    5235. +
    5236. 59
    5237. »
    5238. diff --git a/doc/pub/week36/html/._week36-bs023.html b/doc/pub/week36/html/._week36-bs023.html index ebd843e94..3c8b0d906 100644 --- a/doc/pub/week36/html/._week36-bs023.html +++ b/doc/pub/week36/html/._week36-bs023.html @@ -37,7 +37,6 @@ doconce format html week36.do.txt --html_style=bootstrap --pygments_html_style=d
    5239. Plans for week 36
    5240. -
    5241. Thursday September 8
    5242. -
    5243. Summary from last Week and discussion of SVD, Ridge and Lasso regression with examples
    5244. -
    5245. Linear Regression and the SVD
    5246. -
    5247. What does it mean?
    5248. -
    5249. And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
    5250. -
    5251. Code for SVD and Inversion of Matrices
    5252. -
    5253. Inverse of Rectangular Matrix
    5254. -
    5255. Ridge and LASSO Regression
    5256. -
    5257. From OLS to Ridge and Lasso
    5258. -
    5259. Deriving the Ridge Regression Equations
    5260. -
    5261. Note on Scikit-Learn
    5262. -
    5263. Comparison with OLS
    5264. -
    5265. SVD analysis
    5266. -
    5267. Interpreting the Ridge results
    5268. -
    5269. More interpretations
    5270. -
    5271. Deriving the Lasso Regression Equations
    5272. -
    5273. Simple example to illustrate Ordinary Least Squares, Ridge and Lasso Regression
    5274. -
    5275. Ridge Regression
    5276. -
    5277. Lasso Regression
    5278. -
    5279. Yet another Example
    5280. -
    5281. The OLS case
    5282. -
    5283. The Ridge case
    5284. -
    5285. Writing the Cost Function
    5286. -
    5287. Lasso case
    5288. -
    5289. The first Case
    5290. -
    5291. Simple code for solving the above problem
    5292. -
    5293. With Lasso Regression
    5294. -
    5295. Another Example, now with a polynomial fit
    5296. -
    5297. To think about, first part
    5298. -
    5299. More thinking
    5300. -
    5301. Still thinking
    5302. -
    5303. What does centering (subtracting the mean values) mean mathematically?
    5304. -
    5305. Further Manipulations
    5306. -
    5307. Wrapping it up
    5308. -
    5309. Linear Regression code, Intercept handling first
    5310. -
    5311. Code Examples
    5312. -
    5313. Taking out the mean
    5314. -
    5315. Friday September 9
    5316. -
    5317. Linking the regression analysis with a statistical interpretation
    5318. -
    5319. Assumptions made
    5320. -
    5321. Expectation value and variance
    5322. -
    5323. Expectation value and variance for \( \boldsymbol{\beta} \)
    5324. -
    5325. Deriving OLS from a probability distribution
    5326. -
    5327. Independent and Identically Distrubuted (iid)
    5328. -
    5329. Maximum Likelihood Estimation (MLE)
    5330. -
    5331. A new Cost Function
    5332. -
    5333. More basic Statistics and Bayes' theorem
    5334. -
    5335. Marginal Probability
    5336. -
    5337. Conditional Probability
    5338. -
    5339. Bayes' Theorem
    5340. -
    5341. Interpretations of Bayes' Theorem
    5342. -
    5343. Example of Usage of Bayes' theorem
    5344. -
    5345. Doing it correctly
    5346. -
    5347. Bayes' Theorem and Ridge and Lasso Regression
    5348. -
    5349. Test Function for what happens with OLS, Ridge and Lasso
    5350. -
    5351. Invoking Bayes' theorem
    5352. -
    5353. Ridge and Bayes
    5354. -
    5355. Lasso and Bayes
    5356. -
    5357. Exercise 1: mean values and variances in linear regression
    5358. -
    5359. Exercise 2: Adding Ridge and Lasso Regression
    5360. +
    5361. Summary from last Week and discussion of SVD, Ridge and Lasso regression with examples
    5362. +
    5363. Linear Regression and the SVD
    5364. +
    5365. What does it mean?
    5366. +
    5367. And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
    5368. +
    5369. Code for SVD and Inversion of Matrices
    5370. +
    5371. Inverse of Rectangular Matrix
    5372. +
    5373. Ridge and LASSO Regression
    5374. +
    5375. From OLS to Ridge and Lasso
    5376. +
    5377. Deriving the Ridge Regression Equations
    5378. +
    5379. Note on Scikit-Learn
    5380. +
    5381. Comparison with OLS
    5382. +
    5383. SVD analysis
    5384. +
    5385. Interpreting the Ridge results
    5386. +
    5387. More interpretations
    5388. +
    5389. Deriving the Lasso Regression Equations
    5390. +
    5391. Simple example to illustrate Ordinary Least Squares, Ridge and Lasso Regression
    5392. +
    5393. Ridge Regression
    5394. +
    5395. Lasso Regression
    5396. +
    5397. Yet another Example
    5398. +
    5399. The OLS case
    5400. +
    5401. The Ridge case
    5402. +
    5403. Writing the Cost Function
    5404. +
    5405. Lasso case
    5406. +
    5407. The first Case
    5408. +
    5409. Simple code for solving the above problem
    5410. +
    5411. With Lasso Regression
    5412. +
    5413. Another Example, now with a polynomial fit
    5414. +
    5415. To think about, first part
    5416. +
    5417. More thinking
    5418. +
    5419. Still thinking
    5420. +
    5421. What does centering (subtracting the mean values) mean mathematically?
    5422. +
    5423. Further Manipulations
    5424. +
    5425. Wrapping it up
    5426. +
    5427. Linear Regression code, Intercept handling first
    5428. +
    5429. Code Examples
    5430. +
    5431. Taking out the mean
    5432. +
    5433. Friday September 9
    5434. +
    5435. Linking the regression analysis with a statistical interpretation
    5436. +
    5437. Assumptions made
    5438. +
    5439. Expectation value and variance
    5440. +
    5441. Expectation value and variance for \( \boldsymbol{\beta} \)
    5442. +
    5443. Deriving OLS from a probability distribution
    5444. +
    5445. Independent and Identically Distrubuted (iid)
    5446. +
    5447. Maximum Likelihood Estimation (MLE)
    5448. +
    5449. A new Cost Function
    5450. +
    5451. More basic Statistics and Bayes' theorem
    5452. +
    5453. Marginal Probability
    5454. +
    5455. Conditional Probability
    5456. +
    5457. Bayes' Theorem
    5458. +
    5459. Interpretations of Bayes' Theorem
    5460. +
    5461. Example of Usage of Bayes' theorem
    5462. +
    5463. Doing it correctly
    5464. +
    5465. Bayes' Theorem and Ridge and Lasso Regression
    5466. +
    5467. Test Function for what happens with OLS, Ridge and Lasso
    5468. +
    5469. Invoking Bayes' theorem
    5470. +
    5471. Ridge and Bayes
    5472. +
    5473. Lasso and Bayes
    5474. +
    5475. Exercise 1: mean values and variances in linear regression
    5476. +
    5477. Exercise 2: Adding Ridge and Lasso Regression
    5478. @@ -297,25 +295,34 @@ MathJax.Hub.Config({

       

       

       

      -

      The Ridge case

      - -

      For Ridge regression we have

      +

      Writing the Cost Function

      +

      We define the MSE without the \( 1/n \) factor and have then, using that

      $$ -\hat{\boldsymbol{\beta}}^{\mathrm{Ridge}}=\left( \boldsymbol{X}^T\boldsymbol{X}+\lambda\boldsymbol{I}\right)^{-1}\boldsymbol{X}^T\boldsymbol{y}. +\boldsymbol{X}\boldsymbol{\beta}=\begin{bmatrix} 2\beta_0 \\ \beta_1 \\0 \end{bmatrix}, $$ -

      Inserting the above values we obtain that

      $$ -\hat{\boldsymbol{\beta}}^{\mathrm{Ridge}}=\begin{bmatrix}\frac{8}{4+\lambda} \\ \frac{2}{1+\lambda}\end{bmatrix}, +C(\boldsymbol{\beta})=(4-2\beta_0)^2+(2-\beta_1)^2+\lambda(\beta_0^2+\beta_1^2), $$ -

      There is normally a constraint on the value of \( \vert\vert \boldsymbol{\beta}\vert\vert_2 \) via the parameter \( \lambda \). -Let us for simplicity assume that \( \beta_0^2+\beta_1^2=1 \) as constraint. This will allow us to find an expression for the optimal values of \( \beta \) and \( \lambda \). -

      +

      and taking the derivative with respect to \( \beta_0 \) we get

      +$$ +\beta_0=\frac{8}{4+\lambda}, +$$ -

      To see this, let us write the cost function for Ridge regression.

      +

      and for \( \beta_1 \) we obtain

      +$$ +\beta_1=\frac{2}{1+\lambda}, +$$ + +

      Using the constraint for \( \beta_0^2+\beta_1^2=1 \) we can constrain \( \lambda \) by solving

      +$$ +\left(\frac{8}{4+\lambda}\right)^2+\left(\frac{2}{1+\lambda}\right)^2=1, +$$ + +

      which gives \( \lambda=4.571 \) and \( \beta_0=0.933 \) and \( \beta_1=0.359 \).

      @@ -342,7 +349,7 @@ Let us for simplicity assume that \( \beta_0^2+\beta_1^2=1 \) as constraint. Thi

    5479. 32
    5480. 33
    5481. ...
    5482. -
    5483. 60
    5484. +
    5485. 59
    5486. »
    5487. diff --git a/doc/pub/week36/html/._week36-bs024.html b/doc/pub/week36/html/._week36-bs024.html index d8b66110d..312eb3626 100644 --- a/doc/pub/week36/html/._week36-bs024.html +++ b/doc/pub/week36/html/._week36-bs024.html @@ -37,7 +37,6 @@ doconce format html week36.do.txt --html_style=bootstrap --pygments_html_style=d
    5488. Plans for week 36
    5489. -
    5490. Thursday September 8
    5491. -
    5492. Summary from last Week and discussion of SVD, Ridge and Lasso regression with examples
    5493. -
    5494. Linear Regression and the SVD
    5495. -
    5496. What does it mean?
    5497. -
    5498. And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
    5499. -
    5500. Code for SVD and Inversion of Matrices
    5501. -
    5502. Inverse of Rectangular Matrix
    5503. -
    5504. Ridge and LASSO Regression
    5505. -
    5506. From OLS to Ridge and Lasso
    5507. -
    5508. Deriving the Ridge Regression Equations
    5509. -
    5510. Note on Scikit-Learn
    5511. -
    5512. Comparison with OLS
    5513. -
    5514. SVD analysis
    5515. -
    5516. Interpreting the Ridge results
    5517. -
    5518. More interpretations
    5519. -
    5520. Deriving the Lasso Regression Equations
    5521. -
    5522. Simple example to illustrate Ordinary Least Squares, Ridge and Lasso Regression
    5523. -
    5524. Ridge Regression
    5525. -
    5526. Lasso Regression
    5527. -
    5528. Yet another Example
    5529. -
    5530. The OLS case
    5531. -
    5532. The Ridge case
    5533. -
    5534. Writing the Cost Function
    5535. -
    5536. Lasso case
    5537. -
    5538. The first Case
    5539. -
    5540. Simple code for solving the above problem
    5541. -
    5542. With Lasso Regression
    5543. -
    5544. Another Example, now with a polynomial fit
    5545. -
    5546. To think about, first part
    5547. -
    5548. More thinking
    5549. -
    5550. Still thinking
    5551. -
    5552. What does centering (subtracting the mean values) mean mathematically?
    5553. -
    5554. Further Manipulations
    5555. -
    5556. Wrapping it up
    5557. -
    5558. Linear Regression code, Intercept handling first
    5559. -
    5560. Code Examples
    5561. -
    5562. Taking out the mean
    5563. -
    5564. Friday September 9
    5565. -
    5566. Linking the regression analysis with a statistical interpretation
    5567. -
    5568. Assumptions made
    5569. -
    5570. Expectation value and variance
    5571. -
    5572. Expectation value and variance for \( \boldsymbol{\beta} \)
    5573. -
    5574. Deriving OLS from a probability distribution
    5575. -
    5576. Independent and Identically Distrubuted (iid)
    5577. -
    5578. Maximum Likelihood Estimation (MLE)
    5579. -
    5580. A new Cost Function
    5581. -
    5582. More basic Statistics and Bayes' theorem
    5583. -
    5584. Marginal Probability
    5585. -
    5586. Conditional Probability
    5587. -
    5588. Bayes' Theorem
    5589. -
    5590. Interpretations of Bayes' Theorem
    5591. -
    5592. Example of Usage of Bayes' theorem
    5593. -
    5594. Doing it correctly
    5595. -
    5596. Bayes' Theorem and Ridge and Lasso Regression
    5597. -
    5598. Test Function for what happens with OLS, Ridge and Lasso
    5599. -
    5600. Invoking Bayes' theorem
    5601. -
    5602. Ridge and Bayes
    5603. -
    5604. Lasso and Bayes
    5605. -
    5606. Exercise 1: mean values and variances in linear regression
    5607. -
    5608. Exercise 2: Adding Ridge and Lasso Regression
    5609. +
    5610. Summary from last Week and discussion of SVD, Ridge and Lasso regression with examples
    5611. +
    5612. Linear Regression and the SVD
    5613. +
    5614. What does it mean?
    5615. +
    5616. And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
    5617. +
    5618. Code for SVD and Inversion of Matrices
    5619. +
    5620. Inverse of Rectangular Matrix
    5621. +
    5622. Ridge and LASSO Regression
    5623. +
    5624. From OLS to Ridge and Lasso
    5625. +
    5626. Deriving the Ridge Regression Equations
    5627. +
    5628. Note on Scikit-Learn
    5629. +
    5630. Comparison with OLS
    5631. +
    5632. SVD analysis
    5633. +
    5634. Interpreting the Ridge results
    5635. +
    5636. More interpretations
    5637. +
    5638. Deriving the Lasso Regression Equations
    5639. +
    5640. Simple example to illustrate Ordinary Least Squares, Ridge and Lasso Regression
    5641. +
    5642. Ridge Regression
    5643. +
    5644. Lasso Regression
    5645. +
    5646. Yet another Example
    5647. +
    5648. The OLS case
    5649. +
    5650. The Ridge case
    5651. +
    5652. Writing the Cost Function
    5653. +
    5654. Lasso case
    5655. +
    5656. The first Case
    5657. +
    5658. Simple code for solving the above problem
    5659. +
    5660. With Lasso Regression
    5661. +
    5662. Another Example, now with a polynomial fit
    5663. +
    5664. To think about, first part
    5665. +
    5666. More thinking
    5667. +
    5668. Still thinking
    5669. +
    5670. What does centering (subtracting the mean values) mean mathematically?
    5671. +
    5672. Further Manipulations
    5673. +
    5674. Wrapping it up
    5675. +
    5676. Linear Regression code, Intercept handling first
    5677. +
    5678. Code Examples
    5679. +
    5680. Taking out the mean
    5681. +
    5682. Friday September 9
    5683. +
    5684. Linking the regression analysis with a statistical interpretation
    5685. +
    5686. Assumptions made
    5687. +
    5688. Expectation value and variance
    5689. +
    5690. Expectation value and variance for \( \boldsymbol{\beta} \)
    5691. +
    5692. Deriving OLS from a probability distribution
    5693. +
    5694. Independent and Identically Distrubuted (iid)
    5695. +
    5696. Maximum Likelihood Estimation (MLE)
    5697. +
    5698. A new Cost Function
    5699. +
    5700. More basic Statistics and Bayes' theorem
    5701. +
    5702. Marginal Probability
    5703. +
    5704. Conditional Probability
    5705. +
    5706. Bayes' Theorem
    5707. +
    5708. Interpretations of Bayes' Theorem
    5709. +
    5710. Example of Usage of Bayes' theorem
    5711. +
    5712. Doing it correctly
    5713. +
    5714. Bayes' Theorem and Ridge and Lasso Regression
    5715. +
    5716. Test Function for what happens with OLS, Ridge and Lasso
    5717. +
    5718. Invoking Bayes' theorem
    5719. +
    5720. Ridge and Bayes
    5721. +
    5722. Lasso and Bayes
    5723. +
    5724. Exercise 1: mean values and variances in linear regression
    5725. +
    5726. Exercise 2: Adding Ridge and Lasso Regression
    5727. @@ -297,35 +295,32 @@ MathJax.Hub.Config({

       

       

       

      -

      Writing the Cost Function

      +

      Lasso case

      -

      We define the MSE without the \( 1/n \) factor and have then, using that

      +

      For Lasso we need now, keeping a constraint on \( \vert\beta_0\vert+\vert\beta_1\vert=1 \), to take the derivative of the absolute values of \( \beta_0 \) +and \( \beta_1 \). This gives us the following derivatives of the cost function +

      $$ -\boldsymbol{X}\boldsymbol{\beta}=\begin{bmatrix} 2\beta_0 \\ \beta_1 \\0 \end{bmatrix}, +C(\boldsymbol{\beta})=(4-2\beta_0)^2+(2-\beta_1)^2+\lambda(\vert\beta_0\vert+\vert\beta_1\vert), $$ $$ -C(\boldsymbol{\beta})=(4-2\beta_0)^2+(2-\beta_1)^2+\lambda(\beta_0^2+\beta_1^2), +\frac{\partial C(\boldsymbol{\beta})}{\partial \beta_0}=-4(4-2\beta_0)+\lambda\mathrm{sgn}(\beta_0)=0, $$ -

      and taking the derivative with respect to \( \beta_0 \) we get

      +

      and

      $$ -\beta_0=\frac{8}{4+\lambda}, +\frac{\partial C(\boldsymbol{\beta})}{\partial \beta_1}=-2(2-\beta_1)+\lambda\mathrm{sgn}(\beta_1)=0. $$ -

      and for \( \beta_1 \) we obtain

      -$$ -\beta_1=\frac{2}{1+\lambda}, -$$ - -

      Using the constraint for \( \beta_0^2+\beta_1^2=1 \) we can constrain \( \lambda \) by solving

      -$$ -\left(\frac{8}{4+\lambda}\right)^2+\left(\frac{2}{1+\lambda}\right)^2=1, -$$ - -

      which gives \( \lambda=4.571 \) and \( \beta_0=0.933 \) and \( \beta_1=0.359 \).

      - +

      We have now four cases to solve besides the trivial cases \( \beta_0 \) and/or \( \beta_1 \) are zero, namely

      +
        +
      1. \( \beta_0 > 0 \) and \( \beta_1 > 0 \),
      2. +
      3. \( \beta_0 > 0 \) and \( \beta_1 < 0 \),
      4. +
      5. \( \beta_0 < 0 \) and \( \beta_1 > 0 \),
      6. +
      7. \( \beta_0 < 0 \) and \( \beta_1 < 0 \).
      8. +

      diff --git a/doc/pub/week36/html/._week36-bs025.html b/doc/pub/week36/html/._week36-bs025.html index ba60331b2..69a2195ae 100644 --- a/doc/pub/week36/html/._week36-bs025.html +++ b/doc/pub/week36/html/._week36-bs025.html @@ -37,7 +37,6 @@ doconce format html week36.do.txt --html_style=bootstrap --pygments_html_style=d
    5728. Plans for week 36
    5729. -
    5730. Thursday September 8
    5731. -
    5732. Summary from last Week and discussion of SVD, Ridge and Lasso regression with examples
    5733. -
    5734. Linear Regression and the SVD
    5735. -
    5736. What does it mean?
    5737. -
    5738. And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
    5739. -
    5740. Code for SVD and Inversion of Matrices
    5741. -
    5742. Inverse of Rectangular Matrix
    5743. -
    5744. Ridge and LASSO Regression
    5745. -
    5746. From OLS to Ridge and Lasso
    5747. -
    5748. Deriving the Ridge Regression Equations
    5749. -
    5750. Note on Scikit-Learn
    5751. -
    5752. Comparison with OLS
    5753. -
    5754. SVD analysis
    5755. -
    5756. Interpreting the Ridge results
    5757. -
    5758. More interpretations
    5759. -
    5760. Deriving the Lasso Regression Equations
    5761. -
    5762. Simple example to illustrate Ordinary Least Squares, Ridge and Lasso Regression
    5763. -
    5764. Ridge Regression
    5765. -
    5766. Lasso Regression
    5767. -
    5768. Yet another Example
    5769. -
    5770. The OLS case
    5771. -
    5772. The Ridge case
    5773. -
    5774. Writing the Cost Function
    5775. -
    5776. Lasso case
    5777. -
    5778. The first Case
    5779. -
    5780. Simple code for solving the above problem
    5781. -
    5782. With Lasso Regression
    5783. -
    5784. Another Example, now with a polynomial fit
    5785. -
    5786. To think about, first part
    5787. -
    5788. More thinking
    5789. -
    5790. Still thinking
    5791. -
    5792. What does centering (subtracting the mean values) mean mathematically?
    5793. -
    5794. Further Manipulations
    5795. -
    5796. Wrapping it up
    5797. -
    5798. Linear Regression code, Intercept handling first
    5799. -
    5800. Code Examples
    5801. -
    5802. Taking out the mean
    5803. -
    5804. Friday September 9
    5805. -
    5806. Linking the regression analysis with a statistical interpretation
    5807. -
    5808. Assumptions made
    5809. -
    5810. Expectation value and variance
    5811. -
    5812. Expectation value and variance for \( \boldsymbol{\beta} \)
    5813. -
    5814. Deriving OLS from a probability distribution
    5815. -
    5816. Independent and Identically Distrubuted (iid)
    5817. -
    5818. Maximum Likelihood Estimation (MLE)
    5819. -
    5820. A new Cost Function
    5821. -
    5822. More basic Statistics and Bayes' theorem
    5823. -
    5824. Marginal Probability
    5825. -
    5826. Conditional Probability
    5827. -
    5828. Bayes' Theorem
    5829. -
    5830. Interpretations of Bayes' Theorem
    5831. -
    5832. Example of Usage of Bayes' theorem
    5833. -
    5834. Doing it correctly
    5835. -
    5836. Bayes' Theorem and Ridge and Lasso Regression
    5837. -
    5838. Test Function for what happens with OLS, Ridge and Lasso
    5839. -
    5840. Invoking Bayes' theorem
    5841. -
    5842. Ridge and Bayes
    5843. -
    5844. Lasso and Bayes
    5845. -
    5846. Exercise 1: mean values and variances in linear regression
    5847. -
    5848. Exercise 2: Adding Ridge and Lasso Regression
    5849. +
    5850. Summary from last Week and discussion of SVD, Ridge and Lasso regression with examples
    5851. +
    5852. Linear Regression and the SVD
    5853. +
    5854. What does it mean?
    5855. +
    5856. And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
    5857. +
    5858. Code for SVD and Inversion of Matrices
    5859. +
    5860. Inverse of Rectangular Matrix
    5861. +
    5862. Ridge and LASSO Regression
    5863. +
    5864. From OLS to Ridge and Lasso
    5865. +
    5866. Deriving the Ridge Regression Equations
    5867. +
    5868. Note on Scikit-Learn
    5869. +
    5870. Comparison with OLS
    5871. +
    5872. SVD analysis
    5873. +
    5874. Interpreting the Ridge results
    5875. +
    5876. More interpretations
    5877. +
    5878. Deriving the Lasso Regression Equations
    5879. +
    5880. Simple example to illustrate Ordinary Least Squares, Ridge and Lasso Regression
    5881. +
    5882. Ridge Regression
    5883. +
    5884. Lasso Regression
    5885. +
    5886. Yet another Example
    5887. +
    5888. The OLS case
    5889. +
    5890. The Ridge case
    5891. +
    5892. Writing the Cost Function
    5893. +
    5894. Lasso case
    5895. +
    5896. The first Case
    5897. +
    5898. Simple code for solving the above problem
    5899. +
    5900. With Lasso Regression
    5901. +
    5902. Another Example, now with a polynomial fit
    5903. +
    5904. To think about, first part
    5905. +
    5906. More thinking
    5907. +
    5908. Still thinking
    5909. +
    5910. What does centering (subtracting the mean values) mean mathematically?
    5911. +
    5912. Further Manipulations
    5913. +
    5914. Wrapping it up
    5915. +
    5916. Linear Regression code, Intercept handling first
    5917. +
    5918. Code Examples
    5919. +
    5920. Taking out the mean
    5921. +
    5922. Friday September 9
    5923. +
    5924. Linking the regression analysis with a statistical interpretation
    5925. +
    5926. Assumptions made
    5927. +
    5928. Expectation value and variance
    5929. +
    5930. Expectation value and variance for \( \boldsymbol{\beta} \)
    5931. +
    5932. Deriving OLS from a probability distribution
    5933. +
    5934. Independent and Identically Distrubuted (iid)
    5935. +
    5936. Maximum Likelihood Estimation (MLE)
    5937. +
    5938. A new Cost Function
    5939. +
    5940. More basic Statistics and Bayes' theorem
    5941. +
    5942. Marginal Probability
    5943. +
    5944. Conditional Probability
    5945. +
    5946. Bayes' Theorem
    5947. +
    5948. Interpretations of Bayes' Theorem
    5949. +
    5950. Example of Usage of Bayes' theorem
    5951. +
    5952. Doing it correctly
    5953. +
    5954. Bayes' Theorem and Ridge and Lasso Regression
    5955. +
    5956. Test Function for what happens with OLS, Ridge and Lasso
    5957. +
    5958. Invoking Bayes' theorem
    5959. +
    5960. Ridge and Bayes
    5961. +
    5962. Lasso and Bayes
    5963. +
    5964. Exercise 1: mean values and variances in linear regression
    5965. +
    5966. Exercise 2: Adding Ridge and Lasso Regression
    5967. @@ -297,32 +295,31 @@ MathJax.Hub.Config({

       

       

       

      -

      Lasso case

      +

      The first Case

      -

      For Lasso we need now, keeping a constraint on \( \vert\beta_0\vert+\vert\beta_1\vert=1 \), to take the derivative of the absolute values of \( \beta_0 \) -and \( \beta_1 \). This gives us the following derivatives of the cost function -

      +

      If we consider the first case, we have then

      $$ -C(\boldsymbol{\beta})=(4-2\beta_0)^2+(2-\beta_1)^2+\lambda(\vert\beta_0\vert+\vert\beta_1\vert), -$$ - - -$$ -\frac{\partial C(\boldsymbol{\beta})}{\partial \beta_0}=-4(4-2\beta_0)+\lambda\mathrm{sgn}(\beta_0)=0, +-4(4-2\beta_0)+\lambda=0, $$

      and

      $$ -\frac{\partial C(\boldsymbol{\beta})}{\partial \beta_1}=-2(2-\beta_1)+\lambda\mathrm{sgn}(\beta_1)=0. +-2(2-\beta_1)+\lambda=0. $$ -

      We have now four cases to solve besides the trivial cases \( \beta_0 \) and/or \( \beta_1 \) are zero, namely

      -
        -
      1. \( \beta_0 > 0 \) and \( \beta_1 > 0 \),
      2. -
      3. \( \beta_0 > 0 \) and \( \beta_1 < 0 \),
      4. -
      5. \( \beta_0 < 0 \) and \( \beta_1 > 0 \),
      6. -
      7. \( \beta_0 < 0 \) and \( \beta_1 < 0 \).
      8. -
      +

      which yields

      + +$$ +\beta_0=\frac{16+\lambda}{8}, +$$ + +

      and

      +$$ +\beta_1=\frac{4+\lambda}{2}. +$$ + +

      Using the constraint on \( \beta_0 \) and \( \beta_1 \) we can then find the optimal value of \( \lambda \) for the different cases. We leave this as an exercise to you.

      +

      diff --git a/doc/pub/week36/html/._week36-bs026.html b/doc/pub/week36/html/._week36-bs026.html index 06abdeea0..f294e6484 100644 --- a/doc/pub/week36/html/._week36-bs026.html +++ b/doc/pub/week36/html/._week36-bs026.html @@ -37,7 +37,6 @@ doconce format html week36.do.txt --html_style=bootstrap --pygments_html_style=d
    5968. Plans for week 36
    5969. -
    5970. Thursday September 8
    5971. -
    5972. Summary from last Week and discussion of SVD, Ridge and Lasso regression with examples
    5973. -
    5974. Linear Regression and the SVD
    5975. -
    5976. What does it mean?
    5977. -
    5978. And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
    5979. -
    5980. Code for SVD and Inversion of Matrices
    5981. -
    5982. Inverse of Rectangular Matrix
    5983. -
    5984. Ridge and LASSO Regression
    5985. -
    5986. From OLS to Ridge and Lasso
    5987. -
    5988. Deriving the Ridge Regression Equations
    5989. -
    5990. Note on Scikit-Learn
    5991. -
    5992. Comparison with OLS
    5993. -
    5994. SVD analysis
    5995. -
    5996. Interpreting the Ridge results
    5997. -
    5998. More interpretations
    5999. -
    6000. Deriving the Lasso Regression Equations
    6001. -
    6002. Simple example to illustrate Ordinary Least Squares, Ridge and Lasso Regression
    6003. -
    6004. Ridge Regression
    6005. -
    6006. Lasso Regression
    6007. -
    6008. Yet another Example
    6009. -
    6010. The OLS case
    6011. -
    6012. The Ridge case
    6013. -
    6014. Writing the Cost Function
    6015. -
    6016. Lasso case
    6017. -
    6018. The first Case
    6019. -
    6020. Simple code for solving the above problem
    6021. -
    6022. With Lasso Regression
    6023. -
    6024. Another Example, now with a polynomial fit
    6025. -
    6026. To think about, first part
    6027. -
    6028. More thinking
    6029. -
    6030. Still thinking
    6031. -
    6032. What does centering (subtracting the mean values) mean mathematically?
    6033. -
    6034. Further Manipulations
    6035. -
    6036. Wrapping it up
    6037. -
    6038. Linear Regression code, Intercept handling first
    6039. -
    6040. Code Examples
    6041. -
    6042. Taking out the mean
    6043. -
    6044. Friday September 9
    6045. -
    6046. Linking the regression analysis with a statistical interpretation
    6047. -
    6048. Assumptions made
    6049. -
    6050. Expectation value and variance
    6051. -
    6052. Expectation value and variance for \( \boldsymbol{\beta} \)
    6053. -
    6054. Deriving OLS from a probability distribution
    6055. -
    6056. Independent and Identically Distrubuted (iid)
    6057. -
    6058. Maximum Likelihood Estimation (MLE)
    6059. -
    6060. A new Cost Function
    6061. -
    6062. More basic Statistics and Bayes' theorem
    6063. -
    6064. Marginal Probability
    6065. -
    6066. Conditional Probability
    6067. -
    6068. Bayes' Theorem
    6069. -
    6070. Interpretations of Bayes' Theorem
    6071. -
    6072. Example of Usage of Bayes' theorem
    6073. -
    6074. Doing it correctly
    6075. -
    6076. Bayes' Theorem and Ridge and Lasso Regression
    6077. -
    6078. Test Function for what happens with OLS, Ridge and Lasso
    6079. -
    6080. Invoking Bayes' theorem
    6081. -
    6082. Ridge and Bayes
    6083. -
    6084. Lasso and Bayes
    6085. -
    6086. Exercise 1: mean values and variances in linear regression
    6087. -
    6088. Exercise 2: Adding Ridge and Lasso Regression
    6089. +
    6090. Summary from last Week and discussion of SVD, Ridge and Lasso regression with examples
    6091. +
    6092. Linear Regression and the SVD
    6093. +
    6094. What does it mean?
    6095. +
    6096. And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
    6097. +
    6098. Code for SVD and Inversion of Matrices
    6099. +
    6100. Inverse of Rectangular Matrix
    6101. +
    6102. Ridge and LASSO Regression
    6103. +
    6104. From OLS to Ridge and Lasso
    6105. +
    6106. Deriving the Ridge Regression Equations
    6107. +
    6108. Note on Scikit-Learn
    6109. +
    6110. Comparison with OLS
    6111. +
    6112. SVD analysis
    6113. +
    6114. Interpreting the Ridge results
    6115. +
    6116. More interpretations
    6117. +
    6118. Deriving the Lasso Regression Equations
    6119. +
    6120. Simple example to illustrate Ordinary Least Squares, Ridge and Lasso Regression
    6121. +
    6122. Ridge Regression
    6123. +
    6124. Lasso Regression
    6125. +
    6126. Yet another Example
    6127. +
    6128. The OLS case
    6129. +
    6130. The Ridge case
    6131. +
    6132. Writing the Cost Function
    6133. +
    6134. Lasso case
    6135. +
    6136. The first Case
    6137. +
    6138. Simple code for solving the above problem
    6139. +
    6140. With Lasso Regression
    6141. +
    6142. Another Example, now with a polynomial fit
    6143. +
    6144. To think about, first part
    6145. +
    6146. More thinking
    6147. +
    6148. Still thinking
    6149. +
    6150. What does centering (subtracting the mean values) mean mathematically?
    6151. +
    6152. Further Manipulations
    6153. +
    6154. Wrapping it up
    6155. +
    6156. Linear Regression code, Intercept handling first
    6157. +
    6158. Code Examples
    6159. +
    6160. Taking out the mean
    6161. +
    6162. Friday September 9
    6163. +
    6164. Linking the regression analysis with a statistical interpretation
    6165. +
    6166. Assumptions made
    6167. +
    6168. Expectation value and variance
    6169. +
    6170. Expectation value and variance for \( \boldsymbol{\beta} \)
    6171. +
    6172. Deriving OLS from a probability distribution
    6173. +
    6174. Independent and Identically Distrubuted (iid)
    6175. +
    6176. Maximum Likelihood Estimation (MLE)
    6177. +
    6178. A new Cost Function
    6179. +
    6180. More basic Statistics and Bayes' theorem
    6181. +
    6182. Marginal Probability
    6183. +
    6184. Conditional Probability
    6185. +
    6186. Bayes' Theorem
    6187. +
    6188. Interpretations of Bayes' Theorem
    6189. +
    6190. Example of Usage of Bayes' theorem
    6191. +
    6192. Doing it correctly
    6193. +
    6194. Bayes' Theorem and Ridge and Lasso Regression
    6195. +
    6196. Test Function for what happens with OLS, Ridge and Lasso
    6197. +
    6198. Invoking Bayes' theorem
    6199. +
    6200. Ridge and Bayes
    6201. +
    6202. Lasso and Bayes
    6203. +
    6204. Exercise 1: mean values and variances in linear regression
    6205. +
    6206. Exercise 2: Adding Ridge and Lasso Regression
    6207. @@ -297,30 +295,84 @@ MathJax.Hub.Config({

       

       

       

      -

      The first Case

      +

      Simple code for solving the above problem

      -

      If we consider the first case, we have then

      -$$ --4(4-2\beta_0)+\lambda=0, -$$ +

      Here we set up the OLS, Ridge and Lasso functionality in order to study the above example. Note that here we have opted for a set of values of \( \lambda \), meaning that we need to perform a search in order to find the optimal values.

      -

      and

      -$$ --2(2-\beta_1)+\lambda=0. -$$ +

      First we study and compare the OLS and Ridge results. The next code compares all three methods.

      -

      which yields

      -$$ -\beta_0=\frac{16+\lambda}{8}, -$$ + +
      +
      +
      +
      +
      +
      import os
      +import numpy as np
      +import pandas as pd
      +import matplotlib.pyplot as plt
       
      -

      and

      -$$ -\beta_1=\frac{4+\lambda}{2}. -$$ +def R2(y_data, y_model): + return 1 - np.sum((y_data - y_model) ** 2) / np.sum((y_data - np.mean(y_data)) ** 2) +def MSE(y_data,y_model): + n = np.size(y_model) + return np.sum((y_data-y_model)**2)/n -

      Using the constraint on \( \beta_0 \) and \( \beta_1 \) we can then find the optimal value of \( \lambda \) for the different cases. We leave this as an exercise to you.

      + +# A seed just to ensure that the random numbers are the same for every run. +# Useful for eventual debugging. + +X = np.array( [ [ 2, 0], [0, 1], [0,0]]) +y = np.array( [4, 2, 3]) + + +# matrix inversion to find beta +OLSbeta = np.linalg.inv(X.T @ X) @ X.T @ y +print(OLSbeta) +# and then make the prediction +ytildeOLS = X @ OLSbeta +print("Training MSE for OLS") +print(MSE(y,ytildeOLS)) +ypredictOLS = X @ OLSbeta + +# Repeat now for Ridge regression and various values of the regularization parameter +I = np.eye(2,2) +# Decide which values of lambda to use +nlambdas = 100 +MSEPredict = np.zeros(nlambdas) +lambdas = np.logspace(-4, 4, nlambdas) +for i in range(nlambdas): + lmb = lambdas[i] + Ridgebeta = np.linalg.inv(X.T @ X+lmb*I) @ X.T @ y +# print(Ridgebeta) + # and then make the prediction + ypredictRidge = X @ Ridgebeta + MSEPredict[i] = MSE(y,ypredictRidge) +# print(MSEPredict[i]) + # Now plot the results +plt.figure() +plt.plot(np.log10(lambdas), MSEPredict, 'r--', label = 'MSE Ridge Train') +plt.xlabel('log10(lambda)') +plt.ylabel('MSE') +plt.legend() +plt.show() +
      +
      +
      +
      +
      +
      +
      +
      +
      +
      +
      +
      +
      +
      + +

      We see here that we reach a plateau. What is actually happening?

      @@ -347,7 +399,7 @@ $$

    6208. 35
    6209. 36
    6210. ...
    6211. -
    6212. 60
    6213. +
    6214. 59
    6215. »
    6216. diff --git a/doc/pub/week36/html/._week36-bs027.html b/doc/pub/week36/html/._week36-bs027.html index f4c61bc49..e01086037 100644 --- a/doc/pub/week36/html/._week36-bs027.html +++ b/doc/pub/week36/html/._week36-bs027.html @@ -37,7 +37,6 @@ doconce format html week36.do.txt --html_style=bootstrap --pygments_html_style=d
    6217. Plans for week 36
    6218. -
    6219. Thursday September 8
    6220. -
    6221. Summary from last Week and discussion of SVD, Ridge and Lasso regression with examples
    6222. -
    6223. Linear Regression and the SVD
    6224. -
    6225. What does it mean?
    6226. -
    6227. And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
    6228. -
    6229. Code for SVD and Inversion of Matrices
    6230. -
    6231. Inverse of Rectangular Matrix
    6232. -
    6233. Ridge and LASSO Regression
    6234. -
    6235. From OLS to Ridge and Lasso
    6236. -
    6237. Deriving the Ridge Regression Equations
    6238. -
    6239. Note on Scikit-Learn
    6240. -
    6241. Comparison with OLS
    6242. -
    6243. SVD analysis
    6244. -
    6245. Interpreting the Ridge results
    6246. -
    6247. More interpretations
    6248. -
    6249. Deriving the Lasso Regression Equations
    6250. -
    6251. Simple example to illustrate Ordinary Least Squares, Ridge and Lasso Regression
    6252. -
    6253. Ridge Regression
    6254. -
    6255. Lasso Regression
    6256. -
    6257. Yet another Example
    6258. -
    6259. The OLS case
    6260. -
    6261. The Ridge case
    6262. -
    6263. Writing the Cost Function
    6264. -
    6265. Lasso case
    6266. -
    6267. The first Case
    6268. -
    6269. Simple code for solving the above problem
    6270. -
    6271. With Lasso Regression
    6272. -
    6273. Another Example, now with a polynomial fit
    6274. -
    6275. To think about, first part
    6276. -
    6277. More thinking
    6278. -
    6279. Still thinking
    6280. -
    6281. What does centering (subtracting the mean values) mean mathematically?
    6282. -
    6283. Further Manipulations
    6284. -
    6285. Wrapping it up
    6286. -
    6287. Linear Regression code, Intercept handling first
    6288. -
    6289. Code Examples
    6290. -
    6291. Taking out the mean
    6292. -
    6293. Friday September 9
    6294. -
    6295. Linking the regression analysis with a statistical interpretation
    6296. -
    6297. Assumptions made
    6298. -
    6299. Expectation value and variance
    6300. -
    6301. Expectation value and variance for \( \boldsymbol{\beta} \)
    6302. -
    6303. Deriving OLS from a probability distribution
    6304. -
    6305. Independent and Identically Distrubuted (iid)
    6306. -
    6307. Maximum Likelihood Estimation (MLE)
    6308. -
    6309. A new Cost Function
    6310. -
    6311. More basic Statistics and Bayes' theorem
    6312. -
    6313. Marginal Probability
    6314. -
    6315. Conditional Probability
    6316. -
    6317. Bayes' Theorem
    6318. -
    6319. Interpretations of Bayes' Theorem
    6320. -
    6321. Example of Usage of Bayes' theorem
    6322. -
    6323. Doing it correctly
    6324. -
    6325. Bayes' Theorem and Ridge and Lasso Regression
    6326. -
    6327. Test Function for what happens with OLS, Ridge and Lasso
    6328. -
    6329. Invoking Bayes' theorem
    6330. -
    6331. Ridge and Bayes
    6332. -
    6333. Lasso and Bayes
    6334. -
    6335. Exercise 1: mean values and variances in linear regression
    6336. -
    6337. Exercise 2: Adding Ridge and Lasso Regression
    6338. +
    6339. Summary from last Week and discussion of SVD, Ridge and Lasso regression with examples
    6340. +
    6341. Linear Regression and the SVD
    6342. +
    6343. What does it mean?
    6344. +
    6345. And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
    6346. +
    6347. Code for SVD and Inversion of Matrices
    6348. +
    6349. Inverse of Rectangular Matrix
    6350. +
    6351. Ridge and LASSO Regression
    6352. +
    6353. From OLS to Ridge and Lasso
    6354. +
    6355. Deriving the Ridge Regression Equations
    6356. +
    6357. Note on Scikit-Learn
    6358. +
    6359. Comparison with OLS
    6360. +
    6361. SVD analysis
    6362. +
    6363. Interpreting the Ridge results
    6364. +
    6365. More interpretations
    6366. +
    6367. Deriving the Lasso Regression Equations
    6368. +
    6369. Simple example to illustrate Ordinary Least Squares, Ridge and Lasso Regression
    6370. +
    6371. Ridge Regression
    6372. +
    6373. Lasso Regression
    6374. +
    6375. Yet another Example
    6376. +
    6377. The OLS case
    6378. +
    6379. The Ridge case
    6380. +
    6381. Writing the Cost Function
    6382. +
    6383. Lasso case
    6384. +
    6385. The first Case
    6386. +
    6387. Simple code for solving the above problem
    6388. +
    6389. With Lasso Regression
    6390. +
    6391. Another Example, now with a polynomial fit
    6392. +
    6393. To think about, first part
    6394. +
    6395. More thinking
    6396. +
    6397. Still thinking
    6398. +
    6399. What does centering (subtracting the mean values) mean mathematically?
    6400. +
    6401. Further Manipulations
    6402. +
    6403. Wrapping it up
    6404. +
    6405. Linear Regression code, Intercept handling first
    6406. +
    6407. Code Examples
    6408. +
    6409. Taking out the mean
    6410. +
    6411. Friday September 9
    6412. +
    6413. Linking the regression analysis with a statistical interpretation
    6414. +
    6415. Assumptions made
    6416. +
    6417. Expectation value and variance
    6418. +
    6419. Expectation value and variance for \( \boldsymbol{\beta} \)
    6420. +
    6421. Deriving OLS from a probability distribution
    6422. +
    6423. Independent and Identically Distrubuted (iid)
    6424. +
    6425. Maximum Likelihood Estimation (MLE)
    6426. +
    6427. A new Cost Function
    6428. +
    6429. More basic Statistics and Bayes' theorem
    6430. +
    6431. Marginal Probability
    6432. +
    6433. Conditional Probability
    6434. +
    6435. Bayes' Theorem
    6436. +
    6437. Interpretations of Bayes' Theorem
    6438. +
    6439. Example of Usage of Bayes' theorem
    6440. +
    6441. Doing it correctly
    6442. +
    6443. Bayes' Theorem and Ridge and Lasso Regression
    6444. +
    6445. Test Function for what happens with OLS, Ridge and Lasso
    6446. +
    6447. Invoking Bayes' theorem
    6448. +
    6449. Ridge and Bayes
    6450. +
    6451. Lasso and Bayes
    6452. +
    6453. Exercise 1: mean values and variances in linear regression
    6454. +
    6455. Exercise 2: Adding Ridge and Lasso Regression
    6456. @@ -297,11 +295,7 @@ MathJax.Hub.Config({

       

       

       

      -

      Simple code for solving the above problem

      - -

      Here we set up the OLS, Ridge and Lasso functionality in order to study the above example. Note that here we have opted for a set of values of \( \lambda \), meaning that we need to perform a search in order to find the optimal values.

      - -

      First we study and compare the OLS and Ridge results. The next code compares all three methods.

      +

      With Lasso Regression

      @@ -314,6 +308,7 @@ MathJax.Hub.Config({ import numpy as np import pandas as pd import matplotlib.pyplot as plt +from sklearn import linear_model def R2(y_data, y_model): return 1 - np.sum((y_data - y_model) ** 2) / np.sum((y_data - np.mean(y_data)) ** 2) @@ -342,19 +337,25 @@ ypredictOLS = X = np.eye(2,2) # Decide which values of lambda to use nlambdas = 100 -MSEPredict = np.zeros(nlambdas) +MSERidgePredict = np.zeros(nlambdas) +MSELassoPredict = np.zeros(nlambdas) lambdas = np.logspace(-4, 4, nlambdas) for i in range(nlambdas): lmb = lambdas[i] Ridgebeta = np.linalg.inv(X.T @ X+lmb*I) @ X.T @ y -# print(Ridgebeta) + print(Ridgebeta) # and then make the prediction ypredictRidge = X @ Ridgebeta - MSEPredict[i] = MSE(y,ypredictRidge) -# print(MSEPredict[i]) - # Now plot the results + MSERidgePredict[i] = MSE(y,ypredictRidge) + RegLasso = linear_model.Lasso(lmb) + RegLasso.fit(X,y) + ypredictLasso = RegLasso.predict(X) + print(RegLasso.coef_) + MSELassoPredict[i] = MSE(y,ypredictLasso) +# Now plot the results plt.figure() -plt.plot(np.log10(lambdas), MSEPredict, 'r--', label = 'MSE Ridge Train') +plt.plot(np.log10(lambdas), MSERidgePredict, 'r--', label = 'MSE Ridge Train') +plt.plot(np.log10(lambdas), MSELassoPredict, 'r--', label = 'MSE Lasso Train') plt.xlabel('log10(lambda)') plt.ylabel('MSE') plt.legend() @@ -374,7 +375,6 @@ plt.show() -

      We see here that we reach a plateau. What is actually happening?

      @@ -401,7 +401,7 @@ plt.show()

    6457. 36
    6458. 37
    6459. ...
    6460. -
    6461. 60
    6462. +
    6463. 59
    6464. »
    6465. diff --git a/doc/pub/week36/html/._week36-bs028.html b/doc/pub/week36/html/._week36-bs028.html index 6a12b712a..6dec31562 100644 --- a/doc/pub/week36/html/._week36-bs028.html +++ b/doc/pub/week36/html/._week36-bs028.html @@ -37,7 +37,6 @@ doconce format html week36.do.txt --html_style=bootstrap --pygments_html_style=d
    6466. Plans for week 36
    6467. -
    6468. Thursday September 8
    6469. -
    6470. Summary from last Week and discussion of SVD, Ridge and Lasso regression with examples
    6471. -
    6472. Linear Regression and the SVD
    6473. -
    6474. What does it mean?
    6475. -
    6476. And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
    6477. -
    6478. Code for SVD and Inversion of Matrices
    6479. -
    6480. Inverse of Rectangular Matrix
    6481. -
    6482. Ridge and LASSO Regression
    6483. -
    6484. From OLS to Ridge and Lasso
    6485. -
    6486. Deriving the Ridge Regression Equations
    6487. -
    6488. Note on Scikit-Learn
    6489. -
    6490. Comparison with OLS
    6491. -
    6492. SVD analysis
    6493. -
    6494. Interpreting the Ridge results
    6495. -
    6496. More interpretations
    6497. -
    6498. Deriving the Lasso Regression Equations
    6499. -
    6500. Simple example to illustrate Ordinary Least Squares, Ridge and Lasso Regression
    6501. -
    6502. Ridge Regression
    6503. -
    6504. Lasso Regression
    6505. -
    6506. Yet another Example
    6507. -
    6508. The OLS case
    6509. -
    6510. The Ridge case
    6511. -
    6512. Writing the Cost Function
    6513. -
    6514. Lasso case
    6515. -
    6516. The first Case
    6517. -
    6518. Simple code for solving the above problem
    6519. -
    6520. With Lasso Regression
    6521. -
    6522. Another Example, now with a polynomial fit
    6523. -
    6524. To think about, first part
    6525. -
    6526. More thinking
    6527. -
    6528. Still thinking
    6529. -
    6530. What does centering (subtracting the mean values) mean mathematically?
    6531. -
    6532. Further Manipulations
    6533. -
    6534. Wrapping it up
    6535. -
    6536. Linear Regression code, Intercept handling first
    6537. -
    6538. Code Examples
    6539. -
    6540. Taking out the mean
    6541. -
    6542. Friday September 9
    6543. -
    6544. Linking the regression analysis with a statistical interpretation
    6545. -
    6546. Assumptions made
    6547. -
    6548. Expectation value and variance
    6549. -
    6550. Expectation value and variance for \( \boldsymbol{\beta} \)
    6551. -
    6552. Deriving OLS from a probability distribution
    6553. -
    6554. Independent and Identically Distrubuted (iid)
    6555. -
    6556. Maximum Likelihood Estimation (MLE)
    6557. -
    6558. A new Cost Function
    6559. -
    6560. More basic Statistics and Bayes' theorem
    6561. -
    6562. Marginal Probability
    6563. -
    6564. Conditional Probability
    6565. -
    6566. Bayes' Theorem
    6567. -
    6568. Interpretations of Bayes' Theorem
    6569. -
    6570. Example of Usage of Bayes' theorem
    6571. -
    6572. Doing it correctly
    6573. -
    6574. Bayes' Theorem and Ridge and Lasso Regression
    6575. -
    6576. Test Function for what happens with OLS, Ridge and Lasso
    6577. -
    6578. Invoking Bayes' theorem
    6579. -
    6580. Ridge and Bayes
    6581. -
    6582. Lasso and Bayes
    6583. -
    6584. Exercise 1: mean values and variances in linear regression
    6585. -
    6586. Exercise 2: Adding Ridge and Lasso Regression
    6587. +
    6588. Summary from last Week and discussion of SVD, Ridge and Lasso regression with examples
    6589. +
    6590. Linear Regression and the SVD
    6591. +
    6592. What does it mean?
    6593. +
    6594. And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
    6595. +
    6596. Code for SVD and Inversion of Matrices
    6597. +
    6598. Inverse of Rectangular Matrix
    6599. +
    6600. Ridge and LASSO Regression
    6601. +
    6602. From OLS to Ridge and Lasso
    6603. +
    6604. Deriving the Ridge Regression Equations
    6605. +
    6606. Note on Scikit-Learn
    6607. +
    6608. Comparison with OLS
    6609. +
    6610. SVD analysis
    6611. +
    6612. Interpreting the Ridge results
    6613. +
    6614. More interpretations
    6615. +
    6616. Deriving the Lasso Regression Equations
    6617. +
    6618. Simple example to illustrate Ordinary Least Squares, Ridge and Lasso Regression
    6619. +
    6620. Ridge Regression
    6621. +
    6622. Lasso Regression
    6623. +
    6624. Yet another Example
    6625. +
    6626. The OLS case
    6627. +
    6628. The Ridge case
    6629. +
    6630. Writing the Cost Function
    6631. +
    6632. Lasso case
    6633. +
    6634. The first Case
    6635. +
    6636. Simple code for solving the above problem
    6637. +
    6638. With Lasso Regression
    6639. +
    6640. Another Example, now with a polynomial fit
    6641. +
    6642. To think about, first part
    6643. +
    6644. More thinking
    6645. +
    6646. Still thinking
    6647. +
    6648. What does centering (subtracting the mean values) mean mathematically?
    6649. +
    6650. Further Manipulations
    6651. +
    6652. Wrapping it up
    6653. +
    6654. Linear Regression code, Intercept handling first
    6655. +
    6656. Code Examples
    6657. +
    6658. Taking out the mean
    6659. +
    6660. Friday September 9
    6661. +
    6662. Linking the regression analysis with a statistical interpretation
    6663. +
    6664. Assumptions made
    6665. +
    6666. Expectation value and variance
    6667. +
    6668. Expectation value and variance for \( \boldsymbol{\beta} \)
    6669. +
    6670. Deriving OLS from a probability distribution
    6671. +
    6672. Independent and Identically Distrubuted (iid)
    6673. +
    6674. Maximum Likelihood Estimation (MLE)
    6675. +
    6676. A new Cost Function
    6677. +
    6678. More basic Statistics and Bayes' theorem
    6679. +
    6680. Marginal Probability
    6681. +
    6682. Conditional Probability
    6683. +
    6684. Bayes' Theorem
    6685. +
    6686. Interpretations of Bayes' Theorem
    6687. +
    6688. Example of Usage of Bayes' theorem
    6689. +
    6690. Doing it correctly
    6691. +
    6692. Bayes' Theorem and Ridge and Lasso Regression
    6693. +
    6694. Test Function for what happens with OLS, Ridge and Lasso
    6695. +
    6696. Invoking Bayes' theorem
    6697. +
    6698. Ridge and Bayes
    6699. +
    6700. Lasso and Bayes
    6701. +
    6702. Exercise 1: mean values and variances in linear regression
    6703. +
    6704. Exercise 2: Adding Ridge and Lasso Regression
    6705. @@ -297,7 +295,7 @@ MathJax.Hub.Config({

       

       

       

      -

      With Lasso Regression

      +

      Another Example, now with a polynomial fit

      @@ -310,6 +308,7 @@ MathJax.Hub.Config({ import numpy as np import pandas as pd import matplotlib.pyplot as plt +from sklearn.model_selection import train_test_split from sklearn import linear_model def R2(y_data, y_model): @@ -321,43 +320,64 @@ MathJax.Hub.Config({ # A seed just to ensure that the random numbers are the same for every run. # Useful for eventual debugging. +np.random.seed(3155) -X = np.array( [ [ 2, 0], [0, 1], [0,0]]) -y = np.array( [4, 2, 3]) +x = np.random.rand(100) +y = 2.0+5*x*x+0.1*np.random.randn(100) +# number of features p (here degree of polynomial +p = 3 +# The design matrix now as function of a given polynomial +X = np.zeros((len(x),p)) +X[:,0] = 1.0 +X[:,1] = x +X[:,2] = x*x +# We split the data in test and training data +X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2) # matrix inversion to find beta -OLSbeta = np.linalg.inv(X.T @ X) @ X.T @ y +OLSbeta = np.linalg.inv(X_train.T @ X_train) @ X_train.T @ y_train print(OLSbeta) # and then make the prediction -ytildeOLS = X @ OLSbeta +ytildeOLS = X_train @ OLSbeta print("Training MSE for OLS") -print(MSE(y,ytildeOLS)) -ypredictOLS = X @ OLSbeta +print(MSE(y_train,ytildeOLS)) +ypredictOLS = X_test @ OLSbeta +print("Test MSE OLS") +print(MSE(y_test,ypredictOLS)) -# Repeat now for Ridge regression and various values of the regularization parameter -I = np.eye(2,2) +# Repeat now for Lasso and Ridge regression and various values of the regularization parameter +I = np.eye(p,p) # Decide which values of lambda to use nlambdas = 100 -MSERidgePredict = np.zeros(nlambdas) +MSEPredict = np.zeros(nlambdas) +MSETrain = np.zeros(nlambdas) MSELassoPredict = np.zeros(nlambdas) +MSELassoTrain = np.zeros(nlambdas) lambdas = np.logspace(-4, 4, nlambdas) for i in range(nlambdas): lmb = lambdas[i] - Ridgebeta = np.linalg.inv(X.T @ X+lmb*I) @ X.T @ y - print(Ridgebeta) - # and then make the prediction - ypredictRidge = X @ Ridgebeta - MSERidgePredict[i] = MSE(y,ypredictRidge) + Ridgebeta = np.linalg.inv(X_train.T @ X_train+lmb*I) @ X_train.T @ y_train + # include lasso using Scikit-Learn RegLasso = linear_model.Lasso(lmb) - RegLasso.fit(X,y) - ypredictLasso = RegLasso.predict(X) - print(RegLasso.coef_) - MSELassoPredict[i] = MSE(y,ypredictLasso) + RegLasso.fit(X_train,y_train) + # and then make the prediction + ytildeRidge = X_train @ Ridgebeta + ypredictRidge = X_test @ Ridgebeta + ytildeLasso = RegLasso.predict(X_train) + ypredictLasso = RegLasso.predict(X_test) + MSEPredict[i] = MSE(y_test,ypredictRidge) + MSETrain[i] = MSE(y_train,ytildeRidge) + MSELassoPredict[i] = MSE(y_test,ypredictLasso) + MSELassoTrain[i] = MSE(y_train,ytildeLasso) + # Now plot the results plt.figure() -plt.plot(np.log10(lambdas), MSERidgePredict, 'r--', label = 'MSE Ridge Train') -plt.plot(np.log10(lambdas), MSELassoPredict, 'r--', label = 'MSE Lasso Train') +plt.plot(np.log10(lambdas), MSETrain, label = 'MSE Ridge train') +plt.plot(np.log10(lambdas), MSEPredict, 'r--', label = 'MSE Ridge Test') +plt.plot(np.log10(lambdas), MSELassoTrain, label = 'MSE Lasso train') +plt.plot(np.log10(lambdas), MSELassoPredict, 'r--', label = 'MSE Lasso Test') + plt.xlabel('log10(lambda)') plt.ylabel('MSE') plt.legend() @@ -403,7 +423,7 @@ plt.show()
    6706. 37
    6707. 38
    6708. ...
    6709. -
    6710. 60
    6711. +
    6712. 59
    6713. »
    6714. diff --git a/doc/pub/week36/html/._week36-bs029.html b/doc/pub/week36/html/._week36-bs029.html index 1d0525f83..c649c8408 100644 --- a/doc/pub/week36/html/._week36-bs029.html +++ b/doc/pub/week36/html/._week36-bs029.html @@ -37,7 +37,6 @@ doconce format html week36.do.txt --html_style=bootstrap --pygments_html_style=d
    6715. Plans for week 36
    6716. -
    6717. Thursday September 8
    6718. -
    6719. Summary from last Week and discussion of SVD, Ridge and Lasso regression with examples
    6720. -
    6721. Linear Regression and the SVD
    6722. -
    6723. What does it mean?
    6724. -
    6725. And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
    6726. -
    6727. Code for SVD and Inversion of Matrices
    6728. -
    6729. Inverse of Rectangular Matrix
    6730. -
    6731. Ridge and LASSO Regression
    6732. -
    6733. From OLS to Ridge and Lasso
    6734. -
    6735. Deriving the Ridge Regression Equations
    6736. -
    6737. Note on Scikit-Learn
    6738. -
    6739. Comparison with OLS
    6740. -
    6741. SVD analysis
    6742. -
    6743. Interpreting the Ridge results
    6744. -
    6745. More interpretations
    6746. -
    6747. Deriving the Lasso Regression Equations
    6748. -
    6749. Simple example to illustrate Ordinary Least Squares, Ridge and Lasso Regression
    6750. -
    6751. Ridge Regression
    6752. -
    6753. Lasso Regression
    6754. -
    6755. Yet another Example
    6756. -
    6757. The OLS case
    6758. -
    6759. The Ridge case
    6760. -
    6761. Writing the Cost Function
    6762. -
    6763. Lasso case
    6764. -
    6765. The first Case
    6766. -
    6767. Simple code for solving the above problem
    6768. -
    6769. With Lasso Regression
    6770. -
    6771. Another Example, now with a polynomial fit
    6772. -
    6773. To think about, first part
    6774. -
    6775. More thinking
    6776. -
    6777. Still thinking
    6778. -
    6779. What does centering (subtracting the mean values) mean mathematically?
    6780. -
    6781. Further Manipulations
    6782. -
    6783. Wrapping it up
    6784. -
    6785. Linear Regression code, Intercept handling first
    6786. -
    6787. Code Examples
    6788. -
    6789. Taking out the mean
    6790. -
    6791. Friday September 9
    6792. -
    6793. Linking the regression analysis with a statistical interpretation
    6794. -
    6795. Assumptions made
    6796. -
    6797. Expectation value and variance
    6798. -
    6799. Expectation value and variance for \( \boldsymbol{\beta} \)
    6800. -
    6801. Deriving OLS from a probability distribution
    6802. -
    6803. Independent and Identically Distrubuted (iid)
    6804. -
    6805. Maximum Likelihood Estimation (MLE)
    6806. -
    6807. A new Cost Function
    6808. -
    6809. More basic Statistics and Bayes' theorem
    6810. -
    6811. Marginal Probability
    6812. -
    6813. Conditional Probability
    6814. -
    6815. Bayes' Theorem
    6816. -
    6817. Interpretations of Bayes' Theorem
    6818. -
    6819. Example of Usage of Bayes' theorem
    6820. -
    6821. Doing it correctly
    6822. -
    6823. Bayes' Theorem and Ridge and Lasso Regression
    6824. -
    6825. Test Function for what happens with OLS, Ridge and Lasso
    6826. -
    6827. Invoking Bayes' theorem
    6828. -
    6829. Ridge and Bayes
    6830. -
    6831. Lasso and Bayes
    6832. -
    6833. Exercise 1: mean values and variances in linear regression
    6834. -
    6835. Exercise 2: Adding Ridge and Lasso Regression
    6836. +
    6837. Summary from last Week and discussion of SVD, Ridge and Lasso regression with examples
    6838. +
    6839. Linear Regression and the SVD
    6840. +
    6841. What does it mean?
    6842. +
    6843. And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
    6844. +
    6845. Code for SVD and Inversion of Matrices
    6846. +
    6847. Inverse of Rectangular Matrix
    6848. +
    6849. Ridge and LASSO Regression
    6850. +
    6851. From OLS to Ridge and Lasso
    6852. +
    6853. Deriving the Ridge Regression Equations
    6854. +
    6855. Note on Scikit-Learn
    6856. +
    6857. Comparison with OLS
    6858. +
    6859. SVD analysis
    6860. +
    6861. Interpreting the Ridge results
    6862. +
    6863. More interpretations
    6864. +
    6865. Deriving the Lasso Regression Equations
    6866. +
    6867. Simple example to illustrate Ordinary Least Squares, Ridge and Lasso Regression
    6868. +
    6869. Ridge Regression
    6870. +
    6871. Lasso Regression
    6872. +
    6873. Yet another Example
    6874. +
    6875. The OLS case
    6876. +
    6877. The Ridge case
    6878. +
    6879. Writing the Cost Function
    6880. +
    6881. Lasso case
    6882. +
    6883. The first Case
    6884. +
    6885. Simple code for solving the above problem
    6886. +
    6887. With Lasso Regression
    6888. +
    6889. Another Example, now with a polynomial fit
    6890. +
    6891. To think about, first part
    6892. +
    6893. More thinking
    6894. +
    6895. Still thinking
    6896. +
    6897. What does centering (subtracting the mean values) mean mathematically?
    6898. +
    6899. Further Manipulations
    6900. +
    6901. Wrapping it up
    6902. +
    6903. Linear Regression code, Intercept handling first
    6904. +
    6905. Code Examples
    6906. +
    6907. Taking out the mean
    6908. +
    6909. Friday September 9
    6910. +
    6911. Linking the regression analysis with a statistical interpretation
    6912. +
    6913. Assumptions made
    6914. +
    6915. Expectation value and variance
    6916. +
    6917. Expectation value and variance for \( \boldsymbol{\beta} \)
    6918. +
    6919. Deriving OLS from a probability distribution
    6920. +
    6921. Independent and Identically Distrubuted (iid)
    6922. +
    6923. Maximum Likelihood Estimation (MLE)
    6924. +
    6925. A new Cost Function
    6926. +
    6927. More basic Statistics and Bayes' theorem
    6928. +
    6929. Marginal Probability
    6930. +
    6931. Conditional Probability
    6932. +
    6933. Bayes' Theorem
    6934. +
    6935. Interpretations of Bayes' Theorem
    6936. +
    6937. Example of Usage of Bayes' theorem
    6938. +
    6939. Doing it correctly
    6940. +
    6941. Bayes' Theorem and Ridge and Lasso Regression
    6942. +
    6943. Test Function for what happens with OLS, Ridge and Lasso
    6944. +
    6945. Invoking Bayes' theorem
    6946. +
    6947. Ridge and Bayes
    6948. +
    6949. Lasso and Bayes
    6950. +
    6951. Exercise 1: mean values and variances in linear regression
    6952. +
    6953. Exercise 2: Adding Ridge and Lasso Regression
    6954. @@ -297,108 +295,28 @@ MathJax.Hub.Config({

       

       

       

      -

      Another Example, now with a polynomial fit

      +

      To think about, first part

      +

      When you are comparing your own code with for example Scikit-Learn's +library, there are some technicalities to keep in mind. The examples +here demonstrate some of these aspects with potential pitfalls. +

      - -
      -
      -
      -
      -
      -
      import os
      -import numpy as np
      -import pandas as pd
      -import matplotlib.pyplot as plt
      -from sklearn.model_selection import train_test_split
      -from sklearn import linear_model
      -
      -def R2(y_data, y_model):
      -    return 1 - np.sum((y_data - y_model) ** 2) / np.sum((y_data - np.mean(y_data)) ** 2)
      -def MSE(y_data,y_model):
      -    n = np.size(y_model)
      -    return np.sum((y_data-y_model)**2)/n
      -
      -
      -# A seed just to ensure that the random numbers are the same for every run.
      -# Useful for eventual debugging.
      -np.random.seed(3155)
      -
      -x = np.random.rand(100)
      -y = 2.0+5*x*x+0.1*np.random.randn(100)
      -
      -# number of features p (here degree of polynomial
      -p = 3
      -#  The design matrix now as function of a given polynomial
      -X = np.zeros((len(x),p))
      -X[:,0] = 1.0
      -X[:,1] = x
      -X[:,2] = x*x
      -# We split the data in test and training data
      -X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
      -
      -# matrix inversion to find beta
      -OLSbeta = np.linalg.inv(X_train.T @ X_train) @ X_train.T @ y_train
      -print(OLSbeta)
      -# and then make the prediction
      -ytildeOLS = X_train @ OLSbeta
      -print("Training MSE for OLS")
      -print(MSE(y_train,ytildeOLS))
      -ypredictOLS = X_test @ OLSbeta
      -print("Test MSE OLS")
      -print(MSE(y_test,ypredictOLS))
      -
      -# Repeat now for Lasso and Ridge regression and various values of the regularization parameter
      -I = np.eye(p,p)
      -# Decide which values of lambda to use
      -nlambdas = 100
      -MSEPredict = np.zeros(nlambdas)
      -MSETrain = np.zeros(nlambdas)
      -MSELassoPredict = np.zeros(nlambdas)
      -MSELassoTrain = np.zeros(nlambdas)
      -lambdas = np.logspace(-4, 4, nlambdas)
      -for i in range(nlambdas):
      -    lmb = lambdas[i]
      -    Ridgebeta = np.linalg.inv(X_train.T @ X_train+lmb*I) @ X_train.T @ y_train
      -    # include lasso using Scikit-Learn
      -    RegLasso = linear_model.Lasso(lmb)
      -    RegLasso.fit(X_train,y_train)
      -    # and then make the prediction
      -    ytildeRidge = X_train @ Ridgebeta
      -    ypredictRidge = X_test @ Ridgebeta
      -    ytildeLasso = RegLasso.predict(X_train)
      -    ypredictLasso = RegLasso.predict(X_test)
      -    MSEPredict[i] = MSE(y_test,ypredictRidge)
      -    MSETrain[i] = MSE(y_train,ytildeRidge)
      -    MSELassoPredict[i] = MSE(y_test,ypredictLasso)
      -    MSELassoTrain[i] = MSE(y_train,ytildeLasso)
      -
      -# Now plot the results
      -plt.figure()
      -plt.plot(np.log10(lambdas), MSETrain, label = 'MSE Ridge train')
      -plt.plot(np.log10(lambdas), MSEPredict, 'r--', label = 'MSE Ridge Test')
      -plt.plot(np.log10(lambdas), MSELassoTrain, label = 'MSE Lasso train')
      -plt.plot(np.log10(lambdas), MSELassoPredict, 'r--', label = 'MSE Lasso Test')
      -
      -plt.xlabel('log10(lambda)')
      -plt.ylabel('MSE')
      -plt.legend()
      -plt.show()
      -
      -
      -
      -
      -
      -
      -
      -
      -
      -
      -
      -
      -
      -
      +

      The discussion here focuses on the role of the intercept, how we can +set up the design matrix, what scaling we should use and other topics +which tend confuse us. +

      +

      The intercept can be interpreted as the expected value of our +target/output variables when all other predictors are set to zero. +Thus, if we cannot assume that the expected outputs/targets are zero +when all predictors are zero (the columns in the design matrix), it +may be a bad idea to implement a model which penalizes the intercept. +Furthermore, in for example Ridge and Lasso regression, the default solutions +from the library Scikit-Learn (when not shrinking \( \beta_0 \)) for the unknown parameters +\( \boldsymbol{\beta} \), are derived under the assumption that both \( \boldsymbol{y} \) and +\( \boldsymbol{X} \) are zero centered, that is we subtract the mean values. +

      @@ -425,7 +343,7 @@ plt.show()

    6955. 38
    6956. 39
    6957. ...
    6958. -
    6959. 60
    6960. +
    6961. 59
    6962. »
    6963. diff --git a/doc/pub/week36/html/._week36-bs030.html b/doc/pub/week36/html/._week36-bs030.html index ec0271389..0e6419f88 100644 --- a/doc/pub/week36/html/._week36-bs030.html +++ b/doc/pub/week36/html/._week36-bs030.html @@ -37,7 +37,6 @@ doconce format html week36.do.txt --html_style=bootstrap --pygments_html_style=d
    6964. Plans for week 36
    6965. -
    6966. Thursday September 8
    6967. -
    6968. Summary from last Week and discussion of SVD, Ridge and Lasso regression with examples
    6969. -
    6970. Linear Regression and the SVD
    6971. -
    6972. What does it mean?
    6973. -
    6974. And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
    6975. -
    6976. Code for SVD and Inversion of Matrices
    6977. -
    6978. Inverse of Rectangular Matrix
    6979. -
    6980. Ridge and LASSO Regression
    6981. -
    6982. From OLS to Ridge and Lasso
    6983. -
    6984. Deriving the Ridge Regression Equations
    6985. -
    6986. Note on Scikit-Learn
    6987. -
    6988. Comparison with OLS
    6989. -
    6990. SVD analysis
    6991. -
    6992. Interpreting the Ridge results
    6993. -
    6994. More interpretations
    6995. -
    6996. Deriving the Lasso Regression Equations
    6997. -
    6998. Simple example to illustrate Ordinary Least Squares, Ridge and Lasso Regression
    6999. -
    7000. Ridge Regression
    7001. -
    7002. Lasso Regression
    7003. -
    7004. Yet another Example
    7005. -
    7006. The OLS case
    7007. -
    7008. The Ridge case
    7009. -
    7010. Writing the Cost Function
    7011. -
    7012. Lasso case
    7013. -
    7014. The first Case
    7015. -
    7016. Simple code for solving the above problem
    7017. -
    7018. With Lasso Regression
    7019. -
    7020. Another Example, now with a polynomial fit
    7021. -
    7022. To think about, first part
    7023. -
    7024. More thinking
    7025. -
    7026. Still thinking
    7027. -
    7028. What does centering (subtracting the mean values) mean mathematically?
    7029. -
    7030. Further Manipulations
    7031. -
    7032. Wrapping it up
    7033. -
    7034. Linear Regression code, Intercept handling first
    7035. -
    7036. Code Examples
    7037. -
    7038. Taking out the mean
    7039. -
    7040. Friday September 9
    7041. -
    7042. Linking the regression analysis with a statistical interpretation
    7043. -
    7044. Assumptions made
    7045. -
    7046. Expectation value and variance
    7047. -
    7048. Expectation value and variance for \( \boldsymbol{\beta} \)
    7049. -
    7050. Deriving OLS from a probability distribution
    7051. -
    7052. Independent and Identically Distrubuted (iid)
    7053. -
    7054. Maximum Likelihood Estimation (MLE)
    7055. -
    7056. A new Cost Function
    7057. -
    7058. More basic Statistics and Bayes' theorem
    7059. -
    7060. Marginal Probability
    7061. -
    7062. Conditional Probability
    7063. -
    7064. Bayes' Theorem
    7065. -
    7066. Interpretations of Bayes' Theorem
    7067. -
    7068. Example of Usage of Bayes' theorem
    7069. -
    7070. Doing it correctly
    7071. -
    7072. Bayes' Theorem and Ridge and Lasso Regression
    7073. -
    7074. Test Function for what happens with OLS, Ridge and Lasso
    7075. -
    7076. Invoking Bayes' theorem
    7077. -
    7078. Ridge and Bayes
    7079. -
    7080. Lasso and Bayes
    7081. -
    7082. Exercise 1: mean values and variances in linear regression
    7083. -
    7084. Exercise 2: Adding Ridge and Lasso Regression
    7085. +
    7086. Summary from last Week and discussion of SVD, Ridge and Lasso regression with examples
    7087. +
    7088. Linear Regression and the SVD
    7089. +
    7090. What does it mean?
    7091. +
    7092. And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
    7093. +
    7094. Code for SVD and Inversion of Matrices
    7095. +
    7096. Inverse of Rectangular Matrix
    7097. +
    7098. Ridge and LASSO Regression
    7099. +
    7100. From OLS to Ridge and Lasso
    7101. +
    7102. Deriving the Ridge Regression Equations
    7103. +
    7104. Note on Scikit-Learn
    7105. +
    7106. Comparison with OLS
    7107. +
    7108. SVD analysis
    7109. +
    7110. Interpreting the Ridge results
    7111. +
    7112. More interpretations
    7113. +
    7114. Deriving the Lasso Regression Equations
    7115. +
    7116. Simple example to illustrate Ordinary Least Squares, Ridge and Lasso Regression
    7117. +
    7118. Ridge Regression
    7119. +
    7120. Lasso Regression
    7121. +
    7122. Yet another Example
    7123. +
    7124. The OLS case
    7125. +
    7126. The Ridge case
    7127. +
    7128. Writing the Cost Function
    7129. +
    7130. Lasso case
    7131. +
    7132. The first Case
    7133. +
    7134. Simple code for solving the above problem
    7135. +
    7136. With Lasso Regression
    7137. +
    7138. Another Example, now with a polynomial fit
    7139. +
    7140. To think about, first part
    7141. +
    7142. More thinking
    7143. +
    7144. Still thinking
    7145. +
    7146. What does centering (subtracting the mean values) mean mathematically?
    7147. +
    7148. Further Manipulations
    7149. +
    7150. Wrapping it up
    7151. +
    7152. Linear Regression code, Intercept handling first
    7153. +
    7154. Code Examples
    7155. +
    7156. Taking out the mean
    7157. +
    7158. Friday September 9
    7159. +
    7160. Linking the regression analysis with a statistical interpretation
    7161. +
    7162. Assumptions made
    7163. +
    7164. Expectation value and variance
    7165. +
    7166. Expectation value and variance for \( \boldsymbol{\beta} \)
    7167. +
    7168. Deriving OLS from a probability distribution
    7169. +
    7170. Independent and Identically Distrubuted (iid)
    7171. +
    7172. Maximum Likelihood Estimation (MLE)
    7173. +
    7174. A new Cost Function
    7175. +
    7176. More basic Statistics and Bayes' theorem
    7177. +
    7178. Marginal Probability
    7179. +
    7180. Conditional Probability
    7181. +
    7182. Bayes' Theorem
    7183. +
    7184. Interpretations of Bayes' Theorem
    7185. +
    7186. Example of Usage of Bayes' theorem
    7187. +
    7188. Doing it correctly
    7189. +
    7190. Bayes' Theorem and Ridge and Lasso Regression
    7191. +
    7192. Test Function for what happens with OLS, Ridge and Lasso
    7193. +
    7194. Invoking Bayes' theorem
    7195. +
    7196. Ridge and Bayes
    7197. +
    7198. Lasso and Bayes
    7199. +
    7200. Exercise 1: mean values and variances in linear regression
    7201. +
    7202. Exercise 2: Adding Ridge and Lasso Regression
    7203. @@ -297,27 +295,35 @@ MathJax.Hub.Config({

       

       

       

      -

      To think about, first part

      +

      More thinking

      -

      When you are comparing your own code with for example Scikit-Learn's -library, there are some technicalities to keep in mind. The examples -here demonstrate some of these aspects with potential pitfalls. +

      If our predictors represent different scales, then it is important to +standardize the design matrix \( \boldsymbol{X} \) by subtracting the mean of each +column from the corresponding column and dividing the column with its +standard deviation. Most machine learning libraries do this as a default. This means that if you compare your code with the results from a given library, +the results may differ.

      -

      The discussion here focuses on the role of the intercept, how we can -set up the design matrix, what scaling we should use and other topics -which tend confuse us. +

      The +Standadscaler +function in Scikit-Learn does this for us. For the data sets we +have been studying in our various examples, the data are in many cases +already scaled and there is no need to scale them. You as a user of different machine learning algorithms, should always perform a +survey of your data, with a critical assessment of them in case you need to scale the data.

      -

      The intercept can be interpreted as the expected value of our -target/output variables when all other predictors are set to zero. -Thus, if we cannot assume that the expected outputs/targets are zero -when all predictors are zero (the columns in the design matrix), it -may be a bad idea to implement a model which penalizes the intercept. -Furthermore, in for example Ridge and Lasso regression, the default solutions -from the library Scikit-Learn (when not shrinking \( \beta_0 \)) for the unknown parameters -\( \boldsymbol{\beta} \), are derived under the assumption that both \( \boldsymbol{y} \) and -\( \boldsymbol{X} \) are zero centered, that is we subtract the mean values. +

      If you need to scale the data, not doing so will give an unfair +penalization of the parameters since their magnitude depends on the +scale of their corresponding predictor. +

      + +

      Suppose as an example that you +you have an input variable given by the heights of different persons. +Human height might be measured in inches or meters or +kilometers. If measured in kilometers, a standard linear regression +model with this predictor would probably give a much bigger +coefficient term, than if measured in millimeters. +This can clearly lead to problems in evaluating the cost/loss functions.

      @@ -345,7 +351,7 @@ from the library Scikit-Learn (when not shrinking \( \beta_0 \)) for the

    7204. 39
    7205. 40
    7206. ...
    7207. -
    7208. 60
    7209. +
    7210. 59
    7211. »
    7212. diff --git a/doc/pub/week36/html/._week36-bs031.html b/doc/pub/week36/html/._week36-bs031.html index 045b0cb25..f4b879d80 100644 --- a/doc/pub/week36/html/._week36-bs031.html +++ b/doc/pub/week36/html/._week36-bs031.html @@ -37,7 +37,6 @@ doconce format html week36.do.txt --html_style=bootstrap --pygments_html_style=d
    7213. Plans for week 36
    7214. -
    7215. Thursday September 8
    7216. -
    7217. Summary from last Week and discussion of SVD, Ridge and Lasso regression with examples
    7218. -
    7219. Linear Regression and the SVD
    7220. -
    7221. What does it mean?
    7222. -
    7223. And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
    7224. -
    7225. Code for SVD and Inversion of Matrices
    7226. -
    7227. Inverse of Rectangular Matrix
    7228. -
    7229. Ridge and LASSO Regression
    7230. -
    7231. From OLS to Ridge and Lasso
    7232. -
    7233. Deriving the Ridge Regression Equations
    7234. -
    7235. Note on Scikit-Learn
    7236. -
    7237. Comparison with OLS
    7238. -
    7239. SVD analysis
    7240. -
    7241. Interpreting the Ridge results
    7242. -
    7243. More interpretations
    7244. -
    7245. Deriving the Lasso Regression Equations
    7246. -
    7247. Simple example to illustrate Ordinary Least Squares, Ridge and Lasso Regression
    7248. -
    7249. Ridge Regression
    7250. -
    7251. Lasso Regression
    7252. -
    7253. Yet another Example
    7254. -
    7255. The OLS case
    7256. -
    7257. The Ridge case
    7258. -
    7259. Writing the Cost Function
    7260. -
    7261. Lasso case
    7262. -
    7263. The first Case
    7264. -
    7265. Simple code for solving the above problem
    7266. -
    7267. With Lasso Regression
    7268. -
    7269. Another Example, now with a polynomial fit
    7270. -
    7271. To think about, first part
    7272. -
    7273. More thinking
    7274. -
    7275. Still thinking
    7276. -
    7277. What does centering (subtracting the mean values) mean mathematically?
    7278. -
    7279. Further Manipulations
    7280. -
    7281. Wrapping it up
    7282. -
    7283. Linear Regression code, Intercept handling first
    7284. -
    7285. Code Examples
    7286. -
    7287. Taking out the mean
    7288. -
    7289. Friday September 9
    7290. -
    7291. Linking the regression analysis with a statistical interpretation
    7292. -
    7293. Assumptions made
    7294. -
    7295. Expectation value and variance
    7296. -
    7297. Expectation value and variance for \( \boldsymbol{\beta} \)
    7298. -
    7299. Deriving OLS from a probability distribution
    7300. -
    7301. Independent and Identically Distrubuted (iid)
    7302. -
    7303. Maximum Likelihood Estimation (MLE)
    7304. -
    7305. A new Cost Function
    7306. -
    7307. More basic Statistics and Bayes' theorem
    7308. -
    7309. Marginal Probability
    7310. -
    7311. Conditional Probability
    7312. -
    7313. Bayes' Theorem
    7314. -
    7315. Interpretations of Bayes' Theorem
    7316. -
    7317. Example of Usage of Bayes' theorem
    7318. -
    7319. Doing it correctly
    7320. -
    7321. Bayes' Theorem and Ridge and Lasso Regression
    7322. -
    7323. Test Function for what happens with OLS, Ridge and Lasso
    7324. -
    7325. Invoking Bayes' theorem
    7326. -
    7327. Ridge and Bayes
    7328. -
    7329. Lasso and Bayes
    7330. -
    7331. Exercise 1: mean values and variances in linear regression
    7332. -
    7333. Exercise 2: Adding Ridge and Lasso Regression
    7334. +
    7335. Summary from last Week and discussion of SVD, Ridge and Lasso regression with examples
    7336. +
    7337. Linear Regression and the SVD
    7338. +
    7339. What does it mean?
    7340. +
    7341. And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
    7342. +
    7343. Code for SVD and Inversion of Matrices
    7344. +
    7345. Inverse of Rectangular Matrix
    7346. +
    7347. Ridge and LASSO Regression
    7348. +
    7349. From OLS to Ridge and Lasso
    7350. +
    7351. Deriving the Ridge Regression Equations
    7352. +
    7353. Note on Scikit-Learn
    7354. +
    7355. Comparison with OLS
    7356. +
    7357. SVD analysis
    7358. +
    7359. Interpreting the Ridge results
    7360. +
    7361. More interpretations
    7362. +
    7363. Deriving the Lasso Regression Equations
    7364. +
    7365. Simple example to illustrate Ordinary Least Squares, Ridge and Lasso Regression
    7366. +
    7367. Ridge Regression
    7368. +
    7369. Lasso Regression
    7370. +
    7371. Yet another Example
    7372. +
    7373. The OLS case
    7374. +
    7375. The Ridge case
    7376. +
    7377. Writing the Cost Function
    7378. +
    7379. Lasso case
    7380. +
    7381. The first Case
    7382. +
    7383. Simple code for solving the above problem
    7384. +
    7385. With Lasso Regression
    7386. +
    7387. Another Example, now with a polynomial fit
    7388. +
    7389. To think about, first part
    7390. +
    7391. More thinking
    7392. +
    7393. Still thinking
    7394. +
    7395. What does centering (subtracting the mean values) mean mathematically?
    7396. +
    7397. Further Manipulations
    7398. +
    7399. Wrapping it up
    7400. +
    7401. Linear Regression code, Intercept handling first
    7402. +
    7403. Code Examples
    7404. +
    7405. Taking out the mean
    7406. +
    7407. Friday September 9
    7408. +
    7409. Linking the regression analysis with a statistical interpretation
    7410. +
    7411. Assumptions made
    7412. +
    7413. Expectation value and variance
    7414. +
    7415. Expectation value and variance for \( \boldsymbol{\beta} \)
    7416. +
    7417. Deriving OLS from a probability distribution
    7418. +
    7419. Independent and Identically Distrubuted (iid)
    7420. +
    7421. Maximum Likelihood Estimation (MLE)
    7422. +
    7423. A new Cost Function
    7424. +
    7425. More basic Statistics and Bayes' theorem
    7426. +
    7427. Marginal Probability
    7428. +
    7429. Conditional Probability
    7430. +
    7431. Bayes' Theorem
    7432. +
    7433. Interpretations of Bayes' Theorem
    7434. +
    7435. Example of Usage of Bayes' theorem
    7436. +
    7437. Doing it correctly
    7438. +
    7439. Bayes' Theorem and Ridge and Lasso Regression
    7440. +
    7441. Test Function for what happens with OLS, Ridge and Lasso
    7442. +
    7443. Invoking Bayes' theorem
    7444. +
    7445. Ridge and Bayes
    7446. +
    7447. Lasso and Bayes
    7448. +
    7449. Exercise 1: mean values and variances in linear regression
    7450. +
    7451. Exercise 2: Adding Ridge and Lasso Regression
    7452. @@ -297,36 +295,48 @@ MathJax.Hub.Config({

       

       

       

      -

      More thinking

      +

      Still thinking

      -

      If our predictors represent different scales, then it is important to -standardize the design matrix \( \boldsymbol{X} \) by subtracting the mean of each -column from the corresponding column and dividing the column with its -standard deviation. Most machine learning libraries do this as a default. This means that if you compare your code with the results from a given library, -the results may differ. +

      Keep in mind that when you transform your data set before training a model, the same transformation needs to be done +on your eventual new data set before making a prediction. If we translate this into a Python code, it would could be implemented as follows

      -

      The -Standadscaler -function in Scikit-Learn does this for us. For the data sets we -have been studying in our various examples, the data are in many cases -already scaled and there is no need to scale them. You as a user of different machine learning algorithms, should always perform a -survey of your data, with a critical assessment of them in case you need to scale the data. -

      -

      If you need to scale the data, not doing so will give an unfair -penalization of the parameters since their magnitude depends on the -scale of their corresponding predictor. -

      + +
      +
      +
      +
      +
      +
      #Model training, we compute the mean value of y and X
      +y_train_mean = np.mean(y_train)
      +X_train_mean = np.mean(X_train,axis=0)
      +X_train = X_train - X_train_mean
      +y_train = y_train - y_train_mean
      +
      +# The we fit our model with the training data
      +trained_model = some_model.fit(X_train,y_train)
      +
      +
      +#Model prediction, we need also to transform our data set used for the prediction.
      +X_test = X_test - X_train_mean #Use mean from training data
      +y_pred = trained_model(X_test)
      +y_pred = y_pred + y_train_mean
      +
      +
      +
      +
      +
      +
      +
      +
      +
      +
      +
      +
      +
      +
      -

      Suppose as an example that you -you have an input variable given by the heights of different persons. -Human height might be measured in inches or meters or -kilometers. If measured in kilometers, a standard linear regression -model with this predictor would probably give a much bigger -coefficient term, than if measured in millimeters. -This can clearly lead to problems in evaluating the cost/loss functions. -

      @@ -353,7 +363,7 @@ This can clearly lead to problems in evaluating the cost/loss functions.

    7453. 40
    7454. 41
    7455. ...
    7456. -
    7457. 60
    7458. +
    7459. 59
    7460. »
    7461. diff --git a/doc/pub/week36/html/._week36-bs032.html b/doc/pub/week36/html/._week36-bs032.html index 06111bcce..fde17c13b 100644 --- a/doc/pub/week36/html/._week36-bs032.html +++ b/doc/pub/week36/html/._week36-bs032.html @@ -37,7 +37,6 @@ doconce format html week36.do.txt --html_style=bootstrap --pygments_html_style=d
    7462. Plans for week 36
    7463. -
    7464. Thursday September 8
    7465. -
    7466. Summary from last Week and discussion of SVD, Ridge and Lasso regression with examples
    7467. -
    7468. Linear Regression and the SVD
    7469. -
    7470. What does it mean?
    7471. -
    7472. And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
    7473. -
    7474. Code for SVD and Inversion of Matrices
    7475. -
    7476. Inverse of Rectangular Matrix
    7477. -
    7478. Ridge and LASSO Regression
    7479. -
    7480. From OLS to Ridge and Lasso
    7481. -
    7482. Deriving the Ridge Regression Equations
    7483. -
    7484. Note on Scikit-Learn
    7485. -
    7486. Comparison with OLS
    7487. -
    7488. SVD analysis
    7489. -
    7490. Interpreting the Ridge results
    7491. -
    7492. More interpretations
    7493. -
    7494. Deriving the Lasso Regression Equations
    7495. -
    7496. Simple example to illustrate Ordinary Least Squares, Ridge and Lasso Regression
    7497. -
    7498. Ridge Regression
    7499. -
    7500. Lasso Regression
    7501. -
    7502. Yet another Example
    7503. -
    7504. The OLS case
    7505. -
    7506. The Ridge case
    7507. -
    7508. Writing the Cost Function
    7509. -
    7510. Lasso case
    7511. -
    7512. The first Case
    7513. -
    7514. Simple code for solving the above problem
    7515. -
    7516. With Lasso Regression
    7517. -
    7518. Another Example, now with a polynomial fit
    7519. -
    7520. To think about, first part
    7521. -
    7522. More thinking
    7523. -
    7524. Still thinking
    7525. -
    7526. What does centering (subtracting the mean values) mean mathematically?
    7527. -
    7528. Further Manipulations
    7529. -
    7530. Wrapping it up
    7531. -
    7532. Linear Regression code, Intercept handling first
    7533. -
    7534. Code Examples
    7535. -
    7536. Taking out the mean
    7537. -
    7538. Friday September 9
    7539. -
    7540. Linking the regression analysis with a statistical interpretation
    7541. -
    7542. Assumptions made
    7543. -
    7544. Expectation value and variance
    7545. -
    7546. Expectation value and variance for \( \boldsymbol{\beta} \)
    7547. -
    7548. Deriving OLS from a probability distribution
    7549. -
    7550. Independent and Identically Distrubuted (iid)
    7551. -
    7552. Maximum Likelihood Estimation (MLE)
    7553. -
    7554. A new Cost Function
    7555. -
    7556. More basic Statistics and Bayes' theorem
    7557. -
    7558. Marginal Probability
    7559. -
    7560. Conditional Probability
    7561. -
    7562. Bayes' Theorem
    7563. -
    7564. Interpretations of Bayes' Theorem
    7565. -
    7566. Example of Usage of Bayes' theorem
    7567. -
    7568. Doing it correctly
    7569. -
    7570. Bayes' Theorem and Ridge and Lasso Regression
    7571. -
    7572. Test Function for what happens with OLS, Ridge and Lasso
    7573. -
    7574. Invoking Bayes' theorem
    7575. -
    7576. Ridge and Bayes
    7577. -
    7578. Lasso and Bayes
    7579. -
    7580. Exercise 1: mean values and variances in linear regression
    7581. -
    7582. Exercise 2: Adding Ridge and Lasso Regression
    7583. +
    7584. Summary from last Week and discussion of SVD, Ridge and Lasso regression with examples
    7585. +
    7586. Linear Regression and the SVD
    7587. +
    7588. What does it mean?
    7589. +
    7590. And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
    7591. +
    7592. Code for SVD and Inversion of Matrices
    7593. +
    7594. Inverse of Rectangular Matrix
    7595. +
    7596. Ridge and LASSO Regression
    7597. +
    7598. From OLS to Ridge and Lasso
    7599. +
    7600. Deriving the Ridge Regression Equations
    7601. +
    7602. Note on Scikit-Learn
    7603. +
    7604. Comparison with OLS
    7605. +
    7606. SVD analysis
    7607. +
    7608. Interpreting the Ridge results
    7609. +
    7610. More interpretations
    7611. +
    7612. Deriving the Lasso Regression Equations
    7613. +
    7614. Simple example to illustrate Ordinary Least Squares, Ridge and Lasso Regression
    7615. +
    7616. Ridge Regression
    7617. +
    7618. Lasso Regression
    7619. +
    7620. Yet another Example
    7621. +
    7622. The OLS case
    7623. +
    7624. The Ridge case
    7625. +
    7626. Writing the Cost Function
    7627. +
    7628. Lasso case
    7629. +
    7630. The first Case
    7631. +
    7632. Simple code for solving the above problem
    7633. +
    7634. With Lasso Regression
    7635. +
    7636. Another Example, now with a polynomial fit
    7637. +
    7638. To think about, first part
    7639. +
    7640. More thinking
    7641. +
    7642. Still thinking
    7643. +
    7644. What does centering (subtracting the mean values) mean mathematically?
    7645. +
    7646. Further Manipulations
    7647. +
    7648. Wrapping it up
    7649. +
    7650. Linear Regression code, Intercept handling first
    7651. +
    7652. Code Examples
    7653. +
    7654. Taking out the mean
    7655. +
    7656. Friday September 9
    7657. +
    7658. Linking the regression analysis with a statistical interpretation
    7659. +
    7660. Assumptions made
    7661. +
    7662. Expectation value and variance
    7663. +
    7664. Expectation value and variance for \( \boldsymbol{\beta} \)
    7665. +
    7666. Deriving OLS from a probability distribution
    7667. +
    7668. Independent and Identically Distrubuted (iid)
    7669. +
    7670. Maximum Likelihood Estimation (MLE)
    7671. +
    7672. A new Cost Function
    7673. +
    7674. More basic Statistics and Bayes' theorem
    7675. +
    7676. Marginal Probability
    7677. +
    7678. Conditional Probability
    7679. +
    7680. Bayes' Theorem
    7681. +
    7682. Interpretations of Bayes' Theorem
    7683. +
    7684. Example of Usage of Bayes' theorem
    7685. +
    7686. Doing it correctly
    7687. +
    7688. Bayes' Theorem and Ridge and Lasso Regression
    7689. +
    7690. Test Function for what happens with OLS, Ridge and Lasso
    7691. +
    7692. Invoking Bayes' theorem
    7693. +
    7694. Ridge and Bayes
    7695. +
    7696. Lasso and Bayes
    7697. +
    7698. Exercise 1: mean values and variances in linear regression
    7699. +
    7700. Exercise 2: Adding Ridge and Lasso Regression
    7701. @@ -297,47 +295,39 @@ MathJax.Hub.Config({

       

       

       

      -

      Still thinking

      +

      What does centering (subtracting the mean values) mean mathematically?

      -

      Keep in mind that when you transform your data set before training a model, the same transformation needs to be done -on your eventual new data set before making a prediction. If we translate this into a Python code, it would could be implemented as follows +

      Let us try to understand what this may imply mathematically when we +subtract the mean values, also known as zero centering. For +simplicity, we will focus on ordinary regression, as done in the above example.

      +

      The cost/loss function for regression is

      +$$ +C(\beta_0, \beta_1, ... , \beta_{p-1}) = \frac{1}{n}\sum_{i=0}^{n} \left(y_i - \beta_0 - \sum_{j=1}^{p-1} X_{ij}\beta_j\right)^2,. +$$ - -
      -
      -
      -
      -
      -
      #Model training, we compute the mean value of y and X
      -y_train_mean = np.mean(y_train)
      -X_train_mean = np.mean(X_train,axis=0)
      -X_train = X_train - X_train_mean
      -y_train = y_train - y_train_mean
      +

      Recall also that we use the squared value since this leads to an increase of the penalty for higher differences between predicted and output/target values.

      -# The we fit our model with the training data -trained_model = some_model.fit(X_train,y_train) +

      What we have done is to single out the \( \beta_0 \) term in the definition of the mean squared error (MSE). +The design matrix +\( X \) does in this case not contain any intercept column. +When we take the derivative with respect to \( \beta_0 \), we want the derivative to obey +

      +$$ +\frac{\partial C}{\partial \beta_j} = 0, +$$ +

      for all \( j \). For \( \beta_0 \) we have

      -#Model prediction, we need also to transform our data set used for the prediction. -X_test = X_test - X_train_mean #Use mean from training data -y_pred = trained_model(X_test) -y_pred = y_pred + y_train_mean -
      -
      -
      -
      -
      -
      -
      -
      -
      -
      -
      -
      -
      -
      +$$ +\frac{\partial C}{\partial \beta_0} = -\frac{2}{n}\sum_{i=0}^{n-1} \left(y_i - \beta_0 - \sum_{j=1}^{p-1} X_{ij} \beta_j\right). +$$ + +

      Multiplying away the constant \( 2/n \), we obtain

      +$$ +\sum_{i=0}^{n-1} \beta_0 = \sum_{i=0}^{n-1}y_i - \sum_{i=0}^{n-1} \sum_{j=1}^{p-1} X_{ij} \beta_j. +$$

      @@ -365,7 +355,7 @@ y_pred = y_pred 41

    7702. 42
    7703. ...
    7704. -
    7705. 60
    7706. +
    7707. 59
    7708. »
    7709. diff --git a/doc/pub/week36/html/._week36-bs033.html b/doc/pub/week36/html/._week36-bs033.html index e4682d120..ad9dd29d6 100644 --- a/doc/pub/week36/html/._week36-bs033.html +++ b/doc/pub/week36/html/._week36-bs033.html @@ -37,7 +37,6 @@ doconce format html week36.do.txt --html_style=bootstrap --pygments_html_style=d
    7710. Plans for week 36
    7711. -
    7712. Thursday September 8
    7713. -
    7714. Summary from last Week and discussion of SVD, Ridge and Lasso regression with examples
    7715. -
    7716. Linear Regression and the SVD
    7717. -
    7718. What does it mean?
    7719. -
    7720. And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
    7721. -
    7722. Code for SVD and Inversion of Matrices
    7723. -
    7724. Inverse of Rectangular Matrix
    7725. -
    7726. Ridge and LASSO Regression
    7727. -
    7728. From OLS to Ridge and Lasso
    7729. -
    7730. Deriving the Ridge Regression Equations
    7731. -
    7732. Note on Scikit-Learn
    7733. -
    7734. Comparison with OLS
    7735. -
    7736. SVD analysis
    7737. -
    7738. Interpreting the Ridge results
    7739. -
    7740. More interpretations
    7741. -
    7742. Deriving the Lasso Regression Equations
    7743. -
    7744. Simple example to illustrate Ordinary Least Squares, Ridge and Lasso Regression
    7745. -
    7746. Ridge Regression
    7747. -
    7748. Lasso Regression
    7749. -
    7750. Yet another Example
    7751. -
    7752. The OLS case
    7753. -
    7754. The Ridge case
    7755. -
    7756. Writing the Cost Function
    7757. -
    7758. Lasso case
    7759. -
    7760. The first Case
    7761. -
    7762. Simple code for solving the above problem
    7763. -
    7764. With Lasso Regression
    7765. -
    7766. Another Example, now with a polynomial fit
    7767. -
    7768. To think about, first part
    7769. -
    7770. More thinking
    7771. -
    7772. Still thinking
    7773. -
    7774. What does centering (subtracting the mean values) mean mathematically?
    7775. -
    7776. Further Manipulations
    7777. -
    7778. Wrapping it up
    7779. -
    7780. Linear Regression code, Intercept handling first
    7781. -
    7782. Code Examples
    7783. -
    7784. Taking out the mean
    7785. -
    7786. Friday September 9
    7787. -
    7788. Linking the regression analysis with a statistical interpretation
    7789. -
    7790. Assumptions made
    7791. -
    7792. Expectation value and variance
    7793. -
    7794. Expectation value and variance for \( \boldsymbol{\beta} \)
    7795. -
    7796. Deriving OLS from a probability distribution
    7797. -
    7798. Independent and Identically Distrubuted (iid)
    7799. -
    7800. Maximum Likelihood Estimation (MLE)
    7801. -
    7802. A new Cost Function
    7803. -
    7804. More basic Statistics and Bayes' theorem
    7805. -
    7806. Marginal Probability
    7807. -
    7808. Conditional Probability
    7809. -
    7810. Bayes' Theorem
    7811. -
    7812. Interpretations of Bayes' Theorem
    7813. -
    7814. Example of Usage of Bayes' theorem
    7815. -
    7816. Doing it correctly
    7817. -
    7818. Bayes' Theorem and Ridge and Lasso Regression
    7819. -
    7820. Test Function for what happens with OLS, Ridge and Lasso
    7821. -
    7822. Invoking Bayes' theorem
    7823. -
    7824. Ridge and Bayes
    7825. -
    7826. Lasso and Bayes
    7827. -
    7828. Exercise 1: mean values and variances in linear regression
    7829. -
    7830. Exercise 2: Adding Ridge and Lasso Regression
    7831. +
    7832. Summary from last Week and discussion of SVD, Ridge and Lasso regression with examples
    7833. +
    7834. Linear Regression and the SVD
    7835. +
    7836. What does it mean?
    7837. +
    7838. And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
    7839. +
    7840. Code for SVD and Inversion of Matrices
    7841. +
    7842. Inverse of Rectangular Matrix
    7843. +
    7844. Ridge and LASSO Regression
    7845. +
    7846. From OLS to Ridge and Lasso
    7847. +
    7848. Deriving the Ridge Regression Equations
    7849. +
    7850. Note on Scikit-Learn
    7851. +
    7852. Comparison with OLS
    7853. +
    7854. SVD analysis
    7855. +
    7856. Interpreting the Ridge results
    7857. +
    7858. More interpretations
    7859. +
    7860. Deriving the Lasso Regression Equations
    7861. +
    7862. Simple example to illustrate Ordinary Least Squares, Ridge and Lasso Regression
    7863. +
    7864. Ridge Regression
    7865. +
    7866. Lasso Regression
    7867. +
    7868. Yet another Example
    7869. +
    7870. The OLS case
    7871. +
    7872. The Ridge case
    7873. +
    7874. Writing the Cost Function
    7875. +
    7876. Lasso case
    7877. +
    7878. The first Case
    7879. +
    7880. Simple code for solving the above problem
    7881. +
    7882. With Lasso Regression
    7883. +
    7884. Another Example, now with a polynomial fit
    7885. +
    7886. To think about, first part
    7887. +
    7888. More thinking
    7889. +
    7890. Still thinking
    7891. +
    7892. What does centering (subtracting the mean values) mean mathematically?
    7893. +
    7894. Further Manipulations
    7895. +
    7896. Wrapping it up
    7897. +
    7898. Linear Regression code, Intercept handling first
    7899. +
    7900. Code Examples
    7901. +
    7902. Taking out the mean
    7903. +
    7904. Friday September 9
    7905. +
    7906. Linking the regression analysis with a statistical interpretation
    7907. +
    7908. Assumptions made
    7909. +
    7910. Expectation value and variance
    7911. +
    7912. Expectation value and variance for \( \boldsymbol{\beta} \)
    7913. +
    7914. Deriving OLS from a probability distribution
    7915. +
    7916. Independent and Identically Distrubuted (iid)
    7917. +
    7918. Maximum Likelihood Estimation (MLE)
    7919. +
    7920. A new Cost Function
    7921. +
    7922. More basic Statistics and Bayes' theorem
    7923. +
    7924. Marginal Probability
    7925. +
    7926. Conditional Probability
    7927. +
    7928. Bayes' Theorem
    7929. +
    7930. Interpretations of Bayes' Theorem
    7931. +
    7932. Example of Usage of Bayes' theorem
    7933. +
    7934. Doing it correctly
    7935. +
    7936. Bayes' Theorem and Ridge and Lasso Regression
    7937. +
    7938. Test Function for what happens with OLS, Ridge and Lasso
    7939. +
    7940. Invoking Bayes' theorem
    7941. +
    7942. Ridge and Bayes
    7943. +
    7944. Lasso and Bayes
    7945. +
    7946. Exercise 1: mean values and variances in linear regression
    7947. +
    7948. Exercise 2: Adding Ridge and Lasso Regression
    7949. @@ -297,38 +295,43 @@ MathJax.Hub.Config({

       

       

       

      -

      What does centering (subtracting the mean values) mean mathematically?

      +

      Further Manipulations

      -

      Let us try to understand what this may imply mathematically when we -subtract the mean values, also known as zero centering. For -simplicity, we will focus on ordinary regression, as done in the above example. -

      - -

      The cost/loss function for regression is

      -$$ -C(\beta_0, \beta_1, ... , \beta_{p-1}) = \frac{1}{n}\sum_{i=0}^{n} \left(y_i - \beta_0 - \sum_{j=1}^{p-1} X_{ij}\beta_j\right)^2,. -$$ - -

      Recall also that we use the squared value since this leads to an increase of the penalty for higher differences between predicted and output/target values.

      - -

      What we have done is to single out the \( \beta_0 \) term in the definition of the mean squared error (MSE). -The design matrix -\( X \) does in this case not contain any intercept column. -When we take the derivative with respect to \( \beta_0 \), we want the derivative to obey +

      Let us special first to the case where we have only two parameters \( \beta_0 \) and \( \beta_1 \). +Our result for \( \beta_0 \) simplifies then to

      $$ -\frac{\partial C}{\partial \beta_j} = 0, +n\beta_0 = \sum_{i=0}^{n-1}y_i - \sum_{i=0}^{n-1} X_{i1} \beta_1. $$ -

      for all \( j \). For \( \beta_0 \) we have

      - +

      We obtain then

      $$ -\frac{\partial C}{\partial \beta_0} = -\frac{2}{n}\sum_{i=0}^{n-1} \left(y_i - \beta_0 - \sum_{j=1}^{p-1} X_{ij} \beta_j\right). +\beta_0 = \frac{1}{n}\sum_{i=0}^{n-1}y_i - \beta_1\frac{1}{n}\sum_{i=0}^{n-1} X_{i1}. $$ -

      Multiplying away the constant \( 2/n \), we obtain

      +

      If we define

      $$ -\sum_{i=0}^{n-1} \beta_0 = \sum_{i=0}^{n-1}y_i - \sum_{i=0}^{n-1} \sum_{j=1}^{p-1} X_{ij} \beta_j. +\mu_1=\frac{1}{n}\sum_{i=0}^{n-1} (X_{i1}, +$$ + +

      and if we define the mean value of the outputs as

      +$$ +\mu_y=\frac{1}{n}\sum_{i=0}^{n-1}y_i, +$$ + +

      we have

      +$$ +\beta_0 = \mu_y - \beta_1\mu_{1}. +$$ + +

      In the general case, that is we have more parameters than \( \beta_0 \) and \( \beta_1 \), we have

      +$$ +\beta_0 = \frac{1}{n}\sum_{i=0}^{n-1}y_i - \frac{1}{n}\sum_{i=0}^{n-1}\sum_{j=1}^{p-1} X_{ij}\beta_j. +$$ + +

      Replacing \( y_i \) with \( y_i - y_i - \overline{\boldsymbol{y}} \) and centering also our design matrix results in a cost function (in vector-matrix disguise)

      +$$ +C(\boldsymbol{\beta}) = (\boldsymbol{\tilde{y}} - \tilde{X}\boldsymbol{\beta})^T(\boldsymbol{\tilde{y}} - \tilde{X}\boldsymbol{\beta}). $$ @@ -357,7 +360,7 @@ $$
    7950. 42
    7951. 43
    7952. ...
    7953. -
    7954. 60
    7955. +
    7956. 59
    7957. »
    7958. diff --git a/doc/pub/week36/html/._week36-bs034.html b/doc/pub/week36/html/._week36-bs034.html index aa618dc1b..13a93dad3 100644 --- a/doc/pub/week36/html/._week36-bs034.html +++ b/doc/pub/week36/html/._week36-bs034.html @@ -37,7 +37,6 @@ doconce format html week36.do.txt --html_style=bootstrap --pygments_html_style=d
    7959. Plans for week 36
    7960. -
    7961. Thursday September 8
    7962. -
    7963. Summary from last Week and discussion of SVD, Ridge and Lasso regression with examples
    7964. -
    7965. Linear Regression and the SVD
    7966. -
    7967. What does it mean?
    7968. -
    7969. And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
    7970. -
    7971. Code for SVD and Inversion of Matrices
    7972. -
    7973. Inverse of Rectangular Matrix
    7974. -
    7975. Ridge and LASSO Regression
    7976. -
    7977. From OLS to Ridge and Lasso
    7978. -
    7979. Deriving the Ridge Regression Equations
    7980. -
    7981. Note on Scikit-Learn
    7982. -
    7983. Comparison with OLS
    7984. -
    7985. SVD analysis
    7986. -
    7987. Interpreting the Ridge results
    7988. -
    7989. More interpretations
    7990. -
    7991. Deriving the Lasso Regression Equations
    7992. -
    7993. Simple example to illustrate Ordinary Least Squares, Ridge and Lasso Regression
    7994. -
    7995. Ridge Regression
    7996. -
    7997. Lasso Regression
    7998. -
    7999. Yet another Example
    8000. -
    8001. The OLS case
    8002. -
    8003. The Ridge case
    8004. -
    8005. Writing the Cost Function
    8006. -
    8007. Lasso case
    8008. -
    8009. The first Case
    8010. -
    8011. Simple code for solving the above problem
    8012. -
    8013. With Lasso Regression
    8014. -
    8015. Another Example, now with a polynomial fit
    8016. -
    8017. To think about, first part
    8018. -
    8019. More thinking
    8020. -
    8021. Still thinking
    8022. -
    8023. What does centering (subtracting the mean values) mean mathematically?
    8024. -
    8025. Further Manipulations
    8026. -
    8027. Wrapping it up
    8028. -
    8029. Linear Regression code, Intercept handling first
    8030. -
    8031. Code Examples
    8032. -
    8033. Taking out the mean
    8034. -
    8035. Friday September 9
    8036. -
    8037. Linking the regression analysis with a statistical interpretation
    8038. -
    8039. Assumptions made
    8040. -
    8041. Expectation value and variance
    8042. -
    8043. Expectation value and variance for \( \boldsymbol{\beta} \)
    8044. -
    8045. Deriving OLS from a probability distribution
    8046. -
    8047. Independent and Identically Distrubuted (iid)
    8048. -
    8049. Maximum Likelihood Estimation (MLE)
    8050. -
    8051. A new Cost Function
    8052. -
    8053. More basic Statistics and Bayes' theorem
    8054. -
    8055. Marginal Probability
    8056. -
    8057. Conditional Probability
    8058. -
    8059. Bayes' Theorem
    8060. -
    8061. Interpretations of Bayes' Theorem
    8062. -
    8063. Example of Usage of Bayes' theorem
    8064. -
    8065. Doing it correctly
    8066. -
    8067. Bayes' Theorem and Ridge and Lasso Regression
    8068. -
    8069. Test Function for what happens with OLS, Ridge and Lasso
    8070. -
    8071. Invoking Bayes' theorem
    8072. -
    8073. Ridge and Bayes
    8074. -
    8075. Lasso and Bayes
    8076. -
    8077. Exercise 1: mean values and variances in linear regression
    8078. -
    8079. Exercise 2: Adding Ridge and Lasso Regression
    8080. +
    8081. Summary from last Week and discussion of SVD, Ridge and Lasso regression with examples
    8082. +
    8083. Linear Regression and the SVD
    8084. +
    8085. What does it mean?
    8086. +
    8087. And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
    8088. +
    8089. Code for SVD and Inversion of Matrices
    8090. +
    8091. Inverse of Rectangular Matrix
    8092. +
    8093. Ridge and LASSO Regression
    8094. +
    8095. From OLS to Ridge and Lasso
    8096. +
    8097. Deriving the Ridge Regression Equations
    8098. +
    8099. Note on Scikit-Learn
    8100. +
    8101. Comparison with OLS
    8102. +
    8103. SVD analysis
    8104. +
    8105. Interpreting the Ridge results
    8106. +
    8107. More interpretations
    8108. +
    8109. Deriving the Lasso Regression Equations
    8110. +
    8111. Simple example to illustrate Ordinary Least Squares, Ridge and Lasso Regression
    8112. +
    8113. Ridge Regression
    8114. +
    8115. Lasso Regression
    8116. +
    8117. Yet another Example
    8118. +
    8119. The OLS case
    8120. +
    8121. The Ridge case
    8122. +
    8123. Writing the Cost Function
    8124. +
    8125. Lasso case
    8126. +
    8127. The first Case
    8128. +
    8129. Simple code for solving the above problem
    8130. +
    8131. With Lasso Regression
    8132. +
    8133. Another Example, now with a polynomial fit
    8134. +
    8135. To think about, first part
    8136. +
    8137. More thinking
    8138. +
    8139. Still thinking
    8140. +
    8141. What does centering (subtracting the mean values) mean mathematically?
    8142. +
    8143. Further Manipulations
    8144. +
    8145. Wrapping it up
    8146. +
    8147. Linear Regression code, Intercept handling first
    8148. +
    8149. Code Examples
    8150. +
    8151. Taking out the mean
    8152. +
    8153. Friday September 9
    8154. +
    8155. Linking the regression analysis with a statistical interpretation
    8156. +
    8157. Assumptions made
    8158. +
    8159. Expectation value and variance
    8160. +
    8161. Expectation value and variance for \( \boldsymbol{\beta} \)
    8162. +
    8163. Deriving OLS from a probability distribution
    8164. +
    8165. Independent and Identically Distrubuted (iid)
    8166. +
    8167. Maximum Likelihood Estimation (MLE)
    8168. +
    8169. A new Cost Function
    8170. +
    8171. More basic Statistics and Bayes' theorem
    8172. +
    8173. Marginal Probability
    8174. +
    8175. Conditional Probability
    8176. +
    8177. Bayes' Theorem
    8178. +
    8179. Interpretations of Bayes' Theorem
    8180. +
    8181. Example of Usage of Bayes' theorem
    8182. +
    8183. Doing it correctly
    8184. +
    8185. Bayes' Theorem and Ridge and Lasso Regression
    8186. +
    8187. Test Function for what happens with OLS, Ridge and Lasso
    8188. +
    8189. Invoking Bayes' theorem
    8190. +
    8191. Ridge and Bayes
    8192. +
    8193. Lasso and Bayes
    8194. +
    8195. Exercise 1: mean values and variances in linear regression
    8196. +
    8197. Exercise 2: Adding Ridge and Lasso Regression
    8198. @@ -297,45 +295,24 @@ MathJax.Hub.Config({

       

       

       

      -

      Further Manipulations

      +

      Wrapping it up

      -

      Let us special first to the case where we have only two parameters \( \beta_0 \) and \( \beta_1 \). -Our result for \( \beta_0 \) simplifies then to +

      If we minimize with respect to \( \boldsymbol{\beta} \) we have then

      + +$$ +\hat{\boldsymbol{\beta}} = (\tilde{X}^T\tilde{X})^{-1}\tilde{X}^T\boldsymbol{\tilde{y}}, +$$ + +

      where \( \boldsymbol{\tilde{y}} = \boldsymbol{y} - \overline{\boldsymbol{y}} \) +and \( \tilde{X}_{ij} = X_{ij} - \frac{1}{n}\sum_{k=0}^{n-1}X_{kj} \).

      + +

      For Ridge regression we need to add \( \lambda \boldsymbol{\beta}^T\boldsymbol{\beta} \) to the cost function and get then

      $$ -n\beta_0 = \sum_{i=0}^{n-1}y_i - \sum_{i=0}^{n-1} X_{i1} \beta_1. -$$ - -

      We obtain then

      -$$ -\beta_0 = \frac{1}{n}\sum_{i=0}^{n-1}y_i - \beta_1\frac{1}{n}\sum_{i=0}^{n-1} X_{i1}. -$$ - -

      If we define

      -$$ -\mu_1=\frac{1}{n}\sum_{i=0}^{n-1} (X_{i1}, -$$ - -

      and if we define the mean value of the outputs as

      -$$ -\mu_y=\frac{1}{n}\sum_{i=0}^{n-1}y_i, -$$ - -

      we have

      -$$ -\beta_0 = \mu_y - \beta_1\mu_{1}. -$$ - -

      In the general case, that is we have more parameters than \( \beta_0 \) and \( \beta_1 \), we have

      -$$ -\beta_0 = \frac{1}{n}\sum_{i=0}^{n-1}y_i - \frac{1}{n}\sum_{i=0}^{n-1}\sum_{j=1}^{p-1} X_{ij}\beta_j. -$$ - -

      Replacing \( y_i \) with \( y_i - y_i - \overline{\boldsymbol{y}} \) and centering also our design matrix results in a cost function (in vector-matrix disguise)

      -$$ -C(\boldsymbol{\beta}) = (\boldsymbol{\tilde{y}} - \tilde{X}\boldsymbol{\beta})^T(\boldsymbol{\tilde{y}} - \tilde{X}\boldsymbol{\beta}). +\hat{\boldsymbol{\beta}} = (\tilde{X}^T\tilde{X} + \lambda I)^{-1}\tilde{X}^T\boldsymbol{\tilde{y}}. $$ +

      What does this mean? And why do we insist on all this? Let us look at some examples.

      @@ -362,7 +339,7 @@ $$

    8199. 43
    8200. 44
    8201. ...
    8202. -
    8203. 60
    8204. +
    8205. 59
    8206. »
    8207. diff --git a/doc/pub/week36/html/._week36-bs035.html b/doc/pub/week36/html/._week36-bs035.html index f1f7af7cd..64ff998bf 100644 --- a/doc/pub/week36/html/._week36-bs035.html +++ b/doc/pub/week36/html/._week36-bs035.html @@ -37,7 +37,6 @@ doconce format html week36.do.txt --html_style=bootstrap --pygments_html_style=d
    8208. Plans for week 36
    8209. -
    8210. Thursday September 8
    8211. -
    8212. Summary from last Week and discussion of SVD, Ridge and Lasso regression with examples
    8213. -
    8214. Linear Regression and the SVD
    8215. -
    8216. What does it mean?
    8217. -
    8218. And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
    8219. -
    8220. Code for SVD and Inversion of Matrices
    8221. -
    8222. Inverse of Rectangular Matrix
    8223. -
    8224. Ridge and LASSO Regression
    8225. -
    8226. From OLS to Ridge and Lasso
    8227. -
    8228. Deriving the Ridge Regression Equations
    8229. -
    8230. Note on Scikit-Learn
    8231. -
    8232. Comparison with OLS
    8233. -
    8234. SVD analysis
    8235. -
    8236. Interpreting the Ridge results
    8237. -
    8238. More interpretations
    8239. -
    8240. Deriving the Lasso Regression Equations
    8241. -
    8242. Simple example to illustrate Ordinary Least Squares, Ridge and Lasso Regression
    8243. -
    8244. Ridge Regression
    8245. -
    8246. Lasso Regression
    8247. -
    8248. Yet another Example
    8249. -
    8250. The OLS case
    8251. -
    8252. The Ridge case
    8253. -
    8254. Writing the Cost Function
    8255. -
    8256. Lasso case
    8257. -
    8258. The first Case
    8259. -
    8260. Simple code for solving the above problem
    8261. -
    8262. With Lasso Regression
    8263. -
    8264. Another Example, now with a polynomial fit
    8265. -
    8266. To think about, first part
    8267. -
    8268. More thinking
    8269. -
    8270. Still thinking
    8271. -
    8272. What does centering (subtracting the mean values) mean mathematically?
    8273. -
    8274. Further Manipulations
    8275. -
    8276. Wrapping it up
    8277. -
    8278. Linear Regression code, Intercept handling first
    8279. -
    8280. Code Examples
    8281. -
    8282. Taking out the mean
    8283. -
    8284. Friday September 9
    8285. -
    8286. Linking the regression analysis with a statistical interpretation
    8287. -
    8288. Assumptions made
    8289. -
    8290. Expectation value and variance
    8291. -
    8292. Expectation value and variance for \( \boldsymbol{\beta} \)
    8293. -
    8294. Deriving OLS from a probability distribution
    8295. -
    8296. Independent and Identically Distrubuted (iid)
    8297. -
    8298. Maximum Likelihood Estimation (MLE)
    8299. -
    8300. A new Cost Function
    8301. -
    8302. More basic Statistics and Bayes' theorem
    8303. -
    8304. Marginal Probability
    8305. -
    8306. Conditional Probability
    8307. -
    8308. Bayes' Theorem
    8309. -
    8310. Interpretations of Bayes' Theorem
    8311. -
    8312. Example of Usage of Bayes' theorem
    8313. -
    8314. Doing it correctly
    8315. -
    8316. Bayes' Theorem and Ridge and Lasso Regression
    8317. -
    8318. Test Function for what happens with OLS, Ridge and Lasso
    8319. -
    8320. Invoking Bayes' theorem
    8321. -
    8322. Ridge and Bayes
    8323. -
    8324. Lasso and Bayes
    8325. -
    8326. Exercise 1: mean values and variances in linear regression
    8327. -
    8328. Exercise 2: Adding Ridge and Lasso Regression
    8329. +
    8330. Summary from last Week and discussion of SVD, Ridge and Lasso regression with examples
    8331. +
    8332. Linear Regression and the SVD
    8333. +
    8334. What does it mean?
    8335. +
    8336. And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
    8337. +
    8338. Code for SVD and Inversion of Matrices
    8339. +
    8340. Inverse of Rectangular Matrix
    8341. +
    8342. Ridge and LASSO Regression
    8343. +
    8344. From OLS to Ridge and Lasso
    8345. +
    8346. Deriving the Ridge Regression Equations
    8347. +
    8348. Note on Scikit-Learn
    8349. +
    8350. Comparison with OLS
    8351. +
    8352. SVD analysis
    8353. +
    8354. Interpreting the Ridge results
    8355. +
    8356. More interpretations
    8357. +
    8358. Deriving the Lasso Regression Equations
    8359. +
    8360. Simple example to illustrate Ordinary Least Squares, Ridge and Lasso Regression
    8361. +
    8362. Ridge Regression
    8363. +
    8364. Lasso Regression
    8365. +
    8366. Yet another Example
    8367. +
    8368. The OLS case
    8369. +
    8370. The Ridge case
    8371. +
    8372. Writing the Cost Function
    8373. +
    8374. Lasso case
    8375. +
    8376. The first Case
    8377. +
    8378. Simple code for solving the above problem
    8379. +
    8380. With Lasso Regression
    8381. +
    8382. Another Example, now with a polynomial fit
    8383. +
    8384. To think about, first part
    8385. +
    8386. More thinking
    8387. +
    8388. Still thinking
    8389. +
    8390. What does centering (subtracting the mean values) mean mathematically?
    8391. +
    8392. Further Manipulations
    8393. +
    8394. Wrapping it up
    8395. +
    8396. Linear Regression code, Intercept handling first
    8397. +
    8398. Code Examples
    8399. +
    8400. Taking out the mean
    8401. +
    8402. Friday September 9
    8403. +
    8404. Linking the regression analysis with a statistical interpretation
    8405. +
    8406. Assumptions made
    8407. +
    8408. Expectation value and variance
    8409. +
    8410. Expectation value and variance for \( \boldsymbol{\beta} \)
    8411. +
    8412. Deriving OLS from a probability distribution
    8413. +
    8414. Independent and Identically Distrubuted (iid)
    8415. +
    8416. Maximum Likelihood Estimation (MLE)
    8417. +
    8418. A new Cost Function
    8419. +
    8420. More basic Statistics and Bayes' theorem
    8421. +
    8422. Marginal Probability
    8423. +
    8424. Conditional Probability
    8425. +
    8426. Bayes' Theorem
    8427. +
    8428. Interpretations of Bayes' Theorem
    8429. +
    8430. Example of Usage of Bayes' theorem
    8431. +
    8432. Doing it correctly
    8433. +
    8434. Bayes' Theorem and Ridge and Lasso Regression
    8435. +
    8436. Test Function for what happens with OLS, Ridge and Lasso
    8437. +
    8438. Invoking Bayes' theorem
    8439. +
    8440. Ridge and Bayes
    8441. +
    8442. Lasso and Bayes
    8443. +
    8444. Exercise 1: mean values and variances in linear regression
    8445. +
    8446. Exercise 2: Adding Ridge and Lasso Regression
    8447. @@ -297,24 +295,149 @@ MathJax.Hub.Config({

       

       

       

      -

      Wrapping it up

      +

      Linear Regression code, Intercept handling first

      -

      If we minimize with respect to \( \boldsymbol{\beta} \) we have then

      - -$$ -\hat{\boldsymbol{\beta}} = (\tilde{X}^T\tilde{X})^{-1}\tilde{X}^T\boldsymbol{\tilde{y}}, -$$ - -

      where \( \boldsymbol{\tilde{y}} = \boldsymbol{y} - \overline{\boldsymbol{y}} \) -and \( \tilde{X}_{ij} = X_{ij} - \frac{1}{n}\sum_{k=0}^{n-1}X_{kj} \). +

      This code shows a simple first-order fit to a data set using the above transformed data, where we consider the role of the intercept first, by either excluding it or including it (code example thanks to Øyvind Sigmundson Schøyen). Here our scaling of the data is done by subtracting the mean values only. +Note also that we do not split the data into training and test.

      -

      For Ridge regression we need to add \( \lambda \boldsymbol{\beta}^T\boldsymbol{\beta} \) to the cost function and get then

      + + +
      +
      +
      +
      +
      +
      import numpy as np
      +import matplotlib.pyplot as plt
      +
      +from sklearn.linear_model import LinearRegression
      +
      +
      +np.random.seed(2021)
      +
      +def MSE(y_data,y_model):
      +    n = np.size(y_model)
      +    return np.sum((y_data-y_model)**2)/n
      +
      +
      +def fit_beta(X, y):
      +    return np.linalg.pinv(X.T @ X) @ X.T @ y
      +
      +
      +true_beta = [2, 0.5, 3.7]
      +
      +x = np.linspace(0, 1, 11)
      +y = np.sum(
      +    np.asarray([x ** p * b for p, b in enumerate(true_beta)]), axis=0
      +) + 0.1 * np.random.normal(size=len(x))
      +
      +degree = 3
      +X = np.zeros((len(x), degree))
      +
      +# Include the intercept in the design matrix
      +for p in range(degree):
      +    X[:, p] = x ** p
      +
      +beta = fit_beta(X, y)
      +
      +# Intercept is included in the design matrix
      +skl = LinearRegression(fit_intercept=False).fit(X, y)
      +
      +print(f"True beta: {true_beta}")
      +print(f"Fitted beta: {beta}")
      +print(f"Sklearn fitted beta: {skl.coef_}")
      +ypredictOwn = X @ beta
      +ypredictSKL = skl.predict(X)
      +print(f"MSE with intercept column")
      +print(MSE(y,ypredictOwn))
      +print(f"MSE with intercept column from SKL")
      +print(MSE(y,ypredictSKL))
      +
      +
      +plt.figure()
      +plt.scatter(x, y, label="Data")
      +plt.plot(x, X @ beta, label="Fit")
      +plt.plot(x, skl.predict(X), label="Sklearn (fit_intercept=False)")
      +
      +
      +# Do not include the intercept in the design matrix
      +X = np.zeros((len(x), degree - 1))
      +
      +for p in range(degree - 1):
      +    X[:, p] = x ** (p + 1)
      +
      +# Intercept is not included in the design matrix
      +skl = LinearRegression(fit_intercept=True).fit(X, y)
      +
      +# Use centered values for X and y when computing coefficients
      +y_offset = np.average(y, axis=0)
      +X_offset = np.average(X, axis=0)
      +
      +beta = fit_beta(X - X_offset, y - y_offset)
      +intercept = np.mean(y_offset - X_offset @ beta)
      +
      +print(f"Manual intercept: {intercept}")
      +print(f"Fitted beta (wiothout intercept): {beta}")
      +print(f"Sklearn intercept: {skl.intercept_}")
      +print(f"Sklearn fitted beta (without intercept): {skl.coef_}")
      +ypredictOwn = X @ beta
      +ypredictSKL = skl.predict(X)
      +print(f"MSE with Manual intercept")
      +print(MSE(y,ypredictOwn+intercept))
      +print(f"MSE with Sklearn intercept")
      +print(MSE(y,ypredictSKL))
      +
      +plt.plot(x, X @ beta + intercept, "--", label="Fit (manual intercept)")
      +plt.plot(x, skl.predict(X), "--", label="Sklearn (fit_intercept=True)")
      +plt.grid()
      +plt.legend()
      +
      +plt.show()
      +
      +
      +
      +
      +
      +
      +
      +
      +
      +
      +
      +
      +
      +
      + +

      The intercept is the value of our output/target variable +when all our features are zero and our function crosses the \( y \)-axis (for a one-dimensional case). +

      + +

      Printing the MSE, we see first that both methods give the same MSE, as +they should. However, when we move to for example Ridge regression, +the way we treat the intercept may give a larger or smaller MSE, +meaning that the MSE can be penalized by the value of the +intercept. Not including the intercept in the fit, means that the +regularization term does not include \( \beta_0 \). For different values +of \( \lambda \), this may lead to differeing MSE values. +

      + +

      To remind the reader, the regularization term, with the intercept in Ridge regression is given by

      $$ -\hat{\boldsymbol{\beta}} = (\tilde{X}^T\tilde{X} + \lambda I)^{-1}\tilde{X}^T\boldsymbol{\tilde{y}}. +\lambda \vert\vert \boldsymbol{\beta} \vert\vert_2^2 = \lambda \sum_{j=0}^{p-1}\beta_j^2, $$ -

      What does this mean? And why do we insist on all this? Let us look at some examples.

      +

      but when we take out the intercept, this equation becomes

      +$$ +\lambda \vert\vert \boldsymbol{\beta} \vert\vert_2^2 = \lambda \sum_{j=1}^{p-1}\beta_j^2. +$$ + +

      For Lasso regression we have

      +$$ +\lambda \vert\vert \boldsymbol{\beta} \vert\vert_1 = \lambda \sum_{j=1}^{p-1}\vert\beta_j\vert. +$$ + +

      It means that, when scaling the design matrix and the outputs/targets, by subtracting the mean values, we have an optimization problem which is not penalized by the intercept. The MSE value can then be smaller since it focuses only on the remaining quantities. If we however bring back the intercept, we will get a MSE which then contains the intercept.

      @@ -341,7 +464,7 @@ $$

    8448. 44
    8449. 45
    8450. ...
    8451. -
    8452. 60
    8453. +
    8454. 59
    8455. »
    8456. diff --git a/doc/pub/week36/html/._week36-bs036.html b/doc/pub/week36/html/._week36-bs036.html index d3056dd33..7cda3efb7 100644 --- a/doc/pub/week36/html/._week36-bs036.html +++ b/doc/pub/week36/html/._week36-bs036.html @@ -37,7 +37,6 @@ doconce format html week36.do.txt --html_style=bootstrap --pygments_html_style=d
    8457. Plans for week 36
    8458. -
    8459. Thursday September 8
    8460. -
    8461. Summary from last Week and discussion of SVD, Ridge and Lasso regression with examples
    8462. -
    8463. Linear Regression and the SVD
    8464. -
    8465. What does it mean?
    8466. -
    8467. And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
    8468. -
    8469. Code for SVD and Inversion of Matrices
    8470. -
    8471. Inverse of Rectangular Matrix
    8472. -
    8473. Ridge and LASSO Regression
    8474. -
    8475. From OLS to Ridge and Lasso
    8476. -
    8477. Deriving the Ridge Regression Equations
    8478. -
    8479. Note on Scikit-Learn
    8480. -
    8481. Comparison with OLS
    8482. -
    8483. SVD analysis
    8484. -
    8485. Interpreting the Ridge results
    8486. -
    8487. More interpretations
    8488. -
    8489. Deriving the Lasso Regression Equations
    8490. -
    8491. Simple example to illustrate Ordinary Least Squares, Ridge and Lasso Regression
    8492. -
    8493. Ridge Regression
    8494. -
    8495. Lasso Regression
    8496. -
    8497. Yet another Example
    8498. -
    8499. The OLS case
    8500. -
    8501. The Ridge case
    8502. -
    8503. Writing the Cost Function
    8504. -
    8505. Lasso case
    8506. -
    8507. The first Case
    8508. -
    8509. Simple code for solving the above problem
    8510. -
    8511. With Lasso Regression
    8512. -
    8513. Another Example, now with a polynomial fit
    8514. -
    8515. To think about, first part
    8516. -
    8517. More thinking
    8518. -
    8519. Still thinking
    8520. -
    8521. What does centering (subtracting the mean values) mean mathematically?
    8522. -
    8523. Further Manipulations
    8524. -
    8525. Wrapping it up
    8526. -
    8527. Linear Regression code, Intercept handling first
    8528. -
    8529. Code Examples
    8530. -
    8531. Taking out the mean
    8532. -
    8533. Friday September 9
    8534. -
    8535. Linking the regression analysis with a statistical interpretation
    8536. -
    8537. Assumptions made
    8538. -
    8539. Expectation value and variance
    8540. -
    8541. Expectation value and variance for \( \boldsymbol{\beta} \)
    8542. -
    8543. Deriving OLS from a probability distribution
    8544. -
    8545. Independent and Identically Distrubuted (iid)
    8546. -
    8547. Maximum Likelihood Estimation (MLE)
    8548. -
    8549. A new Cost Function
    8550. -
    8551. More basic Statistics and Bayes' theorem
    8552. -
    8553. Marginal Probability
    8554. -
    8555. Conditional Probability
    8556. -
    8557. Bayes' Theorem
    8558. -
    8559. Interpretations of Bayes' Theorem
    8560. -
    8561. Example of Usage of Bayes' theorem
    8562. -
    8563. Doing it correctly
    8564. -
    8565. Bayes' Theorem and Ridge and Lasso Regression
    8566. -
    8567. Test Function for what happens with OLS, Ridge and Lasso
    8568. -
    8569. Invoking Bayes' theorem
    8570. -
    8571. Ridge and Bayes
    8572. -
    8573. Lasso and Bayes
    8574. -
    8575. Exercise 1: mean values and variances in linear regression
    8576. -
    8577. Exercise 2: Adding Ridge and Lasso Regression
    8578. +
    8579. Summary from last Week and discussion of SVD, Ridge and Lasso regression with examples
    8580. +
    8581. Linear Regression and the SVD
    8582. +
    8583. What does it mean?
    8584. +
    8585. And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
    8586. +
    8587. Code for SVD and Inversion of Matrices
    8588. +
    8589. Inverse of Rectangular Matrix
    8590. +
    8591. Ridge and LASSO Regression
    8592. +
    8593. From OLS to Ridge and Lasso
    8594. +
    8595. Deriving the Ridge Regression Equations
    8596. +
    8597. Note on Scikit-Learn
    8598. +
    8599. Comparison with OLS
    8600. +
    8601. SVD analysis
    8602. +
    8603. Interpreting the Ridge results
    8604. +
    8605. More interpretations
    8606. +
    8607. Deriving the Lasso Regression Equations
    8608. +
    8609. Simple example to illustrate Ordinary Least Squares, Ridge and Lasso Regression
    8610. +
    8611. Ridge Regression
    8612. +
    8613. Lasso Regression
    8614. +
    8615. Yet another Example
    8616. +
    8617. The OLS case
    8618. +
    8619. The Ridge case
    8620. +
    8621. Writing the Cost Function
    8622. +
    8623. Lasso case
    8624. +
    8625. The first Case
    8626. +
    8627. Simple code for solving the above problem
    8628. +
    8629. With Lasso Regression
    8630. +
    8631. Another Example, now with a polynomial fit
    8632. +
    8633. To think about, first part
    8634. +
    8635. More thinking
    8636. +
    8637. Still thinking
    8638. +
    8639. What does centering (subtracting the mean values) mean mathematically?
    8640. +
    8641. Further Manipulations
    8642. +
    8643. Wrapping it up
    8644. +
    8645. Linear Regression code, Intercept handling first
    8646. +
    8647. Code Examples
    8648. +
    8649. Taking out the mean
    8650. +
    8651. Friday September 9
    8652. +
    8653. Linking the regression analysis with a statistical interpretation
    8654. +
    8655. Assumptions made
    8656. +
    8657. Expectation value and variance
    8658. +
    8659. Expectation value and variance for \( \boldsymbol{\beta} \)
    8660. +
    8661. Deriving OLS from a probability distribution
    8662. +
    8663. Independent and Identically Distrubuted (iid)
    8664. +
    8665. Maximum Likelihood Estimation (MLE)
    8666. +
    8667. A new Cost Function
    8668. +
    8669. More basic Statistics and Bayes' theorem
    8670. +
    8671. Marginal Probability
    8672. +
    8673. Conditional Probability
    8674. +
    8675. Bayes' Theorem
    8676. +
    8677. Interpretations of Bayes' Theorem
    8678. +
    8679. Example of Usage of Bayes' theorem
    8680. +
    8681. Doing it correctly
    8682. +
    8683. Bayes' Theorem and Ridge and Lasso Regression
    8684. +
    8685. Test Function for what happens with OLS, Ridge and Lasso
    8686. +
    8687. Invoking Bayes' theorem
    8688. +
    8689. Ridge and Bayes
    8690. +
    8691. Lasso and Bayes
    8692. +
    8693. Exercise 1: mean values and variances in linear regression
    8694. +
    8695. Exercise 2: Adding Ridge and Lasso Regression
    8696. @@ -297,11 +295,9 @@ MathJax.Hub.Config({

       

       

       

      -

      Linear Regression code, Intercept handling first

      +

      Code Examples

      -

      This code shows a simple first-order fit to a data set using the above transformed data, where we consider the role of the intercept first, by either excluding it or including it (code example thanks to Øyvind Sigmundson Schøyen). Here our scaling of the data is done by subtracting the mean values only. -Note also that we do not split the data into training and test. -

      +

      Armed with this wisdom, we attempt first to simply set the intercept equal to False in our implementation of Ridge regression for our well-known vanilla data set.

      @@ -311,90 +307,69 @@ Note also that we do not split the data into training and test.
      import numpy as np
      +import pandas as pd
       import matplotlib.pyplot as plt
      -
      -from sklearn.linear_model import LinearRegression
      -
      -
      -np.random.seed(2021)
      +from sklearn.model_selection import train_test_split
      +from sklearn import linear_model
       
       def MSE(y_data,y_model):
           n = np.size(y_model)
           return np.sum((y_data-y_model)**2)/n
       
       
      -def fit_beta(X, y):
      -    return np.linalg.pinv(X.T @ X) @ X.T @ y
      +# A seed just to ensure that the random numbers are the same for every run.
      +# Useful for eventual debugging.
      +np.random.seed(3155)
       
      +n = 100
      +x = np.random.rand(n)
      +y = np.exp(-x**2) + 1.5 * np.exp(-(x-2)**2)
       
      -true_beta = [2, 0.5, 3.7]
      -
      -x = np.linspace(0, 1, 11)
      -y = np.sum(
      -    np.asarray([x ** p * b for p, b in enumerate(true_beta)]), axis=0
      -) + 0.1 * np.random.normal(size=len(x))
      -
      -degree = 3
      -X = np.zeros((len(x), degree))
      -
      -# Include the intercept in the design matrix
      -for p in range(degree):
      -    X[:, p] = x ** p
      -
      -beta = fit_beta(X, y)
      -
      -# Intercept is included in the design matrix
      -skl = LinearRegression(fit_intercept=False).fit(X, y)
      -
      -print(f"True beta: {true_beta}")
      -print(f"Fitted beta: {beta}")
      -print(f"Sklearn fitted beta: {skl.coef_}")
      -ypredictOwn = X @ beta
      -ypredictSKL = skl.predict(X)
      -print(f"MSE with intercept column")
      -print(MSE(y,ypredictOwn))
      -print(f"MSE with intercept column from SKL")
      -print(MSE(y,ypredictSKL))
      +Maxpolydegree = 20
      +X = np.zeros((n,Maxpolydegree))
      +#We include explicitely the intercept column
      +for degree in range(Maxpolydegree):
      +    X[:,degree] = x**degree
      +# We split the data in test and training data
      +X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
       
      +p = Maxpolydegree
      +I = np.eye(p,p)
      +# Decide which values of lambda to use
      +nlambdas = 6
      +MSEOwnRidgePredict = np.zeros(nlambdas)
      +MSERidgePredict = np.zeros(nlambdas)
      +lambdas = np.logspace(-4, 2, nlambdas)
      +for i in range(nlambdas):
      +    lmb = lambdas[i]
      +    OwnRidgeBeta = np.linalg.pinv(X_train.T @ X_train+lmb*I) @ X_train.T @ y_train
      +    # Note: we include the intercept column and no scaling
      +    RegRidge = linear_model.Ridge(lmb,fit_intercept=False)
      +    RegRidge.fit(X_train,y_train)
      +    # and then make the prediction
      +    ytildeOwnRidge = X_train @ OwnRidgeBeta
      +    ypredictOwnRidge = X_test @ OwnRidgeBeta
      +    ytildeRidge = RegRidge.predict(X_train)
      +    ypredictRidge = RegRidge.predict(X_test)
      +    MSEOwnRidgePredict[i] = MSE(y_test,ypredictOwnRidge)
      +    MSERidgePredict[i] = MSE(y_test,ypredictRidge)
      +    print("Beta values for own Ridge implementation")
      +    print(OwnRidgeBeta)
      +    print("Beta values for Scikit-Learn Ridge implementation")
      +    print(RegRidge.coef_)
      +    print("MSE values for own Ridge implementation")
      +    print(MSEOwnRidgePredict[i])
      +    print("MSE values for Scikit-Learn Ridge implementation")
      +    print(MSERidgePredict[i])
       
      +# Now plot the results
       plt.figure()
      -plt.scatter(x, y, label="Data")
      -plt.plot(x, X @ beta, label="Fit")
      -plt.plot(x, skl.predict(X), label="Sklearn (fit_intercept=False)")
      +plt.plot(np.log10(lambdas), MSEOwnRidgePredict, 'r', label = 'MSE own Ridge Test')
      +plt.plot(np.log10(lambdas), MSERidgePredict, 'g', label = 'MSE Ridge Test')
       
      -
      -# Do not include the intercept in the design matrix
      -X = np.zeros((len(x), degree - 1))
      -
      -for p in range(degree - 1):
      -    X[:, p] = x ** (p + 1)
      -
      -# Intercept is not included in the design matrix
      -skl = LinearRegression(fit_intercept=True).fit(X, y)
      -
      -# Use centered values for X and y when computing coefficients
      -y_offset = np.average(y, axis=0)
      -X_offset = np.average(X, axis=0)
      -
      -beta = fit_beta(X - X_offset, y - y_offset)
      -intercept = np.mean(y_offset - X_offset @ beta)
      -
      -print(f"Manual intercept: {intercept}")
      -print(f"Fitted beta (wiothout intercept): {beta}")
      -print(f"Sklearn intercept: {skl.intercept_}")
      -print(f"Sklearn fitted beta (without intercept): {skl.coef_}")
      -ypredictOwn = X @ beta
      -ypredictSKL = skl.predict(X)
      -print(f"MSE with Manual intercept")
      -print(MSE(y,ypredictOwn+intercept))
      -print(f"MSE with Sklearn intercept")
      -print(MSE(y,ypredictSKL))
      -
      -plt.plot(x, X @ beta + intercept, "--", label="Fit (manual intercept)")
      -plt.plot(x, skl.predict(X), "--", label="Sklearn (fit_intercept=True)")
      -plt.grid()
      +plt.xlabel('log10(lambda)')
      +plt.ylabel('MSE')
       plt.legend()
      -
       plt.show()
       
      @@ -411,36 +386,12 @@ plt.show()
      -

      The intercept is the value of our output/target variable -when all our features are zero and our function crosses the \( y \)-axis (for a one-dimensional case). +

      The results here agree when we force Scikit-Learn's Ridge function to include the first column in our design matrix. +We see that the results agree very well. Here we have thus explicitely included the intercept column in the design matrix. +What happens if we do not include the intercept in our fit? +Let us see how we can change this code by zero centering (thanks to Stian Bilek for inpouts here).

      -

      Printing the MSE, we see first that both methods give the same MSE, as -they should. However, when we move to for example Ridge regression, -the way we treat the intercept may give a larger or smaller MSE, -meaning that the MSE can be penalized by the value of the -intercept. Not including the intercept in the fit, means that the -regularization term does not include \( \beta_0 \). For different values -of \( \lambda \), this may lead to differeing MSE values. -

      - -

      To remind the reader, the regularization term, with the intercept in Ridge regression is given by

      -$$ -\lambda \vert\vert \boldsymbol{\beta} \vert\vert_2^2 = \lambda \sum_{j=0}^{p-1}\beta_j^2, -$$ - -

      but when we take out the intercept, this equation becomes

      -$$ -\lambda \vert\vert \boldsymbol{\beta} \vert\vert_2^2 = \lambda \sum_{j=1}^{p-1}\beta_j^2. -$$ - -

      For Lasso regression we have

      -$$ -\lambda \vert\vert \boldsymbol{\beta} \vert\vert_1 = \lambda \sum_{j=1}^{p-1}\vert\beta_j\vert. -$$ - -

      It means that, when scaling the design matrix and the outputs/targets, by subtracting the mean values, we have an optimization problem which is not penalized by the intercept. The MSE value can then be smaller since it focuses only on the remaining quantities. If we however bring back the intercept, we will get a MSE which then contains the intercept.

      -

      diff --git a/doc/pub/week36/html/._week36-bs037.html b/doc/pub/week36/html/._week36-bs037.html index 2ced7b245..cfcab41c0 100644 --- a/doc/pub/week36/html/._week36-bs037.html +++ b/doc/pub/week36/html/._week36-bs037.html @@ -37,7 +37,6 @@ doconce format html week36.do.txt --html_style=bootstrap --pygments_html_style=d
    8697. Plans for week 36
    8698. -
    8699. Thursday September 8
    8700. -
    8701. Summary from last Week and discussion of SVD, Ridge and Lasso regression with examples
    8702. -
    8703. Linear Regression and the SVD
    8704. -
    8705. What does it mean?
    8706. -
    8707. And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
    8708. -
    8709. Code for SVD and Inversion of Matrices
    8710. -
    8711. Inverse of Rectangular Matrix
    8712. -
    8713. Ridge and LASSO Regression
    8714. -
    8715. From OLS to Ridge and Lasso
    8716. -
    8717. Deriving the Ridge Regression Equations
    8718. -
    8719. Note on Scikit-Learn
    8720. -
    8721. Comparison with OLS
    8722. -
    8723. SVD analysis
    8724. -
    8725. Interpreting the Ridge results
    8726. -
    8727. More interpretations
    8728. -
    8729. Deriving the Lasso Regression Equations
    8730. -
    8731. Simple example to illustrate Ordinary Least Squares, Ridge and Lasso Regression
    8732. -
    8733. Ridge Regression
    8734. -
    8735. Lasso Regression
    8736. -
    8737. Yet another Example
    8738. -
    8739. The OLS case
    8740. -
    8741. The Ridge case
    8742. -
    8743. Writing the Cost Function
    8744. -
    8745. Lasso case
    8746. -
    8747. The first Case
    8748. -
    8749. Simple code for solving the above problem
    8750. -
    8751. With Lasso Regression
    8752. -
    8753. Another Example, now with a polynomial fit
    8754. -
    8755. To think about, first part
    8756. -
    8757. More thinking
    8758. -
    8759. Still thinking
    8760. -
    8761. What does centering (subtracting the mean values) mean mathematically?
    8762. -
    8763. Further Manipulations
    8764. -
    8765. Wrapping it up
    8766. -
    8767. Linear Regression code, Intercept handling first
    8768. -
    8769. Code Examples
    8770. -
    8771. Taking out the mean
    8772. -
    8773. Friday September 9
    8774. -
    8775. Linking the regression analysis with a statistical interpretation
    8776. -
    8777. Assumptions made
    8778. -
    8779. Expectation value and variance
    8780. -
    8781. Expectation value and variance for \( \boldsymbol{\beta} \)
    8782. -
    8783. Deriving OLS from a probability distribution
    8784. -
    8785. Independent and Identically Distrubuted (iid)
    8786. -
    8787. Maximum Likelihood Estimation (MLE)
    8788. -
    8789. A new Cost Function
    8790. -
    8791. More basic Statistics and Bayes' theorem
    8792. -
    8793. Marginal Probability
    8794. -
    8795. Conditional Probability
    8796. -
    8797. Bayes' Theorem
    8798. -
    8799. Interpretations of Bayes' Theorem
    8800. -
    8801. Example of Usage of Bayes' theorem
    8802. -
    8803. Doing it correctly
    8804. -
    8805. Bayes' Theorem and Ridge and Lasso Regression
    8806. -
    8807. Test Function for what happens with OLS, Ridge and Lasso
    8808. -
    8809. Invoking Bayes' theorem
    8810. -
    8811. Ridge and Bayes
    8812. -
    8813. Lasso and Bayes
    8814. -
    8815. Exercise 1: mean values and variances in linear regression
    8816. -
    8817. Exercise 2: Adding Ridge and Lasso Regression
    8818. +
    8819. Summary from last Week and discussion of SVD, Ridge and Lasso regression with examples
    8820. +
    8821. Linear Regression and the SVD
    8822. +
    8823. What does it mean?
    8824. +
    8825. And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
    8826. +
    8827. Code for SVD and Inversion of Matrices
    8828. +
    8829. Inverse of Rectangular Matrix
    8830. +
    8831. Ridge and LASSO Regression
    8832. +
    8833. From OLS to Ridge and Lasso
    8834. +
    8835. Deriving the Ridge Regression Equations
    8836. +
    8837. Note on Scikit-Learn
    8838. +
    8839. Comparison with OLS
    8840. +
    8841. SVD analysis
    8842. +
    8843. Interpreting the Ridge results
    8844. +
    8845. More interpretations
    8846. +
    8847. Deriving the Lasso Regression Equations
    8848. +
    8849. Simple example to illustrate Ordinary Least Squares, Ridge and Lasso Regression
    8850. +
    8851. Ridge Regression
    8852. +
    8853. Lasso Regression
    8854. +
    8855. Yet another Example
    8856. +
    8857. The OLS case
    8858. +
    8859. The Ridge case
    8860. +
    8861. Writing the Cost Function
    8862. +
    8863. Lasso case
    8864. +
    8865. The first Case
    8866. +
    8867. Simple code for solving the above problem
    8868. +
    8869. With Lasso Regression
    8870. +
    8871. Another Example, now with a polynomial fit
    8872. +
    8873. To think about, first part
    8874. +
    8875. More thinking
    8876. +
    8877. Still thinking
    8878. +
    8879. What does centering (subtracting the mean values) mean mathematically?
    8880. +
    8881. Further Manipulations
    8882. +
    8883. Wrapping it up
    8884. +
    8885. Linear Regression code, Intercept handling first
    8886. +
    8887. Code Examples
    8888. +
    8889. Taking out the mean
    8890. +
    8891. Friday September 9
    8892. +
    8893. Linking the regression analysis with a statistical interpretation
    8894. +
    8895. Assumptions made
    8896. +
    8897. Expectation value and variance
    8898. +
    8899. Expectation value and variance for \( \boldsymbol{\beta} \)
    8900. +
    8901. Deriving OLS from a probability distribution
    8902. +
    8903. Independent and Identically Distrubuted (iid)
    8904. +
    8905. Maximum Likelihood Estimation (MLE)
    8906. +
    8907. A new Cost Function
    8908. +
    8909. More basic Statistics and Bayes' theorem
    8910. +
    8911. Marginal Probability
    8912. +
    8913. Conditional Probability
    8914. +
    8915. Bayes' Theorem
    8916. +
    8917. Interpretations of Bayes' Theorem
    8918. +
    8919. Example of Usage of Bayes' theorem
    8920. +
    8921. Doing it correctly
    8922. +
    8923. Bayes' Theorem and Ridge and Lasso Regression
    8924. +
    8925. Test Function for what happens with OLS, Ridge and Lasso
    8926. +
    8927. Invoking Bayes' theorem
    8928. +
    8929. Ridge and Bayes
    8930. +
    8931. Lasso and Bayes
    8932. +
    8933. Exercise 1: mean values and variances in linear regression
    8934. +
    8935. Exercise 2: Adding Ridge and Lasso Regression
    8936. @@ -297,10 +295,7 @@ MathJax.Hub.Config({

       

       

       

      -

      Code Examples

      - -

      Armed with this wisdom, we attempt first to simply set the intercept equal to False in our implementation of Ridge regression for our well-known vanilla data set.

      - +

      Taking out the mean

      @@ -313,62 +308,75 @@ MathJax.Hub.Config({ import matplotlib.pyplot as plt from sklearn.model_selection import train_test_split from sklearn import linear_model +from sklearn.preprocessing import StandardScaler def MSE(y_data,y_model): n = np.size(y_model) return np.sum((y_data-y_model)**2)/n - - # A seed just to ensure that the random numbers are the same for every run. # Useful for eventual debugging. -np.random.seed(3155) +np.random.seed(315) n = 100 x = np.random.rand(n) y = np.exp(-x**2) + 1.5 * np.exp(-(x-2)**2) Maxpolydegree = 20 -X = np.zeros((n,Maxpolydegree)) -#We include explicitely the intercept column -for degree in range(Maxpolydegree): - X[:,degree] = x**degree +X = np.zeros((n,Maxpolydegree-1)) + +for degree in range(1,Maxpolydegree): #No intercept column + X[:,degree-1] = x**(degree) + # We split the data in test and training data X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2) -p = Maxpolydegree +#For our own implementation, we will need to deal with the intercept by centering the design matrix and the target variable +X_train_mean = np.mean(X_train,axis=0) +#Center by removing mean from each feature +X_train_scaled = X_train - X_train_mean +X_test_scaled = X_test - X_train_mean +#The model intercept (called y_scaler) is given by the mean of the target variable (IF X is centered) +#Remove the intercept from the training data. +y_scaler = np.mean(y_train) +y_train_scaled = y_train - y_scaler + +p = Maxpolydegree-1 I = np.eye(p,p) # Decide which values of lambda to use nlambdas = 6 MSEOwnRidgePredict = np.zeros(nlambdas) MSERidgePredict = np.zeros(nlambdas) + lambdas = np.logspace(-4, 2, nlambdas) for i in range(nlambdas): lmb = lambdas[i] - OwnRidgeBeta = np.linalg.pinv(X_train.T @ X_train+lmb*I) @ X_train.T @ y_train - # Note: we include the intercept column and no scaling - RegRidge = linear_model.Ridge(lmb,fit_intercept=False) + OwnRidgeBeta = np.linalg.pinv(X_train_scaled.T @ X_train_scaled+lmb*I) @ X_train_scaled.T @ (y_train_scaled) + intercept_ = y_scaler - X_train_mean@OwnRidgeBeta #The intercept can be shifted so the model can predict on uncentered data + #Add intercept to prediction + ypredictOwnRidge = X_test_scaled @ OwnRidgeBeta + y_scaler + RegRidge = linear_model.Ridge(lmb) RegRidge.fit(X_train,y_train) - # and then make the prediction - ytildeOwnRidge = X_train @ OwnRidgeBeta - ypredictOwnRidge = X_test @ OwnRidgeBeta - ytildeRidge = RegRidge.predict(X_train) ypredictRidge = RegRidge.predict(X_test) MSEOwnRidgePredict[i] = MSE(y_test,ypredictOwnRidge) MSERidgePredict[i] = MSE(y_test,ypredictRidge) print("Beta values for own Ridge implementation") - print(OwnRidgeBeta) + print(OwnRidgeBeta) #Intercept is given by mean of target variable print("Beta values for Scikit-Learn Ridge implementation") print(RegRidge.coef_) + print('Intercept from own implementation:') + print(intercept_) + print('Intercept from Scikit-Learn Ridge implementation') + print(RegRidge.intercept_) print("MSE values for own Ridge implementation") print(MSEOwnRidgePredict[i]) print("MSE values for Scikit-Learn Ridge implementation") print(MSERidgePredict[i]) + # Now plot the results plt.figure() -plt.plot(np.log10(lambdas), MSEOwnRidgePredict, 'r', label = 'MSE own Ridge Test') -plt.plot(np.log10(lambdas), MSERidgePredict, 'g', label = 'MSE Ridge Test') - +plt.plot(np.log10(lambdas), MSEOwnRidgePredict, 'b--', label = 'MSE own Ridge Test') +plt.plot(np.log10(lambdas), MSERidgePredict, 'g--', label = 'MSE SL Ridge Test') plt.xlabel('log10(lambda)') plt.ylabel('MSE') plt.legend() @@ -388,10 +396,14 @@ plt.show()
      -

      The results here agree when we force Scikit-Learn's Ridge function to include the first column in our design matrix. -We see that the results agree very well. Here we have thus explicitely included the intercept column in the design matrix. -What happens if we do not include the intercept in our fit? -Let us see how we can change this code by zero centering (thanks to Stian Bilek for inpouts here). +

      We see here, when compared to the code which includes explicitely the +intercept column, that our MSE value is actually smaller. This is +because the regularization term does not include the intercept value +\( \beta_0 \) in the fitting. This applies to Lasso regularization as +well. It means that our optimization is now done only with the +centered matrix and/or vector that enter the fitting procedure. Note +also that the problem with the intercept occurs mainly in these type +of polynomial fitting problem.

      @@ -419,7 +431,7 @@ Let us see how we can change this code by zero centering (thanks to Stian Bilek

    8937. 46
    8938. 47
    8939. ...
    8940. -
    8941. 60
    8942. +
    8943. 59
    8944. »
    8945. diff --git a/doc/pub/week36/html/._week36-bs038.html b/doc/pub/week36/html/._week36-bs038.html index 6d2422437..767fb68fd 100644 --- a/doc/pub/week36/html/._week36-bs038.html +++ b/doc/pub/week36/html/._week36-bs038.html @@ -37,7 +37,6 @@ doconce format html week36.do.txt --html_style=bootstrap --pygments_html_style=d
    8946. Plans for week 36
    8947. -
    8948. Thursday September 8
    8949. -
    8950. Summary from last Week and discussion of SVD, Ridge and Lasso regression with examples
    8951. -
    8952. Linear Regression and the SVD
    8953. -
    8954. What does it mean?
    8955. -
    8956. And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
    8957. -
    8958. Code for SVD and Inversion of Matrices
    8959. -
    8960. Inverse of Rectangular Matrix
    8961. -
    8962. Ridge and LASSO Regression
    8963. -
    8964. From OLS to Ridge and Lasso
    8965. -
    8966. Deriving the Ridge Regression Equations
    8967. -
    8968. Note on Scikit-Learn
    8969. -
    8970. Comparison with OLS
    8971. -
    8972. SVD analysis
    8973. -
    8974. Interpreting the Ridge results
    8975. -
    8976. More interpretations
    8977. -
    8978. Deriving the Lasso Regression Equations
    8979. -
    8980. Simple example to illustrate Ordinary Least Squares, Ridge and Lasso Regression
    8981. -
    8982. Ridge Regression
    8983. -
    8984. Lasso Regression
    8985. -
    8986. Yet another Example
    8987. -
    8988. The OLS case
    8989. -
    8990. The Ridge case
    8991. -
    8992. Writing the Cost Function
    8993. -
    8994. Lasso case
    8995. -
    8996. The first Case
    8997. -
    8998. Simple code for solving the above problem
    8999. -
    9000. With Lasso Regression
    9001. -
    9002. Another Example, now with a polynomial fit
    9003. -
    9004. To think about, first part
    9005. -
    9006. More thinking
    9007. -
    9008. Still thinking
    9009. -
    9010. What does centering (subtracting the mean values) mean mathematically?
    9011. -
    9012. Further Manipulations
    9013. -
    9014. Wrapping it up
    9015. -
    9016. Linear Regression code, Intercept handling first
    9017. -
    9018. Code Examples
    9019. -
    9020. Taking out the mean
    9021. -
    9022. Friday September 9
    9023. -
    9024. Linking the regression analysis with a statistical interpretation
    9025. -
    9026. Assumptions made
    9027. -
    9028. Expectation value and variance
    9029. -
    9030. Expectation value and variance for \( \boldsymbol{\beta} \)
    9031. -
    9032. Deriving OLS from a probability distribution
    9033. -
    9034. Independent and Identically Distrubuted (iid)
    9035. -
    9036. Maximum Likelihood Estimation (MLE)
    9037. -
    9038. A new Cost Function
    9039. -
    9040. More basic Statistics and Bayes' theorem
    9041. -
    9042. Marginal Probability
    9043. -
    9044. Conditional Probability
    9045. -
    9046. Bayes' Theorem
    9047. -
    9048. Interpretations of Bayes' Theorem
    9049. -
    9050. Example of Usage of Bayes' theorem
    9051. -
    9052. Doing it correctly
    9053. -
    9054. Bayes' Theorem and Ridge and Lasso Regression
    9055. -
    9056. Test Function for what happens with OLS, Ridge and Lasso
    9057. -
    9058. Invoking Bayes' theorem
    9059. -
    9060. Ridge and Bayes
    9061. -
    9062. Lasso and Bayes
    9063. -
    9064. Exercise 1: mean values and variances in linear regression
    9065. -
    9066. Exercise 2: Adding Ridge and Lasso Regression
    9067. +
    9068. Summary from last Week and discussion of SVD, Ridge and Lasso regression with examples
    9069. +
    9070. Linear Regression and the SVD
    9071. +
    9072. What does it mean?
    9073. +
    9074. And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
    9075. +
    9076. Code for SVD and Inversion of Matrices
    9077. +
    9078. Inverse of Rectangular Matrix
    9079. +
    9080. Ridge and LASSO Regression
    9081. +
    9082. From OLS to Ridge and Lasso
    9083. +
    9084. Deriving the Ridge Regression Equations
    9085. +
    9086. Note on Scikit-Learn
    9087. +
    9088. Comparison with OLS
    9089. +
    9090. SVD analysis
    9091. +
    9092. Interpreting the Ridge results
    9093. +
    9094. More interpretations
    9095. +
    9096. Deriving the Lasso Regression Equations
    9097. +
    9098. Simple example to illustrate Ordinary Least Squares, Ridge and Lasso Regression
    9099. +
    9100. Ridge Regression
    9101. +
    9102. Lasso Regression
    9103. +
    9104. Yet another Example
    9105. +
    9106. The OLS case
    9107. +
    9108. The Ridge case
    9109. +
    9110. Writing the Cost Function
    9111. +
    9112. Lasso case
    9113. +
    9114. The first Case
    9115. +
    9116. Simple code for solving the above problem
    9117. +
    9118. With Lasso Regression
    9119. +
    9120. Another Example, now with a polynomial fit
    9121. +
    9122. To think about, first part
    9123. +
    9124. More thinking
    9125. +
    9126. Still thinking
    9127. +
    9128. What does centering (subtracting the mean values) mean mathematically?
    9129. +
    9130. Further Manipulations
    9131. +
    9132. Wrapping it up
    9133. +
    9134. Linear Regression code, Intercept handling first
    9135. +
    9136. Code Examples
    9137. +
    9138. Taking out the mean
    9139. +
    9140. Friday September 9
    9141. +
    9142. Linking the regression analysis with a statistical interpretation
    9143. +
    9144. Assumptions made
    9145. +
    9146. Expectation value and variance
    9147. +
    9148. Expectation value and variance for \( \boldsymbol{\beta} \)
    9149. +
    9150. Deriving OLS from a probability distribution
    9151. +
    9152. Independent and Identically Distrubuted (iid)
    9153. +
    9154. Maximum Likelihood Estimation (MLE)
    9155. +
    9156. A new Cost Function
    9157. +
    9158. More basic Statistics and Bayes' theorem
    9159. +
    9160. Marginal Probability
    9161. +
    9162. Conditional Probability
    9163. +
    9164. Bayes' Theorem
    9165. +
    9166. Interpretations of Bayes' Theorem
    9167. +
    9168. Example of Usage of Bayes' theorem
    9169. +
    9170. Doing it correctly
    9171. +
    9172. Bayes' Theorem and Ridge and Lasso Regression
    9173. +
    9174. Test Function for what happens with OLS, Ridge and Lasso
    9175. +
    9176. Invoking Bayes' theorem
    9177. +
    9178. Ridge and Bayes
    9179. +
    9180. Lasso and Bayes
    9181. +
    9182. Exercise 1: mean values and variances in linear regression
    9183. +
    9184. Exercise 2: Adding Ridge and Lasso Regression
    9185. @@ -297,116 +295,7 @@ MathJax.Hub.Config({

       

       

       

      -

      Taking out the mean

      - - -
      -
      -
      -
      -
      -
      import numpy as np
      -import pandas as pd
      -import matplotlib.pyplot as plt
      -from sklearn.model_selection import train_test_split
      -from sklearn import linear_model
      -from sklearn.preprocessing import StandardScaler
      -
      -def MSE(y_data,y_model):
      -    n = np.size(y_model)
      -    return np.sum((y_data-y_model)**2)/n
      -# A seed just to ensure that the random numbers are the same for every run.
      -# Useful for eventual debugging.
      -np.random.seed(315)
      -
      -n = 100
      -x = np.random.rand(n)
      -y = np.exp(-x**2) + 1.5 * np.exp(-(x-2)**2)
      -
      -Maxpolydegree = 20
      -X = np.zeros((n,Maxpolydegree-1))
      -
      -for degree in range(1,Maxpolydegree): #No intercept column
      -    X[:,degree-1] = x**(degree)
      -
      -# We split the data in test and training data
      -X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
      -
      -#For our own implementation, we will need to deal with the intercept by centering the design matrix and the target variable
      -X_train_mean = np.mean(X_train,axis=0)
      -#Center by removing mean from each feature
      -X_train_scaled = X_train - X_train_mean 
      -X_test_scaled = X_test - X_train_mean
      -#The model intercept (called y_scaler) is given by the mean of the target variable (IF X is centered)
      -#Remove the intercept from the training data.
      -y_scaler = np.mean(y_train)           
      -y_train_scaled = y_train - y_scaler   
      -
      -p = Maxpolydegree-1
      -I = np.eye(p,p)
      -# Decide which values of lambda to use
      -nlambdas = 6
      -MSEOwnRidgePredict = np.zeros(nlambdas)
      -MSERidgePredict = np.zeros(nlambdas)
      -
      -lambdas = np.logspace(-4, 2, nlambdas)
      -for i in range(nlambdas):
      -    lmb = lambdas[i]
      -    OwnRidgeBeta = np.linalg.pinv(X_train_scaled.T @ X_train_scaled+lmb*I) @ X_train_scaled.T @ (y_train_scaled)
      -    intercept_ = y_scaler - X_train_mean@OwnRidgeBeta #The intercept can be shifted so the model can predict on uncentered data
      -    #Add intercept to prediction
      -    ypredictOwnRidge = X_test_scaled @ OwnRidgeBeta + y_scaler 
      -    RegRidge = linear_model.Ridge(lmb)
      -    RegRidge.fit(X_train,y_train)
      -    ypredictRidge = RegRidge.predict(X_test)
      -    MSEOwnRidgePredict[i] = MSE(y_test,ypredictOwnRidge)
      -    MSERidgePredict[i] = MSE(y_test,ypredictRidge)
      -    print("Beta values for own Ridge implementation")
      -    print(OwnRidgeBeta) #Intercept is given by mean of target variable
      -    print("Beta values for Scikit-Learn Ridge implementation")
      -    print(RegRidge.coef_)
      -    print('Intercept from own implementation:')
      -    print(intercept_)
      -    print('Intercept from Scikit-Learn Ridge implementation')
      -    print(RegRidge.intercept_)
      -    print("MSE values for own Ridge implementation")
      -    print(MSEOwnRidgePredict[i])
      -    print("MSE values for Scikit-Learn Ridge implementation")
      -    print(MSERidgePredict[i])
      -
      -
      -# Now plot the results
      -plt.figure()
      -plt.plot(np.log10(lambdas), MSEOwnRidgePredict, 'b--', label = 'MSE own Ridge Test')
      -plt.plot(np.log10(lambdas), MSERidgePredict, 'g--', label = 'MSE SL Ridge Test')
      -plt.xlabel('log10(lambda)')
      -plt.ylabel('MSE')
      -plt.legend()
      -plt.show()
      -
      -
      -
      -
      -
      -
      -
      -
      -
      -
      -
      -
      -
      -
      - -

      We see here, when compared to the code which includes explicitely the -intercept column, that our MSE value is actually smaller. This is -because the regularization term does not include the intercept value -\( \beta_0 \) in the fitting. This applies to Lasso regularization as -well. It means that our optimization is now done only with the -centered matrix and/or vector that enter the fitting procedure. Note -also that the problem with the intercept occurs mainly in these type -of polynomial fitting problem. -

      +

      Friday September 9

      @@ -433,7 +322,7 @@ of polynomial fitting problem.

    9186. 47
    9187. 48
    9188. ...
    9189. -
    9190. 60
    9191. +
    9192. 59
    9193. »
    9194. diff --git a/doc/pub/week36/html/._week36-bs039.html b/doc/pub/week36/html/._week36-bs039.html index bec8f70a3..f255d2e5a 100644 --- a/doc/pub/week36/html/._week36-bs039.html +++ b/doc/pub/week36/html/._week36-bs039.html @@ -37,7 +37,6 @@ doconce format html week36.do.txt --html_style=bootstrap --pygments_html_style=d
    9195. Plans for week 36
    9196. -
    9197. Thursday September 8
    9198. -
    9199. Summary from last Week and discussion of SVD, Ridge and Lasso regression with examples
    9200. -
    9201. Linear Regression and the SVD
    9202. -
    9203. What does it mean?
    9204. -
    9205. And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
    9206. -
    9207. Code for SVD and Inversion of Matrices
    9208. -
    9209. Inverse of Rectangular Matrix
    9210. -
    9211. Ridge and LASSO Regression
    9212. -
    9213. From OLS to Ridge and Lasso
    9214. -
    9215. Deriving the Ridge Regression Equations
    9216. -
    9217. Note on Scikit-Learn
    9218. -
    9219. Comparison with OLS
    9220. -
    9221. SVD analysis
    9222. -
    9223. Interpreting the Ridge results
    9224. -
    9225. More interpretations
    9226. -
    9227. Deriving the Lasso Regression Equations
    9228. -
    9229. Simple example to illustrate Ordinary Least Squares, Ridge and Lasso Regression
    9230. -
    9231. Ridge Regression
    9232. -
    9233. Lasso Regression
    9234. -
    9235. Yet another Example
    9236. -
    9237. The OLS case
    9238. -
    9239. The Ridge case
    9240. -
    9241. Writing the Cost Function
    9242. -
    9243. Lasso case
    9244. -
    9245. The first Case
    9246. -
    9247. Simple code for solving the above problem
    9248. -
    9249. With Lasso Regression
    9250. -
    9251. Another Example, now with a polynomial fit
    9252. -
    9253. To think about, first part
    9254. -
    9255. More thinking
    9256. -
    9257. Still thinking
    9258. -
    9259. What does centering (subtracting the mean values) mean mathematically?
    9260. -
    9261. Further Manipulations
    9262. -
    9263. Wrapping it up
    9264. -
    9265. Linear Regression code, Intercept handling first
    9266. -
    9267. Code Examples
    9268. -
    9269. Taking out the mean
    9270. -
    9271. Friday September 9
    9272. -
    9273. Linking the regression analysis with a statistical interpretation
    9274. -
    9275. Assumptions made
    9276. -
    9277. Expectation value and variance
    9278. -
    9279. Expectation value and variance for \( \boldsymbol{\beta} \)
    9280. -
    9281. Deriving OLS from a probability distribution
    9282. -
    9283. Independent and Identically Distrubuted (iid)
    9284. -
    9285. Maximum Likelihood Estimation (MLE)
    9286. -
    9287. A new Cost Function
    9288. -
    9289. More basic Statistics and Bayes' theorem
    9290. -
    9291. Marginal Probability
    9292. -
    9293. Conditional Probability
    9294. -
    9295. Bayes' Theorem
    9296. -
    9297. Interpretations of Bayes' Theorem
    9298. -
    9299. Example of Usage of Bayes' theorem
    9300. -
    9301. Doing it correctly
    9302. -
    9303. Bayes' Theorem and Ridge and Lasso Regression
    9304. -
    9305. Test Function for what happens with OLS, Ridge and Lasso
    9306. -
    9307. Invoking Bayes' theorem
    9308. -
    9309. Ridge and Bayes
    9310. -
    9311. Lasso and Bayes
    9312. -
    9313. Exercise 1: mean values and variances in linear regression
    9314. -
    9315. Exercise 2: Adding Ridge and Lasso Regression
    9316. +
    9317. Summary from last Week and discussion of SVD, Ridge and Lasso regression with examples
    9318. +
    9319. Linear Regression and the SVD
    9320. +
    9321. What does it mean?
    9322. +
    9323. And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
    9324. +
    9325. Code for SVD and Inversion of Matrices
    9326. +
    9327. Inverse of Rectangular Matrix
    9328. +
    9329. Ridge and LASSO Regression
    9330. +
    9331. From OLS to Ridge and Lasso
    9332. +
    9333. Deriving the Ridge Regression Equations
    9334. +
    9335. Note on Scikit-Learn
    9336. +
    9337. Comparison with OLS
    9338. +
    9339. SVD analysis
    9340. +
    9341. Interpreting the Ridge results
    9342. +
    9343. More interpretations
    9344. +
    9345. Deriving the Lasso Regression Equations
    9346. +
    9347. Simple example to illustrate Ordinary Least Squares, Ridge and Lasso Regression
    9348. +
    9349. Ridge Regression
    9350. +
    9351. Lasso Regression
    9352. +
    9353. Yet another Example
    9354. +
    9355. The OLS case
    9356. +
    9357. The Ridge case
    9358. +
    9359. Writing the Cost Function
    9360. +
    9361. Lasso case
    9362. +
    9363. The first Case
    9364. +
    9365. Simple code for solving the above problem
    9366. +
    9367. With Lasso Regression
    9368. +
    9369. Another Example, now with a polynomial fit
    9370. +
    9371. To think about, first part
    9372. +
    9373. More thinking
    9374. +
    9375. Still thinking
    9376. +
    9377. What does centering (subtracting the mean values) mean mathematically?
    9378. +
    9379. Further Manipulations
    9380. +
    9381. Wrapping it up
    9382. +
    9383. Linear Regression code, Intercept handling first
    9384. +
    9385. Code Examples
    9386. +
    9387. Taking out the mean
    9388. +
    9389. Friday September 9
    9390. +
    9391. Linking the regression analysis with a statistical interpretation
    9392. +
    9393. Assumptions made
    9394. +
    9395. Expectation value and variance
    9396. +
    9397. Expectation value and variance for \( \boldsymbol{\beta} \)
    9398. +
    9399. Deriving OLS from a probability distribution
    9400. +
    9401. Independent and Identically Distrubuted (iid)
    9402. +
    9403. Maximum Likelihood Estimation (MLE)
    9404. +
    9405. A new Cost Function
    9406. +
    9407. More basic Statistics and Bayes' theorem
    9408. +
    9409. Marginal Probability
    9410. +
    9411. Conditional Probability
    9412. +
    9413. Bayes' Theorem
    9414. +
    9415. Interpretations of Bayes' Theorem
    9416. +
    9417. Example of Usage of Bayes' theorem
    9418. +
    9419. Doing it correctly
    9420. +
    9421. Bayes' Theorem and Ridge and Lasso Regression
    9422. +
    9423. Test Function for what happens with OLS, Ridge and Lasso
    9424. +
    9425. Invoking Bayes' theorem
    9426. +
    9427. Ridge and Bayes
    9428. +
    9429. Lasso and Bayes
    9430. +
    9431. Exercise 1: mean values and variances in linear regression
    9432. +
    9433. Exercise 2: Adding Ridge and Lasso Regression
    9434. @@ -296,8 +294,50 @@ MathJax.Hub.Config({

       

       

       

      - -

      Friday September 9

      + +

      Linking the regression analysis with a statistical interpretation

      + +

      We will now couple the discussions of ordinary least squares, Ridge +and Lasso regression with a statistical interpretation, that is we +move from a linear algebra analysis to a statistical analysis. In +particular, we will focus on what the regularization terms can result +in. We will amongst other things show that the regularization +parameter can reduce considerably the variance of the parameters +\( \beta \). +

      + +

      The +advantage of doing linear regression is that we actually end up with +analytical expressions for several statistical quantities. +Standard least squares and Ridge regression allow us to +derive quantities like the variance and other expectation values in a +rather straightforward way. +

      + +

      It is assumed that \( \varepsilon_i +\sim \mathcal{N}(0, \sigma^2) \) and the \( \varepsilon_{i} \) are +independent, i.e.: +

      +$$ +\begin{align*} +\mbox{Cov}(\varepsilon_{i_1}, +\varepsilon_{i_2}) & = \left\{ \begin{array}{lcc} \sigma^2 & \mbox{if} +& i_1 = i_2, \\ 0 & \mbox{if} & i_1 \not= i_2. \end{array} \right. +\end{align*} +$$ + +

      The randomness of \( \varepsilon_i \) implies that +\( \mathbf{y}_i \) is also a random variable. In particular, +\( \mathbf{y}_i \) is normally distributed, because \( \varepsilon_i \sim +\mathcal{N}(0, \sigma^2) \) and \( \mathbf{X}_{i,\ast} \, \boldsymbol{\beta} \) is a +non-random scalar. To specify the parameters of the distribution of +\( \mathbf{y}_i \) we need to calculate its first two moments. +

      + +

      Recall that \( \boldsymbol{X} \) is a matrix of dimensionality \( n\times p \). The +notation above \( \mathbf{X}_{i,\ast} \) means that we are looking at the +row number \( i \) and perform a sum over all values \( p \). +

      @@ -324,7 +364,7 @@ MathJax.Hub.Config({

    9435. 48
    9436. 49
    9437. ...
    9438. -
    9439. 60
    9440. +
    9441. 59
    9442. »
    9443. diff --git a/doc/pub/week36/html/._week36-bs040.html b/doc/pub/week36/html/._week36-bs040.html index 338bd46f3..d09b6f9cd 100644 --- a/doc/pub/week36/html/._week36-bs040.html +++ b/doc/pub/week36/html/._week36-bs040.html @@ -37,7 +37,6 @@ doconce format html week36.do.txt --html_style=bootstrap --pygments_html_style=d
    9444. Plans for week 36
    9445. -
    9446. Thursday September 8
    9447. -
    9448. Summary from last Week and discussion of SVD, Ridge and Lasso regression with examples
    9449. -
    9450. Linear Regression and the SVD
    9451. -
    9452. What does it mean?
    9453. -
    9454. And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
    9455. -
    9456. Code for SVD and Inversion of Matrices
    9457. -
    9458. Inverse of Rectangular Matrix
    9459. -
    9460. Ridge and LASSO Regression
    9461. -
    9462. From OLS to Ridge and Lasso
    9463. -
    9464. Deriving the Ridge Regression Equations
    9465. -
    9466. Note on Scikit-Learn
    9467. -
    9468. Comparison with OLS
    9469. -
    9470. SVD analysis
    9471. -
    9472. Interpreting the Ridge results
    9473. -
    9474. More interpretations
    9475. -
    9476. Deriving the Lasso Regression Equations
    9477. -
    9478. Simple example to illustrate Ordinary Least Squares, Ridge and Lasso Regression
    9479. -
    9480. Ridge Regression
    9481. -
    9482. Lasso Regression
    9483. -
    9484. Yet another Example
    9485. -
    9486. The OLS case
    9487. -
    9488. The Ridge case
    9489. -
    9490. Writing the Cost Function
    9491. -
    9492. Lasso case
    9493. -
    9494. The first Case
    9495. -
    9496. Simple code for solving the above problem
    9497. -
    9498. With Lasso Regression
    9499. -
    9500. Another Example, now with a polynomial fit
    9501. -
    9502. To think about, first part
    9503. -
    9504. More thinking
    9505. -
    9506. Still thinking
    9507. -
    9508. What does centering (subtracting the mean values) mean mathematically?
    9509. -
    9510. Further Manipulations
    9511. -
    9512. Wrapping it up
    9513. -
    9514. Linear Regression code, Intercept handling first
    9515. -
    9516. Code Examples
    9517. -
    9518. Taking out the mean
    9519. -
    9520. Friday September 9
    9521. -
    9522. Linking the regression analysis with a statistical interpretation
    9523. -
    9524. Assumptions made
    9525. -
    9526. Expectation value and variance
    9527. -
    9528. Expectation value and variance for \( \boldsymbol{\beta} \)
    9529. -
    9530. Deriving OLS from a probability distribution
    9531. -
    9532. Independent and Identically Distrubuted (iid)
    9533. -
    9534. Maximum Likelihood Estimation (MLE)
    9535. -
    9536. A new Cost Function
    9537. -
    9538. More basic Statistics and Bayes' theorem
    9539. -
    9540. Marginal Probability
    9541. -
    9542. Conditional Probability
    9543. -
    9544. Bayes' Theorem
    9545. -
    9546. Interpretations of Bayes' Theorem
    9547. -
    9548. Example of Usage of Bayes' theorem
    9549. -
    9550. Doing it correctly
    9551. -
    9552. Bayes' Theorem and Ridge and Lasso Regression
    9553. -
    9554. Test Function for what happens with OLS, Ridge and Lasso
    9555. -
    9556. Invoking Bayes' theorem
    9557. -
    9558. Ridge and Bayes
    9559. -
    9560. Lasso and Bayes
    9561. -
    9562. Exercise 1: mean values and variances in linear regression
    9563. -
    9564. Exercise 2: Adding Ridge and Lasso Regression
    9565. +
    9566. Summary from last Week and discussion of SVD, Ridge and Lasso regression with examples
    9567. +
    9568. Linear Regression and the SVD
    9569. +
    9570. What does it mean?
    9571. +
    9572. And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
    9573. +
    9574. Code for SVD and Inversion of Matrices
    9575. +
    9576. Inverse of Rectangular Matrix
    9577. +
    9578. Ridge and LASSO Regression
    9579. +
    9580. From OLS to Ridge and Lasso
    9581. +
    9582. Deriving the Ridge Regression Equations
    9583. +
    9584. Note on Scikit-Learn
    9585. +
    9586. Comparison with OLS
    9587. +
    9588. SVD analysis
    9589. +
    9590. Interpreting the Ridge results
    9591. +
    9592. More interpretations
    9593. +
    9594. Deriving the Lasso Regression Equations
    9595. +
    9596. Simple example to illustrate Ordinary Least Squares, Ridge and Lasso Regression
    9597. +
    9598. Ridge Regression
    9599. +
    9600. Lasso Regression
    9601. +
    9602. Yet another Example
    9603. +
    9604. The OLS case
    9605. +
    9606. The Ridge case
    9607. +
    9608. Writing the Cost Function
    9609. +
    9610. Lasso case
    9611. +
    9612. The first Case
    9613. +
    9614. Simple code for solving the above problem
    9615. +
    9616. With Lasso Regression
    9617. +
    9618. Another Example, now with a polynomial fit
    9619. +
    9620. To think about, first part
    9621. +
    9622. More thinking
    9623. +
    9624. Still thinking
    9625. +
    9626. What does centering (subtracting the mean values) mean mathematically?
    9627. +
    9628. Further Manipulations
    9629. +
    9630. Wrapping it up
    9631. +
    9632. Linear Regression code, Intercept handling first
    9633. +
    9634. Code Examples
    9635. +
    9636. Taking out the mean
    9637. +
    9638. Friday September 9
    9639. +
    9640. Linking the regression analysis with a statistical interpretation
    9641. +
    9642. Assumptions made
    9643. +
    9644. Expectation value and variance
    9645. +
    9646. Expectation value and variance for \( \boldsymbol{\beta} \)
    9647. +
    9648. Deriving OLS from a probability distribution
    9649. +
    9650. Independent and Identically Distrubuted (iid)
    9651. +
    9652. Maximum Likelihood Estimation (MLE)
    9653. +
    9654. A new Cost Function
    9655. +
    9656. More basic Statistics and Bayes' theorem
    9657. +
    9658. Marginal Probability
    9659. +
    9660. Conditional Probability
    9661. +
    9662. Bayes' Theorem
    9663. +
    9664. Interpretations of Bayes' Theorem
    9665. +
    9666. Example of Usage of Bayes' theorem
    9667. +
    9668. Doing it correctly
    9669. +
    9670. Bayes' Theorem and Ridge and Lasso Regression
    9671. +
    9672. Test Function for what happens with OLS, Ridge and Lasso
    9673. +
    9674. Invoking Bayes' theorem
    9675. +
    9676. Ridge and Bayes
    9677. +
    9678. Lasso and Bayes
    9679. +
    9680. Exercise 1: mean values and variances in linear regression
    9681. +
    9682. Exercise 2: Adding Ridge and Lasso Regression
    9683. @@ -296,50 +294,24 @@ MathJax.Hub.Config({

       

       

       

      - -

      Linking the regression analysis with a statistical interpretation

      + +

      Assumptions made

      -

      We will now couple the discussions of ordinary least squares, Ridge -and Lasso regression with a statistical interpretation, that is we -move from a linear algebra analysis to a statistical analysis. In -particular, we will focus on what the regularization terms can result -in. We will amongst other things show that the regularization -parameter can reduce considerably the variance of the parameters -\( \beta \). -

      - -

      The -advantage of doing linear regression is that we actually end up with -analytical expressions for several statistical quantities. -Standard least squares and Ridge regression allow us to -derive quantities like the variance and other expectation values in a -rather straightforward way. -

      - -

      It is assumed that \( \varepsilon_i -\sim \mathcal{N}(0, \sigma^2) \) and the \( \varepsilon_{i} \) are -independent, i.e.: +

      The assumption we have made here can be summarized as (and this is going to be useful when we discuss the bias-variance trade off) +that there exists a function \( f(\boldsymbol{x}) \) and a normal distributed error \( \boldsymbol{\varepsilon}\sim \mathcal{N}(0, \sigma^2) \) +which describe our data

      $$ -\begin{align*} -\mbox{Cov}(\varepsilon_{i_1}, -\varepsilon_{i_2}) & = \left\{ \begin{array}{lcc} \sigma^2 & \mbox{if} -& i_1 = i_2, \\ 0 & \mbox{if} & i_1 \not= i_2. \end{array} \right. -\end{align*} +\boldsymbol{y} = f(\boldsymbol{x})+\boldsymbol{\varepsilon} $$ -

      The randomness of \( \varepsilon_i \) implies that -\( \mathbf{y}_i \) is also a random variable. In particular, -\( \mathbf{y}_i \) is normally distributed, because \( \varepsilon_i \sim -\mathcal{N}(0, \sigma^2) \) and \( \mathbf{X}_{i,\ast} \, \boldsymbol{\beta} \) is a -non-random scalar. To specify the parameters of the distribution of -\( \mathbf{y}_i \) we need to calculate its first two moments. +

      We approximate this function with our model from the solution of the linear regression equations, that is our +function \( f \) is approximated by \( \boldsymbol{\tilde{y}} \) where we want to minimize \( (\boldsymbol{y}-\boldsymbol{\tilde{y}})^2 \), our MSE, with

      +$$ +\boldsymbol{\tilde{y}} = \boldsymbol{X}\boldsymbol{\beta}. +$$ -

      Recall that \( \boldsymbol{X} \) is a matrix of dimensionality \( n\times p \). The -notation above \( \mathbf{X}_{i,\ast} \) means that we are looking at the -row number \( i \) and perform a sum over all values \( p \). -

      @@ -366,7 +338,7 @@ row number \( i \) and perform a sum over all values \( p \).

    9684. 49
    9685. 50
    9686. ...
    9687. -
    9688. 60
    9689. +
    9690. 59
    9691. »
    9692. diff --git a/doc/pub/week36/html/._week36-bs041.html b/doc/pub/week36/html/._week36-bs041.html index 711c5de52..3a6cf832a 100644 --- a/doc/pub/week36/html/._week36-bs041.html +++ b/doc/pub/week36/html/._week36-bs041.html @@ -37,7 +37,6 @@ doconce format html week36.do.txt --html_style=bootstrap --pygments_html_style=d
    9693. Plans for week 36
    9694. -
    9695. Thursday September 8
    9696. -
    9697. Summary from last Week and discussion of SVD, Ridge and Lasso regression with examples
    9698. -
    9699. Linear Regression and the SVD
    9700. -
    9701. What does it mean?
    9702. -
    9703. And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
    9704. -
    9705. Code for SVD and Inversion of Matrices
    9706. -
    9707. Inverse of Rectangular Matrix
    9708. -
    9709. Ridge and LASSO Regression
    9710. -
    9711. From OLS to Ridge and Lasso
    9712. -
    9713. Deriving the Ridge Regression Equations
    9714. -
    9715. Note on Scikit-Learn
    9716. -
    9717. Comparison with OLS
    9718. -
    9719. SVD analysis
    9720. -
    9721. Interpreting the Ridge results
    9722. -
    9723. More interpretations
    9724. -
    9725. Deriving the Lasso Regression Equations
    9726. -
    9727. Simple example to illustrate Ordinary Least Squares, Ridge and Lasso Regression
    9728. -
    9729. Ridge Regression
    9730. -
    9731. Lasso Regression
    9732. -
    9733. Yet another Example
    9734. -
    9735. The OLS case
    9736. -
    9737. The Ridge case
    9738. -
    9739. Writing the Cost Function
    9740. -
    9741. Lasso case
    9742. -
    9743. The first Case
    9744. -
    9745. Simple code for solving the above problem
    9746. -
    9747. With Lasso Regression
    9748. -
    9749. Another Example, now with a polynomial fit
    9750. -
    9751. To think about, first part
    9752. -
    9753. More thinking
    9754. -
    9755. Still thinking
    9756. -
    9757. What does centering (subtracting the mean values) mean mathematically?
    9758. -
    9759. Further Manipulations
    9760. -
    9761. Wrapping it up
    9762. -
    9763. Linear Regression code, Intercept handling first
    9764. -
    9765. Code Examples
    9766. -
    9767. Taking out the mean
    9768. -
    9769. Friday September 9
    9770. -
    9771. Linking the regression analysis with a statistical interpretation
    9772. -
    9773. Assumptions made
    9774. -
    9775. Expectation value and variance
    9776. -
    9777. Expectation value and variance for \( \boldsymbol{\beta} \)
    9778. -
    9779. Deriving OLS from a probability distribution
    9780. -
    9781. Independent and Identically Distrubuted (iid)
    9782. -
    9783. Maximum Likelihood Estimation (MLE)
    9784. -
    9785. A new Cost Function
    9786. -
    9787. More basic Statistics and Bayes' theorem
    9788. -
    9789. Marginal Probability
    9790. -
    9791. Conditional Probability
    9792. -
    9793. Bayes' Theorem
    9794. -
    9795. Interpretations of Bayes' Theorem
    9796. -
    9797. Example of Usage of Bayes' theorem
    9798. -
    9799. Doing it correctly
    9800. -
    9801. Bayes' Theorem and Ridge and Lasso Regression
    9802. -
    9803. Test Function for what happens with OLS, Ridge and Lasso
    9804. -
    9805. Invoking Bayes' theorem
    9806. -
    9807. Ridge and Bayes
    9808. -
    9809. Lasso and Bayes
    9810. -
    9811. Exercise 1: mean values and variances in linear regression
    9812. -
    9813. Exercise 2: Adding Ridge and Lasso Regression
    9814. +
    9815. Summary from last Week and discussion of SVD, Ridge and Lasso regression with examples
    9816. +
    9817. Linear Regression and the SVD
    9818. +
    9819. What does it mean?
    9820. +
    9821. And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
    9822. +
    9823. Code for SVD and Inversion of Matrices
    9824. +
    9825. Inverse of Rectangular Matrix
    9826. +
    9827. Ridge and LASSO Regression
    9828. +
    9829. From OLS to Ridge and Lasso
    9830. +
    9831. Deriving the Ridge Regression Equations
    9832. +
    9833. Note on Scikit-Learn
    9834. +
    9835. Comparison with OLS
    9836. +
    9837. SVD analysis
    9838. +
    9839. Interpreting the Ridge results
    9840. +
    9841. More interpretations
    9842. +
    9843. Deriving the Lasso Regression Equations
    9844. +
    9845. Simple example to illustrate Ordinary Least Squares, Ridge and Lasso Regression
    9846. +
    9847. Ridge Regression
    9848. +
    9849. Lasso Regression
    9850. +
    9851. Yet another Example
    9852. +
    9853. The OLS case
    9854. +
    9855. The Ridge case
    9856. +
    9857. Writing the Cost Function
    9858. +
    9859. Lasso case
    9860. +
    9861. The first Case
    9862. +
    9863. Simple code for solving the above problem
    9864. +
    9865. With Lasso Regression
    9866. +
    9867. Another Example, now with a polynomial fit
    9868. +
    9869. To think about, first part
    9870. +
    9871. More thinking
    9872. +
    9873. Still thinking
    9874. +
    9875. What does centering (subtracting the mean values) mean mathematically?
    9876. +
    9877. Further Manipulations
    9878. +
    9879. Wrapping it up
    9880. +
    9881. Linear Regression code, Intercept handling first
    9882. +
    9883. Code Examples
    9884. +
    9885. Taking out the mean
    9886. +
    9887. Friday September 9
    9888. +
    9889. Linking the regression analysis with a statistical interpretation
    9890. +
    9891. Assumptions made
    9892. +
    9893. Expectation value and variance
    9894. +
    9895. Expectation value and variance for \( \boldsymbol{\beta} \)
    9896. +
    9897. Deriving OLS from a probability distribution
    9898. +
    9899. Independent and Identically Distrubuted (iid)
    9900. +
    9901. Maximum Likelihood Estimation (MLE)
    9902. +
    9903. A new Cost Function
    9904. +
    9905. More basic Statistics and Bayes' theorem
    9906. +
    9907. Marginal Probability
    9908. +
    9909. Conditional Probability
    9910. +
    9911. Bayes' Theorem
    9912. +
    9913. Interpretations of Bayes' Theorem
    9914. +
    9915. Example of Usage of Bayes' theorem
    9916. +
    9917. Doing it correctly
    9918. +
    9919. Bayes' Theorem and Ridge and Lasso Regression
    9920. +
    9921. Test Function for what happens with OLS, Ridge and Lasso
    9922. +
    9923. Invoking Bayes' theorem
    9924. +
    9925. Ridge and Bayes
    9926. +
    9927. Lasso and Bayes
    9928. +
    9929. Exercise 1: mean values and variances in linear regression
    9930. +
    9931. Exercise 2: Adding Ridge and Lasso Regression
    9932. @@ -297,23 +295,38 @@ MathJax.Hub.Config({

       

       

       

      -

      Assumptions made

      +

      Expectation value and variance

      -

      The assumption we have made here can be summarized as (and this is going to be useful when we discuss the bias-variance trade off) -that there exists a function \( f(\boldsymbol{x}) \) and a normal distributed error \( \boldsymbol{\varepsilon}\sim \mathcal{N}(0, \sigma^2) \) -which describe our data +

      We can calculate the expectation value of \( \boldsymbol{y} \) for a given element \( i \)

      +$$ +\begin{align*} +\mathbb{E}(y_i) & = +\mathbb{E}(\mathbf{X}_{i, \ast} \, \boldsymbol{\beta}) + \mathbb{E}(\varepsilon_i) +\, \, \, = \, \, \, \mathbf{X}_{i, \ast} \, \beta, +\end{align*} +$$ + +

      while +its variance is

      $$ -\boldsymbol{y} = f(\boldsymbol{x})+\boldsymbol{\varepsilon} +\begin{align*} \mbox{Var}(y_i) & = \mathbb{E} \{ [y_i +- \mathbb{E}(y_i)]^2 \} \, \, \, = \, \, \, \mathbb{E} ( y_i^2 ) - +[\mathbb{E}(y_i)]^2 \\ & = \mathbb{E} [ ( \mathbf{X}_{i, \ast} \, +\beta + \varepsilon_i )^2] - ( \mathbf{X}_{i, \ast} \, \boldsymbol{\beta})^2 \\ & += \mathbb{E} [ ( \mathbf{X}_{i, \ast} \, \boldsymbol{\beta})^2 + 2 \varepsilon_i +\mathbf{X}_{i, \ast} \, \boldsymbol{\beta} + \varepsilon_i^2 ] - ( \mathbf{X}_{i, +\ast} \, \beta)^2 \\ & = ( \mathbf{X}_{i, \ast} \, \boldsymbol{\beta})^2 + 2 +\mathbb{E}(\varepsilon_i) \mathbf{X}_{i, \ast} \, \boldsymbol{\beta} + +\mathbb{E}(\varepsilon_i^2 ) - ( \mathbf{X}_{i, \ast} \, \boldsymbol{\beta})^2 +\\ & = \mathbb{E}(\varepsilon_i^2 ) \, \, \, = \, \, \, +\mbox{Var}(\varepsilon_i) \, \, \, = \, \, \, \sigma^2. +\end{align*} $$ -

      We approximate this function with our model from the solution of the linear regression equations, that is our -function \( f \) is approximated by \( \boldsymbol{\tilde{y}} \) where we want to minimize \( (\boldsymbol{y}-\boldsymbol{\tilde{y}})^2 \), our MSE, with +

      Hence, \( y_i \sim \mathcal{N}( \mathbf{X}_{i, \ast} \, \boldsymbol{\beta}, \sigma^2) \), that is \( \boldsymbol{y} \) follows a normal distribution with +mean value \( \boldsymbol{X}\boldsymbol{\beta} \) and variance \( \sigma^2 \) (not be confused with the singular values of the SVD).

      -$$ -\boldsymbol{\tilde{y}} = \boldsymbol{X}\boldsymbol{\beta}. -$$ -

      @@ -340,7 +353,7 @@ $$

    9933. 50
    9934. 51
    9935. ...
    9936. -
    9937. 60
    9938. +
    9939. 59
    9940. »
    9941. diff --git a/doc/pub/week36/html/._week36-bs042.html b/doc/pub/week36/html/._week36-bs042.html index 446c5273b..05c78da57 100644 --- a/doc/pub/week36/html/._week36-bs042.html +++ b/doc/pub/week36/html/._week36-bs042.html @@ -37,7 +37,6 @@ doconce format html week36.do.txt --html_style=bootstrap --pygments_html_style=d
    9942. Plans for week 36
    9943. -
    9944. Thursday September 8
    9945. -
    9946. Summary from last Week and discussion of SVD, Ridge and Lasso regression with examples
    9947. -
    9948. Linear Regression and the SVD
    9949. -
    9950. What does it mean?
    9951. -
    9952. And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
    9953. -
    9954. Code for SVD and Inversion of Matrices
    9955. -
    9956. Inverse of Rectangular Matrix
    9957. -
    9958. Ridge and LASSO Regression
    9959. -
    9960. From OLS to Ridge and Lasso
    9961. -
    9962. Deriving the Ridge Regression Equations
    9963. -
    9964. Note on Scikit-Learn
    9965. -
    9966. Comparison with OLS
    9967. -
    9968. SVD analysis
    9969. -
    9970. Interpreting the Ridge results
    9971. -
    9972. More interpretations
    9973. -
    9974. Deriving the Lasso Regression Equations
    9975. -
    9976. Simple example to illustrate Ordinary Least Squares, Ridge and Lasso Regression
    9977. -
    9978. Ridge Regression
    9979. -
    9980. Lasso Regression
    9981. -
    9982. Yet another Example
    9983. -
    9984. The OLS case
    9985. -
    9986. The Ridge case
    9987. -
    9988. Writing the Cost Function
    9989. -
    9990. Lasso case
    9991. -
    9992. The first Case
    9993. -
    9994. Simple code for solving the above problem
    9995. -
    9996. With Lasso Regression
    9997. -
    9998. Another Example, now with a polynomial fit
    9999. -
    10000. To think about, first part
    10001. -
    10002. More thinking
    10003. -
    10004. Still thinking
    10005. -
    10006. What does centering (subtracting the mean values) mean mathematically?
    10007. -
    10008. Further Manipulations
    10009. -
    10010. Wrapping it up
    10011. -
    10012. Linear Regression code, Intercept handling first
    10013. -
    10014. Code Examples
    10015. -
    10016. Taking out the mean
    10017. -
    10018. Friday September 9
    10019. -
    10020. Linking the regression analysis with a statistical interpretation
    10021. -
    10022. Assumptions made
    10023. -
    10024. Expectation value and variance
    10025. -
    10026. Expectation value and variance for \( \boldsymbol{\beta} \)
    10027. -
    10028. Deriving OLS from a probability distribution
    10029. -
    10030. Independent and Identically Distrubuted (iid)
    10031. -
    10032. Maximum Likelihood Estimation (MLE)
    10033. -
    10034. A new Cost Function
    10035. -
    10036. More basic Statistics and Bayes' theorem
    10037. -
    10038. Marginal Probability
    10039. -
    10040. Conditional Probability
    10041. -
    10042. Bayes' Theorem
    10043. -
    10044. Interpretations of Bayes' Theorem
    10045. -
    10046. Example of Usage of Bayes' theorem
    10047. -
    10048. Doing it correctly
    10049. -
    10050. Bayes' Theorem and Ridge and Lasso Regression
    10051. -
    10052. Test Function for what happens with OLS, Ridge and Lasso
    10053. -
    10054. Invoking Bayes' theorem
    10055. -
    10056. Ridge and Bayes
    10057. -
    10058. Lasso and Bayes
    10059. -
    10060. Exercise 1: mean values and variances in linear regression
    10061. -
    10062. Exercise 2: Adding Ridge and Lasso Regression
    10063. +
    10064. Summary from last Week and discussion of SVD, Ridge and Lasso regression with examples
    10065. +
    10066. Linear Regression and the SVD
    10067. +
    10068. What does it mean?
    10069. +
    10070. And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
    10071. +
    10072. Code for SVD and Inversion of Matrices
    10073. +
    10074. Inverse of Rectangular Matrix
    10075. +
    10076. Ridge and LASSO Regression
    10077. +
    10078. From OLS to Ridge and Lasso
    10079. +
    10080. Deriving the Ridge Regression Equations
    10081. +
    10082. Note on Scikit-Learn
    10083. +
    10084. Comparison with OLS
    10085. +
    10086. SVD analysis
    10087. +
    10088. Interpreting the Ridge results
    10089. +
    10090. More interpretations
    10091. +
    10092. Deriving the Lasso Regression Equations
    10093. +
    10094. Simple example to illustrate Ordinary Least Squares, Ridge and Lasso Regression
    10095. +
    10096. Ridge Regression
    10097. +
    10098. Lasso Regression
    10099. +
    10100. Yet another Example
    10101. +
    10102. The OLS case
    10103. +
    10104. The Ridge case
    10105. +
    10106. Writing the Cost Function
    10107. +
    10108. Lasso case
    10109. +
    10110. The first Case
    10111. +
    10112. Simple code for solving the above problem
    10113. +
    10114. With Lasso Regression
    10115. +
    10116. Another Example, now with a polynomial fit
    10117. +
    10118. To think about, first part
    10119. +
    10120. More thinking
    10121. +
    10122. Still thinking
    10123. +
    10124. What does centering (subtracting the mean values) mean mathematically?
    10125. +
    10126. Further Manipulations
    10127. +
    10128. Wrapping it up
    10129. +
    10130. Linear Regression code, Intercept handling first
    10131. +
    10132. Code Examples
    10133. +
    10134. Taking out the mean
    10135. +
    10136. Friday September 9
    10137. +
    10138. Linking the regression analysis with a statistical interpretation
    10139. +
    10140. Assumptions made
    10141. +
    10142. Expectation value and variance
    10143. +
    10144. Expectation value and variance for \( \boldsymbol{\beta} \)
    10145. +
    10146. Deriving OLS from a probability distribution
    10147. +
    10148. Independent and Identically Distrubuted (iid)
    10149. +
    10150. Maximum Likelihood Estimation (MLE)
    10151. +
    10152. A new Cost Function
    10153. +
    10154. More basic Statistics and Bayes' theorem
    10155. +
    10156. Marginal Probability
    10157. +
    10158. Conditional Probability
    10159. +
    10160. Bayes' Theorem
    10161. +
    10162. Interpretations of Bayes' Theorem
    10163. +
    10164. Example of Usage of Bayes' theorem
    10165. +
    10166. Doing it correctly
    10167. +
    10168. Bayes' Theorem and Ridge and Lasso Regression
    10169. +
    10170. Test Function for what happens with OLS, Ridge and Lasso
    10171. +
    10172. Invoking Bayes' theorem
    10173. +
    10174. Ridge and Bayes
    10175. +
    10176. Lasso and Bayes
    10177. +
    10178. Exercise 1: mean values and variances in linear regression
    10179. +
    10180. Exercise 2: Adding Ridge and Lasso Regression
    10181. @@ -297,37 +295,81 @@ MathJax.Hub.Config({

       

       

       

      -

      Expectation value and variance

      +

      Expectation value and variance for \( \boldsymbol{\beta} \)

      -

      We can calculate the expectation value of \( \boldsymbol{y} \) for a given element \( i \)

      +

      With the OLS expressions for the optimal parameters \( \boldsymbol{\hat{\beta}} \) we can evaluate the expectation value

      $$ -\begin{align*} -\mathbb{E}(y_i) & = -\mathbb{E}(\mathbf{X}_{i, \ast} \, \boldsymbol{\beta}) + \mathbb{E}(\varepsilon_i) -\, \, \, = \, \, \, \mathbf{X}_{i, \ast} \, \beta, -\end{align*} +\mathbb{E}(\boldsymbol{\hat{\beta}}) = \mathbb{E}[ (\mathbf{X}^{\top} \mathbf{X})^{-1}\mathbf{X}^{T} \mathbf{Y}]=(\mathbf{X}^{T} \mathbf{X})^{-1}\mathbf{X}^{T} \mathbb{E}[ \mathbf{Y}]=(\mathbf{X}^{T} \mathbf{X})^{-1} \mathbf{X}^{T}\mathbf{X}\boldsymbol{\beta}=\boldsymbol{\beta}. $$ -

      while -its variance is +

      This means that the estimator of the regression parameters is unbiased.

      + +

      We can also calculate the variance

      + +

      The variance of the optimal value \( \boldsymbol{\hat{\beta}} \) is

      +$$ +\begin{eqnarray*} +\mbox{Var}(\boldsymbol{\hat{\beta}}) & = & \mathbb{E} \{ [\boldsymbol{\beta} - \mathbb{E}(\boldsymbol{\beta})] [\boldsymbol{\beta} - \mathbb{E}(\boldsymbol{\beta})]^{T} \} +\\ +& = & \mathbb{E} \{ [(\mathbf{X}^{T} \mathbf{X})^{-1} \, \mathbf{X}^{T} \mathbf{Y} - \boldsymbol{\beta}] \, [(\mathbf{X}^{T} \mathbf{X})^{-1} \, \mathbf{X}^{T} \mathbf{Y} - \boldsymbol{\beta}]^{T} \} +\\ +% & = & \mathbb{E} \{ [(\mathbf{X}^{T} \mathbf{X})^{-1} \, \mathbf{X}^{T} \mathbf{Y}] \, [(\mathbf{X}^{T} \mathbf{X})^{-1} \, \mathbf{X}^{T} \mathbf{Y}]^{T} \} - \boldsymbol{\beta} \, \boldsymbol{\beta}^{T} +% \\ +% & = & \mathbb{E} \{ (\mathbf{X}^{T} \mathbf{X})^{-1} \, \mathbf{X}^{T} \mathbf{Y} \, \mathbf{Y}^{T} \, \mathbf{X} \, (\mathbf{X}^{T} \mathbf{X})^{-1} \} - \boldsymbol{\beta} \, \boldsymbol{\beta}^{T} +% \\ +& = & (\mathbf{X}^{T} \mathbf{X})^{-1} \, \mathbf{X}^{T} \, \mathbb{E} \{ \mathbf{Y} \, \mathbf{Y}^{T} \} \, \mathbf{X} \, (\mathbf{X}^{T} \mathbf{X})^{-1} - \boldsymbol{\beta} \, \boldsymbol{\beta}^{T} +\\ +& = & (\mathbf{X}^{T} \mathbf{X})^{-1} \, \mathbf{X}^{T} \, \{ \mathbf{X} \, \boldsymbol{\beta} \, \boldsymbol{\beta}^{T} \, \mathbf{X}^{T} + \sigma^2 \} \, \mathbf{X} \, (\mathbf{X}^{T} \mathbf{X})^{-1} - \boldsymbol{\beta} \, \boldsymbol{\beta}^{T} +% \\ +% & = & (\mathbf{X}^T \mathbf{X})^{-1} \, \mathbf{X}^T \, \mathbf{X} \, \boldsymbol{\beta} \, \boldsymbol{\beta}^T \, \mathbf{X}^T \, \mathbf{X} \, (\mathbf{X}^T % \mathbf{X})^{-1} +% \\ +% & & + \, \, \sigma^2 \, (\mathbf{X}^T \mathbf{X})^{-1} \, \mathbf{X}^T \, \mathbf{X} \, (\mathbf{X}^T \mathbf{X})^{-1} - \boldsymbol{\beta} \boldsymbol{\beta}^T +\\ +& = & \boldsymbol{\beta} \, \boldsymbol{\beta}^{T} + \sigma^2 \, (\mathbf{X}^{T} \mathbf{X})^{-1} - \boldsymbol{\beta} \, \boldsymbol{\beta}^{T} +\, \, \, = \, \, \, \sigma^2 \, (\mathbf{X}^{T} \mathbf{X})^{-1}, +\end{eqnarray*} +$$ + +

      where we have used that \( \mathbb{E} (\mathbf{Y} \mathbf{Y}^{T}) = +\mathbf{X} \, \boldsymbol{\beta} \, \boldsymbol{\beta}^{T} \, \mathbf{X}^{T} + +\sigma^2 \, \mathbf{I}_{nn} \). From \( \mbox{Var}(\boldsymbol{\beta}) = \sigma^2 +\, (\mathbf{X}^{T} \mathbf{X})^{-1} \), one obtains an estimate of the +variance of the estimate of the \( j \)-th regression coefficient: +\( \boldsymbol{\sigma}^2 (\boldsymbol{\beta}_j ) = \boldsymbol{\sigma}^2 [(\mathbf{X}^{T} \mathbf{X})^{-1}]_{jj} \). This may be used to +construct a confidence interval for the estimates.

      + +

      In a similar way, we can obtain analytical expressions for say the +expectation values of the parameters \( \boldsymbol{\beta} \) and their variance +when we employ Ridge regression, allowing us again to define a confidence interval. +

      + +

      It is rather straightforward to show that

      $$ -\begin{align*} \mbox{Var}(y_i) & = \mathbb{E} \{ [y_i -- \mathbb{E}(y_i)]^2 \} \, \, \, = \, \, \, \mathbb{E} ( y_i^2 ) - -[\mathbb{E}(y_i)]^2 \\ & = \mathbb{E} [ ( \mathbf{X}_{i, \ast} \, -\beta + \varepsilon_i )^2] - ( \mathbf{X}_{i, \ast} \, \boldsymbol{\beta})^2 \\ & -= \mathbb{E} [ ( \mathbf{X}_{i, \ast} \, \boldsymbol{\beta})^2 + 2 \varepsilon_i -\mathbf{X}_{i, \ast} \, \boldsymbol{\beta} + \varepsilon_i^2 ] - ( \mathbf{X}_{i, -\ast} \, \beta)^2 \\ & = ( \mathbf{X}_{i, \ast} \, \boldsymbol{\beta})^2 + 2 -\mathbb{E}(\varepsilon_i) \mathbf{X}_{i, \ast} \, \boldsymbol{\beta} + -\mathbb{E}(\varepsilon_i^2 ) - ( \mathbf{X}_{i, \ast} \, \boldsymbol{\beta})^2 -\\ & = \mathbb{E}(\varepsilon_i^2 ) \, \, \, = \, \, \, -\mbox{Var}(\varepsilon_i) \, \, \, = \, \, \, \sigma^2. -\end{align*} +\mathbb{E} \big[ \boldsymbol{\beta}^{\mathrm{Ridge}} \big]=(\mathbf{X}^{T} \mathbf{X} + \lambda \mathbf{I}_{pp})^{-1} (\mathbf{X}^{\top} \mathbf{X})\boldsymbol{\beta}^{\mathrm{OLS}}. $$ -

      Hence, \( y_i \sim \mathcal{N}( \mathbf{X}_{i, \ast} \, \boldsymbol{\beta}, \sigma^2) \), that is \( \boldsymbol{y} \) follows a normal distribution with -mean value \( \boldsymbol{X}\boldsymbol{\beta} \) and variance \( \sigma^2 \) (not be confused with the singular values of the SVD). +

      We see clearly that +\( \mathbb{E} \big[ \boldsymbol{\beta}^{\mathrm{Ridge}} \big] \not= \boldsymbol{\beta}^{\mathrm{OLS}} \) for any \( \lambda > 0 \). We say then that the ridge estimator is biased. +

      + +

      We can also compute the variance as

      + +$$ +\mbox{Var}[\boldsymbol{\beta}^{\mathrm{Ridge}}]=\sigma^2[ \mathbf{X}^{T} \mathbf{X} + \lambda \mathbf{I} ]^{-1} \mathbf{X}^{T} \mathbf{X} \{ [ \mathbf{X}^{\top} \mathbf{X} + \lambda \mathbf{I} ]^{-1}\}^{T}, +$$ + +

      and it is easy to see that if the parameter \( \lambda \) goes to infinity then the variance of Ridge parameters \( \boldsymbol{\beta} \) goes to zero.

      + +

      With this, we can compute the difference

      + +$$ +\mbox{Var}[\boldsymbol{\beta}^{\mathrm{OLS}}]-\mbox{Var}(\boldsymbol{\beta}^{\mathrm{Ridge}})=\sigma^2 [ \mathbf{X}^{T} \mathbf{X} + \lambda \mathbf{I} ]^{-1}[ 2\lambda\mathbf{I} + \lambda^2 (\mathbf{X}^{T} \mathbf{X})^{-1} ] \{ [ \mathbf{X}^{T} \mathbf{X} + \lambda \mathbf{I} ]^{-1}\}^{T}. +$$ + +

      The difference is non-negative definite since each component of the +matrix product is non-negative definite. +This means the variance we obtain with the standard OLS will always for \( \lambda > 0 \) be larger than the variance of \( \boldsymbol{\beta} \) obtained with the Ridge estimator. This has interesting consequences when we discuss the so-called bias-variance trade-off below.

      @@ -355,7 +397,7 @@ mean value \( \boldsymbol{X}\boldsymbol{\beta} \) and variance \( \sigma^2 \) (n

    10182. 51
    10183. 52
    10184. ...
    10185. -
    10186. 60
    10187. +
    10188. 59
    10189. »
    10190. diff --git a/doc/pub/week36/html/._week36-bs043.html b/doc/pub/week36/html/._week36-bs043.html index 4e0a74c6d..54edcc8fb 100644 --- a/doc/pub/week36/html/._week36-bs043.html +++ b/doc/pub/week36/html/._week36-bs043.html @@ -37,7 +37,6 @@ doconce format html week36.do.txt --html_style=bootstrap --pygments_html_style=d
    10191. Plans for week 36
    10192. -
    10193. Thursday September 8
    10194. -
    10195. Summary from last Week and discussion of SVD, Ridge and Lasso regression with examples
    10196. -
    10197. Linear Regression and the SVD
    10198. -
    10199. What does it mean?
    10200. -
    10201. And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
    10202. -
    10203. Code for SVD and Inversion of Matrices
    10204. -
    10205. Inverse of Rectangular Matrix
    10206. -
    10207. Ridge and LASSO Regression
    10208. -
    10209. From OLS to Ridge and Lasso
    10210. -
    10211. Deriving the Ridge Regression Equations
    10212. -
    10213. Note on Scikit-Learn
    10214. -
    10215. Comparison with OLS
    10216. -
    10217. SVD analysis
    10218. -
    10219. Interpreting the Ridge results
    10220. -
    10221. More interpretations
    10222. -
    10223. Deriving the Lasso Regression Equations
    10224. -
    10225. Simple example to illustrate Ordinary Least Squares, Ridge and Lasso Regression
    10226. -
    10227. Ridge Regression
    10228. -
    10229. Lasso Regression
    10230. -
    10231. Yet another Example
    10232. -
    10233. The OLS case
    10234. -
    10235. The Ridge case
    10236. -
    10237. Writing the Cost Function
    10238. -
    10239. Lasso case
    10240. -
    10241. The first Case
    10242. -
    10243. Simple code for solving the above problem
    10244. -
    10245. With Lasso Regression
    10246. -
    10247. Another Example, now with a polynomial fit
    10248. -
    10249. To think about, first part
    10250. -
    10251. More thinking
    10252. -
    10253. Still thinking
    10254. -
    10255. What does centering (subtracting the mean values) mean mathematically?
    10256. -
    10257. Further Manipulations
    10258. -
    10259. Wrapping it up
    10260. -
    10261. Linear Regression code, Intercept handling first
    10262. -
    10263. Code Examples
    10264. -
    10265. Taking out the mean
    10266. -
    10267. Friday September 9
    10268. -
    10269. Linking the regression analysis with a statistical interpretation
    10270. -
    10271. Assumptions made
    10272. -
    10273. Expectation value and variance
    10274. -
    10275. Expectation value and variance for \( \boldsymbol{\beta} \)
    10276. -
    10277. Deriving OLS from a probability distribution
    10278. -
    10279. Independent and Identically Distrubuted (iid)
    10280. -
    10281. Maximum Likelihood Estimation (MLE)
    10282. -
    10283. A new Cost Function
    10284. -
    10285. More basic Statistics and Bayes' theorem
    10286. -
    10287. Marginal Probability
    10288. -
    10289. Conditional Probability
    10290. -
    10291. Bayes' Theorem
    10292. -
    10293. Interpretations of Bayes' Theorem
    10294. -
    10295. Example of Usage of Bayes' theorem
    10296. -
    10297. Doing it correctly
    10298. -
    10299. Bayes' Theorem and Ridge and Lasso Regression
    10300. -
    10301. Test Function for what happens with OLS, Ridge and Lasso
    10302. -
    10303. Invoking Bayes' theorem
    10304. -
    10305. Ridge and Bayes
    10306. -
    10307. Lasso and Bayes
    10308. -
    10309. Exercise 1: mean values and variances in linear regression
    10310. -
    10311. Exercise 2: Adding Ridge and Lasso Regression
    10312. +
    10313. Summary from last Week and discussion of SVD, Ridge and Lasso regression with examples
    10314. +
    10315. Linear Regression and the SVD
    10316. +
    10317. What does it mean?
    10318. +
    10319. And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
    10320. +
    10321. Code for SVD and Inversion of Matrices
    10322. +
    10323. Inverse of Rectangular Matrix
    10324. +
    10325. Ridge and LASSO Regression
    10326. +
    10327. From OLS to Ridge and Lasso
    10328. +
    10329. Deriving the Ridge Regression Equations
    10330. +
    10331. Note on Scikit-Learn
    10332. +
    10333. Comparison with OLS
    10334. +
    10335. SVD analysis
    10336. +
    10337. Interpreting the Ridge results
    10338. +
    10339. More interpretations
    10340. +
    10341. Deriving the Lasso Regression Equations
    10342. +
    10343. Simple example to illustrate Ordinary Least Squares, Ridge and Lasso Regression
    10344. +
    10345. Ridge Regression
    10346. +
    10347. Lasso Regression
    10348. +
    10349. Yet another Example
    10350. +
    10351. The OLS case
    10352. +
    10353. The Ridge case
    10354. +
    10355. Writing the Cost Function
    10356. +
    10357. Lasso case
    10358. +
    10359. The first Case
    10360. +
    10361. Simple code for solving the above problem
    10362. +
    10363. With Lasso Regression
    10364. +
    10365. Another Example, now with a polynomial fit
    10366. +
    10367. To think about, first part
    10368. +
    10369. More thinking
    10370. +
    10371. Still thinking
    10372. +
    10373. What does centering (subtracting the mean values) mean mathematically?
    10374. +
    10375. Further Manipulations
    10376. +
    10377. Wrapping it up
    10378. +
    10379. Linear Regression code, Intercept handling first
    10380. +
    10381. Code Examples
    10382. +
    10383. Taking out the mean
    10384. +
    10385. Friday September 9
    10386. +
    10387. Linking the regression analysis with a statistical interpretation
    10388. +
    10389. Assumptions made
    10390. +
    10391. Expectation value and variance
    10392. +
    10393. Expectation value and variance for \( \boldsymbol{\beta} \)
    10394. +
    10395. Deriving OLS from a probability distribution
    10396. +
    10397. Independent and Identically Distrubuted (iid)
    10398. +
    10399. Maximum Likelihood Estimation (MLE)
    10400. +
    10401. A new Cost Function
    10402. +
    10403. More basic Statistics and Bayes' theorem
    10404. +
    10405. Marginal Probability
    10406. +
    10407. Conditional Probability
    10408. +
    10409. Bayes' Theorem
    10410. +
    10411. Interpretations of Bayes' Theorem
    10412. +
    10413. Example of Usage of Bayes' theorem
    10414. +
    10415. Doing it correctly
    10416. +
    10417. Bayes' Theorem and Ridge and Lasso Regression
    10418. +
    10419. Test Function for what happens with OLS, Ridge and Lasso
    10420. +
    10421. Invoking Bayes' theorem
    10422. +
    10423. Ridge and Bayes
    10424. +
    10425. Lasso and Bayes
    10426. +
    10427. Exercise 1: mean values and variances in linear regression
    10428. +
    10429. Exercise 2: Adding Ridge and Lasso Regression
    10430. @@ -297,82 +295,27 @@ MathJax.Hub.Config({

       

       

       

      -

      Expectation value and variance for \( \boldsymbol{\beta} \)

      +

      Deriving OLS from a probability distribution

      -

      With the OLS expressions for the optimal parameters \( \boldsymbol{\hat{\beta}} \) we can evaluate the expectation value

      -$$ -\mathbb{E}(\boldsymbol{\hat{\beta}}) = \mathbb{E}[ (\mathbf{X}^{\top} \mathbf{X})^{-1}\mathbf{X}^{T} \mathbf{Y}]=(\mathbf{X}^{T} \mathbf{X})^{-1}\mathbf{X}^{T} \mathbb{E}[ \mathbf{Y}]=(\mathbf{X}^{T} \mathbf{X})^{-1} \mathbf{X}^{T}\mathbf{X}\boldsymbol{\beta}=\boldsymbol{\beta}. -$$ - -

      This means that the estimator of the regression parameters is unbiased.

      - -

      We can also calculate the variance

      - -

      The variance of the optimal value \( \boldsymbol{\hat{\beta}} \) is

      -$$ -\begin{eqnarray*} -\mbox{Var}(\boldsymbol{\hat{\beta}}) & = & \mathbb{E} \{ [\boldsymbol{\beta} - \mathbb{E}(\boldsymbol{\beta})] [\boldsymbol{\beta} - \mathbb{E}(\boldsymbol{\beta})]^{T} \} -\\ -& = & \mathbb{E} \{ [(\mathbf{X}^{T} \mathbf{X})^{-1} \, \mathbf{X}^{T} \mathbf{Y} - \boldsymbol{\beta}] \, [(\mathbf{X}^{T} \mathbf{X})^{-1} \, \mathbf{X}^{T} \mathbf{Y} - \boldsymbol{\beta}]^{T} \} -\\ -% & = & \mathbb{E} \{ [(\mathbf{X}^{T} \mathbf{X})^{-1} \, \mathbf{X}^{T} \mathbf{Y}] \, [(\mathbf{X}^{T} \mathbf{X})^{-1} \, \mathbf{X}^{T} \mathbf{Y}]^{T} \} - \boldsymbol{\beta} \, \boldsymbol{\beta}^{T} -% \\ -% & = & \mathbb{E} \{ (\mathbf{X}^{T} \mathbf{X})^{-1} \, \mathbf{X}^{T} \mathbf{Y} \, \mathbf{Y}^{T} \, \mathbf{X} \, (\mathbf{X}^{T} \mathbf{X})^{-1} \} - \boldsymbol{\beta} \, \boldsymbol{\beta}^{T} -% \\ -& = & (\mathbf{X}^{T} \mathbf{X})^{-1} \, \mathbf{X}^{T} \, \mathbb{E} \{ \mathbf{Y} \, \mathbf{Y}^{T} \} \, \mathbf{X} \, (\mathbf{X}^{T} \mathbf{X})^{-1} - \boldsymbol{\beta} \, \boldsymbol{\beta}^{T} -\\ -& = & (\mathbf{X}^{T} \mathbf{X})^{-1} \, \mathbf{X}^{T} \, \{ \mathbf{X} \, \boldsymbol{\beta} \, \boldsymbol{\beta}^{T} \, \mathbf{X}^{T} + \sigma^2 \} \, \mathbf{X} \, (\mathbf{X}^{T} \mathbf{X})^{-1} - \boldsymbol{\beta} \, \boldsymbol{\beta}^{T} -% \\ -% & = & (\mathbf{X}^T \mathbf{X})^{-1} \, \mathbf{X}^T \, \mathbf{X} \, \boldsymbol{\beta} \, \boldsymbol{\beta}^T \, \mathbf{X}^T \, \mathbf{X} \, (\mathbf{X}^T % \mathbf{X})^{-1} -% \\ -% & & + \, \, \sigma^2 \, (\mathbf{X}^T \mathbf{X})^{-1} \, \mathbf{X}^T \, \mathbf{X} \, (\mathbf{X}^T \mathbf{X})^{-1} - \boldsymbol{\beta} \boldsymbol{\beta}^T -\\ -& = & \boldsymbol{\beta} \, \boldsymbol{\beta}^{T} + \sigma^2 \, (\mathbf{X}^{T} \mathbf{X})^{-1} - \boldsymbol{\beta} \, \boldsymbol{\beta}^{T} -\, \, \, = \, \, \, \sigma^2 \, (\mathbf{X}^{T} \mathbf{X})^{-1}, -\end{eqnarray*} -$$ - -

      where we have used that \( \mathbb{E} (\mathbf{Y} \mathbf{Y}^{T}) = -\mathbf{X} \, \boldsymbol{\beta} \, \boldsymbol{\beta}^{T} \, \mathbf{X}^{T} + -\sigma^2 \, \mathbf{I}_{nn} \). From \( \mbox{Var}(\boldsymbol{\beta}) = \sigma^2 -\, (\mathbf{X}^{T} \mathbf{X})^{-1} \), one obtains an estimate of the -variance of the estimate of the \( j \)-th regression coefficient: -\( \boldsymbol{\sigma}^2 (\boldsymbol{\beta}_j ) = \boldsymbol{\sigma}^2 [(\mathbf{X}^{T} \mathbf{X})^{-1}]_{jj} \). This may be used to -construct a confidence interval for the estimates. +

      Our basic assumption when we derived the OLS equations was to assume +that our output is determined by a given continuous function +\( f(\boldsymbol{x}) \) and a random noise \( \boldsymbol{\epsilon} \) given by the normal +distribution with zero mean value and an undetermined variance +\( \sigma^2 \).

      -

      In a similar way, we can obtain analytical expressions for say the -expectation values of the parameters \( \boldsymbol{\beta} \) and their variance -when we employ Ridge regression, allowing us again to define a confidence interval. +

      We found above that the outputs \( \boldsymbol{y} \) have a mean value given by +\( \boldsymbol{X}\hat{\boldsymbol{\beta}} \) and variance \( \sigma^2 \). Since the entries to +the design matrix are not stochastic variables, we can assume that the +probability distribution of our targets is also a normal distribution +but now with mean value \( \boldsymbol{X}\hat{\boldsymbol{\beta}} \). This means that a +single output \( y_i \) is given by the Gaussian distribution

      -

      It is rather straightforward to show that

      $$ -\mathbb{E} \big[ \boldsymbol{\beta}^{\mathrm{Ridge}} \big]=(\mathbf{X}^{T} \mathbf{X} + \lambda \mathbf{I}_{pp})^{-1} (\mathbf{X}^{\top} \mathbf{X})\boldsymbol{\beta}^{\mathrm{OLS}}. +y_i\sim \mathcal{N}(\boldsymbol{X}_{i,*}\boldsymbol{\beta}, \sigma^2)=\frac{1}{\sqrt{2\pi\sigma^2}}\exp{\left[-\frac{(y_i-\boldsymbol{X}_{i,*}\boldsymbol{\beta})^2}{2\sigma^2}\right]}. $$ -

      We see clearly that -\( \mathbb{E} \big[ \boldsymbol{\beta}^{\mathrm{Ridge}} \big] \not= \boldsymbol{\beta}^{\mathrm{OLS}} \) for any \( \lambda > 0 \). We say then that the ridge estimator is biased. -

      - -

      We can also compute the variance as

      - -$$ -\mbox{Var}[\boldsymbol{\beta}^{\mathrm{Ridge}}]=\sigma^2[ \mathbf{X}^{T} \mathbf{X} + \lambda \mathbf{I} ]^{-1} \mathbf{X}^{T} \mathbf{X} \{ [ \mathbf{X}^{\top} \mathbf{X} + \lambda \mathbf{I} ]^{-1}\}^{T}, -$$ - -

      and it is easy to see that if the parameter \( \lambda \) goes to infinity then the variance of Ridge parameters \( \boldsymbol{\beta} \) goes to zero.

      - -

      With this, we can compute the difference

      - -$$ -\mbox{Var}[\boldsymbol{\beta}^{\mathrm{OLS}}]-\mbox{Var}(\boldsymbol{\beta}^{\mathrm{Ridge}})=\sigma^2 [ \mathbf{X}^{T} \mathbf{X} + \lambda \mathbf{I} ]^{-1}[ 2\lambda\mathbf{I} + \lambda^2 (\mathbf{X}^{T} \mathbf{X})^{-1} ] \{ [ \mathbf{X}^{T} \mathbf{X} + \lambda \mathbf{I} ]^{-1}\}^{T}. -$$ - -

      The difference is non-negative definite since each component of the -matrix product is non-negative definite. -This means the variance we obtain with the standard OLS will always for \( \lambda > 0 \) be larger than the variance of \( \boldsymbol{\beta} \) obtained with the Ridge estimator. This has interesting consequences when we discuss the so-called bias-variance trade-off below. -

      @@ -399,7 +342,7 @@ This means the variance we obtain with the standard OLS will always for \( \lamb

    10431. 52
    10432. 53
    10433. ...
    10434. -
    10435. 60
    10436. +
    10437. 59
    10438. »
    10439. diff --git a/doc/pub/week36/html/._week36-bs044.html b/doc/pub/week36/html/._week36-bs044.html index a006bb45f..5597ca0bc 100644 --- a/doc/pub/week36/html/._week36-bs044.html +++ b/doc/pub/week36/html/._week36-bs044.html @@ -37,7 +37,6 @@ doconce format html week36.do.txt --html_style=bootstrap --pygments_html_style=d
    10440. Plans for week 36
    10441. -
    10442. Thursday September 8
    10443. -
    10444. Summary from last Week and discussion of SVD, Ridge and Lasso regression with examples
    10445. -
    10446. Linear Regression and the SVD
    10447. -
    10448. What does it mean?
    10449. -
    10450. And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
    10451. -
    10452. Code for SVD and Inversion of Matrices
    10453. -
    10454. Inverse of Rectangular Matrix
    10455. -
    10456. Ridge and LASSO Regression
    10457. -
    10458. From OLS to Ridge and Lasso
    10459. -
    10460. Deriving the Ridge Regression Equations
    10461. -
    10462. Note on Scikit-Learn
    10463. -
    10464. Comparison with OLS
    10465. -
    10466. SVD analysis
    10467. -
    10468. Interpreting the Ridge results
    10469. -
    10470. More interpretations
    10471. -
    10472. Deriving the Lasso Regression Equations
    10473. -
    10474. Simple example to illustrate Ordinary Least Squares, Ridge and Lasso Regression
    10475. -
    10476. Ridge Regression
    10477. -
    10478. Lasso Regression
    10479. -
    10480. Yet another Example
    10481. -
    10482. The OLS case
    10483. -
    10484. The Ridge case
    10485. -
    10486. Writing the Cost Function
    10487. -
    10488. Lasso case
    10489. -
    10490. The first Case
    10491. -
    10492. Simple code for solving the above problem
    10493. -
    10494. With Lasso Regression
    10495. -
    10496. Another Example, now with a polynomial fit
    10497. -
    10498. To think about, first part
    10499. -
    10500. More thinking
    10501. -
    10502. Still thinking
    10503. -
    10504. What does centering (subtracting the mean values) mean mathematically?
    10505. -
    10506. Further Manipulations
    10507. -
    10508. Wrapping it up
    10509. -
    10510. Linear Regression code, Intercept handling first
    10511. -
    10512. Code Examples
    10513. -
    10514. Taking out the mean
    10515. -
    10516. Friday September 9
    10517. -
    10518. Linking the regression analysis with a statistical interpretation
    10519. -
    10520. Assumptions made
    10521. -
    10522. Expectation value and variance
    10523. -
    10524. Expectation value and variance for \( \boldsymbol{\beta} \)
    10525. -
    10526. Deriving OLS from a probability distribution
    10527. -
    10528. Independent and Identically Distrubuted (iid)
    10529. -
    10530. Maximum Likelihood Estimation (MLE)
    10531. -
    10532. A new Cost Function
    10533. -
    10534. More basic Statistics and Bayes' theorem
    10535. -
    10536. Marginal Probability
    10537. -
    10538. Conditional Probability
    10539. -
    10540. Bayes' Theorem
    10541. -
    10542. Interpretations of Bayes' Theorem
    10543. -
    10544. Example of Usage of Bayes' theorem
    10545. -
    10546. Doing it correctly
    10547. -
    10548. Bayes' Theorem and Ridge and Lasso Regression
    10549. -
    10550. Test Function for what happens with OLS, Ridge and Lasso
    10551. -
    10552. Invoking Bayes' theorem
    10553. -
    10554. Ridge and Bayes
    10555. -
    10556. Lasso and Bayes
    10557. -
    10558. Exercise 1: mean values and variances in linear regression
    10559. -
    10560. Exercise 2: Adding Ridge and Lasso Regression
    10561. +
    10562. Summary from last Week and discussion of SVD, Ridge and Lasso regression with examples
    10563. +
    10564. Linear Regression and the SVD
    10565. +
    10566. What does it mean?
    10567. +
    10568. And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
    10569. +
    10570. Code for SVD and Inversion of Matrices
    10571. +
    10572. Inverse of Rectangular Matrix
    10573. +
    10574. Ridge and LASSO Regression
    10575. +
    10576. From OLS to Ridge and Lasso
    10577. +
    10578. Deriving the Ridge Regression Equations
    10579. +
    10580. Note on Scikit-Learn
    10581. +
    10582. Comparison with OLS
    10583. +
    10584. SVD analysis
    10585. +
    10586. Interpreting the Ridge results
    10587. +
    10588. More interpretations
    10589. +
    10590. Deriving the Lasso Regression Equations
    10591. +
    10592. Simple example to illustrate Ordinary Least Squares, Ridge and Lasso Regression
    10593. +
    10594. Ridge Regression
    10595. +
    10596. Lasso Regression
    10597. +
    10598. Yet another Example
    10599. +
    10600. The OLS case
    10601. +
    10602. The Ridge case
    10603. +
    10604. Writing the Cost Function
    10605. +
    10606. Lasso case
    10607. +
    10608. The first Case
    10609. +
    10610. Simple code for solving the above problem
    10611. +
    10612. With Lasso Regression
    10613. +
    10614. Another Example, now with a polynomial fit
    10615. +
    10616. To think about, first part
    10617. +
    10618. More thinking
    10619. +
    10620. Still thinking
    10621. +
    10622. What does centering (subtracting the mean values) mean mathematically?
    10623. +
    10624. Further Manipulations
    10625. +
    10626. Wrapping it up
    10627. +
    10628. Linear Regression code, Intercept handling first
    10629. +
    10630. Code Examples
    10631. +
    10632. Taking out the mean
    10633. +
    10634. Friday September 9
    10635. +
    10636. Linking the regression analysis with a statistical interpretation
    10637. +
    10638. Assumptions made
    10639. +
    10640. Expectation value and variance
    10641. +
    10642. Expectation value and variance for \( \boldsymbol{\beta} \)
    10643. +
    10644. Deriving OLS from a probability distribution
    10645. +
    10646. Independent and Identically Distrubuted (iid)
    10647. +
    10648. Maximum Likelihood Estimation (MLE)
    10649. +
    10650. A new Cost Function
    10651. +
    10652. More basic Statistics and Bayes' theorem
    10653. +
    10654. Marginal Probability
    10655. +
    10656. Conditional Probability
    10657. +
    10658. Bayes' Theorem
    10659. +
    10660. Interpretations of Bayes' Theorem
    10661. +
    10662. Example of Usage of Bayes' theorem
    10663. +
    10664. Doing it correctly
    10665. +
    10666. Bayes' Theorem and Ridge and Lasso Regression
    10667. +
    10668. Test Function for what happens with OLS, Ridge and Lasso
    10669. +
    10670. Invoking Bayes' theorem
    10671. +
    10672. Ridge and Bayes
    10673. +
    10674. Lasso and Bayes
    10675. +
    10676. Exercise 1: mean values and variances in linear regression
    10677. +
    10678. Exercise 2: Adding Ridge and Lasso Regression
    10679. @@ -297,27 +295,38 @@ MathJax.Hub.Config({

       

       

       

      -

      Deriving OLS from a probability distribution

      +

      Independent and Identically Distrubuted (iid)

      -

      Our basic assumption when we derived the OLS equations was to assume -that our output is determined by a given continuous function -\( f(\boldsymbol{x}) \) and a random noise \( \boldsymbol{\epsilon} \) given by the normal -distribution with zero mean value and an undetermined variance -\( \sigma^2 \). +

      We assume now that the various \( y_i \) values are stochastically distributed according to the above Gaussian distribution. +We define this distribution as

      - -

      We found above that the outputs \( \boldsymbol{y} \) have a mean value given by -\( \boldsymbol{X}\hat{\boldsymbol{\beta}} \) and variance \( \sigma^2 \). Since the entries to -the design matrix are not stochastic variables, we can assume that the -probability distribution of our targets is also a normal distribution -but now with mean value \( \boldsymbol{X}\hat{\boldsymbol{\beta}} \). This means that a -single output \( y_i \) is given by the Gaussian distribution -

      - $$ -y_i\sim \mathcal{N}(\boldsymbol{X}_{i,*}\boldsymbol{\beta}, \sigma^2)=\frac{1}{\sqrt{2\pi\sigma^2}}\exp{\left[-\frac{(y_i-\boldsymbol{X}_{i,*}\boldsymbol{\beta})^2}{2\sigma^2}\right]}. +p(y_i, \boldsymbol{X}\vert\boldsymbol{\beta})=\frac{1}{\sqrt{2\pi\sigma^2}}\exp{\left[-\frac{(y_i-\boldsymbol{X}_{i,*}\boldsymbol{\beta})^2}{2\sigma^2}\right]}, $$ +

      which reads as finding the likelihood of an event \( y_i \) with the input variables \( \boldsymbol{X} \) given the parameters (to be determined) \( \boldsymbol{\beta} \).

      + +

      Since these events are assumed to be independent and identicall distributed we can build the probability distribution function (PDF) for all possible event \( \boldsymbol{y} \) as the product of the single events, that is we have

      + +$$ +p(\boldsymbol{y},\boldsymbol{X}\vert\boldsymbol{\beta})=\prod_{i=0}^{n-1}\frac{1}{\sqrt{2\pi\sigma^2}}\exp{\left[-\frac{(y_i-\boldsymbol{X}_{i,*}\boldsymbol{\beta})^2}{2\sigma^2}\right]}=\prod_{i=0}^{n-1}p(y_i,\boldsymbol{X}\vert\boldsymbol{\beta}). +$$ + +

      We will write this in a more compact form reserving \( \boldsymbol{D} \) for the domain of events, including the ouputs (targets) and the inputs. That is +in case we have a simple one-dimensional input and output case +

      +$$ +\boldsymbol{D}=[(x_0,y_0), (x_1,y_1),\dots, (x_{n-1},y_{n-1})]. +$$ + +

      In the more general case the various inputs should be replaced by the possible features represented by the input data set \( \boldsymbol{X} \). +We can now rewrite the above probability as +

      +$$ +p(\boldsymbol{D}\vert\boldsymbol{\beta})=\prod_{i=0}^{n-1}\frac{1}{\sqrt{2\pi\sigma^2}}\exp{\left[-\frac{(y_i-\boldsymbol{X}_{i,*}\boldsymbol{\beta})^2}{2\sigma^2}\right]}. +$$ + +

      It is a conditional probability (see below) and reads as the likelihood of a domain of events \( \boldsymbol{D} \) given a set of parameters \( \boldsymbol{\beta} \).

      @@ -344,7 +353,7 @@ $$

    10680. 53
    10681. 54
    10682. ...
    10683. -
    10684. 60
    10685. +
    10686. 59
    10687. »
    10688. diff --git a/doc/pub/week36/html/._week36-bs045.html b/doc/pub/week36/html/._week36-bs045.html index de31e2cd5..e8f2b2d4b 100644 --- a/doc/pub/week36/html/._week36-bs045.html +++ b/doc/pub/week36/html/._week36-bs045.html @@ -37,7 +37,6 @@ doconce format html week36.do.txt --html_style=bootstrap --pygments_html_style=d
    10689. Plans for week 36
    10690. -
    10691. Thursday September 8
    10692. -
    10693. Summary from last Week and discussion of SVD, Ridge and Lasso regression with examples
    10694. -
    10695. Linear Regression and the SVD
    10696. -
    10697. What does it mean?
    10698. -
    10699. And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
    10700. -
    10701. Code for SVD and Inversion of Matrices
    10702. -
    10703. Inverse of Rectangular Matrix
    10704. -
    10705. Ridge and LASSO Regression
    10706. -
    10707. From OLS to Ridge and Lasso
    10708. -
    10709. Deriving the Ridge Regression Equations
    10710. -
    10711. Note on Scikit-Learn
    10712. -
    10713. Comparison with OLS
    10714. -
    10715. SVD analysis
    10716. -
    10717. Interpreting the Ridge results
    10718. -
    10719. More interpretations
    10720. -
    10721. Deriving the Lasso Regression Equations
    10722. -
    10723. Simple example to illustrate Ordinary Least Squares, Ridge and Lasso Regression
    10724. -
    10725. Ridge Regression
    10726. -
    10727. Lasso Regression
    10728. -
    10729. Yet another Example
    10730. -
    10731. The OLS case
    10732. -
    10733. The Ridge case
    10734. -
    10735. Writing the Cost Function
    10736. -
    10737. Lasso case
    10738. -
    10739. The first Case
    10740. -
    10741. Simple code for solving the above problem
    10742. -
    10743. With Lasso Regression
    10744. -
    10745. Another Example, now with a polynomial fit
    10746. -
    10747. To think about, first part
    10748. -
    10749. More thinking
    10750. -
    10751. Still thinking
    10752. -
    10753. What does centering (subtracting the mean values) mean mathematically?
    10754. -
    10755. Further Manipulations
    10756. -
    10757. Wrapping it up
    10758. -
    10759. Linear Regression code, Intercept handling first
    10760. -
    10761. Code Examples
    10762. -
    10763. Taking out the mean
    10764. -
    10765. Friday September 9
    10766. -
    10767. Linking the regression analysis with a statistical interpretation
    10768. -
    10769. Assumptions made
    10770. -
    10771. Expectation value and variance
    10772. -
    10773. Expectation value and variance for \( \boldsymbol{\beta} \)
    10774. -
    10775. Deriving OLS from a probability distribution
    10776. -
    10777. Independent and Identically Distrubuted (iid)
    10778. -
    10779. Maximum Likelihood Estimation (MLE)
    10780. -
    10781. A new Cost Function
    10782. -
    10783. More basic Statistics and Bayes' theorem
    10784. -
    10785. Marginal Probability
    10786. -
    10787. Conditional Probability
    10788. -
    10789. Bayes' Theorem
    10790. -
    10791. Interpretations of Bayes' Theorem
    10792. -
    10793. Example of Usage of Bayes' theorem
    10794. -
    10795. Doing it correctly
    10796. -
    10797. Bayes' Theorem and Ridge and Lasso Regression
    10798. -
    10799. Test Function for what happens with OLS, Ridge and Lasso
    10800. -
    10801. Invoking Bayes' theorem
    10802. -
    10803. Ridge and Bayes
    10804. -
    10805. Lasso and Bayes
    10806. -
    10807. Exercise 1: mean values and variances in linear regression
    10808. -
    10809. Exercise 2: Adding Ridge and Lasso Regression
    10810. +
    10811. Summary from last Week and discussion of SVD, Ridge and Lasso regression with examples
    10812. +
    10813. Linear Regression and the SVD
    10814. +
    10815. What does it mean?
    10816. +
    10817. And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
    10818. +
    10819. Code for SVD and Inversion of Matrices
    10820. +
    10821. Inverse of Rectangular Matrix
    10822. +
    10823. Ridge and LASSO Regression
    10824. +
    10825. From OLS to Ridge and Lasso
    10826. +
    10827. Deriving the Ridge Regression Equations
    10828. +
    10829. Note on Scikit-Learn
    10830. +
    10831. Comparison with OLS
    10832. +
    10833. SVD analysis
    10834. +
    10835. Interpreting the Ridge results
    10836. +
    10837. More interpretations
    10838. +
    10839. Deriving the Lasso Regression Equations
    10840. +
    10841. Simple example to illustrate Ordinary Least Squares, Ridge and Lasso Regression
    10842. +
    10843. Ridge Regression
    10844. +
    10845. Lasso Regression
    10846. +
    10847. Yet another Example
    10848. +
    10849. The OLS case
    10850. +
    10851. The Ridge case
    10852. +
    10853. Writing the Cost Function
    10854. +
    10855. Lasso case
    10856. +
    10857. The first Case
    10858. +
    10859. Simple code for solving the above problem
    10860. +
    10861. With Lasso Regression
    10862. +
    10863. Another Example, now with a polynomial fit
    10864. +
    10865. To think about, first part
    10866. +
    10867. More thinking
    10868. +
    10869. Still thinking
    10870. +
    10871. What does centering (subtracting the mean values) mean mathematically?
    10872. +
    10873. Further Manipulations
    10874. +
    10875. Wrapping it up
    10876. +
    10877. Linear Regression code, Intercept handling first
    10878. +
    10879. Code Examples
    10880. +
    10881. Taking out the mean
    10882. +
    10883. Friday September 9
    10884. +
    10885. Linking the regression analysis with a statistical interpretation
    10886. +
    10887. Assumptions made
    10888. +
    10889. Expectation value and variance
    10890. +
    10891. Expectation value and variance for \( \boldsymbol{\beta} \)
    10892. +
    10893. Deriving OLS from a probability distribution
    10894. +
    10895. Independent and Identically Distrubuted (iid)
    10896. +
    10897. Maximum Likelihood Estimation (MLE)
    10898. +
    10899. A new Cost Function
    10900. +
    10901. More basic Statistics and Bayes' theorem
    10902. +
    10903. Marginal Probability
    10904. +
    10905. Conditional Probability
    10906. +
    10907. Bayes' Theorem
    10908. +
    10909. Interpretations of Bayes' Theorem
    10910. +
    10911. Example of Usage of Bayes' theorem
    10912. +
    10913. Doing it correctly
    10914. +
    10915. Bayes' Theorem and Ridge and Lasso Regression
    10916. +
    10917. Test Function for what happens with OLS, Ridge and Lasso
    10918. +
    10919. Invoking Bayes' theorem
    10920. +
    10921. Ridge and Bayes
    10922. +
    10923. Lasso and Bayes
    10924. +
    10925. Exercise 1: mean values and variances in linear regression
    10926. +
    10927. Exercise 2: Adding Ridge and Lasso Regression
    10928. @@ -297,38 +295,32 @@ MathJax.Hub.Config({

       

       

       

      -

      Independent and Identically Distrubuted (iid)

      +

      Maximum Likelihood Estimation (MLE)

      -

      We assume now that the various \( y_i \) values are stochastically distributed according to the above Gaussian distribution. -We define this distribution as +

      In statistics, maximum likelihood estimation (MLE) is a method of +estimating the parameters of an assumed probability distribution, +given some observed data. This is achieved by maximizing a likelihood +function so that, under the assumed statistical model, the observed +data is the most probable.

      -$$ -p(y_i, \boldsymbol{X}\vert\boldsymbol{\beta})=\frac{1}{\sqrt{2\pi\sigma^2}}\exp{\left[-\frac{(y_i-\boldsymbol{X}_{i,*}\boldsymbol{\beta})^2}{2\sigma^2}\right]}, -$$ -

      which reads as finding the likelihood of an event \( y_i \) with the input variables \( \boldsymbol{X} \) given the parameters (to be determined) \( \boldsymbol{\beta} \).

      - -

      Since these events are assumed to be independent and identicall distributed we can build the probability distribution function (PDF) for all possible event \( \boldsymbol{y} \) as the product of the single events, that is we have

      - -$$ -p(\boldsymbol{y},\boldsymbol{X}\vert\boldsymbol{\beta})=\prod_{i=0}^{n-1}\frac{1}{\sqrt{2\pi\sigma^2}}\exp{\left[-\frac{(y_i-\boldsymbol{X}_{i,*}\boldsymbol{\beta})^2}{2\sigma^2}\right]}=\prod_{i=0}^{n-1}p(y_i,\boldsymbol{X}\vert\boldsymbol{\beta}). -$$ - -

      We will write this in a more compact form reserving \( \boldsymbol{D} \) for the domain of events, including the ouputs (targets) and the inputs. That is -in case we have a simple one-dimensional input and output case +

      We will assume here that our events are given by the above Gaussian +distribution and we will determine the optimal parameters \( \beta \) by +maximizing the above PDF. However, computing the derivatives of a +product function is cumbersome and can easily lead to overflow and/or +underflowproblems, with potentials for loss of numerical precision.

      -$$ -\boldsymbol{D}=[(x_0,y_0), (x_1,y_1),\dots, (x_{n-1},y_{n-1})]. -$$ -

      In the more general case the various inputs should be replaced by the possible features represented by the input data set \( \boldsymbol{X} \). -We can now rewrite the above probability as +

      In practice, it is more convenient to maximize the logarithm of the +PDF because it is a monotonically increasing function of the argument. +Alternatively, and this will be our option, we will minimize the +negative of the logarithm since this is a monotonically decreasing +function.

      -$$ -p(\boldsymbol{D}\vert\boldsymbol{\beta})=\prod_{i=0}^{n-1}\frac{1}{\sqrt{2\pi\sigma^2}}\exp{\left[-\frac{(y_i-\boldsymbol{X}_{i,*}\boldsymbol{\beta})^2}{2\sigma^2}\right]}. -$$ -

      It is a conditional probability (see below) and reads as the likelihood of a domain of events \( \boldsymbol{D} \) given a set of parameters \( \boldsymbol{\beta} \).

      +

      Note also that maximization/minimization of the logarithm of the PDF +is equivalent to the maximization/minimization of the function itself. +

      @@ -355,7 +347,7 @@ $$

    10929. 54
    10930. 55
    10931. ...
    10932. -
    10933. 60
    10934. +
    10935. 59
    10936. »
    10937. diff --git a/doc/pub/week36/html/._week36-bs046.html b/doc/pub/week36/html/._week36-bs046.html index cd6ecbe89..11e698ad0 100644 --- a/doc/pub/week36/html/._week36-bs046.html +++ b/doc/pub/week36/html/._week36-bs046.html @@ -37,7 +37,6 @@ doconce format html week36.do.txt --html_style=bootstrap --pygments_html_style=d
    10938. Plans for week 36
    10939. -
    10940. Thursday September 8
    10941. -
    10942. Summary from last Week and discussion of SVD, Ridge and Lasso regression with examples
    10943. -
    10944. Linear Regression and the SVD
    10945. -
    10946. What does it mean?
    10947. -
    10948. And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
    10949. -
    10950. Code for SVD and Inversion of Matrices
    10951. -
    10952. Inverse of Rectangular Matrix
    10953. -
    10954. Ridge and LASSO Regression
    10955. -
    10956. From OLS to Ridge and Lasso
    10957. -
    10958. Deriving the Ridge Regression Equations
    10959. -
    10960. Note on Scikit-Learn
    10961. -
    10962. Comparison with OLS
    10963. -
    10964. SVD analysis
    10965. -
    10966. Interpreting the Ridge results
    10967. -
    10968. More interpretations
    10969. -
    10970. Deriving the Lasso Regression Equations
    10971. -
    10972. Simple example to illustrate Ordinary Least Squares, Ridge and Lasso Regression
    10973. -
    10974. Ridge Regression
    10975. -
    10976. Lasso Regression
    10977. -
    10978. Yet another Example
    10979. -
    10980. The OLS case
    10981. -
    10982. The Ridge case
    10983. -
    10984. Writing the Cost Function
    10985. -
    10986. Lasso case
    10987. -
    10988. The first Case
    10989. -
    10990. Simple code for solving the above problem
    10991. -
    10992. With Lasso Regression
    10993. -
    10994. Another Example, now with a polynomial fit
    10995. -
    10996. To think about, first part
    10997. -
    10998. More thinking
    10999. -
    11000. Still thinking
    11001. -
    11002. What does centering (subtracting the mean values) mean mathematically?
    11003. -
    11004. Further Manipulations
    11005. -
    11006. Wrapping it up
    11007. -
    11008. Linear Regression code, Intercept handling first
    11009. -
    11010. Code Examples
    11011. -
    11012. Taking out the mean
    11013. -
    11014. Friday September 9
    11015. -
    11016. Linking the regression analysis with a statistical interpretation
    11017. -
    11018. Assumptions made
    11019. -
    11020. Expectation value and variance
    11021. -
    11022. Expectation value and variance for \( \boldsymbol{\beta} \)
    11023. -
    11024. Deriving OLS from a probability distribution
    11025. -
    11026. Independent and Identically Distrubuted (iid)
    11027. -
    11028. Maximum Likelihood Estimation (MLE)
    11029. -
    11030. A new Cost Function
    11031. -
    11032. More basic Statistics and Bayes' theorem
    11033. -
    11034. Marginal Probability
    11035. -
    11036. Conditional Probability
    11037. -
    11038. Bayes' Theorem
    11039. -
    11040. Interpretations of Bayes' Theorem
    11041. -
    11042. Example of Usage of Bayes' theorem
    11043. -
    11044. Doing it correctly
    11045. -
    11046. Bayes' Theorem and Ridge and Lasso Regression
    11047. -
    11048. Test Function for what happens with OLS, Ridge and Lasso
    11049. -
    11050. Invoking Bayes' theorem
    11051. -
    11052. Ridge and Bayes
    11053. -
    11054. Lasso and Bayes
    11055. -
    11056. Exercise 1: mean values and variances in linear regression
    11057. -
    11058. Exercise 2: Adding Ridge and Lasso Regression
    11059. +
    11060. Summary from last Week and discussion of SVD, Ridge and Lasso regression with examples
    11061. +
    11062. Linear Regression and the SVD
    11063. +
    11064. What does it mean?
    11065. +
    11066. And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
    11067. +
    11068. Code for SVD and Inversion of Matrices
    11069. +
    11070. Inverse of Rectangular Matrix
    11071. +
    11072. Ridge and LASSO Regression
    11073. +
    11074. From OLS to Ridge and Lasso
    11075. +
    11076. Deriving the Ridge Regression Equations
    11077. +
    11078. Note on Scikit-Learn
    11079. +
    11080. Comparison with OLS
    11081. +
    11082. SVD analysis
    11083. +
    11084. Interpreting the Ridge results
    11085. +
    11086. More interpretations
    11087. +
    11088. Deriving the Lasso Regression Equations
    11089. +
    11090. Simple example to illustrate Ordinary Least Squares, Ridge and Lasso Regression
    11091. +
    11092. Ridge Regression
    11093. +
    11094. Lasso Regression
    11095. +
    11096. Yet another Example
    11097. +
    11098. The OLS case
    11099. +
    11100. The Ridge case
    11101. +
    11102. Writing the Cost Function
    11103. +
    11104. Lasso case
    11105. +
    11106. The first Case
    11107. +
    11108. Simple code for solving the above problem
    11109. +
    11110. With Lasso Regression
    11111. +
    11112. Another Example, now with a polynomial fit
    11113. +
    11114. To think about, first part
    11115. +
    11116. More thinking
    11117. +
    11118. Still thinking
    11119. +
    11120. What does centering (subtracting the mean values) mean mathematically?
    11121. +
    11122. Further Manipulations
    11123. +
    11124. Wrapping it up
    11125. +
    11126. Linear Regression code, Intercept handling first
    11127. +
    11128. Code Examples
    11129. +
    11130. Taking out the mean
    11131. +
    11132. Friday September 9
    11133. +
    11134. Linking the regression analysis with a statistical interpretation
    11135. +
    11136. Assumptions made
    11137. +
    11138. Expectation value and variance
    11139. +
    11140. Expectation value and variance for \( \boldsymbol{\beta} \)
    11141. +
    11142. Deriving OLS from a probability distribution
    11143. +
    11144. Independent and Identically Distrubuted (iid)
    11145. +
    11146. Maximum Likelihood Estimation (MLE)
    11147. +
    11148. A new Cost Function
    11149. +
    11150. More basic Statistics and Bayes' theorem
    11151. +
    11152. Marginal Probability
    11153. +
    11154. Conditional Probability
    11155. +
    11156. Bayes' Theorem
    11157. +
    11158. Interpretations of Bayes' Theorem
    11159. +
    11160. Example of Usage of Bayes' theorem
    11161. +
    11162. Doing it correctly
    11163. +
    11164. Bayes' Theorem and Ridge and Lasso Regression
    11165. +
    11166. Test Function for what happens with OLS, Ridge and Lasso
    11167. +
    11168. Invoking Bayes' theorem
    11169. +
    11170. Ridge and Bayes
    11171. +
    11172. Lasso and Bayes
    11173. +
    11174. Exercise 1: mean values and variances in linear regression
    11175. +
    11176. Exercise 2: Adding Ridge and Lasso Regression
    11177. @@ -297,32 +295,31 @@ MathJax.Hub.Config({

       

       

       

      -

      Maximum Likelihood Estimation (MLE)

      +

      A new Cost Function

      -

      In statistics, maximum likelihood estimation (MLE) is a method of -estimating the parameters of an assumed probability distribution, -given some observed data. This is achieved by maximizing a likelihood -function so that, under the assumed statistical model, the observed -data is the most probable. -

      +

      We could now define a new cost function to minimize, namely the negative logarithm of the above PDF

      -

      We will assume here that our events are given by the above Gaussian -distribution and we will determine the optimal parameters \( \beta \) by -maximizing the above PDF. However, computing the derivatives of a -product function is cumbersome and can easily lead to overflow and/or -underflowproblems, with potentials for loss of numerical precision. -

      +$$ +C(\boldsymbol{\beta}=-\log{\prod_{i=0}^{n-1}p(y_i,\boldsymbol{X}\vert\boldsymbol{\beta})}=-\sum_{i=0}^{n-1}\log{p(y_i,\boldsymbol{X}\vert\boldsymbol{\beta})}, +$$ -

      In practice, it is more convenient to maximize the logarithm of the -PDF because it is a monotonically increasing function of the argument. -Alternatively, and this will be our option, we will minimize the -negative of the logarithm since this is a monotonically decreasing -function. -

      +

      which becomes

      +$$ +C(\boldsymbol{\beta}=\frac{n}{2}\log{2\pi\sigma^2}+\frac{\vert\vert (\boldsymbol{y}-\boldsymbol{X}\boldsymbol{\beta})\vert\vert_2^2}{2\sigma^2}. +$$ -

      Note also that maximization/minimization of the logarithm of the PDF -is equivalent to the maximization/minimization of the function itself. -

      +

      Taking the derivative of the new cost function with respect to the parameters \( \beta \) we recognize our familiar OLS equation, namely

      + +$$ +\boldsymbol{X}^T\left(\boldsymbol{y}-\boldsymbol{X}\boldsymbol{\beta}\right) =0, +$$ + +

      which leads to the well-known OLS equation for the optimal paramters \( \beta \)

      +$$ +\hat{\boldsymbol{\beta}}^{\mathrm{OLS}}=\left(\boldsymbol{X}^T\boldsymbol{X}\right)^{-1}\boldsymbol{X}^T\boldsymbol{y}! +$$ + +

      Before we make a similar analysis for Ridge and Lasso regression, we need a short reminder on statistics.

      @@ -349,7 +346,7 @@ is equivalent to the maximization/minimization of the function itself.

    11178. 55
    11179. 56
    11180. ...
    11181. -
    11182. 60
    11183. +
    11184. 59
    11185. »
    11186. diff --git a/doc/pub/week36/html/._week36-bs047.html b/doc/pub/week36/html/._week36-bs047.html index 298886237..c91b1e9db 100644 --- a/doc/pub/week36/html/._week36-bs047.html +++ b/doc/pub/week36/html/._week36-bs047.html @@ -37,7 +37,6 @@ doconce format html week36.do.txt --html_style=bootstrap --pygments_html_style=d
    11187. Plans for week 36
    11188. -
    11189. Thursday September 8
    11190. -
    11191. Summary from last Week and discussion of SVD, Ridge and Lasso regression with examples
    11192. -
    11193. Linear Regression and the SVD
    11194. -
    11195. What does it mean?
    11196. -
    11197. And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
    11198. -
    11199. Code for SVD and Inversion of Matrices
    11200. -
    11201. Inverse of Rectangular Matrix
    11202. -
    11203. Ridge and LASSO Regression
    11204. -
    11205. From OLS to Ridge and Lasso
    11206. -
    11207. Deriving the Ridge Regression Equations
    11208. -
    11209. Note on Scikit-Learn
    11210. -
    11211. Comparison with OLS
    11212. -
    11213. SVD analysis
    11214. -
    11215. Interpreting the Ridge results
    11216. -
    11217. More interpretations
    11218. -
    11219. Deriving the Lasso Regression Equations
    11220. -
    11221. Simple example to illustrate Ordinary Least Squares, Ridge and Lasso Regression
    11222. -
    11223. Ridge Regression
    11224. -
    11225. Lasso Regression
    11226. -
    11227. Yet another Example
    11228. -
    11229. The OLS case
    11230. -
    11231. The Ridge case
    11232. -
    11233. Writing the Cost Function
    11234. -
    11235. Lasso case
    11236. -
    11237. The first Case
    11238. -
    11239. Simple code for solving the above problem
    11240. -
    11241. With Lasso Regression
    11242. -
    11243. Another Example, now with a polynomial fit
    11244. -
    11245. To think about, first part
    11246. -
    11247. More thinking
    11248. -
    11249. Still thinking
    11250. -
    11251. What does centering (subtracting the mean values) mean mathematically?
    11252. -
    11253. Further Manipulations
    11254. -
    11255. Wrapping it up
    11256. -
    11257. Linear Regression code, Intercept handling first
    11258. -
    11259. Code Examples
    11260. -
    11261. Taking out the mean
    11262. -
    11263. Friday September 9
    11264. -
    11265. Linking the regression analysis with a statistical interpretation
    11266. -
    11267. Assumptions made
    11268. -
    11269. Expectation value and variance
    11270. -
    11271. Expectation value and variance for \( \boldsymbol{\beta} \)
    11272. -
    11273. Deriving OLS from a probability distribution
    11274. -
    11275. Independent and Identically Distrubuted (iid)
    11276. -
    11277. Maximum Likelihood Estimation (MLE)
    11278. -
    11279. A new Cost Function
    11280. -
    11281. More basic Statistics and Bayes' theorem
    11282. -
    11283. Marginal Probability
    11284. -
    11285. Conditional Probability
    11286. -
    11287. Bayes' Theorem
    11288. -
    11289. Interpretations of Bayes' Theorem
    11290. -
    11291. Example of Usage of Bayes' theorem
    11292. -
    11293. Doing it correctly
    11294. -
    11295. Bayes' Theorem and Ridge and Lasso Regression
    11296. -
    11297. Test Function for what happens with OLS, Ridge and Lasso
    11298. -
    11299. Invoking Bayes' theorem
    11300. -
    11301. Ridge and Bayes
    11302. -
    11303. Lasso and Bayes
    11304. -
    11305. Exercise 1: mean values and variances in linear regression
    11306. -
    11307. Exercise 2: Adding Ridge and Lasso Regression
    11308. +
    11309. Summary from last Week and discussion of SVD, Ridge and Lasso regression with examples
    11310. +
    11311. Linear Regression and the SVD
    11312. +
    11313. What does it mean?
    11314. +
    11315. And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
    11316. +
    11317. Code for SVD and Inversion of Matrices
    11318. +
    11319. Inverse of Rectangular Matrix
    11320. +
    11321. Ridge and LASSO Regression
    11322. +
    11323. From OLS to Ridge and Lasso
    11324. +
    11325. Deriving the Ridge Regression Equations
    11326. +
    11327. Note on Scikit-Learn
    11328. +
    11329. Comparison with OLS
    11330. +
    11331. SVD analysis
    11332. +
    11333. Interpreting the Ridge results
    11334. +
    11335. More interpretations
    11336. +
    11337. Deriving the Lasso Regression Equations
    11338. +
    11339. Simple example to illustrate Ordinary Least Squares, Ridge and Lasso Regression
    11340. +
    11341. Ridge Regression
    11342. +
    11343. Lasso Regression
    11344. +
    11345. Yet another Example
    11346. +
    11347. The OLS case
    11348. +
    11349. The Ridge case
    11350. +
    11351. Writing the Cost Function
    11352. +
    11353. Lasso case
    11354. +
    11355. The first Case
    11356. +
    11357. Simple code for solving the above problem
    11358. +
    11359. With Lasso Regression
    11360. +
    11361. Another Example, now with a polynomial fit
    11362. +
    11363. To think about, first part
    11364. +
    11365. More thinking
    11366. +
    11367. Still thinking
    11368. +
    11369. What does centering (subtracting the mean values) mean mathematically?
    11370. +
    11371. Further Manipulations
    11372. +
    11373. Wrapping it up
    11374. +
    11375. Linear Regression code, Intercept handling first
    11376. +
    11377. Code Examples
    11378. +
    11379. Taking out the mean
    11380. +
    11381. Friday September 9
    11382. +
    11383. Linking the regression analysis with a statistical interpretation
    11384. +
    11385. Assumptions made
    11386. +
    11387. Expectation value and variance
    11388. +
    11389. Expectation value and variance for \( \boldsymbol{\beta} \)
    11390. +
    11391. Deriving OLS from a probability distribution
    11392. +
    11393. Independent and Identically Distrubuted (iid)
    11394. +
    11395. Maximum Likelihood Estimation (MLE)
    11396. +
    11397. A new Cost Function
    11398. +
    11399. More basic Statistics and Bayes' theorem
    11400. +
    11401. Marginal Probability
    11402. +
    11403. Conditional Probability
    11404. +
    11405. Bayes' Theorem
    11406. +
    11407. Interpretations of Bayes' Theorem
    11408. +
    11409. Example of Usage of Bayes' theorem
    11410. +
    11411. Doing it correctly
    11412. +
    11413. Bayes' Theorem and Ridge and Lasso Regression
    11414. +
    11415. Test Function for what happens with OLS, Ridge and Lasso
    11416. +
    11417. Invoking Bayes' theorem
    11418. +
    11419. Ridge and Bayes
    11420. +
    11421. Lasso and Bayes
    11422. +
    11423. Exercise 1: mean values and variances in linear regression
    11424. +
    11425. Exercise 2: Adding Ridge and Lasso Regression
    11426. @@ -297,31 +295,41 @@ MathJax.Hub.Config({

       

       

       

      -

      A new Cost Function

      +

      More basic Statistics and Bayes' theorem

      -

      We could now define a new cost function to minimize, namely the negative logarithm of the above PDF

      +

      A central theorem in statistics is Bayes' theorem. This theorem plays a similar role as the good old Pythagoras' theorem in geometry. +Bayes' theorem is extremely simple to derive. But to do so we need some basic axioms from statistics. +

      +

      Assume we have two domains of events \( X=[x_0,x_1,\dots,x_{n-1}] \) and \( Y=[y_0,y_1,\dots,y_{n-1}] \).

      + +

      We define also the likelihood for \( X \) and \( Y \) as \( p(X) \) and \( p(Y) \) respectively. +The likelihood of a specific event \( x_i \) (or \( y_i \)) is then written as \( p(X=x_i) \) or just \( p(x_i)=p_i \). +

      + +
      +
      + $$ -C(\boldsymbol{\beta}=-\log{\prod_{i=0}^{n-1}p(y_i,\boldsymbol{X}\vert\boldsymbol{\beta})}=-\sum_{i=0}^{n-1}\log{p(y_i,\boldsymbol{X}\vert\boldsymbol{\beta})}, +p(X \cup Y)= p(X)+p(Y)-p(X \cap Y). +$$ +
      +
      + + +
      +
      + +$$ +p(X \cup Y)= p(X,Y)= p(X\vert Y)p(Y)=p(Y\vert X)p(X), $$ -

      which becomes

      -$$ -C(\boldsymbol{\beta}=\frac{n}{2}\log{2\pi\sigma^2}+\frac{\vert\vert (\boldsymbol{y}-\boldsymbol{X}\boldsymbol{\beta})\vert\vert_2^2}{2\sigma^2}. -$$ +

      where we read \( p(X\vert Y) \) as the likelihood of obtaining \( X \) given \( Y \).

      +
      +
      -

      Taking the derivative of the new cost function with respect to the parameters \( \beta \) we recognize our familiar OLS equation, namely

      -$$ -\boldsymbol{X}^T\left(\boldsymbol{y}-\boldsymbol{X}\boldsymbol{\beta}\right) =0, -$$ - -

      which leads to the well-known OLS equation for the optimal paramters \( \beta \)

      -$$ -\hat{\boldsymbol{\beta}}^{\mathrm{OLS}}=\left(\boldsymbol{X}^T\boldsymbol{X}\right)^{-1}\boldsymbol{X}^T\boldsymbol{y}! -$$ - -

      Before we make a similar analysis for Ridge and Lasso regression, we need a short reminder on statistics.

      +

      If we have independent events then \( p(X,Y)=p(X)p(Y) \).

      @@ -348,7 +356,7 @@ $$

    11427. 56
    11428. 57
    11429. ...
    11430. -
    11431. 60
    11432. +
    11433. 59
    11434. »
    11435. diff --git a/doc/pub/week36/html/._week36-bs048.html b/doc/pub/week36/html/._week36-bs048.html index 752f71e51..d4253959f 100644 --- a/doc/pub/week36/html/._week36-bs048.html +++ b/doc/pub/week36/html/._week36-bs048.html @@ -37,7 +37,6 @@ doconce format html week36.do.txt --html_style=bootstrap --pygments_html_style=d
    11436. Plans for week 36
    11437. -
    11438. Thursday September 8
    11439. -
    11440. Summary from last Week and discussion of SVD, Ridge and Lasso regression with examples
    11441. -
    11442. Linear Regression and the SVD
    11443. -
    11444. What does it mean?
    11445. -
    11446. And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
    11447. -
    11448. Code for SVD and Inversion of Matrices
    11449. -
    11450. Inverse of Rectangular Matrix
    11451. -
    11452. Ridge and LASSO Regression
    11453. -
    11454. From OLS to Ridge and Lasso
    11455. -
    11456. Deriving the Ridge Regression Equations
    11457. -
    11458. Note on Scikit-Learn
    11459. -
    11460. Comparison with OLS
    11461. -
    11462. SVD analysis
    11463. -
    11464. Interpreting the Ridge results
    11465. -
    11466. More interpretations
    11467. -
    11468. Deriving the Lasso Regression Equations
    11469. -
    11470. Simple example to illustrate Ordinary Least Squares, Ridge and Lasso Regression
    11471. -
    11472. Ridge Regression
    11473. -
    11474. Lasso Regression
    11475. -
    11476. Yet another Example
    11477. -
    11478. The OLS case
    11479. -
    11480. The Ridge case
    11481. -
    11482. Writing the Cost Function
    11483. -
    11484. Lasso case
    11485. -
    11486. The first Case
    11487. -
    11488. Simple code for solving the above problem
    11489. -
    11490. With Lasso Regression
    11491. -
    11492. Another Example, now with a polynomial fit
    11493. -
    11494. To think about, first part
    11495. -
    11496. More thinking
    11497. -
    11498. Still thinking
    11499. -
    11500. What does centering (subtracting the mean values) mean mathematically?
    11501. -
    11502. Further Manipulations
    11503. -
    11504. Wrapping it up
    11505. -
    11506. Linear Regression code, Intercept handling first
    11507. -
    11508. Code Examples
    11509. -
    11510. Taking out the mean
    11511. -
    11512. Friday September 9
    11513. -
    11514. Linking the regression analysis with a statistical interpretation
    11515. -
    11516. Assumptions made
    11517. -
    11518. Expectation value and variance
    11519. -
    11520. Expectation value and variance for \( \boldsymbol{\beta} \)
    11521. -
    11522. Deriving OLS from a probability distribution
    11523. -
    11524. Independent and Identically Distrubuted (iid)
    11525. -
    11526. Maximum Likelihood Estimation (MLE)
    11527. -
    11528. A new Cost Function
    11529. -
    11530. More basic Statistics and Bayes' theorem
    11531. -
    11532. Marginal Probability
    11533. -
    11534. Conditional Probability
    11535. -
    11536. Bayes' Theorem
    11537. -
    11538. Interpretations of Bayes' Theorem
    11539. -
    11540. Example of Usage of Bayes' theorem
    11541. -
    11542. Doing it correctly
    11543. -
    11544. Bayes' Theorem and Ridge and Lasso Regression
    11545. -
    11546. Test Function for what happens with OLS, Ridge and Lasso
    11547. -
    11548. Invoking Bayes' theorem
    11549. -
    11550. Ridge and Bayes
    11551. -
    11552. Lasso and Bayes
    11553. -
    11554. Exercise 1: mean values and variances in linear regression
    11555. -
    11556. Exercise 2: Adding Ridge and Lasso Regression
    11557. +
    11558. Summary from last Week and discussion of SVD, Ridge and Lasso regression with examples
    11559. +
    11560. Linear Regression and the SVD
    11561. +
    11562. What does it mean?
    11563. +
    11564. And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
    11565. +
    11566. Code for SVD and Inversion of Matrices
    11567. +
    11568. Inverse of Rectangular Matrix
    11569. +
    11570. Ridge and LASSO Regression
    11571. +
    11572. From OLS to Ridge and Lasso
    11573. +
    11574. Deriving the Ridge Regression Equations
    11575. +
    11576. Note on Scikit-Learn
    11577. +
    11578. Comparison with OLS
    11579. +
    11580. SVD analysis
    11581. +
    11582. Interpreting the Ridge results
    11583. +
    11584. More interpretations
    11585. +
    11586. Deriving the Lasso Regression Equations
    11587. +
    11588. Simple example to illustrate Ordinary Least Squares, Ridge and Lasso Regression
    11589. +
    11590. Ridge Regression
    11591. +
    11592. Lasso Regression
    11593. +
    11594. Yet another Example
    11595. +
    11596. The OLS case
    11597. +
    11598. The Ridge case
    11599. +
    11600. Writing the Cost Function
    11601. +
    11602. Lasso case
    11603. +
    11604. The first Case
    11605. +
    11606. Simple code for solving the above problem
    11607. +
    11608. With Lasso Regression
    11609. +
    11610. Another Example, now with a polynomial fit
    11611. +
    11612. To think about, first part
    11613. +
    11614. More thinking
    11615. +
    11616. Still thinking
    11617. +
    11618. What does centering (subtracting the mean values) mean mathematically?
    11619. +
    11620. Further Manipulations
    11621. +
    11622. Wrapping it up
    11623. +
    11624. Linear Regression code, Intercept handling first
    11625. +
    11626. Code Examples
    11627. +
    11628. Taking out the mean
    11629. +
    11630. Friday September 9
    11631. +
    11632. Linking the regression analysis with a statistical interpretation
    11633. +
    11634. Assumptions made
    11635. +
    11636. Expectation value and variance
    11637. +
    11638. Expectation value and variance for \( \boldsymbol{\beta} \)
    11639. +
    11640. Deriving OLS from a probability distribution
    11641. +
    11642. Independent and Identically Distrubuted (iid)
    11643. +
    11644. Maximum Likelihood Estimation (MLE)
    11645. +
    11646. A new Cost Function
    11647. +
    11648. More basic Statistics and Bayes' theorem
    11649. +
    11650. Marginal Probability
    11651. +
    11652. Conditional Probability
    11653. +
    11654. Bayes' Theorem
    11655. +
    11656. Interpretations of Bayes' Theorem
    11657. +
    11658. Example of Usage of Bayes' theorem
    11659. +
    11660. Doing it correctly
    11661. +
    11662. Bayes' Theorem and Ridge and Lasso Regression
    11663. +
    11664. Test Function for what happens with OLS, Ridge and Lasso
    11665. +
    11666. Invoking Bayes' theorem
    11667. +
    11668. Ridge and Bayes
    11669. +
    11670. Lasso and Bayes
    11671. +
    11672. Exercise 1: mean values and variances in linear regression
    11673. +
    11674. Exercise 2: Adding Ridge and Lasso Regression
    11675. @@ -297,42 +295,19 @@ MathJax.Hub.Config({

       

       

       

      -

      More basic Statistics and Bayes' theorem

      - -

      A central theorem in statistics is Bayes' theorem. This theorem plays a similar role as the good old Pythagoras' theorem in geometry. -Bayes' theorem is extremely simple to derive. But to do so we need some basic axioms from statistics. -

      - -

      Assume we have two domains of events \( X=[x_0,x_1,\dots,x_{n-1}] \) and \( Y=[y_0,y_1,\dots,y_{n-1}] \).

      - -

      We define also the likelihood for \( X \) and \( Y \) as \( p(X) \) and \( p(Y) \) respectively. -The likelihood of a specific event \( x_i \) (or \( y_i \)) is then written as \( p(X=x_i) \) or just \( p(x_i)=p_i \). -

      +

      Marginal Probability

      +

      The marginal probability is defined in terms of only one of the set of variables \( X,Y \). For a discrete probability we have

      $$ -p(X \cup Y)= p(X)+p(Y)-p(X \cap Y). +p(X)=\sum_{i=0}^{n-1}p(X,Y=y_i)=\sum_{i=0}^{n-1}p(X\vert Y=y_i)p(Y=y_i)=\sum_{i=0}^{n-1}p(X\vert y_i)p(y_i). $$
      -
      -
      - -$$ -p(X \cup Y)= p(X,Y)= p(X\vert Y)p(Y)=p(Y\vert X)p(X), -$$ - -

      where we read \( p(X\vert Y) \) as the likelihood of obtaining \( X \) given \( Y \).

      -
      -
      - - -

      If we have independent events then \( p(X,Y)=p(X)p(Y) \).

      -

      diff --git a/doc/pub/week36/html/._week36-bs049.html b/doc/pub/week36/html/._week36-bs049.html index c20dafc4c..66f4abff1 100644 --- a/doc/pub/week36/html/._week36-bs049.html +++ b/doc/pub/week36/html/._week36-bs049.html @@ -37,7 +37,6 @@ doconce format html week36.do.txt --html_style=bootstrap --pygments_html_style=d
    11676. Plans for week 36
    11677. -
    11678. Thursday September 8
    11679. -
    11680. Summary from last Week and discussion of SVD, Ridge and Lasso regression with examples
    11681. -
    11682. Linear Regression and the SVD
    11683. -
    11684. What does it mean?
    11685. -
    11686. And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
    11687. -
    11688. Code for SVD and Inversion of Matrices
    11689. -
    11690. Inverse of Rectangular Matrix
    11691. -
    11692. Ridge and LASSO Regression
    11693. -
    11694. From OLS to Ridge and Lasso
    11695. -
    11696. Deriving the Ridge Regression Equations
    11697. -
    11698. Note on Scikit-Learn
    11699. -
    11700. Comparison with OLS
    11701. -
    11702. SVD analysis
    11703. -
    11704. Interpreting the Ridge results
    11705. -
    11706. More interpretations
    11707. -
    11708. Deriving the Lasso Regression Equations
    11709. -
    11710. Simple example to illustrate Ordinary Least Squares, Ridge and Lasso Regression
    11711. -
    11712. Ridge Regression
    11713. -
    11714. Lasso Regression
    11715. -
    11716. Yet another Example
    11717. -
    11718. The OLS case
    11719. -
    11720. The Ridge case
    11721. -
    11722. Writing the Cost Function
    11723. -
    11724. Lasso case
    11725. -
    11726. The first Case
    11727. -
    11728. Simple code for solving the above problem
    11729. -
    11730. With Lasso Regression
    11731. -
    11732. Another Example, now with a polynomial fit
    11733. -
    11734. To think about, first part
    11735. -
    11736. More thinking
    11737. -
    11738. Still thinking
    11739. -
    11740. What does centering (subtracting the mean values) mean mathematically?
    11741. -
    11742. Further Manipulations
    11743. -
    11744. Wrapping it up
    11745. -
    11746. Linear Regression code, Intercept handling first
    11747. -
    11748. Code Examples
    11749. -
    11750. Taking out the mean
    11751. -
    11752. Friday September 9
    11753. -
    11754. Linking the regression analysis with a statistical interpretation
    11755. -
    11756. Assumptions made
    11757. -
    11758. Expectation value and variance
    11759. -
    11760. Expectation value and variance for \( \boldsymbol{\beta} \)
    11761. -
    11762. Deriving OLS from a probability distribution
    11763. -
    11764. Independent and Identically Distrubuted (iid)
    11765. -
    11766. Maximum Likelihood Estimation (MLE)
    11767. -
    11768. A new Cost Function
    11769. -
    11770. More basic Statistics and Bayes' theorem
    11771. -
    11772. Marginal Probability
    11773. -
    11774. Conditional Probability
    11775. -
    11776. Bayes' Theorem
    11777. -
    11778. Interpretations of Bayes' Theorem
    11779. -
    11780. Example of Usage of Bayes' theorem
    11781. -
    11782. Doing it correctly
    11783. -
    11784. Bayes' Theorem and Ridge and Lasso Regression
    11785. -
    11786. Test Function for what happens with OLS, Ridge and Lasso
    11787. -
    11788. Invoking Bayes' theorem
    11789. -
    11790. Ridge and Bayes
    11791. -
    11792. Lasso and Bayes
    11793. -
    11794. Exercise 1: mean values and variances in linear regression
    11795. -
    11796. Exercise 2: Adding Ridge and Lasso Regression
    11797. +
    11798. Summary from last Week and discussion of SVD, Ridge and Lasso regression with examples
    11799. +
    11800. Linear Regression and the SVD
    11801. +
    11802. What does it mean?
    11803. +
    11804. And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
    11805. +
    11806. Code for SVD and Inversion of Matrices
    11807. +
    11808. Inverse of Rectangular Matrix
    11809. +
    11810. Ridge and LASSO Regression
    11811. +
    11812. From OLS to Ridge and Lasso
    11813. +
    11814. Deriving the Ridge Regression Equations
    11815. +
    11816. Note on Scikit-Learn
    11817. +
    11818. Comparison with OLS
    11819. +
    11820. SVD analysis
    11821. +
    11822. Interpreting the Ridge results
    11823. +
    11824. More interpretations
    11825. +
    11826. Deriving the Lasso Regression Equations
    11827. +
    11828. Simple example to illustrate Ordinary Least Squares, Ridge and Lasso Regression
    11829. +
    11830. Ridge Regression
    11831. +
    11832. Lasso Regression
    11833. +
    11834. Yet another Example
    11835. +
    11836. The OLS case
    11837. +
    11838. The Ridge case
    11839. +
    11840. Writing the Cost Function
    11841. +
    11842. Lasso case
    11843. +
    11844. The first Case
    11845. +
    11846. Simple code for solving the above problem
    11847. +
    11848. With Lasso Regression
    11849. +
    11850. Another Example, now with a polynomial fit
    11851. +
    11852. To think about, first part
    11853. +
    11854. More thinking
    11855. +
    11856. Still thinking
    11857. +
    11858. What does centering (subtracting the mean values) mean mathematically?
    11859. +
    11860. Further Manipulations
    11861. +
    11862. Wrapping it up
    11863. +
    11864. Linear Regression code, Intercept handling first
    11865. +
    11866. Code Examples
    11867. +
    11868. Taking out the mean
    11869. +
    11870. Friday September 9
    11871. +
    11872. Linking the regression analysis with a statistical interpretation
    11873. +
    11874. Assumptions made
    11875. +
    11876. Expectation value and variance
    11877. +
    11878. Expectation value and variance for \( \boldsymbol{\beta} \)
    11879. +
    11880. Deriving OLS from a probability distribution
    11881. +
    11882. Independent and Identically Distrubuted (iid)
    11883. +
    11884. Maximum Likelihood Estimation (MLE)
    11885. +
    11886. A new Cost Function
    11887. +
    11888. More basic Statistics and Bayes' theorem
    11889. +
    11890. Marginal Probability
    11891. +
    11892. Conditional Probability
    11893. +
    11894. Bayes' Theorem
    11895. +
    11896. Interpretations of Bayes' Theorem
    11897. +
    11898. Example of Usage of Bayes' theorem
    11899. +
    11900. Doing it correctly
    11901. +
    11902. Bayes' Theorem and Ridge and Lasso Regression
    11903. +
    11904. Test Function for what happens with OLS, Ridge and Lasso
    11905. +
    11906. Invoking Bayes' theorem
    11907. +
    11908. Ridge and Bayes
    11909. +
    11910. Lasso and Bayes
    11911. +
    11912. Exercise 1: mean values and variances in linear regression
    11913. +
    11914. Exercise 2: Adding Ridge and Lasso Regression
    11915. @@ -297,14 +295,14 @@ MathJax.Hub.Config({

       

       

       

      -

      Marginal Probability

      +

      Conditional Probability

      -

      The marginal probability is defined in terms of only one of the set of variables \( X,Y \). For a discrete probability we have

      +

      The conditional probability, if \( p(Y) > 0 \), is

      $$ -p(X)=\sum_{i=0}^{n-1}p(X,Y=y_i)=\sum_{i=0}^{n-1}p(X\vert Y=y_i)p(Y=y_i)=\sum_{i=0}^{n-1}p(X\vert y_i)p(y_i). +p(X\vert Y)= \frac{p(X,Y)}{p(Y)}=\frac{p(X,Y)}{\sum_{i=0}^{n-1}p(Y\vert X=x_i)p(x_i)}. $$
      @@ -334,8 +332,6 @@ $$
    11916. 57
    11917. 58
    11918. 59
    11919. -
    11920. ...
    11921. -
    11922. 60
    11923. »
    11924. diff --git a/doc/pub/week36/html/._week36-bs050.html b/doc/pub/week36/html/._week36-bs050.html index 0065c56ef..fd8c055d7 100644 --- a/doc/pub/week36/html/._week36-bs050.html +++ b/doc/pub/week36/html/._week36-bs050.html @@ -37,7 +37,6 @@ doconce format html week36.do.txt --html_style=bootstrap --pygments_html_style=d
    11925. Plans for week 36
    11926. -
    11927. Thursday September 8
    11928. -
    11929. Summary from last Week and discussion of SVD, Ridge and Lasso regression with examples
    11930. -
    11931. Linear Regression and the SVD
    11932. -
    11933. What does it mean?
    11934. -
    11935. And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
    11936. -
    11937. Code for SVD and Inversion of Matrices
    11938. -
    11939. Inverse of Rectangular Matrix
    11940. -
    11941. Ridge and LASSO Regression
    11942. -
    11943. From OLS to Ridge and Lasso
    11944. -
    11945. Deriving the Ridge Regression Equations
    11946. -
    11947. Note on Scikit-Learn
    11948. -
    11949. Comparison with OLS
    11950. -
    11951. SVD analysis
    11952. -
    11953. Interpreting the Ridge results
    11954. -
    11955. More interpretations
    11956. -
    11957. Deriving the Lasso Regression Equations
    11958. -
    11959. Simple example to illustrate Ordinary Least Squares, Ridge and Lasso Regression
    11960. -
    11961. Ridge Regression
    11962. -
    11963. Lasso Regression
    11964. -
    11965. Yet another Example
    11966. -
    11967. The OLS case
    11968. -
    11969. The Ridge case
    11970. -
    11971. Writing the Cost Function
    11972. -
    11973. Lasso case
    11974. -
    11975. The first Case
    11976. -
    11977. Simple code for solving the above problem
    11978. -
    11979. With Lasso Regression
    11980. -
    11981. Another Example, now with a polynomial fit
    11982. -
    11983. To think about, first part
    11984. -
    11985. More thinking
    11986. -
    11987. Still thinking
    11988. -
    11989. What does centering (subtracting the mean values) mean mathematically?
    11990. -
    11991. Further Manipulations
    11992. -
    11993. Wrapping it up
    11994. -
    11995. Linear Regression code, Intercept handling first
    11996. -
    11997. Code Examples
    11998. -
    11999. Taking out the mean
    12000. -
    12001. Friday September 9
    12002. -
    12003. Linking the regression analysis with a statistical interpretation
    12004. -
    12005. Assumptions made
    12006. -
    12007. Expectation value and variance
    12008. -
    12009. Expectation value and variance for \( \boldsymbol{\beta} \)
    12010. -
    12011. Deriving OLS from a probability distribution
    12012. -
    12013. Independent and Identically Distrubuted (iid)
    12014. -
    12015. Maximum Likelihood Estimation (MLE)
    12016. -
    12017. A new Cost Function
    12018. -
    12019. More basic Statistics and Bayes' theorem
    12020. -
    12021. Marginal Probability
    12022. -
    12023. Conditional Probability
    12024. -
    12025. Bayes' Theorem
    12026. -
    12027. Interpretations of Bayes' Theorem
    12028. -
    12029. Example of Usage of Bayes' theorem
    12030. -
    12031. Doing it correctly
    12032. -
    12033. Bayes' Theorem and Ridge and Lasso Regression
    12034. -
    12035. Test Function for what happens with OLS, Ridge and Lasso
    12036. -
    12037. Invoking Bayes' theorem
    12038. -
    12039. Ridge and Bayes
    12040. -
    12041. Lasso and Bayes
    12042. -
    12043. Exercise 1: mean values and variances in linear regression
    12044. -
    12045. Exercise 2: Adding Ridge and Lasso Regression
    12046. +
    12047. Summary from last Week and discussion of SVD, Ridge and Lasso regression with examples
    12048. +
    12049. Linear Regression and the SVD
    12050. +
    12051. What does it mean?
    12052. +
    12053. And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
    12054. +
    12055. Code for SVD and Inversion of Matrices
    12056. +
    12057. Inverse of Rectangular Matrix
    12058. +
    12059. Ridge and LASSO Regression
    12060. +
    12061. From OLS to Ridge and Lasso
    12062. +
    12063. Deriving the Ridge Regression Equations
    12064. +
    12065. Note on Scikit-Learn
    12066. +
    12067. Comparison with OLS
    12068. +
    12069. SVD analysis
    12070. +
    12071. Interpreting the Ridge results
    12072. +
    12073. More interpretations
    12074. +
    12075. Deriving the Lasso Regression Equations
    12076. +
    12077. Simple example to illustrate Ordinary Least Squares, Ridge and Lasso Regression
    12078. +
    12079. Ridge Regression
    12080. +
    12081. Lasso Regression
    12082. +
    12083. Yet another Example
    12084. +
    12085. The OLS case
    12086. +
    12087. The Ridge case
    12088. +
    12089. Writing the Cost Function
    12090. +
    12091. Lasso case
    12092. +
    12093. The first Case
    12094. +
    12095. Simple code for solving the above problem
    12096. +
    12097. With Lasso Regression
    12098. +
    12099. Another Example, now with a polynomial fit
    12100. +
    12101. To think about, first part
    12102. +
    12103. More thinking
    12104. +
    12105. Still thinking
    12106. +
    12107. What does centering (subtracting the mean values) mean mathematically?
    12108. +
    12109. Further Manipulations
    12110. +
    12111. Wrapping it up
    12112. +
    12113. Linear Regression code, Intercept handling first
    12114. +
    12115. Code Examples
    12116. +
    12117. Taking out the mean
    12118. +
    12119. Friday September 9
    12120. +
    12121. Linking the regression analysis with a statistical interpretation
    12122. +
    12123. Assumptions made
    12124. +
    12125. Expectation value and variance
    12126. +
    12127. Expectation value and variance for \( \boldsymbol{\beta} \)
    12128. +
    12129. Deriving OLS from a probability distribution
    12130. +
    12131. Independent and Identically Distrubuted (iid)
    12132. +
    12133. Maximum Likelihood Estimation (MLE)
    12134. +
    12135. A new Cost Function
    12136. +
    12137. More basic Statistics and Bayes' theorem
    12138. +
    12139. Marginal Probability
    12140. +
    12141. Conditional Probability
    12142. +
    12143. Bayes' Theorem
    12144. +
    12145. Interpretations of Bayes' Theorem
    12146. +
    12147. Example of Usage of Bayes' theorem
    12148. +
    12149. Doing it correctly
    12150. +
    12151. Bayes' Theorem and Ridge and Lasso Regression
    12152. +
    12153. Test Function for what happens with OLS, Ridge and Lasso
    12154. +
    12155. Invoking Bayes' theorem
    12156. +
    12157. Ridge and Bayes
    12158. +
    12159. Lasso and Bayes
    12160. +
    12161. Exercise 1: mean values and variances in linear regression
    12162. +
    12163. Exercise 2: Adding Ridge and Lasso Regression
    12164. @@ -297,18 +295,20 @@ MathJax.Hub.Config({

       

       

       

      -

      Conditional Probability

      +

      Bayes' Theorem

      -

      The conditional probability, if \( p(Y) > 0 \), is

      -
      -
      - +

      If we combine the conditional probability with the marginal probability and the standard product rule, we have

      $$ -p(X\vert Y)= \frac{p(X,Y)}{p(Y)}=\frac{p(X,Y)}{\sum_{i=0}^{n-1}p(Y\vert X=x_i)p(x_i)}. +p(X\vert Y)= \frac{p(X,Y)}{p(Y)}, $$ -
      -
      +

      which we can rewrite as

      + +$$ +p(X\vert Y)= \frac{p(X,Y)}{\sum_{i=0}^{n-1}p(Y\vert X=x_i)p(x_i)}=\frac{p(Y\vert X)p(X)}{\sum_{i=0}^{n-1}p(Y\vert X=x_i)p(x_i)}, +$$ + +

      which is Bayes' theorem. It allows us to evaluate the uncertainty in in \( X \) after we have observed \( Y \). We can easily interchange \( X \) with \( Y \).

      @@ -333,7 +333,6 @@ $$

    12165. 57
    12166. 58
    12167. 59
    12168. -
    12169. 60
    12170. »
    12171. diff --git a/doc/pub/week36/html/._week36-bs051.html b/doc/pub/week36/html/._week36-bs051.html index 3317c8a93..993271ae5 100644 --- a/doc/pub/week36/html/._week36-bs051.html +++ b/doc/pub/week36/html/._week36-bs051.html @@ -37,7 +37,6 @@ doconce format html week36.do.txt --html_style=bootstrap --pygments_html_style=d
    12172. Plans for week 36
    12173. -
    12174. Thursday September 8
    12175. -
    12176. Summary from last Week and discussion of SVD, Ridge and Lasso regression with examples
    12177. -
    12178. Linear Regression and the SVD
    12179. -
    12180. What does it mean?
    12181. -
    12182. And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
    12183. -
    12184. Code for SVD and Inversion of Matrices
    12185. -
    12186. Inverse of Rectangular Matrix
    12187. -
    12188. Ridge and LASSO Regression
    12189. -
    12190. From OLS to Ridge and Lasso
    12191. -
    12192. Deriving the Ridge Regression Equations
    12193. -
    12194. Note on Scikit-Learn
    12195. -
    12196. Comparison with OLS
    12197. -
    12198. SVD analysis
    12199. -
    12200. Interpreting the Ridge results
    12201. -
    12202. More interpretations
    12203. -
    12204. Deriving the Lasso Regression Equations
    12205. -
    12206. Simple example to illustrate Ordinary Least Squares, Ridge and Lasso Regression
    12207. -
    12208. Ridge Regression
    12209. -
    12210. Lasso Regression
    12211. -
    12212. Yet another Example
    12213. -
    12214. The OLS case
    12215. -
    12216. The Ridge case
    12217. -
    12218. Writing the Cost Function
    12219. -
    12220. Lasso case
    12221. -
    12222. The first Case
    12223. -
    12224. Simple code for solving the above problem
    12225. -
    12226. With Lasso Regression
    12227. -
    12228. Another Example, now with a polynomial fit
    12229. -
    12230. To think about, first part
    12231. -
    12232. More thinking
    12233. -
    12234. Still thinking
    12235. -
    12236. What does centering (subtracting the mean values) mean mathematically?
    12237. -
    12238. Further Manipulations
    12239. -
    12240. Wrapping it up
    12241. -
    12242. Linear Regression code, Intercept handling first
    12243. -
    12244. Code Examples
    12245. -
    12246. Taking out the mean
    12247. -
    12248. Friday September 9
    12249. -
    12250. Linking the regression analysis with a statistical interpretation
    12251. -
    12252. Assumptions made
    12253. -
    12254. Expectation value and variance
    12255. -
    12256. Expectation value and variance for \( \boldsymbol{\beta} \)
    12257. -
    12258. Deriving OLS from a probability distribution
    12259. -
    12260. Independent and Identically Distrubuted (iid)
    12261. -
    12262. Maximum Likelihood Estimation (MLE)
    12263. -
    12264. A new Cost Function
    12265. -
    12266. More basic Statistics and Bayes' theorem
    12267. -
    12268. Marginal Probability
    12269. -
    12270. Conditional Probability
    12271. -
    12272. Bayes' Theorem
    12273. -
    12274. Interpretations of Bayes' Theorem
    12275. -
    12276. Example of Usage of Bayes' theorem
    12277. -
    12278. Doing it correctly
    12279. -
    12280. Bayes' Theorem and Ridge and Lasso Regression
    12281. -
    12282. Test Function for what happens with OLS, Ridge and Lasso
    12283. -
    12284. Invoking Bayes' theorem
    12285. -
    12286. Ridge and Bayes
    12287. -
    12288. Lasso and Bayes
    12289. -
    12290. Exercise 1: mean values and variances in linear regression
    12291. -
    12292. Exercise 2: Adding Ridge and Lasso Regression
    12293. +
    12294. Summary from last Week and discussion of SVD, Ridge and Lasso regression with examples
    12295. +
    12296. Linear Regression and the SVD
    12297. +
    12298. What does it mean?
    12299. +
    12300. And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
    12301. +
    12302. Code for SVD and Inversion of Matrices
    12303. +
    12304. Inverse of Rectangular Matrix
    12305. +
    12306. Ridge and LASSO Regression
    12307. +
    12308. From OLS to Ridge and Lasso
    12309. +
    12310. Deriving the Ridge Regression Equations
    12311. +
    12312. Note on Scikit-Learn
    12313. +
    12314. Comparison with OLS
    12315. +
    12316. SVD analysis
    12317. +
    12318. Interpreting the Ridge results
    12319. +
    12320. More interpretations
    12321. +
    12322. Deriving the Lasso Regression Equations
    12323. +
    12324. Simple example to illustrate Ordinary Least Squares, Ridge and Lasso Regression
    12325. +
    12326. Ridge Regression
    12327. +
    12328. Lasso Regression
    12329. +
    12330. Yet another Example
    12331. +
    12332. The OLS case
    12333. +
    12334. The Ridge case
    12335. +
    12336. Writing the Cost Function
    12337. +
    12338. Lasso case
    12339. +
    12340. The first Case
    12341. +
    12342. Simple code for solving the above problem
    12343. +
    12344. With Lasso Regression
    12345. +
    12346. Another Example, now with a polynomial fit
    12347. +
    12348. To think about, first part
    12349. +
    12350. More thinking
    12351. +
    12352. Still thinking
    12353. +
    12354. What does centering (subtracting the mean values) mean mathematically?
    12355. +
    12356. Further Manipulations
    12357. +
    12358. Wrapping it up
    12359. +
    12360. Linear Regression code, Intercept handling first
    12361. +
    12362. Code Examples
    12363. +
    12364. Taking out the mean
    12365. +
    12366. Friday September 9
    12367. +
    12368. Linking the regression analysis with a statistical interpretation
    12369. +
    12370. Assumptions made
    12371. +
    12372. Expectation value and variance
    12373. +
    12374. Expectation value and variance for \( \boldsymbol{\beta} \)
    12375. +
    12376. Deriving OLS from a probability distribution
    12377. +
    12378. Independent and Identically Distrubuted (iid)
    12379. +
    12380. Maximum Likelihood Estimation (MLE)
    12381. +
    12382. A new Cost Function
    12383. +
    12384. More basic Statistics and Bayes' theorem
    12385. +
    12386. Marginal Probability
    12387. +
    12388. Conditional Probability
    12389. +
    12390. Bayes' Theorem
    12391. +
    12392. Interpretations of Bayes' Theorem
    12393. +
    12394. Example of Usage of Bayes' theorem
    12395. +
    12396. Doing it correctly
    12397. +
    12398. Bayes' Theorem and Ridge and Lasso Regression
    12399. +
    12400. Test Function for what happens with OLS, Ridge and Lasso
    12401. +
    12402. Invoking Bayes' theorem
    12403. +
    12404. Ridge and Bayes
    12405. +
    12406. Lasso and Bayes
    12407. +
    12408. Exercise 1: mean values and variances in linear regression
    12409. +
    12410. Exercise 2: Adding Ridge and Lasso Regression
    12411. @@ -297,20 +295,17 @@ MathJax.Hub.Config({

       

       

       

      -

      Bayes' Theorem

      +

      Interpretations of Bayes' Theorem

      -

      If we combine the conditional probability with the marginal probability and the standard product rule, we have

      -$$ -p(X\vert Y)= \frac{p(X,Y)}{p(Y)}, -$$ +

      The quantity \( p(Y\vert X) \) on the right-hand side of the theorem is +evaluated for the observed data \( Y \) and can be viewed as a function of +the parameter space represented by \( X \). This function is not +necesseraly normalized and is normally called the likelihood function. +

      -

      which we can rewrite as

      +

      The function \( p(X) \) on the right hand side is called the prior while the function on the left hand side is the called the posterior probability. The denominator on the right hand side serves as a normalization factor for the posterior distribution.

      -$$ -p(X\vert Y)= \frac{p(X,Y)}{\sum_{i=0}^{n-1}p(Y\vert X=x_i)p(x_i)}=\frac{p(Y\vert X)p(X)}{\sum_{i=0}^{n-1}p(Y\vert X=x_i)p(x_i)}, -$$ - -

      which is Bayes' theorem. It allows us to evaluate the uncertainty in in \( X \) after we have observed \( Y \). We can easily interchange \( X \) with \( Y \).

      +

      Let us try to illustrate Bayes' theorem through an example.

      @@ -334,7 +329,6 @@ $$

    12412. 57
    12413. 58
    12414. 59
    12415. -
    12416. 60
    12417. »
    12418. diff --git a/doc/pub/week36/html/._week36-bs052.html b/doc/pub/week36/html/._week36-bs052.html index 0eae500c9..64f292ff0 100644 --- a/doc/pub/week36/html/._week36-bs052.html +++ b/doc/pub/week36/html/._week36-bs052.html @@ -37,7 +37,6 @@ doconce format html week36.do.txt --html_style=bootstrap --pygments_html_style=d
    12419. Plans for week 36
    12420. -
    12421. Thursday September 8
    12422. -
    12423. Summary from last Week and discussion of SVD, Ridge and Lasso regression with examples
    12424. -
    12425. Linear Regression and the SVD
    12426. -
    12427. What does it mean?
    12428. -
    12429. And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
    12430. -
    12431. Code for SVD and Inversion of Matrices
    12432. -
    12433. Inverse of Rectangular Matrix
    12434. -
    12435. Ridge and LASSO Regression
    12436. -
    12437. From OLS to Ridge and Lasso
    12438. -
    12439. Deriving the Ridge Regression Equations
    12440. -
    12441. Note on Scikit-Learn
    12442. -
    12443. Comparison with OLS
    12444. -
    12445. SVD analysis
    12446. -
    12447. Interpreting the Ridge results
    12448. -
    12449. More interpretations
    12450. -
    12451. Deriving the Lasso Regression Equations
    12452. -
    12453. Simple example to illustrate Ordinary Least Squares, Ridge and Lasso Regression
    12454. -
    12455. Ridge Regression
    12456. -
    12457. Lasso Regression
    12458. -
    12459. Yet another Example
    12460. -
    12461. The OLS case
    12462. -
    12463. The Ridge case
    12464. -
    12465. Writing the Cost Function
    12466. -
    12467. Lasso case
    12468. -
    12469. The first Case
    12470. -
    12471. Simple code for solving the above problem
    12472. -
    12473. With Lasso Regression
    12474. -
    12475. Another Example, now with a polynomial fit
    12476. -
    12477. To think about, first part
    12478. -
    12479. More thinking
    12480. -
    12481. Still thinking
    12482. -
    12483. What does centering (subtracting the mean values) mean mathematically?
    12484. -
    12485. Further Manipulations
    12486. -
    12487. Wrapping it up
    12488. -
    12489. Linear Regression code, Intercept handling first
    12490. -
    12491. Code Examples
    12492. -
    12493. Taking out the mean
    12494. -
    12495. Friday September 9
    12496. -
    12497. Linking the regression analysis with a statistical interpretation
    12498. -
    12499. Assumptions made
    12500. -
    12501. Expectation value and variance
    12502. -
    12503. Expectation value and variance for \( \boldsymbol{\beta} \)
    12504. -
    12505. Deriving OLS from a probability distribution
    12506. -
    12507. Independent and Identically Distrubuted (iid)
    12508. -
    12509. Maximum Likelihood Estimation (MLE)
    12510. -
    12511. A new Cost Function
    12512. -
    12513. More basic Statistics and Bayes' theorem
    12514. -
    12515. Marginal Probability
    12516. -
    12517. Conditional Probability
    12518. -
    12519. Bayes' Theorem
    12520. -
    12521. Interpretations of Bayes' Theorem
    12522. -
    12523. Example of Usage of Bayes' theorem
    12524. -
    12525. Doing it correctly
    12526. -
    12527. Bayes' Theorem and Ridge and Lasso Regression
    12528. -
    12529. Test Function for what happens with OLS, Ridge and Lasso
    12530. -
    12531. Invoking Bayes' theorem
    12532. -
    12533. Ridge and Bayes
    12534. -
    12535. Lasso and Bayes
    12536. -
    12537. Exercise 1: mean values and variances in linear regression
    12538. -
    12539. Exercise 2: Adding Ridge and Lasso Regression
    12540. +
    12541. Summary from last Week and discussion of SVD, Ridge and Lasso regression with examples
    12542. +
    12543. Linear Regression and the SVD
    12544. +
    12545. What does it mean?
    12546. +
    12547. And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
    12548. +
    12549. Code for SVD and Inversion of Matrices
    12550. +
    12551. Inverse of Rectangular Matrix
    12552. +
    12553. Ridge and LASSO Regression
    12554. +
    12555. From OLS to Ridge and Lasso
    12556. +
    12557. Deriving the Ridge Regression Equations
    12558. +
    12559. Note on Scikit-Learn
    12560. +
    12561. Comparison with OLS
    12562. +
    12563. SVD analysis
    12564. +
    12565. Interpreting the Ridge results
    12566. +
    12567. More interpretations
    12568. +
    12569. Deriving the Lasso Regression Equations
    12570. +
    12571. Simple example to illustrate Ordinary Least Squares, Ridge and Lasso Regression
    12572. +
    12573. Ridge Regression
    12574. +
    12575. Lasso Regression
    12576. +
    12577. Yet another Example
    12578. +
    12579. The OLS case
    12580. +
    12581. The Ridge case
    12582. +
    12583. Writing the Cost Function
    12584. +
    12585. Lasso case
    12586. +
    12587. The first Case
    12588. +
    12589. Simple code for solving the above problem
    12590. +
    12591. With Lasso Regression
    12592. +
    12593. Another Example, now with a polynomial fit
    12594. +
    12595. To think about, first part
    12596. +
    12597. More thinking
    12598. +
    12599. Still thinking
    12600. +
    12601. What does centering (subtracting the mean values) mean mathematically?
    12602. +
    12603. Further Manipulations
    12604. +
    12605. Wrapping it up
    12606. +
    12607. Linear Regression code, Intercept handling first
    12608. +
    12609. Code Examples
    12610. +
    12611. Taking out the mean
    12612. +
    12613. Friday September 9
    12614. +
    12615. Linking the regression analysis with a statistical interpretation
    12616. +
    12617. Assumptions made
    12618. +
    12619. Expectation value and variance
    12620. +
    12621. Expectation value and variance for \( \boldsymbol{\beta} \)
    12622. +
    12623. Deriving OLS from a probability distribution
    12624. +
    12625. Independent and Identically Distrubuted (iid)
    12626. +
    12627. Maximum Likelihood Estimation (MLE)
    12628. +
    12629. A new Cost Function
    12630. +
    12631. More basic Statistics and Bayes' theorem
    12632. +
    12633. Marginal Probability
    12634. +
    12635. Conditional Probability
    12636. +
    12637. Bayes' Theorem
    12638. +
    12639. Interpretations of Bayes' Theorem
    12640. +
    12641. Example of Usage of Bayes' theorem
    12642. +
    12643. Doing it correctly
    12644. +
    12645. Bayes' Theorem and Ridge and Lasso Regression
    12646. +
    12647. Test Function for what happens with OLS, Ridge and Lasso
    12648. +
    12649. Invoking Bayes' theorem
    12650. +
    12651. Ridge and Bayes
    12652. +
    12653. Lasso and Bayes
    12654. +
    12655. Exercise 1: mean values and variances in linear regression
    12656. +
    12657. Exercise 2: Adding Ridge and Lasso Regression
    12658. @@ -297,17 +295,27 @@ MathJax.Hub.Config({

       

       

       

      -

      Interpretations of Bayes' Theorem

      +

      Example of Usage of Bayes' theorem

      -

      The quantity \( p(Y\vert X) \) on the right-hand side of the theorem is -evaluated for the observed data \( Y \) and can be viewed as a function of -the parameter space represented by \( X \). This function is not -necesseraly normalized and is normally called the likelihood function. +

      Let us suppose that you are undergoing a series of mammography scans in +order to rule out possible breast cancer cases. We define the +sensitivity for a positive event by the variable \( X \). It takes binary +values with \( X=1 \) representing a positive event and \( X=0 \) being a +negative event. We reserve \( Y \) as a classification parameter for +either a negative or a positive breast cancer confirmation. (Short note on wordings: positive here means having breast cancer, although none of us would consider this being a positive thing).

      -

      The function \( p(X) \) on the right hand side is called the prior while the function on the left hand side is the called the posterior probability. The denominator on the right hand side serves as a normalization factor for the posterior distribution.

      +

      We let \( Y=1 \) represent the the case of having breast cancer and \( Y=0 \) as not.

      -

      Let us try to illustrate Bayes' theorem through an example.

      +

      Let us assume that if you have breast cancer, the test will be positive with a probability of \( 0.8 \), that is we have

      + +$$ +p(X=1\vert Y=1) =0.8. +$$ + +

      This obviously sounds scary since many would conclude that if the test is positive, there is a likelihood of \( 80\% \) for having cancer. +It is however not correct, as the following Bayesian analysis shows. +

      @@ -330,7 +338,6 @@ necesseraly normalized and is normally called the likelihood function.

    12659. 57
    12660. 58
    12661. 59
    12662. -
    12663. 60
    12664. »
    12665. diff --git a/doc/pub/week36/html/._week36-bs053.html b/doc/pub/week36/html/._week36-bs053.html index 1ee31c0ff..242731eb8 100644 --- a/doc/pub/week36/html/._week36-bs053.html +++ b/doc/pub/week36/html/._week36-bs053.html @@ -37,7 +37,6 @@ doconce format html week36.do.txt --html_style=bootstrap --pygments_html_style=d
    12666. Plans for week 36
    12667. -
    12668. Thursday September 8
    12669. -
    12670. Summary from last Week and discussion of SVD, Ridge and Lasso regression with examples
    12671. -
    12672. Linear Regression and the SVD
    12673. -
    12674. What does it mean?
    12675. -
    12676. And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
    12677. -
    12678. Code for SVD and Inversion of Matrices
    12679. -
    12680. Inverse of Rectangular Matrix
    12681. -
    12682. Ridge and LASSO Regression
    12683. -
    12684. From OLS to Ridge and Lasso
    12685. -
    12686. Deriving the Ridge Regression Equations
    12687. -
    12688. Note on Scikit-Learn
    12689. -
    12690. Comparison with OLS
    12691. -
    12692. SVD analysis
    12693. -
    12694. Interpreting the Ridge results
    12695. -
    12696. More interpretations
    12697. -
    12698. Deriving the Lasso Regression Equations
    12699. -
    12700. Simple example to illustrate Ordinary Least Squares, Ridge and Lasso Regression
    12701. -
    12702. Ridge Regression
    12703. -
    12704. Lasso Regression
    12705. -
    12706. Yet another Example
    12707. -
    12708. The OLS case
    12709. -
    12710. The Ridge case
    12711. -
    12712. Writing the Cost Function
    12713. -
    12714. Lasso case
    12715. -
    12716. The first Case
    12717. -
    12718. Simple code for solving the above problem
    12719. -
    12720. With Lasso Regression
    12721. -
    12722. Another Example, now with a polynomial fit
    12723. -
    12724. To think about, first part
    12725. -
    12726. More thinking
    12727. -
    12728. Still thinking
    12729. -
    12730. What does centering (subtracting the mean values) mean mathematically?
    12731. -
    12732. Further Manipulations
    12733. -
    12734. Wrapping it up
    12735. -
    12736. Linear Regression code, Intercept handling first
    12737. -
    12738. Code Examples
    12739. -
    12740. Taking out the mean
    12741. -
    12742. Friday September 9
    12743. -
    12744. Linking the regression analysis with a statistical interpretation
    12745. -
    12746. Assumptions made
    12747. -
    12748. Expectation value and variance
    12749. -
    12750. Expectation value and variance for \( \boldsymbol{\beta} \)
    12751. -
    12752. Deriving OLS from a probability distribution
    12753. -
    12754. Independent and Identically Distrubuted (iid)
    12755. -
    12756. Maximum Likelihood Estimation (MLE)
    12757. -
    12758. A new Cost Function
    12759. -
    12760. More basic Statistics and Bayes' theorem
    12761. -
    12762. Marginal Probability
    12763. -
    12764. Conditional Probability
    12765. -
    12766. Bayes' Theorem
    12767. -
    12768. Interpretations of Bayes' Theorem
    12769. -
    12770. Example of Usage of Bayes' theorem
    12771. -
    12772. Doing it correctly
    12773. -
    12774. Bayes' Theorem and Ridge and Lasso Regression
    12775. -
    12776. Test Function for what happens with OLS, Ridge and Lasso
    12777. -
    12778. Invoking Bayes' theorem
    12779. -
    12780. Ridge and Bayes
    12781. -
    12782. Lasso and Bayes
    12783. -
    12784. Exercise 1: mean values and variances in linear regression
    12785. -
    12786. Exercise 2: Adding Ridge and Lasso Regression
    12787. +
    12788. Summary from last Week and discussion of SVD, Ridge and Lasso regression with examples
    12789. +
    12790. Linear Regression and the SVD
    12791. +
    12792. What does it mean?
    12793. +
    12794. And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
    12795. +
    12796. Code for SVD and Inversion of Matrices
    12797. +
    12798. Inverse of Rectangular Matrix
    12799. +
    12800. Ridge and LASSO Regression
    12801. +
    12802. From OLS to Ridge and Lasso
    12803. +
    12804. Deriving the Ridge Regression Equations
    12805. +
    12806. Note on Scikit-Learn
    12807. +
    12808. Comparison with OLS
    12809. +
    12810. SVD analysis
    12811. +
    12812. Interpreting the Ridge results
    12813. +
    12814. More interpretations
    12815. +
    12816. Deriving the Lasso Regression Equations
    12817. +
    12818. Simple example to illustrate Ordinary Least Squares, Ridge and Lasso Regression
    12819. +
    12820. Ridge Regression
    12821. +
    12822. Lasso Regression
    12823. +
    12824. Yet another Example
    12825. +
    12826. The OLS case
    12827. +
    12828. The Ridge case
    12829. +
    12830. Writing the Cost Function
    12831. +
    12832. Lasso case
    12833. +
    12834. The first Case
    12835. +
    12836. Simple code for solving the above problem
    12837. +
    12838. With Lasso Regression
    12839. +
    12840. Another Example, now with a polynomial fit
    12841. +
    12842. To think about, first part
    12843. +
    12844. More thinking
    12845. +
    12846. Still thinking
    12847. +
    12848. What does centering (subtracting the mean values) mean mathematically?
    12849. +
    12850. Further Manipulations
    12851. +
    12852. Wrapping it up
    12853. +
    12854. Linear Regression code, Intercept handling first
    12855. +
    12856. Code Examples
    12857. +
    12858. Taking out the mean
    12859. +
    12860. Friday September 9
    12861. +
    12862. Linking the regression analysis with a statistical interpretation
    12863. +
    12864. Assumptions made
    12865. +
    12866. Expectation value and variance
    12867. +
    12868. Expectation value and variance for \( \boldsymbol{\beta} \)
    12869. +
    12870. Deriving OLS from a probability distribution
    12871. +
    12872. Independent and Identically Distrubuted (iid)
    12873. +
    12874. Maximum Likelihood Estimation (MLE)
    12875. +
    12876. A new Cost Function
    12877. +
    12878. More basic Statistics and Bayes' theorem
    12879. +
    12880. Marginal Probability
    12881. +
    12882. Conditional Probability
    12883. +
    12884. Bayes' Theorem
    12885. +
    12886. Interpretations of Bayes' Theorem
    12887. +
    12888. Example of Usage of Bayes' theorem
    12889. +
    12890. Doing it correctly
    12891. +
    12892. Bayes' Theorem and Ridge and Lasso Regression
    12893. +
    12894. Test Function for what happens with OLS, Ridge and Lasso
    12895. +
    12896. Invoking Bayes' theorem
    12897. +
    12898. Ridge and Bayes
    12899. +
    12900. Lasso and Bayes
    12901. +
    12902. Exercise 1: mean values and variances in linear regression
    12903. +
    12904. Exercise 2: Adding Ridge and Lasso Regression
    12905. @@ -297,27 +295,28 @@ MathJax.Hub.Config({

       

       

       

      -

      Example of Usage of Bayes' theorem

      +

      Doing it correctly

      -

      Let us suppose that you are undergoing a series of mammography scans in -order to rule out possible breast cancer cases. We define the -sensitivity for a positive event by the variable \( X \). It takes binary -values with \( X=1 \) representing a positive event and \( X=0 \) being a -negative event. We reserve \( Y \) as a classification parameter for -either a negative or a positive breast cancer confirmation. (Short note on wordings: positive here means having breast cancer, although none of us would consider this being a positive thing). +

      If we look at various national surveys on breast cancer, the general likelihood of developing breast cancer is a very small number. +Let us assume that the prior probability in the population as a whole is

      -

      We let \( Y=1 \) represent the the case of having breast cancer and \( Y=0 \) as not.

      - -

      Let us assume that if you have breast cancer, the test will be positive with a probability of \( 0.8 \), that is we have

      - $$ -p(X=1\vert Y=1) =0.8. +p(Y=1) =0.004. $$ -

      This obviously sounds scary since many would conclude that if the test is positive, there is a likelihood of \( 80\% \) for having cancer. -It is however not correct, as the following Bayesian analysis shows. -

      +

      We need also to account for the fact that the test may produce a false positive result (false alarm). Let us here assume that we have

      +$$ +p(X=1\vert Y=0) =0.1. +$$ + +

      Using Bayes' theorem we can then find the posterior probability that the person has breast cancer in case of a positive test, that is we can compute

      + +$$ +p(Y=1\vert X=1)=\frac{p(X=1\vert Y=1)p(Y=1)}{p(X=1\vert Y=1)p(Y=1)+p(X=1\vert Y=0)p(Y=0)}=\frac{0.8\times 0.004}{0.8\times 0.004+0.1\times 0.996}=0.031. +$$ + +

      That is, in case of a positive test, there is only a \( 3\% \) chance of having breast cancer!

      @@ -339,7 +338,6 @@ It is however not correct, as the following Bayesian analysis shows.

    12906. 57
    12907. 58
    12908. 59
    12909. -
    12910. 60
    12911. »
    12912. diff --git a/doc/pub/week36/html/._week36-bs054.html b/doc/pub/week36/html/._week36-bs054.html index 6d76121de..9fb68e006 100644 --- a/doc/pub/week36/html/._week36-bs054.html +++ b/doc/pub/week36/html/._week36-bs054.html @@ -37,7 +37,6 @@ doconce format html week36.do.txt --html_style=bootstrap --pygments_html_style=d
    12913. Plans for week 36
    12914. -
    12915. Thursday September 8
    12916. -
    12917. Summary from last Week and discussion of SVD, Ridge and Lasso regression with examples
    12918. -
    12919. Linear Regression and the SVD
    12920. -
    12921. What does it mean?
    12922. -
    12923. And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
    12924. -
    12925. Code for SVD and Inversion of Matrices
    12926. -
    12927. Inverse of Rectangular Matrix
    12928. -
    12929. Ridge and LASSO Regression
    12930. -
    12931. From OLS to Ridge and Lasso
    12932. -
    12933. Deriving the Ridge Regression Equations
    12934. -
    12935. Note on Scikit-Learn
    12936. -
    12937. Comparison with OLS
    12938. -
    12939. SVD analysis
    12940. -
    12941. Interpreting the Ridge results
    12942. -
    12943. More interpretations
    12944. -
    12945. Deriving the Lasso Regression Equations
    12946. -
    12947. Simple example to illustrate Ordinary Least Squares, Ridge and Lasso Regression
    12948. -
    12949. Ridge Regression
    12950. -
    12951. Lasso Regression
    12952. -
    12953. Yet another Example
    12954. -
    12955. The OLS case
    12956. -
    12957. The Ridge case
    12958. -
    12959. Writing the Cost Function
    12960. -
    12961. Lasso case
    12962. -
    12963. The first Case
    12964. -
    12965. Simple code for solving the above problem
    12966. -
    12967. With Lasso Regression
    12968. -
    12969. Another Example, now with a polynomial fit
    12970. -
    12971. To think about, first part
    12972. -
    12973. More thinking
    12974. -
    12975. Still thinking
    12976. -
    12977. What does centering (subtracting the mean values) mean mathematically?
    12978. -
    12979. Further Manipulations
    12980. -
    12981. Wrapping it up
    12982. -
    12983. Linear Regression code, Intercept handling first
    12984. -
    12985. Code Examples
    12986. -
    12987. Taking out the mean
    12988. -
    12989. Friday September 9
    12990. -
    12991. Linking the regression analysis with a statistical interpretation
    12992. -
    12993. Assumptions made
    12994. -
    12995. Expectation value and variance
    12996. -
    12997. Expectation value and variance for \( \boldsymbol{\beta} \)
    12998. -
    12999. Deriving OLS from a probability distribution
    13000. -
    13001. Independent and Identically Distrubuted (iid)
    13002. -
    13003. Maximum Likelihood Estimation (MLE)
    13004. -
    13005. A new Cost Function
    13006. -
    13007. More basic Statistics and Bayes' theorem
    13008. -
    13009. Marginal Probability
    13010. -
    13011. Conditional Probability
    13012. -
    13013. Bayes' Theorem
    13014. -
    13015. Interpretations of Bayes' Theorem
    13016. -
    13017. Example of Usage of Bayes' theorem
    13018. -
    13019. Doing it correctly
    13020. -
    13021. Bayes' Theorem and Ridge and Lasso Regression
    13022. -
    13023. Test Function for what happens with OLS, Ridge and Lasso
    13024. -
    13025. Invoking Bayes' theorem
    13026. -
    13027. Ridge and Bayes
    13028. -
    13029. Lasso and Bayes
    13030. -
    13031. Exercise 1: mean values and variances in linear regression
    13032. -
    13033. Exercise 2: Adding Ridge and Lasso Regression
    13034. +
    13035. Summary from last Week and discussion of SVD, Ridge and Lasso regression with examples
    13036. +
    13037. Linear Regression and the SVD
    13038. +
    13039. What does it mean?
    13040. +
    13041. And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
    13042. +
    13043. Code for SVD and Inversion of Matrices
    13044. +
    13045. Inverse of Rectangular Matrix
    13046. +
    13047. Ridge and LASSO Regression
    13048. +
    13049. From OLS to Ridge and Lasso
    13050. +
    13051. Deriving the Ridge Regression Equations
    13052. +
    13053. Note on Scikit-Learn
    13054. +
    13055. Comparison with OLS
    13056. +
    13057. SVD analysis
    13058. +
    13059. Interpreting the Ridge results
    13060. +
    13061. More interpretations
    13062. +
    13063. Deriving the Lasso Regression Equations
    13064. +
    13065. Simple example to illustrate Ordinary Least Squares, Ridge and Lasso Regression
    13066. +
    13067. Ridge Regression
    13068. +
    13069. Lasso Regression
    13070. +
    13071. Yet another Example
    13072. +
    13073. The OLS case
    13074. +
    13075. The Ridge case
    13076. +
    13077. Writing the Cost Function
    13078. +
    13079. Lasso case
    13080. +
    13081. The first Case
    13082. +
    13083. Simple code for solving the above problem
    13084. +
    13085. With Lasso Regression
    13086. +
    13087. Another Example, now with a polynomial fit
    13088. +
    13089. To think about, first part
    13090. +
    13091. More thinking
    13092. +
    13093. Still thinking
    13094. +
    13095. What does centering (subtracting the mean values) mean mathematically?
    13096. +
    13097. Further Manipulations
    13098. +
    13099. Wrapping it up
    13100. +
    13101. Linear Regression code, Intercept handling first
    13102. +
    13103. Code Examples
    13104. +
    13105. Taking out the mean
    13106. +
    13107. Friday September 9
    13108. +
    13109. Linking the regression analysis with a statistical interpretation
    13110. +
    13111. Assumptions made
    13112. +
    13113. Expectation value and variance
    13114. +
    13115. Expectation value and variance for \( \boldsymbol{\beta} \)
    13116. +
    13117. Deriving OLS from a probability distribution
    13118. +
    13119. Independent and Identically Distrubuted (iid)
    13120. +
    13121. Maximum Likelihood Estimation (MLE)
    13122. +
    13123. A new Cost Function
    13124. +
    13125. More basic Statistics and Bayes' theorem
    13126. +
    13127. Marginal Probability
    13128. +
    13129. Conditional Probability
    13130. +
    13131. Bayes' Theorem
    13132. +
    13133. Interpretations of Bayes' Theorem
    13134. +
    13135. Example of Usage of Bayes' theorem
    13136. +
    13137. Doing it correctly
    13138. +
    13139. Bayes' Theorem and Ridge and Lasso Regression
    13140. +
    13141. Test Function for what happens with OLS, Ridge and Lasso
    13142. +
    13143. Invoking Bayes' theorem
    13144. +
    13145. Ridge and Bayes
    13146. +
    13147. Lasso and Bayes
    13148. +
    13149. Exercise 1: mean values and variances in linear regression
    13150. +
    13151. Exercise 2: Adding Ridge and Lasso Regression
    13152. @@ -297,28 +295,15 @@ MathJax.Hub.Config({

       

       

       

      -

      Doing it correctly

      +

      Bayes' Theorem and Ridge and Lasso Regression

      -

      If we look at various national surveys on breast cancer, the general likelihood of developing breast cancer is a very small number. -Let us assume that the prior probability in the population as a whole is +

      Hitherto we have discussed Ridge and Lasso regression in terms of a +linear analysis. This may to many of you feel rather technical and +perhaps not that intuitive. The question is whether we can develop a +more intuitive way of understanding what Ridge and Lasso express.

      -$$ -p(Y=1) =0.004. -$$ - -

      We need also to account for the fact that the test may produce a false positive result (false alarm). Let us here assume that we have

      -$$ -p(X=1\vert Y=0) =0.1. -$$ - -

      Using Bayes' theorem we can then find the posterior probability that the person has breast cancer in case of a positive test, that is we can compute

      - -$$ -p(Y=1\vert X=1)=\frac{p(X=1\vert Y=1)p(Y=1)}{p(X=1\vert Y=1)p(Y=1)+p(X=1\vert Y=0)p(Y=0)}=\frac{0.8\times 0.004}{0.8\times 0.004+0.1\times 0.996}=0.031. -$$ - -

      That is, in case of a positive test, there is only a \( 3\% \) chance of having breast cancer!

      +

      Before we proceed let us perform a Ridge, Lasso and OLS analysis of a polynomial fit.

      @@ -339,7 +324,6 @@ $$

    13153. 57
    13154. 58
    13155. 59
    13156. -
    13157. 60
    13158. »
    13159. diff --git a/doc/pub/week36/html/._week36-bs055.html b/doc/pub/week36/html/._week36-bs055.html index 89b466920..5073e52ea 100644 --- a/doc/pub/week36/html/._week36-bs055.html +++ b/doc/pub/week36/html/._week36-bs055.html @@ -37,7 +37,6 @@ doconce format html week36.do.txt --html_style=bootstrap --pygments_html_style=d
    13160. Plans for week 36
    13161. -
    13162. Thursday September 8
    13163. -
    13164. Summary from last Week and discussion of SVD, Ridge and Lasso regression with examples
    13165. -
    13166. Linear Regression and the SVD
    13167. -
    13168. What does it mean?
    13169. -
    13170. And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
    13171. -
    13172. Code for SVD and Inversion of Matrices
    13173. -
    13174. Inverse of Rectangular Matrix
    13175. -
    13176. Ridge and LASSO Regression
    13177. -
    13178. From OLS to Ridge and Lasso
    13179. -
    13180. Deriving the Ridge Regression Equations
    13181. -
    13182. Note on Scikit-Learn
    13183. -
    13184. Comparison with OLS
    13185. -
    13186. SVD analysis
    13187. -
    13188. Interpreting the Ridge results
    13189. -
    13190. More interpretations
    13191. -
    13192. Deriving the Lasso Regression Equations
    13193. -
    13194. Simple example to illustrate Ordinary Least Squares, Ridge and Lasso Regression
    13195. -
    13196. Ridge Regression
    13197. -
    13198. Lasso Regression
    13199. -
    13200. Yet another Example
    13201. -
    13202. The OLS case
    13203. -
    13204. The Ridge case
    13205. -
    13206. Writing the Cost Function
    13207. -
    13208. Lasso case
    13209. -
    13210. The first Case
    13211. -
    13212. Simple code for solving the above problem
    13213. -
    13214. With Lasso Regression
    13215. -
    13216. Another Example, now with a polynomial fit
    13217. -
    13218. To think about, first part
    13219. -
    13220. More thinking
    13221. -
    13222. Still thinking
    13223. -
    13224. What does centering (subtracting the mean values) mean mathematically?
    13225. -
    13226. Further Manipulations
    13227. -
    13228. Wrapping it up
    13229. -
    13230. Linear Regression code, Intercept handling first
    13231. -
    13232. Code Examples
    13233. -
    13234. Taking out the mean
    13235. -
    13236. Friday September 9
    13237. -
    13238. Linking the regression analysis with a statistical interpretation
    13239. -
    13240. Assumptions made
    13241. -
    13242. Expectation value and variance
    13243. -
    13244. Expectation value and variance for \( \boldsymbol{\beta} \)
    13245. -
    13246. Deriving OLS from a probability distribution
    13247. -
    13248. Independent and Identically Distrubuted (iid)
    13249. -
    13250. Maximum Likelihood Estimation (MLE)
    13251. -
    13252. A new Cost Function
    13253. -
    13254. More basic Statistics and Bayes' theorem
    13255. -
    13256. Marginal Probability
    13257. -
    13258. Conditional Probability
    13259. -
    13260. Bayes' Theorem
    13261. -
    13262. Interpretations of Bayes' Theorem
    13263. -
    13264. Example of Usage of Bayes' theorem
    13265. -
    13266. Doing it correctly
    13267. -
    13268. Bayes' Theorem and Ridge and Lasso Regression
    13269. -
    13270. Test Function for what happens with OLS, Ridge and Lasso
    13271. -
    13272. Invoking Bayes' theorem
    13273. -
    13274. Ridge and Bayes
    13275. -
    13276. Lasso and Bayes
    13277. -
    13278. Exercise 1: mean values and variances in linear regression
    13279. -
    13280. Exercise 2: Adding Ridge and Lasso Regression
    13281. +
    13282. Summary from last Week and discussion of SVD, Ridge and Lasso regression with examples
    13283. +
    13284. Linear Regression and the SVD
    13285. +
    13286. What does it mean?
    13287. +
    13288. And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
    13289. +
    13290. Code for SVD and Inversion of Matrices
    13291. +
    13292. Inverse of Rectangular Matrix
    13293. +
    13294. Ridge and LASSO Regression
    13295. +
    13296. From OLS to Ridge and Lasso
    13297. +
    13298. Deriving the Ridge Regression Equations
    13299. +
    13300. Note on Scikit-Learn
    13301. +
    13302. Comparison with OLS
    13303. +
    13304. SVD analysis
    13305. +
    13306. Interpreting the Ridge results
    13307. +
    13308. More interpretations
    13309. +
    13310. Deriving the Lasso Regression Equations
    13311. +
    13312. Simple example to illustrate Ordinary Least Squares, Ridge and Lasso Regression
    13313. +
    13314. Ridge Regression
    13315. +
    13316. Lasso Regression
    13317. +
    13318. Yet another Example
    13319. +
    13320. The OLS case
    13321. +
    13322. The Ridge case
    13323. +
    13324. Writing the Cost Function
    13325. +
    13326. Lasso case
    13327. +
    13328. The first Case
    13329. +
    13330. Simple code for solving the above problem
    13331. +
    13332. With Lasso Regression
    13333. +
    13334. Another Example, now with a polynomial fit
    13335. +
    13336. To think about, first part
    13337. +
    13338. More thinking
    13339. +
    13340. Still thinking
    13341. +
    13342. What does centering (subtracting the mean values) mean mathematically?
    13343. +
    13344. Further Manipulations
    13345. +
    13346. Wrapping it up
    13347. +
    13348. Linear Regression code, Intercept handling first
    13349. +
    13350. Code Examples
    13351. +
    13352. Taking out the mean
    13353. +
    13354. Friday September 9
    13355. +
    13356. Linking the regression analysis with a statistical interpretation
    13357. +
    13358. Assumptions made
    13359. +
    13360. Expectation value and variance
    13361. +
    13362. Expectation value and variance for \( \boldsymbol{\beta} \)
    13363. +
    13364. Deriving OLS from a probability distribution
    13365. +
    13366. Independent and Identically Distrubuted (iid)
    13367. +
    13368. Maximum Likelihood Estimation (MLE)
    13369. +
    13370. A new Cost Function
    13371. +
    13372. More basic Statistics and Bayes' theorem
    13373. +
    13374. Marginal Probability
    13375. +
    13376. Conditional Probability
    13377. +
    13378. Bayes' Theorem
    13379. +
    13380. Interpretations of Bayes' Theorem
    13381. +
    13382. Example of Usage of Bayes' theorem
    13383. +
    13384. Doing it correctly
    13385. +
    13386. Bayes' Theorem and Ridge and Lasso Regression
    13387. +
    13388. Test Function for what happens with OLS, Ridge and Lasso
    13389. +
    13390. Invoking Bayes' theorem
    13391. +
    13392. Ridge and Bayes
    13393. +
    13394. Lasso and Bayes
    13395. +
    13396. Exercise 1: mean values and variances in linear regression
    13397. +
    13398. Exercise 2: Adding Ridge and Lasso Regression
    13399. @@ -297,15 +295,103 @@ MathJax.Hub.Config({

       

       

       

      -

      Bayes' Theorem and Ridge and Lasso Regression

      +

      Test Function for what happens with OLS, Ridge and Lasso

      -

      Hitherto we have discussed Ridge and Lasso regression in terms of a -linear analysis. This may to many of you feel rather technical and -perhaps not that intuitive. The question is whether we can develop a -more intuitive way of understanding what Ridge and Lasso express. +

      We will play around with a study of the values for the optimal +parameters \( \boldsymbol{\beta} \) using OLS, Ridge and Lasso regression. For +OLS, you will notice as function of the noise and polynomial degree, +that the parameters \( \beta \) will fluctuate from order to order in the +polynomial fit and that for larger and larger polynomial degrees of freedom, the parameters will tend to increase in value for OLS.

      -

      Before we proceed let us perform a Ridge, Lasso and OLS analysis of a polynomial fit.

      +

      For Ridge and Lasso regression, the higher order parameters will typically be reduced, providing thereby less fluctuations from one order to another one.

      + + + +
      +
      +
      +
      +
      +
      import numpy as np
      +import matplotlib.pyplot as plt
      +from sklearn.model_selection import train_test_split
      +from sklearn import linear_model
      +
      +def R2(y_data, y_model):
      +    return 1 - np.sum((y_data - y_model) ** 2) / np.sum((y_data - np.mean(y_data)) ** 2)
      +def MSE(y_data,y_model):
      +    n = np.size(y_model)
      +    return np.sum((y_data-y_model)**2)/n
      +
      +# Make data set.
      +n = 10000
      +x = np.random.rand(n)
      +y = np.exp(-x**2) + 1.5 * np.exp(-(x-2)**2)+ np.random.randn(n)
      +
      +Maxpolydegree = 5
      +X = np.zeros((len(x),Maxpolydegree))
      +X[:,0] = 1.0
      +
      +for polydegree in range(1, Maxpolydegree):
      +    for degree in range(polydegree):
      +        X[:,degree] = x**(degree)
      +
      +
      +# We split the data in test and training data
      +X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
      +
      +# matrix inversion to find beta
      +OLSbeta = np.linalg.pinv(X_train.T @ X_train) @ X_train.T @ y_train
      +print(OLSbeta)
      +ypredictOLS = X_test @ OLSbeta
      +print("Test MSE OLS")
      +print(MSE(y_test,ypredictOLS))
      +# Repeat now for Lasso and Ridge regression and various values of the regularization parameter using Scikit-Learn
      +# Decide which values of lambda to use
      +nlambdas = 4
      +MSERidgePredict = np.zeros(nlambdas)
      +MSELassoPredict = np.zeros(nlambdas)
      +lambdas = np.logspace(-3, 1, nlambdas)
      +for i in range(nlambdas):
      +    lmb = lambdas[i]
      +    # Make the fit using Ridge and Lasso
      +    RegRidge = linear_model.Ridge(lmb,fit_intercept=False)
      +    RegRidge.fit(X_train,y_train)
      +    RegLasso = linear_model.Lasso(lmb,fit_intercept=False)
      +    RegLasso.fit(X_train,y_train)
      +    # and then make the prediction
      +    ypredictRidge = RegRidge.predict(X_test)
      +    ypredictLasso = RegLasso.predict(X_test)
      +    # Compute the MSE and print it
      +    MSERidgePredict[i] = MSE(y_test,ypredictRidge)
      +    MSELassoPredict[i] = MSE(y_test,ypredictLasso)
      +    print(lmb,RegRidge.coef_)
      +    print(lmb,RegLasso.coef_)
      +# Now plot the results
      +plt.figure()
      +plt.plot(np.log10(lambdas), MSERidgePredict, 'b', label = 'MSE Ridge Test')
      +plt.plot(np.log10(lambdas), MSELassoPredict, 'r', label = 'MSE Lasso Test')
      +plt.xlabel('log10(lambda)')
      +plt.ylabel('MSE')
      +plt.legend()
      +plt.show()
      +
      +
      +
      +
      +
      +
      +
      +
      +
      +
      +
      +
      +
      +
      + +

      How can we understand this?

      @@ -325,7 +411,6 @@ more intuitive way of understanding what Ridge and Lasso express.

    13400. 57
    13401. 58
    13402. 59
    13403. -
    13404. 60
    13405. »
    13406. diff --git a/doc/pub/week36/html/._week36-bs056.html b/doc/pub/week36/html/._week36-bs056.html index 3670e8727..d675aa3bc 100644 --- a/doc/pub/week36/html/._week36-bs056.html +++ b/doc/pub/week36/html/._week36-bs056.html @@ -37,7 +37,6 @@ doconce format html week36.do.txt --html_style=bootstrap --pygments_html_style=d
    13407. Plans for week 36
    13408. -
    13409. Thursday September 8
    13410. -
    13411. Summary from last Week and discussion of SVD, Ridge and Lasso regression with examples
    13412. -
    13413. Linear Regression and the SVD
    13414. -
    13415. What does it mean?
    13416. -
    13417. And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
    13418. -
    13419. Code for SVD and Inversion of Matrices
    13420. -
    13421. Inverse of Rectangular Matrix
    13422. -
    13423. Ridge and LASSO Regression
    13424. -
    13425. From OLS to Ridge and Lasso
    13426. -
    13427. Deriving the Ridge Regression Equations
    13428. -
    13429. Note on Scikit-Learn
    13430. -
    13431. Comparison with OLS
    13432. -
    13433. SVD analysis
    13434. -
    13435. Interpreting the Ridge results
    13436. -
    13437. More interpretations
    13438. -
    13439. Deriving the Lasso Regression Equations
    13440. -
    13441. Simple example to illustrate Ordinary Least Squares, Ridge and Lasso Regression
    13442. -
    13443. Ridge Regression
    13444. -
    13445. Lasso Regression
    13446. -
    13447. Yet another Example
    13448. -
    13449. The OLS case
    13450. -
    13451. The Ridge case
    13452. -
    13453. Writing the Cost Function
    13454. -
    13455. Lasso case
    13456. -
    13457. The first Case
    13458. -
    13459. Simple code for solving the above problem
    13460. -
    13461. With Lasso Regression
    13462. -
    13463. Another Example, now with a polynomial fit
    13464. -
    13465. To think about, first part
    13466. -
    13467. More thinking
    13468. -
    13469. Still thinking
    13470. -
    13471. What does centering (subtracting the mean values) mean mathematically?
    13472. -
    13473. Further Manipulations
    13474. -
    13475. Wrapping it up
    13476. -
    13477. Linear Regression code, Intercept handling first
    13478. -
    13479. Code Examples
    13480. -
    13481. Taking out the mean
    13482. -
    13483. Friday September 9
    13484. -
    13485. Linking the regression analysis with a statistical interpretation
    13486. -
    13487. Assumptions made
    13488. -
    13489. Expectation value and variance
    13490. -
    13491. Expectation value and variance for \( \boldsymbol{\beta} \)
    13492. -
    13493. Deriving OLS from a probability distribution
    13494. -
    13495. Independent and Identically Distrubuted (iid)
    13496. -
    13497. Maximum Likelihood Estimation (MLE)
    13498. -
    13499. A new Cost Function
    13500. -
    13501. More basic Statistics and Bayes' theorem
    13502. -
    13503. Marginal Probability
    13504. -
    13505. Conditional Probability
    13506. -
    13507. Bayes' Theorem
    13508. -
    13509. Interpretations of Bayes' Theorem
    13510. -
    13511. Example of Usage of Bayes' theorem
    13512. -
    13513. Doing it correctly
    13514. -
    13515. Bayes' Theorem and Ridge and Lasso Regression
    13516. -
    13517. Test Function for what happens with OLS, Ridge and Lasso
    13518. -
    13519. Invoking Bayes' theorem
    13520. -
    13521. Ridge and Bayes
    13522. -
    13523. Lasso and Bayes
    13524. -
    13525. Exercise 1: mean values and variances in linear regression
    13526. -
    13527. Exercise 2: Adding Ridge and Lasso Regression
    13528. +
    13529. Summary from last Week and discussion of SVD, Ridge and Lasso regression with examples
    13530. +
    13531. Linear Regression and the SVD
    13532. +
    13533. What does it mean?
    13534. +
    13535. And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
    13536. +
    13537. Code for SVD and Inversion of Matrices
    13538. +
    13539. Inverse of Rectangular Matrix
    13540. +
    13541. Ridge and LASSO Regression
    13542. +
    13543. From OLS to Ridge and Lasso
    13544. +
    13545. Deriving the Ridge Regression Equations
    13546. +
    13547. Note on Scikit-Learn
    13548. +
    13549. Comparison with OLS
    13550. +
    13551. SVD analysis
    13552. +
    13553. Interpreting the Ridge results
    13554. +
    13555. More interpretations
    13556. +
    13557. Deriving the Lasso Regression Equations
    13558. +
    13559. Simple example to illustrate Ordinary Least Squares, Ridge and Lasso Regression
    13560. +
    13561. Ridge Regression
    13562. +
    13563. Lasso Regression
    13564. +
    13565. Yet another Example
    13566. +
    13567. The OLS case
    13568. +
    13569. The Ridge case
    13570. +
    13571. Writing the Cost Function
    13572. +
    13573. Lasso case
    13574. +
    13575. The first Case
    13576. +
    13577. Simple code for solving the above problem
    13578. +
    13579. With Lasso Regression
    13580. +
    13581. Another Example, now with a polynomial fit
    13582. +
    13583. To think about, first part
    13584. +
    13585. More thinking
    13586. +
    13587. Still thinking
    13588. +
    13589. What does centering (subtracting the mean values) mean mathematically?
    13590. +
    13591. Further Manipulations
    13592. +
    13593. Wrapping it up
    13594. +
    13595. Linear Regression code, Intercept handling first
    13596. +
    13597. Code Examples
    13598. +
    13599. Taking out the mean
    13600. +
    13601. Friday September 9
    13602. +
    13603. Linking the regression analysis with a statistical interpretation
    13604. +
    13605. Assumptions made
    13606. +
    13607. Expectation value and variance
    13608. +
    13609. Expectation value and variance for \( \boldsymbol{\beta} \)
    13610. +
    13611. Deriving OLS from a probability distribution
    13612. +
    13613. Independent and Identically Distrubuted (iid)
    13614. +
    13615. Maximum Likelihood Estimation (MLE)
    13616. +
    13617. A new Cost Function
    13618. +
    13619. More basic Statistics and Bayes' theorem
    13620. +
    13621. Marginal Probability
    13622. +
    13623. Conditional Probability
    13624. +
    13625. Bayes' Theorem
    13626. +
    13627. Interpretations of Bayes' Theorem
    13628. +
    13629. Example of Usage of Bayes' theorem
    13630. +
    13631. Doing it correctly
    13632. +
    13633. Bayes' Theorem and Ridge and Lasso Regression
    13634. +
    13635. Test Function for what happens with OLS, Ridge and Lasso
    13636. +
    13637. Invoking Bayes' theorem
    13638. +
    13639. Ridge and Bayes
    13640. +
    13641. Lasso and Bayes
    13642. +
    13643. Exercise 1: mean values and variances in linear regression
    13644. +
    13645. Exercise 2: Adding Ridge and Lasso Regression
    13646. @@ -297,103 +295,32 @@ MathJax.Hub.Config({

       

       

       

      -

      Test Function for what happens with OLS, Ridge and Lasso

      +

      Invoking Bayes' theorem

      -

      We will play around with a study of the values for the optimal -parameters \( \boldsymbol{\beta} \) using OLS, Ridge and Lasso regression. For -OLS, you will notice as function of the noise and polynomial degree, -that the parameters \( \beta \) will fluctuate from order to order in the -polynomial fit and that for larger and larger polynomial degrees of freedom, the parameters will tend to increase in value for OLS. -

      +

      Using Bayes' theorem we can gain a better intuition about Ridge and Lasso regression.

      -

      For Ridge and Lasso regression, the higher order parameters will typically be reduced, providing thereby less fluctuations from one order to another one.

      +

      For ordinary least squares we postulated that the maximum likelihood for the doamin of events \( \boldsymbol{D} \) (one-dimensional case)

      +$$ +\boldsymbol{D}=[(x_0,y_0), (x_1,y_1),\dots, (x_{n-1},y_{n-1})], +$$ +

      is given by

      +$$ +p(\boldsymbol{D}\vert\boldsymbol{\beta})=\prod_{i=0}^{n-1}\frac{1}{\sqrt{2\pi\sigma^2}}\exp{\left[-\frac{(y_i-\boldsymbol{X}_{i,*}\boldsymbol{\beta})^2}{2\sigma^2}\right]}. +$$ - -
      -
      -
      -
      -
      -
      import numpy as np
      -import matplotlib.pyplot as plt
      -from sklearn.model_selection import train_test_split
      -from sklearn import linear_model
      +

      In Bayes' theorem this function plays the role of the so-called likelihood. We could now ask the question what is the posterior probability of a parameter set \( \boldsymbol{\beta} \) given a domain of events \( \boldsymbol{D} \)? That is, how can we define the posterior probability

      -def R2(y_data, y_model): - return 1 - np.sum((y_data - y_model) ** 2) / np.sum((y_data - np.mean(y_data)) ** 2) -def MSE(y_data,y_model): - n = np.size(y_model) - return np.sum((y_data-y_model)**2)/n +$$ +p(\boldsymbol{\beta}\vert\boldsymbol{D}). +$$ -# Make data set. -n = 10000 -x = np.random.rand(n) -y = np.exp(-x**2) + 1.5 * np.exp(-(x-2)**2)+ np.random.randn(n) +

      Bayes' theorem comes to our rescue here since (omitting the normalization constant)

      +$$ +p(\boldsymbol{\beta}\vert\boldsymbol{D})\propto p(\boldsymbol{D}\vert\boldsymbol{\beta})p(\boldsymbol{\beta}). +$$ -Maxpolydegree = 5 -X = np.zeros((len(x),Maxpolydegree)) -X[:,0] = 1.0 - -for polydegree in range(1, Maxpolydegree): - for degree in range(polydegree): - X[:,degree] = x**(degree) - - -# We split the data in test and training data -X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2) - -# matrix inversion to find beta -OLSbeta = np.linalg.pinv(X_train.T @ X_train) @ X_train.T @ y_train -print(OLSbeta) -ypredictOLS = X_test @ OLSbeta -print("Test MSE OLS") -print(MSE(y_test,ypredictOLS)) -# Repeat now for Lasso and Ridge regression and various values of the regularization parameter using Scikit-Learn -# Decide which values of lambda to use -nlambdas = 4 -MSERidgePredict = np.zeros(nlambdas) -MSELassoPredict = np.zeros(nlambdas) -lambdas = np.logspace(-3, 1, nlambdas) -for i in range(nlambdas): - lmb = lambdas[i] - # Make the fit using Ridge and Lasso - RegRidge = linear_model.Ridge(lmb,fit_intercept=False) - RegRidge.fit(X_train,y_train) - RegLasso = linear_model.Lasso(lmb,fit_intercept=False) - RegLasso.fit(X_train,y_train) - # and then make the prediction - ypredictRidge = RegRidge.predict(X_test) - ypredictLasso = RegLasso.predict(X_test) - # Compute the MSE and print it - MSERidgePredict[i] = MSE(y_test,ypredictRidge) - MSELassoPredict[i] = MSE(y_test,ypredictLasso) - print(lmb,RegRidge.coef_) - print(lmb,RegLasso.coef_) -# Now plot the results -plt.figure() -plt.plot(np.log10(lambdas), MSERidgePredict, 'b', label = 'MSE Ridge Test') -plt.plot(np.log10(lambdas), MSELassoPredict, 'r', label = 'MSE Lasso Test') -plt.xlabel('log10(lambda)') -plt.ylabel('MSE') -plt.legend() -plt.show() -
      -
      -
      -
      -
      -
      -
      -
      -
      -
      -
      -
      -
      -
      - -

      How can we understand this?

      +

      We have a model for \( p(\boldsymbol{D}\vert\boldsymbol{\beta}) \) but need one for the prior \( p(\boldsymbol{\beta} \)!

      @@ -412,7 +339,6 @@ plt.show()

    13647. 57
    13648. 58
    13649. 59
    13650. -
    13651. 60
    13652. »
    13653. diff --git a/doc/pub/week36/html/._week36-bs057.html b/doc/pub/week36/html/._week36-bs057.html index 57f956961..aa93d7987 100644 --- a/doc/pub/week36/html/._week36-bs057.html +++ b/doc/pub/week36/html/._week36-bs057.html @@ -37,7 +37,6 @@ doconce format html week36.do.txt --html_style=bootstrap --pygments_html_style=d
    13654. Plans for week 36
    13655. -
    13656. Thursday September 8
    13657. -
    13658. Summary from last Week and discussion of SVD, Ridge and Lasso regression with examples
    13659. -
    13660. Linear Regression and the SVD
    13661. -
    13662. What does it mean?
    13663. -
    13664. And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
    13665. -
    13666. Code for SVD and Inversion of Matrices
    13667. -
    13668. Inverse of Rectangular Matrix
    13669. -
    13670. Ridge and LASSO Regression
    13671. -
    13672. From OLS to Ridge and Lasso
    13673. -
    13674. Deriving the Ridge Regression Equations
    13675. -
    13676. Note on Scikit-Learn
    13677. -
    13678. Comparison with OLS
    13679. -
    13680. SVD analysis
    13681. -
    13682. Interpreting the Ridge results
    13683. -
    13684. More interpretations
    13685. -
    13686. Deriving the Lasso Regression Equations
    13687. -
    13688. Simple example to illustrate Ordinary Least Squares, Ridge and Lasso Regression
    13689. -
    13690. Ridge Regression
    13691. -
    13692. Lasso Regression
    13693. -
    13694. Yet another Example
    13695. -
    13696. The OLS case
    13697. -
    13698. The Ridge case
    13699. -
    13700. Writing the Cost Function
    13701. -
    13702. Lasso case
    13703. -
    13704. The first Case
    13705. -
    13706. Simple code for solving the above problem
    13707. -
    13708. With Lasso Regression
    13709. -
    13710. Another Example, now with a polynomial fit
    13711. -
    13712. To think about, first part
    13713. -
    13714. More thinking
    13715. -
    13716. Still thinking
    13717. -
    13718. What does centering (subtracting the mean values) mean mathematically?
    13719. -
    13720. Further Manipulations
    13721. -
    13722. Wrapping it up
    13723. -
    13724. Linear Regression code, Intercept handling first
    13725. -
    13726. Code Examples
    13727. -
    13728. Taking out the mean
    13729. -
    13730. Friday September 9
    13731. -
    13732. Linking the regression analysis with a statistical interpretation
    13733. -
    13734. Assumptions made
    13735. -
    13736. Expectation value and variance
    13737. -
    13738. Expectation value and variance for \( \boldsymbol{\beta} \)
    13739. -
    13740. Deriving OLS from a probability distribution
    13741. -
    13742. Independent and Identically Distrubuted (iid)
    13743. -
    13744. Maximum Likelihood Estimation (MLE)
    13745. -
    13746. A new Cost Function
    13747. -
    13748. More basic Statistics and Bayes' theorem
    13749. -
    13750. Marginal Probability
    13751. -
    13752. Conditional Probability
    13753. -
    13754. Bayes' Theorem
    13755. -
    13756. Interpretations of Bayes' Theorem
    13757. -
    13758. Example of Usage of Bayes' theorem
    13759. -
    13760. Doing it correctly
    13761. -
    13762. Bayes' Theorem and Ridge and Lasso Regression
    13763. -
    13764. Test Function for what happens with OLS, Ridge and Lasso
    13765. -
    13766. Invoking Bayes' theorem
    13767. -
    13768. Ridge and Bayes
    13769. -
    13770. Lasso and Bayes
    13771. -
    13772. Exercise 1: mean values and variances in linear regression
    13773. -
    13774. Exercise 2: Adding Ridge and Lasso Regression
    13775. +
    13776. Summary from last Week and discussion of SVD, Ridge and Lasso regression with examples
    13777. +
    13778. Linear Regression and the SVD
    13779. +
    13780. What does it mean?
    13781. +
    13782. And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
    13783. +
    13784. Code for SVD and Inversion of Matrices
    13785. +
    13786. Inverse of Rectangular Matrix
    13787. +
    13788. Ridge and LASSO Regression
    13789. +
    13790. From OLS to Ridge and Lasso
    13791. +
    13792. Deriving the Ridge Regression Equations
    13793. +
    13794. Note on Scikit-Learn
    13795. +
    13796. Comparison with OLS
    13797. +
    13798. SVD analysis
    13799. +
    13800. Interpreting the Ridge results
    13801. +
    13802. More interpretations
    13803. +
    13804. Deriving the Lasso Regression Equations
    13805. +
    13806. Simple example to illustrate Ordinary Least Squares, Ridge and Lasso Regression
    13807. +
    13808. Ridge Regression
    13809. +
    13810. Lasso Regression
    13811. +
    13812. Yet another Example
    13813. +
    13814. The OLS case
    13815. +
    13816. The Ridge case
    13817. +
    13818. Writing the Cost Function
    13819. +
    13820. Lasso case
    13821. +
    13822. The first Case
    13823. +
    13824. Simple code for solving the above problem
    13825. +
    13826. With Lasso Regression
    13827. +
    13828. Another Example, now with a polynomial fit
    13829. +
    13830. To think about, first part
    13831. +
    13832. More thinking
    13833. +
    13834. Still thinking
    13835. +
    13836. What does centering (subtracting the mean values) mean mathematically?
    13837. +
    13838. Further Manipulations
    13839. +
    13840. Wrapping it up
    13841. +
    13842. Linear Regression code, Intercept handling first
    13843. +
    13844. Code Examples
    13845. +
    13846. Taking out the mean
    13847. +
    13848. Friday September 9
    13849. +
    13850. Linking the regression analysis with a statistical interpretation
    13851. +
    13852. Assumptions made
    13853. +
    13854. Expectation value and variance
    13855. +
    13856. Expectation value and variance for \( \boldsymbol{\beta} \)
    13857. +
    13858. Deriving OLS from a probability distribution
    13859. +
    13860. Independent and Identically Distrubuted (iid)
    13861. +
    13862. Maximum Likelihood Estimation (MLE)
    13863. +
    13864. A new Cost Function
    13865. +
    13866. More basic Statistics and Bayes' theorem
    13867. +
    13868. Marginal Probability
    13869. +
    13870. Conditional Probability
    13871. +
    13872. Bayes' Theorem
    13873. +
    13874. Interpretations of Bayes' Theorem
    13875. +
    13876. Example of Usage of Bayes' theorem
    13877. +
    13878. Doing it correctly
    13879. +
    13880. Bayes' Theorem and Ridge and Lasso Regression
    13881. +
    13882. Test Function for what happens with OLS, Ridge and Lasso
    13883. +
    13884. Invoking Bayes' theorem
    13885. +
    13886. Ridge and Bayes
    13887. +
    13888. Lasso and Bayes
    13889. +
    13890. Exercise 1: mean values and variances in linear regression
    13891. +
    13892. Exercise 2: Adding Ridge and Lasso Regression
    13893. @@ -297,32 +295,41 @@ MathJax.Hub.Config({

       

       

       

      -

      Invoking Bayes' theorem

      +

      Ridge and Bayes

      -

      Using Bayes' theorem we can gain a better intuition about Ridge and Lasso regression.

      +

      With the posterior probability defined by a likelihood which we have +already modeled and an unknown prior, we are now ready to make +additional models for the prior. +

      -

      For ordinary least squares we postulated that the maximum likelihood for the doamin of events \( \boldsymbol{D} \) (one-dimensional case)

      -$$ -\boldsymbol{D}=[(x_0,y_0), (x_1,y_1),\dots, (x_{n-1},y_{n-1})], -$$ - -

      is given by

      -$$ -p(\boldsymbol{D}\vert\boldsymbol{\beta})=\prod_{i=0}^{n-1}\frac{1}{\sqrt{2\pi\sigma^2}}\exp{\left[-\frac{(y_i-\boldsymbol{X}_{i,*}\boldsymbol{\beta})^2}{2\sigma^2}\right]}. -$$ - -

      In Bayes' theorem this function plays the role of the so-called likelihood. We could now ask the question what is the posterior probability of a parameter set \( \boldsymbol{\beta} \) given a domain of events \( \boldsymbol{D} \)? That is, how can we define the posterior probability

      +

      We can, based on our discussions of the variance of \( \boldsymbol{\beta} \) and the mean value, assume that the prior for the values \( \boldsymbol{\beta} \) is given by a Gaussian with mean value zero and variance \( \tau^2 \), that is

      $$ -p(\boldsymbol{\beta}\vert\boldsymbol{D}). +p(\boldsymbol{\beta})=\prod_{j=0}^{p-1}\exp{\left(-\frac{\beta_j^2}{2\tau^2}\right)}. $$ -

      Bayes' theorem comes to our rescue here since (omitting the normalization constant)

      +

      Our posterior probability becomes then (omitting the normalization factor which is just a constant)

      $$ -p(\boldsymbol{\beta}\vert\boldsymbol{D})\propto p(\boldsymbol{D}\vert\boldsymbol{\beta})p(\boldsymbol{\beta}). +p(\boldsymbol{\beta\vert\boldsymbol{D})}=\prod_{i=0}^{n-1}\frac{1}{\sqrt{2\pi\sigma^2}}\exp{\left[-\frac{(y_i-\boldsymbol{X}_{i,*}\boldsymbol{\beta})^2}{2\sigma^2}\right]}\prod_{j=0}^{p-1}\exp{\left(-\frac{\beta_j^2}{2\tau^2}\right)}. $$ -

      We have a model for \( p(\boldsymbol{D}\vert\boldsymbol{\beta}) \) but need one for the prior \( p(\boldsymbol{\beta} \)!

      +

      We can now optimize this quantity with respect to \( \boldsymbol{\beta} \). As we +did for OLS, this is most conveniently done by taking the negative +logarithm of the posterior probability. Doing so and leaving out the +constants terms that do not depend on \( \beta \), we have +

      + +$$ +C(\boldsymbol{\beta})=\frac{\vert\vert (\boldsymbol{y}-\boldsymbol{X}\boldsymbol{\beta})\vert\vert_2^2}{2\sigma^2}+\frac{1}{2\tau^2}\vert\vert\boldsymbol{\beta}\vert\vert_2^2, +$$ + +

      and replacing \( 1/2\tau^2 \) with \( \lambda \) we have

      + +$$ +C(\boldsymbol{\beta})=\frac{\vert\vert (\boldsymbol{y}-\boldsymbol{X}\boldsymbol{\beta})\vert\vert_2^2}{2\sigma^2}+\lambda\vert\vert\boldsymbol{\beta}\vert\vert_2^2, +$$ + +

      which is our Ridge cost function! Nice, isn't it?

      @@ -340,7 +347,6 @@ $$

    13894. 57
    13895. 58
    13896. 59
    13897. -
    13898. 60
    13899. »
    13900. diff --git a/doc/pub/week36/html/._week36-bs058.html b/doc/pub/week36/html/._week36-bs058.html index 0e365e002..75d994099 100644 --- a/doc/pub/week36/html/._week36-bs058.html +++ b/doc/pub/week36/html/._week36-bs058.html @@ -37,7 +37,6 @@ doconce format html week36.do.txt --html_style=bootstrap --pygments_html_style=d
    13901. Plans for week 36
    13902. -
    13903. Thursday September 8
    13904. -
    13905. Summary from last Week and discussion of SVD, Ridge and Lasso regression with examples
    13906. -
    13907. Linear Regression and the SVD
    13908. -
    13909. What does it mean?
    13910. -
    13911. And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
    13912. -
    13913. Code for SVD and Inversion of Matrices
    13914. -
    13915. Inverse of Rectangular Matrix
    13916. -
    13917. Ridge and LASSO Regression
    13918. -
    13919. From OLS to Ridge and Lasso
    13920. -
    13921. Deriving the Ridge Regression Equations
    13922. -
    13923. Note on Scikit-Learn
    13924. -
    13925. Comparison with OLS
    13926. -
    13927. SVD analysis
    13928. -
    13929. Interpreting the Ridge results
    13930. -
    13931. More interpretations
    13932. -
    13933. Deriving the Lasso Regression Equations
    13934. -
    13935. Simple example to illustrate Ordinary Least Squares, Ridge and Lasso Regression
    13936. -
    13937. Ridge Regression
    13938. -
    13939. Lasso Regression
    13940. -
    13941. Yet another Example
    13942. -
    13943. The OLS case
    13944. -
    13945. The Ridge case
    13946. -
    13947. Writing the Cost Function
    13948. -
    13949. Lasso case
    13950. -
    13951. The first Case
    13952. -
    13953. Simple code for solving the above problem
    13954. -
    13955. With Lasso Regression
    13956. -
    13957. Another Example, now with a polynomial fit
    13958. -
    13959. To think about, first part
    13960. -
    13961. More thinking
    13962. -
    13963. Still thinking
    13964. -
    13965. What does centering (subtracting the mean values) mean mathematically?
    13966. -
    13967. Further Manipulations
    13968. -
    13969. Wrapping it up
    13970. -
    13971. Linear Regression code, Intercept handling first
    13972. -
    13973. Code Examples
    13974. -
    13975. Taking out the mean
    13976. -
    13977. Friday September 9
    13978. -
    13979. Linking the regression analysis with a statistical interpretation
    13980. -
    13981. Assumptions made
    13982. -
    13983. Expectation value and variance
    13984. -
    13985. Expectation value and variance for \( \boldsymbol{\beta} \)
    13986. -
    13987. Deriving OLS from a probability distribution
    13988. -
    13989. Independent and Identically Distrubuted (iid)
    13990. -
    13991. Maximum Likelihood Estimation (MLE)
    13992. -
    13993. A new Cost Function
    13994. -
    13995. More basic Statistics and Bayes' theorem
    13996. -
    13997. Marginal Probability
    13998. -
    13999. Conditional Probability
    14000. -
    14001. Bayes' Theorem
    14002. -
    14003. Interpretations of Bayes' Theorem
    14004. -
    14005. Example of Usage of Bayes' theorem
    14006. -
    14007. Doing it correctly
    14008. -
    14009. Bayes' Theorem and Ridge and Lasso Regression
    14010. -
    14011. Test Function for what happens with OLS, Ridge and Lasso
    14012. -
    14013. Invoking Bayes' theorem
    14014. -
    14015. Ridge and Bayes
    14016. -
    14017. Lasso and Bayes
    14018. -
    14019. Exercise 1: mean values and variances in linear regression
    14020. -
    14021. Exercise 2: Adding Ridge and Lasso Regression
    14022. +
    14023. Summary from last Week and discussion of SVD, Ridge and Lasso regression with examples
    14024. +
    14025. Linear Regression and the SVD
    14026. +
    14027. What does it mean?
    14028. +
    14029. And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
    14030. +
    14031. Code for SVD and Inversion of Matrices
    14032. +
    14033. Inverse of Rectangular Matrix
    14034. +
    14035. Ridge and LASSO Regression
    14036. +
    14037. From OLS to Ridge and Lasso
    14038. +
    14039. Deriving the Ridge Regression Equations
    14040. +
    14041. Note on Scikit-Learn
    14042. +
    14043. Comparison with OLS
    14044. +
    14045. SVD analysis
    14046. +
    14047. Interpreting the Ridge results
    14048. +
    14049. More interpretations
    14050. +
    14051. Deriving the Lasso Regression Equations
    14052. +
    14053. Simple example to illustrate Ordinary Least Squares, Ridge and Lasso Regression
    14054. +
    14055. Ridge Regression
    14056. +
    14057. Lasso Regression
    14058. +
    14059. Yet another Example
    14060. +
    14061. The OLS case
    14062. +
    14063. The Ridge case
    14064. +
    14065. Writing the Cost Function
    14066. +
    14067. Lasso case
    14068. +
    14069. The first Case
    14070. +
    14071. Simple code for solving the above problem
    14072. +
    14073. With Lasso Regression
    14074. +
    14075. Another Example, now with a polynomial fit
    14076. +
    14077. To think about, first part
    14078. +
    14079. More thinking
    14080. +
    14081. Still thinking
    14082. +
    14083. What does centering (subtracting the mean values) mean mathematically?
    14084. +
    14085. Further Manipulations
    14086. +
    14087. Wrapping it up
    14088. +
    14089. Linear Regression code, Intercept handling first
    14090. +
    14091. Code Examples
    14092. +
    14093. Taking out the mean
    14094. +
    14095. Friday September 9
    14096. +
    14097. Linking the regression analysis with a statistical interpretation
    14098. +
    14099. Assumptions made
    14100. +
    14101. Expectation value and variance
    14102. +
    14103. Expectation value and variance for \( \boldsymbol{\beta} \)
    14104. +
    14105. Deriving OLS from a probability distribution
    14106. +
    14107. Independent and Identically Distrubuted (iid)
    14108. +
    14109. Maximum Likelihood Estimation (MLE)
    14110. +
    14111. A new Cost Function
    14112. +
    14113. More basic Statistics and Bayes' theorem
    14114. +
    14115. Marginal Probability
    14116. +
    14117. Conditional Probability
    14118. +
    14119. Bayes' Theorem
    14120. +
    14121. Interpretations of Bayes' Theorem
    14122. +
    14123. Example of Usage of Bayes' theorem
    14124. +
    14125. Doing it correctly
    14126. +
    14127. Bayes' Theorem and Ridge and Lasso Regression
    14128. +
    14129. Test Function for what happens with OLS, Ridge and Lasso
    14130. +
    14131. Invoking Bayes' theorem
    14132. +
    14133. Ridge and Bayes
    14134. +
    14135. Lasso and Bayes
    14136. +
    14137. Exercise 1: mean values and variances in linear regression
    14138. +
    14139. Exercise 2: Adding Ridge and Lasso Regression
    14140. @@ -297,42 +295,172 @@ MathJax.Hub.Config({

       

       

       

      -

      Ridge and Bayes

      +

      Lasso and Bayes

      -

      With the posterior probability defined by a likelihood which we have -already modeled and an unknown prior, we are now ready to make -additional models for the prior. -

      - -

      We can, based on our discussions of the variance of \( \boldsymbol{\beta} \) and the mean value, assume that the prior for the values \( \boldsymbol{\beta} \) is given by a Gaussian with mean value zero and variance \( \tau^2 \), that is

      +

      To derive the Lasso cost function, we simply replace the Gaussian prior with an exponential distribution (Laplace in this case) with zero mean value, that is

      $$ -p(\boldsymbol{\beta})=\prod_{j=0}^{p-1}\exp{\left(-\frac{\beta_j^2}{2\tau^2}\right)}. +p(\boldsymbol{\beta})=\prod_{j=0}^{p-1}\exp{\left(-\frac{\vert\beta_j\vert}{\tau}\right)}. $$

      Our posterior probability becomes then (omitting the normalization factor which is just a constant)

      $$ -p(\boldsymbol{\beta\vert\boldsymbol{D})}=\prod_{i=0}^{n-1}\frac{1}{\sqrt{2\pi\sigma^2}}\exp{\left[-\frac{(y_i-\boldsymbol{X}_{i,*}\boldsymbol{\beta})^2}{2\sigma^2}\right]}\prod_{j=0}^{p-1}\exp{\left(-\frac{\beta_j^2}{2\tau^2}\right)}. +p(\boldsymbol{\beta}\vert\boldsymbol{D})=\prod_{i=0}^{n-1}\frac{1}{\sqrt{2\pi\sigma^2}}\exp{\left[-\frac{(y_i-\boldsymbol{X}_{i,*}\boldsymbol{\beta})^2}{2\sigma^2}\right]}\prod_{j=0}^{p-1}\exp{\left(-\frac{\vert\beta_j\vert}{\tau}\right)}. $$ -

      We can now optimize this quantity with respect to \( \boldsymbol{\beta} \). As we -did for OLS, this is most conveniently done by taking the negative -logarithm of the posterior probability. Doing so and leaving out the +

      Taking the negative +logarithm of the posterior probability and leaving out the constants terms that do not depend on \( \beta \), we have

      $$ -C(\boldsymbol{\beta})=\frac{\vert\vert (\boldsymbol{y}-\boldsymbol{X}\boldsymbol{\beta})\vert\vert_2^2}{2\sigma^2}+\frac{1}{2\tau^2}\vert\vert\boldsymbol{\beta}\vert\vert_2^2, +C(\boldsymbol{\beta}=\frac{\vert\vert (\boldsymbol{y}-\boldsymbol{X}\boldsymbol{\beta})\vert\vert_2^2}{2\sigma^2}+\frac{1}{\tau}\vert\vert\boldsymbol{\beta}\vert\vert_1, $$ -

      and replacing \( 1/2\tau^2 \) with \( \lambda \) we have

      +

      and replacing \( 1/\tau \) with \( \lambda \) we have

      $$ -C(\boldsymbol{\beta})=\frac{\vert\vert (\boldsymbol{y}-\boldsymbol{X}\boldsymbol{\beta})\vert\vert_2^2}{2\sigma^2}+\lambda\vert\vert\boldsymbol{\beta}\vert\vert_2^2, +C(\boldsymbol{\beta}=\frac{\vert\vert (\boldsymbol{y}-\boldsymbol{X}\boldsymbol{\beta})\vert\vert_2^2}{2\sigma^2}+\lambda\vert\vert\boldsymbol{\beta}\vert\vert_1, $$ -

      which is our Ridge cost function! Nice, isn't it?

      +

      which is our Lasso cost function!

      + +

      Exercise 1: mean values and variances in linear regression

      + +

      This exercise deals with various mean values ad variances in linear regression method (here it may be useful to look up chapter 3, equation (3.8) of Trevor Hastie, Robert Tibshirani, Jerome H. Friedman, The Elements of Statistical Learning, Springer).

      + +

      The assumption we have made is +that there exists a function \( f(\boldsymbol{x}) \) and a normal distributed error \( \boldsymbol{\varepsilon}\sim \mathcal{N}(0, \sigma^2) \) +which describes our data +

      +$$ +\boldsymbol{y} = f(\boldsymbol{x})+\boldsymbol{\varepsilon} +$$ + +

      We then approximate this function with our model from the solution of the linear regression equations (ordinary least squares OLS), that is our +function \( f \) is approximated by \( \boldsymbol{\tilde{y}} \) where we minimized \( (\boldsymbol{y}-\boldsymbol{\tilde{y}})^2 \), with +

      +$$ +\boldsymbol{\tilde{y}} = \boldsymbol{X}\boldsymbol{\beta}. +$$ + +

      The matrix \( \boldsymbol{X} \) is the so-called design matrix.

      + + +

      +a) +Show that the expectation value of \( \boldsymbol{y} \) for a given element \( i \) +

      +$$ +\begin{align*} +\mathbb{E}(y_i) & =\mathbf{X}_{i, \ast} \, \beta, +\end{align*} +$$ + +

      and that +its variance is +

      +$$ +\begin{align*} \mbox{Var}(y_i) & = \sigma^2. +\end{align*} +$$ + +

      Hence, \( y_i \sim \mathcal{N}( \mathbf{X}_{i, \ast} \, \boldsymbol{\beta}, \sigma^2) \), that is \( \boldsymbol{y} \) follows a normal distribution with +mean value \( \boldsymbol{X}\boldsymbol{\beta} \) and variance \( \sigma^2 \). +

      + + + + +

      +b) +With the OLS expressions for the parameters \( \boldsymbol{\beta} \) show that +

      +$$ +\mathbb{E}(\boldsymbol{\beta}) = \boldsymbol{\beta}. +$$ + + + + + +

      +c) +Show finally that the variance of \( \boldsymbol{\beta} \) is +

      +$$ +\begin{eqnarray*} +\mbox{Var}(\boldsymbol{\beta}) & = & \sigma^2 \, (\mathbf{X}^{T} \mathbf{X})^{-1}. +\end{eqnarray*} +$$ + + + + + + + +

      Exercise 2: Adding Ridge and Lasso Regression

      + +

      This exercise is a continuation of the exercises from week 35.

      + +

      We will +use the same function to generate our data set, still staying with a +simple function \( y(x) \) which we want to fit using linear regression, +but now extending the analysis to include the Ridge and the Lasso +regression methods. +

      + +

      We will thus again generate our own dataset for a function \( y(x) \) where +\( x \in [0,1] \) and defined by random numbers computed with the uniform +distribution. The function \( y \) is a quadratic polynomial in \( x \) with +added stochastic noise according to the normal distribution \( \cal{N}(0,1) \). +

      + +

      The following simple Python instructions define our \( x \) and \( y \) values (with 100 data points).

      + + +
      +
      +
      +
      +
      +
      x = np.random.rand(100)
      +y = 2.0+5*x*x+0.1*np.random.randn(100)
      +
      +
      +
      +
      +
      +
      +
      +
      +
      +
      +
      +
      +
      +
      + + + +

      +a) +Write your own code for the Ridge method (see chapter 3.4 of Hastie et al., equations (3.43) and (3.44)) and compute the parametrization for different values of \( \lambda \). Study the dependence on \( \lambda \) while also varying the strength of the noise in your expression for \( y(x) \). +

      + + + + +

      +b) +Our next step is to study the variance of the parameters \( \beta_1 \) and \( \beta_2 \) (assuming that we are parameterizing our function with a second-order polynomial). We will use standard linear regression and the Ridge regression. You can now opt for either writing your own function or using Scikit-Learn to find the parameters \( \beta \). From your results calculate the variance of these parameters (recall that this is equal to the diagonal elements of the matrix \( (\hat{X}^T\hat{X})+\lambda\hat{I})^{-1} \)). Discuss the results of these variances as functions of \( \lambda \). In particular, try to link your discussion with the discussion in Hastie et al. and their figures 3.10 and 3.11. Scikit-Learn may not provide the variance of the parameters \( \beta \). This needs to be checked. With your own code you can however do so. +

      + + + +

      diff --git a/doc/pub/week36/html/week36-bs.html b/doc/pub/week36/html/week36-bs.html index c79950484..12be7d203 100644 --- a/doc/pub/week36/html/week36-bs.html +++ b/doc/pub/week36/html/week36-bs.html @@ -37,7 +37,6 @@ doconce format html week36.do.txt --html_style=bootstrap --pygments_html_style=d
    14141. Plans for week 36
    14142. -
    14143. Thursday September 8
    14144. -
    14145. Summary from last Week and discussion of SVD, Ridge and Lasso regression with examples
    14146. -
    14147. Linear Regression and the SVD
    14148. -
    14149. What does it mean?
    14150. -
    14151. And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
    14152. -
    14153. Code for SVD and Inversion of Matrices
    14154. -
    14155. Inverse of Rectangular Matrix
    14156. -
    14157. Ridge and LASSO Regression
    14158. -
    14159. From OLS to Ridge and Lasso
    14160. -
    14161. Deriving the Ridge Regression Equations
    14162. -
    14163. Note on Scikit-Learn
    14164. -
    14165. Comparison with OLS
    14166. -
    14167. SVD analysis
    14168. -
    14169. Interpreting the Ridge results
    14170. -
    14171. More interpretations
    14172. -
    14173. Deriving the Lasso Regression Equations
    14174. -
    14175. Simple example to illustrate Ordinary Least Squares, Ridge and Lasso Regression
    14176. -
    14177. Ridge Regression
    14178. -
    14179. Lasso Regression
    14180. -
    14181. Yet another Example
    14182. -
    14183. The OLS case
    14184. -
    14185. The Ridge case
    14186. -
    14187. Writing the Cost Function
    14188. -
    14189. Lasso case
    14190. -
    14191. The first Case
    14192. -
    14193. Simple code for solving the above problem
    14194. -
    14195. With Lasso Regression
    14196. -
    14197. Another Example, now with a polynomial fit
    14198. -
    14199. To think about, first part
    14200. -
    14201. More thinking
    14202. -
    14203. Still thinking
    14204. -
    14205. What does centering (subtracting the mean values) mean mathematically?
    14206. -
    14207. Further Manipulations
    14208. -
    14209. Wrapping it up
    14210. -
    14211. Linear Regression code, Intercept handling first
    14212. -
    14213. Code Examples
    14214. -
    14215. Taking out the mean
    14216. -
    14217. Friday September 9
    14218. -
    14219. Linking the regression analysis with a statistical interpretation
    14220. -
    14221. Assumptions made
    14222. -
    14223. Expectation value and variance
    14224. -
    14225. Expectation value and variance for \( \boldsymbol{\beta} \)
    14226. -
    14227. Deriving OLS from a probability distribution
    14228. -
    14229. Independent and Identically Distrubuted (iid)
    14230. -
    14231. Maximum Likelihood Estimation (MLE)
    14232. -
    14233. A new Cost Function
    14234. -
    14235. More basic Statistics and Bayes' theorem
    14236. -
    14237. Marginal Probability
    14238. -
    14239. Conditional Probability
    14240. -
    14241. Bayes' Theorem
    14242. -
    14243. Interpretations of Bayes' Theorem
    14244. -
    14245. Example of Usage of Bayes' theorem
    14246. -
    14247. Doing it correctly
    14248. -
    14249. Bayes' Theorem and Ridge and Lasso Regression
    14250. -
    14251. Test Function for what happens with OLS, Ridge and Lasso
    14252. -
    14253. Invoking Bayes' theorem
    14254. -
    14255. Ridge and Bayes
    14256. -
    14257. Lasso and Bayes
    14258. -
    14259. Exercise 1: mean values and variances in linear regression
    14260. -
    14261. Exercise 2: Adding Ridge and Lasso Regression
    14262. +
    14263. Summary from last Week and discussion of SVD, Ridge and Lasso regression with examples
    14264. +
    14265. Linear Regression and the SVD
    14266. +
    14267. What does it mean?
    14268. +
    14269. And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
    14270. +
    14271. Code for SVD and Inversion of Matrices
    14272. +
    14273. Inverse of Rectangular Matrix
    14274. +
    14275. Ridge and LASSO Regression
    14276. +
    14277. From OLS to Ridge and Lasso
    14278. +
    14279. Deriving the Ridge Regression Equations
    14280. +
    14281. Note on Scikit-Learn
    14282. +
    14283. Comparison with OLS
    14284. +
    14285. SVD analysis
    14286. +
    14287. Interpreting the Ridge results
    14288. +
    14289. More interpretations
    14290. +
    14291. Deriving the Lasso Regression Equations
    14292. +
    14293. Simple example to illustrate Ordinary Least Squares, Ridge and Lasso Regression
    14294. +
    14295. Ridge Regression
    14296. +
    14297. Lasso Regression
    14298. +
    14299. Yet another Example
    14300. +
    14301. The OLS case
    14302. +
    14303. The Ridge case
    14304. +
    14305. Writing the Cost Function
    14306. +
    14307. Lasso case
    14308. +
    14309. The first Case
    14310. +
    14311. Simple code for solving the above problem
    14312. +
    14313. With Lasso Regression
    14314. +
    14315. Another Example, now with a polynomial fit
    14316. +
    14317. To think about, first part
    14318. +
    14319. More thinking
    14320. +
    14321. Still thinking
    14322. +
    14323. What does centering (subtracting the mean values) mean mathematically?
    14324. +
    14325. Further Manipulations
    14326. +
    14327. Wrapping it up
    14328. +
    14329. Linear Regression code, Intercept handling first
    14330. +
    14331. Code Examples
    14332. +
    14333. Taking out the mean
    14334. +
    14335. Friday September 9
    14336. +
    14337. Linking the regression analysis with a statistical interpretation
    14338. +
    14339. Assumptions made
    14340. +
    14341. Expectation value and variance
    14342. +
    14343. Expectation value and variance for \( \boldsymbol{\beta} \)
    14344. +
    14345. Deriving OLS from a probability distribution
    14346. +
    14347. Independent and Identically Distrubuted (iid)
    14348. +
    14349. Maximum Likelihood Estimation (MLE)
    14350. +
    14351. A new Cost Function
    14352. +
    14353. More basic Statistics and Bayes' theorem
    14354. +
    14355. Marginal Probability
    14356. +
    14357. Conditional Probability
    14358. +
    14359. Bayes' Theorem
    14360. +
    14361. Interpretations of Bayes' Theorem
    14362. +
    14363. Example of Usage of Bayes' theorem
    14364. +
    14365. Doing it correctly
    14366. +
    14367. Bayes' Theorem and Ridge and Lasso Regression
    14368. +
    14369. Test Function for what happens with OLS, Ridge and Lasso
    14370. +
    14371. Invoking Bayes' theorem
    14372. +
    14373. Ridge and Bayes
    14374. +
    14375. Lasso and Bayes
    14376. +
    14377. Exercise 1: mean values and variances in linear regression
    14378. +
    14379. Exercise 2: Adding Ridge and Lasso Regression
    14380. @@ -315,7 +313,7 @@ MathJax.Hub.Config({
      -

      Sep 9, 2022

      +

      May 29, 2023


      @@ -340,7 +338,7 @@ MathJax.Hub.Config({
    14381. 9
    14382. 10
    14383. ...
    14384. -
    14385. 60
    14386. +
    14387. 59
    14388. »
    14389. @@ -354,7 +352,7 @@ MathJax.Hub.Config({ -->
      - © 1999-2022, Morten Hjorth-Jensen. Released under CC Attribution-NonCommercial 4.0 license + © 1999-2023, Morten Hjorth-Jensen. Released under CC Attribution-NonCommercial 4.0 license
      diff --git a/doc/pub/week36/html/week36-reveal.html b/doc/pub/week36/html/week36-reveal.html index b03749e38..79ad92daf 100644 --- a/doc/pub/week36/html/week36-reveal.html +++ b/doc/pub/week36/html/week36-reveal.html @@ -184,13 +184,13 @@ MathJax.Hub.Config({
      -

      Sep 9, 2022

      +

      May 29, 2023


      - © 1999-2022, Morten Hjorth-Jensen. Released under CC Attribution-NonCommercial 4.0 license + © 1999-2023, Morten Hjorth-Jensen. Released under CC Attribution-NonCommercial 4.0 license
      @@ -198,18 +198,8 @@ MathJax.Hub.Config({

      Plans for week 36

      Recommended Reading:

      @@ -220,10 +210,6 @@ MathJax.Hub.Config({
    -
    -

    Thursday September 8

    -
    -

    Summary from last Week and discussion of SVD, Ridge and Lasso regression with examples

    diff --git a/doc/pub/week36/html/week36-solarized.html b/doc/pub/week36/html/week36-solarized.html index d7c58a7ed..ac9235d19 100644 --- a/doc/pub/week36/html/week36-solarized.html +++ b/doc/pub/week36/html/week36-solarized.html @@ -64,7 +64,6 @@ div.toc p,a {
    @@ -263,14 +262,8 @@ MathJax.Hub.Config({

    Plans for week 36

    Recommended Reading:

      @@ -278,9 +271,6 @@ MathJax.Hub.Config({
    1. Bishop 1.1, 1.2, 2.1, 2.2, 2.3 and 3.1
    2. Hastie et al chapter 3
    -









    -

    Thursday September 8

    -









    Summary from last Week and discussion of SVD, Ridge and Lasso regression with examples

    @@ -2533,7 +2523,7 @@ Our next step is to study the variance of the parameters \( \beta_1 \) and \( \b
    - © 1999-2022, Morten Hjorth-Jensen. Released under CC Attribution-NonCommercial 4.0 license + © 1999-2023, Morten Hjorth-Jensen. Released under CC Attribution-NonCommercial 4.0 license
    diff --git a/doc/pub/week36/html/week36.html b/doc/pub/week36/html/week36.html index 65f853f33..dd2e0cc28 100644 --- a/doc/pub/week36/html/week36.html +++ b/doc/pub/week36/html/week36.html @@ -141,7 +141,6 @@ div.toc p,a {
    @@ -340,14 +339,8 @@ MathJax.Hub.Config({

    Plans for week 36

    Recommended Reading:

      @@ -355,9 +348,6 @@ MathJax.Hub.Config({
    1. Bishop 1.1, 1.2, 2.1, 2.2, 2.3 and 3.1
    2. Hastie et al chapter 3
    -









    -

    Thursday September 8

    -









    Summary from last Week and discussion of SVD, Ridge and Lasso regression with examples

    @@ -2610,7 +2600,7 @@ Our next step is to study the variance of the parameters \( \beta_1 \) and \( \b
    - © 1999-2022, Morten Hjorth-Jensen. Released under CC Attribution-NonCommercial 4.0 license + © 1999-2023, Morten Hjorth-Jensen. Released under CC Attribution-NonCommercial 4.0 license
    diff --git a/doc/pub/week36/ipynb/ipynb-week36-src.tar.gz b/doc/pub/week36/ipynb/ipynb-week36-src.tar.gz index 9dd28a12a..c634bf9f0 100644 Binary files a/doc/pub/week36/ipynb/ipynb-week36-src.tar.gz and b/doc/pub/week36/ipynb/ipynb-week36-src.tar.gz differ diff --git a/doc/pub/week36/ipynb/week36.ipynb b/doc/pub/week36/ipynb/week36.ipynb index ed649678e..6be1b27d0 100644 --- a/doc/pub/week36/ipynb/week36.ipynb +++ b/doc/pub/week36/ipynb/week36.ipynb @@ -2,8 +2,10 @@ "cells": [ { "cell_type": "markdown", - "id": "54f68878", - "metadata": {}, + "id": "de098695", + "metadata": { + "editable": true + }, "source": [ "\n", @@ -12,31 +14,31 @@ }, { "cell_type": "markdown", - "id": "b2606fb6", - "metadata": {}, + "id": "9d896464", + "metadata": { + "editable": true + }, "source": [ "# Week 36: Statistical interpretation of Linear Regression and Resampling techniques\n", "**Morten Hjorth-Jensen**, Department of Physics, University of Oslo and Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University\n", "\n", - "Date: **Sep 9, 2022**\n", + "Date: **May 29, 2023**\n", "\n", - "Copyright 1999-2022, Morten Hjorth-Jensen. Released under CC Attribution-NonCommercial 4.0 license" + "Copyright 1999-2023, Morten Hjorth-Jensen. Released under CC Attribution-NonCommercial 4.0 license" ] }, { "cell_type": "markdown", - "id": "998031f2", - "metadata": {}, + "id": "2d144eda", + "metadata": { + "editable": true + }, "source": [ "## Plans for week 36\n", "\n", - "* Thursday: Summary from last week on discussion of SVD, Ridge and Lasso linear regression.\n", + "* Summary from last week on discussion of SVD, Ridge and Lasso linear regression.\n", "\n", - " * [Video of Lecture](https://youtu.be/qn_BAVhMD8U)\n", - "\n", - "* Friday: Linear Regression and links with Statistics, Resampling methods and presentation of first project\n", - "\n", - " * [Video of Lecture](https://youtu.be/_CPGg0JYH8M)\n", + "* Linear Regression and links with Statistics, Resampling methods and presentation of first project\n", "\n", "Recommended Reading:\n", "1. Lectures on Regression\n", @@ -48,24 +50,20 @@ }, { "cell_type": "markdown", - "id": "6cd06197", - "metadata": {}, - "source": [ - "## Thursday September 8" - ] - }, - { - "cell_type": "markdown", - "id": "d54cdcb7", - "metadata": {}, + "id": "de778f2b", + "metadata": { + "editable": true + }, "source": [ "## Summary from last Week and discussion of SVD, Ridge and Lasso regression with examples" ] }, { "cell_type": "markdown", - "id": "e00e68d6", - "metadata": {}, + "id": "ca2b6500", + "metadata": { + "editable": true + }, "source": [ "## Linear Regression and the SVD\n", "\n", @@ -74,8 +72,10 @@ }, { "cell_type": "markdown", - "id": "34b87b68", - "metadata": {}, + "id": "a42c6c2d", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\boldsymbol{X}^T\\boldsymbol{X}=\\boldsymbol{V}\\boldsymbol{\\Sigma}^T\\boldsymbol{U}^T\\boldsymbol{U}\\boldsymbol{\\Sigma}\\boldsymbol{V}^T=\\boldsymbol{V}\\boldsymbol{\\Sigma}^T\\boldsymbol{\\Sigma}\\boldsymbol{V}^T.\n", @@ -84,16 +84,20 @@ }, { "cell_type": "markdown", - "id": "ce084898", - "metadata": {}, + "id": "b42bcaa4", + "metadata": { + "editable": true + }, "source": [ "Since the matrices here have dimension $p\\times p$, with $p$ corresponding to the singular values, we defined last week the matrix" ] }, { "cell_type": "markdown", - "id": "2976c1d5", - "metadata": {}, + "id": "e208741e", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\boldsymbol{\\Sigma}^T\\boldsymbol{\\Sigma} = \\begin{bmatrix} \\tilde{\\boldsymbol{\\Sigma}} & \\boldsymbol{0}\\\\ \\end{bmatrix}\\begin{bmatrix} \\tilde{\\boldsymbol{\\Sigma}} \\\\ \\boldsymbol{0}\\end{bmatrix},\n", @@ -102,16 +106,20 @@ }, { "cell_type": "markdown", - "id": "6a83cdb5", - "metadata": {}, + "id": "1ca945f1", + "metadata": { + "editable": true + }, "source": [ "where the tilde-matrix $\\tilde{\\boldsymbol{\\Sigma}}$ is a matrix of dimension $p\\times p$ containing only the singular values $\\sigma_i$, that is" ] }, { "cell_type": "markdown", - "id": "a7a4fa16", - "metadata": {}, + "id": "0df614ad", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\tilde{\\boldsymbol{\\Sigma}}=\\begin{bmatrix} \\sigma_0 & 0 & 0 & \\dots & 0 & 0 \\\\\n", @@ -125,16 +133,20 @@ }, { "cell_type": "markdown", - "id": "03e6fafb", - "metadata": {}, + "id": "07ba6f53", + "metadata": { + "editable": true + }, "source": [ "meaning we can write" ] }, { "cell_type": "markdown", - "id": "7b638f4f", - "metadata": {}, + "id": "05d566fa", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\boldsymbol{X}^T\\boldsymbol{X}=\\boldsymbol{V}\\tilde{\\boldsymbol{\\Sigma}}^2\\boldsymbol{V}^T.\n", @@ -143,16 +155,20 @@ }, { "cell_type": "markdown", - "id": "ec26d490", - "metadata": {}, + "id": "bef58181", + "metadata": { + "editable": true + }, "source": [ "Multiplying from the right with $\\boldsymbol{V}$ (using the orthogonality of $\\boldsymbol{V}$) we get" ] }, { "cell_type": "markdown", - "id": "89a41f1b", - "metadata": {}, + "id": "88faff60", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\left(\\boldsymbol{X}^T\\boldsymbol{X}\\right)\\boldsymbol{V}=\\boldsymbol{V}\\tilde{\\boldsymbol{\\Sigma}}^2.\n", @@ -161,8 +177,10 @@ }, { "cell_type": "markdown", - "id": "1a1ec91f", - "metadata": {}, + "id": "78db1996", + "metadata": { + "editable": true + }, "source": [ "## What does it mean?\n", "\n", @@ -173,8 +191,10 @@ }, { "cell_type": "markdown", - "id": "9be07751", - "metadata": {}, + "id": "bacb305d", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\left(\\boldsymbol{X}^T\\boldsymbol{X}\\right)\\boldsymbol{v}_i=\\boldsymbol{v}_i\\sigma_i^2.\n", @@ -183,8 +203,10 @@ }, { "cell_type": "markdown", - "id": "71861426", - "metadata": {}, + "id": "f72c3acc", + "metadata": { + "editable": true + }, "source": [ "In other words, each non-zero singular value of $\\boldsymbol{X}$ is a positive\n", "square root of an eigenvalue of $\\boldsymbol{X}^T\\boldsymbol{X}$. It means also that\n", @@ -203,8 +225,10 @@ }, { "cell_type": "markdown", - "id": "8696c3c0", - "metadata": {}, + "id": "70d77e0a", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\boldsymbol{C}[\\boldsymbol{X}]=\\frac{1}{n}\\boldsymbol{X}^T\\boldsymbol{X},\n", @@ -213,8 +237,10 @@ }, { "cell_type": "markdown", - "id": "9cf87ecc", - "metadata": {}, + "id": "4e09c60a", + "metadata": { + "editable": true + }, "source": [ "meaning that every squared non-singular value of $\\boldsymbol{X}$ divided by $n$ (\n", "the number of samples) are the eigenvalues of the covariance\n", @@ -226,8 +252,10 @@ }, { "cell_type": "markdown", - "id": "20032abb", - "metadata": {}, + "id": "454ed107", + "metadata": { + "editable": true + }, "source": [ "## And finally $\\boldsymbol{X}\\boldsymbol{X}^T$\n", "\n", @@ -236,8 +264,10 @@ }, { "cell_type": "markdown", - "id": "fbcc9b3f", - "metadata": {}, + "id": "e680ab8d", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\boldsymbol{X}\\boldsymbol{X}^T=\\boldsymbol{U}\\boldsymbol{\\Sigma}\\boldsymbol{V}^T\\boldsymbol{V}\\boldsymbol{\\Sigma}^T\\boldsymbol{U}^T=\\boldsymbol{U}\\boldsymbol{\\Sigma}^T\\boldsymbol{\\Sigma}\\boldsymbol{U}^T.\n", @@ -246,16 +276,20 @@ }, { "cell_type": "markdown", - "id": "1e18e19f", - "metadata": {}, + "id": "55e8064a", + "metadata": { + "editable": true + }, "source": [ "Since the matrices here have dimension $n\\times n$, we have" ] }, { "cell_type": "markdown", - "id": "747b9d45", - "metadata": {}, + "id": "54323d93", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\boldsymbol{\\Sigma}\\boldsymbol{\\Sigma}^T = \\begin{bmatrix} \\tilde{\\boldsymbol{\\Sigma}} \\\\ \\boldsymbol{0}\\\\ \\end{bmatrix}\\begin{bmatrix} \\tilde{\\boldsymbol{\\Sigma}} \\boldsymbol{0}\\\\ \\end{bmatrix}=\\begin{bmatrix} \\tilde{\\boldsymbol{\\Sigma}} & \\boldsymbol{0} \\\\ \\boldsymbol{0} & \\boldsymbol{0}\\\\ \\end{bmatrix},\n", @@ -264,16 +298,20 @@ }, { "cell_type": "markdown", - "id": "134925f1", - "metadata": {}, + "id": "11308aeb", + "metadata": { + "editable": true + }, "source": [ "leading to" ] }, { "cell_type": "markdown", - "id": "0050225b", - "metadata": {}, + "id": "e6329c2b", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\boldsymbol{X}\\boldsymbol{X}^T=\\boldsymbol{U}\\begin{bmatrix} \\tilde{\\boldsymbol{\\Sigma}} & \\boldsymbol{0} \\\\ \\boldsymbol{0} & \\boldsymbol{0}\\\\ \\end{bmatrix}\\boldsymbol{U}^T.\n", @@ -282,16 +320,20 @@ }, { "cell_type": "markdown", - "id": "8ec325de", - "metadata": {}, + "id": "850c985b", + "metadata": { + "editable": true + }, "source": [ "Multiplying with $\\boldsymbol{U}$ from the right gives us the eigenvalue problem" ] }, { "cell_type": "markdown", - "id": "5059b729", - "metadata": {}, + "id": "967c3ea7", + "metadata": { + "editable": true + }, "source": [ "$$\n", "(\\boldsymbol{X}\\boldsymbol{X}^T)\\boldsymbol{U}=\\boldsymbol{U}\\begin{bmatrix} \\tilde{\\boldsymbol{\\Sigma}} & \\boldsymbol{0} \\\\ \\boldsymbol{0} & \\boldsymbol{0}\\\\ \\end{bmatrix}.\n", @@ -300,8 +342,10 @@ }, { "cell_type": "markdown", - "id": "1815abe6", - "metadata": {}, + "id": "482029c6", + "metadata": { + "editable": true + }, "source": [ "It means that the eigenvalues of $\\boldsymbol{X}\\boldsymbol{X}^T$ are again given by\n", "the non-zero singular values plus now a series of zeros. The column\n", @@ -315,8 +359,10 @@ }, { "cell_type": "markdown", - "id": "ade9b7ed", - "metadata": {}, + "id": "dc0004a9", + "metadata": { + "editable": true + }, "source": [ "## Code for SVD and Inversion of Matrices\n", "\n", @@ -327,8 +373,11 @@ { "cell_type": "code", "execution_count": 1, - "id": "24749dad", - "metadata": {}, + "id": "534ac439", + "metadata": { + "collapsed": false, + "editable": true + }, "outputs": [], "source": [ "Ainv = np.linlag.pinv(A)" @@ -336,40 +385,23 @@ }, { "cell_type": "markdown", - "id": "770bef7d", - "metadata": {}, + "id": "03919b93", + "metadata": { + "editable": true + }, "source": [ "Let us first look at a matrix which does not causes problems and write our own function where we just use the SVD." ] }, { "cell_type": "code", - "execution_count": 4, - "id": "23a63ba1", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[[ 1. -1. 2.]\n", - " [ 1. 0. 1.]\n", - " [ 1. 2. -1.]\n", - " [ 1. 1. 0.]]\n", - "test U\n", - "[[-3.33066907e-16 -1.11022302e-16 3.33066907e-16]\n", - " [-1.11022302e-16 4.44089210e-16 -2.49800181e-16]\n", - " [ 3.33066907e-16 -2.49800181e-16 0.00000000e+00]]\n", - "test VT\n", - "[[ 2.22044605e-16 5.55111512e-17 -2.22044605e-16]\n", - " [ 5.55111512e-17 -2.22044605e-16 5.55111512e-17]\n", - " [-2.22044605e-16 5.55111512e-17 2.22044605e-16]]\n", - "[[1.82969604e+30 1.82969604e+30 1.82969604e+30]\n", - " [1.82969604e+30 1.82969604e+30 1.82969604e+30]\n", - " [1.82969604e+30 1.82969604e+30 1.82969604e+30]]\n" - ] - } - ], + "execution_count": 2, + "id": "f765d0a0", + "metadata": { + "collapsed": false, + "editable": true + }, + "outputs": [], "source": [ "import numpy as np\n", "# SVD inversion\n", @@ -391,21 +423,23 @@ " return np.matmul(V,np.matmul(invD,UT))\n", "\n", "\n", - "X = np.array([ [1.0, -1.0, 2.0], [1.0, 0.0, 1.0], [1.0, 2.0, -1.0], [1.0, 1.0, 0.0] ])\n", + "#X = np.array([ [1.0, -1.0, 2.0], [1.0, 0.0, 1.0], [1.0, 2.0, -1.0], [1.0, 1.0, 0.0] ])\n", "# Non-singular square matrix\n", - "#X = np.array( [ [1,2,3],[2,4,5],[3,5,6]])\n", + "X = np.array( [ [1,2,3],[2,4,5],[3,5,6]])\n", "print(X)\n", "A = np.transpose(X) @ X\n", "# Brute force inversion\n", - "B = np.linalg.pinv(A) # here we could use np.linalg.pinv(A)\n", + "B = np.linalg.inv(A) # here we could use np.linalg.pinv(A)\n", "C = SVDinv(A)\n", "print(np.abs(B-C))" ] }, { "cell_type": "markdown", - "id": "7bd93f48", - "metadata": {}, + "id": "e459209e", + "metadata": { + "editable": true + }, "source": [ "## Inverse of Rectangular Matrix\n", "\n", @@ -422,8 +456,10 @@ }, { "cell_type": "markdown", - "id": "3fd0e27b", - "metadata": {}, + "id": "6c6da8a6", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\boldsymbol{A}_{\\mathrm{PI}}= \\boldsymbol{V}\\boldsymbol{D}_{\\mathrm{PI}}\\boldsymbol{U}^T,\n", @@ -432,33 +468,23 @@ }, { "cell_type": "markdown", - "id": "067d781b", - "metadata": {}, + "id": "efd71633", + "metadata": { + "editable": true + }, "source": [ "where $\\boldsymbol{D}_{\\mathrm{PI}}$ can be calculated by creating a diagonal matrix from $\\boldsymbol{\\Sigma}$ where we only keep the singular values (the non-zero values). The following code computes the pseudoinvers of the matrix based on the SVD." ] }, { "cell_type": "code", - "execution_count": 5, - "id": "c17f9502", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[[0.3 0.4]\n", - " [0.5 0.6]\n", - " [0.7 0.8]\n", - " [0.9 1. ]]\n", - "[[-13. -6. 1. 8. ]\n", - " [ 11.5 5.5 -0.5 -6.5]]\n", - "[[0. 0. 0. 0.]\n", - " [0. 0. 0. 0.]]\n" - ] - } - ], + "execution_count": 3, + "id": "db9d7882", + "metadata": { + "collapsed": false, + "editable": true + }, + "outputs": [], "source": [ "import numpy as np\n", "# SVD inversion\n", @@ -487,16 +513,20 @@ }, { "cell_type": "markdown", - "id": "c9e2f968", - "metadata": {}, + "id": "7fee3c4d", + "metadata": { + "editable": true + }, "source": [ "As you can see from this example, our own decomposition based on the SVD agrees the pseudoinverse algorithm provided by **Numpy**." ] }, { "cell_type": "markdown", - "id": "715bfc44", - "metadata": {}, + "id": "09aa560a", + "metadata": { + "editable": true + }, "source": [ "## Ridge and LASSO Regression\n", "\n", @@ -506,8 +536,10 @@ }, { "cell_type": "markdown", - "id": "a7250920", - "metadata": {}, + "id": "4b628c4f", + "metadata": { + "editable": true + }, "source": [ "$$\n", "{\\displaystyle \\min_{\\boldsymbol{\\beta}\\in {\\mathbb{R}}^{p}}}\\frac{1}{n}\\left\\{\\left(\\boldsymbol{y}-\\boldsymbol{X}\\boldsymbol{\\beta}\\right)^T\\left(\\boldsymbol{y}-\\boldsymbol{X}\\boldsymbol{\\beta}\\right)\\right\\}.\n", @@ -516,16 +548,20 @@ }, { "cell_type": "markdown", - "id": "2d4eca08", - "metadata": {}, + "id": "56abee21", + "metadata": { + "editable": true + }, "source": [ "or we can state it as" ] }, { "cell_type": "markdown", - "id": "5c47d606", - "metadata": {}, + "id": "aeef03ba", + "metadata": { + "editable": true + }, "source": [ "$$\n", "{\\displaystyle \\min_{\\boldsymbol{\\beta}\\in\n", @@ -535,16 +571,20 @@ }, { "cell_type": "markdown", - "id": "be48173a", - "metadata": {}, + "id": "b0ded245", + "metadata": { + "editable": true + }, "source": [ "where we have used the definition of a norm-2 vector, that is" ] }, { "cell_type": "markdown", - "id": "c1cc0c52", - "metadata": {}, + "id": "832509eb", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\vert\\vert \\boldsymbol{x}\\vert\\vert_2 = \\sqrt{\\sum_i x_i^2}.\n", @@ -553,8 +593,10 @@ }, { "cell_type": "markdown", - "id": "85043b85", - "metadata": {}, + "id": "146dc50a", + "metadata": { + "editable": true + }, "source": [ "## From OLS to Ridge and Lasso\n", "\n", @@ -566,8 +608,10 @@ }, { "cell_type": "markdown", - "id": "8b28ed2a", - "metadata": {}, + "id": "aec12b0f", + "metadata": { + "editable": true + }, "source": [ "$$\n", "{\\displaystyle \\min_{\\boldsymbol{\\beta}\\in\n", @@ -577,8 +621,10 @@ }, { "cell_type": "markdown", - "id": "498c8111", - "metadata": {}, + "id": "1326c8e8", + "metadata": { + "editable": true + }, "source": [ "which leads to the Ridge regression minimization problem where we\n", "require that $\\vert\\vert \\boldsymbol{\\beta}\\vert\\vert_2^2\\le t$, where $t$ is\n", @@ -587,8 +633,10 @@ }, { "cell_type": "markdown", - "id": "9d41fbd8", - "metadata": {}, + "id": "b68d575d", + "metadata": { + "editable": true + }, "source": [ "$$\n", "C(\\boldsymbol{X},\\boldsymbol{\\beta})=\\frac{1}{n}\\vert\\vert \\boldsymbol{y}-\\boldsymbol{X}\\boldsymbol{\\beta}\\vert\\vert_2^2+\\lambda\\vert\\vert \\boldsymbol{\\beta}\\vert\\vert_1,\n", @@ -597,16 +645,20 @@ }, { "cell_type": "markdown", - "id": "6bacb19c", - "metadata": {}, + "id": "11e6ed59", + "metadata": { + "editable": true + }, "source": [ "we have a new optimization equation" ] }, { "cell_type": "markdown", - "id": "4e386cd1", - "metadata": {}, + "id": "e6e1e17e", + "metadata": { + "editable": true + }, "source": [ "$$\n", "{\\displaystyle \\min_{\\boldsymbol{\\beta}\\in\n", @@ -616,8 +668,10 @@ }, { "cell_type": "markdown", - "id": "1bd20824", - "metadata": {}, + "id": "957ec3e8", + "metadata": { + "editable": true + }, "source": [ "which leads to Lasso regression. Lasso stands for least absolute shrinkage and selection operator. \n", "\n", @@ -626,8 +680,10 @@ }, { "cell_type": "markdown", - "id": "4264b9bb", - "metadata": {}, + "id": "9aca13ad", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\vert\\vert \\boldsymbol{x}\\vert\\vert_1 = \\sum_i \\vert x_i\\vert.\n", @@ -636,8 +692,10 @@ }, { "cell_type": "markdown", - "id": "eec0c847", - "metadata": {}, + "id": "786b818b", + "metadata": { + "editable": true + }, "source": [ "## Deriving the Ridge Regression Equations\n", "\n", @@ -646,8 +704,10 @@ }, { "cell_type": "markdown", - "id": "e11a479f", - "metadata": {}, + "id": "923eb0db", + "metadata": { + "editable": true + }, "source": [ "$$\n", "C(\\boldsymbol{X},\\boldsymbol{\\beta})=\\left\\{(\\boldsymbol{y}-\\boldsymbol{X}\\boldsymbol{\\beta})^T(\\boldsymbol{y}-\\boldsymbol{X}\\boldsymbol{\\beta})\\right\\}+\\lambda\\boldsymbol{\\beta}^T\\boldsymbol{\\beta},\n", @@ -656,8 +716,10 @@ }, { "cell_type": "markdown", - "id": "0f614521", - "metadata": {}, + "id": "d65e713e", + "metadata": { + "editable": true + }, "source": [ "and \n", "taking the derivatives with respect to $\\boldsymbol{\\beta}$ we obtain then\n", @@ -668,8 +730,10 @@ }, { "cell_type": "markdown", - "id": "e0a2c65a", - "metadata": {}, + "id": "3392d021", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\hat{\\boldsymbol{\\beta}}_{\\mathrm{Ridge}} = \\left(\\boldsymbol{X}^T\\boldsymbol{X}+\\lambda\\boldsymbol{I}\\right)^{-1}\\boldsymbol{X}^T\\boldsymbol{y},\n", @@ -678,16 +742,20 @@ }, { "cell_type": "markdown", - "id": "9a36d518", - "metadata": {}, + "id": "beea32ad", + "metadata": { + "editable": true + }, "source": [ "with $\\boldsymbol{I}$ being a $p\\times p$ identity matrix with the constraint that" ] }, { "cell_type": "markdown", - "id": "ec99d6ff", - "metadata": {}, + "id": "8c9054f2", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\sum_{i=0}^{p-1} \\beta_i^2 \\leq t,\n", @@ -696,16 +764,20 @@ }, { "cell_type": "markdown", - "id": "e5102c81", - "metadata": {}, + "id": "ce0df680", + "metadata": { + "editable": true + }, "source": [ "with $t$ a finite positive number." ] }, { "cell_type": "markdown", - "id": "277c29d3", - "metadata": {}, + "id": "c85bcd8f", + "metadata": { + "editable": true + }, "source": [ "## Note on Scikit-Learn\n", "\n", @@ -714,8 +786,10 @@ }, { "cell_type": "markdown", - "id": "3ee1172e", - "metadata": {}, + "id": "3d57bfb2", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\hat{\\boldsymbol{\\beta}}_{\\mathrm{Ridge}} = \\left(\\boldsymbol{X}^T\\boldsymbol{X}+n\\lambda\\boldsymbol{I}\\right)^{-1}\\boldsymbol{X}^T\\boldsymbol{y}.\n", @@ -724,16 +798,20 @@ }, { "cell_type": "markdown", - "id": "5555becd", - "metadata": {}, + "id": "4be71669", + "metadata": { + "editable": true + }, "source": [ "In our codes where we compare our own codes with **Scikit-Learn**, we do thus not include the $1/n$ factor in the cost function." ] }, { "cell_type": "markdown", - "id": "bf920b7a", - "metadata": {}, + "id": "78291c69", + "metadata": { + "editable": true + }, "source": [ "## Comparison with OLS\n", "When we compare this with the ordinary least squares result we have" @@ -741,8 +819,10 @@ }, { "cell_type": "markdown", - "id": "dcc8b3a0", - "metadata": {}, + "id": "8e2ce9db", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\hat{\\boldsymbol{\\beta}}_{\\mathrm{OLS}} = \\left(\\boldsymbol{X}^T\\boldsymbol{X}\\right)^{-1}\\boldsymbol{X}^T\\boldsymbol{y},\n", @@ -751,8 +831,10 @@ }, { "cell_type": "markdown", - "id": "b51d29a2", - "metadata": {}, + "id": "e5733ed1", + "metadata": { + "editable": true + }, "source": [ "which can lead to singular matrices. However, with the SVD, we can always compute the inverse of the matrix $\\boldsymbol{X}^T\\boldsymbol{X}$.\n", "\n", @@ -765,8 +847,10 @@ }, { "cell_type": "markdown", - "id": "4c81e874", - "metadata": {}, + "id": "168c1b1b", + "metadata": { + "editable": true + }, "source": [ "## SVD analysis\n", "\n", @@ -776,8 +860,10 @@ }, { "cell_type": "markdown", - "id": "7e9a88c2", - "metadata": {}, + "id": "c9876061", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\tilde{\\boldsymbol{y}}_{\\mathrm{OLS}}=\\boldsymbol{X}\\boldsymbol{\\beta} =\\boldsymbol{U}\\boldsymbol{U}^T\\boldsymbol{y}.\n", @@ -786,16 +872,20 @@ }, { "cell_type": "markdown", - "id": "16c44a1f", - "metadata": {}, + "id": "b5e3df11", + "metadata": { + "editable": true + }, "source": [ "For Ridge regression this becomes" ] }, { "cell_type": "markdown", - "id": "be091747", - "metadata": {}, + "id": "d86a5a2a", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\tilde{\\boldsymbol{y}}_{\\mathrm{Ridge}}=\\boldsymbol{X}\\boldsymbol{\\beta}_{\\mathrm{Ridge}} = \\boldsymbol{U\\Sigma V^T}\\left(\\boldsymbol{V}\\boldsymbol{\\Sigma}^2\\boldsymbol{V}^T+\\lambda\\boldsymbol{I} \\right)^{-1}(\\boldsymbol{U\\Sigma V^T})^T\\boldsymbol{y}=\\sum_{j=0}^{p-1}\\boldsymbol{u}_j\\boldsymbol{u}_j^T\\frac{\\sigma_j^2}{\\sigma_j^2+\\lambda}\\boldsymbol{y},\n", @@ -804,16 +894,20 @@ }, { "cell_type": "markdown", - "id": "e7c4958c", - "metadata": {}, + "id": "f905e4b4", + "metadata": { + "editable": true + }, "source": [ "with the vectors $\\boldsymbol{u}_j$ being the columns of $\\boldsymbol{U}$ from the SVD of the matrix $\\boldsymbol{X}$." ] }, { "cell_type": "markdown", - "id": "096042d4", - "metadata": {}, + "id": "ab780b24", + "metadata": { + "editable": true + }, "source": [ "## Interpreting the Ridge results\n", "\n", @@ -822,8 +916,10 @@ }, { "cell_type": "markdown", - "id": "a6133162", - "metadata": {}, + "id": "d634de61", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\frac{\\sigma_j^2}{\\sigma_j^2+\\lambda} \\leq 1.\n", @@ -832,8 +928,10 @@ }, { "cell_type": "markdown", - "id": "de47ccce", - "metadata": {}, + "id": "670a41a0", + "metadata": { + "editable": true + }, "source": [ "Ridge regression finds the coordinates of $\\boldsymbol{y}$ with respect to the\n", "orthonormal basis $\\boldsymbol{U}$, it then shrinks the coordinates by\n", @@ -846,8 +944,10 @@ }, { "cell_type": "markdown", - "id": "38d8e676", - "metadata": {}, + "id": "a66e2e86", + "metadata": { + "editable": true + }, "source": [ "## More interpretations\n", "\n", @@ -856,8 +956,10 @@ }, { "cell_type": "markdown", - "id": "cf226622", - "metadata": {}, + "id": "d7be9ae4", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\boldsymbol{X}^T\\boldsymbol{X}=(\\boldsymbol{X}^T\\boldsymbol{X})^{-1} =\\boldsymbol{I}.\n", @@ -866,16 +968,20 @@ }, { "cell_type": "markdown", - "id": "e51ab72f", - "metadata": {}, + "id": "417c1432", + "metadata": { + "editable": true + }, "source": [ "In this case the standard OLS results in" ] }, { "cell_type": "markdown", - "id": "aded2c2f", - "metadata": {}, + "id": "bf21b1eb", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\boldsymbol{\\beta}^{\\mathrm{OLS}} = \\boldsymbol{X}^T\\boldsymbol{y}=\\sum_{i=0}^{n-1}\\boldsymbol{u}_i\\boldsymbol{u}_i^T\\boldsymbol{y},\n", @@ -884,16 +990,20 @@ }, { "cell_type": "markdown", - "id": "d18586ec", - "metadata": {}, + "id": "e8c6c561", + "metadata": { + "editable": true + }, "source": [ "and" ] }, { "cell_type": "markdown", - "id": "d60836ed", - "metadata": {}, + "id": "cc83e17d", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\boldsymbol{\\beta}^{\\mathrm{Ridge}} = \\left(\\boldsymbol{I}+\\lambda\\boldsymbol{I}\\right)^{-1}\\boldsymbol{X}^T\\boldsymbol{y}=\\left(1+\\lambda\\right)^{-1}\\boldsymbol{\\beta}^{\\mathrm{OLS}},\n", @@ -902,8 +1012,10 @@ }, { "cell_type": "markdown", - "id": "83ee48f4", - "metadata": {}, + "id": "85e44116", + "metadata": { + "editable": true + }, "source": [ "that is the Ridge estimator scales the OLS estimator by the inverse of a factor $1+\\lambda$, and\n", "the Ridge estimator converges to zero when the hyperparameter goes to\n", @@ -917,8 +1029,10 @@ }, { "cell_type": "markdown", - "id": "6379bc41", - "metadata": {}, + "id": "743ab24a", + "metadata": { + "editable": true + }, "source": [ "## Deriving the Lasso Regression Equations\n", "\n", @@ -927,8 +1041,10 @@ }, { "cell_type": "markdown", - "id": "60a42dcc", - "metadata": {}, + "id": "cf09ce78", + "metadata": { + "editable": true + }, "source": [ "$$\n", "C(\\boldsymbol{X},\\boldsymbol{\\beta})=\\left\\{(\\boldsymbol{y}-\\boldsymbol{X}\\boldsymbol{\\beta})^T(\\boldsymbol{y}-\\boldsymbol{X}\\boldsymbol{\\beta})\\right\\}+\\lambda\\vert\\vert\\boldsymbol{\\beta}\\vert\\vert_1,\n", @@ -937,16 +1053,20 @@ }, { "cell_type": "markdown", - "id": "d335d8fe", - "metadata": {}, + "id": "9bdf27d8", + "metadata": { + "editable": true + }, "source": [ "Taking the derivative with respect to $\\boldsymbol{\\beta}$ and recalling that the derivative of the absolute value is (we drop the boldfaced vector symbol for simplicty)" ] }, { "cell_type": "markdown", - "id": "f7988d24", - "metadata": {}, + "id": "1397b054", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\frac{d \\vert \\beta\\vert}{d \\boldsymbol{\\beta}}=\\mathrm{sgn}(\\boldsymbol{\\beta})=\\left\\{\\begin{array}{cc} 1 & \\beta > 0 \\\\-1 & \\beta < 0, \\end{array}\\right.\n", @@ -955,16 +1075,20 @@ }, { "cell_type": "markdown", - "id": "455c0597", - "metadata": {}, + "id": "6d232041", + "metadata": { + "editable": true + }, "source": [ "we have that the derivative of the cost function is" ] }, { "cell_type": "markdown", - "id": "31b80ae8", - "metadata": {}, + "id": "7c743042", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\frac{\\partial C(\\boldsymbol{X},\\boldsymbol{\\beta})}{\\partial \\boldsymbol{\\beta}}=-2\\boldsymbol{X}^T(\\boldsymbol{y}-\\boldsymbol{X}\\boldsymbol{\\beta})+\\lambda sgn(\\boldsymbol{\\beta})=0,\n", @@ -973,16 +1097,20 @@ }, { "cell_type": "markdown", - "id": "89cfcd2e", - "metadata": {}, + "id": "ecf32c47", + "metadata": { + "editable": true + }, "source": [ "and reordering we have" ] }, { "cell_type": "markdown", - "id": "7b9c1855", - "metadata": {}, + "id": "04d7809b", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\boldsymbol{X}^T\\boldsymbol{X}\\boldsymbol{\\beta}+\\lambda sgn(\\boldsymbol{\\beta})=2\\boldsymbol{X}^T\\boldsymbol{y}.\n", @@ -991,16 +1119,20 @@ }, { "cell_type": "markdown", - "id": "64b3eb85", - "metadata": {}, + "id": "9ebd04d9", + "metadata": { + "editable": true + }, "source": [ "This equation does not lead to a nice analytical equation as in Ridge regression or ordinary least squares. This equation can however be solved by using standard convex optimization algorithms using for example the Python package [CVXOPT](https://cvxopt.org/). We will discuss this later." ] }, { "cell_type": "markdown", - "id": "57e33e83", - "metadata": {}, + "id": "b9aa9a0a", + "metadata": { + "editable": true + }, "source": [ "## Simple example to illustrate Ordinary Least Squares, Ridge and Lasso Regression\n", "\n", @@ -1012,8 +1144,10 @@ }, { "cell_type": "markdown", - "id": "9aaf5adc", - "metadata": {}, + "id": "d912d78a", + "metadata": { + "editable": true + }, "source": [ "$$\n", "C(\\boldsymbol{\\beta})=\\sum_{i=0}^{p-1}(y_i-\\beta_i)^2,\n", @@ -1022,16 +1156,20 @@ }, { "cell_type": "markdown", - "id": "a7e0855d", - "metadata": {}, + "id": "71180921", + "metadata": { + "editable": true + }, "source": [ "and minimizing we have that" ] }, { "cell_type": "markdown", - "id": "282ec43f", - "metadata": {}, + "id": "24877aab", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\hat{\\beta}_i^{\\mathrm{OLS}} = y_i.\n", @@ -1040,8 +1178,10 @@ }, { "cell_type": "markdown", - "id": "9a8686a0", - "metadata": {}, + "id": "5ceac1f1", + "metadata": { + "editable": true + }, "source": [ "## Ridge Regression\n", "\n", @@ -1050,8 +1190,10 @@ }, { "cell_type": "markdown", - "id": "d0c7441a", - "metadata": {}, + "id": "a43fbf8e", + "metadata": { + "editable": true + }, "source": [ "$$\n", "C(\\boldsymbol{\\beta})=\\sum_{i=0}^{p-1}(y_i-\\beta_i)^2+\\lambda\\sum_{i=0}^{p-1}\\beta_i^2,\n", @@ -1060,16 +1202,20 @@ }, { "cell_type": "markdown", - "id": "123a953a", - "metadata": {}, + "id": "ada51a1b", + "metadata": { + "editable": true + }, "source": [ "and minimizing we have that" ] }, { "cell_type": "markdown", - "id": "b80e95f5", - "metadata": {}, + "id": "eccc52bb", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\hat{\\beta}_i^{\\mathrm{Ridge}} = \\frac{y_i}{1+\\lambda}.\n", @@ -1078,8 +1224,10 @@ }, { "cell_type": "markdown", - "id": "1272391e", - "metadata": {}, + "id": "83b0d869", + "metadata": { + "editable": true + }, "source": [ "## Lasso Regression\n", "\n", @@ -1088,8 +1236,10 @@ }, { "cell_type": "markdown", - "id": "8a65a800", - "metadata": {}, + "id": "32c3342e", + "metadata": { + "editable": true + }, "source": [ "$$\n", "C(\\boldsymbol{\\beta})=\\sum_{i=0}^{p-1}(y_i-\\beta_i)^2+\\lambda\\sum_{i=0}^{p-1}\\vert\\beta_i\\vert=\\sum_{i=0}^{p-1}(y_i-\\beta_i)^2+\\lambda\\sum_{i=0}^{p-1}\\sqrt{\\beta_i^2},\n", @@ -1098,16 +1248,20 @@ }, { "cell_type": "markdown", - "id": "129b29dc", - "metadata": {}, + "id": "63ea1201", + "metadata": { + "editable": true + }, "source": [ "and minimizing we have that" ] }, { "cell_type": "markdown", - "id": "9671fc3b", - "metadata": {}, + "id": "ae009692", + "metadata": { + "editable": true + }, "source": [ "$$\n", "-2\\sum_{i=0}^{p-1}(y_i-\\beta_i)+\\lambda \\sum_{i=0}^{p-1}\\frac{(\\beta_i)}{\\vert\\beta_i\\vert}=0,\n", @@ -1116,16 +1270,20 @@ }, { "cell_type": "markdown", - "id": "645d20a1", - "metadata": {}, + "id": "afbd034f", + "metadata": { + "editable": true + }, "source": [ "which leads to" ] }, { "cell_type": "markdown", - "id": "4a940735", - "metadata": {}, + "id": "4917d37f", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\hat{\\boldsymbol{\\beta}}_i^{\\mathrm{Lasso}} = \\left\\{\\begin{array}{ccc}y_i-\\frac{\\lambda}{2} &\\mathrm{if} & y_i> \\frac{\\lambda}{2}\\\\\n", @@ -1136,16 +1294,20 @@ }, { "cell_type": "markdown", - "id": "f5d24fed", - "metadata": {}, + "id": "987f0c76", + "metadata": { + "editable": true + }, "source": [ "Plotting these results ([figure in handwritten notes for week 36](https://github.com/CompPhysics/MachineLearning/blob/master/doc/HandWrittenNotes/2021/NotesSeptember9.pdf)) shows clearly that Lasso regression suppresses (sets to zero) values of $\\beta_i$ for specific values of $\\lambda$. Ridge regression reduces on the other hand the values of $\\beta_i$ as function of $\\lambda$." ] }, { "cell_type": "markdown", - "id": "56fe26cc", - "metadata": {}, + "id": "97d73103", + "metadata": { + "editable": true + }, "source": [ "## Yet another Example\n", "\n", @@ -1154,8 +1316,10 @@ }, { "cell_type": "markdown", - "id": "bcdecc9d", - "metadata": {}, + "id": "1bb0bc4f", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\boldsymbol{y}=\\begin{bmatrix}4 \\\\ 2 \\\\3\\end{bmatrix},\n", @@ -1164,16 +1328,20 @@ }, { "cell_type": "markdown", - "id": "088b5e9c", - "metadata": {}, + "id": "60fa4c41", + "metadata": { + "editable": true + }, "source": [ "and our inputs as a $3\\times 2$ design matrix" ] }, { "cell_type": "markdown", - "id": "813decf7", - "metadata": {}, + "id": "9868a55b", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\boldsymbol{X}=\\begin{bmatrix}2 & 0\\\\ 0 & 1 \\\\ 0 & 0\\end{bmatrix},\n", @@ -1182,16 +1350,20 @@ }, { "cell_type": "markdown", - "id": "d2887852", - "metadata": {}, + "id": "e04ddc75", + "metadata": { + "editable": true + }, "source": [ "meaning that we have two features and two unknown parameters $\\beta_0$ and $\\beta_1$ to be determined either by ordinary least squares, Ridge or Lasso regression." ] }, { "cell_type": "markdown", - "id": "9d7b7953", - "metadata": {}, + "id": "50b5d084", + "metadata": { + "editable": true + }, "source": [ "## The OLS case\n", "\n", @@ -1200,8 +1372,10 @@ }, { "cell_type": "markdown", - "id": "e837f852", - "metadata": {}, + "id": "4e7afb4b", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\hat{\\boldsymbol{\\beta}}^{\\mathrm{OLS}}=\\left( \\boldsymbol{X}^T\\boldsymbol{X}\\right)^{-1}\\boldsymbol{X}^T\\boldsymbol{y}.\n", @@ -1210,16 +1384,20 @@ }, { "cell_type": "markdown", - "id": "15d802eb", - "metadata": {}, + "id": "b02ec7ca", + "metadata": { + "editable": true + }, "source": [ "Inserting the above values we obtain that" ] }, { "cell_type": "markdown", - "id": "cd4b96b1", - "metadata": {}, + "id": "685a6243", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\hat{\\boldsymbol{\\beta}}^{\\mathrm{OLS}}=\\begin{bmatrix}2 \\\\ 2\\end{bmatrix},\n", @@ -1228,16 +1406,20 @@ }, { "cell_type": "markdown", - "id": "434b97ea", - "metadata": {}, + "id": "05887870", + "metadata": { + "editable": true + }, "source": [ "The code which implements this simpler case is presented after the discussion of Ridge and Lasso." ] }, { "cell_type": "markdown", - "id": "d640a5fb", - "metadata": {}, + "id": "9b746aac", + "metadata": { + "editable": true + }, "source": [ "## The Ridge case\n", "\n", @@ -1246,8 +1428,10 @@ }, { "cell_type": "markdown", - "id": "f8a077e1", - "metadata": {}, + "id": "247a5768", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\hat{\\boldsymbol{\\beta}}^{\\mathrm{Ridge}}=\\left( \\boldsymbol{X}^T\\boldsymbol{X}+\\lambda\\boldsymbol{I}\\right)^{-1}\\boldsymbol{X}^T\\boldsymbol{y}.\n", @@ -1256,16 +1440,20 @@ }, { "cell_type": "markdown", - "id": "30897a80", - "metadata": {}, + "id": "4fcd1df5", + "metadata": { + "editable": true + }, "source": [ "Inserting the above values we obtain that" ] }, { "cell_type": "markdown", - "id": "1e751907", - "metadata": {}, + "id": "438c8cca", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\hat{\\boldsymbol{\\beta}}^{\\mathrm{Ridge}}=\\begin{bmatrix}\\frac{8}{4+\\lambda} \\\\ \\frac{2}{1+\\lambda}\\end{bmatrix},\n", @@ -1274,8 +1462,10 @@ }, { "cell_type": "markdown", - "id": "ca66337b", - "metadata": {}, + "id": "c55b0860", + "metadata": { + "editable": true + }, "source": [ "There is normally a constraint on the value of $\\vert\\vert \\boldsymbol{\\beta}\\vert\\vert_2$ via the parameter $\\lambda$.\n", "Let us for simplicity assume that $\\beta_0^2+\\beta_1^2=1$ as constraint. This will allow us to find an expression for the optimal values of $\\beta$ and $\\lambda$.\n", @@ -1285,8 +1475,10 @@ }, { "cell_type": "markdown", - "id": "30cb3a5b", - "metadata": {}, + "id": "74cf0cde", + "metadata": { + "editable": true + }, "source": [ "## Writing the Cost Function\n", "\n", @@ -1295,8 +1487,10 @@ }, { "cell_type": "markdown", - "id": "365400dc", - "metadata": {}, + "id": "8e25d3a0", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\boldsymbol{X}\\boldsymbol{\\beta}=\\begin{bmatrix} 2\\beta_0 \\\\ \\beta_1 \\\\0 \\end{bmatrix},\n", @@ -1305,8 +1499,10 @@ }, { "cell_type": "markdown", - "id": "f09ebaf1", - "metadata": {}, + "id": "f2dce6dc", + "metadata": { + "editable": true + }, "source": [ "$$\n", "C(\\boldsymbol{\\beta})=(4-2\\beta_0)^2+(2-\\beta_1)^2+\\lambda(\\beta_0^2+\\beta_1^2),\n", @@ -1315,16 +1511,20 @@ }, { "cell_type": "markdown", - "id": "1de97d2c", - "metadata": {}, + "id": "7e466c2e", + "metadata": { + "editable": true + }, "source": [ "and taking the derivative with respect to $\\beta_0$ we get" ] }, { "cell_type": "markdown", - "id": "78632ac0", - "metadata": {}, + "id": "1794597d", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\beta_0=\\frac{8}{4+\\lambda},\n", @@ -1333,16 +1533,20 @@ }, { "cell_type": "markdown", - "id": "3c88d85f", - "metadata": {}, + "id": "0c2a81c3", + "metadata": { + "editable": true + }, "source": [ "and for $\\beta_1$ we obtain" ] }, { "cell_type": "markdown", - "id": "97140392", - "metadata": {}, + "id": "6078099e", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\beta_1=\\frac{2}{1+\\lambda},\n", @@ -1351,16 +1555,20 @@ }, { "cell_type": "markdown", - "id": "7d1ea782", - "metadata": {}, + "id": "4a82e7ac", + "metadata": { + "editable": true + }, "source": [ "Using the constraint for $\\beta_0^2+\\beta_1^2=1$ we can constrain $\\lambda$ by solving" ] }, { "cell_type": "markdown", - "id": "9b49be00", - "metadata": {}, + "id": "5c4dea33", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\left(\\frac{8}{4+\\lambda}\\right)^2+\\left(\\frac{2}{1+\\lambda}\\right)^2=1,\n", @@ -1369,16 +1577,20 @@ }, { "cell_type": "markdown", - "id": "7bb324bc", - "metadata": {}, + "id": "cb4a101d", + "metadata": { + "editable": true + }, "source": [ "which gives $\\lambda=4.571$ and $\\beta_0=0.933$ and $\\beta_1=0.359$." ] }, { "cell_type": "markdown", - "id": "610a2a56", - "metadata": {}, + "id": "0d32288d", + "metadata": { + "editable": true + }, "source": [ "## Lasso case\n", "\n", @@ -1388,8 +1600,10 @@ }, { "cell_type": "markdown", - "id": "fc93bd4d", - "metadata": {}, + "id": "75dd3e83", + "metadata": { + "editable": true + }, "source": [ "$$\n", "C(\\boldsymbol{\\beta})=(4-2\\beta_0)^2+(2-\\beta_1)^2+\\lambda(\\vert\\beta_0\\vert+\\vert\\beta_1\\vert),\n", @@ -1398,8 +1612,10 @@ }, { "cell_type": "markdown", - "id": "7ac09787", - "metadata": {}, + "id": "47d4704a", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\frac{\\partial C(\\boldsymbol{\\beta})}{\\partial \\beta_0}=-4(4-2\\beta_0)+\\lambda\\mathrm{sgn}(\\beta_0)=0,\n", @@ -1408,16 +1624,20 @@ }, { "cell_type": "markdown", - "id": "a7b2f817", - "metadata": {}, + "id": "3127a2c8", + "metadata": { + "editable": true + }, "source": [ "and" ] }, { "cell_type": "markdown", - "id": "99632928", - "metadata": {}, + "id": "175b8660", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\frac{\\partial C(\\boldsymbol{\\beta})}{\\partial \\beta_1}=-2(2-\\beta_1)+\\lambda\\mathrm{sgn}(\\beta_1)=0.\n", @@ -1426,8 +1646,10 @@ }, { "cell_type": "markdown", - "id": "94c7a534", - "metadata": {}, + "id": "f820cdbe", + "metadata": { + "editable": true + }, "source": [ "We have now four cases to solve besides the trivial cases $\\beta_0$ and/or $\\beta_1$ are zero, namely\n", "1. $\\beta_0 > 0$ and $\\beta_1 > 0$,\n", @@ -1441,8 +1663,10 @@ }, { "cell_type": "markdown", - "id": "e55fc2b2", - "metadata": {}, + "id": "b004066b", + "metadata": { + "editable": true + }, "source": [ "## The first Case\n", "\n", @@ -1451,8 +1675,10 @@ }, { "cell_type": "markdown", - "id": "e2ef7286", - "metadata": {}, + "id": "4def35de", + "metadata": { + "editable": true + }, "source": [ "$$\n", "-4(4-2\\beta_0)+\\lambda=0,\n", @@ -1461,16 +1687,20 @@ }, { "cell_type": "markdown", - "id": "3a5c871e", - "metadata": {}, + "id": "7fdd8604", + "metadata": { + "editable": true + }, "source": [ "and" ] }, { "cell_type": "markdown", - "id": "b9ca6fd3", - "metadata": {}, + "id": "a33da947", + "metadata": { + "editable": true + }, "source": [ "$$\n", "-2(2-\\beta_1)+\\lambda=0.\n", @@ -1479,16 +1709,20 @@ }, { "cell_type": "markdown", - "id": "4c69db28", - "metadata": {}, + "id": "8ad8d519", + "metadata": { + "editable": true + }, "source": [ "which yields" ] }, { "cell_type": "markdown", - "id": "c77c93cb", - "metadata": {}, + "id": "9637d8ec", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\beta_0=\\frac{16+\\lambda}{8},\n", @@ -1497,16 +1731,20 @@ }, { "cell_type": "markdown", - "id": "cfdc629c", - "metadata": {}, + "id": "a50871a8", + "metadata": { + "editable": true + }, "source": [ "and" ] }, { "cell_type": "markdown", - "id": "07e71e01", - "metadata": {}, + "id": "2152879d", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\beta_1=\\frac{4+\\lambda}{2}.\n", @@ -1515,16 +1753,20 @@ }, { "cell_type": "markdown", - "id": "c8f65682", - "metadata": {}, + "id": "717ca01d", + "metadata": { + "editable": true + }, "source": [ "Using the constraint on $\\beta_0$ and $\\beta_1$ we can then find the optimal value of $\\lambda$ for the different cases. We leave this as an exercise to you." ] }, { "cell_type": "markdown", - "id": "bc6233b2", - "metadata": {}, + "id": "cbedfde8", + "metadata": { + "editable": true + }, "source": [ "## Simple code for solving the above problem\n", "\n", @@ -1536,8 +1778,11 @@ { "cell_type": "code", "execution_count": 4, - "id": "d594a12f", - "metadata": {}, + "id": "cced0760", + "metadata": { + "collapsed": false, + "editable": true + }, "outputs": [], "source": [ "%matplotlib inline\n", @@ -1595,16 +1840,20 @@ }, { "cell_type": "markdown", - "id": "6bd62e0a", - "metadata": {}, + "id": "5cc9d0bb", + "metadata": { + "editable": true + }, "source": [ "We see here that we reach a plateau. What is actually happening?" ] }, { "cell_type": "markdown", - "id": "715aeaa5", - "metadata": {}, + "id": "8f26fc0f", + "metadata": { + "editable": true + }, "source": [ "## With Lasso Regression" ] @@ -1612,8 +1861,11 @@ { "cell_type": "code", "execution_count": 5, - "id": "84affb9e", - "metadata": {}, + "id": "bf06f54c", + "metadata": { + "collapsed": false, + "editable": true + }, "outputs": [], "source": [ "import os\n", @@ -1676,8 +1928,10 @@ }, { "cell_type": "markdown", - "id": "70698b7b", - "metadata": {}, + "id": "9e346aac", + "metadata": { + "editable": true + }, "source": [ "## Another Example, now with a polynomial fit" ] @@ -1685,8 +1939,11 @@ { "cell_type": "code", "execution_count": 6, - "id": "d5e411b4", - "metadata": {}, + "id": "b3ce165d", + "metadata": { + "collapsed": false, + "editable": true + }, "outputs": [], "source": [ "import os\n", @@ -1771,8 +2028,10 @@ }, { "cell_type": "markdown", - "id": "10b0a7eb", - "metadata": {}, + "id": "6c6d6c09", + "metadata": { + "editable": true + }, "source": [ "## To think about, first part\n", "\n", @@ -1797,8 +2056,10 @@ }, { "cell_type": "markdown", - "id": "0fdacb5f", - "metadata": {}, + "id": "6a80380c", + "metadata": { + "editable": true + }, "source": [ "## More thinking\n", "\n", @@ -1830,8 +2091,10 @@ }, { "cell_type": "markdown", - "id": "6ade509a", - "metadata": {}, + "id": "1f6d0596", + "metadata": { + "editable": true + }, "source": [ "## Still thinking\n", "\n", @@ -1842,8 +2105,11 @@ { "cell_type": "code", "execution_count": 7, - "id": "ca8d02c2", - "metadata": {}, + "id": "79e0be06", + "metadata": { + "collapsed": false, + "editable": true + }, "outputs": [], "source": [ "#Model training, we compute the mean value of y and X\n", @@ -1864,8 +2130,10 @@ }, { "cell_type": "markdown", - "id": "bb52b09b", - "metadata": {}, + "id": "525c4ef5", + "metadata": { + "editable": true + }, "source": [ "## What does centering (subtracting the mean values) mean mathematically?\n", "\n", @@ -1878,8 +2146,10 @@ }, { "cell_type": "markdown", - "id": "3cde4c58", - "metadata": {}, + "id": "4585e4a1", + "metadata": { + "editable": true + }, "source": [ "$$\n", "C(\\beta_0, \\beta_1, ... , \\beta_{p-1}) = \\frac{1}{n}\\sum_{i=0}^{n} \\left(y_i - \\beta_0 - \\sum_{j=1}^{p-1} X_{ij}\\beta_j\\right)^2,.\n", @@ -1888,8 +2158,10 @@ }, { "cell_type": "markdown", - "id": "e0e6cac8", - "metadata": {}, + "id": "ba6207c3", + "metadata": { + "editable": true + }, "source": [ "Recall also that we use the squared value since this leads to an increase of the penalty for higher differences between predicted and output/target values.\n", "\n", @@ -1901,8 +2173,10 @@ }, { "cell_type": "markdown", - "id": "1e631321", - "metadata": {}, + "id": "81c29129", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\frac{\\partial C}{\\partial \\beta_j} = 0,\n", @@ -1911,16 +2185,20 @@ }, { "cell_type": "markdown", - "id": "8e5038d9", - "metadata": {}, + "id": "67596e4b", + "metadata": { + "editable": true + }, "source": [ "for all $j$. For $\\beta_0$ we have" ] }, { "cell_type": "markdown", - "id": "bb56c7ca", - "metadata": {}, + "id": "9bdb9d87", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\frac{\\partial C}{\\partial \\beta_0} = -\\frac{2}{n}\\sum_{i=0}^{n-1} \\left(y_i - \\beta_0 - \\sum_{j=1}^{p-1} X_{ij} \\beta_j\\right).\n", @@ -1929,16 +2207,20 @@ }, { "cell_type": "markdown", - "id": "3ed4442a", - "metadata": {}, + "id": "cb81ff47", + "metadata": { + "editable": true + }, "source": [ "Multiplying away the constant $2/n$, we obtain" ] }, { "cell_type": "markdown", - "id": "9104f8b7", - "metadata": {}, + "id": "71a389fc", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\sum_{i=0}^{n-1} \\beta_0 = \\sum_{i=0}^{n-1}y_i - \\sum_{i=0}^{n-1} \\sum_{j=1}^{p-1} X_{ij} \\beta_j.\n", @@ -1947,8 +2229,10 @@ }, { "cell_type": "markdown", - "id": "4a12287c", - "metadata": {}, + "id": "fa4b9339", + "metadata": { + "editable": true + }, "source": [ "## Further Manipulations\n", "\n", @@ -1958,8 +2242,10 @@ }, { "cell_type": "markdown", - "id": "5b425023", - "metadata": {}, + "id": "a31e16e7", + "metadata": { + "editable": true + }, "source": [ "$$\n", "n\\beta_0 = \\sum_{i=0}^{n-1}y_i - \\sum_{i=0}^{n-1} X_{i1} \\beta_1.\n", @@ -1968,16 +2254,20 @@ }, { "cell_type": "markdown", - "id": "e6fa30b0", - "metadata": {}, + "id": "5d6c27e0", + "metadata": { + "editable": true + }, "source": [ "We obtain then" ] }, { "cell_type": "markdown", - "id": "28be70e6", - "metadata": {}, + "id": "6e855d2b", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\beta_0 = \\frac{1}{n}\\sum_{i=0}^{n-1}y_i - \\beta_1\\frac{1}{n}\\sum_{i=0}^{n-1} X_{i1}.\n", @@ -1986,16 +2276,20 @@ }, { "cell_type": "markdown", - "id": "2b41476d", - "metadata": {}, + "id": "ac3b2e85", + "metadata": { + "editable": true + }, "source": [ "If we define" ] }, { "cell_type": "markdown", - "id": "97b05bfa", - "metadata": {}, + "id": "d677c104", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\mu_1=\\frac{1}{n}\\sum_{i=0}^{n-1} (X_{i1},\n", @@ -2004,16 +2298,20 @@ }, { "cell_type": "markdown", - "id": "bbb7f94a", - "metadata": {}, + "id": "b7d4987d", + "metadata": { + "editable": true + }, "source": [ "and if we define the mean value of the outputs as" ] }, { "cell_type": "markdown", - "id": "c0d06b59", - "metadata": {}, + "id": "f610e10c", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\mu_y=\\frac{1}{n}\\sum_{i=0}^{n-1}y_i,\n", @@ -2022,16 +2320,20 @@ }, { "cell_type": "markdown", - "id": "da1e1b48", - "metadata": {}, + "id": "592c0ada", + "metadata": { + "editable": true + }, "source": [ "we have" ] }, { "cell_type": "markdown", - "id": "b840be0b", - "metadata": {}, + "id": "95eb9032", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\beta_0 = \\mu_y - \\beta_1\\mu_{1}.\n", @@ -2040,16 +2342,20 @@ }, { "cell_type": "markdown", - "id": "c034bf6b", - "metadata": {}, + "id": "6d40a325", + "metadata": { + "editable": true + }, "source": [ "In the general case, that is we have more parameters than $\\beta_0$ and $\\beta_1$, we have" ] }, { "cell_type": "markdown", - "id": "8f64ec46", - "metadata": {}, + "id": "a73ec1dc", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\beta_0 = \\frac{1}{n}\\sum_{i=0}^{n-1}y_i - \\frac{1}{n}\\sum_{i=0}^{n-1}\\sum_{j=1}^{p-1} X_{ij}\\beta_j.\n", @@ -2058,16 +2364,20 @@ }, { "cell_type": "markdown", - "id": "dea8f0e3", - "metadata": {}, + "id": "110f580a", + "metadata": { + "editable": true + }, "source": [ "Replacing $y_i$ with $y_i - y_i - \\overline{\\boldsymbol{y}}$ and centering also our design matrix results in a cost function (in vector-matrix disguise)" ] }, { "cell_type": "markdown", - "id": "7d83c4bd", - "metadata": {}, + "id": "df442fd7", + "metadata": { + "editable": true + }, "source": [ "$$\n", "C(\\boldsymbol{\\beta}) = (\\boldsymbol{\\tilde{y}} - \\tilde{X}\\boldsymbol{\\beta})^T(\\boldsymbol{\\tilde{y}} - \\tilde{X}\\boldsymbol{\\beta}).\n", @@ -2076,8 +2386,10 @@ }, { "cell_type": "markdown", - "id": "bff682c6", - "metadata": {}, + "id": "4ae3df8b", + "metadata": { + "editable": true + }, "source": [ "## Wrapping it up\n", "\n", @@ -2086,8 +2398,10 @@ }, { "cell_type": "markdown", - "id": "be879f0d", - "metadata": {}, + "id": "145ac8a9", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\hat{\\boldsymbol{\\beta}} = (\\tilde{X}^T\\tilde{X})^{-1}\\tilde{X}^T\\boldsymbol{\\tilde{y}},\n", @@ -2096,8 +2410,10 @@ }, { "cell_type": "markdown", - "id": "c6f4c9e0", - "metadata": {}, + "id": "2ebb35d5", + "metadata": { + "editable": true + }, "source": [ "where $\\boldsymbol{\\tilde{y}} = \\boldsymbol{y} - \\overline{\\boldsymbol{y}}$\n", "and $\\tilde{X}_{ij} = X_{ij} - \\frac{1}{n}\\sum_{k=0}^{n-1}X_{kj}$.\n", @@ -2107,8 +2423,10 @@ }, { "cell_type": "markdown", - "id": "d82a7342", - "metadata": {}, + "id": "3f2293ec", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\hat{\\boldsymbol{\\beta}} = (\\tilde{X}^T\\tilde{X} + \\lambda I)^{-1}\\tilde{X}^T\\boldsymbol{\\tilde{y}}.\n", @@ -2117,16 +2435,20 @@ }, { "cell_type": "markdown", - "id": "2917e850", - "metadata": {}, + "id": "73504771", + "metadata": { + "editable": true + }, "source": [ "What does this mean? And why do we insist on all this? Let us look at some examples." ] }, { "cell_type": "markdown", - "id": "50b08ee1", - "metadata": {}, + "id": "3d935fba", + "metadata": { + "editable": true + }, "source": [ "## Linear Regression code, Intercept handling first\n", "\n", @@ -2137,8 +2459,11 @@ { "cell_type": "code", "execution_count": 8, - "id": "2c599d3e", - "metadata": {}, + "id": "c109b1b5", + "metadata": { + "collapsed": false, + "editable": true + }, "outputs": [], "source": [ "import numpy as np\n", @@ -2231,8 +2556,10 @@ }, { "cell_type": "markdown", - "id": "b27a10b8", - "metadata": {}, + "id": "534b2f34", + "metadata": { + "editable": true + }, "source": [ "The intercept is the value of our output/target variable\n", "when all our features are zero and our function crosses the $y$-axis (for a one-dimensional case). \n", @@ -2250,8 +2577,10 @@ }, { "cell_type": "markdown", - "id": "b217703e", - "metadata": {}, + "id": "622d039d", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\lambda \\vert\\vert \\boldsymbol{\\beta} \\vert\\vert_2^2 = \\lambda \\sum_{j=0}^{p-1}\\beta_j^2,\n", @@ -2260,16 +2589,20 @@ }, { "cell_type": "markdown", - "id": "04f6b527", - "metadata": {}, + "id": "c6b33efa", + "metadata": { + "editable": true + }, "source": [ "but when we take out the intercept, this equation becomes" ] }, { "cell_type": "markdown", - "id": "ca6755df", - "metadata": {}, + "id": "c184e92a", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\lambda \\vert\\vert \\boldsymbol{\\beta} \\vert\\vert_2^2 = \\lambda \\sum_{j=1}^{p-1}\\beta_j^2.\n", @@ -2278,16 +2611,20 @@ }, { "cell_type": "markdown", - "id": "3e2e804d", - "metadata": {}, + "id": "61f55115", + "metadata": { + "editable": true + }, "source": [ "For Lasso regression we have" ] }, { "cell_type": "markdown", - "id": "b28a22e4", - "metadata": {}, + "id": "4aa4ba1c", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\lambda \\vert\\vert \\boldsymbol{\\beta} \\vert\\vert_1 = \\lambda \\sum_{j=1}^{p-1}\\vert\\beta_j\\vert.\n", @@ -2296,16 +2633,20 @@ }, { "cell_type": "markdown", - "id": "b580959b", - "metadata": {}, + "id": "534a2cca", + "metadata": { + "editable": true + }, "source": [ "It means that, when scaling the design matrix and the outputs/targets, by subtracting the mean values, we have an optimization problem which is not penalized by the intercept. The MSE value can then be smaller since it focuses only on the remaining quantities. If we however bring back the intercept, we will get a MSE which then contains the intercept." ] }, { "cell_type": "markdown", - "id": "0a7136a9", - "metadata": {}, + "id": "c45e132c", + "metadata": { + "editable": true + }, "source": [ "## Code Examples\n", "\n", @@ -2314,43 +2655,13 @@ }, { "cell_type": "code", - "execution_count": 2, - "id": "fb95e6e4", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Beta values for own Ridge implementation\n", - "[ 1.04152388 -0.08796629 -0.2265552 -0.06821103 0.04383751 0.08561268\n", - " 0.08570563 0.06773487 0.04507 0.02407463 0.00721908 -0.00497338\n", - " -0.01285634 -0.01711057 -0.0184785 -0.01764665 -0.01520244 -0.01162643\n", - " -0.00730033 -0.00252065]\n", - "Beta values for Scikit-Learn Ridge implementation\n", - "[ 1.04152388 -0.08796629 -0.2265552 -0.06821103 0.04383751 0.08561268\n", - " 0.08570563 0.06773487 0.04507 0.02407463 0.00721908 -0.00497338\n", - " -0.01285634 -0.01711057 -0.0184785 -0.01764665 -0.01520244 -0.01162643\n", - " -0.00730033 -0.00252065]\n", - "MSE values for own Ridge implementation\n", - "1.621815415486309e-05\n", - "MSE values for Scikit-Learn Ridge implementation\n", - "1.6218154154878254e-05\n" - ] - }, - { - "data": { - "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYUAAAERCAYAAACU1LsdAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjQuMywgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/MnkTPAAAACXBIWXMAAAsTAAALEwEAmpwYAAAmM0lEQVR4nO3de5gV1Znv8e8vAoKiYKDHG6OgIyRcG2g0qBAdVBSVqKMHUGOQOEgSNU4iBz3J8RLHUWMmQXOBId6SyKCj8S4SjRHhREUuIqKIihAFUVsSFVCQhvf8UdWVzXb3vXc3jb/P8/RD7VWrVr273Na7q1bttRQRmJmZAXyhuQMwM7Mdh5OCmZllnBTMzCzjpGBmZhknBTMzyzgpmJlZpkUmBUm3SnpP0tJGam+WpA8kPZxXPlfS4vTvbUn3N3aMko5K239J0lNp2T9KelLSy2n5d3Pqn5GWbZNUllPeWtJvJL0oaZmky2qKRdKVktbkvMcReesPkLRB0iX1bUtSV0mf5JRPre0xrOGYfZjT5uUNbdPMEi0yKQC3A8c3Yns3AF/PL4yIIRFRGhGlwDPAvfl1JN0u6aj6xCipI/ArYGRE9ALOSFdVAN+PiJ7AV4DvSOqZrlsKnAbMyWvuDGDXiOgDDATOl9S1FrH8rPI9RsTMvHU/BR6tw/uqqq0VOeUTqti2rubmtPmjRmrT7HOvRSaFiJgD/DW3TNLB6Tf+hek3/C/Vob0ngPVVrZe0J/DPwP0NibGAM4F7I+LNdJv30n/XRsSidHk9sAzYP329LCKWF9olsLukVkA74FPgozrEsh1JpwArgZfq8b5qu4/jJD0jaZGkuyW1b4x2zaz+WmRSqMI04MKIGAhcQvINvLGcAjwRER81YpsA3YG9JM1Ok9k5+RXSb/v9gXk1tHUPsBFYC7wJ/CQianPyvkDSkvS20F7pPtsDk4Crav9WCreV6ibpeUlPSRqS7qMz8EPgmIgYACwAvleHfQ2W9IKkRyX1qmOcZlaFVs0dQGNIT2KHA3dLqizeNV13GlDo9sKaiBhey12MAW7O2d9w4Pr05QHAkZI2AJsj4rA6hN6K5FbPMJJv989IejYiXs15X78HLq5FQjoU2ArsB+wFzJX0x4h4o5ptpgBXk1xlXA38JzAOuJLkVtCGnONZk6raWgscEBHrJA0E7k9P4l8BegJ/TvfRhuQWHZKuBU4usI/7I+KHwCLgwDS+ESRXcIfUNlAzq9pOkRRIrng+SO/9byci7qVAX0Btpd9oDwVOzWnzD8Af0vW3A7dHxOx6NL8aWBcRG4GNkuYA/YBXJbUmSQjT0/dQkzOBWRGxBXhP0p+BMqDKpBAR71YuS/o1UNnRfhhwuqQfAx2BbZI2RcQv6tpWRGwGNqfLCyWtILlCEvB4RIwp0NZlwGX55TnrP8pZninpV5I6R8T7VW1jZrWzU9w+Sk8SKyWdAaBEv0Zq/nTg4YjY1Ejt5XqA5CqjlaTdSE7Gy5R8db4FWBYRP61lW2+S9HsgaXeSb+KvVLeBpH1zXp5K0old2cHeNSK6ApOB/6guIVTXlqQSSbukyweRfKN/A3gWOELSP1XGLKl7bd6opH3SY4SkQ0k+x+tqs62ZVa9FJgVJM0huNfSQtFrSN4GzgG9KeoGkc/RrdWhvLnA3MCxtL/e20mhgRiPFiKQJkiZA0mkMzAKWAM8BN0fEUuAIkqeh/rnAI56nSloNDAYekfSHdJe/BNpLegmYD9wWEUuqiwX4sZJHWJcARwP/Vt/3VU1bQ4ElkhaT9HtMiIi/RkQ5MBaYkW7zDFDbhwNOB5am/61vAkaHh/s1axTy/0tmZlapRV4pmJlZcbS4jubOnTtH165dmzsMM7MWZeHChe9HRElN9VpcUujatSsLFixo7jDMzFoUSX+pTT3fPjIzs4yTgpmZZZwUzMws0+L6FMwssWXLFlavXs2mTcX4XaW1VG3btqVLly60bt26Xts7KZi1UKtXr2aPPfaga9eu1GGMKtuJRQTr1q1j9erVdOvWrV5t+PaRWQu1adMmOnXq5IRgGUl06tSpQVePTgpmLZgTguVr6GfCScHMzDJFSwqSeuQM5rZY0keSLs6r00HSQ+lkKS9JOrdY8ZhZ45PE2Wefnb2uqKigpKSEk046CYB3332Xk046iX79+tGzZ09GjEimAV+1ahXt2rWjtLQ0+/vtb3/bpLHnxtCzZ0/OOecctmzZAsCCBQu46KKLCm7XtWtX3n+/YaO033bbbdn7btOmDX369KG0tJRLL7201m1MnjyZjz/+uEFxFBQRRf8DdgHeIZkYJbf8/wDXp8slJNM8tqmurYEDB4aZRbz88svNHULsvvvu0a9fv/j4448jImLmzJnRr1+/OPHEEyMiYvz48TF58uSs/gsvvBAREStXroxevXo1fcA5cmOoqKiIo48+Ou64444atzvwwAOjvLy80eKob3vVbVfoswEsiFqcr5vq9tEwksnb839mHcAe6dj47dOkUNFEMZlZIxgxYgSPPPIIADNmzGDMmL/Pm7R27Vq6dOmSve7bt2+d2n7iiSfo378/ffr0Ydy4cWzevJn58+dz2mmnAfDAAw/Qrl07Pv30UzZt2sRBBx0EwFFHHcWkSZM49NBD6d69O3Pnzq12P7vssguHHnooa9asAWD27NnZ1c66des47rjj6NWrF+edd17lF1oArr76anr06MGRRx7JmDFj+MlPfgLAihUrOP744xk4cCBDhgzhlVeqndokc8MNNzBo0CD69u3LFVdcAcDGjRs58cQT6devH7179+auu+7ipptu4u233+boo4/m6KOPrsMRrVlTPZJa1ZwEvwAeBN4G9gBGRcS2JorJbOdx8cWweHHjtllaCpMn11ht9OjR/OhHP+Kkk05iyZIljBs3LjsJf+c732HUqFH84he/4JhjjuHcc89lv/32A5ITZ2lpadbOz3/+c4YMGZK93rRpE2PHjuWJJ56ge/funHPOOUyZMoULLriAxel7nTt3Lr1792b+/PlUVFRw2GF/nw23oqKC5557jpkzZ3LVVVfxxz/+scr3sGnTJubNm8eNN974mXVXXXUVRx55JJdffjmPPPIIt9xyCwDz58/n97//PS+88AJbtmxhwIABDBw4EIDx48czdepUDjnkEObNm8e3v/1t/vSnP1V7HB977DFee+01nnvuOSKCkSNHMmfOHMrLy9lvv/2yxPvhhx/SoUMHfvrTn/Lkk0/SuXPnatutq6InBUltgJEUnl5xOLCYZMawg4HHJc2NvPmIJY0HxgMccMABRY3XzOqmb9++rFq1ihkzZmR9BpWGDx/OG2+8waxZs3j00Ufp378/S5cuBeDggw/OTu6FLF++nG7dutG9ezIh3ze+8Q1++ctfcvHFF3PwwQezbNkynnvuOb73ve8xZ84ctm7dul1SqbyaGDhwIKtWrSq4j8rEtHLlSk488cSCVzJz5szh3nuTGXFPPPFE9tprLwD+/Oc/87WvfY22bdvStm1bTj45mVZ8w4YNPP3005xxxhlZG5s3b67uEAJJUnjsscfo379/1s5rr73GkCFD+P73v8+kSZM46aSTtnuPxdAUVwonAIsiZw7fHOcC16X3u16XtJJk9q3ncitFxDRgGkBZWZlnBTLLV4tv9MU0cuRILrnkEmbPns26ddvPjPrFL36RM888kzPPPJOTTjqJOXPmZN+o62vo0KE8+uijtG7dmmOOOYaxY8eydetWbrjhhqzOrrvuCiS3hioqCt+VrkxM77//PkcccQQPPvggI0eObFBs27Zto2PHjtUmvEIigssuu4zzzz//M+sWLVrEzJkz+eEPf8iwYcO4/PLLGxRjdZqiT2EMVU9n+SZJfwOS9gZ6UM1E82a2Yxo3bhxXXHEFffr02a78T3/6U/aEzPr161mxYkWtr/Z79OjBqlWreP311wH43e9+x1e/+lUAhgwZwuTJkxk8eDAlJSWsW7eO5cuX07t373rF37lzZ6677jquvfbaz6wbOnQo//3f/w3Ao48+yt/+9jcAjjjiCB566CE2bdrEhg0bePjhhwHYc8896datG3fffTeQnOxfeOGFGmMYPnw4t956Kxs2bABgzZo1vPfee7z99tvstttunH322UycOJFFixYBsMcee7B+/fp6vd/qFPVKIZ1A/ljg/JyyyvmJpwJXA7dLehEQMCkiGvasl5k1uS5duhR8hHPhwoVccMEFtGrVim3btnHeeecxaNAgVq1a9Zk+hXHjxm3XRtu2bbnttts444wzqKioYNCgQUyYMAGAww47jHfffZehQ4cCyS2sd955p0E/3DrllFO48sorP9MpfcUVVzBmzBh69erF4YcfniW1QYMGMXLkSPr27cvee+9Nnz596NChAwDTp0/nW9/6Fv/+7//Oli1bGD16NP369at2/8cddxzLli1j8ODBALRv35477riD119/nYkTJ/KFL3yB1q1bM2XKFCDptzj++OPZb7/9ePLJJ+v9vvO1uDmay8rKwpPsmMGyZcv48pe/3NxhfK5t2LCB9u3b8/HHHzN06FCmTZvGgAEDmjusgp8NSQsjoqymbT0gnplZPY0fP56XX36ZTZs28Y1vfGOHSAgN5aRgZlZPlX0NOxOPfWRmZhknBTMzyzgpmJlZxknBzMwyTgpmVm/FHjr7qKOOokePHvTr149BgwZt9yvhESNG8MEHH3xmmyuvvDIbmK6+1q1bl8W1zz77sP/++2evP/3001q1MXv2bJ5++ukGxdEc/PSRmdXb7rvvztKlS/nkk09o164djz/+OPvvv3+2/vLLL+fYY4/lu9/9LgBLlizJ1tU09lGl6dOnU1ZWxm233cbEiRN5/PHHAZg5c2bjvpkcnTp1ymK78sorad++PZdcckmd2pg9ezbt27fn8MMPL0KExeMrBTNrkGIOnZ1r8ODB2dDWsP1kN9dccw3du3fnyCOPZPny5Vmd+fPn07dvX0pLS5k4cWI2DMbWrVuZOHFiNkz1f/3Xf9UqhoULF/LVr36VgQMHMnz4cNauXQvATTfdRM+ePenbty+jR49m1apVTJ06lZ/97GeUlpbWOHT3jsRXCmY7gYtnXczidxY3apul+5Qy+fjJNdYr1tDZ+WbNmsUpp5zymfKFCxdy5513snjxYioqKrYbwvrcc8/l17/+NYMHD95uVrNbbrmFDh06MH/+fDZv3swRRxzBcccdR7du3arc/5YtW7jwwgt54IEHKCkp4a677uIHP/gBt956K9dddx0rV65k11135YMPPqBjx45MmDChXlcYzc1JwcwapFhDZ1c666yz+PTTT9mwYUPB+nPnzuXUU09lt912A8hGOf3ggw9Yv359NpbQmWeemQ1a99hjj7FkyRLuueceIJmj4LXXXqs2KSxfvpylS5dy7LHHAsnVxr777psdg7POOotTTjmlYOJqSZwUzHYCtflGX0zFHDp7+vTpDBw4kIkTJ3LhhRdmcxs0RETw85//nOHDh9dpm169evHMM898Zt0jjzzCnDlzeOihh7jmmmt48cUXGxxjc3Gfgpk1WDGGzs4liauvvppnn332M1NbDh06lPvvv59PPvmE9evX89BDDwHQsWNH9thjD+bNmwfAnXfemW0zfPhwpkyZwpYtWwB49dVX2bhxY7Ux9OjRg/Ly8iwpbNmyhZdeeolt27bx1ltvcfTRR3P99dfz4YcfsmHDhqINbV1sTgpm1mDVDZ1dVlZG3759GTx4cDZ0Nvy9T6Hy76abbqp2H+3ateP73//+dhPpAAwYMIBRo0bRr18/TjjhhKx9SPoO/vVf/5XS0lI2btyYDW193nnn0bNnTwYMGEDv3r05//zzq5yIp1KbNm245557mDRpEv369aO0tJSnn36arVu3cvbZZ9OnTx/69+/PRRddRMeOHTn55JO57777WlxHs4fONmuhPHR2zSqHtga47rrrWLt2bcF5mHc2HjrbzKyARx55hGuvvZaKigoOPPBAbr/99uYOaYfnpGBmO61Ro0YxatSo5g6jRSlan4KkHpIW5/x9JOniAvWOSte/JOmpYsVjtjNqabd/rfga+pko2pVCRCwHSgEk7QKsAe7LrSOpI/Ar4PiIeFPSPxQrHrOdTdu2bVm3bh2dOnVq0NzEtvOICNatW0fbtm3r3UZT3T4aBqyIiL/klZ8J3BsRbwJExHtNFI9Zi9elSxdWr15NeXl5c4diO5C2bdtuN7RIXTVVUhgNzChQ3h1oLWk2sAdwY0R8dqhEM/uM1q1bV/sLXLP6KHpSkNQGGAlcVsX+B5JcSbQDnpH0bES8mtfGeGA8UK8fvpiZWe00xY/XTgAWRcS7BdatBv4QERsj4n1gDtAvv1JETIuIsogoKykpKXK4ZmafX02RFMZQ+NYRwAPAkZJaSdoNOAxY1gQxmZlZAUW9fSRpd+BY4PycsgkAETE1IpZJmgUsAbYBN0fE0mLGZGZmVStqUoiIjUCnvLKpea9vALYfzMTMzJqFB8QzM7OMk4KZmWWcFMzMLOOkYGZmGScFMzPLOCmYmVnGScHMzDJOCmZmlnFSMDOzjJOCmZllnBTMzCzjpGBmZhknBTMzyzgpmJlZxknBzMwyTgpmZpZxUjAzs4yTgpmZZYqWFCT1kLQ45+8jSRdXUXeQpApJpxcrHjMzq1nR5miOiOVAKYCkXYA1wH359dJ11wOPFSsWMzOrnaa6fTQMWBERfymw7kLg98B7TRSLmZlVoamSwmhgRn6hpP2BU4Ep1W0sabykBZIWlJeXFylEMzMrelKQ1AYYCdxdYPVkYFJEbKuujYiYFhFlEVFWUlJShCjNzAyK2KeQ4wRgUUS8W2BdGXCnJIDOwAhJFRFxfxPEZWZmeZoiKYyhwK0jgIjoVrks6XbgYScEM7PmU9TbR5J2B44F7s0pmyBpQjH3a2Zm9VPUK4WI2Ah0yiubWkXdscWMxczMauZfNJuZWcZJwczMMk4KZmaWcVIwM7OMk4KZmWWcFMzMLOOkYGZmGScFMzPLOCmYmVnGScHMzDJOCmZmlnFSMDOzjJOCmZllnBTMzCzjpGBmZhknBTMzyzgpmJlZpmhJQVIPSYtz/j6SdHFenbMkLZH0oqSnJfUrVjxmZlazok3HGRHLgVIASbsAa4D78qqtBL4aEX+TdAIwDTisWDGZmVn1ijpHc45hwIqI+EtuYUQ8nfPyWaBLE8VjZmYFNFWfwmhgRg11vgk8WmiFpPGSFkhaUF5e3ujBmZlZouhJQVIbYCRwdzV1jiZJCpMKrY+IaRFRFhFlJSUlxQnUzMya5PbRCcCiiHi30EpJfYGbgRMiYl0TxGNmZlVoittHY6ji1pGkA4B7ga9HxKtNEIuZmVWjqFcKknYHjgXOzymbABARU4HLgU7AryQBVEREWTFjMjOzqhU1KUTERpKTfm7Z1Jzl84DzihmDmZnVnn/RbGZmGScFMzPLOCmYmVmm2qQg6eyc5SPy1l1QrKDMzKx51HSl8L2c5Z/nrRvXyLGYmVkzqykpqIrlQq/NzKyFqykpRBXLhV6bmVkLV9PvFL4kaQnJVcHB6TLp64OKGpmZmTW5mpLCl5skCjMz2yFUmxTy5z+Q1AkYCrwZEQuLGZiZmTW9mh5JfVhS73R5X2ApyVNHv8ufWtPMzFq+mjqau0XE0nT5XODxiDiZZMpMP5JqZraTqSkpbMlZHgbMBIiI9cC2YgVlZmbNo6aO5rckXQisBgYAswAktQNaFzk2MzNrYjVdKXwT6AWMBUZFxAdp+VeA24oXlpmZNYeanj56D5hQoPxJ4MliBWVmZs2j2qQg6cHq1kfEyMYNx8zMmlNNfQqDgbdI5lieRx3GO5LUA7grp+gg4PKImJxTR8CNwAjgY2BsRCyq7T7MzKxx1ZQU9iGZY3kMcCbwCDAjIl6qqeGIWA6UAkjaBVgD3JdX7QTgkPTvMGBK+q+ZmTWDajuaI2JrRMyKiG+QdC6/Dsyux1wKw4AV+b+QBr4G/DYSzwId0x/JmZlZM6jpSgFJuwInklwtdAVu4rPf+GsymuQWVL79SW5PVVqdlq3Ni2E8MB7ggAMOqOOuzcystmrqaP4t0JvkR2tX5fy6udYktQFGApfVK0IgIqYB0wDKyso8ZLeZWZHUdKVwNrAR+C5wUdIvDCQdzhERe9ZiHycAiyLi3QLr1gD/mPO6S1pmZmbNoKbfKdT047baGEPhW0cADwIXSLqTpIP5w4hYW0VdMzMrshr7FBpC0u4kTy+dn1M2ASAippLclhpB0oH9Mcmge2Zm1kyKmhQiYiPQKa9sas5yAN8pZgxmZlZ7jXF7yMzMdhJOCmZmlnFSMDOzjJOCmZllnBTMzCzjpGBmZhknBTMzyzgpmJlZxknBzMwyTgpmZpZxUjAzs4yTgpmZZZwUzMws46RgZmYZJwUzM8s4KZiZWcZJwczMMkVNCpI6SrpH0iuSlkkanLe+g6SHJL0g6SVJno7TzKwZFXU6TuBGYFZEnC6pDbBb3vrvAC9HxMmSSoDlkqZHxKdFjsvMzAooWlKQ1AEYCowFSE/0+Sf7APaQJKA98FegolgxmZlZ9Yp5+6gbUA7cJul5STdL2j2vzi+ALwNvAy8C342IbfkNSRovaYGkBeXl5UUM2czs862YSaEVMACYEhH9gY3ApXl1hgOLgf2AUuAXkvbMbygipkVEWUSUlZSUFDFkM7PPt2ImhdXA6oiYl76+hyRJ5DoXuDcSrwMrgS8VMSYzM6tG0ZJCRLwDvCWpR1o0DHg5r9qbaTmS9gZ6AG8UKyYzM6tesZ8+uhCYnj559AZwrqQJABExFbgauF3Si4CASRHxfpFjMjOzKhQ1KUTEYqAsr3hqzvq3geOKGYOZmdWef9FsZmYZJwUzM8s4KZiZWcZJwczMMk4KZmaWcVIwM7OMk4KZmWWcFMzMLOOkYGZmGScFMzPLOCmYmVnGScHMzDJOCmZmlnFSMDOzjJOCmZllnBTMzCzjpGBmZpmiJgVJHSXdI+kVScskDS5Q5yhJiyW9JOmpYsZjZmbVK/YczTcCsyLi9HSe5t1yV0rqCPwKOD4i3pT0D0WOx8zMqlG0pCCpAzAUGAsQEZ8Cn+ZVOxO4NyLeTOu8V6x4zMysZsW8fdQNKAduk/S8pJsl7Z5Xpzuwl6TZkhZKOqdQQ5LGS1ogaUF5eXkRQzYz+3wrZlJoBQwApkREf2AjcGmBOgOBE4HhwP+V1D2/oYiYFhFlEVFWUlJSxJDNzD7fipkUVgOrI2Je+voekiSRX+cPEbExIt4H5gD9ihiTmZlVo2hJISLeAd6S1CMtGga8nFftAeBISa0k7QYcBiwrVkxmZla9Yj99dCEwPX3y6A3gXEkTACJiakQskzQLWAJsA26OiKVFjsnMzKqgiGjuGOqkrKwsFixY0NxhmJm1KJIWRkRZTfX8i2YzM8s4KZiZWcZJwczMMk4KZmaWcVIwM7OMk4KZmWWcFMzMLOOkYGZmGScFMzPLOCmYmVnGScHMzDJOCmZmlnFSMDOzjJOCmZllnBTMzCzjpGBmZhknBTMzyxQ1KUjqKOkeSa9IWiZpcBX1BkmqkHR6MeMxM7PqFXuO5huBWRFxejpP8275FSTtAlwPPFbkWMzMrAZFu1KQ1AEYCtwCEBGfRsQHBapeCPweeK9YsZiZWe0U8/ZRN6AcuE3S85JulrR7bgVJ+wOnAlOqa0jSeEkLJC0oLy8vXsRmZp9zxUwKrYABwJSI6A9sBC7NqzMZmBQR26prKCKmRURZRJSVlJQUJVgzMytun8JqYHVEzEtf38Nnk0IZcKckgM7ACEkVEXF/EeMyM7MqFC0pRMQ7kt6S1CMilgPDgJfz6nSrXJZ0O/CwE4KZWfMp9tNHFwLT0yeP3gDOlTQBICKmFnnfZmZWR0VNChGxmOQWUa6CySAixhYzFjMzq5l/0WxmZhknBTMzyzgpmJlZxknBzMwyTgpmZpZxUjAzs4yTgpmZZZwUzMws46RgZmYZJwUzM8s4KZiZWcZJwczMMk4KZmaWcVIwM7OMk4KZmWWcFMzMLKOIaO4Y6kRSOfCXem7eGXi/EcNpLDtqXLDjxua46sZx1c3OGNeBEVFSU6UWlxQaQtKCiMifCa7Z7ahxwY4bm+OqG8dVN5/nuHz7yMzMMk4KZmaW+bwlhWnNHUAVdtS4YMeNzXHVjeOqm89tXJ+rPgUzM6ve5+1KwczMquGkYGZmmZ0iKUi6QdIrkpZIuk9Sxyrq3SrpPUlL88q/KOlxSa+l/+6VlkvSTZJeT9seUKS4jpe0PN3PpTnlcyUtTv/elnR/Wn6UpA9z1l3exHHdLmllzv5L0/LmPl7T0/Kl6X/r1ml5cx+vbpLmpeV3SWqTlu+avn49Xd+1jnGdIeklSdskVfmYoqTvpsfkJUkX55TflXNMVklanJZ3lfRJzrqpTRzXlZLW5Ox/RM66y9LjtVzS8CaOq+DnYAc4Xo17/oqIFv8HHAe0SpevB66vot5QYACwNK/8x8Cl6fKlldsDI4BHAQFfAeY1dlzALsAK4CCgDfAC0LNAvd8D56TLRwEPF/N4VRcXcDtweoFtmvV4pftX+jcD+NYOcrz+BxidLk/NievbwNR0eTRwVx3j+jLQA5gNlFVRpzewFNgNaAX8EfinAvX+E7g8Xe6a//9IU8YFXAlcUmCbnulx3RXolh7vXZowroKfgx3geDXq+WunuFKIiMcioiJ9+SzQpYp6c4C/Flj1NeA36fJvgFNyyn8biWeBjpL2beS4DgVej4g3IuJT4M50vxlJewL/DNxf2303RVwFNOvxioiZ6b4DeK6K7eusIXFJEsl/u3vSevmfr8rP3T3AsLR+beNaFhHLa6j2ZZKTwcfpe3gKOC23QrrP/0WSSBusseIq4GvAnRGxOSJWAq+THPcmiau255m6aoTj1ajnr50iKeQZR5Id62LviFibLr8D7J0u7w+8lVNvdVrWmHHVZh+nAE9ExEc5ZYMlvSDpUUm96hlTQ+K6Jr0k/ZmkXWu5TVPERXrb6OvArJzi5jpenYAPck4mufFm26TrP0zrN6alwBBJnSTtRvLt8R/z6gwB3o2I13LKukl6XtJTkoY0cky1ieuC9PN1a+XtEBr381XfuCrlfw6a83g16vmrVePEW3yS/gjsU2DVDyLigbTOD4AKYHp99xMRIanWz+k2UVxjgJtzXi8iGcdkQ3q/9X7gkCaM6zKSD18bkuemJwE/qs2GTXS8fgXMiYi56evmPl71Vpu4qhMRyyRdDzwGbAQWA1vzqo1h+6uEtcABEbFO0kDgfkm9cr+UFDmuKcDVQKT//ifJSbhGTXG8CnwOmvt45dar0/mrkBaTFCLimOrWSxoLnAQMS28f1MW7kvaNiLXp5dV7afkatv+W0CUta8y4qt2HpM4kl8in5uzzo5zlmZJ+JalzRLyfU160uHK+lWyWdBtwSW3eS7HjSre/AigBzs/ZZ3Mer3Ukl+2t0quB3Hgrt1ktqRXQIa2fqSmu2oiIW4Bb0vfxHyTfGCvfVyuS2xADc+pvBjanywslrQC6AwuaIq6IeDcnvl8DD6cvG/z5akhc6eux5H0Omvt40YDzV1U7avF/wPHAy0BJLep25bMdzTewfUfNj9PlE9m+o+a5xo6LJDG/QdJxVtlB2Stn/QTgN3nb7MPff3h4KPBm5eumiAvYN/1XwGTguh3heAHnAU8D7Xaw43U323c0fztd/g7bdzT/Tz0//7OpooMyXf8P6b8HAK8AHfPe21N59UtIO3BJOs7XAF9sqrgqP1/p8r+R9CMA9GL7juY3qENHcyPEVfBzsAMcr0Y9f9X5A7gj/pF0OL1Fckm1OOd/tP2AmTn1ZpBc6m0hybLfTMs7AU8Ar5H06n8xLRfwS5KnHF6s7j9YA+MaAbya7ucHBT4ox+eVXQC8lP4P8ixweFPGBfwpPR5LgTuA9jvC8SK5pF+Rs33l0zTNfbwOIun4fp0kQeyalrdNX7+erj+ojnGdmn6ONwPvAn+oIq65JCezF0i+4ea2cTswIa/sX9LjtZjk1tvJTRkX8Lv087MEeJDtk8QP0uO7HDihieOq6nPQ3MerUc9fHubCzMwyO+PTR2ZmVk9OCmZmlnFSMDOzjJOCmZllnBTMzCzjpGAtmqQNDdj2gnQEyUh/JFhZXuXokpL2lfRwunxU5XJDSZqtakbIzKm3KjfWKur8MWdoCLM6cVKwz7M/A8cAf8krP4FkGIxDgPEkwy5U+h7w6yaJrv5+RzICq1mdOSnYTiH9dn+DkvHmX5Q0Ki3/QjqsxStKxpqfKel0gIh4PiJWFWiuutEl/4XtB9qr3P+hkp5JB0V7WlKPtHyspPvTfa9Kr06+l9Z7VtIXc5r5upLx+JdKOjTdvpOkx5SMoX8zyQ+SKvd5v6SF6brxOe08SDKekVmdOSnYzuI0oBToR/Lt/4b0RH4aydAmPUlGTh1ci7YKji4pqRvwt0jGusn3CjAkIvoDlwP/kbOudxrHIOAa4OO03jPAOTn1douIUpJv+bemZVcA/y8iegH3kQxxUGlcRAwEyoCLJHUCiIi/AbtWvjarixYzIJ5ZDY4EZkTEVpIBwp4iOQkfCdwdEduAdyQ92YB97AuUV7GuA/AbSYeQjO7ZOmfdkxGxHlgv6UPgobT8RaBvTr0ZkMz7IWlPJTN7DeXv4/k/IulvOfUvklQ5UOI/ktzuqhxQ7z2SYRK2G2DPrCa+UjD7rKpGl/yEZLyiQq4mOfn3Bk7Oq5d7ZbEt5/U2tv9ilj/mTJVj0Eg6iuSKaHBE9AOez9tn2zReszpxUrCdxVxglKRdJJWQfMN+jqQz+V/SvoW9SabmrMmDwDlpP8VXgA8jGS78VZJbUYV04O/DEo+t53uo7Ac5Mt3nh8Ac4My0/ASg8qmiDiS3sj6W9CWSUTBJ64lkZNhV9YzDPsecFGxncR/JqJovkIzi+r8j4h2Sua1Xk4wueQfJKJYfAki6SNJqkiuBJWlHLsBMkmGZXyd50ujbABGxEVgh6Z8K7P/HwLWSnqf+t2U3pdtPBb6Zll0FDJX0EsltpDfT8llAK0nLgOtIRn+tNBB4Nv4+25tZrXmUVNvpSWofyaxrnUiuHo5IE0Z92joVGBgRP2zUIBuRpBuBByPiieaOxVoedzTb58HDaadtG+Dq+iYEgIi4rwU81bPUCcHqy1cKZmaWcZ+CmZllnBTMzCzjpGBmZhknBTMzyzgpmJlZ5v8DkpQeAPsEM8MAAAAASUVORK5CYII=\n", - "text/plain": [ - "
    " - ] - }, - "metadata": { - "needs_background": "light" - }, - "output_type": "display_data" - } - ], + "execution_count": 9, + "id": "63d15269", + "metadata": { + "collapsed": false, + "editable": true + }, + "outputs": [], "source": [ "import numpy as np\n", "import pandas as pd\n", @@ -2382,10 +2693,10 @@ "p = Maxpolydegree\n", "I = np.eye(p,p)\n", "# Decide which values of lambda to use\n", - "nlambdas = 1\n", + "nlambdas = 6\n", "MSEOwnRidgePredict = np.zeros(nlambdas)\n", "MSERidgePredict = np.zeros(nlambdas)\n", - "lambdas = np.logspace(-2, 2, nlambdas)\n", + "lambdas = np.logspace(-4, 2, nlambdas)\n", "for i in range(nlambdas):\n", " lmb = lambdas[i]\n", " OwnRidgeBeta = np.linalg.pinv(X_train.T @ X_train+lmb*I) @ X_train.T @ y_train\n", @@ -2421,8 +2732,10 @@ }, { "cell_type": "markdown", - "id": "345ca257", - "metadata": {}, + "id": "62fa350a", + "metadata": { + "editable": true + }, "source": [ "The results here agree when we force **Scikit-Learn**'s Ridge function to include the first column in our design matrix.\n", "We see that the results agree very well. Here we have thus explicitely included the intercept column in the design matrix.\n", @@ -2432,55 +2745,23 @@ }, { "cell_type": "markdown", - "id": "0c591f94", - "metadata": {}, + "id": "d4d6005b", + "metadata": { + "editable": true + }, "source": [ "## Taking out the mean" ] }, { "cell_type": "code", - "execution_count": 3, - "id": "d9dbc988", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Beta values for own Ridge implementation\n", - "[-0.11987476 -0.17941937 -0.06692651 0.02707229 0.07073126 0.07820093\n", - " 0.06661455 0.04763463 0.02786784 0.01059519 -0.00283875 -0.01213556\n", - " -0.01751971 -0.01944576 -0.01844105 -0.0150256 -0.00967471 -0.00280481\n", - " 0.00522883]\n", - "Beta values for Scikit-Learn Ridge implementation\n", - "[-0.11987476 -0.17941937 -0.06692651 0.02707229 0.07073126 0.07820093\n", - " 0.06661455 0.04763463 0.02786784 0.01059519 -0.00283875 -0.01213556\n", - " -0.01751971 -0.01944576 -0.01844105 -0.0150256 -0.00967471 -0.00280481\n", - " 0.00522883]\n", - "Intercept from own implementation:\n", - "1.0475047612229758\n", - "Intercept from Scikit-Learn Ridge implementation\n", - "1.0475047612229833\n", - "MSE values for own Ridge implementation\n", - "4.398879605532181e-05\n", - "MSE values for Scikit-Learn Ridge implementation\n", - "4.398879605534439e-05\n" - ] - }, - { - "data": { - "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYUAAAERCAYAAACU1LsdAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjQuMywgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/MnkTPAAAACXBIWXMAAAsTAAALEwEAmpwYAAAlGElEQVR4nO3deZwU1bn/8c8jIMOiEGBiRNRBoyjLMMiAIkIAQQmMuERvGFdELy54cYle9ZdEXBMVr3JdEzWIWxCjEQkBggsEo1cQlE2WCIqKqCwqAckgA8/vjzpTNs3M9Gw9wwzf9+vVL6rPOXXq6Zqmnj5V3afM3REREQHYp6YDEBGRPYeSgoiIxJQUREQkpqQgIiIxJQUREYkpKYiISKxWJgUzG2dm68xsSRX1N93MvjGzKUnlb5jZgvBYa2aTytlvPTN7L7nfhPpLzWxx6P8fZtY+lO9rZk+EuoVm1idhnfxQvijE3SqU55jZ26GveWbWPZRfl/AalpjZDjNrEequDGXvm9lVSbH9l5ktD3V3J5TfaGYrzWyFmZ2cUL464bXMSyi/2cw+S4hhUHn2YQn7bbyZfZTQZ05l+xSRwN1r3QPoDRwDLKmi/k4ETgGmlNLmReD8YsrHA31KWOca4I8l9Qvsn7A8BJgelkcCT4TlHwLziRJ4fWAd0CrU3Q3cHJZnAD8Ny4OAWcVs7xTg9bDcEVgCNA79vgr8ONT1Dc8bFsUQ/m0PLAQaAm2BVUC9ULe6KK6kbd4MXFvFf//xwJk1/T7UQ4+6+KiVIwV3nw18lVhmZoeHT87zwyf8o8rR32vA5pLqzWx/oB8wqax9mlkbYDDweCnb/VfC0yZA0S8J2wOvhzbrgG+AXMDCo4mZGbA/sLaou/AcoFlCeaJ8YEJYPhqY4+5b3b0Q+DtwRqi7DLjT3bclxABwKvCcu29z94+AlUD3kvdCycIoaoyZvRNGPZdUpB8RqVq1MimU4FHgv9y9K3At8HAV9n0a8FrSQTyVscB/AztLa2RmI81sFdGn/lGheCEwxMzqm1lboCtwsLtvJzpgLyY66LcH/hDWuQoYY2afAvcANyZtpzEwkGjEA9EooZeZtQx1g4CDQ92RoW6Omf3dzLqF8oOATxO6XRPKIEpKM0JSHpH0Mq8IB/5xZvaDUHYRsMnduwHdgP8Mr7Ws7gh93mdmDcuxnoiUok4kBTNrChwP/MnMFgC/Bw4MdWeE8+bJj7+VYxOJn7Axs5OLzmcTnfZ5PDyfE+rzgHXuPj9Vx+7+kLsfDlwP/CoUjyM64M4jSi5vATvMrAFRUugCtAYW8f3B/zLganc/GLia75NFkVOAN939q7DdZcBdRKedpgMLgB2hbX2gBXAccB3wfBiZlOYEdz8G+Ckw0sx6h/JHgMOBHOBz4H9C+UnA+WEfzgFaAkeEEURxf68lCcnpRuAoomTSIuw7EakKNX3+qqIPIItwTYHotMnnleyvD8Wc+wdaARuBjBLWG0/SNQXgt0QH9dXAF8BW4JkU29+H6JNzcXVvEY0KuhGNWIrKewNTw/ImwMKyAf9K6uMl4OxStv8b4PKwPB3om1C3CsgkOhjfmFD+N6BHMX3dTDHXEZL+Zi8CJ1fB+6DYv5seeuhRsUedGCl4dFrnIzM7C8Ainauo+zOJDjoF5YjnRndv4+5ZwFCii7vnJrczsyMSng4GPgjljc2sSVgeABS6+1LgM6C9mWWGdQYAy8LyWuAnYblfUV+hj2ah7uWk7f8w/HsI0fWEP4aqSUQXmzGzI4F9gQ3AZGComTUMp3qOAOaaWRMz2y+0b0I0ClgSnh+YsMnTi8qJEsplYfSDmR1Z9JpTKeozjF5OS+hTRCqpfk0HUBFmNoHoE2IrM1sDjAbOAR4xs18BDYDniM7Nl6W/N4hORzQN/V3k7kWnl4YCd1Zh7LcC89x9MtG59v7AduBr4ILQ7IfA38xsJ1EiOA/A3dea2S3AbDPbDnwMDAvr/Cfwv2ZWHygAEs/rnw7McPdvk8J50cxahu2PdPdvQvk4YJxFX/n9DrjA3R1438yeB5YChWGdHWZ2APBSOMNUH/iju08Pfd0dvjLqRCOnogvKjxONHN4NB/f1RAf4sng2JEYjOu11aRnXE5EUik43iIiI1I3TRyIiUjVq3emjVq1aeVZWVk2HISJSq8yfP3+Du2emalfrkkJWVhbz5s1L3VBERGJm9nFZ2qX99JGVMv+PmQ0zs/UJc9hcnO54RESkZNUxUriS6GuT+5dQP9Hdr6iGOEREJIW0jhTKMv+PiIjsOdI9UhhLNP/PfqW0+VmYEuGfRNM0fJrcIMylMwLgkEMOSUOYIrXP9u3bWbNmDQUFZf5dpewFMjIyaNOmDQ0aNKjQ+mlLConz/1jC/QCS/AWY4O7bwiyZTxL9GncX7v4o0YR35Obm6ocVIsCaNWvYb7/9yMrKIvXUVLI3cHc2btzImjVraNu2PPNLfi+dp496Es30uZro18X9zOyZxAbuvtHD9MxEp5i6pjEekTqloKCAli1bKiFIzMxo2bJlpUaPaUsKZZn/J2lenCF8P4+PiJSBEoIkq+x7otp/p5A0988oMxtCNI/OV3w/j4+IiNSAapnmwt1nuXteWL4pJISi0UQHd+/s7n3dfXl1xCMiVcPMOPfc708AFBYWkpmZSV5eHgBffvkleXl5dO7cmfbt2zNoUHSL7tWrV9OoUSNycnLix1NPPVWtsSfG0L59e84//3y2b98OwLx58xg1alSx62VlZbFhw4ZKbfuJJ56IX/e+++5Lp06dyMnJ4YYbbihzH2PHjmXr1q2ViqM4te4XzSKy52jSpAlLlizh3//+N40aNeKVV17hoIMOiutvuukmBgwYwJVXXgnAokWL4rrDDz+cBQsWVHfIuyiKYceOHQwYMIDnn3+ec845h9zcXHJzc9O23QsvvJALL7wQiJLMzJkzadWqVbn6GDt2LOeeey6NGzeu0tg0IZ6IVMqgQYP461//CsCECRPIz8+P6z7//HPatGkTP8/Ozi5X36+99hpdunShU6dODB8+nG3btvHOO+9wxhnR7cRffvllGjVqxHfffUdBQQGHHXYYAH369OH666+ne/fuHHnkkbzxxhulbqdevXp0796dzz77DIBZs2bFo52NGzdy0kkn0aFDBy6++GISZ5a+7bbbaNeuHSeccAL5+fncc889AKxatYqBAwfStWtXevXqxfLlZTsJMmbMGLp160Z2djajR48G4Ntvv2Xw4MF07tyZjh07MnHiRO6//37Wrl1L37596du3bzn2aGpKCiJ1RJ8+uz8eDncq37q1+Prx46P6DRt2ryuroUOH8txzz1FQUMCiRYs49thj47qRI0dy0UUX0bdvX+644w7Wrl0b161atWqX00fJB+6CggKGDRvGxIkTWbx4MYWFhTzyyCN06dIlHmG88cYbdOzYkXfeeYc5c+bssu3CwkLmzp3L2LFjueWWW0p9DQUFBcyZM4eBAwfuVnfLLbdwwgkn8P7773P66afzySefAPDOO+/w4osvsnDhQqZNm7bLnGwjRozggQceYP78+dxzzz1cfvnlKffjjBkz+OCDD5g7dy4LFixg/vz5zJ49m+nTp9O6dWsWLlzIkiVLGDhwIKNGjaJ169bMnDmTmTNnpuy7PHT6SEQqJTs7m9WrVzNhwoT4mkGRk08+mQ8//JDp06czbdo0unTpwpIl0Y3yUp0+WrFiBW3btuXII48E4IILLuChhx7iqquu4vDDD2fZsmXMnTuXa665htmzZ7Njxw569eoVr180mujatSurV68udhtFiemjjz5i8ODBxY5kZs+ezZ///GcABg8ezA9+8AMA3nzzTU499VQyMjLIyMjglFNOAWDLli289dZbnHXWWXEf27Zt263fZDNmzGDGjBl06dIl7ueDDz6gV69e/OIXv+D6668nLy9vl9eYDkoKInXErFkl1zVuXHp9q1al16cyZMgQrr32WmbNmsXGjRt3qWvRogVnn302Z599Nnl5ecyePZuuXSv3k6TevXszbdo0GjRoQP/+/Rk2bBg7duxgzJgxcZuGDRsC0amhwsLCYvspSkwbNmygZ8+eTJ48mSFDhlQqtp07d9K8efNyXy9xd2688UYuueSS3ereffddpk6dyq9+9StOPPFEbrrppkrFWBqdPhKRShs+fDijR4+mU6dOu5S//vrr8TdkNm/ezKpVq8o8VU27du1YvXo1K1euBODpp5/mJz+JbkPeq1cvxo4dS48ePcjMzGTjxo2sWLGCjh07Vij+Vq1aceedd/Lb3/52t7revXvzxz9Gty+fNm0aX3/9NQA9e/bkL3/5CwUFBWzZsoUpU6KJoPfff3/atm3Ln/70JyA62C9cmPrOwCeffDLjxo1jy5YtAHz22WesW7eOtWvX0rhxY84991yuu+463n33XQD2228/Nm/eXKHXWxqNFESk0tq0aVPsVzjnz5/PFVdcQf369dm5cycXX3wx3bp1Y/Xq1fGpmyLDhw/fpY+MjAyeeOIJzjrrLAoLC+nWrRuXXhrdjvvYY4/lyy+/pHfv3kB0CuuLL76o1A+3TjvtNG6++ebdrm2MHj2a/Px8OnTowPHHHx8ntW7dujFkyBCys7M54IAD6NSpE82aNQPg2Wef5bLLLuP2229n+/btDB06lM6dO5e6/ZNOOolly5bRo0cPAJo2bcozzzzDypUrue6669hnn31o0KABjzzyCBBdtxg4cGB8baGq1Lp7NOfm5rpusiMCy5Yt4+ijj67pMPZqW7ZsoWnTpmzdupXevXvz6KOPcswxx9R0WMW+N8xsvrun/J6tRgoiIhU0YsQIli5dSkFBARdccMEekRAqS0lBRKSCiq411CW60CwiIjElBRERiSkpiIhITElBRERiSgoiUmHpnjp7ypQpdOnSJV7/97//PQA333xzPPlcSbKysujUqRPZ2dn85Cc/4eOPP47rjj/++GLXGTZsGC+88EL5dkKSxYsXx6+pRYsWtG3blpycHPr371/mPiZNmsTSpUsrFUdF6dtHIlJh6Zw6e/v27YwYMYK5c+fSpk0btm3bVuIcRiUpmpJ69OjR3H777Tz22GMAvPXWW+Xqpzw6deoUv65hw4aRl5fHmWeeWa4+Jk2aRF5eHu3bt09DhKXTSEFEKiVdU2dv3ryZwsJCWrZsCURzGbVr165CMfbo0SOeFhuiXwtDNAXFFVdcQbt27ejfvz/r1q2L20ydOpWjjjqKrl27MmrUqHj08+233zJ8+HC6d+9Oly5dePnll8sUw4wZM+jRowfHHHMMZ511VjydxQ033ED79u3Jzs7m2muv5a233mLy5Mlcd9115OTksGrVqgq95orSSEGkjugzvs9uZf/R4T+4vNvlbN2+lUHPDtqtfljOMIblDGPD1g2c+fyun2ZnDZtVpu0OHTqUW2+9lby8PBYtWsTw4cPjqSJGjhzJz3/+cx588EH69+/PhRdeSOvWrQF2m+bigQce2GUG0BYtWjBkyBAOPfRQTjzxRPLy8sjPz2effcr/WXb69Omcdtppu5W/9NJLrFixgqVLl/Lll1/Svn17hg8fTkFBAZdccgmzZ8+mbdu2uyS6O+64g379+jFu3Di++eYbunfvTv/+/WnSpEmJ29+wYQO33347r776Kk2aNOGuu+7i3nvvZeTIkbz00kssX74cM+Obb76hefPmDBkypEIjjKqgpCAilZKuqbMBHn/8cRYvXsyrr77KPffcwyuvvML4optAlEHfvn356quvaNq0Kbfddttu9bNnzyY/P5969erRunVr+vXrB8Dy5cs57LDDaNu2LQD5+fk8+uijQPSJf/LkyfE1jYKCAj755JNSpxx5++23Wbp0KT179gTgu+++o0ePHjRr1oyMjAwuuugi8vLy4tFITVJSEKkjSvtk37hB41LrWzVuVeaRQXHSOXV2p06d6NSpE+eddx5t27YtV1KYOXMmzZs355xzzmH06NHce++9ZV63JO7Oiy++WK5TWe7OgAEDmDBhwm51c+fO5bXXXuOFF17gwQcf5PXXX690jJWhawoiUmnpmDp7y5YtzEq4ycOCBQs49NBDyx1b/fr1GTt2LE899RRfffXVLnW9e/dm4sSJ7Nixg88//zyebbRdu3Z8+OGH8YXtiRMnxuucfPLJPPDAA/FtOd97772UMRx33HG8+eab8TTg3377Lf/85z/ZsmULmzZtYtCgQdx3333xFNvpmha7LJQURKTSSps6Ozc3l+zsbHr06BFPnQ27347z/vvv32Vdd+fuu++mXbt25OTkMHr06F1GCbfffjtt2rSJH6U58MADyc/P56GHHtql/PTTT+eII46gffv2nH/++fG01Y0aNeLhhx+O77O83377xdNi//rXv2b79u1kZ2fToUMHfv3rX6fcP5mZmYwfP578/Px4XyxfvpzNmzeTl5dHdnY2J5xwQjySGTp0KGPGjKFLly7VfqFZU2eL1FKaOju9iqbFdndGjhzJEUccwdVXX13TYZVJZabO1khBRKQYjz32GDk5OXTo0IFNmzYVe5vMukgXmkVEinH11VfXmpFBVdJIQaQWq22nfyX9KvueUFIQqaUyMjLYuHGjEoPE3J2NGzeSkZFR4T50+kiklmrTpg1r1qxh/fr1NR2K7EEyMjJSfhurNEoKIrVUgwYN4l/cilQVnT4SEZGYkoKIiMSUFEREJKakICIiMSUFERGJKSmIiEgs7UnBzOqZ2XtmNqWYuoZmNtHMVprZHDPLSnc8IiJSsuoYKVwJLCuh7iLga3f/MXAfcFc1xCMiIiVIa1IwszbAYODxEpqcCjwZll8ATjQzS2dMIiJSsnSPFMYC/w3sLKH+IOBTAHcvBDYBLZMbmdkIM5tnZvP0k34RkfRJW1IwszxgnbvPr2xf7v6ou+e6e25mZmYVRCciIsVJ50ihJzDEzFYDzwH9zOyZpDafAQcDmFl9oBmwERERqRFpSwrufqO7t3H3LGAo8Lq7n5vUbDJwQVg+M7TRPMAiIjWk2mdJNbNbgXnuPhn4A/C0ma0EviJKHiIiUkOqJSm4+yxgVli+KaG8ADirOmIQEZHU9ItmERGJKSmIiEhMSUFERGJKCiIiElNSEBGRmJKCiIjElBRERCSmpCAiIjElBRERiSkpiIhITElBRERiSgoiIhJTUhARkZiSgoiIxJQUREQkpqQgIiIxJQUREYkpKYiISExJQUREYkoKIiISU1IQEZGYkoKIiMSUFEREJKakICIiMSUFERGJKSmIiEhMSUFERGJKCiIiElNSEBGRmJKCiIjElBRERCSmpCAiIjElBRERiSkpiIhILG1JwcwyzGyumS00s/fN7JZi2gwzs/VmtiA8Lk5XPCIiklr9NPa9Dejn7lvMrAHwDzOb5u5vJ7Wb6O5XpDEOEREpo7QlBXd3YEt42iA8PF3bExGRykvrNQUzq2dmC4B1wCvuPqeYZj8zs0Vm9oKZHVxCPyPMbJ6ZzVu/fn06QxYR2aulNSm4+w53zwHaAN3NrGNSk78AWe6eDbwCPFlCP4+6e66752ZmZqYzZBGRvVq1fPvI3b8BZgIDk8o3uvu28PRxoGt1xCMiIsVL57ePMs2seVhuBAwAlie1OTDh6RBgWbriERGR1NL57aMDgSfNrB5R8nne3aeY2a3APHefDIwysyFAIfAVMCyN8YiISAoWfUmo9sjNzfV58+bVdBgiIrWKmc1399xU7fSLZhERiSkpiIhITElBRERipSYFMzs3YblnUp2mphARqWNSjRSuSVh+IKlueBXHIiIiNSxVUrASlot7LiIitVyqpOAlLBf3XEREarlUP147yswWEY0KDg/LhOeHpTUyERGpdqmSwtHVEoWIiOwRSk0K7v5x4nMzawn0Bj5x9/npDExERKpfqq+kTima7jpMXreE6FtHT5vZVekPT0REqlOqC81t3X1JWL6Q6EY5pwDHoq+kiojUOamSwvaE5ROBqQDuvhnYma6gRESkZqS60Pypmf0XsAY4BpgO8f0RGqQ5NhERqWapRgoXAR2I7nPw83AHNYDjgCfSF5aIiNSEVN8+WgdcWkz5TKLba4qISB1SalIws8ml1bv7kKoNR0REalKqawo9gE+BCcAcNN+RiEidliop/AgYAOQDZwN/BSa4+/vpDkxERKpfqRea3X2Hu0939wuILi6vBGbpXgoiInVTqpECZtYQGEw0WsgC7gdeSm9YIiJSE1JdaH4K6Ej0o7VbEn7dLCIidVCqkcK5wLfAlcAos/g6swHu7vunMTYREalmqX6nkOrHbSIiUofooC8iIjElBRERiSkpiIhITElBRERiSgoiIhJTUhARkZiSgoiIxJQUREQkpqQgIiIxJQUREYkpKYiISCxtScHMMsxsrpktNLP3zeyWYto0NLOJZrbSzOaYWVa64hERkdTSOVLYBvRz985ADjDQzI5LanMR8LW7/xi4D7grjfGIiEgKaUsKHtkSnjYID09qdirwZFh+ATjREubnFhGR6pXWawpmVs/MFgDrgFfcfU5Sk4OATwHcvRDYBLQspp8RZjbPzOatX78+nSGLiOzV0poUwj2ec4A2QHcz61jBfh5191x3z83MzKzSGEVE5HvV8u0jd/8GmAkMTKr6DDgYwMzqA82AjdURk4iI7C6d3z7KNLPmYbkRMABYntRsMnBBWD4TeN3dk687iIhINUl1j+bKOBB40szqESWf5919ipndCsxz98nAH4CnzWwl8BUwNI3xiIhICmlLCu6+COhSTPlNCcsFwFnpikFERMpHv2gWEZGYkoKIiMSUFEREJKakICIiMSUFERGJKSmIiEhMSUFERGJKCiIiElNSEBGRmJKCiIjElBRERCSmpCAiIjElBRERiSkpiIhITElBRERiSgoiIhJTUhARkZiSgoiIxJQUREQkpqQgIiIxJQUREYkpKYiISExJQUREYkoKIiISU1IQEZGYkoKIiMSUFEREJKakICIiMSUFERGJKSmIiEhMSUFERGJKCiIiElNSEBGRmJKCiIjElBRERCSWtqRgZgeb2UwzW2pm75vZlcW06WNmm8xsQXjclK54REQktfpp7LsQ+IW7v2tm+wHzzewVd1+a1O4Nd89LYxwiIlJGaRspuPvn7v5uWN4MLAMOStf2RESk8qrlmoKZZQFdgDnFVPcws4VmNs3MOpSw/ggzm2dm89avX5/OUEVE9mppTwpm1hR4EbjK3f+VVP0ucKi7dwYeACYV14e7P+ruue6em5mZmdZ4RUT2ZmlNCmbWgCghPOvuf06ud/d/ufuWsDwVaGBmrdIZk4iIlCyd3z4y4A/AMne/t4Q2PwrtMLPuIZ6N6YpJRERKl85vH/UEzgMWm9mCUPb/gEMA3P13wJnAZWZWCPwbGOrunsaYRESkFGlLCu7+D8BStHkQeDBdMYiISPnoF80iIhJTUhARkZiSgoiIxJQUREQkpqQgIiIxJQUREYkpKYiISExJQUREYkoKIiISU1IQEZGYkoKIiMSUFEREJKakICIiMSUFERGJKSmIiEhMSUFERGJKCiIiElNSEBGRmJKCiIjElBRERCSmpCAiIjElBRERiSkpiIhITElBRERiSgoiIhJTUhARkZiSgoiIxJQUREQkpqQgIiIxJQUREYkpKYiISExJQUREYubuNR1DuZjZeuDjCq7eCthQheFUlT01LthzY1Nc5aO4yqcuxnWou2emalTrkkJlmNk8d8+t6TiS7alxwZ4bm+IqH8VVPntzXDp9JCIiMSUFERGJ7W1J4dGaDqAEe2pcsOfGprjKR3GVz14b1151TUFEREq3t40URESkFEoKIiISqxNJwczGmNlyM1tkZi+ZWfMS2o0zs3VmtiSpvIWZvWJmH4R/fxDKzczuN7OVoe9j0hTXQDNbEbZzQ0L5G2a2IDzWmtmkUN7HzDYl1N1UzXGNN7OPErafE8pren89G8qXhL91g1Be0/urrZnNCeUTzWzfUN4wPF8Z6rPKGddZZva+me00sxK/pmhmV4Z98r6ZXZVQPjFhn6w2swWhPMvM/p1Q97tqjutmM/ssYfuDEupuDPtrhZmdXM1xFfs+2AP2V9Uev9y91j+Ak4D6Yfku4K4S2vUGjgGWJJXfDdwQlm8oWh8YBEwDDDgOmFPVcQH1gFXAYcC+wEKgfTHtXgTOD8t9gCnp3F+lxQWMB84sZp0a3V9h+xYeE4DL9pD99TwwNCz/LiGuy4HfheWhwMRyxnU00A6YBeSW0KYjsARoDNQHXgV+XEy7/wFuCstZyf9HqjMu4Gbg2mLWaR/2a0Ogbdjf9aoxrmLfB3vA/qrS41edGCm4+wx3LwxP3wbalNBuNvBVMVWnAk+G5SeB0xLKn/LI20BzMzuwiuPqDqx09w/d/TvgubDdmJntD/QDJpV129URVzFqdH+5+9SwbQfmlrB+uVUmLjMzor/dC6Fd8vur6H33AnBiaF/WuJa5+4oUzY4mOhhsDa/h78AZiQ3CNv+DKJFWWlXFVYxTgefcfZu7fwSsJNrv1RJXWY8z5VUF+6tKj191IikkGU6UHcvjAHf/PCx/ARwQlg8CPk1otyaUVWVcZdnGacBr7v6vhLIeZrbQzKaZWYcKxlSZuO4IQ9L7zKxhGdepjrgIp43OA6YnFNfU/moJfJNwMEmMN14n1G8K7avSEqCXmbU0s8ZEnx4PTmrTC/jS3T9IKGtrZu+Z2d/NrFcVx1SWuK4I769xRadDqNr3V0XjKpL8PqjJ/VWlx6/6VRNv+pnZq8CPiqn6pbu/HNr8EigEnq3odtzdzazM39OtprjygccTnr9LNI/JlnC+dRJwRDXGdSPRm29fou9NXw/cWpYVq2l/PQzMdvc3wvOa3l8VVpa4SuPuy8zsLmAG8C2wANiR1CyfXUcJnwOHuPtGM+sKTDKzDokfStIc1yPAbYCHf/+H6CCcUnXsr2LeBzW9vxLblev4VZxakxTcvX9p9WY2DMgDTgynD8rjSzM70N0/D8OrdaH8M3b9lNAmlFVlXKVuw8xaEQ2RT0/Y5r8Slqea2cNm1srdNySUpy2uhE8l28zsCeDasryWdMcV1h8NZAKXJGyzJvfXRqJhe/0wGkiMt2idNWZWH2gW2sdSxVUW7v4H4A/hdfyG6BNj0euqT3QaomtC+23AtrA838xWAUcC86ojLnf/MiG+x4Ap4Wml31+ViSs8H0bS+6Cm9xeVOH6VtKFa/wAGAkuBzDK0zWL3C81j2PVCzd1heTC7XqiZW9VxESXmD4kunBVdoOyQUH8p8GTSOj/i+x8edgc+KXpeHXEBB4Z/DRgL3Lkn7C/gYuAtoNEetr/+xK4Xmi8PyyPZ9ULz8xV8/8+ihAuUof6H4d9DgOVA86TX9vek9pmEC7hEF84/A1pUV1xF76+wfDXRdQSADux6oflDynGhuQriKvZ9sAfsryo9fpX7DbgnPoguOH1KNKRakPAfrTUwNaHdBKKh3naiLHtRKG8JvAZ8QHRVv0UoN+Ahom85LC7tD1bJuAYB/wzb+WUxb5SBSWVXAO+H/yBvA8dXZ1zA62F/LAGeAZruCfuLaEi/KmH9om/T1PT+OozowvdKogTRMJRnhOcrQ/1h5Yzr9PA+3gZ8CfythLjeIDqYLST6hJvYx3jg0qSyn4X9tYDo1Nsp1RkX8HR4/ywCJrNrkvhl2L8rgJ9Wc1wlvQ9qen9V6fFL01yIiEisLn77SEREKkhJQUREYkoKIiISU1IQEZGYkoKIiMSUFKRWM7MtlVj3ijCDpIcfCRaVlzi7pJkdaGZTwnKfouXKMrNZVsoMmQntVifGWkKbVxOmhhApFyUF2Zu9CfQHPk4q/ynRNBhHACOIpl0ocg3wWLVEV3FPE83AKlJuSgpSJ4RP92Msmm9+sZn9PJTvE6a1WG7RXPNTzexMAHd/z91XF9NdabNL/oxdJ9or2n53M/u/MCnaW2bWLpQPM7NJYdurw+jkmtDubTNrkdDNeRbNx7/EzLqH9Vua2QyL5tB/nOgHSUXbnGRm80PdiIR+JhPNZyRSbkoKUlecAeQAnYk+/Y8JB/IziKY2aU80c2qPMvRV7OySZtYW+NqjuW6SLQd6uXsX4CbgNwl1HUMc3YA7gK2h3f8B5ye0a+zuOUSf8seFstHAP9y9A/AS0RQHRYa7e1cgFxhlZi0B3P1roGHRc5HyqDUT4omkcAIwwd13EE0Q9neig/AJwJ/cfSfwhZnNrMQ2DgTWl1DXDHjSzI4gmt2zQULdTHffDGw2s03AX0L5YiA7od0EiO77YWb7W3Rnr958P5//X83s64T2o8ysaKLEg4lOdxVNqLeOaJqEXSbYE0lFIwWR3ZU0u+S/ieYrKs5tRAf/jsApSe0SRxY7E57vZNcPZslzzpQ4B42Z9SEaEfVw987Ae0nbzAjxipSLkoLUFW8APzezemaWSfQJey7RxeSfhWsLBxDdmjOVycD54TrFccAmj6YL/yfRqajiNOP7aYmHVfA1FF0HOSFscxMwGzg7lP8UKPpWUTOiU1lbzewoolkwCe2MaGbY1RWMQ/ZiSgpSV7xENKvmQqJZXP/b3b8gurf1GqLZJZ8hmsVyE4CZjTKzNUQjgUXhQi7AVKJpmVcSfdPocgB3/xZYZWY/Lmb7dwO/NbP3qPhp2YKw/u+Ai0LZLUBvM3uf6DTSJ6F8OlDfzJYBdxLN/lqkK/C2f3+3N5Ey0yypUueZWVOP7rrWkmj00DMkjIr0dTrQ1d1/VaVBViEz+19gsru/VtOxSO2jC82yN5gSLtruC9xW0YQA4O4v1YJv9SxRQpCK0khBRERiuqYgIiIxJQUREYkpKYiISExJQUREYkoKIiIS+//pVaNwjKaKmwAAAABJRU5ErkJggg==\n", - "text/plain": [ - "
    " - ] - }, - "metadata": { - "needs_background": "light" - }, - "output_type": "display_data" - } - ], + "execution_count": 10, + "id": "3bac9cff", + "metadata": { + "collapsed": false, + "editable": true + }, + "outputs": [], "source": [ "import numpy as np\n", "import pandas as pd\n", @@ -2522,11 +2803,11 @@ "p = Maxpolydegree-1\n", "I = np.eye(p,p)\n", "# Decide which values of lambda to use\n", - "nlambdas = 1\n", + "nlambdas = 6\n", "MSEOwnRidgePredict = np.zeros(nlambdas)\n", "MSERidgePredict = np.zeros(nlambdas)\n", "\n", - "lambdas = np.logspace(-2, 2, nlambdas)\n", + "lambdas = np.logspace(-4, 2, nlambdas)\n", "for i in range(nlambdas):\n", " lmb = lambdas[i]\n", " OwnRidgeBeta = np.linalg.pinv(X_train_scaled.T @ X_train_scaled+lmb*I) @ X_train_scaled.T @ (y_train_scaled)\n", @@ -2564,8 +2845,10 @@ }, { "cell_type": "markdown", - "id": "9cd41fb5", - "metadata": {}, + "id": "9431aefa", + "metadata": { + "editable": true + }, "source": [ "We see here, when compared to the code which includes explicitely the\n", "intercept column, that our MSE value is actually smaller. This is\n", @@ -2579,16 +2862,20 @@ }, { "cell_type": "markdown", - "id": "85a6e4f5", - "metadata": {}, + "id": "a06f0a20", + "metadata": { + "editable": true + }, "source": [ "## Friday September 9" ] }, { "cell_type": "markdown", - "id": "a2a65ad6", - "metadata": {}, + "id": "491bae26", + "metadata": { + "editable": true + }, "source": [ "## Linking the regression analysis with a statistical interpretation\n", "\n", @@ -2614,8 +2901,10 @@ }, { "cell_type": "markdown", - "id": "e8d65034", - "metadata": {}, + "id": "a2c774e7", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\begin{align*} \n", @@ -2628,8 +2917,10 @@ }, { "cell_type": "markdown", - "id": "6fa8ce61", - "metadata": {}, + "id": "fdf93799", + "metadata": { + "editable": true + }, "source": [ "The randomness of $\\varepsilon_i$ implies that\n", "$\\mathbf{y}_i$ is also a random variable. In particular,\n", @@ -2645,8 +2936,10 @@ }, { "cell_type": "markdown", - "id": "e503e716", - "metadata": {}, + "id": "61783020", + "metadata": { + "editable": true + }, "source": [ "## Assumptions made\n", "\n", @@ -2657,8 +2950,10 @@ }, { "cell_type": "markdown", - "id": "6590f6c6", - "metadata": {}, + "id": "579a3e8e", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\boldsymbol{y} = f(\\boldsymbol{x})+\\boldsymbol{\\varepsilon}\n", @@ -2667,8 +2962,10 @@ }, { "cell_type": "markdown", - "id": "36c66ae3", - "metadata": {}, + "id": "8919ec8d", + "metadata": { + "editable": true + }, "source": [ "We approximate this function with our model from the solution of the linear regression equations, that is our\n", "function $f$ is approximated by $\\boldsymbol{\\tilde{y}}$ where we want to minimize $(\\boldsymbol{y}-\\boldsymbol{\\tilde{y}})^2$, our MSE, with" @@ -2676,8 +2973,10 @@ }, { "cell_type": "markdown", - "id": "c794166a", - "metadata": {}, + "id": "8e6d1273", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\boldsymbol{\\tilde{y}} = \\boldsymbol{X}\\boldsymbol{\\beta}.\n", @@ -2686,8 +2985,10 @@ }, { "cell_type": "markdown", - "id": "23801ca2", - "metadata": {}, + "id": "9b397651", + "metadata": { + "editable": true + }, "source": [ "## Expectation value and variance\n", "\n", @@ -2696,8 +2997,10 @@ }, { "cell_type": "markdown", - "id": "859a80be", - "metadata": {}, + "id": "b87296ba", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\begin{align*} \n", @@ -2710,8 +3013,10 @@ }, { "cell_type": "markdown", - "id": "8df27f31", - "metadata": {}, + "id": "075a29e0", + "metadata": { + "editable": true + }, "source": [ "while\n", "its variance is" @@ -2719,8 +3024,10 @@ }, { "cell_type": "markdown", - "id": "efab0a0d", - "metadata": {}, + "id": "ab22e3a9", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\begin{align*} \\mbox{Var}(y_i) & = \\mathbb{E} \\{ [y_i\n", @@ -2740,8 +3047,10 @@ }, { "cell_type": "markdown", - "id": "c41b6e91", - "metadata": {}, + "id": "8064c896", + "metadata": { + "editable": true + }, "source": [ "Hence, $y_i \\sim \\mathcal{N}( \\mathbf{X}_{i, \\ast} \\, \\boldsymbol{\\beta}, \\sigma^2)$, that is $\\boldsymbol{y}$ follows a normal distribution with \n", "mean value $\\boldsymbol{X}\\boldsymbol{\\beta}$ and variance $\\sigma^2$ (not be confused with the singular values of the SVD)." @@ -2749,8 +3058,10 @@ }, { "cell_type": "markdown", - "id": "2f7a7871", - "metadata": {}, + "id": "9ecea4d2", + "metadata": { + "editable": true + }, "source": [ "## Expectation value and variance for $\\boldsymbol{\\beta}$\n", "\n", @@ -2759,8 +3070,10 @@ }, { "cell_type": "markdown", - "id": "0008f742", - "metadata": {}, + "id": "ddbbbf7e", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\mathbb{E}(\\boldsymbol{\\hat{\\beta}}) = \\mathbb{E}[ (\\mathbf{X}^{\\top} \\mathbf{X})^{-1}\\mathbf{X}^{T} \\mathbf{Y}]=(\\mathbf{X}^{T} \\mathbf{X})^{-1}\\mathbf{X}^{T} \\mathbb{E}[ \\mathbf{Y}]=(\\mathbf{X}^{T} \\mathbf{X})^{-1} \\mathbf{X}^{T}\\mathbf{X}\\boldsymbol{\\beta}=\\boldsymbol{\\beta}.\n", @@ -2769,8 +3082,10 @@ }, { "cell_type": "markdown", - "id": "72111c95", - "metadata": {}, + "id": "2d2e304c", + "metadata": { + "editable": true + }, "source": [ "This means that the estimator of the regression parameters is unbiased.\n", "\n", @@ -2781,8 +3096,10 @@ }, { "cell_type": "markdown", - "id": "52790c16", - "metadata": {}, + "id": "8902a5b2", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\begin{eqnarray*}\n", @@ -2810,8 +3127,10 @@ }, { "cell_type": "markdown", - "id": "864688c4", - "metadata": {}, + "id": "33a2838d", + "metadata": { + "editable": true + }, "source": [ "where we have used that $\\mathbb{E} (\\mathbf{Y} \\mathbf{Y}^{T}) =\n", "\\mathbf{X} \\, \\boldsymbol{\\beta} \\, \\boldsymbol{\\beta}^{T} \\, \\mathbf{X}^{T} +\n", @@ -2830,8 +3149,10 @@ }, { "cell_type": "markdown", - "id": "37738b35", - "metadata": {}, + "id": "85db379b", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\mathbb{E} \\big[ \\boldsymbol{\\beta}^{\\mathrm{Ridge}} \\big]=(\\mathbf{X}^{T} \\mathbf{X} + \\lambda \\mathbf{I}_{pp})^{-1} (\\mathbf{X}^{\\top} \\mathbf{X})\\boldsymbol{\\beta}^{\\mathrm{OLS}}.\n", @@ -2840,8 +3161,10 @@ }, { "cell_type": "markdown", - "id": "aeff32a4", - "metadata": {}, + "id": "18c22f28", + "metadata": { + "editable": true + }, "source": [ "We see clearly that \n", "$\\mathbb{E} \\big[ \\boldsymbol{\\beta}^{\\mathrm{Ridge}} \\big] \\not= \\boldsymbol{\\beta}^{\\mathrm{OLS}}$ for any $\\lambda > 0$. We say then that the ridge estimator is biased.\n", @@ -2851,8 +3174,10 @@ }, { "cell_type": "markdown", - "id": "3321cbc4", - "metadata": {}, + "id": "8bc69031", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\mbox{Var}[\\boldsymbol{\\beta}^{\\mathrm{Ridge}}]=\\sigma^2[ \\mathbf{X}^{T} \\mathbf{X} + \\lambda \\mathbf{I} ]^{-1} \\mathbf{X}^{T} \\mathbf{X} \\{ [ \\mathbf{X}^{\\top} \\mathbf{X} + \\lambda \\mathbf{I} ]^{-1}\\}^{T},\n", @@ -2861,8 +3186,10 @@ }, { "cell_type": "markdown", - "id": "486213d7", - "metadata": {}, + "id": "d6544f85", + "metadata": { + "editable": true + }, "source": [ "and it is easy to see that if the parameter $\\lambda$ goes to infinity then the variance of Ridge parameters $\\boldsymbol{\\beta}$ goes to zero. \n", "\n", @@ -2871,8 +3198,10 @@ }, { "cell_type": "markdown", - "id": "ca94083c", - "metadata": {}, + "id": "2457b453", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\mbox{Var}[\\boldsymbol{\\beta}^{\\mathrm{OLS}}]-\\mbox{Var}(\\boldsymbol{\\beta}^{\\mathrm{Ridge}})=\\sigma^2 [ \\mathbf{X}^{T} \\mathbf{X} + \\lambda \\mathbf{I} ]^{-1}[ 2\\lambda\\mathbf{I} + \\lambda^2 (\\mathbf{X}^{T} \\mathbf{X})^{-1} ] \\{ [ \\mathbf{X}^{T} \\mathbf{X} + \\lambda \\mathbf{I} ]^{-1}\\}^{T}.\n", @@ -2881,8 +3210,10 @@ }, { "cell_type": "markdown", - "id": "167df200", - "metadata": {}, + "id": "b051314b", + "metadata": { + "editable": true + }, "source": [ "The difference is non-negative definite since each component of the\n", "matrix product is non-negative definite. \n", @@ -2891,8 +3222,10 @@ }, { "cell_type": "markdown", - "id": "b8c4a1e4", - "metadata": {}, + "id": "cfda23b3", + "metadata": { + "editable": true + }, "source": [ "## Deriving OLS from a probability distribution\n", "\n", @@ -2912,8 +3245,10 @@ }, { "cell_type": "markdown", - "id": "148b3cdb", - "metadata": {}, + "id": "a23408ee", + "metadata": { + "editable": true + }, "source": [ "$$\n", "y_i\\sim \\mathcal{N}(\\boldsymbol{X}_{i,*}\\boldsymbol{\\beta}, \\sigma^2)=\\frac{1}{\\sqrt{2\\pi\\sigma^2}}\\exp{\\left[-\\frac{(y_i-\\boldsymbol{X}_{i,*}\\boldsymbol{\\beta})^2}{2\\sigma^2}\\right]}.\n", @@ -2922,8 +3257,10 @@ }, { "cell_type": "markdown", - "id": "686d7fa3", - "metadata": {}, + "id": "202e37fb", + "metadata": { + "editable": true + }, "source": [ "## Independent and Identically Distrubuted (iid)\n", "\n", @@ -2933,8 +3270,10 @@ }, { "cell_type": "markdown", - "id": "17caa70f", - "metadata": {}, + "id": "dbcf24e6", + "metadata": { + "editable": true + }, "source": [ "$$\n", "p(y_i, \\boldsymbol{X}\\vert\\boldsymbol{\\beta})=\\frac{1}{\\sqrt{2\\pi\\sigma^2}}\\exp{\\left[-\\frac{(y_i-\\boldsymbol{X}_{i,*}\\boldsymbol{\\beta})^2}{2\\sigma^2}\\right]},\n", @@ -2943,8 +3282,10 @@ }, { "cell_type": "markdown", - "id": "011cc0f0", - "metadata": {}, + "id": "06ceed52", + "metadata": { + "editable": true + }, "source": [ "which reads as finding the likelihood of an event $y_i$ with the input variables $\\boldsymbol{X}$ given the parameters (to be determined) $\\boldsymbol{\\beta}$.\n", "\n", @@ -2953,8 +3294,10 @@ }, { "cell_type": "markdown", - "id": "fd47caec", - "metadata": {}, + "id": "a7fb86c6", + "metadata": { + "editable": true + }, "source": [ "$$\n", "p(\\boldsymbol{y},\\boldsymbol{X}\\vert\\boldsymbol{\\beta})=\\prod_{i=0}^{n-1}\\frac{1}{\\sqrt{2\\pi\\sigma^2}}\\exp{\\left[-\\frac{(y_i-\\boldsymbol{X}_{i,*}\\boldsymbol{\\beta})^2}{2\\sigma^2}\\right]}=\\prod_{i=0}^{n-1}p(y_i,\\boldsymbol{X}\\vert\\boldsymbol{\\beta}).\n", @@ -2963,8 +3306,10 @@ }, { "cell_type": "markdown", - "id": "be92a8b7", - "metadata": {}, + "id": "05f05891", + "metadata": { + "editable": true + }, "source": [ "We will write this in a more compact form reserving $\\boldsymbol{D}$ for the domain of events, including the ouputs (targets) and the inputs. That is\n", "in case we have a simple one-dimensional input and output case" @@ -2972,8 +3317,10 @@ }, { "cell_type": "markdown", - "id": "7af51d2b", - "metadata": {}, + "id": "b94d1190", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\boldsymbol{D}=[(x_0,y_0), (x_1,y_1),\\dots, (x_{n-1},y_{n-1})].\n", @@ -2982,8 +3329,10 @@ }, { "cell_type": "markdown", - "id": "856dff7d", - "metadata": {}, + "id": "d4c0ad8e", + "metadata": { + "editable": true + }, "source": [ "In the more general case the various inputs should be replaced by the possible features represented by the input data set $\\boldsymbol{X}$. \n", "We can now rewrite the above probability as" @@ -2991,8 +3340,10 @@ }, { "cell_type": "markdown", - "id": "5a446379", - "metadata": {}, + "id": "65a46e42", + "metadata": { + "editable": true + }, "source": [ "$$\n", "p(\\boldsymbol{D}\\vert\\boldsymbol{\\beta})=\\prod_{i=0}^{n-1}\\frac{1}{\\sqrt{2\\pi\\sigma^2}}\\exp{\\left[-\\frac{(y_i-\\boldsymbol{X}_{i,*}\\boldsymbol{\\beta})^2}{2\\sigma^2}\\right]}.\n", @@ -3001,16 +3352,20 @@ }, { "cell_type": "markdown", - "id": "4204c439", - "metadata": {}, + "id": "679d99d2", + "metadata": { + "editable": true + }, "source": [ "It is a conditional probability (see below) and reads as the likelihood of a domain of events $\\boldsymbol{D}$ given a set of parameters $\\boldsymbol{\\beta}$." ] }, { "cell_type": "markdown", - "id": "d3bc6574", - "metadata": {}, + "id": "d4362fd5", + "metadata": { + "editable": true + }, "source": [ "## Maximum Likelihood Estimation (MLE)\n", "\n", @@ -3038,8 +3393,10 @@ }, { "cell_type": "markdown", - "id": "5605b443", - "metadata": {}, + "id": "9e242cda", + "metadata": { + "editable": true + }, "source": [ "## A new Cost Function\n", "\n", @@ -3048,8 +3405,10 @@ }, { "cell_type": "markdown", - "id": "7eb23f48", - "metadata": {}, + "id": "a7543db7", + "metadata": { + "editable": true + }, "source": [ "$$\n", "C(\\boldsymbol{\\beta}=-\\log{\\prod_{i=0}^{n-1}p(y_i,\\boldsymbol{X}\\vert\\boldsymbol{\\beta})}=-\\sum_{i=0}^{n-1}\\log{p(y_i,\\boldsymbol{X}\\vert\\boldsymbol{\\beta})},\n", @@ -3058,16 +3417,20 @@ }, { "cell_type": "markdown", - "id": "6167023f", - "metadata": {}, + "id": "fcc3cb78", + "metadata": { + "editable": true + }, "source": [ "which becomes" ] }, { "cell_type": "markdown", - "id": "a4443fe7", - "metadata": {}, + "id": "f76e3a84", + "metadata": { + "editable": true + }, "source": [ "$$\n", "C(\\boldsymbol{\\beta}=\\frac{n}{2}\\log{2\\pi\\sigma^2}+\\frac{\\vert\\vert (\\boldsymbol{y}-\\boldsymbol{X}\\boldsymbol{\\beta})\\vert\\vert_2^2}{2\\sigma^2}.\n", @@ -3076,16 +3439,20 @@ }, { "cell_type": "markdown", - "id": "358b1b20", - "metadata": {}, + "id": "aaacd43a", + "metadata": { + "editable": true + }, "source": [ "Taking the derivative of the *new* cost function with respect to the parameters $\\beta$ we recognize our familiar OLS equation, namely" ] }, { "cell_type": "markdown", - "id": "50135271", - "metadata": {}, + "id": "44194843", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\boldsymbol{X}^T\\left(\\boldsymbol{y}-\\boldsymbol{X}\\boldsymbol{\\beta}\\right) =0,\n", @@ -3094,16 +3461,20 @@ }, { "cell_type": "markdown", - "id": "4f04c33f", - "metadata": {}, + "id": "a1203a00", + "metadata": { + "editable": true + }, "source": [ "which leads to the well-known OLS equation for the optimal paramters $\\beta$" ] }, { "cell_type": "markdown", - "id": "9adef013", - "metadata": {}, + "id": "cfb9ea5c", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\hat{\\boldsymbol{\\beta}}^{\\mathrm{OLS}}=\\left(\\boldsymbol{X}^T\\boldsymbol{X}\\right)^{-1}\\boldsymbol{X}^T\\boldsymbol{y}!\n", @@ -3112,16 +3483,20 @@ }, { "cell_type": "markdown", - "id": "88521aab", - "metadata": {}, + "id": "5eb9a0ea", + "metadata": { + "editable": true + }, "source": [ "Before we make a similar analysis for Ridge and Lasso regression, we need a short reminder on statistics." ] }, { "cell_type": "markdown", - "id": "48e28fb9", - "metadata": {}, + "id": "147ba03e", + "metadata": { + "editable": true + }, "source": [ "## More basic Statistics and Bayes' theorem\n", "\n", @@ -3138,8 +3513,10 @@ }, { "cell_type": "markdown", - "id": "d75b9c96", - "metadata": {}, + "id": "15b862d7", + "metadata": { + "editable": true + }, "source": [ "$$\n", "p(X \\cup Y)= p(X)+p(Y)-p(X \\cap Y).\n", @@ -3148,16 +3525,20 @@ }, { "cell_type": "markdown", - "id": "597adf34", - "metadata": {}, + "id": "c0ed74ec", + "metadata": { + "editable": true + }, "source": [ "**The product rule (aka joint probability) is given by.**" ] }, { "cell_type": "markdown", - "id": "ae73334c", - "metadata": {}, + "id": "1e5c85a3", + "metadata": { + "editable": true + }, "source": [ "$$\n", "p(X \\cup Y)= p(X,Y)= p(X\\vert Y)p(Y)=p(Y\\vert X)p(X),\n", @@ -3166,8 +3547,10 @@ }, { "cell_type": "markdown", - "id": "6b91371c", - "metadata": {}, + "id": "eb1ad707", + "metadata": { + "editable": true + }, "source": [ "where we read $p(X\\vert Y)$ as the likelihood of obtaining $X$ given $Y$.\n", "\n", @@ -3176,8 +3559,10 @@ }, { "cell_type": "markdown", - "id": "60c017df", - "metadata": {}, + "id": "f5862c9a", + "metadata": { + "editable": true + }, "source": [ "## Marginal Probability\n", "\n", @@ -3186,8 +3571,10 @@ }, { "cell_type": "markdown", - "id": "bfc5883c", - "metadata": {}, + "id": "a93994dd", + "metadata": { + "editable": true + }, "source": [ "$$\n", "p(X)=\\sum_{i=0}^{n-1}p(X,Y=y_i)=\\sum_{i=0}^{n-1}p(X\\vert Y=y_i)p(Y=y_i)=\\sum_{i=0}^{n-1}p(X\\vert y_i)p(y_i).\n", @@ -3196,8 +3583,10 @@ }, { "cell_type": "markdown", - "id": "c5227ac5", - "metadata": {}, + "id": "48362ea4", + "metadata": { + "editable": true + }, "source": [ "## Conditional Probability\n", "\n", @@ -3206,8 +3595,10 @@ }, { "cell_type": "markdown", - "id": "6e13d49f", - "metadata": {}, + "id": "a736264f", + "metadata": { + "editable": true + }, "source": [ "$$\n", "p(X\\vert Y)= \\frac{p(X,Y)}{p(Y)}=\\frac{p(X,Y)}{\\sum_{i=0}^{n-1}p(Y\\vert X=x_i)p(x_i)}.\n", @@ -3216,8 +3607,10 @@ }, { "cell_type": "markdown", - "id": "9f576b4b", - "metadata": {}, + "id": "2ca55904", + "metadata": { + "editable": true + }, "source": [ "## Bayes' Theorem\n", "\n", @@ -3226,8 +3619,10 @@ }, { "cell_type": "markdown", - "id": "79f8fe23", - "metadata": {}, + "id": "3232736a", + "metadata": { + "editable": true + }, "source": [ "$$\n", "p(X\\vert Y)= \\frac{p(X,Y)}{p(Y)},\n", @@ -3236,16 +3631,20 @@ }, { "cell_type": "markdown", - "id": "b5b3d792", - "metadata": {}, + "id": "a05c0eb9", + "metadata": { + "editable": true + }, "source": [ "which we can rewrite as" ] }, { "cell_type": "markdown", - "id": "1fff2eb5", - "metadata": {}, + "id": "2d8a77dc", + "metadata": { + "editable": true + }, "source": [ "$$\n", "p(X\\vert Y)= \\frac{p(X,Y)}{\\sum_{i=0}^{n-1}p(Y\\vert X=x_i)p(x_i)}=\\frac{p(Y\\vert X)p(X)}{\\sum_{i=0}^{n-1}p(Y\\vert X=x_i)p(x_i)},\n", @@ -3254,16 +3653,20 @@ }, { "cell_type": "markdown", - "id": "ebfbf1a4", - "metadata": {}, + "id": "c2983edd", + "metadata": { + "editable": true + }, "source": [ "which is Bayes' theorem. It allows us to evaluate the uncertainty in in $X$ after we have observed $Y$. We can easily interchange $X$ with $Y$." ] }, { "cell_type": "markdown", - "id": "99f96e4d", - "metadata": {}, + "id": "285d34f1", + "metadata": { + "editable": true + }, "source": [ "## Interpretations of Bayes' Theorem\n", "\n", @@ -3279,8 +3682,10 @@ }, { "cell_type": "markdown", - "id": "46017fbc", - "metadata": {}, + "id": "4daab924", + "metadata": { + "editable": true + }, "source": [ "## Example of Usage of Bayes' theorem\n", "\n", @@ -3298,8 +3703,10 @@ }, { "cell_type": "markdown", - "id": "45acf8d6", - "metadata": {}, + "id": "134a7757", + "metadata": { + "editable": true + }, "source": [ "$$\n", "p(X=1\\vert Y=1) =0.8.\n", @@ -3308,8 +3715,10 @@ }, { "cell_type": "markdown", - "id": "d7189c6c", - "metadata": {}, + "id": "c43dde74", + "metadata": { + "editable": true + }, "source": [ "This obviously sounds scary since many would conclude that if the test is positive, there is a likelihood of $80\\%$ for having cancer.\n", "It is however not correct, as the following Bayesian analysis shows." @@ -3317,8 +3726,10 @@ }, { "cell_type": "markdown", - "id": "095f1344", - "metadata": {}, + "id": "18a5f41d", + "metadata": { + "editable": true + }, "source": [ "## Doing it correctly\n", "\n", @@ -3328,8 +3739,10 @@ }, { "cell_type": "markdown", - "id": "7a421514", - "metadata": {}, + "id": "31edd0a3", + "metadata": { + "editable": true + }, "source": [ "$$\n", "p(Y=1) =0.004.\n", @@ -3338,16 +3751,20 @@ }, { "cell_type": "markdown", - "id": "f6cb9991", - "metadata": {}, + "id": "f9171778", + "metadata": { + "editable": true + }, "source": [ "We need also to account for the fact that the test may produce a false positive result (false alarm). Let us here assume that we have" ] }, { "cell_type": "markdown", - "id": "4ca65a78", - "metadata": {}, + "id": "33f83b25", + "metadata": { + "editable": true + }, "source": [ "$$\n", "p(X=1\\vert Y=0) =0.1.\n", @@ -3356,16 +3773,20 @@ }, { "cell_type": "markdown", - "id": "940490f2", - "metadata": {}, + "id": "b23ddfdd", + "metadata": { + "editable": true + }, "source": [ "Using Bayes' theorem we can then find the posterior probability that the person has breast cancer in case of a positive test, that is we can compute" ] }, { "cell_type": "markdown", - "id": "fd376a0c", - "metadata": {}, + "id": "0c52e6db", + "metadata": { + "editable": true + }, "source": [ "$$\n", "p(Y=1\\vert X=1)=\\frac{p(X=1\\vert Y=1)p(Y=1)}{p(X=1\\vert Y=1)p(Y=1)+p(X=1\\vert Y=0)p(Y=0)}=\\frac{0.8\\times 0.004}{0.8\\times 0.004+0.1\\times 0.996}=0.031.\n", @@ -3374,16 +3795,20 @@ }, { "cell_type": "markdown", - "id": "891b9eda", - "metadata": {}, + "id": "bcbb9fb7", + "metadata": { + "editable": true + }, "source": [ "That is, in case of a positive test, there is only a $3\\%$ chance of having breast cancer!" ] }, { "cell_type": "markdown", - "id": "3734a062", - "metadata": {}, + "id": "5d5effd4", + "metadata": { + "editable": true + }, "source": [ "## Bayes' Theorem and Ridge and Lasso Regression\n", "\n", @@ -3397,8 +3822,10 @@ }, { "cell_type": "markdown", - "id": "2b6262c1", - "metadata": {}, + "id": "12191b9e", + "metadata": { + "editable": true + }, "source": [ "## Test Function for what happens with OLS, Ridge and Lasso\n", "\n", @@ -3414,8 +3841,11 @@ { "cell_type": "code", "execution_count": 11, - "id": "6028f12f", - "metadata": {}, + "id": "80b8d381", + "metadata": { + "collapsed": false, + "editable": true + }, "outputs": [], "source": [ "import numpy as np\n", @@ -3485,16 +3915,20 @@ }, { "cell_type": "markdown", - "id": "1e94a8e4", - "metadata": {}, + "id": "3d846a7b", + "metadata": { + "editable": true + }, "source": [ "How can we understand this?" ] }, { "cell_type": "markdown", - "id": "e89f9a23", - "metadata": {}, + "id": "cdba3cee", + "metadata": { + "editable": true + }, "source": [ "## Invoking Bayes' theorem\n", "\n", @@ -3505,8 +3939,10 @@ }, { "cell_type": "markdown", - "id": "bcc23e22", - "metadata": {}, + "id": "f2f7790a", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\boldsymbol{D}=[(x_0,y_0), (x_1,y_1),\\dots, (x_{n-1},y_{n-1})],\n", @@ -3515,16 +3951,20 @@ }, { "cell_type": "markdown", - "id": "ab35b334", - "metadata": {}, + "id": "14655685", + "metadata": { + "editable": true + }, "source": [ "is given by" ] }, { "cell_type": "markdown", - "id": "7bad9dac", - "metadata": {}, + "id": "a87a0d7d", + "metadata": { + "editable": true + }, "source": [ "$$\n", "p(\\boldsymbol{D}\\vert\\boldsymbol{\\beta})=\\prod_{i=0}^{n-1}\\frac{1}{\\sqrt{2\\pi\\sigma^2}}\\exp{\\left[-\\frac{(y_i-\\boldsymbol{X}_{i,*}\\boldsymbol{\\beta})^2}{2\\sigma^2}\\right]}.\n", @@ -3533,16 +3973,20 @@ }, { "cell_type": "markdown", - "id": "d58ce44e", - "metadata": {}, + "id": "c49cf8f2", + "metadata": { + "editable": true + }, "source": [ "In Bayes' theorem this function plays the role of the so-called likelihood. We could now ask the question what is the posterior probability of a parameter set $\\boldsymbol{\\beta}$ given a domain of events $\\boldsymbol{D}$? That is, how can we define the posterior probability" ] }, { "cell_type": "markdown", - "id": "e302a211", - "metadata": {}, + "id": "093333d4", + "metadata": { + "editable": true + }, "source": [ "$$\n", "p(\\boldsymbol{\\beta}\\vert\\boldsymbol{D}).\n", @@ -3551,16 +3995,20 @@ }, { "cell_type": "markdown", - "id": "ffc980d1", - "metadata": {}, + "id": "1b9fa92a", + "metadata": { + "editable": true + }, "source": [ "Bayes' theorem comes to our rescue here since (omitting the normalization constant)" ] }, { "cell_type": "markdown", - "id": "860cdf65", - "metadata": {}, + "id": "53c11861", + "metadata": { + "editable": true + }, "source": [ "$$\n", "p(\\boldsymbol{\\beta}\\vert\\boldsymbol{D})\\propto p(\\boldsymbol{D}\\vert\\boldsymbol{\\beta})p(\\boldsymbol{\\beta}).\n", @@ -3569,16 +4017,20 @@ }, { "cell_type": "markdown", - "id": "8ad984c0", - "metadata": {}, + "id": "2f8298c1", + "metadata": { + "editable": true + }, "source": [ "We have a model for $p(\\boldsymbol{D}\\vert\\boldsymbol{\\beta})$ but need one for the **prior** $p(\\boldsymbol{\\beta}$!" ] }, { "cell_type": "markdown", - "id": "d0bee78c", - "metadata": {}, + "id": "87d74e6a", + "metadata": { + "editable": true + }, "source": [ "## Ridge and Bayes\n", "\n", @@ -3591,8 +4043,10 @@ }, { "cell_type": "markdown", - "id": "bf039e41", - "metadata": {}, + "id": "a049d0ea", + "metadata": { + "editable": true + }, "source": [ "$$\n", "p(\\boldsymbol{\\beta})=\\prod_{j=0}^{p-1}\\exp{\\left(-\\frac{\\beta_j^2}{2\\tau^2}\\right)}.\n", @@ -3601,16 +4055,20 @@ }, { "cell_type": "markdown", - "id": "fffb60aa", - "metadata": {}, + "id": "423dbbec", + "metadata": { + "editable": true + }, "source": [ "Our posterior probability becomes then (omitting the normalization factor which is just a constant)" ] }, { "cell_type": "markdown", - "id": "67b49eab", - "metadata": {}, + "id": "8f81c0dd", + "metadata": { + "editable": true + }, "source": [ "$$\n", "p(\\boldsymbol{\\beta\\vert\\boldsymbol{D})}=\\prod_{i=0}^{n-1}\\frac{1}{\\sqrt{2\\pi\\sigma^2}}\\exp{\\left[-\\frac{(y_i-\\boldsymbol{X}_{i,*}\\boldsymbol{\\beta})^2}{2\\sigma^2}\\right]}\\prod_{j=0}^{p-1}\\exp{\\left(-\\frac{\\beta_j^2}{2\\tau^2}\\right)}.\n", @@ -3619,8 +4077,10 @@ }, { "cell_type": "markdown", - "id": "cae7309e", - "metadata": {}, + "id": "d44ba548", + "metadata": { + "editable": true + }, "source": [ "We can now optimize this quantity with respect to $\\boldsymbol{\\beta}$. As we\n", "did for OLS, this is most conveniently done by taking the negative\n", @@ -3630,8 +4090,10 @@ }, { "cell_type": "markdown", - "id": "e15e70d1", - "metadata": {}, + "id": "759e1a46", + "metadata": { + "editable": true + }, "source": [ "$$\n", "C(\\boldsymbol{\\beta})=\\frac{\\vert\\vert (\\boldsymbol{y}-\\boldsymbol{X}\\boldsymbol{\\beta})\\vert\\vert_2^2}{2\\sigma^2}+\\frac{1}{2\\tau^2}\\vert\\vert\\boldsymbol{\\beta}\\vert\\vert_2^2,\n", @@ -3640,16 +4102,20 @@ }, { "cell_type": "markdown", - "id": "36d005b5", - "metadata": {}, + "id": "32b1de62", + "metadata": { + "editable": true + }, "source": [ "and replacing $1/2\\tau^2$ with $\\lambda$ we have" ] }, { "cell_type": "markdown", - "id": "4deb0ca9", - "metadata": {}, + "id": "2378e1f6", + "metadata": { + "editable": true + }, "source": [ "$$\n", "C(\\boldsymbol{\\beta})=\\frac{\\vert\\vert (\\boldsymbol{y}-\\boldsymbol{X}\\boldsymbol{\\beta})\\vert\\vert_2^2}{2\\sigma^2}+\\lambda\\vert\\vert\\boldsymbol{\\beta}\\vert\\vert_2^2,\n", @@ -3658,16 +4124,20 @@ }, { "cell_type": "markdown", - "id": "84fcc55c", - "metadata": {}, + "id": "787f1ed7", + "metadata": { + "editable": true + }, "source": [ "which is our Ridge cost function! Nice, isn't it?" ] }, { "cell_type": "markdown", - "id": "47d7c3ca", - "metadata": {}, + "id": "401c6c4e", + "metadata": { + "editable": true + }, "source": [ "## Lasso and Bayes\n", "\n", @@ -3676,8 +4146,10 @@ }, { "cell_type": "markdown", - "id": "8e745a19", - "metadata": {}, + "id": "3f7aece4", + "metadata": { + "editable": true + }, "source": [ "$$\n", "p(\\boldsymbol{\\beta})=\\prod_{j=0}^{p-1}\\exp{\\left(-\\frac{\\vert\\beta_j\\vert}{\\tau}\\right)}.\n", @@ -3686,16 +4158,20 @@ }, { "cell_type": "markdown", - "id": "55142cc6", - "metadata": {}, + "id": "deb8ed7f", + "metadata": { + "editable": true + }, "source": [ "Our posterior probability becomes then (omitting the normalization factor which is just a constant)" ] }, { "cell_type": "markdown", - "id": "f627b93a", - "metadata": {}, + "id": "354d2fea", + "metadata": { + "editable": true + }, "source": [ "$$\n", "p(\\boldsymbol{\\beta}\\vert\\boldsymbol{D})=\\prod_{i=0}^{n-1}\\frac{1}{\\sqrt{2\\pi\\sigma^2}}\\exp{\\left[-\\frac{(y_i-\\boldsymbol{X}_{i,*}\\boldsymbol{\\beta})^2}{2\\sigma^2}\\right]}\\prod_{j=0}^{p-1}\\exp{\\left(-\\frac{\\vert\\beta_j\\vert}{\\tau}\\right)}.\n", @@ -3704,8 +4180,10 @@ }, { "cell_type": "markdown", - "id": "1a1bae81", - "metadata": {}, + "id": "834e893c", + "metadata": { + "editable": true + }, "source": [ "Taking the negative\n", "logarithm of the posterior probability and leaving out the\n", @@ -3714,8 +4192,10 @@ }, { "cell_type": "markdown", - "id": "0c8a5a7b", - "metadata": {}, + "id": "9f14f991", + "metadata": { + "editable": true + }, "source": [ "$$\n", "C(\\boldsymbol{\\beta}=\\frac{\\vert\\vert (\\boldsymbol{y}-\\boldsymbol{X}\\boldsymbol{\\beta})\\vert\\vert_2^2}{2\\sigma^2}+\\frac{1}{\\tau}\\vert\\vert\\boldsymbol{\\beta}\\vert\\vert_1,\n", @@ -3724,16 +4204,20 @@ }, { "cell_type": "markdown", - "id": "05bcb4ee", - "metadata": {}, + "id": "e749f01f", + "metadata": { + "editable": true + }, "source": [ "and replacing $1/\\tau$ with $\\lambda$ we have" ] }, { "cell_type": "markdown", - "id": "308e688d", - "metadata": {}, + "id": "06f11820", + "metadata": { + "editable": true + }, "source": [ "$$\n", "C(\\boldsymbol{\\beta}=\\frac{\\vert\\vert (\\boldsymbol{y}-\\boldsymbol{X}\\boldsymbol{\\beta})\\vert\\vert_2^2}{2\\sigma^2}+\\lambda\\vert\\vert\\boldsymbol{\\beta}\\vert\\vert_1,\n", @@ -3742,16 +4226,20 @@ }, { "cell_type": "markdown", - "id": "e127394e", - "metadata": {}, + "id": "94c9065c", + "metadata": { + "editable": true + }, "source": [ "which is our Lasso cost function!" ] }, { "cell_type": "markdown", - "id": "ca26f6f7", - "metadata": {}, + "id": "20c64501", + "metadata": { + "editable": true + }, "source": [ "## Exercise 1: mean values and variances in linear regression\n", "\n", @@ -3764,8 +4252,10 @@ }, { "cell_type": "markdown", - "id": "a0f78141", - "metadata": {}, + "id": "bc069f72", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\boldsymbol{y} = f(\\boldsymbol{x})+\\boldsymbol{\\varepsilon}\n", @@ -3774,8 +4264,10 @@ }, { "cell_type": "markdown", - "id": "a285460c", - "metadata": {}, + "id": "ee99deec", + "metadata": { + "editable": true + }, "source": [ "We then approximate this function with our model from the solution of the linear regression equations (ordinary least squares OLS), that is our\n", "function $f$ is approximated by $\\boldsymbol{\\tilde{y}}$ where we minimized $(\\boldsymbol{y}-\\boldsymbol{\\tilde{y}})^2$, with" @@ -3783,8 +4275,10 @@ }, { "cell_type": "markdown", - "id": "f5f96083", - "metadata": {}, + "id": "7b02bf27", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\boldsymbol{\\tilde{y}} = \\boldsymbol{X}\\boldsymbol{\\beta}.\n", @@ -3793,16 +4287,20 @@ }, { "cell_type": "markdown", - "id": "21e62126", - "metadata": {}, + "id": "fdfd39db", + "metadata": { + "editable": true + }, "source": [ "The matrix $\\boldsymbol{X}$ is the so-called design matrix." ] }, { "cell_type": "markdown", - "id": "e5730a62", - "metadata": {}, + "id": "8d268428", + "metadata": { + "editable": true + }, "source": [ "**a)**\n", "Show that the expectation value of $\\boldsymbol{y}$ for a given element $i$" @@ -3810,8 +4308,10 @@ }, { "cell_type": "markdown", - "id": "55b4af74", - "metadata": {}, + "id": "23b6f545", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\begin{align*} \n", @@ -3822,8 +4322,10 @@ }, { "cell_type": "markdown", - "id": "c4fb720a", - "metadata": {}, + "id": "2db7ea8d", + "metadata": { + "editable": true + }, "source": [ "and that\n", "its variance is" @@ -3831,8 +4333,10 @@ }, { "cell_type": "markdown", - "id": "31a32444", - "metadata": {}, + "id": "ce163d77", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\begin{align*} \\mbox{Var}(y_i) & = \\sigma^2. \n", @@ -3842,8 +4346,10 @@ }, { "cell_type": "markdown", - "id": "18736ca9", - "metadata": {}, + "id": "bbd3a9ca", + "metadata": { + "editable": true + }, "source": [ "Hence, $y_i \\sim \\mathcal{N}( \\mathbf{X}_{i, \\ast} \\, \\boldsymbol{\\beta}, \\sigma^2)$, that is $\\boldsymbol{y}$ follows a normal distribution with \n", "mean value $\\boldsymbol{X}\\boldsymbol{\\beta}$ and variance $\\sigma^2$." @@ -3851,8 +4357,10 @@ }, { "cell_type": "markdown", - "id": "889dcad9", - "metadata": {}, + "id": "11a2440e", + "metadata": { + "editable": true + }, "source": [ "**b)**\n", "With the OLS expressions for the parameters $\\boldsymbol{\\beta}$ show that" @@ -3860,8 +4368,10 @@ }, { "cell_type": "markdown", - "id": "65d7114c", - "metadata": {}, + "id": "e94c5e5e", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\mathbb{E}(\\boldsymbol{\\beta}) = \\boldsymbol{\\beta}.\n", @@ -3870,8 +4380,10 @@ }, { "cell_type": "markdown", - "id": "0246cb71", - "metadata": {}, + "id": "cfaebf91", + "metadata": { + "editable": true + }, "source": [ "**c)**\n", "Show finally that the variance of $\\boldsymbol{\\beta}$ is" @@ -3879,8 +4391,10 @@ }, { "cell_type": "markdown", - "id": "9a239b08", - "metadata": {}, + "id": "2fb5c3f9", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\begin{eqnarray*}\n", @@ -3891,8 +4405,10 @@ }, { "cell_type": "markdown", - "id": "9b868ddb", - "metadata": {}, + "id": "8121d786", + "metadata": { + "editable": true + }, "source": [ "## Exercise 2: Adding Ridge and Lasso Regression\n", "\n", @@ -3915,8 +4431,11 @@ { "cell_type": "code", "execution_count": 12, - "id": "fe00295c", - "metadata": {}, + "id": "f91041e7", + "metadata": { + "collapsed": false, + "editable": true + }, "outputs": [], "source": [ "x = np.random.rand(100)\n", @@ -3925,8 +4444,10 @@ }, { "cell_type": "markdown", - "id": "d59b60b2", - "metadata": {}, + "id": "df8b4c9c", + "metadata": { + "editable": true + }, "source": [ "**a)**\n", "Write your own code for the Ridge method (see chapter 3.4 of Hastie *et al.*, equations (3.43) and (3.44)) and compute the parametrization for different values of $\\lambda$. Study the dependence on $\\lambda$ while also varying the strength of the noise in your expression for $y(x)$." @@ -3934,33 +4455,17 @@ }, { "cell_type": "markdown", - "id": "47dc99e1", - "metadata": {}, + "id": "9acc49d5", + "metadata": { + "editable": true + }, "source": [ "**b)**\n", "Our next step is to study the variance of the parameters $\\beta_1$ and $\\beta_2$ (assuming that we are parameterizing our function with a second-order polynomial). We will use standard linear regression and the Ridge regression. You can now opt for either writing your own function or using **Scikit-Learn** to find the parameters $\\beta$. From your results calculate the variance of these parameters (recall that this is equal to the diagonal elements of the matrix $(\\hat{X}^T\\hat{X})+\\lambda\\hat{I})^{-1}$). Discuss the results of these variances as functions of $\\lambda$. In particular, try to link your discussion with the discussion in Hastie *et al.* and their figures 3.10 and 3.11. **Scikit-Learn** may not provide the variance of the parameters $\\beta$. This needs to be checked. With your own code you can however do so." ] } ], - "metadata": { - "kernelspec": { - "display_name": "Python 3 (ipykernel)", - "language": "python", - "name": "python3" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.9.14" - } - }, + "metadata": {}, "nbformat": 4, "nbformat_minor": 5 } diff --git a/doc/src/week36/week36.do.txt b/doc/src/week36/week36.do.txt index a583f36cb..11da16c5c 100644 --- a/doc/src/week36/week36.do.txt +++ b/doc/src/week36/week36.do.txt @@ -6,10 +6,8 @@ DATE: today !split ===== Plans for week 36 ===== -* Thursday: Summary from last week on discussion of SVD, Ridge and Lasso linear regression. - * "Video of Lecture":"https://youtu.be/qn_BAVhMD8U" -* Friday: Linear Regression and links with Statistics, Resampling methods and presentation of first project - * "Video of Lecture":"https://youtu.be/_CPGg0JYH8M" +* Summary from last week on discussion of SVD, Ridge and Lasso linear regression. +* Linear Regression and links with Statistics, Resampling methods and presentation of first project Recommended Reading: o Lectures on Regression @@ -17,9 +15,6 @@ o Bishop 1.1, 1.2, 2.1, 2.2, 2.3 and 3.1 o Hastie et al chapter 3 -!split -===== Thursday September 8 ===== - !split ===== Summary from last Week and discussion of SVD, Ridge and Lasso regression with examples =====