diff --git a/doc/pub/week37/html/._week37-bs000.html b/doc/pub/week37/html/._week37-bs000.html
index a60ece31a..c76d88187 100644
--- a/doc/pub/week37/html/._week37-bs000.html
+++ b/doc/pub/week37/html/._week37-bs000.html
@@ -69,6 +69,7 @@ Automatically generated HTML file from DocOnce source
2,
None,
'test-function-for-what-happens-with-ols-ridge-and-lasso'),
+ ('Rerunning the above code', 2, None, 'rerunning-the-above-code'),
("Invoking Bayes' theorem", 2, None, 'invoking-bayes-theorem'),
('Ridge and Bayes', 2, None, 'ridge-and-bayes'),
('Lasso and Bayes', 2, None, 'lasso-and-bayes'),
@@ -206,36 +207,37 @@ MathJax.Hub.Config({
Bayes' Theorem
Interpretations of Bayes' Theorem
Test Function for what happens with OLS, Ridge and Lasso
- Invoking Bayes' theorem
- Ridge and Bayes
- Lasso and Bayes
- Why resampling methods
- Resampling methods
- Resampling approaches can be computationally expensive
- Why resampling methods ?
- Statistical analysis
- Resampling methods
- Resampling methods: Jackknife and Bootstrap
- Resampling methods: Jackknife
- Jackknife code example
- Resampling methods: Bootstrap
- Resampling methods: Bootstrap background
- Resampling methods: More Bootstrap background
- Resampling methods: Bootstrap approach
- Resampling methods: Bootstrap steps
- Code example for the Bootstrap method
- The bias-variance tradeoff
- Example code for Bias-Variance tradeoff
- Understanding what happens
- Summing up
- Another Example from Scikit-Learn's Repository
- Various steps in cross-validation
- How to set up the cross-validation for Ridge and/or Lasso
- Cross-validation in brief
- Code Example for Cross-validation and \( k \)-fold Cross-validation
- More examples on bootstrap and cross-validation and errors
- The same example but now with cross-validation
- Cross-validation with Ridge
+ Rerunning the above code
+ Invoking Bayes' theorem
+ Ridge and Bayes
+ Lasso and Bayes
+ Why resampling methods
+ Resampling methods
+ Resampling approaches can be computationally expensive
+ Why resampling methods ?
+ Statistical analysis
+ Resampling methods
+ Resampling methods: Jackknife and Bootstrap
+ Resampling methods: Jackknife
+ Jackknife code example
+ Resampling methods: Bootstrap
+ Resampling methods: Bootstrap background
+ Resampling methods: More Bootstrap background
+ Resampling methods: Bootstrap approach
+ Resampling methods: Bootstrap steps
+ Code example for the Bootstrap method
+ The bias-variance tradeoff
+ Example code for Bias-Variance tradeoff
+ Understanding what happens
+ Summing up
+ Another Example from Scikit-Learn's Repository
+ Various steps in cross-validation
+ How to set up the cross-validation for Ridge and/or Lasso
+ Cross-validation in brief
+ Code Example for Cross-validation and \( k \)-fold Cross-validation
+ More examples on bootstrap and cross-validation and errors
+ The same example but now with cross-validation
+ Cross-validation with Ridge
@@ -270,7 +272,7 @@ MathJax.Hub.Config({
[2] Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University
-
Sep 15, 2021
+Sep 16, 2021
@@ -294,7 +296,7 @@ MathJax.Hub.Config({
9
10
...
- 40
+ 41
»
diff --git a/doc/pub/week37/html/._week37-bs001.html b/doc/pub/week37/html/._week37-bs001.html
index 65ca2ed47..9ccc0f4c0 100644
--- a/doc/pub/week37/html/._week37-bs001.html
+++ b/doc/pub/week37/html/._week37-bs001.html
@@ -69,6 +69,7 @@ Automatically generated HTML file from DocOnce source
2,
None,
'test-function-for-what-happens-with-ols-ridge-and-lasso'),
+ ('Rerunning the above code', 2, None, 'rerunning-the-above-code'),
("Invoking Bayes' theorem", 2, None, 'invoking-bayes-theorem'),
('Ridge and Bayes', 2, None, 'ridge-and-bayes'),
('Lasso and Bayes', 2, None, 'lasso-and-bayes'),
@@ -206,36 +207,37 @@ MathJax.Hub.Config({
Bayes' Theorem
Interpretations of Bayes' Theorem
Test Function for what happens with OLS, Ridge and Lasso
- Invoking Bayes' theorem
- Ridge and Bayes
- Lasso and Bayes
- Why resampling methods
- Resampling methods
- Resampling approaches can be computationally expensive
- Why resampling methods ?
- Statistical analysis
- Resampling methods
- Resampling methods: Jackknife and Bootstrap
- Resampling methods: Jackknife
- Jackknife code example
- Resampling methods: Bootstrap
- Resampling methods: Bootstrap background
- Resampling methods: More Bootstrap background
- Resampling methods: Bootstrap approach
- Resampling methods: Bootstrap steps
- Code example for the Bootstrap method
- The bias-variance tradeoff
- Example code for Bias-Variance tradeoff
- Understanding what happens
- Summing up
- Another Example from Scikit-Learn's Repository
- Various steps in cross-validation
- How to set up the cross-validation for Ridge and/or Lasso
- Cross-validation in brief
- Code Example for Cross-validation and \( k \)-fold Cross-validation
- More examples on bootstrap and cross-validation and errors
- The same example but now with cross-validation
- Cross-validation with Ridge
+ Rerunning the above code
+ Invoking Bayes' theorem
+ Ridge and Bayes
+ Lasso and Bayes
+ Why resampling methods
+ Resampling methods
+ Resampling approaches can be computationally expensive
+ Why resampling methods ?
+ Statistical analysis
+ Resampling methods
+ Resampling methods: Jackknife and Bootstrap
+ Resampling methods: Jackknife
+ Jackknife code example
+ Resampling methods: Bootstrap
+ Resampling methods: Bootstrap background
+ Resampling methods: More Bootstrap background
+ Resampling methods: Bootstrap approach
+ Resampling methods: Bootstrap steps
+ Code example for the Bootstrap method
+ The bias-variance tradeoff
+ Example code for Bias-Variance tradeoff
+ Understanding what happens
+ Summing up
+ Another Example from Scikit-Learn's Repository
+ Various steps in cross-validation
+ How to set up the cross-validation for Ridge and/or Lasso
+ Cross-validation in brief
+ Code Example for Cross-validation and \( k \)-fold Cross-validation
+ More examples on bootstrap and cross-validation and errors
+ The same example but now with cross-validation
+ Cross-validation with Ridge
@@ -282,7 +284,7 @@ Recommended Reading:
10
11
...
- 40
+ 41
»
diff --git a/doc/pub/week37/html/._week37-bs002.html b/doc/pub/week37/html/._week37-bs002.html
index c0d2620cb..9cb66a8d0 100644
--- a/doc/pub/week37/html/._week37-bs002.html
+++ b/doc/pub/week37/html/._week37-bs002.html
@@ -69,6 +69,7 @@ Automatically generated HTML file from DocOnce source
2,
None,
'test-function-for-what-happens-with-ols-ridge-and-lasso'),
+ ('Rerunning the above code', 2, None, 'rerunning-the-above-code'),
("Invoking Bayes' theorem", 2, None, 'invoking-bayes-theorem'),
('Ridge and Bayes', 2, None, 'ridge-and-bayes'),
('Lasso and Bayes', 2, None, 'lasso-and-bayes'),
@@ -206,36 +207,37 @@ MathJax.Hub.Config({
Bayes' Theorem
Interpretations of Bayes' Theorem
Test Function for what happens with OLS, Ridge and Lasso
- Invoking Bayes' theorem
- Ridge and Bayes
- Lasso and Bayes
- Why resampling methods
- Resampling methods
- Resampling approaches can be computationally expensive
- Why resampling methods ?
- Statistical analysis
- Resampling methods
- Resampling methods: Jackknife and Bootstrap
- Resampling methods: Jackknife
- Jackknife code example
- Resampling methods: Bootstrap
- Resampling methods: Bootstrap background
- Resampling methods: More Bootstrap background
- Resampling methods: Bootstrap approach
- Resampling methods: Bootstrap steps
- Code example for the Bootstrap method
- The bias-variance tradeoff
- Example code for Bias-Variance tradeoff
- Understanding what happens
- Summing up
- Another Example from Scikit-Learn's Repository
- Various steps in cross-validation
- How to set up the cross-validation for Ridge and/or Lasso
- Cross-validation in brief
- Code Example for Cross-validation and \( k \)-fold Cross-validation
- More examples on bootstrap and cross-validation and errors
- The same example but now with cross-validation
- Cross-validation with Ridge
+ Rerunning the above code
+ Invoking Bayes' theorem
+ Ridge and Bayes
+ Lasso and Bayes
+ Why resampling methods
+ Resampling methods
+ Resampling approaches can be computationally expensive
+ Why resampling methods ?
+ Statistical analysis
+ Resampling methods
+ Resampling methods: Jackknife and Bootstrap
+ Resampling methods: Jackknife
+ Jackknife code example
+ Resampling methods: Bootstrap
+ Resampling methods: Bootstrap background
+ Resampling methods: More Bootstrap background
+ Resampling methods: Bootstrap approach
+ Resampling methods: Bootstrap steps
+ Code example for the Bootstrap method
+ The bias-variance tradeoff
+ Example code for Bias-Variance tradeoff
+ Understanding what happens
+ Summing up
+ Another Example from Scikit-Learn's Repository
+ Various steps in cross-validation
+ How to set up the cross-validation for Ridge and/or Lasso
+ Cross-validation in brief
+ Code Example for Cross-validation and \( k \)-fold Cross-validation
+ More examples on bootstrap and cross-validation and errors
+ The same example but now with cross-validation
+ Cross-validation with Ridge
@@ -271,7 +273,7 @@ MathJax.Hub.Config({
11
12
...
- 40
+ 41
»
diff --git a/doc/pub/week37/html/._week37-bs003.html b/doc/pub/week37/html/._week37-bs003.html
index f54737de8..e274a724f 100644
--- a/doc/pub/week37/html/._week37-bs003.html
+++ b/doc/pub/week37/html/._week37-bs003.html
@@ -69,6 +69,7 @@ Automatically generated HTML file from DocOnce source
2,
None,
'test-function-for-what-happens-with-ols-ridge-and-lasso'),
+ ('Rerunning the above code', 2, None, 'rerunning-the-above-code'),
("Invoking Bayes' theorem", 2, None, 'invoking-bayes-theorem'),
('Ridge and Bayes', 2, None, 'ridge-and-bayes'),
('Lasso and Bayes', 2, None, 'lasso-and-bayes'),
@@ -206,36 +207,37 @@ MathJax.Hub.Config({
Bayes' Theorem
Interpretations of Bayes' Theorem
Test Function for what happens with OLS, Ridge and Lasso
- Invoking Bayes' theorem
- Ridge and Bayes
- Lasso and Bayes
- Why resampling methods
- Resampling methods
- Resampling approaches can be computationally expensive
- Why resampling methods ?
- Statistical analysis
- Resampling methods
- Resampling methods: Jackknife and Bootstrap
- Resampling methods: Jackknife
- Jackknife code example
- Resampling methods: Bootstrap
- Resampling methods: Bootstrap background
- Resampling methods: More Bootstrap background
- Resampling methods: Bootstrap approach
- Resampling methods: Bootstrap steps
- Code example for the Bootstrap method
- The bias-variance tradeoff
- Example code for Bias-Variance tradeoff
- Understanding what happens
- Summing up
- Another Example from Scikit-Learn's Repository
- Various steps in cross-validation
- How to set up the cross-validation for Ridge and/or Lasso
- Cross-validation in brief
- Code Example for Cross-validation and \( k \)-fold Cross-validation
- More examples on bootstrap and cross-validation and errors
- The same example but now with cross-validation
- Cross-validation with Ridge
+ Rerunning the above code
+ Invoking Bayes' theorem
+ Ridge and Bayes
+ Lasso and Bayes
+ Why resampling methods
+ Resampling methods
+ Resampling approaches can be computationally expensive
+ Why resampling methods ?
+ Statistical analysis
+ Resampling methods
+ Resampling methods: Jackknife and Bootstrap
+ Resampling methods: Jackknife
+ Jackknife code example
+ Resampling methods: Bootstrap
+ Resampling methods: Bootstrap background
+ Resampling methods: More Bootstrap background
+ Resampling methods: Bootstrap approach
+ Resampling methods: Bootstrap steps
+ Code example for the Bootstrap method
+ The bias-variance tradeoff
+ Example code for Bias-Variance tradeoff
+ Understanding what happens
+ Summing up
+ Another Example from Scikit-Learn's Repository
+ Various steps in cross-validation
+ How to set up the cross-validation for Ridge and/or Lasso
+ Cross-validation in brief
+ Code Example for Cross-validation and \( k \)-fold Cross-validation
+ More examples on bootstrap and cross-validation and errors
+ The same example but now with cross-validation
+ Cross-validation with Ridge
@@ -291,7 +293,7 @@ $$
12
13
...
- 40
+ 41
»
diff --git a/doc/pub/week37/html/._week37-bs004.html b/doc/pub/week37/html/._week37-bs004.html
index 019e4986a..88f8f81e4 100644
--- a/doc/pub/week37/html/._week37-bs004.html
+++ b/doc/pub/week37/html/._week37-bs004.html
@@ -69,6 +69,7 @@ Automatically generated HTML file from DocOnce source
2,
None,
'test-function-for-what-happens-with-ols-ridge-and-lasso'),
+ ('Rerunning the above code', 2, None, 'rerunning-the-above-code'),
("Invoking Bayes' theorem", 2, None, 'invoking-bayes-theorem'),
('Ridge and Bayes', 2, None, 'ridge-and-bayes'),
('Lasso and Bayes', 2, None, 'lasso-and-bayes'),
@@ -206,36 +207,37 @@ MathJax.Hub.Config({
Bayes' Theorem
Interpretations of Bayes' Theorem
Test Function for what happens with OLS, Ridge and Lasso
- Invoking Bayes' theorem
- Ridge and Bayes
- Lasso and Bayes
- Why resampling methods
- Resampling methods
- Resampling approaches can be computationally expensive
- Why resampling methods ?
- Statistical analysis
- Resampling methods
- Resampling methods: Jackknife and Bootstrap
- Resampling methods: Jackknife
- Jackknife code example
- Resampling methods: Bootstrap
- Resampling methods: Bootstrap background
- Resampling methods: More Bootstrap background
- Resampling methods: Bootstrap approach
- Resampling methods: Bootstrap steps
- Code example for the Bootstrap method
- The bias-variance tradeoff
- Example code for Bias-Variance tradeoff
- Understanding what happens
- Summing up
- Another Example from Scikit-Learn's Repository
- Various steps in cross-validation
- How to set up the cross-validation for Ridge and/or Lasso
- Cross-validation in brief
- Code Example for Cross-validation and \( k \)-fold Cross-validation
- More examples on bootstrap and cross-validation and errors
- The same example but now with cross-validation
- Cross-validation with Ridge
+ Rerunning the above code
+ Invoking Bayes' theorem
+ Ridge and Bayes
+ Lasso and Bayes
+ Why resampling methods
+ Resampling methods
+ Resampling approaches can be computationally expensive
+ Why resampling methods ?
+ Statistical analysis
+ Resampling methods
+ Resampling methods: Jackknife and Bootstrap
+ Resampling methods: Jackknife
+ Jackknife code example
+ Resampling methods: Bootstrap
+ Resampling methods: Bootstrap background
+ Resampling methods: More Bootstrap background
+ Resampling methods: Bootstrap approach
+ Resampling methods: Bootstrap steps
+ Code example for the Bootstrap method
+ The bias-variance tradeoff
+ Example code for Bias-Variance tradeoff
+ Understanding what happens
+ Summing up
+ Another Example from Scikit-Learn's Repository
+ Various steps in cross-validation
+ How to set up the cross-validation for Ridge and/or Lasso
+ Cross-validation in brief
+ Code Example for Cross-validation and \( k \)-fold Cross-validation
+ More examples on bootstrap and cross-validation and errors
+ The same example but now with cross-validation
+ Cross-validation with Ridge
@@ -305,7 +307,7 @@ It is a conditional probability (see below) and reads as the likelihood of a dom
13
14
...
- 40
+ 41
»
diff --git a/doc/pub/week37/html/._week37-bs005.html b/doc/pub/week37/html/._week37-bs005.html
index 2e76fb785..7ff12725a 100644
--- a/doc/pub/week37/html/._week37-bs005.html
+++ b/doc/pub/week37/html/._week37-bs005.html
@@ -69,6 +69,7 @@ Automatically generated HTML file from DocOnce source
2,
None,
'test-function-for-what-happens-with-ols-ridge-and-lasso'),
+ ('Rerunning the above code', 2, None, 'rerunning-the-above-code'),
("Invoking Bayes' theorem", 2, None, 'invoking-bayes-theorem'),
('Ridge and Bayes', 2, None, 'ridge-and-bayes'),
('Lasso and Bayes', 2, None, 'lasso-and-bayes'),
@@ -206,36 +207,37 @@ MathJax.Hub.Config({
Bayes' Theorem
Interpretations of Bayes' Theorem
Test Function for what happens with OLS, Ridge and Lasso
- Invoking Bayes' theorem
- Ridge and Bayes
- Lasso and Bayes
- Why resampling methods
- Resampling methods
- Resampling approaches can be computationally expensive
- Why resampling methods ?
- Statistical analysis
- Resampling methods
- Resampling methods: Jackknife and Bootstrap
- Resampling methods: Jackknife
- Jackknife code example
- Resampling methods: Bootstrap
- Resampling methods: Bootstrap background
- Resampling methods: More Bootstrap background
- Resampling methods: Bootstrap approach
- Resampling methods: Bootstrap steps
- Code example for the Bootstrap method
- The bias-variance tradeoff
- Example code for Bias-Variance tradeoff
- Understanding what happens
- Summing up
- Another Example from Scikit-Learn's Repository
- Various steps in cross-validation
- How to set up the cross-validation for Ridge and/or Lasso
- Cross-validation in brief
- Code Example for Cross-validation and \( k \)-fold Cross-validation
- More examples on bootstrap and cross-validation and errors
- The same example but now with cross-validation
- Cross-validation with Ridge
+ Rerunning the above code
+ Invoking Bayes' theorem
+ Ridge and Bayes
+ Lasso and Bayes
+ Why resampling methods
+ Resampling methods
+ Resampling approaches can be computationally expensive
+ Why resampling methods ?
+ Statistical analysis
+ Resampling methods
+ Resampling methods: Jackknife and Bootstrap
+ Resampling methods: Jackknife
+ Jackknife code example
+ Resampling methods: Bootstrap
+ Resampling methods: Bootstrap background
+ Resampling methods: More Bootstrap background
+ Resampling methods: Bootstrap approach
+ Resampling methods: Bootstrap steps
+ Code example for the Bootstrap method
+ The bias-variance tradeoff
+ Example code for Bias-Variance tradeoff
+ Understanding what happens
+ Summing up
+ Another Example from Scikit-Learn's Repository
+ Various steps in cross-validation
+ How to set up the cross-validation for Ridge and/or Lasso
+ Cross-validation in brief
+ Code Example for Cross-validation and \( k \)-fold Cross-validation
+ More examples on bootstrap and cross-validation and errors
+ The same example but now with cross-validation
+ Cross-validation with Ridge
@@ -299,7 +301,7 @@ is equivalent to the maximization/minimization of the function itself.
14
15
...
- 40
+ 41
»
diff --git a/doc/pub/week37/html/._week37-bs006.html b/doc/pub/week37/html/._week37-bs006.html
index 8316792af..e0c81b325 100644
--- a/doc/pub/week37/html/._week37-bs006.html
+++ b/doc/pub/week37/html/._week37-bs006.html
@@ -69,6 +69,7 @@ Automatically generated HTML file from DocOnce source
2,
None,
'test-function-for-what-happens-with-ols-ridge-and-lasso'),
+ ('Rerunning the above code', 2, None, 'rerunning-the-above-code'),
("Invoking Bayes' theorem", 2, None, 'invoking-bayes-theorem'),
('Ridge and Bayes', 2, None, 'ridge-and-bayes'),
('Lasso and Bayes', 2, None, 'lasso-and-bayes'),
@@ -206,36 +207,37 @@ MathJax.Hub.Config({
Bayes' Theorem
Interpretations of Bayes' Theorem
Test Function for what happens with OLS, Ridge and Lasso
- Invoking Bayes' theorem
- Ridge and Bayes
- Lasso and Bayes
- Why resampling methods
- Resampling methods
- Resampling approaches can be computationally expensive
- Why resampling methods ?
- Statistical analysis
- Resampling methods
- Resampling methods: Jackknife and Bootstrap
- Resampling methods: Jackknife
- Jackknife code example
- Resampling methods: Bootstrap
- Resampling methods: Bootstrap background
- Resampling methods: More Bootstrap background
- Resampling methods: Bootstrap approach
- Resampling methods: Bootstrap steps
- Code example for the Bootstrap method
- The bias-variance tradeoff
- Example code for Bias-Variance tradeoff
- Understanding what happens
- Summing up
- Another Example from Scikit-Learn's Repository
- Various steps in cross-validation
- How to set up the cross-validation for Ridge and/or Lasso
- Cross-validation in brief
- Code Example for Cross-validation and \( k \)-fold Cross-validation
- More examples on bootstrap and cross-validation and errors
- The same example but now with cross-validation
- Cross-validation with Ridge
+ Rerunning the above code
+ Invoking Bayes' theorem
+ Ridge and Bayes
+ Lasso and Bayes
+ Why resampling methods
+ Resampling methods
+ Resampling approaches can be computationally expensive
+ Why resampling methods ?
+ Statistical analysis
+ Resampling methods
+ Resampling methods: Jackknife and Bootstrap
+ Resampling methods: Jackknife
+ Jackknife code example
+ Resampling methods: Bootstrap
+ Resampling methods: Bootstrap background
+ Resampling methods: More Bootstrap background
+ Resampling methods: Bootstrap approach
+ Resampling methods: Bootstrap steps
+ Code example for the Bootstrap method
+ The bias-variance tradeoff
+ Example code for Bias-Variance tradeoff
+ Understanding what happens
+ Summing up
+ Another Example from Scikit-Learn's Repository
+ Various steps in cross-validation
+ How to set up the cross-validation for Ridge and/or Lasso
+ Cross-validation in brief
+ Code Example for Cross-validation and \( k \)-fold Cross-validation
+ More examples on bootstrap and cross-validation and errors
+ The same example but now with cross-validation
+ Cross-validation with Ridge
@@ -299,7 +301,7 @@ $$
15
16
...
- 40
+ 41
»
diff --git a/doc/pub/week37/html/._week37-bs007.html b/doc/pub/week37/html/._week37-bs007.html
index e6f4b3dd1..4c73ef313 100644
--- a/doc/pub/week37/html/._week37-bs007.html
+++ b/doc/pub/week37/html/._week37-bs007.html
@@ -69,6 +69,7 @@ Automatically generated HTML file from DocOnce source
2,
None,
'test-function-for-what-happens-with-ols-ridge-and-lasso'),
+ ('Rerunning the above code', 2, None, 'rerunning-the-above-code'),
("Invoking Bayes' theorem", 2, None, 'invoking-bayes-theorem'),
('Ridge and Bayes', 2, None, 'ridge-and-bayes'),
('Lasso and Bayes', 2, None, 'lasso-and-bayes'),
@@ -206,36 +207,37 @@ MathJax.Hub.Config({
Bayes' Theorem
Interpretations of Bayes' Theorem
Test Function for what happens with OLS, Ridge and Lasso
- Invoking Bayes' theorem
- Ridge and Bayes
- Lasso and Bayes
- Why resampling methods
- Resampling methods
- Resampling approaches can be computationally expensive
- Why resampling methods ?
- Statistical analysis
- Resampling methods
- Resampling methods: Jackknife and Bootstrap
- Resampling methods: Jackknife
- Jackknife code example
- Resampling methods: Bootstrap
- Resampling methods: Bootstrap background
- Resampling methods: More Bootstrap background
- Resampling methods: Bootstrap approach
- Resampling methods: Bootstrap steps
- Code example for the Bootstrap method
- The bias-variance tradeoff
- Example code for Bias-Variance tradeoff
- Understanding what happens
- Summing up
- Another Example from Scikit-Learn's Repository
- Various steps in cross-validation
- How to set up the cross-validation for Ridge and/or Lasso
- Cross-validation in brief
- Code Example for Cross-validation and \( k \)-fold Cross-validation
- More examples on bootstrap and cross-validation and errors
- The same example but now with cross-validation
- Cross-validation with Ridge
+ Rerunning the above code
+ Invoking Bayes' theorem
+ Ridge and Bayes
+ Lasso and Bayes
+ Why resampling methods
+ Resampling methods
+ Resampling approaches can be computationally expensive
+ Why resampling methods ?
+ Statistical analysis
+ Resampling methods
+ Resampling methods: Jackknife and Bootstrap
+ Resampling methods: Jackknife
+ Jackknife code example
+ Resampling methods: Bootstrap
+ Resampling methods: Bootstrap background
+ Resampling methods: More Bootstrap background
+ Resampling methods: Bootstrap approach
+ Resampling methods: Bootstrap steps
+ Code example for the Bootstrap method
+ The bias-variance tradeoff
+ Example code for Bias-Variance tradeoff
+ Understanding what happens
+ Summing up
+ Another Example from Scikit-Learn's Repository
+ Various steps in cross-validation
+ How to set up the cross-validation for Ridge and/or Lasso
+ Cross-validation in brief
+ Code Example for Cross-validation and \( k \)-fold Cross-validation
+ More examples on bootstrap and cross-validation and errors
+ The same example but now with cross-validation
+ Cross-validation with Ridge
@@ -290,7 +292,7 @@ which is Bayes' theorem. It allows us to evaluate the uncertainty in in \( X \)
16
17
...
- 40
+ 41
»
diff --git a/doc/pub/week37/html/._week37-bs008.html b/doc/pub/week37/html/._week37-bs008.html
index 76401c449..97dd65b8f 100644
--- a/doc/pub/week37/html/._week37-bs008.html
+++ b/doc/pub/week37/html/._week37-bs008.html
@@ -69,6 +69,7 @@ Automatically generated HTML file from DocOnce source
2,
None,
'test-function-for-what-happens-with-ols-ridge-and-lasso'),
+ ('Rerunning the above code', 2, None, 'rerunning-the-above-code'),
("Invoking Bayes' theorem", 2, None, 'invoking-bayes-theorem'),
('Ridge and Bayes', 2, None, 'ridge-and-bayes'),
('Lasso and Bayes', 2, None, 'lasso-and-bayes'),
@@ -206,36 +207,37 @@ MathJax.Hub.Config({
Bayes' Theorem
Interpretations of Bayes' Theorem
Test Function for what happens with OLS, Ridge and Lasso
- Invoking Bayes' theorem
- Ridge and Bayes
- Lasso and Bayes
- Why resampling methods
- Resampling methods
- Resampling approaches can be computationally expensive
- Why resampling methods ?
- Statistical analysis
- Resampling methods
- Resampling methods: Jackknife and Bootstrap
- Resampling methods: Jackknife
- Jackknife code example
- Resampling methods: Bootstrap
- Resampling methods: Bootstrap background
- Resampling methods: More Bootstrap background
- Resampling methods: Bootstrap approach
- Resampling methods: Bootstrap steps
- Code example for the Bootstrap method
- The bias-variance tradeoff
- Example code for Bias-Variance tradeoff
- Understanding what happens
- Summing up
- Another Example from Scikit-Learn's Repository
- Various steps in cross-validation
- How to set up the cross-validation for Ridge and/or Lasso
- Cross-validation in brief
- Code Example for Cross-validation and \( k \)-fold Cross-validation
- More examples on bootstrap and cross-validation and errors
- The same example but now with cross-validation
- Cross-validation with Ridge
+ Rerunning the above code
+ Invoking Bayes' theorem
+ Ridge and Bayes
+ Lasso and Bayes
+ Why resampling methods
+ Resampling methods
+ Resampling approaches can be computationally expensive
+ Why resampling methods ?
+ Statistical analysis
+ Resampling methods
+ Resampling methods: Jackknife and Bootstrap
+ Resampling methods: Jackknife
+ Jackknife code example
+ Resampling methods: Bootstrap
+ Resampling methods: Bootstrap background
+ Resampling methods: More Bootstrap background
+ Resampling methods: Bootstrap approach
+ Resampling methods: Bootstrap steps
+ Code example for the Bootstrap method
+ The bias-variance tradeoff
+ Example code for Bias-Variance tradeoff
+ Understanding what happens
+ Summing up
+ Another Example from Scikit-Learn's Repository
+ Various steps in cross-validation
+ How to set up the cross-validation for Ridge and/or Lasso
+ Cross-validation in brief
+ Code Example for Cross-validation and \( k \)-fold Cross-validation
+ More examples on bootstrap and cross-validation and errors
+ The same example but now with cross-validation
+ Cross-validation with Ridge
@@ -286,7 +288,7 @@ The function \( p(X) \) on the right hand side is called the prior while the fun
17
18
...
- 40
+ 41
»
diff --git a/doc/pub/week37/html/._week37-bs009.html b/doc/pub/week37/html/._week37-bs009.html
index 9bf6f7345..dce65b3f3 100644
--- a/doc/pub/week37/html/._week37-bs009.html
+++ b/doc/pub/week37/html/._week37-bs009.html
@@ -69,6 +69,7 @@ Automatically generated HTML file from DocOnce source
2,
None,
'test-function-for-what-happens-with-ols-ridge-and-lasso'),
+ ('Rerunning the above code', 2, None, 'rerunning-the-above-code'),
("Invoking Bayes' theorem", 2, None, 'invoking-bayes-theorem'),
('Ridge and Bayes', 2, None, 'ridge-and-bayes'),
('Lasso and Bayes', 2, None, 'lasso-and-bayes'),
@@ -206,36 +207,37 @@ MathJax.Hub.Config({
Bayes' Theorem
Interpretations of Bayes' Theorem
Test Function for what happens with OLS, Ridge and Lasso
- Invoking Bayes' theorem
- Ridge and Bayes
- Lasso and Bayes
- Why resampling methods
- Resampling methods
- Resampling approaches can be computationally expensive
- Why resampling methods ?
- Statistical analysis
- Resampling methods
- Resampling methods: Jackknife and Bootstrap
- Resampling methods: Jackknife
- Jackknife code example
- Resampling methods: Bootstrap
- Resampling methods: Bootstrap background
- Resampling methods: More Bootstrap background
- Resampling methods: Bootstrap approach
- Resampling methods: Bootstrap steps
- Code example for the Bootstrap method
- The bias-variance tradeoff
- Example code for Bias-Variance tradeoff
- Understanding what happens
- Summing up
- Another Example from Scikit-Learn's Repository
- Various steps in cross-validation
- How to set up the cross-validation for Ridge and/or Lasso
- Cross-validation in brief
- Code Example for Cross-validation and \( k \)-fold Cross-validation
- More examples on bootstrap and cross-validation and errors
- The same example but now with cross-validation
- Cross-validation with Ridge
+ Rerunning the above code
+ Invoking Bayes' theorem
+ Ridge and Bayes
+ Lasso and Bayes
+ Why resampling methods
+ Resampling methods
+ Resampling approaches can be computationally expensive
+ Why resampling methods ?
+ Statistical analysis
+ Resampling methods
+ Resampling methods: Jackknife and Bootstrap
+ Resampling methods: Jackknife
+ Jackknife code example
+ Resampling methods: Bootstrap
+ Resampling methods: Bootstrap background
+ Resampling methods: More Bootstrap background
+ Resampling methods: Bootstrap approach
+ Resampling methods: Bootstrap steps
+ Code example for the Bootstrap method
+ The bias-variance tradeoff
+ Example code for Bias-Variance tradeoff
+ Understanding what happens
+ Summing up
+ Another Example from Scikit-Learn's Repository
+ Various steps in cross-validation
+ How to set up the cross-validation for Ridge and/or Lasso
+ Cross-validation in brief
+ Code Example for Cross-validation and \( k \)-fold Cross-validation
+ More examples on bootstrap and cross-validation and errors
+ The same example but now with cross-validation
+ Cross-validation with Ridge
@@ -358,7 +360,7 @@ How can we understand this?
18
19
...
- 40
+ 41
»
diff --git a/doc/pub/week37/html/._week37-bs010.html b/doc/pub/week37/html/._week37-bs010.html
index 72009680d..9bc442eba 100644
--- a/doc/pub/week37/html/._week37-bs010.html
+++ b/doc/pub/week37/html/._week37-bs010.html
@@ -69,6 +69,7 @@ Automatically generated HTML file from DocOnce source
2,
None,
'test-function-for-what-happens-with-ols-ridge-and-lasso'),
+ ('Rerunning the above code', 2, None, 'rerunning-the-above-code'),
("Invoking Bayes' theorem", 2, None, 'invoking-bayes-theorem'),
('Ridge and Bayes', 2, None, 'ridge-and-bayes'),
('Lasso and Bayes', 2, None, 'lasso-and-bayes'),
@@ -206,36 +207,37 @@ MathJax.Hub.Config({
Bayes' Theorem
Interpretations of Bayes' Theorem
Test Function for what happens with OLS, Ridge and Lasso
- Invoking Bayes' theorem
- Ridge and Bayes
- Lasso and Bayes
- Why resampling methods
- Resampling methods
- Resampling approaches can be computationally expensive
- Why resampling methods ?
- Statistical analysis
- Resampling methods
- Resampling methods: Jackknife and Bootstrap
- Resampling methods: Jackknife
- Jackknife code example
- Resampling methods: Bootstrap
- Resampling methods: Bootstrap background
- Resampling methods: More Bootstrap background
- Resampling methods: Bootstrap approach
- Resampling methods: Bootstrap steps
- Code example for the Bootstrap method
- The bias-variance tradeoff
- Example code for Bias-Variance tradeoff
- Understanding what happens
- Summing up
- Another Example from Scikit-Learn's Repository
- Various steps in cross-validation
- How to set up the cross-validation for Ridge and/or Lasso
- Cross-validation in brief
- Code Example for Cross-validation and \( k \)-fold Cross-validation
- More examples on bootstrap and cross-validation and errors
- The same example but now with cross-validation
- Cross-validation with Ridge
+ Rerunning the above code
+ Invoking Bayes' theorem
+ Ridge and Bayes
+ Lasso and Bayes
+ Why resampling methods
+ Resampling methods
+ Resampling approaches can be computationally expensive
+ Why resampling methods ?
+ Statistical analysis
+ Resampling methods
+ Resampling methods: Jackknife and Bootstrap
+ Resampling methods: Jackknife
+ Jackknife code example
+ Resampling methods: Bootstrap
+ Resampling methods: Bootstrap background
+ Resampling methods: More Bootstrap background
+ Resampling methods: Bootstrap approach
+ Resampling methods: Bootstrap steps
+ Code example for the Bootstrap method
+ The bias-variance tradeoff
+ Example code for Bias-Variance tradeoff
+ Understanding what happens
+ Summing up
+ Another Example from Scikit-Learn's Repository
+ Various steps in cross-validation
+ How to set up the cross-validation for Ridge and/or Lasso
+ Cross-validation in brief
+ Code Example for Cross-validation and \( k \)-fold Cross-validation
+ More examples on bootstrap and cross-validation and errors
+ The same example but now with cross-validation
+ Cross-validation with Ridge
@@ -251,38 +253,72 @@ MathJax.Hub.Config({
-Invoking Bayes' theorem
+Rerunning the above code
-Using Bayes' theorem we can gain a better intuition about Ridge and Lasso regression.
+Let us write out the values of the coefficients \( \beta_i \) as functions
+of the polynomial degree and noise. We will focus only on the Ridge
+results and some few selected values of the hyperparameter \( \lambda \).
-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]}.
-$$
+If we don't include any noise and run this code for different values
+of the polynomial degree, we notice that the results for \( \beta_i \) do
+not show great changes from one order to the next. This is an
+indication that for higher polynomial orders, our parameters become
+less important.
-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
-
-$$
-p(\boldsymbol{\beta}\vert\boldsymbol{D}).
-$$
+If we however add noise, what happens is that the polynomial fit is
+trying to adjust the fit to traverse in the best possible way all data
+points. This can lead to large fluctuations in the parameters
+\( \beta_i \) as functions of polynomial order. It will also be reflected
+in a larger value of the variance of each parameter \( \beta_i \). What
+Ridge regression (and Lasso as well) are doing then is to try to
+quench the fluctuations in the parameters of \( \beta_i \) which have a
+large variance (normally for higher orders in the polynomial).
-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}).
-$$
-
-We have a model for \( p(\boldsymbol{D}\vert\boldsymbol{\beta}) \) but need one for the prior \( p(\boldsymbol{\beta} \)!
+
+
import numpy as np
+import pandas as pd
+from IPython.display import display
+import matplotlib.pyplot as plt
+from sklearn.model_selection import train_test_split
+from sklearn import linear_model
+# Make data set.
+n = 1000
+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)
+
+# Decide which values of lambda to use
+nlambdas = 5
+lambdas = np.logspace(-3, 2, nlambdas)
+for i in range(nlambdas):
+ lmb = lambdas[i]
+ # Make the fit using Ridge only
+ RegRidge = linear_model.Ridge(lmb,fit_intercept=False)
+ RegRidge.fit(X_train,y_train)
+ # and then make the prediction
+ ypredictRidge = RegRidge.predict(X_test)
+ Coeffs = np.array(RegRidge.coef_)
+ BetaValues = pd.DataFrame(Coeffs)
+ BetaValues.columns = ['beta']
+ display(BetaValues)
+
@@ -309,7 +345,7 @@ We have a model for \( p(\boldsymbol{D}\vert\boldsymbol{\beta}) \) but need one
19
20
...
- 40
+ 41
»
diff --git a/doc/pub/week37/html/._week37-bs011.html b/doc/pub/week37/html/._week37-bs011.html
index e95709ff6..a3ed3af59 100644
--- a/doc/pub/week37/html/._week37-bs011.html
+++ b/doc/pub/week37/html/._week37-bs011.html
@@ -69,6 +69,7 @@ Automatically generated HTML file from DocOnce source
2,
None,
'test-function-for-what-happens-with-ols-ridge-and-lasso'),
+ ('Rerunning the above code', 2, None, 'rerunning-the-above-code'),
("Invoking Bayes' theorem", 2, None, 'invoking-bayes-theorem'),
('Ridge and Bayes', 2, None, 'ridge-and-bayes'),
('Lasso and Bayes', 2, None, 'lasso-and-bayes'),
@@ -206,36 +207,37 @@ MathJax.Hub.Config({
Bayes' Theorem
Interpretations of Bayes' Theorem
Test Function for what happens with OLS, Ridge and Lasso
- Invoking Bayes' theorem
- Ridge and Bayes
- Lasso and Bayes
- Why resampling methods
- Resampling methods
- Resampling approaches can be computationally expensive
- Why resampling methods ?
- Statistical analysis
- Resampling methods
- Resampling methods: Jackknife and Bootstrap
- Resampling methods: Jackknife
- Jackknife code example
- Resampling methods: Bootstrap
- Resampling methods: Bootstrap background
- Resampling methods: More Bootstrap background
- Resampling methods: Bootstrap approach
- Resampling methods: Bootstrap steps
- Code example for the Bootstrap method
- The bias-variance tradeoff
- Example code for Bias-Variance tradeoff
- Understanding what happens
- Summing up
- Another Example from Scikit-Learn's Repository
- Various steps in cross-validation
- How to set up the cross-validation for Ridge and/or Lasso
- Cross-validation in brief
- Code Example for Cross-validation and \( k \)-fold Cross-validation
- More examples on bootstrap and cross-validation and errors
- The same example but now with cross-validation
- Cross-validation with Ridge
+ Rerunning the above code
+ Invoking Bayes' theorem
+ Ridge and Bayes
+ Lasso and Bayes
+ Why resampling methods
+ Resampling methods
+ Resampling approaches can be computationally expensive
+ Why resampling methods ?
+ Statistical analysis
+ Resampling methods
+ Resampling methods: Jackknife and Bootstrap
+ Resampling methods: Jackknife
+ Jackknife code example
+ Resampling methods: Bootstrap
+ Resampling methods: Bootstrap background
+ Resampling methods: More Bootstrap background
+ Resampling methods: Bootstrap approach
+ Resampling methods: Bootstrap steps
+ Code example for the Bootstrap method
+ The bias-variance tradeoff
+ Example code for Bias-Variance tradeoff
+ Understanding what happens
+ Summing up
+ Another Example from Scikit-Learn's Repository
+ Various steps in cross-validation
+ How to set up the cross-validation for Ridge and/or Lasso
+ Cross-validation in brief
+ Code Example for Cross-validation and \( k \)-fold Cross-validation
+ More examples on bootstrap and cross-validation and errors
+ The same example but now with cross-validation
+ Cross-validation with Ridge
@@ -251,43 +253,37 @@ MathJax.Hub.Config({
-Ridge and Bayes
+Invoking Bayes' theorem
-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.
+Using Bayes' theorem we can gain a better intuition about Ridge and Lasso regression.
-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
-
+For ordinary least squares we postulated that the maximum likelihood for the doamin of events \( \boldsymbol{D} \) (one-dimensional case)
$$
-p(\boldsymbol{\beta})=\prod_{j=0}^{p-1}\exp{\left(-\frac{\beta_j^2}{2\tau^2}\right)}.
+\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]}.
$$
-Our posterior probability becomes then (omitting the normalization factor which is just a constant)
+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
+
$$
-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}).
$$
-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
-
+Bayes' theorem comes to our rescue here since (omitting the normalization constant)
$$
-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,
+p(\boldsymbol{\beta}\vert\boldsymbol{D})\propto p(\boldsymbol{D}\vert\boldsymbol{\beta})p(\boldsymbol{\beta}).
$$
-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?
+
+We have a model for \( p(\boldsymbol{D}\vert\boldsymbol{\beta}) \) but need one for the prior \( p(\boldsymbol{\beta} \)!
@@ -315,7 +311,7 @@ which is our Ridge cost function! Nice, isn't it?
20
21
...
- 40
+ 41
»
diff --git a/doc/pub/week37/html/._week37-bs012.html b/doc/pub/week37/html/._week37-bs012.html
index 928e0b053..a103da9ed 100644
--- a/doc/pub/week37/html/._week37-bs012.html
+++ b/doc/pub/week37/html/._week37-bs012.html
@@ -69,6 +69,7 @@ Automatically generated HTML file from DocOnce source
2,
None,
'test-function-for-what-happens-with-ols-ridge-and-lasso'),
+ ('Rerunning the above code', 2, None, 'rerunning-the-above-code'),
("Invoking Bayes' theorem", 2, None, 'invoking-bayes-theorem'),
('Ridge and Bayes', 2, None, 'ridge-and-bayes'),
('Lasso and Bayes', 2, None, 'lasso-and-bayes'),
@@ -206,36 +207,37 @@ MathJax.Hub.Config({
Bayes' Theorem
Interpretations of Bayes' Theorem
Test Function for what happens with OLS, Ridge and Lasso
- Invoking Bayes' theorem
- Ridge and Bayes
- Lasso and Bayes
- Why resampling methods
- Resampling methods
- Resampling approaches can be computationally expensive
- Why resampling methods ?
- Statistical analysis
- Resampling methods
- Resampling methods: Jackknife and Bootstrap
- Resampling methods: Jackknife
- Jackknife code example
- Resampling methods: Bootstrap
- Resampling methods: Bootstrap background
- Resampling methods: More Bootstrap background
- Resampling methods: Bootstrap approach
- Resampling methods: Bootstrap steps
- Code example for the Bootstrap method
- The bias-variance tradeoff
- Example code for Bias-Variance tradeoff
- Understanding what happens
- Summing up
- Another Example from Scikit-Learn's Repository
- Various steps in cross-validation
- How to set up the cross-validation for Ridge and/or Lasso
- Cross-validation in brief
- Code Example for Cross-validation and \( k \)-fold Cross-validation
- More examples on bootstrap and cross-validation and errors
- The same example but now with cross-validation
- Cross-validation with Ridge
+ Rerunning the above code
+ Invoking Bayes' theorem
+ Ridge and Bayes
+ Lasso and Bayes
+ Why resampling methods
+ Resampling methods
+ Resampling approaches can be computationally expensive
+ Why resampling methods ?
+ Statistical analysis
+ Resampling methods
+ Resampling methods: Jackknife and Bootstrap
+ Resampling methods: Jackknife
+ Jackknife code example
+ Resampling methods: Bootstrap
+ Resampling methods: Bootstrap background
+ Resampling methods: More Bootstrap background
+ Resampling methods: Bootstrap approach
+ Resampling methods: Bootstrap steps
+ Code example for the Bootstrap method
+ The bias-variance tradeoff
+ Example code for Bias-Variance tradeoff
+ Understanding what happens
+ Summing up
+ Another Example from Scikit-Learn's Repository
+ Various steps in cross-validation
+ How to set up the cross-validation for Ridge and/or Lasso
+ Cross-validation in brief
+ Code Example for Cross-validation and \( k \)-fold Cross-validation
+ More examples on bootstrap and cross-validation and errors
+ The same example but now with cross-validation
+ Cross-validation with Ridge
@@ -251,37 +253,43 @@ MathJax.Hub.Config({
-Lasso and Bayes
+Ridge and Bayes
-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
+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
$$
-p(\boldsymbol{\beta})=\prod_{j=0}^{p-1}\exp{\left(-\frac{\vert\beta_j\vert}{\tau}\right)}.
+p(\boldsymbol{\beta})=\prod_{j=0}^{p-1}\exp{\left(-\frac{\beta_j^2}{2\tau^2}\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{\vert\beta_j\vert}{\tau}\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{\beta_j^2}{2\tau^2}\right)}.
$$
-Taking the negative
-logarithm of the posterior probability and leaving out the
+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}{\tau}\vert\vert\boldsymbol{\beta}\vert\vert_1,
+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/\tau \) with \( \lambda \) we have
+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_1,
+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 Lasso cost function!
+which is our Ridge cost function! Nice, isn't it?
@@ -309,7 +317,7 @@ which is our Lasso cost function!
21
22
...
- 40
+ 41
»
diff --git a/doc/pub/week37/html/._week37-bs013.html b/doc/pub/week37/html/._week37-bs013.html
index 32ee0175e..3f25daeba 100644
--- a/doc/pub/week37/html/._week37-bs013.html
+++ b/doc/pub/week37/html/._week37-bs013.html
@@ -69,6 +69,7 @@ Automatically generated HTML file from DocOnce source
2,
None,
'test-function-for-what-happens-with-ols-ridge-and-lasso'),
+ ('Rerunning the above code', 2, None, 'rerunning-the-above-code'),
("Invoking Bayes' theorem", 2, None, 'invoking-bayes-theorem'),
('Ridge and Bayes', 2, None, 'ridge-and-bayes'),
('Lasso and Bayes', 2, None, 'lasso-and-bayes'),
@@ -206,36 +207,37 @@ MathJax.Hub.Config({
Bayes' Theorem
Interpretations of Bayes' Theorem
Test Function for what happens with OLS, Ridge and Lasso
- Invoking Bayes' theorem
- Ridge and Bayes
- Lasso and Bayes
- Why resampling methods
- Resampling methods
- Resampling approaches can be computationally expensive
- Why resampling methods ?
- Statistical analysis
- Resampling methods
- Resampling methods: Jackknife and Bootstrap
- Resampling methods: Jackknife
- Jackknife code example
- Resampling methods: Bootstrap
- Resampling methods: Bootstrap background
- Resampling methods: More Bootstrap background
- Resampling methods: Bootstrap approach
- Resampling methods: Bootstrap steps
- Code example for the Bootstrap method
- The bias-variance tradeoff
- Example code for Bias-Variance tradeoff
- Understanding what happens
- Summing up
- Another Example from Scikit-Learn's Repository
- Various steps in cross-validation
- How to set up the cross-validation for Ridge and/or Lasso
- Cross-validation in brief
- Code Example for Cross-validation and \( k \)-fold Cross-validation
- More examples on bootstrap and cross-validation and errors
- The same example but now with cross-validation
- Cross-validation with Ridge
+ Rerunning the above code
+ Invoking Bayes' theorem
+ Ridge and Bayes
+ Lasso and Bayes
+ Why resampling methods
+ Resampling methods
+ Resampling approaches can be computationally expensive
+ Why resampling methods ?
+ Statistical analysis
+ Resampling methods
+ Resampling methods: Jackknife and Bootstrap
+ Resampling methods: Jackknife
+ Jackknife code example
+ Resampling methods: Bootstrap
+ Resampling methods: Bootstrap background
+ Resampling methods: More Bootstrap background
+ Resampling methods: Bootstrap approach
+ Resampling methods: Bootstrap steps
+ Code example for the Bootstrap method
+ The bias-variance tradeoff
+ Example code for Bias-Variance tradeoff
+ Understanding what happens
+ Summing up
+ Another Example from Scikit-Learn's Repository
+ Various steps in cross-validation
+ How to set up the cross-validation for Ridge and/or Lasso
+ Cross-validation in brief
+ Code Example for Cross-validation and \( k \)-fold Cross-validation
+ More examples on bootstrap and cross-validation and errors
+ The same example but now with cross-validation
+ Cross-validation with Ridge
@@ -251,19 +253,37 @@ MathJax.Hub.Config({
-Why resampling methods
+Lasso and Bayes
-Before we proceed, we need to rethink what we have been doing. In our
-eager to fit the data, we have omitted several important elements in
-our regression analysis. In what follows we will
+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
-
-- look at statistical properties, including a discussion of mean values, variance and the so-called bias-variance tradeoff
-- introduce resampling techniques like cross-validation, bootstrapping and jackknife and more
-
+$$
+p(\boldsymbol{\beta})=\prod_{j=0}^{p-1}\exp{\left(-\frac{\vert\beta_j\vert}{\tau}\right)}.
+$$
-and discuss how to select a given model (one of the difficult parts in machine learning).
+
+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{\vert\beta_j\vert}{\tau}\right)}.
+$$
+
+
+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}{\tau}\vert\vert\boldsymbol{\beta}\vert\vert_1,
+$$
+
+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_1,
+$$
+
+which is our Lasso cost function!
@@ -291,7 +311,7 @@ and discuss how to select a given model (one of the difficult parts in machine l
22
23
...
- 40
+ 41
»
diff --git a/doc/pub/week37/html/._week37-bs014.html b/doc/pub/week37/html/._week37-bs014.html
index e258c9911..7099f3c8a 100644
--- a/doc/pub/week37/html/._week37-bs014.html
+++ b/doc/pub/week37/html/._week37-bs014.html
@@ -69,6 +69,7 @@ Automatically generated HTML file from DocOnce source
2,
None,
'test-function-for-what-happens-with-ols-ridge-and-lasso'),
+ ('Rerunning the above code', 2, None, 'rerunning-the-above-code'),
("Invoking Bayes' theorem", 2, None, 'invoking-bayes-theorem'),
('Ridge and Bayes', 2, None, 'ridge-and-bayes'),
('Lasso and Bayes', 2, None, 'lasso-and-bayes'),
@@ -206,36 +207,37 @@ MathJax.Hub.Config({
Bayes' Theorem
Interpretations of Bayes' Theorem
Test Function for what happens with OLS, Ridge and Lasso
- Invoking Bayes' theorem
- Ridge and Bayes
- Lasso and Bayes
- Why resampling methods
- Resampling methods
- Resampling approaches can be computationally expensive
- Why resampling methods ?
- Statistical analysis
- Resampling methods
- Resampling methods: Jackknife and Bootstrap
- Resampling methods: Jackknife
- Jackknife code example
- Resampling methods: Bootstrap
- Resampling methods: Bootstrap background
- Resampling methods: More Bootstrap background
- Resampling methods: Bootstrap approach
- Resampling methods: Bootstrap steps
- Code example for the Bootstrap method
- The bias-variance tradeoff
- Example code for Bias-Variance tradeoff
- Understanding what happens
- Summing up
- Another Example from Scikit-Learn's Repository
- Various steps in cross-validation
- How to set up the cross-validation for Ridge and/or Lasso
- Cross-validation in brief
- Code Example for Cross-validation and \( k \)-fold Cross-validation
- More examples on bootstrap and cross-validation and errors
- The same example but now with cross-validation
- Cross-validation with Ridge
+ Rerunning the above code
+ Invoking Bayes' theorem
+ Ridge and Bayes
+ Lasso and Bayes
+ Why resampling methods
+ Resampling methods
+ Resampling approaches can be computationally expensive
+ Why resampling methods ?
+ Statistical analysis
+ Resampling methods
+ Resampling methods: Jackknife and Bootstrap
+ Resampling methods: Jackknife
+ Jackknife code example
+ Resampling methods: Bootstrap
+ Resampling methods: Bootstrap background
+ Resampling methods: More Bootstrap background
+ Resampling methods: Bootstrap approach
+ Resampling methods: Bootstrap steps
+ Code example for the Bootstrap method
+ The bias-variance tradeoff
+ Example code for Bias-Variance tradeoff
+ Understanding what happens
+ Summing up
+ Another Example from Scikit-Learn's Repository
+ Various steps in cross-validation
+ How to set up the cross-validation for Ridge and/or Lasso
+ Cross-validation in brief
+ Code Example for Cross-validation and \( k \)-fold Cross-validation
+ More examples on bootstrap and cross-validation and errors
+ The same example but now with cross-validation
+ Cross-validation with Ridge
@@ -251,36 +253,19 @@ MathJax.Hub.Config({
-Resampling methods
-
-
-
-Resampling methods are an indispensable tool in modern
-statistics. They involve repeatedly drawing samples from a training
-set and refitting a model of interest on each sample in order to
-obtain additional information about the fitted model. For example, in
-order to estimate the variability of a linear regression fit, we can
-repeatedly draw different samples from the training data, fit a linear
-regression to each new sample, and then examine the extent to which
-the resulting fits differ. Such an approach may allow us to obtain
-information that would not be available from fitting the model only
-once using the original training sample.
+
Why resampling methods
-Two resampling methods are often used in Machine Learning analyses,
+Before we proceed, we need to rethink what we have been doing. In our
+eager to fit the data, we have omitted several important elements in
+our regression analysis. In what follows we will
-- The bootstrap method
-- and Cross-Validation
+- look at statistical properties, including a discussion of mean values, variance and the so-called bias-variance tradeoff
+- introduce resampling techniques like cross-validation, bootstrapping and jackknife and more
-In addition there are several other methods such as the Jackknife and the Blocking methods. We will discuss in particular
-cross-validation and the bootstrap method.
-
-
-
-
-
+and discuss how to select a given model (one of the difficult parts in machine learning).
@@ -308,7 +293,7 @@ cross-validation and the bootstrap method.
23
24
...
- 40
+ 41
»
diff --git a/doc/pub/week37/html/._week37-bs015.html b/doc/pub/week37/html/._week37-bs015.html
index 253f9f40e..f89038534 100644
--- a/doc/pub/week37/html/._week37-bs015.html
+++ b/doc/pub/week37/html/._week37-bs015.html
@@ -69,6 +69,7 @@ Automatically generated HTML file from DocOnce source
2,
None,
'test-function-for-what-happens-with-ols-ridge-and-lasso'),
+ ('Rerunning the above code', 2, None, 'rerunning-the-above-code'),
("Invoking Bayes' theorem", 2, None, 'invoking-bayes-theorem'),
('Ridge and Bayes', 2, None, 'ridge-and-bayes'),
('Lasso and Bayes', 2, None, 'lasso-and-bayes'),
@@ -206,36 +207,37 @@ MathJax.Hub.Config({
Bayes' Theorem
Interpretations of Bayes' Theorem
Test Function for what happens with OLS, Ridge and Lasso
- Invoking Bayes' theorem
- Ridge and Bayes
- Lasso and Bayes
- Why resampling methods
- Resampling methods
- Resampling approaches can be computationally expensive
- Why resampling methods ?
- Statistical analysis
- Resampling methods
- Resampling methods: Jackknife and Bootstrap
- Resampling methods: Jackknife
- Jackknife code example
- Resampling methods: Bootstrap
- Resampling methods: Bootstrap background
- Resampling methods: More Bootstrap background
- Resampling methods: Bootstrap approach
- Resampling methods: Bootstrap steps
- Code example for the Bootstrap method
- The bias-variance tradeoff
- Example code for Bias-Variance tradeoff
- Understanding what happens
- Summing up
- Another Example from Scikit-Learn's Repository
- Various steps in cross-validation
- How to set up the cross-validation for Ridge and/or Lasso
- Cross-validation in brief
- Code Example for Cross-validation and \( k \)-fold Cross-validation
- More examples on bootstrap and cross-validation and errors
- The same example but now with cross-validation
- Cross-validation with Ridge
+ Rerunning the above code
+ Invoking Bayes' theorem
+ Ridge and Bayes
+ Lasso and Bayes
+ Why resampling methods
+ Resampling methods
+ Resampling approaches can be computationally expensive
+ Why resampling methods ?
+ Statistical analysis
+ Resampling methods
+ Resampling methods: Jackknife and Bootstrap
+ Resampling methods: Jackknife
+ Jackknife code example
+ Resampling methods: Bootstrap
+ Resampling methods: Bootstrap background
+ Resampling methods: More Bootstrap background
+ Resampling methods: Bootstrap approach
+ Resampling methods: Bootstrap steps
+ Code example for the Bootstrap method
+ The bias-variance tradeoff
+ Example code for Bias-Variance tradeoff
+ Understanding what happens
+ Summing up
+ Another Example from Scikit-Learn's Repository
+ Various steps in cross-validation
+ How to set up the cross-validation for Ridge and/or Lasso
+ Cross-validation in brief
+ Code Example for Cross-validation and \( k \)-fold Cross-validation
+ More examples on bootstrap and cross-validation and errors
+ The same example but now with cross-validation
+ Cross-validation with Ridge
@@ -251,27 +253,31 @@ MathJax.Hub.Config({
-Resampling approaches can be computationally expensive
+Resampling methods
+Resampling methods are an indispensable tool in modern
+statistics. They involve repeatedly drawing samples from a training
+set and refitting a model of interest on each sample in order to
+obtain additional information about the fitted model. For example, in
+order to estimate the variability of a linear regression fit, we can
+repeatedly draw different samples from the training data, fit a linear
+regression to each new sample, and then examine the extent to which
+the resulting fits differ. Such an approach may allow us to obtain
+information that would not be available from fitting the model only
+once using the original training sample.
-Resampling approaches can be computationally expensive, because they
-involve fitting the same statistical method multiple times using
-different subsets of the training data. However, due to recent
-advances in computing power, the computational requirements of
-resampling methods generally are not prohibitive. In this chapter, we
-discuss two of the most commonly used resampling methods,
-cross-validation and the bootstrap. Both methods are important tools
-in the practical application of many statistical learning
-procedures. For example, cross-validation can be used to estimate the
-test error associated with a given statistical learning method in
-order to evaluate its performance, or to select the appropriate level
-of flexibility. The process of evaluating a model’s performance is
-known as model assessment, whereas the process of selecting the proper
-level of flexibility for a model is known as model selection. The
-bootstrap is widely used.
+Two resampling methods are often used in Machine Learning analyses,
+
+
+- The bootstrap method
+- and Cross-Validation
+
+
+In addition there are several other methods such as the Jackknife and the Blocking methods. We will discuss in particular
+cross-validation and the bootstrap method.
@@ -304,7 +310,7 @@ bootstrap is widely used.
24
25
...
-
40
+
41
»
diff --git a/doc/pub/week37/html/._week37-bs016.html b/doc/pub/week37/html/._week37-bs016.html
index d4d915059..824aaf17d 100644
--- a/doc/pub/week37/html/._week37-bs016.html
+++ b/doc/pub/week37/html/._week37-bs016.html
@@ -69,6 +69,7 @@ Automatically generated HTML file from DocOnce source
2,
None,
'test-function-for-what-happens-with-ols-ridge-and-lasso'),
+ ('Rerunning the above code', 2, None, 'rerunning-the-above-code'),
("Invoking Bayes' theorem", 2, None, 'invoking-bayes-theorem'),
('Ridge and Bayes', 2, None, 'ridge-and-bayes'),
('Lasso and Bayes', 2, None, 'lasso-and-bayes'),
@@ -206,36 +207,37 @@ MathJax.Hub.Config({
Bayes' Theorem
Interpretations of Bayes' Theorem
Test Function for what happens with OLS, Ridge and Lasso
-
Invoking Bayes' theorem
-
Ridge and Bayes
-
Lasso and Bayes
-
Why resampling methods
-
Resampling methods
-
Resampling approaches can be computationally expensive
-
Why resampling methods ?
-
Statistical analysis
-
Resampling methods
-
Resampling methods: Jackknife and Bootstrap
-
Resampling methods: Jackknife
-
Jackknife code example
-
Resampling methods: Bootstrap
-
Resampling methods: Bootstrap background
-
Resampling methods: More Bootstrap background
-
Resampling methods: Bootstrap approach
-
Resampling methods: Bootstrap steps
-
Code example for the Bootstrap method
-
The bias-variance tradeoff
-
Example code for Bias-Variance tradeoff
-
Understanding what happens
-
Summing up
-
Another Example from Scikit-Learn's Repository
-
Various steps in cross-validation
-
How to set up the cross-validation for Ridge and/or Lasso
-
Cross-validation in brief
-
Code Example for Cross-validation and \( k \)-fold Cross-validation
-
More examples on bootstrap and cross-validation and errors
-
The same example but now with cross-validation
-
Cross-validation with Ridge
+
Rerunning the above code
+
Invoking Bayes' theorem
+
Ridge and Bayes
+
Lasso and Bayes
+
Why resampling methods
+
Resampling methods
+
Resampling approaches can be computationally expensive
+
Why resampling methods ?
+
Statistical analysis
+
Resampling methods
+
Resampling methods: Jackknife and Bootstrap
+
Resampling methods: Jackknife
+
Jackknife code example
+
Resampling methods: Bootstrap
+
Resampling methods: Bootstrap background
+
Resampling methods: More Bootstrap background
+
Resampling methods: Bootstrap approach
+
Resampling methods: Bootstrap steps
+
Code example for the Bootstrap method
+
The bias-variance tradeoff
+
Example code for Bias-Variance tradeoff
+
Understanding what happens
+
Summing up
+
Another Example from Scikit-Learn's Repository
+
Various steps in cross-validation
+
How to set up the cross-validation for Ridge and/or Lasso
+
Cross-validation in brief
+
Code Example for Cross-validation and \( k \)-fold Cross-validation
+
More examples on bootstrap and cross-validation and errors
+
The same example but now with cross-validation
+
Cross-validation with Ridge
@@ -251,16 +253,29 @@ MathJax.Hub.Config({
-
Why resampling methods ?
+
Resampling approaches can be computationally expensive
-
-- Our simulations can be treated as computer experiments. This is particularly the case for Monte Carlo methods which are widely used in statistical analyses.
-- The results can be analysed with the same statistical tools as we would use when analysing experimental data.
-- As in all experiments, we are looking for expectation values and an estimate of how accurate they are, i.e., possible sources for errors.
-
+
+Resampling approaches can be computationally expensive, because they
+involve fitting the same statistical method multiple times using
+different subsets of the training data. However, due to recent
+advances in computing power, the computational requirements of
+resampling methods generally are not prohibitive. In this chapter, we
+discuss two of the most commonly used resampling methods,
+cross-validation and the bootstrap. Both methods are important tools
+in the practical application of many statistical learning
+procedures. For example, cross-validation can be used to estimate the
+test error associated with a given statistical learning method in
+order to evaluate its performance, or to select the appropriate level
+of flexibility. The process of evaluating a model’s performance is
+known as model assessment, whereas the process of selecting the proper
+level of flexibility for a model is known as model selection. The
+bootstrap is widely used.
+
+
@@ -291,7 +306,7 @@ MathJax.Hub.Config({
25
26
...
-
40
+
41
»
diff --git a/doc/pub/week37/html/._week37-bs017.html b/doc/pub/week37/html/._week37-bs017.html
index 0944b0fb7..ba3745d38 100644
--- a/doc/pub/week37/html/._week37-bs017.html
+++ b/doc/pub/week37/html/._week37-bs017.html
@@ -69,6 +69,7 @@ Automatically generated HTML file from DocOnce source
2,
None,
'test-function-for-what-happens-with-ols-ridge-and-lasso'),
+ ('Rerunning the above code', 2, None, 'rerunning-the-above-code'),
("Invoking Bayes' theorem", 2, None, 'invoking-bayes-theorem'),
('Ridge and Bayes', 2, None, 'ridge-and-bayes'),
('Lasso and Bayes', 2, None, 'lasso-and-bayes'),
@@ -206,36 +207,37 @@ MathJax.Hub.Config({
Bayes' Theorem
Interpretations of Bayes' Theorem
Test Function for what happens with OLS, Ridge and Lasso
-
Invoking Bayes' theorem
-
Ridge and Bayes
-
Lasso and Bayes
-
Why resampling methods
-
Resampling methods
-
Resampling approaches can be computationally expensive
-
Why resampling methods ?
-
Statistical analysis
-
Resampling methods
-
Resampling methods: Jackknife and Bootstrap
-
Resampling methods: Jackknife
-
Jackknife code example
-
Resampling methods: Bootstrap
-
Resampling methods: Bootstrap background
-
Resampling methods: More Bootstrap background
-
Resampling methods: Bootstrap approach
-
Resampling methods: Bootstrap steps
-
Code example for the Bootstrap method
-
The bias-variance tradeoff
-
Example code for Bias-Variance tradeoff
-
Understanding what happens
-
Summing up
-
Another Example from Scikit-Learn's Repository
-
Various steps in cross-validation
-
How to set up the cross-validation for Ridge and/or Lasso
-
Cross-validation in brief
-
Code Example for Cross-validation and \( k \)-fold Cross-validation
-
More examples on bootstrap and cross-validation and errors
-
The same example but now with cross-validation
-
Cross-validation with Ridge
+
Rerunning the above code
+
Invoking Bayes' theorem
+
Ridge and Bayes
+
Lasso and Bayes
+
Why resampling methods
+
Resampling methods
+
Resampling approaches can be computationally expensive
+
Why resampling methods ?
+
Statistical analysis
+
Resampling methods
+
Resampling methods: Jackknife and Bootstrap
+
Resampling methods: Jackknife
+
Jackknife code example
+
Resampling methods: Bootstrap
+
Resampling methods: Bootstrap background
+
Resampling methods: More Bootstrap background
+
Resampling methods: Bootstrap approach
+
Resampling methods: Bootstrap steps
+
Code example for the Bootstrap method
+
The bias-variance tradeoff
+
Example code for Bias-Variance tradeoff
+
Understanding what happens
+
Summing up
+
Another Example from Scikit-Learn's Repository
+
Various steps in cross-validation
+
How to set up the cross-validation for Ridge and/or Lasso
+
Cross-validation in brief
+
Code Example for Cross-validation and \( k \)-fold Cross-validation
+
More examples on bootstrap and cross-validation and errors
+
The same example but now with cross-validation
+
Cross-validation with Ridge
@@ -251,21 +253,15 @@ MathJax.Hub.Config({
-
Statistical analysis
+
Why resampling methods ?
-- As in other experiments, many numerical experiments have two classes of errors:
-
-
- - Statistical errors
- - Systematical errors
-
-
-- Statistical errors can be estimated using standard tools from statistics
-- Systematical errors are method specific and must be treated differently from case to case.
+- Our simulations can be treated as computer experiments. This is particularly the case for Monte Carlo methods which are widely used in statistical analyses.
+- The results can be analysed with the same statistical tools as we would use when analysing experimental data.
+- As in all experiments, we are looking for expectation values and an estimate of how accurate they are, i.e., possible sources for errors.
@@ -297,7 +293,7 @@ MathJax.Hub.Config({
26
27
...
-
40
+
41
»
diff --git a/doc/pub/week37/html/._week37-bs018.html b/doc/pub/week37/html/._week37-bs018.html
index cf010edb8..88f6119bc 100644
--- a/doc/pub/week37/html/._week37-bs018.html
+++ b/doc/pub/week37/html/._week37-bs018.html
@@ -69,6 +69,7 @@ Automatically generated HTML file from DocOnce source
2,
None,
'test-function-for-what-happens-with-ols-ridge-and-lasso'),
+ ('Rerunning the above code', 2, None, 'rerunning-the-above-code'),
("Invoking Bayes' theorem", 2, None, 'invoking-bayes-theorem'),
('Ridge and Bayes', 2, None, 'ridge-and-bayes'),
('Lasso and Bayes', 2, None, 'lasso-and-bayes'),
@@ -206,36 +207,37 @@ MathJax.Hub.Config({
Bayes' Theorem
Interpretations of Bayes' Theorem
Test Function for what happens with OLS, Ridge and Lasso
-
Invoking Bayes' theorem
-
Ridge and Bayes
-
Lasso and Bayes
-
Why resampling methods
-
Resampling methods
-
Resampling approaches can be computationally expensive
-
Why resampling methods ?
-
Statistical analysis
-
Resampling methods
-
Resampling methods: Jackknife and Bootstrap
-
Resampling methods: Jackknife
-
Jackknife code example
-
Resampling methods: Bootstrap
-
Resampling methods: Bootstrap background
-
Resampling methods: More Bootstrap background
-
Resampling methods: Bootstrap approach
-
Resampling methods: Bootstrap steps
-
Code example for the Bootstrap method
-
The bias-variance tradeoff
-
Example code for Bias-Variance tradeoff
-
Understanding what happens
-
Summing up
-
Another Example from Scikit-Learn's Repository
-
Various steps in cross-validation
-
How to set up the cross-validation for Ridge and/or Lasso
-
Cross-validation in brief
-
Code Example for Cross-validation and \( k \)-fold Cross-validation
-
More examples on bootstrap and cross-validation and errors
-
The same example but now with cross-validation
-
Cross-validation with Ridge
+
Rerunning the above code
+
Invoking Bayes' theorem
+
Ridge and Bayes
+
Lasso and Bayes
+
Why resampling methods
+
Resampling methods
+
Resampling approaches can be computationally expensive
+
Why resampling methods ?
+
Statistical analysis
+
Resampling methods
+
Resampling methods: Jackknife and Bootstrap
+
Resampling methods: Jackknife
+
Jackknife code example
+
Resampling methods: Bootstrap
+
Resampling methods: Bootstrap background
+
Resampling methods: More Bootstrap background
+
Resampling methods: Bootstrap approach
+
Resampling methods: Bootstrap steps
+
Code example for the Bootstrap method
+
The bias-variance tradeoff
+
Example code for Bias-Variance tradeoff
+
Understanding what happens
+
Summing up
+
Another Example from Scikit-Learn's Repository
+
Various steps in cross-validation
+
How to set up the cross-validation for Ridge and/or Lasso
+
Cross-validation in brief
+
Code Example for Cross-validation and \( k \)-fold Cross-validation
+
More examples on bootstrap and cross-validation and errors
+
The same example but now with cross-validation
+
Cross-validation with Ridge
@@ -251,31 +253,25 @@ MathJax.Hub.Config({
-
Resampling methods
+
Statistical analysis
+
+
+
-
-With all these analytical equations for both the OLS and Ridge
-regression, we will now outline how to assess a given model. This will
-lead to a discussion of the so-called bias-variance tradeoff (see
-below) and so-called resampling methods.
+
+- As in other experiments, many numerical experiments have two classes of errors:
-
-One of the quantities we have discussed as a way to measure errors is
-the mean-squared error (MSE), mainly used for fitting of continuous
-functions. Another choice is the absolute error.
+
+ - Statistical errors
+ - Systematical errors
+
-
-In the discussions below we will focus on the MSE and in particular since we will split the data into test and training data,
-we discuss the
+
- Statistical errors can be estimated using standard tools from statistics
+- Systematical errors are method specific and must be treated differently from case to case.
+
+
+
-
-- prediction error or simply the test error \( \mathrm{Err_{Test}} \), where we have a fixed training set and the test error is the MSE arising from the data reserved for testing. We discuss also the
-- training error \( \mathrm{Err_{Train}} \), which is the average loss over the training data.
-
-
-As our model becomes more and more complex, more of the training data tends to used. The training may thence adapt to more complicated structures in the data. This may lead to a decrease in the bias (see below for code example) and a slight increase of the variance for the test error.
-For a certain level of complexity the test error will reach minimum, before starting to increase again. The
-training error reaches a saturation.
@@ -303,7 +299,7 @@ training error reaches a saturation.
27
28
...
-
40
+
41
»
diff --git a/doc/pub/week37/html/._week37-bs019.html b/doc/pub/week37/html/._week37-bs019.html
index b965e2612..535dc6955 100644
--- a/doc/pub/week37/html/._week37-bs019.html
+++ b/doc/pub/week37/html/._week37-bs019.html
@@ -69,6 +69,7 @@ Automatically generated HTML file from DocOnce source
2,
None,
'test-function-for-what-happens-with-ols-ridge-and-lasso'),
+ ('Rerunning the above code', 2, None, 'rerunning-the-above-code'),
("Invoking Bayes' theorem", 2, None, 'invoking-bayes-theorem'),
('Ridge and Bayes', 2, None, 'ridge-and-bayes'),
('Lasso and Bayes', 2, None, 'lasso-and-bayes'),
@@ -206,36 +207,37 @@ MathJax.Hub.Config({
Bayes' Theorem
Interpretations of Bayes' Theorem
Test Function for what happens with OLS, Ridge and Lasso
-
Invoking Bayes' theorem
-
Ridge and Bayes
-
Lasso and Bayes
-
Why resampling methods
-
Resampling methods
-
Resampling approaches can be computationally expensive
-
Why resampling methods ?
-
Statistical analysis
-
Resampling methods
-
Resampling methods: Jackknife and Bootstrap
-
Resampling methods: Jackknife
-
Jackknife code example
-
Resampling methods: Bootstrap
-
Resampling methods: Bootstrap background
-
Resampling methods: More Bootstrap background
-
Resampling methods: Bootstrap approach
-
Resampling methods: Bootstrap steps
-
Code example for the Bootstrap method
-
The bias-variance tradeoff
-
Example code for Bias-Variance tradeoff
-
Understanding what happens
-
Summing up
-
Another Example from Scikit-Learn's Repository
-
Various steps in cross-validation
-
How to set up the cross-validation for Ridge and/or Lasso
-
Cross-validation in brief
-
Code Example for Cross-validation and \( k \)-fold Cross-validation
-
More examples on bootstrap and cross-validation and errors
-
The same example but now with cross-validation
-
Cross-validation with Ridge
+
Rerunning the above code
+
Invoking Bayes' theorem
+
Ridge and Bayes
+
Lasso and Bayes
+
Why resampling methods
+
Resampling methods
+
Resampling approaches can be computationally expensive
+
Why resampling methods ?
+
Statistical analysis
+
Resampling methods
+
Resampling methods: Jackknife and Bootstrap
+
Resampling methods: Jackknife
+
Jackknife code example
+
Resampling methods: Bootstrap
+
Resampling methods: Bootstrap background
+
Resampling methods: More Bootstrap background
+
Resampling methods: Bootstrap approach
+
Resampling methods: Bootstrap steps
+
Code example for the Bootstrap method
+
The bias-variance tradeoff
+
Example code for Bias-Variance tradeoff
+
Understanding what happens
+
Summing up
+
Another Example from Scikit-Learn's Repository
+
Various steps in cross-validation
+
How to set up the cross-validation for Ridge and/or Lasso
+
Cross-validation in brief
+
Code Example for Cross-validation and \( k \)-fold Cross-validation
+
More examples on bootstrap and cross-validation and errors
+
The same example but now with cross-validation
+
Cross-validation with Ridge
@@ -251,25 +253,31 @@ MathJax.Hub.Config({
-
Resampling methods: Jackknife and Bootstrap
+
Resampling methods
-Two famous
-resampling methods are the independent bootstrap and the jackknife.
+With all these analytical equations for both the OLS and Ridge
+regression, we will now outline how to assess a given model. This will
+lead to a discussion of the so-called bias-variance tradeoff (see
+below) and so-called resampling methods.
-The jackknife is a special case of the independent bootstrap. Still, the jackknife was made
-popular prior to the independent bootstrap. And as the popularity of
-the independent bootstrap soared, new variants, such as the dependent bootstrap have also been developed..
+One of the quantities we have discussed as a way to measure errors is
+the mean-squared error (MSE), mainly used for fitting of continuous
+functions. Another choice is the absolute error.
-The Jackknife and independent bootstrap work for
-independent, identically distributed random variables.
-If these conditions are not
-satisfied, the methods will fail. Yet, it should be said that if the data are
-independent, identically distributed, and we only want to estimate the
-variance of \( \overline{X} \) (which often is the case), then there is no
-need for bootstrapping.
+In the discussions below we will focus on the MSE and in particular since we will split the data into test and training data,
+we discuss the
+
+
+- prediction error or simply the test error \( \mathrm{Err_{Test}} \), where we have a fixed training set and the test error is the MSE arising from the data reserved for testing. We discuss also the
+- training error \( \mathrm{Err_{Train}} \), which is the average loss over the training data.
+
+
+As our model becomes more and more complex, more of the training data tends to used. The training may thence adapt to more complicated structures in the data. This may lead to a decrease in the bias (see below for code example) and a slight increase of the variance for the test error.
+For a certain level of complexity the test error will reach minimum, before starting to increase again. The
+training error reaches a saturation.
@@ -297,7 +305,7 @@ need for bootstrapping.
28
29
...
-
40
+
41
»
diff --git a/doc/pub/week37/html/._week37-bs020.html b/doc/pub/week37/html/._week37-bs020.html
index c5abe310a..3e9efadf1 100644
--- a/doc/pub/week37/html/._week37-bs020.html
+++ b/doc/pub/week37/html/._week37-bs020.html
@@ -69,6 +69,7 @@ Automatically generated HTML file from DocOnce source
2,
None,
'test-function-for-what-happens-with-ols-ridge-and-lasso'),
+ ('Rerunning the above code', 2, None, 'rerunning-the-above-code'),
("Invoking Bayes' theorem", 2, None, 'invoking-bayes-theorem'),
('Ridge and Bayes', 2, None, 'ridge-and-bayes'),
('Lasso and Bayes', 2, None, 'lasso-and-bayes'),
@@ -206,36 +207,37 @@ MathJax.Hub.Config({
Bayes' Theorem
Interpretations of Bayes' Theorem
Test Function for what happens with OLS, Ridge and Lasso
-
Invoking Bayes' theorem
-
Ridge and Bayes
-
Lasso and Bayes
-
Why resampling methods
-
Resampling methods
-
Resampling approaches can be computationally expensive
-
Why resampling methods ?
-
Statistical analysis
-
Resampling methods
-
Resampling methods: Jackknife and Bootstrap
-
Resampling methods: Jackknife
-
Jackknife code example
-
Resampling methods: Bootstrap
-
Resampling methods: Bootstrap background
-
Resampling methods: More Bootstrap background
-
Resampling methods: Bootstrap approach
-
Resampling methods: Bootstrap steps
-
Code example for the Bootstrap method
-
The bias-variance tradeoff
-
Example code for Bias-Variance tradeoff
-
Understanding what happens
-
Summing up
-
Another Example from Scikit-Learn's Repository
-
Various steps in cross-validation
-
How to set up the cross-validation for Ridge and/or Lasso
-
Cross-validation in brief
-
Code Example for Cross-validation and \( k \)-fold Cross-validation
-
More examples on bootstrap and cross-validation and errors
-
The same example but now with cross-validation
-
Cross-validation with Ridge
+
Rerunning the above code
+
Invoking Bayes' theorem
+
Ridge and Bayes
+
Lasso and Bayes
+
Why resampling methods
+
Resampling methods
+
Resampling approaches can be computationally expensive
+
Why resampling methods ?
+
Statistical analysis
+
Resampling methods
+
Resampling methods: Jackknife and Bootstrap
+
Resampling methods: Jackknife
+
Jackknife code example
+
Resampling methods: Bootstrap
+
Resampling methods: Bootstrap background
+
Resampling methods: More Bootstrap background
+
Resampling methods: Bootstrap approach
+
Resampling methods: Bootstrap steps
+
Code example for the Bootstrap method
+
The bias-variance tradeoff
+
Example code for Bias-Variance tradeoff
+
Understanding what happens
+
Summing up
+
Another Example from Scikit-Learn's Repository
+
Various steps in cross-validation
+
How to set up the cross-validation for Ridge and/or Lasso
+
Cross-validation in brief
+
Code Example for Cross-validation and \( k \)-fold Cross-validation
+
More examples on bootstrap and cross-validation and errors
+
The same example but now with cross-validation
+
Cross-validation with Ridge
@@ -251,21 +253,25 @@ MathJax.Hub.Config({
-
Resampling methods: Jackknife
+
Resampling methods: Jackknife and Bootstrap
-The Jackknife works by making many replicas of the estimator \( \widehat{\beta} \).
-The jackknife is a resampling method where we systematically leave out one observation from the vector of observed values \( \boldsymbol{x} = (x_1,x_2,\cdots,X_n) \).
-Let \( \boldsymbol{x}_i \) denote the vector
-$$
-\boldsymbol{x}_i = (x_1,x_2,\cdots,x_{i-1},x_{i+1},\cdots,x_n),
-$$
+Two famous
+resampling methods are the independent bootstrap and the jackknife.
-which equals the vector \( \boldsymbol{x} \) with the exception that observation
-number \( i \) is left out. Using this notation, define
-\( \widehat{\beta}_i \) to be the estimator
-\( \widehat{\beta} \) computed using \( \vec{X}_i \).
+The jackknife is a special case of the independent bootstrap. Still, the jackknife was made
+popular prior to the independent bootstrap. And as the popularity of
+the independent bootstrap soared, new variants, such as the dependent bootstrap have also been developed..
+
+
+The Jackknife and independent bootstrap work for
+independent, identically distributed random variables.
+If these conditions are not
+satisfied, the methods will fail. Yet, it should be said that if the data are
+independent, identically distributed, and we only want to estimate the
+variance of \( \overline{X} \) (which often is the case), then there is no
+need for bootstrapping.
@@ -293,7 +299,7 @@ number \( i \) is left out. Using this notation, define
29
30
...
-
40
+
41
»
diff --git a/doc/pub/week37/html/._week37-bs021.html b/doc/pub/week37/html/._week37-bs021.html
index d1418d762..6820b3568 100644
--- a/doc/pub/week37/html/._week37-bs021.html
+++ b/doc/pub/week37/html/._week37-bs021.html
@@ -69,6 +69,7 @@ Automatically generated HTML file from DocOnce source
2,
None,
'test-function-for-what-happens-with-ols-ridge-and-lasso'),
+ ('Rerunning the above code', 2, None, 'rerunning-the-above-code'),
("Invoking Bayes' theorem", 2, None, 'invoking-bayes-theorem'),
('Ridge and Bayes', 2, None, 'ridge-and-bayes'),
('Lasso and Bayes', 2, None, 'lasso-and-bayes'),
@@ -206,36 +207,37 @@ MathJax.Hub.Config({
Bayes' Theorem
Interpretations of Bayes' Theorem
Test Function for what happens with OLS, Ridge and Lasso
-
Invoking Bayes' theorem
-
Ridge and Bayes
-
Lasso and Bayes
-
Why resampling methods
-
Resampling methods
-
Resampling approaches can be computationally expensive
-
Why resampling methods ?
-
Statistical analysis
-
Resampling methods
-
Resampling methods: Jackknife and Bootstrap
-
Resampling methods: Jackknife
-
Jackknife code example
-
Resampling methods: Bootstrap
-
Resampling methods: Bootstrap background
-
Resampling methods: More Bootstrap background
-
Resampling methods: Bootstrap approach
-
Resampling methods: Bootstrap steps
-
Code example for the Bootstrap method
-
The bias-variance tradeoff
-
Example code for Bias-Variance tradeoff
-
Understanding what happens
-
Summing up
-
Another Example from Scikit-Learn's Repository
-
Various steps in cross-validation
-
How to set up the cross-validation for Ridge and/or Lasso
-
Cross-validation in brief
-
Code Example for Cross-validation and \( k \)-fold Cross-validation
-
More examples on bootstrap and cross-validation and errors
-
The same example but now with cross-validation
-
Cross-validation with Ridge
+
Rerunning the above code
+
Invoking Bayes' theorem
+
Ridge and Bayes
+
Lasso and Bayes
+
Why resampling methods
+
Resampling methods
+
Resampling approaches can be computationally expensive
+
Why resampling methods ?
+
Statistical analysis
+
Resampling methods
+
Resampling methods: Jackknife and Bootstrap
+
Resampling methods: Jackknife
+
Jackknife code example
+
Resampling methods: Bootstrap
+
Resampling methods: Bootstrap background
+
Resampling methods: More Bootstrap background
+
Resampling methods: Bootstrap approach
+
Resampling methods: Bootstrap steps
+
Code example for the Bootstrap method
+
The bias-variance tradeoff
+
Example code for Bias-Variance tradeoff
+
Understanding what happens
+
Summing up
+
Another Example from Scikit-Learn's Repository
+
Various steps in cross-validation
+
How to set up the cross-validation for Ridge and/or Lasso
+
Cross-validation in brief
+
Code Example for Cross-validation and \( k \)-fold Cross-validation
+
More examples on bootstrap and cross-validation and errors
+
The same example but now with cross-validation
+
Cross-validation with Ridge
@@ -251,39 +253,22 @@ MathJax.Hub.Config({
-
Jackknife code example
+
Resampling methods: Jackknife
+
+The Jackknife works by making many replicas of the estimator \( \widehat{\beta} \).
+The jackknife is a resampling method where we systematically leave out one observation from the vector of observed values \( \boldsymbol{x} = (x_1,x_2,\cdots,X_n) \).
+Let \( \boldsymbol{x}_i \) denote the vector
+$$
+\boldsymbol{x}_i = (x_1,x_2,\cdots,x_{i-1},x_{i+1},\cdots,x_n),
+$$
-
-
from numpy import *
-from numpy.random import randint, randn
-from time import time
+
+which equals the vector \( \boldsymbol{x} \) with the exception that observation
+number \( i \) is left out. Using this notation, define
+\( \widehat{\beta}_i \) to be the estimator
+\( \widehat{\beta} \) computed using \( \vec{X}_i \).
-def jackknife(data, stat):
- n = len(data);t = zeros(n); inds = arange(n); t0 = time()
- ## 'jackknifing' by leaving out an observation for each i
- for i in range(n):
- t[i] = stat(delete(data,i) )
-
- # analysis
- print("Runtime: %g sec" % (time()-t0)); print("Jackknife Statistics :")
- print("original bias std. error")
- print("%8g %14g %15g" % (stat(data),(n-1)*mean(t)/n, (n*var(t))**.5))
-
- return t
-
-
-# Returns mean of data samples
-def stat(data):
- return mean(data)
-
-
-mu, sigma = 100, 15
-datapoints = 10000
-x = mu + sigma*random.randn(datapoints)
-# jackknife returns the data sample
-t = jackknife(x, stat)
-
@@ -310,7 +295,7 @@ t = jackknife(x, stat)
30
31
...
-
40
+
41
»
diff --git a/doc/pub/week37/html/._week37-bs022.html b/doc/pub/week37/html/._week37-bs022.html
index 11fb837fd..45763c521 100644
--- a/doc/pub/week37/html/._week37-bs022.html
+++ b/doc/pub/week37/html/._week37-bs022.html
@@ -69,6 +69,7 @@ Automatically generated HTML file from DocOnce source
2,
None,
'test-function-for-what-happens-with-ols-ridge-and-lasso'),
+ ('Rerunning the above code', 2, None, 'rerunning-the-above-code'),
("Invoking Bayes' theorem", 2, None, 'invoking-bayes-theorem'),
('Ridge and Bayes', 2, None, 'ridge-and-bayes'),
('Lasso and Bayes', 2, None, 'lasso-and-bayes'),
@@ -206,36 +207,37 @@ MathJax.Hub.Config({
Bayes' Theorem
Interpretations of Bayes' Theorem
Test Function for what happens with OLS, Ridge and Lasso
-
Invoking Bayes' theorem
-
Ridge and Bayes
-
Lasso and Bayes
-
Why resampling methods
-
Resampling methods
-
Resampling approaches can be computationally expensive
-
Why resampling methods ?
-
Statistical analysis
-
Resampling methods
-
Resampling methods: Jackknife and Bootstrap
-
Resampling methods: Jackknife
-
Jackknife code example
-
Resampling methods: Bootstrap
-
Resampling methods: Bootstrap background
-
Resampling methods: More Bootstrap background
-
Resampling methods: Bootstrap approach
-
Resampling methods: Bootstrap steps
-
Code example for the Bootstrap method
-
The bias-variance tradeoff
-
Example code for Bias-Variance tradeoff
-
Understanding what happens
-
Summing up
-
Another Example from Scikit-Learn's Repository
-
Various steps in cross-validation
-
How to set up the cross-validation for Ridge and/or Lasso
-
Cross-validation in brief
-
Code Example for Cross-validation and \( k \)-fold Cross-validation
-
More examples on bootstrap and cross-validation and errors
-
The same example but now with cross-validation
-
Cross-validation with Ridge
+
Rerunning the above code
+
Invoking Bayes' theorem
+
Ridge and Bayes
+
Lasso and Bayes
+
Why resampling methods
+
Resampling methods
+
Resampling approaches can be computationally expensive
+
Why resampling methods ?
+
Statistical analysis
+
Resampling methods
+
Resampling methods: Jackknife and Bootstrap
+
Resampling methods: Jackknife
+
Jackknife code example
+
Resampling methods: Bootstrap
+
Resampling methods: Bootstrap background
+
Resampling methods: More Bootstrap background
+
Resampling methods: Bootstrap approach
+
Resampling methods: Bootstrap steps
+
Code example for the Bootstrap method
+
The bias-variance tradeoff
+
Example code for Bias-Variance tradeoff
+
Understanding what happens
+
Summing up
+
Another Example from Scikit-Learn's Repository
+
Various steps in cross-validation
+
How to set up the cross-validation for Ridge and/or Lasso
+
Cross-validation in brief
+
Code Example for Cross-validation and \( k \)-fold Cross-validation
+
More examples on bootstrap and cross-validation and errors
+
The same example but now with cross-validation
+
Cross-validation with Ridge
@@ -251,31 +253,39 @@ MathJax.Hub.Config({
-
Resampling methods: Bootstrap
-
-
-
-Bootstrapping is a non-parametric approach to statistical inference
-that substitutes computation for more traditional distributional
-assumptions and asymptotic results. Bootstrapping offers a number of
-advantages:
-
-
-- The bootstrap is quite general, although there are some cases in which it fails.
-- Because it does not require distributional assumptions (such as normally distributed errors), the bootstrap can provide more accurate inferences when the data are not well behaved or when the sample size is small.
-- It is possible to apply the bootstrap to statistics with sampling distributions that are difficult to derive, even asymptotically.
-- It is relatively simple to apply the bootstrap to complex data-collection plans (such as stratified and clustered samples).
-
-
-
-
-
+
Jackknife code example
-The textbook by Davison on the Bootstrap Methods and their Applications provides many more insights and proofs. In this course we will take a more practical approach and use the results and theorems provided in the literature. For those interested in reading more about the bootstrap methods, we recommend the above text and the one by Efron and Tibshirani.
-
-Before we proceed however, we need to remind ourselves about a central theorem in statistics, namely the so-called central limit theorem.
+
+
from numpy import *
+from numpy.random import randint, randn
+from time import time
+def jackknife(data, stat):
+ n = len(data);t = zeros(n); inds = arange(n); t0 = time()
+ ## 'jackknifing' by leaving out an observation for each i
+ for i in range(n):
+ t[i] = stat(delete(data,i) )
+
+ # analysis
+ print("Runtime: %g sec" % (time()-t0)); print("Jackknife Statistics :")
+ print("original bias std. error")
+ print("%8g %14g %15g" % (stat(data),(n-1)*mean(t)/n, (n*var(t))**.5))
+
+ return t
+
+
+# Returns mean of data samples
+def stat(data):
+ return mean(data)
+
+
+mu, sigma = 100, 15
+datapoints = 10000
+x = mu + sigma*random.randn(datapoints)
+# jackknife returns the data sample
+t = jackknife(x, stat)
+
@@ -302,7 +312,7 @@ Before we proceed however, we need to remind ourselves about a central theorem i
31
32
...
-
40
+
41
»
diff --git a/doc/pub/week37/html/._week37-bs023.html b/doc/pub/week37/html/._week37-bs023.html
index 06ccacb99..69121a3de 100644
--- a/doc/pub/week37/html/._week37-bs023.html
+++ b/doc/pub/week37/html/._week37-bs023.html
@@ -69,6 +69,7 @@ Automatically generated HTML file from DocOnce source
2,
None,
'test-function-for-what-happens-with-ols-ridge-and-lasso'),
+ ('Rerunning the above code', 2, None, 'rerunning-the-above-code'),
("Invoking Bayes' theorem", 2, None, 'invoking-bayes-theorem'),
('Ridge and Bayes', 2, None, 'ridge-and-bayes'),
('Lasso and Bayes', 2, None, 'lasso-and-bayes'),
@@ -206,36 +207,37 @@ MathJax.Hub.Config({
Bayes' Theorem
Interpretations of Bayes' Theorem
Test Function for what happens with OLS, Ridge and Lasso
-
Invoking Bayes' theorem
-
Ridge and Bayes
-
Lasso and Bayes
-
Why resampling methods
-
Resampling methods
-
Resampling approaches can be computationally expensive
-
Why resampling methods ?
-
Statistical analysis
-
Resampling methods
-
Resampling methods: Jackknife and Bootstrap
-
Resampling methods: Jackknife
-
Jackknife code example
-
Resampling methods: Bootstrap
-
Resampling methods: Bootstrap background
-
Resampling methods: More Bootstrap background
-
Resampling methods: Bootstrap approach
-
Resampling methods: Bootstrap steps
-
Code example for the Bootstrap method
-
The bias-variance tradeoff
-
Example code for Bias-Variance tradeoff
-
Understanding what happens
-
Summing up
-
Another Example from Scikit-Learn's Repository
-
Various steps in cross-validation
-
How to set up the cross-validation for Ridge and/or Lasso
-
Cross-validation in brief
-
Code Example for Cross-validation and \( k \)-fold Cross-validation
-
More examples on bootstrap and cross-validation and errors
-
The same example but now with cross-validation
-
Cross-validation with Ridge
+
Rerunning the above code
+
Invoking Bayes' theorem
+
Ridge and Bayes
+
Lasso and Bayes
+
Why resampling methods
+
Resampling methods
+
Resampling approaches can be computationally expensive
+
Why resampling methods ?
+
Statistical analysis
+
Resampling methods
+
Resampling methods: Jackknife and Bootstrap
+
Resampling methods: Jackknife
+
Jackknife code example
+
Resampling methods: Bootstrap
+
Resampling methods: Bootstrap background
+
Resampling methods: More Bootstrap background
+
Resampling methods: Bootstrap approach
+
Resampling methods: Bootstrap steps
+
Code example for the Bootstrap method
+
The bias-variance tradeoff
+
Example code for Bias-Variance tradeoff
+
Understanding what happens
+
Summing up
+
Another Example from Scikit-Learn's Repository
+
Various steps in cross-validation
+
How to set up the cross-validation for Ridge and/or Lasso
+
Cross-validation in brief
+
Code Example for Cross-validation and \( k \)-fold Cross-validation
+
More examples on bootstrap and cross-validation and errors
+
The same example but now with cross-validation
+
Cross-validation with Ridge
@@ -251,18 +253,30 @@ MathJax.Hub.Config({
-
Resampling methods: Bootstrap background
+
Resampling methods: Bootstrap
+
+
+
+Bootstrapping is a non-parametric approach to statistical inference
+that substitutes computation for more traditional distributional
+assumptions and asymptotic results. Bootstrapping offers a number of
+advantages:
+
+
+- The bootstrap is quite general, although there are some cases in which it fails.
+- Because it does not require distributional assumptions (such as normally distributed errors), the bootstrap can provide more accurate inferences when the data are not well behaved or when the sample size is small.
+- It is possible to apply the bootstrap to statistics with sampling distributions that are difficult to derive, even asymptotically.
+- It is relatively simple to apply the bootstrap to complex data-collection plans (such as stratified and clustered samples).
+
+
+
+
-Since \( \widehat{\beta} = \widehat{\beta}(\boldsymbol{X}) \) is a function of random variables,
-\( \widehat{\beta} \) itself must be a random variable. Thus it has
-a pdf, call this function \( p(\boldsymbol{t}) \). The aim of the bootstrap is to
-estimate \( p(\boldsymbol{t}) \) by the relative frequency of
-\( \widehat{\beta} \). You can think of this as using a histogram
-in the place of \( p(\boldsymbol{t}) \). If the relative frequency closely
-resembles \( p(\vec{t}) \), then using numerics, it is straight forward to
-estimate all the interesting parameters of \( p(\boldsymbol{t}) \) using point
-estimators.
+The textbook by Davison on the Bootstrap Methods and their Applications provides many more insights and proofs. In this course we will take a more practical approach and use the results and theorems provided in the literature. For those interested in reading more about the bootstrap methods, we recommend the above text and the one by Efron and Tibshirani.
+
+
+Before we proceed however, we need to remind ourselves about a central theorem in statistics, namely the so-called central limit theorem.
@@ -290,7 +304,7 @@ estimators.
32
33
...
-
40
+
41
»
diff --git a/doc/pub/week37/html/._week37-bs024.html b/doc/pub/week37/html/._week37-bs024.html
index d505dbe57..6e3118d71 100644
--- a/doc/pub/week37/html/._week37-bs024.html
+++ b/doc/pub/week37/html/._week37-bs024.html
@@ -69,6 +69,7 @@ Automatically generated HTML file from DocOnce source
2,
None,
'test-function-for-what-happens-with-ols-ridge-and-lasso'),
+ ('Rerunning the above code', 2, None, 'rerunning-the-above-code'),
("Invoking Bayes' theorem", 2, None, 'invoking-bayes-theorem'),
('Ridge and Bayes', 2, None, 'ridge-and-bayes'),
('Lasso and Bayes', 2, None, 'lasso-and-bayes'),
@@ -206,36 +207,37 @@ MathJax.Hub.Config({
Bayes' Theorem
Interpretations of Bayes' Theorem
Test Function for what happens with OLS, Ridge and Lasso
-
Invoking Bayes' theorem
-
Ridge and Bayes
-
Lasso and Bayes
-
Why resampling methods
-
Resampling methods
-
Resampling approaches can be computationally expensive
-
Why resampling methods ?
-
Statistical analysis
-
Resampling methods
-
Resampling methods: Jackknife and Bootstrap
-
Resampling methods: Jackknife
-
Jackknife code example
-
Resampling methods: Bootstrap
-
Resampling methods: Bootstrap background
-
Resampling methods: More Bootstrap background
-
Resampling methods: Bootstrap approach
-
Resampling methods: Bootstrap steps
-
Code example for the Bootstrap method
-
The bias-variance tradeoff
-
Example code for Bias-Variance tradeoff
-
Understanding what happens
-
Summing up
-
Another Example from Scikit-Learn's Repository
-
Various steps in cross-validation
-
How to set up the cross-validation for Ridge and/or Lasso
-
Cross-validation in brief
-
Code Example for Cross-validation and \( k \)-fold Cross-validation
-
More examples on bootstrap and cross-validation and errors
-
The same example but now with cross-validation
-
Cross-validation with Ridge
+
Rerunning the above code
+
Invoking Bayes' theorem
+
Ridge and Bayes
+
Lasso and Bayes
+
Why resampling methods
+
Resampling methods
+
Resampling approaches can be computationally expensive
+
Why resampling methods ?
+
Statistical analysis
+
Resampling methods
+
Resampling methods: Jackknife and Bootstrap
+
Resampling methods: Jackknife
+
Jackknife code example
+
Resampling methods: Bootstrap
+
Resampling methods: Bootstrap background
+
Resampling methods: More Bootstrap background
+
Resampling methods: Bootstrap approach
+
Resampling methods: Bootstrap steps
+
Code example for the Bootstrap method
+
The bias-variance tradeoff
+
Example code for Bias-Variance tradeoff
+
Understanding what happens
+
Summing up
+
Another Example from Scikit-Learn's Repository
+
Various steps in cross-validation
+
How to set up the cross-validation for Ridge and/or Lasso
+
Cross-validation in brief
+
Code Example for Cross-validation and \( k \)-fold Cross-validation
+
More examples on bootstrap and cross-validation and errors
+
The same example but now with cross-validation
+
Cross-validation with Ridge
@@ -251,24 +253,18 @@ MathJax.Hub.Config({
-
Resampling methods: More Bootstrap background
+
Resampling methods: Bootstrap background
-In the case that \( \widehat{\beta} \) has
-more than one component, and the components are independent, we use the
-same estimator on each component separately. If the probability
-density function of \( X_i \), \( p(x) \), had been known, then it would have
-been straightforward to do this by:
-
-
-- Drawing lots of numbers from \( p(x) \), suppose we call one such set of numbers \( (X_1^*, X_2^*, \cdots, X_n^*) \).
-- Then using these numbers, we could compute a replica of \( \widehat{\beta} \) called \( \widehat{\beta}^* \).
-
-
-By repeated use of the above two points, many
-estimates of \( \widehat{\beta} \) can be obtained. The
-idea is to use the relative frequency of \( \widehat{\beta}^* \)
-(think of a histogram) as an estimate of \( p(\boldsymbol{t}) \).
+Since \( \widehat{\beta} = \widehat{\beta}(\boldsymbol{X}) \) is a function of random variables,
+\( \widehat{\beta} \) itself must be a random variable. Thus it has
+a pdf, call this function \( p(\boldsymbol{t}) \). The aim of the bootstrap is to
+estimate \( p(\boldsymbol{t}) \) by the relative frequency of
+\( \widehat{\beta} \). You can think of this as using a histogram
+in the place of \( p(\boldsymbol{t}) \). If the relative frequency closely
+resembles \( p(\vec{t}) \), then using numerics, it is straight forward to
+estimate all the interesting parameters of \( p(\boldsymbol{t}) \) using point
+estimators.
@@ -296,7 +292,7 @@ idea is to use the relative frequency of \( \widehat{\beta}^* \)
33
34
...
-
40
+
41
»
diff --git a/doc/pub/week37/html/._week37-bs025.html b/doc/pub/week37/html/._week37-bs025.html
index c773534d4..16a9f7ac0 100644
--- a/doc/pub/week37/html/._week37-bs025.html
+++ b/doc/pub/week37/html/._week37-bs025.html
@@ -69,6 +69,7 @@ Automatically generated HTML file from DocOnce source
2,
None,
'test-function-for-what-happens-with-ols-ridge-and-lasso'),
+ ('Rerunning the above code', 2, None, 'rerunning-the-above-code'),
("Invoking Bayes' theorem", 2, None, 'invoking-bayes-theorem'),
('Ridge and Bayes', 2, None, 'ridge-and-bayes'),
('Lasso and Bayes', 2, None, 'lasso-and-bayes'),
@@ -206,36 +207,37 @@ MathJax.Hub.Config({
Bayes' Theorem
Interpretations of Bayes' Theorem
Test Function for what happens with OLS, Ridge and Lasso
-
Invoking Bayes' theorem
-
Ridge and Bayes
-
Lasso and Bayes
-
Why resampling methods
-
Resampling methods
-
Resampling approaches can be computationally expensive
-
Why resampling methods ?
-
Statistical analysis
-
Resampling methods
-
Resampling methods: Jackknife and Bootstrap
-
Resampling methods: Jackknife
-
Jackknife code example
-
Resampling methods: Bootstrap
-
Resampling methods: Bootstrap background
-
Resampling methods: More Bootstrap background
-
Resampling methods: Bootstrap approach
-
Resampling methods: Bootstrap steps
-
Code example for the Bootstrap method
-
The bias-variance tradeoff
-
Example code for Bias-Variance tradeoff
-
Understanding what happens
-
Summing up
-
Another Example from Scikit-Learn's Repository
-
Various steps in cross-validation
-
How to set up the cross-validation for Ridge and/or Lasso
-
Cross-validation in brief
-
Code Example for Cross-validation and \( k \)-fold Cross-validation
-
More examples on bootstrap and cross-validation and errors
-
The same example but now with cross-validation
-
Cross-validation with Ridge
+
Rerunning the above code
+
Invoking Bayes' theorem
+
Ridge and Bayes
+
Lasso and Bayes
+
Why resampling methods
+
Resampling methods
+
Resampling approaches can be computationally expensive
+
Why resampling methods ?
+
Statistical analysis
+
Resampling methods
+
Resampling methods: Jackknife and Bootstrap
+
Resampling methods: Jackknife
+
Jackknife code example
+
Resampling methods: Bootstrap
+
Resampling methods: Bootstrap background
+
Resampling methods: More Bootstrap background
+
Resampling methods: Bootstrap approach
+
Resampling methods: Bootstrap steps
+
Code example for the Bootstrap method
+
The bias-variance tradeoff
+
Example code for Bias-Variance tradeoff
+
Understanding what happens
+
Summing up
+
Another Example from Scikit-Learn's Repository
+
Various steps in cross-validation
+
How to set up the cross-validation for Ridge and/or Lasso
+
Cross-validation in brief
+
Code Example for Cross-validation and \( k \)-fold Cross-validation
+
More examples on bootstrap and cross-validation and errors
+
The same example but now with cross-validation
+
Cross-validation with Ridge
@@ -251,20 +253,24 @@ MathJax.Hub.Config({
-
Resampling methods: Bootstrap approach
+
Resampling methods: More Bootstrap background
-But
-unless there is enough information available about the process that
-generated \( X_1,X_2,\cdots,X_n \), \( p(x) \) is in general
-unknown. Therefore, Efron in 1979 asked the
-question: What if we replace \( p(x) \) by the relative frequency
-of the observation \( X_i \)?
+In the case that \( \widehat{\beta} \) has
+more than one component, and the components are independent, we use the
+same estimator on each component separately. If the probability
+density function of \( X_i \), \( p(x) \), had been known, then it would have
+been straightforward to do this by:
-
-If we draw observations in accordance with
-the relative frequency of the observations, will we obtain the same
-result in some asymptotic sense? The answer is yes.
+
+- Drawing lots of numbers from \( p(x) \), suppose we call one such set of numbers \( (X_1^*, X_2^*, \cdots, X_n^*) \).
+- Then using these numbers, we could compute a replica of \( \widehat{\beta} \) called \( \widehat{\beta}^* \).
+
+
+By repeated use of the above two points, many
+estimates of \( \widehat{\beta} \) can be obtained. The
+idea is to use the relative frequency of \( \widehat{\beta}^* \)
+(think of a histogram) as an estimate of \( p(\boldsymbol{t}) \).
@@ -292,7 +298,7 @@ result in some asymptotic sense? The answer is yes.
34
35
...
-
40
+
41
»
diff --git a/doc/pub/week37/html/._week37-bs026.html b/doc/pub/week37/html/._week37-bs026.html
index 9d05e820e..4f46b9cea 100644
--- a/doc/pub/week37/html/._week37-bs026.html
+++ b/doc/pub/week37/html/._week37-bs026.html
@@ -69,6 +69,7 @@ Automatically generated HTML file from DocOnce source
2,
None,
'test-function-for-what-happens-with-ols-ridge-and-lasso'),
+ ('Rerunning the above code', 2, None, 'rerunning-the-above-code'),
("Invoking Bayes' theorem", 2, None, 'invoking-bayes-theorem'),
('Ridge and Bayes', 2, None, 'ridge-and-bayes'),
('Lasso and Bayes', 2, None, 'lasso-and-bayes'),
@@ -206,36 +207,37 @@ MathJax.Hub.Config({
Bayes' Theorem
Interpretations of Bayes' Theorem
Test Function for what happens with OLS, Ridge and Lasso
-
Invoking Bayes' theorem
-
Ridge and Bayes
-
Lasso and Bayes
-
Why resampling methods
-
Resampling methods
-
Resampling approaches can be computationally expensive
-
Why resampling methods ?
-
Statistical analysis
-
Resampling methods
-
Resampling methods: Jackknife and Bootstrap
-
Resampling methods: Jackknife
-
Jackknife code example
-
Resampling methods: Bootstrap
-
Resampling methods: Bootstrap background
-
Resampling methods: More Bootstrap background
-
Resampling methods: Bootstrap approach
-
Resampling methods: Bootstrap steps
-
Code example for the Bootstrap method
-
The bias-variance tradeoff
-
Example code for Bias-Variance tradeoff
-
Understanding what happens
-
Summing up
-
Another Example from Scikit-Learn's Repository
-
Various steps in cross-validation
-
How to set up the cross-validation for Ridge and/or Lasso
-
Cross-validation in brief
-
Code Example for Cross-validation and \( k \)-fold Cross-validation
-
More examples on bootstrap and cross-validation and errors
-
The same example but now with cross-validation
-
Cross-validation with Ridge
+
Rerunning the above code
+
Invoking Bayes' theorem
+
Ridge and Bayes
+
Lasso and Bayes
+
Why resampling methods
+
Resampling methods
+
Resampling approaches can be computationally expensive
+
Why resampling methods ?
+
Statistical analysis
+
Resampling methods
+
Resampling methods: Jackknife and Bootstrap
+
Resampling methods: Jackknife
+
Jackknife code example
+
Resampling methods: Bootstrap
+
Resampling methods: Bootstrap background
+
Resampling methods: More Bootstrap background
+
Resampling methods: Bootstrap approach
+
Resampling methods: Bootstrap steps
+
Code example for the Bootstrap method
+
The bias-variance tradeoff
+
Example code for Bias-Variance tradeoff
+
Understanding what happens
+
Summing up
+
Another Example from Scikit-Learn's Repository
+
Various steps in cross-validation
+
How to set up the cross-validation for Ridge and/or Lasso
+
Cross-validation in brief
+
Code Example for Cross-validation and \( k \)-fold Cross-validation
+
More examples on bootstrap and cross-validation and errors
+
The same example but now with cross-validation
+
Cross-validation with Ridge
@@ -251,27 +253,20 @@ MathJax.Hub.Config({
-
Resampling methods: Bootstrap steps
+
Resampling methods: Bootstrap approach
-The independent bootstrap works like this:
+But
+unless there is enough information available about the process that
+generated \( X_1,X_2,\cdots,X_n \), \( p(x) \) is in general
+unknown. Therefore, Efron in 1979 asked the
+question: What if we replace \( p(x) \) by the relative frequency
+of the observation \( X_i \)?
-
-- Draw with replacement \( n \) numbers for the observed variables \( \boldsymbol{x} = (x_1,x_2,\cdots,x_n) \).
-- Define a vector \( \boldsymbol{x}^* \) containing the values which were drawn from \( \boldsymbol{x} \).
-- Using the vector \( \boldsymbol{x}^* \) compute \( \widehat{\beta}^* \) by evaluating \( \widehat \beta \) under the observations \( \boldsymbol{x}^* \).
-- Repeat this process \( k \) times.
-
-
-When you are done, you can draw a histogram of the relative frequency
-of \( \widehat \beta^* \). This is your estimate of the probability
-distribution \( p(t) \). Using this probability distribution you can
-estimate any statistics thereof. In principle you never draw the
-histogram of the relative frequency of \( \widehat{\beta}^* \). Instead
-you use the estimators corresponding to the statistic of interest. For
-example, if you are interested in estimating the variance of \( \widehat
-\beta \), apply the etsimator \( \widehat \sigma^2 \) to the values
-\( \widehat \beta^* \).
+
+If we draw observations in accordance with
+the relative frequency of the observations, will we obtain the same
+result in some asymptotic sense? The answer is yes.
@@ -299,7 +294,7 @@ example, if you are interested in estimating the variance of \( \widehat
35
36
...
-
40
+
41
»
diff --git a/doc/pub/week37/html/._week37-bs027.html b/doc/pub/week37/html/._week37-bs027.html
index 7aa240f14..9f24bf346 100644
--- a/doc/pub/week37/html/._week37-bs027.html
+++ b/doc/pub/week37/html/._week37-bs027.html
@@ -69,6 +69,7 @@ Automatically generated HTML file from DocOnce source
2,
None,
'test-function-for-what-happens-with-ols-ridge-and-lasso'),
+ ('Rerunning the above code', 2, None, 'rerunning-the-above-code'),
("Invoking Bayes' theorem", 2, None, 'invoking-bayes-theorem'),
('Ridge and Bayes', 2, None, 'ridge-and-bayes'),
('Lasso and Bayes', 2, None, 'lasso-and-bayes'),
@@ -206,36 +207,37 @@ MathJax.Hub.Config({
Bayes' Theorem
Interpretations of Bayes' Theorem
Test Function for what happens with OLS, Ridge and Lasso
-
Invoking Bayes' theorem
-
Ridge and Bayes
-
Lasso and Bayes
-
Why resampling methods
-
Resampling methods
-
Resampling approaches can be computationally expensive
-
Why resampling methods ?
-
Statistical analysis
-
Resampling methods
-
Resampling methods: Jackknife and Bootstrap
-
Resampling methods: Jackknife
-
Jackknife code example
-
Resampling methods: Bootstrap
-
Resampling methods: Bootstrap background
-
Resampling methods: More Bootstrap background
-
Resampling methods: Bootstrap approach
-
Resampling methods: Bootstrap steps
-
Code example for the Bootstrap method
-
The bias-variance tradeoff
-
Example code for Bias-Variance tradeoff
-
Understanding what happens
-
Summing up
-
Another Example from Scikit-Learn's Repository
-
Various steps in cross-validation
-
How to set up the cross-validation for Ridge and/or Lasso
-
Cross-validation in brief
-
Code Example for Cross-validation and \( k \)-fold Cross-validation
-
More examples on bootstrap and cross-validation and errors
-
The same example but now with cross-validation
-
Cross-validation with Ridge
+
Rerunning the above code
+
Invoking Bayes' theorem
+
Ridge and Bayes
+
Lasso and Bayes
+
Why resampling methods
+
Resampling methods
+
Resampling approaches can be computationally expensive
+
Why resampling methods ?
+
Statistical analysis
+
Resampling methods
+
Resampling methods: Jackknife and Bootstrap
+
Resampling methods: Jackknife
+
Jackknife code example
+
Resampling methods: Bootstrap
+
Resampling methods: Bootstrap background
+
Resampling methods: More Bootstrap background
+
Resampling methods: Bootstrap approach
+
Resampling methods: Bootstrap steps
+
Code example for the Bootstrap method
+
The bias-variance tradeoff
+
Example code for Bias-Variance tradeoff
+
Understanding what happens
+
Summing up
+
Another Example from Scikit-Learn's Repository
+
Various steps in cross-validation
+
How to set up the cross-validation for Ridge and/or Lasso
+
Cross-validation in brief
+
Code Example for Cross-validation and \( k \)-fold Cross-validation
+
More examples on bootstrap and cross-validation and errors
+
The same example but now with cross-validation
+
Cross-validation with Ridge
@@ -251,67 +253,28 @@ MathJax.Hub.Config({
-
Code example for the Bootstrap method
+
Resampling methods: Bootstrap steps
-The following code starts with a Gaussian distribution with mean value
-\( \mu =100 \) and variance \( \sigma=15 \). We use this to generate the data
-used in the bootstrap analysis. The bootstrap analysis returns a data
-set after a given number of bootstrap operations (as many as we have
-data points). This data set consists of estimated mean values for each
-bootstrap operation. The histogram generated by the bootstrap method
-shows that the distribution for these mean values is also a Gaussian,
-centered around the mean value \( \mu=100 \) but with standard deviation
-\( \sigma/\sqrt{n} \), where \( n \) is the number of bootstrap samples (in
-this case the same as the number of original data points). The value
-of the standard deviation is what we expect from the central limit
-theorem.
+The independent bootstrap works like this:
-
+
+- Draw with replacement \( n \) numbers for the observed variables \( \boldsymbol{x} = (x_1,x_2,\cdots,x_n) \).
+- Define a vector \( \boldsymbol{x}^* \) containing the values which were drawn from \( \boldsymbol{x} \).
+- Using the vector \( \boldsymbol{x}^* \) compute \( \widehat{\beta}^* \) by evaluating \( \widehat \beta \) under the observations \( \boldsymbol{x}^* \).
+- Repeat this process \( k \) times.
+
-
-
from numpy import *
-from numpy.random import randint, randn
-from time import time
-import matplotlib.mlab as mlab
-import matplotlib.pyplot as plt
+When you are done, you can draw a histogram of the relative frequency
+of \( \widehat \beta^* \). This is your estimate of the probability
+distribution \( p(t) \). Using this probability distribution you can
+estimate any statistics thereof. In principle you never draw the
+histogram of the relative frequency of \( \widehat{\beta}^* \). Instead
+you use the estimators corresponding to the statistic of interest. For
+example, if you are interested in estimating the variance of \( \widehat
+\beta \), apply the etsimator \( \widehat \sigma^2 \) to the values
+\( \widehat \beta^* \).
-# Returns mean of bootstrap samples
-def stat(data):
- return mean(data)
-
-# Bootstrap algorithm
-def bootstrap(data, statistic, R):
- t = zeros(R); n = len(data); inds = arange(n); t0 = time()
- # non-parametric bootstrap
- for i in range(R):
- t[i] = statistic(data[randint(0,n,n)])
-
- # analysis
- print("Runtime: %g sec" % (time()-t0)); print("Bootstrap Statistics :")
- print("original bias std. error")
- print("%8g %8g %14g %15g" % (statistic(data), std(data),mean(t),std(t)))
- return t
-
-
-mu, sigma = 100, 15
-datapoints = 10000
-x = mu + sigma*random.randn(datapoints)
-# bootstrap returns the data sample
-t = bootstrap(x, stat, datapoints)
-# the histogram of the bootstrapped data
-n, binsboot, patches = plt.hist(t, 50, normed=1, facecolor='red', alpha=0.75)
-
-# add a 'best fit' line
-y = mlab.normpdf( binsboot, mean(t), std(t))
-lt = plt.plot(binsboot, y, 'r--', linewidth=1)
-plt.xlabel('Smarts')
-plt.ylabel('Probability')
-plt.axis([99.5, 100.6, 0, 3.0])
-plt.grid(True)
-
-plt.show()
-
@@ -338,7 +301,7 @@ plt.show()
36
37
...
-
40
+
41
»
diff --git a/doc/pub/week37/html/._week37-bs028.html b/doc/pub/week37/html/._week37-bs028.html
index 59f9cb2d8..e5ae78549 100644
--- a/doc/pub/week37/html/._week37-bs028.html
+++ b/doc/pub/week37/html/._week37-bs028.html
@@ -69,6 +69,7 @@ Automatically generated HTML file from DocOnce source
2,
None,
'test-function-for-what-happens-with-ols-ridge-and-lasso'),
+ ('Rerunning the above code', 2, None, 'rerunning-the-above-code'),
("Invoking Bayes' theorem", 2, None, 'invoking-bayes-theorem'),
('Ridge and Bayes', 2, None, 'ridge-and-bayes'),
('Lasso and Bayes', 2, None, 'lasso-and-bayes'),
@@ -206,36 +207,37 @@ MathJax.Hub.Config({
Bayes' Theorem
Interpretations of Bayes' Theorem
Test Function for what happens with OLS, Ridge and Lasso
-
Invoking Bayes' theorem
-
Ridge and Bayes
-
Lasso and Bayes
-
Why resampling methods
-
Resampling methods
-
Resampling approaches can be computationally expensive
-
Why resampling methods ?
-
Statistical analysis
-
Resampling methods
-
Resampling methods: Jackknife and Bootstrap
-
Resampling methods: Jackknife
-
Jackknife code example
-
Resampling methods: Bootstrap
-
Resampling methods: Bootstrap background
-
Resampling methods: More Bootstrap background
-
Resampling methods: Bootstrap approach
-
Resampling methods: Bootstrap steps
-
Code example for the Bootstrap method
-
The bias-variance tradeoff
-
Example code for Bias-Variance tradeoff
-
Understanding what happens
-
Summing up
-
Another Example from Scikit-Learn's Repository
-
Various steps in cross-validation
-
How to set up the cross-validation for Ridge and/or Lasso
-
Cross-validation in brief
-
Code Example for Cross-validation and \( k \)-fold Cross-validation
-
More examples on bootstrap and cross-validation and errors
-
The same example but now with cross-validation
-
Cross-validation with Ridge
+
Rerunning the above code
+
Invoking Bayes' theorem
+
Ridge and Bayes
+
Lasso and Bayes
+
Why resampling methods
+
Resampling methods
+
Resampling approaches can be computationally expensive
+
Why resampling methods ?
+
Statistical analysis
+
Resampling methods
+
Resampling methods: Jackknife and Bootstrap
+
Resampling methods: Jackknife
+
Jackknife code example
+
Resampling methods: Bootstrap
+
Resampling methods: Bootstrap background
+
Resampling methods: More Bootstrap background
+
Resampling methods: Bootstrap approach
+
Resampling methods: Bootstrap steps
+
Code example for the Bootstrap method
+
The bias-variance tradeoff
+
Example code for Bias-Variance tradeoff
+
Understanding what happens
+
Summing up
+
Another Example from Scikit-Learn's Repository
+
Various steps in cross-validation
+
How to set up the cross-validation for Ridge and/or Lasso
+
Cross-validation in brief
+
Code Example for Cross-validation and \( k \)-fold Cross-validation
+
More examples on bootstrap and cross-validation and errors
+
The same example but now with cross-validation
+
Cross-validation with Ridge
@@ -251,69 +253,67 @@ MathJax.Hub.Config({
-
The bias-variance tradeoff
+
Code example for the Bootstrap method
-We will discuss the bias-variance tradeoff in the context of
-continuous predictions such as regression. However, many of the
-intuitions and ideas discussed here also carry over to classification
-tasks. Consider a dataset \( \mathcal{L} \) consisting of the data
-\( \mathbf{X}_\mathcal{L}=\{(y_j, \boldsymbol{x}_j), j=0\ldots n-1\} \).
+The following code starts with a Gaussian distribution with mean value
+\( \mu =100 \) and variance \( \sigma=15 \). We use this to generate the data
+used in the bootstrap analysis. The bootstrap analysis returns a data
+set after a given number of bootstrap operations (as many as we have
+data points). This data set consists of estimated mean values for each
+bootstrap operation. The histogram generated by the bootstrap method
+shows that the distribution for these mean values is also a Gaussian,
+centered around the mean value \( \mu=100 \) but with standard deviation
+\( \sigma/\sqrt{n} \), where \( n \) is the number of bootstrap samples (in
+this case the same as the number of original data points). The value
+of the standard deviation is what we expect from the central limit
+theorem.
-Let us assume that the true data is generated from a noisy model
-$$
-\boldsymbol{y}=f(\boldsymbol{x}) + \boldsymbol{\epsilon}
-$$
+
+
from numpy import *
+from numpy.random import randint, randn
+from time import time
+import matplotlib.mlab as mlab
+import matplotlib.pyplot as plt
-
-where \( \epsilon \) is normally distributed with mean zero and standard deviation \( \sigma^2 \).
+# Returns mean of bootstrap samples
+def stat(data):
+ return mean(data)
-
-In our derivation of the ordinary least squares method we defined then
-an approximation to the function \( f \) in terms of the parameters
-\( \boldsymbol{\beta} \) and the design matrix \( \boldsymbol{X} \) which embody our model,
-that is \( \boldsymbol{\tilde{y}}=\boldsymbol{X}\boldsymbol{\beta} \).
+# Bootstrap algorithm
+def bootstrap(data, statistic, R):
+ t = zeros(R); n = len(data); inds = arange(n); t0 = time()
+ # non-parametric bootstrap
+ for i in range(R):
+ t[i] = statistic(data[randint(0,n,n)])
-
-Thereafter we found the parameters \( \boldsymbol{\beta} \) by optimizing the means squared error via the so-called cost function
-$$
-C(\boldsymbol{X},\boldsymbol{\beta}) =\frac{1}{n}\sum_{i=0}^{n-1}(y_i-\tilde{y}_i)^2=\mathbb{E}\left[(\boldsymbol{y}-\boldsymbol{\tilde{y}})^2\right].
-$$
+ # analysis
+ print("Runtime: %g sec" % (time()-t0)); print("Bootstrap Statistics :")
+ print("original bias std. error")
+ print("%8g %8g %14g %15g" % (statistic(data), std(data),mean(t),std(t)))
+ return t
-
-We can rewrite this as
-$$
-\mathbb{E}\left[(\boldsymbol{y}-\boldsymbol{\tilde{y}})^2\right]=\frac{1}{n}\sum_i(f_i-\mathbb{E}\left[\boldsymbol{\tilde{y}}\right])^2+\frac{1}{n}\sum_i(\tilde{y}_i-\mathbb{E}\left[\boldsymbol{\tilde{y}}\right])^2+\sigma^2.
-$$
-
-The three terms represent the square of the bias of the learning
-method, which can be thought of as the error caused by the simplifying
-assumptions built into the method. The second term represents the
-variance of the chosen model and finally the last terms is variance of
-the error \( \boldsymbol{\epsilon} \).
+mu, sigma = 100, 15
+datapoints = 10000
+x = mu + sigma*random.randn(datapoints)
+# bootstrap returns the data sample
+t = bootstrap(x, stat, datapoints)
+# the histogram of the bootstrapped data
+n, binsboot, patches = plt.hist(t, 50, normed=1, facecolor='red', alpha=0.75)
-
-To derive this equation, we need to recall that the variance of \( \boldsymbol{y} \) and \( \boldsymbol{\epsilon} \) are both equal to \( \sigma^2 \). The mean value of \( \boldsymbol{\epsilon} \) is by definition equal to zero. Furthermore, the function \( f \) is not a stochastics variable, idem for \( \boldsymbol{\tilde{y}} \).
-We use a more compact notation in terms of the expectation value
-$$
-\mathbb{E}\left[(\boldsymbol{y}-\boldsymbol{\tilde{y}})^2\right]=\mathbb{E}\left[(\boldsymbol{f}+\boldsymbol{\epsilon}-\boldsymbol{\tilde{y}})^2\right],
-$$
-
-and adding and subtracting \( \mathbb{E}\left[\boldsymbol{\tilde{y}}\right] \) we get
-$$
-\mathbb{E}\left[(\boldsymbol{y}-\boldsymbol{\tilde{y}})^2\right]=\mathbb{E}\left[(\boldsymbol{f}+\boldsymbol{\epsilon}-\boldsymbol{\tilde{y}}+\mathbb{E}\left[\boldsymbol{\tilde{y}}\right]-\mathbb{E}\left[\boldsymbol{\tilde{y}}\right])^2\right],
-$$
-
-which, using the abovementioned expectation values can be rewritten as
-$$
-\mathbb{E}\left[(\boldsymbol{y}-\boldsymbol{\tilde{y}})^2\right]=\mathbb{E}\left[(\boldsymbol{y}-\mathbb{E}\left[\boldsymbol{\tilde{y}}\right])^2\right]+\mathrm{Var}\left[\boldsymbol{\tilde{y}}\right]+\sigma^2,
-$$
-
-that is the rewriting in terms of the so-called bias, the variance of the model \( \boldsymbol{\tilde{y}} \) and the variance of \( \boldsymbol{\epsilon} \).
+# add a 'best fit' line
+y = mlab.normpdf( binsboot, mean(t), std(t))
+lt = plt.plot(binsboot, y, 'r--', linewidth=1)
+plt.xlabel('Smarts')
+plt.ylabel('Probability')
+plt.axis([99.5, 100.6, 0, 3.0])
+plt.grid(True)
+plt.show()
+
@@ -340,7 +340,7 @@ that is the rewriting in terms of the so-called bias, the variance of the model
37
38
...
-
40
+
41
»
diff --git a/doc/pub/week37/html/._week37-bs029.html b/doc/pub/week37/html/._week37-bs029.html
index 7c0c5cccd..5b6079c13 100644
--- a/doc/pub/week37/html/._week37-bs029.html
+++ b/doc/pub/week37/html/._week37-bs029.html
@@ -69,6 +69,7 @@ Automatically generated HTML file from DocOnce source
2,
None,
'test-function-for-what-happens-with-ols-ridge-and-lasso'),
+ ('Rerunning the above code', 2, None, 'rerunning-the-above-code'),
("Invoking Bayes' theorem", 2, None, 'invoking-bayes-theorem'),
('Ridge and Bayes', 2, None, 'ridge-and-bayes'),
('Lasso and Bayes', 2, None, 'lasso-and-bayes'),
@@ -206,36 +207,37 @@ MathJax.Hub.Config({
Bayes' Theorem
Interpretations of Bayes' Theorem
Test Function for what happens with OLS, Ridge and Lasso
-
Invoking Bayes' theorem
-
Ridge and Bayes
-
Lasso and Bayes
-
Why resampling methods
-
Resampling methods
-
Resampling approaches can be computationally expensive
-
Why resampling methods ?
-
Statistical analysis
-
Resampling methods
-
Resampling methods: Jackknife and Bootstrap
-
Resampling methods: Jackknife
-
Jackknife code example
-
Resampling methods: Bootstrap
-
Resampling methods: Bootstrap background
-
Resampling methods: More Bootstrap background
-
Resampling methods: Bootstrap approach
-
Resampling methods: Bootstrap steps
-
Code example for the Bootstrap method
-
The bias-variance tradeoff
-
Example code for Bias-Variance tradeoff
-
Understanding what happens
-
Summing up
-
Another Example from Scikit-Learn's Repository
-
Various steps in cross-validation
-
How to set up the cross-validation for Ridge and/or Lasso
-
Cross-validation in brief
-
Code Example for Cross-validation and \( k \)-fold Cross-validation
-
More examples on bootstrap and cross-validation and errors
-
The same example but now with cross-validation
-
Cross-validation with Ridge
+
Rerunning the above code
+
Invoking Bayes' theorem
+
Ridge and Bayes
+
Lasso and Bayes
+
Why resampling methods
+
Resampling methods
+
Resampling approaches can be computationally expensive
+
Why resampling methods ?
+
Statistical analysis
+
Resampling methods
+
Resampling methods: Jackknife and Bootstrap
+
Resampling methods: Jackknife
+
Jackknife code example
+
Resampling methods: Bootstrap
+
Resampling methods: Bootstrap background
+
Resampling methods: More Bootstrap background
+
Resampling methods: Bootstrap approach
+
Resampling methods: Bootstrap steps
+
Code example for the Bootstrap method
+
The bias-variance tradeoff
+
Example code for Bias-Variance tradeoff
+
Understanding what happens
+
Summing up
+
Another Example from Scikit-Learn's Repository
+
Various steps in cross-validation
+
How to set up the cross-validation for Ridge and/or Lasso
+
Cross-validation in brief
+
Code Example for Cross-validation and \( k \)-fold Cross-validation
+
More examples on bootstrap and cross-validation and errors
+
The same example but now with cross-validation
+
Cross-validation with Ridge
@@ -251,65 +253,69 @@ MathJax.Hub.Config({
-
Example code for Bias-Variance tradeoff
+
The bias-variance tradeoff
+
+We will discuss the bias-variance tradeoff in the context of
+continuous predictions such as regression. However, many of the
+intuitions and ideas discussed here also carry over to classification
+tasks. Consider a dataset \( \mathcal{L} \) consisting of the data
+\( \mathbf{X}_\mathcal{L}=\{(y_j, \boldsymbol{x}_j), j=0\ldots n-1\} \).
-
-
import matplotlib.pyplot as plt
-import numpy as np
-from sklearn.linear_model import LinearRegression, Ridge, Lasso
-from sklearn.preprocessing import PolynomialFeatures
-from sklearn.model_selection import train_test_split
-from sklearn.pipeline import make_pipeline
-from sklearn.utils import resample
+
+Let us assume that the true data is generated from a noisy model
-np.random.seed(2018)
+$$
+\boldsymbol{y}=f(\boldsymbol{x}) + \boldsymbol{\epsilon}
+$$
-n = 500
-n_boostraps = 100
-degree = 18 # A quite high value, just to show.
-noise = 0.1
+
+where \( \epsilon \) is normally distributed with mean zero and standard deviation \( \sigma^2 \).
-# Make data set.
-x = np.linspace(-1, 3, n).reshape(-1, 1)
-y = np.exp(-x**2) + 1.5 * np.exp(-(x-2)**2) + np.random.normal(0, 0.1, x.shape)
+
+In our derivation of the ordinary least squares method we defined then
+an approximation to the function \( f \) in terms of the parameters
+\( \boldsymbol{\beta} \) and the design matrix \( \boldsymbol{X} \) which embody our model,
+that is \( \boldsymbol{\tilde{y}}=\boldsymbol{X}\boldsymbol{\beta} \).
-# Hold out some test data that is never used in training.
-x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2)
+
+Thereafter we found the parameters \( \boldsymbol{\beta} \) by optimizing the means squared error via the so-called cost function
+$$
+C(\boldsymbol{X},\boldsymbol{\beta}) =\frac{1}{n}\sum_{i=0}^{n-1}(y_i-\tilde{y}_i)^2=\mathbb{E}\left[(\boldsymbol{y}-\boldsymbol{\tilde{y}})^2\right].
+$$
-# Combine x transformation and model into one operation.
-# Not neccesary, but convenient.
-model = make_pipeline(PolynomialFeatures(degree=degree), LinearRegression(fit_intercept=False))
+
+We can rewrite this as
+$$
+\mathbb{E}\left[(\boldsymbol{y}-\boldsymbol{\tilde{y}})^2\right]=\frac{1}{n}\sum_i(f_i-\mathbb{E}\left[\boldsymbol{\tilde{y}}\right])^2+\frac{1}{n}\sum_i(\tilde{y}_i-\mathbb{E}\left[\boldsymbol{\tilde{y}}\right])^2+\sigma^2.
+$$
-# The following (m x n_bootstraps) matrix holds the column vectors y_pred
-# for each bootstrap iteration.
-y_pred = np.empty((y_test.shape[0], n_boostraps))
-for i in range(n_boostraps):
- x_, y_ = resample(x_train, y_train)
+
+The three terms represent the square of the bias of the learning
+method, which can be thought of as the error caused by the simplifying
+assumptions built into the method. The second term represents the
+variance of the chosen model and finally the last terms is variance of
+the error \( \boldsymbol{\epsilon} \).
- # Evaluate the new model on the same test data each time.
- y_pred[:, i] = model.fit(x_, y_).predict(x_test).ravel()
+
+To derive this equation, we need to recall that the variance of \( \boldsymbol{y} \) and \( \boldsymbol{\epsilon} \) are both equal to \( \sigma^2 \). The mean value of \( \boldsymbol{\epsilon} \) is by definition equal to zero. Furthermore, the function \( f \) is not a stochastics variable, idem for \( \boldsymbol{\tilde{y}} \).
+We use a more compact notation in terms of the expectation value
+$$
+\mathbb{E}\left[(\boldsymbol{y}-\boldsymbol{\tilde{y}})^2\right]=\mathbb{E}\left[(\boldsymbol{f}+\boldsymbol{\epsilon}-\boldsymbol{\tilde{y}})^2\right],
+$$
-# Note: Expectations and variances taken w.r.t. different training
-# data sets, hence the axis=1. Subsequent means are taken across the test data
-# set in order to obtain a total value, but before this we have error/bias/variance
-# calculated per data point in the test set.
-# Note 2: The use of keepdims=True is important in the calculation of bias as this
-# maintains the column vector form. Dropping this yields very unexpected results.
-error = np.mean( np.mean((y_test - y_pred)**2, axis=1, keepdims=True) )
-bias = np.mean( (y_test - np.mean(y_pred, axis=1, keepdims=True))**2 )
-variance = np.mean( np.var(y_pred, axis=1, keepdims=True) )
-print('Error:', error)
-print('Bias^2:', bias)
-print('Var:', variance)
-print('{} >= {} + {} = {}'.format(error, bias, variance, bias+variance))
+and adding and subtracting \( \mathbb{E}\left[\boldsymbol{\tilde{y}}\right] \) we get
+$$
+\mathbb{E}\left[(\boldsymbol{y}-\boldsymbol{\tilde{y}})^2\right]=\mathbb{E}\left[(\boldsymbol{f}+\boldsymbol{\epsilon}-\boldsymbol{\tilde{y}}+\mathbb{E}\left[\boldsymbol{\tilde{y}}\right]-\mathbb{E}\left[\boldsymbol{\tilde{y}}\right])^2\right],
+$$
+
+which, using the abovementioned expectation values can be rewritten as
+$$
+\mathbb{E}\left[(\boldsymbol{y}-\boldsymbol{\tilde{y}})^2\right]=\mathbb{E}\left[(\boldsymbol{y}-\mathbb{E}\left[\boldsymbol{\tilde{y}}\right])^2\right]+\mathrm{Var}\left[\boldsymbol{\tilde{y}}\right]+\sigma^2,
+$$
+
+that is the rewriting in terms of the so-called bias, the variance of the model \( \boldsymbol{\tilde{y}} \) and the variance of \( \boldsymbol{\epsilon} \).
-plt.plot(x[::5, :], y[::5, :], label='f(x)')
-plt.scatter(x_test, y_test, label='Data points')
-plt.scatter(x_test, np.mean(y_pred, axis=1), label='Pred')
-plt.legend()
-plt.show()
-
@@ -336,7 +342,7 @@ plt.show()
38
39
...
-
40
+
41
»
diff --git a/doc/pub/week37/html/._week37-bs030.html b/doc/pub/week37/html/._week37-bs030.html
index abac74154..d8cb071fd 100644
--- a/doc/pub/week37/html/._week37-bs030.html
+++ b/doc/pub/week37/html/._week37-bs030.html
@@ -69,6 +69,7 @@ Automatically generated HTML file from DocOnce source
2,
None,
'test-function-for-what-happens-with-ols-ridge-and-lasso'),
+ ('Rerunning the above code', 2, None, 'rerunning-the-above-code'),
("Invoking Bayes' theorem", 2, None, 'invoking-bayes-theorem'),
('Ridge and Bayes', 2, None, 'ridge-and-bayes'),
('Lasso and Bayes', 2, None, 'lasso-and-bayes'),
@@ -206,36 +207,37 @@ MathJax.Hub.Config({
Bayes' Theorem
Interpretations of Bayes' Theorem
Test Function for what happens with OLS, Ridge and Lasso
-
Invoking Bayes' theorem
-
Ridge and Bayes
-
Lasso and Bayes
-
Why resampling methods
-
Resampling methods
-
Resampling approaches can be computationally expensive
-
Why resampling methods ?
-
Statistical analysis
-
Resampling methods
-
Resampling methods: Jackknife and Bootstrap
-
Resampling methods: Jackknife
-
Jackknife code example
-
Resampling methods: Bootstrap
-
Resampling methods: Bootstrap background
-
Resampling methods: More Bootstrap background
-
Resampling methods: Bootstrap approach
-
Resampling methods: Bootstrap steps
-
Code example for the Bootstrap method
-
The bias-variance tradeoff
-
Example code for Bias-Variance tradeoff
-
Understanding what happens
-
Summing up
-
Another Example from Scikit-Learn's Repository
-
Various steps in cross-validation
-
How to set up the cross-validation for Ridge and/or Lasso
-
Cross-validation in brief
-
Code Example for Cross-validation and \( k \)-fold Cross-validation
-
More examples on bootstrap and cross-validation and errors
-
The same example but now with cross-validation
-
Cross-validation with Ridge
+
Rerunning the above code
+
Invoking Bayes' theorem
+
Ridge and Bayes
+
Lasso and Bayes
+
Why resampling methods
+
Resampling methods
+
Resampling approaches can be computationally expensive
+
Why resampling methods ?
+
Statistical analysis
+
Resampling methods
+
Resampling methods: Jackknife and Bootstrap
+
Resampling methods: Jackknife
+
Jackknife code example
+
Resampling methods: Bootstrap
+
Resampling methods: Bootstrap background
+
Resampling methods: More Bootstrap background
+
Resampling methods: Bootstrap approach
+
Resampling methods: Bootstrap steps
+
Code example for the Bootstrap method
+
The bias-variance tradeoff
+
Example code for Bias-Variance tradeoff
+
Understanding what happens
+
Summing up
+
Another Example from Scikit-Learn's Repository
+
Various steps in cross-validation
+
How to set up the cross-validation for Ridge and/or Lasso
+
Cross-validation in brief
+
Code Example for Cross-validation and \( k \)-fold Cross-validation
+
More examples on bootstrap and cross-validation and errors
+
The same example but now with cross-validation
+
Cross-validation with Ridge
@@ -251,7 +253,7 @@ MathJax.Hub.Config({
-
Understanding what happens
+
Example code for Bias-Variance tradeoff
@@ -265,40 +267,48 @@ MathJax.Hub.Config({
np.random.seed(2018)
-n = 40
+n = 500
n_boostraps = 100
-maxdegree = 14
-
+degree = 18 # A quite high value, just to show.
+noise = 0.1
# Make data set.
-x = np.linspace(-3, 3, n).reshape(-1, 1)
-y = np.exp(-x**2) + 1.5 * np.exp(-(x-2)**2)+ np.random.normal(0, 0.1, x.shape)
-error = np.zeros(maxdegree)
-bias = np.zeros(maxdegree)
-variance = np.zeros(maxdegree)
-polydegree = np.zeros(maxdegree)
+x = np.linspace(-1, 3, n).reshape(-1, 1)
+y = np.exp(-x**2) + 1.5 * np.exp(-(x-2)**2) + np.random.normal(0, 0.1, x.shape)
+
+# Hold out some test data that is never used in training.
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2)
-for degree in range(maxdegree):
- model = make_pipeline(PolynomialFeatures(degree=degree), LinearRegression(fit_intercept=False))
- y_pred = np.empty((y_test.shape[0], n_boostraps))
- for i in range(n_boostraps):
- x_, y_ = resample(x_train, y_train)
- y_pred[:, i] = model.fit(x_, y_).predict(x_test).ravel()
+# Combine x transformation and model into one operation.
+# Not neccesary, but convenient.
+model = make_pipeline(PolynomialFeatures(degree=degree), LinearRegression(fit_intercept=False))
- polydegree[degree] = degree
- error[degree] = np.mean( np.mean((y_test - y_pred)**2, axis=1, keepdims=True) )
- bias[degree] = np.mean( (y_test - np.mean(y_pred, axis=1, keepdims=True))**2 )
- variance[degree] = np.mean( np.var(y_pred, axis=1, keepdims=True) )
- print('Polynomial degree:', degree)
- print('Error:', error[degree])
- print('Bias^2:', bias[degree])
- print('Var:', variance[degree])
- print('{} >= {} + {} = {}'.format(error[degree], bias[degree], variance[degree], bias[degree]+variance[degree]))
+# The following (m x n_bootstraps) matrix holds the column vectors y_pred
+# for each bootstrap iteration.
+y_pred = np.empty((y_test.shape[0], n_boostraps))
+for i in range(n_boostraps):
+ x_, y_ = resample(x_train, y_train)
-plt.plot(polydegree, error, label='Error')
-plt.plot(polydegree, bias, label='bias')
-plt.plot(polydegree, variance, label='Variance')
+ # Evaluate the new model on the same test data each time.
+ y_pred[:, i] = model.fit(x_, y_).predict(x_test).ravel()
+
+# Note: Expectations and variances taken w.r.t. different training
+# data sets, hence the axis=1. Subsequent means are taken across the test data
+# set in order to obtain a total value, but before this we have error/bias/variance
+# calculated per data point in the test set.
+# Note 2: The use of keepdims=True is important in the calculation of bias as this
+# maintains the column vector form. Dropping this yields very unexpected results.
+error = np.mean( np.mean((y_test - y_pred)**2, axis=1, keepdims=True) )
+bias = np.mean( (y_test - np.mean(y_pred, axis=1, keepdims=True))**2 )
+variance = np.mean( np.var(y_pred, axis=1, keepdims=True) )
+print('Error:', error)
+print('Bias^2:', bias)
+print('Var:', variance)
+print('{} >= {} + {} = {}'.format(error, bias, variance, bias+variance))
+
+plt.plot(x[::5, :], y[::5, :], label='f(x)')
+plt.scatter(x_test, y_test, label='Data points')
+plt.scatter(x_test, np.mean(y_pred, axis=1), label='Pred')
plt.legend()
plt.show()
@@ -327,6 +337,8 @@ plt.show()
38
39
40
+ ...
+ 41
»
diff --git a/doc/pub/week37/html/._week37-bs031.html b/doc/pub/week37/html/._week37-bs031.html
index 0e0cdc374..94e215c0b 100644
--- a/doc/pub/week37/html/._week37-bs031.html
+++ b/doc/pub/week37/html/._week37-bs031.html
@@ -69,6 +69,7 @@ Automatically generated HTML file from DocOnce source
2,
None,
'test-function-for-what-happens-with-ols-ridge-and-lasso'),
+ ('Rerunning the above code', 2, None, 'rerunning-the-above-code'),
("Invoking Bayes' theorem", 2, None, 'invoking-bayes-theorem'),
('Ridge and Bayes', 2, None, 'ridge-and-bayes'),
('Lasso and Bayes', 2, None, 'lasso-and-bayes'),
@@ -206,36 +207,37 @@ MathJax.Hub.Config({
Bayes' Theorem
Interpretations of Bayes' Theorem
Test Function for what happens with OLS, Ridge and Lasso
- Invoking Bayes' theorem
- Ridge and Bayes
- Lasso and Bayes
- Why resampling methods
- Resampling methods
- Resampling approaches can be computationally expensive
- Why resampling methods ?
- Statistical analysis
- Resampling methods
- Resampling methods: Jackknife and Bootstrap
- Resampling methods: Jackknife
- Jackknife code example
- Resampling methods: Bootstrap
- Resampling methods: Bootstrap background
- Resampling methods: More Bootstrap background
- Resampling methods: Bootstrap approach
- Resampling methods: Bootstrap steps
- Code example for the Bootstrap method
- The bias-variance tradeoff
- Example code for Bias-Variance tradeoff
- Understanding what happens
- Summing up
- Another Example from Scikit-Learn's Repository
- Various steps in cross-validation
- How to set up the cross-validation for Ridge and/or Lasso
- Cross-validation in brief
- Code Example for Cross-validation and \( k \)-fold Cross-validation
- More examples on bootstrap and cross-validation and errors
- The same example but now with cross-validation
- Cross-validation with Ridge
+ Rerunning the above code
+ Invoking Bayes' theorem
+ Ridge and Bayes
+ Lasso and Bayes
+ Why resampling methods
+ Resampling methods
+ Resampling approaches can be computationally expensive
+ Why resampling methods ?
+ Statistical analysis
+ Resampling methods
+ Resampling methods: Jackknife and Bootstrap
+ Resampling methods: Jackknife
+ Jackknife code example
+ Resampling methods: Bootstrap
+ Resampling methods: Bootstrap background
+ Resampling methods: More Bootstrap background
+ Resampling methods: Bootstrap approach
+ Resampling methods: Bootstrap steps
+ Code example for the Bootstrap method
+ The bias-variance tradeoff
+ Example code for Bias-Variance tradeoff
+ Understanding what happens
+ Summing up
+ Another Example from Scikit-Learn's Repository
+ Various steps in cross-validation
+ How to set up the cross-validation for Ridge and/or Lasso
+ Cross-validation in brief
+ Code Example for Cross-validation and \( k \)-fold Cross-validation
+ More examples on bootstrap and cross-validation and errors
+ The same example but now with cross-validation
+ Cross-validation with Ridge
@@ -249,42 +251,59 @@ MathJax.Hub.Config({
-
-
-Summing up
+
+Understanding what happens
-The bias-variance tradeoff summarizes the fundamental tension in
-machine learning, particularly supervised learning, between the
-complexity of a model and the amount of training data needed to train
-it. Since data is often limited, in practice it is often useful to
-use a less-complex model with higher bias, that is a model whose asymptotic
-performance is worse than another model because it is easier to
-train and less sensitive to sampling noise arising from having a
-finite-sized training dataset (smaller variance).
-
-The above equations tell us that in
-order to minimize the expected test error, we need to select a
-statistical learning method that simultaneously achieves low variance
-and low bias. Note that variance is inherently a nonnegative quantity,
-and squared bias is also nonnegative. Hence, we see that the expected
-test MSE can never lie below \( Var(\epsilon) \), the irreducible error.
+
+
import matplotlib.pyplot as plt
+import numpy as np
+from sklearn.linear_model import LinearRegression, Ridge, Lasso
+from sklearn.preprocessing import PolynomialFeatures
+from sklearn.model_selection import train_test_split
+from sklearn.pipeline import make_pipeline
+from sklearn.utils import resample
-
-What do we mean by the variance and bias of a statistical learning
-method? The variance refers to the amount by which our model would change if we
-estimated it using a different training data set. Since the training
-data are used to fit the statistical learning method, different
-training data sets will result in a different estimate. But ideally the
-estimate for our model should not vary too much between training
-sets. However, if a method has high variance then small changes in
-the training data can result in large changes in the model. In general, more
-flexible statistical methods have higher variance.
+np.random.seed(2018)
-
-You may also find this recent article of interest.
+n = 40
+n_boostraps = 100
+maxdegree = 14
+
+# Make data set.
+x = np.linspace(-3, 3, n).reshape(-1, 1)
+y = np.exp(-x**2) + 1.5 * np.exp(-(x-2)**2)+ np.random.normal(0, 0.1, x.shape)
+error = np.zeros(maxdegree)
+bias = np.zeros(maxdegree)
+variance = np.zeros(maxdegree)
+polydegree = np.zeros(maxdegree)
+x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2)
+
+for degree in range(maxdegree):
+ model = make_pipeline(PolynomialFeatures(degree=degree), LinearRegression(fit_intercept=False))
+ y_pred = np.empty((y_test.shape[0], n_boostraps))
+ for i in range(n_boostraps):
+ x_, y_ = resample(x_train, y_train)
+ y_pred[:, i] = model.fit(x_, y_).predict(x_test).ravel()
+
+ polydegree[degree] = degree
+ error[degree] = np.mean( np.mean((y_test - y_pred)**2, axis=1, keepdims=True) )
+ bias[degree] = np.mean( (y_test - np.mean(y_pred, axis=1, keepdims=True))**2 )
+ variance[degree] = np.mean( np.var(y_pred, axis=1, keepdims=True) )
+ print('Polynomial degree:', degree)
+ print('Error:', error[degree])
+ print('Bias^2:', bias[degree])
+ print('Var:', variance[degree])
+ print('{} >= {} + {} = {}'.format(error[degree], bias[degree], variance[degree], bias[degree]+variance[degree]))
+
+plt.plot(polydegree, error, label='Error')
+plt.plot(polydegree, bias, label='bias')
+plt.plot(polydegree, variance, label='Variance')
+plt.legend()
+plt.show()
+
@@ -309,6 +328,7 @@ You may also find this recent 38
39
40
+ 41
»
diff --git a/doc/pub/week37/html/._week37-bs032.html b/doc/pub/week37/html/._week37-bs032.html
index 571591c50..d5abaff49 100644
--- a/doc/pub/week37/html/._week37-bs032.html
+++ b/doc/pub/week37/html/._week37-bs032.html
@@ -69,6 +69,7 @@ Automatically generated HTML file from DocOnce source
2,
None,
'test-function-for-what-happens-with-ols-ridge-and-lasso'),
+ ('Rerunning the above code', 2, None, 'rerunning-the-above-code'),
("Invoking Bayes' theorem", 2, None, 'invoking-bayes-theorem'),
('Ridge and Bayes', 2, None, 'ridge-and-bayes'),
('Lasso and Bayes', 2, None, 'lasso-and-bayes'),
@@ -206,36 +207,37 @@ MathJax.Hub.Config({
Bayes' Theorem
Interpretations of Bayes' Theorem
Test Function for what happens with OLS, Ridge and Lasso
- Invoking Bayes' theorem
- Ridge and Bayes
- Lasso and Bayes
- Why resampling methods
- Resampling methods
- Resampling approaches can be computationally expensive
- Why resampling methods ?
- Statistical analysis
- Resampling methods
- Resampling methods: Jackknife and Bootstrap
- Resampling methods: Jackknife
- Jackknife code example
- Resampling methods: Bootstrap
- Resampling methods: Bootstrap background
- Resampling methods: More Bootstrap background
- Resampling methods: Bootstrap approach
- Resampling methods: Bootstrap steps
- Code example for the Bootstrap method
- The bias-variance tradeoff
- Example code for Bias-Variance tradeoff
- Understanding what happens
- Summing up
- Another Example from Scikit-Learn's Repository
- Various steps in cross-validation
- How to set up the cross-validation for Ridge and/or Lasso
- Cross-validation in brief
- Code Example for Cross-validation and \( k \)-fold Cross-validation
- More examples on bootstrap and cross-validation and errors
- The same example but now with cross-validation
- Cross-validation with Ridge
+ Rerunning the above code
+ Invoking Bayes' theorem
+ Ridge and Bayes
+ Lasso and Bayes
+ Why resampling methods
+ Resampling methods
+ Resampling approaches can be computationally expensive
+ Why resampling methods ?
+ Statistical analysis
+ Resampling methods
+ Resampling methods: Jackknife and Bootstrap
+ Resampling methods: Jackknife
+ Jackknife code example
+ Resampling methods: Bootstrap
+ Resampling methods: Bootstrap background
+ Resampling methods: More Bootstrap background
+ Resampling methods: Bootstrap approach
+ Resampling methods: Bootstrap steps
+ Code example for the Bootstrap method
+ The bias-variance tradeoff
+ Example code for Bias-Variance tradeoff
+ Understanding what happens
+ Summing up
+ Another Example from Scikit-Learn's Repository
+ Various steps in cross-validation
+ How to set up the cross-validation for Ridge and/or Lasso
+ Cross-validation in brief
+ Code Example for Cross-validation and \( k \)-fold Cross-validation
+ More examples on bootstrap and cross-validation and errors
+ The same example but now with cross-validation
+ Cross-validation with Ridge
@@ -249,84 +251,42 @@ MathJax.Hub.Config({
-
+
+
+Summing up
-Another Example from Scikit-Learn's Repository
+The bias-variance tradeoff summarizes the fundamental tension in
+machine learning, particularly supervised learning, between the
+complexity of a model and the amount of training data needed to train
+it. Since data is often limited, in practice it is often useful to
+use a less-complex model with higher bias, that is a model whose asymptotic
+performance is worse than another model because it is easier to
+train and less sensitive to sampling noise arising from having a
+finite-sized training dataset (smaller variance).
-
-
"""
-============================
-Underfitting vs. Overfitting
-============================
+
+The above equations tell us that in
+order to minimize the expected test error, we need to select a
+statistical learning method that simultaneously achieves low variance
+and low bias. Note that variance is inherently a nonnegative quantity,
+and squared bias is also nonnegative. Hence, we see that the expected
+test MSE can never lie below \( Var(\epsilon) \), the irreducible error.
-This example demonstrates the problems of underfitting and overfitting and
-how we can use linear regression with polynomial features to approximate
-nonlinear functions. The plot shows the function that we want to approximate,
-which is a part of the cosine function. In addition, the samples from the
-real function and the approximations of different models are displayed. The
-models have polynomial features of different degrees. We can see that a
-linear function (polynomial with degree 1) is not sufficient to fit the
-training samples. This is called **underfitting**. A polynomial of degree 4
-approximates the true function almost perfectly. However, for higher degrees
-the model will **overfit** the training data, i.e. it learns the noise of the
-training data.
-We evaluate quantitatively **overfitting** / **underfitting** by using
-cross-validation. We calculate the mean squared error (MSE) on the validation
-set, the higher, the less likely the model generalizes correctly from the
-training data.
-"""
+
+What do we mean by the variance and bias of a statistical learning
+method? The variance refers to the amount by which our model would change if we
+estimated it using a different training data set. Since the training
+data are used to fit the statistical learning method, different
+training data sets will result in a different estimate. But ideally the
+estimate for our model should not vary too much between training
+sets. However, if a method has high variance then small changes in
+the training data can result in large changes in the model. In general, more
+flexible statistical methods have higher variance.
-print(__doc__)
+
+You may also find this recent article of interest.
-import numpy as np
-import matplotlib.pyplot as plt
-from sklearn.pipeline import Pipeline
-from sklearn.preprocessing import PolynomialFeatures
-from sklearn.linear_model import LinearRegression
-from sklearn.model_selection import cross_val_score
-
-
-def true_fun(X):
- return np.cos(1.5 * np.pi * X)
-
-np.random.seed(0)
-
-n_samples = 30
-degrees = [1, 4, 15]
-
-X = np.sort(np.random.rand(n_samples))
-y = true_fun(X) + np.random.randn(n_samples) * 0.1
-
-plt.figure(figsize=(14, 5))
-for i in range(len(degrees)):
- ax = plt.subplot(1, len(degrees), i + 1)
- plt.setp(ax, xticks=(), yticks=())
-
- polynomial_features = PolynomialFeatures(degree=degrees[i],
- include_bias=False)
- linear_regression = LinearRegression()
- pipeline = Pipeline([("polynomial_features", polynomial_features),
- ("linear_regression", linear_regression)])
- pipeline.fit(X[:, np.newaxis], y)
-
- # Evaluate the models using crossvalidation
- scores = cross_val_score(pipeline, X[:, np.newaxis], y,
- scoring="neg_mean_squared_error", cv=10)
-
- X_test = np.linspace(0, 1, 100)
- plt.plot(X_test, pipeline.predict(X_test[:, np.newaxis]), label="Model")
- plt.plot(X_test, true_fun(X_test), label="True function")
- plt.scatter(X, y, edgecolor='b', s=20, label="Samples")
- plt.xlabel("x")
- plt.ylabel("y")
- plt.xlim((0, 1))
- plt.ylim((-2, 2))
- plt.legend(loc="best")
- plt.title("Degree {}\nMSE = {:.2e}(+/- {:.2e})".format(
- degrees[i], -scores.mean(), scores.std()))
-plt.show()
-
@@ -350,6 +310,7 @@ plt.show()
38
39
40
+ 41
»
diff --git a/doc/pub/week37/html/._week37-bs033.html b/doc/pub/week37/html/._week37-bs033.html
index 89eb15903..2313c3c86 100644
--- a/doc/pub/week37/html/._week37-bs033.html
+++ b/doc/pub/week37/html/._week37-bs033.html
@@ -69,6 +69,7 @@ Automatically generated HTML file from DocOnce source
2,
None,
'test-function-for-what-happens-with-ols-ridge-and-lasso'),
+ ('Rerunning the above code', 2, None, 'rerunning-the-above-code'),
("Invoking Bayes' theorem", 2, None, 'invoking-bayes-theorem'),
('Ridge and Bayes', 2, None, 'ridge-and-bayes'),
('Lasso and Bayes', 2, None, 'lasso-and-bayes'),
@@ -206,36 +207,37 @@ MathJax.Hub.Config({
Bayes' Theorem
Interpretations of Bayes' Theorem
Test Function for what happens with OLS, Ridge and Lasso
- Invoking Bayes' theorem
- Ridge and Bayes
- Lasso and Bayes
- Why resampling methods
- Resampling methods
- Resampling approaches can be computationally expensive
- Why resampling methods ?
- Statistical analysis
- Resampling methods
- Resampling methods: Jackknife and Bootstrap
- Resampling methods: Jackknife
- Jackknife code example
- Resampling methods: Bootstrap
- Resampling methods: Bootstrap background
- Resampling methods: More Bootstrap background
- Resampling methods: Bootstrap approach
- Resampling methods: Bootstrap steps
- Code example for the Bootstrap method
- The bias-variance tradeoff
- Example code for Bias-Variance tradeoff
- Understanding what happens
- Summing up
- Another Example from Scikit-Learn's Repository
- Various steps in cross-validation
- How to set up the cross-validation for Ridge and/or Lasso
- Cross-validation in brief
- Code Example for Cross-validation and \( k \)-fold Cross-validation
- More examples on bootstrap and cross-validation and errors
- The same example but now with cross-validation
- Cross-validation with Ridge
+ Rerunning the above code
+ Invoking Bayes' theorem
+ Ridge and Bayes
+ Lasso and Bayes
+ Why resampling methods
+ Resampling methods
+ Resampling approaches can be computationally expensive
+ Why resampling methods ?
+ Statistical analysis
+ Resampling methods
+ Resampling methods: Jackknife and Bootstrap
+ Resampling methods: Jackknife
+ Jackknife code example
+ Resampling methods: Bootstrap
+ Resampling methods: Bootstrap background
+ Resampling methods: More Bootstrap background
+ Resampling methods: Bootstrap approach
+ Resampling methods: Bootstrap steps
+ Code example for the Bootstrap method
+ The bias-variance tradeoff
+ Example code for Bias-Variance tradeoff
+ Understanding what happens
+ Summing up
+ Another Example from Scikit-Learn's Repository
+ Various steps in cross-validation
+ How to set up the cross-validation for Ridge and/or Lasso
+ Cross-validation in brief
+ Code Example for Cross-validation and \( k \)-fold Cross-validation
+ More examples on bootstrap and cross-validation and errors
+ The same example but now with cross-validation
+ Cross-validation with Ridge
@@ -249,26 +251,84 @@ MathJax.Hub.Config({
-
-
-Various steps in cross-validation
+
+Another Example from Scikit-Learn's Repository
-When the repetitive splitting of the data set is done randomly,
-samples may accidently end up in a fast majority of the splits in
-either training or test set. Such samples may have an unbalanced
-influence on either model building or prediction evaluation. To avoid
-this \( k \)-fold cross-validation structures the data splitting. The
-samples are divided into \( k \) more or less equally sized exhaustive and
-mutually exclusive subsets. In turn (at each split) one of these
-subsets plays the role of the test set while the union of the
-remaining subsets constitutes the training set. Such a splitting
-warrants a balanced representation of each sample in both training and
-test set over the splits. Still the division into the \( k \) subsets
-involves a degree of randomness. This may be fully excluded when
-choosing \( k=n \). This particular case is referred to as leave-one-out
-cross-validation (LOOCV).
+
+
"""
+============================
+Underfitting vs. Overfitting
+============================
+
+This example demonstrates the problems of underfitting and overfitting and
+how we can use linear regression with polynomial features to approximate
+nonlinear functions. The plot shows the function that we want to approximate,
+which is a part of the cosine function. In addition, the samples from the
+real function and the approximations of different models are displayed. The
+models have polynomial features of different degrees. We can see that a
+linear function (polynomial with degree 1) is not sufficient to fit the
+training samples. This is called **underfitting**. A polynomial of degree 4
+approximates the true function almost perfectly. However, for higher degrees
+the model will **overfit** the training data, i.e. it learns the noise of the
+training data.
+We evaluate quantitatively **overfitting** / **underfitting** by using
+cross-validation. We calculate the mean squared error (MSE) on the validation
+set, the higher, the less likely the model generalizes correctly from the
+training data.
+"""
+
+print(__doc__)
+
+import numpy as np
+import matplotlib.pyplot as plt
+from sklearn.pipeline import Pipeline
+from sklearn.preprocessing import PolynomialFeatures
+from sklearn.linear_model import LinearRegression
+from sklearn.model_selection import cross_val_score
+
+
+def true_fun(X):
+ return np.cos(1.5 * np.pi * X)
+
+np.random.seed(0)
+
+n_samples = 30
+degrees = [1, 4, 15]
+
+X = np.sort(np.random.rand(n_samples))
+y = true_fun(X) + np.random.randn(n_samples) * 0.1
+
+plt.figure(figsize=(14, 5))
+for i in range(len(degrees)):
+ ax = plt.subplot(1, len(degrees), i + 1)
+ plt.setp(ax, xticks=(), yticks=())
+
+ polynomial_features = PolynomialFeatures(degree=degrees[i],
+ include_bias=False)
+ linear_regression = LinearRegression()
+ pipeline = Pipeline([("polynomial_features", polynomial_features),
+ ("linear_regression", linear_regression)])
+ pipeline.fit(X[:, np.newaxis], y)
+
+ # Evaluate the models using crossvalidation
+ scores = cross_val_score(pipeline, X[:, np.newaxis], y,
+ scoring="neg_mean_squared_error", cv=10)
+
+ X_test = np.linspace(0, 1, 100)
+ plt.plot(X_test, pipeline.predict(X_test[:, np.newaxis]), label="Model")
+ plt.plot(X_test, true_fun(X_test), label="True function")
+ plt.scatter(X, y, edgecolor='b', s=20, label="Samples")
+ plt.xlabel("x")
+ plt.ylabel("y")
+ plt.xlim((0, 1))
+ plt.ylim((-2, 2))
+ plt.legend(loc="best")
+ plt.title("Degree {}\nMSE = {:.2e}(+/- {:.2e})".format(
+ degrees[i], -scores.mean(), scores.std()))
+plt.show()
+
@@ -291,6 +351,7 @@ cross-validation (LOOCV).
38
39
40
+ 41
»
diff --git a/doc/pub/week37/html/._week37-bs034.html b/doc/pub/week37/html/._week37-bs034.html
index a48a1356c..3197dd223 100644
--- a/doc/pub/week37/html/._week37-bs034.html
+++ b/doc/pub/week37/html/._week37-bs034.html
@@ -69,6 +69,7 @@ Automatically generated HTML file from DocOnce source
2,
None,
'test-function-for-what-happens-with-ols-ridge-and-lasso'),
+ ('Rerunning the above code', 2, None, 'rerunning-the-above-code'),
("Invoking Bayes' theorem", 2, None, 'invoking-bayes-theorem'),
('Ridge and Bayes', 2, None, 'ridge-and-bayes'),
('Lasso and Bayes', 2, None, 'lasso-and-bayes'),
@@ -206,36 +207,37 @@ MathJax.Hub.Config({
Bayes' Theorem
Interpretations of Bayes' Theorem
Test Function for what happens with OLS, Ridge and Lasso
- Invoking Bayes' theorem
- Ridge and Bayes
- Lasso and Bayes
- Why resampling methods
- Resampling methods
- Resampling approaches can be computationally expensive
- Why resampling methods ?
- Statistical analysis
- Resampling methods
- Resampling methods: Jackknife and Bootstrap
- Resampling methods: Jackknife
- Jackknife code example
- Resampling methods: Bootstrap
- Resampling methods: Bootstrap background
- Resampling methods: More Bootstrap background
- Resampling methods: Bootstrap approach
- Resampling methods: Bootstrap steps
- Code example for the Bootstrap method
- The bias-variance tradeoff
- Example code for Bias-Variance tradeoff
- Understanding what happens
- Summing up
- Another Example from Scikit-Learn's Repository
- Various steps in cross-validation
- How to set up the cross-validation for Ridge and/or Lasso
- Cross-validation in brief
- Code Example for Cross-validation and \( k \)-fold Cross-validation
- More examples on bootstrap and cross-validation and errors
- The same example but now with cross-validation
- Cross-validation with Ridge
+ Rerunning the above code
+ Invoking Bayes' theorem
+ Ridge and Bayes
+ Lasso and Bayes
+ Why resampling methods
+ Resampling methods
+ Resampling approaches can be computationally expensive
+ Why resampling methods ?
+ Statistical analysis
+ Resampling methods
+ Resampling methods: Jackknife and Bootstrap
+ Resampling methods: Jackknife
+ Jackknife code example
+ Resampling methods: Bootstrap
+ Resampling methods: Bootstrap background
+ Resampling methods: More Bootstrap background
+ Resampling methods: Bootstrap approach
+ Resampling methods: Bootstrap steps
+ Code example for the Bootstrap method
+ The bias-variance tradeoff
+ Example code for Bias-Variance tradeoff
+ Understanding what happens
+ Summing up
+ Another Example from Scikit-Learn's Repository
+ Various steps in cross-validation
+ How to set up the cross-validation for Ridge and/or Lasso
+ Cross-validation in brief
+ Code Example for Cross-validation and \( k \)-fold Cross-validation
+ More examples on bootstrap and cross-validation and errors
+ The same example but now with cross-validation
+ Cross-validation with Ridge
@@ -251,34 +253,23 @@ MathJax.Hub.Config({
-How to set up the cross-validation for Ridge and/or Lasso
+Various steps in cross-validation
-
-- Define a range of interest for the penalty parameter.
-- Divide the data set into training and test set comprising samples \( \{1, \ldots, n\} \setminus i \) and \( \{ i \} \), respectively.
-- Fit the linear regression model by means of ridge estimation for each \( \lambda \) in the grid using the training set, and the corresponding estimate of the error variance \( \boldsymbol{\sigma}_{-i}^2(\lambda) \), as
-
-
-$$
-\begin{align*}
-\boldsymbol{\beta}_{-i}(\lambda) & = ( \boldsymbol{X}_{-i, \ast}^{T}
-\boldsymbol{X}_{-i, \ast} + \lambda \boldsymbol{I}_{pp})^{-1}
-\boldsymbol{X}_{-i, \ast}^{T} \boldsymbol{y}_{-i}
-\end{align*}
-$$
-
-
-
-- Evaluate the prediction performance of these models on the test set by \( \log\{L[y_i, \boldsymbol{X}_{i, \ast}; \boldsymbol{\beta}_{-i}(\lambda), \boldsymbol{\sigma}_{-i}^2(\lambda)]\} \). Or, by the prediction error \( |y_i - \boldsymbol{X}_{i, \ast} \boldsymbol{\beta}_{-i}(\lambda)| \), the relative error, the error squared or the R2 score function.
-- Repeat the first three steps such that each sample plays the role of the test set once.
-- Average the prediction performances of the test sets at each grid point of the penalty bias/parameter. It is an estimate of the prediction performance of the model corresponding to this value of the penalty parameter on novel data. It is defined as
-
-
-$$
-\begin{align*}
-\frac{1}{n} \sum_{i = 1}^n \log\{L[y_i, \mathbf{X}_{i, \ast}; \boldsymbol{\beta}_{-i}(\lambda), \boldsymbol{\sigma}_{-i}^2(\lambda)]\}.
-\end{align*}
-$$
+
+When the repetitive splitting of the data set is done randomly,
+samples may accidently end up in a fast majority of the splits in
+either training or test set. Such samples may have an unbalanced
+influence on either model building or prediction evaluation. To avoid
+this \( k \)-fold cross-validation structures the data splitting. The
+samples are divided into \( k \) more or less equally sized exhaustive and
+mutually exclusive subsets. In turn (at each split) one of these
+subsets plays the role of the test set while the union of the
+remaining subsets constitutes the training set. Such a splitting
+warrants a balanced representation of each sample in both training and
+test set over the splits. Still the division into the \( k \) subsets
+involves a degree of randomness. This may be fully excluded when
+choosing \( k=n \). This particular case is referred to as leave-one-out
+cross-validation (LOOCV).
@@ -301,6 +292,7 @@ $$
38
39
40
+ 41
»
diff --git a/doc/pub/week37/html/._week37-bs035.html b/doc/pub/week37/html/._week37-bs035.html
index 25a0efbb7..c2319bfa8 100644
--- a/doc/pub/week37/html/._week37-bs035.html
+++ b/doc/pub/week37/html/._week37-bs035.html
@@ -69,6 +69,7 @@ Automatically generated HTML file from DocOnce source
2,
None,
'test-function-for-what-happens-with-ols-ridge-and-lasso'),
+ ('Rerunning the above code', 2, None, 'rerunning-the-above-code'),
("Invoking Bayes' theorem", 2, None, 'invoking-bayes-theorem'),
('Ridge and Bayes', 2, None, 'ridge-and-bayes'),
('Lasso and Bayes', 2, None, 'lasso-and-bayes'),
@@ -206,36 +207,37 @@ MathJax.Hub.Config({
Bayes' Theorem
Interpretations of Bayes' Theorem
Test Function for what happens with OLS, Ridge and Lasso
- Invoking Bayes' theorem
- Ridge and Bayes
- Lasso and Bayes
- Why resampling methods
- Resampling methods
- Resampling approaches can be computationally expensive
- Why resampling methods ?
- Statistical analysis
- Resampling methods
- Resampling methods: Jackknife and Bootstrap
- Resampling methods: Jackknife
- Jackknife code example
- Resampling methods: Bootstrap
- Resampling methods: Bootstrap background
- Resampling methods: More Bootstrap background
- Resampling methods: Bootstrap approach
- Resampling methods: Bootstrap steps
- Code example for the Bootstrap method
- The bias-variance tradeoff
- Example code for Bias-Variance tradeoff
- Understanding what happens
- Summing up
- Another Example from Scikit-Learn's Repository
- Various steps in cross-validation
- How to set up the cross-validation for Ridge and/or Lasso
- Cross-validation in brief
- Code Example for Cross-validation and \( k \)-fold Cross-validation
- More examples on bootstrap and cross-validation and errors
- The same example but now with cross-validation
- Cross-validation with Ridge
+ Rerunning the above code
+ Invoking Bayes' theorem
+ Ridge and Bayes
+ Lasso and Bayes
+ Why resampling methods
+ Resampling methods
+ Resampling approaches can be computationally expensive
+ Why resampling methods ?
+ Statistical analysis
+ Resampling methods
+ Resampling methods: Jackknife and Bootstrap
+ Resampling methods: Jackknife
+ Jackknife code example
+ Resampling methods: Bootstrap
+ Resampling methods: Bootstrap background
+ Resampling methods: More Bootstrap background
+ Resampling methods: Bootstrap approach
+ Resampling methods: Bootstrap steps
+ Code example for the Bootstrap method
+ The bias-variance tradeoff
+ Example code for Bias-Variance tradeoff
+ Understanding what happens
+ Summing up
+ Another Example from Scikit-Learn's Repository
+ Various steps in cross-validation
+ How to set up the cross-validation for Ridge and/or Lasso
+ Cross-validation in brief
+ Code Example for Cross-validation and \( k \)-fold Cross-validation
+ More examples on bootstrap and cross-validation and errors
+ The same example but now with cross-validation
+ Cross-validation with Ridge
@@ -249,28 +251,38 @@ MathJax.Hub.Config({
-
+
-Cross-validation in brief
+How to set up the cross-validation for Ridge and/or Lasso
+
+
+- Define a range of interest for the penalty parameter.
+- Divide the data set into training and test set comprising samples \( \{1, \ldots, n\} \setminus i \) and \( \{ i \} \), respectively.
+- Fit the linear regression model by means of ridge estimation for each \( \lambda \) in the grid using the training set, and the corresponding estimate of the error variance \( \boldsymbol{\sigma}_{-i}^2(\lambda) \), as
+
+
+$$
+\begin{align*}
+\boldsymbol{\beta}_{-i}(\lambda) & = ( \boldsymbol{X}_{-i, \ast}^{T}
+\boldsymbol{X}_{-i, \ast} + \lambda \boldsymbol{I}_{pp})^{-1}
+\boldsymbol{X}_{-i, \ast}^{T} \boldsymbol{y}_{-i}
+\end{align*}
+$$
+
+
+
+- Evaluate the prediction performance of these models on the test set by \( \log\{L[y_i, \boldsymbol{X}_{i, \ast}; \boldsymbol{\beta}_{-i}(\lambda), \boldsymbol{\sigma}_{-i}^2(\lambda)]\} \). Or, by the prediction error \( |y_i - \boldsymbol{X}_{i, \ast} \boldsymbol{\beta}_{-i}(\lambda)| \), the relative error, the error squared or the R2 score function.
+- Repeat the first three steps such that each sample plays the role of the test set once.
+- Average the prediction performances of the test sets at each grid point of the penalty bias/parameter. It is an estimate of the prediction performance of the model corresponding to this value of the penalty parameter on novel data. It is defined as
+
+
+$$
+\begin{align*}
+\frac{1}{n} \sum_{i = 1}^n \log\{L[y_i, \mathbf{X}_{i, \ast}; \boldsymbol{\beta}_{-i}(\lambda), \boldsymbol{\sigma}_{-i}^2(\lambda)]\}.
+\end{align*}
+$$
-For the various values of \( k \)
-
-
-- shuffle the dataset randomly.
-- Split the dataset into \( k \) groups.
-- For each unique group:
-
-
-- Decide which group to use as set for test data
-- Take the remaining groups as a training data set
-- Fit a model on the training set and evaluate it on the test set
-- Retain the evaluation score and discard the model
-
-
- Summarize the model using the sample of model evaluation scores
-
-
diff --git a/doc/pub/week37/html/._week37-bs036.html b/doc/pub/week37/html/._week37-bs036.html
index 6f3b8e3b7..8c99bfee7 100644
--- a/doc/pub/week37/html/._week37-bs036.html
+++ b/doc/pub/week37/html/._week37-bs036.html
@@ -69,6 +69,7 @@ Automatically generated HTML file from DocOnce source
2,
None,
'test-function-for-what-happens-with-ols-ridge-and-lasso'),
+ ('Rerunning the above code', 2, None, 'rerunning-the-above-code'),
("Invoking Bayes' theorem", 2, None, 'invoking-bayes-theorem'),
('Ridge and Bayes', 2, None, 'ridge-and-bayes'),
('Lasso and Bayes', 2, None, 'lasso-and-bayes'),
@@ -206,36 +207,37 @@ MathJax.Hub.Config({
Bayes' Theorem
Interpretations of Bayes' Theorem
Test Function for what happens with OLS, Ridge and Lasso
- Invoking Bayes' theorem
- Ridge and Bayes
- Lasso and Bayes
- Why resampling methods
- Resampling methods
- Resampling approaches can be computationally expensive
- Why resampling methods ?
- Statistical analysis
- Resampling methods
- Resampling methods: Jackknife and Bootstrap
- Resampling methods: Jackknife
- Jackknife code example
- Resampling methods: Bootstrap
- Resampling methods: Bootstrap background
- Resampling methods: More Bootstrap background
- Resampling methods: Bootstrap approach
- Resampling methods: Bootstrap steps
- Code example for the Bootstrap method
- The bias-variance tradeoff
- Example code for Bias-Variance tradeoff
- Understanding what happens
- Summing up
- Another Example from Scikit-Learn's Repository
- Various steps in cross-validation
- How to set up the cross-validation for Ridge and/or Lasso
- Cross-validation in brief
- Code Example for Cross-validation and \( k \)-fold Cross-validation
- More examples on bootstrap and cross-validation and errors
- The same example but now with cross-validation
- Cross-validation with Ridge
+ Rerunning the above code
+ Invoking Bayes' theorem
+ Ridge and Bayes
+ Lasso and Bayes
+ Why resampling methods
+ Resampling methods
+ Resampling approaches can be computationally expensive
+ Why resampling methods ?
+ Statistical analysis
+ Resampling methods
+ Resampling methods: Jackknife and Bootstrap
+ Resampling methods: Jackknife
+ Jackknife code example
+ Resampling methods: Bootstrap
+ Resampling methods: Bootstrap background
+ Resampling methods: More Bootstrap background
+ Resampling methods: Bootstrap approach
+ Resampling methods: Bootstrap steps
+ Code example for the Bootstrap method
+ The bias-variance tradeoff
+ Example code for Bias-Variance tradeoff
+ Understanding what happens
+ Summing up
+ Another Example from Scikit-Learn's Repository
+ Various steps in cross-validation
+ How to set up the cross-validation for Ridge and/or Lasso
+ Cross-validation in brief
+ Code Example for Cross-validation and \( k \)-fold Cross-validation
+ More examples on bootstrap and cross-validation and errors
+ The same example but now with cross-validation
+ Cross-validation with Ridge
@@ -251,104 +253,26 @@ MathJax.Hub.Config({
-Code Example for Cross-validation and \( k \)-fold Cross-validation
+Cross-validation in brief
-The code here uses Ridge regression with cross-validation (CV) resampling and \( k \)-fold CV in order to fit a specific polynomial.
-
+For the various values of \( k \)
-
-
import numpy as np
-import matplotlib.pyplot as plt
-from sklearn.model_selection import KFold
-from sklearn.linear_model import Ridge
-from sklearn.model_selection import cross_val_score
-from sklearn.preprocessing import PolynomialFeatures
+
+- shuffle the dataset randomly.
+- Split the dataset into \( k \) groups.
+- For each unique group:
-# A seed just to ensure that the random numbers are the same for every run.
-# Useful for eventual debugging.
-np.random.seed(3155)
+
+- Decide which group to use as set for test data
+- Take the remaining groups as a training data set
+- Fit a model on the training set and evaluate it on the test set
+- Retain the evaluation score and discard the model
+
-# Generate the data.
-nsamples = 100
-x = np.random.randn(nsamples)
-y = 3*x**2 + np.random.randn(nsamples)
+
Summarize the model using the sample of model evaluation scores
+
-
## Cross-validation on Ridge regression using KFold only
-
-
# Decide degree on polynomial to fit
-poly
= PolynomialFeatures(degree
= 6)
-
-
# Decide which values of lambda to use
-nlambdas
= 500
-lambdas
= np
.logspace(
-3,
5, nlambdas)
-
-
# Initialize a KFold instance
-k
= 5
-kfold
= KFold(n_splits
= k)
-
-
# Perform the cross-validation to estimate MSE
-scores_KFold
= np
.zeros((nlambdas, k))
-
-i
= 0
-
for lmb
in lambdas:
- ridge
= Ridge(alpha
= lmb)
- j
= 0
-
for train_inds, test_inds
in kfold
.split(x):
- xtrain
= x[train_inds]
- ytrain
= y[train_inds]
-
- xtest
= x[test_inds]
- ytest
= y[test_inds]
-
- Xtrain
= poly
.fit_transform(xtrain[:, np
.newaxis])
- ridge
.fit(Xtrain, ytrain[:, np
.newaxis])
-
- Xtest
= poly
.fit_transform(xtest[:, np
.newaxis])
- ypred
= ridge
.predict(Xtest)
-
- scores_KFold[i,j]
= np
.sum((ypred
- ytest[:, np
.newaxis])
**2)
/np
.size(ypred)
-
- j
+= 1
- i
+= 1
-
-
-estimated_mse_KFold
= np
.mean(scores_KFold, axis
= 1)
-
-
## Cross-validation using cross_val_score from sklearn along with KFold
-
-
# kfold is an instance initialized above as:
-
# kfold = KFold(n_splits = k)
-
-estimated_mse_sklearn
= np
.zeros(nlambdas)
-i
= 0
-
for lmb
in lambdas:
- ridge
= Ridge(alpha
= lmb)
-
- X
= poly
.fit_transform(x[:, np
.newaxis])
- estimated_mse_folds
= cross_val_score(ridge, X, y[:, np
.newaxis], scoring
='neg_mean_squared_error', cv
=kfold)
-
-
# cross_val_score return an array containing the estimated negative mse for every fold.
-
# we have to the the mean of every array in order to get an estimate of the mse of the model
- estimated_mse_sklearn[i]
= np
.mean(
-estimated_mse_folds)
-
- i
+= 1
-
-
## Plot and compare the slightly different ways to perform cross-validation
-
-plt
.figure()
-
-plt
.plot(np
.log10(lambdas), estimated_mse_sklearn, label
= 'cross_val_score')
-plt
.plot(np
.log10(lambdas), estimated_mse_KFold,
'r--', label
= 'KFold')
-
-plt
.xlabel(
'log10(lambda)')
-plt
.ylabel(
'mse')
-
-plt
.legend()
-
-plt
.show()
-
-
diff --git a/doc/pub/week37/html/._week37-bs037.html b/doc/pub/week37/html/._week37-bs037.html
index 7e0533725..6e8fb56b3 100644
--- a/doc/pub/week37/html/._week37-bs037.html
+++ b/doc/pub/week37/html/._week37-bs037.html
@@ -69,6 +69,7 @@ Automatically generated HTML file from DocOnce source
2,
None,
'test-function-for-what-happens-with-ols-ridge-and-lasso'),
+ ('Rerunning the above code', 2, None, 'rerunning-the-above-code'),
("Invoking Bayes' theorem", 2, None, 'invoking-bayes-theorem'),
('Ridge and Bayes', 2, None, 'ridge-and-bayes'),
('Lasso and Bayes', 2, None, 'lasso-and-bayes'),
@@ -206,36 +207,37 @@ MathJax.Hub.Config({
Bayes' Theorem
Interpretations of Bayes' Theorem
Test Function for what happens with OLS, Ridge and Lasso
- Invoking Bayes' theorem
- Ridge and Bayes
- Lasso and Bayes
- Why resampling methods
- Resampling methods
- Resampling approaches can be computationally expensive
- Why resampling methods ?
- Statistical analysis
- Resampling methods
- Resampling methods: Jackknife and Bootstrap
- Resampling methods: Jackknife
- Jackknife code example
- Resampling methods: Bootstrap
- Resampling methods: Bootstrap background
- Resampling methods: More Bootstrap background
- Resampling methods: Bootstrap approach
- Resampling methods: Bootstrap steps
- Code example for the Bootstrap method
- The bias-variance tradeoff
- Example code for Bias-Variance tradeoff
- Understanding what happens
- Summing up
- Another Example from Scikit-Learn's Repository
- Various steps in cross-validation
- How to set up the cross-validation for Ridge and/or Lasso
- Cross-validation in brief
- Code Example for Cross-validation and \( k \)-fold Cross-validation
- More examples on bootstrap and cross-validation and errors
- The same example but now with cross-validation
- Cross-validation with Ridge
+ Rerunning the above code
+ Invoking Bayes' theorem
+ Ridge and Bayes
+ Lasso and Bayes
+ Why resampling methods
+ Resampling methods
+ Resampling approaches can be computationally expensive
+ Why resampling methods ?
+ Statistical analysis
+ Resampling methods
+ Resampling methods: Jackknife and Bootstrap
+ Resampling methods: Jackknife
+ Jackknife code example
+ Resampling methods: Bootstrap
+ Resampling methods: Bootstrap background
+ Resampling methods: More Bootstrap background
+ Resampling methods: Bootstrap approach
+ Resampling methods: Bootstrap steps
+ Code example for the Bootstrap method
+ The bias-variance tradeoff
+ Example code for Bias-Variance tradeoff
+ Understanding what happens
+ Summing up
+ Another Example from Scikit-Learn's Repository
+ Various steps in cross-validation
+ How to set up the cross-validation for Ridge and/or Lasso
+ Cross-validation in brief
+ Code Example for Cross-validation and \( k \)-fold Cross-validation
+ More examples on bootstrap and cross-validation and errors
+ The same example but now with cross-validation
+ Cross-validation with Ridge
@@ -251,88 +253,101 @@ MathJax.Hub.Config({
-More examples on bootstrap and cross-validation and errors
+Code Example for Cross-validation and \( k \)-fold Cross-validation
+
+The code here uses Ridge regression with cross-validation (CV) resampling and \( k \)-fold CV in order to fit a specific polynomial.
-
# Common imports
-import os
-import numpy as np
-import pandas as pd
+import numpy as np
import matplotlib.pyplot as plt
-from sklearn.linear_model import LinearRegression, Ridge, Lasso
-from sklearn.model_selection import train_test_split
-from sklearn.utils import resample
-from sklearn.metrics import mean_squared_error
-# Where to save the figures and data files
-PROJECT_ROOT_DIR = "Results"
-FIGURE_ID = "Results/FigureFiles"
-DATA_ID = "DataFiles/"
+from sklearn.model_selection import KFold
+from sklearn.linear_model import Ridge
+from sklearn.model_selection import cross_val_score
+from sklearn.preprocessing import PolynomialFeatures
-if not os.path.exists(PROJECT_ROOT_DIR):
- os.mkdir(PROJECT_ROOT_DIR)
+# A seed just to ensure that the random numbers are the same for every run.
+# Useful for eventual debugging.
+np.random.seed(3155)
-if not os.path.exists(FIGURE_ID):
- os.makedirs(FIGURE_ID)
+# Generate the data.
+nsamples = 100
+x = np.random.randn(nsamples)
+y = 3*x**2 + np.random.randn(nsamples)
-if not os.path.exists(DATA_ID):
- os.makedirs(DATA_ID)
+## Cross-validation on Ridge regression using KFold only
-def image_path(fig_id):
- return os.path.join(FIGURE_ID, fig_id)
+# Decide degree on polynomial to fit
+poly = PolynomialFeatures(degree = 6)
-def data_path(dat_id):
- return os.path.join(DATA_ID, dat_id)
+# Decide which values of lambda to use
+nlambdas = 500
+lambdas = np.logspace(-3, 5, nlambdas)
-def save_fig(fig_id):
- plt.savefig(image_path(fig_id) + ".png", format='png')
+# Initialize a KFold instance
+k = 5
+kfold = KFold(n_splits = k)
-infile = open(data_path("EoS.csv"),'r')
+# Perform the cross-validation to estimate MSE
+scores_KFold = np.zeros((nlambdas, k))
-# Read the EoS data as csv file and organize the data into two arrays with density and energies
-EoS = pd.read_csv(infile, names=('Density', 'Energy'))
-EoS['Energy'] = pd.to_numeric(EoS['Energy'], errors='coerce')
-EoS = EoS.dropna()
-Energies = EoS['Energy']
-Density = EoS['Density']
-# The design matrix now as function of various polytrops
+i = 0
+for lmb in lambdas:
+ ridge = Ridge(alpha = lmb)
+ j = 0
+ for train_inds, test_inds in kfold.split(x):
+ xtrain = x[train_inds]
+ ytrain = y[train_inds]
-Maxpolydegree = 30
-X = np.zeros((len(Density),Maxpolydegree))
-X[:,0] = 1.0
-testerror = np.zeros(Maxpolydegree)
-trainingerror = np.zeros(Maxpolydegree)
-polynomial = np.zeros(Maxpolydegree)
+ xtest = x[test_inds]
+ ytest = y[test_inds]
-trials = 100
-for polydegree in range(1, Maxpolydegree):
- polynomial[polydegree] = polydegree
- for degree in range(polydegree):
- X[:,degree] = Density**(degree/3.0)
+ Xtrain = poly.fit_transform(xtrain[:, np.newaxis])
+ ridge.fit(Xtrain, ytrain[:, np.newaxis])
-# loop over trials in order to estimate the expectation value of the MSE
- testerror[polydegree] = 0.0
- trainingerror[polydegree] = 0.0
- for samples in range(trials):
- x_train, x_test, y_train, y_test = train_test_split(X, Energies, test_size=0.2)
- model = LinearRegression(fit_intercept=True).fit(x_train, y_train)
- ypred = model.predict(x_train)
- ytilde = model.predict(x_test)
- testerror[polydegree] += mean_squared_error(y_test, ytilde)
- trainingerror[polydegree] += mean_squared_error(y_train, ypred)
+ Xtest = poly.fit_transform(xtest[:, np.newaxis])
+ ypred = ridge.predict(Xtest)
- testerror[polydegree] /= trials
- trainingerror[polydegree] /= trials
- print("Degree of polynomial: %3d"% polynomial[polydegree])
- print("Mean squared error on training data: %.8f" % trainingerror[polydegree])
- print("Mean squared error on test data: %.8f" % testerror[polydegree])
+ scores_KFold[i,j] = np.sum((ypred - ytest[:, np.newaxis])**2)/np.size(ypred)
+
+ j += 1
+ i += 1
+
+
+estimated_mse_KFold = np.mean(scores_KFold, axis = 1)
+
+## Cross-validation using cross_val_score from sklearn along with KFold
+
+# kfold is an instance initialized above as:
+# kfold = KFold(n_splits = k)
+
+estimated_mse_sklearn = np.zeros(nlambdas)
+i = 0
+for lmb in lambdas:
+ ridge = Ridge(alpha = lmb)
+
+ X = poly.fit_transform(x[:, np.newaxis])
+ estimated_mse_folds = cross_val_score(ridge, X, y[:, np.newaxis], scoring='neg_mean_squared_error', cv=kfold)
+
+ # cross_val_score return an array containing the estimated negative mse for every fold.
+ # we have to the the mean of every array in order to get an estimate of the mse of the model
+ estimated_mse_sklearn[i] = np.mean(-estimated_mse_folds)
+
+ i += 1
+
+## Plot and compare the slightly different ways to perform cross-validation
+
+plt.figure()
+
+plt.plot(np.log10(lambdas), estimated_mse_sklearn, label = 'cross_val_score')
+plt.plot(np.log10(lambdas), estimated_mse_KFold, 'r--', label = 'KFold')
+
+plt.xlabel('log10(lambda)')
+plt.ylabel('mse')
-plt.plot(polynomial, np.log10(trainingerror), label='Training Error')
-plt.plot(polynomial, np.log10(testerror), label='Test Error')
-plt.xlabel('Polynomial degree')
-plt.ylabel('log10[MSE]')
plt.legend()
+
plt.show()
@@ -353,6 +368,7 @@ plt.show()
38
39
40
+
41
»
diff --git a/doc/pub/week37/html/._week37-bs038.html b/doc/pub/week37/html/._week37-bs038.html
index eaa2a732b..d27c4bdb2 100644
--- a/doc/pub/week37/html/._week37-bs038.html
+++ b/doc/pub/week37/html/._week37-bs038.html
@@ -69,6 +69,7 @@ Automatically generated HTML file from DocOnce source
2,
None,
'test-function-for-what-happens-with-ols-ridge-and-lasso'),
+ ('Rerunning the above code', 2, None, 'rerunning-the-above-code'),
("Invoking Bayes' theorem", 2, None, 'invoking-bayes-theorem'),
('Ridge and Bayes', 2, None, 'ridge-and-bayes'),
('Lasso and Bayes', 2, None, 'lasso-and-bayes'),
@@ -206,36 +207,37 @@ MathJax.Hub.Config({
Bayes' Theorem
Interpretations of Bayes' Theorem
Test Function for what happens with OLS, Ridge and Lasso
-
Invoking Bayes' theorem
-
Ridge and Bayes
-
Lasso and Bayes
-
Why resampling methods
-
Resampling methods
-
Resampling approaches can be computationally expensive
-
Why resampling methods ?
-
Statistical analysis
-
Resampling methods
-
Resampling methods: Jackknife and Bootstrap
-
Resampling methods: Jackknife
-
Jackknife code example
-
Resampling methods: Bootstrap
-
Resampling methods: Bootstrap background
-
Resampling methods: More Bootstrap background
-
Resampling methods: Bootstrap approach
-
Resampling methods: Bootstrap steps
-
Code example for the Bootstrap method
-
The bias-variance tradeoff
-
Example code for Bias-Variance tradeoff
-
Understanding what happens
-
Summing up
-
Another Example from Scikit-Learn's Repository
-
Various steps in cross-validation
-
How to set up the cross-validation for Ridge and/or Lasso
-
Cross-validation in brief
-
Code Example for Cross-validation and \( k \)-fold Cross-validation
-
More examples on bootstrap and cross-validation and errors
-
The same example but now with cross-validation
-
Cross-validation with Ridge
+
Rerunning the above code
+
Invoking Bayes' theorem
+
Ridge and Bayes
+
Lasso and Bayes
+
Why resampling methods
+
Resampling methods
+
Resampling approaches can be computationally expensive
+
Why resampling methods ?
+
Statistical analysis
+
Resampling methods
+
Resampling methods: Jackknife and Bootstrap
+
Resampling methods: Jackknife
+
Jackknife code example
+
Resampling methods: Bootstrap
+
Resampling methods: Bootstrap background
+
Resampling methods: More Bootstrap background
+
Resampling methods: Bootstrap approach
+
Resampling methods: Bootstrap steps
+
Code example for the Bootstrap method
+
The bias-variance tradeoff
+
Example code for Bias-Variance tradeoff
+
Understanding what happens
+
Summing up
+
Another Example from Scikit-Learn's Repository
+
Various steps in cross-validation
+
How to set up the cross-validation for Ridge and/or Lasso
+
Cross-validation in brief
+
Code Example for Cross-validation and \( k \)-fold Cross-validation
+
More examples on bootstrap and cross-validation and errors
+
The same example but now with cross-validation
+
Cross-validation with Ridge
@@ -249,9 +251,9 @@ MathJax.Hub.Config({
-
+
-
The same example but now with cross-validation
+
More examples on bootstrap and cross-validation and errors
@@ -262,11 +264,9 @@ MathJax.Hub.Config({
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression, Ridge, Lasso
+from sklearn.model_selection import train_test_split
+from sklearn.utils import resample
from sklearn.metrics import mean_squared_error
-from sklearn.model_selection import KFold
-from sklearn.model_selection import cross_val_score
-
-
# Where to save the figures and data files
PROJECT_ROOT_DIR = "Results"
FIGURE_ID = "Results/FigureFiles"
@@ -303,22 +303,35 @@ Density = EoS[&
Maxpolydegree = 30
X = np.zeros((len(Density),Maxpolydegree))
X[:,0] = 1.0
-estimated_mse_sklearn = np.zeros(Maxpolydegree)
+testerror = np.zeros(Maxpolydegree)
+trainingerror = np.zeros(Maxpolydegree)
polynomial = np.zeros(Maxpolydegree)
-k =5
-kfold = KFold(n_splits = k)
+trials = 100
for polydegree in range(1, Maxpolydegree):
polynomial[polydegree] = polydegree
for degree in range(polydegree):
X[:,degree] = Density**(degree/3.0)
- OLS = LinearRegression()
-# loop over trials in order to estimate the expectation value of the MSE
- estimated_mse_folds = cross_val_score(OLS, X, Energies, scoring='neg_mean_squared_error', cv=kfold)
-#[:, np.newaxis]
- estimated_mse_sklearn[polydegree] = np.mean(-estimated_mse_folds)
-plt.plot(polynomial, np.log10(estimated_mse_sklearn), label='Test Error')
+# loop over trials in order to estimate the expectation value of the MSE
+ testerror[polydegree] = 0.0
+ trainingerror[polydegree] = 0.0
+ for samples in range(trials):
+ x_train, x_test, y_train, y_test = train_test_split(X, Energies, test_size=0.2)
+ model = LinearRegression(fit_intercept=True).fit(x_train, y_train)
+ ypred = model.predict(x_train)
+ ytilde = model.predict(x_test)
+ testerror[polydegree] += mean_squared_error(y_test, ytilde)
+ trainingerror[polydegree] += mean_squared_error(y_train, ypred)
+
+ testerror[polydegree] /= trials
+ trainingerror[polydegree] /= trials
+ print("Degree of polynomial: %3d"% polynomial[polydegree])
+ print("Mean squared error on training data: %.8f" % trainingerror[polydegree])
+ print("Mean squared error on test data: %.8f" % testerror[polydegree])
+
+plt.plot(polynomial, np.log10(trainingerror), label='Training Error')
+plt.plot(polynomial, np.log10(testerror), label='Test Error')
plt.xlabel('Polynomial degree')
plt.ylabel('log10[MSE]')
plt.legend()
@@ -341,6 +354,7 @@ plt.show()
38
39
40
+ 41
»
diff --git a/doc/pub/week37/html/._week37-bs039.html b/doc/pub/week37/html/._week37-bs039.html
index 901692e8b..3b8e683ba 100644
--- a/doc/pub/week37/html/._week37-bs039.html
+++ b/doc/pub/week37/html/._week37-bs039.html
@@ -69,6 +69,7 @@ Automatically generated HTML file from DocOnce source
2,
None,
'test-function-for-what-happens-with-ols-ridge-and-lasso'),
+ ('Rerunning the above code', 2, None, 'rerunning-the-above-code'),
("Invoking Bayes' theorem", 2, None, 'invoking-bayes-theorem'),
('Ridge and Bayes', 2, None, 'ridge-and-bayes'),
('Lasso and Bayes', 2, None, 'lasso-and-bayes'),
@@ -206,36 +207,37 @@ MathJax.Hub.Config({
Bayes' Theorem
Interpretations of Bayes' Theorem
Test Function for what happens with OLS, Ridge and Lasso
- Invoking Bayes' theorem
- Ridge and Bayes
- Lasso and Bayes
- Why resampling methods
- Resampling methods
- Resampling approaches can be computationally expensive
- Why resampling methods ?
- Statistical analysis
- Resampling methods
- Resampling methods: Jackknife and Bootstrap
- Resampling methods: Jackknife
- Jackknife code example
- Resampling methods: Bootstrap
- Resampling methods: Bootstrap background
- Resampling methods: More Bootstrap background
- Resampling methods: Bootstrap approach
- Resampling methods: Bootstrap steps
- Code example for the Bootstrap method
- The bias-variance tradeoff
- Example code for Bias-Variance tradeoff
- Understanding what happens
- Summing up
- Another Example from Scikit-Learn's Repository
- Various steps in cross-validation
- How to set up the cross-validation for Ridge and/or Lasso
- Cross-validation in brief
- Code Example for Cross-validation and \( k \)-fold Cross-validation
- More examples on bootstrap and cross-validation and errors
- The same example but now with cross-validation
- Cross-validation with Ridge
+ Rerunning the above code
+ Invoking Bayes' theorem
+ Ridge and Bayes
+ Lasso and Bayes
+ Why resampling methods
+ Resampling methods
+ Resampling approaches can be computationally expensive
+ Why resampling methods ?
+ Statistical analysis
+ Resampling methods
+ Resampling methods: Jackknife and Bootstrap
+ Resampling methods: Jackknife
+ Jackknife code example
+ Resampling methods: Bootstrap
+ Resampling methods: Bootstrap background
+ Resampling methods: More Bootstrap background
+ Resampling methods: Bootstrap approach
+ Resampling methods: Bootstrap steps
+ Code example for the Bootstrap method
+ The bias-variance tradeoff
+ Example code for Bias-Variance tradeoff
+ Understanding what happens
+ Summing up
+ Another Example from Scikit-Learn's Repository
+ Various steps in cross-validation
+ How to set up the cross-validation for Ridge and/or Lasso
+ Cross-validation in brief
+ Code Example for Cross-validation and \( k \)-fold Cross-validation
+ More examples on bootstrap and cross-validation and errors
+ The same example but now with cross-validation
+ Cross-validation with Ridge
@@ -249,50 +251,82 @@ MathJax.Hub.Config({
-
+
+
+The same example but now with cross-validation
-Cross-validation with Ridge
-
import numpy as np
+# Common imports
+import os
+import numpy as np
+import pandas as pd
import matplotlib.pyplot as plt
+from sklearn.linear_model import LinearRegression, Ridge, Lasso
+from sklearn.metrics import mean_squared_error
from sklearn.model_selection import KFold
-from sklearn.linear_model import Ridge
from sklearn.model_selection import cross_val_score
-from sklearn.preprocessing import PolynomialFeatures
-# A seed just to ensure that the random numbers are the same for every run.
-np.random.seed(3155)
-# Generate the data.
-n = 100
-x = np.linspace(-3, 3, n).reshape(-1, 1)
-y = np.exp(-x**2) + 1.5 * np.exp(-(x-2)**2)+ np.random.normal(0, 0.1, x.shape)
-# Decide degree on polynomial to fit
-poly = PolynomialFeatures(degree = 10)
-# Decide which values of lambda to use
-nlambdas = 500
-lambdas = np.logspace(-3, 5, nlambdas)
-# Initialize a KFold instance
-k = 5
+# Where to save the figures and data files
+PROJECT_ROOT_DIR = "Results"
+FIGURE_ID = "Results/FigureFiles"
+DATA_ID = "DataFiles/"
+
+if not os.path.exists(PROJECT_ROOT_DIR):
+ os.mkdir(PROJECT_ROOT_DIR)
+
+if not os.path.exists(FIGURE_ID):
+ os.makedirs(FIGURE_ID)
+
+if not os.path.exists(DATA_ID):
+ os.makedirs(DATA_ID)
+
+def image_path(fig_id):
+ return os.path.join(FIGURE_ID, fig_id)
+
+def data_path(dat_id):
+ return os.path.join(DATA_ID, dat_id)
+
+def save_fig(fig_id):
+ plt.savefig(image_path(fig_id) + ".png", format='png')
+
+infile = open(data_path("EoS.csv"),'r')
+
+# Read the EoS data as csv file and organize the data into two arrays with density and energies
+EoS = pd.read_csv(infile, names=('Density', 'Energy'))
+EoS['Energy'] = pd.to_numeric(EoS['Energy'], errors='coerce')
+EoS = EoS.dropna()
+Energies = EoS['Energy']
+Density = EoS['Density']
+# The design matrix now as function of various polytrops
+
+Maxpolydegree = 30
+X = np.zeros((len(Density),Maxpolydegree))
+X[:,0] = 1.0
+estimated_mse_sklearn = np.zeros(Maxpolydegree)
+polynomial = np.zeros(Maxpolydegree)
+k =5
kfold = KFold(n_splits = k)
-estimated_mse_sklearn = np.zeros(nlambdas)
-i = 0
-for lmb in lambdas:
- ridge = Ridge(alpha = lmb)
- estimated_mse_folds = cross_val_score(ridge, x, y, scoring='neg_mean_squared_error', cv=kfold)
- estimated_mse_sklearn[i] = np.mean(-estimated_mse_folds)
- i += 1
-plt.figure()
-plt.plot(np.log10(lambdas), estimated_mse_sklearn, label = 'cross_val_score')
-plt.xlabel('log10(lambda)')
-plt.ylabel('MSE')
+
+for polydegree in range(1, Maxpolydegree):
+ polynomial[polydegree] = polydegree
+ for degree in range(polydegree):
+ X[:,degree] = Density**(degree/3.0)
+ OLS = LinearRegression()
+# loop over trials in order to estimate the expectation value of the MSE
+ estimated_mse_folds = cross_val_score(OLS, X, Energies, scoring='neg_mean_squared_error', cv=kfold)
+#[:, np.newaxis]
+ estimated_mse_sklearn[polydegree] = np.mean(-estimated_mse_folds)
+
+plt.plot(polynomial, np.log10(estimated_mse_sklearn), label='Test Error')
+plt.xlabel('Polynomial degree')
+plt.ylabel('log10[MSE]')
plt.legend()
plt.show()
-
diff --git a/doc/pub/week37/html/week37-bs.html b/doc/pub/week37/html/week37-bs.html
index a60ece31a..c76d88187 100644
--- a/doc/pub/week37/html/week37-bs.html
+++ b/doc/pub/week37/html/week37-bs.html
@@ -69,6 +69,7 @@ Automatically generated HTML file from DocOnce source
2,
None,
'test-function-for-what-happens-with-ols-ridge-and-lasso'),
+ ('Rerunning the above code', 2, None, 'rerunning-the-above-code'),
("Invoking Bayes' theorem", 2, None, 'invoking-bayes-theorem'),
('Ridge and Bayes', 2, None, 'ridge-and-bayes'),
('Lasso and Bayes', 2, None, 'lasso-and-bayes'),
@@ -206,36 +207,37 @@ MathJax.Hub.Config({
Bayes' Theorem
Interpretations of Bayes' Theorem
Test Function for what happens with OLS, Ridge and Lasso
-
Invoking Bayes' theorem
-
Ridge and Bayes
-
Lasso and Bayes
-
Why resampling methods
-
Resampling methods
-
Resampling approaches can be computationally expensive
-
Why resampling methods ?
-
Statistical analysis
-
Resampling methods
-
Resampling methods: Jackknife and Bootstrap
-
Resampling methods: Jackknife
-
Jackknife code example
-
Resampling methods: Bootstrap
-
Resampling methods: Bootstrap background
-
Resampling methods: More Bootstrap background
-
Resampling methods: Bootstrap approach
-
Resampling methods: Bootstrap steps
-
Code example for the Bootstrap method
-
The bias-variance tradeoff
-
Example code for Bias-Variance tradeoff
-
Understanding what happens
-
Summing up
-
Another Example from Scikit-Learn's Repository
-
Various steps in cross-validation
-
How to set up the cross-validation for Ridge and/or Lasso
-
Cross-validation in brief
-
Code Example for Cross-validation and \( k \)-fold Cross-validation
-
More examples on bootstrap and cross-validation and errors
-
The same example but now with cross-validation
-
Cross-validation with Ridge
+
Rerunning the above code
+
Invoking Bayes' theorem
+
Ridge and Bayes
+
Lasso and Bayes
+
Why resampling methods
+
Resampling methods
+
Resampling approaches can be computationally expensive
+
Why resampling methods ?
+
Statistical analysis
+
Resampling methods
+
Resampling methods: Jackknife and Bootstrap
+
Resampling methods: Jackknife
+
Jackknife code example
+
Resampling methods: Bootstrap
+
Resampling methods: Bootstrap background
+
Resampling methods: More Bootstrap background
+
Resampling methods: Bootstrap approach
+
Resampling methods: Bootstrap steps
+
Code example for the Bootstrap method
+
The bias-variance tradeoff
+
Example code for Bias-Variance tradeoff
+
Understanding what happens
+
Summing up
+
Another Example from Scikit-Learn's Repository
+
Various steps in cross-validation
+
How to set up the cross-validation for Ridge and/or Lasso
+
Cross-validation in brief
+
Code Example for Cross-validation and \( k \)-fold Cross-validation
+
More examples on bootstrap and cross-validation and errors
+
The same example but now with cross-validation
+
Cross-validation with Ridge
@@ -270,7 +272,7 @@ MathJax.Hub.Config({
[2] Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University
-
Sep 15, 2021
+
Sep 16, 2021
@@ -294,7 +296,7 @@ MathJax.Hub.Config({
9
10
...
-
40
+
41
»
diff --git a/doc/pub/week37/html/week37-reveal.html b/doc/pub/week37/html/week37-reveal.html
index ed627abc0..024e972a1 100644
--- a/doc/pub/week37/html/week37-reveal.html
+++ b/doc/pub/week37/html/week37-reveal.html
@@ -148,7 +148,7 @@ MathJax.Hub.Config({
[2] Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University
-
Sep 15, 2021
+
Sep 16, 2021
@@ -442,6 +442,76 @@ How can we understand this?
+
+Rerunning the above code
+
+
+Let us write out the values of the coefficients \( \beta_i \) as functions
+of the polynomial degree and noise. We will focus only on the Ridge
+results and some few selected values of the hyperparameter \( \lambda \).
+
+
+If we don't include any noise and run this code for different values
+of the polynomial degree, we notice that the results for \( \beta_i \) do
+not show great changes from one order to the next. This is an
+indication that for higher polynomial orders, our parameters become
+less important.
+
+
+If we however add noise, what happens is that the polynomial fit is
+trying to adjust the fit to traverse in the best possible way all data
+points. This can lead to large fluctuations in the parameters
+\( \beta_i \) as functions of polynomial order. It will also be reflected
+in a larger value of the variance of each parameter \( \beta_i \). What
+Ridge regression (and Lasso as well) are doing then is to try to
+quench the fluctuations in the parameters of \( \beta_i \) which have a
+large variance (normally for higher orders in the polynomial).
+
+
+
+
+
import numpy as np
+import pandas as pd
+from IPython.display import display
+import matplotlib.pyplot as plt
+from sklearn.model_selection import train_test_split
+from sklearn import linear_model
+
+# Make data set.
+n = 1000
+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)
+
+# Decide which values of lambda to use
+nlambdas = 5
+lambdas = np.logspace(-3, 2, nlambdas)
+for i in range(nlambdas):
+ lmb = lambdas[i]
+ # Make the fit using Ridge only
+ RegRidge = linear_model.Ridge(lmb,fit_intercept=False)
+ RegRidge.fit(X_train,y_train)
+ # and then make the prediction
+ ypredictRidge = RegRidge.predict(X_test)
+ Coeffs = np.array(RegRidge.coef_)
+ BetaValues = pd.DataFrame(Coeffs)
+ BetaValues.columns = ['beta']
+ display(BetaValues)
+
+
+
+
Invoking Bayes' theorem
diff --git a/doc/pub/week37/html/week37-solarized.html b/doc/pub/week37/html/week37-solarized.html
index b54409fe1..1a7823d9f 100644
--- a/doc/pub/week37/html/week37-solarized.html
+++ b/doc/pub/week37/html/week37-solarized.html
@@ -89,6 +89,7 @@ div { text-align: justify; text-justify: inter-word; }
2,
None,
'test-function-for-what-happens-with-ols-ridge-and-lasso'),
+ ('Rerunning the above code', 2, None, 'rerunning-the-above-code'),
("Invoking Bayes' theorem", 2, None, 'invoking-bayes-theorem'),
('Ridge and Bayes', 2, None, 'ridge-and-bayes'),
('Lasso and Bayes', 2, None, 'lasso-and-bayes'),
@@ -221,7 +222,7 @@ MathJax.Hub.Config({
[2] Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University
-
Sep 15, 2021
+Sep 16, 2021
@@ -486,6 +487,75 @@ How can we understand this?
+
Rerunning the above code
+
+
+Let us write out the values of the coefficients \( \beta_i \) as functions
+of the polynomial degree and noise. We will focus only on the Ridge
+results and some few selected values of the hyperparameter \( \lambda \).
+
+
+If we don't include any noise and run this code for different values
+of the polynomial degree, we notice that the results for \( \beta_i \) do
+not show great changes from one order to the next. This is an
+indication that for higher polynomial orders, our parameters become
+less important.
+
+
+If we however add noise, what happens is that the polynomial fit is
+trying to adjust the fit to traverse in the best possible way all data
+points. This can lead to large fluctuations in the parameters
+\( \beta_i \) as functions of polynomial order. It will also be reflected
+in a larger value of the variance of each parameter \( \beta_i \). What
+Ridge regression (and Lasso as well) are doing then is to try to
+quench the fluctuations in the parameters of \( \beta_i \) which have a
+large variance (normally for higher orders in the polynomial).
+
+
+
+
+
import numpy as np
+import pandas as pd
+from IPython.display import display
+import matplotlib.pyplot as plt
+from sklearn.model_selection import train_test_split
+from sklearn import linear_model
+
+# Make data set.
+n = 1000
+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)
+
+# Decide which values of lambda to use
+nlambdas = 5
+lambdas = np.logspace(-3, 2, nlambdas)
+for i in range(nlambdas):
+ lmb = lambdas[i]
+ # Make the fit using Ridge only
+ RegRidge = linear_model.Ridge(lmb,fit_intercept=False)
+ RegRidge.fit(X_train,y_train)
+ # and then make the prediction
+ ypredictRidge = RegRidge.predict(X_test)
+ Coeffs = np.array(RegRidge.coef_)
+ BetaValues = pd.DataFrame(Coeffs)
+ BetaValues.columns = ['beta']
+ display(BetaValues)
+
+
+
+
Invoking Bayes' theorem
diff --git a/doc/pub/week37/html/week37.html b/doc/pub/week37/html/week37.html
index de5b07941..55b22f101 100644
--- a/doc/pub/week37/html/week37.html
+++ b/doc/pub/week37/html/week37.html
@@ -94,6 +94,7 @@ div { text-align: justify; text-justify: inter-word; }
2,
None,
'test-function-for-what-happens-with-ols-ridge-and-lasso'),
+ ('Rerunning the above code', 2, None, 'rerunning-the-above-code'),
("Invoking Bayes' theorem", 2, None, 'invoking-bayes-theorem'),
('Ridge and Bayes', 2, None, 'ridge-and-bayes'),
('Lasso and Bayes', 2, None, 'lasso-and-bayes'),
@@ -226,7 +227,7 @@ MathJax.Hub.Config({
[2] Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University
-
Sep 15, 2021
+Sep 16, 2021
@@ -491,6 +492,75 @@ How can we understand this?
+
Rerunning the above code
+
+
+Let us write out the values of the coefficients \( \beta_i \) as functions
+of the polynomial degree and noise. We will focus only on the Ridge
+results and some few selected values of the hyperparameter \( \lambda \).
+
+
+If we don't include any noise and run this code for different values
+of the polynomial degree, we notice that the results for \( \beta_i \) do
+not show great changes from one order to the next. This is an
+indication that for higher polynomial orders, our parameters become
+less important.
+
+
+If we however add noise, what happens is that the polynomial fit is
+trying to adjust the fit to traverse in the best possible way all data
+points. This can lead to large fluctuations in the parameters
+\( \beta_i \) as functions of polynomial order. It will also be reflected
+in a larger value of the variance of each parameter \( \beta_i \). What
+Ridge regression (and Lasso as well) are doing then is to try to
+quench the fluctuations in the parameters of \( \beta_i \) which have a
+large variance (normally for higher orders in the polynomial).
+
+
+
+
+
import numpy as np
+import pandas as pd
+from IPython.display import display
+import matplotlib.pyplot as plt
+from sklearn.model_selection import train_test_split
+from sklearn import linear_model
+
+# Make data set.
+n = 1000
+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)
+
+# Decide which values of lambda to use
+nlambdas = 5
+lambdas = np.logspace(-3, 2, nlambdas)
+for i in range(nlambdas):
+ lmb = lambdas[i]
+ # Make the fit using Ridge only
+ RegRidge = linear_model.Ridge(lmb,fit_intercept=False)
+ RegRidge.fit(X_train,y_train)
+ # and then make the prediction
+ ypredictRidge = RegRidge.predict(X_test)
+ Coeffs = np.array(RegRidge.coef_)
+ BetaValues = pd.DataFrame(Coeffs)
+ BetaValues.columns = ['beta']
+ display(BetaValues)
+
+
+
+
Invoking Bayes' theorem
diff --git a/doc/pub/week37/ipynb/ipynb-week37-src.tar.gz b/doc/pub/week37/ipynb/ipynb-week37-src.tar.gz
index 5edd4061c..788a81b6a 100644
Binary files a/doc/pub/week37/ipynb/ipynb-week37-src.tar.gz and b/doc/pub/week37/ipynb/ipynb-week37-src.tar.gz differ
diff --git a/doc/pub/week37/ipynb/week37.ipynb b/doc/pub/week37/ipynb/week37.ipynb
index c075114df..ba7ad38df 100644
--- a/doc/pub/week37/ipynb/week37.ipynb
+++ b/doc/pub/week37/ipynb/week37.ipynb
@@ -10,7 +10,7 @@
" \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 15, 2021**\n",
+ "Date: **Sep 16, 2021**\n",
"\n",
"Copyright 1999-2021, Morten Hjorth-Jensen. Released under CC Attribution-NonCommercial 4.0 license\n",
"\n",
@@ -366,6 +366,81 @@
"How can we understand this? \n",
"\n",
"\n",
+ "## Rerunning the above code\n",
+ "\n",
+ "Let us write out the values of the coefficients $\\beta_i$ as functions\n",
+ "of the polynomial degree and noise. We will focus only on the Ridge\n",
+ "results and some few selected values of the hyperparameter $\\lambda$.\n",
+ "\n",
+ "If we don't include any noise and run this code for different values\n",
+ "of the polynomial degree, we notice that the results for $\\beta_i$ do\n",
+ "not show great changes from one order to the next. This is an\n",
+ "indication that for higher polynomial orders, our parameters become\n",
+ "less important.\n",
+ "\n",
+ "If we however add noise, what happens is that the polynomial fit is\n",
+ "trying to adjust the fit to traverse in the best possible way all data\n",
+ "points. This can lead to large fluctuations in the parameters\n",
+ "$\\beta_i$ as functions of polynomial order. It will also be reflected\n",
+ "in a larger value of the variance of each parameter $\\beta_i$. What\n",
+ "Ridge regression (and Lasso as well) are doing then is to try to\n",
+ "quench the fluctuations in the parameters of $\\beta_i$ which have a\n",
+ "large variance (normally for higher orders in the polynomial)."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {
+ "collapsed": false,
+ "editable": true
+ },
+ "outputs": [],
+ "source": [
+ "import numpy as np\n",
+ "import pandas as pd\n",
+ "from IPython.display import display\n",
+ "import matplotlib.pyplot as plt\n",
+ "from sklearn.model_selection import train_test_split\n",
+ "from sklearn import linear_model\n",
+ "\n",
+ "# Make data set.\n",
+ "n = 1000\n",
+ "x = np.random.rand(n)\n",
+ "y = np.exp(-x**2) + 1.5 * np.exp(-(x-2)**2)+ np.random.randn(n)\n",
+ "\n",
+ "Maxpolydegree = 5\n",
+ "X = np.zeros((len(x),Maxpolydegree))\n",
+ "X[:,0] = 1.0\n",
+ "\n",
+ "for polydegree in range(1, Maxpolydegree):\n",
+ " for degree in range(polydegree):\n",
+ " X[:,degree] = x**(degree)\n",
+ "\n",
+ "\n",
+ "# We split the data in test and training data\n",
+ "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)\n",
+ "\n",
+ "# Decide which values of lambda to use\n",
+ "nlambdas = 5\n",
+ "lambdas = np.logspace(-3, 2, nlambdas)\n",
+ "for i in range(nlambdas):\n",
+ " lmb = lambdas[i]\n",
+ " # Make the fit using Ridge only\n",
+ " RegRidge = linear_model.Ridge(lmb,fit_intercept=False)\n",
+ " RegRidge.fit(X_train,y_train)\n",
+ " # and then make the prediction\n",
+ " ypredictRidge = RegRidge.predict(X_test)\n",
+ " Coeffs = np.array(RegRidge.coef_)\n",
+ " BetaValues = pd.DataFrame(Coeffs)\n",
+ " BetaValues.columns = ['beta']\n",
+ " display(BetaValues)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
"## Invoking Bayes' theorem\n",
"\n",
"Using Bayes' theorem we can gain a better intuition about Ridge and Lasso regression. \n",
diff --git a/doc/src/week37/test.py b/doc/src/week37/test.py
new file mode 100644
index 000000000..958bceb5f
--- /dev/null
+++ b/doc/src/week37/test.py
@@ -0,0 +1,38 @@
+import numpy as np
+import pandas as pd
+from IPython.display import display
+import matplotlib.pyplot as plt
+from sklearn.model_selection import train_test_split
+from sklearn import linear_model
+
+# Make data set.
+n = 1000
+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)
+
+# Decide which values of lambda to use
+nlambdas = 2
+lambdas = np.logspace(-3, -1, nlambdas)
+for i in range(nlambdas):
+ lmb = lambdas[i]
+ # Make the fit using Ridge only
+ RegRidge = linear_model.Ridge(lmb,fit_intercept=False)
+ RegRidge.fit(X_train,y_train)
+ # and then make the prediction
+ ypredictRidge = RegRidge.predict(X_test)
+ Coeffs = np.array(RegRidge.coef_)
+ BetaValues = pd.DataFrame(Coeffs)
+ BetaValues.columns = ['beta']
+ display(BetaValues)
diff --git a/doc/src/week37/week37.do.txt b/doc/src/week37/week37.do.txt
index c7c6fa37c..74f5eae2f 100644
--- a/doc/src/week37/week37.do.txt
+++ b/doc/src/week37/week37.do.txt
@@ -249,6 +249,73 @@ plt.show()
How can we understand this?
+!split
+===== Rerunning the above code =====
+
+Let us write out the values of the coefficients $\beta_i$ as functions
+of the polynomial degree and noise. We will focus only on the Ridge
+results and some few selected values of the hyperparameter $\lambda$.
+
+If we don't include any noise and run this code for different values
+of the polynomial degree, we notice that the results for $\beta_i$ do
+not show great changes from one order to the next. This is an
+indication that for higher polynomial orders, our parameters become
+less important.
+
+If we however add noise, what happens is that the polynomial fit is
+trying to adjust the fit to traverse in the best possible way all data
+points. This can lead to large fluctuations in the parameters
+$\beta_i$ as functions of polynomial order. It will also be reflected
+in a larger value of the variance of each parameter $\beta_i$. What
+Ridge regression (and Lasso as well) are doing then is to try to
+quench the fluctuations in the parameters of $\beta_i$ which have a
+large variance (normally for higher orders in the polynomial).
+
+!bc pycod
+import numpy as np
+import pandas as pd
+from IPython.display import display
+import matplotlib.pyplot as plt
+from sklearn.model_selection import train_test_split
+from sklearn import linear_model
+
+# Make data set.
+n = 1000
+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)
+
+# Decide which values of lambda to use
+nlambdas = 5
+lambdas = np.logspace(-3, 2, nlambdas)
+for i in range(nlambdas):
+ lmb = lambdas[i]
+ # Make the fit using Ridge only
+ RegRidge = linear_model.Ridge(lmb,fit_intercept=False)
+ RegRidge.fit(X_train,y_train)
+ # and then make the prediction
+ ypredictRidge = RegRidge.predict(X_test)
+ Coeffs = np.array(RegRidge.coef_)
+ BetaValues = pd.DataFrame(Coeffs)
+ BetaValues.columns = ['beta']
+ display(BetaValues)
+
+!ec
+
+
+
+
!split
===== Invoking Bayes' theorem =====