diff --git a/doc/pub/week35/html/._week35-bs000.html b/doc/pub/week35/html/._week35-bs000.html index e9a353109..3a6fa9a41 100644 --- a/doc/pub/week35/html/._week35-bs000.html +++ b/doc/pub/week35/html/._week35-bs000.html @@ -74,10 +74,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'optimizing-our-parameters'), - ('Our model for the nuclear binding energies', + ('Examples relevant for the exercises', 2, None, - 'our-model-for-the-nuclear-binding-energies'), + 'examples-relevant-for-the-exercises'), ('Optimizing our parameters, more details', 2, None, @@ -94,6 +94,8 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'some-useful-matrix-and-vector-expressions'), + ('The Jacobian', 2, None, 'the-jacobian'), + ('Derivatives, example 1', 2, None, 'derivatives-example-1'), ('Meet the Hessian Matrix', 2, None, 'meet-the-hessian-matrix'), ('Interpretations and optimizing our parameters', 2, @@ -148,6 +150,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'more-preprocessing-examples-franke-function-and-regression'), + ('Material for lecture Thursday, August 31', + 2, + None, + 'material-for-lecture-thursday-august-31'), ('Mathematical Interpretation of Ordinary Least Squares', 2, None, @@ -303,74 +309,77 @@ MathJax.Hub.Config({
  • Why Linear Regression (aka Ordinary Least Squares and family), repeat from last week
  • Regression analysis, overarching aims
  • Regression analysis, overarching aims II
  • -
  • Examples
  • +
  • Examples
  • General linear models
  • Rewriting the fitting procedure as a linear algebra problem
  • Rewriting the fitting procedure as a linear algebra problem, more details
  • Generalizing the fitting procedure as a linear algebra problem
  • Generalizing the fitting procedure as a linear algebra problem
  • Optimizing our parameters
  • -
  • Our model for the nuclear binding energies
  • +
  • Examples relevant for the exercises
  • Optimizing our parameters, more details
  • -
  • Interpretations and optimizing our parameters
  • -
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • Some useful matrix and vector expressions
  • -
  • Meet the Hessian Matrix
  • -
  • Interpretations and optimizing our parameters
  • -
  • Own code for Ordinary Least Squares
  • -
  • Adding error analysis and training set up
  • -
  • Splitting our Data in Training and Test data
  • -
  • Examples
  • -
  • Making your own test-train splitting
  • -
  • The Boston housing data example
  • -
  • Housing data, the code
  • -
  • Reducing the number of degrees of freedom, overarching view
  • -
  • Preprocessing our data
  • -
  • Functionality in Scikit-Learn
  • -
  • More preprocessing
  • -
  • Frequently used scaling functions
  • -
  • Example of own Standard scaling
  • -
  • Min-Max Scaling
  • -
  • Testing the Means Squared Error as function of Complexity
  • -
  • More preprocessing examples, Franke function and regression
  • -
  • Mathematical Interpretation of Ordinary Least Squares
  • -
  • Residual Error
  • -
  • Simple case
  • -
  • The singular value decomposition
  • -
  • Linear Regression Problems
  • -
  • Fixing the singularity
  • -
  • Basic math of the SVD
  • -
  • The SVD, a Fantastic Algorithm
  • -
  • Economy-size SVD
  • -
  • Codes for the SVD
  • -
  • Note about SVD Calculations
  • -
  • Mathematics of the SVD and implications
  • -
  • Example Matrix
  • -
  • Setting up the Matrix to be inverted
  • -
  • Further properties (important for our analyses later)
  • -
  • Meet the Covariance Matrix
  • -
  • Introducing the Covariance and Correlation functions
  • -
  • Covariance and Correlation Matrix
  • -
  • Correlation Function and Design/Feature Matrix
  • -
  • Covariance Matrix Examples
  • -
  • Correlation Matrix
  • -
  • Correlation Matrix with Pandas
  • -
  • Correlation Matrix with Pandas and the Franke function
  • -
  • Rewriting the Covariance and/or Correlation Matrix
  • -
  • Linking with the SVD
  • -
  • What does it mean?
  • -
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • -
  • Ridge and LASSO Regression
  • -
  • Deriving the Ridge Regression Equations
  • -
  • Interpreting the Ridge results
  • -
  • More interpretations
  • -
  • Deriving the Lasso Regression Equations
  • -
  • Exercises for week 35
  • -
  • Exercise 1: Setting up various Python environments
  • -
  • Exercise 2: making your own data and exploring scikit-learn
  • -
  • Exercise 3: Normalizing our data
  • -
  • Exercise 4: Adding Ridge Regression
  • -
  • Exercise 5: Analytical exercises
  • +
  • The Jacobian
  • +
  • Derivatives, example 1
  • +
  • Meet the Hessian Matrix
  • +
  • Interpretations and optimizing our parameters
  • +
  • Own code for Ordinary Least Squares
  • +
  • Adding error analysis and training set up
  • +
  • Splitting our Data in Training and Test data
  • +
  • Examples
  • +
  • Making your own test-train splitting
  • +
  • The Boston housing data example
  • +
  • Housing data, the code
  • +
  • Reducing the number of degrees of freedom, overarching view
  • +
  • Preprocessing our data
  • +
  • Functionality in Scikit-Learn
  • +
  • More preprocessing
  • +
  • Frequently used scaling functions
  • +
  • Example of own Standard scaling
  • +
  • Min-Max Scaling
  • +
  • Testing the Means Squared Error as function of Complexity
  • +
  • More preprocessing examples, Franke function and regression
  • +
  • Material for lecture Thursday, August 31
  • +
  • Mathematical Interpretation of Ordinary Least Squares
  • +
  • Residual Error
  • +
  • Simple case
  • +
  • The singular value decomposition
  • +
  • Linear Regression Problems
  • +
  • Fixing the singularity
  • +
  • Basic math of the SVD
  • +
  • The SVD, a Fantastic Algorithm
  • +
  • Economy-size SVD
  • +
  • Codes for the SVD
  • +
  • Note about SVD Calculations
  • +
  • Mathematics of the SVD and implications
  • +
  • Example Matrix
  • +
  • Setting up the Matrix to be inverted
  • +
  • Further properties (important for our analyses later)
  • +
  • Meet the Covariance Matrix
  • +
  • Introducing the Covariance and Correlation functions
  • +
  • Covariance and Correlation Matrix
  • +
  • Correlation Function and Design/Feature Matrix
  • +
  • Covariance Matrix Examples
  • +
  • Correlation Matrix
  • +
  • Correlation Matrix with Pandas
  • +
  • Correlation Matrix with Pandas and the Franke function
  • +
  • Rewriting the Covariance and/or Correlation Matrix
  • +
  • Linking with the SVD
  • +
  • What does it mean?
  • +
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • +
  • Ridge and LASSO Regression
  • +
  • Deriving the Ridge Regression Equations
  • +
  • Interpreting the Ridge results
  • +
  • More interpretations
  • +
  • Deriving the Lasso Regression Equations
  • +
  • Exercises for week 35
  • +
  • Exercise 1: Setting up various Python environments
  • +
  • Exercise 2: making your own data and exploring scikit-learn
  • +
  • Exercise 3: Normalizing our data
  • +
  • Exercise 4: Adding Ridge Regression
  • +
  • Exercise 5: Analytical exercises
  • @@ -400,7 +409,7 @@ MathJax.Hub.Config({
    -

    Jul 8, 2023

    +

    August 28-September 1


    @@ -425,7 +434,7 @@ MathJax.Hub.Config({
  • 9
  • 10
  • ...
  • -
  • 68
  • +
  • 71
  • »
  • diff --git a/doc/pub/week35/html/._week35-bs001.html b/doc/pub/week35/html/._week35-bs001.html index fb87a3a81..3b93c9c52 100644 --- a/doc/pub/week35/html/._week35-bs001.html +++ b/doc/pub/week35/html/._week35-bs001.html @@ -74,10 +74,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'optimizing-our-parameters'), - ('Our model for the nuclear binding energies', + ('Examples relevant for the exercises', 2, None, - 'our-model-for-the-nuclear-binding-energies'), + 'examples-relevant-for-the-exercises'), ('Optimizing our parameters, more details', 2, None, @@ -94,6 +94,8 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'some-useful-matrix-and-vector-expressions'), + ('The Jacobian', 2, None, 'the-jacobian'), + ('Derivatives, example 1', 2, None, 'derivatives-example-1'), ('Meet the Hessian Matrix', 2, None, 'meet-the-hessian-matrix'), ('Interpretations and optimizing our parameters', 2, @@ -148,6 +150,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'more-preprocessing-examples-franke-function-and-regression'), + ('Material for lecture Thursday, August 31', + 2, + None, + 'material-for-lecture-thursday-august-31'), ('Mathematical Interpretation of Ordinary Least Squares', 2, None, @@ -303,74 +309,77 @@ MathJax.Hub.Config({
  • Why Linear Regression (aka Ordinary Least Squares and family), repeat from last week
  • Regression analysis, overarching aims
  • Regression analysis, overarching aims II
  • -
  • Examples
  • +
  • Examples
  • General linear models
  • Rewriting the fitting procedure as a linear algebra problem
  • Rewriting the fitting procedure as a linear algebra problem, more details
  • Generalizing the fitting procedure as a linear algebra problem
  • Generalizing the fitting procedure as a linear algebra problem
  • Optimizing our parameters
  • -
  • Our model for the nuclear binding energies
  • +
  • Examples relevant for the exercises
  • Optimizing our parameters, more details
  • -
  • Interpretations and optimizing our parameters
  • -
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • Some useful matrix and vector expressions
  • -
  • Meet the Hessian Matrix
  • -
  • Interpretations and optimizing our parameters
  • -
  • Own code for Ordinary Least Squares
  • -
  • Adding error analysis and training set up
  • -
  • Splitting our Data in Training and Test data
  • -
  • Examples
  • -
  • Making your own test-train splitting
  • -
  • The Boston housing data example
  • -
  • Housing data, the code
  • -
  • Reducing the number of degrees of freedom, overarching view
  • -
  • Preprocessing our data
  • -
  • Functionality in Scikit-Learn
  • -
  • More preprocessing
  • -
  • Frequently used scaling functions
  • -
  • Example of own Standard scaling
  • -
  • Min-Max Scaling
  • -
  • Testing the Means Squared Error as function of Complexity
  • -
  • More preprocessing examples, Franke function and regression
  • -
  • Mathematical Interpretation of Ordinary Least Squares
  • -
  • Residual Error
  • -
  • Simple case
  • -
  • The singular value decomposition
  • -
  • Linear Regression Problems
  • -
  • Fixing the singularity
  • -
  • Basic math of the SVD
  • -
  • The SVD, a Fantastic Algorithm
  • -
  • Economy-size SVD
  • -
  • Codes for the SVD
  • -
  • Note about SVD Calculations
  • -
  • Mathematics of the SVD and implications
  • -
  • Example Matrix
  • -
  • Setting up the Matrix to be inverted
  • -
  • Further properties (important for our analyses later)
  • -
  • Meet the Covariance Matrix
  • -
  • Introducing the Covariance and Correlation functions
  • -
  • Covariance and Correlation Matrix
  • -
  • Correlation Function and Design/Feature Matrix
  • -
  • Covariance Matrix Examples
  • -
  • Correlation Matrix
  • -
  • Correlation Matrix with Pandas
  • -
  • Correlation Matrix with Pandas and the Franke function
  • -
  • Rewriting the Covariance and/or Correlation Matrix
  • -
  • Linking with the SVD
  • -
  • What does it mean?
  • -
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • -
  • Ridge and LASSO Regression
  • -
  • Deriving the Ridge Regression Equations
  • -
  • Interpreting the Ridge results
  • -
  • More interpretations
  • -
  • Deriving the Lasso Regression Equations
  • -
  • Exercises for week 35
  • -
  • Exercise 1: Setting up various Python environments
  • -
  • Exercise 2: making your own data and exploring scikit-learn
  • -
  • Exercise 3: Normalizing our data
  • -
  • Exercise 4: Adding Ridge Regression
  • -
  • Exercise 5: Analytical exercises
  • +
  • The Jacobian
  • +
  • Derivatives, example 1
  • +
  • Meet the Hessian Matrix
  • +
  • Interpretations and optimizing our parameters
  • +
  • Own code for Ordinary Least Squares
  • +
  • Adding error analysis and training set up
  • +
  • Splitting our Data in Training and Test data
  • +
  • Examples
  • +
  • Making your own test-train splitting
  • +
  • The Boston housing data example
  • +
  • Housing data, the code
  • +
  • Reducing the number of degrees of freedom, overarching view
  • +
  • Preprocessing our data
  • +
  • Functionality in Scikit-Learn
  • +
  • More preprocessing
  • +
  • Frequently used scaling functions
  • +
  • Example of own Standard scaling
  • +
  • Min-Max Scaling
  • +
  • Testing the Means Squared Error as function of Complexity
  • +
  • More preprocessing examples, Franke function and regression
  • +
  • Material for lecture Thursday, August 31
  • +
  • Mathematical Interpretation of Ordinary Least Squares
  • +
  • Residual Error
  • +
  • Simple case
  • +
  • The singular value decomposition
  • +
  • Linear Regression Problems
  • +
  • Fixing the singularity
  • +
  • Basic math of the SVD
  • +
  • The SVD, a Fantastic Algorithm
  • +
  • Economy-size SVD
  • +
  • Codes for the SVD
  • +
  • Note about SVD Calculations
  • +
  • Mathematics of the SVD and implications
  • +
  • Example Matrix
  • +
  • Setting up the Matrix to be inverted
  • +
  • Further properties (important for our analyses later)
  • +
  • Meet the Covariance Matrix
  • +
  • Introducing the Covariance and Correlation functions
  • +
  • Covariance and Correlation Matrix
  • +
  • Correlation Function and Design/Feature Matrix
  • +
  • Covariance Matrix Examples
  • +
  • Correlation Matrix
  • +
  • Correlation Matrix with Pandas
  • +
  • Correlation Matrix with Pandas and the Franke function
  • +
  • Rewriting the Covariance and/or Correlation Matrix
  • +
  • Linking with the SVD
  • +
  • What does it mean?
  • +
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • +
  • Ridge and LASSO Regression
  • +
  • Deriving the Ridge Regression Equations
  • +
  • Interpreting the Ridge results
  • +
  • More interpretations
  • +
  • Deriving the Lasso Regression Equations
  • +
  • Exercises for week 35
  • +
  • Exercise 1: Setting up various Python environments
  • +
  • Exercise 2: making your own data and exploring scikit-learn
  • +
  • Exercise 3: Normalizing our data
  • +
  • Exercise 4: Adding Ridge Regression
  • +
  • Exercise 5: Analytical exercises
  • @@ -418,7 +427,7 @@ MathJax.Hub.Config({
  • 10
  • 11
  • ...
  • -
  • 68
  • +
  • 71
  • »
  • diff --git a/doc/pub/week35/html/._week35-bs002.html b/doc/pub/week35/html/._week35-bs002.html index ac172751e..82afcddfc 100644 --- a/doc/pub/week35/html/._week35-bs002.html +++ b/doc/pub/week35/html/._week35-bs002.html @@ -74,10 +74,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'optimizing-our-parameters'), - ('Our model for the nuclear binding energies', + ('Examples relevant for the exercises', 2, None, - 'our-model-for-the-nuclear-binding-energies'), + 'examples-relevant-for-the-exercises'), ('Optimizing our parameters, more details', 2, None, @@ -94,6 +94,8 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'some-useful-matrix-and-vector-expressions'), + ('The Jacobian', 2, None, 'the-jacobian'), + ('Derivatives, example 1', 2, None, 'derivatives-example-1'), ('Meet the Hessian Matrix', 2, None, 'meet-the-hessian-matrix'), ('Interpretations and optimizing our parameters', 2, @@ -148,6 +150,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'more-preprocessing-examples-franke-function-and-regression'), + ('Material for lecture Thursday, August 31', + 2, + None, + 'material-for-lecture-thursday-august-31'), ('Mathematical Interpretation of Ordinary Least Squares', 2, None, @@ -303,74 +309,77 @@ MathJax.Hub.Config({
  • Why Linear Regression (aka Ordinary Least Squares and family), repeat from last week
  • Regression analysis, overarching aims
  • Regression analysis, overarching aims II
  • -
  • Examples
  • +
  • Examples
  • General linear models
  • Rewriting the fitting procedure as a linear algebra problem
  • Rewriting the fitting procedure as a linear algebra problem, more details
  • Generalizing the fitting procedure as a linear algebra problem
  • Generalizing the fitting procedure as a linear algebra problem
  • Optimizing our parameters
  • -
  • Our model for the nuclear binding energies
  • +
  • Examples relevant for the exercises
  • Optimizing our parameters, more details
  • -
  • Interpretations and optimizing our parameters
  • -
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • Some useful matrix and vector expressions
  • -
  • Meet the Hessian Matrix
  • -
  • Interpretations and optimizing our parameters
  • -
  • Own code for Ordinary Least Squares
  • -
  • Adding error analysis and training set up
  • -
  • Splitting our Data in Training and Test data
  • -
  • Examples
  • -
  • Making your own test-train splitting
  • -
  • The Boston housing data example
  • -
  • Housing data, the code
  • -
  • Reducing the number of degrees of freedom, overarching view
  • -
  • Preprocessing our data
  • -
  • Functionality in Scikit-Learn
  • -
  • More preprocessing
  • -
  • Frequently used scaling functions
  • -
  • Example of own Standard scaling
  • -
  • Min-Max Scaling
  • -
  • Testing the Means Squared Error as function of Complexity
  • -
  • More preprocessing examples, Franke function and regression
  • -
  • Mathematical Interpretation of Ordinary Least Squares
  • -
  • Residual Error
  • -
  • Simple case
  • -
  • The singular value decomposition
  • -
  • Linear Regression Problems
  • -
  • Fixing the singularity
  • -
  • Basic math of the SVD
  • -
  • The SVD, a Fantastic Algorithm
  • -
  • Economy-size SVD
  • -
  • Codes for the SVD
  • -
  • Note about SVD Calculations
  • -
  • Mathematics of the SVD and implications
  • -
  • Example Matrix
  • -
  • Setting up the Matrix to be inverted
  • -
  • Further properties (important for our analyses later)
  • -
  • Meet the Covariance Matrix
  • -
  • Introducing the Covariance and Correlation functions
  • -
  • Covariance and Correlation Matrix
  • -
  • Correlation Function and Design/Feature Matrix
  • -
  • Covariance Matrix Examples
  • -
  • Correlation Matrix
  • -
  • Correlation Matrix with Pandas
  • -
  • Correlation Matrix with Pandas and the Franke function
  • -
  • Rewriting the Covariance and/or Correlation Matrix
  • -
  • Linking with the SVD
  • -
  • What does it mean?
  • -
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • -
  • Ridge and LASSO Regression
  • -
  • Deriving the Ridge Regression Equations
  • -
  • Interpreting the Ridge results
  • -
  • More interpretations
  • -
  • Deriving the Lasso Regression Equations
  • -
  • Exercises for week 35
  • -
  • Exercise 1: Setting up various Python environments
  • -
  • Exercise 2: making your own data and exploring scikit-learn
  • -
  • Exercise 3: Normalizing our data
  • -
  • Exercise 4: Adding Ridge Regression
  • -
  • Exercise 5: Analytical exercises
  • +
  • The Jacobian
  • +
  • Derivatives, example 1
  • +
  • Meet the Hessian Matrix
  • +
  • Interpretations and optimizing our parameters
  • +
  • Own code for Ordinary Least Squares
  • +
  • Adding error analysis and training set up
  • +
  • Splitting our Data in Training and Test data
  • +
  • Examples
  • +
  • Making your own test-train splitting
  • +
  • The Boston housing data example
  • +
  • Housing data, the code
  • +
  • Reducing the number of degrees of freedom, overarching view
  • +
  • Preprocessing our data
  • +
  • Functionality in Scikit-Learn
  • +
  • More preprocessing
  • +
  • Frequently used scaling functions
  • +
  • Example of own Standard scaling
  • +
  • Min-Max Scaling
  • +
  • Testing the Means Squared Error as function of Complexity
  • +
  • More preprocessing examples, Franke function and regression
  • +
  • Material for lecture Thursday, August 31
  • +
  • Mathematical Interpretation of Ordinary Least Squares
  • +
  • Residual Error
  • +
  • Simple case
  • +
  • The singular value decomposition
  • +
  • Linear Regression Problems
  • +
  • Fixing the singularity
  • +
  • Basic math of the SVD
  • +
  • The SVD, a Fantastic Algorithm
  • +
  • Economy-size SVD
  • +
  • Codes for the SVD
  • +
  • Note about SVD Calculations
  • +
  • Mathematics of the SVD and implications
  • +
  • Example Matrix
  • +
  • Setting up the Matrix to be inverted
  • +
  • Further properties (important for our analyses later)
  • +
  • Meet the Covariance Matrix
  • +
  • Introducing the Covariance and Correlation functions
  • +
  • Covariance and Correlation Matrix
  • +
  • Correlation Function and Design/Feature Matrix
  • +
  • Covariance Matrix Examples
  • +
  • Correlation Matrix
  • +
  • Correlation Matrix with Pandas
  • +
  • Correlation Matrix with Pandas and the Franke function
  • +
  • Rewriting the Covariance and/or Correlation Matrix
  • +
  • Linking with the SVD
  • +
  • What does it mean?
  • +
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • +
  • Ridge and LASSO Regression
  • +
  • Deriving the Ridge Regression Equations
  • +
  • Interpreting the Ridge results
  • +
  • More interpretations
  • +
  • Deriving the Lasso Regression Equations
  • +
  • Exercises for week 35
  • +
  • Exercise 1: Setting up various Python environments
  • +
  • Exercise 2: making your own data and exploring scikit-learn
  • +
  • Exercise 3: Normalizing our data
  • +
  • Exercise 4: Adding Ridge Regression
  • +
  • Exercise 5: Analytical exercises
  • @@ -419,7 +428,7 @@ Similarly, Mehta et al
  • 11
  • 12
  • ...
  • -
  • 68
  • +
  • 71
  • »
  • diff --git a/doc/pub/week35/html/._week35-bs003.html b/doc/pub/week35/html/._week35-bs003.html index 193110ba2..a3af94f99 100644 --- a/doc/pub/week35/html/._week35-bs003.html +++ b/doc/pub/week35/html/._week35-bs003.html @@ -74,10 +74,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'optimizing-our-parameters'), - ('Our model for the nuclear binding energies', + ('Examples relevant for the exercises', 2, None, - 'our-model-for-the-nuclear-binding-energies'), + 'examples-relevant-for-the-exercises'), ('Optimizing our parameters, more details', 2, None, @@ -94,6 +94,8 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'some-useful-matrix-and-vector-expressions'), + ('The Jacobian', 2, None, 'the-jacobian'), + ('Derivatives, example 1', 2, None, 'derivatives-example-1'), ('Meet the Hessian Matrix', 2, None, 'meet-the-hessian-matrix'), ('Interpretations and optimizing our parameters', 2, @@ -148,6 +150,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'more-preprocessing-examples-franke-function-and-regression'), + ('Material for lecture Thursday, August 31', + 2, + None, + 'material-for-lecture-thursday-august-31'), ('Mathematical Interpretation of Ordinary Least Squares', 2, None, @@ -303,74 +309,77 @@ MathJax.Hub.Config({
  • Why Linear Regression (aka Ordinary Least Squares and family), repeat from last week
  • Regression analysis, overarching aims
  • Regression analysis, overarching aims II
  • -
  • Examples
  • +
  • Examples
  • General linear models
  • Rewriting the fitting procedure as a linear algebra problem
  • Rewriting the fitting procedure as a linear algebra problem, more details
  • Generalizing the fitting procedure as a linear algebra problem
  • Generalizing the fitting procedure as a linear algebra problem
  • Optimizing our parameters
  • -
  • Our model for the nuclear binding energies
  • +
  • Examples relevant for the exercises
  • Optimizing our parameters, more details
  • -
  • Interpretations and optimizing our parameters
  • -
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • Some useful matrix and vector expressions
  • -
  • Meet the Hessian Matrix
  • -
  • Interpretations and optimizing our parameters
  • -
  • Own code for Ordinary Least Squares
  • -
  • Adding error analysis and training set up
  • -
  • Splitting our Data in Training and Test data
  • -
  • Examples
  • -
  • Making your own test-train splitting
  • -
  • The Boston housing data example
  • -
  • Housing data, the code
  • -
  • Reducing the number of degrees of freedom, overarching view
  • -
  • Preprocessing our data
  • -
  • Functionality in Scikit-Learn
  • -
  • More preprocessing
  • -
  • Frequently used scaling functions
  • -
  • Example of own Standard scaling
  • -
  • Min-Max Scaling
  • -
  • Testing the Means Squared Error as function of Complexity
  • -
  • More preprocessing examples, Franke function and regression
  • -
  • Mathematical Interpretation of Ordinary Least Squares
  • -
  • Residual Error
  • -
  • Simple case
  • -
  • The singular value decomposition
  • -
  • Linear Regression Problems
  • -
  • Fixing the singularity
  • -
  • Basic math of the SVD
  • -
  • The SVD, a Fantastic Algorithm
  • -
  • Economy-size SVD
  • -
  • Codes for the SVD
  • -
  • Note about SVD Calculations
  • -
  • Mathematics of the SVD and implications
  • -
  • Example Matrix
  • -
  • Setting up the Matrix to be inverted
  • -
  • Further properties (important for our analyses later)
  • -
  • Meet the Covariance Matrix
  • -
  • Introducing the Covariance and Correlation functions
  • -
  • Covariance and Correlation Matrix
  • -
  • Correlation Function and Design/Feature Matrix
  • -
  • Covariance Matrix Examples
  • -
  • Correlation Matrix
  • -
  • Correlation Matrix with Pandas
  • -
  • Correlation Matrix with Pandas and the Franke function
  • -
  • Rewriting the Covariance and/or Correlation Matrix
  • -
  • Linking with the SVD
  • -
  • What does it mean?
  • -
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • -
  • Ridge and LASSO Regression
  • -
  • Deriving the Ridge Regression Equations
  • -
  • Interpreting the Ridge results
  • -
  • More interpretations
  • -
  • Deriving the Lasso Regression Equations
  • -
  • Exercises for week 35
  • -
  • Exercise 1: Setting up various Python environments
  • -
  • Exercise 2: making your own data and exploring scikit-learn
  • -
  • Exercise 3: Normalizing our data
  • -
  • Exercise 4: Adding Ridge Regression
  • -
  • Exercise 5: Analytical exercises
  • +
  • The Jacobian
  • +
  • Derivatives, example 1
  • +
  • Meet the Hessian Matrix
  • +
  • Interpretations and optimizing our parameters
  • +
  • Own code for Ordinary Least Squares
  • +
  • Adding error analysis and training set up
  • +
  • Splitting our Data in Training and Test data
  • +
  • Examples
  • +
  • Making your own test-train splitting
  • +
  • The Boston housing data example
  • +
  • Housing data, the code
  • +
  • Reducing the number of degrees of freedom, overarching view
  • +
  • Preprocessing our data
  • +
  • Functionality in Scikit-Learn
  • +
  • More preprocessing
  • +
  • Frequently used scaling functions
  • +
  • Example of own Standard scaling
  • +
  • Min-Max Scaling
  • +
  • Testing the Means Squared Error as function of Complexity
  • +
  • More preprocessing examples, Franke function and regression
  • +
  • Material for lecture Thursday, August 31
  • +
  • Mathematical Interpretation of Ordinary Least Squares
  • +
  • Residual Error
  • +
  • Simple case
  • +
  • The singular value decomposition
  • +
  • Linear Regression Problems
  • +
  • Fixing the singularity
  • +
  • Basic math of the SVD
  • +
  • The SVD, a Fantastic Algorithm
  • +
  • Economy-size SVD
  • +
  • Codes for the SVD
  • +
  • Note about SVD Calculations
  • +
  • Mathematics of the SVD and implications
  • +
  • Example Matrix
  • +
  • Setting up the Matrix to be inverted
  • +
  • Further properties (important for our analyses later)
  • +
  • Meet the Covariance Matrix
  • +
  • Introducing the Covariance and Correlation functions
  • +
  • Covariance and Correlation Matrix
  • +
  • Correlation Function and Design/Feature Matrix
  • +
  • Covariance Matrix Examples
  • +
  • Correlation Matrix
  • +
  • Correlation Matrix with Pandas
  • +
  • Correlation Matrix with Pandas and the Franke function
  • +
  • Rewriting the Covariance and/or Correlation Matrix
  • +
  • Linking with the SVD
  • +
  • What does it mean?
  • +
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • +
  • Ridge and LASSO Regression
  • +
  • Deriving the Ridge Regression Equations
  • +
  • Interpreting the Ridge results
  • +
  • More interpretations
  • +
  • Deriving the Lasso Regression Equations
  • +
  • Exercises for week 35
  • +
  • Exercise 1: Setting up various Python environments
  • +
  • Exercise 2: making your own data and exploring scikit-learn
  • +
  • Exercise 3: Normalizing our data
  • +
  • Exercise 4: Adding Ridge Regression
  • +
  • Exercise 5: Analytical exercises
  • @@ -420,7 +429,7 @@ The first variable is called the dependent, the outcome or the
  • 12
  • 13
  • ...
  • -
  • 68
  • +
  • 71
  • »
  • diff --git a/doc/pub/week35/html/._week35-bs004.html b/doc/pub/week35/html/._week35-bs004.html index cbd3a0a00..3dbaf6de2 100644 --- a/doc/pub/week35/html/._week35-bs004.html +++ b/doc/pub/week35/html/._week35-bs004.html @@ -74,10 +74,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'optimizing-our-parameters'), - ('Our model for the nuclear binding energies', + ('Examples relevant for the exercises', 2, None, - 'our-model-for-the-nuclear-binding-energies'), + 'examples-relevant-for-the-exercises'), ('Optimizing our parameters, more details', 2, None, @@ -94,6 +94,8 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'some-useful-matrix-and-vector-expressions'), + ('The Jacobian', 2, None, 'the-jacobian'), + ('Derivatives, example 1', 2, None, 'derivatives-example-1'), ('Meet the Hessian Matrix', 2, None, 'meet-the-hessian-matrix'), ('Interpretations and optimizing our parameters', 2, @@ -148,6 +150,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'more-preprocessing-examples-franke-function-and-regression'), + ('Material for lecture Thursday, August 31', + 2, + None, + 'material-for-lecture-thursday-august-31'), ('Mathematical Interpretation of Ordinary Least Squares', 2, None, @@ -303,74 +309,77 @@ MathJax.Hub.Config({
  • Why Linear Regression (aka Ordinary Least Squares and family), repeat from last week
  • Regression analysis, overarching aims
  • Regression analysis, overarching aims II
  • -
  • Examples
  • +
  • Examples
  • General linear models
  • Rewriting the fitting procedure as a linear algebra problem
  • Rewriting the fitting procedure as a linear algebra problem, more details
  • Generalizing the fitting procedure as a linear algebra problem
  • Generalizing the fitting procedure as a linear algebra problem
  • Optimizing our parameters
  • -
  • Our model for the nuclear binding energies
  • +
  • Examples relevant for the exercises
  • Optimizing our parameters, more details
  • -
  • Interpretations and optimizing our parameters
  • -
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • Some useful matrix and vector expressions
  • -
  • Meet the Hessian Matrix
  • -
  • Interpretations and optimizing our parameters
  • -
  • Own code for Ordinary Least Squares
  • -
  • Adding error analysis and training set up
  • -
  • Splitting our Data in Training and Test data
  • -
  • Examples
  • -
  • Making your own test-train splitting
  • -
  • The Boston housing data example
  • -
  • Housing data, the code
  • -
  • Reducing the number of degrees of freedom, overarching view
  • -
  • Preprocessing our data
  • -
  • Functionality in Scikit-Learn
  • -
  • More preprocessing
  • -
  • Frequently used scaling functions
  • -
  • Example of own Standard scaling
  • -
  • Min-Max Scaling
  • -
  • Testing the Means Squared Error as function of Complexity
  • -
  • More preprocessing examples, Franke function and regression
  • -
  • Mathematical Interpretation of Ordinary Least Squares
  • -
  • Residual Error
  • -
  • Simple case
  • -
  • The singular value decomposition
  • -
  • Linear Regression Problems
  • -
  • Fixing the singularity
  • -
  • Basic math of the SVD
  • -
  • The SVD, a Fantastic Algorithm
  • -
  • Economy-size SVD
  • -
  • Codes for the SVD
  • -
  • Note about SVD Calculations
  • -
  • Mathematics of the SVD and implications
  • -
  • Example Matrix
  • -
  • Setting up the Matrix to be inverted
  • -
  • Further properties (important for our analyses later)
  • -
  • Meet the Covariance Matrix
  • -
  • Introducing the Covariance and Correlation functions
  • -
  • Covariance and Correlation Matrix
  • -
  • Correlation Function and Design/Feature Matrix
  • -
  • Covariance Matrix Examples
  • -
  • Correlation Matrix
  • -
  • Correlation Matrix with Pandas
  • -
  • Correlation Matrix with Pandas and the Franke function
  • -
  • Rewriting the Covariance and/or Correlation Matrix
  • -
  • Linking with the SVD
  • -
  • What does it mean?
  • -
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • -
  • Ridge and LASSO Regression
  • -
  • Deriving the Ridge Regression Equations
  • -
  • Interpreting the Ridge results
  • -
  • More interpretations
  • -
  • Deriving the Lasso Regression Equations
  • -
  • Exercises for week 35
  • -
  • Exercise 1: Setting up various Python environments
  • -
  • Exercise 2: making your own data and exploring scikit-learn
  • -
  • Exercise 3: Normalizing our data
  • -
  • Exercise 4: Adding Ridge Regression
  • -
  • Exercise 5: Analytical exercises
  • +
  • The Jacobian
  • +
  • Derivatives, example 1
  • +
  • Meet the Hessian Matrix
  • +
  • Interpretations and optimizing our parameters
  • +
  • Own code for Ordinary Least Squares
  • +
  • Adding error analysis and training set up
  • +
  • Splitting our Data in Training and Test data
  • +
  • Examples
  • +
  • Making your own test-train splitting
  • +
  • The Boston housing data example
  • +
  • Housing data, the code
  • +
  • Reducing the number of degrees of freedom, overarching view
  • +
  • Preprocessing our data
  • +
  • Functionality in Scikit-Learn
  • +
  • More preprocessing
  • +
  • Frequently used scaling functions
  • +
  • Example of own Standard scaling
  • +
  • Min-Max Scaling
  • +
  • Testing the Means Squared Error as function of Complexity
  • +
  • More preprocessing examples, Franke function and regression
  • +
  • Material for lecture Thursday, August 31
  • +
  • Mathematical Interpretation of Ordinary Least Squares
  • +
  • Residual Error
  • +
  • Simple case
  • +
  • The singular value decomposition
  • +
  • Linear Regression Problems
  • +
  • Fixing the singularity
  • +
  • Basic math of the SVD
  • +
  • The SVD, a Fantastic Algorithm
  • +
  • Economy-size SVD
  • +
  • Codes for the SVD
  • +
  • Note about SVD Calculations
  • +
  • Mathematics of the SVD and implications
  • +
  • Example Matrix
  • +
  • Setting up the Matrix to be inverted
  • +
  • Further properties (important for our analyses later)
  • +
  • Meet the Covariance Matrix
  • +
  • Introducing the Covariance and Correlation functions
  • +
  • Covariance and Correlation Matrix
  • +
  • Correlation Function and Design/Feature Matrix
  • +
  • Covariance Matrix Examples
  • +
  • Correlation Matrix
  • +
  • Correlation Matrix with Pandas
  • +
  • Correlation Matrix with Pandas and the Franke function
  • +
  • Rewriting the Covariance and/or Correlation Matrix
  • +
  • Linking with the SVD
  • +
  • What does it mean?
  • +
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • +
  • Ridge and LASSO Regression
  • +
  • Deriving the Ridge Regression Equations
  • +
  • Interpreting the Ridge results
  • +
  • More interpretations
  • +
  • Deriving the Lasso Regression Equations
  • +
  • Exercises for week 35
  • +
  • Exercise 1: Setting up various Python environments
  • +
  • Exercise 2: making your own data and exploring scikit-learn
  • +
  • Exercise 3: Normalizing our data
  • +
  • Exercise 4: Adding Ridge Regression
  • +
  • Exercise 5: Analytical exercises
  • @@ -429,7 +438,7 @@ the linear regression model where \( \boldsymbol{\beta} = [\beta_0, \ld
  • 13
  • 14
  • ...
  • -
  • 68
  • +
  • 71
  • »
  • diff --git a/doc/pub/week35/html/._week35-bs005.html b/doc/pub/week35/html/._week35-bs005.html index 90eb6106b..b08690218 100644 --- a/doc/pub/week35/html/._week35-bs005.html +++ b/doc/pub/week35/html/._week35-bs005.html @@ -74,10 +74,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'optimizing-our-parameters'), - ('Our model for the nuclear binding energies', + ('Examples relevant for the exercises', 2, None, - 'our-model-for-the-nuclear-binding-energies'), + 'examples-relevant-for-the-exercises'), ('Optimizing our parameters, more details', 2, None, @@ -94,6 +94,8 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'some-useful-matrix-and-vector-expressions'), + ('The Jacobian', 2, None, 'the-jacobian'), + ('Derivatives, example 1', 2, None, 'derivatives-example-1'), ('Meet the Hessian Matrix', 2, None, 'meet-the-hessian-matrix'), ('Interpretations and optimizing our parameters', 2, @@ -148,6 +150,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'more-preprocessing-examples-franke-function-and-regression'), + ('Material for lecture Thursday, August 31', + 2, + None, + 'material-for-lecture-thursday-august-31'), ('Mathematical Interpretation of Ordinary Least Squares', 2, None, @@ -303,74 +309,77 @@ MathJax.Hub.Config({
  • Why Linear Regression (aka Ordinary Least Squares and family), repeat from last week
  • Regression analysis, overarching aims
  • Regression analysis, overarching aims II
  • -
  • Examples
  • +
  • Examples
  • General linear models
  • Rewriting the fitting procedure as a linear algebra problem
  • Rewriting the fitting procedure as a linear algebra problem, more details
  • Generalizing the fitting procedure as a linear algebra problem
  • Generalizing the fitting procedure as a linear algebra problem
  • Optimizing our parameters
  • -
  • Our model for the nuclear binding energies
  • +
  • Examples relevant for the exercises
  • Optimizing our parameters, more details
  • -
  • Interpretations and optimizing our parameters
  • -
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • Some useful matrix and vector expressions
  • -
  • Meet the Hessian Matrix
  • -
  • Interpretations and optimizing our parameters
  • -
  • Own code for Ordinary Least Squares
  • -
  • Adding error analysis and training set up
  • -
  • Splitting our Data in Training and Test data
  • -
  • Examples
  • -
  • Making your own test-train splitting
  • -
  • The Boston housing data example
  • -
  • Housing data, the code
  • -
  • Reducing the number of degrees of freedom, overarching view
  • -
  • Preprocessing our data
  • -
  • Functionality in Scikit-Learn
  • -
  • More preprocessing
  • -
  • Frequently used scaling functions
  • -
  • Example of own Standard scaling
  • -
  • Min-Max Scaling
  • -
  • Testing the Means Squared Error as function of Complexity
  • -
  • More preprocessing examples, Franke function and regression
  • -
  • Mathematical Interpretation of Ordinary Least Squares
  • -
  • Residual Error
  • -
  • Simple case
  • -
  • The singular value decomposition
  • -
  • Linear Regression Problems
  • -
  • Fixing the singularity
  • -
  • Basic math of the SVD
  • -
  • The SVD, a Fantastic Algorithm
  • -
  • Economy-size SVD
  • -
  • Codes for the SVD
  • -
  • Note about SVD Calculations
  • -
  • Mathematics of the SVD and implications
  • -
  • Example Matrix
  • -
  • Setting up the Matrix to be inverted
  • -
  • Further properties (important for our analyses later)
  • -
  • Meet the Covariance Matrix
  • -
  • Introducing the Covariance and Correlation functions
  • -
  • Covariance and Correlation Matrix
  • -
  • Correlation Function and Design/Feature Matrix
  • -
  • Covariance Matrix Examples
  • -
  • Correlation Matrix
  • -
  • Correlation Matrix with Pandas
  • -
  • Correlation Matrix with Pandas and the Franke function
  • -
  • Rewriting the Covariance and/or Correlation Matrix
  • -
  • Linking with the SVD
  • -
  • What does it mean?
  • -
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • -
  • Ridge and LASSO Regression
  • -
  • Deriving the Ridge Regression Equations
  • -
  • Interpreting the Ridge results
  • -
  • More interpretations
  • -
  • Deriving the Lasso Regression Equations
  • -
  • Exercises for week 35
  • -
  • Exercise 1: Setting up various Python environments
  • -
  • Exercise 2: making your own data and exploring scikit-learn
  • -
  • Exercise 3: Normalizing our data
  • -
  • Exercise 4: Adding Ridge Regression
  • -
  • Exercise 5: Analytical exercises
  • +
  • The Jacobian
  • +
  • Derivatives, example 1
  • +
  • Meet the Hessian Matrix
  • +
  • Interpretations and optimizing our parameters
  • +
  • Own code for Ordinary Least Squares
  • +
  • Adding error analysis and training set up
  • +
  • Splitting our Data in Training and Test data
  • +
  • Examples
  • +
  • Making your own test-train splitting
  • +
  • The Boston housing data example
  • +
  • Housing data, the code
  • +
  • Reducing the number of degrees of freedom, overarching view
  • +
  • Preprocessing our data
  • +
  • Functionality in Scikit-Learn
  • +
  • More preprocessing
  • +
  • Frequently used scaling functions
  • +
  • Example of own Standard scaling
  • +
  • Min-Max Scaling
  • +
  • Testing the Means Squared Error as function of Complexity
  • +
  • More preprocessing examples, Franke function and regression
  • +
  • Material for lecture Thursday, August 31
  • +
  • Mathematical Interpretation of Ordinary Least Squares
  • +
  • Residual Error
  • +
  • Simple case
  • +
  • The singular value decomposition
  • +
  • Linear Regression Problems
  • +
  • Fixing the singularity
  • +
  • Basic math of the SVD
  • +
  • The SVD, a Fantastic Algorithm
  • +
  • Economy-size SVD
  • +
  • Codes for the SVD
  • +
  • Note about SVD Calculations
  • +
  • Mathematics of the SVD and implications
  • +
  • Example Matrix
  • +
  • Setting up the Matrix to be inverted
  • +
  • Further properties (important for our analyses later)
  • +
  • Meet the Covariance Matrix
  • +
  • Introducing the Covariance and Correlation functions
  • +
  • Covariance and Correlation Matrix
  • +
  • Correlation Function and Design/Feature Matrix
  • +
  • Covariance Matrix Examples
  • +
  • Correlation Matrix
  • +
  • Correlation Matrix with Pandas
  • +
  • Correlation Matrix with Pandas and the Franke function
  • +
  • Rewriting the Covariance and/or Correlation Matrix
  • +
  • Linking with the SVD
  • +
  • What does it mean?
  • +
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • +
  • Ridge and LASSO Regression
  • +
  • Deriving the Ridge Regression Equations
  • +
  • Interpreting the Ridge results
  • +
  • More interpretations
  • +
  • Deriving the Lasso Regression Equations
  • +
  • Exercises for week 35
  • +
  • Exercise 1: Setting up various Python environments
  • +
  • Exercise 2: making your own data and exploring scikit-learn
  • +
  • Exercise 3: Normalizing our data
  • +
  • Exercise 4: Adding Ridge Regression
  • +
  • Exercise 5: Analytical exercises
  • @@ -429,7 +438,7 @@ so-called 14
  • 15
  • ...
  • -
  • 68
  • +
  • 71
  • »
  • diff --git a/doc/pub/week35/html/._week35-bs006.html b/doc/pub/week35/html/._week35-bs006.html index ebe28a33e..25f1dbadb 100644 --- a/doc/pub/week35/html/._week35-bs006.html +++ b/doc/pub/week35/html/._week35-bs006.html @@ -74,10 +74,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'optimizing-our-parameters'), - ('Our model for the nuclear binding energies', + ('Examples relevant for the exercises', 2, None, - 'our-model-for-the-nuclear-binding-energies'), + 'examples-relevant-for-the-exercises'), ('Optimizing our parameters, more details', 2, None, @@ -94,6 +94,8 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'some-useful-matrix-and-vector-expressions'), + ('The Jacobian', 2, None, 'the-jacobian'), + ('Derivatives, example 1', 2, None, 'derivatives-example-1'), ('Meet the Hessian Matrix', 2, None, 'meet-the-hessian-matrix'), ('Interpretations and optimizing our parameters', 2, @@ -148,6 +150,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'more-preprocessing-examples-franke-function-and-regression'), + ('Material for lecture Thursday, August 31', + 2, + None, + 'material-for-lecture-thursday-august-31'), ('Mathematical Interpretation of Ordinary Least Squares', 2, None, @@ -303,74 +309,77 @@ MathJax.Hub.Config({
  • Why Linear Regression (aka Ordinary Least Squares and family), repeat from last week
  • Regression analysis, overarching aims
  • Regression analysis, overarching aims II
  • -
  • Examples
  • +
  • Examples
  • General linear models
  • Rewriting the fitting procedure as a linear algebra problem
  • Rewriting the fitting procedure as a linear algebra problem, more details
  • Generalizing the fitting procedure as a linear algebra problem
  • Generalizing the fitting procedure as a linear algebra problem
  • Optimizing our parameters
  • -
  • Our model for the nuclear binding energies
  • +
  • Examples relevant for the exercises
  • Optimizing our parameters, more details
  • -
  • Interpretations and optimizing our parameters
  • -
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • Some useful matrix and vector expressions
  • -
  • Meet the Hessian Matrix
  • -
  • Interpretations and optimizing our parameters
  • -
  • Own code for Ordinary Least Squares
  • -
  • Adding error analysis and training set up
  • -
  • Splitting our Data in Training and Test data
  • -
  • Examples
  • -
  • Making your own test-train splitting
  • -
  • The Boston housing data example
  • -
  • Housing data, the code
  • -
  • Reducing the number of degrees of freedom, overarching view
  • -
  • Preprocessing our data
  • -
  • Functionality in Scikit-Learn
  • -
  • More preprocessing
  • -
  • Frequently used scaling functions
  • -
  • Example of own Standard scaling
  • -
  • Min-Max Scaling
  • -
  • Testing the Means Squared Error as function of Complexity
  • -
  • More preprocessing examples, Franke function and regression
  • -
  • Mathematical Interpretation of Ordinary Least Squares
  • -
  • Residual Error
  • -
  • Simple case
  • -
  • The singular value decomposition
  • -
  • Linear Regression Problems
  • -
  • Fixing the singularity
  • -
  • Basic math of the SVD
  • -
  • The SVD, a Fantastic Algorithm
  • -
  • Economy-size SVD
  • -
  • Codes for the SVD
  • -
  • Note about SVD Calculations
  • -
  • Mathematics of the SVD and implications
  • -
  • Example Matrix
  • -
  • Setting up the Matrix to be inverted
  • -
  • Further properties (important for our analyses later)
  • -
  • Meet the Covariance Matrix
  • -
  • Introducing the Covariance and Correlation functions
  • -
  • Covariance and Correlation Matrix
  • -
  • Correlation Function and Design/Feature Matrix
  • -
  • Covariance Matrix Examples
  • -
  • Correlation Matrix
  • -
  • Correlation Matrix with Pandas
  • -
  • Correlation Matrix with Pandas and the Franke function
  • -
  • Rewriting the Covariance and/or Correlation Matrix
  • -
  • Linking with the SVD
  • -
  • What does it mean?
  • -
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • -
  • Ridge and LASSO Regression
  • -
  • Deriving the Ridge Regression Equations
  • -
  • Interpreting the Ridge results
  • -
  • More interpretations
  • -
  • Deriving the Lasso Regression Equations
  • -
  • Exercises for week 35
  • -
  • Exercise 1: Setting up various Python environments
  • -
  • Exercise 2: making your own data and exploring scikit-learn
  • -
  • Exercise 3: Normalizing our data
  • -
  • Exercise 4: Adding Ridge Regression
  • -
  • Exercise 5: Analytical exercises
  • +
  • The Jacobian
  • +
  • Derivatives, example 1
  • +
  • Meet the Hessian Matrix
  • +
  • Interpretations and optimizing our parameters
  • +
  • Own code for Ordinary Least Squares
  • +
  • Adding error analysis and training set up
  • +
  • Splitting our Data in Training and Test data
  • +
  • Examples
  • +
  • Making your own test-train splitting
  • +
  • The Boston housing data example
  • +
  • Housing data, the code
  • +
  • Reducing the number of degrees of freedom, overarching view
  • +
  • Preprocessing our data
  • +
  • Functionality in Scikit-Learn
  • +
  • More preprocessing
  • +
  • Frequently used scaling functions
  • +
  • Example of own Standard scaling
  • +
  • Min-Max Scaling
  • +
  • Testing the Means Squared Error as function of Complexity
  • +
  • More preprocessing examples, Franke function and regression
  • +
  • Material for lecture Thursday, August 31
  • +
  • Mathematical Interpretation of Ordinary Least Squares
  • +
  • Residual Error
  • +
  • Simple case
  • +
  • The singular value decomposition
  • +
  • Linear Regression Problems
  • +
  • Fixing the singularity
  • +
  • Basic math of the SVD
  • +
  • The SVD, a Fantastic Algorithm
  • +
  • Economy-size SVD
  • +
  • Codes for the SVD
  • +
  • Note about SVD Calculations
  • +
  • Mathematics of the SVD and implications
  • +
  • Example Matrix
  • +
  • Setting up the Matrix to be inverted
  • +
  • Further properties (important for our analyses later)
  • +
  • Meet the Covariance Matrix
  • +
  • Introducing the Covariance and Correlation functions
  • +
  • Covariance and Correlation Matrix
  • +
  • Correlation Function and Design/Feature Matrix
  • +
  • Covariance Matrix Examples
  • +
  • Correlation Matrix
  • +
  • Correlation Matrix with Pandas
  • +
  • Correlation Matrix with Pandas and the Franke function
  • +
  • Rewriting the Covariance and/or Correlation Matrix
  • +
  • Linking with the SVD
  • +
  • What does it mean?
  • +
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • +
  • Ridge and LASSO Regression
  • +
  • Deriving the Ridge Regression Equations
  • +
  • Interpreting the Ridge results
  • +
  • More interpretations
  • +
  • Deriving the Lasso Regression Equations
  • +
  • Exercises for week 35
  • +
  • Exercise 1: Setting up various Python environments
  • +
  • Exercise 2: making your own data and exploring scikit-learn
  • +
  • Exercise 3: Normalizing our data
  • +
  • Exercise 4: Adding Ridge Regression
  • +
  • Exercise 5: Analytical exercises
  • @@ -419,7 +428,7 @@ $$
  • 15
  • 16
  • ...
  • -
  • 68
  • +
  • 71
  • »
  • diff --git a/doc/pub/week35/html/._week35-bs007.html b/doc/pub/week35/html/._week35-bs007.html index 0f5a1ee91..921b69887 100644 --- a/doc/pub/week35/html/._week35-bs007.html +++ b/doc/pub/week35/html/._week35-bs007.html @@ -74,10 +74,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'optimizing-our-parameters'), - ('Our model for the nuclear binding energies', + ('Examples relevant for the exercises', 2, None, - 'our-model-for-the-nuclear-binding-energies'), + 'examples-relevant-for-the-exercises'), ('Optimizing our parameters, more details', 2, None, @@ -94,6 +94,8 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'some-useful-matrix-and-vector-expressions'), + ('The Jacobian', 2, None, 'the-jacobian'), + ('Derivatives, example 1', 2, None, 'derivatives-example-1'), ('Meet the Hessian Matrix', 2, None, 'meet-the-hessian-matrix'), ('Interpretations and optimizing our parameters', 2, @@ -148,6 +150,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'more-preprocessing-examples-franke-function-and-regression'), + ('Material for lecture Thursday, August 31', + 2, + None, + 'material-for-lecture-thursday-august-31'), ('Mathematical Interpretation of Ordinary Least Squares', 2, None, @@ -303,74 +309,77 @@ MathJax.Hub.Config({
  • Why Linear Regression (aka Ordinary Least Squares and family), repeat from last week
  • Regression analysis, overarching aims
  • Regression analysis, overarching aims II
  • -
  • Examples
  • +
  • Examples
  • General linear models
  • Rewriting the fitting procedure as a linear algebra problem
  • Rewriting the fitting procedure as a linear algebra problem, more details
  • Generalizing the fitting procedure as a linear algebra problem
  • Generalizing the fitting procedure as a linear algebra problem
  • Optimizing our parameters
  • -
  • Our model for the nuclear binding energies
  • +
  • Examples relevant for the exercises
  • Optimizing our parameters, more details
  • -
  • Interpretations and optimizing our parameters
  • -
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • Some useful matrix and vector expressions
  • -
  • Meet the Hessian Matrix
  • -
  • Interpretations and optimizing our parameters
  • -
  • Own code for Ordinary Least Squares
  • -
  • Adding error analysis and training set up
  • -
  • Splitting our Data in Training and Test data
  • -
  • Examples
  • -
  • Making your own test-train splitting
  • -
  • The Boston housing data example
  • -
  • Housing data, the code
  • -
  • Reducing the number of degrees of freedom, overarching view
  • -
  • Preprocessing our data
  • -
  • Functionality in Scikit-Learn
  • -
  • More preprocessing
  • -
  • Frequently used scaling functions
  • -
  • Example of own Standard scaling
  • -
  • Min-Max Scaling
  • -
  • Testing the Means Squared Error as function of Complexity
  • -
  • More preprocessing examples, Franke function and regression
  • -
  • Mathematical Interpretation of Ordinary Least Squares
  • -
  • Residual Error
  • -
  • Simple case
  • -
  • The singular value decomposition
  • -
  • Linear Regression Problems
  • -
  • Fixing the singularity
  • -
  • Basic math of the SVD
  • -
  • The SVD, a Fantastic Algorithm
  • -
  • Economy-size SVD
  • -
  • Codes for the SVD
  • -
  • Note about SVD Calculations
  • -
  • Mathematics of the SVD and implications
  • -
  • Example Matrix
  • -
  • Setting up the Matrix to be inverted
  • -
  • Further properties (important for our analyses later)
  • -
  • Meet the Covariance Matrix
  • -
  • Introducing the Covariance and Correlation functions
  • -
  • Covariance and Correlation Matrix
  • -
  • Correlation Function and Design/Feature Matrix
  • -
  • Covariance Matrix Examples
  • -
  • Correlation Matrix
  • -
  • Correlation Matrix with Pandas
  • -
  • Correlation Matrix with Pandas and the Franke function
  • -
  • Rewriting the Covariance and/or Correlation Matrix
  • -
  • Linking with the SVD
  • -
  • What does it mean?
  • -
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • -
  • Ridge and LASSO Regression
  • -
  • Deriving the Ridge Regression Equations
  • -
  • Interpreting the Ridge results
  • -
  • More interpretations
  • -
  • Deriving the Lasso Regression Equations
  • -
  • Exercises for week 35
  • -
  • Exercise 1: Setting up various Python environments
  • -
  • Exercise 2: making your own data and exploring scikit-learn
  • -
  • Exercise 3: Normalizing our data
  • -
  • Exercise 4: Adding Ridge Regression
  • -
  • Exercise 5: Analytical exercises
  • +
  • The Jacobian
  • +
  • Derivatives, example 1
  • +
  • Meet the Hessian Matrix
  • +
  • Interpretations and optimizing our parameters
  • +
  • Own code for Ordinary Least Squares
  • +
  • Adding error analysis and training set up
  • +
  • Splitting our Data in Training and Test data
  • +
  • Examples
  • +
  • Making your own test-train splitting
  • +
  • The Boston housing data example
  • +
  • Housing data, the code
  • +
  • Reducing the number of degrees of freedom, overarching view
  • +
  • Preprocessing our data
  • +
  • Functionality in Scikit-Learn
  • +
  • More preprocessing
  • +
  • Frequently used scaling functions
  • +
  • Example of own Standard scaling
  • +
  • Min-Max Scaling
  • +
  • Testing the Means Squared Error as function of Complexity
  • +
  • More preprocessing examples, Franke function and regression
  • +
  • Material for lecture Thursday, August 31
  • +
  • Mathematical Interpretation of Ordinary Least Squares
  • +
  • Residual Error
  • +
  • Simple case
  • +
  • The singular value decomposition
  • +
  • Linear Regression Problems
  • +
  • Fixing the singularity
  • +
  • Basic math of the SVD
  • +
  • The SVD, a Fantastic Algorithm
  • +
  • Economy-size SVD
  • +
  • Codes for the SVD
  • +
  • Note about SVD Calculations
  • +
  • Mathematics of the SVD and implications
  • +
  • Example Matrix
  • +
  • Setting up the Matrix to be inverted
  • +
  • Further properties (important for our analyses later)
  • +
  • Meet the Covariance Matrix
  • +
  • Introducing the Covariance and Correlation functions
  • +
  • Covariance and Correlation Matrix
  • +
  • Correlation Function and Design/Feature Matrix
  • +
  • Covariance Matrix Examples
  • +
  • Correlation Matrix
  • +
  • Correlation Matrix with Pandas
  • +
  • Correlation Matrix with Pandas and the Franke function
  • +
  • Rewriting the Covariance and/or Correlation Matrix
  • +
  • Linking with the SVD
  • +
  • What does it mean?
  • +
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • +
  • Ridge and LASSO Regression
  • +
  • Deriving the Ridge Regression Equations
  • +
  • Interpreting the Ridge results
  • +
  • More interpretations
  • +
  • Deriving the Lasso Regression Equations
  • +
  • Exercises for week 35
  • +
  • Exercise 1: Setting up various Python environments
  • +
  • Exercise 2: making your own data and exploring scikit-learn
  • +
  • Exercise 3: Normalizing our data
  • +
  • Exercise 4: Adding Ridge Regression
  • +
  • Exercise 5: Analytical exercises
  • @@ -422,7 +431,7 @@ $$
  • 16
  • 17
  • ...
  • -
  • 68
  • +
  • 71
  • »
  • diff --git a/doc/pub/week35/html/._week35-bs008.html b/doc/pub/week35/html/._week35-bs008.html index 89791fcec..5e2912090 100644 --- a/doc/pub/week35/html/._week35-bs008.html +++ b/doc/pub/week35/html/._week35-bs008.html @@ -74,10 +74,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'optimizing-our-parameters'), - ('Our model for the nuclear binding energies', + ('Examples relevant for the exercises', 2, None, - 'our-model-for-the-nuclear-binding-energies'), + 'examples-relevant-for-the-exercises'), ('Optimizing our parameters, more details', 2, None, @@ -94,6 +94,8 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'some-useful-matrix-and-vector-expressions'), + ('The Jacobian', 2, None, 'the-jacobian'), + ('Derivatives, example 1', 2, None, 'derivatives-example-1'), ('Meet the Hessian Matrix', 2, None, 'meet-the-hessian-matrix'), ('Interpretations and optimizing our parameters', 2, @@ -148,6 +150,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'more-preprocessing-examples-franke-function-and-regression'), + ('Material for lecture Thursday, August 31', + 2, + None, + 'material-for-lecture-thursday-august-31'), ('Mathematical Interpretation of Ordinary Least Squares', 2, None, @@ -303,74 +309,77 @@ MathJax.Hub.Config({
  • Why Linear Regression (aka Ordinary Least Squares and family), repeat from last week
  • Regression analysis, overarching aims
  • Regression analysis, overarching aims II
  • -
  • Examples
  • +
  • Examples
  • General linear models
  • Rewriting the fitting procedure as a linear algebra problem
  • Rewriting the fitting procedure as a linear algebra problem, more details
  • Generalizing the fitting procedure as a linear algebra problem
  • Generalizing the fitting procedure as a linear algebra problem
  • Optimizing our parameters
  • -
  • Our model for the nuclear binding energies
  • +
  • Examples relevant for the exercises
  • Optimizing our parameters, more details
  • -
  • Interpretations and optimizing our parameters
  • -
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • Some useful matrix and vector expressions
  • -
  • Meet the Hessian Matrix
  • -
  • Interpretations and optimizing our parameters
  • -
  • Own code for Ordinary Least Squares
  • -
  • Adding error analysis and training set up
  • -
  • Splitting our Data in Training and Test data
  • -
  • Examples
  • -
  • Making your own test-train splitting
  • -
  • The Boston housing data example
  • -
  • Housing data, the code
  • -
  • Reducing the number of degrees of freedom, overarching view
  • -
  • Preprocessing our data
  • -
  • Functionality in Scikit-Learn
  • -
  • More preprocessing
  • -
  • Frequently used scaling functions
  • -
  • Example of own Standard scaling
  • -
  • Min-Max Scaling
  • -
  • Testing the Means Squared Error as function of Complexity
  • -
  • More preprocessing examples, Franke function and regression
  • -
  • Mathematical Interpretation of Ordinary Least Squares
  • -
  • Residual Error
  • -
  • Simple case
  • -
  • The singular value decomposition
  • -
  • Linear Regression Problems
  • -
  • Fixing the singularity
  • -
  • Basic math of the SVD
  • -
  • The SVD, a Fantastic Algorithm
  • -
  • Economy-size SVD
  • -
  • Codes for the SVD
  • -
  • Note about SVD Calculations
  • -
  • Mathematics of the SVD and implications
  • -
  • Example Matrix
  • -
  • Setting up the Matrix to be inverted
  • -
  • Further properties (important for our analyses later)
  • -
  • Meet the Covariance Matrix
  • -
  • Introducing the Covariance and Correlation functions
  • -
  • Covariance and Correlation Matrix
  • -
  • Correlation Function and Design/Feature Matrix
  • -
  • Covariance Matrix Examples
  • -
  • Correlation Matrix
  • -
  • Correlation Matrix with Pandas
  • -
  • Correlation Matrix with Pandas and the Franke function
  • -
  • Rewriting the Covariance and/or Correlation Matrix
  • -
  • Linking with the SVD
  • -
  • What does it mean?
  • -
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • -
  • Ridge and LASSO Regression
  • -
  • Deriving the Ridge Regression Equations
  • -
  • Interpreting the Ridge results
  • -
  • More interpretations
  • -
  • Deriving the Lasso Regression Equations
  • -
  • Exercises for week 35
  • -
  • Exercise 1: Setting up various Python environments
  • -
  • Exercise 2: making your own data and exploring scikit-learn
  • -
  • Exercise 3: Normalizing our data
  • -
  • Exercise 4: Adding Ridge Regression
  • -
  • Exercise 5: Analytical exercises
  • +
  • The Jacobian
  • +
  • Derivatives, example 1
  • +
  • Meet the Hessian Matrix
  • +
  • Interpretations and optimizing our parameters
  • +
  • Own code for Ordinary Least Squares
  • +
  • Adding error analysis and training set up
  • +
  • Splitting our Data in Training and Test data
  • +
  • Examples
  • +
  • Making your own test-train splitting
  • +
  • The Boston housing data example
  • +
  • Housing data, the code
  • +
  • Reducing the number of degrees of freedom, overarching view
  • +
  • Preprocessing our data
  • +
  • Functionality in Scikit-Learn
  • +
  • More preprocessing
  • +
  • Frequently used scaling functions
  • +
  • Example of own Standard scaling
  • +
  • Min-Max Scaling
  • +
  • Testing the Means Squared Error as function of Complexity
  • +
  • More preprocessing examples, Franke function and regression
  • +
  • Material for lecture Thursday, August 31
  • +
  • Mathematical Interpretation of Ordinary Least Squares
  • +
  • Residual Error
  • +
  • Simple case
  • +
  • The singular value decomposition
  • +
  • Linear Regression Problems
  • +
  • Fixing the singularity
  • +
  • Basic math of the SVD
  • +
  • The SVD, a Fantastic Algorithm
  • +
  • Economy-size SVD
  • +
  • Codes for the SVD
  • +
  • Note about SVD Calculations
  • +
  • Mathematics of the SVD and implications
  • +
  • Example Matrix
  • +
  • Setting up the Matrix to be inverted
  • +
  • Further properties (important for our analyses later)
  • +
  • Meet the Covariance Matrix
  • +
  • Introducing the Covariance and Correlation functions
  • +
  • Covariance and Correlation Matrix
  • +
  • Correlation Function and Design/Feature Matrix
  • +
  • Covariance Matrix Examples
  • +
  • Correlation Matrix
  • +
  • Correlation Matrix with Pandas
  • +
  • Correlation Matrix with Pandas and the Franke function
  • +
  • Rewriting the Covariance and/or Correlation Matrix
  • +
  • Linking with the SVD
  • +
  • What does it mean?
  • +
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • +
  • Ridge and LASSO Regression
  • +
  • Deriving the Ridge Regression Equations
  • +
  • Interpreting the Ridge results
  • +
  • More interpretations
  • +
  • Deriving the Lasso Regression Equations
  • +
  • Exercises for week 35
  • +
  • Exercise 1: Setting up various Python environments
  • +
  • Exercise 2: making your own data and exploring scikit-learn
  • +
  • Exercise 3: Normalizing our data
  • +
  • Exercise 4: Adding Ridge Regression
  • +
  • Exercise 5: Analytical exercises
  • @@ -446,7 +455,7 @@ $$
  • 17
  • 18
  • ...
  • -
  • 68
  • +
  • 71
  • »
  • diff --git a/doc/pub/week35/html/._week35-bs009.html b/doc/pub/week35/html/._week35-bs009.html index dbc73c491..4b5791b80 100644 --- a/doc/pub/week35/html/._week35-bs009.html +++ b/doc/pub/week35/html/._week35-bs009.html @@ -74,10 +74,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'optimizing-our-parameters'), - ('Our model for the nuclear binding energies', + ('Examples relevant for the exercises', 2, None, - 'our-model-for-the-nuclear-binding-energies'), + 'examples-relevant-for-the-exercises'), ('Optimizing our parameters, more details', 2, None, @@ -94,6 +94,8 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'some-useful-matrix-and-vector-expressions'), + ('The Jacobian', 2, None, 'the-jacobian'), + ('Derivatives, example 1', 2, None, 'derivatives-example-1'), ('Meet the Hessian Matrix', 2, None, 'meet-the-hessian-matrix'), ('Interpretations and optimizing our parameters', 2, @@ -148,6 +150,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'more-preprocessing-examples-franke-function-and-regression'), + ('Material for lecture Thursday, August 31', + 2, + None, + 'material-for-lecture-thursday-august-31'), ('Mathematical Interpretation of Ordinary Least Squares', 2, None, @@ -303,74 +309,77 @@ MathJax.Hub.Config({
  • Why Linear Regression (aka Ordinary Least Squares and family), repeat from last week
  • Regression analysis, overarching aims
  • Regression analysis, overarching aims II
  • -
  • Examples
  • +
  • Examples
  • General linear models
  • Rewriting the fitting procedure as a linear algebra problem
  • Rewriting the fitting procedure as a linear algebra problem, more details
  • Generalizing the fitting procedure as a linear algebra problem
  • Generalizing the fitting procedure as a linear algebra problem
  • Optimizing our parameters
  • -
  • Our model for the nuclear binding energies
  • +
  • Examples relevant for the exercises
  • Optimizing our parameters, more details
  • -
  • Interpretations and optimizing our parameters
  • -
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • Some useful matrix and vector expressions
  • -
  • Meet the Hessian Matrix
  • -
  • Interpretations and optimizing our parameters
  • -
  • Own code for Ordinary Least Squares
  • -
  • Adding error analysis and training set up
  • -
  • Splitting our Data in Training and Test data
  • -
  • Examples
  • -
  • Making your own test-train splitting
  • -
  • The Boston housing data example
  • -
  • Housing data, the code
  • -
  • Reducing the number of degrees of freedom, overarching view
  • -
  • Preprocessing our data
  • -
  • Functionality in Scikit-Learn
  • -
  • More preprocessing
  • -
  • Frequently used scaling functions
  • -
  • Example of own Standard scaling
  • -
  • Min-Max Scaling
  • -
  • Testing the Means Squared Error as function of Complexity
  • -
  • More preprocessing examples, Franke function and regression
  • -
  • Mathematical Interpretation of Ordinary Least Squares
  • -
  • Residual Error
  • -
  • Simple case
  • -
  • The singular value decomposition
  • -
  • Linear Regression Problems
  • -
  • Fixing the singularity
  • -
  • Basic math of the SVD
  • -
  • The SVD, a Fantastic Algorithm
  • -
  • Economy-size SVD
  • -
  • Codes for the SVD
  • -
  • Note about SVD Calculations
  • -
  • Mathematics of the SVD and implications
  • -
  • Example Matrix
  • -
  • Setting up the Matrix to be inverted
  • -
  • Further properties (important for our analyses later)
  • -
  • Meet the Covariance Matrix
  • -
  • Introducing the Covariance and Correlation functions
  • -
  • Covariance and Correlation Matrix
  • -
  • Correlation Function and Design/Feature Matrix
  • -
  • Covariance Matrix Examples
  • -
  • Correlation Matrix
  • -
  • Correlation Matrix with Pandas
  • -
  • Correlation Matrix with Pandas and the Franke function
  • -
  • Rewriting the Covariance and/or Correlation Matrix
  • -
  • Linking with the SVD
  • -
  • What does it mean?
  • -
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • -
  • Ridge and LASSO Regression
  • -
  • Deriving the Ridge Regression Equations
  • -
  • Interpreting the Ridge results
  • -
  • More interpretations
  • -
  • Deriving the Lasso Regression Equations
  • -
  • Exercises for week 35
  • -
  • Exercise 1: Setting up various Python environments
  • -
  • Exercise 2: making your own data and exploring scikit-learn
  • -
  • Exercise 3: Normalizing our data
  • -
  • Exercise 4: Adding Ridge Regression
  • -
  • Exercise 5: Analytical exercises
  • +
  • The Jacobian
  • +
  • Derivatives, example 1
  • +
  • Meet the Hessian Matrix
  • +
  • Interpretations and optimizing our parameters
  • +
  • Own code for Ordinary Least Squares
  • +
  • Adding error analysis and training set up
  • +
  • Splitting our Data in Training and Test data
  • +
  • Examples
  • +
  • Making your own test-train splitting
  • +
  • The Boston housing data example
  • +
  • Housing data, the code
  • +
  • Reducing the number of degrees of freedom, overarching view
  • +
  • Preprocessing our data
  • +
  • Functionality in Scikit-Learn
  • +
  • More preprocessing
  • +
  • Frequently used scaling functions
  • +
  • Example of own Standard scaling
  • +
  • Min-Max Scaling
  • +
  • Testing the Means Squared Error as function of Complexity
  • +
  • More preprocessing examples, Franke function and regression
  • +
  • Material for lecture Thursday, August 31
  • +
  • Mathematical Interpretation of Ordinary Least Squares
  • +
  • Residual Error
  • +
  • Simple case
  • +
  • The singular value decomposition
  • +
  • Linear Regression Problems
  • +
  • Fixing the singularity
  • +
  • Basic math of the SVD
  • +
  • The SVD, a Fantastic Algorithm
  • +
  • Economy-size SVD
  • +
  • Codes for the SVD
  • +
  • Note about SVD Calculations
  • +
  • Mathematics of the SVD and implications
  • +
  • Example Matrix
  • +
  • Setting up the Matrix to be inverted
  • +
  • Further properties (important for our analyses later)
  • +
  • Meet the Covariance Matrix
  • +
  • Introducing the Covariance and Correlation functions
  • +
  • Covariance and Correlation Matrix
  • +
  • Correlation Function and Design/Feature Matrix
  • +
  • Covariance Matrix Examples
  • +
  • Correlation Matrix
  • +
  • Correlation Matrix with Pandas
  • +
  • Correlation Matrix with Pandas and the Franke function
  • +
  • Rewriting the Covariance and/or Correlation Matrix
  • +
  • Linking with the SVD
  • +
  • What does it mean?
  • +
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • +
  • Ridge and LASSO Regression
  • +
  • Deriving the Ridge Regression Equations
  • +
  • Interpreting the Ridge results
  • +
  • More interpretations
  • +
  • Deriving the Lasso Regression Equations
  • +
  • Exercises for week 35
  • +
  • Exercise 1: Setting up various Python environments
  • +
  • Exercise 2: making your own data and exploring scikit-learn
  • +
  • Exercise 3: Normalizing our data
  • +
  • Exercise 4: Adding Ridge Regression
  • +
  • Exercise 5: Analytical exercises
  • @@ -436,7 +445,7 @@ $$
  • 18
  • 19
  • ...
  • -
  • 68
  • +
  • 71
  • »
  • diff --git a/doc/pub/week35/html/._week35-bs010.html b/doc/pub/week35/html/._week35-bs010.html index 50df538a8..c788516b0 100644 --- a/doc/pub/week35/html/._week35-bs010.html +++ b/doc/pub/week35/html/._week35-bs010.html @@ -74,10 +74,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'optimizing-our-parameters'), - ('Our model for the nuclear binding energies', + ('Examples relevant for the exercises', 2, None, - 'our-model-for-the-nuclear-binding-energies'), + 'examples-relevant-for-the-exercises'), ('Optimizing our parameters, more details', 2, None, @@ -94,6 +94,8 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'some-useful-matrix-and-vector-expressions'), + ('The Jacobian', 2, None, 'the-jacobian'), + ('Derivatives, example 1', 2, None, 'derivatives-example-1'), ('Meet the Hessian Matrix', 2, None, 'meet-the-hessian-matrix'), ('Interpretations and optimizing our parameters', 2, @@ -148,6 +150,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'more-preprocessing-examples-franke-function-and-regression'), + ('Material for lecture Thursday, August 31', + 2, + None, + 'material-for-lecture-thursday-august-31'), ('Mathematical Interpretation of Ordinary Least Squares', 2, None, @@ -303,74 +309,77 @@ MathJax.Hub.Config({
  • Why Linear Regression (aka Ordinary Least Squares and family), repeat from last week
  • Regression analysis, overarching aims
  • Regression analysis, overarching aims II
  • -
  • Examples
  • +
  • Examples
  • General linear models
  • Rewriting the fitting procedure as a linear algebra problem
  • Rewriting the fitting procedure as a linear algebra problem, more details
  • Generalizing the fitting procedure as a linear algebra problem
  • Generalizing the fitting procedure as a linear algebra problem
  • Optimizing our parameters
  • -
  • Our model for the nuclear binding energies
  • +
  • Examples relevant for the exercises
  • Optimizing our parameters, more details
  • -
  • Interpretations and optimizing our parameters
  • -
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • Some useful matrix and vector expressions
  • -
  • Meet the Hessian Matrix
  • -
  • Interpretations and optimizing our parameters
  • -
  • Own code for Ordinary Least Squares
  • -
  • Adding error analysis and training set up
  • -
  • Splitting our Data in Training and Test data
  • -
  • Examples
  • -
  • Making your own test-train splitting
  • -
  • The Boston housing data example
  • -
  • Housing data, the code
  • -
  • Reducing the number of degrees of freedom, overarching view
  • -
  • Preprocessing our data
  • -
  • Functionality in Scikit-Learn
  • -
  • More preprocessing
  • -
  • Frequently used scaling functions
  • -
  • Example of own Standard scaling
  • -
  • Min-Max Scaling
  • -
  • Testing the Means Squared Error as function of Complexity
  • -
  • More preprocessing examples, Franke function and regression
  • -
  • Mathematical Interpretation of Ordinary Least Squares
  • -
  • Residual Error
  • -
  • Simple case
  • -
  • The singular value decomposition
  • -
  • Linear Regression Problems
  • -
  • Fixing the singularity
  • -
  • Basic math of the SVD
  • -
  • The SVD, a Fantastic Algorithm
  • -
  • Economy-size SVD
  • -
  • Codes for the SVD
  • -
  • Note about SVD Calculations
  • -
  • Mathematics of the SVD and implications
  • -
  • Example Matrix
  • -
  • Setting up the Matrix to be inverted
  • -
  • Further properties (important for our analyses later)
  • -
  • Meet the Covariance Matrix
  • -
  • Introducing the Covariance and Correlation functions
  • -
  • Covariance and Correlation Matrix
  • -
  • Correlation Function and Design/Feature Matrix
  • -
  • Covariance Matrix Examples
  • -
  • Correlation Matrix
  • -
  • Correlation Matrix with Pandas
  • -
  • Correlation Matrix with Pandas and the Franke function
  • -
  • Rewriting the Covariance and/or Correlation Matrix
  • -
  • Linking with the SVD
  • -
  • What does it mean?
  • -
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • -
  • Ridge and LASSO Regression
  • -
  • Deriving the Ridge Regression Equations
  • -
  • Interpreting the Ridge results
  • -
  • More interpretations
  • -
  • Deriving the Lasso Regression Equations
  • -
  • Exercises for week 35
  • -
  • Exercise 1: Setting up various Python environments
  • -
  • Exercise 2: making your own data and exploring scikit-learn
  • -
  • Exercise 3: Normalizing our data
  • -
  • Exercise 4: Adding Ridge Regression
  • -
  • Exercise 5: Analytical exercises
  • +
  • The Jacobian
  • +
  • Derivatives, example 1
  • +
  • Meet the Hessian Matrix
  • +
  • Interpretations and optimizing our parameters
  • +
  • Own code for Ordinary Least Squares
  • +
  • Adding error analysis and training set up
  • +
  • Splitting our Data in Training and Test data
  • +
  • Examples
  • +
  • Making your own test-train splitting
  • +
  • The Boston housing data example
  • +
  • Housing data, the code
  • +
  • Reducing the number of degrees of freedom, overarching view
  • +
  • Preprocessing our data
  • +
  • Functionality in Scikit-Learn
  • +
  • More preprocessing
  • +
  • Frequently used scaling functions
  • +
  • Example of own Standard scaling
  • +
  • Min-Max Scaling
  • +
  • Testing the Means Squared Error as function of Complexity
  • +
  • More preprocessing examples, Franke function and regression
  • +
  • Material for lecture Thursday, August 31
  • +
  • Mathematical Interpretation of Ordinary Least Squares
  • +
  • Residual Error
  • +
  • Simple case
  • +
  • The singular value decomposition
  • +
  • Linear Regression Problems
  • +
  • Fixing the singularity
  • +
  • Basic math of the SVD
  • +
  • The SVD, a Fantastic Algorithm
  • +
  • Economy-size SVD
  • +
  • Codes for the SVD
  • +
  • Note about SVD Calculations
  • +
  • Mathematics of the SVD and implications
  • +
  • Example Matrix
  • +
  • Setting up the Matrix to be inverted
  • +
  • Further properties (important for our analyses later)
  • +
  • Meet the Covariance Matrix
  • +
  • Introducing the Covariance and Correlation functions
  • +
  • Covariance and Correlation Matrix
  • +
  • Correlation Function and Design/Feature Matrix
  • +
  • Covariance Matrix Examples
  • +
  • Correlation Matrix
  • +
  • Correlation Matrix with Pandas
  • +
  • Correlation Matrix with Pandas and the Franke function
  • +
  • Rewriting the Covariance and/or Correlation Matrix
  • +
  • Linking with the SVD
  • +
  • What does it mean?
  • +
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • +
  • Ridge and LASSO Regression
  • +
  • Deriving the Ridge Regression Equations
  • +
  • Interpreting the Ridge results
  • +
  • More interpretations
  • +
  • Deriving the Lasso Regression Equations
  • +
  • Exercises for week 35
  • +
  • Exercise 1: Setting up various Python environments
  • +
  • Exercise 2: making your own data and exploring scikit-learn
  • +
  • Exercise 3: Normalizing our data
  • +
  • Exercise 4: Adding Ridge Regression
  • +
  • Exercise 5: Analytical exercises
  • @@ -433,7 +442,7 @@ $$
  • 19
  • 20
  • ...
  • -
  • 68
  • +
  • 71
  • »
  • diff --git a/doc/pub/week35/html/._week35-bs011.html b/doc/pub/week35/html/._week35-bs011.html index 3dc9f73f1..61e67ef08 100644 --- a/doc/pub/week35/html/._week35-bs011.html +++ b/doc/pub/week35/html/._week35-bs011.html @@ -74,10 +74,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'optimizing-our-parameters'), - ('Our model for the nuclear binding energies', + ('Examples relevant for the exercises', 2, None, - 'our-model-for-the-nuclear-binding-energies'), + 'examples-relevant-for-the-exercises'), ('Optimizing our parameters, more details', 2, None, @@ -94,6 +94,8 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'some-useful-matrix-and-vector-expressions'), + ('The Jacobian', 2, None, 'the-jacobian'), + ('Derivatives, example 1', 2, None, 'derivatives-example-1'), ('Meet the Hessian Matrix', 2, None, 'meet-the-hessian-matrix'), ('Interpretations and optimizing our parameters', 2, @@ -148,6 +150,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'more-preprocessing-examples-franke-function-and-regression'), + ('Material for lecture Thursday, August 31', + 2, + None, + 'material-for-lecture-thursday-august-31'), ('Mathematical Interpretation of Ordinary Least Squares', 2, None, @@ -303,74 +309,77 @@ MathJax.Hub.Config({
  • Why Linear Regression (aka Ordinary Least Squares and family), repeat from last week
  • Regression analysis, overarching aims
  • Regression analysis, overarching aims II
  • -
  • Examples
  • +
  • Examples
  • General linear models
  • Rewriting the fitting procedure as a linear algebra problem
  • Rewriting the fitting procedure as a linear algebra problem, more details
  • Generalizing the fitting procedure as a linear algebra problem
  • Generalizing the fitting procedure as a linear algebra problem
  • Optimizing our parameters
  • -
  • Our model for the nuclear binding energies
  • +
  • Examples relevant for the exercises
  • Optimizing our parameters, more details
  • -
  • Interpretations and optimizing our parameters
  • -
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • Some useful matrix and vector expressions
  • -
  • Meet the Hessian Matrix
  • -
  • Interpretations and optimizing our parameters
  • -
  • Own code for Ordinary Least Squares
  • -
  • Adding error analysis and training set up
  • -
  • Splitting our Data in Training and Test data
  • -
  • Examples
  • -
  • Making your own test-train splitting
  • -
  • The Boston housing data example
  • -
  • Housing data, the code
  • -
  • Reducing the number of degrees of freedom, overarching view
  • -
  • Preprocessing our data
  • -
  • Functionality in Scikit-Learn
  • -
  • More preprocessing
  • -
  • Frequently used scaling functions
  • -
  • Example of own Standard scaling
  • -
  • Min-Max Scaling
  • -
  • Testing the Means Squared Error as function of Complexity
  • -
  • More preprocessing examples, Franke function and regression
  • -
  • Mathematical Interpretation of Ordinary Least Squares
  • -
  • Residual Error
  • -
  • Simple case
  • -
  • The singular value decomposition
  • -
  • Linear Regression Problems
  • -
  • Fixing the singularity
  • -
  • Basic math of the SVD
  • -
  • The SVD, a Fantastic Algorithm
  • -
  • Economy-size SVD
  • -
  • Codes for the SVD
  • -
  • Note about SVD Calculations
  • -
  • Mathematics of the SVD and implications
  • -
  • Example Matrix
  • -
  • Setting up the Matrix to be inverted
  • -
  • Further properties (important for our analyses later)
  • -
  • Meet the Covariance Matrix
  • -
  • Introducing the Covariance and Correlation functions
  • -
  • Covariance and Correlation Matrix
  • -
  • Correlation Function and Design/Feature Matrix
  • -
  • Covariance Matrix Examples
  • -
  • Correlation Matrix
  • -
  • Correlation Matrix with Pandas
  • -
  • Correlation Matrix with Pandas and the Franke function
  • -
  • Rewriting the Covariance and/or Correlation Matrix
  • -
  • Linking with the SVD
  • -
  • What does it mean?
  • -
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • -
  • Ridge and LASSO Regression
  • -
  • Deriving the Ridge Regression Equations
  • -
  • Interpreting the Ridge results
  • -
  • More interpretations
  • -
  • Deriving the Lasso Regression Equations
  • -
  • Exercises for week 35
  • -
  • Exercise 1: Setting up various Python environments
  • -
  • Exercise 2: making your own data and exploring scikit-learn
  • -
  • Exercise 3: Normalizing our data
  • -
  • Exercise 4: Adding Ridge Regression
  • -
  • Exercise 5: Analytical exercises
  • +
  • The Jacobian
  • +
  • Derivatives, example 1
  • +
  • Meet the Hessian Matrix
  • +
  • Interpretations and optimizing our parameters
  • +
  • Own code for Ordinary Least Squares
  • +
  • Adding error analysis and training set up
  • +
  • Splitting our Data in Training and Test data
  • +
  • Examples
  • +
  • Making your own test-train splitting
  • +
  • The Boston housing data example
  • +
  • Housing data, the code
  • +
  • Reducing the number of degrees of freedom, overarching view
  • +
  • Preprocessing our data
  • +
  • Functionality in Scikit-Learn
  • +
  • More preprocessing
  • +
  • Frequently used scaling functions
  • +
  • Example of own Standard scaling
  • +
  • Min-Max Scaling
  • +
  • Testing the Means Squared Error as function of Complexity
  • +
  • More preprocessing examples, Franke function and regression
  • +
  • Material for lecture Thursday, August 31
  • +
  • Mathematical Interpretation of Ordinary Least Squares
  • +
  • Residual Error
  • +
  • Simple case
  • +
  • The singular value decomposition
  • +
  • Linear Regression Problems
  • +
  • Fixing the singularity
  • +
  • Basic math of the SVD
  • +
  • The SVD, a Fantastic Algorithm
  • +
  • Economy-size SVD
  • +
  • Codes for the SVD
  • +
  • Note about SVD Calculations
  • +
  • Mathematics of the SVD and implications
  • +
  • Example Matrix
  • +
  • Setting up the Matrix to be inverted
  • +
  • Further properties (important for our analyses later)
  • +
  • Meet the Covariance Matrix
  • +
  • Introducing the Covariance and Correlation functions
  • +
  • Covariance and Correlation Matrix
  • +
  • Correlation Function and Design/Feature Matrix
  • +
  • Covariance Matrix Examples
  • +
  • Correlation Matrix
  • +
  • Correlation Matrix with Pandas
  • +
  • Correlation Matrix with Pandas and the Franke function
  • +
  • Rewriting the Covariance and/or Correlation Matrix
  • +
  • Linking with the SVD
  • +
  • What does it mean?
  • +
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • +
  • Ridge and LASSO Regression
  • +
  • Deriving the Ridge Regression Equations
  • +
  • Interpreting the Ridge results
  • +
  • More interpretations
  • +
  • Deriving the Lasso Regression Equations
  • +
  • Exercises for week 35
  • +
  • Exercise 1: Setting up various Python environments
  • +
  • Exercise 2: making your own data and exploring scikit-learn
  • +
  • Exercise 3: Normalizing our data
  • +
  • Exercise 4: Adding Ridge Regression
  • +
  • Exercise 5: Analytical exercises
  • @@ -432,7 +441,7 @@ our matrix as \( \boldsymbol{X}\in {\mathbb{R}}^{n\times p} \), with the predict
  • 20
  • 21
  • ...
  • -
  • 68
  • +
  • 71
  • »
  • diff --git a/doc/pub/week35/html/._week35-bs012.html b/doc/pub/week35/html/._week35-bs012.html index ed3a6cbee..7c3c4aa85 100644 --- a/doc/pub/week35/html/._week35-bs012.html +++ b/doc/pub/week35/html/._week35-bs012.html @@ -74,10 +74,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'optimizing-our-parameters'), - ('Our model for the nuclear binding energies', + ('Examples relevant for the exercises', 2, None, - 'our-model-for-the-nuclear-binding-energies'), + 'examples-relevant-for-the-exercises'), ('Optimizing our parameters, more details', 2, None, @@ -94,6 +94,8 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'some-useful-matrix-and-vector-expressions'), + ('The Jacobian', 2, None, 'the-jacobian'), + ('Derivatives, example 1', 2, None, 'derivatives-example-1'), ('Meet the Hessian Matrix', 2, None, 'meet-the-hessian-matrix'), ('Interpretations and optimizing our parameters', 2, @@ -148,6 +150,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'more-preprocessing-examples-franke-function-and-regression'), + ('Material for lecture Thursday, August 31', + 2, + None, + 'material-for-lecture-thursday-august-31'), ('Mathematical Interpretation of Ordinary Least Squares', 2, None, @@ -303,74 +309,77 @@ MathJax.Hub.Config({
  • Why Linear Regression (aka Ordinary Least Squares and family), repeat from last week
  • Regression analysis, overarching aims
  • Regression analysis, overarching aims II
  • -
  • Examples
  • +
  • Examples
  • General linear models
  • Rewriting the fitting procedure as a linear algebra problem
  • Rewriting the fitting procedure as a linear algebra problem, more details
  • Generalizing the fitting procedure as a linear algebra problem
  • Generalizing the fitting procedure as a linear algebra problem
  • Optimizing our parameters
  • -
  • Our model for the nuclear binding energies
  • +
  • Examples relevant for the exercises
  • Optimizing our parameters, more details
  • -
  • Interpretations and optimizing our parameters
  • -
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • Some useful matrix and vector expressions
  • -
  • Meet the Hessian Matrix
  • -
  • Interpretations and optimizing our parameters
  • -
  • Own code for Ordinary Least Squares
  • -
  • Adding error analysis and training set up
  • -
  • Splitting our Data in Training and Test data
  • -
  • Examples
  • -
  • Making your own test-train splitting
  • -
  • The Boston housing data example
  • -
  • Housing data, the code
  • -
  • Reducing the number of degrees of freedom, overarching view
  • -
  • Preprocessing our data
  • -
  • Functionality in Scikit-Learn
  • -
  • More preprocessing
  • -
  • Frequently used scaling functions
  • -
  • Example of own Standard scaling
  • -
  • Min-Max Scaling
  • -
  • Testing the Means Squared Error as function of Complexity
  • -
  • More preprocessing examples, Franke function and regression
  • -
  • Mathematical Interpretation of Ordinary Least Squares
  • -
  • Residual Error
  • -
  • Simple case
  • -
  • The singular value decomposition
  • -
  • Linear Regression Problems
  • -
  • Fixing the singularity
  • -
  • Basic math of the SVD
  • -
  • The SVD, a Fantastic Algorithm
  • -
  • Economy-size SVD
  • -
  • Codes for the SVD
  • -
  • Note about SVD Calculations
  • -
  • Mathematics of the SVD and implications
  • -
  • Example Matrix
  • -
  • Setting up the Matrix to be inverted
  • -
  • Further properties (important for our analyses later)
  • -
  • Meet the Covariance Matrix
  • -
  • Introducing the Covariance and Correlation functions
  • -
  • Covariance and Correlation Matrix
  • -
  • Correlation Function and Design/Feature Matrix
  • -
  • Covariance Matrix Examples
  • -
  • Correlation Matrix
  • -
  • Correlation Matrix with Pandas
  • -
  • Correlation Matrix with Pandas and the Franke function
  • -
  • Rewriting the Covariance and/or Correlation Matrix
  • -
  • Linking with the SVD
  • -
  • What does it mean?
  • -
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • -
  • Ridge and LASSO Regression
  • -
  • Deriving the Ridge Regression Equations
  • -
  • Interpreting the Ridge results
  • -
  • More interpretations
  • -
  • Deriving the Lasso Regression Equations
  • -
  • Exercises for week 35
  • -
  • Exercise 1: Setting up various Python environments
  • -
  • Exercise 2: making your own data and exploring scikit-learn
  • -
  • Exercise 3: Normalizing our data
  • -
  • Exercise 4: Adding Ridge Regression
  • -
  • Exercise 5: Analytical exercises
  • +
  • The Jacobian
  • +
  • Derivatives, example 1
  • +
  • Meet the Hessian Matrix
  • +
  • Interpretations and optimizing our parameters
  • +
  • Own code for Ordinary Least Squares
  • +
  • Adding error analysis and training set up
  • +
  • Splitting our Data in Training and Test data
  • +
  • Examples
  • +
  • Making your own test-train splitting
  • +
  • The Boston housing data example
  • +
  • Housing data, the code
  • +
  • Reducing the number of degrees of freedom, overarching view
  • +
  • Preprocessing our data
  • +
  • Functionality in Scikit-Learn
  • +
  • More preprocessing
  • +
  • Frequently used scaling functions
  • +
  • Example of own Standard scaling
  • +
  • Min-Max Scaling
  • +
  • Testing the Means Squared Error as function of Complexity
  • +
  • More preprocessing examples, Franke function and regression
  • +
  • Material for lecture Thursday, August 31
  • +
  • Mathematical Interpretation of Ordinary Least Squares
  • +
  • Residual Error
  • +
  • Simple case
  • +
  • The singular value decomposition
  • +
  • Linear Regression Problems
  • +
  • Fixing the singularity
  • +
  • Basic math of the SVD
  • +
  • The SVD, a Fantastic Algorithm
  • +
  • Economy-size SVD
  • +
  • Codes for the SVD
  • +
  • Note about SVD Calculations
  • +
  • Mathematics of the SVD and implications
  • +
  • Example Matrix
  • +
  • Setting up the Matrix to be inverted
  • +
  • Further properties (important for our analyses later)
  • +
  • Meet the Covariance Matrix
  • +
  • Introducing the Covariance and Correlation functions
  • +
  • Covariance and Correlation Matrix
  • +
  • Correlation Function and Design/Feature Matrix
  • +
  • Covariance Matrix Examples
  • +
  • Correlation Matrix
  • +
  • Correlation Matrix with Pandas
  • +
  • Correlation Matrix with Pandas and the Franke function
  • +
  • Rewriting the Covariance and/or Correlation Matrix
  • +
  • Linking with the SVD
  • +
  • What does it mean?
  • +
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • +
  • Ridge and LASSO Regression
  • +
  • Deriving the Ridge Regression Equations
  • +
  • Interpreting the Ridge results
  • +
  • More interpretations
  • +
  • Deriving the Lasso Regression Equations
  • +
  • Exercises for week 35
  • +
  • Exercise 1: Setting up various Python environments
  • +
  • Exercise 2: making your own data and exploring scikit-learn
  • +
  • Exercise 3: Normalizing our data
  • +
  • Exercise 4: Adding Ridge Regression
  • +
  • Exercise 5: Analytical exercises
  • @@ -382,7 +391,7 @@ MathJax.Hub.Config({

     

     

     

    -

    Our model for the nuclear binding energies

    +

    Examples relevant for the exercises

    In our introductory notes we looked at the so-called liquid drop model. Let us remind ourselves about what we did by looking at the code.

    @@ -510,7 +519,7 @@ $$
  • 21
  • 22
  • ...
  • -
  • 68
  • +
  • 71
  • »
  • diff --git a/doc/pub/week35/html/._week35-bs013.html b/doc/pub/week35/html/._week35-bs013.html index 8f688de2c..b77253763 100644 --- a/doc/pub/week35/html/._week35-bs013.html +++ b/doc/pub/week35/html/._week35-bs013.html @@ -74,10 +74,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'optimizing-our-parameters'), - ('Our model for the nuclear binding energies', + ('Examples relevant for the exercises', 2, None, - 'our-model-for-the-nuclear-binding-energies'), + 'examples-relevant-for-the-exercises'), ('Optimizing our parameters, more details', 2, None, @@ -94,6 +94,8 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'some-useful-matrix-and-vector-expressions'), + ('The Jacobian', 2, None, 'the-jacobian'), + ('Derivatives, example 1', 2, None, 'derivatives-example-1'), ('Meet the Hessian Matrix', 2, None, 'meet-the-hessian-matrix'), ('Interpretations and optimizing our parameters', 2, @@ -148,6 +150,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'more-preprocessing-examples-franke-function-and-regression'), + ('Material for lecture Thursday, August 31', + 2, + None, + 'material-for-lecture-thursday-august-31'), ('Mathematical Interpretation of Ordinary Least Squares', 2, None, @@ -303,74 +309,77 @@ MathJax.Hub.Config({
  • Why Linear Regression (aka Ordinary Least Squares and family), repeat from last week
  • Regression analysis, overarching aims
  • Regression analysis, overarching aims II
  • -
  • Examples
  • +
  • Examples
  • General linear models
  • Rewriting the fitting procedure as a linear algebra problem
  • Rewriting the fitting procedure as a linear algebra problem, more details
  • Generalizing the fitting procedure as a linear algebra problem
  • Generalizing the fitting procedure as a linear algebra problem
  • Optimizing our parameters
  • -
  • Our model for the nuclear binding energies
  • +
  • Examples relevant for the exercises
  • Optimizing our parameters, more details
  • -
  • Interpretations and optimizing our parameters
  • -
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • Some useful matrix and vector expressions
  • -
  • Meet the Hessian Matrix
  • -
  • Interpretations and optimizing our parameters
  • -
  • Own code for Ordinary Least Squares
  • -
  • Adding error analysis and training set up
  • -
  • Splitting our Data in Training and Test data
  • -
  • Examples
  • -
  • Making your own test-train splitting
  • -
  • The Boston housing data example
  • -
  • Housing data, the code
  • -
  • Reducing the number of degrees of freedom, overarching view
  • -
  • Preprocessing our data
  • -
  • Functionality in Scikit-Learn
  • -
  • More preprocessing
  • -
  • Frequently used scaling functions
  • -
  • Example of own Standard scaling
  • -
  • Min-Max Scaling
  • -
  • Testing the Means Squared Error as function of Complexity
  • -
  • More preprocessing examples, Franke function and regression
  • -
  • Mathematical Interpretation of Ordinary Least Squares
  • -
  • Residual Error
  • -
  • Simple case
  • -
  • The singular value decomposition
  • -
  • Linear Regression Problems
  • -
  • Fixing the singularity
  • -
  • Basic math of the SVD
  • -
  • The SVD, a Fantastic Algorithm
  • -
  • Economy-size SVD
  • -
  • Codes for the SVD
  • -
  • Note about SVD Calculations
  • -
  • Mathematics of the SVD and implications
  • -
  • Example Matrix
  • -
  • Setting up the Matrix to be inverted
  • -
  • Further properties (important for our analyses later)
  • -
  • Meet the Covariance Matrix
  • -
  • Introducing the Covariance and Correlation functions
  • -
  • Covariance and Correlation Matrix
  • -
  • Correlation Function and Design/Feature Matrix
  • -
  • Covariance Matrix Examples
  • -
  • Correlation Matrix
  • -
  • Correlation Matrix with Pandas
  • -
  • Correlation Matrix with Pandas and the Franke function
  • -
  • Rewriting the Covariance and/or Correlation Matrix
  • -
  • Linking with the SVD
  • -
  • What does it mean?
  • -
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • -
  • Ridge and LASSO Regression
  • -
  • Deriving the Ridge Regression Equations
  • -
  • Interpreting the Ridge results
  • -
  • More interpretations
  • -
  • Deriving the Lasso Regression Equations
  • -
  • Exercises for week 35
  • -
  • Exercise 1: Setting up various Python environments
  • -
  • Exercise 2: making your own data and exploring scikit-learn
  • -
  • Exercise 3: Normalizing our data
  • -
  • Exercise 4: Adding Ridge Regression
  • -
  • Exercise 5: Analytical exercises
  • +
  • The Jacobian
  • +
  • Derivatives, example 1
  • +
  • Meet the Hessian Matrix
  • +
  • Interpretations and optimizing our parameters
  • +
  • Own code for Ordinary Least Squares
  • +
  • Adding error analysis and training set up
  • +
  • Splitting our Data in Training and Test data
  • +
  • Examples
  • +
  • Making your own test-train splitting
  • +
  • The Boston housing data example
  • +
  • Housing data, the code
  • +
  • Reducing the number of degrees of freedom, overarching view
  • +
  • Preprocessing our data
  • +
  • Functionality in Scikit-Learn
  • +
  • More preprocessing
  • +
  • Frequently used scaling functions
  • +
  • Example of own Standard scaling
  • +
  • Min-Max Scaling
  • +
  • Testing the Means Squared Error as function of Complexity
  • +
  • More preprocessing examples, Franke function and regression
  • +
  • Material for lecture Thursday, August 31
  • +
  • Mathematical Interpretation of Ordinary Least Squares
  • +
  • Residual Error
  • +
  • Simple case
  • +
  • The singular value decomposition
  • +
  • Linear Regression Problems
  • +
  • Fixing the singularity
  • +
  • Basic math of the SVD
  • +
  • The SVD, a Fantastic Algorithm
  • +
  • Economy-size SVD
  • +
  • Codes for the SVD
  • +
  • Note about SVD Calculations
  • +
  • Mathematics of the SVD and implications
  • +
  • Example Matrix
  • +
  • Setting up the Matrix to be inverted
  • +
  • Further properties (important for our analyses later)
  • +
  • Meet the Covariance Matrix
  • +
  • Introducing the Covariance and Correlation functions
  • +
  • Covariance and Correlation Matrix
  • +
  • Correlation Function and Design/Feature Matrix
  • +
  • Covariance Matrix Examples
  • +
  • Correlation Matrix
  • +
  • Correlation Matrix with Pandas
  • +
  • Correlation Matrix with Pandas and the Franke function
  • +
  • Rewriting the Covariance and/or Correlation Matrix
  • +
  • Linking with the SVD
  • +
  • What does it mean?
  • +
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • +
  • Ridge and LASSO Regression
  • +
  • Deriving the Ridge Regression Equations
  • +
  • Interpreting the Ridge results
  • +
  • More interpretations
  • +
  • Deriving the Lasso Regression Equations
  • +
  • Exercises for week 35
  • +
  • Exercise 1: Setting up various Python environments
  • +
  • Exercise 2: making your own data and exploring scikit-learn
  • +
  • Exercise 3: Normalizing our data
  • +
  • Exercise 4: Adding Ridge Regression
  • +
  • Exercise 5: Analytical exercises
  • @@ -441,7 +450,7 @@ $$
  • 22
  • 23
  • ...
  • -
  • 68
  • +
  • 71
  • »
  • diff --git a/doc/pub/week35/html/._week35-bs014.html b/doc/pub/week35/html/._week35-bs014.html index 821813e45..19a78961e 100644 --- a/doc/pub/week35/html/._week35-bs014.html +++ b/doc/pub/week35/html/._week35-bs014.html @@ -74,10 +74,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'optimizing-our-parameters'), - ('Our model for the nuclear binding energies', + ('Examples relevant for the exercises', 2, None, - 'our-model-for-the-nuclear-binding-energies'), + 'examples-relevant-for-the-exercises'), ('Optimizing our parameters, more details', 2, None, @@ -94,6 +94,8 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'some-useful-matrix-and-vector-expressions'), + ('The Jacobian', 2, None, 'the-jacobian'), + ('Derivatives, example 1', 2, None, 'derivatives-example-1'), ('Meet the Hessian Matrix', 2, None, 'meet-the-hessian-matrix'), ('Interpretations and optimizing our parameters', 2, @@ -148,6 +150,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'more-preprocessing-examples-franke-function-and-regression'), + ('Material for lecture Thursday, August 31', + 2, + None, + 'material-for-lecture-thursday-august-31'), ('Mathematical Interpretation of Ordinary Least Squares', 2, None, @@ -303,74 +309,77 @@ MathJax.Hub.Config({
  • Why Linear Regression (aka Ordinary Least Squares and family), repeat from last week
  • Regression analysis, overarching aims
  • Regression analysis, overarching aims II
  • -
  • Examples
  • +
  • Examples
  • General linear models
  • Rewriting the fitting procedure as a linear algebra problem
  • Rewriting the fitting procedure as a linear algebra problem, more details
  • Generalizing the fitting procedure as a linear algebra problem
  • Generalizing the fitting procedure as a linear algebra problem
  • Optimizing our parameters
  • -
  • Our model for the nuclear binding energies
  • +
  • Examples relevant for the exercises
  • Optimizing our parameters, more details
  • -
  • Interpretations and optimizing our parameters
  • -
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • Some useful matrix and vector expressions
  • -
  • Meet the Hessian Matrix
  • -
  • Interpretations and optimizing our parameters
  • -
  • Own code for Ordinary Least Squares
  • -
  • Adding error analysis and training set up
  • -
  • Splitting our Data in Training and Test data
  • -
  • Examples
  • -
  • Making your own test-train splitting
  • -
  • The Boston housing data example
  • -
  • Housing data, the code
  • -
  • Reducing the number of degrees of freedom, overarching view
  • -
  • Preprocessing our data
  • -
  • Functionality in Scikit-Learn
  • -
  • More preprocessing
  • -
  • Frequently used scaling functions
  • -
  • Example of own Standard scaling
  • -
  • Min-Max Scaling
  • -
  • Testing the Means Squared Error as function of Complexity
  • -
  • More preprocessing examples, Franke function and regression
  • -
  • Mathematical Interpretation of Ordinary Least Squares
  • -
  • Residual Error
  • -
  • Simple case
  • -
  • The singular value decomposition
  • -
  • Linear Regression Problems
  • -
  • Fixing the singularity
  • -
  • Basic math of the SVD
  • -
  • The SVD, a Fantastic Algorithm
  • -
  • Economy-size SVD
  • -
  • Codes for the SVD
  • -
  • Note about SVD Calculations
  • -
  • Mathematics of the SVD and implications
  • -
  • Example Matrix
  • -
  • Setting up the Matrix to be inverted
  • -
  • Further properties (important for our analyses later)
  • -
  • Meet the Covariance Matrix
  • -
  • Introducing the Covariance and Correlation functions
  • -
  • Covariance and Correlation Matrix
  • -
  • Correlation Function and Design/Feature Matrix
  • -
  • Covariance Matrix Examples
  • -
  • Correlation Matrix
  • -
  • Correlation Matrix with Pandas
  • -
  • Correlation Matrix with Pandas and the Franke function
  • -
  • Rewriting the Covariance and/or Correlation Matrix
  • -
  • Linking with the SVD
  • -
  • What does it mean?
  • -
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • -
  • Ridge and LASSO Regression
  • -
  • Deriving the Ridge Regression Equations
  • -
  • Interpreting the Ridge results
  • -
  • More interpretations
  • -
  • Deriving the Lasso Regression Equations
  • -
  • Exercises for week 35
  • -
  • Exercise 1: Setting up various Python environments
  • -
  • Exercise 2: making your own data and exploring scikit-learn
  • -
  • Exercise 3: Normalizing our data
  • -
  • Exercise 4: Adding Ridge Regression
  • -
  • Exercise 5: Analytical exercises
  • +
  • The Jacobian
  • +
  • Derivatives, example 1
  • +
  • Meet the Hessian Matrix
  • +
  • Interpretations and optimizing our parameters
  • +
  • Own code for Ordinary Least Squares
  • +
  • Adding error analysis and training set up
  • +
  • Splitting our Data in Training and Test data
  • +
  • Examples
  • +
  • Making your own test-train splitting
  • +
  • The Boston housing data example
  • +
  • Housing data, the code
  • +
  • Reducing the number of degrees of freedom, overarching view
  • +
  • Preprocessing our data
  • +
  • Functionality in Scikit-Learn
  • +
  • More preprocessing
  • +
  • Frequently used scaling functions
  • +
  • Example of own Standard scaling
  • +
  • Min-Max Scaling
  • +
  • Testing the Means Squared Error as function of Complexity
  • +
  • More preprocessing examples, Franke function and regression
  • +
  • Material for lecture Thursday, August 31
  • +
  • Mathematical Interpretation of Ordinary Least Squares
  • +
  • Residual Error
  • +
  • Simple case
  • +
  • The singular value decomposition
  • +
  • Linear Regression Problems
  • +
  • Fixing the singularity
  • +
  • Basic math of the SVD
  • +
  • The SVD, a Fantastic Algorithm
  • +
  • Economy-size SVD
  • +
  • Codes for the SVD
  • +
  • Note about SVD Calculations
  • +
  • Mathematics of the SVD and implications
  • +
  • Example Matrix
  • +
  • Setting up the Matrix to be inverted
  • +
  • Further properties (important for our analyses later)
  • +
  • Meet the Covariance Matrix
  • +
  • Introducing the Covariance and Correlation functions
  • +
  • Covariance and Correlation Matrix
  • +
  • Correlation Function and Design/Feature Matrix
  • +
  • Covariance Matrix Examples
  • +
  • Correlation Matrix
  • +
  • Correlation Matrix with Pandas
  • +
  • Correlation Matrix with Pandas and the Franke function
  • +
  • Rewriting the Covariance and/or Correlation Matrix
  • +
  • Linking with the SVD
  • +
  • What does it mean?
  • +
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • +
  • Ridge and LASSO Regression
  • +
  • Deriving the Ridge Regression Equations
  • +
  • Interpreting the Ridge results
  • +
  • More interpretations
  • +
  • Deriving the Lasso Regression Equations
  • +
  • Exercises for week 35
  • +
  • Exercise 1: Setting up various Python environments
  • +
  • Exercise 2: making your own data and exploring scikit-learn
  • +
  • Exercise 3: Normalizing our data
  • +
  • Exercise 4: Adding Ridge Regression
  • +
  • Exercise 5: Analytical exercises
  • @@ -458,7 +467,7 @@ $$
  • 23
  • 24
  • ...
  • -
  • 68
  • +
  • 71
  • »
  • diff --git a/doc/pub/week35/html/._week35-bs015.html b/doc/pub/week35/html/._week35-bs015.html index 52adfb9b5..e35dd9372 100644 --- a/doc/pub/week35/html/._week35-bs015.html +++ b/doc/pub/week35/html/._week35-bs015.html @@ -74,10 +74,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'optimizing-our-parameters'), - ('Our model for the nuclear binding energies', + ('Examples relevant for the exercises', 2, None, - 'our-model-for-the-nuclear-binding-energies'), + 'examples-relevant-for-the-exercises'), ('Optimizing our parameters, more details', 2, None, @@ -94,6 +94,8 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'some-useful-matrix-and-vector-expressions'), + ('The Jacobian', 2, None, 'the-jacobian'), + ('Derivatives, example 1', 2, None, 'derivatives-example-1'), ('Meet the Hessian Matrix', 2, None, 'meet-the-hessian-matrix'), ('Interpretations and optimizing our parameters', 2, @@ -148,6 +150,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'more-preprocessing-examples-franke-function-and-regression'), + ('Material for lecture Thursday, August 31', + 2, + None, + 'material-for-lecture-thursday-august-31'), ('Mathematical Interpretation of Ordinary Least Squares', 2, None, @@ -303,74 +309,77 @@ MathJax.Hub.Config({
  • Why Linear Regression (aka Ordinary Least Squares and family), repeat from last week
  • Regression analysis, overarching aims
  • Regression analysis, overarching aims II
  • -
  • Examples
  • +
  • Examples
  • General linear models
  • Rewriting the fitting procedure as a linear algebra problem
  • Rewriting the fitting procedure as a linear algebra problem, more details
  • Generalizing the fitting procedure as a linear algebra problem
  • Generalizing the fitting procedure as a linear algebra problem
  • Optimizing our parameters
  • -
  • Our model for the nuclear binding energies
  • +
  • Examples relevant for the exercises
  • Optimizing our parameters, more details
  • -
  • Interpretations and optimizing our parameters
  • -
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • Some useful matrix and vector expressions
  • -
  • Meet the Hessian Matrix
  • -
  • Interpretations and optimizing our parameters
  • -
  • Own code for Ordinary Least Squares
  • -
  • Adding error analysis and training set up
  • -
  • Splitting our Data in Training and Test data
  • -
  • Examples
  • -
  • Making your own test-train splitting
  • -
  • The Boston housing data example
  • -
  • Housing data, the code
  • -
  • Reducing the number of degrees of freedom, overarching view
  • -
  • Preprocessing our data
  • -
  • Functionality in Scikit-Learn
  • -
  • More preprocessing
  • -
  • Frequently used scaling functions
  • -
  • Example of own Standard scaling
  • -
  • Min-Max Scaling
  • -
  • Testing the Means Squared Error as function of Complexity
  • -
  • More preprocessing examples, Franke function and regression
  • -
  • Mathematical Interpretation of Ordinary Least Squares
  • -
  • Residual Error
  • -
  • Simple case
  • -
  • The singular value decomposition
  • -
  • Linear Regression Problems
  • -
  • Fixing the singularity
  • -
  • Basic math of the SVD
  • -
  • The SVD, a Fantastic Algorithm
  • -
  • Economy-size SVD
  • -
  • Codes for the SVD
  • -
  • Note about SVD Calculations
  • -
  • Mathematics of the SVD and implications
  • -
  • Example Matrix
  • -
  • Setting up the Matrix to be inverted
  • -
  • Further properties (important for our analyses later)
  • -
  • Meet the Covariance Matrix
  • -
  • Introducing the Covariance and Correlation functions
  • -
  • Covariance and Correlation Matrix
  • -
  • Correlation Function and Design/Feature Matrix
  • -
  • Covariance Matrix Examples
  • -
  • Correlation Matrix
  • -
  • Correlation Matrix with Pandas
  • -
  • Correlation Matrix with Pandas and the Franke function
  • -
  • Rewriting the Covariance and/or Correlation Matrix
  • -
  • Linking with the SVD
  • -
  • What does it mean?
  • -
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • -
  • Ridge and LASSO Regression
  • -
  • Deriving the Ridge Regression Equations
  • -
  • Interpreting the Ridge results
  • -
  • More interpretations
  • -
  • Deriving the Lasso Regression Equations
  • -
  • Exercises for week 35
  • -
  • Exercise 1: Setting up various Python environments
  • -
  • Exercise 2: making your own data and exploring scikit-learn
  • -
  • Exercise 3: Normalizing our data
  • -
  • Exercise 4: Adding Ridge Regression
  • -
  • Exercise 5: Analytical exercises
  • +
  • The Jacobian
  • +
  • Derivatives, example 1
  • +
  • Meet the Hessian Matrix
  • +
  • Interpretations and optimizing our parameters
  • +
  • Own code for Ordinary Least Squares
  • +
  • Adding error analysis and training set up
  • +
  • Splitting our Data in Training and Test data
  • +
  • Examples
  • +
  • Making your own test-train splitting
  • +
  • The Boston housing data example
  • +
  • Housing data, the code
  • +
  • Reducing the number of degrees of freedom, overarching view
  • +
  • Preprocessing our data
  • +
  • Functionality in Scikit-Learn
  • +
  • More preprocessing
  • +
  • Frequently used scaling functions
  • +
  • Example of own Standard scaling
  • +
  • Min-Max Scaling
  • +
  • Testing the Means Squared Error as function of Complexity
  • +
  • More preprocessing examples, Franke function and regression
  • +
  • Material for lecture Thursday, August 31
  • +
  • Mathematical Interpretation of Ordinary Least Squares
  • +
  • Residual Error
  • +
  • Simple case
  • +
  • The singular value decomposition
  • +
  • Linear Regression Problems
  • +
  • Fixing the singularity
  • +
  • Basic math of the SVD
  • +
  • The SVD, a Fantastic Algorithm
  • +
  • Economy-size SVD
  • +
  • Codes for the SVD
  • +
  • Note about SVD Calculations
  • +
  • Mathematics of the SVD and implications
  • +
  • Example Matrix
  • +
  • Setting up the Matrix to be inverted
  • +
  • Further properties (important for our analyses later)
  • +
  • Meet the Covariance Matrix
  • +
  • Introducing the Covariance and Correlation functions
  • +
  • Covariance and Correlation Matrix
  • +
  • Correlation Function and Design/Feature Matrix
  • +
  • Covariance Matrix Examples
  • +
  • Correlation Matrix
  • +
  • Correlation Matrix with Pandas
  • +
  • Correlation Matrix with Pandas and the Franke function
  • +
  • Rewriting the Covariance and/or Correlation Matrix
  • +
  • Linking with the SVD
  • +
  • What does it mean?
  • +
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • +
  • Ridge and LASSO Regression
  • +
  • Deriving the Ridge Regression Equations
  • +
  • Interpreting the Ridge results
  • +
  • More interpretations
  • +
  • Deriving the Lasso Regression Equations
  • +
  • Exercises for week 35
  • +
  • Exercise 1: Setting up various Python environments
  • +
  • Exercise 2: making your own data and exploring scikit-learn
  • +
  • Exercise 3: Normalizing our data
  • +
  • Exercise 4: Adding Ridge Regression
  • +
  • Exercise 5: Analytical exercises
  • @@ -449,7 +458,7 @@ allow for the usage of direct linear algebra methods such as LU decomposi
  • 24
  • 25
  • ...
  • -
  • 68
  • +
  • 71
  • »
  • diff --git a/doc/pub/week35/html/._week35-bs016.html b/doc/pub/week35/html/._week35-bs016.html index 0808e325d..eb56f6e90 100644 --- a/doc/pub/week35/html/._week35-bs016.html +++ b/doc/pub/week35/html/._week35-bs016.html @@ -74,10 +74,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'optimizing-our-parameters'), - ('Our model for the nuclear binding energies', + ('Examples relevant for the exercises', 2, None, - 'our-model-for-the-nuclear-binding-energies'), + 'examples-relevant-for-the-exercises'), ('Optimizing our parameters, more details', 2, None, @@ -94,6 +94,8 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'some-useful-matrix-and-vector-expressions'), + ('The Jacobian', 2, None, 'the-jacobian'), + ('Derivatives, example 1', 2, None, 'derivatives-example-1'), ('Meet the Hessian Matrix', 2, None, 'meet-the-hessian-matrix'), ('Interpretations and optimizing our parameters', 2, @@ -148,6 +150,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'more-preprocessing-examples-franke-function-and-regression'), + ('Material for lecture Thursday, August 31', + 2, + None, + 'material-for-lecture-thursday-august-31'), ('Mathematical Interpretation of Ordinary Least Squares', 2, None, @@ -303,74 +309,77 @@ MathJax.Hub.Config({
  • Why Linear Regression (aka Ordinary Least Squares and family), repeat from last week
  • Regression analysis, overarching aims
  • Regression analysis, overarching aims II
  • -
  • Examples
  • +
  • Examples
  • General linear models
  • Rewriting the fitting procedure as a linear algebra problem
  • Rewriting the fitting procedure as a linear algebra problem, more details
  • Generalizing the fitting procedure as a linear algebra problem
  • Generalizing the fitting procedure as a linear algebra problem
  • Optimizing our parameters
  • -
  • Our model for the nuclear binding energies
  • +
  • Examples relevant for the exercises
  • Optimizing our parameters, more details
  • -
  • Interpretations and optimizing our parameters
  • -
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • Some useful matrix and vector expressions
  • -
  • Meet the Hessian Matrix
  • -
  • Interpretations and optimizing our parameters
  • -
  • Own code for Ordinary Least Squares
  • -
  • Adding error analysis and training set up
  • -
  • Splitting our Data in Training and Test data
  • -
  • Examples
  • -
  • Making your own test-train splitting
  • -
  • The Boston housing data example
  • -
  • Housing data, the code
  • -
  • Reducing the number of degrees of freedom, overarching view
  • -
  • Preprocessing our data
  • -
  • Functionality in Scikit-Learn
  • -
  • More preprocessing
  • -
  • Frequently used scaling functions
  • -
  • Example of own Standard scaling
  • -
  • Min-Max Scaling
  • -
  • Testing the Means Squared Error as function of Complexity
  • -
  • More preprocessing examples, Franke function and regression
  • -
  • Mathematical Interpretation of Ordinary Least Squares
  • -
  • Residual Error
  • -
  • Simple case
  • -
  • The singular value decomposition
  • -
  • Linear Regression Problems
  • -
  • Fixing the singularity
  • -
  • Basic math of the SVD
  • -
  • The SVD, a Fantastic Algorithm
  • -
  • Economy-size SVD
  • -
  • Codes for the SVD
  • -
  • Note about SVD Calculations
  • -
  • Mathematics of the SVD and implications
  • -
  • Example Matrix
  • -
  • Setting up the Matrix to be inverted
  • -
  • Further properties (important for our analyses later)
  • -
  • Meet the Covariance Matrix
  • -
  • Introducing the Covariance and Correlation functions
  • -
  • Covariance and Correlation Matrix
  • -
  • Correlation Function and Design/Feature Matrix
  • -
  • Covariance Matrix Examples
  • -
  • Correlation Matrix
  • -
  • Correlation Matrix with Pandas
  • -
  • Correlation Matrix with Pandas and the Franke function
  • -
  • Rewriting the Covariance and/or Correlation Matrix
  • -
  • Linking with the SVD
  • -
  • What does it mean?
  • -
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • -
  • Ridge and LASSO Regression
  • -
  • Deriving the Ridge Regression Equations
  • -
  • Interpreting the Ridge results
  • -
  • More interpretations
  • -
  • Deriving the Lasso Regression Equations
  • -
  • Exercises for week 35
  • -
  • Exercise 1: Setting up various Python environments
  • -
  • Exercise 2: making your own data and exploring scikit-learn
  • -
  • Exercise 3: Normalizing our data
  • -
  • Exercise 4: Adding Ridge Regression
  • -
  • Exercise 5: Analytical exercises
  • +
  • The Jacobian
  • +
  • Derivatives, example 1
  • +
  • Meet the Hessian Matrix
  • +
  • Interpretations and optimizing our parameters
  • +
  • Own code for Ordinary Least Squares
  • +
  • Adding error analysis and training set up
  • +
  • Splitting our Data in Training and Test data
  • +
  • Examples
  • +
  • Making your own test-train splitting
  • +
  • The Boston housing data example
  • +
  • Housing data, the code
  • +
  • Reducing the number of degrees of freedom, overarching view
  • +
  • Preprocessing our data
  • +
  • Functionality in Scikit-Learn
  • +
  • More preprocessing
  • +
  • Frequently used scaling functions
  • +
  • Example of own Standard scaling
  • +
  • Min-Max Scaling
  • +
  • Testing the Means Squared Error as function of Complexity
  • +
  • More preprocessing examples, Franke function and regression
  • +
  • Material for lecture Thursday, August 31
  • +
  • Mathematical Interpretation of Ordinary Least Squares
  • +
  • Residual Error
  • +
  • Simple case
  • +
  • The singular value decomposition
  • +
  • Linear Regression Problems
  • +
  • Fixing the singularity
  • +
  • Basic math of the SVD
  • +
  • The SVD, a Fantastic Algorithm
  • +
  • Economy-size SVD
  • +
  • Codes for the SVD
  • +
  • Note about SVD Calculations
  • +
  • Mathematics of the SVD and implications
  • +
  • Example Matrix
  • +
  • Setting up the Matrix to be inverted
  • +
  • Further properties (important for our analyses later)
  • +
  • Meet the Covariance Matrix
  • +
  • Introducing the Covariance and Correlation functions
  • +
  • Covariance and Correlation Matrix
  • +
  • Correlation Function and Design/Feature Matrix
  • +
  • Covariance Matrix Examples
  • +
  • Correlation Matrix
  • +
  • Correlation Matrix with Pandas
  • +
  • Correlation Matrix with Pandas and the Franke function
  • +
  • Rewriting the Covariance and/or Correlation Matrix
  • +
  • Linking with the SVD
  • +
  • What does it mean?
  • +
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • +
  • Ridge and LASSO Regression
  • +
  • Deriving the Ridge Regression Equations
  • +
  • Interpreting the Ridge results
  • +
  • More interpretations
  • +
  • Deriving the Lasso Regression Equations
  • +
  • Exercises for week 35
  • +
  • Exercise 1: Setting up various Python environments
  • +
  • Exercise 2: making your own data and exploring scikit-learn
  • +
  • Exercise 3: Normalizing our data
  • +
  • Exercise 4: Adding Ridge Regression
  • +
  • Exercise 5: Analytical exercises
  • @@ -384,27 +393,30 @@ MathJax.Hub.Config({

    Some useful matrix and vector expressions

    -

    The following matrix and vector relation will be useful here and for the rest of the course. Vectors are always written as boldfaced lower case letters and -matrices as upper case boldfaced letters. +

    The following matrix and vector relation will be useful here and for +the rest of the course. Vectors are always written as boldfaced lower +case letters and matrices as upper case boldfaced letters. In the +following we will discuss how to calculate derivatives of various +matrices relevant for machine learning. We will often represent our +data in terms of matrices and vectors. +

    + +

    Let us introduce first some conventions. We assume that \( \boldsymbol{y} \) is a +vector of length \( m \), that is it has \( m \) elements \( y_0,y_1,\dots, +y_{m-1} \). By convention we start labeling vectors with the zeroth +element, as are arrays in Python and C++/C, for example. Similarly, we +have a vector \( \boldsymbol{x} \) of length \( n \), that is +\( \boldsymbol{x}^T=[x_0,x_1,\dots, x_{n-1}] \). +

    + +

    We assume also that \( \boldsymbol{y} \) is a function of \( \boldsymbol{x} \) through some +given function \( f \)

    $$ -\frac{\partial (\boldsymbol{b}^T\boldsymbol{a})}{\partial \boldsymbol{a}} = \boldsymbol{b}, +\boldsymbol{y}=f(\boldsymbol{x}). $$ -$$ -\frac{\partial (\boldsymbol{a}^T\boldsymbol{A}\boldsymbol{a})}{\partial \boldsymbol{a}} = (\boldsymbol{A}+\boldsymbol{A}^T)\boldsymbol{a}, -$$ - -$$ -\frac{\partial tr(\boldsymbol{B}\boldsymbol{A})}{\partial \boldsymbol{A}} = \boldsymbol{B}^T, -$$ - -$$ -\frac{\partial \log{\vert\boldsymbol{A}\vert}}{\partial \boldsymbol{A}} = (\boldsymbol{A}^{-1})^T. -$$ - -

    See the jupyter-book (complete lecture notes) for the derivations of these relations.

    @@ -431,7 +443,7 @@ $$

  • 25
  • 26
  • ...
  • -
  • 68
  • +
  • 71
  • »
  • diff --git a/doc/pub/week35/html/._week35-bs017.html b/doc/pub/week35/html/._week35-bs017.html index 48d1bf6a7..10a327678 100644 --- a/doc/pub/week35/html/._week35-bs017.html +++ b/doc/pub/week35/html/._week35-bs017.html @@ -74,10 +74,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'optimizing-our-parameters'), - ('Our model for the nuclear binding energies', + ('Examples relevant for the exercises', 2, None, - 'our-model-for-the-nuclear-binding-energies'), + 'examples-relevant-for-the-exercises'), ('Optimizing our parameters, more details', 2, None, @@ -94,6 +94,8 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'some-useful-matrix-and-vector-expressions'), + ('The Jacobian', 2, None, 'the-jacobian'), + ('Derivatives, example 1', 2, None, 'derivatives-example-1'), ('Meet the Hessian Matrix', 2, None, 'meet-the-hessian-matrix'), ('Interpretations and optimizing our parameters', 2, @@ -148,6 +150,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'more-preprocessing-examples-franke-function-and-regression'), + ('Material for lecture Thursday, August 31', + 2, + None, + 'material-for-lecture-thursday-august-31'), ('Mathematical Interpretation of Ordinary Least Squares', 2, None, @@ -303,74 +309,77 @@ MathJax.Hub.Config({
  • Why Linear Regression (aka Ordinary Least Squares and family), repeat from last week
  • Regression analysis, overarching aims
  • Regression analysis, overarching aims II
  • -
  • Examples
  • +
  • Examples
  • General linear models
  • Rewriting the fitting procedure as a linear algebra problem
  • Rewriting the fitting procedure as a linear algebra problem, more details
  • Generalizing the fitting procedure as a linear algebra problem
  • Generalizing the fitting procedure as a linear algebra problem
  • Optimizing our parameters
  • -
  • Our model for the nuclear binding energies
  • +
  • Examples relevant for the exercises
  • Optimizing our parameters, more details
  • -
  • Interpretations and optimizing our parameters
  • -
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • Some useful matrix and vector expressions
  • -
  • Meet the Hessian Matrix
  • -
  • Interpretations and optimizing our parameters
  • -
  • Own code for Ordinary Least Squares
  • -
  • Adding error analysis and training set up
  • -
  • Splitting our Data in Training and Test data
  • -
  • Examples
  • -
  • Making your own test-train splitting
  • -
  • The Boston housing data example
  • -
  • Housing data, the code
  • -
  • Reducing the number of degrees of freedom, overarching view
  • -
  • Preprocessing our data
  • -
  • Functionality in Scikit-Learn
  • -
  • More preprocessing
  • -
  • Frequently used scaling functions
  • -
  • Example of own Standard scaling
  • -
  • Min-Max Scaling
  • -
  • Testing the Means Squared Error as function of Complexity
  • -
  • More preprocessing examples, Franke function and regression
  • -
  • Mathematical Interpretation of Ordinary Least Squares
  • -
  • Residual Error
  • -
  • Simple case
  • -
  • The singular value decomposition
  • -
  • Linear Regression Problems
  • -
  • Fixing the singularity
  • -
  • Basic math of the SVD
  • -
  • The SVD, a Fantastic Algorithm
  • -
  • Economy-size SVD
  • -
  • Codes for the SVD
  • -
  • Note about SVD Calculations
  • -
  • Mathematics of the SVD and implications
  • -
  • Example Matrix
  • -
  • Setting up the Matrix to be inverted
  • -
  • Further properties (important for our analyses later)
  • -
  • Meet the Covariance Matrix
  • -
  • Introducing the Covariance and Correlation functions
  • -
  • Covariance and Correlation Matrix
  • -
  • Correlation Function and Design/Feature Matrix
  • -
  • Covariance Matrix Examples
  • -
  • Correlation Matrix
  • -
  • Correlation Matrix with Pandas
  • -
  • Correlation Matrix with Pandas and the Franke function
  • -
  • Rewriting the Covariance and/or Correlation Matrix
  • -
  • Linking with the SVD
  • -
  • What does it mean?
  • -
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • -
  • Ridge and LASSO Regression
  • -
  • Deriving the Ridge Regression Equations
  • -
  • Interpreting the Ridge results
  • -
  • More interpretations
  • -
  • Deriving the Lasso Regression Equations
  • -
  • Exercises for week 35
  • -
  • Exercise 1: Setting up various Python environments
  • -
  • Exercise 2: making your own data and exploring scikit-learn
  • -
  • Exercise 3: Normalizing our data
  • -
  • Exercise 4: Adding Ridge Regression
  • -
  • Exercise 5: Analytical exercises
  • +
  • The Jacobian
  • +
  • Derivatives, example 1
  • +
  • Meet the Hessian Matrix
  • +
  • Interpretations and optimizing our parameters
  • +
  • Own code for Ordinary Least Squares
  • +
  • Adding error analysis and training set up
  • +
  • Splitting our Data in Training and Test data
  • +
  • Examples
  • +
  • Making your own test-train splitting
  • +
  • The Boston housing data example
  • +
  • Housing data, the code
  • +
  • Reducing the number of degrees of freedom, overarching view
  • +
  • Preprocessing our data
  • +
  • Functionality in Scikit-Learn
  • +
  • More preprocessing
  • +
  • Frequently used scaling functions
  • +
  • Example of own Standard scaling
  • +
  • Min-Max Scaling
  • +
  • Testing the Means Squared Error as function of Complexity
  • +
  • More preprocessing examples, Franke function and regression
  • +
  • Material for lecture Thursday, August 31
  • +
  • Mathematical Interpretation of Ordinary Least Squares
  • +
  • Residual Error
  • +
  • Simple case
  • +
  • The singular value decomposition
  • +
  • Linear Regression Problems
  • +
  • Fixing the singularity
  • +
  • Basic math of the SVD
  • +
  • The SVD, a Fantastic Algorithm
  • +
  • Economy-size SVD
  • +
  • Codes for the SVD
  • +
  • Note about SVD Calculations
  • +
  • Mathematics of the SVD and implications
  • +
  • Example Matrix
  • +
  • Setting up the Matrix to be inverted
  • +
  • Further properties (important for our analyses later)
  • +
  • Meet the Covariance Matrix
  • +
  • Introducing the Covariance and Correlation functions
  • +
  • Covariance and Correlation Matrix
  • +
  • Correlation Function and Design/Feature Matrix
  • +
  • Covariance Matrix Examples
  • +
  • Correlation Matrix
  • +
  • Correlation Matrix with Pandas
  • +
  • Correlation Matrix with Pandas and the Franke function
  • +
  • Rewriting the Covariance and/or Correlation Matrix
  • +
  • Linking with the SVD
  • +
  • What does it mean?
  • +
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • +
  • Ridge and LASSO Regression
  • +
  • Deriving the Ridge Regression Equations
  • +
  • Interpreting the Ridge results
  • +
  • More interpretations
  • +
  • Deriving the Lasso Regression Equations
  • +
  • Exercises for week 35
  • +
  • Exercise 1: Setting up various Python environments
  • +
  • Exercise 2: making your own data and exploring scikit-learn
  • +
  • Exercise 3: Normalizing our data
  • +
  • Exercise 4: Adding Ridge Regression
  • +
  • Exercise 5: Analytical exercises
  • @@ -382,36 +391,24 @@ MathJax.Hub.Config({

     

     

     

    -

    Meet the Hessian Matrix

    +

    The Jacobian

    -

    A very important matrix we will meet again and again in Machine -Learning is the Hessian. It is given by the second derivative of the -cost function with respect to the parameter \( \beta \). Using the above -expression for derivatives of vectors and matrices, we find that the -second derivative of the cost function is, +

    We define the partial derivatives of the various components of \( \boldsymbol{y} \) as functions of \( x_i \) in terms of the so-called Jacobian matrix

    + +$$ +\boldsymbol{J}=\frac{\partial \boldsymbol{y}}{\partial \boldsymbol{x}}=\begin{bmatrix} \frac{\partial y_0}{\partial x_0} & \frac{\partial y_0}{\partial x_1} & \frac{\partial y_0}{\partial x_2} & \dots & \dots & \frac{\partial y_0}{\partial x_{n-1}} \\ \frac{\partial y_0}{\partial x_0} & \frac{\partial y_1}{\partial x_1} & \frac{\partial y_1}{\partial x_2} & \dots & \dots & \frac{\partial y_1}{\partial x_{n-1}} \\ +\frac{\partial y_2}{\partial x_0} & \frac{\partial y_2}{\partial x_1} & \frac{\partial y_2}{\partial x_2} & \dots & \dots & \frac{\partial y_2}{\partial x_{n-1}} \\ +\dots & \dots & \dots & \dots & \dots & \dots \\ +\dots & \dots & \dots & \dots & \dots & \dots \\ +\frac{\partial y_{m-1}}{\partial x_0} & \frac{\partial y_{m-1}}{\partial x_1} & \frac{\partial y_{m-1}}{\partial x_2} & \dots & \dots & \frac{\partial y_{m-1}}{\partial x_{n-1}} \end{bmatrix}, +$$ + +

    which is an \( m\times n \) matrix. If \( \boldsymbol{x} \) is a scalar, then the +Jacobian is only a single-column vector, or an \( m\times 1 \) matrix. If +on the other hand \( \boldsymbol{y} \) is a scalar, the Jacobian becomes a +\( 1\times n \) matrix.

    -$$ -\frac{\partial}{\partial \boldsymbol{\beta}^T}\frac{\partial C(\boldsymbol{\beta})}{\partial \boldsymbol{\beta}} =\frac{\partial}{\partial \boldsymbol{\beta}}\left[-\frac{2}{n}\boldsymbol{X}^T\left( \boldsymbol{y}-\boldsymbol{X}\boldsymbol{\beta}\right)\right]=\frac{2}{n}\boldsymbol{X}^T\boldsymbol{X}. -$$ - -

    The Hessian matrix plays an important role and is defined here as

    - -$$ -\boldsymbol{H}=\boldsymbol{X}^T\boldsymbol{X}. -$$ - -

    For ordinary least squares, it is inversely proportional (derivation -next week) with the variance of the optimal parameters -\( \hat{\boldsymbol{\beta}} \). Furthermore, we will see later this week that is -(beside \( 1/n \)) equal to the covariance matrix. It plays also a very -important role in optmization algorithms and Principal Component -Analysis as a way to reduce the dimensionality of a machine learning -problem. -

    - -

    Linear algebra question: Can we use the Hessian matrix to say something about properties of the cost function (our optmization problem)? (hint: think about convex or concave problems and how to relate these to a matrix!).

    -

    diff --git a/doc/pub/week35/html/._week35-bs018.html b/doc/pub/week35/html/._week35-bs018.html index cfdd469d6..79064a873 100644 --- a/doc/pub/week35/html/._week35-bs018.html +++ b/doc/pub/week35/html/._week35-bs018.html @@ -74,10 +74,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'optimizing-our-parameters'), - ('Our model for the nuclear binding energies', + ('Examples relevant for the exercises', 2, None, - 'our-model-for-the-nuclear-binding-energies'), + 'examples-relevant-for-the-exercises'), ('Optimizing our parameters, more details', 2, None, @@ -94,6 +94,8 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'some-useful-matrix-and-vector-expressions'), + ('The Jacobian', 2, None, 'the-jacobian'), + ('Derivatives, example 1', 2, None, 'derivatives-example-1'), ('Meet the Hessian Matrix', 2, None, 'meet-the-hessian-matrix'), ('Interpretations and optimizing our parameters', 2, @@ -148,6 +150,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'more-preprocessing-examples-franke-function-and-regression'), + ('Material for lecture Thursday, August 31', + 2, + None, + 'material-for-lecture-thursday-august-31'), ('Mathematical Interpretation of Ordinary Least Squares', 2, None, @@ -303,74 +309,77 @@ MathJax.Hub.Config({
  • Why Linear Regression (aka Ordinary Least Squares and family), repeat from last week
  • Regression analysis, overarching aims
  • Regression analysis, overarching aims II
  • -
  • Examples
  • +
  • Examples
  • General linear models
  • Rewriting the fitting procedure as a linear algebra problem
  • Rewriting the fitting procedure as a linear algebra problem, more details
  • Generalizing the fitting procedure as a linear algebra problem
  • Generalizing the fitting procedure as a linear algebra problem
  • Optimizing our parameters
  • -
  • Our model for the nuclear binding energies
  • +
  • Examples relevant for the exercises
  • Optimizing our parameters, more details
  • -
  • Interpretations and optimizing our parameters
  • -
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • Some useful matrix and vector expressions
  • -
  • Meet the Hessian Matrix
  • -
  • Interpretations and optimizing our parameters
  • -
  • Own code for Ordinary Least Squares
  • -
  • Adding error analysis and training set up
  • -
  • Splitting our Data in Training and Test data
  • -
  • Examples
  • -
  • Making your own test-train splitting
  • -
  • The Boston housing data example
  • -
  • Housing data, the code
  • -
  • Reducing the number of degrees of freedom, overarching view
  • -
  • Preprocessing our data
  • -
  • Functionality in Scikit-Learn
  • -
  • More preprocessing
  • -
  • Frequently used scaling functions
  • -
  • Example of own Standard scaling
  • -
  • Min-Max Scaling
  • -
  • Testing the Means Squared Error as function of Complexity
  • -
  • More preprocessing examples, Franke function and regression
  • -
  • Mathematical Interpretation of Ordinary Least Squares
  • -
  • Residual Error
  • -
  • Simple case
  • -
  • The singular value decomposition
  • -
  • Linear Regression Problems
  • -
  • Fixing the singularity
  • -
  • Basic math of the SVD
  • -
  • The SVD, a Fantastic Algorithm
  • -
  • Economy-size SVD
  • -
  • Codes for the SVD
  • -
  • Note about SVD Calculations
  • -
  • Mathematics of the SVD and implications
  • -
  • Example Matrix
  • -
  • Setting up the Matrix to be inverted
  • -
  • Further properties (important for our analyses later)
  • -
  • Meet the Covariance Matrix
  • -
  • Introducing the Covariance and Correlation functions
  • -
  • Covariance and Correlation Matrix
  • -
  • Correlation Function and Design/Feature Matrix
  • -
  • Covariance Matrix Examples
  • -
  • Correlation Matrix
  • -
  • Correlation Matrix with Pandas
  • -
  • Correlation Matrix with Pandas and the Franke function
  • -
  • Rewriting the Covariance and/or Correlation Matrix
  • -
  • Linking with the SVD
  • -
  • What does it mean?
  • -
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • -
  • Ridge and LASSO Regression
  • -
  • Deriving the Ridge Regression Equations
  • -
  • Interpreting the Ridge results
  • -
  • More interpretations
  • -
  • Deriving the Lasso Regression Equations
  • -
  • Exercises for week 35
  • -
  • Exercise 1: Setting up various Python environments
  • -
  • Exercise 2: making your own data and exploring scikit-learn
  • -
  • Exercise 3: Normalizing our data
  • -
  • Exercise 4: Adding Ridge Regression
  • -
  • Exercise 5: Analytical exercises
  • +
  • The Jacobian
  • +
  • Derivatives, example 1
  • +
  • Meet the Hessian Matrix
  • +
  • Interpretations and optimizing our parameters
  • +
  • Own code for Ordinary Least Squares
  • +
  • Adding error analysis and training set up
  • +
  • Splitting our Data in Training and Test data
  • +
  • Examples
  • +
  • Making your own test-train splitting
  • +
  • The Boston housing data example
  • +
  • Housing data, the code
  • +
  • Reducing the number of degrees of freedom, overarching view
  • +
  • Preprocessing our data
  • +
  • Functionality in Scikit-Learn
  • +
  • More preprocessing
  • +
  • Frequently used scaling functions
  • +
  • Example of own Standard scaling
  • +
  • Min-Max Scaling
  • +
  • Testing the Means Squared Error as function of Complexity
  • +
  • More preprocessing examples, Franke function and regression
  • +
  • Material for lecture Thursday, August 31
  • +
  • Mathematical Interpretation of Ordinary Least Squares
  • +
  • Residual Error
  • +
  • Simple case
  • +
  • The singular value decomposition
  • +
  • Linear Regression Problems
  • +
  • Fixing the singularity
  • +
  • Basic math of the SVD
  • +
  • The SVD, a Fantastic Algorithm
  • +
  • Economy-size SVD
  • +
  • Codes for the SVD
  • +
  • Note about SVD Calculations
  • +
  • Mathematics of the SVD and implications
  • +
  • Example Matrix
  • +
  • Setting up the Matrix to be inverted
  • +
  • Further properties (important for our analyses later)
  • +
  • Meet the Covariance Matrix
  • +
  • Introducing the Covariance and Correlation functions
  • +
  • Covariance and Correlation Matrix
  • +
  • Correlation Function and Design/Feature Matrix
  • +
  • Covariance Matrix Examples
  • +
  • Correlation Matrix
  • +
  • Correlation Matrix with Pandas
  • +
  • Correlation Matrix with Pandas and the Franke function
  • +
  • Rewriting the Covariance and/or Correlation Matrix
  • +
  • Linking with the SVD
  • +
  • What does it mean?
  • +
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • +
  • Ridge and LASSO Regression
  • +
  • Deriving the Ridge Regression Equations
  • +
  • Interpreting the Ridge results
  • +
  • More interpretations
  • +
  • Deriving the Lasso Regression Equations
  • +
  • Exercises for week 35
  • +
  • Exercise 1: Setting up various Python environments
  • +
  • Exercise 2: making your own data and exploring scikit-learn
  • +
  • Exercise 3: Normalizing our data
  • +
  • Exercise 4: Adding Ridge Regression
  • +
  • Exercise 5: Analytical exercises
  • @@ -382,29 +391,45 @@ MathJax.Hub.Config({

     

     

     

    -

    Interpretations and optimizing our parameters

    -
    -
    - -

    The residuals \( \boldsymbol{\epsilon} \) are in turn given by

    +

    Derivatives, example 1

    + +

    Let now \( \boldsymbol{y}=\boldsymbol{A}\boldsymbol{x} \), where \( \boldsymbol{A} \) is an \( m\times n \) matrix and the matrix does not depend on \( \boldsymbol{x} \). If we write out the vector \( \boldsymbol{y} \) compoment by component we have

    + $$ -\boldsymbol{\epsilon} = \boldsymbol{y}-\boldsymbol{\tilde{y}} = \boldsymbol{y}-\boldsymbol{X}\boldsymbol{\beta}, +y_i = \sum_{j=0}^{n-1}a_{ij}x_j, $$ -

    and with

    +

    with \( \all i=0,1,2,\dots,m-1 \). The individual matrix elements of \( \boldsymbol{A} \) are given by the symbol \( a_{ij} \). +It follows that the partial derivatives of \( y_i \) with respect to \( x_k \) +

    $$ -\boldsymbol{X}^T\left( \boldsymbol{y}-\boldsymbol{X}\boldsymbol{\beta}\right)= 0, +\frac{\partial y_i }{\partial x_k}= a_{ik} \all i=0,1,2,\dots,m-1. $$ -

    we have

    +

    From this we have, using the definition of the Jacobian

    + $$ -\boldsymbol{X}^T\boldsymbol{\epsilon}=\boldsymbol{X}^T\left( \boldsymbol{y}-\boldsymbol{X}\boldsymbol{\beta}\right)= 0, +\frac{\partial \boldsymbol{y} }{\partial \boldsymbol{x}}= \boldsymbol{A}. $$ -

    meaning that the solution for \( \boldsymbol{\beta} \) is the one which minimizes the residuals. Later we will link this with the maximum likelihood approach.

    -
    -
    +$$ +\frac{\partial (\boldsymbol{b}^T\boldsymbol{a})}{\partial \boldsymbol{a}} = \boldsymbol{b}, +$$ + +$$ +\frac{\partial (\boldsymbol{a}^T\boldsymbol{A}\boldsymbol{a})}{\partial \boldsymbol{a}} = (\boldsymbol{A}+\boldsymbol{A}^T)\boldsymbol{a}, +$$ + +$$ +\frac{\partial tr(\boldsymbol{B}\boldsymbol{A})}{\partial \boldsymbol{A}} = \boldsymbol{B}^T, +$$ + +$$ +\frac{\partial \log{\vert\boldsymbol{A}\vert}}{\partial \boldsymbol{A}} = (\boldsymbol{A}^{-1})^T. +$$ + +

    See the jupyter-book (complete lecture notes) for the derivations of these relations.

    @@ -431,7 +456,7 @@ $$

  • 27
  • 28
  • ...
  • -
  • 68
  • +
  • 71
  • »
  • diff --git a/doc/pub/week35/html/._week35-bs019.html b/doc/pub/week35/html/._week35-bs019.html index ce304ac04..258b9aa69 100644 --- a/doc/pub/week35/html/._week35-bs019.html +++ b/doc/pub/week35/html/._week35-bs019.html @@ -74,10 +74,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'optimizing-our-parameters'), - ('Our model for the nuclear binding energies', + ('Examples relevant for the exercises', 2, None, - 'our-model-for-the-nuclear-binding-energies'), + 'examples-relevant-for-the-exercises'), ('Optimizing our parameters, more details', 2, None, @@ -94,6 +94,8 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'some-useful-matrix-and-vector-expressions'), + ('The Jacobian', 2, None, 'the-jacobian'), + ('Derivatives, example 1', 2, None, 'derivatives-example-1'), ('Meet the Hessian Matrix', 2, None, 'meet-the-hessian-matrix'), ('Interpretations and optimizing our parameters', 2, @@ -148,6 +150,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'more-preprocessing-examples-franke-function-and-regression'), + ('Material for lecture Thursday, August 31', + 2, + None, + 'material-for-lecture-thursday-august-31'), ('Mathematical Interpretation of Ordinary Least Squares', 2, None, @@ -303,74 +309,77 @@ MathJax.Hub.Config({
  • Why Linear Regression (aka Ordinary Least Squares and family), repeat from last week
  • Regression analysis, overarching aims
  • Regression analysis, overarching aims II
  • -
  • Examples
  • +
  • Examples
  • General linear models
  • Rewriting the fitting procedure as a linear algebra problem
  • Rewriting the fitting procedure as a linear algebra problem, more details
  • Generalizing the fitting procedure as a linear algebra problem
  • Generalizing the fitting procedure as a linear algebra problem
  • Optimizing our parameters
  • -
  • Our model for the nuclear binding energies
  • +
  • Examples relevant for the exercises
  • Optimizing our parameters, more details
  • -
  • Interpretations and optimizing our parameters
  • -
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • Some useful matrix and vector expressions
  • -
  • Meet the Hessian Matrix
  • -
  • Interpretations and optimizing our parameters
  • -
  • Own code for Ordinary Least Squares
  • -
  • Adding error analysis and training set up
  • -
  • Splitting our Data in Training and Test data
  • -
  • Examples
  • -
  • Making your own test-train splitting
  • -
  • The Boston housing data example
  • -
  • Housing data, the code
  • -
  • Reducing the number of degrees of freedom, overarching view
  • -
  • Preprocessing our data
  • -
  • Functionality in Scikit-Learn
  • -
  • More preprocessing
  • -
  • Frequently used scaling functions
  • -
  • Example of own Standard scaling
  • -
  • Min-Max Scaling
  • -
  • Testing the Means Squared Error as function of Complexity
  • -
  • More preprocessing examples, Franke function and regression
  • -
  • Mathematical Interpretation of Ordinary Least Squares
  • -
  • Residual Error
  • -
  • Simple case
  • -
  • The singular value decomposition
  • -
  • Linear Regression Problems
  • -
  • Fixing the singularity
  • -
  • Basic math of the SVD
  • -
  • The SVD, a Fantastic Algorithm
  • -
  • Economy-size SVD
  • -
  • Codes for the SVD
  • -
  • Note about SVD Calculations
  • -
  • Mathematics of the SVD and implications
  • -
  • Example Matrix
  • -
  • Setting up the Matrix to be inverted
  • -
  • Further properties (important for our analyses later)
  • -
  • Meet the Covariance Matrix
  • -
  • Introducing the Covariance and Correlation functions
  • -
  • Covariance and Correlation Matrix
  • -
  • Correlation Function and Design/Feature Matrix
  • -
  • Covariance Matrix Examples
  • -
  • Correlation Matrix
  • -
  • Correlation Matrix with Pandas
  • -
  • Correlation Matrix with Pandas and the Franke function
  • -
  • Rewriting the Covariance and/or Correlation Matrix
  • -
  • Linking with the SVD
  • -
  • What does it mean?
  • -
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • -
  • Ridge and LASSO Regression
  • -
  • Deriving the Ridge Regression Equations
  • -
  • Interpreting the Ridge results
  • -
  • More interpretations
  • -
  • Deriving the Lasso Regression Equations
  • -
  • Exercises for week 35
  • -
  • Exercise 1: Setting up various Python environments
  • -
  • Exercise 2: making your own data and exploring scikit-learn
  • -
  • Exercise 3: Normalizing our data
  • -
  • Exercise 4: Adding Ridge Regression
  • -
  • Exercise 5: Analytical exercises
  • +
  • The Jacobian
  • +
  • Derivatives, example 1
  • +
  • Meet the Hessian Matrix
  • +
  • Interpretations and optimizing our parameters
  • +
  • Own code for Ordinary Least Squares
  • +
  • Adding error analysis and training set up
  • +
  • Splitting our Data in Training and Test data
  • +
  • Examples
  • +
  • Making your own test-train splitting
  • +
  • The Boston housing data example
  • +
  • Housing data, the code
  • +
  • Reducing the number of degrees of freedom, overarching view
  • +
  • Preprocessing our data
  • +
  • Functionality in Scikit-Learn
  • +
  • More preprocessing
  • +
  • Frequently used scaling functions
  • +
  • Example of own Standard scaling
  • +
  • Min-Max Scaling
  • +
  • Testing the Means Squared Error as function of Complexity
  • +
  • More preprocessing examples, Franke function and regression
  • +
  • Material for lecture Thursday, August 31
  • +
  • Mathematical Interpretation of Ordinary Least Squares
  • +
  • Residual Error
  • +
  • Simple case
  • +
  • The singular value decomposition
  • +
  • Linear Regression Problems
  • +
  • Fixing the singularity
  • +
  • Basic math of the SVD
  • +
  • The SVD, a Fantastic Algorithm
  • +
  • Economy-size SVD
  • +
  • Codes for the SVD
  • +
  • Note about SVD Calculations
  • +
  • Mathematics of the SVD and implications
  • +
  • Example Matrix
  • +
  • Setting up the Matrix to be inverted
  • +
  • Further properties (important for our analyses later)
  • +
  • Meet the Covariance Matrix
  • +
  • Introducing the Covariance and Correlation functions
  • +
  • Covariance and Correlation Matrix
  • +
  • Correlation Function and Design/Feature Matrix
  • +
  • Covariance Matrix Examples
  • +
  • Correlation Matrix
  • +
  • Correlation Matrix with Pandas
  • +
  • Correlation Matrix with Pandas and the Franke function
  • +
  • Rewriting the Covariance and/or Correlation Matrix
  • +
  • Linking with the SVD
  • +
  • What does it mean?
  • +
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • +
  • Ridge and LASSO Regression
  • +
  • Deriving the Ridge Regression Equations
  • +
  • Interpreting the Ridge results
  • +
  • More interpretations
  • +
  • Deriving the Lasso Regression Equations
  • +
  • Exercises for week 35
  • +
  • Exercise 1: Setting up various Python environments
  • +
  • Exercise 2: making your own data and exploring scikit-learn
  • +
  • Exercise 3: Normalizing our data
  • +
  • Exercise 4: Adding Ridge Regression
  • +
  • Exercise 5: Analytical exercises
  • @@ -382,97 +391,35 @@ MathJax.Hub.Config({

     

     

     

    -

    Own code for Ordinary Least Squares

    +

    Meet the Hessian Matrix

    -

    It is rather straightforward to implement the matrix inversion and obtain the parameters \( \boldsymbol{\beta} \). After having defined the matrix \( \boldsymbol{X} \) we simply need to -write +

    A very important matrix we will meet again and again in Machine +Learning is the Hessian. It is given by the second derivative of the +cost function with respect to the parameter \( \beta \). Using the above +expression for derivatives of vectors and matrices, we find that the +second derivative of the cost function is,

    - -
    -
    -
    -
    -
    -
    # matrix inversion to find beta
    -beta = np.linalg.inv(X.T.dot(X)).dot(X.T).dot(Energies)
    -# and then make the prediction
    -ytilde = X @ beta
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    +$$ +\frac{\partial}{\partial \boldsymbol{\beta}^T}\frac{\partial C(\boldsymbol{\beta})}{\partial \boldsymbol{\beta}} =\frac{\partial}{\partial \boldsymbol{\beta}}\left[-\frac{2}{n}\boldsymbol{X}^T\left( \boldsymbol{y}-\boldsymbol{X}\boldsymbol{\beta}\right)\right]=\frac{2}{n}\boldsymbol{X}^T\boldsymbol{X}. +$$ -

    Alternatively, you can use the least squares functionality in Numpy as

    +

    The Hessian matrix plays an important role and is defined here as

    - -
    -
    -
    -
    -
    -
    fit = np.linalg.lstsq(X, Energies, rcond =None)[0]
    -ytildenp = np.dot(fit,X.T)
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    +$$ +\boldsymbol{H}=\boldsymbol{X}^T\boldsymbol{X}. +$$ -

    And finally we plot our fit with and compare with data

    - - -
    -
    -
    -
    -
    -
    Masses['Eapprox']  = ytilde
    -# Generate a plot comparing the experimental with the fitted values values.
    -fig, ax = plt.subplots()
    -ax.set_xlabel(r'$A = N + Z$')
    -ax.set_ylabel(r'$E_\mathrm{bind}\,/\mathrm{MeV}$')
    -ax.plot(Masses['A'], Masses['Ebinding'], alpha=0.7, lw=2,
    -            label='Ame2016')
    -ax.plot(Masses['A'], Masses['Eapprox'], alpha=0.7, lw=2, c='m',
    -            label='Fit')
    -ax.legend()
    -save_fig("Masses2016OLS")
    -plt.show()
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    +

    For ordinary least squares, it is inversely proportional (derivation +next week) with the variance of the optimal parameters +\( \hat{\boldsymbol{\beta}} \). Furthermore, we will see later this week that is +(beside \( 1/n \)) equal to the covariance matrix. It plays also a very +important role in optmization algorithms and Principal Component +Analysis as a way to reduce the dimensionality of a machine learning +problem. +

    +

    Linear algebra question: Can we use the Hessian matrix to say something about properties of the cost function (our optmization problem)? (hint: think about convex or concave problems and how to relate these to a matrix!).

    @@ -499,7 +446,7 @@ plt.show()

  • 28
  • 29
  • ...
  • -
  • 68
  • +
  • 71
  • »
  • diff --git a/doc/pub/week35/html/._week35-bs020.html b/doc/pub/week35/html/._week35-bs020.html index 6e183d78d..7acea4ed4 100644 --- a/doc/pub/week35/html/._week35-bs020.html +++ b/doc/pub/week35/html/._week35-bs020.html @@ -74,10 +74,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'optimizing-our-parameters'), - ('Our model for the nuclear binding energies', + ('Examples relevant for the exercises', 2, None, - 'our-model-for-the-nuclear-binding-energies'), + 'examples-relevant-for-the-exercises'), ('Optimizing our parameters, more details', 2, None, @@ -94,6 +94,8 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'some-useful-matrix-and-vector-expressions'), + ('The Jacobian', 2, None, 'the-jacobian'), + ('Derivatives, example 1', 2, None, 'derivatives-example-1'), ('Meet the Hessian Matrix', 2, None, 'meet-the-hessian-matrix'), ('Interpretations and optimizing our parameters', 2, @@ -148,6 +150,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'more-preprocessing-examples-franke-function-and-regression'), + ('Material for lecture Thursday, August 31', + 2, + None, + 'material-for-lecture-thursday-august-31'), ('Mathematical Interpretation of Ordinary Least Squares', 2, None, @@ -303,74 +309,77 @@ MathJax.Hub.Config({
  • Why Linear Regression (aka Ordinary Least Squares and family), repeat from last week
  • Regression analysis, overarching aims
  • Regression analysis, overarching aims II
  • -
  • Examples
  • +
  • Examples
  • General linear models
  • Rewriting the fitting procedure as a linear algebra problem
  • Rewriting the fitting procedure as a linear algebra problem, more details
  • Generalizing the fitting procedure as a linear algebra problem
  • Generalizing the fitting procedure as a linear algebra problem
  • Optimizing our parameters
  • -
  • Our model for the nuclear binding energies
  • +
  • Examples relevant for the exercises
  • Optimizing our parameters, more details
  • -
  • Interpretations and optimizing our parameters
  • -
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • Some useful matrix and vector expressions
  • -
  • Meet the Hessian Matrix
  • -
  • Interpretations and optimizing our parameters
  • -
  • Own code for Ordinary Least Squares
  • -
  • Adding error analysis and training set up
  • -
  • Splitting our Data in Training and Test data
  • -
  • Examples
  • -
  • Making your own test-train splitting
  • -
  • The Boston housing data example
  • -
  • Housing data, the code
  • -
  • Reducing the number of degrees of freedom, overarching view
  • -
  • Preprocessing our data
  • -
  • Functionality in Scikit-Learn
  • -
  • More preprocessing
  • -
  • Frequently used scaling functions
  • -
  • Example of own Standard scaling
  • -
  • Min-Max Scaling
  • -
  • Testing the Means Squared Error as function of Complexity
  • -
  • More preprocessing examples, Franke function and regression
  • -
  • Mathematical Interpretation of Ordinary Least Squares
  • -
  • Residual Error
  • -
  • Simple case
  • -
  • The singular value decomposition
  • -
  • Linear Regression Problems
  • -
  • Fixing the singularity
  • -
  • Basic math of the SVD
  • -
  • The SVD, a Fantastic Algorithm
  • -
  • Economy-size SVD
  • -
  • Codes for the SVD
  • -
  • Note about SVD Calculations
  • -
  • Mathematics of the SVD and implications
  • -
  • Example Matrix
  • -
  • Setting up the Matrix to be inverted
  • -
  • Further properties (important for our analyses later)
  • -
  • Meet the Covariance Matrix
  • -
  • Introducing the Covariance and Correlation functions
  • -
  • Covariance and Correlation Matrix
  • -
  • Correlation Function and Design/Feature Matrix
  • -
  • Covariance Matrix Examples
  • -
  • Correlation Matrix
  • -
  • Correlation Matrix with Pandas
  • -
  • Correlation Matrix with Pandas and the Franke function
  • -
  • Rewriting the Covariance and/or Correlation Matrix
  • -
  • Linking with the SVD
  • -
  • What does it mean?
  • -
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • -
  • Ridge and LASSO Regression
  • -
  • Deriving the Ridge Regression Equations
  • -
  • Interpreting the Ridge results
  • -
  • More interpretations
  • -
  • Deriving the Lasso Regression Equations
  • -
  • Exercises for week 35
  • -
  • Exercise 1: Setting up various Python environments
  • -
  • Exercise 2: making your own data and exploring scikit-learn
  • -
  • Exercise 3: Normalizing our data
  • -
  • Exercise 4: Adding Ridge Regression
  • -
  • Exercise 5: Analytical exercises
  • +
  • The Jacobian
  • +
  • Derivatives, example 1
  • +
  • Meet the Hessian Matrix
  • +
  • Interpretations and optimizing our parameters
  • +
  • Own code for Ordinary Least Squares
  • +
  • Adding error analysis and training set up
  • +
  • Splitting our Data in Training and Test data
  • +
  • Examples
  • +
  • Making your own test-train splitting
  • +
  • The Boston housing data example
  • +
  • Housing data, the code
  • +
  • Reducing the number of degrees of freedom, overarching view
  • +
  • Preprocessing our data
  • +
  • Functionality in Scikit-Learn
  • +
  • More preprocessing
  • +
  • Frequently used scaling functions
  • +
  • Example of own Standard scaling
  • +
  • Min-Max Scaling
  • +
  • Testing the Means Squared Error as function of Complexity
  • +
  • More preprocessing examples, Franke function and regression
  • +
  • Material for lecture Thursday, August 31
  • +
  • Mathematical Interpretation of Ordinary Least Squares
  • +
  • Residual Error
  • +
  • Simple case
  • +
  • The singular value decomposition
  • +
  • Linear Regression Problems
  • +
  • Fixing the singularity
  • +
  • Basic math of the SVD
  • +
  • The SVD, a Fantastic Algorithm
  • +
  • Economy-size SVD
  • +
  • Codes for the SVD
  • +
  • Note about SVD Calculations
  • +
  • Mathematics of the SVD and implications
  • +
  • Example Matrix
  • +
  • Setting up the Matrix to be inverted
  • +
  • Further properties (important for our analyses later)
  • +
  • Meet the Covariance Matrix
  • +
  • Introducing the Covariance and Correlation functions
  • +
  • Covariance and Correlation Matrix
  • +
  • Correlation Function and Design/Feature Matrix
  • +
  • Covariance Matrix Examples
  • +
  • Correlation Matrix
  • +
  • Correlation Matrix with Pandas
  • +
  • Correlation Matrix with Pandas and the Franke function
  • +
  • Rewriting the Covariance and/or Correlation Matrix
  • +
  • Linking with the SVD
  • +
  • What does it mean?
  • +
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • +
  • Ridge and LASSO Regression
  • +
  • Deriving the Ridge Regression Equations
  • +
  • Interpreting the Ridge results
  • +
  • More interpretations
  • +
  • Deriving the Lasso Regression Equations
  • +
  • Exercises for week 35
  • +
  • Exercise 1: Setting up various Python environments
  • +
  • Exercise 2: making your own data and exploring scikit-learn
  • +
  • Exercise 3: Normalizing our data
  • +
  • Exercise 4: Adding Ridge Regression
  • +
  • Exercise 5: Analytical exercises
  • @@ -382,111 +391,27 @@ MathJax.Hub.Config({

     

     

     

    -

    Adding error analysis and training set up

    +

    Interpretations and optimizing our parameters

    +
    +
    + +

    The residuals \( \boldsymbol{\epsilon} \) are in turn given by

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

    We can easily test our fit by computing the \( R2 \) score that we discussed in connection with the functionality of Scikit-Learn in the introductory slides. -Since we are not using Scikit-Learn here we can define our own \( R2 \) function as -

    +

    and with

    +$$ +\boldsymbol{X}^T\left( \boldsymbol{y}-\boldsymbol{X}\boldsymbol{\beta}\right)= 0, +$$ - -
    -
    -
    -
    -
    -
    def R2(y_data, y_model):
    -    return 1 - np.sum((y_data - y_model) ** 2) / np.sum((y_data - np.mean(y_data)) ** 2)
    -
    +

    we have

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

    meaning that the solution for \( \boldsymbol{\beta} \) is the one which minimizes the residuals. Later we will link this with the maximum likelihood approach.

    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    - -

    and we would be using it as

    - - -
    -
    -
    -
    -
    -
    print(R2(Energies,ytilde))
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    - -

    We can easily add our MSE score as

    - - -
    -
    -
    -
    -
    -
    def MSE(y_data,y_model):
    -    n = np.size(y_model)
    -    return np.sum((y_data-y_model)**2)/n
    -
    -print(MSE(Energies,ytilde))
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    - -

    and finally the relative error as

    - - -
    -
    -
    -
    -
    -
    def RelativeError(y_data,y_model):
    -    return abs((y_data-y_model)/y_data)
    -print(RelativeError(Energies, ytilde))
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    @@ -515,7 +440,7 @@ Since we are not using Scikit-Learn here we can define our own \( R2 \) f
  • 29
  • 30
  • ...
  • -
  • 68
  • +
  • 71
  • »
  • diff --git a/doc/pub/week35/html/._week35-bs021.html b/doc/pub/week35/html/._week35-bs021.html index e709e75c2..94dfaf52d 100644 --- a/doc/pub/week35/html/._week35-bs021.html +++ b/doc/pub/week35/html/._week35-bs021.html @@ -74,10 +74,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'optimizing-our-parameters'), - ('Our model for the nuclear binding energies', + ('Examples relevant for the exercises', 2, None, - 'our-model-for-the-nuclear-binding-energies'), + 'examples-relevant-for-the-exercises'), ('Optimizing our parameters, more details', 2, None, @@ -94,6 +94,8 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'some-useful-matrix-and-vector-expressions'), + ('The Jacobian', 2, None, 'the-jacobian'), + ('Derivatives, example 1', 2, None, 'derivatives-example-1'), ('Meet the Hessian Matrix', 2, None, 'meet-the-hessian-matrix'), ('Interpretations and optimizing our parameters', 2, @@ -148,6 +150,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'more-preprocessing-examples-franke-function-and-regression'), + ('Material for lecture Thursday, August 31', + 2, + None, + 'material-for-lecture-thursday-august-31'), ('Mathematical Interpretation of Ordinary Least Squares', 2, None, @@ -303,74 +309,77 @@ MathJax.Hub.Config({
  • Why Linear Regression (aka Ordinary Least Squares and family), repeat from last week
  • Regression analysis, overarching aims
  • Regression analysis, overarching aims II
  • -
  • Examples
  • +
  • Examples
  • General linear models
  • Rewriting the fitting procedure as a linear algebra problem
  • Rewriting the fitting procedure as a linear algebra problem, more details
  • Generalizing the fitting procedure as a linear algebra problem
  • Generalizing the fitting procedure as a linear algebra problem
  • Optimizing our parameters
  • -
  • Our model for the nuclear binding energies
  • +
  • Examples relevant for the exercises
  • Optimizing our parameters, more details
  • -
  • Interpretations and optimizing our parameters
  • -
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • Some useful matrix and vector expressions
  • -
  • Meet the Hessian Matrix
  • -
  • Interpretations and optimizing our parameters
  • -
  • Own code for Ordinary Least Squares
  • -
  • Adding error analysis and training set up
  • -
  • Splitting our Data in Training and Test data
  • -
  • Examples
  • -
  • Making your own test-train splitting
  • -
  • The Boston housing data example
  • -
  • Housing data, the code
  • -
  • Reducing the number of degrees of freedom, overarching view
  • -
  • Preprocessing our data
  • -
  • Functionality in Scikit-Learn
  • -
  • More preprocessing
  • -
  • Frequently used scaling functions
  • -
  • Example of own Standard scaling
  • -
  • Min-Max Scaling
  • -
  • Testing the Means Squared Error as function of Complexity
  • -
  • More preprocessing examples, Franke function and regression
  • -
  • Mathematical Interpretation of Ordinary Least Squares
  • -
  • Residual Error
  • -
  • Simple case
  • -
  • The singular value decomposition
  • -
  • Linear Regression Problems
  • -
  • Fixing the singularity
  • -
  • Basic math of the SVD
  • -
  • The SVD, a Fantastic Algorithm
  • -
  • Economy-size SVD
  • -
  • Codes for the SVD
  • -
  • Note about SVD Calculations
  • -
  • Mathematics of the SVD and implications
  • -
  • Example Matrix
  • -
  • Setting up the Matrix to be inverted
  • -
  • Further properties (important for our analyses later)
  • -
  • Meet the Covariance Matrix
  • -
  • Introducing the Covariance and Correlation functions
  • -
  • Covariance and Correlation Matrix
  • -
  • Correlation Function and Design/Feature Matrix
  • -
  • Covariance Matrix Examples
  • -
  • Correlation Matrix
  • -
  • Correlation Matrix with Pandas
  • -
  • Correlation Matrix with Pandas and the Franke function
  • -
  • Rewriting the Covariance and/or Correlation Matrix
  • -
  • Linking with the SVD
  • -
  • What does it mean?
  • -
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • -
  • Ridge and LASSO Regression
  • -
  • Deriving the Ridge Regression Equations
  • -
  • Interpreting the Ridge results
  • -
  • More interpretations
  • -
  • Deriving the Lasso Regression Equations
  • -
  • Exercises for week 35
  • -
  • Exercise 1: Setting up various Python environments
  • -
  • Exercise 2: making your own data and exploring scikit-learn
  • -
  • Exercise 3: Normalizing our data
  • -
  • Exercise 4: Adding Ridge Regression
  • -
  • Exercise 5: Analytical exercises
  • +
  • The Jacobian
  • +
  • Derivatives, example 1
  • +
  • Meet the Hessian Matrix
  • +
  • Interpretations and optimizing our parameters
  • +
  • Own code for Ordinary Least Squares
  • +
  • Adding error analysis and training set up
  • +
  • Splitting our Data in Training and Test data
  • +
  • Examples
  • +
  • Making your own test-train splitting
  • +
  • The Boston housing data example
  • +
  • Housing data, the code
  • +
  • Reducing the number of degrees of freedom, overarching view
  • +
  • Preprocessing our data
  • +
  • Functionality in Scikit-Learn
  • +
  • More preprocessing
  • +
  • Frequently used scaling functions
  • +
  • Example of own Standard scaling
  • +
  • Min-Max Scaling
  • +
  • Testing the Means Squared Error as function of Complexity
  • +
  • More preprocessing examples, Franke function and regression
  • +
  • Material for lecture Thursday, August 31
  • +
  • Mathematical Interpretation of Ordinary Least Squares
  • +
  • Residual Error
  • +
  • Simple case
  • +
  • The singular value decomposition
  • +
  • Linear Regression Problems
  • +
  • Fixing the singularity
  • +
  • Basic math of the SVD
  • +
  • The SVD, a Fantastic Algorithm
  • +
  • Economy-size SVD
  • +
  • Codes for the SVD
  • +
  • Note about SVD Calculations
  • +
  • Mathematics of the SVD and implications
  • +
  • Example Matrix
  • +
  • Setting up the Matrix to be inverted
  • +
  • Further properties (important for our analyses later)
  • +
  • Meet the Covariance Matrix
  • +
  • Introducing the Covariance and Correlation functions
  • +
  • Covariance and Correlation Matrix
  • +
  • Correlation Function and Design/Feature Matrix
  • +
  • Covariance Matrix Examples
  • +
  • Correlation Matrix
  • +
  • Correlation Matrix with Pandas
  • +
  • Correlation Matrix with Pandas and the Franke function
  • +
  • Rewriting the Covariance and/or Correlation Matrix
  • +
  • Linking with the SVD
  • +
  • What does it mean?
  • +
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • +
  • Ridge and LASSO Regression
  • +
  • Deriving the Ridge Regression Equations
  • +
  • Interpreting the Ridge results
  • +
  • More interpretations
  • +
  • Deriving the Lasso Regression Equations
  • +
  • Exercises for week 35
  • +
  • Exercise 1: Setting up various Python environments
  • +
  • Exercise 2: making your own data and exploring scikit-learn
  • +
  • Exercise 3: Normalizing our data
  • +
  • Exercise 4: Adding Ridge Regression
  • +
  • Exercise 5: Analytical exercises
  • @@ -382,24 +391,95 @@ MathJax.Hub.Config({

     

     

     

    -

    Splitting our Data in Training and Test data

    +

    Own code for Ordinary Least Squares

    -
    -
    - - -

    It is normal in essentially all Machine Learning studies to split the -data in a training set and a test set (sometimes also an additional -validation set). Scikit-Learn has an own function for this. There -is no explicit recipe for how much data should be included as training -data and say test data. An accepted rule of thumb is to use -approximately \( 2/3 \) to \( 4/5 \) of the data as training data. We will -postpone a discussion of this splitting to the end of these notes and -our discussion of the so-called bias-variance tradeoff. Here we -limit ourselves to repeat the above equation of state fitting example -but now splitting the data into a training set and a test set. +

    It is rather straightforward to implement the matrix inversion and obtain the parameters \( \boldsymbol{\beta} \). After having defined the matrix \( \boldsymbol{X} \) we simply need to +write

    + + +
    +
    +
    +
    +
    +
    # matrix inversion to find beta
    +beta = np.linalg.inv(X.T.dot(X)).dot(X.T).dot(Energies)
    +# and then make the prediction
    +ytilde = X @ beta
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    + +

    Alternatively, you can use the least squares functionality in Numpy as

    + + +
    +
    +
    +
    +
    +
    fit = np.linalg.lstsq(X, Energies, rcond =None)[0]
    +ytildenp = np.dot(fit,X.T)
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    + +

    And finally we plot our fit with and compare with data

    + + +
    +
    +
    +
    +
    +
    Masses['Eapprox']  = ytilde
    +# Generate a plot comparing the experimental with the fitted values values.
    +fig, ax = plt.subplots()
    +ax.set_xlabel(r'$A = N + Z$')
    +ax.set_ylabel(r'$E_\mathrm{bind}\,/\mathrm{MeV}$')
    +ax.plot(Masses['A'], Masses['Ebinding'], alpha=0.7, lw=2,
    +            label='Ame2016')
    +ax.plot(Masses['A'], Masses['Eapprox'], alpha=0.7, lw=2, c='m',
    +            label='Fit')
    +ax.legend()
    +save_fig("Masses2016OLS")
    +plt.show()
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    @@ -428,7 +508,7 @@ but now splitting the data into a training set and a test set.
  • 30
  • 31
  • ...
  • -
  • 68
  • +
  • 71
  • »
  • diff --git a/doc/pub/week35/html/._week35-bs022.html b/doc/pub/week35/html/._week35-bs022.html index 0d21f1237..ee5afaaa9 100644 --- a/doc/pub/week35/html/._week35-bs022.html +++ b/doc/pub/week35/html/._week35-bs022.html @@ -74,10 +74,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'optimizing-our-parameters'), - ('Our model for the nuclear binding energies', + ('Examples relevant for the exercises', 2, None, - 'our-model-for-the-nuclear-binding-energies'), + 'examples-relevant-for-the-exercises'), ('Optimizing our parameters, more details', 2, None, @@ -94,6 +94,8 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'some-useful-matrix-and-vector-expressions'), + ('The Jacobian', 2, None, 'the-jacobian'), + ('Derivatives, example 1', 2, None, 'derivatives-example-1'), ('Meet the Hessian Matrix', 2, None, 'meet-the-hessian-matrix'), ('Interpretations and optimizing our parameters', 2, @@ -148,6 +150,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'more-preprocessing-examples-franke-function-and-regression'), + ('Material for lecture Thursday, August 31', + 2, + None, + 'material-for-lecture-thursday-august-31'), ('Mathematical Interpretation of Ordinary Least Squares', 2, None, @@ -303,74 +309,77 @@ MathJax.Hub.Config({
  • Why Linear Regression (aka Ordinary Least Squares and family), repeat from last week
  • Regression analysis, overarching aims
  • Regression analysis, overarching aims II
  • -
  • Examples
  • +
  • Examples
  • General linear models
  • Rewriting the fitting procedure as a linear algebra problem
  • Rewriting the fitting procedure as a linear algebra problem, more details
  • Generalizing the fitting procedure as a linear algebra problem
  • Generalizing the fitting procedure as a linear algebra problem
  • Optimizing our parameters
  • -
  • Our model for the nuclear binding energies
  • +
  • Examples relevant for the exercises
  • Optimizing our parameters, more details
  • -
  • Interpretations and optimizing our parameters
  • -
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • Some useful matrix and vector expressions
  • -
  • Meet the Hessian Matrix
  • -
  • Interpretations and optimizing our parameters
  • -
  • Own code for Ordinary Least Squares
  • -
  • Adding error analysis and training set up
  • -
  • Splitting our Data in Training and Test data
  • -
  • Examples
  • -
  • Making your own test-train splitting
  • -
  • The Boston housing data example
  • -
  • Housing data, the code
  • -
  • Reducing the number of degrees of freedom, overarching view
  • -
  • Preprocessing our data
  • -
  • Functionality in Scikit-Learn
  • -
  • More preprocessing
  • -
  • Frequently used scaling functions
  • -
  • Example of own Standard scaling
  • -
  • Min-Max Scaling
  • -
  • Testing the Means Squared Error as function of Complexity
  • -
  • More preprocessing examples, Franke function and regression
  • -
  • Mathematical Interpretation of Ordinary Least Squares
  • -
  • Residual Error
  • -
  • Simple case
  • -
  • The singular value decomposition
  • -
  • Linear Regression Problems
  • -
  • Fixing the singularity
  • -
  • Basic math of the SVD
  • -
  • The SVD, a Fantastic Algorithm
  • -
  • Economy-size SVD
  • -
  • Codes for the SVD
  • -
  • Note about SVD Calculations
  • -
  • Mathematics of the SVD and implications
  • -
  • Example Matrix
  • -
  • Setting up the Matrix to be inverted
  • -
  • Further properties (important for our analyses later)
  • -
  • Meet the Covariance Matrix
  • -
  • Introducing the Covariance and Correlation functions
  • -
  • Covariance and Correlation Matrix
  • -
  • Correlation Function and Design/Feature Matrix
  • -
  • Covariance Matrix Examples
  • -
  • Correlation Matrix
  • -
  • Correlation Matrix with Pandas
  • -
  • Correlation Matrix with Pandas and the Franke function
  • -
  • Rewriting the Covariance and/or Correlation Matrix
  • -
  • Linking with the SVD
  • -
  • What does it mean?
  • -
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • -
  • Ridge and LASSO Regression
  • -
  • Deriving the Ridge Regression Equations
  • -
  • Interpreting the Ridge results
  • -
  • More interpretations
  • -
  • Deriving the Lasso Regression Equations
  • -
  • Exercises for week 35
  • -
  • Exercise 1: Setting up various Python environments
  • -
  • Exercise 2: making your own data and exploring scikit-learn
  • -
  • Exercise 3: Normalizing our data
  • -
  • Exercise 4: Adding Ridge Regression
  • -
  • Exercise 5: Analytical exercises
  • +
  • The Jacobian
  • +
  • Derivatives, example 1
  • +
  • Meet the Hessian Matrix
  • +
  • Interpretations and optimizing our parameters
  • +
  • Own code for Ordinary Least Squares
  • +
  • Adding error analysis and training set up
  • +
  • Splitting our Data in Training and Test data
  • +
  • Examples
  • +
  • Making your own test-train splitting
  • +
  • The Boston housing data example
  • +
  • Housing data, the code
  • +
  • Reducing the number of degrees of freedom, overarching view
  • +
  • Preprocessing our data
  • +
  • Functionality in Scikit-Learn
  • +
  • More preprocessing
  • +
  • Frequently used scaling functions
  • +
  • Example of own Standard scaling
  • +
  • Min-Max Scaling
  • +
  • Testing the Means Squared Error as function of Complexity
  • +
  • More preprocessing examples, Franke function and regression
  • +
  • Material for lecture Thursday, August 31
  • +
  • Mathematical Interpretation of Ordinary Least Squares
  • +
  • Residual Error
  • +
  • Simple case
  • +
  • The singular value decomposition
  • +
  • Linear Regression Problems
  • +
  • Fixing the singularity
  • +
  • Basic math of the SVD
  • +
  • The SVD, a Fantastic Algorithm
  • +
  • Economy-size SVD
  • +
  • Codes for the SVD
  • +
  • Note about SVD Calculations
  • +
  • Mathematics of the SVD and implications
  • +
  • Example Matrix
  • +
  • Setting up the Matrix to be inverted
  • +
  • Further properties (important for our analyses later)
  • +
  • Meet the Covariance Matrix
  • +
  • Introducing the Covariance and Correlation functions
  • +
  • Covariance and Correlation Matrix
  • +
  • Correlation Function and Design/Feature Matrix
  • +
  • Covariance Matrix Examples
  • +
  • Correlation Matrix
  • +
  • Correlation Matrix with Pandas
  • +
  • Correlation Matrix with Pandas and the Franke function
  • +
  • Rewriting the Covariance and/or Correlation Matrix
  • +
  • Linking with the SVD
  • +
  • What does it mean?
  • +
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • +
  • Ridge and LASSO Regression
  • +
  • Deriving the Ridge Regression Equations
  • +
  • Interpreting the Ridge results
  • +
  • More interpretations
  • +
  • Deriving the Lasso Regression Equations
  • +
  • Exercises for week 35
  • +
  • Exercise 1: Setting up various Python environments
  • +
  • Exercise 2: making your own data and exploring scikit-learn
  • +
  • Exercise 3: Normalizing our data
  • +
  • Exercise 4: Adding Ridge Regression
  • +
  • Exercise 5: Analytical exercises
  • @@ -382,8 +391,11 @@ MathJax.Hub.Config({

     

     

     

    -

    Examples

    +

    Adding error analysis and training set up

    +

    We can easily test our fit by computing the \( R2 \) score that we discussed in connection with the functionality of Scikit-Learn in the introductory slides. +Since we are not using Scikit-Learn here we can define our own \( R2 \) function as +

    @@ -391,44 +403,86 @@ MathJax.Hub.Config({
    -
    import os
    -import numpy as np
    -import pandas as pd
    -import matplotlib.pyplot as plt
    -from sklearn.model_selection import train_test_split
    -
    -
    -def R2(y_data, y_model):
    +  
    def R2(y_data, y_model):
         return 1 - np.sum((y_data - y_model) ** 2) / np.sum((y_data - np.mean(y_data)) ** 2)
    -def MSE(y_data,y_model):
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    + +

    and we would be using it as

    + + +
    +
    +
    +
    +
    +
    print(R2(Energies,ytilde))
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    + +

    We can easily add our MSE score as

    + + +
    +
    +
    +
    +
    +
    def MSE(y_data,y_model):
         n = np.size(y_model)
         return np.sum((y_data-y_model)**2)/n
     
    -x = np.random.rand(100)
    -y = 2.0+5*x*x+0.1*np.random.randn(100)
    +print(MSE(Energies,ytilde))
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +

    and finally the relative error as

    -# The design matrix now as function of a given polynomial -X = np.zeros((len(x),3)) -X[:,0] = 1.0 -X[:,1] = x -X[:,2] = x**2 -# We split the data in test and training data -X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2) -# matrix inversion to find beta -beta = np.linalg.inv(X_train.T @ X_train) @ X_train.T @ y_train -print(beta) -# and then make the prediction -ytilde = X_train @ beta -print("Training R2") -print(R2(y_train,ytilde)) -print("Training MSE") -print(MSE(y_train,ytilde)) -ypredict = X_test @ beta -print("Test R2") -print(R2(y_test,ypredict)) -print("Test MSE") -print(MSE(y_test,ypredict)) + +
    +
    +
    +
    +
    +
    def RelativeError(y_data,y_model):
    +    return abs((y_data-y_model)/y_data)
    +print(RelativeError(Energies, ytilde))
     
    @@ -470,7 +524,7 @@ ypredict = X_test 31
  • 32
  • ...
  • -
  • 68
  • +
  • 71
  • »
  • diff --git a/doc/pub/week35/html/._week35-bs023.html b/doc/pub/week35/html/._week35-bs023.html index 52ec3b025..cbf192752 100644 --- a/doc/pub/week35/html/._week35-bs023.html +++ b/doc/pub/week35/html/._week35-bs023.html @@ -74,10 +74,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'optimizing-our-parameters'), - ('Our model for the nuclear binding energies', + ('Examples relevant for the exercises', 2, None, - 'our-model-for-the-nuclear-binding-energies'), + 'examples-relevant-for-the-exercises'), ('Optimizing our parameters, more details', 2, None, @@ -94,6 +94,8 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'some-useful-matrix-and-vector-expressions'), + ('The Jacobian', 2, None, 'the-jacobian'), + ('Derivatives, example 1', 2, None, 'derivatives-example-1'), ('Meet the Hessian Matrix', 2, None, 'meet-the-hessian-matrix'), ('Interpretations and optimizing our parameters', 2, @@ -148,6 +150,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'more-preprocessing-examples-franke-function-and-regression'), + ('Material for lecture Thursday, August 31', + 2, + None, + 'material-for-lecture-thursday-august-31'), ('Mathematical Interpretation of Ordinary Least Squares', 2, None, @@ -303,74 +309,77 @@ MathJax.Hub.Config({
  • Why Linear Regression (aka Ordinary Least Squares and family), repeat from last week
  • Regression analysis, overarching aims
  • Regression analysis, overarching aims II
  • -
  • Examples
  • +
  • Examples
  • General linear models
  • Rewriting the fitting procedure as a linear algebra problem
  • Rewriting the fitting procedure as a linear algebra problem, more details
  • Generalizing the fitting procedure as a linear algebra problem
  • Generalizing the fitting procedure as a linear algebra problem
  • Optimizing our parameters
  • -
  • Our model for the nuclear binding energies
  • +
  • Examples relevant for the exercises
  • Optimizing our parameters, more details
  • -
  • Interpretations and optimizing our parameters
  • -
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • Some useful matrix and vector expressions
  • -
  • Meet the Hessian Matrix
  • -
  • Interpretations and optimizing our parameters
  • -
  • Own code for Ordinary Least Squares
  • -
  • Adding error analysis and training set up
  • -
  • Splitting our Data in Training and Test data
  • -
  • Examples
  • -
  • Making your own test-train splitting
  • -
  • The Boston housing data example
  • -
  • Housing data, the code
  • -
  • Reducing the number of degrees of freedom, overarching view
  • -
  • Preprocessing our data
  • -
  • Functionality in Scikit-Learn
  • -
  • More preprocessing
  • -
  • Frequently used scaling functions
  • -
  • Example of own Standard scaling
  • -
  • Min-Max Scaling
  • -
  • Testing the Means Squared Error as function of Complexity
  • -
  • More preprocessing examples, Franke function and regression
  • -
  • Mathematical Interpretation of Ordinary Least Squares
  • -
  • Residual Error
  • -
  • Simple case
  • -
  • The singular value decomposition
  • -
  • Linear Regression Problems
  • -
  • Fixing the singularity
  • -
  • Basic math of the SVD
  • -
  • The SVD, a Fantastic Algorithm
  • -
  • Economy-size SVD
  • -
  • Codes for the SVD
  • -
  • Note about SVD Calculations
  • -
  • Mathematics of the SVD and implications
  • -
  • Example Matrix
  • -
  • Setting up the Matrix to be inverted
  • -
  • Further properties (important for our analyses later)
  • -
  • Meet the Covariance Matrix
  • -
  • Introducing the Covariance and Correlation functions
  • -
  • Covariance and Correlation Matrix
  • -
  • Correlation Function and Design/Feature Matrix
  • -
  • Covariance Matrix Examples
  • -
  • Correlation Matrix
  • -
  • Correlation Matrix with Pandas
  • -
  • Correlation Matrix with Pandas and the Franke function
  • -
  • Rewriting the Covariance and/or Correlation Matrix
  • -
  • Linking with the SVD
  • -
  • What does it mean?
  • -
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • -
  • Ridge and LASSO Regression
  • -
  • Deriving the Ridge Regression Equations
  • -
  • Interpreting the Ridge results
  • -
  • More interpretations
  • -
  • Deriving the Lasso Regression Equations
  • -
  • Exercises for week 35
  • -
  • Exercise 1: Setting up various Python environments
  • -
  • Exercise 2: making your own data and exploring scikit-learn
  • -
  • Exercise 3: Normalizing our data
  • -
  • Exercise 4: Adding Ridge Regression
  • -
  • Exercise 5: Analytical exercises
  • +
  • The Jacobian
  • +
  • Derivatives, example 1
  • +
  • Meet the Hessian Matrix
  • +
  • Interpretations and optimizing our parameters
  • +
  • Own code for Ordinary Least Squares
  • +
  • Adding error analysis and training set up
  • +
  • Splitting our Data in Training and Test data
  • +
  • Examples
  • +
  • Making your own test-train splitting
  • +
  • The Boston housing data example
  • +
  • Housing data, the code
  • +
  • Reducing the number of degrees of freedom, overarching view
  • +
  • Preprocessing our data
  • +
  • Functionality in Scikit-Learn
  • +
  • More preprocessing
  • +
  • Frequently used scaling functions
  • +
  • Example of own Standard scaling
  • +
  • Min-Max Scaling
  • +
  • Testing the Means Squared Error as function of Complexity
  • +
  • More preprocessing examples, Franke function and regression
  • +
  • Material for lecture Thursday, August 31
  • +
  • Mathematical Interpretation of Ordinary Least Squares
  • +
  • Residual Error
  • +
  • Simple case
  • +
  • The singular value decomposition
  • +
  • Linear Regression Problems
  • +
  • Fixing the singularity
  • +
  • Basic math of the SVD
  • +
  • The SVD, a Fantastic Algorithm
  • +
  • Economy-size SVD
  • +
  • Codes for the SVD
  • +
  • Note about SVD Calculations
  • +
  • Mathematics of the SVD and implications
  • +
  • Example Matrix
  • +
  • Setting up the Matrix to be inverted
  • +
  • Further properties (important for our analyses later)
  • +
  • Meet the Covariance Matrix
  • +
  • Introducing the Covariance and Correlation functions
  • +
  • Covariance and Correlation Matrix
  • +
  • Correlation Function and Design/Feature Matrix
  • +
  • Covariance Matrix Examples
  • +
  • Correlation Matrix
  • +
  • Correlation Matrix with Pandas
  • +
  • Correlation Matrix with Pandas and the Franke function
  • +
  • Rewriting the Covariance and/or Correlation Matrix
  • +
  • Linking with the SVD
  • +
  • What does it mean?
  • +
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • +
  • Ridge and LASSO Regression
  • +
  • Deriving the Ridge Regression Equations
  • +
  • Interpreting the Ridge results
  • +
  • More interpretations
  • +
  • Deriving the Lasso Regression Equations
  • +
  • Exercises for week 35
  • +
  • Exercise 1: Setting up various Python environments
  • +
  • Exercise 2: making your own data and exploring scikit-learn
  • +
  • Exercise 3: Normalizing our data
  • +
  • Exercise 4: Adding Ridge Regression
  • +
  • Exercise 5: Analytical exercises
  • @@ -382,48 +391,26 @@ MathJax.Hub.Config({

     

     

     

    -

    Making your own test-train splitting

    +

    Splitting our Data in Training and Test data

    +
    +
    + - -
    -
    -
    -
    -
    -
    # equivalently in numpy
    -def train_test_split_numpy(inputs, labels, train_size, test_size):
    -    n_inputs = len(inputs)
    -    inputs_shuffled = inputs.copy()
    -    labels_shuffled = labels.copy()
    -
    -    np.random.shuffle(inputs_shuffled)
    -    np.random.shuffle(labels_shuffled)
    -
    -    train_end = int(n_inputs*train_size)
    -    X_train, X_test = inputs_shuffled[:train_end], inputs_shuffled[train_end:]
    -    Y_train, Y_test = labels_shuffled[:train_end], labels_shuffled[train_end:]
    -
    -    return X_train, X_test, Y_train, Y_test
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    - -

    But since scikit-learn has its own function for doing this and since -it interfaces easily with tensorflow and other libraries, we -normally recommend using the latter functionality. +

    It is normal in essentially all Machine Learning studies to split the +data in a training set and a test set (sometimes also an additional +validation set). Scikit-Learn has an own function for this. There +is no explicit recipe for how much data should be included as training +data and say test data. An accepted rule of thumb is to use +approximately \( 2/3 \) to \( 4/5 \) of the data as training data. We will +postpone a discussion of this splitting to the end of these notes and +our discussion of the so-called bias-variance tradeoff. Here we +limit ourselves to repeat the above equation of state fitting example +but now splitting the data into a training set and a test set.

    +
    +
    +

    @@ -450,7 +437,7 @@ normally recommend using the latter functionality.

  • 32
  • 33
  • ...
  • -
  • 68
  • +
  • 71
  • »
  • diff --git a/doc/pub/week35/html/._week35-bs024.html b/doc/pub/week35/html/._week35-bs024.html index f9fc90548..db70d660e 100644 --- a/doc/pub/week35/html/._week35-bs024.html +++ b/doc/pub/week35/html/._week35-bs024.html @@ -74,10 +74,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'optimizing-our-parameters'), - ('Our model for the nuclear binding energies', + ('Examples relevant for the exercises', 2, None, - 'our-model-for-the-nuclear-binding-energies'), + 'examples-relevant-for-the-exercises'), ('Optimizing our parameters, more details', 2, None, @@ -94,6 +94,8 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'some-useful-matrix-and-vector-expressions'), + ('The Jacobian', 2, None, 'the-jacobian'), + ('Derivatives, example 1', 2, None, 'derivatives-example-1'), ('Meet the Hessian Matrix', 2, None, 'meet-the-hessian-matrix'), ('Interpretations and optimizing our parameters', 2, @@ -148,6 +150,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'more-preprocessing-examples-franke-function-and-regression'), + ('Material for lecture Thursday, August 31', + 2, + None, + 'material-for-lecture-thursday-august-31'), ('Mathematical Interpretation of Ordinary Least Squares', 2, None, @@ -303,74 +309,77 @@ MathJax.Hub.Config({
  • Why Linear Regression (aka Ordinary Least Squares and family), repeat from last week
  • Regression analysis, overarching aims
  • Regression analysis, overarching aims II
  • -
  • Examples
  • +
  • Examples
  • General linear models
  • Rewriting the fitting procedure as a linear algebra problem
  • Rewriting the fitting procedure as a linear algebra problem, more details
  • Generalizing the fitting procedure as a linear algebra problem
  • Generalizing the fitting procedure as a linear algebra problem
  • Optimizing our parameters
  • -
  • Our model for the nuclear binding energies
  • +
  • Examples relevant for the exercises
  • Optimizing our parameters, more details
  • -
  • Interpretations and optimizing our parameters
  • -
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • Some useful matrix and vector expressions
  • -
  • Meet the Hessian Matrix
  • -
  • Interpretations and optimizing our parameters
  • -
  • Own code for Ordinary Least Squares
  • -
  • Adding error analysis and training set up
  • -
  • Splitting our Data in Training and Test data
  • -
  • Examples
  • -
  • Making your own test-train splitting
  • -
  • The Boston housing data example
  • -
  • Housing data, the code
  • -
  • Reducing the number of degrees of freedom, overarching view
  • -
  • Preprocessing our data
  • -
  • Functionality in Scikit-Learn
  • -
  • More preprocessing
  • -
  • Frequently used scaling functions
  • -
  • Example of own Standard scaling
  • -
  • Min-Max Scaling
  • -
  • Testing the Means Squared Error as function of Complexity
  • -
  • More preprocessing examples, Franke function and regression
  • -
  • Mathematical Interpretation of Ordinary Least Squares
  • -
  • Residual Error
  • -
  • Simple case
  • -
  • The singular value decomposition
  • -
  • Linear Regression Problems
  • -
  • Fixing the singularity
  • -
  • Basic math of the SVD
  • -
  • The SVD, a Fantastic Algorithm
  • -
  • Economy-size SVD
  • -
  • Codes for the SVD
  • -
  • Note about SVD Calculations
  • -
  • Mathematics of the SVD and implications
  • -
  • Example Matrix
  • -
  • Setting up the Matrix to be inverted
  • -
  • Further properties (important for our analyses later)
  • -
  • Meet the Covariance Matrix
  • -
  • Introducing the Covariance and Correlation functions
  • -
  • Covariance and Correlation Matrix
  • -
  • Correlation Function and Design/Feature Matrix
  • -
  • Covariance Matrix Examples
  • -
  • Correlation Matrix
  • -
  • Correlation Matrix with Pandas
  • -
  • Correlation Matrix with Pandas and the Franke function
  • -
  • Rewriting the Covariance and/or Correlation Matrix
  • -
  • Linking with the SVD
  • -
  • What does it mean?
  • -
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • -
  • Ridge and LASSO Regression
  • -
  • Deriving the Ridge Regression Equations
  • -
  • Interpreting the Ridge results
  • -
  • More interpretations
  • -
  • Deriving the Lasso Regression Equations
  • -
  • Exercises for week 35
  • -
  • Exercise 1: Setting up various Python environments
  • -
  • Exercise 2: making your own data and exploring scikit-learn
  • -
  • Exercise 3: Normalizing our data
  • -
  • Exercise 4: Adding Ridge Regression
  • -
  • Exercise 5: Analytical exercises
  • +
  • The Jacobian
  • +
  • Derivatives, example 1
  • +
  • Meet the Hessian Matrix
  • +
  • Interpretations and optimizing our parameters
  • +
  • Own code for Ordinary Least Squares
  • +
  • Adding error analysis and training set up
  • +
  • Splitting our Data in Training and Test data
  • +
  • Examples
  • +
  • Making your own test-train splitting
  • +
  • The Boston housing data example
  • +
  • Housing data, the code
  • +
  • Reducing the number of degrees of freedom, overarching view
  • +
  • Preprocessing our data
  • +
  • Functionality in Scikit-Learn
  • +
  • More preprocessing
  • +
  • Frequently used scaling functions
  • +
  • Example of own Standard scaling
  • +
  • Min-Max Scaling
  • +
  • Testing the Means Squared Error as function of Complexity
  • +
  • More preprocessing examples, Franke function and regression
  • +
  • Material for lecture Thursday, August 31
  • +
  • Mathematical Interpretation of Ordinary Least Squares
  • +
  • Residual Error
  • +
  • Simple case
  • +
  • The singular value decomposition
  • +
  • Linear Regression Problems
  • +
  • Fixing the singularity
  • +
  • Basic math of the SVD
  • +
  • The SVD, a Fantastic Algorithm
  • +
  • Economy-size SVD
  • +
  • Codes for the SVD
  • +
  • Note about SVD Calculations
  • +
  • Mathematics of the SVD and implications
  • +
  • Example Matrix
  • +
  • Setting up the Matrix to be inverted
  • +
  • Further properties (important for our analyses later)
  • +
  • Meet the Covariance Matrix
  • +
  • Introducing the Covariance and Correlation functions
  • +
  • Covariance and Correlation Matrix
  • +
  • Correlation Function and Design/Feature Matrix
  • +
  • Covariance Matrix Examples
  • +
  • Correlation Matrix
  • +
  • Correlation Matrix with Pandas
  • +
  • Correlation Matrix with Pandas and the Franke function
  • +
  • Rewriting the Covariance and/or Correlation Matrix
  • +
  • Linking with the SVD
  • +
  • What does it mean?
  • +
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • +
  • Ridge and LASSO Regression
  • +
  • Deriving the Ridge Regression Equations
  • +
  • Interpreting the Ridge results
  • +
  • More interpretations
  • +
  • Deriving the Lasso Regression Equations
  • +
  • Exercises for week 35
  • +
  • Exercise 1: Setting up various Python environments
  • +
  • Exercise 2: making your own data and exploring scikit-learn
  • +
  • Exercise 3: Normalizing our data
  • +
  • Exercise 4: Adding Ridge Regression
  • +
  • Exercise 5: Analytical exercises
  • @@ -381,33 +390,70 @@ MathJax.Hub.Config({

     

     

     

    - -

    The Boston housing data example

    + +

    Examples

    + + + +
    +
    +
    +
    +
    +
    import os
    +import numpy as np
    +import pandas as pd
    +import matplotlib.pyplot as plt
    +from sklearn.model_selection import train_test_split
    +
    +
    +def R2(y_data, y_model):
    +    return 1 - np.sum((y_data - y_model) ** 2) / np.sum((y_data - np.mean(y_data)) ** 2)
    +def MSE(y_data,y_model):
    +    n = np.size(y_model)
    +    return np.sum((y_data-y_model)**2)/n
    +
    +x = np.random.rand(100)
    +y = 2.0+5*x*x+0.1*np.random.randn(100)
    +
    +
    +#  The design matrix now as function of a given polynomial
    +X = np.zeros((len(x),3))
    +X[:,0] = 1.0
    +X[:,1] = x
    +X[:,2] = x**2
    +# We split the data in test and training data
    +X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
    +# matrix inversion to find beta
    +beta = np.linalg.inv(X_train.T @ X_train) @ X_train.T @ y_train
    +print(beta)
    +# and then make the prediction
    +ytilde = X_train @ beta
    +print("Training R2")
    +print(R2(y_train,ytilde))
    +print("Training MSE")
    +print(MSE(y_train,ytilde))
    +ypredict = X_test @ beta
    +print("Test R2")
    +print(R2(y_test,ypredict))
    +print("Test MSE")
    +print(MSE(y_test,ypredict))
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    -

    The Boston housing -data set was originally a part of UCI Machine Learning Repository -and has been removed now. The data set is now included in Scikit-Learn's -library. There are 506 samples and 13 feature (predictor) variables -in this data set. The objective is to predict the value of prices of -the house using the features (predictors) listed here. -

    -

    The features/predictors are

    -
      -
    1. CRIM: Per capita crime rate by town
    2. -
    3. ZN: Proportion of residential land zoned for lots over 25000 square feet
    4. -
    5. INDUS: Proportion of non-retail business acres per town
    6. -
    7. CHAS: Charles River dummy variable (= 1 if tract bounds river; 0 otherwise)
    8. -
    9. NOX: Nitric oxide concentration (parts per 10 million)
    10. -
    11. RM: Average number of rooms per dwelling
    12. -
    13. AGE: Proportion of owner-occupied units built prior to 1940
    14. -
    15. DIS: Weighted distances to five Boston employment centers
    16. -
    17. RAD: Index of accessibility to radial highways
    18. -
    19. TAX: Full-value property tax rate per USD10000
    20. -
    21. B: \( 1000(Bk - 0.63)^2 \), where \( Bk \) is the proportion of [people of African American descent] by town
    22. -
    23. LSTAT: Percentage of lower status of the population
    24. -
    25. MEDV: Median value of owner-occupied homes in USD 1000s
    26. -

      @@ -433,7 +479,7 @@ the house using the features (predictors) listed here.
    • 33
    • 34
    • ...
    • -
    • 68
    • +
    • 71
    • »
    diff --git a/doc/pub/week35/html/._week35-bs025.html b/doc/pub/week35/html/._week35-bs025.html index 2f1ea72c1..7e7330168 100644 --- a/doc/pub/week35/html/._week35-bs025.html +++ b/doc/pub/week35/html/._week35-bs025.html @@ -74,10 +74,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'optimizing-our-parameters'), - ('Our model for the nuclear binding energies', + ('Examples relevant for the exercises', 2, None, - 'our-model-for-the-nuclear-binding-energies'), + 'examples-relevant-for-the-exercises'), ('Optimizing our parameters, more details', 2, None, @@ -94,6 +94,8 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'some-useful-matrix-and-vector-expressions'), + ('The Jacobian', 2, None, 'the-jacobian'), + ('Derivatives, example 1', 2, None, 'derivatives-example-1'), ('Meet the Hessian Matrix', 2, None, 'meet-the-hessian-matrix'), ('Interpretations and optimizing our parameters', 2, @@ -148,6 +150,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'more-preprocessing-examples-franke-function-and-regression'), + ('Material for lecture Thursday, August 31', + 2, + None, + 'material-for-lecture-thursday-august-31'), ('Mathematical Interpretation of Ordinary Least Squares', 2, None, @@ -303,74 +309,77 @@ MathJax.Hub.Config({
  • Why Linear Regression (aka Ordinary Least Squares and family), repeat from last week
  • Regression analysis, overarching aims
  • Regression analysis, overarching aims II
  • -
  • Examples
  • +
  • Examples
  • General linear models
  • Rewriting the fitting procedure as a linear algebra problem
  • Rewriting the fitting procedure as a linear algebra problem, more details
  • Generalizing the fitting procedure as a linear algebra problem
  • Generalizing the fitting procedure as a linear algebra problem
  • Optimizing our parameters
  • -
  • Our model for the nuclear binding energies
  • +
  • Examples relevant for the exercises
  • Optimizing our parameters, more details
  • -
  • Interpretations and optimizing our parameters
  • -
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • Some useful matrix and vector expressions
  • -
  • Meet the Hessian Matrix
  • -
  • Interpretations and optimizing our parameters
  • -
  • Own code for Ordinary Least Squares
  • -
  • Adding error analysis and training set up
  • -
  • Splitting our Data in Training and Test data
  • -
  • Examples
  • -
  • Making your own test-train splitting
  • -
  • The Boston housing data example
  • -
  • Housing data, the code
  • -
  • Reducing the number of degrees of freedom, overarching view
  • -
  • Preprocessing our data
  • -
  • Functionality in Scikit-Learn
  • -
  • More preprocessing
  • -
  • Frequently used scaling functions
  • -
  • Example of own Standard scaling
  • -
  • Min-Max Scaling
  • -
  • Testing the Means Squared Error as function of Complexity
  • -
  • More preprocessing examples, Franke function and regression
  • -
  • Mathematical Interpretation of Ordinary Least Squares
  • -
  • Residual Error
  • -
  • Simple case
  • -
  • The singular value decomposition
  • -
  • Linear Regression Problems
  • -
  • Fixing the singularity
  • -
  • Basic math of the SVD
  • -
  • The SVD, a Fantastic Algorithm
  • -
  • Economy-size SVD
  • -
  • Codes for the SVD
  • -
  • Note about SVD Calculations
  • -
  • Mathematics of the SVD and implications
  • -
  • Example Matrix
  • -
  • Setting up the Matrix to be inverted
  • -
  • Further properties (important for our analyses later)
  • -
  • Meet the Covariance Matrix
  • -
  • Introducing the Covariance and Correlation functions
  • -
  • Covariance and Correlation Matrix
  • -
  • Correlation Function and Design/Feature Matrix
  • -
  • Covariance Matrix Examples
  • -
  • Correlation Matrix
  • -
  • Correlation Matrix with Pandas
  • -
  • Correlation Matrix with Pandas and the Franke function
  • -
  • Rewriting the Covariance and/or Correlation Matrix
  • -
  • Linking with the SVD
  • -
  • What does it mean?
  • -
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • -
  • Ridge and LASSO Regression
  • -
  • Deriving the Ridge Regression Equations
  • -
  • Interpreting the Ridge results
  • -
  • More interpretations
  • -
  • Deriving the Lasso Regression Equations
  • -
  • Exercises for week 35
  • -
  • Exercise 1: Setting up various Python environments
  • -
  • Exercise 2: making your own data and exploring scikit-learn
  • -
  • Exercise 3: Normalizing our data
  • -
  • Exercise 4: Adding Ridge Regression
  • -
  • Exercise 5: Analytical exercises
  • +
  • The Jacobian
  • +
  • Derivatives, example 1
  • +
  • Meet the Hessian Matrix
  • +
  • Interpretations and optimizing our parameters
  • +
  • Own code for Ordinary Least Squares
  • +
  • Adding error analysis and training set up
  • +
  • Splitting our Data in Training and Test data
  • +
  • Examples
  • +
  • Making your own test-train splitting
  • +
  • The Boston housing data example
  • +
  • Housing data, the code
  • +
  • Reducing the number of degrees of freedom, overarching view
  • +
  • Preprocessing our data
  • +
  • Functionality in Scikit-Learn
  • +
  • More preprocessing
  • +
  • Frequently used scaling functions
  • +
  • Example of own Standard scaling
  • +
  • Min-Max Scaling
  • +
  • Testing the Means Squared Error as function of Complexity
  • +
  • More preprocessing examples, Franke function and regression
  • +
  • Material for lecture Thursday, August 31
  • +
  • Mathematical Interpretation of Ordinary Least Squares
  • +
  • Residual Error
  • +
  • Simple case
  • +
  • The singular value decomposition
  • +
  • Linear Regression Problems
  • +
  • Fixing the singularity
  • +
  • Basic math of the SVD
  • +
  • The SVD, a Fantastic Algorithm
  • +
  • Economy-size SVD
  • +
  • Codes for the SVD
  • +
  • Note about SVD Calculations
  • +
  • Mathematics of the SVD and implications
  • +
  • Example Matrix
  • +
  • Setting up the Matrix to be inverted
  • +
  • Further properties (important for our analyses later)
  • +
  • Meet the Covariance Matrix
  • +
  • Introducing the Covariance and Correlation functions
  • +
  • Covariance and Correlation Matrix
  • +
  • Correlation Function and Design/Feature Matrix
  • +
  • Covariance Matrix Examples
  • +
  • Correlation Matrix
  • +
  • Correlation Matrix with Pandas
  • +
  • Correlation Matrix with Pandas and the Franke function
  • +
  • Rewriting the Covariance and/or Correlation Matrix
  • +
  • Linking with the SVD
  • +
  • What does it mean?
  • +
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • +
  • Ridge and LASSO Regression
  • +
  • Deriving the Ridge Regression Equations
  • +
  • Interpreting the Ridge results
  • +
  • More interpretations
  • +
  • Deriving the Lasso Regression Equations
  • +
  • Exercises for week 35
  • +
  • Exercise 1: Setting up various Python environments
  • +
  • Exercise 2: making your own data and exploring scikit-learn
  • +
  • Exercise 3: Normalizing our data
  • +
  • Exercise 4: Adding Ridge Regression
  • +
  • Exercise 5: Analytical exercises
  • @@ -382,8 +391,8 @@ MathJax.Hub.Config({

     

     

     

    -

    Housing data, the code

    -

    We start by importing the libraries

    +

    Making your own test-train splitting

    +
    @@ -391,322 +400,20 @@ MathJax.Hub.Config({
    -
    import numpy as np
    -import matplotlib.pyplot as plt 
    +  
    # equivalently in numpy
    +def train_test_split_numpy(inputs, labels, train_size, test_size):
    +    n_inputs = len(inputs)
    +    inputs_shuffled = inputs.copy()
    +    labels_shuffled = labels.copy()
     
    -import pandas as pd  
    -import seaborn as sns 
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    - -

    and load the Boston Housing DataSet from Scikit-Learn

    - - - -
    -
    -
    -
    -
    -
    from sklearn.datasets import load_boston
    -
    -boston_dataset = load_boston()
    -
    -# boston_dataset is a dictionary
    -# let's check what it contains
    -boston_dataset.keys()
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    - -

    Then we invoke Pandas

    - - -
    -
    -
    -
    -
    -
    boston = pd.DataFrame(boston_dataset.data, columns=boston_dataset.feature_names)
    -boston.head()
    -boston['MEDV'] = boston_dataset.target
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    - -

    and preprocess the data

    - - -
    -
    -
    -
    -
    -
    # check for missing values in all the columns
    -boston.isnull().sum()
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    - -

    We can then visualize the data

    - - -
    -
    -
    -
    -
    -
    # set the size of the figure
    -sns.set(rc={'figure.figsize':(11.7,8.27)})
    -
    -# plot a histogram showing the distribution of the target values
    -sns.distplot(boston['MEDV'], bins=30)
    -plt.show()
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    - -

    It is now useful to look at the correlation matrix

    - - -
    -
    -
    -
    -
    -
    # compute the pair wise correlation for all columns  
    -correlation_matrix = boston.corr().round(2)
    -# use the heatmap function from seaborn to plot the correlation matrix
    -# annot = True to print the values inside the square
    -sns.heatmap(data=correlation_matrix, annot=True)
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    - -

    From the above coorelation plot we can see that MEDV is strongly correlated to LSTAT and RM. We see also that RAD and TAX are stronly correlated, but we don't include this in our features together to avoid multi-colinearity

    - - - -
    -
    -
    -
    -
    -
    plt.figure(figsize=(20, 5))
    -
    -features = ['LSTAT', 'RM']
    -target = boston['MEDV']
    -
    -for i, col in enumerate(features):
    -    plt.subplot(1, len(features) , i+1)
    -    x = boston[col]
    -    y = target
    -    plt.scatter(x, y, marker='o')
    -    plt.title(col)
    -    plt.xlabel(col)
    -    plt.ylabel('MEDV')
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    - -

    Now we start training our model

    - - -
    -
    -
    -
    -
    -
    X = pd.DataFrame(np.c_[boston['LSTAT'], boston['RM']], columns = ['LSTAT','RM'])
    -Y = boston['MEDV']
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    - -

    We split the data into training and test sets

    - - - -
    -
    -
    -
    -
    -
    from sklearn.model_selection import train_test_split
    -
    -# splits the training and test data set in 80% : 20%
    -# assign random_state to any value.This ensures consistency.
    -X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size = 0.2, random_state=5)
    -print(X_train.shape)
    -print(X_test.shape)
    -print(Y_train.shape)
    -print(Y_test.shape)
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    - -

    Then we use the linear regression functionality from Scikit-Learn

    - - -
    -
    -
    -
    -
    -
    from sklearn.linear_model import LinearRegression
    -from sklearn.metrics import mean_squared_error, r2_score
    -
    -lin_model = LinearRegression()
    -lin_model.fit(X_train, Y_train)
    -
    -# model evaluation for training set
    -
    -y_train_predict = lin_model.predict(X_train)
    -rmse = (np.sqrt(mean_squared_error(Y_train, y_train_predict)))
    -r2 = r2_score(Y_train, y_train_predict)
    -
    -print("The model performance for training set")
    -print("--------------------------------------")
    -print('RMSE is {}'.format(rmse))
    -print('R2 score is {}'.format(r2))
    -print("\n")
    -
    -# model evaluation for testing set
    -
    -y_test_predict = lin_model.predict(X_test)
    -# root mean square error of the model
    -rmse = (np.sqrt(mean_squared_error(Y_test, y_test_predict)))
    -
    -# r-squared score of the model
    -r2 = r2_score(Y_test, y_test_predict)
    -
    -print("The model performance for testing set")
    -print("--------------------------------------")
    -print('RMSE is {}'.format(rmse))
    -print('R2 score is {}'.format(r2))
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    - -
    -
    -
    -
    -
    -
    # plotting the y_test vs y_pred
    -# ideally should have been a straight line
    -plt.scatter(Y_test, y_test_predict)
    -plt.show()
    +    np.random.shuffle(inputs_shuffled)
    +    np.random.shuffle(labels_shuffled)
    +
    +    train_end = int(n_inputs*train_size)
    +    X_train, X_test = inputs_shuffled[:train_end], inputs_shuffled[train_end:]
    +    Y_train, Y_test = labels_shuffled[:train_end], labels_shuffled[train_end:]
    +
    +    return X_train, X_test, Y_train, Y_test
     
    @@ -722,6 +429,10 @@ plt.show()
    +

    But since scikit-learn has its own function for doing this and since +it interfaces easily with tensorflow and other libraries, we +normally recommend using the latter functionality. +

    @@ -748,7 +459,7 @@ plt.show()

  • 34
  • 35
  • ...
  • -
  • 68
  • +
  • 71
  • »
  • diff --git a/doc/pub/week35/html/._week35-bs026.html b/doc/pub/week35/html/._week35-bs026.html index 656571691..3a5ae4efc 100644 --- a/doc/pub/week35/html/._week35-bs026.html +++ b/doc/pub/week35/html/._week35-bs026.html @@ -74,10 +74,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'optimizing-our-parameters'), - ('Our model for the nuclear binding energies', + ('Examples relevant for the exercises', 2, None, - 'our-model-for-the-nuclear-binding-energies'), + 'examples-relevant-for-the-exercises'), ('Optimizing our parameters, more details', 2, None, @@ -94,6 +94,8 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'some-useful-matrix-and-vector-expressions'), + ('The Jacobian', 2, None, 'the-jacobian'), + ('Derivatives, example 1', 2, None, 'derivatives-example-1'), ('Meet the Hessian Matrix', 2, None, 'meet-the-hessian-matrix'), ('Interpretations and optimizing our parameters', 2, @@ -148,6 +150,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'more-preprocessing-examples-franke-function-and-regression'), + ('Material for lecture Thursday, August 31', + 2, + None, + 'material-for-lecture-thursday-august-31'), ('Mathematical Interpretation of Ordinary Least Squares', 2, None, @@ -303,74 +309,77 @@ MathJax.Hub.Config({
  • Why Linear Regression (aka Ordinary Least Squares and family), repeat from last week
  • Regression analysis, overarching aims
  • Regression analysis, overarching aims II
  • -
  • Examples
  • +
  • Examples
  • General linear models
  • Rewriting the fitting procedure as a linear algebra problem
  • Rewriting the fitting procedure as a linear algebra problem, more details
  • Generalizing the fitting procedure as a linear algebra problem
  • Generalizing the fitting procedure as a linear algebra problem
  • Optimizing our parameters
  • -
  • Our model for the nuclear binding energies
  • +
  • Examples relevant for the exercises
  • Optimizing our parameters, more details
  • -
  • Interpretations and optimizing our parameters
  • -
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • Some useful matrix and vector expressions
  • -
  • Meet the Hessian Matrix
  • -
  • Interpretations and optimizing our parameters
  • -
  • Own code for Ordinary Least Squares
  • -
  • Adding error analysis and training set up
  • -
  • Splitting our Data in Training and Test data
  • -
  • Examples
  • -
  • Making your own test-train splitting
  • -
  • The Boston housing data example
  • -
  • Housing data, the code
  • -
  • Reducing the number of degrees of freedom, overarching view
  • -
  • Preprocessing our data
  • -
  • Functionality in Scikit-Learn
  • -
  • More preprocessing
  • -
  • Frequently used scaling functions
  • -
  • Example of own Standard scaling
  • -
  • Min-Max Scaling
  • -
  • Testing the Means Squared Error as function of Complexity
  • -
  • More preprocessing examples, Franke function and regression
  • -
  • Mathematical Interpretation of Ordinary Least Squares
  • -
  • Residual Error
  • -
  • Simple case
  • -
  • The singular value decomposition
  • -
  • Linear Regression Problems
  • -
  • Fixing the singularity
  • -
  • Basic math of the SVD
  • -
  • The SVD, a Fantastic Algorithm
  • -
  • Economy-size SVD
  • -
  • Codes for the SVD
  • -
  • Note about SVD Calculations
  • -
  • Mathematics of the SVD and implications
  • -
  • Example Matrix
  • -
  • Setting up the Matrix to be inverted
  • -
  • Further properties (important for our analyses later)
  • -
  • Meet the Covariance Matrix
  • -
  • Introducing the Covariance and Correlation functions
  • -
  • Covariance and Correlation Matrix
  • -
  • Correlation Function and Design/Feature Matrix
  • -
  • Covariance Matrix Examples
  • -
  • Correlation Matrix
  • -
  • Correlation Matrix with Pandas
  • -
  • Correlation Matrix with Pandas and the Franke function
  • -
  • Rewriting the Covariance and/or Correlation Matrix
  • -
  • Linking with the SVD
  • -
  • What does it mean?
  • -
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • -
  • Ridge and LASSO Regression
  • -
  • Deriving the Ridge Regression Equations
  • -
  • Interpreting the Ridge results
  • -
  • More interpretations
  • -
  • Deriving the Lasso Regression Equations
  • -
  • Exercises for week 35
  • -
  • Exercise 1: Setting up various Python environments
  • -
  • Exercise 2: making your own data and exploring scikit-learn
  • -
  • Exercise 3: Normalizing our data
  • -
  • Exercise 4: Adding Ridge Regression
  • -
  • Exercise 5: Analytical exercises
  • +
  • The Jacobian
  • +
  • Derivatives, example 1
  • +
  • Meet the Hessian Matrix
  • +
  • Interpretations and optimizing our parameters
  • +
  • Own code for Ordinary Least Squares
  • +
  • Adding error analysis and training set up
  • +
  • Splitting our Data in Training and Test data
  • +
  • Examples
  • +
  • Making your own test-train splitting
  • +
  • The Boston housing data example
  • +
  • Housing data, the code
  • +
  • Reducing the number of degrees of freedom, overarching view
  • +
  • Preprocessing our data
  • +
  • Functionality in Scikit-Learn
  • +
  • More preprocessing
  • +
  • Frequently used scaling functions
  • +
  • Example of own Standard scaling
  • +
  • Min-Max Scaling
  • +
  • Testing the Means Squared Error as function of Complexity
  • +
  • More preprocessing examples, Franke function and regression
  • +
  • Material for lecture Thursday, August 31
  • +
  • Mathematical Interpretation of Ordinary Least Squares
  • +
  • Residual Error
  • +
  • Simple case
  • +
  • The singular value decomposition
  • +
  • Linear Regression Problems
  • +
  • Fixing the singularity
  • +
  • Basic math of the SVD
  • +
  • The SVD, a Fantastic Algorithm
  • +
  • Economy-size SVD
  • +
  • Codes for the SVD
  • +
  • Note about SVD Calculations
  • +
  • Mathematics of the SVD and implications
  • +
  • Example Matrix
  • +
  • Setting up the Matrix to be inverted
  • +
  • Further properties (important for our analyses later)
  • +
  • Meet the Covariance Matrix
  • +
  • Introducing the Covariance and Correlation functions
  • +
  • Covariance and Correlation Matrix
  • +
  • Correlation Function and Design/Feature Matrix
  • +
  • Covariance Matrix Examples
  • +
  • Correlation Matrix
  • +
  • Correlation Matrix with Pandas
  • +
  • Correlation Matrix with Pandas and the Franke function
  • +
  • Rewriting the Covariance and/or Correlation Matrix
  • +
  • Linking with the SVD
  • +
  • What does it mean?
  • +
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • +
  • Ridge and LASSO Regression
  • +
  • Deriving the Ridge Regression Equations
  • +
  • Interpreting the Ridge results
  • +
  • More interpretations
  • +
  • Deriving the Lasso Regression Equations
  • +
  • Exercises for week 35
  • +
  • Exercise 1: Setting up various Python environments
  • +
  • Exercise 2: making your own data and exploring scikit-learn
  • +
  • Exercise 3: Normalizing our data
  • +
  • Exercise 4: Adding Ridge Regression
  • +
  • Exercise 5: Analytical exercises
  • @@ -381,37 +390,33 @@ MathJax.Hub.Config({

     

     

     

    - -

    Reducing the number of degrees of freedom, overarching view

    -
    -
    - + +

    The Boston housing data example

    -

    Many Machine Learning problems involve thousands or even millions of -features for each training instance. Not only does this make training -extremely slow, it can also make it much harder to find a good -solution, as we will see. This problem is often referred to as the -curse of dimensionality. Fortunately, in real-world problems, it is -often possible to reduce the number of features considerably, turning -an intractable problem into a tractable one. +

    The Boston housing +data set was originally a part of UCI Machine Learning Repository +and has been removed now. The data set is now included in Scikit-Learn's +library. There are 506 samples and 13 feature (predictor) variables +in this data set. The objective is to predict the value of prices of +the house using the features (predictors) listed here.

    -

    Later we will discuss some of the most popular dimensionality reduction -techniques: the principal component analysis (PCA), Kernel PCA, and -Locally Linear Embedding (LLE). -

    - -

    Principal component analysis and its various variants deal with the -problem of fitting a low-dimensional affine -subspace to a set of of -data points in a high-dimensional space. With its family of methods it -is one of the most used tools in data modeling, compression and -visualization. -

    -
    -
    - - +

    The features/predictors are

    +
      +
    1. CRIM: Per capita crime rate by town
    2. +
    3. ZN: Proportion of residential land zoned for lots over 25000 square feet
    4. +
    5. INDUS: Proportion of non-retail business acres per town
    6. +
    7. CHAS: Charles River dummy variable (= 1 if tract bounds river; 0 otherwise)
    8. +
    9. NOX: Nitric oxide concentration (parts per 10 million)
    10. +
    11. RM: Average number of rooms per dwelling
    12. +
    13. AGE: Proportion of owner-occupied units built prior to 1940
    14. +
    15. DIS: Weighted distances to five Boston employment centers
    16. +
    17. RAD: Index of accessibility to radial highways
    18. +
    19. TAX: Full-value property tax rate per USD10000
    20. +
    21. B: \( 1000(Bk - 0.63)^2 \), where \( Bk \) is the proportion of [people of African American descent] by town
    22. +
    23. LSTAT: Percentage of lower status of the population
    24. +
    25. MEDV: Median value of owner-occupied homes in USD 1000s
    26. +

    diff --git a/doc/pub/week35/html/._week35-bs027.html b/doc/pub/week35/html/._week35-bs027.html index 5ff54e024..cda46d6ca 100644 --- a/doc/pub/week35/html/._week35-bs027.html +++ b/doc/pub/week35/html/._week35-bs027.html @@ -74,10 +74,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'optimizing-our-parameters'), - ('Our model for the nuclear binding energies', + ('Examples relevant for the exercises', 2, None, - 'our-model-for-the-nuclear-binding-energies'), + 'examples-relevant-for-the-exercises'), ('Optimizing our parameters, more details', 2, None, @@ -94,6 +94,8 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'some-useful-matrix-and-vector-expressions'), + ('The Jacobian', 2, None, 'the-jacobian'), + ('Derivatives, example 1', 2, None, 'derivatives-example-1'), ('Meet the Hessian Matrix', 2, None, 'meet-the-hessian-matrix'), ('Interpretations and optimizing our parameters', 2, @@ -148,6 +150,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'more-preprocessing-examples-franke-function-and-regression'), + ('Material for lecture Thursday, August 31', + 2, + None, + 'material-for-lecture-thursday-august-31'), ('Mathematical Interpretation of Ordinary Least Squares', 2, None, @@ -303,74 +309,77 @@ MathJax.Hub.Config({
  • Why Linear Regression (aka Ordinary Least Squares and family), repeat from last week
  • Regression analysis, overarching aims
  • Regression analysis, overarching aims II
  • -
  • Examples
  • +
  • Examples
  • General linear models
  • Rewriting the fitting procedure as a linear algebra problem
  • Rewriting the fitting procedure as a linear algebra problem, more details
  • Generalizing the fitting procedure as a linear algebra problem
  • Generalizing the fitting procedure as a linear algebra problem
  • Optimizing our parameters
  • -
  • Our model for the nuclear binding energies
  • +
  • Examples relevant for the exercises
  • Optimizing our parameters, more details
  • -
  • Interpretations and optimizing our parameters
  • -
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • Some useful matrix and vector expressions
  • -
  • Meet the Hessian Matrix
  • -
  • Interpretations and optimizing our parameters
  • -
  • Own code for Ordinary Least Squares
  • -
  • Adding error analysis and training set up
  • -
  • Splitting our Data in Training and Test data
  • -
  • Examples
  • -
  • Making your own test-train splitting
  • -
  • The Boston housing data example
  • -
  • Housing data, the code
  • -
  • Reducing the number of degrees of freedom, overarching view
  • -
  • Preprocessing our data
  • -
  • Functionality in Scikit-Learn
  • -
  • More preprocessing
  • -
  • Frequently used scaling functions
  • -
  • Example of own Standard scaling
  • -
  • Min-Max Scaling
  • -
  • Testing the Means Squared Error as function of Complexity
  • -
  • More preprocessing examples, Franke function and regression
  • -
  • Mathematical Interpretation of Ordinary Least Squares
  • -
  • Residual Error
  • -
  • Simple case
  • -
  • The singular value decomposition
  • -
  • Linear Regression Problems
  • -
  • Fixing the singularity
  • -
  • Basic math of the SVD
  • -
  • The SVD, a Fantastic Algorithm
  • -
  • Economy-size SVD
  • -
  • Codes for the SVD
  • -
  • Note about SVD Calculations
  • -
  • Mathematics of the SVD and implications
  • -
  • Example Matrix
  • -
  • Setting up the Matrix to be inverted
  • -
  • Further properties (important for our analyses later)
  • -
  • Meet the Covariance Matrix
  • -
  • Introducing the Covariance and Correlation functions
  • -
  • Covariance and Correlation Matrix
  • -
  • Correlation Function and Design/Feature Matrix
  • -
  • Covariance Matrix Examples
  • -
  • Correlation Matrix
  • -
  • Correlation Matrix with Pandas
  • -
  • Correlation Matrix with Pandas and the Franke function
  • -
  • Rewriting the Covariance and/or Correlation Matrix
  • -
  • Linking with the SVD
  • -
  • What does it mean?
  • -
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • -
  • Ridge and LASSO Regression
  • -
  • Deriving the Ridge Regression Equations
  • -
  • Interpreting the Ridge results
  • -
  • More interpretations
  • -
  • Deriving the Lasso Regression Equations
  • -
  • Exercises for week 35
  • -
  • Exercise 1: Setting up various Python environments
  • -
  • Exercise 2: making your own data and exploring scikit-learn
  • -
  • Exercise 3: Normalizing our data
  • -
  • Exercise 4: Adding Ridge Regression
  • -
  • Exercise 5: Analytical exercises
  • +
  • The Jacobian
  • +
  • Derivatives, example 1
  • +
  • Meet the Hessian Matrix
  • +
  • Interpretations and optimizing our parameters
  • +
  • Own code for Ordinary Least Squares
  • +
  • Adding error analysis and training set up
  • +
  • Splitting our Data in Training and Test data
  • +
  • Examples
  • +
  • Making your own test-train splitting
  • +
  • The Boston housing data example
  • +
  • Housing data, the code
  • +
  • Reducing the number of degrees of freedom, overarching view
  • +
  • Preprocessing our data
  • +
  • Functionality in Scikit-Learn
  • +
  • More preprocessing
  • +
  • Frequently used scaling functions
  • +
  • Example of own Standard scaling
  • +
  • Min-Max Scaling
  • +
  • Testing the Means Squared Error as function of Complexity
  • +
  • More preprocessing examples, Franke function and regression
  • +
  • Material for lecture Thursday, August 31
  • +
  • Mathematical Interpretation of Ordinary Least Squares
  • +
  • Residual Error
  • +
  • Simple case
  • +
  • The singular value decomposition
  • +
  • Linear Regression Problems
  • +
  • Fixing the singularity
  • +
  • Basic math of the SVD
  • +
  • The SVD, a Fantastic Algorithm
  • +
  • Economy-size SVD
  • +
  • Codes for the SVD
  • +
  • Note about SVD Calculations
  • +
  • Mathematics of the SVD and implications
  • +
  • Example Matrix
  • +
  • Setting up the Matrix to be inverted
  • +
  • Further properties (important for our analyses later)
  • +
  • Meet the Covariance Matrix
  • +
  • Introducing the Covariance and Correlation functions
  • +
  • Covariance and Correlation Matrix
  • +
  • Correlation Function and Design/Feature Matrix
  • +
  • Covariance Matrix Examples
  • +
  • Correlation Matrix
  • +
  • Correlation Matrix with Pandas
  • +
  • Correlation Matrix with Pandas and the Franke function
  • +
  • Rewriting the Covariance and/or Correlation Matrix
  • +
  • Linking with the SVD
  • +
  • What does it mean?
  • +
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • +
  • Ridge and LASSO Regression
  • +
  • Deriving the Ridge Regression Equations
  • +
  • Interpreting the Ridge results
  • +
  • More interpretations
  • +
  • Deriving the Lasso Regression Equations
  • +
  • Exercises for week 35
  • +
  • Exercise 1: Setting up various Python environments
  • +
  • Exercise 2: making your own data and exploring scikit-learn
  • +
  • Exercise 3: Normalizing our data
  • +
  • Exercise 4: Adding Ridge Regression
  • +
  • Exercise 5: Analytical exercises
  • @@ -382,28 +391,344 @@ MathJax.Hub.Config({

     

     

     

    -

    Preprocessing our data

    -
    -
    - +

    Housing data, the code

    +

    We start by importing the libraries

    -

    Before we proceed however, we will discuss how to preprocess our -data. Till now and in connection with our previous examples we have -not met so many cases where we are too sensitive to the scaling of our -data. Normally the data may need a rescaling and/or may be sensitive -to extreme values. Scaling the data renders our inputs much more -suitable for the algorithms we want to employ. -

    + +
    +
    +
    +
    +
    +
    import numpy as np
    +import matplotlib.pyplot as plt 
     
    -

    For data sets gathered for real world applications, it is rather normal that -different features have very different units and -numerical scales. For example, a data set detailing health habits may include -features such as age in the range \( 0-80 \), and caloric intake of order \( 2000 \). -Many machine learning methods sensitive to the scales of the features and may perform poorly if they -are very different scales. Therefore, it is typical to scale -the features in a way to avoid such outlier values. -

    +import pandas as pd +import seaborn as sns +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    + +

    and load the Boston Housing DataSet from Scikit-Learn

    + + + +
    +
    +
    +
    +
    +
    from sklearn.datasets import load_boston
    +
    +boston_dataset = load_boston()
    +
    +# boston_dataset is a dictionary
    +# let's check what it contains
    +boston_dataset.keys()
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    + +

    Then we invoke Pandas

    + + +
    +
    +
    +
    +
    +
    boston = pd.DataFrame(boston_dataset.data, columns=boston_dataset.feature_names)
    +boston.head()
    +boston['MEDV'] = boston_dataset.target
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    + +

    and preprocess the data

    + + +
    +
    +
    +
    +
    +
    # check for missing values in all the columns
    +boston.isnull().sum()
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    + +

    We can then visualize the data

    + + +
    +
    +
    +
    +
    +
    # set the size of the figure
    +sns.set(rc={'figure.figsize':(11.7,8.27)})
    +
    +# plot a histogram showing the distribution of the target values
    +sns.distplot(boston['MEDV'], bins=30)
    +plt.show()
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    + +

    It is now useful to look at the correlation matrix

    + + +
    +
    +
    +
    +
    +
    # compute the pair wise correlation for all columns  
    +correlation_matrix = boston.corr().round(2)
    +# use the heatmap function from seaborn to plot the correlation matrix
    +# annot = True to print the values inside the square
    +sns.heatmap(data=correlation_matrix, annot=True)
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    + +

    From the above coorelation plot we can see that MEDV is strongly correlated to LSTAT and RM. We see also that RAD and TAX are stronly correlated, but we don't include this in our features together to avoid multi-colinearity

    + + + +
    +
    +
    +
    +
    +
    plt.figure(figsize=(20, 5))
    +
    +features = ['LSTAT', 'RM']
    +target = boston['MEDV']
    +
    +for i, col in enumerate(features):
    +    plt.subplot(1, len(features) , i+1)
    +    x = boston[col]
    +    y = target
    +    plt.scatter(x, y, marker='o')
    +    plt.title(col)
    +    plt.xlabel(col)
    +    plt.ylabel('MEDV')
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    + +

    Now we start training our model

    + + +
    +
    +
    +
    +
    +
    X = pd.DataFrame(np.c_[boston['LSTAT'], boston['RM']], columns = ['LSTAT','RM'])
    +Y = boston['MEDV']
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    + +

    We split the data into training and test sets

    + + + +
    +
    +
    +
    +
    +
    from sklearn.model_selection import train_test_split
    +
    +# splits the training and test data set in 80% : 20%
    +# assign random_state to any value.This ensures consistency.
    +X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size = 0.2, random_state=5)
    +print(X_train.shape)
    +print(X_test.shape)
    +print(Y_train.shape)
    +print(Y_test.shape)
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    + +

    Then we use the linear regression functionality from Scikit-Learn

    + + +
    +
    +
    +
    +
    +
    from sklearn.linear_model import LinearRegression
    +from sklearn.metrics import mean_squared_error, r2_score
    +
    +lin_model = LinearRegression()
    +lin_model.fit(X_train, Y_train)
    +
    +# model evaluation for training set
    +
    +y_train_predict = lin_model.predict(X_train)
    +rmse = (np.sqrt(mean_squared_error(Y_train, y_train_predict)))
    +r2 = r2_score(Y_train, y_train_predict)
    +
    +print("The model performance for training set")
    +print("--------------------------------------")
    +print('RMSE is {}'.format(rmse))
    +print('R2 score is {}'.format(r2))
    +print("\n")
    +
    +# model evaluation for testing set
    +
    +y_test_predict = lin_model.predict(X_test)
    +# root mean square error of the model
    +rmse = (np.sqrt(mean_squared_error(Y_test, y_test_predict)))
    +
    +# r-squared score of the model
    +r2 = r2_score(Y_test, y_test_predict)
    +
    +print("The model performance for testing set")
    +print("--------------------------------------")
    +print('RMSE is {}'.format(rmse))
    +print('R2 score is {}'.format(r2))
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    + +
    +
    +
    +
    +
    +
    # plotting the y_test vs y_pred
    +# ideally should have been a straight line
    +plt.scatter(Y_test, y_test_predict)
    +plt.show()
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    @@ -432,7 +757,7 @@ the features in a way to avoid such outlier values.
  • 36
  • 37
  • ...
  • -
  • 68
  • +
  • 71
  • »
  • diff --git a/doc/pub/week35/html/._week35-bs028.html b/doc/pub/week35/html/._week35-bs028.html index 5e344cbfe..da577dbbc 100644 --- a/doc/pub/week35/html/._week35-bs028.html +++ b/doc/pub/week35/html/._week35-bs028.html @@ -74,10 +74,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'optimizing-our-parameters'), - ('Our model for the nuclear binding energies', + ('Examples relevant for the exercises', 2, None, - 'our-model-for-the-nuclear-binding-energies'), + 'examples-relevant-for-the-exercises'), ('Optimizing our parameters, more details', 2, None, @@ -94,6 +94,8 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'some-useful-matrix-and-vector-expressions'), + ('The Jacobian', 2, None, 'the-jacobian'), + ('Derivatives, example 1', 2, None, 'derivatives-example-1'), ('Meet the Hessian Matrix', 2, None, 'meet-the-hessian-matrix'), ('Interpretations and optimizing our parameters', 2, @@ -148,6 +150,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'more-preprocessing-examples-franke-function-and-regression'), + ('Material for lecture Thursday, August 31', + 2, + None, + 'material-for-lecture-thursday-august-31'), ('Mathematical Interpretation of Ordinary Least Squares', 2, None, @@ -303,74 +309,77 @@ MathJax.Hub.Config({
  • Why Linear Regression (aka Ordinary Least Squares and family), repeat from last week
  • Regression analysis, overarching aims
  • Regression analysis, overarching aims II
  • -
  • Examples
  • +
  • Examples
  • General linear models
  • Rewriting the fitting procedure as a linear algebra problem
  • Rewriting the fitting procedure as a linear algebra problem, more details
  • Generalizing the fitting procedure as a linear algebra problem
  • Generalizing the fitting procedure as a linear algebra problem
  • Optimizing our parameters
  • -
  • Our model for the nuclear binding energies
  • +
  • Examples relevant for the exercises
  • Optimizing our parameters, more details
  • -
  • Interpretations and optimizing our parameters
  • -
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • Some useful matrix and vector expressions
  • -
  • Meet the Hessian Matrix
  • -
  • Interpretations and optimizing our parameters
  • -
  • Own code for Ordinary Least Squares
  • -
  • Adding error analysis and training set up
  • -
  • Splitting our Data in Training and Test data
  • -
  • Examples
  • -
  • Making your own test-train splitting
  • -
  • The Boston housing data example
  • -
  • Housing data, the code
  • -
  • Reducing the number of degrees of freedom, overarching view
  • -
  • Preprocessing our data
  • -
  • Functionality in Scikit-Learn
  • -
  • More preprocessing
  • -
  • Frequently used scaling functions
  • -
  • Example of own Standard scaling
  • -
  • Min-Max Scaling
  • -
  • Testing the Means Squared Error as function of Complexity
  • -
  • More preprocessing examples, Franke function and regression
  • -
  • Mathematical Interpretation of Ordinary Least Squares
  • -
  • Residual Error
  • -
  • Simple case
  • -
  • The singular value decomposition
  • -
  • Linear Regression Problems
  • -
  • Fixing the singularity
  • -
  • Basic math of the SVD
  • -
  • The SVD, a Fantastic Algorithm
  • -
  • Economy-size SVD
  • -
  • Codes for the SVD
  • -
  • Note about SVD Calculations
  • -
  • Mathematics of the SVD and implications
  • -
  • Example Matrix
  • -
  • Setting up the Matrix to be inverted
  • -
  • Further properties (important for our analyses later)
  • -
  • Meet the Covariance Matrix
  • -
  • Introducing the Covariance and Correlation functions
  • -
  • Covariance and Correlation Matrix
  • -
  • Correlation Function and Design/Feature Matrix
  • -
  • Covariance Matrix Examples
  • -
  • Correlation Matrix
  • -
  • Correlation Matrix with Pandas
  • -
  • Correlation Matrix with Pandas and the Franke function
  • -
  • Rewriting the Covariance and/or Correlation Matrix
  • -
  • Linking with the SVD
  • -
  • What does it mean?
  • -
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • -
  • Ridge and LASSO Regression
  • -
  • Deriving the Ridge Regression Equations
  • -
  • Interpreting the Ridge results
  • -
  • More interpretations
  • -
  • Deriving the Lasso Regression Equations
  • -
  • Exercises for week 35
  • -
  • Exercise 1: Setting up various Python environments
  • -
  • Exercise 2: making your own data and exploring scikit-learn
  • -
  • Exercise 3: Normalizing our data
  • -
  • Exercise 4: Adding Ridge Regression
  • -
  • Exercise 5: Analytical exercises
  • +
  • The Jacobian
  • +
  • Derivatives, example 1
  • +
  • Meet the Hessian Matrix
  • +
  • Interpretations and optimizing our parameters
  • +
  • Own code for Ordinary Least Squares
  • +
  • Adding error analysis and training set up
  • +
  • Splitting our Data in Training and Test data
  • +
  • Examples
  • +
  • Making your own test-train splitting
  • +
  • The Boston housing data example
  • +
  • Housing data, the code
  • +
  • Reducing the number of degrees of freedom, overarching view
  • +
  • Preprocessing our data
  • +
  • Functionality in Scikit-Learn
  • +
  • More preprocessing
  • +
  • Frequently used scaling functions
  • +
  • Example of own Standard scaling
  • +
  • Min-Max Scaling
  • +
  • Testing the Means Squared Error as function of Complexity
  • +
  • More preprocessing examples, Franke function and regression
  • +
  • Material for lecture Thursday, August 31
  • +
  • Mathematical Interpretation of Ordinary Least Squares
  • +
  • Residual Error
  • +
  • Simple case
  • +
  • The singular value decomposition
  • +
  • Linear Regression Problems
  • +
  • Fixing the singularity
  • +
  • Basic math of the SVD
  • +
  • The SVD, a Fantastic Algorithm
  • +
  • Economy-size SVD
  • +
  • Codes for the SVD
  • +
  • Note about SVD Calculations
  • +
  • Mathematics of the SVD and implications
  • +
  • Example Matrix
  • +
  • Setting up the Matrix to be inverted
  • +
  • Further properties (important for our analyses later)
  • +
  • Meet the Covariance Matrix
  • +
  • Introducing the Covariance and Correlation functions
  • +
  • Covariance and Correlation Matrix
  • +
  • Correlation Function and Design/Feature Matrix
  • +
  • Covariance Matrix Examples
  • +
  • Correlation Matrix
  • +
  • Correlation Matrix with Pandas
  • +
  • Correlation Matrix with Pandas and the Franke function
  • +
  • Rewriting the Covariance and/or Correlation Matrix
  • +
  • Linking with the SVD
  • +
  • What does it mean?
  • +
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • +
  • Ridge and LASSO Regression
  • +
  • Deriving the Ridge Regression Equations
  • +
  • Interpreting the Ridge results
  • +
  • More interpretations
  • +
  • Deriving the Lasso Regression Equations
  • +
  • Exercises for week 35
  • +
  • Exercise 1: Setting up various Python environments
  • +
  • Exercise 2: making your own data and exploring scikit-learn
  • +
  • Exercise 3: Normalizing our data
  • +
  • Exercise 4: Adding Ridge Regression
  • +
  • Exercise 5: Analytical exercises
  • @@ -382,19 +391,36 @@ MathJax.Hub.Config({

     

     

     

    -

    Functionality in Scikit-Learn

    +

    Reducing the number of degrees of freedom, overarching view

    +
    +
    + -

    Scikit-Learn has several functions which allow us to rescale the -data, normally resulting in much better results in terms of various -accuracy scores. The StandardScaler function in Scikit-Learn -ensures that for each feature/predictor we study the mean value is -zero and the variance is one (every column in the design/feature -matrix). This scaling has the drawback that it does not ensure that -we have a particular maximum or minimum in our data set. Another -function included in Scikit-Learn is the MinMaxScaler which -ensures that all features are exactly between \( 0 \) and \( 1 \). The +

    Many Machine Learning problems involve thousands or even millions of +features for each training instance. Not only does this make training +extremely slow, it can also make it much harder to find a good +solution, as we will see. This problem is often referred to as the +curse of dimensionality. Fortunately, in real-world problems, it is +often possible to reduce the number of features considerably, turning +an intractable problem into a tractable one.

    +

    Later we will discuss some of the most popular dimensionality reduction +techniques: the principal component analysis (PCA), Kernel PCA, and +Locally Linear Embedding (LLE). +

    + +

    Principal component analysis and its various variants deal with the +problem of fitting a low-dimensional affine +subspace to a set of of +data points in a high-dimensional space. With its family of methods it +is one of the most used tools in data modeling, compression and +visualization. +

    +
    +
    + +

      @@ -420,7 +446,7 @@ ensures that all features are exactly between \( 0 \) and \( 1 \). The
    • 37
    • 38
    • ...
    • -
    • 68
    • +
    • 71
    • »
    diff --git a/doc/pub/week35/html/._week35-bs029.html b/doc/pub/week35/html/._week35-bs029.html index 185a34d2e..0f5260319 100644 --- a/doc/pub/week35/html/._week35-bs029.html +++ b/doc/pub/week35/html/._week35-bs029.html @@ -74,10 +74,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'optimizing-our-parameters'), - ('Our model for the nuclear binding energies', + ('Examples relevant for the exercises', 2, None, - 'our-model-for-the-nuclear-binding-energies'), + 'examples-relevant-for-the-exercises'), ('Optimizing our parameters, more details', 2, None, @@ -94,6 +94,8 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'some-useful-matrix-and-vector-expressions'), + ('The Jacobian', 2, None, 'the-jacobian'), + ('Derivatives, example 1', 2, None, 'derivatives-example-1'), ('Meet the Hessian Matrix', 2, None, 'meet-the-hessian-matrix'), ('Interpretations and optimizing our parameters', 2, @@ -148,6 +150,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'more-preprocessing-examples-franke-function-and-regression'), + ('Material for lecture Thursday, August 31', + 2, + None, + 'material-for-lecture-thursday-august-31'), ('Mathematical Interpretation of Ordinary Least Squares', 2, None, @@ -303,74 +309,77 @@ MathJax.Hub.Config({
  • Why Linear Regression (aka Ordinary Least Squares and family), repeat from last week
  • Regression analysis, overarching aims
  • Regression analysis, overarching aims II
  • -
  • Examples
  • +
  • Examples
  • General linear models
  • Rewriting the fitting procedure as a linear algebra problem
  • Rewriting the fitting procedure as a linear algebra problem, more details
  • Generalizing the fitting procedure as a linear algebra problem
  • Generalizing the fitting procedure as a linear algebra problem
  • Optimizing our parameters
  • -
  • Our model for the nuclear binding energies
  • +
  • Examples relevant for the exercises
  • Optimizing our parameters, more details
  • -
  • Interpretations and optimizing our parameters
  • -
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • Some useful matrix and vector expressions
  • -
  • Meet the Hessian Matrix
  • -
  • Interpretations and optimizing our parameters
  • -
  • Own code for Ordinary Least Squares
  • -
  • Adding error analysis and training set up
  • -
  • Splitting our Data in Training and Test data
  • -
  • Examples
  • -
  • Making your own test-train splitting
  • -
  • The Boston housing data example
  • -
  • Housing data, the code
  • -
  • Reducing the number of degrees of freedom, overarching view
  • -
  • Preprocessing our data
  • -
  • Functionality in Scikit-Learn
  • -
  • More preprocessing
  • -
  • Frequently used scaling functions
  • -
  • Example of own Standard scaling
  • -
  • Min-Max Scaling
  • -
  • Testing the Means Squared Error as function of Complexity
  • -
  • More preprocessing examples, Franke function and regression
  • -
  • Mathematical Interpretation of Ordinary Least Squares
  • -
  • Residual Error
  • -
  • Simple case
  • -
  • The singular value decomposition
  • -
  • Linear Regression Problems
  • -
  • Fixing the singularity
  • -
  • Basic math of the SVD
  • -
  • The SVD, a Fantastic Algorithm
  • -
  • Economy-size SVD
  • -
  • Codes for the SVD
  • -
  • Note about SVD Calculations
  • -
  • Mathematics of the SVD and implications
  • -
  • Example Matrix
  • -
  • Setting up the Matrix to be inverted
  • -
  • Further properties (important for our analyses later)
  • -
  • Meet the Covariance Matrix
  • -
  • Introducing the Covariance and Correlation functions
  • -
  • Covariance and Correlation Matrix
  • -
  • Correlation Function and Design/Feature Matrix
  • -
  • Covariance Matrix Examples
  • -
  • Correlation Matrix
  • -
  • Correlation Matrix with Pandas
  • -
  • Correlation Matrix with Pandas and the Franke function
  • -
  • Rewriting the Covariance and/or Correlation Matrix
  • -
  • Linking with the SVD
  • -
  • What does it mean?
  • -
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • -
  • Ridge and LASSO Regression
  • -
  • Deriving the Ridge Regression Equations
  • -
  • Interpreting the Ridge results
  • -
  • More interpretations
  • -
  • Deriving the Lasso Regression Equations
  • -
  • Exercises for week 35
  • -
  • Exercise 1: Setting up various Python environments
  • -
  • Exercise 2: making your own data and exploring scikit-learn
  • -
  • Exercise 3: Normalizing our data
  • -
  • Exercise 4: Adding Ridge Regression
  • -
  • Exercise 5: Analytical exercises
  • +
  • The Jacobian
  • +
  • Derivatives, example 1
  • +
  • Meet the Hessian Matrix
  • +
  • Interpretations and optimizing our parameters
  • +
  • Own code for Ordinary Least Squares
  • +
  • Adding error analysis and training set up
  • +
  • Splitting our Data in Training and Test data
  • +
  • Examples
  • +
  • Making your own test-train splitting
  • +
  • The Boston housing data example
  • +
  • Housing data, the code
  • +
  • Reducing the number of degrees of freedom, overarching view
  • +
  • Preprocessing our data
  • +
  • Functionality in Scikit-Learn
  • +
  • More preprocessing
  • +
  • Frequently used scaling functions
  • +
  • Example of own Standard scaling
  • +
  • Min-Max Scaling
  • +
  • Testing the Means Squared Error as function of Complexity
  • +
  • More preprocessing examples, Franke function and regression
  • +
  • Material for lecture Thursday, August 31
  • +
  • Mathematical Interpretation of Ordinary Least Squares
  • +
  • Residual Error
  • +
  • Simple case
  • +
  • The singular value decomposition
  • +
  • Linear Regression Problems
  • +
  • Fixing the singularity
  • +
  • Basic math of the SVD
  • +
  • The SVD, a Fantastic Algorithm
  • +
  • Economy-size SVD
  • +
  • Codes for the SVD
  • +
  • Note about SVD Calculations
  • +
  • Mathematics of the SVD and implications
  • +
  • Example Matrix
  • +
  • Setting up the Matrix to be inverted
  • +
  • Further properties (important for our analyses later)
  • +
  • Meet the Covariance Matrix
  • +
  • Introducing the Covariance and Correlation functions
  • +
  • Covariance and Correlation Matrix
  • +
  • Correlation Function and Design/Feature Matrix
  • +
  • Covariance Matrix Examples
  • +
  • Correlation Matrix
  • +
  • Correlation Matrix with Pandas
  • +
  • Correlation Matrix with Pandas and the Franke function
  • +
  • Rewriting the Covariance and/or Correlation Matrix
  • +
  • Linking with the SVD
  • +
  • What does it mean?
  • +
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • +
  • Ridge and LASSO Regression
  • +
  • Deriving the Ridge Regression Equations
  • +
  • Interpreting the Ridge results
  • +
  • More interpretations
  • +
  • Deriving the Lasso Regression Equations
  • +
  • Exercises for week 35
  • +
  • Exercise 1: Setting up various Python environments
  • +
  • Exercise 2: making your own data and exploring scikit-learn
  • +
  • Exercise 3: Normalizing our data
  • +
  • Exercise 4: Adding Ridge Regression
  • +
  • Exercise 5: Analytical exercises
  • @@ -382,28 +391,26 @@ MathJax.Hub.Config({

     

     

     

    -

    More preprocessing

    - +

    Preprocessing our data

    -

    The Normalizer scales each data -point such that the feature vector has a euclidean length of one. In other words, it -projects a data point on the circle (or sphere in the case of higher dimensions) with a -radius of 1. This means every data point is scaled by a different number (by the -inverse of it’s length). -This normalization is often used when only the direction (or angle) of the data matters, -not the length of the feature vector. + +

    Before we proceed however, we will discuss how to preprocess our +data. Till now and in connection with our previous examples we have +not met so many cases where we are too sensitive to the scaling of our +data. Normally the data may need a rescaling and/or may be sensitive +to extreme values. Scaling the data renders our inputs much more +suitable for the algorithms we want to employ.

    -

    The RobustScaler works similarly to the StandardScaler in that it -ensures statistical properties for each feature that guarantee that -they are on the same scale. However, the RobustScaler uses the median -and quartiles, instead of mean and variance. This makes the -RobustScaler ignore data points that are very different from the rest -(like measurement errors). These odd data points are also called -outliers, and might often lead to trouble for other scaling -techniques. +

    For data sets gathered for real world applications, it is rather normal that +different features have very different units and +numerical scales. For example, a data set detailing health habits may include +features such as age in the range \( 0-80 \), and caloric intake of order \( 2000 \). +Many machine learning methods sensitive to the scales of the features and may perform poorly if they +are very different scales. Therefore, it is typical to scale +the features in a way to avoid such outlier values.

    @@ -434,7 +441,7 @@ techniques.
  • 38
  • 39
  • ...
  • -
  • 68
  • +
  • 71
  • »
  • diff --git a/doc/pub/week35/html/._week35-bs030.html b/doc/pub/week35/html/._week35-bs030.html index d301bd2d8..871c087cd 100644 --- a/doc/pub/week35/html/._week35-bs030.html +++ b/doc/pub/week35/html/._week35-bs030.html @@ -74,10 +74,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'optimizing-our-parameters'), - ('Our model for the nuclear binding energies', + ('Examples relevant for the exercises', 2, None, - 'our-model-for-the-nuclear-binding-energies'), + 'examples-relevant-for-the-exercises'), ('Optimizing our parameters, more details', 2, None, @@ -94,6 +94,8 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'some-useful-matrix-and-vector-expressions'), + ('The Jacobian', 2, None, 'the-jacobian'), + ('Derivatives, example 1', 2, None, 'derivatives-example-1'), ('Meet the Hessian Matrix', 2, None, 'meet-the-hessian-matrix'), ('Interpretations and optimizing our parameters', 2, @@ -148,6 +150,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'more-preprocessing-examples-franke-function-and-regression'), + ('Material for lecture Thursday, August 31', + 2, + None, + 'material-for-lecture-thursday-august-31'), ('Mathematical Interpretation of Ordinary Least Squares', 2, None, @@ -303,74 +309,77 @@ MathJax.Hub.Config({
  • Why Linear Regression (aka Ordinary Least Squares and family), repeat from last week
  • Regression analysis, overarching aims
  • Regression analysis, overarching aims II
  • -
  • Examples
  • +
  • Examples
  • General linear models
  • Rewriting the fitting procedure as a linear algebra problem
  • Rewriting the fitting procedure as a linear algebra problem, more details
  • Generalizing the fitting procedure as a linear algebra problem
  • Generalizing the fitting procedure as a linear algebra problem
  • Optimizing our parameters
  • -
  • Our model for the nuclear binding energies
  • +
  • Examples relevant for the exercises
  • Optimizing our parameters, more details
  • -
  • Interpretations and optimizing our parameters
  • -
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • Some useful matrix and vector expressions
  • -
  • Meet the Hessian Matrix
  • -
  • Interpretations and optimizing our parameters
  • -
  • Own code for Ordinary Least Squares
  • -
  • Adding error analysis and training set up
  • -
  • Splitting our Data in Training and Test data
  • -
  • Examples
  • -
  • Making your own test-train splitting
  • -
  • The Boston housing data example
  • -
  • Housing data, the code
  • -
  • Reducing the number of degrees of freedom, overarching view
  • -
  • Preprocessing our data
  • -
  • Functionality in Scikit-Learn
  • -
  • More preprocessing
  • -
  • Frequently used scaling functions
  • -
  • Example of own Standard scaling
  • -
  • Min-Max Scaling
  • -
  • Testing the Means Squared Error as function of Complexity
  • -
  • More preprocessing examples, Franke function and regression
  • -
  • Mathematical Interpretation of Ordinary Least Squares
  • -
  • Residual Error
  • -
  • Simple case
  • -
  • The singular value decomposition
  • -
  • Linear Regression Problems
  • -
  • Fixing the singularity
  • -
  • Basic math of the SVD
  • -
  • The SVD, a Fantastic Algorithm
  • -
  • Economy-size SVD
  • -
  • Codes for the SVD
  • -
  • Note about SVD Calculations
  • -
  • Mathematics of the SVD and implications
  • -
  • Example Matrix
  • -
  • Setting up the Matrix to be inverted
  • -
  • Further properties (important for our analyses later)
  • -
  • Meet the Covariance Matrix
  • -
  • Introducing the Covariance and Correlation functions
  • -
  • Covariance and Correlation Matrix
  • -
  • Correlation Function and Design/Feature Matrix
  • -
  • Covariance Matrix Examples
  • -
  • Correlation Matrix
  • -
  • Correlation Matrix with Pandas
  • -
  • Correlation Matrix with Pandas and the Franke function
  • -
  • Rewriting the Covariance and/or Correlation Matrix
  • -
  • Linking with the SVD
  • -
  • What does it mean?
  • -
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • -
  • Ridge and LASSO Regression
  • -
  • Deriving the Ridge Regression Equations
  • -
  • Interpreting the Ridge results
  • -
  • More interpretations
  • -
  • Deriving the Lasso Regression Equations
  • -
  • Exercises for week 35
  • -
  • Exercise 1: Setting up various Python environments
  • -
  • Exercise 2: making your own data and exploring scikit-learn
  • -
  • Exercise 3: Normalizing our data
  • -
  • Exercise 4: Adding Ridge Regression
  • -
  • Exercise 5: Analytical exercises
  • +
  • The Jacobian
  • +
  • Derivatives, example 1
  • +
  • Meet the Hessian Matrix
  • +
  • Interpretations and optimizing our parameters
  • +
  • Own code for Ordinary Least Squares
  • +
  • Adding error analysis and training set up
  • +
  • Splitting our Data in Training and Test data
  • +
  • Examples
  • +
  • Making your own test-train splitting
  • +
  • The Boston housing data example
  • +
  • Housing data, the code
  • +
  • Reducing the number of degrees of freedom, overarching view
  • +
  • Preprocessing our data
  • +
  • Functionality in Scikit-Learn
  • +
  • More preprocessing
  • +
  • Frequently used scaling functions
  • +
  • Example of own Standard scaling
  • +
  • Min-Max Scaling
  • +
  • Testing the Means Squared Error as function of Complexity
  • +
  • More preprocessing examples, Franke function and regression
  • +
  • Material for lecture Thursday, August 31
  • +
  • Mathematical Interpretation of Ordinary Least Squares
  • +
  • Residual Error
  • +
  • Simple case
  • +
  • The singular value decomposition
  • +
  • Linear Regression Problems
  • +
  • Fixing the singularity
  • +
  • Basic math of the SVD
  • +
  • The SVD, a Fantastic Algorithm
  • +
  • Economy-size SVD
  • +
  • Codes for the SVD
  • +
  • Note about SVD Calculations
  • +
  • Mathematics of the SVD and implications
  • +
  • Example Matrix
  • +
  • Setting up the Matrix to be inverted
  • +
  • Further properties (important for our analyses later)
  • +
  • Meet the Covariance Matrix
  • +
  • Introducing the Covariance and Correlation functions
  • +
  • Covariance and Correlation Matrix
  • +
  • Correlation Function and Design/Feature Matrix
  • +
  • Covariance Matrix Examples
  • +
  • Correlation Matrix
  • +
  • Correlation Matrix with Pandas
  • +
  • Correlation Matrix with Pandas and the Franke function
  • +
  • Rewriting the Covariance and/or Correlation Matrix
  • +
  • Linking with the SVD
  • +
  • What does it mean?
  • +
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • +
  • Ridge and LASSO Regression
  • +
  • Deriving the Ridge Regression Equations
  • +
  • Interpreting the Ridge results
  • +
  • More interpretations
  • +
  • Deriving the Lasso Regression Equations
  • +
  • Exercises for week 35
  • +
  • Exercise 1: Setting up various Python environments
  • +
  • Exercise 2: making your own data and exploring scikit-learn
  • +
  • Exercise 3: Normalizing our data
  • +
  • Exercise 4: Adding Ridge Regression
  • +
  • Exercise 5: Analytical exercises
  • @@ -382,18 +391,17 @@ MathJax.Hub.Config({

     

     

     

    -

    Frequently used scaling functions

    +

    Functionality in Scikit-Learn

    -

    Many features are often scaled using standardization to improve performance. In Scikit-Learn this is given by the StandardScaler function as discussed above. It is easy however to write your own. -Mathematically, this involves subtracting the mean and divide by the standard deviation over the data set, for each feature: -

    - -$$ - x_j^{(i)} \rightarrow \frac{x_j^{(i)} - \overline{x}_j}{\sigma(x_j)}, -$$ - -

    where \( \overline{x}_j \) and \( \sigma(x_j) \) are the mean and standard deviation, respectively, of the feature \( x_j \). -This ensures that each feature has zero mean and unit standard deviation. For data sets where we do not have the standard deviation or don't wish to calculate it, it is then common to simply set it to one. +

    Scikit-Learn has several functions which allow us to rescale the +data, normally resulting in much better results in terms of various +accuracy scores. The StandardScaler function in Scikit-Learn +ensures that for each feature/predictor we study the mean value is +zero and the variance is one (every column in the design/feature +matrix). This scaling has the drawback that it does not ensure that +we have a particular maximum or minimum in our data set. Another +function included in Scikit-Learn is the MinMaxScaler which +ensures that all features are exactly between \( 0 \) and \( 1 \). The

    @@ -421,7 +429,7 @@ This ensures that each feature has zero mean and unit standard deviation. For d

  • 39
  • 40
  • ...
  • -
  • 68
  • +
  • 71
  • »
  • diff --git a/doc/pub/week35/html/._week35-bs031.html b/doc/pub/week35/html/._week35-bs031.html index 2df66069b..db80db56d 100644 --- a/doc/pub/week35/html/._week35-bs031.html +++ b/doc/pub/week35/html/._week35-bs031.html @@ -74,10 +74,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'optimizing-our-parameters'), - ('Our model for the nuclear binding energies', + ('Examples relevant for the exercises', 2, None, - 'our-model-for-the-nuclear-binding-energies'), + 'examples-relevant-for-the-exercises'), ('Optimizing our parameters, more details', 2, None, @@ -94,6 +94,8 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'some-useful-matrix-and-vector-expressions'), + ('The Jacobian', 2, None, 'the-jacobian'), + ('Derivatives, example 1', 2, None, 'derivatives-example-1'), ('Meet the Hessian Matrix', 2, None, 'meet-the-hessian-matrix'), ('Interpretations and optimizing our parameters', 2, @@ -148,6 +150,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'more-preprocessing-examples-franke-function-and-regression'), + ('Material for lecture Thursday, August 31', + 2, + None, + 'material-for-lecture-thursday-august-31'), ('Mathematical Interpretation of Ordinary Least Squares', 2, None, @@ -303,74 +309,77 @@ MathJax.Hub.Config({
  • Why Linear Regression (aka Ordinary Least Squares and family), repeat from last week
  • Regression analysis, overarching aims
  • Regression analysis, overarching aims II
  • -
  • Examples
  • +
  • Examples
  • General linear models
  • Rewriting the fitting procedure as a linear algebra problem
  • Rewriting the fitting procedure as a linear algebra problem, more details
  • Generalizing the fitting procedure as a linear algebra problem
  • Generalizing the fitting procedure as a linear algebra problem
  • Optimizing our parameters
  • -
  • Our model for the nuclear binding energies
  • +
  • Examples relevant for the exercises
  • Optimizing our parameters, more details
  • -
  • Interpretations and optimizing our parameters
  • -
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • Some useful matrix and vector expressions
  • -
  • Meet the Hessian Matrix
  • -
  • Interpretations and optimizing our parameters
  • -
  • Own code for Ordinary Least Squares
  • -
  • Adding error analysis and training set up
  • -
  • Splitting our Data in Training and Test data
  • -
  • Examples
  • -
  • Making your own test-train splitting
  • -
  • The Boston housing data example
  • -
  • Housing data, the code
  • -
  • Reducing the number of degrees of freedom, overarching view
  • -
  • Preprocessing our data
  • -
  • Functionality in Scikit-Learn
  • -
  • More preprocessing
  • -
  • Frequently used scaling functions
  • -
  • Example of own Standard scaling
  • -
  • Min-Max Scaling
  • -
  • Testing the Means Squared Error as function of Complexity
  • -
  • More preprocessing examples, Franke function and regression
  • -
  • Mathematical Interpretation of Ordinary Least Squares
  • -
  • Residual Error
  • -
  • Simple case
  • -
  • The singular value decomposition
  • -
  • Linear Regression Problems
  • -
  • Fixing the singularity
  • -
  • Basic math of the SVD
  • -
  • The SVD, a Fantastic Algorithm
  • -
  • Economy-size SVD
  • -
  • Codes for the SVD
  • -
  • Note about SVD Calculations
  • -
  • Mathematics of the SVD and implications
  • -
  • Example Matrix
  • -
  • Setting up the Matrix to be inverted
  • -
  • Further properties (important for our analyses later)
  • -
  • Meet the Covariance Matrix
  • -
  • Introducing the Covariance and Correlation functions
  • -
  • Covariance and Correlation Matrix
  • -
  • Correlation Function and Design/Feature Matrix
  • -
  • Covariance Matrix Examples
  • -
  • Correlation Matrix
  • -
  • Correlation Matrix with Pandas
  • -
  • Correlation Matrix with Pandas and the Franke function
  • -
  • Rewriting the Covariance and/or Correlation Matrix
  • -
  • Linking with the SVD
  • -
  • What does it mean?
  • -
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • -
  • Ridge and LASSO Regression
  • -
  • Deriving the Ridge Regression Equations
  • -
  • Interpreting the Ridge results
  • -
  • More interpretations
  • -
  • Deriving the Lasso Regression Equations
  • -
  • Exercises for week 35
  • -
  • Exercise 1: Setting up various Python environments
  • -
  • Exercise 2: making your own data and exploring scikit-learn
  • -
  • Exercise 3: Normalizing our data
  • -
  • Exercise 4: Adding Ridge Regression
  • -
  • Exercise 5: Analytical exercises
  • +
  • The Jacobian
  • +
  • Derivatives, example 1
  • +
  • Meet the Hessian Matrix
  • +
  • Interpretations and optimizing our parameters
  • +
  • Own code for Ordinary Least Squares
  • +
  • Adding error analysis and training set up
  • +
  • Splitting our Data in Training and Test data
  • +
  • Examples
  • +
  • Making your own test-train splitting
  • +
  • The Boston housing data example
  • +
  • Housing data, the code
  • +
  • Reducing the number of degrees of freedom, overarching view
  • +
  • Preprocessing our data
  • +
  • Functionality in Scikit-Learn
  • +
  • More preprocessing
  • +
  • Frequently used scaling functions
  • +
  • Example of own Standard scaling
  • +
  • Min-Max Scaling
  • +
  • Testing the Means Squared Error as function of Complexity
  • +
  • More preprocessing examples, Franke function and regression
  • +
  • Material for lecture Thursday, August 31
  • +
  • Mathematical Interpretation of Ordinary Least Squares
  • +
  • Residual Error
  • +
  • Simple case
  • +
  • The singular value decomposition
  • +
  • Linear Regression Problems
  • +
  • Fixing the singularity
  • +
  • Basic math of the SVD
  • +
  • The SVD, a Fantastic Algorithm
  • +
  • Economy-size SVD
  • +
  • Codes for the SVD
  • +
  • Note about SVD Calculations
  • +
  • Mathematics of the SVD and implications
  • +
  • Example Matrix
  • +
  • Setting up the Matrix to be inverted
  • +
  • Further properties (important for our analyses later)
  • +
  • Meet the Covariance Matrix
  • +
  • Introducing the Covariance and Correlation functions
  • +
  • Covariance and Correlation Matrix
  • +
  • Correlation Function and Design/Feature Matrix
  • +
  • Covariance Matrix Examples
  • +
  • Correlation Matrix
  • +
  • Correlation Matrix with Pandas
  • +
  • Correlation Matrix with Pandas and the Franke function
  • +
  • Rewriting the Covariance and/or Correlation Matrix
  • +
  • Linking with the SVD
  • +
  • What does it mean?
  • +
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • +
  • Ridge and LASSO Regression
  • +
  • Deriving the Ridge Regression Equations
  • +
  • Interpreting the Ridge results
  • +
  • More interpretations
  • +
  • Deriving the Lasso Regression Equations
  • +
  • Exercises for week 35
  • +
  • Exercise 1: Setting up various Python environments
  • +
  • Exercise 2: making your own data and exploring scikit-learn
  • +
  • Exercise 3: Normalizing our data
  • +
  • Exercise 4: Adding Ridge Regression
  • +
  • Exercise 5: Analytical exercises
  • @@ -382,60 +391,32 @@ MathJax.Hub.Config({

     

     

     

    -

    Example of own Standard scaling

    +

    More preprocessing

    -

    Let us consider the following vanilla example where we use both -Scikit-Learn and write our own function as well. We produce a -simple test design matrix with random numbers. Each column could then -represent a specific feature whose mean value is subracted. +

    +
    + +

    The Normalizer scales each data +point such that the feature vector has a euclidean length of one. In other words, it +projects a data point on the circle (or sphere in the case of higher dimensions) with a +radius of 1. This means every data point is scaled by a different number (by the +inverse of it’s length). +This normalization is often used when only the direction (or angle) of the data matters, +not the length of the feature vector.

    - - -
    -
    -
    -
    -
    -
    import sklearn.linear_model as skl
    -from sklearn.metrics import mean_squared_error
    -from sklearn.model_selection import  train_test_split
    -from sklearn.preprocessing import MinMaxScaler, StandardScaler, Normalizer
    -import numpy as np
    -import pandas as pd
    -from IPython.display import display
    -np.random.seed(100)
    -# setting up a 10 x 5 matrix
    -rows = 10
    -cols = 5
    -X = np.random.randn(rows,cols)
    -XPandas = pd.DataFrame(X)
    -display(XPandas)
    -print(XPandas.mean())
    -print(XPandas.std())
    -XPandas = (XPandas -XPandas.mean())
    -display(XPandas)
    -#  This option does not include the standard deviation
    -scaler = StandardScaler(with_std=False)
    -scaler.fit(X)
    -Xscaled = scaler.transform(X)
    -display(XPandas-Xscaled)
    -
    +

    The RobustScaler works similarly to the StandardScaler in that it +ensures statistical properties for each feature that guarantee that +they are on the same scale. However, the RobustScaler uses the median +and quartiles, instead of mean and variance. This makes the +RobustScaler ignore data points that are very different from the rest +(like measurement errors). These odd data points are also called +outliers, and might often lead to trouble for other scaling +techniques. +

    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -

    Small exercise: perform the standard scaling by including the standard deviation and compare with what Scikit-Learn gives.

    @@ -462,7 +443,7 @@ display(XPandas-Xscaled)

  • 40
  • 41
  • ...
  • -
  • 68
  • +
  • 71
  • »
  • diff --git a/doc/pub/week35/html/._week35-bs032.html b/doc/pub/week35/html/._week35-bs032.html index 301a2cf3e..dd530f5ed 100644 --- a/doc/pub/week35/html/._week35-bs032.html +++ b/doc/pub/week35/html/._week35-bs032.html @@ -74,10 +74,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'optimizing-our-parameters'), - ('Our model for the nuclear binding energies', + ('Examples relevant for the exercises', 2, None, - 'our-model-for-the-nuclear-binding-energies'), + 'examples-relevant-for-the-exercises'), ('Optimizing our parameters, more details', 2, None, @@ -94,6 +94,8 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'some-useful-matrix-and-vector-expressions'), + ('The Jacobian', 2, None, 'the-jacobian'), + ('Derivatives, example 1', 2, None, 'derivatives-example-1'), ('Meet the Hessian Matrix', 2, None, 'meet-the-hessian-matrix'), ('Interpretations and optimizing our parameters', 2, @@ -148,6 +150,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'more-preprocessing-examples-franke-function-and-regression'), + ('Material for lecture Thursday, August 31', + 2, + None, + 'material-for-lecture-thursday-august-31'), ('Mathematical Interpretation of Ordinary Least Squares', 2, None, @@ -303,74 +309,77 @@ MathJax.Hub.Config({
  • Why Linear Regression (aka Ordinary Least Squares and family), repeat from last week
  • Regression analysis, overarching aims
  • Regression analysis, overarching aims II
  • -
  • Examples
  • +
  • Examples
  • General linear models
  • Rewriting the fitting procedure as a linear algebra problem
  • Rewriting the fitting procedure as a linear algebra problem, more details
  • Generalizing the fitting procedure as a linear algebra problem
  • Generalizing the fitting procedure as a linear algebra problem
  • Optimizing our parameters
  • -
  • Our model for the nuclear binding energies
  • +
  • Examples relevant for the exercises
  • Optimizing our parameters, more details
  • -
  • Interpretations and optimizing our parameters
  • -
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • Some useful matrix and vector expressions
  • -
  • Meet the Hessian Matrix
  • -
  • Interpretations and optimizing our parameters
  • -
  • Own code for Ordinary Least Squares
  • -
  • Adding error analysis and training set up
  • -
  • Splitting our Data in Training and Test data
  • -
  • Examples
  • -
  • Making your own test-train splitting
  • -
  • The Boston housing data example
  • -
  • Housing data, the code
  • -
  • Reducing the number of degrees of freedom, overarching view
  • -
  • Preprocessing our data
  • -
  • Functionality in Scikit-Learn
  • -
  • More preprocessing
  • -
  • Frequently used scaling functions
  • -
  • Example of own Standard scaling
  • -
  • Min-Max Scaling
  • -
  • Testing the Means Squared Error as function of Complexity
  • -
  • More preprocessing examples, Franke function and regression
  • -
  • Mathematical Interpretation of Ordinary Least Squares
  • -
  • Residual Error
  • -
  • Simple case
  • -
  • The singular value decomposition
  • -
  • Linear Regression Problems
  • -
  • Fixing the singularity
  • -
  • Basic math of the SVD
  • -
  • The SVD, a Fantastic Algorithm
  • -
  • Economy-size SVD
  • -
  • Codes for the SVD
  • -
  • Note about SVD Calculations
  • -
  • Mathematics of the SVD and implications
  • -
  • Example Matrix
  • -
  • Setting up the Matrix to be inverted
  • -
  • Further properties (important for our analyses later)
  • -
  • Meet the Covariance Matrix
  • -
  • Introducing the Covariance and Correlation functions
  • -
  • Covariance and Correlation Matrix
  • -
  • Correlation Function and Design/Feature Matrix
  • -
  • Covariance Matrix Examples
  • -
  • Correlation Matrix
  • -
  • Correlation Matrix with Pandas
  • -
  • Correlation Matrix with Pandas and the Franke function
  • -
  • Rewriting the Covariance and/or Correlation Matrix
  • -
  • Linking with the SVD
  • -
  • What does it mean?
  • -
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • -
  • Ridge and LASSO Regression
  • -
  • Deriving the Ridge Regression Equations
  • -
  • Interpreting the Ridge results
  • -
  • More interpretations
  • -
  • Deriving the Lasso Regression Equations
  • -
  • Exercises for week 35
  • -
  • Exercise 1: Setting up various Python environments
  • -
  • Exercise 2: making your own data and exploring scikit-learn
  • -
  • Exercise 3: Normalizing our data
  • -
  • Exercise 4: Adding Ridge Regression
  • -
  • Exercise 5: Analytical exercises
  • +
  • The Jacobian
  • +
  • Derivatives, example 1
  • +
  • Meet the Hessian Matrix
  • +
  • Interpretations and optimizing our parameters
  • +
  • Own code for Ordinary Least Squares
  • +
  • Adding error analysis and training set up
  • +
  • Splitting our Data in Training and Test data
  • +
  • Examples
  • +
  • Making your own test-train splitting
  • +
  • The Boston housing data example
  • +
  • Housing data, the code
  • +
  • Reducing the number of degrees of freedom, overarching view
  • +
  • Preprocessing our data
  • +
  • Functionality in Scikit-Learn
  • +
  • More preprocessing
  • +
  • Frequently used scaling functions
  • +
  • Example of own Standard scaling
  • +
  • Min-Max Scaling
  • +
  • Testing the Means Squared Error as function of Complexity
  • +
  • More preprocessing examples, Franke function and regression
  • +
  • Material for lecture Thursday, August 31
  • +
  • Mathematical Interpretation of Ordinary Least Squares
  • +
  • Residual Error
  • +
  • Simple case
  • +
  • The singular value decomposition
  • +
  • Linear Regression Problems
  • +
  • Fixing the singularity
  • +
  • Basic math of the SVD
  • +
  • The SVD, a Fantastic Algorithm
  • +
  • Economy-size SVD
  • +
  • Codes for the SVD
  • +
  • Note about SVD Calculations
  • +
  • Mathematics of the SVD and implications
  • +
  • Example Matrix
  • +
  • Setting up the Matrix to be inverted
  • +
  • Further properties (important for our analyses later)
  • +
  • Meet the Covariance Matrix
  • +
  • Introducing the Covariance and Correlation functions
  • +
  • Covariance and Correlation Matrix
  • +
  • Correlation Function and Design/Feature Matrix
  • +
  • Covariance Matrix Examples
  • +
  • Correlation Matrix
  • +
  • Correlation Matrix with Pandas
  • +
  • Correlation Matrix with Pandas and the Franke function
  • +
  • Rewriting the Covariance and/or Correlation Matrix
  • +
  • Linking with the SVD
  • +
  • What does it mean?
  • +
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • +
  • Ridge and LASSO Regression
  • +
  • Deriving the Ridge Regression Equations
  • +
  • Interpreting the Ridge results
  • +
  • More interpretations
  • +
  • Deriving the Lasso Regression Equations
  • +
  • Exercises for week 35
  • +
  • Exercise 1: Setting up various Python environments
  • +
  • Exercise 2: making your own data and exploring scikit-learn
  • +
  • Exercise 3: Normalizing our data
  • +
  • Exercise 4: Adding Ridge Regression
  • +
  • Exercise 5: Analytical exercises
  • @@ -382,19 +391,19 @@ MathJax.Hub.Config({

     

     

     

    -

    Min-Max Scaling

    +

    Frequently used scaling functions

    -

    Another commonly used scaling method is min-max scaling. This is very -useful for when we want the features to lie in a certain interval. To -scale the feature \( x_j \) to the interval \( [a, b] \), we can apply the -transformation +

    Many features are often scaled using standardization to improve performance. In Scikit-Learn this is given by the StandardScaler function as discussed above. It is easy however to write your own. +Mathematically, this involves subtracting the mean and divide by the standard deviation over the data set, for each feature:

    $$ -x_j^{(i)} \rightarrow (b-a)\frac{x_j^{(i)} - \min(x_j)}{\max(x_j) - \min(x_j)} - a + x_j^{(i)} \rightarrow \frac{x_j^{(i)} - \overline{x}_j}{\sigma(x_j)}, $$ -

    where \( \min(x_j) \) and \( \max(x_j) \) return the minimum and maximum value of \( x_j \) over the data set, respectively.

    +

    where \( \overline{x}_j \) and \( \sigma(x_j) \) are the mean and standard deviation, respectively, of the feature \( x_j \). +This ensures that each feature has zero mean and unit standard deviation. For data sets where we do not have the standard deviation or don't wish to calculate it, it is then common to simply set it to one. +

    @@ -421,7 +430,7 @@ $$

  • 41
  • 42
  • ...
  • -
  • 68
  • +
  • 71
  • »
  • diff --git a/doc/pub/week35/html/._week35-bs033.html b/doc/pub/week35/html/._week35-bs033.html index 626259b26..3e81b322c 100644 --- a/doc/pub/week35/html/._week35-bs033.html +++ b/doc/pub/week35/html/._week35-bs033.html @@ -74,10 +74,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'optimizing-our-parameters'), - ('Our model for the nuclear binding energies', + ('Examples relevant for the exercises', 2, None, - 'our-model-for-the-nuclear-binding-energies'), + 'examples-relevant-for-the-exercises'), ('Optimizing our parameters, more details', 2, None, @@ -94,6 +94,8 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'some-useful-matrix-and-vector-expressions'), + ('The Jacobian', 2, None, 'the-jacobian'), + ('Derivatives, example 1', 2, None, 'derivatives-example-1'), ('Meet the Hessian Matrix', 2, None, 'meet-the-hessian-matrix'), ('Interpretations and optimizing our parameters', 2, @@ -148,6 +150,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'more-preprocessing-examples-franke-function-and-regression'), + ('Material for lecture Thursday, August 31', + 2, + None, + 'material-for-lecture-thursday-august-31'), ('Mathematical Interpretation of Ordinary Least Squares', 2, None, @@ -303,74 +309,77 @@ MathJax.Hub.Config({
  • Why Linear Regression (aka Ordinary Least Squares and family), repeat from last week
  • Regression analysis, overarching aims
  • Regression analysis, overarching aims II
  • -
  • Examples
  • +
  • Examples
  • General linear models
  • Rewriting the fitting procedure as a linear algebra problem
  • Rewriting the fitting procedure as a linear algebra problem, more details
  • Generalizing the fitting procedure as a linear algebra problem
  • Generalizing the fitting procedure as a linear algebra problem
  • Optimizing our parameters
  • -
  • Our model for the nuclear binding energies
  • +
  • Examples relevant for the exercises
  • Optimizing our parameters, more details
  • -
  • Interpretations and optimizing our parameters
  • -
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • Some useful matrix and vector expressions
  • -
  • Meet the Hessian Matrix
  • -
  • Interpretations and optimizing our parameters
  • -
  • Own code for Ordinary Least Squares
  • -
  • Adding error analysis and training set up
  • -
  • Splitting our Data in Training and Test data
  • -
  • Examples
  • -
  • Making your own test-train splitting
  • -
  • The Boston housing data example
  • -
  • Housing data, the code
  • -
  • Reducing the number of degrees of freedom, overarching view
  • -
  • Preprocessing our data
  • -
  • Functionality in Scikit-Learn
  • -
  • More preprocessing
  • -
  • Frequently used scaling functions
  • -
  • Example of own Standard scaling
  • -
  • Min-Max Scaling
  • -
  • Testing the Means Squared Error as function of Complexity
  • -
  • More preprocessing examples, Franke function and regression
  • -
  • Mathematical Interpretation of Ordinary Least Squares
  • -
  • Residual Error
  • -
  • Simple case
  • -
  • The singular value decomposition
  • -
  • Linear Regression Problems
  • -
  • Fixing the singularity
  • -
  • Basic math of the SVD
  • -
  • The SVD, a Fantastic Algorithm
  • -
  • Economy-size SVD
  • -
  • Codes for the SVD
  • -
  • Note about SVD Calculations
  • -
  • Mathematics of the SVD and implications
  • -
  • Example Matrix
  • -
  • Setting up the Matrix to be inverted
  • -
  • Further properties (important for our analyses later)
  • -
  • Meet the Covariance Matrix
  • -
  • Introducing the Covariance and Correlation functions
  • -
  • Covariance and Correlation Matrix
  • -
  • Correlation Function and Design/Feature Matrix
  • -
  • Covariance Matrix Examples
  • -
  • Correlation Matrix
  • -
  • Correlation Matrix with Pandas
  • -
  • Correlation Matrix with Pandas and the Franke function
  • -
  • Rewriting the Covariance and/or Correlation Matrix
  • -
  • Linking with the SVD
  • -
  • What does it mean?
  • -
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • -
  • Ridge and LASSO Regression
  • -
  • Deriving the Ridge Regression Equations
  • -
  • Interpreting the Ridge results
  • -
  • More interpretations
  • -
  • Deriving the Lasso Regression Equations
  • -
  • Exercises for week 35
  • -
  • Exercise 1: Setting up various Python environments
  • -
  • Exercise 2: making your own data and exploring scikit-learn
  • -
  • Exercise 3: Normalizing our data
  • -
  • Exercise 4: Adding Ridge Regression
  • -
  • Exercise 5: Analytical exercises
  • +
  • The Jacobian
  • +
  • Derivatives, example 1
  • +
  • Meet the Hessian Matrix
  • +
  • Interpretations and optimizing our parameters
  • +
  • Own code for Ordinary Least Squares
  • +
  • Adding error analysis and training set up
  • +
  • Splitting our Data in Training and Test data
  • +
  • Examples
  • +
  • Making your own test-train splitting
  • +
  • The Boston housing data example
  • +
  • Housing data, the code
  • +
  • Reducing the number of degrees of freedom, overarching view
  • +
  • Preprocessing our data
  • +
  • Functionality in Scikit-Learn
  • +
  • More preprocessing
  • +
  • Frequently used scaling functions
  • +
  • Example of own Standard scaling
  • +
  • Min-Max Scaling
  • +
  • Testing the Means Squared Error as function of Complexity
  • +
  • More preprocessing examples, Franke function and regression
  • +
  • Material for lecture Thursday, August 31
  • +
  • Mathematical Interpretation of Ordinary Least Squares
  • +
  • Residual Error
  • +
  • Simple case
  • +
  • The singular value decomposition
  • +
  • Linear Regression Problems
  • +
  • Fixing the singularity
  • +
  • Basic math of the SVD
  • +
  • The SVD, a Fantastic Algorithm
  • +
  • Economy-size SVD
  • +
  • Codes for the SVD
  • +
  • Note about SVD Calculations
  • +
  • Mathematics of the SVD and implications
  • +
  • Example Matrix
  • +
  • Setting up the Matrix to be inverted
  • +
  • Further properties (important for our analyses later)
  • +
  • Meet the Covariance Matrix
  • +
  • Introducing the Covariance and Correlation functions
  • +
  • Covariance and Correlation Matrix
  • +
  • Correlation Function and Design/Feature Matrix
  • +
  • Covariance Matrix Examples
  • +
  • Correlation Matrix
  • +
  • Correlation Matrix with Pandas
  • +
  • Correlation Matrix with Pandas and the Franke function
  • +
  • Rewriting the Covariance and/or Correlation Matrix
  • +
  • Linking with the SVD
  • +
  • What does it mean?
  • +
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • +
  • Ridge and LASSO Regression
  • +
  • Deriving the Ridge Regression Equations
  • +
  • Interpreting the Ridge results
  • +
  • More interpretations
  • +
  • Deriving the Lasso Regression Equations
  • +
  • Exercises for week 35
  • +
  • Exercise 1: Setting up various Python environments
  • +
  • Exercise 2: making your own data and exploring scikit-learn
  • +
  • Exercise 3: Normalizing our data
  • +
  • Exercise 4: Adding Ridge Regression
  • +
  • Exercise 5: Analytical exercises
  • @@ -382,13 +391,14 @@ MathJax.Hub.Config({

     

     

     

    -

    Testing the Means Squared Error as function of Complexity

    -

    One of -the aims is to reproduce Figure 2.11 of Hastie et al. -We will also use Ridge and Lasso regression. +

    Example of own Standard scaling

    + +

    Let us consider the following vanilla example where we use both +Scikit-Learn and write our own function as well. We produce a +simple test design matrix with random numbers. Each column could then +represent a specific feature whose mean value is subracted.

    -

    Our data is defined by \( x\in [-3,3] \) with a total of for example \( 100 \) data points.

    @@ -396,73 +406,29 @@ We will also use Ridge and Lasso regression.
    -
    np.random.seed()
    -n = 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)
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    - -

    where \( y \) is the function we want to fit with a given polynomial.

    - -

    Write a first code which sets up a design matrix \( X \) defined by a fifth-order polynomial. Scale your data and split it in training and test data.

    - - -
    -
    -
    -
    -
    -
    import matplotlib.pyplot as plt
    +  
    import sklearn.linear_model as skl
    +from sklearn.metrics import mean_squared_error
    +from sklearn.model_selection import  train_test_split
    +from sklearn.preprocessing import MinMaxScaler, StandardScaler, Normalizer
     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
    -
    -
    -np.random.seed(2018)
    -n = 50
    -maxdegree = 5
    -# 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)
    -TestError = np.zeros(maxdegree)
    -TrainError = np.zeros(maxdegree)
    -polydegree = np.zeros(maxdegree)
    -x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2)
    -scaler = StandardScaler()
    -scaler.fit(x_train)
    -x_train_scaled = scaler.transform(x_train)
    -x_test_scaled = scaler.transform(x_test)
    -
    -for degree in range(maxdegree):
    -    model = make_pipeline(PolynomialFeatures(degree=degree), LinearRegression(fit_intercept=False))
    -    clf = model.fit(x_train_scaled,y_train)
    -    y_fit = clf.predict(x_train_scaled)
    -    y_pred = clf.predict(x_test_scaled) 
    -    polydegree[degree] = degree
    -    TestError[degree] = np.mean( np.mean((y_test - y_pred)**2) )
    -    TrainError[degree] = np.mean( np.mean((y_train - y_fit)**2) )
    -
    -plt.plot(polydegree, TestError, label='Test Error')
    -plt.plot(polydegree, TrainError, label='Train Error')
    -plt.legend()
    -plt.show()
    +import pandas as pd
    +from IPython.display import display
    +np.random.seed(100)
    +# setting up a 10 x 5 matrix
    +rows = 10
    +cols = 5
    +X = np.random.randn(rows,cols)
    +XPandas = pd.DataFrame(X)
    +display(XPandas)
    +print(XPandas.mean())
    +print(XPandas.std())
    +XPandas = (XPandas -XPandas.mean())
    +display(XPandas)
    +#  This option does not include the standard deviation
    +scaler = StandardScaler(with_std=False)
    +scaler.fit(X)
    +Xscaled = scaler.transform(X)
    +display(XPandas-Xscaled)
     
    @@ -478,6 +444,7 @@ plt.show()
    +

    Small exercise: perform the standard scaling by including the standard deviation and compare with what Scikit-Learn gives.

    @@ -504,7 +471,7 @@ plt.show()

  • 42
  • 43
  • ...
  • -
  • 68
  • +
  • 71
  • »
  • diff --git a/doc/pub/week35/html/._week35-bs034.html b/doc/pub/week35/html/._week35-bs034.html index a8d7b3711..6e39bd03b 100644 --- a/doc/pub/week35/html/._week35-bs034.html +++ b/doc/pub/week35/html/._week35-bs034.html @@ -74,10 +74,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'optimizing-our-parameters'), - ('Our model for the nuclear binding energies', + ('Examples relevant for the exercises', 2, None, - 'our-model-for-the-nuclear-binding-energies'), + 'examples-relevant-for-the-exercises'), ('Optimizing our parameters, more details', 2, None, @@ -94,6 +94,8 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'some-useful-matrix-and-vector-expressions'), + ('The Jacobian', 2, None, 'the-jacobian'), + ('Derivatives, example 1', 2, None, 'derivatives-example-1'), ('Meet the Hessian Matrix', 2, None, 'meet-the-hessian-matrix'), ('Interpretations and optimizing our parameters', 2, @@ -148,6 +150,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'more-preprocessing-examples-franke-function-and-regression'), + ('Material for lecture Thursday, August 31', + 2, + None, + 'material-for-lecture-thursday-august-31'), ('Mathematical Interpretation of Ordinary Least Squares', 2, None, @@ -303,74 +309,77 @@ MathJax.Hub.Config({
  • Why Linear Regression (aka Ordinary Least Squares and family), repeat from last week
  • Regression analysis, overarching aims
  • Regression analysis, overarching aims II
  • -
  • Examples
  • +
  • Examples
  • General linear models
  • Rewriting the fitting procedure as a linear algebra problem
  • Rewriting the fitting procedure as a linear algebra problem, more details
  • Generalizing the fitting procedure as a linear algebra problem
  • Generalizing the fitting procedure as a linear algebra problem
  • Optimizing our parameters
  • -
  • Our model for the nuclear binding energies
  • +
  • Examples relevant for the exercises
  • Optimizing our parameters, more details
  • -
  • Interpretations and optimizing our parameters
  • -
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • Some useful matrix and vector expressions
  • -
  • Meet the Hessian Matrix
  • -
  • Interpretations and optimizing our parameters
  • -
  • Own code for Ordinary Least Squares
  • -
  • Adding error analysis and training set up
  • -
  • Splitting our Data in Training and Test data
  • -
  • Examples
  • -
  • Making your own test-train splitting
  • -
  • The Boston housing data example
  • -
  • Housing data, the code
  • -
  • Reducing the number of degrees of freedom, overarching view
  • -
  • Preprocessing our data
  • -
  • Functionality in Scikit-Learn
  • -
  • More preprocessing
  • -
  • Frequently used scaling functions
  • -
  • Example of own Standard scaling
  • -
  • Min-Max Scaling
  • -
  • Testing the Means Squared Error as function of Complexity
  • -
  • More preprocessing examples, Franke function and regression
  • -
  • Mathematical Interpretation of Ordinary Least Squares
  • -
  • Residual Error
  • -
  • Simple case
  • -
  • The singular value decomposition
  • -
  • Linear Regression Problems
  • -
  • Fixing the singularity
  • -
  • Basic math of the SVD
  • -
  • The SVD, a Fantastic Algorithm
  • -
  • Economy-size SVD
  • -
  • Codes for the SVD
  • -
  • Note about SVD Calculations
  • -
  • Mathematics of the SVD and implications
  • -
  • Example Matrix
  • -
  • Setting up the Matrix to be inverted
  • -
  • Further properties (important for our analyses later)
  • -
  • Meet the Covariance Matrix
  • -
  • Introducing the Covariance and Correlation functions
  • -
  • Covariance and Correlation Matrix
  • -
  • Correlation Function and Design/Feature Matrix
  • -
  • Covariance Matrix Examples
  • -
  • Correlation Matrix
  • -
  • Correlation Matrix with Pandas
  • -
  • Correlation Matrix with Pandas and the Franke function
  • -
  • Rewriting the Covariance and/or Correlation Matrix
  • -
  • Linking with the SVD
  • -
  • What does it mean?
  • -
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • -
  • Ridge and LASSO Regression
  • -
  • Deriving the Ridge Regression Equations
  • -
  • Interpreting the Ridge results
  • -
  • More interpretations
  • -
  • Deriving the Lasso Regression Equations
  • -
  • Exercises for week 35
  • -
  • Exercise 1: Setting up various Python environments
  • -
  • Exercise 2: making your own data and exploring scikit-learn
  • -
  • Exercise 3: Normalizing our data
  • -
  • Exercise 4: Adding Ridge Regression
  • -
  • Exercise 5: Analytical exercises
  • +
  • The Jacobian
  • +
  • Derivatives, example 1
  • +
  • Meet the Hessian Matrix
  • +
  • Interpretations and optimizing our parameters
  • +
  • Own code for Ordinary Least Squares
  • +
  • Adding error analysis and training set up
  • +
  • Splitting our Data in Training and Test data
  • +
  • Examples
  • +
  • Making your own test-train splitting
  • +
  • The Boston housing data example
  • +
  • Housing data, the code
  • +
  • Reducing the number of degrees of freedom, overarching view
  • +
  • Preprocessing our data
  • +
  • Functionality in Scikit-Learn
  • +
  • More preprocessing
  • +
  • Frequently used scaling functions
  • +
  • Example of own Standard scaling
  • +
  • Min-Max Scaling
  • +
  • Testing the Means Squared Error as function of Complexity
  • +
  • More preprocessing examples, Franke function and regression
  • +
  • Material for lecture Thursday, August 31
  • +
  • Mathematical Interpretation of Ordinary Least Squares
  • +
  • Residual Error
  • +
  • Simple case
  • +
  • The singular value decomposition
  • +
  • Linear Regression Problems
  • +
  • Fixing the singularity
  • +
  • Basic math of the SVD
  • +
  • The SVD, a Fantastic Algorithm
  • +
  • Economy-size SVD
  • +
  • Codes for the SVD
  • +
  • Note about SVD Calculations
  • +
  • Mathematics of the SVD and implications
  • +
  • Example Matrix
  • +
  • Setting up the Matrix to be inverted
  • +
  • Further properties (important for our analyses later)
  • +
  • Meet the Covariance Matrix
  • +
  • Introducing the Covariance and Correlation functions
  • +
  • Covariance and Correlation Matrix
  • +
  • Correlation Function and Design/Feature Matrix
  • +
  • Covariance Matrix Examples
  • +
  • Correlation Matrix
  • +
  • Correlation Matrix with Pandas
  • +
  • Correlation Matrix with Pandas and the Franke function
  • +
  • Rewriting the Covariance and/or Correlation Matrix
  • +
  • Linking with the SVD
  • +
  • What does it mean?
  • +
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • +
  • Ridge and LASSO Regression
  • +
  • Deriving the Ridge Regression Equations
  • +
  • Interpreting the Ridge results
  • +
  • More interpretations
  • +
  • Deriving the Lasso Regression Equations
  • +
  • Exercises for week 35
  • +
  • Exercise 1: Setting up various Python environments
  • +
  • Exercise 2: making your own data and exploring scikit-learn
  • +
  • Exercise 3: Normalizing our data
  • +
  • Exercise 4: Adding Ridge Regression
  • +
  • Exercise 5: Analytical exercises
  • @@ -382,122 +391,19 @@ MathJax.Hub.Config({

     

     

     

    -

    More preprocessing examples, Franke function and regression

    +

    Min-Max Scaling

    +

    Another commonly used scaling method is min-max scaling. This is very +useful for when we want the features to lie in a certain interval. To +scale the feature \( x_j \) to the interval \( [a, b] \), we can apply the +transformation +

    - -
    -
    -
    -
    -
    -
    # Common imports
    -import os
    -import numpy as np
    -import pandas as pd
    -import matplotlib.pyplot as plt
    -import sklearn.linear_model as skl
    -from sklearn.metrics import mean_squared_error
    -from sklearn.model_selection import  train_test_split
    -from sklearn.preprocessing import MinMaxScaler, StandardScaler, Normalizer
    -
    -# 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')
    -
    -
    -def FrankeFunction(x,y):
    -	term1 = 0.75*np.exp(-(0.25*(9*x-2)**2) - 0.25*((9*y-2)**2))
    -	term2 = 0.75*np.exp(-((9*x+1)**2)/49.0 - 0.1*(9*y+1))
    -	term3 = 0.5*np.exp(-(9*x-7)**2/4.0 - 0.25*((9*y-3)**2))
    -	term4 = -0.2*np.exp(-(9*x-4)**2 - (9*y-7)**2)
    -	return term1 + term2 + term3 + term4
    -
    -
    -def create_X(x, y, n ):
    -	if len(x.shape) > 1:
    -		x = np.ravel(x)
    -		y = np.ravel(y)
    -
    -	N = len(x)
    -	l = int((n+1)*(n+2)/2)		# Number of elements in beta
    -	X = np.ones((N,l))
    -
    -	for i in range(1,n+1):
    -		q = int((i)*(i+1)/2)
    -		for k in range(i+1):
    -			X[:,q+k] = (x**(i-k))*(y**k)
    -
    -	return X
    -
    -
    -# Making meshgrid of datapoints and compute Franke's function
    -n = 5
    -N = 1000
    -x = np.sort(np.random.uniform(0, 1, N))
    -y = np.sort(np.random.uniform(0, 1, N))
    -z = FrankeFunction(x, y)
    -X = create_X(x, y, n=n)    
    -# split in training and test data
    -X_train, X_test, y_train, y_test = train_test_split(X,z,test_size=0.2)
    -
    -
    -clf = skl.LinearRegression().fit(X_train, y_train)
    -
    -# The mean squared error and R2 score
    -print("MSE before scaling: {:.2f}".format(mean_squared_error(clf.predict(X_test), y_test)))
    -print("R2 score before scaling {:.2f}".format(clf.score(X_test,y_test)))
    -
    -scaler = StandardScaler()
    -scaler.fit(X_train)
    -X_train_scaled = scaler.transform(X_train)
    -X_test_scaled = scaler.transform(X_test)
    -
    -print("Feature min values before scaling:\n {}".format(X_train.min(axis=0)))
    -print("Feature max values before scaling:\n {}".format(X_train.max(axis=0)))
    -
    -print("Feature min values after scaling:\n {}".format(X_train_scaled.min(axis=0)))
    -print("Feature max values after scaling:\n {}".format(X_train_scaled.max(axis=0)))
    -
    -clf = skl.LinearRegression().fit(X_train_scaled, y_train)
    -
    -
    -print("MSE after  scaling: {:.2f}".format(mean_squared_error(clf.predict(X_test_scaled), y_test)))
    -print("R2 score for  scaled data: {:.2f}".format(clf.score(X_test_scaled,y_test)))
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    +$$ +x_j^{(i)} \rightarrow (b-a)\frac{x_j^{(i)} - \min(x_j)}{\max(x_j) - \min(x_j)} - a +$$ +

    where \( \min(x_j) \) and \( \max(x_j) \) return the minimum and maximum value of \( x_j \) over the data set, respectively.

    @@ -524,7 +430,7 @@ clf = skl.43

  • 44
  • ...
  • -
  • 68
  • +
  • 71
  • »
  • diff --git a/doc/pub/week35/html/._week35-bs035.html b/doc/pub/week35/html/._week35-bs035.html index 4b189917a..424f26ee8 100644 --- a/doc/pub/week35/html/._week35-bs035.html +++ b/doc/pub/week35/html/._week35-bs035.html @@ -74,10 +74,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'optimizing-our-parameters'), - ('Our model for the nuclear binding energies', + ('Examples relevant for the exercises', 2, None, - 'our-model-for-the-nuclear-binding-energies'), + 'examples-relevant-for-the-exercises'), ('Optimizing our parameters, more details', 2, None, @@ -94,6 +94,8 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'some-useful-matrix-and-vector-expressions'), + ('The Jacobian', 2, None, 'the-jacobian'), + ('Derivatives, example 1', 2, None, 'derivatives-example-1'), ('Meet the Hessian Matrix', 2, None, 'meet-the-hessian-matrix'), ('Interpretations and optimizing our parameters', 2, @@ -148,6 +150,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'more-preprocessing-examples-franke-function-and-regression'), + ('Material for lecture Thursday, August 31', + 2, + None, + 'material-for-lecture-thursday-august-31'), ('Mathematical Interpretation of Ordinary Least Squares', 2, None, @@ -303,74 +309,77 @@ MathJax.Hub.Config({
  • Why Linear Regression (aka Ordinary Least Squares and family), repeat from last week
  • Regression analysis, overarching aims
  • Regression analysis, overarching aims II
  • -
  • Examples
  • +
  • Examples
  • General linear models
  • Rewriting the fitting procedure as a linear algebra problem
  • Rewriting the fitting procedure as a linear algebra problem, more details
  • Generalizing the fitting procedure as a linear algebra problem
  • Generalizing the fitting procedure as a linear algebra problem
  • Optimizing our parameters
  • -
  • Our model for the nuclear binding energies
  • +
  • Examples relevant for the exercises
  • Optimizing our parameters, more details
  • -
  • Interpretations and optimizing our parameters
  • -
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • Some useful matrix and vector expressions
  • -
  • Meet the Hessian Matrix
  • -
  • Interpretations and optimizing our parameters
  • -
  • Own code for Ordinary Least Squares
  • -
  • Adding error analysis and training set up
  • -
  • Splitting our Data in Training and Test data
  • -
  • Examples
  • -
  • Making your own test-train splitting
  • -
  • The Boston housing data example
  • -
  • Housing data, the code
  • -
  • Reducing the number of degrees of freedom, overarching view
  • -
  • Preprocessing our data
  • -
  • Functionality in Scikit-Learn
  • -
  • More preprocessing
  • -
  • Frequently used scaling functions
  • -
  • Example of own Standard scaling
  • -
  • Min-Max Scaling
  • -
  • Testing the Means Squared Error as function of Complexity
  • -
  • More preprocessing examples, Franke function and regression
  • -
  • Mathematical Interpretation of Ordinary Least Squares
  • -
  • Residual Error
  • -
  • Simple case
  • -
  • The singular value decomposition
  • -
  • Linear Regression Problems
  • -
  • Fixing the singularity
  • -
  • Basic math of the SVD
  • -
  • The SVD, a Fantastic Algorithm
  • -
  • Economy-size SVD
  • -
  • Codes for the SVD
  • -
  • Note about SVD Calculations
  • -
  • Mathematics of the SVD and implications
  • -
  • Example Matrix
  • -
  • Setting up the Matrix to be inverted
  • -
  • Further properties (important for our analyses later)
  • -
  • Meet the Covariance Matrix
  • -
  • Introducing the Covariance and Correlation functions
  • -
  • Covariance and Correlation Matrix
  • -
  • Correlation Function and Design/Feature Matrix
  • -
  • Covariance Matrix Examples
  • -
  • Correlation Matrix
  • -
  • Correlation Matrix with Pandas
  • -
  • Correlation Matrix with Pandas and the Franke function
  • -
  • Rewriting the Covariance and/or Correlation Matrix
  • -
  • Linking with the SVD
  • -
  • What does it mean?
  • -
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • -
  • Ridge and LASSO Regression
  • -
  • Deriving the Ridge Regression Equations
  • -
  • Interpreting the Ridge results
  • -
  • More interpretations
  • -
  • Deriving the Lasso Regression Equations
  • -
  • Exercises for week 35
  • -
  • Exercise 1: Setting up various Python environments
  • -
  • Exercise 2: making your own data and exploring scikit-learn
  • -
  • Exercise 3: Normalizing our data
  • -
  • Exercise 4: Adding Ridge Regression
  • -
  • Exercise 5: Analytical exercises
  • +
  • The Jacobian
  • +
  • Derivatives, example 1
  • +
  • Meet the Hessian Matrix
  • +
  • Interpretations and optimizing our parameters
  • +
  • Own code for Ordinary Least Squares
  • +
  • Adding error analysis and training set up
  • +
  • Splitting our Data in Training and Test data
  • +
  • Examples
  • +
  • Making your own test-train splitting
  • +
  • The Boston housing data example
  • +
  • Housing data, the code
  • +
  • Reducing the number of degrees of freedom, overarching view
  • +
  • Preprocessing our data
  • +
  • Functionality in Scikit-Learn
  • +
  • More preprocessing
  • +
  • Frequently used scaling functions
  • +
  • Example of own Standard scaling
  • +
  • Min-Max Scaling
  • +
  • Testing the Means Squared Error as function of Complexity
  • +
  • More preprocessing examples, Franke function and regression
  • +
  • Material for lecture Thursday, August 31
  • +
  • Mathematical Interpretation of Ordinary Least Squares
  • +
  • Residual Error
  • +
  • Simple case
  • +
  • The singular value decomposition
  • +
  • Linear Regression Problems
  • +
  • Fixing the singularity
  • +
  • Basic math of the SVD
  • +
  • The SVD, a Fantastic Algorithm
  • +
  • Economy-size SVD
  • +
  • Codes for the SVD
  • +
  • Note about SVD Calculations
  • +
  • Mathematics of the SVD and implications
  • +
  • Example Matrix
  • +
  • Setting up the Matrix to be inverted
  • +
  • Further properties (important for our analyses later)
  • +
  • Meet the Covariance Matrix
  • +
  • Introducing the Covariance and Correlation functions
  • +
  • Covariance and Correlation Matrix
  • +
  • Correlation Function and Design/Feature Matrix
  • +
  • Covariance Matrix Examples
  • +
  • Correlation Matrix
  • +
  • Correlation Matrix with Pandas
  • +
  • Correlation Matrix with Pandas and the Franke function
  • +
  • Rewriting the Covariance and/or Correlation Matrix
  • +
  • Linking with the SVD
  • +
  • What does it mean?
  • +
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • +
  • Ridge and LASSO Regression
  • +
  • Deriving the Ridge Regression Equations
  • +
  • Interpreting the Ridge results
  • +
  • More interpretations
  • +
  • Deriving the Lasso Regression Equations
  • +
  • Exercises for week 35
  • +
  • Exercise 1: Setting up various Python environments
  • +
  • Exercise 2: making your own data and exploring scikit-learn
  • +
  • Exercise 3: Normalizing our data
  • +
  • Exercise 4: Adding Ridge Regression
  • +
  • Exercise 5: Analytical exercises
  • @@ -382,38 +391,103 @@ MathJax.Hub.Config({

     

     

     

    -

    Mathematical Interpretation of Ordinary Least Squares

    - -

    What is presented here is a mathematical analysis of various regression algorithms (ordinary least squares, Ridge and Lasso Regression). The analysis is based on an important algorithm in linear algebra, the so-called Singular Value Decomposition (SVD).

    - -

    We have shown that in ordinary least squares the optimal parameters \( \beta \) are given by

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

    The hat over \( \boldsymbol{\beta} \) means we have the optimal parameters after minimization of the cost function.

    - -

    This means that our best model is defined as

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

    We now define a matrix

    -$$ -\boldsymbol{A}=\boldsymbol{X}\left(\boldsymbol{X}^T\boldsymbol{X}\right)^{-1}\boldsymbol{X}^T. -$$ - -

    We can rewrite

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

    The matrix \( \boldsymbol{A} \) has the important property that \( \boldsymbol{A}^2=\boldsymbol{A} \). This is the definition of a projection matrix. -We can then interpret our optimal model \( \tilde{\boldsymbol{y}} \) as being represented by an orthogonal projection of \( \boldsymbol{y} \) onto a space defined by the column vectors of \( \boldsymbol{X} \). In our case here the matrix \( \boldsymbol{A} \) is a square matrix. If it is a general rectangular matrix we have an oblique projection matrix. +

    Testing the Means Squared Error as function of Complexity

    +

    One of +the aims is to reproduce Figure 2.11 of Hastie et al. +We will also use Ridge and Lasso regression.

    +

    Our data is defined by \( x\in [-3,3] \) with a total of for example \( 100 \) data points.

    + + +
    +
    +
    +
    +
    +
    np.random.seed()
    +n = 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)
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    + +

    where \( y \) is the function we want to fit with a given polynomial.

    + +

    Write a first code which sets up a design matrix \( X \) defined by a fifth-order polynomial. Scale your data and split it in training and test data.

    + + +
    +
    +
    +
    +
    +
    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
    +
    +
    +np.random.seed(2018)
    +n = 50
    +maxdegree = 5
    +# 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)
    +TestError = np.zeros(maxdegree)
    +TrainError = np.zeros(maxdegree)
    +polydegree = np.zeros(maxdegree)
    +x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2)
    +scaler = StandardScaler()
    +scaler.fit(x_train)
    +x_train_scaled = scaler.transform(x_train)
    +x_test_scaled = scaler.transform(x_test)
    +
    +for degree in range(maxdegree):
    +    model = make_pipeline(PolynomialFeatures(degree=degree), LinearRegression(fit_intercept=False))
    +    clf = model.fit(x_train_scaled,y_train)
    +    y_fit = clf.predict(x_train_scaled)
    +    y_pred = clf.predict(x_test_scaled) 
    +    polydegree[degree] = degree
    +    TestError[degree] = np.mean( np.mean((y_test - y_pred)**2) )
    +    TrainError[degree] = np.mean( np.mean((y_train - y_fit)**2) )
    +
    +plt.plot(polydegree, TestError, label='Test Error')
    +plt.plot(polydegree, TrainError, label='Train Error')
    +plt.legend()
    +plt.show()
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    + +

      @@ -439,7 +513,7 @@ We can then interpret our optimal model \( \tilde{\boldsymbol{y}} \) as being re
    • 44
    • 45
    • ...
    • -
    • 68
    • +
    • 71
    • »
    diff --git a/doc/pub/week35/html/._week35-bs036.html b/doc/pub/week35/html/._week35-bs036.html index 9246a46c1..9020e07e1 100644 --- a/doc/pub/week35/html/._week35-bs036.html +++ b/doc/pub/week35/html/._week35-bs036.html @@ -74,10 +74,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'optimizing-our-parameters'), - ('Our model for the nuclear binding energies', + ('Examples relevant for the exercises', 2, None, - 'our-model-for-the-nuclear-binding-energies'), + 'examples-relevant-for-the-exercises'), ('Optimizing our parameters, more details', 2, None, @@ -94,6 +94,8 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'some-useful-matrix-and-vector-expressions'), + ('The Jacobian', 2, None, 'the-jacobian'), + ('Derivatives, example 1', 2, None, 'derivatives-example-1'), ('Meet the Hessian Matrix', 2, None, 'meet-the-hessian-matrix'), ('Interpretations and optimizing our parameters', 2, @@ -148,6 +150,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'more-preprocessing-examples-franke-function-and-regression'), + ('Material for lecture Thursday, August 31', + 2, + None, + 'material-for-lecture-thursday-august-31'), ('Mathematical Interpretation of Ordinary Least Squares', 2, None, @@ -303,74 +309,77 @@ MathJax.Hub.Config({
  • Why Linear Regression (aka Ordinary Least Squares and family), repeat from last week
  • Regression analysis, overarching aims
  • Regression analysis, overarching aims II
  • -
  • Examples
  • +
  • Examples
  • General linear models
  • Rewriting the fitting procedure as a linear algebra problem
  • Rewriting the fitting procedure as a linear algebra problem, more details
  • Generalizing the fitting procedure as a linear algebra problem
  • Generalizing the fitting procedure as a linear algebra problem
  • Optimizing our parameters
  • -
  • Our model for the nuclear binding energies
  • +
  • Examples relevant for the exercises
  • Optimizing our parameters, more details
  • -
  • Interpretations and optimizing our parameters
  • -
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • Some useful matrix and vector expressions
  • -
  • Meet the Hessian Matrix
  • -
  • Interpretations and optimizing our parameters
  • -
  • Own code for Ordinary Least Squares
  • -
  • Adding error analysis and training set up
  • -
  • Splitting our Data in Training and Test data
  • -
  • Examples
  • -
  • Making your own test-train splitting
  • -
  • The Boston housing data example
  • -
  • Housing data, the code
  • -
  • Reducing the number of degrees of freedom, overarching view
  • -
  • Preprocessing our data
  • -
  • Functionality in Scikit-Learn
  • -
  • More preprocessing
  • -
  • Frequently used scaling functions
  • -
  • Example of own Standard scaling
  • -
  • Min-Max Scaling
  • -
  • Testing the Means Squared Error as function of Complexity
  • -
  • More preprocessing examples, Franke function and regression
  • -
  • Mathematical Interpretation of Ordinary Least Squares
  • -
  • Residual Error
  • -
  • Simple case
  • -
  • The singular value decomposition
  • -
  • Linear Regression Problems
  • -
  • Fixing the singularity
  • -
  • Basic math of the SVD
  • -
  • The SVD, a Fantastic Algorithm
  • -
  • Economy-size SVD
  • -
  • Codes for the SVD
  • -
  • Note about SVD Calculations
  • -
  • Mathematics of the SVD and implications
  • -
  • Example Matrix
  • -
  • Setting up the Matrix to be inverted
  • -
  • Further properties (important for our analyses later)
  • -
  • Meet the Covariance Matrix
  • -
  • Introducing the Covariance and Correlation functions
  • -
  • Covariance and Correlation Matrix
  • -
  • Correlation Function and Design/Feature Matrix
  • -
  • Covariance Matrix Examples
  • -
  • Correlation Matrix
  • -
  • Correlation Matrix with Pandas
  • -
  • Correlation Matrix with Pandas and the Franke function
  • -
  • Rewriting the Covariance and/or Correlation Matrix
  • -
  • Linking with the SVD
  • -
  • What does it mean?
  • -
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • -
  • Ridge and LASSO Regression
  • -
  • Deriving the Ridge Regression Equations
  • -
  • Interpreting the Ridge results
  • -
  • More interpretations
  • -
  • Deriving the Lasso Regression Equations
  • -
  • Exercises for week 35
  • -
  • Exercise 1: Setting up various Python environments
  • -
  • Exercise 2: making your own data and exploring scikit-learn
  • -
  • Exercise 3: Normalizing our data
  • -
  • Exercise 4: Adding Ridge Regression
  • -
  • Exercise 5: Analytical exercises
  • +
  • The Jacobian
  • +
  • Derivatives, example 1
  • +
  • Meet the Hessian Matrix
  • +
  • Interpretations and optimizing our parameters
  • +
  • Own code for Ordinary Least Squares
  • +
  • Adding error analysis and training set up
  • +
  • Splitting our Data in Training and Test data
  • +
  • Examples
  • +
  • Making your own test-train splitting
  • +
  • The Boston housing data example
  • +
  • Housing data, the code
  • +
  • Reducing the number of degrees of freedom, overarching view
  • +
  • Preprocessing our data
  • +
  • Functionality in Scikit-Learn
  • +
  • More preprocessing
  • +
  • Frequently used scaling functions
  • +
  • Example of own Standard scaling
  • +
  • Min-Max Scaling
  • +
  • Testing the Means Squared Error as function of Complexity
  • +
  • More preprocessing examples, Franke function and regression
  • +
  • Material for lecture Thursday, August 31
  • +
  • Mathematical Interpretation of Ordinary Least Squares
  • +
  • Residual Error
  • +
  • Simple case
  • +
  • The singular value decomposition
  • +
  • Linear Regression Problems
  • +
  • Fixing the singularity
  • +
  • Basic math of the SVD
  • +
  • The SVD, a Fantastic Algorithm
  • +
  • Economy-size SVD
  • +
  • Codes for the SVD
  • +
  • Note about SVD Calculations
  • +
  • Mathematics of the SVD and implications
  • +
  • Example Matrix
  • +
  • Setting up the Matrix to be inverted
  • +
  • Further properties (important for our analyses later)
  • +
  • Meet the Covariance Matrix
  • +
  • Introducing the Covariance and Correlation functions
  • +
  • Covariance and Correlation Matrix
  • +
  • Correlation Function and Design/Feature Matrix
  • +
  • Covariance Matrix Examples
  • +
  • Correlation Matrix
  • +
  • Correlation Matrix with Pandas
  • +
  • Correlation Matrix with Pandas and the Franke function
  • +
  • Rewriting the Covariance and/or Correlation Matrix
  • +
  • Linking with the SVD
  • +
  • What does it mean?
  • +
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • +
  • Ridge and LASSO Regression
  • +
  • Deriving the Ridge Regression Equations
  • +
  • Interpreting the Ridge results
  • +
  • More interpretations
  • +
  • Deriving the Lasso Regression Equations
  • +
  • Exercises for week 35
  • +
  • Exercise 1: Setting up various Python environments
  • +
  • Exercise 2: making your own data and exploring scikit-learn
  • +
  • Exercise 3: Normalizing our data
  • +
  • Exercise 4: Adding Ridge Regression
  • +
  • Exercise 5: Analytical exercises
  • @@ -382,14 +391,122 @@ MathJax.Hub.Config({

     

     

     

    -

    Residual Error

    +

    More preprocessing examples, Franke function and regression

    -

    We have defined the residual error as

    -$$ -\boldsymbol{\epsilon}=\boldsymbol{y}-\tilde{\boldsymbol{y}}=\left[\boldsymbol{I}-\boldsymbol{X}\left(\boldsymbol{X}^T\boldsymbol{X}\right)^{-1}\boldsymbol{X}^T\right]\boldsymbol{y}. -$$ -

    The residual errors are then the projections of \( \boldsymbol{y} \) onto the orthogonal component of the space defined by the column vectors of \( \boldsymbol{X} \).

    + +
    +
    +
    +
    +
    +
    # Common imports
    +import os
    +import numpy as np
    +import pandas as pd
    +import matplotlib.pyplot as plt
    +import sklearn.linear_model as skl
    +from sklearn.metrics import mean_squared_error
    +from sklearn.model_selection import  train_test_split
    +from sklearn.preprocessing import MinMaxScaler, StandardScaler, Normalizer
    +
    +# 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')
    +
    +
    +def FrankeFunction(x,y):
    +	term1 = 0.75*np.exp(-(0.25*(9*x-2)**2) - 0.25*((9*y-2)**2))
    +	term2 = 0.75*np.exp(-((9*x+1)**2)/49.0 - 0.1*(9*y+1))
    +	term3 = 0.5*np.exp(-(9*x-7)**2/4.0 - 0.25*((9*y-3)**2))
    +	term4 = -0.2*np.exp(-(9*x-4)**2 - (9*y-7)**2)
    +	return term1 + term2 + term3 + term4
    +
    +
    +def create_X(x, y, n ):
    +	if len(x.shape) > 1:
    +		x = np.ravel(x)
    +		y = np.ravel(y)
    +
    +	N = len(x)
    +	l = int((n+1)*(n+2)/2)		# Number of elements in beta
    +	X = np.ones((N,l))
    +
    +	for i in range(1,n+1):
    +		q = int((i)*(i+1)/2)
    +		for k in range(i+1):
    +			X[:,q+k] = (x**(i-k))*(y**k)
    +
    +	return X
    +
    +
    +# Making meshgrid of datapoints and compute Franke's function
    +n = 5
    +N = 1000
    +x = np.sort(np.random.uniform(0, 1, N))
    +y = np.sort(np.random.uniform(0, 1, N))
    +z = FrankeFunction(x, y)
    +X = create_X(x, y, n=n)    
    +# split in training and test data
    +X_train, X_test, y_train, y_test = train_test_split(X,z,test_size=0.2)
    +
    +
    +clf = skl.LinearRegression().fit(X_train, y_train)
    +
    +# The mean squared error and R2 score
    +print("MSE before scaling: {:.2f}".format(mean_squared_error(clf.predict(X_test), y_test)))
    +print("R2 score before scaling {:.2f}".format(clf.score(X_test,y_test)))
    +
    +scaler = StandardScaler()
    +scaler.fit(X_train)
    +X_train_scaled = scaler.transform(X_train)
    +X_test_scaled = scaler.transform(X_test)
    +
    +print("Feature min values before scaling:\n {}".format(X_train.min(axis=0)))
    +print("Feature max values before scaling:\n {}".format(X_train.max(axis=0)))
    +
    +print("Feature min values after scaling:\n {}".format(X_train_scaled.min(axis=0)))
    +print("Feature max values after scaling:\n {}".format(X_train_scaled.max(axis=0)))
    +
    +clf = skl.LinearRegression().fit(X_train_scaled, y_train)
    +
    +
    +print("MSE after  scaling: {:.2f}".format(mean_squared_error(clf.predict(X_test_scaled), y_test)))
    +print("R2 score for  scaled data: {:.2f}".format(clf.score(X_test_scaled,y_test)))
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +

    @@ -416,7 +533,7 @@ $$

  • 45
  • 46
  • ...
  • -
  • 68
  • +
  • 71
  • »
  • diff --git a/doc/pub/week35/html/._week35-bs037.html b/doc/pub/week35/html/._week35-bs037.html index aad8047bb..b04ff1808 100644 --- a/doc/pub/week35/html/._week35-bs037.html +++ b/doc/pub/week35/html/._week35-bs037.html @@ -74,10 +74,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'optimizing-our-parameters'), - ('Our model for the nuclear binding energies', + ('Examples relevant for the exercises', 2, None, - 'our-model-for-the-nuclear-binding-energies'), + 'examples-relevant-for-the-exercises'), ('Optimizing our parameters, more details', 2, None, @@ -94,6 +94,8 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'some-useful-matrix-and-vector-expressions'), + ('The Jacobian', 2, None, 'the-jacobian'), + ('Derivatives, example 1', 2, None, 'derivatives-example-1'), ('Meet the Hessian Matrix', 2, None, 'meet-the-hessian-matrix'), ('Interpretations and optimizing our parameters', 2, @@ -148,6 +150,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'more-preprocessing-examples-franke-function-and-regression'), + ('Material for lecture Thursday, August 31', + 2, + None, + 'material-for-lecture-thursday-august-31'), ('Mathematical Interpretation of Ordinary Least Squares', 2, None, @@ -303,74 +309,77 @@ MathJax.Hub.Config({
  • Why Linear Regression (aka Ordinary Least Squares and family), repeat from last week
  • Regression analysis, overarching aims
  • Regression analysis, overarching aims II
  • -
  • Examples
  • +
  • Examples
  • General linear models
  • Rewriting the fitting procedure as a linear algebra problem
  • Rewriting the fitting procedure as a linear algebra problem, more details
  • Generalizing the fitting procedure as a linear algebra problem
  • Generalizing the fitting procedure as a linear algebra problem
  • Optimizing our parameters
  • -
  • Our model for the nuclear binding energies
  • +
  • Examples relevant for the exercises
  • Optimizing our parameters, more details
  • -
  • Interpretations and optimizing our parameters
  • -
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • Some useful matrix and vector expressions
  • -
  • Meet the Hessian Matrix
  • -
  • Interpretations and optimizing our parameters
  • -
  • Own code for Ordinary Least Squares
  • -
  • Adding error analysis and training set up
  • -
  • Splitting our Data in Training and Test data
  • -
  • Examples
  • -
  • Making your own test-train splitting
  • -
  • The Boston housing data example
  • -
  • Housing data, the code
  • -
  • Reducing the number of degrees of freedom, overarching view
  • -
  • Preprocessing our data
  • -
  • Functionality in Scikit-Learn
  • -
  • More preprocessing
  • -
  • Frequently used scaling functions
  • -
  • Example of own Standard scaling
  • -
  • Min-Max Scaling
  • -
  • Testing the Means Squared Error as function of Complexity
  • -
  • More preprocessing examples, Franke function and regression
  • -
  • Mathematical Interpretation of Ordinary Least Squares
  • -
  • Residual Error
  • -
  • Simple case
  • -
  • The singular value decomposition
  • -
  • Linear Regression Problems
  • -
  • Fixing the singularity
  • -
  • Basic math of the SVD
  • -
  • The SVD, a Fantastic Algorithm
  • -
  • Economy-size SVD
  • -
  • Codes for the SVD
  • -
  • Note about SVD Calculations
  • -
  • Mathematics of the SVD and implications
  • -
  • Example Matrix
  • -
  • Setting up the Matrix to be inverted
  • -
  • Further properties (important for our analyses later)
  • -
  • Meet the Covariance Matrix
  • -
  • Introducing the Covariance and Correlation functions
  • -
  • Covariance and Correlation Matrix
  • -
  • Correlation Function and Design/Feature Matrix
  • -
  • Covariance Matrix Examples
  • -
  • Correlation Matrix
  • -
  • Correlation Matrix with Pandas
  • -
  • Correlation Matrix with Pandas and the Franke function
  • -
  • Rewriting the Covariance and/or Correlation Matrix
  • -
  • Linking with the SVD
  • -
  • What does it mean?
  • -
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • -
  • Ridge and LASSO Regression
  • -
  • Deriving the Ridge Regression Equations
  • -
  • Interpreting the Ridge results
  • -
  • More interpretations
  • -
  • Deriving the Lasso Regression Equations
  • -
  • Exercises for week 35
  • -
  • Exercise 1: Setting up various Python environments
  • -
  • Exercise 2: making your own data and exploring scikit-learn
  • -
  • Exercise 3: Normalizing our data
  • -
  • Exercise 4: Adding Ridge Regression
  • -
  • Exercise 5: Analytical exercises
  • +
  • The Jacobian
  • +
  • Derivatives, example 1
  • +
  • Meet the Hessian Matrix
  • +
  • Interpretations and optimizing our parameters
  • +
  • Own code for Ordinary Least Squares
  • +
  • Adding error analysis and training set up
  • +
  • Splitting our Data in Training and Test data
  • +
  • Examples
  • +
  • Making your own test-train splitting
  • +
  • The Boston housing data example
  • +
  • Housing data, the code
  • +
  • Reducing the number of degrees of freedom, overarching view
  • +
  • Preprocessing our data
  • +
  • Functionality in Scikit-Learn
  • +
  • More preprocessing
  • +
  • Frequently used scaling functions
  • +
  • Example of own Standard scaling
  • +
  • Min-Max Scaling
  • +
  • Testing the Means Squared Error as function of Complexity
  • +
  • More preprocessing examples, Franke function and regression
  • +
  • Material for lecture Thursday, August 31
  • +
  • Mathematical Interpretation of Ordinary Least Squares
  • +
  • Residual Error
  • +
  • Simple case
  • +
  • The singular value decomposition
  • +
  • Linear Regression Problems
  • +
  • Fixing the singularity
  • +
  • Basic math of the SVD
  • +
  • The SVD, a Fantastic Algorithm
  • +
  • Economy-size SVD
  • +
  • Codes for the SVD
  • +
  • Note about SVD Calculations
  • +
  • Mathematics of the SVD and implications
  • +
  • Example Matrix
  • +
  • Setting up the Matrix to be inverted
  • +
  • Further properties (important for our analyses later)
  • +
  • Meet the Covariance Matrix
  • +
  • Introducing the Covariance and Correlation functions
  • +
  • Covariance and Correlation Matrix
  • +
  • Correlation Function and Design/Feature Matrix
  • +
  • Covariance Matrix Examples
  • +
  • Correlation Matrix
  • +
  • Correlation Matrix with Pandas
  • +
  • Correlation Matrix with Pandas and the Franke function
  • +
  • Rewriting the Covariance and/or Correlation Matrix
  • +
  • Linking with the SVD
  • +
  • What does it mean?
  • +
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • +
  • Ridge and LASSO Regression
  • +
  • Deriving the Ridge Regression Equations
  • +
  • Interpreting the Ridge results
  • +
  • More interpretations
  • +
  • Deriving the Lasso Regression Equations
  • +
  • Exercises for week 35
  • +
  • Exercise 1: Setting up various Python environments
  • +
  • Exercise 2: making your own data and exploring scikit-learn
  • +
  • Exercise 3: Normalizing our data
  • +
  • Exercise 4: Adding Ridge Regression
  • +
  • Exercise 5: Analytical exercises
  • @@ -382,25 +391,7 @@ MathJax.Hub.Config({

     

     

     

    -

    Simple case

    - -

    If the matrix \( \boldsymbol{X} \) is an orthogonal (or unitary in case of complex values) matrix, we have

    - -$$ -\boldsymbol{X}^T\boldsymbol{X}=\boldsymbol{X}\boldsymbol{X}^T = \boldsymbol{I}. -$$ - -

    In this case the matrix \( \boldsymbol{A} \) becomes

    -$$ -\boldsymbol{A}=\boldsymbol{X}\left(\boldsymbol{X}^T\boldsymbol{X}\right)^{-1}\boldsymbol{X}^T)=\boldsymbol{I}, -$$ - -

    and we have the obvious case

    -$$ -\boldsymbol{\epsilon}=\boldsymbol{y}-\tilde{\boldsymbol{y}}=0. -$$ - -

    This serves also as a useful test of our codes.

    +

    Material for lecture Thursday, August 31

    @@ -427,7 +418,7 @@ $$

  • 46
  • 47
  • ...
  • -
  • 68
  • +
  • 71
  • »
  • diff --git a/doc/pub/week35/html/._week35-bs038.html b/doc/pub/week35/html/._week35-bs038.html index c75ba5ff3..8a9252f19 100644 --- a/doc/pub/week35/html/._week35-bs038.html +++ b/doc/pub/week35/html/._week35-bs038.html @@ -74,10 +74,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'optimizing-our-parameters'), - ('Our model for the nuclear binding energies', + ('Examples relevant for the exercises', 2, None, - 'our-model-for-the-nuclear-binding-energies'), + 'examples-relevant-for-the-exercises'), ('Optimizing our parameters, more details', 2, None, @@ -94,6 +94,8 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'some-useful-matrix-and-vector-expressions'), + ('The Jacobian', 2, None, 'the-jacobian'), + ('Derivatives, example 1', 2, None, 'derivatives-example-1'), ('Meet the Hessian Matrix', 2, None, 'meet-the-hessian-matrix'), ('Interpretations and optimizing our parameters', 2, @@ -148,6 +150,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'more-preprocessing-examples-franke-function-and-regression'), + ('Material for lecture Thursday, August 31', + 2, + None, + 'material-for-lecture-thursday-august-31'), ('Mathematical Interpretation of Ordinary Least Squares', 2, None, @@ -303,74 +309,77 @@ MathJax.Hub.Config({
  • Why Linear Regression (aka Ordinary Least Squares and family), repeat from last week
  • Regression analysis, overarching aims
  • Regression analysis, overarching aims II
  • -
  • Examples
  • +
  • Examples
  • General linear models
  • Rewriting the fitting procedure as a linear algebra problem
  • Rewriting the fitting procedure as a linear algebra problem, more details
  • Generalizing the fitting procedure as a linear algebra problem
  • Generalizing the fitting procedure as a linear algebra problem
  • Optimizing our parameters
  • -
  • Our model for the nuclear binding energies
  • +
  • Examples relevant for the exercises
  • Optimizing our parameters, more details
  • -
  • Interpretations and optimizing our parameters
  • -
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • Some useful matrix and vector expressions
  • -
  • Meet the Hessian Matrix
  • -
  • Interpretations and optimizing our parameters
  • -
  • Own code for Ordinary Least Squares
  • -
  • Adding error analysis and training set up
  • -
  • Splitting our Data in Training and Test data
  • -
  • Examples
  • -
  • Making your own test-train splitting
  • -
  • The Boston housing data example
  • -
  • Housing data, the code
  • -
  • Reducing the number of degrees of freedom, overarching view
  • -
  • Preprocessing our data
  • -
  • Functionality in Scikit-Learn
  • -
  • More preprocessing
  • -
  • Frequently used scaling functions
  • -
  • Example of own Standard scaling
  • -
  • Min-Max Scaling
  • -
  • Testing the Means Squared Error as function of Complexity
  • -
  • More preprocessing examples, Franke function and regression
  • -
  • Mathematical Interpretation of Ordinary Least Squares
  • -
  • Residual Error
  • -
  • Simple case
  • -
  • The singular value decomposition
  • -
  • Linear Regression Problems
  • -
  • Fixing the singularity
  • -
  • Basic math of the SVD
  • -
  • The SVD, a Fantastic Algorithm
  • -
  • Economy-size SVD
  • -
  • Codes for the SVD
  • -
  • Note about SVD Calculations
  • -
  • Mathematics of the SVD and implications
  • -
  • Example Matrix
  • -
  • Setting up the Matrix to be inverted
  • -
  • Further properties (important for our analyses later)
  • -
  • Meet the Covariance Matrix
  • -
  • Introducing the Covariance and Correlation functions
  • -
  • Covariance and Correlation Matrix
  • -
  • Correlation Function and Design/Feature Matrix
  • -
  • Covariance Matrix Examples
  • -
  • Correlation Matrix
  • -
  • Correlation Matrix with Pandas
  • -
  • Correlation Matrix with Pandas and the Franke function
  • -
  • Rewriting the Covariance and/or Correlation Matrix
  • -
  • Linking with the SVD
  • -
  • What does it mean?
  • -
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • -
  • Ridge and LASSO Regression
  • -
  • Deriving the Ridge Regression Equations
  • -
  • Interpreting the Ridge results
  • -
  • More interpretations
  • -
  • Deriving the Lasso Regression Equations
  • -
  • Exercises for week 35
  • -
  • Exercise 1: Setting up various Python environments
  • -
  • Exercise 2: making your own data and exploring scikit-learn
  • -
  • Exercise 3: Normalizing our data
  • -
  • Exercise 4: Adding Ridge Regression
  • -
  • Exercise 5: Analytical exercises
  • +
  • The Jacobian
  • +
  • Derivatives, example 1
  • +
  • Meet the Hessian Matrix
  • +
  • Interpretations and optimizing our parameters
  • +
  • Own code for Ordinary Least Squares
  • +
  • Adding error analysis and training set up
  • +
  • Splitting our Data in Training and Test data
  • +
  • Examples
  • +
  • Making your own test-train splitting
  • +
  • The Boston housing data example
  • +
  • Housing data, the code
  • +
  • Reducing the number of degrees of freedom, overarching view
  • +
  • Preprocessing our data
  • +
  • Functionality in Scikit-Learn
  • +
  • More preprocessing
  • +
  • Frequently used scaling functions
  • +
  • Example of own Standard scaling
  • +
  • Min-Max Scaling
  • +
  • Testing the Means Squared Error as function of Complexity
  • +
  • More preprocessing examples, Franke function and regression
  • +
  • Material for lecture Thursday, August 31
  • +
  • Mathematical Interpretation of Ordinary Least Squares
  • +
  • Residual Error
  • +
  • Simple case
  • +
  • The singular value decomposition
  • +
  • Linear Regression Problems
  • +
  • Fixing the singularity
  • +
  • Basic math of the SVD
  • +
  • The SVD, a Fantastic Algorithm
  • +
  • Economy-size SVD
  • +
  • Codes for the SVD
  • +
  • Note about SVD Calculations
  • +
  • Mathematics of the SVD and implications
  • +
  • Example Matrix
  • +
  • Setting up the Matrix to be inverted
  • +
  • Further properties (important for our analyses later)
  • +
  • Meet the Covariance Matrix
  • +
  • Introducing the Covariance and Correlation functions
  • +
  • Covariance and Correlation Matrix
  • +
  • Correlation Function and Design/Feature Matrix
  • +
  • Covariance Matrix Examples
  • +
  • Correlation Matrix
  • +
  • Correlation Matrix with Pandas
  • +
  • Correlation Matrix with Pandas and the Franke function
  • +
  • Rewriting the Covariance and/or Correlation Matrix
  • +
  • Linking with the SVD
  • +
  • What does it mean?
  • +
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • +
  • Ridge and LASSO Regression
  • +
  • Deriving the Ridge Regression Equations
  • +
  • Interpreting the Ridge results
  • +
  • More interpretations
  • +
  • Deriving the Lasso Regression Equations
  • +
  • Exercises for week 35
  • +
  • Exercise 1: Setting up various Python environments
  • +
  • Exercise 2: making your own data and exploring scikit-learn
  • +
  • Exercise 3: Normalizing our data
  • +
  • Exercise 4: Adding Ridge Regression
  • +
  • Exercise 5: Analytical exercises
  • @@ -382,50 +391,38 @@ MathJax.Hub.Config({

     

     

     

    -

    The singular value decomposition

    +

    Mathematical Interpretation of Ordinary Least Squares

    -
    -
    - +

    What is presented here is a mathematical analysis of various regression algorithms (ordinary least squares, Ridge and Lasso Regression). The analysis is based on an important algorithm in linear algebra, the so-called Singular Value Decomposition (SVD).

    -

    The examples we have looked at so far are cases where we normally can -invert the matrix \( \boldsymbol{X}^T\boldsymbol{X} \). Using a polynomial expansion where we fit of various functions leads to -row vectors of the design matrix which are essentially orthogonal due -to the polynomial character of our model. Obtaining the inverse of the -design matrix is then often done via a so-called LU, QR or Cholesky -decomposition. +

    We have shown that in ordinary least squares the optimal parameters \( \beta \) are given by

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

    The hat over \( \boldsymbol{\beta} \) means we have the optimal parameters after minimization of the cost function.

    + +

    This means that our best model is defined as

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

    We now define a matrix

    +$$ +\boldsymbol{A}=\boldsymbol{X}\left(\boldsymbol{X}^T\boldsymbol{X}\right)^{-1}\boldsymbol{X}^T. +$$ + +

    We can rewrite

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

    The matrix \( \boldsymbol{A} \) has the important property that \( \boldsymbol{A}^2=\boldsymbol{A} \). This is the definition of a projection matrix. +We can then interpret our optimal model \( \tilde{\boldsymbol{y}} \) as being represented by an orthogonal projection of \( \boldsymbol{y} \) onto a space defined by the column vectors of \( \boldsymbol{X} \). In our case here the matrix \( \boldsymbol{A} \) is a square matrix. If it is a general rectangular matrix we have an oblique projection matrix.

    -

    As we will also see in the first project, -this may -however not the be case in general and a standard matrix inversion -algorithm based on say LU, QR or Cholesky decomposition may lead to singularities. We will see examples of this below. -

    - -

    There is however a way to circumvent this problem and also -gain some insights about the ordinary least squares approach, and -later shrinkage methods like Ridge and Lasso regressions. -

    - -

    This is given by the Singular Value Decomposition (SVD) algorithm, -perhaps the most powerful linear algebra algorithm. The SVD provides -a numerically stable matrix decomposition that is used in a large -swath oc applications and the decomposition is always stable -numerically. -

    - -

    In machine learning it plays a central role in dealing with for -example design matrices that may be near singular or singular. -Furthermore, as we will see here, the singular values can be related -to the covariance matrix (and thereby the correlation matrix) and in -turn the variance of a given quantity. It plays also an important role -in the principal component analysis where high-dimensional data can be -reduced to the statistically relevant features. -

    -
    -
    - -

      @@ -451,7 +448,7 @@ reduced to the statistically relevant features.
    • 47
    • 48
    • ...
    • -
    • 68
    • +
    • 71
    • »
    diff --git a/doc/pub/week35/html/._week35-bs039.html b/doc/pub/week35/html/._week35-bs039.html index caa03cae7..f0055b02b 100644 --- a/doc/pub/week35/html/._week35-bs039.html +++ b/doc/pub/week35/html/._week35-bs039.html @@ -74,10 +74,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'optimizing-our-parameters'), - ('Our model for the nuclear binding energies', + ('Examples relevant for the exercises', 2, None, - 'our-model-for-the-nuclear-binding-energies'), + 'examples-relevant-for-the-exercises'), ('Optimizing our parameters, more details', 2, None, @@ -94,6 +94,8 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'some-useful-matrix-and-vector-expressions'), + ('The Jacobian', 2, None, 'the-jacobian'), + ('Derivatives, example 1', 2, None, 'derivatives-example-1'), ('Meet the Hessian Matrix', 2, None, 'meet-the-hessian-matrix'), ('Interpretations and optimizing our parameters', 2, @@ -148,6 +150,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'more-preprocessing-examples-franke-function-and-regression'), + ('Material for lecture Thursday, August 31', + 2, + None, + 'material-for-lecture-thursday-august-31'), ('Mathematical Interpretation of Ordinary Least Squares', 2, None, @@ -303,74 +309,77 @@ MathJax.Hub.Config({
  • Why Linear Regression (aka Ordinary Least Squares and family), repeat from last week
  • Regression analysis, overarching aims
  • Regression analysis, overarching aims II
  • -
  • Examples
  • +
  • Examples
  • General linear models
  • Rewriting the fitting procedure as a linear algebra problem
  • Rewriting the fitting procedure as a linear algebra problem, more details
  • Generalizing the fitting procedure as a linear algebra problem
  • Generalizing the fitting procedure as a linear algebra problem
  • Optimizing our parameters
  • -
  • Our model for the nuclear binding energies
  • +
  • Examples relevant for the exercises
  • Optimizing our parameters, more details
  • -
  • Interpretations and optimizing our parameters
  • -
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • Some useful matrix and vector expressions
  • -
  • Meet the Hessian Matrix
  • -
  • Interpretations and optimizing our parameters
  • -
  • Own code for Ordinary Least Squares
  • -
  • Adding error analysis and training set up
  • -
  • Splitting our Data in Training and Test data
  • -
  • Examples
  • -
  • Making your own test-train splitting
  • -
  • The Boston housing data example
  • -
  • Housing data, the code
  • -
  • Reducing the number of degrees of freedom, overarching view
  • -
  • Preprocessing our data
  • -
  • Functionality in Scikit-Learn
  • -
  • More preprocessing
  • -
  • Frequently used scaling functions
  • -
  • Example of own Standard scaling
  • -
  • Min-Max Scaling
  • -
  • Testing the Means Squared Error as function of Complexity
  • -
  • More preprocessing examples, Franke function and regression
  • -
  • Mathematical Interpretation of Ordinary Least Squares
  • -
  • Residual Error
  • -
  • Simple case
  • -
  • The singular value decomposition
  • -
  • Linear Regression Problems
  • -
  • Fixing the singularity
  • -
  • Basic math of the SVD
  • -
  • The SVD, a Fantastic Algorithm
  • -
  • Economy-size SVD
  • -
  • Codes for the SVD
  • -
  • Note about SVD Calculations
  • -
  • Mathematics of the SVD and implications
  • -
  • Example Matrix
  • -
  • Setting up the Matrix to be inverted
  • -
  • Further properties (important for our analyses later)
  • -
  • Meet the Covariance Matrix
  • -
  • Introducing the Covariance and Correlation functions
  • -
  • Covariance and Correlation Matrix
  • -
  • Correlation Function and Design/Feature Matrix
  • -
  • Covariance Matrix Examples
  • -
  • Correlation Matrix
  • -
  • Correlation Matrix with Pandas
  • -
  • Correlation Matrix with Pandas and the Franke function
  • -
  • Rewriting the Covariance and/or Correlation Matrix
  • -
  • Linking with the SVD
  • -
  • What does it mean?
  • -
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • -
  • Ridge and LASSO Regression
  • -
  • Deriving the Ridge Regression Equations
  • -
  • Interpreting the Ridge results
  • -
  • More interpretations
  • -
  • Deriving the Lasso Regression Equations
  • -
  • Exercises for week 35
  • -
  • Exercise 1: Setting up various Python environments
  • -
  • Exercise 2: making your own data and exploring scikit-learn
  • -
  • Exercise 3: Normalizing our data
  • -
  • Exercise 4: Adding Ridge Regression
  • -
  • Exercise 5: Analytical exercises
  • +
  • The Jacobian
  • +
  • Derivatives, example 1
  • +
  • Meet the Hessian Matrix
  • +
  • Interpretations and optimizing our parameters
  • +
  • Own code for Ordinary Least Squares
  • +
  • Adding error analysis and training set up
  • +
  • Splitting our Data in Training and Test data
  • +
  • Examples
  • +
  • Making your own test-train splitting
  • +
  • The Boston housing data example
  • +
  • Housing data, the code
  • +
  • Reducing the number of degrees of freedom, overarching view
  • +
  • Preprocessing our data
  • +
  • Functionality in Scikit-Learn
  • +
  • More preprocessing
  • +
  • Frequently used scaling functions
  • +
  • Example of own Standard scaling
  • +
  • Min-Max Scaling
  • +
  • Testing the Means Squared Error as function of Complexity
  • +
  • More preprocessing examples, Franke function and regression
  • +
  • Material for lecture Thursday, August 31
  • +
  • Mathematical Interpretation of Ordinary Least Squares
  • +
  • Residual Error
  • +
  • Simple case
  • +
  • The singular value decomposition
  • +
  • Linear Regression Problems
  • +
  • Fixing the singularity
  • +
  • Basic math of the SVD
  • +
  • The SVD, a Fantastic Algorithm
  • +
  • Economy-size SVD
  • +
  • Codes for the SVD
  • +
  • Note about SVD Calculations
  • +
  • Mathematics of the SVD and implications
  • +
  • Example Matrix
  • +
  • Setting up the Matrix to be inverted
  • +
  • Further properties (important for our analyses later)
  • +
  • Meet the Covariance Matrix
  • +
  • Introducing the Covariance and Correlation functions
  • +
  • Covariance and Correlation Matrix
  • +
  • Correlation Function and Design/Feature Matrix
  • +
  • Covariance Matrix Examples
  • +
  • Correlation Matrix
  • +
  • Correlation Matrix with Pandas
  • +
  • Correlation Matrix with Pandas and the Franke function
  • +
  • Rewriting the Covariance and/or Correlation Matrix
  • +
  • Linking with the SVD
  • +
  • What does it mean?
  • +
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • +
  • Ridge and LASSO Regression
  • +
  • Deriving the Ridge Regression Equations
  • +
  • Interpreting the Ridge results
  • +
  • More interpretations
  • +
  • Deriving the Lasso Regression Equations
  • +
  • Exercises for week 35
  • +
  • Exercise 1: Setting up various Python environments
  • +
  • Exercise 2: making your own data and exploring scikit-learn
  • +
  • Exercise 3: Normalizing our data
  • +
  • Exercise 4: Adding Ridge Regression
  • +
  • Exercise 5: Analytical exercises
  • @@ -382,54 +391,14 @@ MathJax.Hub.Config({

     

     

     

    -

    Linear Regression Problems

    +

    Residual Error

    -

    One of the typical problems we encounter with linear regression, in particular -when the matrix \( \boldsymbol{X} \) (our so-called design matrix) is high-dimensional, -are problems with near singular or singular matrices. The column vectors of \( \boldsymbol{X} \) -may be linearly dependent, normally referred to as super-collinearity. -This means that the matrix may be rank deficient and it is basically impossible to -to model the data using linear regression. As an example, consider the matrix -

    +

    We have defined the residual error as

    $$ -\begin{align*} -\mathbf{X} & = \left[ -\begin{array}{rrr} -1 & -1 & 2 -\\ -1 & 0 & 1 -\\ -1 & 2 & -1 -\\ -1 & 1 & 0 -\end{array} \right] -\end{align*} +\boldsymbol{\epsilon}=\boldsymbol{y}-\tilde{\boldsymbol{y}}=\left[\boldsymbol{I}-\boldsymbol{X}\left(\boldsymbol{X}^T\boldsymbol{X}\right)^{-1}\boldsymbol{X}^T\right]\boldsymbol{y}. $$ -

    The columns of \( \boldsymbol{X} \) are linearly dependent. We see this easily since the -the first column is the row-wise sum of the other two columns. The rank (more correct, -the column rank) of a matrix is the dimension of the space spanned by the -column vectors. Hence, the rank of \( \mathbf{X} \) is equal to the number -of linearly independent columns. In this particular case the matrix has rank 2. -

    - -

    Super-collinearity of an \( (n \times p) \)-dimensional design matrix \( \mathbf{X} \) implies -that the inverse of the matrix \( \boldsymbol{X}^T\boldsymbol{X} \) (the matrix we need to invert to solve the linear regression equations) is non-invertible. If we have a square matrix that does not have an inverse, we say this matrix singular. The example here demonstrates this -

    -$$ -\begin{align*} -\boldsymbol{X} & = \left[ -\begin{array}{rr} -1 & -1 -\\ -1 & -1 -\end{array} \right]. -\end{align*} -$$ - -

    We see easily that \( \mbox{det}(\boldsymbol{X}) = x_{11} x_{22} - x_{12} x_{21} = 1 \times (-1) - 1 \times (-1) = 0 \). Hence, \( \mathbf{X} \) is singular and its inverse is undefined. -This is equivalent to saying that the matrix \( \boldsymbol{X} \) has at least an eigenvalue which is zero. -

    +

    The residual errors are then the projections of \( \boldsymbol{y} \) onto the orthogonal component of the space defined by the column vectors of \( \boldsymbol{X} \).

    @@ -456,7 +425,7 @@ This is equivalent to saying that the matrix \( \boldsymbol{X} \) has at least a

  • 48
  • 49
  • ...
  • -
  • 68
  • +
  • 71
  • »
  • diff --git a/doc/pub/week35/html/._week35-bs040.html b/doc/pub/week35/html/._week35-bs040.html index 9401a36e4..bb1150395 100644 --- a/doc/pub/week35/html/._week35-bs040.html +++ b/doc/pub/week35/html/._week35-bs040.html @@ -74,10 +74,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'optimizing-our-parameters'), - ('Our model for the nuclear binding energies', + ('Examples relevant for the exercises', 2, None, - 'our-model-for-the-nuclear-binding-energies'), + 'examples-relevant-for-the-exercises'), ('Optimizing our parameters, more details', 2, None, @@ -94,6 +94,8 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'some-useful-matrix-and-vector-expressions'), + ('The Jacobian', 2, None, 'the-jacobian'), + ('Derivatives, example 1', 2, None, 'derivatives-example-1'), ('Meet the Hessian Matrix', 2, None, 'meet-the-hessian-matrix'), ('Interpretations and optimizing our parameters', 2, @@ -148,6 +150,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'more-preprocessing-examples-franke-function-and-regression'), + ('Material for lecture Thursday, August 31', + 2, + None, + 'material-for-lecture-thursday-august-31'), ('Mathematical Interpretation of Ordinary Least Squares', 2, None, @@ -303,74 +309,77 @@ MathJax.Hub.Config({
  • Why Linear Regression (aka Ordinary Least Squares and family), repeat from last week
  • Regression analysis, overarching aims
  • Regression analysis, overarching aims II
  • -
  • Examples
  • +
  • Examples
  • General linear models
  • Rewriting the fitting procedure as a linear algebra problem
  • Rewriting the fitting procedure as a linear algebra problem, more details
  • Generalizing the fitting procedure as a linear algebra problem
  • Generalizing the fitting procedure as a linear algebra problem
  • Optimizing our parameters
  • -
  • Our model for the nuclear binding energies
  • +
  • Examples relevant for the exercises
  • Optimizing our parameters, more details
  • -
  • Interpretations and optimizing our parameters
  • -
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • Some useful matrix and vector expressions
  • -
  • Meet the Hessian Matrix
  • -
  • Interpretations and optimizing our parameters
  • -
  • Own code for Ordinary Least Squares
  • -
  • Adding error analysis and training set up
  • -
  • Splitting our Data in Training and Test data
  • -
  • Examples
  • -
  • Making your own test-train splitting
  • -
  • The Boston housing data example
  • -
  • Housing data, the code
  • -
  • Reducing the number of degrees of freedom, overarching view
  • -
  • Preprocessing our data
  • -
  • Functionality in Scikit-Learn
  • -
  • More preprocessing
  • -
  • Frequently used scaling functions
  • -
  • Example of own Standard scaling
  • -
  • Min-Max Scaling
  • -
  • Testing the Means Squared Error as function of Complexity
  • -
  • More preprocessing examples, Franke function and regression
  • -
  • Mathematical Interpretation of Ordinary Least Squares
  • -
  • Residual Error
  • -
  • Simple case
  • -
  • The singular value decomposition
  • -
  • Linear Regression Problems
  • -
  • Fixing the singularity
  • -
  • Basic math of the SVD
  • -
  • The SVD, a Fantastic Algorithm
  • -
  • Economy-size SVD
  • -
  • Codes for the SVD
  • -
  • Note about SVD Calculations
  • -
  • Mathematics of the SVD and implications
  • -
  • Example Matrix
  • -
  • Setting up the Matrix to be inverted
  • -
  • Further properties (important for our analyses later)
  • -
  • Meet the Covariance Matrix
  • -
  • Introducing the Covariance and Correlation functions
  • -
  • Covariance and Correlation Matrix
  • -
  • Correlation Function and Design/Feature Matrix
  • -
  • Covariance Matrix Examples
  • -
  • Correlation Matrix
  • -
  • Correlation Matrix with Pandas
  • -
  • Correlation Matrix with Pandas and the Franke function
  • -
  • Rewriting the Covariance and/or Correlation Matrix
  • -
  • Linking with the SVD
  • -
  • What does it mean?
  • -
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • -
  • Ridge and LASSO Regression
  • -
  • Deriving the Ridge Regression Equations
  • -
  • Interpreting the Ridge results
  • -
  • More interpretations
  • -
  • Deriving the Lasso Regression Equations
  • -
  • Exercises for week 35
  • -
  • Exercise 1: Setting up various Python environments
  • -
  • Exercise 2: making your own data and exploring scikit-learn
  • -
  • Exercise 3: Normalizing our data
  • -
  • Exercise 4: Adding Ridge Regression
  • -
  • Exercise 5: Analytical exercises
  • +
  • The Jacobian
  • +
  • Derivatives, example 1
  • +
  • Meet the Hessian Matrix
  • +
  • Interpretations and optimizing our parameters
  • +
  • Own code for Ordinary Least Squares
  • +
  • Adding error analysis and training set up
  • +
  • Splitting our Data in Training and Test data
  • +
  • Examples
  • +
  • Making your own test-train splitting
  • +
  • The Boston housing data example
  • +
  • Housing data, the code
  • +
  • Reducing the number of degrees of freedom, overarching view
  • +
  • Preprocessing our data
  • +
  • Functionality in Scikit-Learn
  • +
  • More preprocessing
  • +
  • Frequently used scaling functions
  • +
  • Example of own Standard scaling
  • +
  • Min-Max Scaling
  • +
  • Testing the Means Squared Error as function of Complexity
  • +
  • More preprocessing examples, Franke function and regression
  • +
  • Material for lecture Thursday, August 31
  • +
  • Mathematical Interpretation of Ordinary Least Squares
  • +
  • Residual Error
  • +
  • Simple case
  • +
  • The singular value decomposition
  • +
  • Linear Regression Problems
  • +
  • Fixing the singularity
  • +
  • Basic math of the SVD
  • +
  • The SVD, a Fantastic Algorithm
  • +
  • Economy-size SVD
  • +
  • Codes for the SVD
  • +
  • Note about SVD Calculations
  • +
  • Mathematics of the SVD and implications
  • +
  • Example Matrix
  • +
  • Setting up the Matrix to be inverted
  • +
  • Further properties (important for our analyses later)
  • +
  • Meet the Covariance Matrix
  • +
  • Introducing the Covariance and Correlation functions
  • +
  • Covariance and Correlation Matrix
  • +
  • Correlation Function and Design/Feature Matrix
  • +
  • Covariance Matrix Examples
  • +
  • Correlation Matrix
  • +
  • Correlation Matrix with Pandas
  • +
  • Correlation Matrix with Pandas and the Franke function
  • +
  • Rewriting the Covariance and/or Correlation Matrix
  • +
  • Linking with the SVD
  • +
  • What does it mean?
  • +
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • +
  • Ridge and LASSO Regression
  • +
  • Deriving the Ridge Regression Equations
  • +
  • Interpreting the Ridge results
  • +
  • More interpretations
  • +
  • Deriving the Lasso Regression Equations
  • +
  • Exercises for week 35
  • +
  • Exercise 1: Setting up various Python environments
  • +
  • Exercise 2: making your own data and exploring scikit-learn
  • +
  • Exercise 3: Normalizing our data
  • +
  • Exercise 4: Adding Ridge Regression
  • +
  • Exercise 5: Analytical exercises
  • @@ -382,29 +391,25 @@ MathJax.Hub.Config({

     

     

     

    -

    Fixing the singularity

    +

    Simple case

    + +

    If the matrix \( \boldsymbol{X} \) is an orthogonal (or unitary in case of complex values) matrix, we have

    -

    If our design matrix \( \boldsymbol{X} \) which enters the linear regression problem

    $$ -\begin{align} -\boldsymbol{\beta} & = (\boldsymbol{X}^{T} \boldsymbol{X})^{-1} \boldsymbol{X}^{T} \boldsymbol{y}, -\tag{1} -\end{align} +\boldsymbol{X}^T\boldsymbol{X}=\boldsymbol{X}\boldsymbol{X}^T = \boldsymbol{I}. $$ -

    has linearly dependent column vectors, we will not be able to compute the inverse -of \( \boldsymbol{X}^T\boldsymbol{X} \) and we cannot find the parameters (estimators) \( \beta_i \). -The estimators are only well-defined if \( (\boldsymbol{X}^{T}\boldsymbol{X})^{-1} \) exits. -This is more likely to happen when the matrix \( \boldsymbol{X} \) is high-dimensional. In this case it is likely to encounter a situation where -the regression parameters \( \beta_i \) cannot be estimated. -

    - -

    A cheap ad hoc approach is simply to add a small diagonal component to the matrix to invert, that is we change

    +

    In this case the matrix \( \boldsymbol{A} \) becomes

    $$ -\boldsymbol{X}^{T} \boldsymbol{X} \rightarrow \boldsymbol{X}^{T} \boldsymbol{X}+\lambda \boldsymbol{I}, +\boldsymbol{A}=\boldsymbol{X}\left(\boldsymbol{X}^T\boldsymbol{X}\right)^{-1}\boldsymbol{X}^T)=\boldsymbol{I}, $$ -

    where \( \boldsymbol{I} \) is the identity matrix. When we discuss Ridge regression this is actually what we end up evaluating. The parameter \( \lambda \) is called a hyperparameter. More about this later.

    +

    and we have the obvious case

    +$$ +\boldsymbol{\epsilon}=\boldsymbol{y}-\tilde{\boldsymbol{y}}=0. +$$ + +

    This serves also as a useful test of our codes.

    @@ -431,7 +436,7 @@ $$

  • 49
  • 50
  • ...
  • -
  • 68
  • +
  • 71
  • »
  • diff --git a/doc/pub/week35/html/._week35-bs041.html b/doc/pub/week35/html/._week35-bs041.html index 58e19c4fb..d564e8d1c 100644 --- a/doc/pub/week35/html/._week35-bs041.html +++ b/doc/pub/week35/html/._week35-bs041.html @@ -74,10 +74,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'optimizing-our-parameters'), - ('Our model for the nuclear binding energies', + ('Examples relevant for the exercises', 2, None, - 'our-model-for-the-nuclear-binding-energies'), + 'examples-relevant-for-the-exercises'), ('Optimizing our parameters, more details', 2, None, @@ -94,6 +94,8 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'some-useful-matrix-and-vector-expressions'), + ('The Jacobian', 2, None, 'the-jacobian'), + ('Derivatives, example 1', 2, None, 'derivatives-example-1'), ('Meet the Hessian Matrix', 2, None, 'meet-the-hessian-matrix'), ('Interpretations and optimizing our parameters', 2, @@ -148,6 +150,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'more-preprocessing-examples-franke-function-and-regression'), + ('Material for lecture Thursday, August 31', + 2, + None, + 'material-for-lecture-thursday-august-31'), ('Mathematical Interpretation of Ordinary Least Squares', 2, None, @@ -303,74 +309,77 @@ MathJax.Hub.Config({
  • Why Linear Regression (aka Ordinary Least Squares and family), repeat from last week
  • Regression analysis, overarching aims
  • Regression analysis, overarching aims II
  • -
  • Examples
  • +
  • Examples
  • General linear models
  • Rewriting the fitting procedure as a linear algebra problem
  • Rewriting the fitting procedure as a linear algebra problem, more details
  • Generalizing the fitting procedure as a linear algebra problem
  • Generalizing the fitting procedure as a linear algebra problem
  • Optimizing our parameters
  • -
  • Our model for the nuclear binding energies
  • +
  • Examples relevant for the exercises
  • Optimizing our parameters, more details
  • -
  • Interpretations and optimizing our parameters
  • -
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • Some useful matrix and vector expressions
  • -
  • Meet the Hessian Matrix
  • -
  • Interpretations and optimizing our parameters
  • -
  • Own code for Ordinary Least Squares
  • -
  • Adding error analysis and training set up
  • -
  • Splitting our Data in Training and Test data
  • -
  • Examples
  • -
  • Making your own test-train splitting
  • -
  • The Boston housing data example
  • -
  • Housing data, the code
  • -
  • Reducing the number of degrees of freedom, overarching view
  • -
  • Preprocessing our data
  • -
  • Functionality in Scikit-Learn
  • -
  • More preprocessing
  • -
  • Frequently used scaling functions
  • -
  • Example of own Standard scaling
  • -
  • Min-Max Scaling
  • -
  • Testing the Means Squared Error as function of Complexity
  • -
  • More preprocessing examples, Franke function and regression
  • -
  • Mathematical Interpretation of Ordinary Least Squares
  • -
  • Residual Error
  • -
  • Simple case
  • -
  • The singular value decomposition
  • -
  • Linear Regression Problems
  • -
  • Fixing the singularity
  • -
  • Basic math of the SVD
  • -
  • The SVD, a Fantastic Algorithm
  • -
  • Economy-size SVD
  • -
  • Codes for the SVD
  • -
  • Note about SVD Calculations
  • -
  • Mathematics of the SVD and implications
  • -
  • Example Matrix
  • -
  • Setting up the Matrix to be inverted
  • -
  • Further properties (important for our analyses later)
  • -
  • Meet the Covariance Matrix
  • -
  • Introducing the Covariance and Correlation functions
  • -
  • Covariance and Correlation Matrix
  • -
  • Correlation Function and Design/Feature Matrix
  • -
  • Covariance Matrix Examples
  • -
  • Correlation Matrix
  • -
  • Correlation Matrix with Pandas
  • -
  • Correlation Matrix with Pandas and the Franke function
  • -
  • Rewriting the Covariance and/or Correlation Matrix
  • -
  • Linking with the SVD
  • -
  • What does it mean?
  • -
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • -
  • Ridge and LASSO Regression
  • -
  • Deriving the Ridge Regression Equations
  • -
  • Interpreting the Ridge results
  • -
  • More interpretations
  • -
  • Deriving the Lasso Regression Equations
  • -
  • Exercises for week 35
  • -
  • Exercise 1: Setting up various Python environments
  • -
  • Exercise 2: making your own data and exploring scikit-learn
  • -
  • Exercise 3: Normalizing our data
  • -
  • Exercise 4: Adding Ridge Regression
  • -
  • Exercise 5: Analytical exercises
  • +
  • The Jacobian
  • +
  • Derivatives, example 1
  • +
  • Meet the Hessian Matrix
  • +
  • Interpretations and optimizing our parameters
  • +
  • Own code for Ordinary Least Squares
  • +
  • Adding error analysis and training set up
  • +
  • Splitting our Data in Training and Test data
  • +
  • Examples
  • +
  • Making your own test-train splitting
  • +
  • The Boston housing data example
  • +
  • Housing data, the code
  • +
  • Reducing the number of degrees of freedom, overarching view
  • +
  • Preprocessing our data
  • +
  • Functionality in Scikit-Learn
  • +
  • More preprocessing
  • +
  • Frequently used scaling functions
  • +
  • Example of own Standard scaling
  • +
  • Min-Max Scaling
  • +
  • Testing the Means Squared Error as function of Complexity
  • +
  • More preprocessing examples, Franke function and regression
  • +
  • Material for lecture Thursday, August 31
  • +
  • Mathematical Interpretation of Ordinary Least Squares
  • +
  • Residual Error
  • +
  • Simple case
  • +
  • The singular value decomposition
  • +
  • Linear Regression Problems
  • +
  • Fixing the singularity
  • +
  • Basic math of the SVD
  • +
  • The SVD, a Fantastic Algorithm
  • +
  • Economy-size SVD
  • +
  • Codes for the SVD
  • +
  • Note about SVD Calculations
  • +
  • Mathematics of the SVD and implications
  • +
  • Example Matrix
  • +
  • Setting up the Matrix to be inverted
  • +
  • Further properties (important for our analyses later)
  • +
  • Meet the Covariance Matrix
  • +
  • Introducing the Covariance and Correlation functions
  • +
  • Covariance and Correlation Matrix
  • +
  • Correlation Function and Design/Feature Matrix
  • +
  • Covariance Matrix Examples
  • +
  • Correlation Matrix
  • +
  • Correlation Matrix with Pandas
  • +
  • Correlation Matrix with Pandas and the Franke function
  • +
  • Rewriting the Covariance and/or Correlation Matrix
  • +
  • Linking with the SVD
  • +
  • What does it mean?
  • +
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • +
  • Ridge and LASSO Regression
  • +
  • Deriving the Ridge Regression Equations
  • +
  • Interpreting the Ridge results
  • +
  • More interpretations
  • +
  • Deriving the Lasso Regression Equations
  • +
  • Exercises for week 35
  • +
  • Exercise 1: Setting up various Python environments
  • +
  • Exercise 2: making your own data and exploring scikit-learn
  • +
  • Exercise 3: Normalizing our data
  • +
  • Exercise 4: Adding Ridge Regression
  • +
  • Exercise 5: Analytical exercises
  • @@ -382,42 +391,50 @@ MathJax.Hub.Config({

     

     

     

    -

    Basic math of the SVD

    +

    The singular value decomposition

    -

    From standard linear algebra we know that a square matrix \( \boldsymbol{X} \) can be diagonalized if and only it is -a so-called normal matrix, that is if \( \boldsymbol{X}\in {\mathbb{R}}^{n\times n} \) -we have \( \boldsymbol{X}\boldsymbol{X}^T=\boldsymbol{X}^T\boldsymbol{X} \) or if \( \boldsymbol{X}\in {\mathbb{C}}^{n\times n} \) we have \( \boldsymbol{X}\boldsymbol{X}^{\dagger}=\boldsymbol{X}^{\dagger}\boldsymbol{X} \). -The matrix has then a set of eigenpairs +

    +
    + + +

    The examples we have looked at so far are cases where we normally can +invert the matrix \( \boldsymbol{X}^T\boldsymbol{X} \). Using a polynomial expansion where we fit of various functions leads to +row vectors of the design matrix which are essentially orthogonal due +to the polynomial character of our model. Obtaining the inverse of the +design matrix is then often done via a so-called LU, QR or Cholesky +decomposition.

    -$$ -(\lambda_1,\boldsymbol{u}_1),\dots, (\lambda_n,\boldsymbol{u}_n), -$$ - -

    and the eigenvalues are given by the diagonal matrix

    -$$ -\boldsymbol{\Sigma}=\mathrm{Diag}(\lambda_1, \dots,\lambda_n). -$$ - -

    The matrix \( \boldsymbol{X} \) can be written in terms of an orthogonal/unitary transformation \( \boldsymbol{U} \)

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

    with \( \boldsymbol{U}\boldsymbol{U}^T=\boldsymbol{I} \) or \( \boldsymbol{U}\boldsymbol{U}^{\dagger}=\boldsymbol{I} \).

    - -

    Not all square matrices are diagonalizable. A matrix like the one discussed above

    -$$ -\boldsymbol{X} = \begin{bmatrix} -1& -1 \\ -1& -1\\ -\end{bmatrix} -$$ - -

    is not diagonalizable, it is a so-called defective matrix. It is easy to see that the condition -\( \boldsymbol{X}\boldsymbol{X}^T=\boldsymbol{X}^T\boldsymbol{X} \) is not fulfilled. +

    As we will also see in the first project, +this may +however not the be case in general and a standard matrix inversion +algorithm based on say LU, QR or Cholesky decomposition may lead to singularities. We will see examples of this below.

    +

    There is however a way to circumvent this problem and also +gain some insights about the ordinary least squares approach, and +later shrinkage methods like Ridge and Lasso regressions. +

    + +

    This is given by the Singular Value Decomposition (SVD) algorithm, +perhaps the most powerful linear algebra algorithm. The SVD provides +a numerically stable matrix decomposition that is used in a large +swath oc applications and the decomposition is always stable +numerically. +

    + +

    In machine learning it plays a central role in dealing with for +example design matrices that may be near singular or singular. +Furthermore, as we will see here, the singular values can be related +to the covariance matrix (and thereby the correlation matrix) and in +turn the variance of a given quantity. It plays also an important role +in the principal component analysis where high-dimensional data can be +reduced to the statistically relevant features. +

    +
    +
    + +

    diff --git a/doc/pub/week35/html/._week35-bs042.html b/doc/pub/week35/html/._week35-bs042.html index 45e2a6bf5..3edf63337 100644 --- a/doc/pub/week35/html/._week35-bs042.html +++ b/doc/pub/week35/html/._week35-bs042.html @@ -74,10 +74,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'optimizing-our-parameters'), - ('Our model for the nuclear binding energies', + ('Examples relevant for the exercises', 2, None, - 'our-model-for-the-nuclear-binding-energies'), + 'examples-relevant-for-the-exercises'), ('Optimizing our parameters, more details', 2, None, @@ -94,6 +94,8 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'some-useful-matrix-and-vector-expressions'), + ('The Jacobian', 2, None, 'the-jacobian'), + ('Derivatives, example 1', 2, None, 'derivatives-example-1'), ('Meet the Hessian Matrix', 2, None, 'meet-the-hessian-matrix'), ('Interpretations and optimizing our parameters', 2, @@ -148,6 +150,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'more-preprocessing-examples-franke-function-and-regression'), + ('Material for lecture Thursday, August 31', + 2, + None, + 'material-for-lecture-thursday-august-31'), ('Mathematical Interpretation of Ordinary Least Squares', 2, None, @@ -303,74 +309,77 @@ MathJax.Hub.Config({
  • Why Linear Regression (aka Ordinary Least Squares and family), repeat from last week
  • Regression analysis, overarching aims
  • Regression analysis, overarching aims II
  • -
  • Examples
  • +
  • Examples
  • General linear models
  • Rewriting the fitting procedure as a linear algebra problem
  • Rewriting the fitting procedure as a linear algebra problem, more details
  • Generalizing the fitting procedure as a linear algebra problem
  • Generalizing the fitting procedure as a linear algebra problem
  • Optimizing our parameters
  • -
  • Our model for the nuclear binding energies
  • +
  • Examples relevant for the exercises
  • Optimizing our parameters, more details
  • -
  • Interpretations and optimizing our parameters
  • -
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • Some useful matrix and vector expressions
  • -
  • Meet the Hessian Matrix
  • -
  • Interpretations and optimizing our parameters
  • -
  • Own code for Ordinary Least Squares
  • -
  • Adding error analysis and training set up
  • -
  • Splitting our Data in Training and Test data
  • -
  • Examples
  • -
  • Making your own test-train splitting
  • -
  • The Boston housing data example
  • -
  • Housing data, the code
  • -
  • Reducing the number of degrees of freedom, overarching view
  • -
  • Preprocessing our data
  • -
  • Functionality in Scikit-Learn
  • -
  • More preprocessing
  • -
  • Frequently used scaling functions
  • -
  • Example of own Standard scaling
  • -
  • Min-Max Scaling
  • -
  • Testing the Means Squared Error as function of Complexity
  • -
  • More preprocessing examples, Franke function and regression
  • -
  • Mathematical Interpretation of Ordinary Least Squares
  • -
  • Residual Error
  • -
  • Simple case
  • -
  • The singular value decomposition
  • -
  • Linear Regression Problems
  • -
  • Fixing the singularity
  • -
  • Basic math of the SVD
  • -
  • The SVD, a Fantastic Algorithm
  • -
  • Economy-size SVD
  • -
  • Codes for the SVD
  • -
  • Note about SVD Calculations
  • -
  • Mathematics of the SVD and implications
  • -
  • Example Matrix
  • -
  • Setting up the Matrix to be inverted
  • -
  • Further properties (important for our analyses later)
  • -
  • Meet the Covariance Matrix
  • -
  • Introducing the Covariance and Correlation functions
  • -
  • Covariance and Correlation Matrix
  • -
  • Correlation Function and Design/Feature Matrix
  • -
  • Covariance Matrix Examples
  • -
  • Correlation Matrix
  • -
  • Correlation Matrix with Pandas
  • -
  • Correlation Matrix with Pandas and the Franke function
  • -
  • Rewriting the Covariance and/or Correlation Matrix
  • -
  • Linking with the SVD
  • -
  • What does it mean?
  • -
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • -
  • Ridge and LASSO Regression
  • -
  • Deriving the Ridge Regression Equations
  • -
  • Interpreting the Ridge results
  • -
  • More interpretations
  • -
  • Deriving the Lasso Regression Equations
  • -
  • Exercises for week 35
  • -
  • Exercise 1: Setting up various Python environments
  • -
  • Exercise 2: making your own data and exploring scikit-learn
  • -
  • Exercise 3: Normalizing our data
  • -
  • Exercise 4: Adding Ridge Regression
  • -
  • Exercise 5: Analytical exercises
  • +
  • The Jacobian
  • +
  • Derivatives, example 1
  • +
  • Meet the Hessian Matrix
  • +
  • Interpretations and optimizing our parameters
  • +
  • Own code for Ordinary Least Squares
  • +
  • Adding error analysis and training set up
  • +
  • Splitting our Data in Training and Test data
  • +
  • Examples
  • +
  • Making your own test-train splitting
  • +
  • The Boston housing data example
  • +
  • Housing data, the code
  • +
  • Reducing the number of degrees of freedom, overarching view
  • +
  • Preprocessing our data
  • +
  • Functionality in Scikit-Learn
  • +
  • More preprocessing
  • +
  • Frequently used scaling functions
  • +
  • Example of own Standard scaling
  • +
  • Min-Max Scaling
  • +
  • Testing the Means Squared Error as function of Complexity
  • +
  • More preprocessing examples, Franke function and regression
  • +
  • Material for lecture Thursday, August 31
  • +
  • Mathematical Interpretation of Ordinary Least Squares
  • +
  • Residual Error
  • +
  • Simple case
  • +
  • The singular value decomposition
  • +
  • Linear Regression Problems
  • +
  • Fixing the singularity
  • +
  • Basic math of the SVD
  • +
  • The SVD, a Fantastic Algorithm
  • +
  • Economy-size SVD
  • +
  • Codes for the SVD
  • +
  • Note about SVD Calculations
  • +
  • Mathematics of the SVD and implications
  • +
  • Example Matrix
  • +
  • Setting up the Matrix to be inverted
  • +
  • Further properties (important for our analyses later)
  • +
  • Meet the Covariance Matrix
  • +
  • Introducing the Covariance and Correlation functions
  • +
  • Covariance and Correlation Matrix
  • +
  • Correlation Function and Design/Feature Matrix
  • +
  • Covariance Matrix Examples
  • +
  • Correlation Matrix
  • +
  • Correlation Matrix with Pandas
  • +
  • Correlation Matrix with Pandas and the Franke function
  • +
  • Rewriting the Covariance and/or Correlation Matrix
  • +
  • Linking with the SVD
  • +
  • What does it mean?
  • +
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • +
  • Ridge and LASSO Regression
  • +
  • Deriving the Ridge Regression Equations
  • +
  • Interpreting the Ridge results
  • +
  • More interpretations
  • +
  • Deriving the Lasso Regression Equations
  • +
  • Exercises for week 35
  • +
  • Exercise 1: Setting up various Python environments
  • +
  • Exercise 2: making your own data and exploring scikit-learn
  • +
  • Exercise 3: Normalizing our data
  • +
  • Exercise 4: Adding Ridge Regression
  • +
  • Exercise 5: Analytical exercises
  • @@ -382,53 +391,55 @@ MathJax.Hub.Config({

     

     

     

    -

    The SVD, a Fantastic Algorithm

    +

    Linear Regression Problems

    -

    However, and this is the strength of the SVD algorithm, any general -matrix \( \boldsymbol{X} \) can be decomposed in terms of a diagonal matrix and -two orthogonal/unitary matrices. The Singular Value Decompostion -(SVD) theorem -states that a general \( m\times n \) matrix \( \boldsymbol{X} \) can be written in -terms of a diagonal matrix \( \boldsymbol{\Sigma} \) of dimensionality \( m\times n \) -and two orthognal matrices \( \boldsymbol{U} \) and \( \boldsymbol{V} \), where the first has -dimensionality \( m \times m \) and the last dimensionality \( n\times n \). -We have then +

    One of the typical problems we encounter with linear regression, in particular +when the matrix \( \boldsymbol{X} \) (our so-called design matrix) is high-dimensional, +are problems with near singular or singular matrices. The column vectors of \( \boldsymbol{X} \) +may be linearly dependent, normally referred to as super-collinearity. +This means that the matrix may be rank deficient and it is basically impossible to +to model the data using linear regression. As an example, consider the matrix

    - -$$ -\boldsymbol{X} = \boldsymbol{U}\boldsymbol{\Sigma}\boldsymbol{V}^T +$$ +\begin{align*} +\mathbf{X} & = \left[ +\begin{array}{rrr} +1 & -1 & 2 +\\ +1 & 0 & 1 +\\ +1 & 2 & -1 +\\ +1 & 1 & 0 +\end{array} \right] +\end{align*} $$ -

    As an example, the above defective matrix can be decomposed as

    +

    The columns of \( \boldsymbol{X} \) are linearly dependent. We see this easily since the +the first column is the row-wise sum of the other two columns. The rank (more correct, +the column rank) of a matrix is the dimension of the space spanned by the +column vectors. Hence, the rank of \( \mathbf{X} \) is equal to the number +of linearly independent columns. In this particular case the matrix has rank 2. +

    +

    Super-collinearity of an \( (n \times p) \)-dimensional design matrix \( \mathbf{X} \) implies +that the inverse of the matrix \( \boldsymbol{X}^T\boldsymbol{X} \) (the matrix we need to invert to solve the linear regression equations) is non-invertible. If we have a square matrix that does not have an inverse, we say this matrix singular. The example here demonstrates this +

    $$ -\boldsymbol{X} = \frac{1}{\sqrt{2}}\begin{bmatrix} 1& 1 \\ 1& -1\\ \end{bmatrix} \begin{bmatrix} 2& 0 \\ 0& 0\\ \end{bmatrix} \frac{1}{\sqrt{2}}\begin{bmatrix} 1& -1 \\ 1& 1\\ \end{bmatrix}=\boldsymbol{U}\boldsymbol{\Sigma}\boldsymbol{V}^T, +\begin{align*} +\boldsymbol{X} & = \left[ +\begin{array}{rr} +1 & -1 +\\ +1 & -1 +\end{array} \right]. +\end{align*} $$ -

    with eigenvalues \( \sigma_1=2 \) and \( \sigma_2=0 \). -The SVD exits always! +

    We see easily that \( \mbox{det}(\boldsymbol{X}) = x_{11} x_{22} - x_{12} x_{21} = 1 \times (-1) - 1 \times (-1) = 0 \). Hence, \( \mathbf{X} \) is singular and its inverse is undefined. +This is equivalent to saying that the matrix \( \boldsymbol{X} \) has at least an eigenvalue which is zero.

    -

    The SVD -decomposition (singular values) gives eigenvalues -\( \sigma_i\geq\sigma_{i+1} \) for all \( i \) and for dimensions larger than \( i=p \), the -eigenvalues (singular values) are zero. -

    - -

    In the general case, where our design matrix \( \boldsymbol{X} \) has dimension -\( n\times p \), the matrix is thus decomposed into an \( n\times n \) -orthogonal matrix \( \boldsymbol{U} \), a \( p\times p \) orthogonal matrix \( \boldsymbol{V} \) -and a diagonal matrix \( \boldsymbol{\Sigma} \) with \( r=\mathrm{min}(n,p) \) -singular values \( \sigma_i\geq 0 \) on the main diagonal and zeros filling -the rest of the matrix. There are at most \( p \) singular values -assuming that \( n > p \). In our regression examples for the nuclear -masses and the equation of state this is indeed the case, while for -the Ising model we have \( p > n \). These are often cases that lead to -near singular or singular matrices. -

    - -

    The columns of \( \boldsymbol{U} \) are called the left singular vectors while the columns of \( \boldsymbol{V} \) are the right singular vectors.

    -

      @@ -454,7 +465,7 @@ near singular or singular matrices.
    • 51
    • 52
    • ...
    • -
    • 68
    • +
    • 71
    • »
    diff --git a/doc/pub/week35/html/._week35-bs043.html b/doc/pub/week35/html/._week35-bs043.html index 87cb23f65..411ae1d4b 100644 --- a/doc/pub/week35/html/._week35-bs043.html +++ b/doc/pub/week35/html/._week35-bs043.html @@ -74,10 +74,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'optimizing-our-parameters'), - ('Our model for the nuclear binding energies', + ('Examples relevant for the exercises', 2, None, - 'our-model-for-the-nuclear-binding-energies'), + 'examples-relevant-for-the-exercises'), ('Optimizing our parameters, more details', 2, None, @@ -94,6 +94,8 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'some-useful-matrix-and-vector-expressions'), + ('The Jacobian', 2, None, 'the-jacobian'), + ('Derivatives, example 1', 2, None, 'derivatives-example-1'), ('Meet the Hessian Matrix', 2, None, 'meet-the-hessian-matrix'), ('Interpretations and optimizing our parameters', 2, @@ -148,6 +150,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'more-preprocessing-examples-franke-function-and-regression'), + ('Material for lecture Thursday, August 31', + 2, + None, + 'material-for-lecture-thursday-august-31'), ('Mathematical Interpretation of Ordinary Least Squares', 2, None, @@ -303,74 +309,77 @@ MathJax.Hub.Config({
  • Why Linear Regression (aka Ordinary Least Squares and family), repeat from last week
  • Regression analysis, overarching aims
  • Regression analysis, overarching aims II
  • -
  • Examples
  • +
  • Examples
  • General linear models
  • Rewriting the fitting procedure as a linear algebra problem
  • Rewriting the fitting procedure as a linear algebra problem, more details
  • Generalizing the fitting procedure as a linear algebra problem
  • Generalizing the fitting procedure as a linear algebra problem
  • Optimizing our parameters
  • -
  • Our model for the nuclear binding energies
  • +
  • Examples relevant for the exercises
  • Optimizing our parameters, more details
  • -
  • Interpretations and optimizing our parameters
  • -
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • Some useful matrix and vector expressions
  • -
  • Meet the Hessian Matrix
  • -
  • Interpretations and optimizing our parameters
  • -
  • Own code for Ordinary Least Squares
  • -
  • Adding error analysis and training set up
  • -
  • Splitting our Data in Training and Test data
  • -
  • Examples
  • -
  • Making your own test-train splitting
  • -
  • The Boston housing data example
  • -
  • Housing data, the code
  • -
  • Reducing the number of degrees of freedom, overarching view
  • -
  • Preprocessing our data
  • -
  • Functionality in Scikit-Learn
  • -
  • More preprocessing
  • -
  • Frequently used scaling functions
  • -
  • Example of own Standard scaling
  • -
  • Min-Max Scaling
  • -
  • Testing the Means Squared Error as function of Complexity
  • -
  • More preprocessing examples, Franke function and regression
  • -
  • Mathematical Interpretation of Ordinary Least Squares
  • -
  • Residual Error
  • -
  • Simple case
  • -
  • The singular value decomposition
  • -
  • Linear Regression Problems
  • -
  • Fixing the singularity
  • -
  • Basic math of the SVD
  • -
  • The SVD, a Fantastic Algorithm
  • -
  • Economy-size SVD
  • -
  • Codes for the SVD
  • -
  • Note about SVD Calculations
  • -
  • Mathematics of the SVD and implications
  • -
  • Example Matrix
  • -
  • Setting up the Matrix to be inverted
  • -
  • Further properties (important for our analyses later)
  • -
  • Meet the Covariance Matrix
  • -
  • Introducing the Covariance and Correlation functions
  • -
  • Covariance and Correlation Matrix
  • -
  • Correlation Function and Design/Feature Matrix
  • -
  • Covariance Matrix Examples
  • -
  • Correlation Matrix
  • -
  • Correlation Matrix with Pandas
  • -
  • Correlation Matrix with Pandas and the Franke function
  • -
  • Rewriting the Covariance and/or Correlation Matrix
  • -
  • Linking with the SVD
  • -
  • What does it mean?
  • -
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • -
  • Ridge and LASSO Regression
  • -
  • Deriving the Ridge Regression Equations
  • -
  • Interpreting the Ridge results
  • -
  • More interpretations
  • -
  • Deriving the Lasso Regression Equations
  • -
  • Exercises for week 35
  • -
  • Exercise 1: Setting up various Python environments
  • -
  • Exercise 2: making your own data and exploring scikit-learn
  • -
  • Exercise 3: Normalizing our data
  • -
  • Exercise 4: Adding Ridge Regression
  • -
  • Exercise 5: Analytical exercises
  • +
  • The Jacobian
  • +
  • Derivatives, example 1
  • +
  • Meet the Hessian Matrix
  • +
  • Interpretations and optimizing our parameters
  • +
  • Own code for Ordinary Least Squares
  • +
  • Adding error analysis and training set up
  • +
  • Splitting our Data in Training and Test data
  • +
  • Examples
  • +
  • Making your own test-train splitting
  • +
  • The Boston housing data example
  • +
  • Housing data, the code
  • +
  • Reducing the number of degrees of freedom, overarching view
  • +
  • Preprocessing our data
  • +
  • Functionality in Scikit-Learn
  • +
  • More preprocessing
  • +
  • Frequently used scaling functions
  • +
  • Example of own Standard scaling
  • +
  • Min-Max Scaling
  • +
  • Testing the Means Squared Error as function of Complexity
  • +
  • More preprocessing examples, Franke function and regression
  • +
  • Material for lecture Thursday, August 31
  • +
  • Mathematical Interpretation of Ordinary Least Squares
  • +
  • Residual Error
  • +
  • Simple case
  • +
  • The singular value decomposition
  • +
  • Linear Regression Problems
  • +
  • Fixing the singularity
  • +
  • Basic math of the SVD
  • +
  • The SVD, a Fantastic Algorithm
  • +
  • Economy-size SVD
  • +
  • Codes for the SVD
  • +
  • Note about SVD Calculations
  • +
  • Mathematics of the SVD and implications
  • +
  • Example Matrix
  • +
  • Setting up the Matrix to be inverted
  • +
  • Further properties (important for our analyses later)
  • +
  • Meet the Covariance Matrix
  • +
  • Introducing the Covariance and Correlation functions
  • +
  • Covariance and Correlation Matrix
  • +
  • Correlation Function and Design/Feature Matrix
  • +
  • Covariance Matrix Examples
  • +
  • Correlation Matrix
  • +
  • Correlation Matrix with Pandas
  • +
  • Correlation Matrix with Pandas and the Franke function
  • +
  • Rewriting the Covariance and/or Correlation Matrix
  • +
  • Linking with the SVD
  • +
  • What does it mean?
  • +
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • +
  • Ridge and LASSO Regression
  • +
  • Deriving the Ridge Regression Equations
  • +
  • Interpreting the Ridge results
  • +
  • More interpretations
  • +
  • Deriving the Lasso Regression Equations
  • +
  • Exercises for week 35
  • +
  • Exercise 1: Setting up various Python environments
  • +
  • Exercise 2: making your own data and exploring scikit-learn
  • +
  • Exercise 3: Normalizing our data
  • +
  • Exercise 4: Adding Ridge Regression
  • +
  • Exercise 5: Analytical exercises
  • @@ -382,27 +391,29 @@ MathJax.Hub.Config({

     

     

     

    -

    Economy-size SVD

    +

    Fixing the singularity

    -

    If we assume that \( n > p \), then our matrix \( \boldsymbol{U} \) has dimension \( n -\times n \). The last \( n-p \) columns of \( \boldsymbol{U} \) become however -irrelevant in our calculations since they are multiplied with the -zeros in \( \boldsymbol{\Sigma} \). +

    If our design matrix \( \boldsymbol{X} \) which enters the linear regression problem

    +$$ +\begin{align} +\boldsymbol{\beta} & = (\boldsymbol{X}^{T} \boldsymbol{X})^{-1} \boldsymbol{X}^{T} \boldsymbol{y}, +\tag{1} +\end{align} +$$ + +

    has linearly dependent column vectors, we will not be able to compute the inverse +of \( \boldsymbol{X}^T\boldsymbol{X} \) and we cannot find the parameters (estimators) \( \beta_i \). +The estimators are only well-defined if \( (\boldsymbol{X}^{T}\boldsymbol{X})^{-1} \) exits. +This is more likely to happen when the matrix \( \boldsymbol{X} \) is high-dimensional. In this case it is likely to encounter a situation where +the regression parameters \( \beta_i \) cannot be estimated.

    -

    The economy-size decomposition removes extra rows or columns of zeros -from the diagonal matrix of singular values, \( \boldsymbol{\Sigma} \), along with the columns -in either \( \boldsymbol{U} \) or \( \boldsymbol{V} \) that multiply those zeros in the expression. -Removing these zeros and columns can improve execution time -and reduce storage requirements without compromising the accuracy of -the decomposition. -

    +

    A cheap ad hoc approach is simply to add a small diagonal component to the matrix to invert, that is we change

    +$$ +\boldsymbol{X}^{T} \boldsymbol{X} \rightarrow \boldsymbol{X}^{T} \boldsymbol{X}+\lambda \boldsymbol{I}, +$$ -

    If \( n > p \), we keep only the first \( p \) columns of \( \boldsymbol{U} \) and \( \boldsymbol{\Sigma} \) has dimension \( p\times p \). -If \( p > n \), then only the first \( n \) columns of \( \boldsymbol{V} \) are computed and \( \boldsymbol{\Sigma} \) has dimension \( n\times n \). -The \( n=p \) case is obvious, we retain the full SVD. -In general the economy-size SVD leads to less FLOPS and still conserving the desired accuracy. -

    +

    where \( \boldsymbol{I} \) is the identity matrix. When we discuss Ridge regression this is actually what we end up evaluating. The parameter \( \lambda \) is called a hyperparameter. More about this later.

    @@ -429,7 +440,7 @@ In general the economy-size SVD leads to less FLOPS and still conserving the des

  • 52
  • 53
  • ...
  • -
  • 68
  • +
  • 71
  • »
  • diff --git a/doc/pub/week35/html/._week35-bs044.html b/doc/pub/week35/html/._week35-bs044.html index 65bc8e2fb..0248c7bd3 100644 --- a/doc/pub/week35/html/._week35-bs044.html +++ b/doc/pub/week35/html/._week35-bs044.html @@ -74,10 +74,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'optimizing-our-parameters'), - ('Our model for the nuclear binding energies', + ('Examples relevant for the exercises', 2, None, - 'our-model-for-the-nuclear-binding-energies'), + 'examples-relevant-for-the-exercises'), ('Optimizing our parameters, more details', 2, None, @@ -94,6 +94,8 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'some-useful-matrix-and-vector-expressions'), + ('The Jacobian', 2, None, 'the-jacobian'), + ('Derivatives, example 1', 2, None, 'derivatives-example-1'), ('Meet the Hessian Matrix', 2, None, 'meet-the-hessian-matrix'), ('Interpretations and optimizing our parameters', 2, @@ -148,6 +150,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'more-preprocessing-examples-franke-function-and-regression'), + ('Material for lecture Thursday, August 31', + 2, + None, + 'material-for-lecture-thursday-august-31'), ('Mathematical Interpretation of Ordinary Least Squares', 2, None, @@ -303,74 +309,77 @@ MathJax.Hub.Config({
  • Why Linear Regression (aka Ordinary Least Squares and family), repeat from last week
  • Regression analysis, overarching aims
  • Regression analysis, overarching aims II
  • -
  • Examples
  • +
  • Examples
  • General linear models
  • Rewriting the fitting procedure as a linear algebra problem
  • Rewriting the fitting procedure as a linear algebra problem, more details
  • Generalizing the fitting procedure as a linear algebra problem
  • Generalizing the fitting procedure as a linear algebra problem
  • Optimizing our parameters
  • -
  • Our model for the nuclear binding energies
  • +
  • Examples relevant for the exercises
  • Optimizing our parameters, more details
  • -
  • Interpretations and optimizing our parameters
  • -
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • Some useful matrix and vector expressions
  • -
  • Meet the Hessian Matrix
  • -
  • Interpretations and optimizing our parameters
  • -
  • Own code for Ordinary Least Squares
  • -
  • Adding error analysis and training set up
  • -
  • Splitting our Data in Training and Test data
  • -
  • Examples
  • -
  • Making your own test-train splitting
  • -
  • The Boston housing data example
  • -
  • Housing data, the code
  • -
  • Reducing the number of degrees of freedom, overarching view
  • -
  • Preprocessing our data
  • -
  • Functionality in Scikit-Learn
  • -
  • More preprocessing
  • -
  • Frequently used scaling functions
  • -
  • Example of own Standard scaling
  • -
  • Min-Max Scaling
  • -
  • Testing the Means Squared Error as function of Complexity
  • -
  • More preprocessing examples, Franke function and regression
  • -
  • Mathematical Interpretation of Ordinary Least Squares
  • -
  • Residual Error
  • -
  • Simple case
  • -
  • The singular value decomposition
  • -
  • Linear Regression Problems
  • -
  • Fixing the singularity
  • -
  • Basic math of the SVD
  • -
  • The SVD, a Fantastic Algorithm
  • -
  • Economy-size SVD
  • -
  • Codes for the SVD
  • -
  • Note about SVD Calculations
  • -
  • Mathematics of the SVD and implications
  • -
  • Example Matrix
  • -
  • Setting up the Matrix to be inverted
  • -
  • Further properties (important for our analyses later)
  • -
  • Meet the Covariance Matrix
  • -
  • Introducing the Covariance and Correlation functions
  • -
  • Covariance and Correlation Matrix
  • -
  • Correlation Function and Design/Feature Matrix
  • -
  • Covariance Matrix Examples
  • -
  • Correlation Matrix
  • -
  • Correlation Matrix with Pandas
  • -
  • Correlation Matrix with Pandas and the Franke function
  • -
  • Rewriting the Covariance and/or Correlation Matrix
  • -
  • Linking with the SVD
  • -
  • What does it mean?
  • -
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • -
  • Ridge and LASSO Regression
  • -
  • Deriving the Ridge Regression Equations
  • -
  • Interpreting the Ridge results
  • -
  • More interpretations
  • -
  • Deriving the Lasso Regression Equations
  • -
  • Exercises for week 35
  • -
  • Exercise 1: Setting up various Python environments
  • -
  • Exercise 2: making your own data and exploring scikit-learn
  • -
  • Exercise 3: Normalizing our data
  • -
  • Exercise 4: Adding Ridge Regression
  • -
  • Exercise 5: Analytical exercises
  • +
  • The Jacobian
  • +
  • Derivatives, example 1
  • +
  • Meet the Hessian Matrix
  • +
  • Interpretations and optimizing our parameters
  • +
  • Own code for Ordinary Least Squares
  • +
  • Adding error analysis and training set up
  • +
  • Splitting our Data in Training and Test data
  • +
  • Examples
  • +
  • Making your own test-train splitting
  • +
  • The Boston housing data example
  • +
  • Housing data, the code
  • +
  • Reducing the number of degrees of freedom, overarching view
  • +
  • Preprocessing our data
  • +
  • Functionality in Scikit-Learn
  • +
  • More preprocessing
  • +
  • Frequently used scaling functions
  • +
  • Example of own Standard scaling
  • +
  • Min-Max Scaling
  • +
  • Testing the Means Squared Error as function of Complexity
  • +
  • More preprocessing examples, Franke function and regression
  • +
  • Material for lecture Thursday, August 31
  • +
  • Mathematical Interpretation of Ordinary Least Squares
  • +
  • Residual Error
  • +
  • Simple case
  • +
  • The singular value decomposition
  • +
  • Linear Regression Problems
  • +
  • Fixing the singularity
  • +
  • Basic math of the SVD
  • +
  • The SVD, a Fantastic Algorithm
  • +
  • Economy-size SVD
  • +
  • Codes for the SVD
  • +
  • Note about SVD Calculations
  • +
  • Mathematics of the SVD and implications
  • +
  • Example Matrix
  • +
  • Setting up the Matrix to be inverted
  • +
  • Further properties (important for our analyses later)
  • +
  • Meet the Covariance Matrix
  • +
  • Introducing the Covariance and Correlation functions
  • +
  • Covariance and Correlation Matrix
  • +
  • Correlation Function and Design/Feature Matrix
  • +
  • Covariance Matrix Examples
  • +
  • Correlation Matrix
  • +
  • Correlation Matrix with Pandas
  • +
  • Correlation Matrix with Pandas and the Franke function
  • +
  • Rewriting the Covariance and/or Correlation Matrix
  • +
  • Linking with the SVD
  • +
  • What does it mean?
  • +
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • +
  • Ridge and LASSO Regression
  • +
  • Deriving the Ridge Regression Equations
  • +
  • Interpreting the Ridge results
  • +
  • More interpretations
  • +
  • Deriving the Lasso Regression Equations
  • +
  • Exercises for week 35
  • +
  • Exercise 1: Setting up various Python environments
  • +
  • Exercise 2: making your own data and exploring scikit-learn
  • +
  • Exercise 3: Normalizing our data
  • +
  • Exercise 4: Adding Ridge Regression
  • +
  • Exercise 5: Analytical exercises
  • @@ -382,67 +391,40 @@ MathJax.Hub.Config({

     

     

     

    -

    Codes for the SVD

    +

    Basic math of the SVD

    +

    From standard linear algebra we know that a square matrix \( \boldsymbol{X} \) can be diagonalized if and only it is +a so-called normal matrix, that is if \( \boldsymbol{X}\in {\mathbb{R}}^{n\times n} \) +we have \( \boldsymbol{X}\boldsymbol{X}^T=\boldsymbol{X}^T\boldsymbol{X} \) or if \( \boldsymbol{X}\in {\mathbb{C}}^{n\times n} \) we have \( \boldsymbol{X}\boldsymbol{X}^{\dagger}=\boldsymbol{X}^{\dagger}\boldsymbol{X} \). +The matrix has then a set of eigenpairs +

    - -
    -
    -
    -
    -
    -
    import numpy as np
    -# SVD inversion
    -def SVD(A):
    -    ''' Takes as input a numpy matrix A and returns inv(A) based on singular value decomposition (SVD).
    -    SVD is numerically more stable than the inversion algorithms provided by
    -    numpy and scipy.linalg at the cost of being slower.
    -    '''
    -    U, S, VT = np.linalg.svd(A,full_matrices=True)
    -    print('test U')
    -    print( (np.transpose(U) @ U - U @np.transpose(U)))
    -    print('test VT')
    -    print( (np.transpose(VT) @ VT - VT @np.transpose(VT)))
    -    print(U)
    -    print(S)
    -    print(VT)
    +$$
    +(\lambda_1,\boldsymbol{u}_1),\dots, (\lambda_n,\boldsymbol{u}_n),
    +$$
     
    -    D = np.zeros((len(U),len(VT)))
    -    for i in range(0,len(VT)):
    -        D[i,i]=S[i]
    -    return U @ D @ VT
    +

    and the eigenvalues are given by the diagonal matrix

    +$$ +\boldsymbol{\Sigma}=\mathrm{Diag}(\lambda_1, \dots,\lambda_n). +$$ +

    The matrix \( \boldsymbol{X} \) can be written in terms of an orthogonal/unitary transformation \( \boldsymbol{U} \)

    +$$ +\boldsymbol{X} = \boldsymbol{U}\boldsymbol{\Sigma}\boldsymbol{V}^T, +$$ -X = np.array([ [1.0,-1.0], [1.0,-1.0]]) -#X = np.array([[1, 2], [3, 4], [5, 6]]) +

    with \( \boldsymbol{U}\boldsymbol{U}^T=\boldsymbol{I} \) or \( \boldsymbol{U}\boldsymbol{U}^{\dagger}=\boldsymbol{I} \).

    -print(X) -C = SVD(X) -# Print the difference between the original matrix and the SVD one -print(C-X) -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    +

    Not all square matrices are diagonalizable. A matrix like the one discussed above

    +$$ +\boldsymbol{X} = \begin{bmatrix} +1& -1 \\ +1& -1\\ +\end{bmatrix} +$$ -

    The matrix \( \boldsymbol{X} \) has columns that are linearly dependent. The first -column is the row-wise sum of the other two columns. The rank of a -matrix (the column rank) is the dimension of space spanned by the -column vectors. The rank of the matrix is the number of linearly -independent columns, in this case just \( 2 \). We see this from the -singular values when running the above code. Running the standard -inversion algorithm for matrix inversion with \( \boldsymbol{X}^T\boldsymbol{X} \) results -in the program terminating due to a singular matrix. +

    is not diagonalizable, it is a so-called defective matrix. It is easy to see that the condition +\( \boldsymbol{X}\boldsymbol{X}^T=\boldsymbol{X}^T\boldsymbol{X} \) is not fulfilled.

    @@ -470,7 +452,7 @@ in the program terminating due to a singular matrix.

  • 53
  • 54
  • ...
  • -
  • 68
  • +
  • 71
  • »
  • diff --git a/doc/pub/week35/html/._week35-bs045.html b/doc/pub/week35/html/._week35-bs045.html index 046d02f49..75cd0ab9b 100644 --- a/doc/pub/week35/html/._week35-bs045.html +++ b/doc/pub/week35/html/._week35-bs045.html @@ -74,10 +74,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'optimizing-our-parameters'), - ('Our model for the nuclear binding energies', + ('Examples relevant for the exercises', 2, None, - 'our-model-for-the-nuclear-binding-energies'), + 'examples-relevant-for-the-exercises'), ('Optimizing our parameters, more details', 2, None, @@ -94,6 +94,8 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'some-useful-matrix-and-vector-expressions'), + ('The Jacobian', 2, None, 'the-jacobian'), + ('Derivatives, example 1', 2, None, 'derivatives-example-1'), ('Meet the Hessian Matrix', 2, None, 'meet-the-hessian-matrix'), ('Interpretations and optimizing our parameters', 2, @@ -148,6 +150,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'more-preprocessing-examples-franke-function-and-regression'), + ('Material for lecture Thursday, August 31', + 2, + None, + 'material-for-lecture-thursday-august-31'), ('Mathematical Interpretation of Ordinary Least Squares', 2, None, @@ -303,74 +309,77 @@ MathJax.Hub.Config({
  • Why Linear Regression (aka Ordinary Least Squares and family), repeat from last week
  • Regression analysis, overarching aims
  • Regression analysis, overarching aims II
  • -
  • Examples
  • +
  • Examples
  • General linear models
  • Rewriting the fitting procedure as a linear algebra problem
  • Rewriting the fitting procedure as a linear algebra problem, more details
  • Generalizing the fitting procedure as a linear algebra problem
  • Generalizing the fitting procedure as a linear algebra problem
  • Optimizing our parameters
  • -
  • Our model for the nuclear binding energies
  • +
  • Examples relevant for the exercises
  • Optimizing our parameters, more details
  • -
  • Interpretations and optimizing our parameters
  • -
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • Some useful matrix and vector expressions
  • -
  • Meet the Hessian Matrix
  • -
  • Interpretations and optimizing our parameters
  • -
  • Own code for Ordinary Least Squares
  • -
  • Adding error analysis and training set up
  • -
  • Splitting our Data in Training and Test data
  • -
  • Examples
  • -
  • Making your own test-train splitting
  • -
  • The Boston housing data example
  • -
  • Housing data, the code
  • -
  • Reducing the number of degrees of freedom, overarching view
  • -
  • Preprocessing our data
  • -
  • Functionality in Scikit-Learn
  • -
  • More preprocessing
  • -
  • Frequently used scaling functions
  • -
  • Example of own Standard scaling
  • -
  • Min-Max Scaling
  • -
  • Testing the Means Squared Error as function of Complexity
  • -
  • More preprocessing examples, Franke function and regression
  • -
  • Mathematical Interpretation of Ordinary Least Squares
  • -
  • Residual Error
  • -
  • Simple case
  • -
  • The singular value decomposition
  • -
  • Linear Regression Problems
  • -
  • Fixing the singularity
  • -
  • Basic math of the SVD
  • -
  • The SVD, a Fantastic Algorithm
  • -
  • Economy-size SVD
  • -
  • Codes for the SVD
  • -
  • Note about SVD Calculations
  • -
  • Mathematics of the SVD and implications
  • -
  • Example Matrix
  • -
  • Setting up the Matrix to be inverted
  • -
  • Further properties (important for our analyses later)
  • -
  • Meet the Covariance Matrix
  • -
  • Introducing the Covariance and Correlation functions
  • -
  • Covariance and Correlation Matrix
  • -
  • Correlation Function and Design/Feature Matrix
  • -
  • Covariance Matrix Examples
  • -
  • Correlation Matrix
  • -
  • Correlation Matrix with Pandas
  • -
  • Correlation Matrix with Pandas and the Franke function
  • -
  • Rewriting the Covariance and/or Correlation Matrix
  • -
  • Linking with the SVD
  • -
  • What does it mean?
  • -
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • -
  • Ridge and LASSO Regression
  • -
  • Deriving the Ridge Regression Equations
  • -
  • Interpreting the Ridge results
  • -
  • More interpretations
  • -
  • Deriving the Lasso Regression Equations
  • -
  • Exercises for week 35
  • -
  • Exercise 1: Setting up various Python environments
  • -
  • Exercise 2: making your own data and exploring scikit-learn
  • -
  • Exercise 3: Normalizing our data
  • -
  • Exercise 4: Adding Ridge Regression
  • -
  • Exercise 5: Analytical exercises
  • +
  • The Jacobian
  • +
  • Derivatives, example 1
  • +
  • Meet the Hessian Matrix
  • +
  • Interpretations and optimizing our parameters
  • +
  • Own code for Ordinary Least Squares
  • +
  • Adding error analysis and training set up
  • +
  • Splitting our Data in Training and Test data
  • +
  • Examples
  • +
  • Making your own test-train splitting
  • +
  • The Boston housing data example
  • +
  • Housing data, the code
  • +
  • Reducing the number of degrees of freedom, overarching view
  • +
  • Preprocessing our data
  • +
  • Functionality in Scikit-Learn
  • +
  • More preprocessing
  • +
  • Frequently used scaling functions
  • +
  • Example of own Standard scaling
  • +
  • Min-Max Scaling
  • +
  • Testing the Means Squared Error as function of Complexity
  • +
  • More preprocessing examples, Franke function and regression
  • +
  • Material for lecture Thursday, August 31
  • +
  • Mathematical Interpretation of Ordinary Least Squares
  • +
  • Residual Error
  • +
  • Simple case
  • +
  • The singular value decomposition
  • +
  • Linear Regression Problems
  • +
  • Fixing the singularity
  • +
  • Basic math of the SVD
  • +
  • The SVD, a Fantastic Algorithm
  • +
  • Economy-size SVD
  • +
  • Codes for the SVD
  • +
  • Note about SVD Calculations
  • +
  • Mathematics of the SVD and implications
  • +
  • Example Matrix
  • +
  • Setting up the Matrix to be inverted
  • +
  • Further properties (important for our analyses later)
  • +
  • Meet the Covariance Matrix
  • +
  • Introducing the Covariance and Correlation functions
  • +
  • Covariance and Correlation Matrix
  • +
  • Correlation Function and Design/Feature Matrix
  • +
  • Covariance Matrix Examples
  • +
  • Correlation Matrix
  • +
  • Correlation Matrix with Pandas
  • +
  • Correlation Matrix with Pandas and the Franke function
  • +
  • Rewriting the Covariance and/or Correlation Matrix
  • +
  • Linking with the SVD
  • +
  • What does it mean?
  • +
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • +
  • Ridge and LASSO Regression
  • +
  • Deriving the Ridge Regression Equations
  • +
  • Interpreting the Ridge results
  • +
  • More interpretations
  • +
  • Deriving the Lasso Regression Equations
  • +
  • Exercises for week 35
  • +
  • Exercise 1: Setting up various Python environments
  • +
  • Exercise 2: making your own data and exploring scikit-learn
  • +
  • Exercise 3: Normalizing our data
  • +
  • Exercise 4: Adding Ridge Regression
  • +
  • Exercise 5: Analytical exercises
  • @@ -382,24 +391,53 @@ MathJax.Hub.Config({

     

     

     

    -

    Note about SVD Calculations

    +

    The SVD, a Fantastic Algorithm

    -

    The \( U \), \( S \), and \( V \) matrices returned from the svd() function -cannot be multiplied directly. +

    However, and this is the strength of the SVD algorithm, any general +matrix \( \boldsymbol{X} \) can be decomposed in terms of a diagonal matrix and +two orthogonal/unitary matrices. The Singular Value Decompostion +(SVD) theorem +states that a general \( m\times n \) matrix \( \boldsymbol{X} \) can be written in +terms of a diagonal matrix \( \boldsymbol{\Sigma} \) of dimensionality \( m\times n \) +and two orthognal matrices \( \boldsymbol{U} \) and \( \boldsymbol{V} \), where the first has +dimensionality \( m \times m \) and the last dimensionality \( n\times n \). +We have then

    -

    As you can see from the code, the \( S \) vector must be converted into a -diagonal matrix. This may cause a problem as the size of the matrices -do not fit the rules of matrix multiplication, where the number of -columns in a matrix must match the number of rows in the subsequent -matrix. +$$ +\boldsymbol{X} = \boldsymbol{U}\boldsymbol{\Sigma}\boldsymbol{V}^T +$$ + +

    As an example, the above defective matrix can be decomposed as

    + +$$ +\boldsymbol{X} = \frac{1}{\sqrt{2}}\begin{bmatrix} 1& 1 \\ 1& -1\\ \end{bmatrix} \begin{bmatrix} 2& 0 \\ 0& 0\\ \end{bmatrix} \frac{1}{\sqrt{2}}\begin{bmatrix} 1& -1 \\ 1& 1\\ \end{bmatrix}=\boldsymbol{U}\boldsymbol{\Sigma}\boldsymbol{V}^T, +$$ + +

    with eigenvalues \( \sigma_1=2 \) and \( \sigma_2=0 \). +The SVD exits always!

    -

    If you wish to include the zero singular values, you will need to -resize the matrices and set up a diagonal matrix as done in the above -example +

    The SVD +decomposition (singular values) gives eigenvalues +\( \sigma_i\geq\sigma_{i+1} \) for all \( i \) and for dimensions larger than \( i=p \), the +eigenvalues (singular values) are zero.

    +

    In the general case, where our design matrix \( \boldsymbol{X} \) has dimension +\( n\times p \), the matrix is thus decomposed into an \( n\times n \) +orthogonal matrix \( \boldsymbol{U} \), a \( p\times p \) orthogonal matrix \( \boldsymbol{V} \) +and a diagonal matrix \( \boldsymbol{\Sigma} \) with \( r=\mathrm{min}(n,p) \) +singular values \( \sigma_i\geq 0 \) on the main diagonal and zeros filling +the rest of the matrix. There are at most \( p \) singular values +assuming that \( n > p \). In our regression examples for the nuclear +masses and the equation of state this is indeed the case, while for +the Ising model we have \( p > n \). These are often cases that lead to +near singular or singular matrices. +

    + +

    The columns of \( \boldsymbol{U} \) are called the left singular vectors while the columns of \( \boldsymbol{V} \) are the right singular vectors.

    +

    diff --git a/doc/pub/week35/html/._week35-bs046.html b/doc/pub/week35/html/._week35-bs046.html index 713a3e716..498c28395 100644 --- a/doc/pub/week35/html/._week35-bs046.html +++ b/doc/pub/week35/html/._week35-bs046.html @@ -74,10 +74,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'optimizing-our-parameters'), - ('Our model for the nuclear binding energies', + ('Examples relevant for the exercises', 2, None, - 'our-model-for-the-nuclear-binding-energies'), + 'examples-relevant-for-the-exercises'), ('Optimizing our parameters, more details', 2, None, @@ -94,6 +94,8 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'some-useful-matrix-and-vector-expressions'), + ('The Jacobian', 2, None, 'the-jacobian'), + ('Derivatives, example 1', 2, None, 'derivatives-example-1'), ('Meet the Hessian Matrix', 2, None, 'meet-the-hessian-matrix'), ('Interpretations and optimizing our parameters', 2, @@ -148,6 +150,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'more-preprocessing-examples-franke-function-and-regression'), + ('Material for lecture Thursday, August 31', + 2, + None, + 'material-for-lecture-thursday-august-31'), ('Mathematical Interpretation of Ordinary Least Squares', 2, None, @@ -303,74 +309,77 @@ MathJax.Hub.Config({
  • Why Linear Regression (aka Ordinary Least Squares and family), repeat from last week
  • Regression analysis, overarching aims
  • Regression analysis, overarching aims II
  • -
  • Examples
  • +
  • Examples
  • General linear models
  • Rewriting the fitting procedure as a linear algebra problem
  • Rewriting the fitting procedure as a linear algebra problem, more details
  • Generalizing the fitting procedure as a linear algebra problem
  • Generalizing the fitting procedure as a linear algebra problem
  • Optimizing our parameters
  • -
  • Our model for the nuclear binding energies
  • +
  • Examples relevant for the exercises
  • Optimizing our parameters, more details
  • -
  • Interpretations and optimizing our parameters
  • -
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • Some useful matrix and vector expressions
  • -
  • Meet the Hessian Matrix
  • -
  • Interpretations and optimizing our parameters
  • -
  • Own code for Ordinary Least Squares
  • -
  • Adding error analysis and training set up
  • -
  • Splitting our Data in Training and Test data
  • -
  • Examples
  • -
  • Making your own test-train splitting
  • -
  • The Boston housing data example
  • -
  • Housing data, the code
  • -
  • Reducing the number of degrees of freedom, overarching view
  • -
  • Preprocessing our data
  • -
  • Functionality in Scikit-Learn
  • -
  • More preprocessing
  • -
  • Frequently used scaling functions
  • -
  • Example of own Standard scaling
  • -
  • Min-Max Scaling
  • -
  • Testing the Means Squared Error as function of Complexity
  • -
  • More preprocessing examples, Franke function and regression
  • -
  • Mathematical Interpretation of Ordinary Least Squares
  • -
  • Residual Error
  • -
  • Simple case
  • -
  • The singular value decomposition
  • -
  • Linear Regression Problems
  • -
  • Fixing the singularity
  • -
  • Basic math of the SVD
  • -
  • The SVD, a Fantastic Algorithm
  • -
  • Economy-size SVD
  • -
  • Codes for the SVD
  • -
  • Note about SVD Calculations
  • -
  • Mathematics of the SVD and implications
  • -
  • Example Matrix
  • -
  • Setting up the Matrix to be inverted
  • -
  • Further properties (important for our analyses later)
  • -
  • Meet the Covariance Matrix
  • -
  • Introducing the Covariance and Correlation functions
  • -
  • Covariance and Correlation Matrix
  • -
  • Correlation Function and Design/Feature Matrix
  • -
  • Covariance Matrix Examples
  • -
  • Correlation Matrix
  • -
  • Correlation Matrix with Pandas
  • -
  • Correlation Matrix with Pandas and the Franke function
  • -
  • Rewriting the Covariance and/or Correlation Matrix
  • -
  • Linking with the SVD
  • -
  • What does it mean?
  • -
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • -
  • Ridge and LASSO Regression
  • -
  • Deriving the Ridge Regression Equations
  • -
  • Interpreting the Ridge results
  • -
  • More interpretations
  • -
  • Deriving the Lasso Regression Equations
  • -
  • Exercises for week 35
  • -
  • Exercise 1: Setting up various Python environments
  • -
  • Exercise 2: making your own data and exploring scikit-learn
  • -
  • Exercise 3: Normalizing our data
  • -
  • Exercise 4: Adding Ridge Regression
  • -
  • Exercise 5: Analytical exercises
  • +
  • The Jacobian
  • +
  • Derivatives, example 1
  • +
  • Meet the Hessian Matrix
  • +
  • Interpretations and optimizing our parameters
  • +
  • Own code for Ordinary Least Squares
  • +
  • Adding error analysis and training set up
  • +
  • Splitting our Data in Training and Test data
  • +
  • Examples
  • +
  • Making your own test-train splitting
  • +
  • The Boston housing data example
  • +
  • Housing data, the code
  • +
  • Reducing the number of degrees of freedom, overarching view
  • +
  • Preprocessing our data
  • +
  • Functionality in Scikit-Learn
  • +
  • More preprocessing
  • +
  • Frequently used scaling functions
  • +
  • Example of own Standard scaling
  • +
  • Min-Max Scaling
  • +
  • Testing the Means Squared Error as function of Complexity
  • +
  • More preprocessing examples, Franke function and regression
  • +
  • Material for lecture Thursday, August 31
  • +
  • Mathematical Interpretation of Ordinary Least Squares
  • +
  • Residual Error
  • +
  • Simple case
  • +
  • The singular value decomposition
  • +
  • Linear Regression Problems
  • +
  • Fixing the singularity
  • +
  • Basic math of the SVD
  • +
  • The SVD, a Fantastic Algorithm
  • +
  • Economy-size SVD
  • +
  • Codes for the SVD
  • +
  • Note about SVD Calculations
  • +
  • Mathematics of the SVD and implications
  • +
  • Example Matrix
  • +
  • Setting up the Matrix to be inverted
  • +
  • Further properties (important for our analyses later)
  • +
  • Meet the Covariance Matrix
  • +
  • Introducing the Covariance and Correlation functions
  • +
  • Covariance and Correlation Matrix
  • +
  • Correlation Function and Design/Feature Matrix
  • +
  • Covariance Matrix Examples
  • +
  • Correlation Matrix
  • +
  • Correlation Matrix with Pandas
  • +
  • Correlation Matrix with Pandas and the Franke function
  • +
  • Rewriting the Covariance and/or Correlation Matrix
  • +
  • Linking with the SVD
  • +
  • What does it mean?
  • +
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • +
  • Ridge and LASSO Regression
  • +
  • Deriving the Ridge Regression Equations
  • +
  • Interpreting the Ridge results
  • +
  • More interpretations
  • +
  • Deriving the Lasso Regression Equations
  • +
  • Exercises for week 35
  • +
  • Exercise 1: Setting up various Python environments
  • +
  • Exercise 2: making your own data and exploring scikit-learn
  • +
  • Exercise 3: Normalizing our data
  • +
  • Exercise 4: Adding Ridge Regression
  • +
  • Exercise 5: Analytical exercises
  • @@ -382,38 +391,27 @@ MathJax.Hub.Config({

     

     

     

    -

    Mathematics of the SVD and implications

    +

    Economy-size SVD

    -

    Let us take a closer look at the mathematics of the SVD and the various implications for machine learning studies.

    +

    If we assume that \( n > p \), then our matrix \( \boldsymbol{U} \) has dimension \( n +\times n \). The last \( n-p \) columns of \( \boldsymbol{U} \) become however +irrelevant in our calculations since they are multiplied with the +zeros in \( \boldsymbol{\Sigma} \). +

    -

    Our starting point is our design matrix \( \boldsymbol{X} \) of dimension \( n\times p \)

    -$$ -\boldsymbol{X}=\begin{bmatrix} -x_{0,0} & x_{0,1} & x_{0,2}& \dots & \dots x_{0,p-1}\\ -x_{1,0} & x_{1,1} & x_{1,2}& \dots & \dots x_{1,p-1}\\ -x_{2,0} & x_{2,1} & x_{2,2}& \dots & \dots x_{2,p-1}\\ -\dots & \dots & \dots & \dots \dots & \dots \\ -x_{n-2,0} & x_{n-2,1} & x_{n-2,2}& \dots & \dots x_{n-2,p-1}\\ -x_{n-1,0} & x_{n-1,1} & x_{n-1,2}& \dots & \dots x_{n-1,p-1}\\ -\end{bmatrix}. -$$ +

    The economy-size decomposition removes extra rows or columns of zeros +from the diagonal matrix of singular values, \( \boldsymbol{\Sigma} \), along with the columns +in either \( \boldsymbol{U} \) or \( \boldsymbol{V} \) that multiply those zeros in the expression. +Removing these zeros and columns can improve execution time +and reduce storage requirements without compromising the accuracy of +the decomposition. +

    -

    We can SVD decompose our matrix as

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

    where \( \boldsymbol{U} \) is an orthogonal matrix of dimension \( n\times n \), meaning that \( \boldsymbol{U}\boldsymbol{U}^T=\boldsymbol{U}^T\boldsymbol{U}=\boldsymbol{I}_n \). Here \( \boldsymbol{I}_n \) is the unit matrix of dimension \( n \times n \).

    - -

    Similarly, \( \boldsymbol{V} \) is an orthogonal matrix of dimension \( p\times p \), meaning that \( \boldsymbol{V}\boldsymbol{V}^T=\boldsymbol{V}^T\boldsymbol{V}=\boldsymbol{I}_p \). Here \( \boldsymbol{I}_n \) is the unit matrix of dimension \( p \times p \).

    - -

    Finally \( \boldsymbol{\Sigma} \) contains the singular values \( \sigma_i \). This matrix has dimension \( n\times p \) and the singular values \( \sigma_i \) are all positive. The non-zero values are ordered in descending order, that is

    - -$$ -\sigma_0 > \sigma_1 > \sigma_2 > \dots > \sigma_{p-1} > 0. -$$ - -

    All values beyond \( p-1 \) are all zero.

    +

    If \( n > p \), we keep only the first \( p \) columns of \( \boldsymbol{U} \) and \( \boldsymbol{\Sigma} \) has dimension \( p\times p \). +If \( p > n \), then only the first \( n \) columns of \( \boldsymbol{V} \) are computed and \( \boldsymbol{\Sigma} \) has dimension \( n\times n \). +The \( n=p \) case is obvious, we retain the full SVD. +In general the economy-size SVD leads to less FLOPS and still conserving the desired accuracy. +

    @@ -440,7 +438,7 @@ $$

  • 55
  • 56
  • ...
  • -
  • 68
  • +
  • 71
  • »
  • diff --git a/doc/pub/week35/html/._week35-bs047.html b/doc/pub/week35/html/._week35-bs047.html index c089b6d2e..2483a8e93 100644 --- a/doc/pub/week35/html/._week35-bs047.html +++ b/doc/pub/week35/html/._week35-bs047.html @@ -74,10 +74,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'optimizing-our-parameters'), - ('Our model for the nuclear binding energies', + ('Examples relevant for the exercises', 2, None, - 'our-model-for-the-nuclear-binding-energies'), + 'examples-relevant-for-the-exercises'), ('Optimizing our parameters, more details', 2, None, @@ -94,6 +94,8 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'some-useful-matrix-and-vector-expressions'), + ('The Jacobian', 2, None, 'the-jacobian'), + ('Derivatives, example 1', 2, None, 'derivatives-example-1'), ('Meet the Hessian Matrix', 2, None, 'meet-the-hessian-matrix'), ('Interpretations and optimizing our parameters', 2, @@ -148,6 +150,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'more-preprocessing-examples-franke-function-and-regression'), + ('Material for lecture Thursday, August 31', + 2, + None, + 'material-for-lecture-thursday-august-31'), ('Mathematical Interpretation of Ordinary Least Squares', 2, None, @@ -303,74 +309,77 @@ MathJax.Hub.Config({
  • Why Linear Regression (aka Ordinary Least Squares and family), repeat from last week
  • Regression analysis, overarching aims
  • Regression analysis, overarching aims II
  • -
  • Examples
  • +
  • Examples
  • General linear models
  • Rewriting the fitting procedure as a linear algebra problem
  • Rewriting the fitting procedure as a linear algebra problem, more details
  • Generalizing the fitting procedure as a linear algebra problem
  • Generalizing the fitting procedure as a linear algebra problem
  • Optimizing our parameters
  • -
  • Our model for the nuclear binding energies
  • +
  • Examples relevant for the exercises
  • Optimizing our parameters, more details
  • -
  • Interpretations and optimizing our parameters
  • -
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • Some useful matrix and vector expressions
  • -
  • Meet the Hessian Matrix
  • -
  • Interpretations and optimizing our parameters
  • -
  • Own code for Ordinary Least Squares
  • -
  • Adding error analysis and training set up
  • -
  • Splitting our Data in Training and Test data
  • -
  • Examples
  • -
  • Making your own test-train splitting
  • -
  • The Boston housing data example
  • -
  • Housing data, the code
  • -
  • Reducing the number of degrees of freedom, overarching view
  • -
  • Preprocessing our data
  • -
  • Functionality in Scikit-Learn
  • -
  • More preprocessing
  • -
  • Frequently used scaling functions
  • -
  • Example of own Standard scaling
  • -
  • Min-Max Scaling
  • -
  • Testing the Means Squared Error as function of Complexity
  • -
  • More preprocessing examples, Franke function and regression
  • -
  • Mathematical Interpretation of Ordinary Least Squares
  • -
  • Residual Error
  • -
  • Simple case
  • -
  • The singular value decomposition
  • -
  • Linear Regression Problems
  • -
  • Fixing the singularity
  • -
  • Basic math of the SVD
  • -
  • The SVD, a Fantastic Algorithm
  • -
  • Economy-size SVD
  • -
  • Codes for the SVD
  • -
  • Note about SVD Calculations
  • -
  • Mathematics of the SVD and implications
  • -
  • Example Matrix
  • -
  • Setting up the Matrix to be inverted
  • -
  • Further properties (important for our analyses later)
  • -
  • Meet the Covariance Matrix
  • -
  • Introducing the Covariance and Correlation functions
  • -
  • Covariance and Correlation Matrix
  • -
  • Correlation Function and Design/Feature Matrix
  • -
  • Covariance Matrix Examples
  • -
  • Correlation Matrix
  • -
  • Correlation Matrix with Pandas
  • -
  • Correlation Matrix with Pandas and the Franke function
  • -
  • Rewriting the Covariance and/or Correlation Matrix
  • -
  • Linking with the SVD
  • -
  • What does it mean?
  • -
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • -
  • Ridge and LASSO Regression
  • -
  • Deriving the Ridge Regression Equations
  • -
  • Interpreting the Ridge results
  • -
  • More interpretations
  • -
  • Deriving the Lasso Regression Equations
  • -
  • Exercises for week 35
  • -
  • Exercise 1: Setting up various Python environments
  • -
  • Exercise 2: making your own data and exploring scikit-learn
  • -
  • Exercise 3: Normalizing our data
  • -
  • Exercise 4: Adding Ridge Regression
  • -
  • Exercise 5: Analytical exercises
  • +
  • The Jacobian
  • +
  • Derivatives, example 1
  • +
  • Meet the Hessian Matrix
  • +
  • Interpretations and optimizing our parameters
  • +
  • Own code for Ordinary Least Squares
  • +
  • Adding error analysis and training set up
  • +
  • Splitting our Data in Training and Test data
  • +
  • Examples
  • +
  • Making your own test-train splitting
  • +
  • The Boston housing data example
  • +
  • Housing data, the code
  • +
  • Reducing the number of degrees of freedom, overarching view
  • +
  • Preprocessing our data
  • +
  • Functionality in Scikit-Learn
  • +
  • More preprocessing
  • +
  • Frequently used scaling functions
  • +
  • Example of own Standard scaling
  • +
  • Min-Max Scaling
  • +
  • Testing the Means Squared Error as function of Complexity
  • +
  • More preprocessing examples, Franke function and regression
  • +
  • Material for lecture Thursday, August 31
  • +
  • Mathematical Interpretation of Ordinary Least Squares
  • +
  • Residual Error
  • +
  • Simple case
  • +
  • The singular value decomposition
  • +
  • Linear Regression Problems
  • +
  • Fixing the singularity
  • +
  • Basic math of the SVD
  • +
  • The SVD, a Fantastic Algorithm
  • +
  • Economy-size SVD
  • +
  • Codes for the SVD
  • +
  • Note about SVD Calculations
  • +
  • Mathematics of the SVD and implications
  • +
  • Example Matrix
  • +
  • Setting up the Matrix to be inverted
  • +
  • Further properties (important for our analyses later)
  • +
  • Meet the Covariance Matrix
  • +
  • Introducing the Covariance and Correlation functions
  • +
  • Covariance and Correlation Matrix
  • +
  • Correlation Function and Design/Feature Matrix
  • +
  • Covariance Matrix Examples
  • +
  • Correlation Matrix
  • +
  • Correlation Matrix with Pandas
  • +
  • Correlation Matrix with Pandas and the Franke function
  • +
  • Rewriting the Covariance and/or Correlation Matrix
  • +
  • Linking with the SVD
  • +
  • What does it mean?
  • +
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • +
  • Ridge and LASSO Regression
  • +
  • Deriving the Ridge Regression Equations
  • +
  • Interpreting the Ridge results
  • +
  • More interpretations
  • +
  • Deriving the Lasso Regression Equations
  • +
  • Exercises for week 35
  • +
  • Exercise 1: Setting up various Python environments
  • +
  • Exercise 2: making your own data and exploring scikit-learn
  • +
  • Exercise 3: Normalizing our data
  • +
  • Exercise 4: Adding Ridge Regression
  • +
  • Exercise 5: Analytical exercises
  • @@ -382,61 +391,67 @@ MathJax.Hub.Config({

     

     

     

    -

    Example Matrix

    +

    Codes for the SVD

    -

    As an example, consider the following \( 3\times 2 \) example for the matrix \( \boldsymbol{\Sigma} \)

    -$$ -\boldsymbol{\Sigma}= -\begin{bmatrix} -2& 0 \\ -0 & 1 \\ -0 & 0 \\ -\end{bmatrix} -$$ + +
    +
    +
    +
    +
    +
    import numpy as np
    +# SVD inversion
    +def SVD(A):
    +    ''' Takes as input a numpy matrix A and returns inv(A) based on singular value decomposition (SVD).
    +    SVD is numerically more stable than the inversion algorithms provided by
    +    numpy and scipy.linalg at the cost of being slower.
    +    '''
    +    U, S, VT = np.linalg.svd(A,full_matrices=True)
    +    print('test U')
    +    print( (np.transpose(U) @ U - U @np.transpose(U)))
    +    print('test VT')
    +    print( (np.transpose(VT) @ VT - VT @np.transpose(VT)))
    +    print(U)
    +    print(S)
    +    print(VT)
     
    -

    The singular values are \( \sigma_0=2 \) and \( \sigma_1=1 \). It is common to rewrite the matrix \( \boldsymbol{\Sigma} \) as

    + D = np.zeros((len(U),len(VT))) + for i in range(0,len(VT)): + D[i,i]=S[i] + return U @ D @ VT -$$ -\boldsymbol{\Sigma}= -\begin{bmatrix} -\boldsymbol{\tilde{\Sigma}}\\ -\boldsymbol{0}\\ -\end{bmatrix}, -$$ -

    where

    -$$ -\boldsymbol{\tilde{\Sigma}}= -\begin{bmatrix} -2& 0 \\ -0 & 1 \\ -\end{bmatrix}, -$$ +X = np.array([ [1.0,-1.0], [1.0,-1.0]]) +#X = np.array([[1, 2], [3, 4], [5, 6]]) -

    contains only the singular values. Note also (and we will use this below) that

    +print(X) +C = SVD(X) +# Print the difference between the original matrix and the SVD one +print(C-X) +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    -$$ -\boldsymbol{\Sigma}^T\boldsymbol{\Sigma}= -\begin{bmatrix} -4& 0 \\ -0 & 1 \\ -\end{bmatrix}, -$$ - -

    which is a \( 2\times 2 \) matrix while

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

    is a \( 3\times 3 \) matrix. The last row and column of this last matrix -contain only zeros. This will have important consequences for our SVD -decomposition of the design matrix. +

    The matrix \( \boldsymbol{X} \) has columns that are linearly dependent. The first +column is the row-wise sum of the other two columns. The rank of a +matrix (the column rank) is the dimension of space spanned by the +column vectors. The rank of the matrix is the number of linearly +independent columns, in this case just \( 2 \). We see this from the +singular values when running the above code. Running the standard +inversion algorithm for matrix inversion with \( \boldsymbol{X}^T\boldsymbol{X} \) results +in the program terminating due to a singular matrix.

    @@ -464,7 +479,7 @@ decomposition of the design matrix.

  • 56
  • 57
  • ...
  • -
  • 68
  • +
  • 71
  • »
  • diff --git a/doc/pub/week35/html/._week35-bs048.html b/doc/pub/week35/html/._week35-bs048.html index 46d7cbb48..d2459b164 100644 --- a/doc/pub/week35/html/._week35-bs048.html +++ b/doc/pub/week35/html/._week35-bs048.html @@ -74,10 +74,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'optimizing-our-parameters'), - ('Our model for the nuclear binding energies', + ('Examples relevant for the exercises', 2, None, - 'our-model-for-the-nuclear-binding-energies'), + 'examples-relevant-for-the-exercises'), ('Optimizing our parameters, more details', 2, None, @@ -94,6 +94,8 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'some-useful-matrix-and-vector-expressions'), + ('The Jacobian', 2, None, 'the-jacobian'), + ('Derivatives, example 1', 2, None, 'derivatives-example-1'), ('Meet the Hessian Matrix', 2, None, 'meet-the-hessian-matrix'), ('Interpretations and optimizing our parameters', 2, @@ -148,6 +150,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'more-preprocessing-examples-franke-function-and-regression'), + ('Material for lecture Thursday, August 31', + 2, + None, + 'material-for-lecture-thursday-august-31'), ('Mathematical Interpretation of Ordinary Least Squares', 2, None, @@ -303,74 +309,77 @@ MathJax.Hub.Config({
  • Why Linear Regression (aka Ordinary Least Squares and family), repeat from last week
  • Regression analysis, overarching aims
  • Regression analysis, overarching aims II
  • -
  • Examples
  • +
  • Examples
  • General linear models
  • Rewriting the fitting procedure as a linear algebra problem
  • Rewriting the fitting procedure as a linear algebra problem, more details
  • Generalizing the fitting procedure as a linear algebra problem
  • Generalizing the fitting procedure as a linear algebra problem
  • Optimizing our parameters
  • -
  • Our model for the nuclear binding energies
  • +
  • Examples relevant for the exercises
  • Optimizing our parameters, more details
  • -
  • Interpretations and optimizing our parameters
  • -
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • Some useful matrix and vector expressions
  • -
  • Meet the Hessian Matrix
  • -
  • Interpretations and optimizing our parameters
  • -
  • Own code for Ordinary Least Squares
  • -
  • Adding error analysis and training set up
  • -
  • Splitting our Data in Training and Test data
  • -
  • Examples
  • -
  • Making your own test-train splitting
  • -
  • The Boston housing data example
  • -
  • Housing data, the code
  • -
  • Reducing the number of degrees of freedom, overarching view
  • -
  • Preprocessing our data
  • -
  • Functionality in Scikit-Learn
  • -
  • More preprocessing
  • -
  • Frequently used scaling functions
  • -
  • Example of own Standard scaling
  • -
  • Min-Max Scaling
  • -
  • Testing the Means Squared Error as function of Complexity
  • -
  • More preprocessing examples, Franke function and regression
  • -
  • Mathematical Interpretation of Ordinary Least Squares
  • -
  • Residual Error
  • -
  • Simple case
  • -
  • The singular value decomposition
  • -
  • Linear Regression Problems
  • -
  • Fixing the singularity
  • -
  • Basic math of the SVD
  • -
  • The SVD, a Fantastic Algorithm
  • -
  • Economy-size SVD
  • -
  • Codes for the SVD
  • -
  • Note about SVD Calculations
  • -
  • Mathematics of the SVD and implications
  • -
  • Example Matrix
  • -
  • Setting up the Matrix to be inverted
  • -
  • Further properties (important for our analyses later)
  • -
  • Meet the Covariance Matrix
  • -
  • Introducing the Covariance and Correlation functions
  • -
  • Covariance and Correlation Matrix
  • -
  • Correlation Function and Design/Feature Matrix
  • -
  • Covariance Matrix Examples
  • -
  • Correlation Matrix
  • -
  • Correlation Matrix with Pandas
  • -
  • Correlation Matrix with Pandas and the Franke function
  • -
  • Rewriting the Covariance and/or Correlation Matrix
  • -
  • Linking with the SVD
  • -
  • What does it mean?
  • -
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • -
  • Ridge and LASSO Regression
  • -
  • Deriving the Ridge Regression Equations
  • -
  • Interpreting the Ridge results
  • -
  • More interpretations
  • -
  • Deriving the Lasso Regression Equations
  • -
  • Exercises for week 35
  • -
  • Exercise 1: Setting up various Python environments
  • -
  • Exercise 2: making your own data and exploring scikit-learn
  • -
  • Exercise 3: Normalizing our data
  • -
  • Exercise 4: Adding Ridge Regression
  • -
  • Exercise 5: Analytical exercises
  • +
  • The Jacobian
  • +
  • Derivatives, example 1
  • +
  • Meet the Hessian Matrix
  • +
  • Interpretations and optimizing our parameters
  • +
  • Own code for Ordinary Least Squares
  • +
  • Adding error analysis and training set up
  • +
  • Splitting our Data in Training and Test data
  • +
  • Examples
  • +
  • Making your own test-train splitting
  • +
  • The Boston housing data example
  • +
  • Housing data, the code
  • +
  • Reducing the number of degrees of freedom, overarching view
  • +
  • Preprocessing our data
  • +
  • Functionality in Scikit-Learn
  • +
  • More preprocessing
  • +
  • Frequently used scaling functions
  • +
  • Example of own Standard scaling
  • +
  • Min-Max Scaling
  • +
  • Testing the Means Squared Error as function of Complexity
  • +
  • More preprocessing examples, Franke function and regression
  • +
  • Material for lecture Thursday, August 31
  • +
  • Mathematical Interpretation of Ordinary Least Squares
  • +
  • Residual Error
  • +
  • Simple case
  • +
  • The singular value decomposition
  • +
  • Linear Regression Problems
  • +
  • Fixing the singularity
  • +
  • Basic math of the SVD
  • +
  • The SVD, a Fantastic Algorithm
  • +
  • Economy-size SVD
  • +
  • Codes for the SVD
  • +
  • Note about SVD Calculations
  • +
  • Mathematics of the SVD and implications
  • +
  • Example Matrix
  • +
  • Setting up the Matrix to be inverted
  • +
  • Further properties (important for our analyses later)
  • +
  • Meet the Covariance Matrix
  • +
  • Introducing the Covariance and Correlation functions
  • +
  • Covariance and Correlation Matrix
  • +
  • Correlation Function and Design/Feature Matrix
  • +
  • Covariance Matrix Examples
  • +
  • Correlation Matrix
  • +
  • Correlation Matrix with Pandas
  • +
  • Correlation Matrix with Pandas and the Franke function
  • +
  • Rewriting the Covariance and/or Correlation Matrix
  • +
  • Linking with the SVD
  • +
  • What does it mean?
  • +
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • +
  • Ridge and LASSO Regression
  • +
  • Deriving the Ridge Regression Equations
  • +
  • Interpreting the Ridge results
  • +
  • More interpretations
  • +
  • Deriving the Lasso Regression Equations
  • +
  • Exercises for week 35
  • +
  • Exercise 1: Setting up various Python environments
  • +
  • Exercise 2: making your own data and exploring scikit-learn
  • +
  • Exercise 3: Normalizing our data
  • +
  • Exercise 4: Adding Ridge Regression
  • +
  • Exercise 5: Analytical exercises
  • @@ -382,45 +391,22 @@ MathJax.Hub.Config({

     

     

     

    -

    Setting up the Matrix to be inverted

    +

    Note about SVD Calculations

    -

    The matrix that may cause problems for us is \( \boldsymbol{X}^T\boldsymbol{X} \). Using the SVD we can rewrite this matrix as

    +

    The \( U \), \( S \), and \( V \) matrices returned from the svd() function +cannot be multiplied directly. +

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

    As you can see from the code, the \( S \) vector must be converted into a +diagonal matrix. This may cause a problem as the size of the matrices +do not fit the rules of matrix multiplication, where the number of +columns in a matrix must match the number of rows in the subsequent +matrix. +

    -

    and using the orthogonality of the matrix \( \boldsymbol{U} \) we have

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

    We define \( \boldsymbol{\Sigma}^T\boldsymbol{\Sigma}=\tilde{\boldsymbol{\Sigma}}^2 \) which is a diagonal matrix containing only the singular values squared. It has dimensionality \( p \times p \).

    - -

    We can now insert the result for the matrix \( \boldsymbol{X}^T\boldsymbol{X} \) into our equation for ordinary least squares where

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

    and using our SVD decomposition of \( \boldsymbol{X} \) we have

    - -$$ -\tilde{y}_{\mathrm{OLS}}=\boldsymbol{U}\boldsymbol{\Sigma}\boldsymbol{V}^T\left(\boldsymbol{V}\tilde{\boldsymbol{\Sigma}}^{2}(\boldsymbol{V}^T\right)^{-1}\boldsymbol{V}\boldsymbol{\Sigma}^T\boldsymbol{U}^T\boldsymbol{y}, -$$ - -

    which gives us, using the orthogonality of the matrices \( \boldsymbol{U} \) and \( \boldsymbol{V} \),

    - -$$ -\tilde{y}_{\mathrm{OLS}}=\boldsymbol{U}\boldsymbol{U}^T\boldsymbol{y}=\sum_{i=0}^{p-1}\boldsymbol{u}_i\boldsymbol{u}^T_i\boldsymbol{y}, -$$ - -

    It means that the ordinary least square model (with the optimal -parameters) \( \boldsymbol{\tilde{y}} \), corresponds to an orthogonal -transformation of the output (or target) vector \( \boldsymbol{y} \) by the -vectors of the matrix \( \boldsymbol{U} \). Note that the summation ends at \( p-1 \), -that is \( \boldsymbol{\tilde{y}}\ne \boldsymbol{y} \). +

    If you wish to include the zero singular values, you will need to +resize the matrices and set up a diagonal matrix as done in the above +example

    @@ -448,7 +434,7 @@ that is \( \boldsymbol{\tilde{y}}\ne \boldsymbol{y} \).

  • 57
  • 58
  • ...
  • -
  • 68
  • +
  • 71
  • »
  • diff --git a/doc/pub/week35/html/._week35-bs049.html b/doc/pub/week35/html/._week35-bs049.html index 59a199607..33801fb72 100644 --- a/doc/pub/week35/html/._week35-bs049.html +++ b/doc/pub/week35/html/._week35-bs049.html @@ -74,10 +74,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'optimizing-our-parameters'), - ('Our model for the nuclear binding energies', + ('Examples relevant for the exercises', 2, None, - 'our-model-for-the-nuclear-binding-energies'), + 'examples-relevant-for-the-exercises'), ('Optimizing our parameters, more details', 2, None, @@ -94,6 +94,8 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'some-useful-matrix-and-vector-expressions'), + ('The Jacobian', 2, None, 'the-jacobian'), + ('Derivatives, example 1', 2, None, 'derivatives-example-1'), ('Meet the Hessian Matrix', 2, None, 'meet-the-hessian-matrix'), ('Interpretations and optimizing our parameters', 2, @@ -148,6 +150,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'more-preprocessing-examples-franke-function-and-regression'), + ('Material for lecture Thursday, August 31', + 2, + None, + 'material-for-lecture-thursday-august-31'), ('Mathematical Interpretation of Ordinary Least Squares', 2, None, @@ -303,74 +309,77 @@ MathJax.Hub.Config({
  • Why Linear Regression (aka Ordinary Least Squares and family), repeat from last week
  • Regression analysis, overarching aims
  • Regression analysis, overarching aims II
  • -
  • Examples
  • +
  • Examples
  • General linear models
  • Rewriting the fitting procedure as a linear algebra problem
  • Rewriting the fitting procedure as a linear algebra problem, more details
  • Generalizing the fitting procedure as a linear algebra problem
  • Generalizing the fitting procedure as a linear algebra problem
  • Optimizing our parameters
  • -
  • Our model for the nuclear binding energies
  • +
  • Examples relevant for the exercises
  • Optimizing our parameters, more details
  • -
  • Interpretations and optimizing our parameters
  • -
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • Some useful matrix and vector expressions
  • -
  • Meet the Hessian Matrix
  • -
  • Interpretations and optimizing our parameters
  • -
  • Own code for Ordinary Least Squares
  • -
  • Adding error analysis and training set up
  • -
  • Splitting our Data in Training and Test data
  • -
  • Examples
  • -
  • Making your own test-train splitting
  • -
  • The Boston housing data example
  • -
  • Housing data, the code
  • -
  • Reducing the number of degrees of freedom, overarching view
  • -
  • Preprocessing our data
  • -
  • Functionality in Scikit-Learn
  • -
  • More preprocessing
  • -
  • Frequently used scaling functions
  • -
  • Example of own Standard scaling
  • -
  • Min-Max Scaling
  • -
  • Testing the Means Squared Error as function of Complexity
  • -
  • More preprocessing examples, Franke function and regression
  • -
  • Mathematical Interpretation of Ordinary Least Squares
  • -
  • Residual Error
  • -
  • Simple case
  • -
  • The singular value decomposition
  • -
  • Linear Regression Problems
  • -
  • Fixing the singularity
  • -
  • Basic math of the SVD
  • -
  • The SVD, a Fantastic Algorithm
  • -
  • Economy-size SVD
  • -
  • Codes for the SVD
  • -
  • Note about SVD Calculations
  • -
  • Mathematics of the SVD and implications
  • -
  • Example Matrix
  • -
  • Setting up the Matrix to be inverted
  • -
  • Further properties (important for our analyses later)
  • -
  • Meet the Covariance Matrix
  • -
  • Introducing the Covariance and Correlation functions
  • -
  • Covariance and Correlation Matrix
  • -
  • Correlation Function and Design/Feature Matrix
  • -
  • Covariance Matrix Examples
  • -
  • Correlation Matrix
  • -
  • Correlation Matrix with Pandas
  • -
  • Correlation Matrix with Pandas and the Franke function
  • -
  • Rewriting the Covariance and/or Correlation Matrix
  • -
  • Linking with the SVD
  • -
  • What does it mean?
  • -
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • -
  • Ridge and LASSO Regression
  • -
  • Deriving the Ridge Regression Equations
  • -
  • Interpreting the Ridge results
  • -
  • More interpretations
  • -
  • Deriving the Lasso Regression Equations
  • -
  • Exercises for week 35
  • -
  • Exercise 1: Setting up various Python environments
  • -
  • Exercise 2: making your own data and exploring scikit-learn
  • -
  • Exercise 3: Normalizing our data
  • -
  • Exercise 4: Adding Ridge Regression
  • -
  • Exercise 5: Analytical exercises
  • +
  • The Jacobian
  • +
  • Derivatives, example 1
  • +
  • Meet the Hessian Matrix
  • +
  • Interpretations and optimizing our parameters
  • +
  • Own code for Ordinary Least Squares
  • +
  • Adding error analysis and training set up
  • +
  • Splitting our Data in Training and Test data
  • +
  • Examples
  • +
  • Making your own test-train splitting
  • +
  • The Boston housing data example
  • +
  • Housing data, the code
  • +
  • Reducing the number of degrees of freedom, overarching view
  • +
  • Preprocessing our data
  • +
  • Functionality in Scikit-Learn
  • +
  • More preprocessing
  • +
  • Frequently used scaling functions
  • +
  • Example of own Standard scaling
  • +
  • Min-Max Scaling
  • +
  • Testing the Means Squared Error as function of Complexity
  • +
  • More preprocessing examples, Franke function and regression
  • +
  • Material for lecture Thursday, August 31
  • +
  • Mathematical Interpretation of Ordinary Least Squares
  • +
  • Residual Error
  • +
  • Simple case
  • +
  • The singular value decomposition
  • +
  • Linear Regression Problems
  • +
  • Fixing the singularity
  • +
  • Basic math of the SVD
  • +
  • The SVD, a Fantastic Algorithm
  • +
  • Economy-size SVD
  • +
  • Codes for the SVD
  • +
  • Note about SVD Calculations
  • +
  • Mathematics of the SVD and implications
  • +
  • Example Matrix
  • +
  • Setting up the Matrix to be inverted
  • +
  • Further properties (important for our analyses later)
  • +
  • Meet the Covariance Matrix
  • +
  • Introducing the Covariance and Correlation functions
  • +
  • Covariance and Correlation Matrix
  • +
  • Correlation Function and Design/Feature Matrix
  • +
  • Covariance Matrix Examples
  • +
  • Correlation Matrix
  • +
  • Correlation Matrix with Pandas
  • +
  • Correlation Matrix with Pandas and the Franke function
  • +
  • Rewriting the Covariance and/or Correlation Matrix
  • +
  • Linking with the SVD
  • +
  • What does it mean?
  • +
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • +
  • Ridge and LASSO Regression
  • +
  • Deriving the Ridge Regression Equations
  • +
  • Interpreting the Ridge results
  • +
  • More interpretations
  • +
  • Deriving the Lasso Regression Equations
  • +
  • Exercises for week 35
  • +
  • Exercise 1: Setting up various Python environments
  • +
  • Exercise 2: making your own data and exploring scikit-learn
  • +
  • Exercise 3: Normalizing our data
  • +
  • Exercise 4: Adding Ridge Regression
  • +
  • Exercise 5: Analytical exercises
  • @@ -382,53 +391,38 @@ MathJax.Hub.Config({

     

     

     

    -

    Further properties (important for our analyses later)

    +

    Mathematics of the SVD and implications

    -

    Let us study again \( \boldsymbol{X}^T\boldsymbol{X} \) in terms of our SVD,

    +

    Let us take a closer look at the mathematics of the SVD and the various implications for machine learning studies.

    + +

    Our starting point is our design matrix \( \boldsymbol{X} \) of dimension \( n\times p \)

    $$ -\boldsymbol{X}^T\boldsymbol{X}=\boldsymbol{V}\boldsymbol{\Sigma}^T\boldsymbol{U}^T\boldsymbol{U}\boldsymbol{\Sigma}\boldsymbol{V}^T=\boldsymbol{V}\boldsymbol{\Sigma}^T\boldsymbol{\Sigma}\boldsymbol{V}^T. +\boldsymbol{X}=\begin{bmatrix} +x_{0,0} & x_{0,1} & x_{0,2}& \dots & \dots x_{0,p-1}\\ +x_{1,0} & x_{1,1} & x_{1,2}& \dots & \dots x_{1,p-1}\\ +x_{2,0} & x_{2,1} & x_{2,2}& \dots & \dots x_{2,p-1}\\ +\dots & \dots & \dots & \dots \dots & \dots \\ +x_{n-2,0} & x_{n-2,1} & x_{n-2,2}& \dots & \dots x_{n-2,p-1}\\ +x_{n-1,0} & x_{n-1,1} & x_{n-1,2}& \dots & \dots x_{n-1,p-1}\\ +\end{bmatrix}. $$ -

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

    +

    We can SVD decompose our matrix as

    $$ -\left(\boldsymbol{X}^T\boldsymbol{X}\right)\boldsymbol{V}=\boldsymbol{V}\boldsymbol{\Sigma}^T\boldsymbol{\Sigma}. +\boldsymbol{X}=\boldsymbol{U}\boldsymbol{\Sigma}\boldsymbol{V}^T, $$ -

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

    +

    where \( \boldsymbol{U} \) is an orthogonal matrix of dimension \( n\times n \), meaning that \( \boldsymbol{U}\boldsymbol{U}^T=\boldsymbol{U}^T\boldsymbol{U}=\boldsymbol{I}_n \). Here \( \boldsymbol{I}_n \) is the unit matrix of dimension \( n \times n \).

    + +

    Similarly, \( \boldsymbol{V} \) is an orthogonal matrix of dimension \( p\times p \), meaning that \( \boldsymbol{V}\boldsymbol{V}^T=\boldsymbol{V}^T\boldsymbol{V}=\boldsymbol{I}_p \). Here \( \boldsymbol{I}_n \) is the unit matrix of dimension \( p \times p \).

    + +

    Finally \( \boldsymbol{\Sigma} \) contains the singular values \( \sigma_i \). This matrix has dimension \( n\times p \) and the singular values \( \sigma_i \) are all positive. The non-zero values are ordered in descending order, that is

    + $$ -\left(\boldsymbol{X}^T\boldsymbol{X}\right)\boldsymbol{v}_i=\boldsymbol{v}_i\sigma_i^2. +\sigma_0 > \sigma_1 > \sigma_2 > \dots > \sigma_{p-1} > 0. $$ -

    Similarly, if we use the SVD decomposition for the matrix \( \boldsymbol{X}\boldsymbol{X}^T \), we have

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

    If we now multiply from the right with \( \boldsymbol{U} \) (using the orthogonality of \( \boldsymbol{U} \)) we get

    -$$ -\left(\boldsymbol{X}\boldsymbol{X}^T\right)\boldsymbol{U}=\boldsymbol{U}\boldsymbol{\Sigma}\boldsymbol{\Sigma}^T. -$$ - -

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

    -$$ -\left(\boldsymbol{X}\boldsymbol{X}^T\right)\boldsymbol{u}_i=\boldsymbol{u}_i\sigma_i^2. -$$ - -

    Important note: we have defined our design matrix \( \boldsymbol{X} \) to be an -\( n\times p \) matrix. In most supervised learning cases we have that \( n -\ge p \), and quite often we have \( n >> p \). For linear algebra based methods like ordinary least squares or Ridge regression, this leads to a matrix \( \boldsymbol{X}^T\boldsymbol{X} \) which is small and thereby easier to handle from a computational point of view (in terms of number of floating point operations). -

    - -

    In our lectures, the number of columns will -always refer to the number of features in our data set, while the -number of rows represents the number of data inputs. Note that in -other texts you may find the opposite notation. This has consequences -for the definition of for example the covariance matrix and its relation to the SVD. -

    +

    All values beyond \( p-1 \) are all zero.

    @@ -455,7 +449,7 @@ for the definition of for example the covariance matrix and its relation to the

  • 58
  • 59
  • ...
  • -
  • 68
  • +
  • 71
  • »
  • diff --git a/doc/pub/week35/html/._week35-bs050.html b/doc/pub/week35/html/._week35-bs050.html index b711a47bc..50865770b 100644 --- a/doc/pub/week35/html/._week35-bs050.html +++ b/doc/pub/week35/html/._week35-bs050.html @@ -74,10 +74,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'optimizing-our-parameters'), - ('Our model for the nuclear binding energies', + ('Examples relevant for the exercises', 2, None, - 'our-model-for-the-nuclear-binding-energies'), + 'examples-relevant-for-the-exercises'), ('Optimizing our parameters, more details', 2, None, @@ -94,6 +94,8 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'some-useful-matrix-and-vector-expressions'), + ('The Jacobian', 2, None, 'the-jacobian'), + ('Derivatives, example 1', 2, None, 'derivatives-example-1'), ('Meet the Hessian Matrix', 2, None, 'meet-the-hessian-matrix'), ('Interpretations and optimizing our parameters', 2, @@ -148,6 +150,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'more-preprocessing-examples-franke-function-and-regression'), + ('Material for lecture Thursday, August 31', + 2, + None, + 'material-for-lecture-thursday-august-31'), ('Mathematical Interpretation of Ordinary Least Squares', 2, None, @@ -303,74 +309,77 @@ MathJax.Hub.Config({
  • Why Linear Regression (aka Ordinary Least Squares and family), repeat from last week
  • Regression analysis, overarching aims
  • Regression analysis, overarching aims II
  • -
  • Examples
  • +
  • Examples
  • General linear models
  • Rewriting the fitting procedure as a linear algebra problem
  • Rewriting the fitting procedure as a linear algebra problem, more details
  • Generalizing the fitting procedure as a linear algebra problem
  • Generalizing the fitting procedure as a linear algebra problem
  • Optimizing our parameters
  • -
  • Our model for the nuclear binding energies
  • +
  • Examples relevant for the exercises
  • Optimizing our parameters, more details
  • -
  • Interpretations and optimizing our parameters
  • -
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • Some useful matrix and vector expressions
  • -
  • Meet the Hessian Matrix
  • -
  • Interpretations and optimizing our parameters
  • -
  • Own code for Ordinary Least Squares
  • -
  • Adding error analysis and training set up
  • -
  • Splitting our Data in Training and Test data
  • -
  • Examples
  • -
  • Making your own test-train splitting
  • -
  • The Boston housing data example
  • -
  • Housing data, the code
  • -
  • Reducing the number of degrees of freedom, overarching view
  • -
  • Preprocessing our data
  • -
  • Functionality in Scikit-Learn
  • -
  • More preprocessing
  • -
  • Frequently used scaling functions
  • -
  • Example of own Standard scaling
  • -
  • Min-Max Scaling
  • -
  • Testing the Means Squared Error as function of Complexity
  • -
  • More preprocessing examples, Franke function and regression
  • -
  • Mathematical Interpretation of Ordinary Least Squares
  • -
  • Residual Error
  • -
  • Simple case
  • -
  • The singular value decomposition
  • -
  • Linear Regression Problems
  • -
  • Fixing the singularity
  • -
  • Basic math of the SVD
  • -
  • The SVD, a Fantastic Algorithm
  • -
  • Economy-size SVD
  • -
  • Codes for the SVD
  • -
  • Note about SVD Calculations
  • -
  • Mathematics of the SVD and implications
  • -
  • Example Matrix
  • -
  • Setting up the Matrix to be inverted
  • -
  • Further properties (important for our analyses later)
  • -
  • Meet the Covariance Matrix
  • -
  • Introducing the Covariance and Correlation functions
  • -
  • Covariance and Correlation Matrix
  • -
  • Correlation Function and Design/Feature Matrix
  • -
  • Covariance Matrix Examples
  • -
  • Correlation Matrix
  • -
  • Correlation Matrix with Pandas
  • -
  • Correlation Matrix with Pandas and the Franke function
  • -
  • Rewriting the Covariance and/or Correlation Matrix
  • -
  • Linking with the SVD
  • -
  • What does it mean?
  • -
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • -
  • Ridge and LASSO Regression
  • -
  • Deriving the Ridge Regression Equations
  • -
  • Interpreting the Ridge results
  • -
  • More interpretations
  • -
  • Deriving the Lasso Regression Equations
  • -
  • Exercises for week 35
  • -
  • Exercise 1: Setting up various Python environments
  • -
  • Exercise 2: making your own data and exploring scikit-learn
  • -
  • Exercise 3: Normalizing our data
  • -
  • Exercise 4: Adding Ridge Regression
  • -
  • Exercise 5: Analytical exercises
  • +
  • The Jacobian
  • +
  • Derivatives, example 1
  • +
  • Meet the Hessian Matrix
  • +
  • Interpretations and optimizing our parameters
  • +
  • Own code for Ordinary Least Squares
  • +
  • Adding error analysis and training set up
  • +
  • Splitting our Data in Training and Test data
  • +
  • Examples
  • +
  • Making your own test-train splitting
  • +
  • The Boston housing data example
  • +
  • Housing data, the code
  • +
  • Reducing the number of degrees of freedom, overarching view
  • +
  • Preprocessing our data
  • +
  • Functionality in Scikit-Learn
  • +
  • More preprocessing
  • +
  • Frequently used scaling functions
  • +
  • Example of own Standard scaling
  • +
  • Min-Max Scaling
  • +
  • Testing the Means Squared Error as function of Complexity
  • +
  • More preprocessing examples, Franke function and regression
  • +
  • Material for lecture Thursday, August 31
  • +
  • Mathematical Interpretation of Ordinary Least Squares
  • +
  • Residual Error
  • +
  • Simple case
  • +
  • The singular value decomposition
  • +
  • Linear Regression Problems
  • +
  • Fixing the singularity
  • +
  • Basic math of the SVD
  • +
  • The SVD, a Fantastic Algorithm
  • +
  • Economy-size SVD
  • +
  • Codes for the SVD
  • +
  • Note about SVD Calculations
  • +
  • Mathematics of the SVD and implications
  • +
  • Example Matrix
  • +
  • Setting up the Matrix to be inverted
  • +
  • Further properties (important for our analyses later)
  • +
  • Meet the Covariance Matrix
  • +
  • Introducing the Covariance and Correlation functions
  • +
  • Covariance and Correlation Matrix
  • +
  • Correlation Function and Design/Feature Matrix
  • +
  • Covariance Matrix Examples
  • +
  • Correlation Matrix
  • +
  • Correlation Matrix with Pandas
  • +
  • Correlation Matrix with Pandas and the Franke function
  • +
  • Rewriting the Covariance and/or Correlation Matrix
  • +
  • Linking with the SVD
  • +
  • What does it mean?
  • +
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • +
  • Ridge and LASSO Regression
  • +
  • Deriving the Ridge Regression Equations
  • +
  • Interpreting the Ridge results
  • +
  • More interpretations
  • +
  • Deriving the Lasso Regression Equations
  • +
  • Exercises for week 35
  • +
  • Exercise 1: Setting up various Python environments
  • +
  • Exercise 2: making your own data and exploring scikit-learn
  • +
  • Exercise 3: Normalizing our data
  • +
  • Exercise 4: Adding Ridge Regression
  • +
  • Exercise 5: Analytical exercises
  • @@ -382,31 +391,61 @@ MathJax.Hub.Config({

     

     

     

    -

    Meet the Covariance Matrix

    +

    Example Matrix

    -

    Before we move on to a discussion of Ridge and Lasso regression, we want to show an important example of the above.

    - -

    We have already noted that the matrix \( \boldsymbol{X}^T\boldsymbol{X} \) in ordinary -least squares is proportional to the second derivative of the cost -function, that is we have -

    +

    As an example, consider the following \( 3\times 2 \) example for the matrix \( \boldsymbol{\Sigma} \)

    $$ -\frac{\partial^2 C(\boldsymbol{\beta})}{\partial \boldsymbol{\beta}^T\partial \boldsymbol{\beta}} =\frac{2}{n}\boldsymbol{X}^T\boldsymbol{X}. +\boldsymbol{\Sigma}= +\begin{bmatrix} +2& 0 \\ +0 & 1 \\ +0 & 0 \\ +\end{bmatrix} $$ -

    This quantity defines was what is called the Hessian matrix (the second derivative of a function we want to optimize).

    - -

    The Hessian matrix plays an important role and is defined in this course as

    +

    The singular values are \( \sigma_0=2 \) and \( \sigma_1=1 \). It is common to rewrite the matrix \( \boldsymbol{\Sigma} \) as

    $$ -\boldsymbol{H}=\boldsymbol{X}^T\boldsymbol{X}. +\boldsymbol{\Sigma}= +\begin{bmatrix} +\boldsymbol{\tilde{\Sigma}}\\ +\boldsymbol{0}\\ +\end{bmatrix}, $$ -

    The Hessian matrix for ordinary least squares is also proportional to -the covariance matrix. This means also that we can use the SVD to find -the eigenvalues of the covariance matrix and the Hessian matrix in -terms of the singular values. Let us develop these arguments, as they will play an important role in our machine learning studies. +

    where

    +$$ +\boldsymbol{\tilde{\Sigma}}= +\begin{bmatrix} +2& 0 \\ +0 & 1 \\ +\end{bmatrix}, +$$ + +

    contains only the singular values. Note also (and we will use this below) that

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

    which is a \( 2\times 2 \) matrix while

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

    is a \( 3\times 3 \) matrix. The last row and column of this last matrix +contain only zeros. This will have important consequences for our SVD +decomposition of the design matrix.

    @@ -434,7 +473,7 @@ terms of the singular values. Let us develop these arguments, as they will pla

  • 59
  • 60
  • ...
  • -
  • 68
  • +
  • 71
  • »
  • diff --git a/doc/pub/week35/html/._week35-bs051.html b/doc/pub/week35/html/._week35-bs051.html index 5678c23c3..61250fd26 100644 --- a/doc/pub/week35/html/._week35-bs051.html +++ b/doc/pub/week35/html/._week35-bs051.html @@ -74,10 +74,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'optimizing-our-parameters'), - ('Our model for the nuclear binding energies', + ('Examples relevant for the exercises', 2, None, - 'our-model-for-the-nuclear-binding-energies'), + 'examples-relevant-for-the-exercises'), ('Optimizing our parameters, more details', 2, None, @@ -94,6 +94,8 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'some-useful-matrix-and-vector-expressions'), + ('The Jacobian', 2, None, 'the-jacobian'), + ('Derivatives, example 1', 2, None, 'derivatives-example-1'), ('Meet the Hessian Matrix', 2, None, 'meet-the-hessian-matrix'), ('Interpretations and optimizing our parameters', 2, @@ -148,6 +150,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'more-preprocessing-examples-franke-function-and-regression'), + ('Material for lecture Thursday, August 31', + 2, + None, + 'material-for-lecture-thursday-august-31'), ('Mathematical Interpretation of Ordinary Least Squares', 2, None, @@ -303,74 +309,77 @@ MathJax.Hub.Config({
  • Why Linear Regression (aka Ordinary Least Squares and family), repeat from last week
  • Regression analysis, overarching aims
  • Regression analysis, overarching aims II
  • -
  • Examples
  • +
  • Examples
  • General linear models
  • Rewriting the fitting procedure as a linear algebra problem
  • Rewriting the fitting procedure as a linear algebra problem, more details
  • Generalizing the fitting procedure as a linear algebra problem
  • Generalizing the fitting procedure as a linear algebra problem
  • Optimizing our parameters
  • -
  • Our model for the nuclear binding energies
  • +
  • Examples relevant for the exercises
  • Optimizing our parameters, more details
  • -
  • Interpretations and optimizing our parameters
  • -
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • Some useful matrix and vector expressions
  • -
  • Meet the Hessian Matrix
  • -
  • Interpretations and optimizing our parameters
  • -
  • Own code for Ordinary Least Squares
  • -
  • Adding error analysis and training set up
  • -
  • Splitting our Data in Training and Test data
  • -
  • Examples
  • -
  • Making your own test-train splitting
  • -
  • The Boston housing data example
  • -
  • Housing data, the code
  • -
  • Reducing the number of degrees of freedom, overarching view
  • -
  • Preprocessing our data
  • -
  • Functionality in Scikit-Learn
  • -
  • More preprocessing
  • -
  • Frequently used scaling functions
  • -
  • Example of own Standard scaling
  • -
  • Min-Max Scaling
  • -
  • Testing the Means Squared Error as function of Complexity
  • -
  • More preprocessing examples, Franke function and regression
  • -
  • Mathematical Interpretation of Ordinary Least Squares
  • -
  • Residual Error
  • -
  • Simple case
  • -
  • The singular value decomposition
  • -
  • Linear Regression Problems
  • -
  • Fixing the singularity
  • -
  • Basic math of the SVD
  • -
  • The SVD, a Fantastic Algorithm
  • -
  • Economy-size SVD
  • -
  • Codes for the SVD
  • -
  • Note about SVD Calculations
  • -
  • Mathematics of the SVD and implications
  • -
  • Example Matrix
  • -
  • Setting up the Matrix to be inverted
  • -
  • Further properties (important for our analyses later)
  • -
  • Meet the Covariance Matrix
  • -
  • Introducing the Covariance and Correlation functions
  • -
  • Covariance and Correlation Matrix
  • -
  • Correlation Function and Design/Feature Matrix
  • -
  • Covariance Matrix Examples
  • -
  • Correlation Matrix
  • -
  • Correlation Matrix with Pandas
  • -
  • Correlation Matrix with Pandas and the Franke function
  • -
  • Rewriting the Covariance and/or Correlation Matrix
  • -
  • Linking with the SVD
  • -
  • What does it mean?
  • -
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • -
  • Ridge and LASSO Regression
  • -
  • Deriving the Ridge Regression Equations
  • -
  • Interpreting the Ridge results
  • -
  • More interpretations
  • -
  • Deriving the Lasso Regression Equations
  • -
  • Exercises for week 35
  • -
  • Exercise 1: Setting up various Python environments
  • -
  • Exercise 2: making your own data and exploring scikit-learn
  • -
  • Exercise 3: Normalizing our data
  • -
  • Exercise 4: Adding Ridge Regression
  • -
  • Exercise 5: Analytical exercises
  • +
  • The Jacobian
  • +
  • Derivatives, example 1
  • +
  • Meet the Hessian Matrix
  • +
  • Interpretations and optimizing our parameters
  • +
  • Own code for Ordinary Least Squares
  • +
  • Adding error analysis and training set up
  • +
  • Splitting our Data in Training and Test data
  • +
  • Examples
  • +
  • Making your own test-train splitting
  • +
  • The Boston housing data example
  • +
  • Housing data, the code
  • +
  • Reducing the number of degrees of freedom, overarching view
  • +
  • Preprocessing our data
  • +
  • Functionality in Scikit-Learn
  • +
  • More preprocessing
  • +
  • Frequently used scaling functions
  • +
  • Example of own Standard scaling
  • +
  • Min-Max Scaling
  • +
  • Testing the Means Squared Error as function of Complexity
  • +
  • More preprocessing examples, Franke function and regression
  • +
  • Material for lecture Thursday, August 31
  • +
  • Mathematical Interpretation of Ordinary Least Squares
  • +
  • Residual Error
  • +
  • Simple case
  • +
  • The singular value decomposition
  • +
  • Linear Regression Problems
  • +
  • Fixing the singularity
  • +
  • Basic math of the SVD
  • +
  • The SVD, a Fantastic Algorithm
  • +
  • Economy-size SVD
  • +
  • Codes for the SVD
  • +
  • Note about SVD Calculations
  • +
  • Mathematics of the SVD and implications
  • +
  • Example Matrix
  • +
  • Setting up the Matrix to be inverted
  • +
  • Further properties (important for our analyses later)
  • +
  • Meet the Covariance Matrix
  • +
  • Introducing the Covariance and Correlation functions
  • +
  • Covariance and Correlation Matrix
  • +
  • Correlation Function and Design/Feature Matrix
  • +
  • Covariance Matrix Examples
  • +
  • Correlation Matrix
  • +
  • Correlation Matrix with Pandas
  • +
  • Correlation Matrix with Pandas and the Franke function
  • +
  • Rewriting the Covariance and/or Correlation Matrix
  • +
  • Linking with the SVD
  • +
  • What does it mean?
  • +
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • +
  • Ridge and LASSO Regression
  • +
  • Deriving the Ridge Regression Equations
  • +
  • Interpreting the Ridge results
  • +
  • More interpretations
  • +
  • Deriving the Lasso Regression Equations
  • +
  • Exercises for week 35
  • +
  • Exercise 1: Setting up various Python environments
  • +
  • Exercise 2: making your own data and exploring scikit-learn
  • +
  • Exercise 3: Normalizing our data
  • +
  • Exercise 4: Adding Ridge Regression
  • +
  • Exercise 5: Analytical exercises
  • @@ -382,46 +391,45 @@ MathJax.Hub.Config({

     

     

     

    -

    Introducing the Covariance and Correlation functions

    +

    Setting up the Matrix to be inverted

    -

    Before we discuss the link between for example Ridge regression and the singular value decomposition, we need to remind ourselves about -the definition of the covariance and the correlation function. These are quantities that play a central role in machine learning methods. -

    +

    The matrix that may cause problems for us is \( \boldsymbol{X}^T\boldsymbol{X} \). Using the SVD we can rewrite this matrix as

    -

    Suppose we have defined two vectors -\( \hat{x} \) and \( \hat{y} \) with \( n \) elements each. The covariance matrix \( \boldsymbol{C} \) is defined as -

    $$ -\boldsymbol{C}[\boldsymbol{x},\boldsymbol{y}] = \begin{bmatrix} \mathrm{cov}[\boldsymbol{x},\boldsymbol{x}] & \mathrm{cov}[\boldsymbol{x},\boldsymbol{y}] \\ - \mathrm{cov}[\boldsymbol{y},\boldsymbol{x}] & \mathrm{cov}[\boldsymbol{y},\boldsymbol{y}] \\ - \end{bmatrix}, +\boldsymbol{X}^T\boldsymbol{X}=\boldsymbol{V}\boldsymbol{\Sigma}^T\boldsymbol{U}^T\boldsymbol{U}\boldsymbol{\Sigma}\boldsymbol{V}^T, $$ -

    where for example

    +

    and using the orthogonality of the matrix \( \boldsymbol{U} \) we have

    + $$ -\mathrm{cov}[\boldsymbol{x},\boldsymbol{y}] =\frac{1}{n} \sum_{i=0}^{n-1}(x_i- \overline{x})(y_i- \overline{y}). +\boldsymbol{X}^T\boldsymbol{X}=\boldsymbol{V}\boldsymbol{\Sigma}^T\boldsymbol{\Sigma}\boldsymbol{V}^T. $$ -

    With this definition and recalling that the variance is defined as

    +

    We define \( \boldsymbol{\Sigma}^T\boldsymbol{\Sigma}=\tilde{\boldsymbol{\Sigma}}^2 \) which is a diagonal matrix containing only the singular values squared. It has dimensionality \( p \times p \).

    + +

    We can now insert the result for the matrix \( \boldsymbol{X}^T\boldsymbol{X} \) into our equation for ordinary least squares where

    + $$ -\mathrm{var}[\boldsymbol{x}]=\frac{1}{n} \sum_{i=0}^{n-1}(x_i- \overline{x})^2, +\tilde{y}_{\mathrm{OLS}}=\boldsymbol{X}\left(\boldsymbol{X}^T\boldsymbol{X}\right)^{-1}\boldsymbol{X}^T\boldsymbol{y}, $$ -

    we can rewrite the covariance matrix as

    +

    and using our SVD decomposition of \( \boldsymbol{X} \) we have

    + $$ -\boldsymbol{C}[\boldsymbol{x},\boldsymbol{y}] = \begin{bmatrix} \mathrm{var}[\boldsymbol{x}] & \mathrm{cov}[\boldsymbol{x},\boldsymbol{y}] \\ - \mathrm{cov}[\boldsymbol{x},\boldsymbol{y}] & \mathrm{var}[\boldsymbol{y}] \\ - \end{bmatrix}. +\tilde{y}_{\mathrm{OLS}}=\boldsymbol{U}\boldsymbol{\Sigma}\boldsymbol{V}^T\left(\boldsymbol{V}\tilde{\boldsymbol{\Sigma}}^{2}(\boldsymbol{V}^T\right)^{-1}\boldsymbol{V}\boldsymbol{\Sigma}^T\boldsymbol{U}^T\boldsymbol{y}, $$ -

    Note: we have used \( 1/n \) in the above definitions of the sample variance and covariance. We assume then that we can calculate the exact mean value. -What you will find in essentially all statistics texts are equations -with a factor \( 1/(n-1) \). This is called Bessel's correction. This -method corrects the bias in the estimation of the population variance -and covariance. It also partially corrects the bias in the estimation -of the population standard deviation. If you use a library like -Scikit-Learn or nunmpy's function calculate the covariance, this -quantity will be computed with a factor \( 1/(n-1) \). +

    which gives us, using the orthogonality of the matrices \( \boldsymbol{U} \) and \( \boldsymbol{V} \),

    + +$$ +\tilde{y}_{\mathrm{OLS}}=\boldsymbol{U}\boldsymbol{U}^T\boldsymbol{y}=\sum_{i=0}^{p-1}\boldsymbol{u}_i\boldsymbol{u}^T_i\boldsymbol{y}, +$$ + +

    It means that the ordinary least square model (with the optimal +parameters) \( \boldsymbol{\tilde{y}} \), corresponds to an orthogonal +transformation of the output (or target) vector \( \boldsymbol{y} \) by the +vectors of the matrix \( \boldsymbol{U} \). Note that the summation ends at \( p-1 \), +that is \( \boldsymbol{\tilde{y}}\ne \boldsymbol{y} \).

    @@ -449,7 +457,7 @@ quantity will be computed with a factor \( 1/(n-1) \).

  • 60
  • 61
  • ...
  • -
  • 68
  • +
  • 71
  • »
  • diff --git a/doc/pub/week35/html/._week35-bs052.html b/doc/pub/week35/html/._week35-bs052.html index fd636c0fe..c86644a2e 100644 --- a/doc/pub/week35/html/._week35-bs052.html +++ b/doc/pub/week35/html/._week35-bs052.html @@ -74,10 +74,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'optimizing-our-parameters'), - ('Our model for the nuclear binding energies', + ('Examples relevant for the exercises', 2, None, - 'our-model-for-the-nuclear-binding-energies'), + 'examples-relevant-for-the-exercises'), ('Optimizing our parameters, more details', 2, None, @@ -94,6 +94,8 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'some-useful-matrix-and-vector-expressions'), + ('The Jacobian', 2, None, 'the-jacobian'), + ('Derivatives, example 1', 2, None, 'derivatives-example-1'), ('Meet the Hessian Matrix', 2, None, 'meet-the-hessian-matrix'), ('Interpretations and optimizing our parameters', 2, @@ -148,6 +150,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'more-preprocessing-examples-franke-function-and-regression'), + ('Material for lecture Thursday, August 31', + 2, + None, + 'material-for-lecture-thursday-august-31'), ('Mathematical Interpretation of Ordinary Least Squares', 2, None, @@ -303,74 +309,77 @@ MathJax.Hub.Config({
  • Why Linear Regression (aka Ordinary Least Squares and family), repeat from last week
  • Regression analysis, overarching aims
  • Regression analysis, overarching aims II
  • -
  • Examples
  • +
  • Examples
  • General linear models
  • Rewriting the fitting procedure as a linear algebra problem
  • Rewriting the fitting procedure as a linear algebra problem, more details
  • Generalizing the fitting procedure as a linear algebra problem
  • Generalizing the fitting procedure as a linear algebra problem
  • Optimizing our parameters
  • -
  • Our model for the nuclear binding energies
  • +
  • Examples relevant for the exercises
  • Optimizing our parameters, more details
  • -
  • Interpretations and optimizing our parameters
  • -
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • Some useful matrix and vector expressions
  • -
  • Meet the Hessian Matrix
  • -
  • Interpretations and optimizing our parameters
  • -
  • Own code for Ordinary Least Squares
  • -
  • Adding error analysis and training set up
  • -
  • Splitting our Data in Training and Test data
  • -
  • Examples
  • -
  • Making your own test-train splitting
  • -
  • The Boston housing data example
  • -
  • Housing data, the code
  • -
  • Reducing the number of degrees of freedom, overarching view
  • -
  • Preprocessing our data
  • -
  • Functionality in Scikit-Learn
  • -
  • More preprocessing
  • -
  • Frequently used scaling functions
  • -
  • Example of own Standard scaling
  • -
  • Min-Max Scaling
  • -
  • Testing the Means Squared Error as function of Complexity
  • -
  • More preprocessing examples, Franke function and regression
  • -
  • Mathematical Interpretation of Ordinary Least Squares
  • -
  • Residual Error
  • -
  • Simple case
  • -
  • The singular value decomposition
  • -
  • Linear Regression Problems
  • -
  • Fixing the singularity
  • -
  • Basic math of the SVD
  • -
  • The SVD, a Fantastic Algorithm
  • -
  • Economy-size SVD
  • -
  • Codes for the SVD
  • -
  • Note about SVD Calculations
  • -
  • Mathematics of the SVD and implications
  • -
  • Example Matrix
  • -
  • Setting up the Matrix to be inverted
  • -
  • Further properties (important for our analyses later)
  • -
  • Meet the Covariance Matrix
  • -
  • Introducing the Covariance and Correlation functions
  • -
  • Covariance and Correlation Matrix
  • -
  • Correlation Function and Design/Feature Matrix
  • -
  • Covariance Matrix Examples
  • -
  • Correlation Matrix
  • -
  • Correlation Matrix with Pandas
  • -
  • Correlation Matrix with Pandas and the Franke function
  • -
  • Rewriting the Covariance and/or Correlation Matrix
  • -
  • Linking with the SVD
  • -
  • What does it mean?
  • -
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • -
  • Ridge and LASSO Regression
  • -
  • Deriving the Ridge Regression Equations
  • -
  • Interpreting the Ridge results
  • -
  • More interpretations
  • -
  • Deriving the Lasso Regression Equations
  • -
  • Exercises for week 35
  • -
  • Exercise 1: Setting up various Python environments
  • -
  • Exercise 2: making your own data and exploring scikit-learn
  • -
  • Exercise 3: Normalizing our data
  • -
  • Exercise 4: Adding Ridge Regression
  • -
  • Exercise 5: Analytical exercises
  • +
  • The Jacobian
  • +
  • Derivatives, example 1
  • +
  • Meet the Hessian Matrix
  • +
  • Interpretations and optimizing our parameters
  • +
  • Own code for Ordinary Least Squares
  • +
  • Adding error analysis and training set up
  • +
  • Splitting our Data in Training and Test data
  • +
  • Examples
  • +
  • Making your own test-train splitting
  • +
  • The Boston housing data example
  • +
  • Housing data, the code
  • +
  • Reducing the number of degrees of freedom, overarching view
  • +
  • Preprocessing our data
  • +
  • Functionality in Scikit-Learn
  • +
  • More preprocessing
  • +
  • Frequently used scaling functions
  • +
  • Example of own Standard scaling
  • +
  • Min-Max Scaling
  • +
  • Testing the Means Squared Error as function of Complexity
  • +
  • More preprocessing examples, Franke function and regression
  • +
  • Material for lecture Thursday, August 31
  • +
  • Mathematical Interpretation of Ordinary Least Squares
  • +
  • Residual Error
  • +
  • Simple case
  • +
  • The singular value decomposition
  • +
  • Linear Regression Problems
  • +
  • Fixing the singularity
  • +
  • Basic math of the SVD
  • +
  • The SVD, a Fantastic Algorithm
  • +
  • Economy-size SVD
  • +
  • Codes for the SVD
  • +
  • Note about SVD Calculations
  • +
  • Mathematics of the SVD and implications
  • +
  • Example Matrix
  • +
  • Setting up the Matrix to be inverted
  • +
  • Further properties (important for our analyses later)
  • +
  • Meet the Covariance Matrix
  • +
  • Introducing the Covariance and Correlation functions
  • +
  • Covariance and Correlation Matrix
  • +
  • Correlation Function and Design/Feature Matrix
  • +
  • Covariance Matrix Examples
  • +
  • Correlation Matrix
  • +
  • Correlation Matrix with Pandas
  • +
  • Correlation Matrix with Pandas and the Franke function
  • +
  • Rewriting the Covariance and/or Correlation Matrix
  • +
  • Linking with the SVD
  • +
  • What does it mean?
  • +
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • +
  • Ridge and LASSO Regression
  • +
  • Deriving the Ridge Regression Equations
  • +
  • Interpreting the Ridge results
  • +
  • More interpretations
  • +
  • Deriving the Lasso Regression Equations
  • +
  • Exercises for week 35
  • +
  • Exercise 1: Setting up various Python environments
  • +
  • Exercise 2: making your own data and exploring scikit-learn
  • +
  • Exercise 3: Normalizing our data
  • +
  • Exercise 4: Adding Ridge Regression
  • +
  • Exercise 5: Analytical exercises
  • @@ -382,33 +391,54 @@ MathJax.Hub.Config({

     

     

     

    -

    Covariance and Correlation Matrix

    +

    Further properties (important for our analyses later)

    -

    The covariance takes values between zero and infinity and may thus -lead to problems with loss of numerical precision for particularly -large values. It is common to scale the covariance matrix by -introducing instead the correlation matrix defined via the so-called -correlation function +

    Let us study again \( \boldsymbol{X}^T\boldsymbol{X} \) in terms of our SVD,

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

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

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

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

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

    Similarly, if we use the SVD decomposition for the matrix \( \boldsymbol{X}\boldsymbol{X}^T \), we have

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

    If we now multiply from the right with \( \boldsymbol{U} \) (using the orthogonality of \( \boldsymbol{U} \)) we get

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

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

    +$$ +\left(\boldsymbol{X}\boldsymbol{X}^T\right)\boldsymbol{u}_i=\boldsymbol{u}_i\sigma_i^2. +$$ + +

    Important note: we have defined our design matrix \( \boldsymbol{X} \) to be an +\( n\times p \) matrix. In most supervised learning cases we have that \( n +\ge p \), and quite often we have \( n >> p \). For linear algebra based methods like ordinary least squares or Ridge regression, this leads to a matrix \( \boldsymbol{X}^T\boldsymbol{X} \) which is small and thereby easier to handle from a computational point of view (in terms of number of floating point operations).

    -$$ -\mathrm{corr}[\boldsymbol{x},\boldsymbol{y}]=\frac{\mathrm{cov}[\boldsymbol{x},\boldsymbol{y}]}{\sqrt{\mathrm{var}[\boldsymbol{x}] \mathrm{var}[\boldsymbol{y}]}}. -$$ - -

    The correlation function is then given by values \( \mathrm{corr}[\boldsymbol{x},\boldsymbol{y}] -\in [-1,1] \). This avoids eventual problems with too large values. We -can then define the correlation matrix for the two vectors \( \boldsymbol{x} \) -and \( \boldsymbol{y} \) as +

    In our lectures, the number of columns will +always refer to the number of features in our data set, while the +number of rows represents the number of data inputs. Note that in +other texts you may find the opposite notation. This has consequences +for the definition of for example the covariance matrix and its relation to the SVD.

    -$$ -\boldsymbol{K}[\boldsymbol{x},\boldsymbol{y}] = \begin{bmatrix} 1 & \mathrm{corr}[\boldsymbol{x},\boldsymbol{y}] \\ - \mathrm{corr}[\boldsymbol{y},\boldsymbol{x}] & 1 \\ - \end{bmatrix}, -$$ - -

    In the above example this is the function we constructed using pandas.

    -

    diff --git a/doc/pub/week35/html/._week35-bs053.html b/doc/pub/week35/html/._week35-bs053.html index 4edec9a76..53519a187 100644 --- a/doc/pub/week35/html/._week35-bs053.html +++ b/doc/pub/week35/html/._week35-bs053.html @@ -74,10 +74,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'optimizing-our-parameters'), - ('Our model for the nuclear binding energies', + ('Examples relevant for the exercises', 2, None, - 'our-model-for-the-nuclear-binding-energies'), + 'examples-relevant-for-the-exercises'), ('Optimizing our parameters, more details', 2, None, @@ -94,6 +94,8 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'some-useful-matrix-and-vector-expressions'), + ('The Jacobian', 2, None, 'the-jacobian'), + ('Derivatives, example 1', 2, None, 'derivatives-example-1'), ('Meet the Hessian Matrix', 2, None, 'meet-the-hessian-matrix'), ('Interpretations and optimizing our parameters', 2, @@ -148,6 +150,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'more-preprocessing-examples-franke-function-and-regression'), + ('Material for lecture Thursday, August 31', + 2, + None, + 'material-for-lecture-thursday-august-31'), ('Mathematical Interpretation of Ordinary Least Squares', 2, None, @@ -303,74 +309,77 @@ MathJax.Hub.Config({
  • Why Linear Regression (aka Ordinary Least Squares and family), repeat from last week
  • Regression analysis, overarching aims
  • Regression analysis, overarching aims II
  • -
  • Examples
  • +
  • Examples
  • General linear models
  • Rewriting the fitting procedure as a linear algebra problem
  • Rewriting the fitting procedure as a linear algebra problem, more details
  • Generalizing the fitting procedure as a linear algebra problem
  • Generalizing the fitting procedure as a linear algebra problem
  • Optimizing our parameters
  • -
  • Our model for the nuclear binding energies
  • +
  • Examples relevant for the exercises
  • Optimizing our parameters, more details
  • -
  • Interpretations and optimizing our parameters
  • -
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • Some useful matrix and vector expressions
  • -
  • Meet the Hessian Matrix
  • -
  • Interpretations and optimizing our parameters
  • -
  • Own code for Ordinary Least Squares
  • -
  • Adding error analysis and training set up
  • -
  • Splitting our Data in Training and Test data
  • -
  • Examples
  • -
  • Making your own test-train splitting
  • -
  • The Boston housing data example
  • -
  • Housing data, the code
  • -
  • Reducing the number of degrees of freedom, overarching view
  • -
  • Preprocessing our data
  • -
  • Functionality in Scikit-Learn
  • -
  • More preprocessing
  • -
  • Frequently used scaling functions
  • -
  • Example of own Standard scaling
  • -
  • Min-Max Scaling
  • -
  • Testing the Means Squared Error as function of Complexity
  • -
  • More preprocessing examples, Franke function and regression
  • -
  • Mathematical Interpretation of Ordinary Least Squares
  • -
  • Residual Error
  • -
  • Simple case
  • -
  • The singular value decomposition
  • -
  • Linear Regression Problems
  • -
  • Fixing the singularity
  • -
  • Basic math of the SVD
  • -
  • The SVD, a Fantastic Algorithm
  • -
  • Economy-size SVD
  • -
  • Codes for the SVD
  • -
  • Note about SVD Calculations
  • -
  • Mathematics of the SVD and implications
  • -
  • Example Matrix
  • -
  • Setting up the Matrix to be inverted
  • -
  • Further properties (important for our analyses later)
  • -
  • Meet the Covariance Matrix
  • -
  • Introducing the Covariance and Correlation functions
  • -
  • Covariance and Correlation Matrix
  • -
  • Correlation Function and Design/Feature Matrix
  • -
  • Covariance Matrix Examples
  • -
  • Correlation Matrix
  • -
  • Correlation Matrix with Pandas
  • -
  • Correlation Matrix with Pandas and the Franke function
  • -
  • Rewriting the Covariance and/or Correlation Matrix
  • -
  • Linking with the SVD
  • -
  • What does it mean?
  • -
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • -
  • Ridge and LASSO Regression
  • -
  • Deriving the Ridge Regression Equations
  • -
  • Interpreting the Ridge results
  • -
  • More interpretations
  • -
  • Deriving the Lasso Regression Equations
  • -
  • Exercises for week 35
  • -
  • Exercise 1: Setting up various Python environments
  • -
  • Exercise 2: making your own data and exploring scikit-learn
  • -
  • Exercise 3: Normalizing our data
  • -
  • Exercise 4: Adding Ridge Regression
  • -
  • Exercise 5: Analytical exercises
  • +
  • The Jacobian
  • +
  • Derivatives, example 1
  • +
  • Meet the Hessian Matrix
  • +
  • Interpretations and optimizing our parameters
  • +
  • Own code for Ordinary Least Squares
  • +
  • Adding error analysis and training set up
  • +
  • Splitting our Data in Training and Test data
  • +
  • Examples
  • +
  • Making your own test-train splitting
  • +
  • The Boston housing data example
  • +
  • Housing data, the code
  • +
  • Reducing the number of degrees of freedom, overarching view
  • +
  • Preprocessing our data
  • +
  • Functionality in Scikit-Learn
  • +
  • More preprocessing
  • +
  • Frequently used scaling functions
  • +
  • Example of own Standard scaling
  • +
  • Min-Max Scaling
  • +
  • Testing the Means Squared Error as function of Complexity
  • +
  • More preprocessing examples, Franke function and regression
  • +
  • Material for lecture Thursday, August 31
  • +
  • Mathematical Interpretation of Ordinary Least Squares
  • +
  • Residual Error
  • +
  • Simple case
  • +
  • The singular value decomposition
  • +
  • Linear Regression Problems
  • +
  • Fixing the singularity
  • +
  • Basic math of the SVD
  • +
  • The SVD, a Fantastic Algorithm
  • +
  • Economy-size SVD
  • +
  • Codes for the SVD
  • +
  • Note about SVD Calculations
  • +
  • Mathematics of the SVD and implications
  • +
  • Example Matrix
  • +
  • Setting up the Matrix to be inverted
  • +
  • Further properties (important for our analyses later)
  • +
  • Meet the Covariance Matrix
  • +
  • Introducing the Covariance and Correlation functions
  • +
  • Covariance and Correlation Matrix
  • +
  • Correlation Function and Design/Feature Matrix
  • +
  • Covariance Matrix Examples
  • +
  • Correlation Matrix
  • +
  • Correlation Matrix with Pandas
  • +
  • Correlation Matrix with Pandas and the Franke function
  • +
  • Rewriting the Covariance and/or Correlation Matrix
  • +
  • Linking with the SVD
  • +
  • What does it mean?
  • +
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • +
  • Ridge and LASSO Regression
  • +
  • Deriving the Ridge Regression Equations
  • +
  • Interpreting the Ridge results
  • +
  • More interpretations
  • +
  • Deriving the Lasso Regression Equations
  • +
  • Exercises for week 35
  • +
  • Exercise 1: Setting up various Python environments
  • +
  • Exercise 2: making your own data and exploring scikit-learn
  • +
  • Exercise 3: Normalizing our data
  • +
  • Exercise 4: Adding Ridge Regression
  • +
  • Exercise 5: Analytical exercises
  • @@ -382,65 +391,32 @@ MathJax.Hub.Config({

     

     

     

    -

    Correlation Function and Design/Feature Matrix

    +

    Meet the Covariance Matrix

    -

    In our derivation of the various regression algorithms like Ordinary Least Squares or Ridge regression -we defined the design/feature matrix \( \boldsymbol{X} \) as +

    Before we move on to a discussion of Ridge and Lasso regression, we want to show an important example of the above.

    + +

    We have already noted that the matrix \( \boldsymbol{X}^T\boldsymbol{X} \) in ordinary +least squares is proportional to the second derivative of the cost +function, that is we have

    $$ -\boldsymbol{X}=\begin{bmatrix} -x_{0,0} & x_{0,1} & x_{0,2}& \dots & \dots x_{0,p-1}\\ -x_{1,0} & x_{1,1} & x_{1,2}& \dots & \dots x_{1,p-1}\\ -x_{2,0} & x_{2,1} & x_{2,2}& \dots & \dots x_{2,p-1}\\ -\dots & \dots & \dots & \dots \dots & \dots \\ -x_{n-2,0} & x_{n-2,1} & x_{n-2,2}& \dots & \dots x_{n-2,p-1}\\ -x_{n-1,0} & x_{n-1,1} & x_{n-1,2}& \dots & \dots x_{n-1,p-1}\\ -\end{bmatrix}, +\frac{\partial^2 C(\boldsymbol{\beta})}{\partial \boldsymbol{\beta}^T\partial \boldsymbol{\beta}} =\frac{2}{n}\boldsymbol{X}^T\boldsymbol{X}. $$ -

    with \( \boldsymbol{X}\in {\mathbb{R}}^{n\times p} \), with the predictors/features \( p \) refering to the column numbers and the -entries \( n \) being the row elements. -We can rewrite the design/feature matrix in terms of its column vectors as +

    This quantity defines was what is called the Hessian matrix (the second derivative of a function we want to optimize).

    + +

    The Hessian matrix plays an important role and is defined in this course as

    + +$$ +\boldsymbol{H}=\boldsymbol{X}^T\boldsymbol{X}. +$$ + +

    The Hessian matrix for ordinary least squares is also proportional to +the covariance matrix. This means also that we can use the SVD to find +the eigenvalues of the covariance matrix and the Hessian matrix in +terms of the singular values. Let us develop these arguments, as they will play an important role in our machine learning studies.

    -$$ -\boldsymbol{X}=\begin{bmatrix} \boldsymbol{x}_0 & \boldsymbol{x}_1 & \boldsymbol{x}_2 & \dots & \dots & \boldsymbol{x}_{p-1}\end{bmatrix}, -$$ - -

    with a given vector

    -$$ -\boldsymbol{x}_i^T = \begin{bmatrix}x_{0,i} & x_{1,i} & x_{2,i}& \dots & \dots x_{n-1,i}\end{bmatrix}. -$$ - -

    With these definitions, we can now rewrite our \( 2\times 2 \) -correlation/covariance matrix in terms of a moe general design/feature -matrix \( \boldsymbol{X}\in {\mathbb{R}}^{n\times p} \). This leads to a \( p\times p \) -covariance matrix for the vectors \( \boldsymbol{x}_i \) with \( i=0,1,\dots,p-1 \) -

    - -$$ -\boldsymbol{C}[\boldsymbol{x}] = \begin{bmatrix} -\mathrm{var}[\boldsymbol{x}_0] & \mathrm{cov}[\boldsymbol{x}_0,\boldsymbol{x}_1] & \mathrm{cov}[\boldsymbol{x}_0,\boldsymbol{x}_2] & \dots & \dots & \mathrm{cov}[\boldsymbol{x}_0,\boldsymbol{x}_{p-1}]\\ -\mathrm{cov}[\boldsymbol{x}_1,\boldsymbol{x}_0] & \mathrm{var}[\boldsymbol{x}_1] & \mathrm{cov}[\boldsymbol{x}_1,\boldsymbol{x}_2] & \dots & \dots & \mathrm{cov}[\boldsymbol{x}_1,\boldsymbol{x}_{p-1}]\\ -\mathrm{cov}[\boldsymbol{x}_2,\boldsymbol{x}_0] & \mathrm{cov}[\boldsymbol{x}_2,\boldsymbol{x}_1] & \mathrm{var}[\boldsymbol{x}_2] & \dots & \dots & \mathrm{cov}[\boldsymbol{x}_2,\boldsymbol{x}_{p-1}]\\ -\dots & \dots & \dots & \dots & \dots & \dots \\ -\dots & \dots & \dots & \dots & \dots & \dots \\ -\mathrm{cov}[\boldsymbol{x}_{p-1},\boldsymbol{x}_0] & \mathrm{cov}[\boldsymbol{x}_{p-1},\boldsymbol{x}_1] & \mathrm{cov}[\boldsymbol{x}_{p-1},\boldsymbol{x}_{2}] & \dots & \dots & \mathrm{var}[\boldsymbol{x}_{p-1}]\\ -\end{bmatrix}, -$$ - -

    and the correlation matrix

    -$$ -\boldsymbol{K}[\boldsymbol{x}] = \begin{bmatrix} -1 & \mathrm{corr}[\boldsymbol{x}_0,\boldsymbol{x}_1] & \mathrm{corr}[\boldsymbol{x}_0,\boldsymbol{x}_2] & \dots & \dots & \mathrm{corr}[\boldsymbol{x}_0,\boldsymbol{x}_{p-1}]\\ -\mathrm{corr}[\boldsymbol{x}_1,\boldsymbol{x}_0] & 1 & \mathrm{corr}[\boldsymbol{x}_1,\boldsymbol{x}_2] & \dots & \dots & \mathrm{corr}[\boldsymbol{x}_1,\boldsymbol{x}_{p-1}]\\ -\mathrm{corr}[\boldsymbol{x}_2,\boldsymbol{x}_0] & \mathrm{corr}[\boldsymbol{x}_2,\boldsymbol{x}_1] & 1 & \dots & \dots & \mathrm{corr}[\boldsymbol{x}_2,\boldsymbol{x}_{p-1}]\\ -\dots & \dots & \dots & \dots & \dots & \dots \\ -\dots & \dots & \dots & \dots & \dots & \dots \\ -\mathrm{corr}[\boldsymbol{x}_{p-1},\boldsymbol{x}_0] & \mathrm{corr}[\boldsymbol{x}_{p-1},\boldsymbol{x}_1] & \mathrm{corr}[\boldsymbol{x}_{p-1},\boldsymbol{x}_{2}] & \dots & \dots & 1\\ -\end{bmatrix}, -$$ -

    @@ -467,7 +443,7 @@ $$

  • 62
  • 63
  • ...
  • -
  • 68
  • +
  • 71
  • »
  • diff --git a/doc/pub/week35/html/._week35-bs054.html b/doc/pub/week35/html/._week35-bs054.html index 595d32a0f..a6431c9c7 100644 --- a/doc/pub/week35/html/._week35-bs054.html +++ b/doc/pub/week35/html/._week35-bs054.html @@ -74,10 +74,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'optimizing-our-parameters'), - ('Our model for the nuclear binding energies', + ('Examples relevant for the exercises', 2, None, - 'our-model-for-the-nuclear-binding-energies'), + 'examples-relevant-for-the-exercises'), ('Optimizing our parameters, more details', 2, None, @@ -94,6 +94,8 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'some-useful-matrix-and-vector-expressions'), + ('The Jacobian', 2, None, 'the-jacobian'), + ('Derivatives, example 1', 2, None, 'derivatives-example-1'), ('Meet the Hessian Matrix', 2, None, 'meet-the-hessian-matrix'), ('Interpretations and optimizing our parameters', 2, @@ -148,6 +150,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'more-preprocessing-examples-franke-function-and-regression'), + ('Material for lecture Thursday, August 31', + 2, + None, + 'material-for-lecture-thursday-august-31'), ('Mathematical Interpretation of Ordinary Least Squares', 2, None, @@ -303,74 +309,77 @@ MathJax.Hub.Config({
  • Why Linear Regression (aka Ordinary Least Squares and family), repeat from last week
  • Regression analysis, overarching aims
  • Regression analysis, overarching aims II
  • -
  • Examples
  • +
  • Examples
  • General linear models
  • Rewriting the fitting procedure as a linear algebra problem
  • Rewriting the fitting procedure as a linear algebra problem, more details
  • Generalizing the fitting procedure as a linear algebra problem
  • Generalizing the fitting procedure as a linear algebra problem
  • Optimizing our parameters
  • -
  • Our model for the nuclear binding energies
  • +
  • Examples relevant for the exercises
  • Optimizing our parameters, more details
  • -
  • Interpretations and optimizing our parameters
  • -
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • Some useful matrix and vector expressions
  • -
  • Meet the Hessian Matrix
  • -
  • Interpretations and optimizing our parameters
  • -
  • Own code for Ordinary Least Squares
  • -
  • Adding error analysis and training set up
  • -
  • Splitting our Data in Training and Test data
  • -
  • Examples
  • -
  • Making your own test-train splitting
  • -
  • The Boston housing data example
  • -
  • Housing data, the code
  • -
  • Reducing the number of degrees of freedom, overarching view
  • -
  • Preprocessing our data
  • -
  • Functionality in Scikit-Learn
  • -
  • More preprocessing
  • -
  • Frequently used scaling functions
  • -
  • Example of own Standard scaling
  • -
  • Min-Max Scaling
  • -
  • Testing the Means Squared Error as function of Complexity
  • -
  • More preprocessing examples, Franke function and regression
  • -
  • Mathematical Interpretation of Ordinary Least Squares
  • -
  • Residual Error
  • -
  • Simple case
  • -
  • The singular value decomposition
  • -
  • Linear Regression Problems
  • -
  • Fixing the singularity
  • -
  • Basic math of the SVD
  • -
  • The SVD, a Fantastic Algorithm
  • -
  • Economy-size SVD
  • -
  • Codes for the SVD
  • -
  • Note about SVD Calculations
  • -
  • Mathematics of the SVD and implications
  • -
  • Example Matrix
  • -
  • Setting up the Matrix to be inverted
  • -
  • Further properties (important for our analyses later)
  • -
  • Meet the Covariance Matrix
  • -
  • Introducing the Covariance and Correlation functions
  • -
  • Covariance and Correlation Matrix
  • -
  • Correlation Function and Design/Feature Matrix
  • -
  • Covariance Matrix Examples
  • -
  • Correlation Matrix
  • -
  • Correlation Matrix with Pandas
  • -
  • Correlation Matrix with Pandas and the Franke function
  • -
  • Rewriting the Covariance and/or Correlation Matrix
  • -
  • Linking with the SVD
  • -
  • What does it mean?
  • -
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • -
  • Ridge and LASSO Regression
  • -
  • Deriving the Ridge Regression Equations
  • -
  • Interpreting the Ridge results
  • -
  • More interpretations
  • -
  • Deriving the Lasso Regression Equations
  • -
  • Exercises for week 35
  • -
  • Exercise 1: Setting up various Python environments
  • -
  • Exercise 2: making your own data and exploring scikit-learn
  • -
  • Exercise 3: Normalizing our data
  • -
  • Exercise 4: Adding Ridge Regression
  • -
  • Exercise 5: Analytical exercises
  • +
  • The Jacobian
  • +
  • Derivatives, example 1
  • +
  • Meet the Hessian Matrix
  • +
  • Interpretations and optimizing our parameters
  • +
  • Own code for Ordinary Least Squares
  • +
  • Adding error analysis and training set up
  • +
  • Splitting our Data in Training and Test data
  • +
  • Examples
  • +
  • Making your own test-train splitting
  • +
  • The Boston housing data example
  • +
  • Housing data, the code
  • +
  • Reducing the number of degrees of freedom, overarching view
  • +
  • Preprocessing our data
  • +
  • Functionality in Scikit-Learn
  • +
  • More preprocessing
  • +
  • Frequently used scaling functions
  • +
  • Example of own Standard scaling
  • +
  • Min-Max Scaling
  • +
  • Testing the Means Squared Error as function of Complexity
  • +
  • More preprocessing examples, Franke function and regression
  • +
  • Material for lecture Thursday, August 31
  • +
  • Mathematical Interpretation of Ordinary Least Squares
  • +
  • Residual Error
  • +
  • Simple case
  • +
  • The singular value decomposition
  • +
  • Linear Regression Problems
  • +
  • Fixing the singularity
  • +
  • Basic math of the SVD
  • +
  • The SVD, a Fantastic Algorithm
  • +
  • Economy-size SVD
  • +
  • Codes for the SVD
  • +
  • Note about SVD Calculations
  • +
  • Mathematics of the SVD and implications
  • +
  • Example Matrix
  • +
  • Setting up the Matrix to be inverted
  • +
  • Further properties (important for our analyses later)
  • +
  • Meet the Covariance Matrix
  • +
  • Introducing the Covariance and Correlation functions
  • +
  • Covariance and Correlation Matrix
  • +
  • Correlation Function and Design/Feature Matrix
  • +
  • Covariance Matrix Examples
  • +
  • Correlation Matrix
  • +
  • Correlation Matrix with Pandas
  • +
  • Correlation Matrix with Pandas and the Franke function
  • +
  • Rewriting the Covariance and/or Correlation Matrix
  • +
  • Linking with the SVD
  • +
  • What does it mean?
  • +
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • +
  • Ridge and LASSO Regression
  • +
  • Deriving the Ridge Regression Equations
  • +
  • Interpreting the Ridge results
  • +
  • More interpretations
  • +
  • Deriving the Lasso Regression Equations
  • +
  • Exercises for week 35
  • +
  • Exercise 1: Setting up various Python environments
  • +
  • Exercise 2: making your own data and exploring scikit-learn
  • +
  • Exercise 3: Normalizing our data
  • +
  • Exercise 4: Adding Ridge Regression
  • +
  • Exercise 5: Analytical exercises
  • @@ -382,62 +391,48 @@ MathJax.Hub.Config({

     

     

     

    -

    Covariance Matrix Examples

    +

    Introducing the Covariance and Correlation functions

    -

    The Numpy function np.cov calculates the covariance elements using -the factor \( 1/(n-1) \) instead of \( 1/n \) since it assumes we do not have -the exact mean values. The following simple function uses the -np.vstack function which takes each vector of dimension \( 1\times n \) -and produces a \( 2\times n \) matrix \( \boldsymbol{W} \) +

    Before we discuss the link between for example Ridge regression and the singular value decomposition, we need to remind ourselves about +the definition of the covariance and the correlation function. These are quantities that play a central role in machine learning methods.

    -

    Note that this assumes you have the features as the rows, and the inputs as columns, that is

    +

    Suppose we have defined two vectors +\( \hat{x} \) and \( \hat{y} \) with \( n \) elements each. The covariance matrix \( \boldsymbol{C} \) is defined as +

    $$ -\boldsymbol{W} = \begin{bmatrix} x_0 & x_1 & x_2 & \dots & x_{n-2} & x_{n-1} \\ - y_0 & y_1 & y_2 & \dots & y_{n-2} & y_{n-1} \\ +\boldsymbol{C}[\boldsymbol{x},\boldsymbol{y}] = \begin{bmatrix} \mathrm{cov}[\boldsymbol{x},\boldsymbol{x}] & \mathrm{cov}[\boldsymbol{x},\boldsymbol{y}] \\ + \mathrm{cov}[\boldsymbol{y},\boldsymbol{x}] & \mathrm{cov}[\boldsymbol{y},\boldsymbol{y}] \\ \end{bmatrix}, $$ -

    which in turn is converted into into the \( 2\times 2 \) covariance matrix -\( \boldsymbol{C} \) via the Numpy function np.cov(). We note that we can also calculate -the mean value of each set of samples \( \boldsymbol{x} \) etc using the Numpy -function np.mean(x). We can also extract the eigenvalues of the -covariance matrix through the np.linalg.eig() function. +

    where for example

    +$$ +\mathrm{cov}[\boldsymbol{x},\boldsymbol{y}] =\frac{1}{n} \sum_{i=0}^{n-1}(x_i- \overline{x})(y_i- \overline{y}). +$$ + +

    With this definition and recalling that the variance is defined as

    +$$ +\mathrm{var}[\boldsymbol{x}]=\frac{1}{n} \sum_{i=0}^{n-1}(x_i- \overline{x})^2, +$$ + +

    we can rewrite the covariance matrix as

    +$$ +\boldsymbol{C}[\boldsymbol{x},\boldsymbol{y}] = \begin{bmatrix} \mathrm{var}[\boldsymbol{x}] & \mathrm{cov}[\boldsymbol{x},\boldsymbol{y}] \\ + \mathrm{cov}[\boldsymbol{x},\boldsymbol{y}] & \mathrm{var}[\boldsymbol{y}] \\ + \end{bmatrix}. +$$ + +

    Note: we have used \( 1/n \) in the above definitions of the sample variance and covariance. We assume then that we can calculate the exact mean value. +What you will find in essentially all statistics texts are equations +with a factor \( 1/(n-1) \). This is called Bessel's correction. This +method corrects the bias in the estimation of the population variance +and covariance. It also partially corrects the bias in the estimation +of the population standard deviation. If you use a library like +Scikit-Learn or nunmpy's function calculate the covariance, this +quantity will be computed with a factor \( 1/(n-1) \).

    - - -
    -
    -
    -
    -
    -
    # Importing various packages
    -import numpy as np
    -n = 100
    -x = np.random.normal(size=n)
    -print(np.mean(x))
    -y = 4+3*x+np.random.normal(size=n)
    -print(np.mean(y))
    -W = np.vstack((x, y))
    -C = np.cov(W)
    -print(C)
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    - -

    diff --git a/doc/pub/week35/html/._week35-bs055.html b/doc/pub/week35/html/._week35-bs055.html index bf3eee13e..92d62e583 100644 --- a/doc/pub/week35/html/._week35-bs055.html +++ b/doc/pub/week35/html/._week35-bs055.html @@ -74,10 +74,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'optimizing-our-parameters'), - ('Our model for the nuclear binding energies', + ('Examples relevant for the exercises', 2, None, - 'our-model-for-the-nuclear-binding-energies'), + 'examples-relevant-for-the-exercises'), ('Optimizing our parameters, more details', 2, None, @@ -94,6 +94,8 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'some-useful-matrix-and-vector-expressions'), + ('The Jacobian', 2, None, 'the-jacobian'), + ('Derivatives, example 1', 2, None, 'derivatives-example-1'), ('Meet the Hessian Matrix', 2, None, 'meet-the-hessian-matrix'), ('Interpretations and optimizing our parameters', 2, @@ -148,6 +150,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'more-preprocessing-examples-franke-function-and-regression'), + ('Material for lecture Thursday, August 31', + 2, + None, + 'material-for-lecture-thursday-august-31'), ('Mathematical Interpretation of Ordinary Least Squares', 2, None, @@ -303,74 +309,77 @@ MathJax.Hub.Config({
  • Why Linear Regression (aka Ordinary Least Squares and family), repeat from last week
  • Regression analysis, overarching aims
  • Regression analysis, overarching aims II
  • -
  • Examples
  • +
  • Examples
  • General linear models
  • Rewriting the fitting procedure as a linear algebra problem
  • Rewriting the fitting procedure as a linear algebra problem, more details
  • Generalizing the fitting procedure as a linear algebra problem
  • Generalizing the fitting procedure as a linear algebra problem
  • Optimizing our parameters
  • -
  • Our model for the nuclear binding energies
  • +
  • Examples relevant for the exercises
  • Optimizing our parameters, more details
  • -
  • Interpretations and optimizing our parameters
  • -
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • Some useful matrix and vector expressions
  • -
  • Meet the Hessian Matrix
  • -
  • Interpretations and optimizing our parameters
  • -
  • Own code for Ordinary Least Squares
  • -
  • Adding error analysis and training set up
  • -
  • Splitting our Data in Training and Test data
  • -
  • Examples
  • -
  • Making your own test-train splitting
  • -
  • The Boston housing data example
  • -
  • Housing data, the code
  • -
  • Reducing the number of degrees of freedom, overarching view
  • -
  • Preprocessing our data
  • -
  • Functionality in Scikit-Learn
  • -
  • More preprocessing
  • -
  • Frequently used scaling functions
  • -
  • Example of own Standard scaling
  • -
  • Min-Max Scaling
  • -
  • Testing the Means Squared Error as function of Complexity
  • -
  • More preprocessing examples, Franke function and regression
  • -
  • Mathematical Interpretation of Ordinary Least Squares
  • -
  • Residual Error
  • -
  • Simple case
  • -
  • The singular value decomposition
  • -
  • Linear Regression Problems
  • -
  • Fixing the singularity
  • -
  • Basic math of the SVD
  • -
  • The SVD, a Fantastic Algorithm
  • -
  • Economy-size SVD
  • -
  • Codes for the SVD
  • -
  • Note about SVD Calculations
  • -
  • Mathematics of the SVD and implications
  • -
  • Example Matrix
  • -
  • Setting up the Matrix to be inverted
  • -
  • Further properties (important for our analyses later)
  • -
  • Meet the Covariance Matrix
  • -
  • Introducing the Covariance and Correlation functions
  • -
  • Covariance and Correlation Matrix
  • -
  • Correlation Function and Design/Feature Matrix
  • -
  • Covariance Matrix Examples
  • -
  • Correlation Matrix
  • -
  • Correlation Matrix with Pandas
  • -
  • Correlation Matrix with Pandas and the Franke function
  • -
  • Rewriting the Covariance and/or Correlation Matrix
  • -
  • Linking with the SVD
  • -
  • What does it mean?
  • -
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • -
  • Ridge and LASSO Regression
  • -
  • Deriving the Ridge Regression Equations
  • -
  • Interpreting the Ridge results
  • -
  • More interpretations
  • -
  • Deriving the Lasso Regression Equations
  • -
  • Exercises for week 35
  • -
  • Exercise 1: Setting up various Python environments
  • -
  • Exercise 2: making your own data and exploring scikit-learn
  • -
  • Exercise 3: Normalizing our data
  • -
  • Exercise 4: Adding Ridge Regression
  • -
  • Exercise 5: Analytical exercises
  • +
  • The Jacobian
  • +
  • Derivatives, example 1
  • +
  • Meet the Hessian Matrix
  • +
  • Interpretations and optimizing our parameters
  • +
  • Own code for Ordinary Least Squares
  • +
  • Adding error analysis and training set up
  • +
  • Splitting our Data in Training and Test data
  • +
  • Examples
  • +
  • Making your own test-train splitting
  • +
  • The Boston housing data example
  • +
  • Housing data, the code
  • +
  • Reducing the number of degrees of freedom, overarching view
  • +
  • Preprocessing our data
  • +
  • Functionality in Scikit-Learn
  • +
  • More preprocessing
  • +
  • Frequently used scaling functions
  • +
  • Example of own Standard scaling
  • +
  • Min-Max Scaling
  • +
  • Testing the Means Squared Error as function of Complexity
  • +
  • More preprocessing examples, Franke function and regression
  • +
  • Material for lecture Thursday, August 31
  • +
  • Mathematical Interpretation of Ordinary Least Squares
  • +
  • Residual Error
  • +
  • Simple case
  • +
  • The singular value decomposition
  • +
  • Linear Regression Problems
  • +
  • Fixing the singularity
  • +
  • Basic math of the SVD
  • +
  • The SVD, a Fantastic Algorithm
  • +
  • Economy-size SVD
  • +
  • Codes for the SVD
  • +
  • Note about SVD Calculations
  • +
  • Mathematics of the SVD and implications
  • +
  • Example Matrix
  • +
  • Setting up the Matrix to be inverted
  • +
  • Further properties (important for our analyses later)
  • +
  • Meet the Covariance Matrix
  • +
  • Introducing the Covariance and Correlation functions
  • +
  • Covariance and Correlation Matrix
  • +
  • Correlation Function and Design/Feature Matrix
  • +
  • Covariance Matrix Examples
  • +
  • Correlation Matrix
  • +
  • Correlation Matrix with Pandas
  • +
  • Correlation Matrix with Pandas and the Franke function
  • +
  • Rewriting the Covariance and/or Correlation Matrix
  • +
  • Linking with the SVD
  • +
  • What does it mean?
  • +
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • +
  • Ridge and LASSO Regression
  • +
  • Deriving the Ridge Regression Equations
  • +
  • Interpreting the Ridge results
  • +
  • More interpretations
  • +
  • Deriving the Lasso Regression Equations
  • +
  • Exercises for week 35
  • +
  • Exercise 1: Setting up various Python environments
  • +
  • Exercise 2: making your own data and exploring scikit-learn
  • +
  • Exercise 3: Normalizing our data
  • +
  • Exercise 4: Adding Ridge Regression
  • +
  • Exercise 5: Analytical exercises
  • @@ -382,64 +391,32 @@ MathJax.Hub.Config({

     

     

     

    -

    Correlation Matrix

    +

    Covariance and Correlation Matrix

    -

    The previous example can be converted into the correlation matrix by -simply scaling the matrix elements with the variances. We should also -subtract the mean values for each column. This leads to the following -code which sets up the correlations matrix for the previous example in -a more brute force way. Here we scale the mean values for each column of the design matrix, calculate the relevant mean values and variances and then finally set up the \( 2\times 2 \) correlation matrix (since we have only two vectors). +

    The covariance takes values between zero and infinity and may thus +lead to problems with loss of numerical precision for particularly +large values. It is common to scale the covariance matrix by +introducing instead the correlation matrix defined via the so-called +correlation function

    +$$ +\mathrm{corr}[\boldsymbol{x},\boldsymbol{y}]=\frac{\mathrm{cov}[\boldsymbol{x},\boldsymbol{y}]}{\sqrt{\mathrm{var}[\boldsymbol{x}] \mathrm{var}[\boldsymbol{y}]}}. +$$ - -
    -
    -
    -
    -
    -
    import numpy as np
    -n = 100
    -# define two vectors                                                                                           
    -x = np.random.random(size=n)
    -y = 4+3*x+np.random.normal(size=n)
    -#scaling the x and y vectors                                                                                   
    -x = x - np.mean(x)
    -y = y - np.mean(y)
    -variance_x = np.sum(x@x)/n
    -variance_y = np.sum(y@y)/n
    -print(variance_x)
    -print(variance_y)
    -cov_xy = np.sum(x@y)/n
    -cov_xx = np.sum(x@x)/n
    -cov_yy = np.sum(y@y)/n
    -C = np.zeros((2,2))
    -C[0,0]= cov_xx/variance_x
    -C[1,1]= cov_yy/variance_y
    -C[0,1]= cov_xy/np.sqrt(variance_y*variance_x)
    -C[1,0]= C[0,1]
    -print(C)
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    - -

    We see that the matrix elements along the diagonal are one as they -should be and that the matrix is symmetric. Furthermore, diagonalizing -this matrix we easily see that it is a positive definite matrix. +

    The correlation function is then given by values \( \mathrm{corr}[\boldsymbol{x},\boldsymbol{y}] +\in [-1,1] \). This avoids eventual problems with too large values. We +can then define the correlation matrix for the two vectors \( \boldsymbol{x} \) +and \( \boldsymbol{y} \) as

    -

    The above procedure with numpy can be made more compact if we use pandas.

    +$$ +\boldsymbol{K}[\boldsymbol{x},\boldsymbol{y}] = \begin{bmatrix} 1 & \mathrm{corr}[\boldsymbol{x},\boldsymbol{y}] \\ + \mathrm{corr}[\boldsymbol{y},\boldsymbol{x}] & 1 \\ + \end{bmatrix}, +$$ + +

    In the above example this is the function we constructed using pandas.

    @@ -466,7 +443,7 @@ this matrix we easily see that it is a positive definite matrix.

  • 64
  • 65
  • ...
  • -
  • 68
  • +
  • 71
  • »
  • diff --git a/doc/pub/week35/html/._week35-bs056.html b/doc/pub/week35/html/._week35-bs056.html index 4b6d548a3..dae40d18b 100644 --- a/doc/pub/week35/html/._week35-bs056.html +++ b/doc/pub/week35/html/._week35-bs056.html @@ -74,10 +74,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'optimizing-our-parameters'), - ('Our model for the nuclear binding energies', + ('Examples relevant for the exercises', 2, None, - 'our-model-for-the-nuclear-binding-energies'), + 'examples-relevant-for-the-exercises'), ('Optimizing our parameters, more details', 2, None, @@ -94,6 +94,8 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'some-useful-matrix-and-vector-expressions'), + ('The Jacobian', 2, None, 'the-jacobian'), + ('Derivatives, example 1', 2, None, 'derivatives-example-1'), ('Meet the Hessian Matrix', 2, None, 'meet-the-hessian-matrix'), ('Interpretations and optimizing our parameters', 2, @@ -148,6 +150,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'more-preprocessing-examples-franke-function-and-regression'), + ('Material for lecture Thursday, August 31', + 2, + None, + 'material-for-lecture-thursday-august-31'), ('Mathematical Interpretation of Ordinary Least Squares', 2, None, @@ -303,74 +309,77 @@ MathJax.Hub.Config({
  • Why Linear Regression (aka Ordinary Least Squares and family), repeat from last week
  • Regression analysis, overarching aims
  • Regression analysis, overarching aims II
  • -
  • Examples
  • +
  • Examples
  • General linear models
  • Rewriting the fitting procedure as a linear algebra problem
  • Rewriting the fitting procedure as a linear algebra problem, more details
  • Generalizing the fitting procedure as a linear algebra problem
  • Generalizing the fitting procedure as a linear algebra problem
  • Optimizing our parameters
  • -
  • Our model for the nuclear binding energies
  • +
  • Examples relevant for the exercises
  • Optimizing our parameters, more details
  • -
  • Interpretations and optimizing our parameters
  • -
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • Some useful matrix and vector expressions
  • -
  • Meet the Hessian Matrix
  • -
  • Interpretations and optimizing our parameters
  • -
  • Own code for Ordinary Least Squares
  • -
  • Adding error analysis and training set up
  • -
  • Splitting our Data in Training and Test data
  • -
  • Examples
  • -
  • Making your own test-train splitting
  • -
  • The Boston housing data example
  • -
  • Housing data, the code
  • -
  • Reducing the number of degrees of freedom, overarching view
  • -
  • Preprocessing our data
  • -
  • Functionality in Scikit-Learn
  • -
  • More preprocessing
  • -
  • Frequently used scaling functions
  • -
  • Example of own Standard scaling
  • -
  • Min-Max Scaling
  • -
  • Testing the Means Squared Error as function of Complexity
  • -
  • More preprocessing examples, Franke function and regression
  • -
  • Mathematical Interpretation of Ordinary Least Squares
  • -
  • Residual Error
  • -
  • Simple case
  • -
  • The singular value decomposition
  • -
  • Linear Regression Problems
  • -
  • Fixing the singularity
  • -
  • Basic math of the SVD
  • -
  • The SVD, a Fantastic Algorithm
  • -
  • Economy-size SVD
  • -
  • Codes for the SVD
  • -
  • Note about SVD Calculations
  • -
  • Mathematics of the SVD and implications
  • -
  • Example Matrix
  • -
  • Setting up the Matrix to be inverted
  • -
  • Further properties (important for our analyses later)
  • -
  • Meet the Covariance Matrix
  • -
  • Introducing the Covariance and Correlation functions
  • -
  • Covariance and Correlation Matrix
  • -
  • Correlation Function and Design/Feature Matrix
  • -
  • Covariance Matrix Examples
  • -
  • Correlation Matrix
  • -
  • Correlation Matrix with Pandas
  • -
  • Correlation Matrix with Pandas and the Franke function
  • -
  • Rewriting the Covariance and/or Correlation Matrix
  • -
  • Linking with the SVD
  • -
  • What does it mean?
  • -
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • -
  • Ridge and LASSO Regression
  • -
  • Deriving the Ridge Regression Equations
  • -
  • Interpreting the Ridge results
  • -
  • More interpretations
  • -
  • Deriving the Lasso Regression Equations
  • -
  • Exercises for week 35
  • -
  • Exercise 1: Setting up various Python environments
  • -
  • Exercise 2: making your own data and exploring scikit-learn
  • -
  • Exercise 3: Normalizing our data
  • -
  • Exercise 4: Adding Ridge Regression
  • -
  • Exercise 5: Analytical exercises
  • +
  • The Jacobian
  • +
  • Derivatives, example 1
  • +
  • Meet the Hessian Matrix
  • +
  • Interpretations and optimizing our parameters
  • +
  • Own code for Ordinary Least Squares
  • +
  • Adding error analysis and training set up
  • +
  • Splitting our Data in Training and Test data
  • +
  • Examples
  • +
  • Making your own test-train splitting
  • +
  • The Boston housing data example
  • +
  • Housing data, the code
  • +
  • Reducing the number of degrees of freedom, overarching view
  • +
  • Preprocessing our data
  • +
  • Functionality in Scikit-Learn
  • +
  • More preprocessing
  • +
  • Frequently used scaling functions
  • +
  • Example of own Standard scaling
  • +
  • Min-Max Scaling
  • +
  • Testing the Means Squared Error as function of Complexity
  • +
  • More preprocessing examples, Franke function and regression
  • +
  • Material for lecture Thursday, August 31
  • +
  • Mathematical Interpretation of Ordinary Least Squares
  • +
  • Residual Error
  • +
  • Simple case
  • +
  • The singular value decomposition
  • +
  • Linear Regression Problems
  • +
  • Fixing the singularity
  • +
  • Basic math of the SVD
  • +
  • The SVD, a Fantastic Algorithm
  • +
  • Economy-size SVD
  • +
  • Codes for the SVD
  • +
  • Note about SVD Calculations
  • +
  • Mathematics of the SVD and implications
  • +
  • Example Matrix
  • +
  • Setting up the Matrix to be inverted
  • +
  • Further properties (important for our analyses later)
  • +
  • Meet the Covariance Matrix
  • +
  • Introducing the Covariance and Correlation functions
  • +
  • Covariance and Correlation Matrix
  • +
  • Correlation Function and Design/Feature Matrix
  • +
  • Covariance Matrix Examples
  • +
  • Correlation Matrix
  • +
  • Correlation Matrix with Pandas
  • +
  • Correlation Matrix with Pandas and the Franke function
  • +
  • Rewriting the Covariance and/or Correlation Matrix
  • +
  • Linking with the SVD
  • +
  • What does it mean?
  • +
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • +
  • Ridge and LASSO Regression
  • +
  • Deriving the Ridge Regression Equations
  • +
  • Interpreting the Ridge results
  • +
  • More interpretations
  • +
  • Deriving the Lasso Regression Equations
  • +
  • Exercises for week 35
  • +
  • Exercise 1: Setting up various Python environments
  • +
  • Exercise 2: making your own data and exploring scikit-learn
  • +
  • Exercise 3: Normalizing our data
  • +
  • Exercise 4: Adding Ridge Regression
  • +
  • Exercise 5: Analytical exercises
  • @@ -382,46 +391,65 @@ MathJax.Hub.Config({

     

     

     

    -

    Correlation Matrix with Pandas

    +

    Correlation Function and Design/Feature Matrix

    -

    We whow here how we can set up the correlation matrix using pandas, as done in this simple code

    +

    In our derivation of the various regression algorithms like Ordinary Least Squares or Ridge regression +we defined the design/feature matrix \( \boldsymbol{X} \) as +

    - -
    -
    -
    -
    -
    -
    import numpy as np
    -import pandas as pd
    -n = 10
    -x = np.random.normal(size=n)
    -x = x - np.mean(x)
    -y = 4+3*x+np.random.normal(size=n)
    -y = y - np.mean(y)
    -# Note that we transpose the matrix in order to stay with our ordering n x p
    -X = (np.vstack((x, y))).T
    -print(X)
    -Xpd = pd.DataFrame(X)
    -print(Xpd)
    -correlation_matrix = Xpd.corr()
    -print(correlation_matrix)
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    +$$ +\boldsymbol{X}=\begin{bmatrix} +x_{0,0} & x_{0,1} & x_{0,2}& \dots & \dots x_{0,p-1}\\ +x_{1,0} & x_{1,1} & x_{1,2}& \dots & \dots x_{1,p-1}\\ +x_{2,0} & x_{2,1} & x_{2,2}& \dots & \dots x_{2,p-1}\\ +\dots & \dots & \dots & \dots \dots & \dots \\ +x_{n-2,0} & x_{n-2,1} & x_{n-2,2}& \dots & \dots x_{n-2,p-1}\\ +x_{n-1,0} & x_{n-1,1} & x_{n-1,2}& \dots & \dots x_{n-1,p-1}\\ +\end{bmatrix}, +$$ + +

    with \( \boldsymbol{X}\in {\mathbb{R}}^{n\times p} \), with the predictors/features \( p \) refering to the column numbers and the +entries \( n \) being the row elements. +We can rewrite the design/feature matrix in terms of its column vectors as +

    +$$ +\boldsymbol{X}=\begin{bmatrix} \boldsymbol{x}_0 & \boldsymbol{x}_1 & \boldsymbol{x}_2 & \dots & \dots & \boldsymbol{x}_{p-1}\end{bmatrix}, +$$ + +

    with a given vector

    +$$ +\boldsymbol{x}_i^T = \begin{bmatrix}x_{0,i} & x_{1,i} & x_{2,i}& \dots & \dots x_{n-1,i}\end{bmatrix}. +$$ + +

    With these definitions, we can now rewrite our \( 2\times 2 \) +correlation/covariance matrix in terms of a moe general design/feature +matrix \( \boldsymbol{X}\in {\mathbb{R}}^{n\times p} \). This leads to a \( p\times p \) +covariance matrix for the vectors \( \boldsymbol{x}_i \) with \( i=0,1,\dots,p-1 \) +

    + +$$ +\boldsymbol{C}[\boldsymbol{x}] = \begin{bmatrix} +\mathrm{var}[\boldsymbol{x}_0] & \mathrm{cov}[\boldsymbol{x}_0,\boldsymbol{x}_1] & \mathrm{cov}[\boldsymbol{x}_0,\boldsymbol{x}_2] & \dots & \dots & \mathrm{cov}[\boldsymbol{x}_0,\boldsymbol{x}_{p-1}]\\ +\mathrm{cov}[\boldsymbol{x}_1,\boldsymbol{x}_0] & \mathrm{var}[\boldsymbol{x}_1] & \mathrm{cov}[\boldsymbol{x}_1,\boldsymbol{x}_2] & \dots & \dots & \mathrm{cov}[\boldsymbol{x}_1,\boldsymbol{x}_{p-1}]\\ +\mathrm{cov}[\boldsymbol{x}_2,\boldsymbol{x}_0] & \mathrm{cov}[\boldsymbol{x}_2,\boldsymbol{x}_1] & \mathrm{var}[\boldsymbol{x}_2] & \dots & \dots & \mathrm{cov}[\boldsymbol{x}_2,\boldsymbol{x}_{p-1}]\\ +\dots & \dots & \dots & \dots & \dots & \dots \\ +\dots & \dots & \dots & \dots & \dots & \dots \\ +\mathrm{cov}[\boldsymbol{x}_{p-1},\boldsymbol{x}_0] & \mathrm{cov}[\boldsymbol{x}_{p-1},\boldsymbol{x}_1] & \mathrm{cov}[\boldsymbol{x}_{p-1},\boldsymbol{x}_{2}] & \dots & \dots & \mathrm{var}[\boldsymbol{x}_{p-1}]\\ +\end{bmatrix}, +$$ + +

    and the correlation matrix

    +$$ +\boldsymbol{K}[\boldsymbol{x}] = \begin{bmatrix} +1 & \mathrm{corr}[\boldsymbol{x}_0,\boldsymbol{x}_1] & \mathrm{corr}[\boldsymbol{x}_0,\boldsymbol{x}_2] & \dots & \dots & \mathrm{corr}[\boldsymbol{x}_0,\boldsymbol{x}_{p-1}]\\ +\mathrm{corr}[\boldsymbol{x}_1,\boldsymbol{x}_0] & 1 & \mathrm{corr}[\boldsymbol{x}_1,\boldsymbol{x}_2] & \dots & \dots & \mathrm{corr}[\boldsymbol{x}_1,\boldsymbol{x}_{p-1}]\\ +\mathrm{corr}[\boldsymbol{x}_2,\boldsymbol{x}_0] & \mathrm{corr}[\boldsymbol{x}_2,\boldsymbol{x}_1] & 1 & \dots & \dots & \mathrm{corr}[\boldsymbol{x}_2,\boldsymbol{x}_{p-1}]\\ +\dots & \dots & \dots & \dots & \dots & \dots \\ +\dots & \dots & \dots & \dots & \dots & \dots \\ +\mathrm{corr}[\boldsymbol{x}_{p-1},\boldsymbol{x}_0] & \mathrm{corr}[\boldsymbol{x}_{p-1},\boldsymbol{x}_1] & \mathrm{corr}[\boldsymbol{x}_{p-1},\boldsymbol{x}_{2}] & \dots & \dots & 1\\ +\end{bmatrix}, +$$ -

    We expand this model to the Franke function discussed above.

    @@ -448,7 +476,7 @@ correlation_matrix = Xpd65

  • 66
  • ...
  • -
  • 68
  • +
  • 71
  • »
  • diff --git a/doc/pub/week35/html/._week35-bs057.html b/doc/pub/week35/html/._week35-bs057.html index 5fb43b876..3b95aee16 100644 --- a/doc/pub/week35/html/._week35-bs057.html +++ b/doc/pub/week35/html/._week35-bs057.html @@ -74,10 +74,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'optimizing-our-parameters'), - ('Our model for the nuclear binding energies', + ('Examples relevant for the exercises', 2, None, - 'our-model-for-the-nuclear-binding-energies'), + 'examples-relevant-for-the-exercises'), ('Optimizing our parameters, more details', 2, None, @@ -94,6 +94,8 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'some-useful-matrix-and-vector-expressions'), + ('The Jacobian', 2, None, 'the-jacobian'), + ('Derivatives, example 1', 2, None, 'derivatives-example-1'), ('Meet the Hessian Matrix', 2, None, 'meet-the-hessian-matrix'), ('Interpretations and optimizing our parameters', 2, @@ -148,6 +150,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'more-preprocessing-examples-franke-function-and-regression'), + ('Material for lecture Thursday, August 31', + 2, + None, + 'material-for-lecture-thursday-august-31'), ('Mathematical Interpretation of Ordinary Least Squares', 2, None, @@ -303,74 +309,77 @@ MathJax.Hub.Config({
  • Why Linear Regression (aka Ordinary Least Squares and family), repeat from last week
  • Regression analysis, overarching aims
  • Regression analysis, overarching aims II
  • -
  • Examples
  • +
  • Examples
  • General linear models
  • Rewriting the fitting procedure as a linear algebra problem
  • Rewriting the fitting procedure as a linear algebra problem, more details
  • Generalizing the fitting procedure as a linear algebra problem
  • Generalizing the fitting procedure as a linear algebra problem
  • Optimizing our parameters
  • -
  • Our model for the nuclear binding energies
  • +
  • Examples relevant for the exercises
  • Optimizing our parameters, more details
  • -
  • Interpretations and optimizing our parameters
  • -
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • Some useful matrix and vector expressions
  • -
  • Meet the Hessian Matrix
  • -
  • Interpretations and optimizing our parameters
  • -
  • Own code for Ordinary Least Squares
  • -
  • Adding error analysis and training set up
  • -
  • Splitting our Data in Training and Test data
  • -
  • Examples
  • -
  • Making your own test-train splitting
  • -
  • The Boston housing data example
  • -
  • Housing data, the code
  • -
  • Reducing the number of degrees of freedom, overarching view
  • -
  • Preprocessing our data
  • -
  • Functionality in Scikit-Learn
  • -
  • More preprocessing
  • -
  • Frequently used scaling functions
  • -
  • Example of own Standard scaling
  • -
  • Min-Max Scaling
  • -
  • Testing the Means Squared Error as function of Complexity
  • -
  • More preprocessing examples, Franke function and regression
  • -
  • Mathematical Interpretation of Ordinary Least Squares
  • -
  • Residual Error
  • -
  • Simple case
  • -
  • The singular value decomposition
  • -
  • Linear Regression Problems
  • -
  • Fixing the singularity
  • -
  • Basic math of the SVD
  • -
  • The SVD, a Fantastic Algorithm
  • -
  • Economy-size SVD
  • -
  • Codes for the SVD
  • -
  • Note about SVD Calculations
  • -
  • Mathematics of the SVD and implications
  • -
  • Example Matrix
  • -
  • Setting up the Matrix to be inverted
  • -
  • Further properties (important for our analyses later)
  • -
  • Meet the Covariance Matrix
  • -
  • Introducing the Covariance and Correlation functions
  • -
  • Covariance and Correlation Matrix
  • -
  • Correlation Function and Design/Feature Matrix
  • -
  • Covariance Matrix Examples
  • -
  • Correlation Matrix
  • -
  • Correlation Matrix with Pandas
  • -
  • Correlation Matrix with Pandas and the Franke function
  • -
  • Rewriting the Covariance and/or Correlation Matrix
  • -
  • Linking with the SVD
  • -
  • What does it mean?
  • -
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • -
  • Ridge and LASSO Regression
  • -
  • Deriving the Ridge Regression Equations
  • -
  • Interpreting the Ridge results
  • -
  • More interpretations
  • -
  • Deriving the Lasso Regression Equations
  • -
  • Exercises for week 35
  • -
  • Exercise 1: Setting up various Python environments
  • -
  • Exercise 2: making your own data and exploring scikit-learn
  • -
  • Exercise 3: Normalizing our data
  • -
  • Exercise 4: Adding Ridge Regression
  • -
  • Exercise 5: Analytical exercises
  • +
  • The Jacobian
  • +
  • Derivatives, example 1
  • +
  • Meet the Hessian Matrix
  • +
  • Interpretations and optimizing our parameters
  • +
  • Own code for Ordinary Least Squares
  • +
  • Adding error analysis and training set up
  • +
  • Splitting our Data in Training and Test data
  • +
  • Examples
  • +
  • Making your own test-train splitting
  • +
  • The Boston housing data example
  • +
  • Housing data, the code
  • +
  • Reducing the number of degrees of freedom, overarching view
  • +
  • Preprocessing our data
  • +
  • Functionality in Scikit-Learn
  • +
  • More preprocessing
  • +
  • Frequently used scaling functions
  • +
  • Example of own Standard scaling
  • +
  • Min-Max Scaling
  • +
  • Testing the Means Squared Error as function of Complexity
  • +
  • More preprocessing examples, Franke function and regression
  • +
  • Material for lecture Thursday, August 31
  • +
  • Mathematical Interpretation of Ordinary Least Squares
  • +
  • Residual Error
  • +
  • Simple case
  • +
  • The singular value decomposition
  • +
  • Linear Regression Problems
  • +
  • Fixing the singularity
  • +
  • Basic math of the SVD
  • +
  • The SVD, a Fantastic Algorithm
  • +
  • Economy-size SVD
  • +
  • Codes for the SVD
  • +
  • Note about SVD Calculations
  • +
  • Mathematics of the SVD and implications
  • +
  • Example Matrix
  • +
  • Setting up the Matrix to be inverted
  • +
  • Further properties (important for our analyses later)
  • +
  • Meet the Covariance Matrix
  • +
  • Introducing the Covariance and Correlation functions
  • +
  • Covariance and Correlation Matrix
  • +
  • Correlation Function and Design/Feature Matrix
  • +
  • Covariance Matrix Examples
  • +
  • Correlation Matrix
  • +
  • Correlation Matrix with Pandas
  • +
  • Correlation Matrix with Pandas and the Franke function
  • +
  • Rewriting the Covariance and/or Correlation Matrix
  • +
  • Linking with the SVD
  • +
  • What does it mean?
  • +
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • +
  • Ridge and LASSO Regression
  • +
  • Deriving the Ridge Regression Equations
  • +
  • Interpreting the Ridge results
  • +
  • More interpretations
  • +
  • Deriving the Lasso Regression Equations
  • +
  • Exercises for week 35
  • +
  • Exercise 1: Setting up various Python environments
  • +
  • Exercise 2: making your own data and exploring scikit-learn
  • +
  • Exercise 3: Normalizing our data
  • +
  • Exercise 4: Adding Ridge Regression
  • +
  • Exercise 5: Analytical exercises
  • @@ -382,7 +391,28 @@ MathJax.Hub.Config({

     

     

     

    -

    Correlation Matrix with Pandas and the Franke function

    +

    Covariance Matrix Examples

    + +

    The Numpy function np.cov calculates the covariance elements using +the factor \( 1/(n-1) \) instead of \( 1/n \) since it assumes we do not have +the exact mean values. The following simple function uses the +np.vstack function which takes each vector of dimension \( 1\times n \) +and produces a \( 2\times n \) matrix \( \boldsymbol{W} \) +

    + +

    Note that this assumes you have the features as the rows, and the inputs as columns, that is

    +$$ +\boldsymbol{W} = \begin{bmatrix} x_0 & x_1 & x_2 & \dots & x_{n-2} & x_{n-1} \\ + y_0 & y_1 & y_2 & \dots & y_{n-2} & y_{n-1} \\ + \end{bmatrix}, +$$ + +

    which in turn is converted into into the \( 2\times 2 \) covariance matrix +\( \boldsymbol{C} \) via the Numpy function np.cov(). We note that we can also calculate +the mean value of each set of samples \( \boldsymbol{x} \) etc using the Numpy +function np.mean(x). We can also extract the eigenvalues of the +covariance matrix through the np.linalg.eig() function. +

    @@ -391,49 +421,16 @@ MathJax.Hub.Config({
    -
    # Common imports
    +  
    # Importing various packages
     import numpy as np
    -import pandas as pd
    -
    -
    -def FrankeFunction(x,y):
    -	term1 = 0.75*np.exp(-(0.25*(9*x-2)**2) - 0.25*((9*y-2)**2))
    -	term2 = 0.75*np.exp(-((9*x+1)**2)/49.0 - 0.1*(9*y+1))
    -	term3 = 0.5*np.exp(-(9*x-7)**2/4.0 - 0.25*((9*y-3)**2))
    -	term4 = -0.2*np.exp(-(9*x-4)**2 - (9*y-7)**2)
    -	return term1 + term2 + term3 + term4
    -
    -
    -def create_X(x, y, n ):
    -	if len(x.shape) > 1:
    -		x = np.ravel(x)
    -		y = np.ravel(y)
    -
    -	N = len(x)
    -	l = int((n+1)*(n+2)/2)		# Number of elements in beta
    -	X = np.ones((N,l))
    -
    -	for i in range(1,n+1):
    -		q = int((i)*(i+1)/2)
    -		for k in range(i+1):
    -			X[:,q+k] = (x**(i-k))*(y**k)
    -
    -	return X
    -
    -
    -# Making meshgrid of datapoints and compute Franke's function
    -n = 4
    -N = 100
    -x = np.sort(np.random.uniform(0, 1, N))
    -y = np.sort(np.random.uniform(0, 1, N))
    -z = FrankeFunction(x, y)
    -X = create_X(x, y, n=n)    
    -
    -Xpd = pd.DataFrame(X)
    -# subtract the mean values and set up the covariance matrix
    -Xpd = Xpd - Xpd.mean()
    -covariance_matrix = Xpd.cov()
    -print(covariance_matrix)
    +n = 100
    +x = np.random.normal(size=n)
    +print(np.mean(x))
    +y = 4+3*x+np.random.normal(size=n)
    +print(np.mean(y))
    +W = np.vstack((x, y))
    +C = np.cov(W)
    +print(C)
     
    @@ -449,16 +446,6 @@ covariance_matrix = Xpd66
  • 67
  • ...
  • -
  • 68
  • +
  • 71
  • »
  • diff --git a/doc/pub/week35/html/._week35-bs058.html b/doc/pub/week35/html/._week35-bs058.html index b42efbf7d..808b5e8b5 100644 --- a/doc/pub/week35/html/._week35-bs058.html +++ b/doc/pub/week35/html/._week35-bs058.html @@ -74,10 +74,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'optimizing-our-parameters'), - ('Our model for the nuclear binding energies', + ('Examples relevant for the exercises', 2, None, - 'our-model-for-the-nuclear-binding-energies'), + 'examples-relevant-for-the-exercises'), ('Optimizing our parameters, more details', 2, None, @@ -94,6 +94,8 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'some-useful-matrix-and-vector-expressions'), + ('The Jacobian', 2, None, 'the-jacobian'), + ('Derivatives, example 1', 2, None, 'derivatives-example-1'), ('Meet the Hessian Matrix', 2, None, 'meet-the-hessian-matrix'), ('Interpretations and optimizing our parameters', 2, @@ -148,6 +150,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'more-preprocessing-examples-franke-function-and-regression'), + ('Material for lecture Thursday, August 31', + 2, + None, + 'material-for-lecture-thursday-august-31'), ('Mathematical Interpretation of Ordinary Least Squares', 2, None, @@ -303,74 +309,77 @@ MathJax.Hub.Config({
  • Why Linear Regression (aka Ordinary Least Squares and family), repeat from last week
  • Regression analysis, overarching aims
  • Regression analysis, overarching aims II
  • -
  • Examples
  • +
  • Examples
  • General linear models
  • Rewriting the fitting procedure as a linear algebra problem
  • Rewriting the fitting procedure as a linear algebra problem, more details
  • Generalizing the fitting procedure as a linear algebra problem
  • Generalizing the fitting procedure as a linear algebra problem
  • Optimizing our parameters
  • -
  • Our model for the nuclear binding energies
  • +
  • Examples relevant for the exercises
  • Optimizing our parameters, more details
  • -
  • Interpretations and optimizing our parameters
  • -
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • Some useful matrix and vector expressions
  • -
  • Meet the Hessian Matrix
  • -
  • Interpretations and optimizing our parameters
  • -
  • Own code for Ordinary Least Squares
  • -
  • Adding error analysis and training set up
  • -
  • Splitting our Data in Training and Test data
  • -
  • Examples
  • -
  • Making your own test-train splitting
  • -
  • The Boston housing data example
  • -
  • Housing data, the code
  • -
  • Reducing the number of degrees of freedom, overarching view
  • -
  • Preprocessing our data
  • -
  • Functionality in Scikit-Learn
  • -
  • More preprocessing
  • -
  • Frequently used scaling functions
  • -
  • Example of own Standard scaling
  • -
  • Min-Max Scaling
  • -
  • Testing the Means Squared Error as function of Complexity
  • -
  • More preprocessing examples, Franke function and regression
  • -
  • Mathematical Interpretation of Ordinary Least Squares
  • -
  • Residual Error
  • -
  • Simple case
  • -
  • The singular value decomposition
  • -
  • Linear Regression Problems
  • -
  • Fixing the singularity
  • -
  • Basic math of the SVD
  • -
  • The SVD, a Fantastic Algorithm
  • -
  • Economy-size SVD
  • -
  • Codes for the SVD
  • -
  • Note about SVD Calculations
  • -
  • Mathematics of the SVD and implications
  • -
  • Example Matrix
  • -
  • Setting up the Matrix to be inverted
  • -
  • Further properties (important for our analyses later)
  • -
  • Meet the Covariance Matrix
  • -
  • Introducing the Covariance and Correlation functions
  • -
  • Covariance and Correlation Matrix
  • -
  • Correlation Function and Design/Feature Matrix
  • -
  • Covariance Matrix Examples
  • -
  • Correlation Matrix
  • -
  • Correlation Matrix with Pandas
  • -
  • Correlation Matrix with Pandas and the Franke function
  • -
  • Rewriting the Covariance and/or Correlation Matrix
  • -
  • Linking with the SVD
  • -
  • What does it mean?
  • -
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • -
  • Ridge and LASSO Regression
  • -
  • Deriving the Ridge Regression Equations
  • -
  • Interpreting the Ridge results
  • -
  • More interpretations
  • -
  • Deriving the Lasso Regression Equations
  • -
  • Exercises for week 35
  • -
  • Exercise 1: Setting up various Python environments
  • -
  • Exercise 2: making your own data and exploring scikit-learn
  • -
  • Exercise 3: Normalizing our data
  • -
  • Exercise 4: Adding Ridge Regression
  • -
  • Exercise 5: Analytical exercises
  • +
  • The Jacobian
  • +
  • Derivatives, example 1
  • +
  • Meet the Hessian Matrix
  • +
  • Interpretations and optimizing our parameters
  • +
  • Own code for Ordinary Least Squares
  • +
  • Adding error analysis and training set up
  • +
  • Splitting our Data in Training and Test data
  • +
  • Examples
  • +
  • Making your own test-train splitting
  • +
  • The Boston housing data example
  • +
  • Housing data, the code
  • +
  • Reducing the number of degrees of freedom, overarching view
  • +
  • Preprocessing our data
  • +
  • Functionality in Scikit-Learn
  • +
  • More preprocessing
  • +
  • Frequently used scaling functions
  • +
  • Example of own Standard scaling
  • +
  • Min-Max Scaling
  • +
  • Testing the Means Squared Error as function of Complexity
  • +
  • More preprocessing examples, Franke function and regression
  • +
  • Material for lecture Thursday, August 31
  • +
  • Mathematical Interpretation of Ordinary Least Squares
  • +
  • Residual Error
  • +
  • Simple case
  • +
  • The singular value decomposition
  • +
  • Linear Regression Problems
  • +
  • Fixing the singularity
  • +
  • Basic math of the SVD
  • +
  • The SVD, a Fantastic Algorithm
  • +
  • Economy-size SVD
  • +
  • Codes for the SVD
  • +
  • Note about SVD Calculations
  • +
  • Mathematics of the SVD and implications
  • +
  • Example Matrix
  • +
  • Setting up the Matrix to be inverted
  • +
  • Further properties (important for our analyses later)
  • +
  • Meet the Covariance Matrix
  • +
  • Introducing the Covariance and Correlation functions
  • +
  • Covariance and Correlation Matrix
  • +
  • Correlation Function and Design/Feature Matrix
  • +
  • Covariance Matrix Examples
  • +
  • Correlation Matrix
  • +
  • Correlation Matrix with Pandas
  • +
  • Correlation Matrix with Pandas and the Franke function
  • +
  • Rewriting the Covariance and/or Correlation Matrix
  • +
  • Linking with the SVD
  • +
  • What does it mean?
  • +
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • +
  • Ridge and LASSO Regression
  • +
  • Deriving the Ridge Regression Equations
  • +
  • Interpreting the Ridge results
  • +
  • More interpretations
  • +
  • Deriving the Lasso Regression Equations
  • +
  • Exercises for week 35
  • +
  • Exercise 1: Setting up various Python environments
  • +
  • Exercise 2: making your own data and exploring scikit-learn
  • +
  • Exercise 3: Normalizing our data
  • +
  • Exercise 4: Adding Ridge Regression
  • +
  • Exercise 5: Analytical exercises
  • @@ -382,41 +391,64 @@ MathJax.Hub.Config({

     

     

     

    -

    Rewriting the Covariance and/or Correlation Matrix

    +

    Correlation Matrix

    -

    We can rewrite the covariance matrix in a more compact form in terms of the design/feature matrix \( \boldsymbol{X} \) as

    -$$ -\boldsymbol{C}[\boldsymbol{x}] = \frac{1}{n}\boldsymbol{X}^T\boldsymbol{X}= \mathbb{E}[\boldsymbol{X}^T\boldsymbol{X}]. -$$ +

    The previous example can be converted into the correlation matrix by +simply scaling the matrix elements with the variances. We should also +subtract the mean values for each column. This leads to the following +code which sets up the correlations matrix for the previous example in +a more brute force way. Here we scale the mean values for each column of the design matrix, calculate the relevant mean values and variances and then finally set up the \( 2\times 2 \) correlation matrix (since we have only two vectors). +

    -

    To see this let us simply look at a design matrix \( \boldsymbol{X}\in {\mathbb{R}}^{2\times 2} \)

    -$$ -\boldsymbol{X}=\begin{bmatrix} -x_{00} & x_{01}\\ -x_{10} & x_{11}\\ -\end{bmatrix}=\begin{bmatrix} -\boldsymbol{x}_{0} & \boldsymbol{x}_{1}\\ -\end{bmatrix}. -$$ -

    If we then compute the expectation value (note the \( 1/n \) factor instead of \( 1/(n-1) \))

    -$$ -\mathbb{E}[\boldsymbol{X}^T\boldsymbol{X}] = \frac{1}{n}\boldsymbol{X}^T\boldsymbol{X}=\frac{1}{n}\begin{bmatrix} -x_{00}^2+x_{10}^2 & x_{00}x_{01}+x_{10}x_{11}\\ -x_{01}x_{00}+x_{11}x_{10} & x_{01}^2+x_{11}^2\\ -\end{bmatrix}, -$$ + +
    +
    +
    +
    +
    +
    import numpy as np
    +n = 100
    +# define two vectors                                                                                           
    +x = np.random.random(size=n)
    +y = 4+3*x+np.random.normal(size=n)
    +#scaling the x and y vectors                                                                                   
    +x = x - np.mean(x)
    +y = y - np.mean(y)
    +variance_x = np.sum(x@x)/n
    +variance_y = np.sum(y@y)/n
    +print(variance_x)
    +print(variance_y)
    +cov_xy = np.sum(x@y)/n
    +cov_xx = np.sum(x@x)/n
    +cov_yy = np.sum(y@y)/n
    +C = np.zeros((2,2))
    +C[0,0]= cov_xx/variance_x
    +C[1,1]= cov_yy/variance_y
    +C[0,1]= cov_xy/np.sqrt(variance_y*variance_x)
    +C[1,0]= C[0,1]
    +print(C)
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    -

    which is just

    -$$ -\boldsymbol{C}[\boldsymbol{x}_0,\boldsymbol{x}_1] = \boldsymbol{C}[\boldsymbol{x}]=\begin{bmatrix} \mathrm{var}[\boldsymbol{x}_0] & \mathrm{cov}[\boldsymbol{x}_0,\boldsymbol{x}_1] \\ - \mathrm{cov}[\boldsymbol{x}_1,\boldsymbol{x}_0] & \mathrm{var}[\boldsymbol{x}_1] \\ - \end{bmatrix}, -$$ +

    We see that the matrix elements along the diagonal are one as they +should be and that the matrix is symmetric. Furthermore, diagonalizing +this matrix we easily see that it is a positive definite matrix. +

    -

    where we wrote $$\boldsymbol{C}[\boldsymbol{x}_0,\boldsymbol{x}_1] = \boldsymbol{C}[\boldsymbol{x}]$$ to indicate that this is the covariance of the vectors \( \boldsymbol{x} \) of the design/feature matrix \( \boldsymbol{X} \).

    - -

    It is easy to generalize this to a matrix \( \boldsymbol{X}\in {\mathbb{R}}^{n\times p} \).

    +

    The above procedure with numpy can be made more compact if we use pandas.

    @@ -442,6 +474,8 @@ $$

  • 66
  • 67
  • 68
  • +
  • ...
  • +
  • 71
  • »
  • diff --git a/doc/pub/week35/html/._week35-bs059.html b/doc/pub/week35/html/._week35-bs059.html index 95d74cfd2..619fea4c0 100644 --- a/doc/pub/week35/html/._week35-bs059.html +++ b/doc/pub/week35/html/._week35-bs059.html @@ -74,10 +74,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'optimizing-our-parameters'), - ('Our model for the nuclear binding energies', + ('Examples relevant for the exercises', 2, None, - 'our-model-for-the-nuclear-binding-energies'), + 'examples-relevant-for-the-exercises'), ('Optimizing our parameters, more details', 2, None, @@ -94,6 +94,8 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'some-useful-matrix-and-vector-expressions'), + ('The Jacobian', 2, None, 'the-jacobian'), + ('Derivatives, example 1', 2, None, 'derivatives-example-1'), ('Meet the Hessian Matrix', 2, None, 'meet-the-hessian-matrix'), ('Interpretations and optimizing our parameters', 2, @@ -148,6 +150,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'more-preprocessing-examples-franke-function-and-regression'), + ('Material for lecture Thursday, August 31', + 2, + None, + 'material-for-lecture-thursday-august-31'), ('Mathematical Interpretation of Ordinary Least Squares', 2, None, @@ -303,74 +309,77 @@ MathJax.Hub.Config({
  • Why Linear Regression (aka Ordinary Least Squares and family), repeat from last week
  • Regression analysis, overarching aims
  • Regression analysis, overarching aims II
  • -
  • Examples
  • +
  • Examples
  • General linear models
  • Rewriting the fitting procedure as a linear algebra problem
  • Rewriting the fitting procedure as a linear algebra problem, more details
  • Generalizing the fitting procedure as a linear algebra problem
  • Generalizing the fitting procedure as a linear algebra problem
  • Optimizing our parameters
  • -
  • Our model for the nuclear binding energies
  • +
  • Examples relevant for the exercises
  • Optimizing our parameters, more details
  • -
  • Interpretations and optimizing our parameters
  • -
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • Some useful matrix and vector expressions
  • -
  • Meet the Hessian Matrix
  • -
  • Interpretations and optimizing our parameters
  • -
  • Own code for Ordinary Least Squares
  • -
  • Adding error analysis and training set up
  • -
  • Splitting our Data in Training and Test data
  • -
  • Examples
  • -
  • Making your own test-train splitting
  • -
  • The Boston housing data example
  • -
  • Housing data, the code
  • -
  • Reducing the number of degrees of freedom, overarching view
  • -
  • Preprocessing our data
  • -
  • Functionality in Scikit-Learn
  • -
  • More preprocessing
  • -
  • Frequently used scaling functions
  • -
  • Example of own Standard scaling
  • -
  • Min-Max Scaling
  • -
  • Testing the Means Squared Error as function of Complexity
  • -
  • More preprocessing examples, Franke function and regression
  • -
  • Mathematical Interpretation of Ordinary Least Squares
  • -
  • Residual Error
  • -
  • Simple case
  • -
  • The singular value decomposition
  • -
  • Linear Regression Problems
  • -
  • Fixing the singularity
  • -
  • Basic math of the SVD
  • -
  • The SVD, a Fantastic Algorithm
  • -
  • Economy-size SVD
  • -
  • Codes for the SVD
  • -
  • Note about SVD Calculations
  • -
  • Mathematics of the SVD and implications
  • -
  • Example Matrix
  • -
  • Setting up the Matrix to be inverted
  • -
  • Further properties (important for our analyses later)
  • -
  • Meet the Covariance Matrix
  • -
  • Introducing the Covariance and Correlation functions
  • -
  • Covariance and Correlation Matrix
  • -
  • Correlation Function and Design/Feature Matrix
  • -
  • Covariance Matrix Examples
  • -
  • Correlation Matrix
  • -
  • Correlation Matrix with Pandas
  • -
  • Correlation Matrix with Pandas and the Franke function
  • -
  • Rewriting the Covariance and/or Correlation Matrix
  • -
  • Linking with the SVD
  • -
  • What does it mean?
  • -
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • -
  • Ridge and LASSO Regression
  • -
  • Deriving the Ridge Regression Equations
  • -
  • Interpreting the Ridge results
  • -
  • More interpretations
  • -
  • Deriving the Lasso Regression Equations
  • -
  • Exercises for week 35
  • -
  • Exercise 1: Setting up various Python environments
  • -
  • Exercise 2: making your own data and exploring scikit-learn
  • -
  • Exercise 3: Normalizing our data
  • -
  • Exercise 4: Adding Ridge Regression
  • -
  • Exercise 5: Analytical exercises
  • +
  • The Jacobian
  • +
  • Derivatives, example 1
  • +
  • Meet the Hessian Matrix
  • +
  • Interpretations and optimizing our parameters
  • +
  • Own code for Ordinary Least Squares
  • +
  • Adding error analysis and training set up
  • +
  • Splitting our Data in Training and Test data
  • +
  • Examples
  • +
  • Making your own test-train splitting
  • +
  • The Boston housing data example
  • +
  • Housing data, the code
  • +
  • Reducing the number of degrees of freedom, overarching view
  • +
  • Preprocessing our data
  • +
  • Functionality in Scikit-Learn
  • +
  • More preprocessing
  • +
  • Frequently used scaling functions
  • +
  • Example of own Standard scaling
  • +
  • Min-Max Scaling
  • +
  • Testing the Means Squared Error as function of Complexity
  • +
  • More preprocessing examples, Franke function and regression
  • +
  • Material for lecture Thursday, August 31
  • +
  • Mathematical Interpretation of Ordinary Least Squares
  • +
  • Residual Error
  • +
  • Simple case
  • +
  • The singular value decomposition
  • +
  • Linear Regression Problems
  • +
  • Fixing the singularity
  • +
  • Basic math of the SVD
  • +
  • The SVD, a Fantastic Algorithm
  • +
  • Economy-size SVD
  • +
  • Codes for the SVD
  • +
  • Note about SVD Calculations
  • +
  • Mathematics of the SVD and implications
  • +
  • Example Matrix
  • +
  • Setting up the Matrix to be inverted
  • +
  • Further properties (important for our analyses later)
  • +
  • Meet the Covariance Matrix
  • +
  • Introducing the Covariance and Correlation functions
  • +
  • Covariance and Correlation Matrix
  • +
  • Correlation Function and Design/Feature Matrix
  • +
  • Covariance Matrix Examples
  • +
  • Correlation Matrix
  • +
  • Correlation Matrix with Pandas
  • +
  • Correlation Matrix with Pandas and the Franke function
  • +
  • Rewriting the Covariance and/or Correlation Matrix
  • +
  • Linking with the SVD
  • +
  • What does it mean?
  • +
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • +
  • Ridge and LASSO Regression
  • +
  • Deriving the Ridge Regression Equations
  • +
  • Interpreting the Ridge results
  • +
  • More interpretations
  • +
  • Deriving the Lasso Regression Equations
  • +
  • Exercises for week 35
  • +
  • Exercise 1: Setting up various Python environments
  • +
  • Exercise 2: making your own data and exploring scikit-learn
  • +
  • Exercise 3: Normalizing our data
  • +
  • Exercise 4: Adding Ridge Regression
  • +
  • Exercise 5: Analytical exercises
  • @@ -382,39 +391,46 @@ MathJax.Hub.Config({

     

     

     

    -

    Linking with the SVD

    +

    Correlation Matrix with Pandas

    -

    We saw earlier that

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

    We whow here how we can set up the correlation matrix using pandas, as done in this simple code

    -

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

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

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

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

    meaning we can write

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

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

    -$$ -\left(\boldsymbol{X}^T\boldsymbol{X}\right)\boldsymbol{V}=\boldsymbol{V}\tilde{\boldsymbol{\Sigma}}^2. -$$ + +
    +
    +
    +
    +
    +
    import numpy as np
    +import pandas as pd
    +n = 10
    +x = np.random.normal(size=n)
    +x = x - np.mean(x)
    +y = 4+3*x+np.random.normal(size=n)
    +y = y - np.mean(y)
    +# Note that we transpose the matrix in order to stay with our ordering n x p
    +X = (np.vstack((x, y))).T
    +print(X)
    +Xpd = pd.DataFrame(X)
    +print(Xpd)
    +correlation_matrix = Xpd.corr()
    +print(correlation_matrix)
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +

    We expand this model to the Franke function discussed above.

    @@ -439,6 +455,9 @@ $$

  • 66
  • 67
  • 68
  • +
  • 69
  • +
  • ...
  • +
  • 71
  • »
  • diff --git a/doc/pub/week35/html/._week35-bs060.html b/doc/pub/week35/html/._week35-bs060.html index 831ac57e3..5c51da739 100644 --- a/doc/pub/week35/html/._week35-bs060.html +++ b/doc/pub/week35/html/._week35-bs060.html @@ -74,10 +74,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'optimizing-our-parameters'), - ('Our model for the nuclear binding energies', + ('Examples relevant for the exercises', 2, None, - 'our-model-for-the-nuclear-binding-energies'), + 'examples-relevant-for-the-exercises'), ('Optimizing our parameters, more details', 2, None, @@ -94,6 +94,8 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'some-useful-matrix-and-vector-expressions'), + ('The Jacobian', 2, None, 'the-jacobian'), + ('Derivatives, example 1', 2, None, 'derivatives-example-1'), ('Meet the Hessian Matrix', 2, None, 'meet-the-hessian-matrix'), ('Interpretations and optimizing our parameters', 2, @@ -148,6 +150,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'more-preprocessing-examples-franke-function-and-regression'), + ('Material for lecture Thursday, August 31', + 2, + None, + 'material-for-lecture-thursday-august-31'), ('Mathematical Interpretation of Ordinary Least Squares', 2, None, @@ -303,74 +309,77 @@ MathJax.Hub.Config({
  • Why Linear Regression (aka Ordinary Least Squares and family), repeat from last week
  • Regression analysis, overarching aims
  • Regression analysis, overarching aims II
  • -
  • Examples
  • +
  • Examples
  • General linear models
  • Rewriting the fitting procedure as a linear algebra problem
  • Rewriting the fitting procedure as a linear algebra problem, more details
  • Generalizing the fitting procedure as a linear algebra problem
  • Generalizing the fitting procedure as a linear algebra problem
  • Optimizing our parameters
  • -
  • Our model for the nuclear binding energies
  • +
  • Examples relevant for the exercises
  • Optimizing our parameters, more details
  • -
  • Interpretations and optimizing our parameters
  • -
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • Some useful matrix and vector expressions
  • -
  • Meet the Hessian Matrix
  • -
  • Interpretations and optimizing our parameters
  • -
  • Own code for Ordinary Least Squares
  • -
  • Adding error analysis and training set up
  • -
  • Splitting our Data in Training and Test data
  • -
  • Examples
  • -
  • Making your own test-train splitting
  • -
  • The Boston housing data example
  • -
  • Housing data, the code
  • -
  • Reducing the number of degrees of freedom, overarching view
  • -
  • Preprocessing our data
  • -
  • Functionality in Scikit-Learn
  • -
  • More preprocessing
  • -
  • Frequently used scaling functions
  • -
  • Example of own Standard scaling
  • -
  • Min-Max Scaling
  • -
  • Testing the Means Squared Error as function of Complexity
  • -
  • More preprocessing examples, Franke function and regression
  • -
  • Mathematical Interpretation of Ordinary Least Squares
  • -
  • Residual Error
  • -
  • Simple case
  • -
  • The singular value decomposition
  • -
  • Linear Regression Problems
  • -
  • Fixing the singularity
  • -
  • Basic math of the SVD
  • -
  • The SVD, a Fantastic Algorithm
  • -
  • Economy-size SVD
  • -
  • Codes for the SVD
  • -
  • Note about SVD Calculations
  • -
  • Mathematics of the SVD and implications
  • -
  • Example Matrix
  • -
  • Setting up the Matrix to be inverted
  • -
  • Further properties (important for our analyses later)
  • -
  • Meet the Covariance Matrix
  • -
  • Introducing the Covariance and Correlation functions
  • -
  • Covariance and Correlation Matrix
  • -
  • Correlation Function and Design/Feature Matrix
  • -
  • Covariance Matrix Examples
  • -
  • Correlation Matrix
  • -
  • Correlation Matrix with Pandas
  • -
  • Correlation Matrix with Pandas and the Franke function
  • -
  • Rewriting the Covariance and/or Correlation Matrix
  • -
  • Linking with the SVD
  • -
  • What does it mean?
  • -
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • -
  • Ridge and LASSO Regression
  • -
  • Deriving the Ridge Regression Equations
  • -
  • Interpreting the Ridge results
  • -
  • More interpretations
  • -
  • Deriving the Lasso Regression Equations
  • -
  • Exercises for week 35
  • -
  • Exercise 1: Setting up various Python environments
  • -
  • Exercise 2: making your own data and exploring scikit-learn
  • -
  • Exercise 3: Normalizing our data
  • -
  • Exercise 4: Adding Ridge Regression
  • -
  • Exercise 5: Analytical exercises
  • +
  • The Jacobian
  • +
  • Derivatives, example 1
  • +
  • Meet the Hessian Matrix
  • +
  • Interpretations and optimizing our parameters
  • +
  • Own code for Ordinary Least Squares
  • +
  • Adding error analysis and training set up
  • +
  • Splitting our Data in Training and Test data
  • +
  • Examples
  • +
  • Making your own test-train splitting
  • +
  • The Boston housing data example
  • +
  • Housing data, the code
  • +
  • Reducing the number of degrees of freedom, overarching view
  • +
  • Preprocessing our data
  • +
  • Functionality in Scikit-Learn
  • +
  • More preprocessing
  • +
  • Frequently used scaling functions
  • +
  • Example of own Standard scaling
  • +
  • Min-Max Scaling
  • +
  • Testing the Means Squared Error as function of Complexity
  • +
  • More preprocessing examples, Franke function and regression
  • +
  • Material for lecture Thursday, August 31
  • +
  • Mathematical Interpretation of Ordinary Least Squares
  • +
  • Residual Error
  • +
  • Simple case
  • +
  • The singular value decomposition
  • +
  • Linear Regression Problems
  • +
  • Fixing the singularity
  • +
  • Basic math of the SVD
  • +
  • The SVD, a Fantastic Algorithm
  • +
  • Economy-size SVD
  • +
  • Codes for the SVD
  • +
  • Note about SVD Calculations
  • +
  • Mathematics of the SVD and implications
  • +
  • Example Matrix
  • +
  • Setting up the Matrix to be inverted
  • +
  • Further properties (important for our analyses later)
  • +
  • Meet the Covariance Matrix
  • +
  • Introducing the Covariance and Correlation functions
  • +
  • Covariance and Correlation Matrix
  • +
  • Correlation Function and Design/Feature Matrix
  • +
  • Covariance Matrix Examples
  • +
  • Correlation Matrix
  • +
  • Correlation Matrix with Pandas
  • +
  • Correlation Matrix with Pandas and the Franke function
  • +
  • Rewriting the Covariance and/or Correlation Matrix
  • +
  • Linking with the SVD
  • +
  • What does it mean?
  • +
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • +
  • Ridge and LASSO Regression
  • +
  • Deriving the Ridge Regression Equations
  • +
  • Interpreting the Ridge results
  • +
  • More interpretations
  • +
  • Deriving the Lasso Regression Equations
  • +
  • Exercises for week 35
  • +
  • Exercise 1: Setting up various Python environments
  • +
  • Exercise 2: making your own data and exploring scikit-learn
  • +
  • Exercise 3: Normalizing our data
  • +
  • Exercise 4: Adding Ridge Regression
  • +
  • Exercise 5: Analytical exercises
  • @@ -382,44 +391,82 @@ MathJax.Hub.Config({

     

     

     

    -

    What does it mean?

    +

    Correlation Matrix with Pandas and the Franke function

    -

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

    +
    +
    +
    +
    +
    # Common imports
    +import numpy as np
    +import pandas as pd
    +
    +
    +def FrankeFunction(x,y):
    +	term1 = 0.75*np.exp(-(0.25*(9*x-2)**2) - 0.25*((9*y-2)**2))
    +	term2 = 0.75*np.exp(-((9*x+1)**2)/49.0 - 0.1*(9*y+1))
    +	term3 = 0.5*np.exp(-(9*x-7)**2/4.0 - 0.25*((9*y-3)**2))
    +	term4 = -0.2*np.exp(-(9*x-4)**2 - (9*y-7)**2)
    +	return term1 + term2 + term3 + term4
    +
    +
    +def create_X(x, y, n ):
    +	if len(x.shape) > 1:
    +		x = np.ravel(x)
    +		y = np.ravel(y)
    +
    +	N = len(x)
    +	l = int((n+1)*(n+2)/2)		# Number of elements in beta
    +	X = np.ones((N,l))
    +
    +	for i in range(1,n+1):
    +		q = int((i)*(i+1)/2)
    +		for k in range(i+1):
    +			X[:,q+k] = (x**(i-k))*(y**k)
    +
    +	return X
    +
    +
    +# Making meshgrid of datapoints and compute Franke's function
    +n = 4
    +N = 100
    +x = np.sort(np.random.uniform(0, 1, N))
    +y = np.sort(np.random.uniform(0, 1, N))
    +z = FrankeFunction(x, y)
    +X = create_X(x, y, n=n)    
    +
    +Xpd = pd.DataFrame(X)
    +# subtract the mean values and set up the covariance matrix
    +Xpd = Xpd - Xpd.mean()
    +covariance_matrix = Xpd.cov()
    +print(covariance_matrix)
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    + +

    We note here that the covariance is zero for the first rows and +columns since all matrix elements in the design matrix were set to one +(we are fitting the function in terms of a polynomial of degree \( n \)).

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

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

    - -

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

    - -

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

    - -$$ -\boldsymbol{C}[\boldsymbol{X}]=\frac{1}{n}\boldsymbol{X}^T\boldsymbol{X}, -$$ - -

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

    This means that the variance for these elements will be zero and will +cause problems when we set up the correlation matrix. We can simply +drop these elements and construct a correlation +matrix without these elements.

    @@ -444,6 +491,10 @@ absolute value of the eigenvalues of \( \boldsymbol{X} \).

  • 66
  • 67
  • 68
  • +
  • 69
  • +
  • 70
  • +
  • ...
  • +
  • 71
  • »
  • diff --git a/doc/pub/week35/html/._week35-bs061.html b/doc/pub/week35/html/._week35-bs061.html index 287982539..be2fe7e5c 100644 --- a/doc/pub/week35/html/._week35-bs061.html +++ b/doc/pub/week35/html/._week35-bs061.html @@ -74,10 +74,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'optimizing-our-parameters'), - ('Our model for the nuclear binding energies', + ('Examples relevant for the exercises', 2, None, - 'our-model-for-the-nuclear-binding-energies'), + 'examples-relevant-for-the-exercises'), ('Optimizing our parameters, more details', 2, None, @@ -94,6 +94,8 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'some-useful-matrix-and-vector-expressions'), + ('The Jacobian', 2, None, 'the-jacobian'), + ('Derivatives, example 1', 2, None, 'derivatives-example-1'), ('Meet the Hessian Matrix', 2, None, 'meet-the-hessian-matrix'), ('Interpretations and optimizing our parameters', 2, @@ -148,6 +150,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'more-preprocessing-examples-franke-function-and-regression'), + ('Material for lecture Thursday, August 31', + 2, + None, + 'material-for-lecture-thursday-august-31'), ('Mathematical Interpretation of Ordinary Least Squares', 2, None, @@ -303,74 +309,77 @@ MathJax.Hub.Config({
  • Why Linear Regression (aka Ordinary Least Squares and family), repeat from last week
  • Regression analysis, overarching aims
  • Regression analysis, overarching aims II
  • -
  • Examples
  • +
  • Examples
  • General linear models
  • Rewriting the fitting procedure as a linear algebra problem
  • Rewriting the fitting procedure as a linear algebra problem, more details
  • Generalizing the fitting procedure as a linear algebra problem
  • Generalizing the fitting procedure as a linear algebra problem
  • Optimizing our parameters
  • -
  • Our model for the nuclear binding energies
  • +
  • Examples relevant for the exercises
  • Optimizing our parameters, more details
  • -
  • Interpretations and optimizing our parameters
  • -
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • Some useful matrix and vector expressions
  • -
  • Meet the Hessian Matrix
  • -
  • Interpretations and optimizing our parameters
  • -
  • Own code for Ordinary Least Squares
  • -
  • Adding error analysis and training set up
  • -
  • Splitting our Data in Training and Test data
  • -
  • Examples
  • -
  • Making your own test-train splitting
  • -
  • The Boston housing data example
  • -
  • Housing data, the code
  • -
  • Reducing the number of degrees of freedom, overarching view
  • -
  • Preprocessing our data
  • -
  • Functionality in Scikit-Learn
  • -
  • More preprocessing
  • -
  • Frequently used scaling functions
  • -
  • Example of own Standard scaling
  • -
  • Min-Max Scaling
  • -
  • Testing the Means Squared Error as function of Complexity
  • -
  • More preprocessing examples, Franke function and regression
  • -
  • Mathematical Interpretation of Ordinary Least Squares
  • -
  • Residual Error
  • -
  • Simple case
  • -
  • The singular value decomposition
  • -
  • Linear Regression Problems
  • -
  • Fixing the singularity
  • -
  • Basic math of the SVD
  • -
  • The SVD, a Fantastic Algorithm
  • -
  • Economy-size SVD
  • -
  • Codes for the SVD
  • -
  • Note about SVD Calculations
  • -
  • Mathematics of the SVD and implications
  • -
  • Example Matrix
  • -
  • Setting up the Matrix to be inverted
  • -
  • Further properties (important for our analyses later)
  • -
  • Meet the Covariance Matrix
  • -
  • Introducing the Covariance and Correlation functions
  • -
  • Covariance and Correlation Matrix
  • -
  • Correlation Function and Design/Feature Matrix
  • -
  • Covariance Matrix Examples
  • -
  • Correlation Matrix
  • -
  • Correlation Matrix with Pandas
  • -
  • Correlation Matrix with Pandas and the Franke function
  • -
  • Rewriting the Covariance and/or Correlation Matrix
  • -
  • Linking with the SVD
  • -
  • What does it mean?
  • -
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • -
  • Ridge and LASSO Regression
  • -
  • Deriving the Ridge Regression Equations
  • -
  • Interpreting the Ridge results
  • -
  • More interpretations
  • -
  • Deriving the Lasso Regression Equations
  • -
  • Exercises for week 35
  • -
  • Exercise 1: Setting up various Python environments
  • -
  • Exercise 2: making your own data and exploring scikit-learn
  • -
  • Exercise 3: Normalizing our data
  • -
  • Exercise 4: Adding Ridge Regression
  • -
  • Exercise 5: Analytical exercises
  • +
  • The Jacobian
  • +
  • Derivatives, example 1
  • +
  • Meet the Hessian Matrix
  • +
  • Interpretations and optimizing our parameters
  • +
  • Own code for Ordinary Least Squares
  • +
  • Adding error analysis and training set up
  • +
  • Splitting our Data in Training and Test data
  • +
  • Examples
  • +
  • Making your own test-train splitting
  • +
  • The Boston housing data example
  • +
  • Housing data, the code
  • +
  • Reducing the number of degrees of freedom, overarching view
  • +
  • Preprocessing our data
  • +
  • Functionality in Scikit-Learn
  • +
  • More preprocessing
  • +
  • Frequently used scaling functions
  • +
  • Example of own Standard scaling
  • +
  • Min-Max Scaling
  • +
  • Testing the Means Squared Error as function of Complexity
  • +
  • More preprocessing examples, Franke function and regression
  • +
  • Material for lecture Thursday, August 31
  • +
  • Mathematical Interpretation of Ordinary Least Squares
  • +
  • Residual Error
  • +
  • Simple case
  • +
  • The singular value decomposition
  • +
  • Linear Regression Problems
  • +
  • Fixing the singularity
  • +
  • Basic math of the SVD
  • +
  • The SVD, a Fantastic Algorithm
  • +
  • Economy-size SVD
  • +
  • Codes for the SVD
  • +
  • Note about SVD Calculations
  • +
  • Mathematics of the SVD and implications
  • +
  • Example Matrix
  • +
  • Setting up the Matrix to be inverted
  • +
  • Further properties (important for our analyses later)
  • +
  • Meet the Covariance Matrix
  • +
  • Introducing the Covariance and Correlation functions
  • +
  • Covariance and Correlation Matrix
  • +
  • Correlation Function and Design/Feature Matrix
  • +
  • Covariance Matrix Examples
  • +
  • Correlation Matrix
  • +
  • Correlation Matrix with Pandas
  • +
  • Correlation Matrix with Pandas and the Franke function
  • +
  • Rewriting the Covariance and/or Correlation Matrix
  • +
  • Linking with the SVD
  • +
  • What does it mean?
  • +
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • +
  • Ridge and LASSO Regression
  • +
  • Deriving the Ridge Regression Equations
  • +
  • Interpreting the Ridge results
  • +
  • More interpretations
  • +
  • Deriving the Lasso Regression Equations
  • +
  • Exercises for week 35
  • +
  • Exercise 1: Setting up various Python environments
  • +
  • Exercise 2: making your own data and exploring scikit-learn
  • +
  • Exercise 3: Normalizing our data
  • +
  • Exercise 4: Adding Ridge Regression
  • +
  • Exercise 5: Analytical exercises
  • @@ -382,39 +391,41 @@ MathJax.Hub.Config({

     

     

     

    -

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

    - -

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

    +

    Rewriting the Covariance and/or Correlation Matrix

    +

    We can rewrite the covariance matrix in a more compact form in terms of the design/feature matrix \( \boldsymbol{X} \) as

    $$ -\boldsymbol{X}\boldsymbol{X}^T=\boldsymbol{U}\boldsymbol{\Sigma}\boldsymbol{V}^T\boldsymbol{V}\boldsymbol{\Sigma}^T\boldsymbol{U}^T=\boldsymbol{U}\boldsymbol{\Sigma}^T\boldsymbol{\Sigma}\boldsymbol{U}^T. +\boldsymbol{C}[\boldsymbol{x}] = \frac{1}{n}\boldsymbol{X}^T\boldsymbol{X}= \mathbb{E}[\boldsymbol{X}^T\boldsymbol{X}]. $$ -

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

    +

    To see this let us simply look at a design matrix \( \boldsymbol{X}\in {\mathbb{R}}^{2\times 2} \)

    $$ -\boldsymbol{\Sigma}\boldsymbol{\Sigma}^T = \begin{bmatrix} \tilde{\boldsymbol{\Sigma}} \\ \boldsymbol{0}\\ \end{bmatrix}\begin{bmatrix} \tilde{\boldsymbol{\Sigma}} \boldsymbol{0}\\ \end{bmatrix}=\begin{bmatrix} \tilde{\boldsymbol{\Sigma}} & \boldsymbol{0} \\ \boldsymbol{0} & \boldsymbol{0}\\ \end{bmatrix}, +\boldsymbol{X}=\begin{bmatrix} +x_{00} & x_{01}\\ +x_{10} & x_{11}\\ +\end{bmatrix}=\begin{bmatrix} +\boldsymbol{x}_{0} & \boldsymbol{x}_{1}\\ +\end{bmatrix}. $$ -

    leading to

    +

    If we then compute the expectation value (note the \( 1/n \) factor instead of \( 1/(n-1) \))

    $$ -\boldsymbol{X}\boldsymbol{X}^T=\boldsymbol{U}\begin{bmatrix} \tilde{\boldsymbol{\Sigma}} & \boldsymbol{0} \\ \boldsymbol{0} & \boldsymbol{0}\\ \end{bmatrix}\boldsymbol{U}^T. +\mathbb{E}[\boldsymbol{X}^T\boldsymbol{X}] = \frac{1}{n}\boldsymbol{X}^T\boldsymbol{X}=\frac{1}{n}\begin{bmatrix} +x_{00}^2+x_{10}^2 & x_{00}x_{01}+x_{10}x_{11}\\ +x_{01}x_{00}+x_{11}x_{10} & x_{01}^2+x_{11}^2\\ +\end{bmatrix}, $$ -

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

    +

    which is just

    $$ -(\boldsymbol{X}\boldsymbol{X}^T)\boldsymbol{U}=\boldsymbol{U}\begin{bmatrix} \tilde{\boldsymbol{\Sigma}} & \boldsymbol{0} \\ \boldsymbol{0} & \boldsymbol{0}\\ \end{bmatrix}. +\boldsymbol{C}[\boldsymbol{x}_0,\boldsymbol{x}_1] = \boldsymbol{C}[\boldsymbol{x}]=\begin{bmatrix} \mathrm{var}[\boldsymbol{x}_0] & \mathrm{cov}[\boldsymbol{x}_0,\boldsymbol{x}_1] \\ + \mathrm{cov}[\boldsymbol{x}_1,\boldsymbol{x}_0] & \mathrm{var}[\boldsymbol{x}_1] \\ + \end{bmatrix}, $$ -

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

    +

    where we wrote $$\boldsymbol{C}[\boldsymbol{x}_0,\boldsymbol{x}_1] = \boldsymbol{C}[\boldsymbol{x}]$$ to indicate that this is the covariance of the vectors \( \boldsymbol{x} \) of the design/feature matrix \( \boldsymbol{X} \).

    -

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

    +

    It is easy to generalize this to a matrix \( \boldsymbol{X}\in {\mathbb{R}}^{n\times p} \).

    @@ -437,6 +448,9 @@ values and the column vectors of \( \boldsymbol{V} \).

  • 66
  • 67
  • 68
  • +
  • 69
  • +
  • 70
  • +
  • 71
  • »
  • diff --git a/doc/pub/week35/html/._week35-bs062.html b/doc/pub/week35/html/._week35-bs062.html index e8b14ac05..f79c2719e 100644 --- a/doc/pub/week35/html/._week35-bs062.html +++ b/doc/pub/week35/html/._week35-bs062.html @@ -74,10 +74,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'optimizing-our-parameters'), - ('Our model for the nuclear binding energies', + ('Examples relevant for the exercises', 2, None, - 'our-model-for-the-nuclear-binding-energies'), + 'examples-relevant-for-the-exercises'), ('Optimizing our parameters, more details', 2, None, @@ -94,6 +94,8 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'some-useful-matrix-and-vector-expressions'), + ('The Jacobian', 2, None, 'the-jacobian'), + ('Derivatives, example 1', 2, None, 'derivatives-example-1'), ('Meet the Hessian Matrix', 2, None, 'meet-the-hessian-matrix'), ('Interpretations and optimizing our parameters', 2, @@ -148,6 +150,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'more-preprocessing-examples-franke-function-and-regression'), + ('Material for lecture Thursday, August 31', + 2, + None, + 'material-for-lecture-thursday-august-31'), ('Mathematical Interpretation of Ordinary Least Squares', 2, None, @@ -303,74 +309,77 @@ MathJax.Hub.Config({
  • Why Linear Regression (aka Ordinary Least Squares and family), repeat from last week
  • Regression analysis, overarching aims
  • Regression analysis, overarching aims II
  • -
  • Examples
  • +
  • Examples
  • General linear models
  • Rewriting the fitting procedure as a linear algebra problem
  • Rewriting the fitting procedure as a linear algebra problem, more details
  • Generalizing the fitting procedure as a linear algebra problem
  • Generalizing the fitting procedure as a linear algebra problem
  • Optimizing our parameters
  • -
  • Our model for the nuclear binding energies
  • +
  • Examples relevant for the exercises
  • Optimizing our parameters, more details
  • -
  • Interpretations and optimizing our parameters
  • -
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • Some useful matrix and vector expressions
  • -
  • Meet the Hessian Matrix
  • -
  • Interpretations and optimizing our parameters
  • -
  • Own code for Ordinary Least Squares
  • -
  • Adding error analysis and training set up
  • -
  • Splitting our Data in Training and Test data
  • -
  • Examples
  • -
  • Making your own test-train splitting
  • -
  • The Boston housing data example
  • -
  • Housing data, the code
  • -
  • Reducing the number of degrees of freedom, overarching view
  • -
  • Preprocessing our data
  • -
  • Functionality in Scikit-Learn
  • -
  • More preprocessing
  • -
  • Frequently used scaling functions
  • -
  • Example of own Standard scaling
  • -
  • Min-Max Scaling
  • -
  • Testing the Means Squared Error as function of Complexity
  • -
  • More preprocessing examples, Franke function and regression
  • -
  • Mathematical Interpretation of Ordinary Least Squares
  • -
  • Residual Error
  • -
  • Simple case
  • -
  • The singular value decomposition
  • -
  • Linear Regression Problems
  • -
  • Fixing the singularity
  • -
  • Basic math of the SVD
  • -
  • The SVD, a Fantastic Algorithm
  • -
  • Economy-size SVD
  • -
  • Codes for the SVD
  • -
  • Note about SVD Calculations
  • -
  • Mathematics of the SVD and implications
  • -
  • Example Matrix
  • -
  • Setting up the Matrix to be inverted
  • -
  • Further properties (important for our analyses later)
  • -
  • Meet the Covariance Matrix
  • -
  • Introducing the Covariance and Correlation functions
  • -
  • Covariance and Correlation Matrix
  • -
  • Correlation Function and Design/Feature Matrix
  • -
  • Covariance Matrix Examples
  • -
  • Correlation Matrix
  • -
  • Correlation Matrix with Pandas
  • -
  • Correlation Matrix with Pandas and the Franke function
  • -
  • Rewriting the Covariance and/or Correlation Matrix
  • -
  • Linking with the SVD
  • -
  • What does it mean?
  • -
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • -
  • Ridge and LASSO Regression
  • -
  • Deriving the Ridge Regression Equations
  • -
  • Interpreting the Ridge results
  • -
  • More interpretations
  • -
  • Deriving the Lasso Regression Equations
  • -
  • Exercises for week 35
  • -
  • Exercise 1: Setting up various Python environments
  • -
  • Exercise 2: making your own data and exploring scikit-learn
  • -
  • Exercise 3: Normalizing our data
  • -
  • Exercise 4: Adding Ridge Regression
  • -
  • Exercise 5: Analytical exercises
  • +
  • The Jacobian
  • +
  • Derivatives, example 1
  • +
  • Meet the Hessian Matrix
  • +
  • Interpretations and optimizing our parameters
  • +
  • Own code for Ordinary Least Squares
  • +
  • Adding error analysis and training set up
  • +
  • Splitting our Data in Training and Test data
  • +
  • Examples
  • +
  • Making your own test-train splitting
  • +
  • The Boston housing data example
  • +
  • Housing data, the code
  • +
  • Reducing the number of degrees of freedom, overarching view
  • +
  • Preprocessing our data
  • +
  • Functionality in Scikit-Learn
  • +
  • More preprocessing
  • +
  • Frequently used scaling functions
  • +
  • Example of own Standard scaling
  • +
  • Min-Max Scaling
  • +
  • Testing the Means Squared Error as function of Complexity
  • +
  • More preprocessing examples, Franke function and regression
  • +
  • Material for lecture Thursday, August 31
  • +
  • Mathematical Interpretation of Ordinary Least Squares
  • +
  • Residual Error
  • +
  • Simple case
  • +
  • The singular value decomposition
  • +
  • Linear Regression Problems
  • +
  • Fixing the singularity
  • +
  • Basic math of the SVD
  • +
  • The SVD, a Fantastic Algorithm
  • +
  • Economy-size SVD
  • +
  • Codes for the SVD
  • +
  • Note about SVD Calculations
  • +
  • Mathematics of the SVD and implications
  • +
  • Example Matrix
  • +
  • Setting up the Matrix to be inverted
  • +
  • Further properties (important for our analyses later)
  • +
  • Meet the Covariance Matrix
  • +
  • Introducing the Covariance and Correlation functions
  • +
  • Covariance and Correlation Matrix
  • +
  • Correlation Function and Design/Feature Matrix
  • +
  • Covariance Matrix Examples
  • +
  • Correlation Matrix
  • +
  • Correlation Matrix with Pandas
  • +
  • Correlation Matrix with Pandas and the Franke function
  • +
  • Rewriting the Covariance and/or Correlation Matrix
  • +
  • Linking with the SVD
  • +
  • What does it mean?
  • +
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • +
  • Ridge and LASSO Regression
  • +
  • Deriving the Ridge Regression Equations
  • +
  • Interpreting the Ridge results
  • +
  • More interpretations
  • +
  • Deriving the Lasso Regression Equations
  • +
  • Exercises for week 35
  • +
  • Exercise 1: Setting up various Python environments
  • +
  • Exercise 2: making your own data and exploring scikit-learn
  • +
  • Exercise 3: Normalizing our data
  • +
  • Exercise 4: Adding Ridge Regression
  • +
  • Exercise 5: Analytical exercises
  • @@ -382,57 +391,37 @@ MathJax.Hub.Config({

     

     

     

    -

    Ridge and LASSO Regression

    +

    Linking with the SVD

    -

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

    +

    We saw earlier that

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

    or we can state it as

    +

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

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

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

    -$$ -\vert\vert \boldsymbol{x}\vert\vert_2 = \sqrt{\sum_i x_i^2}. -$$ - -

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

    +

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

    $$ -{\displaystyle \min_{\boldsymbol{\beta}\in -{\mathbb{R}}^{p}}}\frac{1}{n}\vert\vert \boldsymbol{y}-\boldsymbol{X}\boldsymbol{\beta}\vert\vert_2^2+\lambda\vert\vert \boldsymbol{\beta}\vert\vert_2^2 +\tilde{\boldsymbol{\Sigma}}=\begin{bmatrix} \sigma_0 & 0 & 0 & \dots & 0 & 0 \\ + 0 & \sigma_1 & 0 & \dots & 0 & 0 \\ + 0 & 0 & \sigma_2 & \dots & 0 & 0 \\ + 0 & 0 & 0 & \dots & \sigma_{p-2} & 0 \\ + 0 & 0 & 0 & \dots & 0 & \sigma_{p-1} \\ +\end{bmatrix}, $$ -

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

    - +

    meaning we can write

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

    we have a new optimization equation

    +

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

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

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

    - -

    Here we have defined the norm-1 as

    -$$ -\vert\vert \boldsymbol{x}\vert\vert_1 = \sum_i \vert x_i\vert. +\left(\boldsymbol{X}^T\boldsymbol{X}\right)\boldsymbol{V}=\boldsymbol{V}\tilde{\boldsymbol{\Sigma}}^2. $$ @@ -456,6 +445,9 @@ $$
  • 66
  • 67
  • 68
  • +
  • 69
  • +
  • 70
  • +
  • 71
  • »
  • diff --git a/doc/pub/week35/html/._week35-bs063.html b/doc/pub/week35/html/._week35-bs063.html index ccf414d14..426d246a9 100644 --- a/doc/pub/week35/html/._week35-bs063.html +++ b/doc/pub/week35/html/._week35-bs063.html @@ -74,10 +74,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'optimizing-our-parameters'), - ('Our model for the nuclear binding energies', + ('Examples relevant for the exercises', 2, None, - 'our-model-for-the-nuclear-binding-energies'), + 'examples-relevant-for-the-exercises'), ('Optimizing our parameters, more details', 2, None, @@ -94,6 +94,8 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'some-useful-matrix-and-vector-expressions'), + ('The Jacobian', 2, None, 'the-jacobian'), + ('Derivatives, example 1', 2, None, 'derivatives-example-1'), ('Meet the Hessian Matrix', 2, None, 'meet-the-hessian-matrix'), ('Interpretations and optimizing our parameters', 2, @@ -148,6 +150,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'more-preprocessing-examples-franke-function-and-regression'), + ('Material for lecture Thursday, August 31', + 2, + None, + 'material-for-lecture-thursday-august-31'), ('Mathematical Interpretation of Ordinary Least Squares', 2, None, @@ -303,74 +309,77 @@ MathJax.Hub.Config({
  • Why Linear Regression (aka Ordinary Least Squares and family), repeat from last week
  • Regression analysis, overarching aims
  • Regression analysis, overarching aims II
  • -
  • Examples
  • +
  • Examples
  • General linear models
  • Rewriting the fitting procedure as a linear algebra problem
  • Rewriting the fitting procedure as a linear algebra problem, more details
  • Generalizing the fitting procedure as a linear algebra problem
  • Generalizing the fitting procedure as a linear algebra problem
  • Optimizing our parameters
  • -
  • Our model for the nuclear binding energies
  • +
  • Examples relevant for the exercises
  • Optimizing our parameters, more details
  • -
  • Interpretations and optimizing our parameters
  • -
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • Some useful matrix and vector expressions
  • -
  • Meet the Hessian Matrix
  • -
  • Interpretations and optimizing our parameters
  • -
  • Own code for Ordinary Least Squares
  • -
  • Adding error analysis and training set up
  • -
  • Splitting our Data in Training and Test data
  • -
  • Examples
  • -
  • Making your own test-train splitting
  • -
  • The Boston housing data example
  • -
  • Housing data, the code
  • -
  • Reducing the number of degrees of freedom, overarching view
  • -
  • Preprocessing our data
  • -
  • Functionality in Scikit-Learn
  • -
  • More preprocessing
  • -
  • Frequently used scaling functions
  • -
  • Example of own Standard scaling
  • -
  • Min-Max Scaling
  • -
  • Testing the Means Squared Error as function of Complexity
  • -
  • More preprocessing examples, Franke function and regression
  • -
  • Mathematical Interpretation of Ordinary Least Squares
  • -
  • Residual Error
  • -
  • Simple case
  • -
  • The singular value decomposition
  • -
  • Linear Regression Problems
  • -
  • Fixing the singularity
  • -
  • Basic math of the SVD
  • -
  • The SVD, a Fantastic Algorithm
  • -
  • Economy-size SVD
  • -
  • Codes for the SVD
  • -
  • Note about SVD Calculations
  • -
  • Mathematics of the SVD and implications
  • -
  • Example Matrix
  • -
  • Setting up the Matrix to be inverted
  • -
  • Further properties (important for our analyses later)
  • -
  • Meet the Covariance Matrix
  • -
  • Introducing the Covariance and Correlation functions
  • -
  • Covariance and Correlation Matrix
  • -
  • Correlation Function and Design/Feature Matrix
  • -
  • Covariance Matrix Examples
  • -
  • Correlation Matrix
  • -
  • Correlation Matrix with Pandas
  • -
  • Correlation Matrix with Pandas and the Franke function
  • -
  • Rewriting the Covariance and/or Correlation Matrix
  • -
  • Linking with the SVD
  • -
  • What does it mean?
  • -
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • -
  • Ridge and LASSO Regression
  • -
  • Deriving the Ridge Regression Equations
  • -
  • Interpreting the Ridge results
  • -
  • More interpretations
  • -
  • Deriving the Lasso Regression Equations
  • -
  • Exercises for week 35
  • -
  • Exercise 1: Setting up various Python environments
  • -
  • Exercise 2: making your own data and exploring scikit-learn
  • -
  • Exercise 3: Normalizing our data
  • -
  • Exercise 4: Adding Ridge Regression
  • -
  • Exercise 5: Analytical exercises
  • +
  • The Jacobian
  • +
  • Derivatives, example 1
  • +
  • Meet the Hessian Matrix
  • +
  • Interpretations and optimizing our parameters
  • +
  • Own code for Ordinary Least Squares
  • +
  • Adding error analysis and training set up
  • +
  • Splitting our Data in Training and Test data
  • +
  • Examples
  • +
  • Making your own test-train splitting
  • +
  • The Boston housing data example
  • +
  • Housing data, the code
  • +
  • Reducing the number of degrees of freedom, overarching view
  • +
  • Preprocessing our data
  • +
  • Functionality in Scikit-Learn
  • +
  • More preprocessing
  • +
  • Frequently used scaling functions
  • +
  • Example of own Standard scaling
  • +
  • Min-Max Scaling
  • +
  • Testing the Means Squared Error as function of Complexity
  • +
  • More preprocessing examples, Franke function and regression
  • +
  • Material for lecture Thursday, August 31
  • +
  • Mathematical Interpretation of Ordinary Least Squares
  • +
  • Residual Error
  • +
  • Simple case
  • +
  • The singular value decomposition
  • +
  • Linear Regression Problems
  • +
  • Fixing the singularity
  • +
  • Basic math of the SVD
  • +
  • The SVD, a Fantastic Algorithm
  • +
  • Economy-size SVD
  • +
  • Codes for the SVD
  • +
  • Note about SVD Calculations
  • +
  • Mathematics of the SVD and implications
  • +
  • Example Matrix
  • +
  • Setting up the Matrix to be inverted
  • +
  • Further properties (important for our analyses later)
  • +
  • Meet the Covariance Matrix
  • +
  • Introducing the Covariance and Correlation functions
  • +
  • Covariance and Correlation Matrix
  • +
  • Correlation Function and Design/Feature Matrix
  • +
  • Covariance Matrix Examples
  • +
  • Correlation Matrix
  • +
  • Correlation Matrix with Pandas
  • +
  • Correlation Matrix with Pandas and the Franke function
  • +
  • Rewriting the Covariance and/or Correlation Matrix
  • +
  • Linking with the SVD
  • +
  • What does it mean?
  • +
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • +
  • Ridge and LASSO Regression
  • +
  • Deriving the Ridge Regression Equations
  • +
  • Interpreting the Ridge results
  • +
  • More interpretations
  • +
  • Deriving the Lasso Regression Equations
  • +
  • Exercises for week 35
  • +
  • Exercise 1: Setting up various Python environments
  • +
  • Exercise 2: making your own data and exploring scikit-learn
  • +
  • Exercise 3: Normalizing our data
  • +
  • Exercise 4: Adding Ridge Regression
  • +
  • Exercise 5: Analytical exercises
  • @@ -382,67 +391,45 @@ MathJax.Hub.Config({

     

     

     

    -

    Deriving the Ridge Regression Equations

    +

    What does it mean?

    -

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

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

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

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

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

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

    with \( t \) a finite positive number.

    - -

    If we keep the \( 1/n \) factor, the equation for the optimal \( \beta \) changes to

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

    In many textbooks the \( 1/n \) term is often omitted. Note that a library like Scikit-Learn does not include the \( 1/n \) factor in the setup of the cost function.

    - -

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

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

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

    - -

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

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

    -

    Using our insights about the SVD of the design matrix \( \boldsymbol{X} \) -We have already analyzed the OLS solutions in terms of the eigenvectors (the columns) of the right singular value matrix \( \boldsymbol{U} \) as +$$ +\left(\boldsymbol{X}^T\boldsymbol{X}\right)\boldsymbol{v}_i=\boldsymbol{v}_i\sigma_i^2. +$$ + +

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

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

    For Ridge regression this becomes

    +

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

    + +

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

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

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

    +

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

    @@ -463,6 +450,9 @@ $$

  • 66
  • 67
  • 68
  • +
  • 69
  • +
  • 70
  • +
  • 71
  • »
  • diff --git a/doc/pub/week35/html/._week35-bs064.html b/doc/pub/week35/html/._week35-bs064.html index 750f36254..fb422c4d7 100644 --- a/doc/pub/week35/html/._week35-bs064.html +++ b/doc/pub/week35/html/._week35-bs064.html @@ -74,10 +74,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'optimizing-our-parameters'), - ('Our model for the nuclear binding energies', + ('Examples relevant for the exercises', 2, None, - 'our-model-for-the-nuclear-binding-energies'), + 'examples-relevant-for-the-exercises'), ('Optimizing our parameters, more details', 2, None, @@ -94,6 +94,8 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'some-useful-matrix-and-vector-expressions'), + ('The Jacobian', 2, None, 'the-jacobian'), + ('Derivatives, example 1', 2, None, 'derivatives-example-1'), ('Meet the Hessian Matrix', 2, None, 'meet-the-hessian-matrix'), ('Interpretations and optimizing our parameters', 2, @@ -148,6 +150,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'more-preprocessing-examples-franke-function-and-regression'), + ('Material for lecture Thursday, August 31', + 2, + None, + 'material-for-lecture-thursday-august-31'), ('Mathematical Interpretation of Ordinary Least Squares', 2, None, @@ -303,74 +309,77 @@ MathJax.Hub.Config({
  • Why Linear Regression (aka Ordinary Least Squares and family), repeat from last week
  • Regression analysis, overarching aims
  • Regression analysis, overarching aims II
  • -
  • Examples
  • +
  • Examples
  • General linear models
  • Rewriting the fitting procedure as a linear algebra problem
  • Rewriting the fitting procedure as a linear algebra problem, more details
  • Generalizing the fitting procedure as a linear algebra problem
  • Generalizing the fitting procedure as a linear algebra problem
  • Optimizing our parameters
  • -
  • Our model for the nuclear binding energies
  • +
  • Examples relevant for the exercises
  • Optimizing our parameters, more details
  • -
  • Interpretations and optimizing our parameters
  • -
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • Some useful matrix and vector expressions
  • -
  • Meet the Hessian Matrix
  • -
  • Interpretations and optimizing our parameters
  • -
  • Own code for Ordinary Least Squares
  • -
  • Adding error analysis and training set up
  • -
  • Splitting our Data in Training and Test data
  • -
  • Examples
  • -
  • Making your own test-train splitting
  • -
  • The Boston housing data example
  • -
  • Housing data, the code
  • -
  • Reducing the number of degrees of freedom, overarching view
  • -
  • Preprocessing our data
  • -
  • Functionality in Scikit-Learn
  • -
  • More preprocessing
  • -
  • Frequently used scaling functions
  • -
  • Example of own Standard scaling
  • -
  • Min-Max Scaling
  • -
  • Testing the Means Squared Error as function of Complexity
  • -
  • More preprocessing examples, Franke function and regression
  • -
  • Mathematical Interpretation of Ordinary Least Squares
  • -
  • Residual Error
  • -
  • Simple case
  • -
  • The singular value decomposition
  • -
  • Linear Regression Problems
  • -
  • Fixing the singularity
  • -
  • Basic math of the SVD
  • -
  • The SVD, a Fantastic Algorithm
  • -
  • Economy-size SVD
  • -
  • Codes for the SVD
  • -
  • Note about SVD Calculations
  • -
  • Mathematics of the SVD and implications
  • -
  • Example Matrix
  • -
  • Setting up the Matrix to be inverted
  • -
  • Further properties (important for our analyses later)
  • -
  • Meet the Covariance Matrix
  • -
  • Introducing the Covariance and Correlation functions
  • -
  • Covariance and Correlation Matrix
  • -
  • Correlation Function and Design/Feature Matrix
  • -
  • Covariance Matrix Examples
  • -
  • Correlation Matrix
  • -
  • Correlation Matrix with Pandas
  • -
  • Correlation Matrix with Pandas and the Franke function
  • -
  • Rewriting the Covariance and/or Correlation Matrix
  • -
  • Linking with the SVD
  • -
  • What does it mean?
  • -
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • -
  • Ridge and LASSO Regression
  • -
  • Deriving the Ridge Regression Equations
  • -
  • Interpreting the Ridge results
  • -
  • More interpretations
  • -
  • Deriving the Lasso Regression Equations
  • -
  • Exercises for week 35
  • -
  • Exercise 1: Setting up various Python environments
  • -
  • Exercise 2: making your own data and exploring scikit-learn
  • -
  • Exercise 3: Normalizing our data
  • -
  • Exercise 4: Adding Ridge Regression
  • -
  • Exercise 5: Analytical exercises
  • +
  • The Jacobian
  • +
  • Derivatives, example 1
  • +
  • Meet the Hessian Matrix
  • +
  • Interpretations and optimizing our parameters
  • +
  • Own code for Ordinary Least Squares
  • +
  • Adding error analysis and training set up
  • +
  • Splitting our Data in Training and Test data
  • +
  • Examples
  • +
  • Making your own test-train splitting
  • +
  • The Boston housing data example
  • +
  • Housing data, the code
  • +
  • Reducing the number of degrees of freedom, overarching view
  • +
  • Preprocessing our data
  • +
  • Functionality in Scikit-Learn
  • +
  • More preprocessing
  • +
  • Frequently used scaling functions
  • +
  • Example of own Standard scaling
  • +
  • Min-Max Scaling
  • +
  • Testing the Means Squared Error as function of Complexity
  • +
  • More preprocessing examples, Franke function and regression
  • +
  • Material for lecture Thursday, August 31
  • +
  • Mathematical Interpretation of Ordinary Least Squares
  • +
  • Residual Error
  • +
  • Simple case
  • +
  • The singular value decomposition
  • +
  • Linear Regression Problems
  • +
  • Fixing the singularity
  • +
  • Basic math of the SVD
  • +
  • The SVD, a Fantastic Algorithm
  • +
  • Economy-size SVD
  • +
  • Codes for the SVD
  • +
  • Note about SVD Calculations
  • +
  • Mathematics of the SVD and implications
  • +
  • Example Matrix
  • +
  • Setting up the Matrix to be inverted
  • +
  • Further properties (important for our analyses later)
  • +
  • Meet the Covariance Matrix
  • +
  • Introducing the Covariance and Correlation functions
  • +
  • Covariance and Correlation Matrix
  • +
  • Correlation Function and Design/Feature Matrix
  • +
  • Covariance Matrix Examples
  • +
  • Correlation Matrix
  • +
  • Correlation Matrix with Pandas
  • +
  • Correlation Matrix with Pandas and the Franke function
  • +
  • Rewriting the Covariance and/or Correlation Matrix
  • +
  • Linking with the SVD
  • +
  • What does it mean?
  • +
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • +
  • Ridge and LASSO Regression
  • +
  • Deriving the Ridge Regression Equations
  • +
  • Interpreting the Ridge results
  • +
  • More interpretations
  • +
  • Deriving the Lasso Regression Equations
  • +
  • Exercises for week 35
  • +
  • Exercise 1: Setting up various Python environments
  • +
  • Exercise 2: making your own data and exploring scikit-learn
  • +
  • Exercise 3: Normalizing our data
  • +
  • Exercise 4: Adding Ridge Regression
  • +
  • Exercise 5: Analytical exercises
  • @@ -382,22 +391,39 @@ MathJax.Hub.Config({

     

     

     

    -

    Interpreting the Ridge results

    +

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

    -

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

    +

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

    $$ -\frac{\sigma_j^2}{\sigma_j^2+\lambda} \leq 1. +\boldsymbol{X}\boldsymbol{X}^T=\boldsymbol{U}\boldsymbol{\Sigma}\boldsymbol{V}^T\boldsymbol{V}\boldsymbol{\Sigma}^T\boldsymbol{U}^T=\boldsymbol{U}\boldsymbol{\Sigma}^T\boldsymbol{\Sigma}\boldsymbol{U}^T. $$ -

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

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

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

    leading to

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

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

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

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

    -

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

    +

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

    @@ -417,6 +443,9 @@ eigenvalues ordered in a descending way, that is \( \sigma_i \geq

  • 66
  • 67
  • 68
  • +
  • 69
  • +
  • 70
  • +
  • 71
  • »
  • diff --git a/doc/pub/week35/html/._week35-bs065.html b/doc/pub/week35/html/._week35-bs065.html index 6b58f2d3a..b086d03b0 100644 --- a/doc/pub/week35/html/._week35-bs065.html +++ b/doc/pub/week35/html/._week35-bs065.html @@ -74,10 +74,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'optimizing-our-parameters'), - ('Our model for the nuclear binding energies', + ('Examples relevant for the exercises', 2, None, - 'our-model-for-the-nuclear-binding-energies'), + 'examples-relevant-for-the-exercises'), ('Optimizing our parameters, more details', 2, None, @@ -94,6 +94,8 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'some-useful-matrix-and-vector-expressions'), + ('The Jacobian', 2, None, 'the-jacobian'), + ('Derivatives, example 1', 2, None, 'derivatives-example-1'), ('Meet the Hessian Matrix', 2, None, 'meet-the-hessian-matrix'), ('Interpretations and optimizing our parameters', 2, @@ -148,6 +150,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'more-preprocessing-examples-franke-function-and-regression'), + ('Material for lecture Thursday, August 31', + 2, + None, + 'material-for-lecture-thursday-august-31'), ('Mathematical Interpretation of Ordinary Least Squares', 2, None, @@ -303,74 +309,77 @@ MathJax.Hub.Config({
  • Why Linear Regression (aka Ordinary Least Squares and family), repeat from last week
  • Regression analysis, overarching aims
  • Regression analysis, overarching aims II
  • -
  • Examples
  • +
  • Examples
  • General linear models
  • Rewriting the fitting procedure as a linear algebra problem
  • Rewriting the fitting procedure as a linear algebra problem, more details
  • Generalizing the fitting procedure as a linear algebra problem
  • Generalizing the fitting procedure as a linear algebra problem
  • Optimizing our parameters
  • -
  • Our model for the nuclear binding energies
  • +
  • Examples relevant for the exercises
  • Optimizing our parameters, more details
  • -
  • Interpretations and optimizing our parameters
  • -
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • Some useful matrix and vector expressions
  • -
  • Meet the Hessian Matrix
  • -
  • Interpretations and optimizing our parameters
  • -
  • Own code for Ordinary Least Squares
  • -
  • Adding error analysis and training set up
  • -
  • Splitting our Data in Training and Test data
  • -
  • Examples
  • -
  • Making your own test-train splitting
  • -
  • The Boston housing data example
  • -
  • Housing data, the code
  • -
  • Reducing the number of degrees of freedom, overarching view
  • -
  • Preprocessing our data
  • -
  • Functionality in Scikit-Learn
  • -
  • More preprocessing
  • -
  • Frequently used scaling functions
  • -
  • Example of own Standard scaling
  • -
  • Min-Max Scaling
  • -
  • Testing the Means Squared Error as function of Complexity
  • -
  • More preprocessing examples, Franke function and regression
  • -
  • Mathematical Interpretation of Ordinary Least Squares
  • -
  • Residual Error
  • -
  • Simple case
  • -
  • The singular value decomposition
  • -
  • Linear Regression Problems
  • -
  • Fixing the singularity
  • -
  • Basic math of the SVD
  • -
  • The SVD, a Fantastic Algorithm
  • -
  • Economy-size SVD
  • -
  • Codes for the SVD
  • -
  • Note about SVD Calculations
  • -
  • Mathematics of the SVD and implications
  • -
  • Example Matrix
  • -
  • Setting up the Matrix to be inverted
  • -
  • Further properties (important for our analyses later)
  • -
  • Meet the Covariance Matrix
  • -
  • Introducing the Covariance and Correlation functions
  • -
  • Covariance and Correlation Matrix
  • -
  • Correlation Function and Design/Feature Matrix
  • -
  • Covariance Matrix Examples
  • -
  • Correlation Matrix
  • -
  • Correlation Matrix with Pandas
  • -
  • Correlation Matrix with Pandas and the Franke function
  • -
  • Rewriting the Covariance and/or Correlation Matrix
  • -
  • Linking with the SVD
  • -
  • What does it mean?
  • -
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • -
  • Ridge and LASSO Regression
  • -
  • Deriving the Ridge Regression Equations
  • -
  • Interpreting the Ridge results
  • -
  • More interpretations
  • -
  • Deriving the Lasso Regression Equations
  • -
  • Exercises for week 35
  • -
  • Exercise 1: Setting up various Python environments
  • -
  • Exercise 2: making your own data and exploring scikit-learn
  • -
  • Exercise 3: Normalizing our data
  • -
  • Exercise 4: Adding Ridge Regression
  • -
  • Exercise 5: Analytical exercises
  • +
  • The Jacobian
  • +
  • Derivatives, example 1
  • +
  • Meet the Hessian Matrix
  • +
  • Interpretations and optimizing our parameters
  • +
  • Own code for Ordinary Least Squares
  • +
  • Adding error analysis and training set up
  • +
  • Splitting our Data in Training and Test data
  • +
  • Examples
  • +
  • Making your own test-train splitting
  • +
  • The Boston housing data example
  • +
  • Housing data, the code
  • +
  • Reducing the number of degrees of freedom, overarching view
  • +
  • Preprocessing our data
  • +
  • Functionality in Scikit-Learn
  • +
  • More preprocessing
  • +
  • Frequently used scaling functions
  • +
  • Example of own Standard scaling
  • +
  • Min-Max Scaling
  • +
  • Testing the Means Squared Error as function of Complexity
  • +
  • More preprocessing examples, Franke function and regression
  • +
  • Material for lecture Thursday, August 31
  • +
  • Mathematical Interpretation of Ordinary Least Squares
  • +
  • Residual Error
  • +
  • Simple case
  • +
  • The singular value decomposition
  • +
  • Linear Regression Problems
  • +
  • Fixing the singularity
  • +
  • Basic math of the SVD
  • +
  • The SVD, a Fantastic Algorithm
  • +
  • Economy-size SVD
  • +
  • Codes for the SVD
  • +
  • Note about SVD Calculations
  • +
  • Mathematics of the SVD and implications
  • +
  • Example Matrix
  • +
  • Setting up the Matrix to be inverted
  • +
  • Further properties (important for our analyses later)
  • +
  • Meet the Covariance Matrix
  • +
  • Introducing the Covariance and Correlation functions
  • +
  • Covariance and Correlation Matrix
  • +
  • Correlation Function and Design/Feature Matrix
  • +
  • Covariance Matrix Examples
  • +
  • Correlation Matrix
  • +
  • Correlation Matrix with Pandas
  • +
  • Correlation Matrix with Pandas and the Franke function
  • +
  • Rewriting the Covariance and/or Correlation Matrix
  • +
  • Linking with the SVD
  • +
  • What does it mean?
  • +
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • +
  • Ridge and LASSO Regression
  • +
  • Deriving the Ridge Regression Equations
  • +
  • Interpreting the Ridge results
  • +
  • More interpretations
  • +
  • Deriving the Lasso Regression Equations
  • +
  • Exercises for week 35
  • +
  • Exercise 1: Setting up various Python environments
  • +
  • Exercise 2: making your own data and exploring scikit-learn
  • +
  • Exercise 3: Normalizing our data
  • +
  • Exercise 4: Adding Ridge Regression
  • +
  • Exercise 5: Analytical exercises
  • @@ -382,36 +391,60 @@ MathJax.Hub.Config({

     

     

     

    -

    More interpretations

    - -

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

    +

    Ridge and LASSO Regression

    +

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

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

    In this case the standard OLS results in

    +

    or we can state it as

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

    and

    - +

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

    $$ -\boldsymbol{\beta}^{\mathrm{Ridge}} = \left(\boldsymbol{I}+\lambda\boldsymbol{I}\right)^{-1}\boldsymbol{X}^T\boldsymbol{y}=\left(1+\lambda\right)^{-1}\boldsymbol{\beta}^{\mathrm{OLS}}, +\vert\vert \boldsymbol{x}\vert\vert_2 = \sqrt{\sum_i x_i^2}. $$ -

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

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

    -

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

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

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

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

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

    we have a new optimization equation

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

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

    + +

    Here we have defined the norm-1 as

    +$$ +\vert\vert \boldsymbol{x}\vert\vert_1 = \sum_i \vert x_i\vert. +$$ + +

    diff --git a/doc/pub/week35/html/._week35-bs066.html b/doc/pub/week35/html/._week35-bs066.html index e40cdc4ae..9709aa90c 100644 --- a/doc/pub/week35/html/._week35-bs066.html +++ b/doc/pub/week35/html/._week35-bs066.html @@ -74,10 +74,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'optimizing-our-parameters'), - ('Our model for the nuclear binding energies', + ('Examples relevant for the exercises', 2, None, - 'our-model-for-the-nuclear-binding-energies'), + 'examples-relevant-for-the-exercises'), ('Optimizing our parameters, more details', 2, None, @@ -94,6 +94,8 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'some-useful-matrix-and-vector-expressions'), + ('The Jacobian', 2, None, 'the-jacobian'), + ('Derivatives, example 1', 2, None, 'derivatives-example-1'), ('Meet the Hessian Matrix', 2, None, 'meet-the-hessian-matrix'), ('Interpretations and optimizing our parameters', 2, @@ -148,6 +150,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'more-preprocessing-examples-franke-function-and-regression'), + ('Material for lecture Thursday, August 31', + 2, + None, + 'material-for-lecture-thursday-august-31'), ('Mathematical Interpretation of Ordinary Least Squares', 2, None, @@ -303,74 +309,77 @@ MathJax.Hub.Config({
  • Why Linear Regression (aka Ordinary Least Squares and family), repeat from last week
  • Regression analysis, overarching aims
  • Regression analysis, overarching aims II
  • -
  • Examples
  • +
  • Examples
  • General linear models
  • Rewriting the fitting procedure as a linear algebra problem
  • Rewriting the fitting procedure as a linear algebra problem, more details
  • Generalizing the fitting procedure as a linear algebra problem
  • Generalizing the fitting procedure as a linear algebra problem
  • Optimizing our parameters
  • -
  • Our model for the nuclear binding energies
  • +
  • Examples relevant for the exercises
  • Optimizing our parameters, more details
  • -
  • Interpretations and optimizing our parameters
  • -
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • Some useful matrix and vector expressions
  • -
  • Meet the Hessian Matrix
  • -
  • Interpretations and optimizing our parameters
  • -
  • Own code for Ordinary Least Squares
  • -
  • Adding error analysis and training set up
  • -
  • Splitting our Data in Training and Test data
  • -
  • Examples
  • -
  • Making your own test-train splitting
  • -
  • The Boston housing data example
  • -
  • Housing data, the code
  • -
  • Reducing the number of degrees of freedom, overarching view
  • -
  • Preprocessing our data
  • -
  • Functionality in Scikit-Learn
  • -
  • More preprocessing
  • -
  • Frequently used scaling functions
  • -
  • Example of own Standard scaling
  • -
  • Min-Max Scaling
  • -
  • Testing the Means Squared Error as function of Complexity
  • -
  • More preprocessing examples, Franke function and regression
  • -
  • Mathematical Interpretation of Ordinary Least Squares
  • -
  • Residual Error
  • -
  • Simple case
  • -
  • The singular value decomposition
  • -
  • Linear Regression Problems
  • -
  • Fixing the singularity
  • -
  • Basic math of the SVD
  • -
  • The SVD, a Fantastic Algorithm
  • -
  • Economy-size SVD
  • -
  • Codes for the SVD
  • -
  • Note about SVD Calculations
  • -
  • Mathematics of the SVD and implications
  • -
  • Example Matrix
  • -
  • Setting up the Matrix to be inverted
  • -
  • Further properties (important for our analyses later)
  • -
  • Meet the Covariance Matrix
  • -
  • Introducing the Covariance and Correlation functions
  • -
  • Covariance and Correlation Matrix
  • -
  • Correlation Function and Design/Feature Matrix
  • -
  • Covariance Matrix Examples
  • -
  • Correlation Matrix
  • -
  • Correlation Matrix with Pandas
  • -
  • Correlation Matrix with Pandas and the Franke function
  • -
  • Rewriting the Covariance and/or Correlation Matrix
  • -
  • Linking with the SVD
  • -
  • What does it mean?
  • -
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • -
  • Ridge and LASSO Regression
  • -
  • Deriving the Ridge Regression Equations
  • -
  • Interpreting the Ridge results
  • -
  • More interpretations
  • -
  • Deriving the Lasso Regression Equations
  • -
  • Exercises for week 35
  • -
  • Exercise 1: Setting up various Python environments
  • -
  • Exercise 2: making your own data and exploring scikit-learn
  • -
  • Exercise 3: Normalizing our data
  • -
  • Exercise 4: Adding Ridge Regression
  • -
  • Exercise 5: Analytical exercises
  • +
  • The Jacobian
  • +
  • Derivatives, example 1
  • +
  • Meet the Hessian Matrix
  • +
  • Interpretations and optimizing our parameters
  • +
  • Own code for Ordinary Least Squares
  • +
  • Adding error analysis and training set up
  • +
  • Splitting our Data in Training and Test data
  • +
  • Examples
  • +
  • Making your own test-train splitting
  • +
  • The Boston housing data example
  • +
  • Housing data, the code
  • +
  • Reducing the number of degrees of freedom, overarching view
  • +
  • Preprocessing our data
  • +
  • Functionality in Scikit-Learn
  • +
  • More preprocessing
  • +
  • Frequently used scaling functions
  • +
  • Example of own Standard scaling
  • +
  • Min-Max Scaling
  • +
  • Testing the Means Squared Error as function of Complexity
  • +
  • More preprocessing examples, Franke function and regression
  • +
  • Material for lecture Thursday, August 31
  • +
  • Mathematical Interpretation of Ordinary Least Squares
  • +
  • Residual Error
  • +
  • Simple case
  • +
  • The singular value decomposition
  • +
  • Linear Regression Problems
  • +
  • Fixing the singularity
  • +
  • Basic math of the SVD
  • +
  • The SVD, a Fantastic Algorithm
  • +
  • Economy-size SVD
  • +
  • Codes for the SVD
  • +
  • Note about SVD Calculations
  • +
  • Mathematics of the SVD and implications
  • +
  • Example Matrix
  • +
  • Setting up the Matrix to be inverted
  • +
  • Further properties (important for our analyses later)
  • +
  • Meet the Covariance Matrix
  • +
  • Introducing the Covariance and Correlation functions
  • +
  • Covariance and Correlation Matrix
  • +
  • Correlation Function and Design/Feature Matrix
  • +
  • Covariance Matrix Examples
  • +
  • Correlation Matrix
  • +
  • Correlation Matrix with Pandas
  • +
  • Correlation Matrix with Pandas and the Franke function
  • +
  • Rewriting the Covariance and/or Correlation Matrix
  • +
  • Linking with the SVD
  • +
  • What does it mean?
  • +
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • +
  • Ridge and LASSO Regression
  • +
  • Deriving the Ridge Regression Equations
  • +
  • Interpreting the Ridge results
  • +
  • More interpretations
  • +
  • Deriving the Lasso Regression Equations
  • +
  • Exercises for week 35
  • +
  • Exercise 1: Setting up various Python environments
  • +
  • Exercise 2: making your own data and exploring scikit-learn
  • +
  • Exercise 3: Normalizing our data
  • +
  • Exercise 4: Adding Ridge Regression
  • +
  • Exercise 5: Analytical exercises
  • @@ -382,31 +391,67 @@ MathJax.Hub.Config({

     

     

     

    -

    Deriving the Lasso Regression Equations

    +

    Deriving the Ridge Regression Equations

    -

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

    +

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

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

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

    +

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

    $$ -\frac{d \vert \beta\vert}{d \boldsymbol{\beta}}=\mathrm{sgn}(\boldsymbol{\beta})=\left\{\begin{array}{cc} 1 & \beta > 0 \\-1 & \beta < 0, \end{array}\right. +\hat{\boldsymbol{\beta}}_{\mathrm{Ridge}} = \left(\boldsymbol{X}^T\boldsymbol{X}+\lambda\boldsymbol{I}\right)^{-1}\boldsymbol{X}^T\boldsymbol{y}, $$ -

    we have that the derivative of the cost function is

    +

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

    $$ -\frac{\partial C(\boldsymbol{X},\boldsymbol{\beta})}{\partial \boldsymbol{\beta}}=-2\boldsymbol{X}^T(\boldsymbol{y}-\boldsymbol{X}\boldsymbol{\beta})+\lambda sgn(\boldsymbol{\beta})=0, +\sum_{i=0}^{p-1} \beta_i^2 \leq t, $$ -

    and reordering we have

    +

    with \( t \) a finite positive number.

    + +

    If we keep the \( 1/n \) factor, the equation for the optimal \( \beta \) changes to

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

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

    +

    In many textbooks the \( 1/n \) term is often omitted. Note that a library like Scikit-Learn does not include the \( 1/n \) factor in the setup of the cost function.

    + +

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

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

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

    + +

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

    + +

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

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

    For Ridge regression this becomes

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

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

    @@ -424,6 +469,9 @@ $$

  • 66
  • 67
  • 68
  • +
  • 69
  • +
  • 70
  • +
  • 71
  • »
  • diff --git a/doc/pub/week35/html/._week35-bs067.html b/doc/pub/week35/html/._week35-bs067.html index fe1b686be..b4eabf91f 100644 --- a/doc/pub/week35/html/._week35-bs067.html +++ b/doc/pub/week35/html/._week35-bs067.html @@ -74,10 +74,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'optimizing-our-parameters'), - ('Our model for the nuclear binding energies', + ('Examples relevant for the exercises', 2, None, - 'our-model-for-the-nuclear-binding-energies'), + 'examples-relevant-for-the-exercises'), ('Optimizing our parameters, more details', 2, None, @@ -94,6 +94,8 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'some-useful-matrix-and-vector-expressions'), + ('The Jacobian', 2, None, 'the-jacobian'), + ('Derivatives, example 1', 2, None, 'derivatives-example-1'), ('Meet the Hessian Matrix', 2, None, 'meet-the-hessian-matrix'), ('Interpretations and optimizing our parameters', 2, @@ -148,6 +150,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'more-preprocessing-examples-franke-function-and-regression'), + ('Material for lecture Thursday, August 31', + 2, + None, + 'material-for-lecture-thursday-august-31'), ('Mathematical Interpretation of Ordinary Least Squares', 2, None, @@ -303,74 +309,77 @@ MathJax.Hub.Config({
  • Why Linear Regression (aka Ordinary Least Squares and family), repeat from last week
  • Regression analysis, overarching aims
  • Regression analysis, overarching aims II
  • -
  • Examples
  • +
  • Examples
  • General linear models
  • Rewriting the fitting procedure as a linear algebra problem
  • Rewriting the fitting procedure as a linear algebra problem, more details
  • Generalizing the fitting procedure as a linear algebra problem
  • Generalizing the fitting procedure as a linear algebra problem
  • Optimizing our parameters
  • -
  • Our model for the nuclear binding energies
  • +
  • Examples relevant for the exercises
  • Optimizing our parameters, more details
  • -
  • Interpretations and optimizing our parameters
  • -
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • Some useful matrix and vector expressions
  • -
  • Meet the Hessian Matrix
  • -
  • Interpretations and optimizing our parameters
  • -
  • Own code for Ordinary Least Squares
  • -
  • Adding error analysis and training set up
  • -
  • Splitting our Data in Training and Test data
  • -
  • Examples
  • -
  • Making your own test-train splitting
  • -
  • The Boston housing data example
  • -
  • Housing data, the code
  • -
  • Reducing the number of degrees of freedom, overarching view
  • -
  • Preprocessing our data
  • -
  • Functionality in Scikit-Learn
  • -
  • More preprocessing
  • -
  • Frequently used scaling functions
  • -
  • Example of own Standard scaling
  • -
  • Min-Max Scaling
  • -
  • Testing the Means Squared Error as function of Complexity
  • -
  • More preprocessing examples, Franke function and regression
  • -
  • Mathematical Interpretation of Ordinary Least Squares
  • -
  • Residual Error
  • -
  • Simple case
  • -
  • The singular value decomposition
  • -
  • Linear Regression Problems
  • -
  • Fixing the singularity
  • -
  • Basic math of the SVD
  • -
  • The SVD, a Fantastic Algorithm
  • -
  • Economy-size SVD
  • -
  • Codes for the SVD
  • -
  • Note about SVD Calculations
  • -
  • Mathematics of the SVD and implications
  • -
  • Example Matrix
  • -
  • Setting up the Matrix to be inverted
  • -
  • Further properties (important for our analyses later)
  • -
  • Meet the Covariance Matrix
  • -
  • Introducing the Covariance and Correlation functions
  • -
  • Covariance and Correlation Matrix
  • -
  • Correlation Function and Design/Feature Matrix
  • -
  • Covariance Matrix Examples
  • -
  • Correlation Matrix
  • -
  • Correlation Matrix with Pandas
  • -
  • Correlation Matrix with Pandas and the Franke function
  • -
  • Rewriting the Covariance and/or Correlation Matrix
  • -
  • Linking with the SVD
  • -
  • What does it mean?
  • -
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • -
  • Ridge and LASSO Regression
  • -
  • Deriving the Ridge Regression Equations
  • -
  • Interpreting the Ridge results
  • -
  • More interpretations
  • -
  • Deriving the Lasso Regression Equations
  • -
  • Exercises for week 35
  • -
  • Exercise 1: Setting up various Python environments
  • -
  • Exercise 2: making your own data and exploring scikit-learn
  • -
  • Exercise 3: Normalizing our data
  • -
  • Exercise 4: Adding Ridge Regression
  • -
  • Exercise 5: Analytical exercises
  • +
  • The Jacobian
  • +
  • Derivatives, example 1
  • +
  • Meet the Hessian Matrix
  • +
  • Interpretations and optimizing our parameters
  • +
  • Own code for Ordinary Least Squares
  • +
  • Adding error analysis and training set up
  • +
  • Splitting our Data in Training and Test data
  • +
  • Examples
  • +
  • Making your own test-train splitting
  • +
  • The Boston housing data example
  • +
  • Housing data, the code
  • +
  • Reducing the number of degrees of freedom, overarching view
  • +
  • Preprocessing our data
  • +
  • Functionality in Scikit-Learn
  • +
  • More preprocessing
  • +
  • Frequently used scaling functions
  • +
  • Example of own Standard scaling
  • +
  • Min-Max Scaling
  • +
  • Testing the Means Squared Error as function of Complexity
  • +
  • More preprocessing examples, Franke function and regression
  • +
  • Material for lecture Thursday, August 31
  • +
  • Mathematical Interpretation of Ordinary Least Squares
  • +
  • Residual Error
  • +
  • Simple case
  • +
  • The singular value decomposition
  • +
  • Linear Regression Problems
  • +
  • Fixing the singularity
  • +
  • Basic math of the SVD
  • +
  • The SVD, a Fantastic Algorithm
  • +
  • Economy-size SVD
  • +
  • Codes for the SVD
  • +
  • Note about SVD Calculations
  • +
  • Mathematics of the SVD and implications
  • +
  • Example Matrix
  • +
  • Setting up the Matrix to be inverted
  • +
  • Further properties (important for our analyses later)
  • +
  • Meet the Covariance Matrix
  • +
  • Introducing the Covariance and Correlation functions
  • +
  • Covariance and Correlation Matrix
  • +
  • Correlation Function and Design/Feature Matrix
  • +
  • Covariance Matrix Examples
  • +
  • Correlation Matrix
  • +
  • Correlation Matrix with Pandas
  • +
  • Correlation Matrix with Pandas and the Franke function
  • +
  • Rewriting the Covariance and/or Correlation Matrix
  • +
  • Linking with the SVD
  • +
  • What does it mean?
  • +
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • +
  • Ridge and LASSO Regression
  • +
  • Deriving the Ridge Regression Equations
  • +
  • Interpreting the Ridge results
  • +
  • More interpretations
  • +
  • Deriving the Lasso Regression Equations
  • +
  • Exercises for week 35
  • +
  • Exercise 1: Setting up various Python environments
  • +
  • Exercise 2: making your own data and exploring scikit-learn
  • +
  • Exercise 3: Normalizing our data
  • +
  • Exercise 4: Adding Ridge Regression
  • +
  • Exercise 5: Analytical exercises
  • @@ -382,610 +391,23 @@ MathJax.Hub.Config({

     

     

     

    -

    Exercises for week 35

    +

    Interpreting the Ridge results

    -

    The exercises here are meant to prepare you for work with project 1. The first exercise is a follow-up of exercise 2 from week 35 August 30-September 3).

    - - -

    Exercise 1: Setting up various Python environments

    - -

    The first exercise here is of a mere technical art. We want you to have

    -
      -
    • git as a version control software and to establish a user account on a provider like GitHub. Other providers like GitLab etc are equally fine. You can also use the University of Oslo GitHub facilities.
    • -
    • Install various Python packages
    • -
    -

    We will make extensive use of Python as programming language and its -myriad of available libraries. You will find -IPython/Jupyter notebooks invaluable in your work. You can run R -codes in the Jupyter/IPython notebooks, with the immediate benefit of -visualizing your data. You can also use compiled languages like C++, -Rust, Fortran etc if you prefer. The focus in these lectures will be -on Python. -

    - -

    If you have Python installed (we recommend Python3) and you feel -pretty familiar with installing different packages, we recommend that -you install the following Python packages via pip as -

    - -
      -
    1. pip install numpy scipy matplotlib ipython scikit-learn sympy pandas pillow
    2. -
    -

    For Tensorflow, we recommend following the instructions in the text of -Aurelien Geron, Hands‑On Machine Learning with Scikit‑Learn and TensorFlow, O'Reilly -

    - -

    We will come back to tensorflow later.

    - -

    For Python3, replace pip with pip3.

    - -

    For OSX users we recommend, after having installed Xcode, to -install brew. Brew allows for a seamless installation of additional -software via for example -

    - -
      -
    1. brew install python3
    2. -
    -

    For Linux users, with its variety of distributions like for example the widely popular Ubuntu distribution, -you can use pip as well and simply install Python as -

    - -
      -
    1. sudo apt-get install python3 (or python for Python2.7)
    2. -
    -

    If you don't want to perform these operations separately and venture -into the hassle of exploring how to set up dependencies and paths, we -recommend two widely used distrubutions which set up all relevant -dependencies for Python, namely -

    - - -

    which is an open source -distribution of the Python and R programming languages for large-scale -data processing, predictive analytics, and scientific computing, that -aims to simplify package management and deployment. Package versions -are managed by the package management system conda. -

    - - -

    is a Python -distribution for scientific and analytic computing distribution and -analysis environment, available for free and under a commercial -license. -

    - -

    We recommend using Anaconda if you are not too familiar with setting paths in a terminal environment.

    - - - - -

    Exercise 2: making your own data and exploring scikit-learn

    - -

    We will generate our own dataset for a function \( y(x) \) where \( x \in [0,1] \) and defined by random numbers computed with the uniform distribution. The function \( y \) is a quadratic polynomial in \( x \) with added stochastic noise according to the normal distribution \( \cal {N}(0,1) \). -The following simple Python instructions define our \( x \) and \( y \) values (with 100 data points). -

    - - -
    -
    -
    -
    -
    -
    x = np.random.rand(100,1)
    -y = 2.0+5*x*x+0.1*np.random.randn(100,1)
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    - - -
      -
    1. Write your own code (following the examples under the regression notes) for computing the parametrization of the data set fitting a second-order polynomial.
    2. -
    3. Use thereafter scikit-learn (see again the examples in the regression slides) and compare with your own code. When compairing with _scikit_learn_, make sure you set the option for the intercept to FALSE, see https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LinearRegression.html. This feature will be explained in more detail during the lectures of week 35 and week 36. You can find more in https://compphysics.github.io/MachineLearning/doc/LectureNotes/_build/html/chapter3.html#more-on-rescaling-data.
    4. -
    5. Using scikit-learn, compute also the mean square error, a risk metric corresponding to the expected value of the squared (quadratic) error defined as
    6. -
    -$$ MSE(\boldsymbol{y},\boldsymbol{\tilde{y}}) = \frac{1}{n} -\sum_{i=0}^{n-1}(y_i-\tilde{y}_i)^2, -$$ - -

    and the \( R^2 \) score function. -If \( \tilde{\boldsymbol{y}}_i \) is the predicted value of the \( i-th \) sample and \( y_i \) is the corresponding true value, then the score \( R^2 \) is defined as -

    -$$ -R^2(\boldsymbol{y}, \tilde{\boldsymbol{y}}) = 1 - \frac{\sum_{i=0}^{n - 1} (y_i - \tilde{y}_i)^2}{\sum_{i=0}^{n - 1} (y_i - \bar{y})^2}, -$$ - -

    where we have defined the mean value of \( \boldsymbol{y} \) as

    -$$ -\bar{y} = \frac{1}{n} \sum_{i=0}^{n - 1} y_i. -$$ - -

    You can use the functionality included in scikit-learn. If you feel for it, you can use your own program and define functions which compute the above two functions. -Discuss the meaning of these results. Try also to vary the coefficient in front of the added stochastic noise term and discuss the quality of the fits. -

    - - - -

    -

    -

    - -

    -Solution. -

    -
    -
    -

    - -

    The code here is an example of where we define our own design matrix and fit parameters \( \beta \).

    - - -
    -
    -
    -
    -
    -
    import os
    -import numpy as np
    -import pandas as pd
    -import matplotlib.pyplot as plt
    -from sklearn.model_selection import train_test_split
    -
    -def save_fig(fig_id):
    -    plt.savefig(image_path(fig_id) + ".png", format='png')
    -
    -def R2(y_data, y_model):
    -    return 1 - np.sum((y_data - y_model) ** 2) / np.sum((y_data - np.mean(y_data)) ** 2)
    -def MSE(y_data,y_model):
    -    n = np.size(y_model)
    -    return np.sum((y_data-y_model)**2)/n
    -
    -x = np.random.rand(100)
    -y = 2.0+5*x*x+0.1*np.random.randn(100)
    -
    -
    -#  The design matrix now as function of a given polynomial
    -X = np.zeros((len(x),3))
    -X[:,0] = 1.0
    -X[:,1] = x
    -X[:,2] = x**2
    -# We split the data in test and training data
    -X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
    -# matrix inversion to find beta
    -beta = np.linalg.inv(X_train.T @ X_train) @ X_train.T @ y_train
    -print(beta)
    -# and then make the prediction
    -ytilde = X_train @ beta
    -print("Training R2")
    -print(R2(y_train,ytilde))
    -print("Training MSE")
    -print(MSE(y_train,ytilde))
    -ypredict = X_test @ beta
    -print("Test R2")
    -print(R2(y_test,ypredict))
    -print("Test MSE")
    -print(MSE(y_test,ypredict))
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    - - -

    -
    -

    - - - - - - -

    Exercise 3: Normalizing our data

    - -

    A much used approach before starting to train the data is to preprocess our -data. Normally the data may need a rescaling and/or may be sensitive -to extreme values. Scaling the data renders our inputs much more -suitable for the algorithms we want to employ. -

    - -

    Scikit-Learn has several functions which allow us to rescale the -data, normally resulting in much better results in terms of various -accuracy scores. The StandardScaler function in Scikit-Learn -ensures that for each feature/predictor we study the mean value is -zero and the variance is one (every column in the design/feature -matrix). This scaling has the drawback that it does not ensure that -we have a particular maximum or minimum in our data set. Another -function included in Scikit-Learn is the MinMaxScaler which -ensures that all features are exactly between \( 0 \) and \( 1 \). The -

    - -

    The Normalizer scales each data -point such that the feature vector has a euclidean length of one. In other words, it -projects a data point on the circle (or sphere in the case of higher dimensions) with a -radius of 1. This means every data point is scaled by a different number (by the -inverse of it’s length). -This normalization is often used when only the direction (or angle) of the data matters, -not the length of the feature vector. -

    - -

    The RobustScaler works similarly to the StandardScaler in that it -ensures statistical properties for each feature that guarantee that -they are on the same scale. However, the RobustScaler uses the median -and quartiles, instead of mean and variance. This makes the -RobustScaler ignore data points that are very different from the rest -(like measurement errors). These odd data points are also called -outliers, and might often lead to trouble for other scaling -techniques. -

    - -

    It also common to split the data in a training set and a testing set. A typical split is to use \( 80\% \) of the data for training and the rest -for testing. This can be done as follows with our design matrix \( \boldsymbol{X} \) and data \( \boldsymbol{y} \) (remember to import scikit-learn) -

    - - -
    -
    -
    -
    -
    -
    # split in training and test data
    -X_train, X_test, y_train, y_test = train_test_split(X,y,test_size=0.2)
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    - -

    Then we can use the standard scaler to scale our data as

    - - -
    -
    -
    -
    -
    -
    scaler = StandardScaler()
    -scaler.fit(X_train)
    -X_train_scaled = scaler.transform(X_train)
    -X_test_scaled = scaler.transform(X_test)
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    - -

    In this exercise we want you to to compute the MSE for the training -data and the test data as function of the complexity of a polynomial, -that is the degree of a given polynomial. We want you also to compute the \( R2 \) score as function of the complexity of the model for both training data and test data. You should also run the calculation with and without scaling. -

    - -

    One of -the aims is to reproduce Figure 2.11 of Hastie et al. -

    - -

    Our data is defined by \( x\in [-3,3] \) with a total of for example \( 100 \) data points.

    - - -
    -
    -
    -
    -
    -
    np.random.seed()
    -n = 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)
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    - -

    where \( y \) is the function we want to fit with a given polynomial.

    - - -

    -a) -Write a first code which sets up a design matrix \( X \) defined by a fifth-order polynomial. Scale your data and split it in training and test data. -

    - - - - -

    -b) -Perform an ordinary least squares and compute the means squared error and the \( R2 \) factor for the training data and the test data, with and without scaling. -

    - - - - -

    -c) -Add now a model which allows you to make polynomials up to degree \( 15 \). Perform a standard OLS fitting of the training data and compute the MSE and \( R2 \) for the training and test data and plot both test and training data MSE and \( R2 \) as functions of the polynomial degree. Compare what you see with Figure 2.11 of Hastie et al. Comment your results. For which polynomial degree do you find an optimal MSE (smallest value)? -

    - - - - - - -

    Exercise 4: Adding Ridge Regression

    - -

    This exercise is a continuation of exercise 2. We will use the same function to -generate our data set, still staying with a simple function \( y(x) \) -which we want to fit using linear regression, but now extending the -analysis to include the Ridge regression method. -

    - -

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

    - -

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

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

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

    - -

    The code here allows you to perform your own Ridge calculation and -perform calculations for various values of the regularization -parameter \( \lambda \). This program can easily be extended upon. -

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

    Repeat the above but using the functionality of -Scikit-Learn. Compare your code with the results from -Scikit-Learn. Remember to run with the same random numbers for -generating \( x \) and \( y \). Observe also that when you compare with Scikit-Learn, you need to pay attention to how the intercept is dealt with. -

    - -

    Finally, using Scikit-Learn or your own code, compute also the mean square error, a risk metric corresponding to the expected value of the squared (quadratic) error defined as

    -$$ MSE(\hat{y},\hat{\tilde{y}}) = \frac{1}{n} -\sum_{i=0}^{n-1}(y_i-\tilde{y}_i)^2, -$$ - -

    and the \( R^2 \) score function. -If \( \tilde{\hat{y}}_i \) is the predicted value of the \( i-th \) sample and \( y_i \) is the corresponding true value, then the score \( R^2 \) is defined as -

    -$$ -R^2(\hat{y}, \tilde{\hat{y}}) = 1 - \frac{\sum_{i=0}^{n - 1} (y_i - \tilde{y}_i)^2}{\sum_{i=0}^{n - 1} (y_i - \bar{y})^2}, -$$ - -

    where we have defined the mean value of \( \hat{y} \) as

    -$$ -\bar{y} = \frac{1}{n} \sum_{i=0}^{n - 1} y_i. -$$ - -

    Discuss these quantities as functions of the variable \( \lambda \) in Ridge regression.

    - - - - -

    Exercise 5: Analytical exercises

    - -

    In this exercise we derive the expressions for various derivatives of -products of vectors and matrices. Such derivatives are central to the -optimization of various cost functions. Although we will often use -automatic differentiation in actual calculations, to be able to have -analytical expressions is extremely helpful in case we have simpler -derivatives as well as when we analyze various properties (like second -derivatives) of the chosen cost functions. Vectors are always written -as boldfaced lower case letters and matrices as upper case boldfaced -letters. -

    - -

    Show that

    -$$ -\frac{\partial (\boldsymbol{b}^T\boldsymbol{a})}{\partial \boldsymbol{a}} = \boldsymbol{b}, -$$ - -

    and

    -$$ -\frac{\partial (\boldsymbol{a}^T\boldsymbol{A}\boldsymbol{a})}{\partial \boldsymbol{a}} = \boldsymbol{a}^T(\boldsymbol{A}+\boldsymbol{A}^T), -$$ - -

    and

    -$$ -\frac{\partial \left(\boldsymbol{x}-\boldsymbol{A}\boldsymbol{s}\right)^T\left(\boldsymbol{x}-\boldsymbol{A}\boldsymbol{s}\right)}{\partial \boldsymbol{s}} = -2\left(\boldsymbol{x}-\boldsymbol{A}\boldsymbol{s}\right)^T\boldsymbol{A}, -$$ - -

    and finally find the second derivative of this function with respect to the vector \( \boldsymbol{s} \).

    - -

    Hint: In these exercises it is always useful to write out with summation indices the various quantities. -As an example, consider the function -

    +

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

    $$ -f(\boldsymbol{x}) =\boldsymbol{A}\boldsymbol{x}, +\frac{\sigma_j^2}{\sigma_j^2+\lambda} \leq 1. $$ -

    which reads for a specific component \( f_i \) (we define the matrix \( \boldsymbol{A} \) to have dimension \( n\times n \) and the vector $\boldsymbol{x} to have length \( n \))

    +

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

    -$$ -f_i =\sum_{j=0}^{n-1}a_{ij}x_j, -$$ +

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

    -

    which leads to

    -$$ -\frac{\partial f_i}{\partial x_j}= a_{ij}, -$$ - -

    and written out in terms of the vector \( \boldsymbol{x} \) we have

    -$$ -\frac{\partial f(\boldsymbol{x})}{\partial \boldsymbol{x}}= \boldsymbol{A}. -$$ - - -

    diff --git a/doc/pub/week35/html/._week35-bs068.html b/doc/pub/week35/html/._week35-bs068.html index ab06f4a48..1f23c58c3 100644 --- a/doc/pub/week35/html/._week35-bs068.html +++ b/doc/pub/week35/html/._week35-bs068.html @@ -38,7 +38,6 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d {'highest level': 2, 'sections': [('Plans for week 35', 2, None, 'plans-for-week-35'), ('Reading recommendations:', 3, None, 'reading-recommendations'), - ('Topics of week 35', 2, None, 'topics-of-week-35'), ('Why Linear Regression (aka Ordinary Least Squares and family), ' 'repeat from last week', 2, @@ -75,10 +74,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'optimizing-our-parameters'), - ('Our model for the nuclear binding energies', + ('Examples relevant for the exercises', 2, None, - 'our-model-for-the-nuclear-binding-energies'), + 'examples-relevant-for-the-exercises'), ('Optimizing our parameters, more details', 2, None, @@ -95,6 +94,8 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'some-useful-matrix-and-vector-expressions'), + ('The Jacobian', 2, None, 'the-jacobian'), + ('Derivatives, example 1', 2, None, 'derivatives-example-1'), ('Meet the Hessian Matrix', 2, None, 'meet-the-hessian-matrix'), ('Interpretations and optimizing our parameters', 2, @@ -149,6 +150,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'more-preprocessing-examples-franke-function-and-regression'), + ('Material for lecture Thursday, August 31', + 2, + None, + 'material-for-lecture-thursday-august-31'), ('Mathematical Interpretation of Ordinary Least Squares', 2, None, @@ -301,78 +306,80 @@ MathJax.Hub.Config({ @@ -384,610 +391,36 @@ MathJax.Hub.Config({

     

     

     

    -

    Exercises for week 35

    +

    More interpretations

    -

    The exercises here are meant to prepare you for work with project 1. The first exercise is a follow-up of exercise 2 from week 35 August 30-September 3).

    +

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

    - -

    Exercise 1: Setting up various Python environments

    - -

    The first exercise here is of a mere technical art. We want you to have

    -
      -
    • git as a version control software and to establish a user account on a provider like GitHub. Other providers like GitLab etc are equally fine. You can also use the University of Oslo GitHub facilities.
    • -
    • Install various Python packages
    • -
    -

    We will make extensive use of Python as programming language and its -myriad of available libraries. You will find -IPython/Jupyter notebooks invaluable in your work. You can run R -codes in the Jupyter/IPython notebooks, with the immediate benefit of -visualizing your data. You can also use compiled languages like C++, -Rust, Fortran etc if you prefer. The focus in these lectures will be -on Python. -

    - -

    If you have Python installed (we recommend Python3) and you feel -pretty familiar with installing different packages, we recommend that -you install the following Python packages via pip as -

    - -
      -
    1. pip install numpy scipy matplotlib ipython scikit-learn sympy pandas pillow
    2. -
    -

    For Tensorflow, we recommend following the instructions in the text of -Aurelien Geron, Hands‑On Machine Learning with Scikit‑Learn and TensorFlow, O'Reilly -

    - -

    We will come back to tensorflow later.

    - -

    For Python3, replace pip with pip3.

    - -

    For OSX users we recommend, after having installed Xcode, to -install brew. Brew allows for a seamless installation of additional -software via for example -

    - -
      -
    1. brew install python3
    2. -
    -

    For Linux users, with its variety of distributions like for example the widely popular Ubuntu distribution, -you can use pip as well and simply install Python as -

    - -
      -
    1. sudo apt-get install python3 (or python for Python2.7)
    2. -
    -

    If you don't want to perform these operations separately and venture -into the hassle of exploring how to set up dependencies and paths, we -recommend two widely used distrubutions which set up all relevant -dependencies for Python, namely -

    - - -

    which is an open source -distribution of the Python and R programming languages for large-scale -data processing, predictive analytics, and scientific computing, that -aims to simplify package management and deployment. Package versions -are managed by the package management system conda. -

    - - -

    is a Python -distribution for scientific and analytic computing distribution and -analysis environment, available for free and under a commercial -license. -

    - -

    We recommend using Anaconda if you are not too familiar with setting paths in a terminal environment.

    - - - - -

    Exercise 2: making your own data and exploring scikit-learn

    - -

    We will generate our own dataset for a function \( y(x) \) where \( x \in [0,1] \) and defined by random numbers computed with the uniform distribution. The function \( y \) is a quadratic polynomial in \( x \) with added stochastic noise according to the normal distribution \( \cal {N}(0,1) \). -The following simple Python instructions define our \( x \) and \( y \) values (with 100 data points). -

    - - -
    -
    -
    -
    -
    -
    x = np.random.rand(100,1)
    -y = 2.0+5*x*x+0.1*np.random.randn(100,1)
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    - - -
      -
    1. Write your own code (following the examples under the regression notes) for computing the parametrization of the data set fitting a second-order polynomial.
    2. -
    3. Use thereafter scikit-learn (see again the examples in the regression slides) and compare with your own code. When compairing with _scikit_learn_, make sure you set the option for the intercept to FALSE, see https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LinearRegression.html. This feature will be explained in more detail during the lectures of week 35 and week 36. You can find more in https://compphysics.github.io/MachineLearning/doc/LectureNotes/_build/html/chapter3.html#more-on-rescaling-data.
    4. -
    5. Using scikit-learn, compute also the mean square error, a risk metric corresponding to the expected value of the squared (quadratic) error defined as
    6. -
    -$$ MSE(\boldsymbol{y},\boldsymbol{\tilde{y}}) = \frac{1}{n} -\sum_{i=0}^{n-1}(y_i-\tilde{y}_i)^2, +$$ +\boldsymbol{X}^T\boldsymbol{X}=(\boldsymbol{X}^T\boldsymbol{X})^{-1} =\boldsymbol{I}. $$ -

    and the \( R^2 \) score function. -If \( \tilde{\boldsymbol{y}}_i \) is the predicted value of the \( i-th \) sample and \( y_i \) is the corresponding true value, then the score \( R^2 \) is defined as -

    +

    In this case the standard OLS results in

    $$ -R^2(\boldsymbol{y}, \tilde{\boldsymbol{y}}) = 1 - \frac{\sum_{i=0}^{n - 1} (y_i - \tilde{y}_i)^2}{\sum_{i=0}^{n - 1} (y_i - \bar{y})^2}, -$$ - -

    where we have defined the mean value of \( \boldsymbol{y} \) as

    -$$ -\bar{y} = \frac{1}{n} \sum_{i=0}^{n - 1} y_i. -$$ - -

    You can use the functionality included in scikit-learn. If you feel for it, you can use your own program and define functions which compute the above two functions. -Discuss the meaning of these results. Try also to vary the coefficient in front of the added stochastic noise term and discuss the quality of the fits. -

    - - - -

    -

    -

    - -

    -Solution. -

    -
    -
    -

    - -

    The code here is an example of where we define our own design matrix and fit parameters \( \beta \).

    - - -
    -
    -
    -
    -
    -
    import os
    -import numpy as np
    -import pandas as pd
    -import matplotlib.pyplot as plt
    -from sklearn.model_selection import train_test_split
    -
    -def save_fig(fig_id):
    -    plt.savefig(image_path(fig_id) + ".png", format='png')
    -
    -def R2(y_data, y_model):
    -    return 1 - np.sum((y_data - y_model) ** 2) / np.sum((y_data - np.mean(y_data)) ** 2)
    -def MSE(y_data,y_model):
    -    n = np.size(y_model)
    -    return np.sum((y_data-y_model)**2)/n
    -
    -x = np.random.rand(100)
    -y = 2.0+5*x*x+0.1*np.random.randn(100)
    -
    -
    -#  The design matrix now as function of a given polynomial
    -X = np.zeros((len(x),3))
    -X[:,0] = 1.0
    -X[:,1] = x
    -X[:,2] = x**2
    -# We split the data in test and training data
    -X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
    -# matrix inversion to find beta
    -beta = np.linalg.inv(X_train.T @ X_train) @ X_train.T @ y_train
    -print(beta)
    -# and then make the prediction
    -ytilde = X_train @ beta
    -print("Training R2")
    -print(R2(y_train,ytilde))
    -print("Training MSE")
    -print(MSE(y_train,ytilde))
    -ypredict = X_test @ beta
    -print("Test R2")
    -print(R2(y_test,ypredict))
    -print("Test MSE")
    -print(MSE(y_test,ypredict))
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    - - -

    -
    -

    - - - - - - -

    Exercise 3: Normalizing our data

    - -

    A much used approach before starting to train the data is to preprocess our -data. Normally the data may need a rescaling and/or may be sensitive -to extreme values. Scaling the data renders our inputs much more -suitable for the algorithms we want to employ. -

    - -

    Scikit-Learn has several functions which allow us to rescale the -data, normally resulting in much better results in terms of various -accuracy scores. The StandardScaler function in Scikit-Learn -ensures that for each feature/predictor we study the mean value is -zero and the variance is one (every column in the design/feature -matrix). This scaling has the drawback that it does not ensure that -we have a particular maximum or minimum in our data set. Another -function included in Scikit-Learn is the MinMaxScaler which -ensures that all features are exactly between \( 0 \) and \( 1 \). The -

    - -

    The Normalizer scales each data -point such that the feature vector has a euclidean length of one. In other words, it -projects a data point on the circle (or sphere in the case of higher dimensions) with a -radius of 1. This means every data point is scaled by a different number (by the -inverse of it’s length). -This normalization is often used when only the direction (or angle) of the data matters, -not the length of the feature vector. -

    - -

    The RobustScaler works similarly to the StandardScaler in that it -ensures statistical properties for each feature that guarantee that -they are on the same scale. However, the RobustScaler uses the median -and quartiles, instead of mean and variance. This makes the -RobustScaler ignore data points that are very different from the rest -(like measurement errors). These odd data points are also called -outliers, and might often lead to trouble for other scaling -techniques. -

    - -

    It also common to split the data in a training set and a testing set. A typical split is to use \( 80\% \) of the data for training and the rest -for testing. This can be done as follows with our design matrix \( \boldsymbol{X} \) and data \( \boldsymbol{y} \) (remember to import scikit-learn) -

    - - -
    -
    -
    -
    -
    -
    # split in training and test data
    -X_train, X_test, y_train, y_test = train_test_split(X,y,test_size=0.2)
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    - -

    Then we can use the standard scaler to scale our data as

    - - -
    -
    -
    -
    -
    -
    scaler = StandardScaler()
    -scaler.fit(X_train)
    -X_train_scaled = scaler.transform(X_train)
    -X_test_scaled = scaler.transform(X_test)
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    - -

    In this exercise we want you to to compute the MSE for the training -data and the test data as function of the complexity of a polynomial, -that is the degree of a given polynomial. We want you also to compute the \( R2 \) score as function of the complexity of the model for both training data and test data. You should also run the calculation with and without scaling. -

    - -

    One of -the aims is to reproduce Figure 2.11 of Hastie et al. -

    - -

    Our data is defined by \( x\in [-3,3] \) with a total of for example \( 100 \) data points.

    - - -
    -
    -
    -
    -
    -
    np.random.seed()
    -n = 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)
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    - -

    where \( y \) is the function we want to fit with a given polynomial.

    - - -

    -a) -Write a first code which sets up a design matrix \( X \) defined by a fifth-order polynomial. Scale your data and split it in training and test data. -

    - - - - -

    -b) -Perform an ordinary least squares and compute the means squared error and the \( R2 \) factor for the training data and the test data, with and without scaling. -

    - - - - -

    -c) -Add now a model which allows you to make polynomials up to degree \( 15 \). Perform a standard OLS fitting of the training data and compute the MSE and \( R2 \) for the training and test data and plot both test and training data MSE and \( R2 \) as functions of the polynomial degree. Compare what you see with Figure 2.11 of Hastie et al. Comment your results. For which polynomial degree do you find an optimal MSE (smallest value)? -

    - - - - - - -

    Exercise 4: Adding Ridge Regression

    - -

    This exercise is a continuation of exercise 2. We will use the same function to -generate our data set, still staying with a simple function \( y(x) \) -which we want to fit using linear regression, but now extending the -analysis to include the Ridge regression method. -

    - -

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

    - -

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

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

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

    - -

    The code here allows you to perform your own Ridge calculation and -perform calculations for various values of the regularization -parameter \( \lambda \). This program can easily be extended upon. -

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

    Repeat the above but using the functionality of -Scikit-Learn. Compare your code with the results from -Scikit-Learn. Remember to run with the same random numbers for -generating \( x \) and \( y \). Observe also that when you compare with Scikit-Learn, you need to pay attention to how the intercept is dealt with. -

    - -

    Finally, using Scikit-Learn or your own code, compute also the mean square error, a risk metric corresponding to the expected value of the squared (quadratic) error defined as

    -$$ MSE(\hat{y},\hat{\tilde{y}}) = \frac{1}{n} -\sum_{i=0}^{n-1}(y_i-\tilde{y}_i)^2, -$$ - -

    and the \( R^2 \) score function. -If \( \tilde{\hat{y}}_i \) is the predicted value of the \( i-th \) sample and \( y_i \) is the corresponding true value, then the score \( R^2 \) is defined as -

    -$$ -R^2(\hat{y}, \tilde{\hat{y}}) = 1 - \frac{\sum_{i=0}^{n - 1} (y_i - \tilde{y}_i)^2}{\sum_{i=0}^{n - 1} (y_i - \bar{y})^2}, -$$ - -

    where we have defined the mean value of \( \hat{y} \) as

    -$$ -\bar{y} = \frac{1}{n} \sum_{i=0}^{n - 1} y_i. -$$ - -

    Discuss these quantities as functions of the variable \( \lambda \) in Ridge regression.

    - - - - -

    Exercise 5: Analytical exercises

    - -

    In this exercise we derive the expressions for various derivatives of -products of vectors and matrices. Such derivatives are central to the -optimization of various cost functions. Although we will often use -automatic differentiation in actual calculations, to be able to have -analytical expressions is extremely helpful in case we have simpler -derivatives as well as when we analyze various properties (like second -derivatives) of the chosen cost functions. Vectors are always written -as boldfaced lower case letters and matrices as upper case boldfaced -letters. -

    - -

    Show that

    -$$ -\frac{\partial (\boldsymbol{b}^T\boldsymbol{a})}{\partial \boldsymbol{a}} = \boldsymbol{b}, +\boldsymbol{\beta}^{\mathrm{OLS}} = \boldsymbol{X}^T\boldsymbol{y}=\sum_{i=0}^{n-1}\boldsymbol{u}_i\boldsymbol{u}_i^T\boldsymbol{y}, $$

    and

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

    and

    -$$ -\frac{\partial \left(\boldsymbol{x}-\boldsymbol{A}\boldsymbol{s}\right)^T\left(\boldsymbol{x}-\boldsymbol{A}\boldsymbol{s}\right)}{\partial \boldsymbol{s}} = -2\left(\boldsymbol{x}-\boldsymbol{A}\boldsymbol{s}\right)^T\boldsymbol{A}, -$$ - -

    and finally find the second derivative of this function with respect to the vector \( \boldsymbol{s} \).

    - -

    Hint: In these exercises it is always useful to write out with summation indices the various quantities. -As an example, consider the function +

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

    -$$ -f(\boldsymbol{x}) =\boldsymbol{A}\boldsymbol{x}, -$$ +

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

    -

    which reads for a specific component \( f_i \) (we define the matrix \( \boldsymbol{A} \) to have dimension \( n\times n \) and the vector $\boldsymbol{x} to have length \( n \))

    +

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

    -$$ -f_i =\sum_{j=0}^{n-1}a_{ij}x_j, -$$ - -

    which leads to

    -$$ -\frac{\partial f_i}{\partial x_j}= a_{ij}, -$$ - -

    and written out in terms of the vector \( \boldsymbol{x} \) we have

    -$$ -\frac{\partial f(\boldsymbol{x})}{\partial \boldsymbol{x}}= \boldsymbol{A}. -$$ - - -

    diff --git a/doc/pub/week35/html/._week35-bs069.html b/doc/pub/week35/html/._week35-bs069.html index a8939cae8..3e27a9cae 100644 --- a/doc/pub/week35/html/._week35-bs069.html +++ b/doc/pub/week35/html/._week35-bs069.html @@ -38,7 +38,6 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d {'highest level': 2, 'sections': [('Plans for week 35', 2, None, 'plans-for-week-35'), ('Reading recommendations:', 3, None, 'reading-recommendations'), - ('Thursday September 1', 2, None, 'thursday-september-1'), ('Why Linear Regression (aka Ordinary Least Squares and family), ' 'repeat from last week', 2, @@ -75,10 +74,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'optimizing-our-parameters'), - ('Our model for the nuclear binding energies', + ('Examples relevant for the exercises', 2, None, - 'our-model-for-the-nuclear-binding-energies'), + 'examples-relevant-for-the-exercises'), ('Optimizing our parameters, more details', 2, None, @@ -95,6 +94,8 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'some-useful-matrix-and-vector-expressions'), + ('The Jacobian', 2, None, 'the-jacobian'), + ('Derivatives, example 1', 2, None, 'derivatives-example-1'), ('Meet the Hessian Matrix', 2, None, 'meet-the-hessian-matrix'), ('Interpretations and optimizing our parameters', 2, @@ -149,6 +150,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'more-preprocessing-examples-franke-function-and-regression'), + ('Material for lecture Thursday, August 31', + 2, + None, + 'material-for-lecture-thursday-august-31'), ('Mathematical Interpretation of Ordinary Least Squares', 2, None, @@ -175,7 +180,6 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'note-about-svd-calculations'), - ('Friday September 2', 2, None, 'friday-september-2'), ('Mathematics of the SVD and implications', 2, None, @@ -302,79 +306,80 @@ MathJax.Hub.Config({ @@ -386,610 +391,32 @@ MathJax.Hub.Config({

     

     

     

    -

    Exercises for week 35

    +

    Deriving the Lasso Regression Equations

    -

    The exercises here are meant to prepare you for work with project 1. The first exercise is a follow-up of exercise 2 from week 35 August 30-September 3).

    - - -

    Exercise 1: Setting up various Python environments

    - -

    The first exercise here is of a mere technical art. We want you to have

    -
      -
    • git as a version control software and to establish a user account on a provider like GitHub. Other providers like GitLab etc are equally fine. You can also use the University of Oslo GitHub facilities.
    • -
    • Install various Python packages
    • -
    -

    We will make extensive use of Python as programming language and its -myriad of available libraries. You will find -IPython/Jupyter notebooks invaluable in your work. You can run R -codes in the Jupyter/IPython notebooks, with the immediate benefit of -visualizing your data. You can also use compiled languages like C++, -Rust, Fortran etc if you prefer. The focus in these lectures will be -on Python. -

    - -

    If you have Python installed (we recommend Python3) and you feel -pretty familiar with installing different packages, we recommend that -you install the following Python packages via pip as -

    - -
      -
    1. pip install numpy scipy matplotlib ipython scikit-learn sympy pandas pillow
    2. -
    -

    For Tensorflow, we recommend following the instructions in the text of -Aurelien Geron, Hands‑On Machine Learning with Scikit‑Learn and TensorFlow, O'Reilly -

    - -

    We will come back to tensorflow later.

    - -

    For Python3, replace pip with pip3.

    - -

    For OSX users we recommend, after having installed Xcode, to -install brew. Brew allows for a seamless installation of additional -software via for example -

    - -
      -
    1. brew install python3
    2. -
    -

    For Linux users, with its variety of distributions like for example the widely popular Ubuntu distribution, -you can use pip as well and simply install Python as -

    - -
      -
    1. sudo apt-get install python3 (or python for Python2.7)
    2. -
    -

    If you don't want to perform these operations separately and venture -into the hassle of exploring how to set up dependencies and paths, we -recommend two widely used distrubutions which set up all relevant -dependencies for Python, namely -

    - - -

    which is an open source -distribution of the Python and R programming languages for large-scale -data processing, predictive analytics, and scientific computing, that -aims to simplify package management and deployment. Package versions -are managed by the package management system conda. -

    - - -

    is a Python -distribution for scientific and analytic computing distribution and -analysis environment, available for free and under a commercial -license. -

    - -

    We recommend using Anaconda if you are not too familiar with setting paths in a terminal environment.

    - - - - -

    Exercise 2: making your own data and exploring scikit-learn

    - -

    We will generate our own dataset for a function \( y(x) \) where \( x \in [0,1] \) and defined by random numbers computed with the uniform distribution. The function \( y \) is a quadratic polynomial in \( x \) with added stochastic noise according to the normal distribution \( \cal {N}(0,1) \). -The following simple Python instructions define our \( x \) and \( y \) values (with 100 data points). -

    - - -
    -
    -
    -
    -
    -
    x = np.random.rand(100,1)
    -y = 2.0+5*x*x+0.1*np.random.randn(100,1)
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    - - -
      -
    1. Write your own code (following the examples under the regression notes) for computing the parametrization of the data set fitting a second-order polynomial.
    2. -
    3. Use thereafter scikit-learn (see again the examples in the regression slides) and compare with your own code. When compairing with _scikit_learn_, make sure you set the option for the intercept to FALSE, see https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LinearRegression.html. This feature will be explained in more detail during the lectures of week 35 and week 36. You can find more in https://compphysics.github.io/MachineLearning/doc/LectureNotes/_build/html/chapter3.html#more-on-rescaling-data.
    4. -
    5. Using scikit-learn, compute also the mean square error, a risk metric corresponding to the expected value of the squared (quadratic) error defined as
    6. -
    -$$ MSE(\boldsymbol{y},\boldsymbol{\tilde{y}}) = \frac{1}{n} -\sum_{i=0}^{n-1}(y_i-\tilde{y}_i)^2, -$$ - -

    and the \( R^2 \) score function. -If \( \tilde{\boldsymbol{y}}_i \) is the predicted value of the \( i-th \) sample and \( y_i \) is the corresponding true value, then the score \( R^2 \) is defined as -

    -$$ -R^2(\boldsymbol{y}, \tilde{\boldsymbol{y}}) = 1 - \frac{\sum_{i=0}^{n - 1} (y_i - \tilde{y}_i)^2}{\sum_{i=0}^{n - 1} (y_i - \bar{y})^2}, -$$ - -

    where we have defined the mean value of \( \boldsymbol{y} \) as

    -$$ -\bar{y} = \frac{1}{n} \sum_{i=0}^{n - 1} y_i. -$$ - -

    You can use the functionality included in scikit-learn. If you feel for it, you can use your own program and define functions which compute the above two functions. -Discuss the meaning of these results. Try also to vary the coefficient in front of the added stochastic noise term and discuss the quality of the fits. -

    - - - -

    -

    -

    - -

    -Solution. -

    -
    -
    -

    - -

    The code here is an example of where we define our own design matrix and fit parameters \( \beta \).

    - - -
    -
    -
    -
    -
    -
    import os
    -import numpy as np
    -import pandas as pd
    -import matplotlib.pyplot as plt
    -from sklearn.model_selection import train_test_split
    -
    -def save_fig(fig_id):
    -    plt.savefig(image_path(fig_id) + ".png", format='png')
    -
    -def R2(y_data, y_model):
    -    return 1 - np.sum((y_data - y_model) ** 2) / np.sum((y_data - np.mean(y_data)) ** 2)
    -def MSE(y_data,y_model):
    -    n = np.size(y_model)
    -    return np.sum((y_data-y_model)**2)/n
    -
    -x = np.random.rand(100)
    -y = 2.0+5*x*x+0.1*np.random.randn(100)
    -
    -
    -#  The design matrix now as function of a given polynomial
    -X = np.zeros((len(x),3))
    -X[:,0] = 1.0
    -X[:,1] = x
    -X[:,2] = x**2
    -# We split the data in test and training data
    -X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
    -# matrix inversion to find beta
    -beta = np.linalg.inv(X_train.T @ X_train) @ X_train.T @ y_train
    -print(beta)
    -# and then make the prediction
    -ytilde = X_train @ beta
    -print("Training R2")
    -print(R2(y_train,ytilde))
    -print("Training MSE")
    -print(MSE(y_train,ytilde))
    -ypredict = X_test @ beta
    -print("Test R2")
    -print(R2(y_test,ypredict))
    -print("Test MSE")
    -print(MSE(y_test,ypredict))
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    - - -

    -
    -

    - - - - - - -

    Exercise 3: Normalizing our data

    - -

    A much used approach before starting to train the data is to preprocess our -data. Normally the data may need a rescaling and/or may be sensitive -to extreme values. Scaling the data renders our inputs much more -suitable for the algorithms we want to employ. -

    - -

    Scikit-Learn has several functions which allow us to rescale the -data, normally resulting in much better results in terms of various -accuracy scores. The StandardScaler function in Scikit-Learn -ensures that for each feature/predictor we study the mean value is -zero and the variance is one (every column in the design/feature -matrix). This scaling has the drawback that it does not ensure that -we have a particular maximum or minimum in our data set. Another -function included in Scikit-Learn is the MinMaxScaler which -ensures that all features are exactly between \( 0 \) and \( 1 \). The -

    - -

    The Normalizer scales each data -point such that the feature vector has a euclidean length of one. In other words, it -projects a data point on the circle (or sphere in the case of higher dimensions) with a -radius of 1. This means every data point is scaled by a different number (by the -inverse of it’s length). -This normalization is often used when only the direction (or angle) of the data matters, -not the length of the feature vector. -

    - -

    The RobustScaler works similarly to the StandardScaler in that it -ensures statistical properties for each feature that guarantee that -they are on the same scale. However, the RobustScaler uses the median -and quartiles, instead of mean and variance. This makes the -RobustScaler ignore data points that are very different from the rest -(like measurement errors). These odd data points are also called -outliers, and might often lead to trouble for other scaling -techniques. -

    - -

    It also common to split the data in a training set and a testing set. A typical split is to use \( 80\% \) of the data for training and the rest -for testing. This can be done as follows with our design matrix \( \boldsymbol{X} \) and data \( \boldsymbol{y} \) (remember to import scikit-learn) -

    - - -
    -
    -
    -
    -
    -
    # split in training and test data
    -X_train, X_test, y_train, y_test = train_test_split(X,y,test_size=0.2)
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    - -

    Then we can use the standard scaler to scale our data as

    - - -
    -
    -
    -
    -
    -
    scaler = StandardScaler()
    -scaler.fit(X_train)
    -X_train_scaled = scaler.transform(X_train)
    -X_test_scaled = scaler.transform(X_test)
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    - -

    In this exercise we want you to to compute the MSE for the training -data and the test data as function of the complexity of a polynomial, -that is the degree of a given polynomial. We want you also to compute the \( R2 \) score as function of the complexity of the model for both training data and test data. You should also run the calculation with and without scaling. -

    - -

    One of -the aims is to reproduce Figure 2.11 of Hastie et al. -

    - -

    Our data is defined by \( x\in [-3,3] \) with a total of for example \( 100 \) data points.

    - - -
    -
    -
    -
    -
    -
    np.random.seed()
    -n = 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)
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    - -

    where \( y \) is the function we want to fit with a given polynomial.

    - - -

    -a) -Write a first code which sets up a design matrix \( X \) defined by a fifth-order polynomial. Scale your data and split it in training and test data. -

    - - - - -

    -b) -Perform an ordinary least squares and compute the means squared error and the \( R2 \) factor for the training data and the test data, with and without scaling. -

    - - - - -

    -c) -Add now a model which allows you to make polynomials up to degree \( 15 \). Perform a standard OLS fitting of the training data and compute the MSE and \( R2 \) for the training and test data and plot both test and training data MSE and \( R2 \) as functions of the polynomial degree. Compare what you see with Figure 2.11 of Hastie et al. Comment your results. For which polynomial degree do you find an optimal MSE (smallest value)? -

    - - - - - - -

    Exercise 4: Adding Ridge Regression

    - -

    This exercise is a continuation of exercise 2. We will use the same function to -generate our data set, still staying with a simple function \( y(x) \) -which we want to fit using linear regression, but now extending the -analysis to include the Ridge regression method. -

    - -

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

    - -

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

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

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

    - -

    The code here allows you to perform your own Ridge calculation and -perform calculations for various values of the regularization -parameter \( \lambda \). This program can easily be extended upon. -

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

    Repeat the above but using the functionality of -Scikit-Learn. Compare your code with the results from -Scikit-Learn. Remember to run with the same random numbers for -generating \( x \) and \( y \). Observe also that when you compare with Scikit-Learn, you need to pay attention to how the intercept is dealt with. -

    - -

    Finally, using Scikit-Learn or your own code, compute also the mean square error, a risk metric corresponding to the expected value of the squared (quadratic) error defined as

    -$$ MSE(\hat{y},\hat{\tilde{y}}) = \frac{1}{n} -\sum_{i=0}^{n-1}(y_i-\tilde{y}_i)^2, -$$ - -

    and the \( R^2 \) score function. -If \( \tilde{\hat{y}}_i \) is the predicted value of the \( i-th \) sample and \( y_i \) is the corresponding true value, then the score \( R^2 \) is defined as -

    -$$ -R^2(\hat{y}, \tilde{\hat{y}}) = 1 - \frac{\sum_{i=0}^{n - 1} (y_i - \tilde{y}_i)^2}{\sum_{i=0}^{n - 1} (y_i - \bar{y})^2}, -$$ - -

    where we have defined the mean value of \( \hat{y} \) as

    -$$ -\bar{y} = \frac{1}{n} \sum_{i=0}^{n - 1} y_i. -$$ - -

    Discuss these quantities as functions of the variable \( \lambda \) in Ridge regression.

    - - - - -

    Exercise 5: Analytical exercises

    - -

    In this exercise we derive the expressions for various derivatives of -products of vectors and matrices. Such derivatives are central to the -optimization of various cost functions. Although we will often use -automatic differentiation in actual calculations, to be able to have -analytical expressions is extremely helpful in case we have simpler -derivatives as well as when we analyze various properties (like second -derivatives) of the chosen cost functions. Vectors are always written -as boldfaced lower case letters and matrices as upper case boldfaced -letters. -

    - -

    Show that

    -$$ -\frac{\partial (\boldsymbol{b}^T\boldsymbol{a})}{\partial \boldsymbol{a}} = \boldsymbol{b}, -$$ - -

    and

    -$$ -\frac{\partial (\boldsymbol{a}^T\boldsymbol{A}\boldsymbol{a})}{\partial \boldsymbol{a}} = \boldsymbol{a}^T(\boldsymbol{A}+\boldsymbol{A}^T), -$$ - -

    and

    -$$ -\frac{\partial \left(\boldsymbol{x}-\boldsymbol{A}\boldsymbol{s}\right)^T\left(\boldsymbol{x}-\boldsymbol{A}\boldsymbol{s}\right)}{\partial \boldsymbol{s}} = -2\left(\boldsymbol{x}-\boldsymbol{A}\boldsymbol{s}\right)^T\boldsymbol{A}, -$$ - -

    and finally find the second derivative of this function with respect to the vector \( \boldsymbol{s} \).

    - -

    Hint: In these exercises it is always useful to write out with summation indices the various quantities. -As an example, consider the function -

    +

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

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

    which reads for a specific component \( f_i \) (we define the matrix \( \boldsymbol{A} \) to have dimension \( n\times n \) and the vector $\boldsymbol{x} to have length \( n \))

    - +

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

    $$ -f_i =\sum_{j=0}^{n-1}a_{ij}x_j, +\frac{d \vert \beta\vert}{d \boldsymbol{\beta}}=\mathrm{sgn}(\boldsymbol{\beta})=\left\{\begin{array}{cc} 1 & \beta > 0 \\-1 & \beta < 0, \end{array}\right. $$ -

    which leads to

    +

    we have that the derivative of the cost function is

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

    and written out in terms of the vector \( \boldsymbol{x} \) we have

    +

    and reordering we have

    $$ -\frac{\partial f(\boldsymbol{x})}{\partial \boldsymbol{x}}= \boldsymbol{A}. +\boldsymbol{X}^T\boldsymbol{X}\boldsymbol{\beta}+\lambda sgn(\boldsymbol{\beta})=2\boldsymbol{X}^T\boldsymbol{y}. $$ +

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

    -

    diff --git a/doc/pub/week35/html/._week35-bs070.html b/doc/pub/week35/html/._week35-bs070.html index 2cf53e509..8d5a73633 100644 --- a/doc/pub/week35/html/._week35-bs070.html +++ b/doc/pub/week35/html/._week35-bs070.html @@ -1,6 +1,7 @@ @@ -8,24 +9,20 @@ Automatically generated HTML file from DocOnce source - Week 35: From Ordinary Linear Regression to Ridge and Lasso Regression - + - - - @@ -285,8 +289,6 @@ MathJax.Hub.Config({ - -
    -
    -

     

     

     

    - +

    Exercises for week 35

    -

    Mathematical Properties

    +

    The exercises here are meant to prepare you for work with project 1. The first exercise is a follow-up of exercise 2 from week 35 August 30-September 3).

    + + +

    Exercise 1: Setting up various Python environments

    + +

    The first exercise here is of a mere technical art. We want you to have

    +
      +
    • git as a version control software and to establish a user account on a provider like GitHub. Other providers like GitLab etc are equally fine. You can also use the University of Oslo GitHub facilities.
    • +
    • Install various Python packages
    • +
    +

    We will make extensive use of Python as programming language and its +myriad of available libraries. You will find +IPython/Jupyter notebooks invaluable in your work. You can run R +codes in the Jupyter/IPython notebooks, with the immediate benefit of +visualizing your data. You can also use compiled languages like C++, +Rust, Fortran etc if you prefer. The focus in these lectures will be +on Python. +

    + +

    If you have Python installed (we recommend Python3) and you feel +pretty familiar with installing different packages, we recommend that +you install the following Python packages via pip as +

    + +
      +
    1. pip install numpy scipy matplotlib ipython scikit-learn sympy pandas pillow
    2. +
    +

    For Tensorflow, we recommend following the instructions in the text of +Aurelien Geron, Hands‑On Machine Learning with Scikit‑Learn and TensorFlow, O'Reilly +

    + +

    We will come back to tensorflow later.

    + +

    For Python3, replace pip with pip3.

    + +

    For OSX users we recommend, after having installed Xcode, to +install brew. Brew allows for a seamless installation of additional +software via for example +

    + +
      +
    1. brew install python3
    2. +
    +

    For Linux users, with its variety of distributions like for example the widely popular Ubuntu distribution, +you can use pip as well and simply install Python as +

    + +
      +
    1. sudo apt-get install python3 (or python for Python2.7)
    2. +
    +

    If you don't want to perform these operations separately and venture +into the hassle of exploring how to set up dependencies and paths, we +recommend two widely used distrubutions which set up all relevant +dependencies for Python, namely +

    + + +

    which is an open source +distribution of the Python and R programming languages for large-scale +data processing, predictive analytics, and scientific computing, that +aims to simplify package management and deployment. Package versions +are managed by the package management system conda. +

    + + +

    is a Python +distribution for scientific and analytic computing distribution and +analysis environment, available for free and under a commercial +license. +

    + +

    We recommend using Anaconda if you are not too familiar with setting paths in a terminal environment.

    + + + + +

    Exercise 2: making your own data and exploring scikit-learn

    + +

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

    + + +
    +
    +
    +
    +
    +
    x = np.random.rand(100,1)
    +y = 2.0+5*x*x+0.1*np.random.randn(100,1)
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    + + +
      +
    1. Write your own code (following the examples under the regression notes) for computing the parametrization of the data set fitting a second-order polynomial.
    2. +
    3. Use thereafter scikit-learn (see again the examples in the regression slides) and compare with your own code. When compairing with _scikit_learn_, make sure you set the option for the intercept to FALSE, see https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LinearRegression.html. This feature will be explained in more detail during the lectures of week 35 and week 36. You can find more in https://compphysics.github.io/MachineLearning/doc/LectureNotes/_build/html/chapter3.html#more-on-rescaling-data.
    4. +
    5. Using scikit-learn, compute also the mean square error, a risk metric corresponding to the expected value of the squared (quadratic) error defined as
    6. +
    +$$ MSE(\boldsymbol{y},\boldsymbol{\tilde{y}}) = \frac{1}{n} +\sum_{i=0}^{n-1}(y_i-\tilde{y}_i)^2, +$$ + +

    and the \( R^2 \) score function. +If \( \tilde{\boldsymbol{y}}_i \) is the predicted value of the \( i-th \) sample and \( y_i \) is the corresponding true value, then the score \( R^2 \) is defined as +

    +$$ +R^2(\boldsymbol{y}, \tilde{\boldsymbol{y}}) = 1 - \frac{\sum_{i=0}^{n - 1} (y_i - \tilde{y}_i)^2}{\sum_{i=0}^{n - 1} (y_i - \bar{y})^2}, +$$ + +

    where we have defined the mean value of \( \boldsymbol{y} \) as

    +$$ +\bar{y} = \frac{1}{n} \sum_{i=0}^{n - 1} y_i. +$$ + +

    You can use the functionality included in scikit-learn. If you feel for it, you can use your own program and define functions which compute the above two functions. +Discuss the meaning of these results. Try also to vary the coefficient in front of the added stochastic noise term and discuss the quality of the fits. +

    + +

    -There are several interesting mathematical properties which will be -relevant when we are going to discuss the differences between say -ordinary least squares (OLS) and Ridge regression. - +

    +

    +

    -We have from OLS that the parameters of the linear approximation are given by -$$ -\boldsymbol{\tilde{y}} = \boldsymbol{X}\boldsymbol{\beta} = \boldsymbol{X}\left(\boldsymbol{X}^T\boldsymbol{X}\right)^{-1}\boldsymbol{X}^T\boldsymbol{y}. -$$ +Solution. +

    +
    +
    +

    +

    The code here is an example of where we define our own design matrix and fit parameters \( \beta \).

    + + +
    +
    +
    +
    +
    +
    import os
    +import numpy as np
    +import pandas as pd
    +import matplotlib.pyplot as plt
    +from sklearn.model_selection import train_test_split
    +
    +def save_fig(fig_id):
    +    plt.savefig(image_path(fig_id) + ".png", format='png')
    +
    +def R2(y_data, y_model):
    +    return 1 - np.sum((y_data - y_model) ** 2) / np.sum((y_data - np.mean(y_data)) ** 2)
    +def MSE(y_data,y_model):
    +    n = np.size(y_model)
    +    return np.sum((y_data-y_model)**2)/n
    +
    +x = np.random.rand(100)
    +y = 2.0+5*x*x+0.1*np.random.randn(100)
    +
    +
    +#  The design matrix now as function of a given polynomial
    +X = np.zeros((len(x),3))
    +X[:,0] = 1.0
    +X[:,1] = x
    +X[:,2] = x**2
    +# We split the data in test and training data
    +X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
    +# matrix inversion to find beta
    +beta = np.linalg.inv(X_train.T @ X_train) @ X_train.T @ y_train
    +print(beta)
    +# and then make the prediction
    +ytilde = X_train @ beta
    +print("Training R2")
    +print(R2(y_train,ytilde))
    +print("Training MSE")
    +print(MSE(y_train,ytilde))
    +ypredict = X_test @ beta
    +print("Test R2")
    +print(R2(y_test,ypredict))
    +print("Test MSE")
    +print(MSE(y_test,ypredict))
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    + + +

    +
    +

    + + + + + + +

    Exercise 3: Normalizing our data

    + +

    A much used approach before starting to train the data is to preprocess our +data. Normally the data may need a rescaling and/or may be sensitive +to extreme values. Scaling the data renders our inputs much more +suitable for the algorithms we want to employ. +

    + +

    Scikit-Learn has several functions which allow us to rescale the +data, normally resulting in much better results in terms of various +accuracy scores. The StandardScaler function in Scikit-Learn +ensures that for each feature/predictor we study the mean value is +zero and the variance is one (every column in the design/feature +matrix). This scaling has the drawback that it does not ensure that +we have a particular maximum or minimum in our data set. Another +function included in Scikit-Learn is the MinMaxScaler which +ensures that all features are exactly between \( 0 \) and \( 1 \). The +

    + +

    The Normalizer scales each data +point such that the feature vector has a euclidean length of one. In other words, it +projects a data point on the circle (or sphere in the case of higher dimensions) with a +radius of 1. This means every data point is scaled by a different number (by the +inverse of it’s length). +This normalization is often used when only the direction (or angle) of the data matters, +not the length of the feature vector. +

    + +

    The RobustScaler works similarly to the StandardScaler in that it +ensures statistical properties for each feature that guarantee that +they are on the same scale. However, the RobustScaler uses the median +and quartiles, instead of mean and variance. This makes the +RobustScaler ignore data points that are very different from the rest +(like measurement errors). These odd data points are also called +outliers, and might often lead to trouble for other scaling +techniques. +

    + +

    It also common to split the data in a training set and a testing set. A typical split is to use \( 80\% \) of the data for training and the rest +for testing. This can be done as follows with our design matrix \( \boldsymbol{X} \) and data \( \boldsymbol{y} \) (remember to import scikit-learn) +

    + + +
    +
    +
    +
    +
    +
    # split in training and test data
    +X_train, X_test, y_train, y_test = train_test_split(X,y,test_size=0.2)
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    + +

    Then we can use the standard scaler to scale our data as

    + + +
    +
    +
    +
    +
    +
    scaler = StandardScaler()
    +scaler.fit(X_train)
    +X_train_scaled = scaler.transform(X_train)
    +X_test_scaled = scaler.transform(X_test)
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    + +

    In this exercise we want you to to compute the MSE for the training +data and the test data as function of the complexity of a polynomial, +that is the degree of a given polynomial. We want you also to compute the \( R2 \) score as function of the complexity of the model for both training data and test data. You should also run the calculation with and without scaling. +

    + +

    One of +the aims is to reproduce Figure 2.11 of Hastie et al. +

    + +

    Our data is defined by \( x\in [-3,3] \) with a total of for example \( 100 \) data points.

    + + +
    +
    +
    +
    +
    +
    np.random.seed()
    +n = 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)
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    + +

    where \( y \) is the function we want to fit with a given polynomial.

    + +

    -The matrix to invert can be rewritten in terms of our SVD decomposition as +a) +Write a first code which sets up a design matrix \( X \) defined by a fifth-order polynomial. Scale your data and split it in training and test data. +

    -$$ -\boldsymbol{X}^T\boldsymbol{X} = \boldsymbol{V}\boldsymbol{\Sigma}^T\boldsymbol{U}^T\boldsymbol{U}\boldsymbol{\Sigma}\boldsymbol{V}^T. -$$ - -Using the orthogonality properties of \( \boldsymbol{U} \) we have - -$$ -\boldsymbol{X}^T\boldsymbol{X} = \boldsymbol{V}\boldsymbol{\Sigma}^T\boldsymbol{\Sigma}\boldsymbol{V}^T = \boldsymbol{V}\boldsymbol{D}\boldsymbol{V}^T, -$$ - -with \( \boldsymbol{D} \) being a diagonal matrix with values along the diagonal given by the singular values squared. + +

    -This means that -$$ -(\boldsymbol{X}^T\boldsymbol{X})\boldsymbol{V} = \boldsymbol{V}\boldsymbol{D}, -$$ +b) +Perform an ordinary least squares and compute the means squared error and the \( R2 \) factor for the training data and the test data, with and without scaling. +

    -that is the eigenvectors of \( (\boldsymbol{X}^T\boldsymbol{X}) \) are given by the columns of the right singular matrix of \( \boldsymbol{X} \) and the eigenvalues are the squared singular values. It is easy to show (show this) that -$$ -(\boldsymbol{X}\boldsymbol{X}^T)\boldsymbol{U} = \boldsymbol{U}\boldsymbol{D}, -$$ - -that is, the eigenvectors of \( (\boldsymbol{X}\boldsymbol{X})^T \) are the columns of the left singular matrix and the eigenvalues are the same. + +

    -Going back to our OLS equation we have -$$ -\boldsymbol{X}\boldsymbol{\beta} = \boldsymbol{X}\left(\boldsymbol{V}\boldsymbol{D}\boldsymbol{V}^T \right)^{-1}\boldsymbol{X}^T\boldsymbol{y}=\boldsymbol{U\Sigma V^T}\left(\boldsymbol{V}\boldsymbol{D}\boldsymbol{V}^T \right)^{-1}(\boldsymbol{U\Sigma V^T})^T\boldsymbol{y}=\boldsymbol{U}\boldsymbol{U}^T\boldsymbol{y}. +c) +Add now a model which allows you to make polynomials up to degree \( 15 \). Perform a standard OLS fitting of the training data and compute the MSE and \( R2 \) for the training and test data and plot both test and training data MSE and \( R2 \) as functions of the polynomial degree. Compare what you see with Figure 2.11 of Hastie et al. Comment your results. For which polynomial degree do you find an optimal MSE (smallest value)? +

    + + + + + + +

    Exercise 4: Adding Ridge Regression

    + +

    This exercise is a continuation of exercise 2. We will use the same function to +generate our data set, still staying with a simple function \( y(x) \) +which we want to fit using linear regression, but now extending the +analysis to include the Ridge regression method. +

    + +

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

    + +

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

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

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

    + +

    The code here allows you to perform your own Ridge calculation and +perform calculations for various values of the regularization +parameter \( \lambda \). This program can easily be extended upon. +

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

    Repeat the above but using the functionality of +Scikit-Learn. Compare your code with the results from +Scikit-Learn. Remember to run with the same random numbers for +generating \( x \) and \( y \). Observe also that when you compare with Scikit-Learn, you need to pay attention to how the intercept is dealt with. +

    + +

    Finally, using Scikit-Learn or your own code, compute also the mean square error, a risk metric corresponding to the expected value of the squared (quadratic) error defined as

    +$$ MSE(\hat{y},\hat{\tilde{y}}) = \frac{1}{n} +\sum_{i=0}^{n-1}(y_i-\tilde{y}_i)^2, $$ -We will come back to this expression when we discuss Ridge regression. +

    and the \( R^2 \) score function. +If \( \tilde{\hat{y}}_i \) is the predicted value of the \( i-th \) sample and \( y_i \) is the corresponding true value, then the score \( R^2 \) is defined as +

    +$$ +R^2(\hat{y}, \tilde{\hat{y}}) = 1 - \frac{\sum_{i=0}^{n - 1} (y_i - \tilde{y}_i)^2}{\sum_{i=0}^{n - 1} (y_i - \bar{y})^2}, +$$ -$$ \tilde{y}^{OLS}={\bf X}\hat{\beta}^{OLS}=\sum_{j=1}^p {\bf u}_j{\bf u}_j^T{\bf y}$$ and for Ridge we have  +

    where we have defined the mean value of \( \hat{y} \) as

    +$$ +\bar{y} = \frac{1}{n} \sum_{i=0}^{n - 1} y_i. +$$ -$$ \tilde{y}^{Ridge}={\bf X}\hat{\beta}^{Ridge}=\sum_{j=1}^p {\bf u}_j\frac{\sigma_j^2}{\sigma_j^2+\lambda}{\bf u}_j^T{\bf y}$$ .  +

    Discuss these quantities as functions of the variable \( \lambda \) in Ridge regression.

    -

    -It is indeed the economy-sized SVD, note the summation runs up tp $$p$$ only and not $$n$$.  + -

    -Here we have that $${\bf X} = {\bf U}{\bf \Sigma}{\bf V}^T$$, with $$\Sigma$$ being an $$ n\times p$$ matrix and $${\bf V}$$ being a $$ p\times p$$ matrix. We also have assumed here that $$ n > p$$.  + +

    Exercise 5: Analytical exercises

    -

    +

    In this exercise we derive the expressions for various derivatives of +products of vectors and matrices. Such derivatives are central to the +optimization of various cost functions. Although we will often use +automatic differentiation in actual calculations, to be able to have +analytical expressions is extremely helpful in case we have simpler +derivatives as well as when we analyze various properties (like second +derivatives) of the chosen cost functions. Vectors are always written +as boldfaced lower case letters and matrices as upper case boldfaced +letters. +

    + +

    Show that

    +$$ +\frac{\partial (\boldsymbol{b}^T\boldsymbol{a})}{\partial \boldsymbol{a}} = \boldsymbol{b}, +$$ + +

    and

    +$$ +\frac{\partial (\boldsymbol{a}^T\boldsymbol{A}\boldsymbol{a})}{\partial \boldsymbol{a}} = \boldsymbol{a}^T(\boldsymbol{A}+\boldsymbol{A}^T), +$$ + +

    and

    +$$ +\frac{\partial \left(\boldsymbol{x}-\boldsymbol{A}\boldsymbol{s}\right)^T\left(\boldsymbol{x}-\boldsymbol{A}\boldsymbol{s}\right)}{\partial \boldsymbol{s}} = -2\left(\boldsymbol{x}-\boldsymbol{A}\boldsymbol{s}\right)^T\boldsymbol{A}, +$$ + +

    and finally find the second derivative of this function with respect to the vector \( \boldsymbol{s} \).

    + +

    Hint: In these exercises it is always useful to write out with summation indices the various quantities. +As an example, consider the function +

    + +$$ +f(\boldsymbol{x}) =\boldsymbol{A}\boldsymbol{x}, +$$ + +

    which reads for a specific component \( f_i \) (we define the matrix \( \boldsymbol{A} \) to have dimension \( n\times n \) and the vector $\boldsymbol{x} to have length \( n \))

    + +$$ +f_i =\sum_{j=0}^{n-1}a_{ij}x_j, +$$ + +

    which leads to

    +$$ +\frac{\partial f_i}{\partial x_j}= a_{ij}, +$$ + +

    and written out in terms of the vector \( \boldsymbol{x} \) we have

    +$$ +\frac{\partial f(\boldsymbol{x})}{\partial \boldsymbol{x}}= \boldsymbol{A}. +$$ + + +

      @@ -466,29 +1010,20 @@ Here we have that $${\bf X} = {\bf U}{\bf \Sigma}{\bf V}^T$$, with $$\Sigma$$ be
    • 69
    • 70
    • 71
    • -
    • 72
    • -
    • »
    -
    - - -
    - - - diff --git a/doc/pub/week35/html/week35-bs.html b/doc/pub/week35/html/week35-bs.html index e9a353109..3a6fa9a41 100644 --- a/doc/pub/week35/html/week35-bs.html +++ b/doc/pub/week35/html/week35-bs.html @@ -74,10 +74,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'optimizing-our-parameters'), - ('Our model for the nuclear binding energies', + ('Examples relevant for the exercises', 2, None, - 'our-model-for-the-nuclear-binding-energies'), + 'examples-relevant-for-the-exercises'), ('Optimizing our parameters, more details', 2, None, @@ -94,6 +94,8 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'some-useful-matrix-and-vector-expressions'), + ('The Jacobian', 2, None, 'the-jacobian'), + ('Derivatives, example 1', 2, None, 'derivatives-example-1'), ('Meet the Hessian Matrix', 2, None, 'meet-the-hessian-matrix'), ('Interpretations and optimizing our parameters', 2, @@ -148,6 +150,10 @@ doconce format html week35.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'more-preprocessing-examples-franke-function-and-regression'), + ('Material for lecture Thursday, August 31', + 2, + None, + 'material-for-lecture-thursday-august-31'), ('Mathematical Interpretation of Ordinary Least Squares', 2, None, @@ -303,74 +309,77 @@ MathJax.Hub.Config({
  • Why Linear Regression (aka Ordinary Least Squares and family), repeat from last week
  • Regression analysis, overarching aims
  • Regression analysis, overarching aims II
  • -
  • Examples
  • +
  • Examples
  • General linear models
  • Rewriting the fitting procedure as a linear algebra problem
  • Rewriting the fitting procedure as a linear algebra problem, more details
  • Generalizing the fitting procedure as a linear algebra problem
  • Generalizing the fitting procedure as a linear algebra problem
  • Optimizing our parameters
  • -
  • Our model for the nuclear binding energies
  • +
  • Examples relevant for the exercises
  • Optimizing our parameters, more details
  • -
  • Interpretations and optimizing our parameters
  • -
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • +
  • Interpretations and optimizing our parameters
  • Some useful matrix and vector expressions
  • -
  • Meet the Hessian Matrix
  • -
  • Interpretations and optimizing our parameters
  • -
  • Own code for Ordinary Least Squares
  • -
  • Adding error analysis and training set up
  • -
  • Splitting our Data in Training and Test data
  • -
  • Examples
  • -
  • Making your own test-train splitting
  • -
  • The Boston housing data example
  • -
  • Housing data, the code
  • -
  • Reducing the number of degrees of freedom, overarching view
  • -
  • Preprocessing our data
  • -
  • Functionality in Scikit-Learn
  • -
  • More preprocessing
  • -
  • Frequently used scaling functions
  • -
  • Example of own Standard scaling
  • -
  • Min-Max Scaling
  • -
  • Testing the Means Squared Error as function of Complexity
  • -
  • More preprocessing examples, Franke function and regression
  • -
  • Mathematical Interpretation of Ordinary Least Squares
  • -
  • Residual Error
  • -
  • Simple case
  • -
  • The singular value decomposition
  • -
  • Linear Regression Problems
  • -
  • Fixing the singularity
  • -
  • Basic math of the SVD
  • -
  • The SVD, a Fantastic Algorithm
  • -
  • Economy-size SVD
  • -
  • Codes for the SVD
  • -
  • Note about SVD Calculations
  • -
  • Mathematics of the SVD and implications
  • -
  • Example Matrix
  • -
  • Setting up the Matrix to be inverted
  • -
  • Further properties (important for our analyses later)
  • -
  • Meet the Covariance Matrix
  • -
  • Introducing the Covariance and Correlation functions
  • -
  • Covariance and Correlation Matrix
  • -
  • Correlation Function and Design/Feature Matrix
  • -
  • Covariance Matrix Examples
  • -
  • Correlation Matrix
  • -
  • Correlation Matrix with Pandas
  • -
  • Correlation Matrix with Pandas and the Franke function
  • -
  • Rewriting the Covariance and/or Correlation Matrix
  • -
  • Linking with the SVD
  • -
  • What does it mean?
  • -
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • -
  • Ridge and LASSO Regression
  • -
  • Deriving the Ridge Regression Equations
  • -
  • Interpreting the Ridge results
  • -
  • More interpretations
  • -
  • Deriving the Lasso Regression Equations
  • -
  • Exercises for week 35
  • -
  • Exercise 1: Setting up various Python environments
  • -
  • Exercise 2: making your own data and exploring scikit-learn
  • -
  • Exercise 3: Normalizing our data
  • -
  • Exercise 4: Adding Ridge Regression
  • -
  • Exercise 5: Analytical exercises
  • +
  • The Jacobian
  • +
  • Derivatives, example 1
  • +
  • Meet the Hessian Matrix
  • +
  • Interpretations and optimizing our parameters
  • +
  • Own code for Ordinary Least Squares
  • +
  • Adding error analysis and training set up
  • +
  • Splitting our Data in Training and Test data
  • +
  • Examples
  • +
  • Making your own test-train splitting
  • +
  • The Boston housing data example
  • +
  • Housing data, the code
  • +
  • Reducing the number of degrees of freedom, overarching view
  • +
  • Preprocessing our data
  • +
  • Functionality in Scikit-Learn
  • +
  • More preprocessing
  • +
  • Frequently used scaling functions
  • +
  • Example of own Standard scaling
  • +
  • Min-Max Scaling
  • +
  • Testing the Means Squared Error as function of Complexity
  • +
  • More preprocessing examples, Franke function and regression
  • +
  • Material for lecture Thursday, August 31
  • +
  • Mathematical Interpretation of Ordinary Least Squares
  • +
  • Residual Error
  • +
  • Simple case
  • +
  • The singular value decomposition
  • +
  • Linear Regression Problems
  • +
  • Fixing the singularity
  • +
  • Basic math of the SVD
  • +
  • The SVD, a Fantastic Algorithm
  • +
  • Economy-size SVD
  • +
  • Codes for the SVD
  • +
  • Note about SVD Calculations
  • +
  • Mathematics of the SVD and implications
  • +
  • Example Matrix
  • +
  • Setting up the Matrix to be inverted
  • +
  • Further properties (important for our analyses later)
  • +
  • Meet the Covariance Matrix
  • +
  • Introducing the Covariance and Correlation functions
  • +
  • Covariance and Correlation Matrix
  • +
  • Correlation Function and Design/Feature Matrix
  • +
  • Covariance Matrix Examples
  • +
  • Correlation Matrix
  • +
  • Correlation Matrix with Pandas
  • +
  • Correlation Matrix with Pandas and the Franke function
  • +
  • Rewriting the Covariance and/or Correlation Matrix
  • +
  • Linking with the SVD
  • +
  • What does it mean?
  • +
  • And finally \( \boldsymbol{X}\boldsymbol{X}^T \)
  • +
  • Ridge and LASSO Regression
  • +
  • Deriving the Ridge Regression Equations
  • +
  • Interpreting the Ridge results
  • +
  • More interpretations
  • +
  • Deriving the Lasso Regression Equations
  • +
  • Exercises for week 35
  • +
  • Exercise 1: Setting up various Python environments
  • +
  • Exercise 2: making your own data and exploring scikit-learn
  • +
  • Exercise 3: Normalizing our data
  • +
  • Exercise 4: Adding Ridge Regression
  • +
  • Exercise 5: Analytical exercises
  • @@ -400,7 +409,7 @@ MathJax.Hub.Config({
    -

    Jul 8, 2023

    +

    August 28-September 1


    @@ -425,7 +434,7 @@ MathJax.Hub.Config({
  • 9
  • 10
  • ...
  • -
  • 68
  • +
  • 71
  • »
  • diff --git a/doc/pub/week35/html/week35-reveal.html b/doc/pub/week35/html/week35-reveal.html index 7a9d88718..1da9a75fa 100644 --- a/doc/pub/week35/html/week35-reveal.html +++ b/doc/pub/week35/html/week35-reveal.html @@ -184,7 +184,7 @@ MathJax.Hub.Config({
    -

    Jul 8, 2023

    +

    August 28-September 1


    @@ -498,7 +498,7 @@ our matrix as \( \boldsymbol{X}\in {\mathbb{R}}^{n\times p} \), with the predict
    -

    Our model for the nuclear binding energies

    +

    Examples relevant for the exercises

    In our introductory notes we looked at the so-called liquid drop model. Let us remind ourselves about what we did by looking at the code.

    @@ -759,10 +759,83 @@ allow for the usage of direct linear algebra methods such as LU decomposi

    Some useful matrix and vector expressions

    -

    The following matrix and vector relation will be useful here and for the rest of the course. Vectors are always written as boldfaced lower case letters and -matrices as upper case boldfaced letters. +

    The following matrix and vector relation will be useful here and for +the rest of the course. Vectors are always written as boldfaced lower +case letters and matrices as upper case boldfaced letters. In the +following we will discuss how to calculate derivatives of various +matrices relevant for machine learning. We will often represent our +data in terms of matrices and vectors.

    +

    Let us introduce first some conventions. We assume that \( \boldsymbol{y} \) is a +vector of length \( m \), that is it has \( m \) elements \( y_0,y_1,\dots, +y_{m-1} \). By convention we start labeling vectors with the zeroth +element, as are arrays in Python and C++/C, for example. Similarly, we +have a vector \( \boldsymbol{x} \) of length \( n \), that is +\( \boldsymbol{x}^T=[x_0,x_1,\dots, x_{n-1}] \). +

    + +

    We assume also that \( \boldsymbol{y} \) is a function of \( \boldsymbol{x} \) through some +given function \( f \) +

    + +

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

     
    +

    + +
    +

    The Jacobian

    + +

    We define the partial derivatives of the various components of \( \boldsymbol{y} \) as functions of \( x_i \) in terms of the so-called Jacobian matrix

    + +

     
    +$$ +\boldsymbol{J}=\frac{\partial \boldsymbol{y}}{\partial \boldsymbol{x}}=\begin{bmatrix} \frac{\partial y_0}{\partial x_0} & \frac{\partial y_0}{\partial x_1} & \frac{\partial y_0}{\partial x_2} & \dots & \dots & \frac{\partial y_0}{\partial x_{n-1}} \\ \frac{\partial y_0}{\partial x_0} & \frac{\partial y_1}{\partial x_1} & \frac{\partial y_1}{\partial x_2} & \dots & \dots & \frac{\partial y_1}{\partial x_{n-1}} \\ +\frac{\partial y_2}{\partial x_0} & \frac{\partial y_2}{\partial x_1} & \frac{\partial y_2}{\partial x_2} & \dots & \dots & \frac{\partial y_2}{\partial x_{n-1}} \\ +\dots & \dots & \dots & \dots & \dots & \dots \\ +\dots & \dots & \dots & \dots & \dots & \dots \\ +\frac{\partial y_{m-1}}{\partial x_0} & \frac{\partial y_{m-1}}{\partial x_1} & \frac{\partial y_{m-1}}{\partial x_2} & \dots & \dots & \frac{\partial y_{m-1}}{\partial x_{n-1}} \end{bmatrix}, +$$ +

     
    + +

    which is an \( m\times n \) matrix. If \( \boldsymbol{x} \) is a scalar, then the +Jacobian is only a single-column vector, or an \( m\times 1 \) matrix. If +on the other hand \( \boldsymbol{y} \) is a scalar, the Jacobian becomes a +\( 1\times n \) matrix. +

    +
    + +
    +

    Derivatives, example 1

    + +

    Let now \( \boldsymbol{y}=\boldsymbol{A}\boldsymbol{x} \), where \( \boldsymbol{A} \) is an \( m\times n \) matrix and the matrix does not depend on \( \boldsymbol{x} \). If we write out the vector \( \boldsymbol{y} \) compoment by component we have

    + +

     
    +$$ +y_i = \sum_{j=0}^{n-1}a_{ij}x_j, +$$ +

     
    + +

    with \( \all i=0,1,2,\dots,m-1 \). The individual matrix elements of \( \boldsymbol{A} \) are given by the symbol \( a_{ij} \). +It follows that the partial derivatives of \( y_i \) with respect to \( x_k \) +

    +

     
    +$$ +\frac{\partial y_i }{\partial x_k}= a_{ik} \all i=0,1,2,\dots,m-1. +$$ +

     
    + +

    From this we have, using the definition of the Jacobian

    + +

     
    +$$ +\frac{\partial \boldsymbol{y} }{\partial \boldsymbol{x}}= \boldsymbol{A}. +$$ +

     
    +

     
    $$ \frac{\partial (\boldsymbol{b}^T\boldsymbol{a})}{\partial \boldsymbol{a}} = \boldsymbol{b}, @@ -1965,6 +2038,10 @@ clf = skl.LinearRegression().fit(X_train_scaled, y_train)

    +
    +

    Material for lecture Thursday, August 31

    +
    +

    Mathematical Interpretation of Ordinary Least Squares

    diff --git a/doc/pub/week35/html/week35-solarized.html b/doc/pub/week35/html/week35-solarized.html index 9b856febf..ea38042b3 100644 --- a/doc/pub/week35/html/week35-solarized.html +++ b/doc/pub/week35/html/week35-solarized.html @@ -101,10 +101,10 @@ div.toc p,a { 2, None, 'optimizing-our-parameters'), - ('Our model for the nuclear binding energies', + ('Examples relevant for the exercises', 2, None, - 'our-model-for-the-nuclear-binding-energies'), + 'examples-relevant-for-the-exercises'), ('Optimizing our parameters, more details', 2, None, @@ -121,6 +121,8 @@ div.toc p,a { 2, None, 'some-useful-matrix-and-vector-expressions'), + ('The Jacobian', 2, None, 'the-jacobian'), + ('Derivatives, example 1', 2, None, 'derivatives-example-1'), ('Meet the Hessian Matrix', 2, None, 'meet-the-hessian-matrix'), ('Interpretations and optimizing our parameters', 2, @@ -175,6 +177,10 @@ div.toc p,a { 2, None, 'more-preprocessing-examples-franke-function-and-regression'), + ('Material for lecture Thursday, August 31', + 2, + None, + 'material-for-lecture-thursday-august-31'), ('Mathematical Interpretation of Ordinary Least Squares', 2, None, @@ -328,7 +334,7 @@ MathJax.Hub.Config({
    -

    Jul 8, 2023

    +

    August 28-September 1


    @@ -606,7 +612,7 @@ our matrix as \( \boldsymbol{X}\in {\mathbb{R}}^{n\times p} \), with the predict









    -

    Our model for the nuclear binding energies

    +

    Examples relevant for the exercises

    In our introductory notes we looked at the so-called liquid drop model. Let us remind ourselves about what we did by looking at the code.

    @@ -838,10 +844,73 @@ allow for the usage of direct linear algebra methods such as LU decomposi









    Some useful matrix and vector expressions

    -

    The following matrix and vector relation will be useful here and for the rest of the course. Vectors are always written as boldfaced lower case letters and -matrices as upper case boldfaced letters. +

    The following matrix and vector relation will be useful here and for +the rest of the course. Vectors are always written as boldfaced lower +case letters and matrices as upper case boldfaced letters. In the +following we will discuss how to calculate derivatives of various +matrices relevant for machine learning. We will often represent our +data in terms of matrices and vectors.

    +

    Let us introduce first some conventions. We assume that \( \boldsymbol{y} \) is a +vector of length \( m \), that is it has \( m \) elements \( y_0,y_1,\dots, +y_{m-1} \). By convention we start labeling vectors with the zeroth +element, as are arrays in Python and C++/C, for example. Similarly, we +have a vector \( \boldsymbol{x} \) of length \( n \), that is +\( \boldsymbol{x}^T=[x_0,x_1,\dots, x_{n-1}] \). +

    + +

    We assume also that \( \boldsymbol{y} \) is a function of \( \boldsymbol{x} \) through some +given function \( f \) +

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









    +

    The Jacobian

    + +

    We define the partial derivatives of the various components of \( \boldsymbol{y} \) as functions of \( x_i \) in terms of the so-called Jacobian matrix

    + +$$ +\boldsymbol{J}=\frac{\partial \boldsymbol{y}}{\partial \boldsymbol{x}}=\begin{bmatrix} \frac{\partial y_0}{\partial x_0} & \frac{\partial y_0}{\partial x_1} & \frac{\partial y_0}{\partial x_2} & \dots & \dots & \frac{\partial y_0}{\partial x_{n-1}} \\ \frac{\partial y_0}{\partial x_0} & \frac{\partial y_1}{\partial x_1} & \frac{\partial y_1}{\partial x_2} & \dots & \dots & \frac{\partial y_1}{\partial x_{n-1}} \\ +\frac{\partial y_2}{\partial x_0} & \frac{\partial y_2}{\partial x_1} & \frac{\partial y_2}{\partial x_2} & \dots & \dots & \frac{\partial y_2}{\partial x_{n-1}} \\ +\dots & \dots & \dots & \dots & \dots & \dots \\ +\dots & \dots & \dots & \dots & \dots & \dots \\ +\frac{\partial y_{m-1}}{\partial x_0} & \frac{\partial y_{m-1}}{\partial x_1} & \frac{\partial y_{m-1}}{\partial x_2} & \dots & \dots & \frac{\partial y_{m-1}}{\partial x_{n-1}} \end{bmatrix}, +$$ + +

    which is an \( m\times n \) matrix. If \( \boldsymbol{x} \) is a scalar, then the +Jacobian is only a single-column vector, or an \( m\times 1 \) matrix. If +on the other hand \( \boldsymbol{y} \) is a scalar, the Jacobian becomes a +\( 1\times n \) matrix. +

    + +









    +

    Derivatives, example 1

    + +

    Let now \( \boldsymbol{y}=\boldsymbol{A}\boldsymbol{x} \), where \( \boldsymbol{A} \) is an \( m\times n \) matrix and the matrix does not depend on \( \boldsymbol{x} \). If we write out the vector \( \boldsymbol{y} \) compoment by component we have

    + +$$ +y_i = \sum_{j=0}^{n-1}a_{ij}x_j, +$$ + +

    with \( \all i=0,1,2,\dots,m-1 \). The individual matrix elements of \( \boldsymbol{A} \) are given by the symbol \( a_{ij} \). +It follows that the partial derivatives of \( y_i \) with respect to \( x_k \) +

    +$$ +\frac{\partial y_i }{\partial x_k}= a_{ik} \all i=0,1,2,\dots,m-1. +$$ + +

    From this we have, using the definition of the Jacobian

    + +$$ +\frac{\partial \boldsymbol{y} }{\partial \boldsymbol{x}}= \boldsymbol{A}. +$$ + + $$ \frac{\partial (\boldsymbol{b}^T\boldsymbol{a})}{\partial \boldsymbol{a}} = \boldsymbol{b}, $$ @@ -2013,6 +2082,9 @@ clf = skl.LinearRegression().fit(X_train_scaled, y_train)
    +









    +

    Material for lecture Thursday, August 31

    +









    Mathematical Interpretation of Ordinary Least Squares

    diff --git a/doc/pub/week35/html/week35.html b/doc/pub/week35/html/week35.html index df14b77b4..4a8b811ab 100644 --- a/doc/pub/week35/html/week35.html +++ b/doc/pub/week35/html/week35.html @@ -178,10 +178,10 @@ div.toc p,a { 2, None, 'optimizing-our-parameters'), - ('Our model for the nuclear binding energies', + ('Examples relevant for the exercises', 2, None, - 'our-model-for-the-nuclear-binding-energies'), + 'examples-relevant-for-the-exercises'), ('Optimizing our parameters, more details', 2, None, @@ -198,6 +198,8 @@ div.toc p,a { 2, None, 'some-useful-matrix-and-vector-expressions'), + ('The Jacobian', 2, None, 'the-jacobian'), + ('Derivatives, example 1', 2, None, 'derivatives-example-1'), ('Meet the Hessian Matrix', 2, None, 'meet-the-hessian-matrix'), ('Interpretations and optimizing our parameters', 2, @@ -252,6 +254,10 @@ div.toc p,a { 2, None, 'more-preprocessing-examples-franke-function-and-regression'), + ('Material for lecture Thursday, August 31', + 2, + None, + 'material-for-lecture-thursday-august-31'), ('Mathematical Interpretation of Ordinary Least Squares', 2, None, @@ -405,7 +411,7 @@ MathJax.Hub.Config({
    -

    Jul 8, 2023

    +

    August 28-September 1


    @@ -683,7 +689,7 @@ our matrix as \( \boldsymbol{X}\in {\mathbb{R}}^{n\times p} \), with the predict









    -

    Our model for the nuclear binding energies

    +

    Examples relevant for the exercises

    In our introductory notes we looked at the so-called liquid drop model. Let us remind ourselves about what we did by looking at the code.

    @@ -915,10 +921,73 @@ allow for the usage of direct linear algebra methods such as LU decomposi









    Some useful matrix and vector expressions

    -

    The following matrix and vector relation will be useful here and for the rest of the course. Vectors are always written as boldfaced lower case letters and -matrices as upper case boldfaced letters. +

    The following matrix and vector relation will be useful here and for +the rest of the course. Vectors are always written as boldfaced lower +case letters and matrices as upper case boldfaced letters. In the +following we will discuss how to calculate derivatives of various +matrices relevant for machine learning. We will often represent our +data in terms of matrices and vectors.

    +

    Let us introduce first some conventions. We assume that \( \boldsymbol{y} \) is a +vector of length \( m \), that is it has \( m \) elements \( y_0,y_1,\dots, +y_{m-1} \). By convention we start labeling vectors with the zeroth +element, as are arrays in Python and C++/C, for example. Similarly, we +have a vector \( \boldsymbol{x} \) of length \( n \), that is +\( \boldsymbol{x}^T=[x_0,x_1,\dots, x_{n-1}] \). +

    + +

    We assume also that \( \boldsymbol{y} \) is a function of \( \boldsymbol{x} \) through some +given function \( f \) +

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









    +

    The Jacobian

    + +

    We define the partial derivatives of the various components of \( \boldsymbol{y} \) as functions of \( x_i \) in terms of the so-called Jacobian matrix

    + +$$ +\boldsymbol{J}=\frac{\partial \boldsymbol{y}}{\partial \boldsymbol{x}}=\begin{bmatrix} \frac{\partial y_0}{\partial x_0} & \frac{\partial y_0}{\partial x_1} & \frac{\partial y_0}{\partial x_2} & \dots & \dots & \frac{\partial y_0}{\partial x_{n-1}} \\ \frac{\partial y_0}{\partial x_0} & \frac{\partial y_1}{\partial x_1} & \frac{\partial y_1}{\partial x_2} & \dots & \dots & \frac{\partial y_1}{\partial x_{n-1}} \\ +\frac{\partial y_2}{\partial x_0} & \frac{\partial y_2}{\partial x_1} & \frac{\partial y_2}{\partial x_2} & \dots & \dots & \frac{\partial y_2}{\partial x_{n-1}} \\ +\dots & \dots & \dots & \dots & \dots & \dots \\ +\dots & \dots & \dots & \dots & \dots & \dots \\ +\frac{\partial y_{m-1}}{\partial x_0} & \frac{\partial y_{m-1}}{\partial x_1} & \frac{\partial y_{m-1}}{\partial x_2} & \dots & \dots & \frac{\partial y_{m-1}}{\partial x_{n-1}} \end{bmatrix}, +$$ + +

    which is an \( m\times n \) matrix. If \( \boldsymbol{x} \) is a scalar, then the +Jacobian is only a single-column vector, or an \( m\times 1 \) matrix. If +on the other hand \( \boldsymbol{y} \) is a scalar, the Jacobian becomes a +\( 1\times n \) matrix. +

    + +









    +

    Derivatives, example 1

    + +

    Let now \( \boldsymbol{y}=\boldsymbol{A}\boldsymbol{x} \), where \( \boldsymbol{A} \) is an \( m\times n \) matrix and the matrix does not depend on \( \boldsymbol{x} \). If we write out the vector \( \boldsymbol{y} \) compoment by component we have

    + +$$ +y_i = \sum_{j=0}^{n-1}a_{ij}x_j, +$$ + +

    with \( \all i=0,1,2,\dots,m-1 \). The individual matrix elements of \( \boldsymbol{A} \) are given by the symbol \( a_{ij} \). +It follows that the partial derivatives of \( y_i \) with respect to \( x_k \) +

    +$$ +\frac{\partial y_i }{\partial x_k}= a_{ik} \all i=0,1,2,\dots,m-1. +$$ + +

    From this we have, using the definition of the Jacobian

    + +$$ +\frac{\partial \boldsymbol{y} }{\partial \boldsymbol{x}}= \boldsymbol{A}. +$$ + + $$ \frac{\partial (\boldsymbol{b}^T\boldsymbol{a})}{\partial \boldsymbol{a}} = \boldsymbol{b}, $$ @@ -2090,6 +2159,9 @@ clf = skl. +









    +

    Material for lecture Thursday, August 31

    +









    Mathematical Interpretation of Ordinary Least Squares

    diff --git a/doc/pub/week35/ipynb/ipynb-week35-src.tar.gz b/doc/pub/week35/ipynb/ipynb-week35-src.tar.gz index 865f6d9ce..47ded15ac 100644 Binary files a/doc/pub/week35/ipynb/ipynb-week35-src.tar.gz and b/doc/pub/week35/ipynb/ipynb-week35-src.tar.gz differ diff --git a/doc/pub/week35/ipynb/week35.ipynb b/doc/pub/week35/ipynb/week35.ipynb index a89f4111c..89291e827 100644 --- a/doc/pub/week35/ipynb/week35.ipynb +++ b/doc/pub/week35/ipynb/week35.ipynb @@ -2,7 +2,7 @@ "cells": [ { "cell_type": "markdown", - "id": "75ce285d", + "id": "272435cb", "metadata": { "editable": true }, @@ -14,7 +14,7 @@ }, { "cell_type": "markdown", - "id": "6819a447", + "id": "f093dc4f", "metadata": { "editable": true }, @@ -22,14 +22,12 @@ "# Week 35: From Ordinary Linear Regression to Ridge and Lasso Regression\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: **Jul 8, 2023**\n", - "\n", - "Copyright 1999-2023, Morten Hjorth-Jensen. Released under CC Attribution-NonCommercial 4.0 license" + "Date: **August 28-September 1**" ] }, { "cell_type": "markdown", - "id": "34510522", + "id": "265f9aa7", "metadata": { "editable": true }, @@ -51,7 +49,7 @@ }, { "cell_type": "markdown", - "id": "d1e32977", + "id": "462c23c5", "metadata": { "editable": true }, @@ -71,7 +69,7 @@ }, { "cell_type": "markdown", - "id": "f48d4422", + "id": "9301fed2", "metadata": { "editable": true }, @@ -105,7 +103,7 @@ }, { "cell_type": "markdown", - "id": "2b8c1c80", + "id": "a65340db", "metadata": { "editable": true }, @@ -127,7 +125,7 @@ }, { "cell_type": "markdown", - "id": "9e3a36c6", + "id": "ac3e11cb", "metadata": { "editable": true }, @@ -155,7 +153,7 @@ }, { "cell_type": "markdown", - "id": "c8f0e76f", + "id": "573be022", "metadata": { "editable": true }, @@ -170,7 +168,7 @@ }, { "cell_type": "markdown", - "id": "8752e175", + "id": "8fd21bf4", "metadata": { "editable": true }, @@ -182,7 +180,7 @@ }, { "cell_type": "markdown", - "id": "6714b875", + "id": "ffe94532", "metadata": { "editable": true }, @@ -197,7 +195,7 @@ }, { "cell_type": "markdown", - "id": "339987f7", + "id": "bc9309fa", "metadata": { "editable": true }, @@ -210,7 +208,7 @@ }, { "cell_type": "markdown", - "id": "f847039c", + "id": "22893c01", "metadata": { "editable": true }, @@ -222,7 +220,7 @@ }, { "cell_type": "markdown", - "id": "0d52a948", + "id": "8bfce1af", "metadata": { "editable": true }, @@ -232,7 +230,7 @@ }, { "cell_type": "markdown", - "id": "76b2bc68", + "id": "0745c236", "metadata": { "editable": true }, @@ -243,7 +241,7 @@ }, { "cell_type": "markdown", - "id": "567fe913", + "id": "6d13835b", "metadata": { "editable": true }, @@ -261,7 +259,7 @@ }, { "cell_type": "markdown", - "id": "c1a6fd23", + "id": "495f813a", "metadata": { "editable": true }, @@ -272,7 +270,7 @@ }, { "cell_type": "markdown", - "id": "9db1559d", + "id": "dad327bb", "metadata": { "editable": true }, @@ -284,7 +282,7 @@ }, { "cell_type": "markdown", - "id": "a005c600", + "id": "79a03a3a", "metadata": { "editable": true }, @@ -294,7 +292,7 @@ }, { "cell_type": "markdown", - "id": "b29a7b70", + "id": "9649f653", "metadata": { "editable": true }, @@ -306,7 +304,7 @@ }, { "cell_type": "markdown", - "id": "a93aea90", + "id": "af19c028", "metadata": { "editable": true }, @@ -316,7 +314,7 @@ }, { "cell_type": "markdown", - "id": "0bf9756e", + "id": "93f05270", "metadata": { "editable": true }, @@ -328,7 +326,7 @@ }, { "cell_type": "markdown", - "id": "6ad492d1", + "id": "c8a3416f", "metadata": { "editable": true }, @@ -338,7 +336,7 @@ }, { "cell_type": "markdown", - "id": "7a46e7f2", + "id": "9a82182d", "metadata": { "editable": true }, @@ -357,7 +355,7 @@ }, { "cell_type": "markdown", - "id": "bbe66c06", + "id": "816ca248", "metadata": { "editable": true }, @@ -367,7 +365,7 @@ }, { "cell_type": "markdown", - "id": "9591f5bb", + "id": "215690d7", "metadata": { "editable": true }, @@ -379,7 +377,7 @@ }, { "cell_type": "markdown", - "id": "3516f877", + "id": "4b2c135a", "metadata": { "editable": true }, @@ -389,7 +387,7 @@ }, { "cell_type": "markdown", - "id": "0982b255", + "id": "18509d96", "metadata": { "editable": true }, @@ -405,7 +403,7 @@ }, { "cell_type": "markdown", - "id": "01edd291", + "id": "6f5f06fb", "metadata": { "editable": true }, @@ -425,7 +423,7 @@ }, { "cell_type": "markdown", - "id": "800aa671", + "id": "23031b67", "metadata": { "editable": true }, @@ -435,7 +433,7 @@ }, { "cell_type": "markdown", - "id": "ca0d5be2", + "id": "2e4d63d0", "metadata": { "editable": true }, @@ -446,7 +444,7 @@ }, { "cell_type": "markdown", - "id": "7ee244e0", + "id": "7f9c0240", "metadata": { "editable": true }, @@ -465,7 +463,7 @@ }, { "cell_type": "markdown", - "id": "bc7d6881", + "id": "c784ade5", "metadata": { "editable": true }, @@ -475,7 +473,7 @@ }, { "cell_type": "markdown", - "id": "b3f7b747", + "id": "c82604ad", "metadata": { "editable": true }, @@ -487,7 +485,7 @@ }, { "cell_type": "markdown", - "id": "3c3519ff", + "id": "c2a79c18", "metadata": { "editable": true }, @@ -497,7 +495,7 @@ }, { "cell_type": "markdown", - "id": "08061645", + "id": "ce13e7da", "metadata": { "editable": true }, @@ -508,7 +506,7 @@ }, { "cell_type": "markdown", - "id": "353a96d3", + "id": "d1a2885a", "metadata": { "editable": true }, @@ -528,7 +526,7 @@ }, { "cell_type": "markdown", - "id": "0c205a85", + "id": "64b948e9", "metadata": { "editable": true }, @@ -540,12 +538,12 @@ }, { "cell_type": "markdown", - "id": "efe73511", + "id": "424cd697", "metadata": { "editable": true }, "source": [ - "## Our model for the nuclear binding energies\n", + "## Examples relevant for the exercises\n", "\n", "In our [introductory notes](https://compphysics.github.io/MachineLearning/doc/pub/How2ReadData/html/How2ReadData.html) we looked at the so-called [liquid drop model](https://en.wikipedia.org/wiki/Semi-empirical_mass_formula). Let us remind ourselves about what we did by looking at the code.\n", "\n", @@ -555,7 +553,7 @@ { "cell_type": "code", "execution_count": 1, - "id": "9999cb80", + "id": "e4b9f801", "metadata": { "collapsed": false, "editable": true @@ -637,7 +635,7 @@ }, { "cell_type": "markdown", - "id": "c68cb3e2", + "id": "7e8d1d9b", "metadata": { "editable": true }, @@ -647,7 +645,7 @@ }, { "cell_type": "markdown", - "id": "1424b894", + "id": "3902f678", "metadata": { "editable": true }, @@ -659,7 +657,7 @@ }, { "cell_type": "markdown", - "id": "84b5a2e6", + "id": "b32729d6", "metadata": { "editable": true }, @@ -669,7 +667,7 @@ }, { "cell_type": "markdown", - "id": "aec9d52b", + "id": "1040517d", "metadata": { "editable": true }, @@ -680,7 +678,7 @@ }, { "cell_type": "markdown", - "id": "7b3883ff", + "id": "a5fe300b", "metadata": { "editable": true }, @@ -692,7 +690,7 @@ }, { "cell_type": "markdown", - "id": "3db3aa7c", + "id": "b42bede4", "metadata": { "editable": true }, @@ -702,7 +700,7 @@ }, { "cell_type": "markdown", - "id": "6688487d", + "id": "dd4ef630", "metadata": { "editable": true }, @@ -714,7 +712,7 @@ }, { "cell_type": "markdown", - "id": "3e293818", + "id": "1637ce36", "metadata": { "editable": true }, @@ -724,7 +722,7 @@ }, { "cell_type": "markdown", - "id": "17b9e246", + "id": "359da65f", "metadata": { "editable": true }, @@ -736,7 +734,7 @@ }, { "cell_type": "markdown", - "id": "0e407c91", + "id": "7a2a8ded", "metadata": { "editable": true }, @@ -749,7 +747,7 @@ }, { "cell_type": "markdown", - "id": "b46358da", + "id": "f94c0601", "metadata": { "editable": true }, @@ -761,7 +759,7 @@ }, { "cell_type": "markdown", - "id": "f8788af2", + "id": "331a2eb8", "metadata": { "editable": true }, @@ -771,7 +769,7 @@ }, { "cell_type": "markdown", - "id": "6b39c987", + "id": "a7da4944", "metadata": { "editable": true }, @@ -783,7 +781,7 @@ }, { "cell_type": "markdown", - "id": "a2d753f1", + "id": "115c35a0", "metadata": { "editable": true }, @@ -795,7 +793,7 @@ }, { "cell_type": "markdown", - "id": "5e64fa3f", + "id": "e11e2f13", "metadata": { "editable": true }, @@ -806,7 +804,7 @@ }, { "cell_type": "markdown", - "id": "e8c0b484", + "id": "1856ce84", "metadata": { "editable": true }, @@ -818,7 +816,7 @@ }, { "cell_type": "markdown", - "id": "dd888c9b", + "id": "6b82f87b", "metadata": { "editable": true }, @@ -837,7 +835,7 @@ }, { "cell_type": "markdown", - "id": "f399c77a", + "id": "33b1d7cb", "metadata": { "editable": true }, @@ -850,7 +848,7 @@ }, { "cell_type": "markdown", - "id": "32784514", + "id": "f315add2", "metadata": { "editable": true }, @@ -860,7 +858,7 @@ }, { "cell_type": "markdown", - "id": "2c8272b0", + "id": "6a0abc23", "metadata": { "editable": true }, @@ -872,7 +870,7 @@ }, { "cell_type": "markdown", - "id": "cb66dbd4", + "id": "bc7438dc", "metadata": { "editable": true }, @@ -882,7 +880,7 @@ }, { "cell_type": "markdown", - "id": "9ffdaf71", + "id": "9a47e07a", "metadata": { "editable": true }, @@ -894,7 +892,7 @@ }, { "cell_type": "markdown", - "id": "604b389a", + "id": "0858cac5", "metadata": { "editable": true }, @@ -904,7 +902,7 @@ }, { "cell_type": "markdown", - "id": "2f12a7fc", + "id": "5351a875", "metadata": { "editable": true }, @@ -916,7 +914,7 @@ }, { "cell_type": "markdown", - "id": "f13d2f22", + "id": "be8e16f7", "metadata": { "editable": true }, @@ -927,7 +925,7 @@ }, { "cell_type": "markdown", - "id": "007fb5af", + "id": "2858ceb7", "metadata": { "editable": true }, @@ -939,7 +937,7 @@ }, { "cell_type": "markdown", - "id": "60933a47", + "id": "14ac2764", "metadata": { "editable": true }, @@ -949,7 +947,7 @@ }, { "cell_type": "markdown", - "id": "fef3db78", + "id": "277b6a55", "metadata": { "editable": true }, @@ -961,7 +959,7 @@ }, { "cell_type": "markdown", - "id": "c12c81eb", + "id": "9f6cbddd", "metadata": { "editable": true }, @@ -971,7 +969,7 @@ }, { "cell_type": "markdown", - "id": "3ba7a26a", + "id": "ce620b5c", "metadata": { "editable": true }, @@ -983,7 +981,7 @@ }, { "cell_type": "markdown", - "id": "72e9f033", + "id": "48bed274", "metadata": { "editable": true }, @@ -1004,20 +1002,156 @@ }, { "cell_type": "markdown", - "id": "becc5c12", + "id": "e0da63bf", "metadata": { "editable": true }, "source": [ "## Some useful matrix and vector expressions\n", "\n", - "The following matrix and vector relation will be useful here and for the rest of the course. Vectors are always written as boldfaced lower case letters and \n", - "matrices as upper case boldfaced letters." + "The following matrix and vector relation will be useful here and for\n", + "the rest of the course. Vectors are always written as boldfaced lower\n", + "case letters and matrices as upper case boldfaced letters. In the\n", + "following we will discuss how to calculate derivatives of various\n", + "matrices relevant for machine learning. We will often represent our\n", + "data in terms of matrices and vectors.\n", + "\n", + "Let us introduce first some conventions. We assume that $\\boldsymbol{y}$ is a\n", + "vector of length $m$, that is it has $m$ elements $y_0,y_1,\\dots,\n", + "y_{m-1}$. By convention we start labeling vectors with the zeroth\n", + "element, as are arrays in Python and C++/C, for example. Similarly, we\n", + "have a vector $\\boldsymbol{x}$ of length $n$, that is\n", + "$\\boldsymbol{x}^T=[x_0,x_1,\\dots, x_{n-1}]$.\n", + "\n", + "We assume also that $\\boldsymbol{y}$ is a function of $\\boldsymbol{x}$ through some\n", + "given function $f$" ] }, { "cell_type": "markdown", - "id": "3fb86e95", + "id": "9d844e47", + "metadata": { + "editable": true + }, + "source": [ + "$$\n", + "\\boldsymbol{y}=f(\\boldsymbol{x}).\n", + "$$" + ] + }, + { + "cell_type": "markdown", + "id": "31b8b53d", + "metadata": { + "editable": true + }, + "source": [ + "## The Jacobian\n", + "\n", + "We define the partial derivatives of the various components of $\\boldsymbol{y}$ as functions of $x_i$ in terms of the so-called [Jacobian matrix](https://en.wikipedia.org/wiki/Jacobian_matrix_and_determinant)" + ] + }, + { + "cell_type": "markdown", + "id": "c8e8ee22", + "metadata": { + "editable": true + }, + "source": [ + "$$\n", + "\\boldsymbol{J}=\\frac{\\partial \\boldsymbol{y}}{\\partial \\boldsymbol{x}}=\\begin{bmatrix} \\frac{\\partial y_0}{\\partial x_0} & \\frac{\\partial y_0}{\\partial x_1} & \\frac{\\partial y_0}{\\partial x_2} & \\dots & \\dots & \\frac{\\partial y_0}{\\partial x_{n-1}} \\\\ \\frac{\\partial y_0}{\\partial x_0} & \\frac{\\partial y_1}{\\partial x_1} & \\frac{\\partial y_1}{\\partial x_2} & \\dots & \\dots & \\frac{\\partial y_1}{\\partial x_{n-1}} \\\\\n", + "\\frac{\\partial y_2}{\\partial x_0} & \\frac{\\partial y_2}{\\partial x_1} & \\frac{\\partial y_2}{\\partial x_2} & \\dots & \\dots & \\frac{\\partial y_2}{\\partial x_{n-1}} \\\\\n", + "\\dots & \\dots & \\dots & \\dots & \\dots & \\dots \\\\\n", + "\\dots & \\dots & \\dots & \\dots & \\dots & \\dots \\\\\n", + "\\frac{\\partial y_{m-1}}{\\partial x_0} & \\frac{\\partial y_{m-1}}{\\partial x_1} & \\frac{\\partial y_{m-1}}{\\partial x_2} & \\dots & \\dots & \\frac{\\partial y_{m-1}}{\\partial x_{n-1}} \\end{bmatrix},\n", + "$$" + ] + }, + { + "cell_type": "markdown", + "id": "112c3851", + "metadata": { + "editable": true + }, + "source": [ + "which is an $m\\times n$ matrix. If $\\boldsymbol{x}$ is a scalar, then the\n", + "Jacobian is only a single-column vector, or an $m\\times 1$ matrix. If\n", + "on the other hand $\\boldsymbol{y}$ is a scalar, the Jacobian becomes a\n", + "$1\\times n$ matrix." + ] + }, + { + "cell_type": "markdown", + "id": "29091702", + "metadata": { + "editable": true + }, + "source": [ + "## Derivatives, example 1\n", + "\n", + "Let now $\\boldsymbol{y}=\\boldsymbol{A}\\boldsymbol{x}$, where $\\boldsymbol{A}$ is an $m\\times n$ matrix and the matrix does not depend on $\\boldsymbol{x}$. If we write out the vector $\\boldsymbol{y}$ compoment by component we have" + ] + }, + { + "cell_type": "markdown", + "id": "5921c4c0", + "metadata": { + "editable": true + }, + "source": [ + "$$\n", + "y_i = \\sum_{j=0}^{n-1}a_{ij}x_j,\n", + "$$" + ] + }, + { + "cell_type": "markdown", + "id": "b4d433ce", + "metadata": { + "editable": true + }, + "source": [ + "with $\\all i=0,1,2,\\dots,m-1$. The individual matrix elements of $\\boldsymbol{A}$ are given by the symbol $a_{ij}$.\n", + "It follows that the partial derivatives of $y_i$ with respect to $x_k$" + ] + }, + { + "cell_type": "markdown", + "id": "e9f4bb68", + "metadata": { + "editable": true + }, + "source": [ + "$$\n", + "\\frac{\\partial y_i }{\\partial x_k}= a_{ik} \\all i=0,1,2,\\dots,m-1.\n", + "$$" + ] + }, + { + "cell_type": "markdown", + "id": "4656a5ae", + "metadata": { + "editable": true + }, + "source": [ + "From this we have, using the definition of the Jacobian" + ] + }, + { + "cell_type": "markdown", + "id": "08880e1e", + "metadata": { + "editable": true + }, + "source": [ + "$$\n", + "\\frac{\\partial \\boldsymbol{y} }{\\partial \\boldsymbol{x}}= \\boldsymbol{A}.\n", + "$$" + ] + }, + { + "cell_type": "markdown", + "id": "0623f4f2", "metadata": { "editable": true }, @@ -1029,7 +1163,7 @@ }, { "cell_type": "markdown", - "id": "05351a9f", + "id": "1b4d4788", "metadata": { "editable": true }, @@ -1041,7 +1175,7 @@ }, { "cell_type": "markdown", - "id": "4611d80e", + "id": "5ffa77d2", "metadata": { "editable": true }, @@ -1053,7 +1187,7 @@ }, { "cell_type": "markdown", - "id": "5eb61087", + "id": "e449fb3f", "metadata": { "editable": true }, @@ -1065,7 +1199,7 @@ }, { "cell_type": "markdown", - "id": "7cff2f92", + "id": "ce40d458", "metadata": { "editable": true }, @@ -1075,7 +1209,7 @@ }, { "cell_type": "markdown", - "id": "e79bbbc9", + "id": "09032e8a", "metadata": { "editable": true }, @@ -1091,7 +1225,7 @@ }, { "cell_type": "markdown", - "id": "16781f8d", + "id": "0febef6f", "metadata": { "editable": true }, @@ -1103,7 +1237,7 @@ }, { "cell_type": "markdown", - "id": "b98afcd0", + "id": "58bce867", "metadata": { "editable": true }, @@ -1113,7 +1247,7 @@ }, { "cell_type": "markdown", - "id": "bed237e9", + "id": "f8fbefd3", "metadata": { "editable": true }, @@ -1125,7 +1259,7 @@ }, { "cell_type": "markdown", - "id": "a5e3b704", + "id": "0d330b80", "metadata": { "editable": true }, @@ -1143,7 +1277,7 @@ }, { "cell_type": "markdown", - "id": "fb7147cc", + "id": "cc309220", "metadata": { "editable": true }, @@ -1154,7 +1288,7 @@ }, { "cell_type": "markdown", - "id": "73c8ee87", + "id": "8eb1e00f", "metadata": { "editable": true }, @@ -1166,7 +1300,7 @@ }, { "cell_type": "markdown", - "id": "c7503b05", + "id": "763bdcc8", "metadata": { "editable": true }, @@ -1176,7 +1310,7 @@ }, { "cell_type": "markdown", - "id": "984a6a0a", + "id": "11763e6b", "metadata": { "editable": true }, @@ -1188,7 +1322,7 @@ }, { "cell_type": "markdown", - "id": "9ad2010c", + "id": "341dc56a", "metadata": { "editable": true }, @@ -1198,7 +1332,7 @@ }, { "cell_type": "markdown", - "id": "21a6fb38", + "id": "7bcd7769", "metadata": { "editable": true }, @@ -1210,7 +1344,7 @@ }, { "cell_type": "markdown", - "id": "6be24793", + "id": "2ba30ee0", "metadata": { "editable": true }, @@ -1220,7 +1354,7 @@ }, { "cell_type": "markdown", - "id": "edf5e133", + "id": "d30b103a", "metadata": { "editable": true }, @@ -1234,7 +1368,7 @@ { "cell_type": "code", "execution_count": 2, - "id": "03843ad2", + "id": "07c65570", "metadata": { "collapsed": false, "editable": true @@ -1249,7 +1383,7 @@ }, { "cell_type": "markdown", - "id": "41be190f", + "id": "ff0c8ae7", "metadata": { "editable": true }, @@ -1260,7 +1394,7 @@ { "cell_type": "code", "execution_count": 3, - "id": "00a1c1cd", + "id": "5738cb64", "metadata": { "collapsed": false, "editable": true @@ -1273,7 +1407,7 @@ }, { "cell_type": "markdown", - "id": "612ea40b", + "id": "8063c994", "metadata": { "editable": true }, @@ -1284,7 +1418,7 @@ { "cell_type": "code", "execution_count": 4, - "id": "fdfb963e", + "id": "efb6044a", "metadata": { "collapsed": false, "editable": true @@ -1307,7 +1441,7 @@ }, { "cell_type": "markdown", - "id": "5ea99cd3", + "id": "a5ee6e08", "metadata": { "editable": true }, @@ -1321,7 +1455,7 @@ { "cell_type": "code", "execution_count": 5, - "id": "2725f834", + "id": "39bc2995", "metadata": { "collapsed": false, "editable": true @@ -1334,7 +1468,7 @@ }, { "cell_type": "markdown", - "id": "3442ac6a", + "id": "b3163039", "metadata": { "editable": true }, @@ -1345,7 +1479,7 @@ { "cell_type": "code", "execution_count": 6, - "id": "729decfe", + "id": "8fe1005e", "metadata": { "collapsed": false, "editable": true @@ -1357,7 +1491,7 @@ }, { "cell_type": "markdown", - "id": "425afefc", + "id": "10357042", "metadata": { "editable": true }, @@ -1368,7 +1502,7 @@ { "cell_type": "code", "execution_count": 7, - "id": "398db4c7", + "id": "a6014a48", "metadata": { "collapsed": false, "editable": true @@ -1384,7 +1518,7 @@ }, { "cell_type": "markdown", - "id": "9b379132", + "id": "75f80804", "metadata": { "editable": true }, @@ -1395,7 +1529,7 @@ { "cell_type": "code", "execution_count": 8, - "id": "321e289d", + "id": "65656a4b", "metadata": { "collapsed": false, "editable": true @@ -1409,7 +1543,7 @@ }, { "cell_type": "markdown", - "id": "f1bbd99e", + "id": "076efb15", "metadata": { "editable": true }, @@ -1430,7 +1564,7 @@ }, { "cell_type": "markdown", - "id": "5bb34250", + "id": "2b116d45", "metadata": { "editable": true }, @@ -1441,7 +1575,7 @@ { "cell_type": "code", "execution_count": 9, - "id": "c842ea57", + "id": "d693a83b", "metadata": { "collapsed": false, "editable": true @@ -1490,7 +1624,7 @@ }, { "cell_type": "markdown", - "id": "ef015bff", + "id": "922803d4", "metadata": { "editable": true }, @@ -1501,7 +1635,7 @@ { "cell_type": "code", "execution_count": 10, - "id": "177d63c3", + "id": "ca2170d3", "metadata": { "collapsed": false, "editable": true @@ -1526,7 +1660,7 @@ }, { "cell_type": "markdown", - "id": "6d7ab706", + "id": "d50664cf", "metadata": { "editable": true }, @@ -1538,7 +1672,7 @@ }, { "cell_type": "markdown", - "id": "2843bce2", + "id": "effa633b", "metadata": { "editable": true }, @@ -1582,7 +1716,7 @@ }, { "cell_type": "markdown", - "id": "7d6025e0", + "id": "1f61b229", "metadata": { "editable": true }, @@ -1594,7 +1728,7 @@ { "cell_type": "code", "execution_count": 11, - "id": "f26e5e64", + "id": "6e9092c5", "metadata": { "collapsed": false, "editable": true @@ -1610,7 +1744,7 @@ }, { "cell_type": "markdown", - "id": "8263e4ec", + "id": "7624c525", "metadata": { "editable": true }, @@ -1621,7 +1755,7 @@ { "cell_type": "code", "execution_count": 12, - "id": "b7001730", + "id": "c9b45f28", "metadata": { "collapsed": false, "editable": true @@ -1639,7 +1773,7 @@ }, { "cell_type": "markdown", - "id": "101a43c1", + "id": "729068b9", "metadata": { "editable": true }, @@ -1650,7 +1784,7 @@ { "cell_type": "code", "execution_count": 13, - "id": "799a59b0", + "id": "29e10e39", "metadata": { "collapsed": false, "editable": true @@ -1664,7 +1798,7 @@ }, { "cell_type": "markdown", - "id": "e223f06a", + "id": "d95f84cc", "metadata": { "editable": true }, @@ -1675,7 +1809,7 @@ { "cell_type": "code", "execution_count": 14, - "id": "10b3a52f", + "id": "42a4809f", "metadata": { "collapsed": false, "editable": true @@ -1688,7 +1822,7 @@ }, { "cell_type": "markdown", - "id": "6765a784", + "id": "246fad06", "metadata": { "editable": true }, @@ -1699,7 +1833,7 @@ { "cell_type": "code", "execution_count": 15, - "id": "c313d9fe", + "id": "8ba59041", "metadata": { "collapsed": false, "editable": true @@ -1716,7 +1850,7 @@ }, { "cell_type": "markdown", - "id": "07049e0b", + "id": "5db136c1", "metadata": { "editable": true }, @@ -1727,7 +1861,7 @@ { "cell_type": "code", "execution_count": 16, - "id": "f6db174d", + "id": "3b27503f", "metadata": { "collapsed": false, "editable": true @@ -1743,7 +1877,7 @@ }, { "cell_type": "markdown", - "id": "9f3dd30e", + "id": "803cbc6f", "metadata": { "editable": true }, @@ -1754,7 +1888,7 @@ { "cell_type": "code", "execution_count": 17, - "id": "88de5144", + "id": "20c209d9", "metadata": { "collapsed": false, "editable": true @@ -1778,7 +1912,7 @@ }, { "cell_type": "markdown", - "id": "ab81dade", + "id": "376f8747", "metadata": { "editable": true }, @@ -1789,7 +1923,7 @@ { "cell_type": "code", "execution_count": 18, - "id": "5fcfc5b8", + "id": "28f2e6af", "metadata": { "collapsed": false, "editable": true @@ -1802,7 +1936,7 @@ }, { "cell_type": "markdown", - "id": "0a7c1287", + "id": "483b3929", "metadata": { "editable": true }, @@ -1813,7 +1947,7 @@ { "cell_type": "code", "execution_count": 19, - "id": "10a630bd", + "id": "f8f3323b", "metadata": { "collapsed": false, "editable": true @@ -1833,7 +1967,7 @@ }, { "cell_type": "markdown", - "id": "6103d064", + "id": "7e0d4a52", "metadata": { "editable": true }, @@ -1844,7 +1978,7 @@ { "cell_type": "code", "execution_count": 20, - "id": "4bc70445", + "id": "f8fcb897", "metadata": { "collapsed": false, "editable": true @@ -1887,7 +2021,7 @@ { "cell_type": "code", "execution_count": 21, - "id": "01eb375e", + "id": "7ee8acf9", "metadata": { "collapsed": false, "editable": true @@ -1902,7 +2036,7 @@ }, { "cell_type": "markdown", - "id": "04f7579f", + "id": "58fce880", "metadata": { "editable": true }, @@ -1931,7 +2065,7 @@ }, { "cell_type": "markdown", - "id": "0a7959ed", + "id": "806ffde3", "metadata": { "editable": true }, @@ -1956,7 +2090,7 @@ }, { "cell_type": "markdown", - "id": "cd9c1690", + "id": "9747cabf", "metadata": { "editable": true }, @@ -1976,7 +2110,7 @@ }, { "cell_type": "markdown", - "id": "6d62d85d", + "id": "ce766c03", "metadata": { "editable": true }, @@ -2003,7 +2137,7 @@ }, { "cell_type": "markdown", - "id": "81920546", + "id": "5d002829", "metadata": { "editable": true }, @@ -2016,7 +2150,7 @@ }, { "cell_type": "markdown", - "id": "753795ef", + "id": "2cce1e61", "metadata": { "editable": true }, @@ -2028,7 +2162,7 @@ }, { "cell_type": "markdown", - "id": "b36fd3e7", + "id": "8be386f7", "metadata": { "editable": true }, @@ -2039,7 +2173,7 @@ }, { "cell_type": "markdown", - "id": "b194e075", + "id": "bd19f379", "metadata": { "editable": true }, @@ -2055,7 +2189,7 @@ { "cell_type": "code", "execution_count": 22, - "id": "c3cb5b99", + "id": "7e06104f", "metadata": { "collapsed": false, "editable": true @@ -2089,7 +2223,7 @@ }, { "cell_type": "markdown", - "id": "a892a73c", + "id": "0d903673", "metadata": { "editable": true }, @@ -2099,7 +2233,7 @@ }, { "cell_type": "markdown", - "id": "0a4a22f0", + "id": "f6aa52e2", "metadata": { "editable": true }, @@ -2114,7 +2248,7 @@ }, { "cell_type": "markdown", - "id": "40af24ef", + "id": "b768c5e4", "metadata": { "editable": true }, @@ -2126,7 +2260,7 @@ }, { "cell_type": "markdown", - "id": "aa726a54", + "id": "98bf7840", "metadata": { "editable": true }, @@ -2136,7 +2270,7 @@ }, { "cell_type": "markdown", - "id": "ca162e9d", + "id": "a81ac118", "metadata": { "editable": true }, @@ -2152,7 +2286,7 @@ { "cell_type": "code", "execution_count": 23, - "id": "0c11c7e9", + "id": "becefd95", "metadata": { "collapsed": false, "editable": true @@ -2169,7 +2303,7 @@ }, { "cell_type": "markdown", - "id": "c382eccf", + "id": "c3f39eaf", "metadata": { "editable": true }, @@ -2182,7 +2316,7 @@ { "cell_type": "code", "execution_count": 24, - "id": "778175b8", + "id": "aaf6e665", "metadata": { "collapsed": false, "editable": true @@ -2229,7 +2363,7 @@ }, { "cell_type": "markdown", - "id": "c4f7fee4", + "id": "7a0e2784", "metadata": { "editable": true }, @@ -2240,7 +2374,7 @@ { "cell_type": "code", "execution_count": 25, - "id": "cbc22809", + "id": "905a7403", "metadata": { "collapsed": false, "editable": true @@ -2343,7 +2477,17 @@ }, { "cell_type": "markdown", - "id": "f0321e9c", + "id": "438c50fc", + "metadata": { + "editable": true + }, + "source": [ + "## Material for lecture Thursday, August 31" + ] + }, + { + "cell_type": "markdown", + "id": "42dfb2e3", "metadata": { "editable": true }, @@ -2357,7 +2501,7 @@ }, { "cell_type": "markdown", - "id": "d65bce0d", + "id": "7cc86bb5", "metadata": { "editable": true }, @@ -2369,7 +2513,7 @@ }, { "cell_type": "markdown", - "id": "d13d39df", + "id": "83fc723c", "metadata": { "editable": true }, @@ -2381,7 +2525,7 @@ }, { "cell_type": "markdown", - "id": "e5cb8f6d", + "id": "406aa8dd", "metadata": { "editable": true }, @@ -2393,7 +2537,7 @@ }, { "cell_type": "markdown", - "id": "27a27374", + "id": "67f52d00", "metadata": { "editable": true }, @@ -2403,7 +2547,7 @@ }, { "cell_type": "markdown", - "id": "d02d9ef4", + "id": "af039735", "metadata": { "editable": true }, @@ -2415,7 +2559,7 @@ }, { "cell_type": "markdown", - "id": "6e55b218", + "id": "d5e5621a", "metadata": { "editable": true }, @@ -2425,7 +2569,7 @@ }, { "cell_type": "markdown", - "id": "2268316e", + "id": "d832b987", "metadata": { "editable": true }, @@ -2437,7 +2581,7 @@ }, { "cell_type": "markdown", - "id": "7b741977", + "id": "456c770b", "metadata": { "editable": true }, @@ -2448,7 +2592,7 @@ }, { "cell_type": "markdown", - "id": "03e3c565", + "id": "e78ee034", "metadata": { "editable": true }, @@ -2460,7 +2604,7 @@ }, { "cell_type": "markdown", - "id": "9e978fb5", + "id": "3074b255", "metadata": { "editable": true }, @@ -2472,7 +2616,7 @@ }, { "cell_type": "markdown", - "id": "46edd18a", + "id": "b9e5b517", "metadata": { "editable": true }, @@ -2482,7 +2626,7 @@ }, { "cell_type": "markdown", - "id": "a2e3cad1", + "id": "61d57ad2", "metadata": { "editable": true }, @@ -2494,7 +2638,7 @@ }, { "cell_type": "markdown", - "id": "84629785", + "id": "b0d206f9", "metadata": { "editable": true }, @@ -2506,7 +2650,7 @@ }, { "cell_type": "markdown", - "id": "cf58cf9b", + "id": "9689cbee", "metadata": { "editable": true }, @@ -2516,7 +2660,7 @@ }, { "cell_type": "markdown", - "id": "849ade3d", + "id": "2fbc8dab", "metadata": { "editable": true }, @@ -2528,7 +2672,7 @@ }, { "cell_type": "markdown", - "id": "e1cf8c55", + "id": "1e500c3f", "metadata": { "editable": true }, @@ -2538,7 +2682,7 @@ }, { "cell_type": "markdown", - "id": "01eb7d40", + "id": "cfa13551", "metadata": { "editable": true }, @@ -2550,7 +2694,7 @@ }, { "cell_type": "markdown", - "id": "b5d9bb3b", + "id": "1f1466c5", "metadata": { "editable": true }, @@ -2560,7 +2704,7 @@ }, { "cell_type": "markdown", - "id": "86405e35", + "id": "bf34f6fa", "metadata": { "editable": true }, @@ -2600,7 +2744,7 @@ }, { "cell_type": "markdown", - "id": "fa5bafad", + "id": "6ddcc022", "metadata": { "editable": true }, @@ -2617,7 +2761,7 @@ }, { "cell_type": "markdown", - "id": "6bfcd2a5", + "id": "0cf69ea3", "metadata": { "editable": true }, @@ -2640,7 +2784,7 @@ }, { "cell_type": "markdown", - "id": "e30ddc4b", + "id": "b4268613", "metadata": { "editable": true }, @@ -2657,7 +2801,7 @@ }, { "cell_type": "markdown", - "id": "ad1d8b4f", + "id": "9046cf24", "metadata": { "editable": true }, @@ -2676,7 +2820,7 @@ }, { "cell_type": "markdown", - "id": "27bd4c11", + "id": "4e91a577", "metadata": { "editable": true }, @@ -2687,7 +2831,7 @@ }, { "cell_type": "markdown", - "id": "8dba725c", + "id": "3a9f2cdc", "metadata": { "editable": true }, @@ -2699,7 +2843,7 @@ }, { "cell_type": "markdown", - "id": "64b8a3f4", + "id": "66788c79", "metadata": { "editable": true }, @@ -2717,7 +2861,7 @@ }, { "cell_type": "markdown", - "id": "4ff8d5b0", + "id": "2f1d2f0a", "metadata": { "editable": true }, @@ -2733,7 +2877,7 @@ }, { "cell_type": "markdown", - "id": "91b3436b", + "id": "27371195", "metadata": { "editable": true }, @@ -2745,7 +2889,7 @@ }, { "cell_type": "markdown", - "id": "253da7ee", + "id": "28ed8a1a", "metadata": { "editable": true }, @@ -2755,7 +2899,7 @@ }, { "cell_type": "markdown", - "id": "70928f7a", + "id": "55fdcad1", "metadata": { "editable": true }, @@ -2770,7 +2914,7 @@ }, { "cell_type": "markdown", - "id": "97f0afb7", + "id": "16f34838", "metadata": { "editable": true }, @@ -2782,7 +2926,7 @@ }, { "cell_type": "markdown", - "id": "46b4231e", + "id": "09503520", "metadata": { "editable": true }, @@ -2792,7 +2936,7 @@ }, { "cell_type": "markdown", - "id": "2abdcd28", + "id": "2d6682ee", "metadata": { "editable": true }, @@ -2804,7 +2948,7 @@ }, { "cell_type": "markdown", - "id": "0576a329", + "id": "5bd76fc1", "metadata": { "editable": true }, @@ -2814,7 +2958,7 @@ }, { "cell_type": "markdown", - "id": "e9e7ffb3", + "id": "3c7c8089", "metadata": { "editable": true }, @@ -2826,7 +2970,7 @@ }, { "cell_type": "markdown", - "id": "d3ac8e29", + "id": "726c5981", "metadata": { "editable": true }, @@ -2838,7 +2982,7 @@ }, { "cell_type": "markdown", - "id": "ae6e61f4", + "id": "2496c4e4", "metadata": { "editable": true }, @@ -2853,7 +2997,7 @@ }, { "cell_type": "markdown", - "id": "f98876c6", + "id": "7c47fc1b", "metadata": { "editable": true }, @@ -2864,7 +3008,7 @@ }, { "cell_type": "markdown", - "id": "92681eca", + "id": "21428876", "metadata": { "editable": true }, @@ -2884,7 +3028,7 @@ }, { "cell_type": "markdown", - "id": "9ce1fc75", + "id": "628da455", "metadata": { "editable": true }, @@ -2896,7 +3040,7 @@ }, { "cell_type": "markdown", - "id": "ae6d9419", + "id": "69e0b0d8", "metadata": { "editable": true }, @@ -2906,7 +3050,7 @@ }, { "cell_type": "markdown", - "id": "2dc240de", + "id": "e684f1d4", "metadata": { "editable": true }, @@ -2918,7 +3062,7 @@ }, { "cell_type": "markdown", - "id": "2fefe489", + "id": "297a174d", "metadata": { "editable": true }, @@ -2947,7 +3091,7 @@ }, { "cell_type": "markdown", - "id": "2f460a6a", + "id": "783a40f5", "metadata": { "editable": true }, @@ -2974,7 +3118,7 @@ }, { "cell_type": "markdown", - "id": "0d9eff11", + "id": "46b81f95", "metadata": { "editable": true }, @@ -2985,7 +3129,7 @@ { "cell_type": "code", "execution_count": 26, - "id": "9ee0046f", + "id": "cf341757", "metadata": { "collapsed": false, "editable": true @@ -3025,7 +3169,7 @@ }, { "cell_type": "markdown", - "id": "e3587abc", + "id": "bf70bf90", "metadata": { "editable": true }, @@ -3042,7 +3186,7 @@ }, { "cell_type": "markdown", - "id": "fa546864", + "id": "a73c1f8d", "metadata": { "editable": true }, @@ -3065,7 +3209,7 @@ }, { "cell_type": "markdown", - "id": "0a938e5a", + "id": "10bf5ca0", "metadata": { "editable": true }, @@ -3079,7 +3223,7 @@ }, { "cell_type": "markdown", - "id": "1a9c241c", + "id": "85061039", "metadata": { "editable": true }, @@ -3098,7 +3242,7 @@ }, { "cell_type": "markdown", - "id": "d1ab96a3", + "id": "c910b923", "metadata": { "editable": true }, @@ -3108,7 +3252,7 @@ }, { "cell_type": "markdown", - "id": "5ac678ff", + "id": "f876a6fa", "metadata": { "editable": true }, @@ -3120,7 +3264,7 @@ }, { "cell_type": "markdown", - "id": "390428ed", + "id": "ea5895c8", "metadata": { "editable": true }, @@ -3134,7 +3278,7 @@ }, { "cell_type": "markdown", - "id": "9601cb68", + "id": "3be8ed04", "metadata": { "editable": true }, @@ -3146,7 +3290,7 @@ }, { "cell_type": "markdown", - "id": "92e331b7", + "id": "e95c8816", "metadata": { "editable": true }, @@ -3156,7 +3300,7 @@ }, { "cell_type": "markdown", - "id": "6bb442d7", + "id": "88468938", "metadata": { "editable": true }, @@ -3168,7 +3312,7 @@ }, { "cell_type": "markdown", - "id": "23482dff", + "id": "9a19b640", "metadata": { "editable": true }, @@ -3185,7 +3329,7 @@ }, { "cell_type": "markdown", - "id": "1685cc78", + "id": "59473174", "metadata": { "editable": true }, @@ -3195,7 +3339,7 @@ }, { "cell_type": "markdown", - "id": "abc63aaf", + "id": "90bbb36f", "metadata": { "editable": true }, @@ -3211,7 +3355,7 @@ }, { "cell_type": "markdown", - "id": "45b7acfb", + "id": "5cc4cd65", "metadata": { "editable": true }, @@ -3221,7 +3365,7 @@ }, { "cell_type": "markdown", - "id": "347d83bb", + "id": "e9913e93", "metadata": { "editable": true }, @@ -3237,7 +3381,7 @@ }, { "cell_type": "markdown", - "id": "3d97920a", + "id": "d4d860b2", "metadata": { "editable": true }, @@ -3247,7 +3391,7 @@ }, { "cell_type": "markdown", - "id": "51e0f3e5", + "id": "0ea33681", "metadata": { "editable": true }, @@ -3263,7 +3407,7 @@ }, { "cell_type": "markdown", - "id": "7f199a02", + "id": "d1229bd7", "metadata": { "editable": true }, @@ -3273,7 +3417,7 @@ }, { "cell_type": "markdown", - "id": "95d57fd8", + "id": "39c6e080", "metadata": { "editable": true }, @@ -3290,7 +3434,7 @@ }, { "cell_type": "markdown", - "id": "c7511e38", + "id": "4b6dc63f", "metadata": { "editable": true }, @@ -3302,7 +3446,7 @@ }, { "cell_type": "markdown", - "id": "28f97ff5", + "id": "5085d614", "metadata": { "editable": true }, @@ -3314,7 +3458,7 @@ }, { "cell_type": "markdown", - "id": "8a561632", + "id": "e1489aee", "metadata": { "editable": true }, @@ -3326,7 +3470,7 @@ }, { "cell_type": "markdown", - "id": "573f4fc0", + "id": "a779fe81", "metadata": { "editable": true }, @@ -3336,7 +3480,7 @@ }, { "cell_type": "markdown", - "id": "ed12ce0e", + "id": "2e1a8999", "metadata": { "editable": true }, @@ -3348,7 +3492,7 @@ }, { "cell_type": "markdown", - "id": "e3bf50b5", + "id": "e9afe762", "metadata": { "editable": true }, @@ -3360,7 +3504,7 @@ }, { "cell_type": "markdown", - "id": "ca7153f5", + "id": "a53312df", "metadata": { "editable": true }, @@ -3372,7 +3516,7 @@ }, { "cell_type": "markdown", - "id": "5ae9c4f5", + "id": "4fcf6b60", "metadata": { "editable": true }, @@ -3382,7 +3526,7 @@ }, { "cell_type": "markdown", - "id": "cb73b253", + "id": "c0f1802d", "metadata": { "editable": true }, @@ -3394,7 +3538,7 @@ }, { "cell_type": "markdown", - "id": "cca76fe6", + "id": "2f3d5b6b", "metadata": { "editable": true }, @@ -3404,7 +3548,7 @@ }, { "cell_type": "markdown", - "id": "336f280f", + "id": "803d0009", "metadata": { "editable": true }, @@ -3416,7 +3560,7 @@ }, { "cell_type": "markdown", - "id": "b89a5de7", + "id": "ae47bb5f", "metadata": { "editable": true }, @@ -3430,7 +3574,7 @@ }, { "cell_type": "markdown", - "id": "83503e2c", + "id": "6bb81684", "metadata": { "editable": true }, @@ -3442,7 +3586,7 @@ }, { "cell_type": "markdown", - "id": "5cdf434d", + "id": "d6a1a850", "metadata": { "editable": true }, @@ -3454,7 +3598,7 @@ }, { "cell_type": "markdown", - "id": "2908b80b", + "id": "2c7dd08f", "metadata": { "editable": true }, @@ -3464,7 +3608,7 @@ }, { "cell_type": "markdown", - "id": "a0b848f6", + "id": "41bb0c81", "metadata": { "editable": true }, @@ -3476,7 +3620,7 @@ }, { "cell_type": "markdown", - "id": "dc067aee", + "id": "494c8770", "metadata": { "editable": true }, @@ -3487,7 +3631,7 @@ }, { "cell_type": "markdown", - "id": "8a5a12ec", + "id": "dea5fb5a", "metadata": { "editable": true }, @@ -3499,7 +3643,7 @@ }, { "cell_type": "markdown", - "id": "fc7e60c2", + "id": "dcbb67ae", "metadata": { "editable": true }, @@ -3509,7 +3653,7 @@ }, { "cell_type": "markdown", - "id": "18ca4292", + "id": "bdd7866e", "metadata": { "editable": true }, @@ -3521,7 +3665,7 @@ }, { "cell_type": "markdown", - "id": "6ce14044", + "id": "bc75c051", "metadata": { "editable": true }, @@ -3531,7 +3675,7 @@ }, { "cell_type": "markdown", - "id": "dda07b92", + "id": "019a2c76", "metadata": { "editable": true }, @@ -3543,7 +3687,7 @@ }, { "cell_type": "markdown", - "id": "a7ca2443", + "id": "1dfde562", "metadata": { "editable": true }, @@ -3554,7 +3698,7 @@ }, { "cell_type": "markdown", - "id": "771ca273", + "id": "b2bc4f99", "metadata": { "editable": true }, @@ -3566,7 +3710,7 @@ }, { "cell_type": "markdown", - "id": "72a206f3", + "id": "5936aa08", "metadata": { "editable": true }, @@ -3584,7 +3728,7 @@ }, { "cell_type": "markdown", - "id": "74877158", + "id": "ca751aae", "metadata": { "editable": true }, @@ -3600,7 +3744,7 @@ }, { "cell_type": "markdown", - "id": "b7a46ddc", + "id": "36d16e4f", "metadata": { "editable": true }, @@ -3612,7 +3756,7 @@ }, { "cell_type": "markdown", - "id": "b1ca2895", + "id": "02c5b006", "metadata": { "editable": true }, @@ -3624,7 +3768,7 @@ }, { "cell_type": "markdown", - "id": "20def6e9", + "id": "34d19a5d", "metadata": { "editable": true }, @@ -3636,7 +3780,7 @@ }, { "cell_type": "markdown", - "id": "0fde8018", + "id": "f3aef9cc", "metadata": { "editable": true }, @@ -3649,7 +3793,7 @@ }, { "cell_type": "markdown", - "id": "f80e69e5", + "id": "104ce871", "metadata": { "editable": true }, @@ -3665,7 +3809,7 @@ }, { "cell_type": "markdown", - "id": "6621f0bf", + "id": "e615d613", "metadata": { "editable": true }, @@ -3679,7 +3823,7 @@ }, { "cell_type": "markdown", - "id": "ad30d270", + "id": "89fac8ef", "metadata": { "editable": true }, @@ -3689,7 +3833,7 @@ }, { "cell_type": "markdown", - "id": "69dbd166", + "id": "a3605ede", "metadata": { "editable": true }, @@ -3701,7 +3845,7 @@ }, { "cell_type": "markdown", - "id": "3cf4b565", + "id": "e11fe9c0", "metadata": { "editable": true }, @@ -3711,7 +3855,7 @@ }, { "cell_type": "markdown", - "id": "46e02ce4", + "id": "97304c79", "metadata": { "editable": true }, @@ -3723,7 +3867,7 @@ }, { "cell_type": "markdown", - "id": "8206d5bf", + "id": "b75e6d3f", "metadata": { "editable": true }, @@ -3733,7 +3877,7 @@ }, { "cell_type": "markdown", - "id": "2331cfae", + "id": "c1ad74b4", "metadata": { "editable": true }, @@ -3747,7 +3891,7 @@ }, { "cell_type": "markdown", - "id": "53668a01", + "id": "18685460", "metadata": { "editable": true }, @@ -3764,7 +3908,7 @@ }, { "cell_type": "markdown", - "id": "b2ea9cb4", + "id": "5c5e287e", "metadata": { "editable": true }, @@ -3780,7 +3924,7 @@ }, { "cell_type": "markdown", - "id": "eaed108b", + "id": "d3fdf3fb", "metadata": { "editable": true }, @@ -3792,7 +3936,7 @@ }, { "cell_type": "markdown", - "id": "ac70d3d9", + "id": "b0bdec8a", "metadata": { "editable": true }, @@ -3805,7 +3949,7 @@ }, { "cell_type": "markdown", - "id": "9e21ecc3", + "id": "d00514ea", "metadata": { "editable": true }, @@ -3819,7 +3963,7 @@ }, { "cell_type": "markdown", - "id": "be47dd9c", + "id": "a9ece68d", "metadata": { "editable": true }, @@ -3829,7 +3973,7 @@ }, { "cell_type": "markdown", - "id": "5940b256", + "id": "dc69f38f", "metadata": { "editable": true }, @@ -3842,7 +3986,7 @@ }, { "cell_type": "markdown", - "id": "e9e5256a", + "id": "f0688117", "metadata": { "editable": true }, @@ -3861,7 +4005,7 @@ }, { "cell_type": "markdown", - "id": "1de69f31", + "id": "c4bd6a07", "metadata": { "editable": true }, @@ -3873,7 +4017,7 @@ }, { "cell_type": "markdown", - "id": "40a69fbf", + "id": "fbc22dbe", "metadata": { "editable": true }, @@ -3885,7 +4029,7 @@ }, { "cell_type": "markdown", - "id": "5a479c8c", + "id": "2e897fdf", "metadata": { "editable": true }, @@ -3895,7 +4039,7 @@ }, { "cell_type": "markdown", - "id": "861c81fc", + "id": "edfa5568", "metadata": { "editable": true }, @@ -3907,7 +4051,7 @@ }, { "cell_type": "markdown", - "id": "57a2647c", + "id": "724c9ccc", "metadata": { "editable": true }, @@ -3920,7 +4064,7 @@ }, { "cell_type": "markdown", - "id": "19b6b938", + "id": "eef4be52", "metadata": { "editable": true }, @@ -3939,7 +4083,7 @@ }, { "cell_type": "markdown", - "id": "e06ed882", + "id": "ac767a45", "metadata": { "editable": true }, @@ -3949,7 +4093,7 @@ }, { "cell_type": "markdown", - "id": "8c5e166f", + "id": "f5f420c2", "metadata": { "editable": true }, @@ -3968,7 +4112,7 @@ }, { "cell_type": "markdown", - "id": "cd1a4307", + "id": "c9c4959d", "metadata": { "editable": true }, @@ -3986,7 +4130,7 @@ }, { "cell_type": "markdown", - "id": "4740ada9", + "id": "eafd6e6d", "metadata": { "editable": true }, @@ -4000,7 +4144,7 @@ }, { "cell_type": "markdown", - "id": "1bcf90ec", + "id": "42f20e92", "metadata": { "editable": true }, @@ -4015,7 +4159,7 @@ { "cell_type": "code", "execution_count": 27, - "id": "462acd11", + "id": "f813e53e", "metadata": { "collapsed": false, "editable": true @@ -4036,7 +4180,7 @@ }, { "cell_type": "markdown", - "id": "b37abd40", + "id": "247471e4", "metadata": { "editable": true }, @@ -4053,7 +4197,7 @@ { "cell_type": "code", "execution_count": 28, - "id": "215f0589", + "id": "d2c36e83", "metadata": { "collapsed": false, "editable": true @@ -4085,7 +4229,7 @@ }, { "cell_type": "markdown", - "id": "f1392726", + "id": "41002463", "metadata": { "editable": true }, @@ -4099,7 +4243,7 @@ }, { "cell_type": "markdown", - "id": "fce259fc", + "id": "56c99f24", "metadata": { "editable": true }, @@ -4112,7 +4256,7 @@ { "cell_type": "code", "execution_count": 29, - "id": "e99acc19", + "id": "38ad5db6", "metadata": { "collapsed": false, "editable": true @@ -4137,7 +4281,7 @@ }, { "cell_type": "markdown", - "id": "80d6673a", + "id": "3121879d", "metadata": { "editable": true }, @@ -4147,7 +4291,7 @@ }, { "cell_type": "markdown", - "id": "b394be66", + "id": "a69a358d", "metadata": { "editable": true }, @@ -4158,7 +4302,7 @@ { "cell_type": "code", "execution_count": 30, - "id": "c3d6cd45", + "id": "148e9543", "metadata": { "collapsed": false, "editable": true @@ -4212,7 +4356,7 @@ }, { "cell_type": "markdown", - "id": "537f7e6a", + "id": "17aff9a1", "metadata": { "editable": true }, @@ -4229,7 +4373,7 @@ }, { "cell_type": "markdown", - "id": "94833946", + "id": "35e1797a", "metadata": { "editable": true }, @@ -4241,7 +4385,7 @@ }, { "cell_type": "markdown", - "id": "dc4d6495", + "id": "252fca16", "metadata": { "editable": true }, @@ -4253,7 +4397,7 @@ }, { "cell_type": "markdown", - "id": "493f2214", + "id": "8732dca9", "metadata": { "editable": true }, @@ -4263,7 +4407,7 @@ }, { "cell_type": "markdown", - "id": "3453050f", + "id": "5d4d1274", "metadata": { "editable": true }, @@ -4280,7 +4424,7 @@ }, { "cell_type": "markdown", - "id": "39a1d59c", + "id": "ee873312", "metadata": { "editable": true }, @@ -4290,7 +4434,7 @@ }, { "cell_type": "markdown", - "id": "209e80b5", + "id": "2497d2d4", "metadata": { "editable": true }, @@ -4305,7 +4449,7 @@ }, { "cell_type": "markdown", - "id": "2433ad8d", + "id": "c66343ac", "metadata": { "editable": true }, @@ -4315,7 +4459,7 @@ }, { "cell_type": "markdown", - "id": "da3d0578", + "id": "286af61f", "metadata": { "editable": true }, @@ -4329,7 +4473,7 @@ }, { "cell_type": "markdown", - "id": "58ca9f28", + "id": "1c409c5f", "metadata": { "editable": true }, @@ -4341,7 +4485,7 @@ }, { "cell_type": "markdown", - "id": "92e73bef", + "id": "61165d5d", "metadata": { "editable": true }, @@ -4353,7 +4497,7 @@ }, { "cell_type": "markdown", - "id": "d6b6c91f", + "id": "fe288d2c", "metadata": { "editable": true }, @@ -4365,7 +4509,7 @@ }, { "cell_type": "markdown", - "id": "42d2e959", + "id": "5410d061", "metadata": { "editable": true }, @@ -4375,7 +4519,7 @@ }, { "cell_type": "markdown", - "id": "d87859e1", + "id": "b546868b", "metadata": { "editable": true }, @@ -4387,7 +4531,7 @@ }, { "cell_type": "markdown", - "id": "a72bcdcc", + "id": "6eb95db8", "metadata": { "editable": true }, @@ -4397,7 +4541,7 @@ }, { "cell_type": "markdown", - "id": "5f384a3d", + "id": "bd9a4a9d", "metadata": { "editable": true }, @@ -4414,7 +4558,7 @@ }, { "cell_type": "markdown", - "id": "d458de79", + "id": "266910cb", "metadata": { "editable": true }, @@ -4424,7 +4568,7 @@ }, { "cell_type": "markdown", - "id": "e41c09ab", + "id": "5f200279", "metadata": { "editable": true }, @@ -4436,7 +4580,7 @@ }, { "cell_type": "markdown", - "id": "9ede1df9", + "id": "60af6314", "metadata": { "editable": true }, @@ -4446,7 +4590,7 @@ }, { "cell_type": "markdown", - "id": "a3e0134c", + "id": "b96e6fdc", "metadata": { "editable": true }, @@ -4458,7 +4602,7 @@ }, { "cell_type": "markdown", - "id": "e431a675", + "id": "e1b333c3", "metadata": { "editable": true }, @@ -4472,7 +4616,7 @@ }, { "cell_type": "markdown", - "id": "e86e9e8c", + "id": "0542f076", "metadata": { "editable": true }, @@ -4484,7 +4628,7 @@ }, { "cell_type": "markdown", - "id": "e2f1477c", + "id": "6fa8ee09", "metadata": { "editable": true }, @@ -4506,7 +4650,7 @@ }, { "cell_type": "markdown", - "id": "0218527f", + "id": "946c5371", "metadata": { "editable": true }, @@ -4518,7 +4662,7 @@ }, { "cell_type": "markdown", - "id": "3f4c8b90", + "id": "5389bab4", "metadata": { "editable": true }, @@ -4533,7 +4677,7 @@ }, { "cell_type": "markdown", - "id": "68074063", + "id": "d18a088f", "metadata": { "editable": true }, @@ -4545,7 +4689,7 @@ }, { "cell_type": "markdown", - "id": "22d5574d", + "id": "1b010eb0", "metadata": { "editable": true }, @@ -4557,7 +4701,7 @@ }, { "cell_type": "markdown", - "id": "5185fd92", + "id": "a99445fa", "metadata": { "editable": true }, @@ -4567,7 +4711,7 @@ }, { "cell_type": "markdown", - "id": "6e7d1d63", + "id": "642cc835", "metadata": { "editable": true }, @@ -4579,7 +4723,7 @@ }, { "cell_type": "markdown", - "id": "c1d495ed", + "id": "1247e26a", "metadata": { "editable": true }, @@ -4589,7 +4733,7 @@ }, { "cell_type": "markdown", - "id": "a872d3b0", + "id": "f0e57efa", "metadata": { "editable": true }, @@ -4601,7 +4745,7 @@ }, { "cell_type": "markdown", - "id": "437cbfdd", + "id": "b2d9771d", "metadata": { "editable": true }, @@ -4611,7 +4755,7 @@ }, { "cell_type": "markdown", - "id": "e67b7cc5", + "id": "467c11e6", "metadata": { "editable": true }, @@ -4623,7 +4767,7 @@ }, { "cell_type": "markdown", - "id": "0ce8538f", + "id": "e48b6a92", "metadata": { "editable": true }, @@ -4640,7 +4784,7 @@ }, { "cell_type": "markdown", - "id": "0c4027f5", + "id": "05b57e15", "metadata": { "editable": true }, @@ -4653,7 +4797,7 @@ }, { "cell_type": "markdown", - "id": "c7803c46", + "id": "2f8aeb1c", "metadata": { "editable": true }, @@ -4665,7 +4809,7 @@ }, { "cell_type": "markdown", - "id": "afceeea1", + "id": "70af1652", "metadata": { "editable": true }, @@ -4675,7 +4819,7 @@ }, { "cell_type": "markdown", - "id": "4fca65ab", + "id": "75c32ff6", "metadata": { "editable": true }, @@ -4688,7 +4832,7 @@ }, { "cell_type": "markdown", - "id": "0540fdc8", + "id": "73e17b6e", "metadata": { "editable": true }, @@ -4698,7 +4842,7 @@ }, { "cell_type": "markdown", - "id": "25768745", + "id": "2be67f9c", "metadata": { "editable": true }, @@ -4710,7 +4854,7 @@ }, { "cell_type": "markdown", - "id": "aea56948", + "id": "0534d345", "metadata": { "editable": true }, @@ -4723,7 +4867,7 @@ }, { "cell_type": "markdown", - "id": "3071cfb4", + "id": "8b76b1a3", "metadata": { "editable": true }, @@ -4736,7 +4880,7 @@ }, { "cell_type": "markdown", - "id": "4150de51", + "id": "a47dc6ec", "metadata": { "editable": true }, @@ -4748,7 +4892,7 @@ }, { "cell_type": "markdown", - "id": "c505e150", + "id": "34552a19", "metadata": { "editable": true }, @@ -4760,7 +4904,7 @@ }, { "cell_type": "markdown", - "id": "efbc248a", + "id": "d5d0b8e0", "metadata": { "editable": true }, @@ -4770,7 +4914,7 @@ }, { "cell_type": "markdown", - "id": "4779d5bc", + "id": "2d8d93b2", "metadata": { "editable": true }, @@ -4783,7 +4927,7 @@ }, { "cell_type": "markdown", - "id": "0ed7e75d", + "id": "6adbf0a8", "metadata": { "editable": true }, @@ -4795,7 +4939,7 @@ }, { "cell_type": "markdown", - "id": "59def60c", + "id": "03e7f601", "metadata": { "editable": true }, @@ -4807,7 +4951,7 @@ }, { "cell_type": "markdown", - "id": "cb235f25", + "id": "fef5b35c", "metadata": { "editable": true }, @@ -4819,7 +4963,7 @@ }, { "cell_type": "markdown", - "id": "107d5e35", + "id": "af511d88", "metadata": { "editable": true }, @@ -4831,7 +4975,7 @@ }, { "cell_type": "markdown", - "id": "f9bbe5a8", + "id": "c478e5b3", "metadata": { "editable": true }, @@ -4845,7 +4989,7 @@ }, { "cell_type": "markdown", - "id": "849520a2", + "id": "197d450d", "metadata": { "editable": true }, @@ -4857,7 +5001,7 @@ }, { "cell_type": "markdown", - "id": "78d6b534", + "id": "3ac39a87", "metadata": { "editable": true }, @@ -4867,7 +5011,7 @@ }, { "cell_type": "markdown", - "id": "c44cec21", + "id": "e06e9fa7", "metadata": { "editable": true }, @@ -4879,7 +5023,7 @@ }, { "cell_type": "markdown", - "id": "ec1ab437", + "id": "cf03f73d", "metadata": { "editable": true }, @@ -4891,7 +5035,7 @@ }, { "cell_type": "markdown", - "id": "9d08739b", + "id": "4a924f31", "metadata": { "editable": true }, @@ -4903,7 +5047,7 @@ }, { "cell_type": "markdown", - "id": "ae7a403c", + "id": "540f1997", "metadata": { "editable": true }, @@ -4915,7 +5059,7 @@ }, { "cell_type": "markdown", - "id": "bc67a9bc", + "id": "cdbc9b89", "metadata": { "editable": true }, @@ -4927,7 +5071,7 @@ }, { "cell_type": "markdown", - "id": "d3ba63ad", + "id": "452330a8", "metadata": { "editable": true }, @@ -4946,7 +5090,7 @@ }, { "cell_type": "markdown", - "id": "15a1252e", + "id": "b6de3477", "metadata": { "editable": true }, @@ -4958,7 +5102,7 @@ }, { "cell_type": "markdown", - "id": "f1a92344", + "id": "03f38cb5", "metadata": { "editable": true }, @@ -4968,7 +5112,7 @@ }, { "cell_type": "markdown", - "id": "0c5a0a87", + "id": "508134a4", "metadata": { "editable": true }, @@ -4980,7 +5124,7 @@ }, { "cell_type": "markdown", - "id": "4bd6b3e5", + "id": "f1a44572", "metadata": { "editable": true }, @@ -4990,7 +5134,7 @@ }, { "cell_type": "markdown", - "id": "01538be8", + "id": "aa80fa4c", "metadata": { "editable": true }, @@ -5002,7 +5146,7 @@ }, { "cell_type": "markdown", - "id": "50ef9bff", + "id": "71fcdc4a", "metadata": { "editable": true }, @@ -5014,7 +5158,7 @@ }, { "cell_type": "markdown", - "id": "589198c8", + "id": "8e856380", "metadata": { "editable": true }, @@ -5030,7 +5174,7 @@ }, { "cell_type": "markdown", - "id": "16932533", + "id": "92df5419", "metadata": { "editable": true }, @@ -5042,7 +5186,7 @@ }, { "cell_type": "markdown", - "id": "833f7e1d", + "id": "5fbe0881", "metadata": { "editable": true }, @@ -5054,7 +5198,7 @@ }, { "cell_type": "markdown", - "id": "684d7ac5", + "id": "dd029d2e", "metadata": { "editable": true }, @@ -5064,7 +5208,7 @@ }, { "cell_type": "markdown", - "id": "5994e3f2", + "id": "75713f0f", "metadata": { "editable": true }, @@ -5076,7 +5220,7 @@ }, { "cell_type": "markdown", - "id": "7dfee8bb", + "id": "90347f92", "metadata": { "editable": true }, @@ -5086,7 +5230,7 @@ }, { "cell_type": "markdown", - "id": "00a53629", + "id": "552ee48d", "metadata": { "editable": true }, @@ -5098,7 +5242,7 @@ }, { "cell_type": "markdown", - "id": "883e9eff", + "id": "7aa9e96d", "metadata": { "editable": true }, @@ -5115,7 +5259,7 @@ }, { "cell_type": "markdown", - "id": "02479371", + "id": "e5a9aca9", "metadata": { "editable": true }, @@ -5127,7 +5271,7 @@ }, { "cell_type": "markdown", - "id": "ef3332c7", + "id": "ce3bfe09", "metadata": { "editable": true }, @@ -5139,7 +5283,7 @@ }, { "cell_type": "markdown", - "id": "e246907e", + "id": "36158aa7", "metadata": { "editable": true }, @@ -5149,7 +5293,7 @@ }, { "cell_type": "markdown", - "id": "0f62d890", + "id": "189d07fb", "metadata": { "editable": true }, @@ -5161,7 +5305,7 @@ }, { "cell_type": "markdown", - "id": "7bc218bf", + "id": "f3e72532", "metadata": { "editable": true }, @@ -5171,7 +5315,7 @@ }, { "cell_type": "markdown", - "id": "7a30ea44", + "id": "624fc00e", "metadata": { "editable": true }, @@ -5183,7 +5327,7 @@ }, { "cell_type": "markdown", - "id": "e2cadc4f", + "id": "aa5063d5", "metadata": { "editable": true }, @@ -5193,7 +5337,7 @@ }, { "cell_type": "markdown", - "id": "c72ab31d", + "id": "42a566f3", "metadata": { "editable": true }, @@ -5205,7 +5349,7 @@ }, { "cell_type": "markdown", - "id": "c43f2887", + "id": "5d3fed6e", "metadata": { "editable": true }, @@ -5215,7 +5359,7 @@ }, { "cell_type": "markdown", - "id": "96f0205f", + "id": "05e91ccb", "metadata": { "editable": true }, @@ -5227,7 +5371,7 @@ }, { "cell_type": "markdown", - "id": "6c7c2b42", + "id": "b059054a", "metadata": { "editable": true }, @@ -5296,7 +5440,7 @@ }, { "cell_type": "markdown", - "id": "cc7944cc", + "id": "0bf6e778", "metadata": { "editable": true }, @@ -5310,7 +5454,7 @@ { "cell_type": "code", "execution_count": 31, - "id": "b5d985c5", + "id": "3ad4858b", "metadata": { "collapsed": false, "editable": true @@ -5323,7 +5467,7 @@ }, { "cell_type": "markdown", - "id": "996a90c0", + "id": "102dd2be", "metadata": { "editable": true }, @@ -5337,7 +5481,7 @@ }, { "cell_type": "markdown", - "id": "7568ca05", + "id": "8b460f71", "metadata": { "editable": true }, @@ -5350,7 +5494,7 @@ }, { "cell_type": "markdown", - "id": "1283b5aa", + "id": "c3217f43", "metadata": { "editable": true }, @@ -5361,7 +5505,7 @@ }, { "cell_type": "markdown", - "id": "08084f21", + "id": "8ee662e2", "metadata": { "editable": true }, @@ -5373,7 +5517,7 @@ }, { "cell_type": "markdown", - "id": "57e59a3b", + "id": "43bba74a", "metadata": { "editable": true }, @@ -5383,7 +5527,7 @@ }, { "cell_type": "markdown", - "id": "6ea0d1ec", + "id": "e5d8e8ff", "metadata": { "editable": true }, @@ -5395,7 +5539,7 @@ }, { "cell_type": "markdown", - "id": "5f930aaf", + "id": "a37a653d", "metadata": { "editable": true }, @@ -5411,7 +5555,7 @@ { "cell_type": "code", "execution_count": 32, - "id": "e3463102", + "id": "d616531d", "metadata": { "collapsed": false, "editable": true @@ -5462,7 +5606,7 @@ }, { "cell_type": "markdown", - "id": "b6b299ef", + "id": "6e0b8e6f", "metadata": { "editable": true }, @@ -5472,7 +5616,7 @@ }, { "cell_type": "markdown", - "id": "2b91f33f", + "id": "4d63369e", "metadata": { "editable": true }, @@ -5518,7 +5662,7 @@ { "cell_type": "code", "execution_count": 33, - "id": "dd8df18f", + "id": "66c8c5a1", "metadata": { "collapsed": false, "editable": true @@ -5531,7 +5675,7 @@ }, { "cell_type": "markdown", - "id": "0c8e7d36", + "id": "59cb5a01", "metadata": { "editable": true }, @@ -5542,7 +5686,7 @@ { "cell_type": "code", "execution_count": 34, - "id": "67122ec3", + "id": "710e63a2", "metadata": { "collapsed": false, "editable": true @@ -5557,7 +5701,7 @@ }, { "cell_type": "markdown", - "id": "64380e4c", + "id": "b636233f", "metadata": { "editable": true }, @@ -5575,7 +5719,7 @@ { "cell_type": "code", "execution_count": 35, - "id": "74b66c47", + "id": "678e5e7c", "metadata": { "collapsed": false, "editable": true @@ -5592,7 +5736,7 @@ }, { "cell_type": "markdown", - "id": "7f9c1d69", + "id": "1fbf5ee7", "metadata": { "editable": true }, @@ -5602,7 +5746,7 @@ }, { "cell_type": "markdown", - "id": "419e926c", + "id": "d7a2439b", "metadata": { "editable": true }, @@ -5613,7 +5757,7 @@ }, { "cell_type": "markdown", - "id": "11577b0a", + "id": "06894ac1", "metadata": { "editable": true }, @@ -5624,7 +5768,7 @@ }, { "cell_type": "markdown", - "id": "d9a8c4be", + "id": "a94958ed", "metadata": { "editable": true }, @@ -5635,7 +5779,7 @@ }, { "cell_type": "markdown", - "id": "8a926f34", + "id": "215d93cd", "metadata": { "editable": true }, @@ -5658,7 +5802,7 @@ { "cell_type": "code", "execution_count": 36, - "id": "409f0f47", + "id": "eed0bc0f", "metadata": { "collapsed": false, "editable": true @@ -5671,7 +5815,7 @@ }, { "cell_type": "markdown", - "id": "7df5b6b3", + "id": "8e9867a7", "metadata": { "editable": true }, @@ -5686,7 +5830,7 @@ { "cell_type": "code", "execution_count": 37, - "id": "1f2230c3", + "id": "8d5faee2", "metadata": { "collapsed": false, "editable": true @@ -5766,7 +5910,7 @@ }, { "cell_type": "markdown", - "id": "747d5fad", + "id": "45c10c0d", "metadata": { "editable": true }, @@ -5781,7 +5925,7 @@ }, { "cell_type": "markdown", - "id": "b345ce8a", + "id": "17886be0", "metadata": { "editable": true }, @@ -5794,7 +5938,7 @@ }, { "cell_type": "markdown", - "id": "6a2618a4", + "id": "389b83e7", "metadata": { "editable": true }, @@ -5805,7 +5949,7 @@ }, { "cell_type": "markdown", - "id": "85b98e88", + "id": "5dd35023", "metadata": { "editable": true }, @@ -5817,7 +5961,7 @@ }, { "cell_type": "markdown", - "id": "55095fad", + "id": "626950ab", "metadata": { "editable": true }, @@ -5827,7 +5971,7 @@ }, { "cell_type": "markdown", - "id": "79f10e17", + "id": "9309f270", "metadata": { "editable": true }, @@ -5839,7 +5983,7 @@ }, { "cell_type": "markdown", - "id": "3f5473a9", + "id": "730a3ff7", "metadata": { "editable": true }, @@ -5849,7 +5993,7 @@ }, { "cell_type": "markdown", - "id": "a8592054", + "id": "c9b656cc", "metadata": { "editable": true }, @@ -5871,7 +6015,7 @@ }, { "cell_type": "markdown", - "id": "1b01d487", + "id": "3055d266", "metadata": { "editable": true }, @@ -5883,7 +6027,7 @@ }, { "cell_type": "markdown", - "id": "db2b2d2e", + "id": "4b7e0dfa", "metadata": { "editable": true }, @@ -5893,7 +6037,7 @@ }, { "cell_type": "markdown", - "id": "82e46c7e", + "id": "9136efc2", "metadata": { "editable": true }, @@ -5905,7 +6049,7 @@ }, { "cell_type": "markdown", - "id": "d2ffccbd", + "id": "24431772", "metadata": { "editable": true }, @@ -5915,7 +6059,7 @@ }, { "cell_type": "markdown", - "id": "ac17eb1f", + "id": "c3f86209", "metadata": { "editable": true }, @@ -5927,7 +6071,7 @@ }, { "cell_type": "markdown", - "id": "28b9b54f", + "id": "fa68bb58", "metadata": { "editable": true }, @@ -5940,7 +6084,7 @@ }, { "cell_type": "markdown", - "id": "23edea6d", + "id": "9f0d1009", "metadata": { "editable": true }, @@ -5952,7 +6096,7 @@ }, { "cell_type": "markdown", - "id": "5391b04f", + "id": "be855e2a", "metadata": { "editable": true }, @@ -5962,7 +6106,7 @@ }, { "cell_type": "markdown", - "id": "e7c1f6ed", + "id": "85d61066", "metadata": { "editable": true }, @@ -5974,7 +6118,7 @@ }, { "cell_type": "markdown", - "id": "8acf268e", + "id": "0d4df9cf", "metadata": { "editable": true }, @@ -5984,7 +6128,7 @@ }, { "cell_type": "markdown", - "id": "6e1d47cd", + "id": "bf6d12a1", "metadata": { "editable": true }, @@ -5996,7 +6140,7 @@ }, { "cell_type": "markdown", - "id": "5abbb76e", + "id": "58782b2d", "metadata": { "editable": true }, @@ -6006,7 +6150,7 @@ }, { "cell_type": "markdown", - "id": "2afbee91", + "id": "52d08547", "metadata": { "editable": true }, diff --git a/doc/src/week35/week35.do.txt b/doc/src/week35/week35.do.txt index dba20299e..a95033659 100644 --- a/doc/src/week35/week35.do.txt +++ b/doc/src/week35/week35.do.txt @@ -539,7 +539,32 @@ $1\times n$ matrix. !split ===== Derivatives, example 1 ===== -Let now $\bm{y}=\bm{A}\bm{x}$, where $\bm{A}$ is an $m\times n$ matrix and the matrix does not depend on $\bm{x}$. If we write out the vector $\bm{y}$ +Let now $\bm{y}=\bm{A}\bm{x}$, where $\bm{A}$ is an $m\times n$ matrix and the matrix does not depend on $\bm{x}$. If we write out the vector $\bm{y}$ compoment by component we have + +!bt +\[ +y_i = \sum_{j=0}^{n-1}a_{ij}x_j, +\] +!et +with $\all i=0,1,2,\dots,m-1$. The individual matrix elements of $\bm{A}$ are given by the symbol $a_{ij}$. +It follows that the partial derivatives of $y_i$ with respect to $x_k$ +!bt +\[ +\frac{\partial y_i }{\partial x_k}= a_{ik} \all i=0,1,2,\dots,m-1. +\] +!et + +From this we have, using the definition of the Jacobian + +!bt +\[ +\frac{\partial \bm{y} }{\partial \bm{x}}= \bm{A}. +\] +!et + + + + !bt