diff --git a/doc/pub/Regression/html/._Regression-bs000.html b/doc/pub/Regression/html/._Regression-bs000.html index b7757f73d..9560a3323 100644 --- a/doc/pub/Regression/html/._Regression-bs000.html +++ b/doc/pub/Regression/html/._Regression-bs000.html @@ -73,33 +73,47 @@ Automatically generated HTML file from DocOnce source 2, None, '___sec10'), - ('Simple regression model', 2, None, '___sec11'), + ('The $\\chi^2$ function', 2, None, '___sec11'), + ('The $\\chi^2$ function', 2, None, '___sec12'), + ('The $\\chi^2$ function', 2, None, '___sec13'), + ('The $\\chi^2$ function', 2, None, '___sec14'), + ('The $\\chi^2$ function', 2, None, '___sec15'), + ('The $\\chi^2$ function', 2, None, '___sec16'), + ('Simple regression model', 2, None, '___sec17'), ('Simple regression model, now using _scikit-learn_', 2, None, - '___sec12'), - ('Correlations and the quality of our results', + '___sec18'), + ('Simple linear regression model using _scikit-learn_', 2, None, - '___sec13'), - ('Estimate of the error', 2, None, '___sec14'), - ('The $\\chi^2$ function', 2, None, '___sec15'), - ('The $\\chi^2$ function', 2, None, '___sec16'), - ('The $\\chi^2$ function', 2, None, '___sec17'), - ('The $\\chi^2$ function', 2, None, '___sec18'), - ('The $\\chi^2$ function', 2, None, '___sec19'), - ('The $\\chi^2$ function', 2, None, '___sec20'), + '___sec19'), + ('Simple linear regression model', 2, None, '___sec20'), + ('Less noise', 2, None, '___sec21'), + ('How to study our fits', 2, None, '___sec22'), + ('Minimizing the cost function', 2, None, '___sec23'), + ('Relative error', 2, None, '___sec24'), + ('The richness of _scikit-learn_', 2, None, '___sec25'), + ('Functions in _scikit-learn_', 2, None, '___sec26'), + ('Other functions in _scikit-learn_', 2, None, '___sec27'), + ('The mean absolute error and other functions in _scikit-learn_', + 2, + None, + '___sec28'), + ('Cubic polynomial in _scikit-learn_', 2, None, '___sec29'), ('Simple regression model with gradient descent', 2, None, - '___sec21'), + '___sec30'), ('Simple regression model with stochastic gradient descent', 2, None, - '___sec22'), - ('Polynomial Regression', 2, None, '___sec23'), - ('Ridge and Lasso Regression', 2, None, '___sec24'), - ('The singular value decompostion', 2, None, '___sec25')]} + '___sec31'), + ('Polynomial Regression', 2, None, '___sec32'), + ('Ridge and Lasso Regression', 2, None, '___sec33'), + ('The singular value decompostion', 2, None, '___sec34'), + ('Lasso and Ridge regression', 2, None, '___sec35'), + ('Logistic regression', 2, None, '___sec36')]} end of tocinfo --> @@ -148,21 +162,32 @@ MathJax.Hub.Config({
  • Interpretations and optimizing our parameters
  • Interpretations and optimizing our parameters
  • Interpretations and optimizing our parameters
  • -
  • Simple regression model
  • -
  • Simple regression model, now using scikit-learn
  • -
  • Correlations and the quality of our results
  • -
  • Estimate of the error
  • +
  • The \( \chi^2 \) function
  • +
  • The \( \chi^2 \) function
  • +
  • The \( \chi^2 \) function
  • +
  • The \( \chi^2 \) function
  • The \( \chi^2 \) function
  • The \( \chi^2 \) function
  • -
  • The \( \chi^2 \) function
  • -
  • The \( \chi^2 \) function
  • -
  • The \( \chi^2 \) function
  • -
  • The \( \chi^2 \) function
  • -
  • Simple regression model with gradient descent
  • -
  • Simple regression model with stochastic gradient descent
  • -
  • Polynomial Regression
  • -
  • Ridge and Lasso Regression
  • -
  • The singular value decompostion
  • +
  • Simple regression model
  • +
  • Simple regression model, now using scikit-learn
  • +
  • Simple linear regression model using scikit-learn
  • +
  • Simple linear regression model
  • +
  • Less noise
  • +
  • How to study our fits
  • +
  • Minimizing the cost function
  • +
  • Relative error
  • +
  • The richness of scikit-learn
  • +
  • Functions in scikit-learn
  • +
  • Other functions in scikit-learn
  • +
  • The mean absolute error and other functions in scikit-learn
  • +
  • Cubic polynomial in scikit-learn
  • +
  • Simple regression model with gradient descent
  • +
  • Simple regression model with stochastic gradient descent
  • +
  • Polynomial Regression
  • +
  • Ridge and Lasso Regression
  • +
  • The singular value decompostion
  • +
  • Lasso and Ridge regression
  • +
  • Logistic regression
  • @@ -197,7 +222,7 @@ MathJax.Hub.Config({
    [2] Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University

    -

    May 30, 2018

    +

    Aug 24, 2018


    @@ -221,7 +246,7 @@ MathJax.Hub.Config({

  • 9
  • 10
  • ...
  • -
  • 27
  • +
  • 38
  • »
  • diff --git a/doc/pub/Regression/html/._Regression-bs001.html b/doc/pub/Regression/html/._Regression-bs001.html index ae0f585f2..33c014dc2 100644 --- a/doc/pub/Regression/html/._Regression-bs001.html +++ b/doc/pub/Regression/html/._Regression-bs001.html @@ -73,33 +73,47 @@ Automatically generated HTML file from DocOnce source 2, None, '___sec10'), - ('Simple regression model', 2, None, '___sec11'), + ('The $\\chi^2$ function', 2, None, '___sec11'), + ('The $\\chi^2$ function', 2, None, '___sec12'), + ('The $\\chi^2$ function', 2, None, '___sec13'), + ('The $\\chi^2$ function', 2, None, '___sec14'), + ('The $\\chi^2$ function', 2, None, '___sec15'), + ('The $\\chi^2$ function', 2, None, '___sec16'), + ('Simple regression model', 2, None, '___sec17'), ('Simple regression model, now using _scikit-learn_', 2, None, - '___sec12'), - ('Correlations and the quality of our results', + '___sec18'), + ('Simple linear regression model using _scikit-learn_', 2, None, - '___sec13'), - ('Estimate of the error', 2, None, '___sec14'), - ('The $\\chi^2$ function', 2, None, '___sec15'), - ('The $\\chi^2$ function', 2, None, '___sec16'), - ('The $\\chi^2$ function', 2, None, '___sec17'), - ('The $\\chi^2$ function', 2, None, '___sec18'), - ('The $\\chi^2$ function', 2, None, '___sec19'), - ('The $\\chi^2$ function', 2, None, '___sec20'), + '___sec19'), + ('Simple linear regression model', 2, None, '___sec20'), + ('Less noise', 2, None, '___sec21'), + ('How to study our fits', 2, None, '___sec22'), + ('Minimizing the cost function', 2, None, '___sec23'), + ('Relative error', 2, None, '___sec24'), + ('The richness of _scikit-learn_', 2, None, '___sec25'), + ('Functions in _scikit-learn_', 2, None, '___sec26'), + ('Other functions in _scikit-learn_', 2, None, '___sec27'), + ('The mean absolute error and other functions in _scikit-learn_', + 2, + None, + '___sec28'), + ('Cubic polynomial in _scikit-learn_', 2, None, '___sec29'), ('Simple regression model with gradient descent', 2, None, - '___sec21'), + '___sec30'), ('Simple regression model with stochastic gradient descent', 2, None, - '___sec22'), - ('Polynomial Regression', 2, None, '___sec23'), - ('Ridge and Lasso Regression', 2, None, '___sec24'), - ('The singular value decompostion', 2, None, '___sec25')]} + '___sec31'), + ('Polynomial Regression', 2, None, '___sec32'), + ('Ridge and Lasso Regression', 2, None, '___sec33'), + ('The singular value decompostion', 2, None, '___sec34'), + ('Lasso and Ridge regression', 2, None, '___sec35'), + ('Logistic regression', 2, None, '___sec36')]} end of tocinfo --> @@ -148,21 +162,32 @@ MathJax.Hub.Config({
  • Interpretations and optimizing our parameters
  • Interpretations and optimizing our parameters
  • Interpretations and optimizing our parameters
  • -
  • Simple regression model
  • -
  • Simple regression model, now using scikit-learn
  • -
  • Correlations and the quality of our results
  • -
  • Estimate of the error
  • +
  • The \( \chi^2 \) function
  • +
  • The \( \chi^2 \) function
  • +
  • The \( \chi^2 \) function
  • +
  • The \( \chi^2 \) function
  • The \( \chi^2 \) function
  • The \( \chi^2 \) function
  • -
  • The \( \chi^2 \) function
  • -
  • The \( \chi^2 \) function
  • -
  • The \( \chi^2 \) function
  • -
  • The \( \chi^2 \) function
  • -
  • Simple regression model with gradient descent
  • -
  • Simple regression model with stochastic gradient descent
  • -
  • Polynomial Regression
  • -
  • Ridge and Lasso Regression
  • -
  • The singular value decompostion
  • +
  • Simple regression model
  • +
  • Simple regression model, now using scikit-learn
  • +
  • Simple linear regression model using scikit-learn
  • +
  • Simple linear regression model
  • +
  • Less noise
  • +
  • How to study our fits
  • +
  • Minimizing the cost function
  • +
  • Relative error
  • +
  • The richness of scikit-learn
  • +
  • Functions in scikit-learn
  • +
  • Other functions in scikit-learn
  • +
  • The mean absolute error and other functions in scikit-learn
  • +
  • Cubic polynomial in scikit-learn
  • +
  • Simple regression model with gradient descent
  • +
  • Simple regression model with stochastic gradient descent
  • +
  • Polynomial Regression
  • +
  • Ridge and Lasso Regression
  • +
  • The singular value decompostion
  • +
  • Lasso and Ridge regression
  • +
  • Logistic regression
  • @@ -218,7 +243,7 @@ A regression model aims at finding a likelihood function \( p(y\vert \hat{x}) \)
  • 10
  • 11
  • ...
  • -
  • 27
  • +
  • 38
  • »
  • diff --git a/doc/pub/Regression/html/._Regression-bs002.html b/doc/pub/Regression/html/._Regression-bs002.html index 262f36a33..25f3b7f34 100644 --- a/doc/pub/Regression/html/._Regression-bs002.html +++ b/doc/pub/Regression/html/._Regression-bs002.html @@ -73,33 +73,47 @@ Automatically generated HTML file from DocOnce source 2, None, '___sec10'), - ('Simple regression model', 2, None, '___sec11'), + ('The $\\chi^2$ function', 2, None, '___sec11'), + ('The $\\chi^2$ function', 2, None, '___sec12'), + ('The $\\chi^2$ function', 2, None, '___sec13'), + ('The $\\chi^2$ function', 2, None, '___sec14'), + ('The $\\chi^2$ function', 2, None, '___sec15'), + ('The $\\chi^2$ function', 2, None, '___sec16'), + ('Simple regression model', 2, None, '___sec17'), ('Simple regression model, now using _scikit-learn_', 2, None, - '___sec12'), - ('Correlations and the quality of our results', + '___sec18'), + ('Simple linear regression model using _scikit-learn_', 2, None, - '___sec13'), - ('Estimate of the error', 2, None, '___sec14'), - ('The $\\chi^2$ function', 2, None, '___sec15'), - ('The $\\chi^2$ function', 2, None, '___sec16'), - ('The $\\chi^2$ function', 2, None, '___sec17'), - ('The $\\chi^2$ function', 2, None, '___sec18'), - ('The $\\chi^2$ function', 2, None, '___sec19'), - ('The $\\chi^2$ function', 2, None, '___sec20'), + '___sec19'), + ('Simple linear regression model', 2, None, '___sec20'), + ('Less noise', 2, None, '___sec21'), + ('How to study our fits', 2, None, '___sec22'), + ('Minimizing the cost function', 2, None, '___sec23'), + ('Relative error', 2, None, '___sec24'), + ('The richness of _scikit-learn_', 2, None, '___sec25'), + ('Functions in _scikit-learn_', 2, None, '___sec26'), + ('Other functions in _scikit-learn_', 2, None, '___sec27'), + ('The mean absolute error and other functions in _scikit-learn_', + 2, + None, + '___sec28'), + ('Cubic polynomial in _scikit-learn_', 2, None, '___sec29'), ('Simple regression model with gradient descent', 2, None, - '___sec21'), + '___sec30'), ('Simple regression model with stochastic gradient descent', 2, None, - '___sec22'), - ('Polynomial Regression', 2, None, '___sec23'), - ('Ridge and Lasso Regression', 2, None, '___sec24'), - ('The singular value decompostion', 2, None, '___sec25')]} + '___sec31'), + ('Polynomial Regression', 2, None, '___sec32'), + ('Ridge and Lasso Regression', 2, None, '___sec33'), + ('The singular value decompostion', 2, None, '___sec34'), + ('Lasso and Ridge regression', 2, None, '___sec35'), + ('Logistic regression', 2, None, '___sec36')]} end of tocinfo --> @@ -148,21 +162,32 @@ MathJax.Hub.Config({
  • Interpretations and optimizing our parameters
  • Interpretations and optimizing our parameters
  • Interpretations and optimizing our parameters
  • -
  • Simple regression model
  • -
  • Simple regression model, now using scikit-learn
  • -
  • Correlations and the quality of our results
  • -
  • Estimate of the error
  • +
  • The \( \chi^2 \) function
  • +
  • The \( \chi^2 \) function
  • +
  • The \( \chi^2 \) function
  • +
  • The \( \chi^2 \) function
  • The \( \chi^2 \) function
  • The \( \chi^2 \) function
  • -
  • The \( \chi^2 \) function
  • -
  • The \( \chi^2 \) function
  • -
  • The \( \chi^2 \) function
  • -
  • The \( \chi^2 \) function
  • -
  • Simple regression model with gradient descent
  • -
  • Simple regression model with stochastic gradient descent
  • -
  • Polynomial Regression
  • -
  • Ridge and Lasso Regression
  • -
  • The singular value decompostion
  • +
  • Simple regression model
  • +
  • Simple regression model, now using scikit-learn
  • +
  • Simple linear regression model using scikit-learn
  • +
  • Simple linear regression model
  • +
  • Less noise
  • +
  • How to study our fits
  • +
  • Minimizing the cost function
  • +
  • Relative error
  • +
  • The richness of scikit-learn
  • +
  • Functions in scikit-learn
  • +
  • Other functions in scikit-learn
  • +
  • The mean absolute error and other functions in scikit-learn
  • +
  • Cubic polynomial in scikit-learn
  • +
  • Simple regression model with gradient descent
  • +
  • Simple regression model with stochastic gradient descent
  • +
  • Polynomial Regression
  • +
  • Ridge and Lasso Regression
  • +
  • The singular value decompostion
  • +
  • Lasso and Ridge regression
  • +
  • Logistic regression
  • @@ -215,7 +240,7 @@ where \( \epsilon_i \) is the error in our approximation.
  • 11
  • 12
  • ...
  • -
  • 27
  • +
  • 38
  • »
  • diff --git a/doc/pub/Regression/html/._Regression-bs003.html b/doc/pub/Regression/html/._Regression-bs003.html index 06a150b46..89470dd65 100644 --- a/doc/pub/Regression/html/._Regression-bs003.html +++ b/doc/pub/Regression/html/._Regression-bs003.html @@ -73,33 +73,47 @@ Automatically generated HTML file from DocOnce source 2, None, '___sec10'), - ('Simple regression model', 2, None, '___sec11'), + ('The $\\chi^2$ function', 2, None, '___sec11'), + ('The $\\chi^2$ function', 2, None, '___sec12'), + ('The $\\chi^2$ function', 2, None, '___sec13'), + ('The $\\chi^2$ function', 2, None, '___sec14'), + ('The $\\chi^2$ function', 2, None, '___sec15'), + ('The $\\chi^2$ function', 2, None, '___sec16'), + ('Simple regression model', 2, None, '___sec17'), ('Simple regression model, now using _scikit-learn_', 2, None, - '___sec12'), - ('Correlations and the quality of our results', + '___sec18'), + ('Simple linear regression model using _scikit-learn_', 2, None, - '___sec13'), - ('Estimate of the error', 2, None, '___sec14'), - ('The $\\chi^2$ function', 2, None, '___sec15'), - ('The $\\chi^2$ function', 2, None, '___sec16'), - ('The $\\chi^2$ function', 2, None, '___sec17'), - ('The $\\chi^2$ function', 2, None, '___sec18'), - ('The $\\chi^2$ function', 2, None, '___sec19'), - ('The $\\chi^2$ function', 2, None, '___sec20'), + '___sec19'), + ('Simple linear regression model', 2, None, '___sec20'), + ('Less noise', 2, None, '___sec21'), + ('How to study our fits', 2, None, '___sec22'), + ('Minimizing the cost function', 2, None, '___sec23'), + ('Relative error', 2, None, '___sec24'), + ('The richness of _scikit-learn_', 2, None, '___sec25'), + ('Functions in _scikit-learn_', 2, None, '___sec26'), + ('Other functions in _scikit-learn_', 2, None, '___sec27'), + ('The mean absolute error and other functions in _scikit-learn_', + 2, + None, + '___sec28'), + ('Cubic polynomial in _scikit-learn_', 2, None, '___sec29'), ('Simple regression model with gradient descent', 2, None, - '___sec21'), + '___sec30'), ('Simple regression model with stochastic gradient descent', 2, None, - '___sec22'), - ('Polynomial Regression', 2, None, '___sec23'), - ('Ridge and Lasso Regression', 2, None, '___sec24'), - ('The singular value decompostion', 2, None, '___sec25')]} + '___sec31'), + ('Polynomial Regression', 2, None, '___sec32'), + ('Ridge and Lasso Regression', 2, None, '___sec33'), + ('The singular value decompostion', 2, None, '___sec34'), + ('Lasso and Ridge regression', 2, None, '___sec35'), + ('Logistic regression', 2, None, '___sec36')]} end of tocinfo --> @@ -148,21 +162,32 @@ MathJax.Hub.Config({
  • Interpretations and optimizing our parameters
  • Interpretations and optimizing our parameters
  • Interpretations and optimizing our parameters
  • -
  • Simple regression model
  • -
  • Simple regression model, now using scikit-learn
  • -
  • Correlations and the quality of our results
  • -
  • Estimate of the error
  • +
  • The \( \chi^2 \) function
  • +
  • The \( \chi^2 \) function
  • +
  • The \( \chi^2 \) function
  • +
  • The \( \chi^2 \) function
  • The \( \chi^2 \) function
  • The \( \chi^2 \) function
  • -
  • The \( \chi^2 \) function
  • -
  • The \( \chi^2 \) function
  • -
  • The \( \chi^2 \) function
  • -
  • The \( \chi^2 \) function
  • -
  • Simple regression model with gradient descent
  • -
  • Simple regression model with stochastic gradient descent
  • -
  • Polynomial Regression
  • -
  • Ridge and Lasso Regression
  • -
  • The singular value decompostion
  • +
  • Simple regression model
  • +
  • Simple regression model, now using scikit-learn
  • +
  • Simple linear regression model using scikit-learn
  • +
  • Simple linear regression model
  • +
  • Less noise
  • +
  • How to study our fits
  • +
  • Minimizing the cost function
  • +
  • Relative error
  • +
  • The richness of scikit-learn
  • +
  • Functions in scikit-learn
  • +
  • Other functions in scikit-learn
  • +
  • The mean absolute error and other functions in scikit-learn
  • +
  • Cubic polynomial in scikit-learn
  • +
  • Simple regression model with gradient descent
  • +
  • Simple regression model with stochastic gradient descent
  • +
  • Polynomial Regression
  • +
  • Ridge and Lasso Regression
  • +
  • The singular value decompostion
  • +
  • Lasso and Ridge regression
  • +
  • Logistic regression
  • @@ -215,7 +240,7 @@ $$
  • 12
  • 13
  • ...
  • -
  • 27
  • +
  • 38
  • »
  • diff --git a/doc/pub/Regression/html/._Regression-bs004.html b/doc/pub/Regression/html/._Regression-bs004.html index 829225e4b..d3f378b54 100644 --- a/doc/pub/Regression/html/._Regression-bs004.html +++ b/doc/pub/Regression/html/._Regression-bs004.html @@ -73,33 +73,47 @@ Automatically generated HTML file from DocOnce source 2, None, '___sec10'), - ('Simple regression model', 2, None, '___sec11'), + ('The $\\chi^2$ function', 2, None, '___sec11'), + ('The $\\chi^2$ function', 2, None, '___sec12'), + ('The $\\chi^2$ function', 2, None, '___sec13'), + ('The $\\chi^2$ function', 2, None, '___sec14'), + ('The $\\chi^2$ function', 2, None, '___sec15'), + ('The $\\chi^2$ function', 2, None, '___sec16'), + ('Simple regression model', 2, None, '___sec17'), ('Simple regression model, now using _scikit-learn_', 2, None, - '___sec12'), - ('Correlations and the quality of our results', + '___sec18'), + ('Simple linear regression model using _scikit-learn_', 2, None, - '___sec13'), - ('Estimate of the error', 2, None, '___sec14'), - ('The $\\chi^2$ function', 2, None, '___sec15'), - ('The $\\chi^2$ function', 2, None, '___sec16'), - ('The $\\chi^2$ function', 2, None, '___sec17'), - ('The $\\chi^2$ function', 2, None, '___sec18'), - ('The $\\chi^2$ function', 2, None, '___sec19'), - ('The $\\chi^2$ function', 2, None, '___sec20'), + '___sec19'), + ('Simple linear regression model', 2, None, '___sec20'), + ('Less noise', 2, None, '___sec21'), + ('How to study our fits', 2, None, '___sec22'), + ('Minimizing the cost function', 2, None, '___sec23'), + ('Relative error', 2, None, '___sec24'), + ('The richness of _scikit-learn_', 2, None, '___sec25'), + ('Functions in _scikit-learn_', 2, None, '___sec26'), + ('Other functions in _scikit-learn_', 2, None, '___sec27'), + ('The mean absolute error and other functions in _scikit-learn_', + 2, + None, + '___sec28'), + ('Cubic polynomial in _scikit-learn_', 2, None, '___sec29'), ('Simple regression model with gradient descent', 2, None, - '___sec21'), + '___sec30'), ('Simple regression model with stochastic gradient descent', 2, None, - '___sec22'), - ('Polynomial Regression', 2, None, '___sec23'), - ('Ridge and Lasso Regression', 2, None, '___sec24'), - ('The singular value decompostion', 2, None, '___sec25')]} + '___sec31'), + ('Polynomial Regression', 2, None, '___sec32'), + ('Ridge and Lasso Regression', 2, None, '___sec33'), + ('The singular value decompostion', 2, None, '___sec34'), + ('Lasso and Ridge regression', 2, None, '___sec35'), + ('Logistic regression', 2, None, '___sec36')]} end of tocinfo --> @@ -148,21 +162,32 @@ MathJax.Hub.Config({
  • Interpretations and optimizing our parameters
  • Interpretations and optimizing our parameters
  • Interpretations and optimizing our parameters
  • -
  • Simple regression model
  • -
  • Simple regression model, now using scikit-learn
  • -
  • Correlations and the quality of our results
  • -
  • Estimate of the error
  • +
  • The \( \chi^2 \) function
  • +
  • The \( \chi^2 \) function
  • +
  • The \( \chi^2 \) function
  • +
  • The \( \chi^2 \) function
  • The \( \chi^2 \) function
  • The \( \chi^2 \) function
  • -
  • The \( \chi^2 \) function
  • -
  • The \( \chi^2 \) function
  • -
  • The \( \chi^2 \) function
  • -
  • The \( \chi^2 \) function
  • -
  • Simple regression model with gradient descent
  • -
  • Simple regression model with stochastic gradient descent
  • -
  • Polynomial Regression
  • -
  • Ridge and Lasso Regression
  • -
  • The singular value decompostion
  • +
  • Simple regression model
  • +
  • Simple regression model, now using scikit-learn
  • +
  • Simple linear regression model using scikit-learn
  • +
  • Simple linear regression model
  • +
  • Less noise
  • +
  • How to study our fits
  • +
  • Minimizing the cost function
  • +
  • Relative error
  • +
  • The richness of scikit-learn
  • +
  • Functions in scikit-learn
  • +
  • Other functions in scikit-learn
  • +
  • The mean absolute error and other functions in scikit-learn
  • +
  • Cubic polynomial in scikit-learn
  • +
  • Simple regression model with gradient descent
  • +
  • Simple regression model with stochastic gradient descent
  • +
  • Polynomial Regression
  • +
  • Ridge and Lasso Regression
  • +
  • The singular value decompostion
  • +
  • Lasso and Ridge regression
  • +
  • Logistic regression
  • @@ -235,7 +260,7 @@ $$
  • 13
  • 14
  • ...
  • -
  • 27
  • +
  • 38
  • »
  • diff --git a/doc/pub/Regression/html/._Regression-bs005.html b/doc/pub/Regression/html/._Regression-bs005.html index 63161c59b..af0a02dcd 100644 --- a/doc/pub/Regression/html/._Regression-bs005.html +++ b/doc/pub/Regression/html/._Regression-bs005.html @@ -73,33 +73,47 @@ Automatically generated HTML file from DocOnce source 2, None, '___sec10'), - ('Simple regression model', 2, None, '___sec11'), + ('The $\\chi^2$ function', 2, None, '___sec11'), + ('The $\\chi^2$ function', 2, None, '___sec12'), + ('The $\\chi^2$ function', 2, None, '___sec13'), + ('The $\\chi^2$ function', 2, None, '___sec14'), + ('The $\\chi^2$ function', 2, None, '___sec15'), + ('The $\\chi^2$ function', 2, None, '___sec16'), + ('Simple regression model', 2, None, '___sec17'), ('Simple regression model, now using _scikit-learn_', 2, None, - '___sec12'), - ('Correlations and the quality of our results', + '___sec18'), + ('Simple linear regression model using _scikit-learn_', 2, None, - '___sec13'), - ('Estimate of the error', 2, None, '___sec14'), - ('The $\\chi^2$ function', 2, None, '___sec15'), - ('The $\\chi^2$ function', 2, None, '___sec16'), - ('The $\\chi^2$ function', 2, None, '___sec17'), - ('The $\\chi^2$ function', 2, None, '___sec18'), - ('The $\\chi^2$ function', 2, None, '___sec19'), - ('The $\\chi^2$ function', 2, None, '___sec20'), + '___sec19'), + ('Simple linear regression model', 2, None, '___sec20'), + ('Less noise', 2, None, '___sec21'), + ('How to study our fits', 2, None, '___sec22'), + ('Minimizing the cost function', 2, None, '___sec23'), + ('Relative error', 2, None, '___sec24'), + ('The richness of _scikit-learn_', 2, None, '___sec25'), + ('Functions in _scikit-learn_', 2, None, '___sec26'), + ('Other functions in _scikit-learn_', 2, None, '___sec27'), + ('The mean absolute error and other functions in _scikit-learn_', + 2, + None, + '___sec28'), + ('Cubic polynomial in _scikit-learn_', 2, None, '___sec29'), ('Simple regression model with gradient descent', 2, None, - '___sec21'), + '___sec30'), ('Simple regression model with stochastic gradient descent', 2, None, - '___sec22'), - ('Polynomial Regression', 2, None, '___sec23'), - ('Ridge and Lasso Regression', 2, None, '___sec24'), - ('The singular value decompostion', 2, None, '___sec25')]} + '___sec31'), + ('Polynomial Regression', 2, None, '___sec32'), + ('Ridge and Lasso Regression', 2, None, '___sec33'), + ('The singular value decompostion', 2, None, '___sec34'), + ('Lasso and Ridge regression', 2, None, '___sec35'), + ('Logistic regression', 2, None, '___sec36')]} end of tocinfo --> @@ -148,21 +162,32 @@ MathJax.Hub.Config({
  • Interpretations and optimizing our parameters
  • Interpretations and optimizing our parameters
  • Interpretations and optimizing our parameters
  • -
  • Simple regression model
  • -
  • Simple regression model, now using scikit-learn
  • -
  • Correlations and the quality of our results
  • -
  • Estimate of the error
  • +
  • The \( \chi^2 \) function
  • +
  • The \( \chi^2 \) function
  • +
  • The \( \chi^2 \) function
  • +
  • The \( \chi^2 \) function
  • The \( \chi^2 \) function
  • The \( \chi^2 \) function
  • -
  • The \( \chi^2 \) function
  • -
  • The \( \chi^2 \) function
  • -
  • The \( \chi^2 \) function
  • -
  • The \( \chi^2 \) function
  • -
  • Simple regression model with gradient descent
  • -
  • Simple regression model with stochastic gradient descent
  • -
  • Polynomial Regression
  • -
  • Ridge and Lasso Regression
  • -
  • The singular value decompostion
  • +
  • Simple regression model
  • +
  • Simple regression model, now using scikit-learn
  • +
  • Simple linear regression model using scikit-learn
  • +
  • Simple linear regression model
  • +
  • Less noise
  • +
  • How to study our fits
  • +
  • Minimizing the cost function
  • +
  • Relative error
  • +
  • The richness of scikit-learn
  • +
  • Functions in scikit-learn
  • +
  • Other functions in scikit-learn
  • +
  • The mean absolute error and other functions in scikit-learn
  • +
  • Cubic polynomial in scikit-learn
  • +
  • Simple regression model with gradient descent
  • +
  • Simple regression model with stochastic gradient descent
  • +
  • Polynomial Regression
  • +
  • Ridge and Lasso Regression
  • +
  • The singular value decompostion
  • +
  • Lasso and Ridge regression
  • +
  • Logistic regression
  • @@ -220,7 +245,7 @@ $$
  • 14
  • 15
  • ...
  • -
  • 27
  • +
  • 38
  • »
  • diff --git a/doc/pub/Regression/html/._Regression-bs006.html b/doc/pub/Regression/html/._Regression-bs006.html index 9bb94703a..8e92c7d0c 100644 --- a/doc/pub/Regression/html/._Regression-bs006.html +++ b/doc/pub/Regression/html/._Regression-bs006.html @@ -73,33 +73,47 @@ Automatically generated HTML file from DocOnce source 2, None, '___sec10'), - ('Simple regression model', 2, None, '___sec11'), + ('The $\\chi^2$ function', 2, None, '___sec11'), + ('The $\\chi^2$ function', 2, None, '___sec12'), + ('The $\\chi^2$ function', 2, None, '___sec13'), + ('The $\\chi^2$ function', 2, None, '___sec14'), + ('The $\\chi^2$ function', 2, None, '___sec15'), + ('The $\\chi^2$ function', 2, None, '___sec16'), + ('Simple regression model', 2, None, '___sec17'), ('Simple regression model, now using _scikit-learn_', 2, None, - '___sec12'), - ('Correlations and the quality of our results', + '___sec18'), + ('Simple linear regression model using _scikit-learn_', 2, None, - '___sec13'), - ('Estimate of the error', 2, None, '___sec14'), - ('The $\\chi^2$ function', 2, None, '___sec15'), - ('The $\\chi^2$ function', 2, None, '___sec16'), - ('The $\\chi^2$ function', 2, None, '___sec17'), - ('The $\\chi^2$ function', 2, None, '___sec18'), - ('The $\\chi^2$ function', 2, None, '___sec19'), - ('The $\\chi^2$ function', 2, None, '___sec20'), + '___sec19'), + ('Simple linear regression model', 2, None, '___sec20'), + ('Less noise', 2, None, '___sec21'), + ('How to study our fits', 2, None, '___sec22'), + ('Minimizing the cost function', 2, None, '___sec23'), + ('Relative error', 2, None, '___sec24'), + ('The richness of _scikit-learn_', 2, None, '___sec25'), + ('Functions in _scikit-learn_', 2, None, '___sec26'), + ('Other functions in _scikit-learn_', 2, None, '___sec27'), + ('The mean absolute error and other functions in _scikit-learn_', + 2, + None, + '___sec28'), + ('Cubic polynomial in _scikit-learn_', 2, None, '___sec29'), ('Simple regression model with gradient descent', 2, None, - '___sec21'), + '___sec30'), ('Simple regression model with stochastic gradient descent', 2, None, - '___sec22'), - ('Polynomial Regression', 2, None, '___sec23'), - ('Ridge and Lasso Regression', 2, None, '___sec24'), - ('The singular value decompostion', 2, None, '___sec25')]} + '___sec31'), + ('Polynomial Regression', 2, None, '___sec32'), + ('Ridge and Lasso Regression', 2, None, '___sec33'), + ('The singular value decompostion', 2, None, '___sec34'), + ('Lasso and Ridge regression', 2, None, '___sec35'), + ('Logistic regression', 2, None, '___sec36')]} end of tocinfo --> @@ -148,21 +162,32 @@ MathJax.Hub.Config({
  • Interpretations and optimizing our parameters
  • Interpretations and optimizing our parameters
  • Interpretations and optimizing our parameters
  • -
  • Simple regression model
  • -
  • Simple regression model, now using scikit-learn
  • -
  • Correlations and the quality of our results
  • -
  • Estimate of the error
  • +
  • The \( \chi^2 \) function
  • +
  • The \( \chi^2 \) function
  • +
  • The \( \chi^2 \) function
  • +
  • The \( \chi^2 \) function
  • The \( \chi^2 \) function
  • The \( \chi^2 \) function
  • -
  • The \( \chi^2 \) function
  • -
  • The \( \chi^2 \) function
  • -
  • The \( \chi^2 \) function
  • -
  • The \( \chi^2 \) function
  • -
  • Simple regression model with gradient descent
  • -
  • Simple regression model with stochastic gradient descent
  • -
  • Polynomial Regression
  • -
  • Ridge and Lasso Regression
  • -
  • The singular value decompostion
  • +
  • Simple regression model
  • +
  • Simple regression model, now using scikit-learn
  • +
  • Simple linear regression model using scikit-learn
  • +
  • Simple linear regression model
  • +
  • Less noise
  • +
  • How to study our fits
  • +
  • Minimizing the cost function
  • +
  • Relative error
  • +
  • The richness of scikit-learn
  • +
  • Functions in scikit-learn
  • +
  • Other functions in scikit-learn
  • +
  • The mean absolute error and other functions in scikit-learn
  • +
  • Cubic polynomial in scikit-learn
  • +
  • Simple regression model with gradient descent
  • +
  • Simple regression model with stochastic gradient descent
  • +
  • Polynomial Regression
  • +
  • Ridge and Lasso Regression
  • +
  • The singular value decompostion
  • +
  • Lasso and Ridge regression
  • +
  • Logistic regression
  • @@ -226,7 +251,7 @@ The left-hand side of this equation forms know. Our error vector \( \hat{\epsilo
  • 15
  • 16
  • ...
  • -
  • 27
  • +
  • 38
  • »
  • diff --git a/doc/pub/Regression/html/._Regression-bs007.html b/doc/pub/Regression/html/._Regression-bs007.html index 93f9326d9..039c86a9e 100644 --- a/doc/pub/Regression/html/._Regression-bs007.html +++ b/doc/pub/Regression/html/._Regression-bs007.html @@ -73,33 +73,47 @@ Automatically generated HTML file from DocOnce source 2, None, '___sec10'), - ('Simple regression model', 2, None, '___sec11'), + ('The $\\chi^2$ function', 2, None, '___sec11'), + ('The $\\chi^2$ function', 2, None, '___sec12'), + ('The $\\chi^2$ function', 2, None, '___sec13'), + ('The $\\chi^2$ function', 2, None, '___sec14'), + ('The $\\chi^2$ function', 2, None, '___sec15'), + ('The $\\chi^2$ function', 2, None, '___sec16'), + ('Simple regression model', 2, None, '___sec17'), ('Simple regression model, now using _scikit-learn_', 2, None, - '___sec12'), - ('Correlations and the quality of our results', + '___sec18'), + ('Simple linear regression model using _scikit-learn_', 2, None, - '___sec13'), - ('Estimate of the error', 2, None, '___sec14'), - ('The $\\chi^2$ function', 2, None, '___sec15'), - ('The $\\chi^2$ function', 2, None, '___sec16'), - ('The $\\chi^2$ function', 2, None, '___sec17'), - ('The $\\chi^2$ function', 2, None, '___sec18'), - ('The $\\chi^2$ function', 2, None, '___sec19'), - ('The $\\chi^2$ function', 2, None, '___sec20'), + '___sec19'), + ('Simple linear regression model', 2, None, '___sec20'), + ('Less noise', 2, None, '___sec21'), + ('How to study our fits', 2, None, '___sec22'), + ('Minimizing the cost function', 2, None, '___sec23'), + ('Relative error', 2, None, '___sec24'), + ('The richness of _scikit-learn_', 2, None, '___sec25'), + ('Functions in _scikit-learn_', 2, None, '___sec26'), + ('Other functions in _scikit-learn_', 2, None, '___sec27'), + ('The mean absolute error and other functions in _scikit-learn_', + 2, + None, + '___sec28'), + ('Cubic polynomial in _scikit-learn_', 2, None, '___sec29'), ('Simple regression model with gradient descent', 2, None, - '___sec21'), + '___sec30'), ('Simple regression model with stochastic gradient descent', 2, None, - '___sec22'), - ('Polynomial Regression', 2, None, '___sec23'), - ('Ridge and Lasso Regression', 2, None, '___sec24'), - ('The singular value decompostion', 2, None, '___sec25')]} + '___sec31'), + ('Polynomial Regression', 2, None, '___sec32'), + ('Ridge and Lasso Regression', 2, None, '___sec33'), + ('The singular value decompostion', 2, None, '___sec34'), + ('Lasso and Ridge regression', 2, None, '___sec35'), + ('Logistic regression', 2, None, '___sec36')]} end of tocinfo --> @@ -148,21 +162,32 @@ MathJax.Hub.Config({
  • Interpretations and optimizing our parameters
  • Interpretations and optimizing our parameters
  • Interpretations and optimizing our parameters
  • -
  • Simple regression model
  • -
  • Simple regression model, now using scikit-learn
  • -
  • Correlations and the quality of our results
  • -
  • Estimate of the error
  • +
  • The \( \chi^2 \) function
  • +
  • The \( \chi^2 \) function
  • +
  • The \( \chi^2 \) function
  • +
  • The \( \chi^2 \) function
  • The \( \chi^2 \) function
  • The \( \chi^2 \) function
  • -
  • The \( \chi^2 \) function
  • -
  • The \( \chi^2 \) function
  • -
  • The \( \chi^2 \) function
  • -
  • The \( \chi^2 \) function
  • -
  • Simple regression model with gradient descent
  • -
  • Simple regression model with stochastic gradient descent
  • -
  • Polynomial Regression
  • -
  • Ridge and Lasso Regression
  • -
  • The singular value decompostion
  • +
  • Simple regression model
  • +
  • Simple regression model, now using scikit-learn
  • +
  • Simple linear regression model using scikit-learn
  • +
  • Simple linear regression model
  • +
  • Less noise
  • +
  • How to study our fits
  • +
  • Minimizing the cost function
  • +
  • Relative error
  • +
  • The richness of scikit-learn
  • +
  • Functions in scikit-learn
  • +
  • Other functions in scikit-learn
  • +
  • The mean absolute error and other functions in scikit-learn
  • +
  • Cubic polynomial in scikit-learn
  • +
  • Simple regression model with gradient descent
  • +
  • Simple regression model with stochastic gradient descent
  • +
  • Polynomial Regression
  • +
  • Ridge and Lasso Regression
  • +
  • The singular value decompostion
  • +
  • Lasso and Ridge regression
  • +
  • Logistic regression
  • @@ -221,7 +246,7 @@ $$
  • 16
  • 17
  • ...
  • -
  • 27
  • +
  • 38
  • »
  • diff --git a/doc/pub/Regression/html/._Regression-bs008.html b/doc/pub/Regression/html/._Regression-bs008.html index 0450a56ab..c165bad53 100644 --- a/doc/pub/Regression/html/._Regression-bs008.html +++ b/doc/pub/Regression/html/._Regression-bs008.html @@ -73,33 +73,47 @@ Automatically generated HTML file from DocOnce source 2, None, '___sec10'), - ('Simple regression model', 2, None, '___sec11'), + ('The $\\chi^2$ function', 2, None, '___sec11'), + ('The $\\chi^2$ function', 2, None, '___sec12'), + ('The $\\chi^2$ function', 2, None, '___sec13'), + ('The $\\chi^2$ function', 2, None, '___sec14'), + ('The $\\chi^2$ function', 2, None, '___sec15'), + ('The $\\chi^2$ function', 2, None, '___sec16'), + ('Simple regression model', 2, None, '___sec17'), ('Simple regression model, now using _scikit-learn_', 2, None, - '___sec12'), - ('Correlations and the quality of our results', + '___sec18'), + ('Simple linear regression model using _scikit-learn_', 2, None, - '___sec13'), - ('Estimate of the error', 2, None, '___sec14'), - ('The $\\chi^2$ function', 2, None, '___sec15'), - ('The $\\chi^2$ function', 2, None, '___sec16'), - ('The $\\chi^2$ function', 2, None, '___sec17'), - ('The $\\chi^2$ function', 2, None, '___sec18'), - ('The $\\chi^2$ function', 2, None, '___sec19'), - ('The $\\chi^2$ function', 2, None, '___sec20'), + '___sec19'), + ('Simple linear regression model', 2, None, '___sec20'), + ('Less noise', 2, None, '___sec21'), + ('How to study our fits', 2, None, '___sec22'), + ('Minimizing the cost function', 2, None, '___sec23'), + ('Relative error', 2, None, '___sec24'), + ('The richness of _scikit-learn_', 2, None, '___sec25'), + ('Functions in _scikit-learn_', 2, None, '___sec26'), + ('Other functions in _scikit-learn_', 2, None, '___sec27'), + ('The mean absolute error and other functions in _scikit-learn_', + 2, + None, + '___sec28'), + ('Cubic polynomial in _scikit-learn_', 2, None, '___sec29'), ('Simple regression model with gradient descent', 2, None, - '___sec21'), + '___sec30'), ('Simple regression model with stochastic gradient descent', 2, None, - '___sec22'), - ('Polynomial Regression', 2, None, '___sec23'), - ('Ridge and Lasso Regression', 2, None, '___sec24'), - ('The singular value decompostion', 2, None, '___sec25')]} + '___sec31'), + ('Polynomial Regression', 2, None, '___sec32'), + ('Ridge and Lasso Regression', 2, None, '___sec33'), + ('The singular value decompostion', 2, None, '___sec34'), + ('Lasso and Ridge regression', 2, None, '___sec35'), + ('Logistic regression', 2, None, '___sec36')]} end of tocinfo --> @@ -148,21 +162,32 @@ MathJax.Hub.Config({
  • Interpretations and optimizing our parameters
  • Interpretations and optimizing our parameters
  • Interpretations and optimizing our parameters
  • -
  • Simple regression model
  • -
  • Simple regression model, now using scikit-learn
  • -
  • Correlations and the quality of our results
  • -
  • Estimate of the error
  • +
  • The \( \chi^2 \) function
  • +
  • The \( \chi^2 \) function
  • +
  • The \( \chi^2 \) function
  • +
  • The \( \chi^2 \) function
  • The \( \chi^2 \) function
  • The \( \chi^2 \) function
  • -
  • The \( \chi^2 \) function
  • -
  • The \( \chi^2 \) function
  • -
  • The \( \chi^2 \) function
  • -
  • The \( \chi^2 \) function
  • -
  • Simple regression model with gradient descent
  • -
  • Simple regression model with stochastic gradient descent
  • -
  • Polynomial Regression
  • -
  • Ridge and Lasso Regression
  • -
  • The singular value decompostion
  • +
  • Simple regression model
  • +
  • Simple regression model, now using scikit-learn
  • +
  • Simple linear regression model using scikit-learn
  • +
  • Simple linear regression model
  • +
  • Less noise
  • +
  • How to study our fits
  • +
  • Minimizing the cost function
  • +
  • Relative error
  • +
  • The richness of scikit-learn
  • +
  • Functions in scikit-learn
  • +
  • Other functions in scikit-learn
  • +
  • The mean absolute error and other functions in scikit-learn
  • +
  • Cubic polynomial in scikit-learn
  • +
  • Simple regression model with gradient descent
  • +
  • Simple regression model with stochastic gradient descent
  • +
  • Polynomial Regression
  • +
  • Ridge and Lasso Regression
  • +
  • The singular value decompostion
  • +
  • Lasso and Ridge regression
  • +
  • Logistic regression
  • @@ -224,7 +249,7 @@ $$
  • 17
  • 18
  • ...
  • -
  • 27
  • +
  • 38
  • »
  • diff --git a/doc/pub/Regression/html/._Regression-bs009.html b/doc/pub/Regression/html/._Regression-bs009.html index 4ceeaa756..8cc5f39fa 100644 --- a/doc/pub/Regression/html/._Regression-bs009.html +++ b/doc/pub/Regression/html/._Regression-bs009.html @@ -73,33 +73,47 @@ Automatically generated HTML file from DocOnce source 2, None, '___sec10'), - ('Simple regression model', 2, None, '___sec11'), + ('The $\\chi^2$ function', 2, None, '___sec11'), + ('The $\\chi^2$ function', 2, None, '___sec12'), + ('The $\\chi^2$ function', 2, None, '___sec13'), + ('The $\\chi^2$ function', 2, None, '___sec14'), + ('The $\\chi^2$ function', 2, None, '___sec15'), + ('The $\\chi^2$ function', 2, None, '___sec16'), + ('Simple regression model', 2, None, '___sec17'), ('Simple regression model, now using _scikit-learn_', 2, None, - '___sec12'), - ('Correlations and the quality of our results', + '___sec18'), + ('Simple linear regression model using _scikit-learn_', 2, None, - '___sec13'), - ('Estimate of the error', 2, None, '___sec14'), - ('The $\\chi^2$ function', 2, None, '___sec15'), - ('The $\\chi^2$ function', 2, None, '___sec16'), - ('The $\\chi^2$ function', 2, None, '___sec17'), - ('The $\\chi^2$ function', 2, None, '___sec18'), - ('The $\\chi^2$ function', 2, None, '___sec19'), - ('The $\\chi^2$ function', 2, None, '___sec20'), + '___sec19'), + ('Simple linear regression model', 2, None, '___sec20'), + ('Less noise', 2, None, '___sec21'), + ('How to study our fits', 2, None, '___sec22'), + ('Minimizing the cost function', 2, None, '___sec23'), + ('Relative error', 2, None, '___sec24'), + ('The richness of _scikit-learn_', 2, None, '___sec25'), + ('Functions in _scikit-learn_', 2, None, '___sec26'), + ('Other functions in _scikit-learn_', 2, None, '___sec27'), + ('The mean absolute error and other functions in _scikit-learn_', + 2, + None, + '___sec28'), + ('Cubic polynomial in _scikit-learn_', 2, None, '___sec29'), ('Simple regression model with gradient descent', 2, None, - '___sec21'), + '___sec30'), ('Simple regression model with stochastic gradient descent', 2, None, - '___sec22'), - ('Polynomial Regression', 2, None, '___sec23'), - ('Ridge and Lasso Regression', 2, None, '___sec24'), - ('The singular value decompostion', 2, None, '___sec25')]} + '___sec31'), + ('Polynomial Regression', 2, None, '___sec32'), + ('Ridge and Lasso Regression', 2, None, '___sec33'), + ('The singular value decompostion', 2, None, '___sec34'), + ('Lasso and Ridge regression', 2, None, '___sec35'), + ('Logistic regression', 2, None, '___sec36')]} end of tocinfo --> @@ -148,21 +162,32 @@ MathJax.Hub.Config({
  • Interpretations and optimizing our parameters
  • Interpretations and optimizing our parameters
  • Interpretations and optimizing our parameters
  • -
  • Simple regression model
  • -
  • Simple regression model, now using scikit-learn
  • -
  • Correlations and the quality of our results
  • -
  • Estimate of the error
  • +
  • The \( \chi^2 \) function
  • +
  • The \( \chi^2 \) function
  • +
  • The \( \chi^2 \) function
  • +
  • The \( \chi^2 \) function
  • The \( \chi^2 \) function
  • The \( \chi^2 \) function
  • -
  • The \( \chi^2 \) function
  • -
  • The \( \chi^2 \) function
  • -
  • The \( \chi^2 \) function
  • -
  • The \( \chi^2 \) function
  • -
  • Simple regression model with gradient descent
  • -
  • Simple regression model with stochastic gradient descent
  • -
  • Polynomial Regression
  • -
  • Ridge and Lasso Regression
  • -
  • The singular value decompostion
  • +
  • Simple regression model
  • +
  • Simple regression model, now using scikit-learn
  • +
  • Simple linear regression model using scikit-learn
  • +
  • Simple linear regression model
  • +
  • Less noise
  • +
  • How to study our fits
  • +
  • Minimizing the cost function
  • +
  • Relative error
  • +
  • The richness of scikit-learn
  • +
  • Functions in scikit-learn
  • +
  • Other functions in scikit-learn
  • +
  • The mean absolute error and other functions in scikit-learn
  • +
  • Cubic polynomial in scikit-learn
  • +
  • Simple regression model with gradient descent
  • +
  • Simple regression model with stochastic gradient descent
  • +
  • Polynomial Regression
  • +
  • Ridge and Lasso Regression
  • +
  • The singular value decompostion
  • +
  • Lasso and Ridge regression
  • +
  • Logistic regression
  • @@ -240,7 +265,7 @@ $$
  • 18
  • 19
  • ...
  • -
  • 27
  • +
  • 38
  • »
  • diff --git a/doc/pub/Regression/html/._Regression-bs010.html b/doc/pub/Regression/html/._Regression-bs010.html index 4608aaefc..a9e44dede 100644 --- a/doc/pub/Regression/html/._Regression-bs010.html +++ b/doc/pub/Regression/html/._Regression-bs010.html @@ -73,33 +73,47 @@ Automatically generated HTML file from DocOnce source 2, None, '___sec10'), - ('Simple regression model', 2, None, '___sec11'), + ('The $\\chi^2$ function', 2, None, '___sec11'), + ('The $\\chi^2$ function', 2, None, '___sec12'), + ('The $\\chi^2$ function', 2, None, '___sec13'), + ('The $\\chi^2$ function', 2, None, '___sec14'), + ('The $\\chi^2$ function', 2, None, '___sec15'), + ('The $\\chi^2$ function', 2, None, '___sec16'), + ('Simple regression model', 2, None, '___sec17'), ('Simple regression model, now using _scikit-learn_', 2, None, - '___sec12'), - ('Correlations and the quality of our results', + '___sec18'), + ('Simple linear regression model using _scikit-learn_', 2, None, - '___sec13'), - ('Estimate of the error', 2, None, '___sec14'), - ('The $\\chi^2$ function', 2, None, '___sec15'), - ('The $\\chi^2$ function', 2, None, '___sec16'), - ('The $\\chi^2$ function', 2, None, '___sec17'), - ('The $\\chi^2$ function', 2, None, '___sec18'), - ('The $\\chi^2$ function', 2, None, '___sec19'), - ('The $\\chi^2$ function', 2, None, '___sec20'), + '___sec19'), + ('Simple linear regression model', 2, None, '___sec20'), + ('Less noise', 2, None, '___sec21'), + ('How to study our fits', 2, None, '___sec22'), + ('Minimizing the cost function', 2, None, '___sec23'), + ('Relative error', 2, None, '___sec24'), + ('The richness of _scikit-learn_', 2, None, '___sec25'), + ('Functions in _scikit-learn_', 2, None, '___sec26'), + ('Other functions in _scikit-learn_', 2, None, '___sec27'), + ('The mean absolute error and other functions in _scikit-learn_', + 2, + None, + '___sec28'), + ('Cubic polynomial in _scikit-learn_', 2, None, '___sec29'), ('Simple regression model with gradient descent', 2, None, - '___sec21'), + '___sec30'), ('Simple regression model with stochastic gradient descent', 2, None, - '___sec22'), - ('Polynomial Regression', 2, None, '___sec23'), - ('Ridge and Lasso Regression', 2, None, '___sec24'), - ('The singular value decompostion', 2, None, '___sec25')]} + '___sec31'), + ('Polynomial Regression', 2, None, '___sec32'), + ('Ridge and Lasso Regression', 2, None, '___sec33'), + ('The singular value decompostion', 2, None, '___sec34'), + ('Lasso and Ridge regression', 2, None, '___sec35'), + ('Logistic regression', 2, None, '___sec36')]} end of tocinfo --> @@ -148,21 +162,32 @@ MathJax.Hub.Config({
  • Interpretations and optimizing our parameters
  • Interpretations and optimizing our parameters
  • Interpretations and optimizing our parameters
  • -
  • Simple regression model
  • -
  • Simple regression model, now using scikit-learn
  • -
  • Correlations and the quality of our results
  • -
  • Estimate of the error
  • +
  • The \( \chi^2 \) function
  • +
  • The \( \chi^2 \) function
  • +
  • The \( \chi^2 \) function
  • +
  • The \( \chi^2 \) function
  • The \( \chi^2 \) function
  • The \( \chi^2 \) function
  • -
  • The \( \chi^2 \) function
  • -
  • The \( \chi^2 \) function
  • -
  • The \( \chi^2 \) function
  • -
  • The \( \chi^2 \) function
  • -
  • Simple regression model with gradient descent
  • -
  • Simple regression model with stochastic gradient descent
  • -
  • Polynomial Regression
  • -
  • Ridge and Lasso Regression
  • -
  • The singular value decompostion
  • +
  • Simple regression model
  • +
  • Simple regression model, now using scikit-learn
  • +
  • Simple linear regression model using scikit-learn
  • +
  • Simple linear regression model
  • +
  • Less noise
  • +
  • How to study our fits
  • +
  • Minimizing the cost function
  • +
  • Relative error
  • +
  • The richness of scikit-learn
  • +
  • Functions in scikit-learn
  • +
  • Other functions in scikit-learn
  • +
  • The mean absolute error and other functions in scikit-learn
  • +
  • Cubic polynomial in scikit-learn
  • +
  • Simple regression model with gradient descent
  • +
  • Simple regression model with stochastic gradient descent
  • +
  • Polynomial Regression
  • +
  • Ridge and Lasso Regression
  • +
  • The singular value decompostion
  • +
  • Lasso and Ridge regression
  • +
  • Logistic regression
  • @@ -228,7 +253,7 @@ $$
  • 19
  • 20
  • ...
  • -
  • 27
  • +
  • 38
  • »
  • diff --git a/doc/pub/Regression/html/._Regression-bs011.html b/doc/pub/Regression/html/._Regression-bs011.html index 2629b4121..d48d2e413 100644 --- a/doc/pub/Regression/html/._Regression-bs011.html +++ b/doc/pub/Regression/html/._Regression-bs011.html @@ -73,33 +73,47 @@ Automatically generated HTML file from DocOnce source 2, None, '___sec10'), - ('Simple regression model', 2, None, '___sec11'), + ('The $\\chi^2$ function', 2, None, '___sec11'), + ('The $\\chi^2$ function', 2, None, '___sec12'), + ('The $\\chi^2$ function', 2, None, '___sec13'), + ('The $\\chi^2$ function', 2, None, '___sec14'), + ('The $\\chi^2$ function', 2, None, '___sec15'), + ('The $\\chi^2$ function', 2, None, '___sec16'), + ('Simple regression model', 2, None, '___sec17'), ('Simple regression model, now using _scikit-learn_', 2, None, - '___sec12'), - ('Correlations and the quality of our results', + '___sec18'), + ('Simple linear regression model using _scikit-learn_', 2, None, - '___sec13'), - ('Estimate of the error', 2, None, '___sec14'), - ('The $\\chi^2$ function', 2, None, '___sec15'), - ('The $\\chi^2$ function', 2, None, '___sec16'), - ('The $\\chi^2$ function', 2, None, '___sec17'), - ('The $\\chi^2$ function', 2, None, '___sec18'), - ('The $\\chi^2$ function', 2, None, '___sec19'), - ('The $\\chi^2$ function', 2, None, '___sec20'), + '___sec19'), + ('Simple linear regression model', 2, None, '___sec20'), + ('Less noise', 2, None, '___sec21'), + ('How to study our fits', 2, None, '___sec22'), + ('Minimizing the cost function', 2, None, '___sec23'), + ('Relative error', 2, None, '___sec24'), + ('The richness of _scikit-learn_', 2, None, '___sec25'), + ('Functions in _scikit-learn_', 2, None, '___sec26'), + ('Other functions in _scikit-learn_', 2, None, '___sec27'), + ('The mean absolute error and other functions in _scikit-learn_', + 2, + None, + '___sec28'), + ('Cubic polynomial in _scikit-learn_', 2, None, '___sec29'), ('Simple regression model with gradient descent', 2, None, - '___sec21'), + '___sec30'), ('Simple regression model with stochastic gradient descent', 2, None, - '___sec22'), - ('Polynomial Regression', 2, None, '___sec23'), - ('Ridge and Lasso Regression', 2, None, '___sec24'), - ('The singular value decompostion', 2, None, '___sec25')]} + '___sec31'), + ('Polynomial Regression', 2, None, '___sec32'), + ('Ridge and Lasso Regression', 2, None, '___sec33'), + ('The singular value decompostion', 2, None, '___sec34'), + ('Lasso and Ridge regression', 2, None, '___sec35'), + ('Logistic regression', 2, None, '___sec36')]} end of tocinfo --> @@ -148,21 +162,32 @@ MathJax.Hub.Config({
  • Interpretations and optimizing our parameters
  • Interpretations and optimizing our parameters
  • Interpretations and optimizing our parameters
  • -
  • Simple regression model
  • -
  • Simple regression model, now using scikit-learn
  • -
  • Correlations and the quality of our results
  • -
  • Estimate of the error
  • +
  • The \( \chi^2 \) function
  • +
  • The \( \chi^2 \) function
  • +
  • The \( \chi^2 \) function
  • +
  • The \( \chi^2 \) function
  • The \( \chi^2 \) function
  • The \( \chi^2 \) function
  • -
  • The \( \chi^2 \) function
  • -
  • The \( \chi^2 \) function
  • -
  • The \( \chi^2 \) function
  • -
  • The \( \chi^2 \) function
  • -
  • Simple regression model with gradient descent
  • -
  • Simple regression model with stochastic gradient descent
  • -
  • Polynomial Regression
  • -
  • Ridge and Lasso Regression
  • -
  • The singular value decompostion
  • +
  • Simple regression model
  • +
  • Simple regression model, now using scikit-learn
  • +
  • Simple linear regression model using scikit-learn
  • +
  • Simple linear regression model
  • +
  • Less noise
  • +
  • How to study our fits
  • +
  • Minimizing the cost function
  • +
  • Relative error
  • +
  • The richness of scikit-learn
  • +
  • Functions in scikit-learn
  • +
  • Other functions in scikit-learn
  • +
  • The mean absolute error and other functions in scikit-learn
  • +
  • Cubic polynomial in scikit-learn
  • +
  • Simple regression model with gradient descent
  • +
  • Simple regression model with stochastic gradient descent
  • +
  • Polynomial Regression
  • +
  • Ridge and Lasso Regression
  • +
  • The singular value decompostion
  • +
  • Lasso and Ridge regression
  • +
  • Logistic regression
  • @@ -230,7 +255,7 @@ meaning that the solution for \( \hat{\beta} \) is the one which minimizes the r
  • 20
  • 21
  • ...
  • -
  • 27
  • +
  • 38
  • »
  • diff --git a/doc/pub/Regression/html/._Regression-bs012.html b/doc/pub/Regression/html/._Regression-bs012.html index c69caa490..70f805206 100644 --- a/doc/pub/Regression/html/._Regression-bs012.html +++ b/doc/pub/Regression/html/._Regression-bs012.html @@ -73,33 +73,47 @@ Automatically generated HTML file from DocOnce source 2, None, '___sec10'), - ('Simple regression model', 2, None, '___sec11'), + ('The $\\chi^2$ function', 2, None, '___sec11'), + ('The $\\chi^2$ function', 2, None, '___sec12'), + ('The $\\chi^2$ function', 2, None, '___sec13'), + ('The $\\chi^2$ function', 2, None, '___sec14'), + ('The $\\chi^2$ function', 2, None, '___sec15'), + ('The $\\chi^2$ function', 2, None, '___sec16'), + ('Simple regression model', 2, None, '___sec17'), ('Simple regression model, now using _scikit-learn_', 2, None, - '___sec12'), - ('Correlations and the quality of our results', + '___sec18'), + ('Simple linear regression model using _scikit-learn_', 2, None, - '___sec13'), - ('Estimate of the error', 2, None, '___sec14'), - ('The $\\chi^2$ function', 2, None, '___sec15'), - ('The $\\chi^2$ function', 2, None, '___sec16'), - ('The $\\chi^2$ function', 2, None, '___sec17'), - ('The $\\chi^2$ function', 2, None, '___sec18'), - ('The $\\chi^2$ function', 2, None, '___sec19'), - ('The $\\chi^2$ function', 2, None, '___sec20'), + '___sec19'), + ('Simple linear regression model', 2, None, '___sec20'), + ('Less noise', 2, None, '___sec21'), + ('How to study our fits', 2, None, '___sec22'), + ('Minimizing the cost function', 2, None, '___sec23'), + ('Relative error', 2, None, '___sec24'), + ('The richness of _scikit-learn_', 2, None, '___sec25'), + ('Functions in _scikit-learn_', 2, None, '___sec26'), + ('Other functions in _scikit-learn_', 2, None, '___sec27'), + ('The mean absolute error and other functions in _scikit-learn_', + 2, + None, + '___sec28'), + ('Cubic polynomial in _scikit-learn_', 2, None, '___sec29'), ('Simple regression model with gradient descent', 2, None, - '___sec21'), + '___sec30'), ('Simple regression model with stochastic gradient descent', 2, None, - '___sec22'), - ('Polynomial Regression', 2, None, '___sec23'), - ('Ridge and Lasso Regression', 2, None, '___sec24'), - ('The singular value decompostion', 2, None, '___sec25')]} + '___sec31'), + ('Polynomial Regression', 2, None, '___sec32'), + ('Ridge and Lasso Regression', 2, None, '___sec33'), + ('The singular value decompostion', 2, None, '___sec34'), + ('Lasso and Ridge regression', 2, None, '___sec35'), + ('Logistic regression', 2, None, '___sec36')]} end of tocinfo --> @@ -148,21 +162,32 @@ MathJax.Hub.Config({
  • Interpretations and optimizing our parameters
  • Interpretations and optimizing our parameters
  • Interpretations and optimizing our parameters
  • -
  • Simple regression model
  • -
  • Simple regression model, now using scikit-learn
  • -
  • Correlations and the quality of our results
  • -
  • Estimate of the error
  • +
  • The \( \chi^2 \) function
  • +
  • The \( \chi^2 \) function
  • +
  • The \( \chi^2 \) function
  • +
  • The \( \chi^2 \) function
  • The \( \chi^2 \) function
  • The \( \chi^2 \) function
  • -
  • The \( \chi^2 \) function
  • -
  • The \( \chi^2 \) function
  • -
  • The \( \chi^2 \) function
  • -
  • The \( \chi^2 \) function
  • -
  • Simple regression model with gradient descent
  • -
  • Simple regression model with stochastic gradient descent
  • -
  • Polynomial Regression
  • -
  • Ridge and Lasso Regression
  • -
  • The singular value decompostion
  • +
  • Simple regression model
  • +
  • Simple regression model, now using scikit-learn
  • +
  • Simple linear regression model using scikit-learn
  • +
  • Simple linear regression model
  • +
  • Less noise
  • +
  • How to study our fits
  • +
  • Minimizing the cost function
  • +
  • Relative error
  • +
  • The richness of scikit-learn
  • +
  • Functions in scikit-learn
  • +
  • Other functions in scikit-learn
  • +
  • The mean absolute error and other functions in scikit-learn
  • +
  • Cubic polynomial in scikit-learn
  • +
  • Simple regression model with gradient descent
  • +
  • Simple regression model with stochastic gradient descent
  • +
  • Polynomial Regression
  • +
  • Ridge and Lasso Regression
  • +
  • The singular value decompostion
  • +
  • Lasso and Ridge regression
  • +
  • Logistic regression
  • @@ -178,42 +203,26 @@ MathJax.Hub.Config({ -

    Simple regression model

    -We are now ready to write our first program which aims at solving the above linear regression equations. We start with data we have produced ourselves, in this case normally distributed random numbers along the \( x \)-axis. These numbers define then the value of a function \( y(x)=4+3x+N(0,1) \). Thereafter we order the \( x \) values and employ our linear regression algorithm to set up the best fit. Here we find it useful to use the numpy function \( c\_ \) arrays where arrays are stacked along their last axis after being upgraded to at least two dimensions with ones post-pended to the shape. The following examples help in understanding what happens +

    The \( \chi^2 \) function

    +
    +
    +

    +

    +Normally, the response (dependent or outcome) variable \( y_i \) the outcome of a numerical experiment or another type of experiment and is thus only an approximation to the true value. It is then always accompanied by an error estimate, often limited to a statistical error estimate given by the standard deviation discussed earlier. In the discussion here we will treat \( y_i \) as our exact value for the response variable. - -

    import numpy as np
    -print(np.c_[np.array([1,2,3]), np.array([4,5,6])])
    -print(np.c_[np.array([[1,2,3]]), 0, 0, np.array([[4,5,6]])])
    -

    +Introducing the standard deviation \( \sigma_i \) for each measurement \( y_i \), we define now the \( \chi^2 \) function as +$$ +\chi^2(\hat{\beta})=\sum_{i=0}^{n-1}\frac{\left(y_i-\tilde{y}_i\right)^2}{\sigma_i^2}=\left(\hat{y}-\hat{\tilde{y}}\right)^T\frac{1}{\hat{\Sigma^2}}\left(\hat{y}-\hat{\tilde{y}}\right), +$$ - -

    # Importing various packages
    -from random import random, seed
    -import numpy as np
    -import matplotlib.pyplot as plt
    +where the matrix \( \hat{\Sigma} \) is a diagonal matrix with \( \sigma_i \) as matrix elements.
     
    -x = 2*np.random.rand(100,1)
    -y = 4+3*x+np.random.randn(100,1)
    -
    -xb = np.c_[np.ones((100,1)), x]
    -theta = np.linalg.inv(xb.T.dot(xb)).dot(xb.T).dot(y)
    -xnew = np.array([[0],[2]])
    -xbnew = np.c_[np.ones((2,1)), xnew]
    -ypredict = xbnew.dot(theta)
    -
    -plt.plot(xnew, ypredict, "r-")
    -plt.plot(x, y ,'ro')
    -plt.axis([0,2.0,0, 15.0])
    -plt.xlabel(r'$x$')
    -plt.ylabel(r'$y$')
    -plt.title(r'Linear Regression')
    -plt.show()
    -

    -We see that, as expected, a linear fit gives a seemingly (from the graph) good representation of the data. +

    +
    +

    @@ -241,7 +250,7 @@ We see that, as expected, a linear fit gives a seemingly (from the graph) good r

  • 21
  • 22
  • ...
  • -
  • 27
  • +
  • 38
  • »
  • diff --git a/doc/pub/Regression/html/._Regression-bs013.html b/doc/pub/Regression/html/._Regression-bs013.html index 16678c484..89a242dec 100644 --- a/doc/pub/Regression/html/._Regression-bs013.html +++ b/doc/pub/Regression/html/._Regression-bs013.html @@ -73,33 +73,47 @@ Automatically generated HTML file from DocOnce source 2, None, '___sec10'), - ('Simple regression model', 2, None, '___sec11'), + ('The $\\chi^2$ function', 2, None, '___sec11'), + ('The $\\chi^2$ function', 2, None, '___sec12'), + ('The $\\chi^2$ function', 2, None, '___sec13'), + ('The $\\chi^2$ function', 2, None, '___sec14'), + ('The $\\chi^2$ function', 2, None, '___sec15'), + ('The $\\chi^2$ function', 2, None, '___sec16'), + ('Simple regression model', 2, None, '___sec17'), ('Simple regression model, now using _scikit-learn_', 2, None, - '___sec12'), - ('Correlations and the quality of our results', + '___sec18'), + ('Simple linear regression model using _scikit-learn_', 2, None, - '___sec13'), - ('Estimate of the error', 2, None, '___sec14'), - ('The $\\chi^2$ function', 2, None, '___sec15'), - ('The $\\chi^2$ function', 2, None, '___sec16'), - ('The $\\chi^2$ function', 2, None, '___sec17'), - ('The $\\chi^2$ function', 2, None, '___sec18'), - ('The $\\chi^2$ function', 2, None, '___sec19'), - ('The $\\chi^2$ function', 2, None, '___sec20'), + '___sec19'), + ('Simple linear regression model', 2, None, '___sec20'), + ('Less noise', 2, None, '___sec21'), + ('How to study our fits', 2, None, '___sec22'), + ('Minimizing the cost function', 2, None, '___sec23'), + ('Relative error', 2, None, '___sec24'), + ('The richness of _scikit-learn_', 2, None, '___sec25'), + ('Functions in _scikit-learn_', 2, None, '___sec26'), + ('Other functions in _scikit-learn_', 2, None, '___sec27'), + ('The mean absolute error and other functions in _scikit-learn_', + 2, + None, + '___sec28'), + ('Cubic polynomial in _scikit-learn_', 2, None, '___sec29'), ('Simple regression model with gradient descent', 2, None, - '___sec21'), + '___sec30'), ('Simple regression model with stochastic gradient descent', 2, None, - '___sec22'), - ('Polynomial Regression', 2, None, '___sec23'), - ('Ridge and Lasso Regression', 2, None, '___sec24'), - ('The singular value decompostion', 2, None, '___sec25')]} + '___sec31'), + ('Polynomial Regression', 2, None, '___sec32'), + ('Ridge and Lasso Regression', 2, None, '___sec33'), + ('The singular value decompostion', 2, None, '___sec34'), + ('Lasso and Ridge regression', 2, None, '___sec35'), + ('Logistic regression', 2, None, '___sec36')]} end of tocinfo --> @@ -148,21 +162,32 @@ MathJax.Hub.Config({
  • Interpretations and optimizing our parameters
  • Interpretations and optimizing our parameters
  • Interpretations and optimizing our parameters
  • -
  • Simple regression model
  • -
  • Simple regression model, now using scikit-learn
  • -
  • Correlations and the quality of our results
  • -
  • Estimate of the error
  • +
  • The \( \chi^2 \) function
  • +
  • The \( \chi^2 \) function
  • +
  • The \( \chi^2 \) function
  • +
  • The \( \chi^2 \) function
  • The \( \chi^2 \) function
  • The \( \chi^2 \) function
  • -
  • The \( \chi^2 \) function
  • -
  • The \( \chi^2 \) function
  • -
  • The \( \chi^2 \) function
  • -
  • The \( \chi^2 \) function
  • -
  • Simple regression model with gradient descent
  • -
  • Simple regression model with stochastic gradient descent
  • -
  • Polynomial Regression
  • -
  • Ridge and Lasso Regression
  • -
  • The singular value decompostion
  • +
  • Simple regression model
  • +
  • Simple regression model, now using scikit-learn
  • +
  • Simple linear regression model using scikit-learn
  • +
  • Simple linear regression model
  • +
  • Less noise
  • +
  • How to study our fits
  • +
  • Minimizing the cost function
  • +
  • Relative error
  • +
  • The richness of scikit-learn
  • +
  • Functions in scikit-learn
  • +
  • Other functions in scikit-learn
  • +
  • The mean absolute error and other functions in scikit-learn
  • +
  • Cubic polynomial in scikit-learn
  • +
  • Simple regression model with gradient descent
  • +
  • Simple regression model with stochastic gradient descent
  • +
  • Polynomial Regression
  • +
  • Ridge and Lasso Regression
  • +
  • The singular value decompostion
  • +
  • Lasso and Ridge regression
  • +
  • Logistic regression
  • @@ -178,34 +203,32 @@ MathJax.Hub.Config({ -

    Simple regression model, now using scikit-learn

    +

    The \( \chi^2 \) function

    +
    +
    +

    -We can repeat the above algorithm using scikit-learn as follows -

    +In order to find the parameters \( \beta_i \) we will then minimize the spread of \( \chi^2(\hat{\beta}) \) by requiring +$$ +\frac{\partial \chi^2(\hat{\beta})}{\partial \beta_j} = \frac{\partial }{\partial \beta_j}\left[ \sum_{i=0}^{n-1}\left(\frac{y_i-\beta_0x_{i,0}-\beta_1x_{i,1}-\beta_2x_{i,2}-\dots-\beta_{n-1}x_{i,n-1}}{\sigma_i}\right)^2\right]=0, +$$ - -

    # Importing various packages
    -from random import random, seed
    -import numpy as np
    -import matplotlib.pyplot as plt
    -from sklearn.linear_model import LinearRegression
    +which results in
    +$$
    +\frac{\partial \chi^2(\hat{\beta})}{\partial \beta_j} = -2\left[ \sum_{i=0}^{n-1}\frac{x_{ij}}{\sigma_i}\left(\frac{y_i-\beta_0x_{i,0}-\beta_1x_{i,1}-\beta_2x_{i,2}-\dots-\beta_{n-1}x_{i,n-1}}{\sigma_i}\right)\right]=0, 
    +$$
    +
    +or in a matrix-vector form as
    +$$
    +\frac{\partial \chi^2(\hat{\beta})}{\partial \hat{\beta}} = 0 = \hat{A}^T\left( \hat{b}-\hat{A}\hat{\beta}\right).  
    +$$
    +
    +where we have defined the matrix \( \hat{A} =\hat{X}/\hat{\Sigma} \) with matrix elements \( a_{ij} = x_{ij}/\sigma_i \) and the vector \( \hat{b} \) with elements \( b_i = y_i/\sigma_i \).
    +
    +
    -x = 2*np.random.rand(100,1) -y = 4+3*x+np.random.randn(100,1) -linreg = LinearRegression() -linreg.fit(x,y) -xnew = np.array([[0],[2]]) -ypredict = linreg.predict(xnew) -plt.plot(xnew, ypredict, "r-") -plt.plot(x, y ,'ro') -plt.axis([0,2.0,0, 15.0]) -plt.xlabel(r'$x$') -plt.ylabel(r'$y$') -plt.title(r'Random numbers ') -plt.show() -

    @@ -232,7 +255,7 @@ plt.show()

  • 22
  • 23
  • ...
  • -
  • 27
  • +
  • 38
  • »
  • diff --git a/doc/pub/Regression/html/._Regression-bs014.html b/doc/pub/Regression/html/._Regression-bs014.html index 322092dfe..896a5038a 100644 --- a/doc/pub/Regression/html/._Regression-bs014.html +++ b/doc/pub/Regression/html/._Regression-bs014.html @@ -73,33 +73,47 @@ Automatically generated HTML file from DocOnce source 2, None, '___sec10'), - ('Simple regression model', 2, None, '___sec11'), + ('The $\\chi^2$ function', 2, None, '___sec11'), + ('The $\\chi^2$ function', 2, None, '___sec12'), + ('The $\\chi^2$ function', 2, None, '___sec13'), + ('The $\\chi^2$ function', 2, None, '___sec14'), + ('The $\\chi^2$ function', 2, None, '___sec15'), + ('The $\\chi^2$ function', 2, None, '___sec16'), + ('Simple regression model', 2, None, '___sec17'), ('Simple regression model, now using _scikit-learn_', 2, None, - '___sec12'), - ('Correlations and the quality of our results', + '___sec18'), + ('Simple linear regression model using _scikit-learn_', 2, None, - '___sec13'), - ('Estimate of the error', 2, None, '___sec14'), - ('The $\\chi^2$ function', 2, None, '___sec15'), - ('The $\\chi^2$ function', 2, None, '___sec16'), - ('The $\\chi^2$ function', 2, None, '___sec17'), - ('The $\\chi^2$ function', 2, None, '___sec18'), - ('The $\\chi^2$ function', 2, None, '___sec19'), - ('The $\\chi^2$ function', 2, None, '___sec20'), + '___sec19'), + ('Simple linear regression model', 2, None, '___sec20'), + ('Less noise', 2, None, '___sec21'), + ('How to study our fits', 2, None, '___sec22'), + ('Minimizing the cost function', 2, None, '___sec23'), + ('Relative error', 2, None, '___sec24'), + ('The richness of _scikit-learn_', 2, None, '___sec25'), + ('Functions in _scikit-learn_', 2, None, '___sec26'), + ('Other functions in _scikit-learn_', 2, None, '___sec27'), + ('The mean absolute error and other functions in _scikit-learn_', + 2, + None, + '___sec28'), + ('Cubic polynomial in _scikit-learn_', 2, None, '___sec29'), ('Simple regression model with gradient descent', 2, None, - '___sec21'), + '___sec30'), ('Simple regression model with stochastic gradient descent', 2, None, - '___sec22'), - ('Polynomial Regression', 2, None, '___sec23'), - ('Ridge and Lasso Regression', 2, None, '___sec24'), - ('The singular value decompostion', 2, None, '___sec25')]} + '___sec31'), + ('Polynomial Regression', 2, None, '___sec32'), + ('Ridge and Lasso Regression', 2, None, '___sec33'), + ('The singular value decompostion', 2, None, '___sec34'), + ('Lasso and Ridge regression', 2, None, '___sec35'), + ('Logistic regression', 2, None, '___sec36')]} end of tocinfo --> @@ -148,21 +162,32 @@ MathJax.Hub.Config({
  • Interpretations and optimizing our parameters
  • Interpretations and optimizing our parameters
  • Interpretations and optimizing our parameters
  • -
  • Simple regression model
  • -
  • Simple regression model, now using scikit-learn
  • -
  • Correlations and the quality of our results
  • -
  • Estimate of the error
  • +
  • The \( \chi^2 \) function
  • +
  • The \( \chi^2 \) function
  • +
  • The \( \chi^2 \) function
  • +
  • The \( \chi^2 \) function
  • The \( \chi^2 \) function
  • The \( \chi^2 \) function
  • -
  • The \( \chi^2 \) function
  • -
  • The \( \chi^2 \) function
  • -
  • The \( \chi^2 \) function
  • -
  • The \( \chi^2 \) function
  • -
  • Simple regression model with gradient descent
  • -
  • Simple regression model with stochastic gradient descent
  • -
  • Polynomial Regression
  • -
  • Ridge and Lasso Regression
  • -
  • The singular value decompostion
  • +
  • Simple regression model
  • +
  • Simple regression model, now using scikit-learn
  • +
  • Simple linear regression model using scikit-learn
  • +
  • Simple linear regression model
  • +
  • Less noise
  • +
  • How to study our fits
  • +
  • Minimizing the cost function
  • +
  • Relative error
  • +
  • The richness of scikit-learn
  • +
  • Functions in scikit-learn
  • +
  • Other functions in scikit-learn
  • +
  • The mean absolute error and other functions in scikit-learn
  • +
  • Cubic polynomial in scikit-learn
  • +
  • Simple regression model with gradient descent
  • +
  • Simple regression model with stochastic gradient descent
  • +
  • Polynomial Regression
  • +
  • Ridge and Lasso Regression
  • +
  • The singular value decompostion
  • +
  • Lasso and Ridge regression
  • +
  • Logistic regression
  • @@ -178,16 +203,29 @@ MathJax.Hub.Config({ -

    Correlations and the quality of our results

    -In order to test the quality of our fit, there are several measures which can be implemented. One is the so-called -correlation function defined as -$$ -\mathrm{Corr}(X,Y) = \frac{\sum_{i=1}^n(x_i-\overline{x})(y_i-\overline{y})}{\sqrt{\sum_{i=1}^n(x_i-\overline{x})^2}\sqrt{\sum_{i=1}^n(y_i-\overline{y})^2} } -$$ +

    The \( \chi^2 \) function

    +
    +
    +

    -Another quantity is the autocorrelation function we discussed in our chapter on statistical analysis. -Let us now try to assess the quality of our fit by studying various measures. +We can rewrite +$$ +\frac{\partial \chi^2(\hat{\beta})}{\partial \hat{\beta}} = 0 = \hat{A}^T\left( \hat{b}-\hat{A}\hat{\beta}\right), +$$ + +as +$$ +\hat{A}^T\hat{b} = \hat{A}^T\hat{A}\hat{\beta}, +$$ + +and if the matrix \( \hat{A}^T\hat{A} \) is invertible we have the solution +$$ +\hat{\beta} =\left(\hat{A}^T\hat{A}\right)^{-1}\hat{A}^T\hat{b}. +$$ +

    +
    +

    @@ -215,7 +253,7 @@ Let us now try to assess the quality of our fit by studying various measures.

  • 23
  • 24
  • ...
  • -
  • 27
  • +
  • 38
  • »
  • diff --git a/doc/pub/Regression/html/._Regression-bs015.html b/doc/pub/Regression/html/._Regression-bs015.html index cc401c657..dd2dbedfa 100644 --- a/doc/pub/Regression/html/._Regression-bs015.html +++ b/doc/pub/Regression/html/._Regression-bs015.html @@ -73,33 +73,47 @@ Automatically generated HTML file from DocOnce source 2, None, '___sec10'), - ('Simple regression model', 2, None, '___sec11'), + ('The $\\chi^2$ function', 2, None, '___sec11'), + ('The $\\chi^2$ function', 2, None, '___sec12'), + ('The $\\chi^2$ function', 2, None, '___sec13'), + ('The $\\chi^2$ function', 2, None, '___sec14'), + ('The $\\chi^2$ function', 2, None, '___sec15'), + ('The $\\chi^2$ function', 2, None, '___sec16'), + ('Simple regression model', 2, None, '___sec17'), ('Simple regression model, now using _scikit-learn_', 2, None, - '___sec12'), - ('Correlations and the quality of our results', + '___sec18'), + ('Simple linear regression model using _scikit-learn_', 2, None, - '___sec13'), - ('Estimate of the error', 2, None, '___sec14'), - ('The $\\chi^2$ function', 2, None, '___sec15'), - ('The $\\chi^2$ function', 2, None, '___sec16'), - ('The $\\chi^2$ function', 2, None, '___sec17'), - ('The $\\chi^2$ function', 2, None, '___sec18'), - ('The $\\chi^2$ function', 2, None, '___sec19'), - ('The $\\chi^2$ function', 2, None, '___sec20'), + '___sec19'), + ('Simple linear regression model', 2, None, '___sec20'), + ('Less noise', 2, None, '___sec21'), + ('How to study our fits', 2, None, '___sec22'), + ('Minimizing the cost function', 2, None, '___sec23'), + ('Relative error', 2, None, '___sec24'), + ('The richness of _scikit-learn_', 2, None, '___sec25'), + ('Functions in _scikit-learn_', 2, None, '___sec26'), + ('Other functions in _scikit-learn_', 2, None, '___sec27'), + ('The mean absolute error and other functions in _scikit-learn_', + 2, + None, + '___sec28'), + ('Cubic polynomial in _scikit-learn_', 2, None, '___sec29'), ('Simple regression model with gradient descent', 2, None, - '___sec21'), + '___sec30'), ('Simple regression model with stochastic gradient descent', 2, None, - '___sec22'), - ('Polynomial Regression', 2, None, '___sec23'), - ('Ridge and Lasso Regression', 2, None, '___sec24'), - ('The singular value decompostion', 2, None, '___sec25')]} + '___sec31'), + ('Polynomial Regression', 2, None, '___sec32'), + ('Ridge and Lasso Regression', 2, None, '___sec33'), + ('The singular value decompostion', 2, None, '___sec34'), + ('Lasso and Ridge regression', 2, None, '___sec35'), + ('Logistic regression', 2, None, '___sec36')]} end of tocinfo --> @@ -148,21 +162,32 @@ MathJax.Hub.Config({
  • Interpretations and optimizing our parameters
  • Interpretations and optimizing our parameters
  • Interpretations and optimizing our parameters
  • -
  • Simple regression model
  • -
  • Simple regression model, now using scikit-learn
  • -
  • Correlations and the quality of our results
  • -
  • Estimate of the error
  • +
  • The \( \chi^2 \) function
  • +
  • The \( \chi^2 \) function
  • +
  • The \( \chi^2 \) function
  • +
  • The \( \chi^2 \) function
  • The \( \chi^2 \) function
  • The \( \chi^2 \) function
  • -
  • The \( \chi^2 \) function
  • -
  • The \( \chi^2 \) function
  • -
  • The \( \chi^2 \) function
  • -
  • The \( \chi^2 \) function
  • -
  • Simple regression model with gradient descent
  • -
  • Simple regression model with stochastic gradient descent
  • -
  • Polynomial Regression
  • -
  • Ridge and Lasso Regression
  • -
  • The singular value decompostion
  • +
  • Simple regression model
  • +
  • Simple regression model, now using scikit-learn
  • +
  • Simple linear regression model using scikit-learn
  • +
  • Simple linear regression model
  • +
  • Less noise
  • +
  • How to study our fits
  • +
  • Minimizing the cost function
  • +
  • Relative error
  • +
  • The richness of scikit-learn
  • +
  • Functions in scikit-learn
  • +
  • Other functions in scikit-learn
  • +
  • The mean absolute error and other functions in scikit-learn
  • +
  • Cubic polynomial in scikit-learn
  • +
  • Simple regression model with gradient descent
  • +
  • Simple regression model with stochastic gradient descent
  • +
  • Polynomial Regression
  • +
  • Ridge and Lasso Regression
  • +
  • The singular value decompostion
  • +
  • Lasso and Ridge regression
  • +
  • Logistic regression
  • @@ -178,7 +203,34 @@ MathJax.Hub.Config({ -

    Estimate of the error

    +

    The \( \chi^2 \) function

    +
    +
    +

    + +

    +If we then introduce the matrix +$$ +\hat{H} = \left(\hat{A}^T\hat{A}\right)^{-1}, +$$ + +we have then the following expression for the parameters \( \beta_j \) (the matrix elements of \( \hat{H} \) are \( h_{ij} \)) +$$ +\beta_j = \sum_{k=0}^{p-1}h_{jk}\sum_{i=0}^{n-1}\frac{y_i}{\sigma_i}\frac{x_{ik}}{\sigma_i} = \sum_{k=0}^{p-1}h_{jk}\sum_{i=0}^{n-1}b_ia_{ik} +$$ + +We state without proof the expression for the uncertainty in the parameters \( \beta_j \) as (we leave this as an exercise) +$$ +\sigma^2(\beta_j) = \sum_{i=0}^{n-1}\sigma_i^2\left( \frac{\partial \beta_j}{\partial y_i}\right)^2, +$$ + +resulting in +$$ +\sigma^2(\beta_j) = \left(\sum_{k=0}^{p-1}h_{jk}\sum_{i=0}^{n-1}a_{ik}\right)\left(\sum_{l=0}^{p-1}h_{jl}\sum_{m=0}^{n-1}a_{ml}\right) = h_{jj}! +$$ +

    +
    +

    @@ -206,7 +258,7 @@ MathJax.Hub.Config({

  • 24
  • 25
  • ...
  • -
  • 27
  • +
  • 38
  • »
  • diff --git a/doc/pub/Regression/html/._Regression-bs016.html b/doc/pub/Regression/html/._Regression-bs016.html index 0214bc199..6a8765bee 100644 --- a/doc/pub/Regression/html/._Regression-bs016.html +++ b/doc/pub/Regression/html/._Regression-bs016.html @@ -73,33 +73,47 @@ Automatically generated HTML file from DocOnce source 2, None, '___sec10'), - ('Simple regression model', 2, None, '___sec11'), + ('The $\\chi^2$ function', 2, None, '___sec11'), + ('The $\\chi^2$ function', 2, None, '___sec12'), + ('The $\\chi^2$ function', 2, None, '___sec13'), + ('The $\\chi^2$ function', 2, None, '___sec14'), + ('The $\\chi^2$ function', 2, None, '___sec15'), + ('The $\\chi^2$ function', 2, None, '___sec16'), + ('Simple regression model', 2, None, '___sec17'), ('Simple regression model, now using _scikit-learn_', 2, None, - '___sec12'), - ('Correlations and the quality of our results', + '___sec18'), + ('Simple linear regression model using _scikit-learn_', 2, None, - '___sec13'), - ('Estimate of the error', 2, None, '___sec14'), - ('The $\\chi^2$ function', 2, None, '___sec15'), - ('The $\\chi^2$ function', 2, None, '___sec16'), - ('The $\\chi^2$ function', 2, None, '___sec17'), - ('The $\\chi^2$ function', 2, None, '___sec18'), - ('The $\\chi^2$ function', 2, None, '___sec19'), - ('The $\\chi^2$ function', 2, None, '___sec20'), + '___sec19'), + ('Simple linear regression model', 2, None, '___sec20'), + ('Less noise', 2, None, '___sec21'), + ('How to study our fits', 2, None, '___sec22'), + ('Minimizing the cost function', 2, None, '___sec23'), + ('Relative error', 2, None, '___sec24'), + ('The richness of _scikit-learn_', 2, None, '___sec25'), + ('Functions in _scikit-learn_', 2, None, '___sec26'), + ('Other functions in _scikit-learn_', 2, None, '___sec27'), + ('The mean absolute error and other functions in _scikit-learn_', + 2, + None, + '___sec28'), + ('Cubic polynomial in _scikit-learn_', 2, None, '___sec29'), ('Simple regression model with gradient descent', 2, None, - '___sec21'), + '___sec30'), ('Simple regression model with stochastic gradient descent', 2, None, - '___sec22'), - ('Polynomial Regression', 2, None, '___sec23'), - ('Ridge and Lasso Regression', 2, None, '___sec24'), - ('The singular value decompostion', 2, None, '___sec25')]} + '___sec31'), + ('Polynomial Regression', 2, None, '___sec32'), + ('Ridge and Lasso Regression', 2, None, '___sec33'), + ('The singular value decompostion', 2, None, '___sec34'), + ('Lasso and Ridge regression', 2, None, '___sec35'), + ('Logistic regression', 2, None, '___sec36')]} end of tocinfo --> @@ -148,21 +162,32 @@ MathJax.Hub.Config({
  • Interpretations and optimizing our parameters
  • Interpretations and optimizing our parameters
  • Interpretations and optimizing our parameters
  • -
  • Simple regression model
  • -
  • Simple regression model, now using scikit-learn
  • -
  • Correlations and the quality of our results
  • -
  • Estimate of the error
  • +
  • The \( \chi^2 \) function
  • +
  • The \( \chi^2 \) function
  • +
  • The \( \chi^2 \) function
  • +
  • The \( \chi^2 \) function
  • The \( \chi^2 \) function
  • The \( \chi^2 \) function
  • -
  • The \( \chi^2 \) function
  • -
  • The \( \chi^2 \) function
  • -
  • The \( \chi^2 \) function
  • -
  • The \( \chi^2 \) function
  • -
  • Simple regression model with gradient descent
  • -
  • Simple regression model with stochastic gradient descent
  • -
  • Polynomial Regression
  • -
  • Ridge and Lasso Regression
  • -
  • The singular value decompostion
  • +
  • Simple regression model
  • +
  • Simple regression model, now using scikit-learn
  • +
  • Simple linear regression model using scikit-learn
  • +
  • Simple linear regression model
  • +
  • Less noise
  • +
  • How to study our fits
  • +
  • Minimizing the cost function
  • +
  • Relative error
  • +
  • The richness of scikit-learn
  • +
  • Functions in scikit-learn
  • +
  • Other functions in scikit-learn
  • +
  • The mean absolute error and other functions in scikit-learn
  • +
  • Cubic polynomial in scikit-learn
  • +
  • Simple regression model with gradient descent
  • +
  • Simple regression model with stochastic gradient descent
  • +
  • Polynomial Regression
  • +
  • Ridge and Lasso Regression
  • +
  • The singular value decompostion
  • +
  • Lasso and Ridge regression
  • +
  • Logistic regression
  • @@ -182,19 +207,20 @@ MathJax.Hub.Config({

    - -

    -Normally, the response (dependent or outcome) variable \( y_i \) the outcome of a numerical experiment or another type of experiment and is thus only an approximation to the true value. It is then always accompanied by an error estimate, often limited to a statistical error estimate given by the standard deviation discussed earlier. In the discussion here we will treat \( y_i \) as our exact value for the response variable. - -

    -Introducing the standard deviation \( \sigma_i \) for each measurement \( y_i \), we define now the \( \chi^2 \) function as +The first step here is to approximate the function \( y \) with a first-order polynomial, that is we write $$ -\chi^2(\hat{\beta})=\sum_{i=0}^{n-1}\frac{\left(y_i-\tilde{y}_i\right)^2}{\sigma_i^2}=\left(\hat{y}-\hat{\tilde{y}}\right)^T\frac{1}{\hat{\Sigma^2}}\left(\hat{y}-\hat{\tilde{y}}\right), +y=y(x) \rightarrow y(x_i) \approx \beta_0+\beta_1 x_i. $$ -where the matrix \( \hat{\Sigma} \) is a diagonal matrix with \( \sigma_i \) as matrix elements. +By computing the derivatives of \( \chi^2 \) with respect to \( \beta_0 \) and \( \beta_1 \) show that these are given by +$$ +\frac{\partial \chi^2(\hat{\beta})}{\partial \beta_0} = -2\left[ \sum_{i=0}^{n-1}\left(\frac{y_i-\beta_0-\beta_1x_{i}}{\sigma_i^2}\right)\right]=0, +$$ -

    +and +$$ +\frac{\partial \chi^2(\hat{\beta})}{\partial \beta_0} = -2\left[ \sum_{i=0}^{n-1}x_i\left(\frac{y_i-\beta_0-\beta_1x_{i}}{\sigma_i^2}\right)\right]=0. +$$

    @@ -225,7 +251,7 @@ where the matrix \( \hat{\Sigma} \) is a diagonal matrix with \( \sigma_i \) as
  • 25
  • 26
  • ...
  • -
  • 27
  • +
  • 38
  • »
  • diff --git a/doc/pub/Regression/html/._Regression-bs017.html b/doc/pub/Regression/html/._Regression-bs017.html index 1310455a9..1ba836c85 100644 --- a/doc/pub/Regression/html/._Regression-bs017.html +++ b/doc/pub/Regression/html/._Regression-bs017.html @@ -73,33 +73,47 @@ Automatically generated HTML file from DocOnce source 2, None, '___sec10'), - ('Simple regression model', 2, None, '___sec11'), + ('The $\\chi^2$ function', 2, None, '___sec11'), + ('The $\\chi^2$ function', 2, None, '___sec12'), + ('The $\\chi^2$ function', 2, None, '___sec13'), + ('The $\\chi^2$ function', 2, None, '___sec14'), + ('The $\\chi^2$ function', 2, None, '___sec15'), + ('The $\\chi^2$ function', 2, None, '___sec16'), + ('Simple regression model', 2, None, '___sec17'), ('Simple regression model, now using _scikit-learn_', 2, None, - '___sec12'), - ('Correlations and the quality of our results', + '___sec18'), + ('Simple linear regression model using _scikit-learn_', 2, None, - '___sec13'), - ('Estimate of the error', 2, None, '___sec14'), - ('The $\\chi^2$ function', 2, None, '___sec15'), - ('The $\\chi^2$ function', 2, None, '___sec16'), - ('The $\\chi^2$ function', 2, None, '___sec17'), - ('The $\\chi^2$ function', 2, None, '___sec18'), - ('The $\\chi^2$ function', 2, None, '___sec19'), - ('The $\\chi^2$ function', 2, None, '___sec20'), + '___sec19'), + ('Simple linear regression model', 2, None, '___sec20'), + ('Less noise', 2, None, '___sec21'), + ('How to study our fits', 2, None, '___sec22'), + ('Minimizing the cost function', 2, None, '___sec23'), + ('Relative error', 2, None, '___sec24'), + ('The richness of _scikit-learn_', 2, None, '___sec25'), + ('Functions in _scikit-learn_', 2, None, '___sec26'), + ('Other functions in _scikit-learn_', 2, None, '___sec27'), + ('The mean absolute error and other functions in _scikit-learn_', + 2, + None, + '___sec28'), + ('Cubic polynomial in _scikit-learn_', 2, None, '___sec29'), ('Simple regression model with gradient descent', 2, None, - '___sec21'), + '___sec30'), ('Simple regression model with stochastic gradient descent', 2, None, - '___sec22'), - ('Polynomial Regression', 2, None, '___sec23'), - ('Ridge and Lasso Regression', 2, None, '___sec24'), - ('The singular value decompostion', 2, None, '___sec25')]} + '___sec31'), + ('Polynomial Regression', 2, None, '___sec32'), + ('Ridge and Lasso Regression', 2, None, '___sec33'), + ('The singular value decompostion', 2, None, '___sec34'), + ('Lasso and Ridge regression', 2, None, '___sec35'), + ('Logistic regression', 2, None, '___sec36')]} end of tocinfo --> @@ -148,21 +162,32 @@ MathJax.Hub.Config({
  • Interpretations and optimizing our parameters
  • Interpretations and optimizing our parameters
  • Interpretations and optimizing our parameters
  • -
  • Simple regression model
  • -
  • Simple regression model, now using scikit-learn
  • -
  • Correlations and the quality of our results
  • -
  • Estimate of the error
  • +
  • The \( \chi^2 \) function
  • +
  • The \( \chi^2 \) function
  • +
  • The \( \chi^2 \) function
  • +
  • The \( \chi^2 \) function
  • The \( \chi^2 \) function
  • The \( \chi^2 \) function
  • -
  • The \( \chi^2 \) function
  • -
  • The \( \chi^2 \) function
  • -
  • The \( \chi^2 \) function
  • -
  • The \( \chi^2 \) function
  • -
  • Simple regression model with gradient descent
  • -
  • Simple regression model with stochastic gradient descent
  • -
  • Polynomial Regression
  • -
  • Ridge and Lasso Regression
  • -
  • The singular value decompostion
  • +
  • Simple regression model
  • +
  • Simple regression model, now using scikit-learn
  • +
  • Simple linear regression model using scikit-learn
  • +
  • Simple linear regression model
  • +
  • Less noise
  • +
  • How to study our fits
  • +
  • Minimizing the cost function
  • +
  • Relative error
  • +
  • The richness of scikit-learn
  • +
  • Functions in scikit-learn
  • +
  • Other functions in scikit-learn
  • +
  • The mean absolute error and other functions in scikit-learn
  • +
  • Cubic polynomial in scikit-learn
  • +
  • Simple regression model with gradient descent
  • +
  • Simple regression model with stochastic gradient descent
  • +
  • Polynomial Regression
  • +
  • Ridge and Lasso Regression
  • +
  • The singular value decompostion
  • +
  • Lasso and Ridge regression
  • +
  • Logistic regression
  • @@ -184,22 +209,39 @@ MathJax.Hub.Config({

    -In order to find the parameters \( \beta_i \) we will then minimize the spread of \( \chi^2(\hat{\beta}) \) by requiring +We define then $$ -\frac{\partial \chi^2(\hat{\beta})}{\partial \beta_j} = \frac{\partial }{\partial \beta_j}\left[ \sum_{i=0}^{n-1}\left(\frac{y_i-\beta_0x_{i,0}-\beta_1x_{i,1}-\beta_2x_{i,2}-\dots-\beta_{n-1}x_{i,n-1}}{\sigma_i}\right)^2\right]=0, +\gamma = \sum_{i=0}^{1}\frac{n-1}{\sigma_i^2}, $$ -which results in + $$ -\frac{\partial \chi^2(\hat{\beta})}{\partial \beta_j} = -2\left[ \sum_{i=0}^{n-1}\frac{x_{ij}}{\sigma_i}\left(\frac{y_i-\beta_0x_{i,0}-\beta_1x_{i,1}-\beta_2x_{i,2}-\dots-\beta_{n-1}x_{i,n-1}}{\sigma_i}\right)\right]=0, +\gamma_x = \sum_{i=0}^{n-1}\frac{x_{i}}{\sigma_i^2}, $$ -or in a matrix-vector form as $$ -\frac{\partial \chi^2(\hat{\beta})}{\partial \hat{\beta}} = 0 = \hat{A}^T\left( \hat{b}-\hat{A}\hat{\beta}\right). +\gamma_y = \sum_{i=0}^{n-1}\left(\frac{y_i}{\sigma_i^2}\right), $$ -where we have defined the matrix \( \hat{A} =\hat{X}/\hat{\Sigma} \) with matrix elements \( a_{ij} = x_{ij}/\sigma_i \) and the vector \( \hat{b} \) with elements \( b_i = y_i/\sigma_i \). +$$ +\gamma_{xx} = \sum_{i=0}^{n-1}\frac{x_ix_{i}}{\sigma_i^2}, +$$ + +$$ +\gamma_{xy} = \sum_{i=0}^{n-1}\frac{y_ix_{i}}{\sigma_i^2}, +$$ + +and show that +$$ +\beta_0 = \frac{\gamma_{xx}\gamma_y-\gamma_x\gamma_y}{\gamma\gamma_{xx}-\gamma_x^2}, +$$ + +$$ +\beta_1 = \frac{\gamma_{xy}\gamma-\gamma_x\gamma_y}{\gamma\gamma_{xx}-\gamma_x^2}. +$$ + +

    +The LSM suffers often from both being underdetermined and overdetermined in the unknown coefficients \( \beta_i \). A better approach is to use the Singular Value Decomposition (SVD) method discussed below. @@ -229,6 +271,8 @@ where we have defined the matrix \( \hat{A} =\hat{X}/\hat{\Sigma} \) with matrix

  • 25
  • 26
  • 27
  • +
  • ...
  • +
  • 38
  • »
  • diff --git a/doc/pub/Regression/html/._Regression-bs018.html b/doc/pub/Regression/html/._Regression-bs018.html index 16e525bd2..0ba0aa5ad 100644 --- a/doc/pub/Regression/html/._Regression-bs018.html +++ b/doc/pub/Regression/html/._Regression-bs018.html @@ -73,33 +73,47 @@ Automatically generated HTML file from DocOnce source 2, None, '___sec10'), - ('Simple regression model', 2, None, '___sec11'), + ('The $\\chi^2$ function', 2, None, '___sec11'), + ('The $\\chi^2$ function', 2, None, '___sec12'), + ('The $\\chi^2$ function', 2, None, '___sec13'), + ('The $\\chi^2$ function', 2, None, '___sec14'), + ('The $\\chi^2$ function', 2, None, '___sec15'), + ('The $\\chi^2$ function', 2, None, '___sec16'), + ('Simple regression model', 2, None, '___sec17'), ('Simple regression model, now using _scikit-learn_', 2, None, - '___sec12'), - ('Correlations and the quality of our results', + '___sec18'), + ('Simple linear regression model using _scikit-learn_', 2, None, - '___sec13'), - ('Estimate of the error', 2, None, '___sec14'), - ('The $\\chi^2$ function', 2, None, '___sec15'), - ('The $\\chi^2$ function', 2, None, '___sec16'), - ('The $\\chi^2$ function', 2, None, '___sec17'), - ('The $\\chi^2$ function', 2, None, '___sec18'), - ('The $\\chi^2$ function', 2, None, '___sec19'), - ('The $\\chi^2$ function', 2, None, '___sec20'), + '___sec19'), + ('Simple linear regression model', 2, None, '___sec20'), + ('Less noise', 2, None, '___sec21'), + ('How to study our fits', 2, None, '___sec22'), + ('Minimizing the cost function', 2, None, '___sec23'), + ('Relative error', 2, None, '___sec24'), + ('The richness of _scikit-learn_', 2, None, '___sec25'), + ('Functions in _scikit-learn_', 2, None, '___sec26'), + ('Other functions in _scikit-learn_', 2, None, '___sec27'), + ('The mean absolute error and other functions in _scikit-learn_', + 2, + None, + '___sec28'), + ('Cubic polynomial in _scikit-learn_', 2, None, '___sec29'), ('Simple regression model with gradient descent', 2, None, - '___sec21'), + '___sec30'), ('Simple regression model with stochastic gradient descent', 2, None, - '___sec22'), - ('Polynomial Regression', 2, None, '___sec23'), - ('Ridge and Lasso Regression', 2, None, '___sec24'), - ('The singular value decompostion', 2, None, '___sec25')]} + '___sec31'), + ('Polynomial Regression', 2, None, '___sec32'), + ('Ridge and Lasso Regression', 2, None, '___sec33'), + ('The singular value decompostion', 2, None, '___sec34'), + ('Lasso and Ridge regression', 2, None, '___sec35'), + ('Logistic regression', 2, None, '___sec36')]} end of tocinfo --> @@ -148,21 +162,32 @@ MathJax.Hub.Config({
  • Interpretations and optimizing our parameters
  • Interpretations and optimizing our parameters
  • Interpretations and optimizing our parameters
  • -
  • Simple regression model
  • -
  • Simple regression model, now using scikit-learn
  • -
  • Correlations and the quality of our results
  • -
  • Estimate of the error
  • +
  • The \( \chi^2 \) function
  • +
  • The \( \chi^2 \) function
  • +
  • The \( \chi^2 \) function
  • +
  • The \( \chi^2 \) function
  • The \( \chi^2 \) function
  • The \( \chi^2 \) function
  • -
  • The \( \chi^2 \) function
  • -
  • The \( \chi^2 \) function
  • -
  • The \( \chi^2 \) function
  • -
  • The \( \chi^2 \) function
  • -
  • Simple regression model with gradient descent
  • -
  • Simple regression model with stochastic gradient descent
  • -
  • Polynomial Regression
  • -
  • Ridge and Lasso Regression
  • -
  • The singular value decompostion
  • +
  • Simple regression model
  • +
  • Simple regression model, now using scikit-learn
  • +
  • Simple linear regression model using scikit-learn
  • +
  • Simple linear regression model
  • +
  • Less noise
  • +
  • How to study our fits
  • +
  • Minimizing the cost function
  • +
  • Relative error
  • +
  • The richness of scikit-learn
  • +
  • Functions in scikit-learn
  • +
  • Other functions in scikit-learn
  • +
  • The mean absolute error and other functions in scikit-learn
  • +
  • Cubic polynomial in scikit-learn
  • +
  • Simple regression model with gradient descent
  • +
  • Simple regression model with stochastic gradient descent
  • +
  • Polynomial Regression
  • +
  • Ridge and Lasso Regression
  • +
  • The singular value decompostion
  • +
  • Lasso and Ridge regression
  • +
  • Logistic regression
  • @@ -178,29 +203,42 @@ MathJax.Hub.Config({ -

    The \( \chi^2 \) function

    -
    -
    -

    - +

    Simple regression model

    +We are now ready to write our first program which aims at solving the above linear regression equations. We start with data we have produced ourselves, in this case normally distributed random numbers along the \( x \)-axis. These numbers define then the value of a function \( y(x)=4+3x+N(0,1) \). Thereafter we order the \( x \) values and employ our linear regression algorithm to set up the best fit. Here we find it useful to use the numpy function \( c\_ \) arrays where arrays are stacked along their last axis after being upgraded to at least two dimensions with ones post-pended to the shape. The following examples help in understanding what happens

    -We can rewrite -$$ -\frac{\partial \chi^2(\hat{\beta})}{\partial \hat{\beta}} = 0 = \hat{A}^T\left( \hat{b}-\hat{A}\hat{\beta}\right), -$$ -as -$$ -\hat{A}^T\hat{b} = \hat{A}^T\hat{A}\hat{\beta}, -$$ + +

    import numpy as np
    +print(np.c_[np.array([1,2,3]), np.array([4,5,6])])
    +print(np.c_[np.array([[1,2,3]]), 0, 0, np.array([[4,5,6]])])
    +
    +

    -and if the matrix \( \hat{A}^T\hat{A} \) is invertible we have the solution -$$ -\hat{\beta} =\left(\hat{A}^T\hat{A}\right)^{-1}\hat{A}^T\hat{b}. -$$ -

    -
    + +
    # Importing various packages
    +from random import random, seed
    +import numpy as np
    +import matplotlib.pyplot as plt
     
    +x = 2*np.random.rand(100,1)
    +y = 4+3*x+np.random.randn(100,1)
    +
    +xb = np.c_[np.ones((100,1)), x]
    +beta = np.linalg.inv(xb.T.dot(xb)).dot(xb.T).dot(y)
    +xnew = np.array([[0],[2]])
    +xbnew = np.c_[np.ones((2,1)), xnew]
    +ypredict = xbnew.dot(beta)
    +
    +plt.plot(xnew, ypredict, "r-")
    +plt.plot(x, y ,'ro')
    +plt.axis([0,2.0,0, 15.0])
    +plt.xlabel(r'$x$')
    +plt.ylabel(r'$y$')
    +plt.title(r'Linear Regression')
    +plt.show()
    +
    +

    +We see that, as expected, a linear fit gives a seemingly (from the graph) good representation of the data.

    @@ -226,6 +264,9 @@ $$

  • 25
  • 26
  • 27
  • +
  • 28
  • +
  • ...
  • +
  • 38
  • »
  • diff --git a/doc/pub/Regression/html/Regression-bs.html b/doc/pub/Regression/html/Regression-bs.html index b7757f73d..9560a3323 100644 --- a/doc/pub/Regression/html/Regression-bs.html +++ b/doc/pub/Regression/html/Regression-bs.html @@ -73,33 +73,47 @@ Automatically generated HTML file from DocOnce source 2, None, '___sec10'), - ('Simple regression model', 2, None, '___sec11'), + ('The $\\chi^2$ function', 2, None, '___sec11'), + ('The $\\chi^2$ function', 2, None, '___sec12'), + ('The $\\chi^2$ function', 2, None, '___sec13'), + ('The $\\chi^2$ function', 2, None, '___sec14'), + ('The $\\chi^2$ function', 2, None, '___sec15'), + ('The $\\chi^2$ function', 2, None, '___sec16'), + ('Simple regression model', 2, None, '___sec17'), ('Simple regression model, now using _scikit-learn_', 2, None, - '___sec12'), - ('Correlations and the quality of our results', + '___sec18'), + ('Simple linear regression model using _scikit-learn_', 2, None, - '___sec13'), - ('Estimate of the error', 2, None, '___sec14'), - ('The $\\chi^2$ function', 2, None, '___sec15'), - ('The $\\chi^2$ function', 2, None, '___sec16'), - ('The $\\chi^2$ function', 2, None, '___sec17'), - ('The $\\chi^2$ function', 2, None, '___sec18'), - ('The $\\chi^2$ function', 2, None, '___sec19'), - ('The $\\chi^2$ function', 2, None, '___sec20'), + '___sec19'), + ('Simple linear regression model', 2, None, '___sec20'), + ('Less noise', 2, None, '___sec21'), + ('How to study our fits', 2, None, '___sec22'), + ('Minimizing the cost function', 2, None, '___sec23'), + ('Relative error', 2, None, '___sec24'), + ('The richness of _scikit-learn_', 2, None, '___sec25'), + ('Functions in _scikit-learn_', 2, None, '___sec26'), + ('Other functions in _scikit-learn_', 2, None, '___sec27'), + ('The mean absolute error and other functions in _scikit-learn_', + 2, + None, + '___sec28'), + ('Cubic polynomial in _scikit-learn_', 2, None, '___sec29'), ('Simple regression model with gradient descent', 2, None, - '___sec21'), + '___sec30'), ('Simple regression model with stochastic gradient descent', 2, None, - '___sec22'), - ('Polynomial Regression', 2, None, '___sec23'), - ('Ridge and Lasso Regression', 2, None, '___sec24'), - ('The singular value decompostion', 2, None, '___sec25')]} + '___sec31'), + ('Polynomial Regression', 2, None, '___sec32'), + ('Ridge and Lasso Regression', 2, None, '___sec33'), + ('The singular value decompostion', 2, None, '___sec34'), + ('Lasso and Ridge regression', 2, None, '___sec35'), + ('Logistic regression', 2, None, '___sec36')]} end of tocinfo --> @@ -148,21 +162,32 @@ MathJax.Hub.Config({
  • Interpretations and optimizing our parameters
  • Interpretations and optimizing our parameters
  • Interpretations and optimizing our parameters
  • -
  • Simple regression model
  • -
  • Simple regression model, now using scikit-learn
  • -
  • Correlations and the quality of our results
  • -
  • Estimate of the error
  • +
  • The \( \chi^2 \) function
  • +
  • The \( \chi^2 \) function
  • +
  • The \( \chi^2 \) function
  • +
  • The \( \chi^2 \) function
  • The \( \chi^2 \) function
  • The \( \chi^2 \) function
  • -
  • The \( \chi^2 \) function
  • -
  • The \( \chi^2 \) function
  • -
  • The \( \chi^2 \) function
  • -
  • The \( \chi^2 \) function
  • -
  • Simple regression model with gradient descent
  • -
  • Simple regression model with stochastic gradient descent
  • -
  • Polynomial Regression
  • -
  • Ridge and Lasso Regression
  • -
  • The singular value decompostion
  • +
  • Simple regression model
  • +
  • Simple regression model, now using scikit-learn
  • +
  • Simple linear regression model using scikit-learn
  • +
  • Simple linear regression model
  • +
  • Less noise
  • +
  • How to study our fits
  • +
  • Minimizing the cost function
  • +
  • Relative error
  • +
  • The richness of scikit-learn
  • +
  • Functions in scikit-learn
  • +
  • Other functions in scikit-learn
  • +
  • The mean absolute error and other functions in scikit-learn
  • +
  • Cubic polynomial in scikit-learn
  • +
  • Simple regression model with gradient descent
  • +
  • Simple regression model with stochastic gradient descent
  • +
  • Polynomial Regression
  • +
  • Ridge and Lasso Regression
  • +
  • The singular value decompostion
  • +
  • Lasso and Ridge regression
  • +
  • Logistic regression
  • @@ -197,7 +222,7 @@ MathJax.Hub.Config({
    [2] Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University

    -

    May 30, 2018

    +

    Aug 24, 2018


    @@ -221,7 +246,7 @@ MathJax.Hub.Config({

  • 9
  • 10
  • ...
  • -
  • 27
  • +
  • 38
  • »
  • diff --git a/doc/pub/Regression/html/Regression-reveal.html b/doc/pub/Regression/html/Regression-reveal.html index c42f02d19..2e93e3ddc 100644 --- a/doc/pub/Regression/html/Regression-reveal.html +++ b/doc/pub/Regression/html/Regression-reveal.html @@ -148,7 +148,7 @@ MathJax.Hub.Config({
    [2] Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University

     
    -

    May 30, 2018

    +

    Aug 24, 2018


    @@ -492,7 +492,210 @@ meaning that the solution for \( \hat{\beta} \) is the one which minimizes the r

    -

    Simple regression model

    +

    The \( \chi^2 \) function

    +
    + +

    +Normally, the response (dependent or outcome) variable \( y_i \) the outcome of a numerical experiment or another type of experiment and is thus only an approximation to the true value. It is then always accompanied by an error estimate, often limited to a statistical error estimate given by the standard deviation discussed earlier. In the discussion here we will treat \( y_i \) as our exact value for the response variable. + +

    +Introducing the standard deviation \( \sigma_i \) for each measurement \( y_i \), we define now the \( \chi^2 \) function as + +

     
    +$$ +\chi^2(\hat{\beta})=\sum_{i=0}^{n-1}\frac{\left(y_i-\tilde{y}_i\right)^2}{\sigma_i^2}=\left(\hat{y}-\hat{\tilde{y}}\right)^T\frac{1}{\hat{\Sigma^2}}\left(\hat{y}-\hat{\tilde{y}}\right), +$$ +

     
    + +where the matrix \( \hat{\Sigma} \) is a diagonal matrix with \( \sigma_i \) as matrix elements. + + +

    +
    + + +
    +

    The \( \chi^2 \) function

    +
    + +

    +In order to find the parameters \( \beta_i \) we will then minimize the spread of \( \chi^2(\hat{\beta}) \) by requiring +

     
    +$$ +\frac{\partial \chi^2(\hat{\beta})}{\partial \beta_j} = \frac{\partial }{\partial \beta_j}\left[ \sum_{i=0}^{n-1}\left(\frac{y_i-\beta_0x_{i,0}-\beta_1x_{i,1}-\beta_2x_{i,2}-\dots-\beta_{n-1}x_{i,n-1}}{\sigma_i}\right)^2\right]=0, +$$ +

     
    + +which results in +

     
    +$$ +\frac{\partial \chi^2(\hat{\beta})}{\partial \beta_j} = -2\left[ \sum_{i=0}^{n-1}\frac{x_{ij}}{\sigma_i}\left(\frac{y_i-\beta_0x_{i,0}-\beta_1x_{i,1}-\beta_2x_{i,2}-\dots-\beta_{n-1}x_{i,n-1}}{\sigma_i}\right)\right]=0, +$$ +

     
    + +or in a matrix-vector form as +

     
    +$$ +\frac{\partial \chi^2(\hat{\beta})}{\partial \hat{\beta}} = 0 = \hat{A}^T\left( \hat{b}-\hat{A}\hat{\beta}\right). +$$ +

     
    + +where we have defined the matrix \( \hat{A} =\hat{X}/\hat{\Sigma} \) with matrix elements \( a_{ij} = x_{ij}/\sigma_i \) and the vector \( \hat{b} \) with elements \( b_i = y_i/\sigma_i \). +

    +
    + + +
    +

    The \( \chi^2 \) function

    +
    + +

    +We can rewrite +

     
    +$$ +\frac{\partial \chi^2(\hat{\beta})}{\partial \hat{\beta}} = 0 = \hat{A}^T\left( \hat{b}-\hat{A}\hat{\beta}\right), +$$ +

     
    + +as +

     
    +$$ +\hat{A}^T\hat{b} = \hat{A}^T\hat{A}\hat{\beta}, +$$ +

     
    + +and if the matrix \( \hat{A}^T\hat{A} \) is invertible we have the solution +

     
    +$$ +\hat{\beta} =\left(\hat{A}^T\hat{A}\right)^{-1}\hat{A}^T\hat{b}. +$$ +

     
    +

    +
    + + +
    +

    The \( \chi^2 \) function

    +
    + +

    +If we then introduce the matrix +

     
    +$$ +\hat{H} = \left(\hat{A}^T\hat{A}\right)^{-1}, +$$ +

     
    + +we have then the following expression for the parameters \( \beta_j \) (the matrix elements of \( \hat{H} \) are \( h_{ij} \)) +

     
    +$$ +\beta_j = \sum_{k=0}^{p-1}h_{jk}\sum_{i=0}^{n-1}\frac{y_i}{\sigma_i}\frac{x_{ik}}{\sigma_i} = \sum_{k=0}^{p-1}h_{jk}\sum_{i=0}^{n-1}b_ia_{ik} +$$ +

     
    + +We state without proof the expression for the uncertainty in the parameters \( \beta_j \) as (we leave this as an exercise) +

     
    +$$ +\sigma^2(\beta_j) = \sum_{i=0}^{n-1}\sigma_i^2\left( \frac{\partial \beta_j}{\partial y_i}\right)^2, +$$ +

     
    + +resulting in +

     
    +$$ +\sigma^2(\beta_j) = \left(\sum_{k=0}^{p-1}h_{jk}\sum_{i=0}^{n-1}a_{ik}\right)\left(\sum_{l=0}^{p-1}h_{jl}\sum_{m=0}^{n-1}a_{ml}\right) = h_{jj}! +$$ +

     
    +

    +
    + + +
    +

    The \( \chi^2 \) function

    +
    + +

    +The first step here is to approximate the function \( y \) with a first-order polynomial, that is we write +

     
    +$$ +y=y(x) \rightarrow y(x_i) \approx \beta_0+\beta_1 x_i. +$$ +

     
    + +By computing the derivatives of \( \chi^2 \) with respect to \( \beta_0 \) and \( \beta_1 \) show that these are given by +

     
    +$$ +\frac{\partial \chi^2(\hat{\beta})}{\partial \beta_0} = -2\left[ \sum_{i=0}^{n-1}\left(\frac{y_i-\beta_0-\beta_1x_{i}}{\sigma_i^2}\right)\right]=0, +$$ +

     
    + +and +

     
    +$$ +\frac{\partial \chi^2(\hat{\beta})}{\partial \beta_0} = -2\left[ \sum_{i=0}^{n-1}x_i\left(\frac{y_i-\beta_0-\beta_1x_{i}}{\sigma_i^2}\right)\right]=0. +$$ +

     
    +

    +
    + + +
    +

    The \( \chi^2 \) function

    +
    + +

    +We define then +

     
    +$$ +\gamma = \sum_{i=0}^{1}\frac{n-1}{\sigma_i^2}, +$$ +

     
    + +

     
    +$$ +\gamma_x = \sum_{i=0}^{n-1}\frac{x_{i}}{\sigma_i^2}, +$$ +

     
    + +

     
    +$$ +\gamma_y = \sum_{i=0}^{n-1}\left(\frac{y_i}{\sigma_i^2}\right), +$$ +

     
    + +

     
    +$$ +\gamma_{xx} = \sum_{i=0}^{n-1}\frac{x_ix_{i}}{\sigma_i^2}, +$$ +

     
    + +

     
    +$$ +\gamma_{xy} = \sum_{i=0}^{n-1}\frac{y_ix_{i}}{\sigma_i^2}, +$$ +

     
    + +and show that +

     
    +$$ +\beta_0 = \frac{\gamma_{xx}\gamma_y-\gamma_x\gamma_y}{\gamma\gamma_{xx}-\gamma_x^2}, +$$ +

     
    + +

     
    +$$ +\beta_1 = \frac{\gamma_{xy}\gamma-\gamma_x\gamma_y}{\gamma\gamma_{xx}-\gamma_x^2}. +$$ +

     
    + +

    +The LSM suffers often from both being underdetermined and overdetermined in the unknown coefficients \( \beta_i \). A better approach is to use the Singular Value Decomposition (SVD) method discussed below. +

    +
    + + +
    +

    Simple regression model

    We are now ready to write our first program which aims at solving the above linear regression equations. We start with data we have produced ourselves, in this case normally distributed random numbers along the \( x \)-axis. These numbers define then the value of a function \( y(x)=4+3x+N(0,1) \). Thereafter we order the \( x \) values and employ our linear regression algorithm to set up the best fit. Here we find it useful to use the numpy function \( c\_ \) arrays where arrays are stacked along their last axis after being upgraded to at least two dimensions with ones post-pended to the shape. The following examples help in understanding what happens

    @@ -513,10 +716,10 @@ x = 2*np.random.rand(4+3*x+np.random.randn(100,1) xb = np.c_[np.ones((100,1)), x] -theta = np.linalg.inv(xb.T.dot(xb)).dot(xb.T).dot(y) +beta = np.linalg.inv(xb.T.dot(xb)).dot(xb.T).dot(y) xnew = np.array([[0],[2]]) xbnew = np.c_[np.ones((2,1)), xnew] -ypredict = xbnew.dot(theta) +ypredict = xbnew.dot(beta) plt.plot(xnew, ypredict, "r-") plt.plot(x, y ,'ro') @@ -532,7 +735,7 @@ We see that, as expected, a linear fit gives a seemingly (from the graph) good r

    -

    Simple regression model, now using scikit-learn

    +

    Simple regression model, now using scikit-learn

    We can repeat the above algorithm using scikit-learn as follows @@ -564,231 +767,372 @@ plt.show()

    -

    Correlations and the quality of our results

    -In order to test the quality of our fit, there are several measures which can be implemented. One is the so-called -correlation function defined as +

    Simple linear regression model using scikit-learn

    + +

    +We start with perhaps our simplest possible example, using scikit-learn to perform linear regression analysis on a data set produced by us. +What follows is a simple Python code where we have defined function \( y \) in terms of the variable \( x \). Both are defined as vectors of dimension \( 1\times 100 \). The entries to the vector \( \hat{x} \) are given by random numbers generated with a uniform distribution with entries \( x_i \in [0,1] \) (more about probability distribution functions later). These values are then used to define a function \( y(x) \) (tabulated again as a vector) with a linear dependence on \( x \) plus a random noise added via the normal distribution. + +

    +The Numpy functions are imported used the import numpy as np +statement and the random number generator for the uniform distribution +is called using the function np.random.rand(), where we specificy +that we want \( 100 \) random variables. Using Numpy we define +automatically an array with the specified number of elements, \( 100 \) in +our case. With the Numpy function randn() we can compute random +numbers with the normal distribution (mean value \( \mu \) equal to zero and +variance \( \sigma^2 \) set to one) and produce the values of \( y \) assuming a linear +dependence as function of \( x \) +

     
    $$ -\mathrm{Corr}(X,Y) = \frac{\sum_{i=1}^n(x_i-\overline{x})(y_i-\overline{y})}{\sqrt{\sum_{i=1}^n(x_i-\overline{x})^2}\sqrt{\sum_{i=1}^n(y_i-\overline{y})^2} } +y = 2x+N(0,1), $$

     

    -Another quantity is the autocorrelation function we discussed in our chapter on statistical analysis. -Let us now try to assess the quality of our fit by studying various measures. +where \( N(0,1) \) represents random numbers generated by the normal +distribution. From scikit-learn we import then the +LinearRegression functionality and make a prediction \( \tilde{y} = +\alpha + \beta x \) using the function fit(x,y). We call the set of +data \( (\hat{x},\hat{y}) \) for our training data. The Python package +scikit-learn has also a functionality which extracts the above +fitting parameters \( \alpha \) and \( \beta \) (see below). Later we will +distinguish between training data and test data. + +

    +For plotting we use the Python package +matplotlib which produces publication +quality figures. Feel free to explore the extensive +gallery of examples. In +this example we plot our original values of \( x \) and \( y \) as well as the +prediction ypredict (\( \tilde{y} \)), which attempts at fitting our +data with a straight line. + +

    +The Python code follows here. +

    + + +

    # Importing various packages
    +import numpy as np
    +import matplotlib.pyplot as plt
    +from sklearn.linear_model import LinearRegression
    +
    +x = np.random.rand(100,1)
    +y = 2*x+np.random.randn(100,1)
    +linreg = LinearRegression()
    +linreg.fit(x,y)
    +xnew = np.array([[0],[1]])
    +ypredict = linreg.predict(xnew)
    +
    +plt.plot(xnew, ypredict, "r-")
    +plt.plot(x, y ,'ro')
    +plt.axis([0,1.0,0, 5.0])
    +plt.xlabel(r'$x$')
    +plt.ylabel(r'$y$')
    +plt.title(r'Simple Linear Regression')
    +plt.show()
    +
    -

    Estimate of the error

    +

    Simple linear regression model

    + +

    +This example serves several aims. It allows us to demonstrate several +aspects of data analysis and later machine learning algorithms. The +immediate visualization shows that our linear fit is not +impressive. It goes through the data points, but there are many +outliers which are not reproduced by our linear regression. We could +now play around with this small program and change for example the +factor in front of \( x \) and the normal distribution. Try to change the +function \( y \) to + +

     
    +$$ +y = 10x+0.01 \times N(0,1), +$$ +

     
    + +

    +where \( x \) is defined as before.

    -

    The \( \chi^2 \) function

    -
    - -

    -Normally, the response (dependent or outcome) variable \( y_i \) the outcome of a numerical experiment or another type of experiment and is thus only an approximation to the true value. It is then always accompanied by an error estimate, often limited to a statistical error estimate given by the standard deviation discussed earlier. In the discussion here we will treat \( y_i \) as our exact value for the response variable. +

    Less noise

    -Introducing the standard deviation \( \sigma_i \) for each measurement \( y_i \), we define now the \( \chi^2 \) function as - -

     
    -$$ -\chi^2(\hat{\beta})=\sum_{i=0}^{n-1}\frac{\left(y_i-\tilde{y}_i\right)^2}{\sigma_i^2}=\left(\hat{y}-\hat{\tilde{y}}\right)^T\frac{1}{\hat{\Sigma^2}}\left(\hat{y}-\hat{\tilde{y}}\right), -$$ -

     
    - -where the matrix \( \hat{\Sigma} \) is a diagonal matrix with \( \sigma_i \) as matrix elements. - - -

    +Does the fit look better? Indeed, by +reducing the role of the normal distribution we see immediately that +our linear prediction seemingly reproduces better the training +set. However, this testing 'by the eye' is obviouly not satisfactory in the +long run. Here we have only defined the training data and our model, and +have not discussed a more rigorous approach to the cost function.
    -

    The \( \chi^2 \) function

    -
    - +

    How to study our fits

    +

    -In order to find the parameters \( \beta_i \) we will then minimize the spread of \( \chi^2(\hat{\beta}) \) by requiring +We need more rigorous criteria in defining whether we have succeeded or +not in modeling our training data. You will be surprised to see that +many scientists seldomly venture beyond this 'by the eye' approach. A +standard approach for the cost function is the so-called \( \chi^2 \) +function +

     
    -$$ -\frac{\partial \chi^2(\hat{\beta})}{\partial \beta_j} = \frac{\partial }{\partial \beta_j}\left[ \sum_{i=0}^{n-1}\left(\frac{y_i-\beta_0x_{i,0}-\beta_1x_{i,1}-\beta_2x_{i,2}-\dots-\beta_{n-1}x_{i,n-1}}{\sigma_i}\right)^2\right]=0, +$$ \chi^2 = \frac{1}{n} +\sum_{i=0}^{n-1}\frac{(y_i-\tilde{y}_i)^2}{\sigma_i^2}, $$

     
    -which results in -

     
    -$$ -\frac{\partial \chi^2(\hat{\beta})}{\partial \beta_j} = -2\left[ \sum_{i=0}^{n-1}\frac{x_{ij}}{\sigma_i}\left(\frac{y_i-\beta_0x_{i,0}-\beta_1x_{i,1}-\beta_2x_{i,2}-\dots-\beta_{n-1}x_{i,n-1}}{\sigma_i}\right)\right]=0, -$$ -

     
    - -or in a matrix-vector form as -

     
    -$$ -\frac{\partial \chi^2(\hat{\beta})}{\partial \hat{\beta}} = 0 = \hat{A}^T\left( \hat{b}-\hat{A}\hat{\beta}\right). -$$ -

     
    - -where we have defined the matrix \( \hat{A} =\hat{X}/\hat{\Sigma} \) with matrix elements \( a_{ij} = x_{ij}/\sigma_i \) and the vector \( \hat{b} \) with elements \( b_i = y_i/\sigma_i \). -

    +

    +where \( \sigma_i^2 \) is the variance (to be defined later) of the entry +\( y_i \). We may not know the explicit value of \( \sigma_i^2 \), it serves +however the aim of scaling the equations and make the cost function +dimensionless.

    -

    The \( \chi^2 \) function

    -
    - +

    Minimizing the cost function

    +

    -We can rewrite -

     
    -$$ -\frac{\partial \chi^2(\hat{\beta})}{\partial \hat{\beta}} = 0 = \hat{A}^T\left( \hat{b}-\hat{A}\hat{\beta}\right), -$$ -

     
    - -as -

     
    -$$ -\hat{A}^T\hat{b} = \hat{A}^T\hat{A}\hat{\beta}, -$$ -

     
    - -and if the matrix \( \hat{A}^T\hat{A} \) is invertible we have the solution -

     
    -$$ -\hat{\beta} =\left(\hat{A}^T\hat{A}\right)^{-1}\hat{A}^T\hat{b}. -$$ -

     
    -

    +Minimizing the cost function is a central aspect of +our discussions to come. Finding its minima as function of the model +parameters (\( \alpha \) and \( \beta \) in our case) will be a recurring +theme in these series of lectures. Essentially all machine learning +algorithms we will discuss center around the minimization of the +chosen cost function. This depends in turn on our specific +model for describing the data, a typical situation in supervised +learning. Automatizing the search for the minima of the cost function is a +central ingredient in all algorithms. Typical methods which are +employed are various variants of gradient methods. These will be +discussed in more detail later. Again, you'll be surprised to hear that +many practitioners minimize the above function ''by the eye', popularly dubbed as +'chi by the eye'. That is, change a parameter and see (visually and numerically) that +the \( \chi^2 \) function becomes smaller.
    -

    The \( \chi^2 \) function

    -
    - +

    Relative error

    +

    -If we then introduce the matrix +There are many ways to define the cost function. A simpler approach is to look at the relative difference between the training data and the predicted data, that is we define +the relative error as +

     
    $$ -\hat{H} = \hat{A}^T\hat{A}, +\epsilon_{\mathrm{relative}}= \frac{\vert \hat{y} -\hat{\tilde{y}}\vert}{\vert \hat{y}\vert}. $$

     
    -we have then the following expression for the parameters \( \beta_j \) (the matrix elements of \( \hat{H} \) are \( h_{ij} \)) -

     
    -$$ -\beta_j = \sum_{k=0}^{p-1}h_{jk}\sum_{i=0}^{n-1}\frac{y_i}{\sigma_i}\frac{x_{ik}}{\sigma_i} = \sum_{k=0}^{p-1}h_{jk}\sum_{i=0}^{n-1}b_ia_{ik} -$$ -

     
    +We can modify easily the above Python code and plot the relative error instead +

    -We state without proof the expression for the uncertainty in the parameters \( \beta_j \) as -

     
    -$$ -\sigma^2(\beta_j) = \sum_{i=0}^{n-1}\sigma_i^2\left( \frac{\partial \beta_j}{\partial y_i}\right)^2, -$$ -

     
    + +

    import numpy as np
    +import matplotlib.pyplot as plt
    +from sklearn.linear_model import LinearRegression
     
    -resulting in 
    -

     
    -$$ -\sigma^2(\beta_j) = \left(\sum_{k=0}^{p-1}h_{jk}\sum_{i=0}^{n-1}a_{ik}\right)\left(\sum_{l=0}^{p-1}h_{jl}\sum_{m=0}^{n-1}a_{ml}\right) = h_{jj}! -$$ -

     
    -

    +x = np.random.rand(100,1) +y = 5*x+0.01*np.random.randn(100,1) +linreg = LinearRegression() +linreg.fit(x,y) +ypredict = linreg.predict(x) + +plt.plot(x, np.abs(ypredict-y)/abs(y), "ro") +plt.axis([0,1.0,0.0, 0.5]) +plt.xlabel(r'$x$') +plt.ylabel(r'$\epsilon_{\mathrm{relative}}$') +plt.title(r'Relative error') +plt.show() +
    +

    +Depending on the parameter in front of the normal distribution, we may +have a small or larger relative error. Try to play around with +different training data sets and study (graphically) the value of the +relative error.

    -

    The \( \chi^2 \) function

    -
    - +

    The richness of scikit-learn

    +

    -The first step here is to approximate the function \( y \) with a first-order polynomial, that is we write -

     
    -$$ -y=y(x) \rightarrow y(x_i) \approx \beta_0+\beta_1 x_i. -$$ -

     
    +As mentioned above, scikit-learn has an impressive functionality. +We can for example extract the values of \( \alpha \) and \( \beta \) and +their error estimates, or the variance and standard deviation and many +other properties from the statistical data analysis. -By computing the derivatives of \( \chi^2 \) with respect to \( \beta_0 \) and \( \beta_1 \) show that these are given by -

     
    -$$ -\frac{\partial \chi^2(\hat{\beta})}{\partial \beta_0} = -2\left[ \sum_{i=0}^{1}\left(\frac{y_i-\beta_0-\beta_1x_{i}}{\sigma_i^2}\right)\right]=0, -$$ -

     
    +

    +Here we show an +example of the functionality of scikit-learn. +

    -and -

     
    -$$ -\frac{\partial \chi^2(\hat{\beta})}{\partial \beta_0} = -2\left[ \sum_{i=0}^{1}x_i\left(\frac{y_i-\beta_0-\beta_1x_{i}}{\sigma_i^2}\right)\right]=0. -$$ -

     
    -

    + +
    import numpy as np 
    +import matplotlib.pyplot as plt 
    +from sklearn.linear_model import LinearRegression 
    +from sklearn.metrics import mean_squared_error, r2_score, mean_squared_log_error, mean_absolute_error
    +
    +x = np.random.rand(100,1)
    +y = 2.0+ 5*x+0.5*np.random.randn(100,1)
    +linreg = LinearRegression()
    +linreg.fit(x,y)
    +ypredict = linreg.predict(x)
    +print('The intercept alpha: \n', linreg.intercept_)
    +print('Coefficient beta : \n', linreg.coef_)
    +# The mean squared error                               
    +print("Mean squared error: %.2f" % mean_squared_error(y, ypredict))
    +# Explained variance score: 1 is perfect prediction                                 
    +print('Variance score: %.2f' % r2_score(y, ypredict))
    +# Mean squared log error                                                        
    +print('Mean squared log error: %.2f' % mean_squared_log_error(y, ypredict) )
    +# Mean absolute error                                                           
    +print('Mean absolute error: %.2f' % mean_absolute_error(y, ypredict))
    +plt.plot(x, ypredict, "r-")
    +plt.plot(x, y ,'ro')
    +plt.axis([0.0,1.0,1.5, 7.0])
    +plt.xlabel(r'$x$')
    +plt.ylabel(r'$y$')
    +plt.title(r'Linear Regression fit ')
    +plt.show()
    +
    -

    The \( \chi^2 \) function

    -
    - +

    Functions in scikit-learn

    +

    -We define then +The function coef gives us the parameter \( \beta \) of our fit while intercept yields +\( \alpha \). Depending on the constant in front of the normal distribution, we get values near or far from \( alpha =2 \) and \( \beta =5 \). Try to play around with different parameters in front of the normal distribution. The function meansquarederror gives us the mean square error, a risk metric corresponding to the expected value of the squared (quadratic) error or loss defined as

     
    -$$ -\gamma = \sum_{i=0}^{1}\frac{1}{\sigma_i^2}, -$$ -

     
    - -

     
    -$$ -\gamma_x = \sum_{i=0}^{1}\frac{x_{i}}{\sigma_i^2}, -$$ -

     
    - -

     
    -$$ -\gamma_y = \sum_{i=0}^{1}\left(\frac{y_i}{\sigma_i^2}\right), -$$ -

     
    - -

     
    -$$ -\gamma_{xx} = \sum_{i=0}^{1}\frac{x_ix_{i}}{\sigma_i^2}, -$$ -

     
    - -

     
    -$$ -\gamma_{xy} = \sum_{i=0}^{1}\frac{y_ix_{i}}{\sigma_i^2}, -$$ -

     
    - -and show that -

     
    -$$ -\beta_0 = \frac{\gamma_{xx}\gamma_y-\gamma_x\gamma_y}{\gamma\gamma_{xx}-\gamma_x^2}, -$$ -

     
    - -

     
    -$$ -\beta_1 = \frac{\gamma_{xy}\gamma-\gamma_x\gamma_y}{\gamma\gamma_{xx}-\gamma_x^2}. +$$ MSE(\hat{y},\hat{\tilde{y}}) = \frac{1}{n} +\sum_{i=0}^{n-1}(y_i-\tilde{y}_i)^2, $$

     

    -The LSM suffers often from both being underdetermined and overdetermined in the unknown coefficients \( \beta_i \). A better approach is to use the Singular Value Decomposition (SVD) method discussed below. -

    +The smaller the value, the better the fit. Ideally we would like to +have an MSE equal zero. The attentive reader has probably recognized +this function as being similar to the \( \chi^2 \) function defined above.
    -

    Simple regression model with gradient descent

    +

    Other functions in scikit-learn

    + +

    +The r2score function computes \( R^2 \), the coefficient of +determination. It provides a measure of how well future samples are +likely to be predicted by the model. Best possible score is 1.0 and it +can be negative (because the model can be arbitrarily worse). A +constant model that always predicts the expected value of \( \hat{y} \), +disregarding the input features, would get a \( R^2 \) score of \( 0.0 \). + +

    +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. +$$ +

     
    +

    + + +
    +

    The mean absolute error and other functions in scikit-learn

    + +

    +Another quantity will meet again in our discussions of regression analysis is + mean absolute error (MAE), a risk metric corresponding to the expected value of the absolute error loss or what we call the \( l1 \)-norm loss. In our discussion above we presented the relative error. +The MAE is defined as follows +

     
    +$$ +\text{MAE}(\hat{y}, \hat{\tilde{y}}) = \frac{1}{n} \sum_{i=0}^{n-1} \left| y_i - \tilde{y}_i \right|. +$$ +

     
    + +Finally we present the +squared logarithmic (quadratic) error +

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

     
    + +

    +where \( \log_e (x) \) stands for the natural logarithm of \( x \). This error +estimate is best to use when targets having exponential growth, such +as population counts, average sales of a commodity over a span of +years etc. +

    + + +
    +

    Cubic polynomial in scikit-learn

    + +

    +We will discuss in more +detail these and other functions in the various lectures. We conclude this part with another example. Instead of +a linear \( x \)-dependence we study now a cubic polynomial and use the polynomial regression analysis tools of scikit-learn. + +

    + + +

    import matplotlib.pyplot as plt
    +import numpy as np
    +import random
    +from sklearn.linear_model import Ridge
    +from sklearn.preprocessing import PolynomialFeatures
    +from sklearn.pipeline import make_pipeline
    +from sklearn.linear_model import LinearRegression
    +
    +x=np.linspace(0.02,0.98,200)
    +noise = np.asarray(random.sample((range(200)),200))
    +y=x**3*noise
    +yn=x**3*100
    +poly3 = PolynomialFeatures(degree=3)
    +X = poly3.fit_transform(x[:,np.newaxis])
    +clf3 = LinearRegression()
    +clf3.fit(X,y)
    +
    +Xplot=poly3.fit_transform(x[:,np.newaxis])
    +poly3_plot=plt.plot(x, clf3.predict(Xplot), label='Cubic Fit')
    +plt.plot(x,yn, color='red', label="True Cubic")
    +plt.scatter(x, y, label='Data', color='orange', s=15)
    +plt.legend()
    +plt.show()
    +
    +def error(a):
    +    for i in y:
    +        err=(y-yn)/yn
    +    return abs(np.sum(err))/len(err)
    +
    +print (error(y))
    +
    +

    +Similarly, using R, we can perform similar studies. +(more details on R will be inserted later). +

    + + +
    +

    Simple regression model with gradient descent

    Add info about the equations, play around with different learning rates

    @@ -833,7 +1177,7 @@ plt.show()

    -

    Simple regression model with stochastic gradient descent

    +

    Simple regression model with stochastic gradient descent

    Add info about the equations, play around with different learning rates

    @@ -859,7 +1203,7 @@ sgdreg.fit(x,y.ravel())

    -

    Polynomial Regression

    +

    Polynomial Regression

    @@ -891,7 +1235,7 @@ plt.show()

    -

    Ridge and Lasso Regression

    +

    Ridge and Lasso Regression

    @@ -973,7 +1317,7 @@ plt.show()

    -

    The singular value decompostion

    +

    The singular value decompostion

    @@ -984,6 +1328,26 @@ $$ $$

     

    + +

    +Add codes and discuss this in connection with lasso and ridge, show example where the standard inversion of a matrix fails and where SVD comes to rescue +

    + + +
    +

    Lasso and Ridge regression

    + +

    +Discuss the mathematics here +

    + + +
    +

    Logistic regression

    +Add discussion about classification versus regression, show examples of more than two cases and why regression is not the best approach. Motivate for k-nearest neighbors + +

    +Add examples on classification problems

    diff --git a/doc/pub/Regression/html/Regression-solarized.html b/doc/pub/Regression/html/Regression-solarized.html index 4efc5b073..69c12c250 100644 --- a/doc/pub/Regression/html/Regression-solarized.html +++ b/doc/pub/Regression/html/Regression-solarized.html @@ -93,33 +93,47 @@ div { text-align: justify; text-justify: inter-word; } 2, None, '___sec10'), - ('Simple regression model', 2, None, '___sec11'), + ('The $\\chi^2$ function', 2, None, '___sec11'), + ('The $\\chi^2$ function', 2, None, '___sec12'), + ('The $\\chi^2$ function', 2, None, '___sec13'), + ('The $\\chi^2$ function', 2, None, '___sec14'), + ('The $\\chi^2$ function', 2, None, '___sec15'), + ('The $\\chi^2$ function', 2, None, '___sec16'), + ('Simple regression model', 2, None, '___sec17'), ('Simple regression model, now using _scikit-learn_', 2, None, - '___sec12'), - ('Correlations and the quality of our results', + '___sec18'), + ('Simple linear regression model using _scikit-learn_', 2, None, - '___sec13'), - ('Estimate of the error', 2, None, '___sec14'), - ('The $\\chi^2$ function', 2, None, '___sec15'), - ('The $\\chi^2$ function', 2, None, '___sec16'), - ('The $\\chi^2$ function', 2, None, '___sec17'), - ('The $\\chi^2$ function', 2, None, '___sec18'), - ('The $\\chi^2$ function', 2, None, '___sec19'), - ('The $\\chi^2$ function', 2, None, '___sec20'), + '___sec19'), + ('Simple linear regression model', 2, None, '___sec20'), + ('Less noise', 2, None, '___sec21'), + ('How to study our fits', 2, None, '___sec22'), + ('Minimizing the cost function', 2, None, '___sec23'), + ('Relative error', 2, None, '___sec24'), + ('The richness of _scikit-learn_', 2, None, '___sec25'), + ('Functions in _scikit-learn_', 2, None, '___sec26'), + ('Other functions in _scikit-learn_', 2, None, '___sec27'), + ('The mean absolute error and other functions in _scikit-learn_', + 2, + None, + '___sec28'), + ('Cubic polynomial in _scikit-learn_', 2, None, '___sec29'), ('Simple regression model with gradient descent', 2, None, - '___sec21'), + '___sec30'), ('Simple regression model with stochastic gradient descent', 2, None, - '___sec22'), - ('Polynomial Regression', 2, None, '___sec23'), - ('Ridge and Lasso Regression', 2, None, '___sec24'), - ('The singular value decompostion', 2, None, '___sec25')]} + '___sec31'), + ('Polynomial Regression', 2, None, '___sec32'), + ('Ridge and Lasso Regression', 2, None, '___sec33'), + ('The singular value decompostion', 2, None, '___sec34'), + ('Lasso and Ridge regression', 2, None, '___sec35'), + ('Logistic regression', 2, None, '___sec36')]} end of tocinfo --> @@ -161,7 +175,7 @@ MathJax.Hub.Config({
    [2] Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University

    -

    May 30, 2018

    +

    Aug 24, 2018












    @@ -461,7 +475,184 @@ meaning that the solution for \( \hat{\beta} \) is the one which minimizes the r











    -

    Simple regression model

    +

    The \( \chi^2 \) function

    +
    + +

    + +

    +Normally, the response (dependent or outcome) variable \( y_i \) the outcome of a numerical experiment or another type of experiment and is thus only an approximation to the true value. It is then always accompanied by an error estimate, often limited to a statistical error estimate given by the standard deviation discussed earlier. In the discussion here we will treat \( y_i \) as our exact value for the response variable. + +

    +Introducing the standard deviation \( \sigma_i \) for each measurement \( y_i \), we define now the \( \chi^2 \) function as +$$ +\chi^2(\hat{\beta})=\sum_{i=0}^{n-1}\frac{\left(y_i-\tilde{y}_i\right)^2}{\sigma_i^2}=\left(\hat{y}-\hat{\tilde{y}}\right)^T\frac{1}{\hat{\Sigma^2}}\left(\hat{y}-\hat{\tilde{y}}\right), +$$ + +where the matrix \( \hat{\Sigma} \) is a diagonal matrix with \( \sigma_i \) as matrix elements. + + +

    + + +

    +









    + +

    The \( \chi^2 \) function

    +
    + +

    + +

    +In order to find the parameters \( \beta_i \) we will then minimize the spread of \( \chi^2(\hat{\beta}) \) by requiring +$$ +\frac{\partial \chi^2(\hat{\beta})}{\partial \beta_j} = \frac{\partial }{\partial \beta_j}\left[ \sum_{i=0}^{n-1}\left(\frac{y_i-\beta_0x_{i,0}-\beta_1x_{i,1}-\beta_2x_{i,2}-\dots-\beta_{n-1}x_{i,n-1}}{\sigma_i}\right)^2\right]=0, +$$ + +which results in +$$ +\frac{\partial \chi^2(\hat{\beta})}{\partial \beta_j} = -2\left[ \sum_{i=0}^{n-1}\frac{x_{ij}}{\sigma_i}\left(\frac{y_i-\beta_0x_{i,0}-\beta_1x_{i,1}-\beta_2x_{i,2}-\dots-\beta_{n-1}x_{i,n-1}}{\sigma_i}\right)\right]=0, +$$ + +or in a matrix-vector form as +$$ +\frac{\partial \chi^2(\hat{\beta})}{\partial \hat{\beta}} = 0 = \hat{A}^T\left( \hat{b}-\hat{A}\hat{\beta}\right). +$$ + +where we have defined the matrix \( \hat{A} =\hat{X}/\hat{\Sigma} \) with matrix elements \( a_{ij} = x_{ij}/\sigma_i \) and the vector \( \hat{b} \) with elements \( b_i = y_i/\sigma_i \). +

    + + +

    +









    + +

    The \( \chi^2 \) function

    +
    + +

    + +

    +We can rewrite +$$ +\frac{\partial \chi^2(\hat{\beta})}{\partial \hat{\beta}} = 0 = \hat{A}^T\left( \hat{b}-\hat{A}\hat{\beta}\right), +$$ + +as +$$ +\hat{A}^T\hat{b} = \hat{A}^T\hat{A}\hat{\beta}, +$$ + +and if the matrix \( \hat{A}^T\hat{A} \) is invertible we have the solution +$$ +\hat{\beta} =\left(\hat{A}^T\hat{A}\right)^{-1}\hat{A}^T\hat{b}. +$$ +

    + + +

    +









    + +

    The \( \chi^2 \) function

    +
    + +

    + +

    +If we then introduce the matrix +$$ +\hat{H} = \left(\hat{A}^T\hat{A}\right)^{-1}, +$$ + +we have then the following expression for the parameters \( \beta_j \) (the matrix elements of \( \hat{H} \) are \( h_{ij} \)) +$$ +\beta_j = \sum_{k=0}^{p-1}h_{jk}\sum_{i=0}^{n-1}\frac{y_i}{\sigma_i}\frac{x_{ik}}{\sigma_i} = \sum_{k=0}^{p-1}h_{jk}\sum_{i=0}^{n-1}b_ia_{ik} +$$ + +We state without proof the expression for the uncertainty in the parameters \( \beta_j \) as (we leave this as an exercise) +$$ +\sigma^2(\beta_j) = \sum_{i=0}^{n-1}\sigma_i^2\left( \frac{\partial \beta_j}{\partial y_i}\right)^2, +$$ + +resulting in +$$ +\sigma^2(\beta_j) = \left(\sum_{k=0}^{p-1}h_{jk}\sum_{i=0}^{n-1}a_{ik}\right)\left(\sum_{l=0}^{p-1}h_{jl}\sum_{m=0}^{n-1}a_{ml}\right) = h_{jj}! +$$ +

    + + +

    +









    + +

    The \( \chi^2 \) function

    +
    + +

    +The first step here is to approximate the function \( y \) with a first-order polynomial, that is we write +$$ +y=y(x) \rightarrow y(x_i) \approx \beta_0+\beta_1 x_i. +$$ + +By computing the derivatives of \( \chi^2 \) with respect to \( \beta_0 \) and \( \beta_1 \) show that these are given by +$$ +\frac{\partial \chi^2(\hat{\beta})}{\partial \beta_0} = -2\left[ \sum_{i=0}^{n-1}\left(\frac{y_i-\beta_0-\beta_1x_{i}}{\sigma_i^2}\right)\right]=0, +$$ + +and +$$ +\frac{\partial \chi^2(\hat{\beta})}{\partial \beta_0} = -2\left[ \sum_{i=0}^{n-1}x_i\left(\frac{y_i-\beta_0-\beta_1x_{i}}{\sigma_i^2}\right)\right]=0. +$$ +

    + + +

    +









    + +

    The \( \chi^2 \) function

    +
    + +

    + +

    +We define then +$$ +\gamma = \sum_{i=0}^{1}\frac{n-1}{\sigma_i^2}, +$$ + + +$$ +\gamma_x = \sum_{i=0}^{n-1}\frac{x_{i}}{\sigma_i^2}, +$$ + +$$ +\gamma_y = \sum_{i=0}^{n-1}\left(\frac{y_i}{\sigma_i^2}\right), +$$ + +$$ +\gamma_{xx} = \sum_{i=0}^{n-1}\frac{x_ix_{i}}{\sigma_i^2}, +$$ + +$$ +\gamma_{xy} = \sum_{i=0}^{n-1}\frac{y_ix_{i}}{\sigma_i^2}, +$$ + +and show that +$$ +\beta_0 = \frac{\gamma_{xx}\gamma_y-\gamma_x\gamma_y}{\gamma\gamma_{xx}-\gamma_x^2}, +$$ + +$$ +\beta_1 = \frac{\gamma_{xy}\gamma-\gamma_x\gamma_y}{\gamma\gamma_{xx}-\gamma_x^2}. +$$ + +

    +The LSM suffers often from both being underdetermined and overdetermined in the unknown coefficients \( \beta_i \). A better approach is to use the Singular Value Decomposition (SVD) method discussed below. +

    + + +

    +









    + +

    Simple regression model

    We are now ready to write our first program which aims at solving the above linear regression equations. We start with data we have produced ourselves, in this case normally distributed random numbers along the \( x \)-axis. These numbers define then the value of a function \( y(x)=4+3x+N(0,1) \). Thereafter we order the \( x \) values and employ our linear regression algorithm to set up the best fit. Here we find it useful to use the numpy function \( c\_ \) arrays where arrays are stacked along their last axis after being upgraded to at least two dimensions with ones post-pended to the shape. The following examples help in understanding what happens

    @@ -482,10 +673,10 @@ x = 2*np.random.rand(4+3*x+np.random.randn(100,1) xb = np.c_[np.ones((100,1)), x] -theta = np.linalg.inv(xb.T.dot(xb)).dot(xb.T).dot(y) +beta = np.linalg.inv(xb.T.dot(xb)).dot(xb.T).dot(y) xnew = np.array([[0],[2]]) xbnew = np.c_[np.ones((2,1)), xnew] -ypredict = xbnew.dot(theta) +ypredict = xbnew.dot(beta) plt.plot(xnew, ypredict, "r-") plt.plot(x, y ,'ro') @@ -501,7 +692,7 @@ We see that, as expected, a linear fit gives a seemingly (from the graph) good r











    -

    Simple regression model, now using scikit-learn

    +

    Simple regression model, now using scikit-learn

    We can repeat the above algorithm using scikit-learn as follows @@ -532,203 +723,352 @@ plt.show()











    -

    Correlations and the quality of our results

    -In order to test the quality of our fit, there are several measures which can be implemented. One is the so-called -correlation function defined as +

    Simple linear regression model using scikit-learn

    + +

    +We start with perhaps our simplest possible example, using scikit-learn to perform linear regression analysis on a data set produced by us. +What follows is a simple Python code where we have defined function \( y \) in terms of the variable \( x \). Both are defined as vectors of dimension \( 1\times 100 \). The entries to the vector \( \hat{x} \) are given by random numbers generated with a uniform distribution with entries \( x_i \in [0,1] \) (more about probability distribution functions later). These values are then used to define a function \( y(x) \) (tabulated again as a vector) with a linear dependence on \( x \) plus a random noise added via the normal distribution. + +

    +The Numpy functions are imported used the import numpy as np +statement and the random number generator for the uniform distribution +is called using the function np.random.rand(), where we specificy +that we want \( 100 \) random variables. Using Numpy we define +automatically an array with the specified number of elements, \( 100 \) in +our case. With the Numpy function randn() we can compute random +numbers with the normal distribution (mean value \( \mu \) equal to zero and +variance \( \sigma^2 \) set to one) and produce the values of \( y \) assuming a linear +dependence as function of \( x \) + $$ -\mathrm{Corr}(X,Y) = \frac{\sum_{i=1}^n(x_i-\overline{x})(y_i-\overline{y})}{\sqrt{\sum_{i=1}^n(x_i-\overline{x})^2}\sqrt{\sum_{i=1}^n(y_i-\overline{y})^2} } +y = 2x+N(0,1), $$

    -Another quantity is the autocorrelation function we discussed in our chapter on statistical analysis. -Let us now try to assess the quality of our fit by studying various measures. +where \( N(0,1) \) represents random numbers generated by the normal +distribution. From scikit-learn we import then the +LinearRegression functionality and make a prediction \( \tilde{y} = +\alpha + \beta x \) using the function fit(x,y). We call the set of +data \( (\hat{x},\hat{y}) \) for our training data. The Python package +scikit-learn has also a functionality which extracts the above +fitting parameters \( \alpha \) and \( \beta \) (see below). Later we will +distinguish between training data and test data. + +

    +For plotting we use the Python package +matplotlib which produces publication +quality figures. Feel free to explore the extensive +gallery of examples. In +this example we plot our original values of \( x \) and \( y \) as well as the +prediction ypredict (\( \tilde{y} \)), which attempts at fitting our +data with a straight line. + +

    +The Python code follows here. +

    + + +

    # Importing various packages
    +import numpy as np
    +import matplotlib.pyplot as plt
    +from sklearn.linear_model import LinearRegression
    +
    +x = np.random.rand(100,1)
    +y = 2*x+np.random.randn(100,1)
    +linreg = LinearRegression()
    +linreg.fit(x,y)
    +xnew = np.array([[0],[1]])
    +ypredict = linreg.predict(xnew)
    +
    +plt.plot(xnew, ypredict, "r-")
    +plt.plot(x, y ,'ro')
    +plt.axis([0,1.0,0, 5.0])
    +plt.xlabel(r'$x$')
    +plt.ylabel(r'$y$')
    +plt.title(r'Simple Linear Regression')
    +plt.show()
    +
    +

    +









    + +

    Simple linear regression model

    + +

    +This example serves several aims. It allows us to demonstrate several +aspects of data analysis and later machine learning algorithms. The +immediate visualization shows that our linear fit is not +impressive. It goes through the data points, but there are many +outliers which are not reproduced by our linear regression. We could +now play around with this small program and change for example the +factor in front of \( x \) and the normal distribution. Try to change the +function \( y \) to + +$$ +y = 10x+0.01 \times N(0,1), +$$ + +

    +where \( x \) is defined as before.











    -

    Estimate of the error

    +

    Less noise

    + +

    +Does the fit look better? Indeed, by +reducing the role of the normal distribution we see immediately that +our linear prediction seemingly reproduces better the training +set. However, this testing 'by the eye' is obviouly not satisfactory in the +long run. Here we have only defined the training data and our model, and +have not discussed a more rigorous approach to the cost function.











    -

    The \( \chi^2 \) function

    -
    - -

    +

    How to study our fits

    -Normally, the response (dependent or outcome) variable \( y_i \) the outcome of a numerical experiment or another type of experiment and is thus only an approximation to the true value. It is then always accompanied by an error estimate, often limited to a statistical error estimate given by the standard deviation discussed earlier. In the discussion here we will treat \( y_i \) as our exact value for the response variable. +We need more rigorous criteria in defining whether we have succeeded or +not in modeling our training data. You will be surprised to see that +many scientists seldomly venture beyond this 'by the eye' approach. A +standard approach for the cost function is the so-called \( \chi^2 \) +function -

    -Introducing the standard deviation \( \sigma_i \) for each measurement \( y_i \), we define now the \( \chi^2 \) function as -$$ -\chi^2(\hat{\beta})=\sum_{i=0}^{n-1}\frac{\left(y_i-\tilde{y}_i\right)^2}{\sigma_i^2}=\left(\hat{y}-\hat{\tilde{y}}\right)^T\frac{1}{\hat{\Sigma^2}}\left(\hat{y}-\hat{\tilde{y}}\right), +$$ \chi^2 = \frac{1}{n} +\sum_{i=0}^{n-1}\frac{(y_i-\tilde{y}_i)^2}{\sigma_i^2}, $$ -where the matrix \( \hat{\Sigma} \) is a diagonal matrix with \( \sigma_i \) as matrix elements. - - -

    - +

    +where \( \sigma_i^2 \) is the variance (to be defined later) of the entry +\( y_i \). We may not know the explicit value of \( \sigma_i^2 \), it serves +however the aim of scaling the equations and make the cost function +dimensionless.











    -

    The \( \chi^2 \) function

    -
    - -

    +

    Minimizing the cost function

    -In order to find the parameters \( \beta_i \) we will then minimize the spread of \( \chi^2(\hat{\beta}) \) by requiring -$$ -\frac{\partial \chi^2(\hat{\beta})}{\partial \beta_j} = \frac{\partial }{\partial \beta_j}\left[ \sum_{i=0}^{n-1}\left(\frac{y_i-\beta_0x_{i,0}-\beta_1x_{i,1}-\beta_2x_{i,2}-\dots-\beta_{n-1}x_{i,n-1}}{\sigma_i}\right)^2\right]=0, -$$ - -which results in -$$ -\frac{\partial \chi^2(\hat{\beta})}{\partial \beta_j} = -2\left[ \sum_{i=0}^{n-1}\frac{x_{ij}}{\sigma_i}\left(\frac{y_i-\beta_0x_{i,0}-\beta_1x_{i,1}-\beta_2x_{i,2}-\dots-\beta_{n-1}x_{i,n-1}}{\sigma_i}\right)\right]=0, -$$ - -or in a matrix-vector form as -$$ -\frac{\partial \chi^2(\hat{\beta})}{\partial \hat{\beta}} = 0 = \hat{A}^T\left( \hat{b}-\hat{A}\hat{\beta}\right). -$$ - -where we have defined the matrix \( \hat{A} =\hat{X}/\hat{\Sigma} \) with matrix elements \( a_{ij} = x_{ij}/\sigma_i \) and the vector \( \hat{b} \) with elements \( b_i = y_i/\sigma_i \). -

    - +Minimizing the cost function is a central aspect of +our discussions to come. Finding its minima as function of the model +parameters (\( \alpha \) and \( \beta \) in our case) will be a recurring +theme in these series of lectures. Essentially all machine learning +algorithms we will discuss center around the minimization of the +chosen cost function. This depends in turn on our specific +model for describing the data, a typical situation in supervised +learning. Automatizing the search for the minima of the cost function is a +central ingredient in all algorithms. Typical methods which are +employed are various variants of gradient methods. These will be +discussed in more detail later. Again, you'll be surprised to hear that +many practitioners minimize the above function ''by the eye', popularly dubbed as +'chi by the eye'. That is, change a parameter and see (visually and numerically) that +the \( \chi^2 \) function becomes smaller.











    -

    The \( \chi^2 \) function

    -
    - -

    +

    Relative error

    -We can rewrite +There are many ways to define the cost function. A simpler approach is to look at the relative difference between the training data and the predicted data, that is we define +the relative error as + $$ -\frac{\partial \chi^2(\hat{\beta})}{\partial \hat{\beta}} = 0 = \hat{A}^T\left( \hat{b}-\hat{A}\hat{\beta}\right), +\epsilon_{\mathrm{relative}}= \frac{\vert \hat{y} -\hat{\tilde{y}}\vert}{\vert \hat{y}\vert}. $$ -as -$$ -\hat{A}^T\hat{b} = \hat{A}^T\hat{A}\hat{\beta}, -$$ +We can modify easily the above Python code and plot the relative error instead +

    -and if the matrix \( \hat{A}^T\hat{A} \) is invertible we have the solution -$$ -\hat{\beta} =\left(\hat{A}^T\hat{A}\right)^{-1}\hat{A}^T\hat{b}. -$$ -

    + +
    import numpy as np
    +import matplotlib.pyplot as plt
    +from sklearn.linear_model import LinearRegression
     
    +x = np.random.rand(100,1)
    +y = 5*x+0.01*np.random.randn(100,1)
    +linreg = LinearRegression()
    +linreg.fit(x,y)
    +ypredict = linreg.predict(x)
    +
    +plt.plot(x, np.abs(ypredict-y)/abs(y), "ro")
    +plt.axis([0,1.0,0.0, 0.5])
    +plt.xlabel(r'$x$')
    +plt.ylabel(r'$\epsilon_{\mathrm{relative}}$')
    +plt.title(r'Relative error')
    +plt.show()
    +
    +

    +Depending on the parameter in front of the normal distribution, we may +have a small or larger relative error. Try to play around with +different training data sets and study (graphically) the value of the +relative error.











    -

    The \( \chi^2 \) function

    -
    - -

    +

    The richness of scikit-learn

    -If we then introduce the matrix -$$ -\hat{H} = \hat{A}^T\hat{A}, +As mentioned above, scikit-learn has an impressive functionality. +We can for example extract the values of \( \alpha \) and \( \beta \) and +their error estimates, or the variance and standard deviation and many +other properties from the statistical data analysis. + +

    +Here we show an +example of the functionality of scikit-learn. +

    + + +

    import numpy as np 
    +import matplotlib.pyplot as plt 
    +from sklearn.linear_model import LinearRegression 
    +from sklearn.metrics import mean_squared_error, r2_score, mean_squared_log_error, mean_absolute_error
    +
    +x = np.random.rand(100,1)
    +y = 2.0+ 5*x+0.5*np.random.randn(100,1)
    +linreg = LinearRegression()
    +linreg.fit(x,y)
    +ypredict = linreg.predict(x)
    +print('The intercept alpha: \n', linreg.intercept_)
    +print('Coefficient beta : \n', linreg.coef_)
    +# The mean squared error                               
    +print("Mean squared error: %.2f" % mean_squared_error(y, ypredict))
    +# Explained variance score: 1 is perfect prediction                                 
    +print('Variance score: %.2f' % r2_score(y, ypredict))
    +# Mean squared log error                                                        
    +print('Mean squared log error: %.2f' % mean_squared_log_error(y, ypredict) )
    +# Mean absolute error                                                           
    +print('Mean absolute error: %.2f' % mean_absolute_error(y, ypredict))
    +plt.plot(x, ypredict, "r-")
    +plt.plot(x, y ,'ro')
    +plt.axis([0.0,1.0,1.5, 7.0])
    +plt.xlabel(r'$x$')
    +plt.ylabel(r'$y$')
    +plt.title(r'Linear Regression fit ')
    +plt.show()
    +
    +

    +









    + +

    Functions in scikit-learn

    + +

    +The function coef gives us the parameter \( \beta \) of our fit while intercept yields +\( \alpha \). Depending on the constant in front of the normal distribution, we get values near or far from \( alpha =2 \) and \( \beta =5 \). Try to play around with different parameters in front of the normal distribution. The function meansquarederror gives us the mean square error, a risk metric corresponding to the expected value of the squared (quadratic) error or loss defined as +$$ MSE(\hat{y},\hat{\tilde{y}}) = \frac{1}{n} +\sum_{i=0}^{n-1}(y_i-\tilde{y}_i)^2, $$ -we have then the following expression for the parameters \( \beta_j \) (the matrix elements of \( \hat{H} \) are \( h_{ij} \)) -$$ -\beta_j = \sum_{k=0}^{p-1}h_{jk}\sum_{i=0}^{n-1}\frac{y_i}{\sigma_i}\frac{x_{ik}}{\sigma_i} = \sum_{k=0}^{p-1}h_{jk}\sum_{i=0}^{n-1}b_ia_{ik} -$$ - -We state without proof the expression for the uncertainty in the parameters \( \beta_j \) as -$$ -\sigma^2(\beta_j) = \sum_{i=0}^{n-1}\sigma_i^2\left( \frac{\partial \beta_j}{\partial y_i}\right)^2, -$$ - -resulting in -$$ -\sigma^2(\beta_j) = \left(\sum_{k=0}^{p-1}h_{jk}\sum_{i=0}^{n-1}a_{ik}\right)\left(\sum_{l=0}^{p-1}h_{jl}\sum_{m=0}^{n-1}a_{ml}\right) = h_{jj}! -$$ -

    - +

    +The smaller the value, the better the fit. Ideally we would like to +have an MSE equal zero. The attentive reader has probably recognized +this function as being similar to the \( \chi^2 \) function defined above.











    -

    The \( \chi^2 \) function

    -
    - +

    Other functions in scikit-learn

    +

    -The first step here is to approximate the function \( y \) with a first-order polynomial, that is we write +The r2score function computes \( R^2 \), the coefficient of +determination. It provides a measure of how well future samples are +likely to be predicted by the model. Best possible score is 1.0 and it +can be negative (because the model can be arbitrarily worse). A +constant model that always predicts the expected value of \( \hat{y} \), +disregarding the input features, would get a \( R^2 \) score of \( 0.0 \). + +

    +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 $$ -y=y(x) \rightarrow y(x_i) \approx \beta_0+\beta_1 x_i. +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}, $$ -By computing the derivatives of \( \chi^2 \) with respect to \( \beta_0 \) and \( \beta_1 \) show that these are given by +where we have defined the mean value of \( \hat{y} \) as $$ -\frac{\partial \chi^2(\hat{\beta})}{\partial \beta_0} = -2\left[ \sum_{i=0}^{1}\left(\frac{y_i-\beta_0-\beta_1x_{i}}{\sigma_i^2}\right)\right]=0, +\bar{y} = \frac{1}{n} \sum_{i=0}^{n - 1} y_i. $$ -and -$$ -\frac{\partial \chi^2(\hat{\beta})}{\partial \beta_0} = -2\left[ \sum_{i=0}^{1}x_i\left(\frac{y_i-\beta_0-\beta_1x_{i}}{\sigma_i^2}\right)\right]=0. -$$ -

    - -











    -

    The \( \chi^2 \) function

    -
    - -

    +

    The mean absolute error and other functions in scikit-learn

    -We define then +Another quantity will meet again in our discussions of regression analysis is + mean absolute error (MAE), a risk metric corresponding to the expected value of the absolute error loss or what we call the \( l1 \)-norm loss. In our discussion above we presented the relative error. +The MAE is defined as follows $$ -\gamma = \sum_{i=0}^{1}\frac{1}{\sigma_i^2}, +\text{MAE}(\hat{y}, \hat{\tilde{y}}) = \frac{1}{n} \sum_{i=0}^{n-1} \left| y_i - \tilde{y}_i \right|. $$ - +Finally we present the +squared logarithmic (quadratic) error $$ -\gamma_x = \sum_{i=0}^{1}\frac{x_{i}}{\sigma_i^2}, -$$ - -$$ -\gamma_y = \sum_{i=0}^{1}\left(\frac{y_i}{\sigma_i^2}\right), -$$ - -$$ -\gamma_{xx} = \sum_{i=0}^{1}\frac{x_ix_{i}}{\sigma_i^2}, -$$ - -$$ -\gamma_{xy} = \sum_{i=0}^{1}\frac{y_ix_{i}}{\sigma_i^2}, -$$ - -and show that -$$ -\beta_0 = \frac{\gamma_{xx}\gamma_y-\gamma_x\gamma_y}{\gamma\gamma_{xx}-\gamma_x^2}, -$$ - -$$ -\beta_1 = \frac{\gamma_{xy}\gamma-\gamma_x\gamma_y}{\gamma\gamma_{xx}-\gamma_x^2}. +\text{MSLE}(\hat{y}, \hat{\tilde{y}}) = \frac{1}{n} \sum_{i=0}^{n - 1} (\log_e (1 + y_i) - \log_e (1 + \tilde{y}_i) )^2, $$

    -The LSM suffers often from both being underdetermined and overdetermined in the unknown coefficients \( \beta_i \). A better approach is to use the Singular Value Decomposition (SVD) method discussed below. -

    - +where \( \log_e (x) \) stands for the natural logarithm of \( x \). This error +estimate is best to use when targets having exponential growth, such +as population counts, average sales of a commodity over a span of +years etc.











    -

    Simple regression model with gradient descent

    +

    Cubic polynomial in scikit-learn

    + +

    +We will discuss in more +detail these and other functions in the various lectures. We conclude this part with another example. Instead of +a linear \( x \)-dependence we study now a cubic polynomial and use the polynomial regression analysis tools of scikit-learn. + +

    + + +

    import matplotlib.pyplot as plt
    +import numpy as np
    +import random
    +from sklearn.linear_model import Ridge
    +from sklearn.preprocessing import PolynomialFeatures
    +from sklearn.pipeline import make_pipeline
    +from sklearn.linear_model import LinearRegression
    +
    +x=np.linspace(0.02,0.98,200)
    +noise = np.asarray(random.sample((range(200)),200))
    +y=x**3*noise
    +yn=x**3*100
    +poly3 = PolynomialFeatures(degree=3)
    +X = poly3.fit_transform(x[:,np.newaxis])
    +clf3 = LinearRegression()
    +clf3.fit(X,y)
    +
    +Xplot=poly3.fit_transform(x[:,np.newaxis])
    +poly3_plot=plt.plot(x, clf3.predict(Xplot), label='Cubic Fit')
    +plt.plot(x,yn, color='red', label="True Cubic")
    +plt.scatter(x, y, label='Data', color='orange', s=15)
    +plt.legend()
    +plt.show()
    +
    +def error(a):
    +    for i in y:
    +        err=(y-yn)/yn
    +    return abs(np.sum(err))/len(err)
    +
    +print (error(y))
    +
    +

    +Similarly, using R, we can perform similar studies. +(more details on R will be inserted later). + +

    +









    + +

    Simple regression model with gradient descent

    Add info about the equations, play around with different learning rates

    @@ -772,7 +1112,7 @@ plt.show()











    -

    Simple regression model with stochastic gradient descent

    +

    Simple regression model with stochastic gradient descent

    Add info about the equations, play around with different learning rates

    @@ -797,7 +1137,7 @@ sgdreg.fit(x,y.ravel())











    -

    Polynomial Regression

    +

    Polynomial Regression

    @@ -828,7 +1168,7 @@ plt.show()











    -

    Ridge and Lasso Regression

    +

    Ridge and Lasso Regression

    @@ -909,7 +1249,7 @@ plt.show()











    -

    The singular value decompostion

    +

    The singular value decompostion

    @@ -920,6 +1260,26 @@ $$

    +

    +Add codes and discuss this in connection with lasso and ridge, show example where the standard inversion of a matrix fails and where SVD comes to rescue + +

    +









    + +

    Lasso and Ridge regression

    + +

    +Discuss the mathematics here + +

    +









    + +

    Logistic regression

    +Add discussion about classification versus regression, show examples of more than two cases and why regression is not the best approach. Motivate for k-nearest neighbors + +

    +Add examples on classification problems +

    diff --git a/doc/pub/Regression/html/Regression.html b/doc/pub/Regression/html/Regression.html index 6df5f8227..c55ca0610 100644 --- a/doc/pub/Regression/html/Regression.html +++ b/doc/pub/Regression/html/Regression.html @@ -98,33 +98,47 @@ div { text-align: justify; text-justify: inter-word; } 2, None, '___sec10'), - ('Simple regression model', 2, None, '___sec11'), + ('The $\\chi^2$ function', 2, None, '___sec11'), + ('The $\\chi^2$ function', 2, None, '___sec12'), + ('The $\\chi^2$ function', 2, None, '___sec13'), + ('The $\\chi^2$ function', 2, None, '___sec14'), + ('The $\\chi^2$ function', 2, None, '___sec15'), + ('The $\\chi^2$ function', 2, None, '___sec16'), + ('Simple regression model', 2, None, '___sec17'), ('Simple regression model, now using _scikit-learn_', 2, None, - '___sec12'), - ('Correlations and the quality of our results', + '___sec18'), + ('Simple linear regression model using _scikit-learn_', 2, None, - '___sec13'), - ('Estimate of the error', 2, None, '___sec14'), - ('The $\\chi^2$ function', 2, None, '___sec15'), - ('The $\\chi^2$ function', 2, None, '___sec16'), - ('The $\\chi^2$ function', 2, None, '___sec17'), - ('The $\\chi^2$ function', 2, None, '___sec18'), - ('The $\\chi^2$ function', 2, None, '___sec19'), - ('The $\\chi^2$ function', 2, None, '___sec20'), + '___sec19'), + ('Simple linear regression model', 2, None, '___sec20'), + ('Less noise', 2, None, '___sec21'), + ('How to study our fits', 2, None, '___sec22'), + ('Minimizing the cost function', 2, None, '___sec23'), + ('Relative error', 2, None, '___sec24'), + ('The richness of _scikit-learn_', 2, None, '___sec25'), + ('Functions in _scikit-learn_', 2, None, '___sec26'), + ('Other functions in _scikit-learn_', 2, None, '___sec27'), + ('The mean absolute error and other functions in _scikit-learn_', + 2, + None, + '___sec28'), + ('Cubic polynomial in _scikit-learn_', 2, None, '___sec29'), ('Simple regression model with gradient descent', 2, None, - '___sec21'), + '___sec30'), ('Simple regression model with stochastic gradient descent', 2, None, - '___sec22'), - ('Polynomial Regression', 2, None, '___sec23'), - ('Ridge and Lasso Regression', 2, None, '___sec24'), - ('The singular value decompostion', 2, None, '___sec25')]} + '___sec31'), + ('Polynomial Regression', 2, None, '___sec32'), + ('Ridge and Lasso Regression', 2, None, '___sec33'), + ('The singular value decompostion', 2, None, '___sec34'), + ('Lasso and Ridge regression', 2, None, '___sec35'), + ('Logistic regression', 2, None, '___sec36')]} end of tocinfo --> @@ -166,7 +180,7 @@ MathJax.Hub.Config({

    [2] Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University

    -

    May 30, 2018

    +

    Aug 24, 2018












    @@ -466,7 +480,184 @@ meaning that the solution for \( \hat{\beta} \) is the one which minimizes the r











    -

    Simple regression model

    +

    The \( \chi^2 \) function

    +
    + +

    + +

    +Normally, the response (dependent or outcome) variable \( y_i \) the outcome of a numerical experiment or another type of experiment and is thus only an approximation to the true value. It is then always accompanied by an error estimate, often limited to a statistical error estimate given by the standard deviation discussed earlier. In the discussion here we will treat \( y_i \) as our exact value for the response variable. + +

    +Introducing the standard deviation \( \sigma_i \) for each measurement \( y_i \), we define now the \( \chi^2 \) function as +$$ +\chi^2(\hat{\beta})=\sum_{i=0}^{n-1}\frac{\left(y_i-\tilde{y}_i\right)^2}{\sigma_i^2}=\left(\hat{y}-\hat{\tilde{y}}\right)^T\frac{1}{\hat{\Sigma^2}}\left(\hat{y}-\hat{\tilde{y}}\right), +$$ + +where the matrix \( \hat{\Sigma} \) is a diagonal matrix with \( \sigma_i \) as matrix elements. + + +

    + + +

    +









    + +

    The \( \chi^2 \) function

    +
    + +

    + +

    +In order to find the parameters \( \beta_i \) we will then minimize the spread of \( \chi^2(\hat{\beta}) \) by requiring +$$ +\frac{\partial \chi^2(\hat{\beta})}{\partial \beta_j} = \frac{\partial }{\partial \beta_j}\left[ \sum_{i=0}^{n-1}\left(\frac{y_i-\beta_0x_{i,0}-\beta_1x_{i,1}-\beta_2x_{i,2}-\dots-\beta_{n-1}x_{i,n-1}}{\sigma_i}\right)^2\right]=0, +$$ + +which results in +$$ +\frac{\partial \chi^2(\hat{\beta})}{\partial \beta_j} = -2\left[ \sum_{i=0}^{n-1}\frac{x_{ij}}{\sigma_i}\left(\frac{y_i-\beta_0x_{i,0}-\beta_1x_{i,1}-\beta_2x_{i,2}-\dots-\beta_{n-1}x_{i,n-1}}{\sigma_i}\right)\right]=0, +$$ + +or in a matrix-vector form as +$$ +\frac{\partial \chi^2(\hat{\beta})}{\partial \hat{\beta}} = 0 = \hat{A}^T\left( \hat{b}-\hat{A}\hat{\beta}\right). +$$ + +where we have defined the matrix \( \hat{A} =\hat{X}/\hat{\Sigma} \) with matrix elements \( a_{ij} = x_{ij}/\sigma_i \) and the vector \( \hat{b} \) with elements \( b_i = y_i/\sigma_i \). +

    + + +

    +









    + +

    The \( \chi^2 \) function

    +
    + +

    + +

    +We can rewrite +$$ +\frac{\partial \chi^2(\hat{\beta})}{\partial \hat{\beta}} = 0 = \hat{A}^T\left( \hat{b}-\hat{A}\hat{\beta}\right), +$$ + +as +$$ +\hat{A}^T\hat{b} = \hat{A}^T\hat{A}\hat{\beta}, +$$ + +and if the matrix \( \hat{A}^T\hat{A} \) is invertible we have the solution +$$ +\hat{\beta} =\left(\hat{A}^T\hat{A}\right)^{-1}\hat{A}^T\hat{b}. +$$ +

    + + +

    +









    + +

    The \( \chi^2 \) function

    +
    + +

    + +

    +If we then introduce the matrix +$$ +\hat{H} = \left(\hat{A}^T\hat{A}\right)^{-1}, +$$ + +we have then the following expression for the parameters \( \beta_j \) (the matrix elements of \( \hat{H} \) are \( h_{ij} \)) +$$ +\beta_j = \sum_{k=0}^{p-1}h_{jk}\sum_{i=0}^{n-1}\frac{y_i}{\sigma_i}\frac{x_{ik}}{\sigma_i} = \sum_{k=0}^{p-1}h_{jk}\sum_{i=0}^{n-1}b_ia_{ik} +$$ + +We state without proof the expression for the uncertainty in the parameters \( \beta_j \) as (we leave this as an exercise) +$$ +\sigma^2(\beta_j) = \sum_{i=0}^{n-1}\sigma_i^2\left( \frac{\partial \beta_j}{\partial y_i}\right)^2, +$$ + +resulting in +$$ +\sigma^2(\beta_j) = \left(\sum_{k=0}^{p-1}h_{jk}\sum_{i=0}^{n-1}a_{ik}\right)\left(\sum_{l=0}^{p-1}h_{jl}\sum_{m=0}^{n-1}a_{ml}\right) = h_{jj}! +$$ +

    + + +

    +









    + +

    The \( \chi^2 \) function

    +
    + +

    +The first step here is to approximate the function \( y \) with a first-order polynomial, that is we write +$$ +y=y(x) \rightarrow y(x_i) \approx \beta_0+\beta_1 x_i. +$$ + +By computing the derivatives of \( \chi^2 \) with respect to \( \beta_0 \) and \( \beta_1 \) show that these are given by +$$ +\frac{\partial \chi^2(\hat{\beta})}{\partial \beta_0} = -2\left[ \sum_{i=0}^{n-1}\left(\frac{y_i-\beta_0-\beta_1x_{i}}{\sigma_i^2}\right)\right]=0, +$$ + +and +$$ +\frac{\partial \chi^2(\hat{\beta})}{\partial \beta_0} = -2\left[ \sum_{i=0}^{n-1}x_i\left(\frac{y_i-\beta_0-\beta_1x_{i}}{\sigma_i^2}\right)\right]=0. +$$ +

    + + +

    +









    + +

    The \( \chi^2 \) function

    +
    + +

    + +

    +We define then +$$ +\gamma = \sum_{i=0}^{1}\frac{n-1}{\sigma_i^2}, +$$ + + +$$ +\gamma_x = \sum_{i=0}^{n-1}\frac{x_{i}}{\sigma_i^2}, +$$ + +$$ +\gamma_y = \sum_{i=0}^{n-1}\left(\frac{y_i}{\sigma_i^2}\right), +$$ + +$$ +\gamma_{xx} = \sum_{i=0}^{n-1}\frac{x_ix_{i}}{\sigma_i^2}, +$$ + +$$ +\gamma_{xy} = \sum_{i=0}^{n-1}\frac{y_ix_{i}}{\sigma_i^2}, +$$ + +and show that +$$ +\beta_0 = \frac{\gamma_{xx}\gamma_y-\gamma_x\gamma_y}{\gamma\gamma_{xx}-\gamma_x^2}, +$$ + +$$ +\beta_1 = \frac{\gamma_{xy}\gamma-\gamma_x\gamma_y}{\gamma\gamma_{xx}-\gamma_x^2}. +$$ + +

    +The LSM suffers often from both being underdetermined and overdetermined in the unknown coefficients \( \beta_i \). A better approach is to use the Singular Value Decomposition (SVD) method discussed below. +

    + + +

    +









    + +

    Simple regression model

    We are now ready to write our first program which aims at solving the above linear regression equations. We start with data we have produced ourselves, in this case normally distributed random numbers along the \( x \)-axis. These numbers define then the value of a function \( y(x)=4+3x+N(0,1) \). Thereafter we order the \( x \) values and employ our linear regression algorithm to set up the best fit. Here we find it useful to use the numpy function \( c\_ \) arrays where arrays are stacked along their last axis after being upgraded to at least two dimensions with ones post-pended to the shape. The following examples help in understanding what happens

    @@ -487,10 +678,10 @@ x = 2*np y = 4+3*x+np.random.randn(100,1) xb = np.c_[np.ones((100,1)), x] -theta = np.linalg.inv(xb.T.dot(xb)).dot(xb.T).dot(y) +beta = np.linalg.inv(xb.T.dot(xb)).dot(xb.T).dot(y) xnew = np.array([[0],[2]]) xbnew = np.c_[np.ones((2,1)), xnew] -ypredict = xbnew.dot(theta) +ypredict = xbnew.dot(beta) plt.plot(xnew, ypredict, "r-") plt.plot(x, y ,'ro') @@ -506,7 +697,7 @@ We see that, as expected, a linear fit gives a seemingly (from the graph) good r











    -

    Simple regression model, now using scikit-learn

    +

    Simple regression model, now using scikit-learn

    We can repeat the above algorithm using scikit-learn as follows @@ -537,203 +728,352 @@ plt.show()











    -

    Correlations and the quality of our results

    -In order to test the quality of our fit, there are several measures which can be implemented. One is the so-called -correlation function defined as +

    Simple linear regression model using scikit-learn

    + +

    +We start with perhaps our simplest possible example, using scikit-learn to perform linear regression analysis on a data set produced by us. +What follows is a simple Python code where we have defined function \( y \) in terms of the variable \( x \). Both are defined as vectors of dimension \( 1\times 100 \). The entries to the vector \( \hat{x} \) are given by random numbers generated with a uniform distribution with entries \( x_i \in [0,1] \) (more about probability distribution functions later). These values are then used to define a function \( y(x) \) (tabulated again as a vector) with a linear dependence on \( x \) plus a random noise added via the normal distribution. + +

    +The Numpy functions are imported used the import numpy as np +statement and the random number generator for the uniform distribution +is called using the function np.random.rand(), where we specificy +that we want \( 100 \) random variables. Using Numpy we define +automatically an array with the specified number of elements, \( 100 \) in +our case. With the Numpy function randn() we can compute random +numbers with the normal distribution (mean value \( \mu \) equal to zero and +variance \( \sigma^2 \) set to one) and produce the values of \( y \) assuming a linear +dependence as function of \( x \) + $$ -\mathrm{Corr}(X,Y) = \frac{\sum_{i=1}^n(x_i-\overline{x})(y_i-\overline{y})}{\sqrt{\sum_{i=1}^n(x_i-\overline{x})^2}\sqrt{\sum_{i=1}^n(y_i-\overline{y})^2} } +y = 2x+N(0,1), $$

    -Another quantity is the autocorrelation function we discussed in our chapter on statistical analysis. -Let us now try to assess the quality of our fit by studying various measures. +where \( N(0,1) \) represents random numbers generated by the normal +distribution. From scikit-learn we import then the +LinearRegression functionality and make a prediction \( \tilde{y} = +\alpha + \beta x \) using the function fit(x,y). We call the set of +data \( (\hat{x},\hat{y}) \) for our training data. The Python package +scikit-learn has also a functionality which extracts the above +fitting parameters \( \alpha \) and \( \beta \) (see below). Later we will +distinguish between training data and test data. + +

    +For plotting we use the Python package +matplotlib which produces publication +quality figures. Feel free to explore the extensive +gallery of examples. In +this example we plot our original values of \( x \) and \( y \) as well as the +prediction ypredict (\( \tilde{y} \)), which attempts at fitting our +data with a straight line. + +

    +The Python code follows here. +

    + + +

    # Importing various packages
    +import numpy as np
    +import matplotlib.pyplot as plt
    +from sklearn.linear_model import LinearRegression
    +
    +x = np.random.rand(100,1)
    +y = 2*x+np.random.randn(100,1)
    +linreg = LinearRegression()
    +linreg.fit(x,y)
    +xnew = np.array([[0],[1]])
    +ypredict = linreg.predict(xnew)
    +
    +plt.plot(xnew, ypredict, "r-")
    +plt.plot(x, y ,'ro')
    +plt.axis([0,1.0,0, 5.0])
    +plt.xlabel(r'$x$')
    +plt.ylabel(r'$y$')
    +plt.title(r'Simple Linear Regression')
    +plt.show()
    +
    +

    +









    + +

    Simple linear regression model

    + +

    +This example serves several aims. It allows us to demonstrate several +aspects of data analysis and later machine learning algorithms. The +immediate visualization shows that our linear fit is not +impressive. It goes through the data points, but there are many +outliers which are not reproduced by our linear regression. We could +now play around with this small program and change for example the +factor in front of \( x \) and the normal distribution. Try to change the +function \( y \) to + +$$ +y = 10x+0.01 \times N(0,1), +$$ + +

    +where \( x \) is defined as before.











    -

    Estimate of the error

    +

    Less noise

    + +

    +Does the fit look better? Indeed, by +reducing the role of the normal distribution we see immediately that +our linear prediction seemingly reproduces better the training +set. However, this testing 'by the eye' is obviouly not satisfactory in the +long run. Here we have only defined the training data and our model, and +have not discussed a more rigorous approach to the cost function.











    -

    The \( \chi^2 \) function

    -
    - -

    +

    How to study our fits

    -Normally, the response (dependent or outcome) variable \( y_i \) the outcome of a numerical experiment or another type of experiment and is thus only an approximation to the true value. It is then always accompanied by an error estimate, often limited to a statistical error estimate given by the standard deviation discussed earlier. In the discussion here we will treat \( y_i \) as our exact value for the response variable. +We need more rigorous criteria in defining whether we have succeeded or +not in modeling our training data. You will be surprised to see that +many scientists seldomly venture beyond this 'by the eye' approach. A +standard approach for the cost function is the so-called \( \chi^2 \) +function -

    -Introducing the standard deviation \( \sigma_i \) for each measurement \( y_i \), we define now the \( \chi^2 \) function as -$$ -\chi^2(\hat{\beta})=\sum_{i=0}^{n-1}\frac{\left(y_i-\tilde{y}_i\right)^2}{\sigma_i^2}=\left(\hat{y}-\hat{\tilde{y}}\right)^T\frac{1}{\hat{\Sigma^2}}\left(\hat{y}-\hat{\tilde{y}}\right), +$$ \chi^2 = \frac{1}{n} +\sum_{i=0}^{n-1}\frac{(y_i-\tilde{y}_i)^2}{\sigma_i^2}, $$ -where the matrix \( \hat{\Sigma} \) is a diagonal matrix with \( \sigma_i \) as matrix elements. - - -

    - +

    +where \( \sigma_i^2 \) is the variance (to be defined later) of the entry +\( y_i \). We may not know the explicit value of \( \sigma_i^2 \), it serves +however the aim of scaling the equations and make the cost function +dimensionless.











    -

    The \( \chi^2 \) function

    -
    - -

    +

    Minimizing the cost function

    -In order to find the parameters \( \beta_i \) we will then minimize the spread of \( \chi^2(\hat{\beta}) \) by requiring -$$ -\frac{\partial \chi^2(\hat{\beta})}{\partial \beta_j} = \frac{\partial }{\partial \beta_j}\left[ \sum_{i=0}^{n-1}\left(\frac{y_i-\beta_0x_{i,0}-\beta_1x_{i,1}-\beta_2x_{i,2}-\dots-\beta_{n-1}x_{i,n-1}}{\sigma_i}\right)^2\right]=0, -$$ - -which results in -$$ -\frac{\partial \chi^2(\hat{\beta})}{\partial \beta_j} = -2\left[ \sum_{i=0}^{n-1}\frac{x_{ij}}{\sigma_i}\left(\frac{y_i-\beta_0x_{i,0}-\beta_1x_{i,1}-\beta_2x_{i,2}-\dots-\beta_{n-1}x_{i,n-1}}{\sigma_i}\right)\right]=0, -$$ - -or in a matrix-vector form as -$$ -\frac{\partial \chi^2(\hat{\beta})}{\partial \hat{\beta}} = 0 = \hat{A}^T\left( \hat{b}-\hat{A}\hat{\beta}\right). -$$ - -where we have defined the matrix \( \hat{A} =\hat{X}/\hat{\Sigma} \) with matrix elements \( a_{ij} = x_{ij}/\sigma_i \) and the vector \( \hat{b} \) with elements \( b_i = y_i/\sigma_i \). -

    - +Minimizing the cost function is a central aspect of +our discussions to come. Finding its minima as function of the model +parameters (\( \alpha \) and \( \beta \) in our case) will be a recurring +theme in these series of lectures. Essentially all machine learning +algorithms we will discuss center around the minimization of the +chosen cost function. This depends in turn on our specific +model for describing the data, a typical situation in supervised +learning. Automatizing the search for the minima of the cost function is a +central ingredient in all algorithms. Typical methods which are +employed are various variants of gradient methods. These will be +discussed in more detail later. Again, you'll be surprised to hear that +many practitioners minimize the above function ''by the eye', popularly dubbed as +'chi by the eye'. That is, change a parameter and see (visually and numerically) that +the \( \chi^2 \) function becomes smaller.











    -

    The \( \chi^2 \) function

    -
    - -

    +

    Relative error

    -We can rewrite +There are many ways to define the cost function. A simpler approach is to look at the relative difference between the training data and the predicted data, that is we define +the relative error as + $$ -\frac{\partial \chi^2(\hat{\beta})}{\partial \hat{\beta}} = 0 = \hat{A}^T\left( \hat{b}-\hat{A}\hat{\beta}\right), +\epsilon_{\mathrm{relative}}= \frac{\vert \hat{y} -\hat{\tilde{y}}\vert}{\vert \hat{y}\vert}. $$ -as -$$ -\hat{A}^T\hat{b} = \hat{A}^T\hat{A}\hat{\beta}, -$$ +We can modify easily the above Python code and plot the relative error instead +

    -and if the matrix \( \hat{A}^T\hat{A} \) is invertible we have the solution -$$ -\hat{\beta} =\left(\hat{A}^T\hat{A}\right)^{-1}\hat{A}^T\hat{b}. -$$ -

    + +
    import numpy as np
    +import matplotlib.pyplot as plt
    +from sklearn.linear_model import LinearRegression
     
    +x = np.random.rand(100,1)
    +y = 5*x+0.01*np.random.randn(100,1)
    +linreg = LinearRegression()
    +linreg.fit(x,y)
    +ypredict = linreg.predict(x)
    +
    +plt.plot(x, np.abs(ypredict-y)/abs(y), "ro")
    +plt.axis([0,1.0,0.0, 0.5])
    +plt.xlabel(r'$x$')
    +plt.ylabel(r'$\epsilon_{\mathrm{relative}}$')
    +plt.title(r'Relative error')
    +plt.show()
    +
    +

    +Depending on the parameter in front of the normal distribution, we may +have a small or larger relative error. Try to play around with +different training data sets and study (graphically) the value of the +relative error.











    -

    The \( \chi^2 \) function

    -
    - -

    +

    The richness of scikit-learn

    -If we then introduce the matrix -$$ -\hat{H} = \hat{A}^T\hat{A}, +As mentioned above, scikit-learn has an impressive functionality. +We can for example extract the values of \( \alpha \) and \( \beta \) and +their error estimates, or the variance and standard deviation and many +other properties from the statistical data analysis. + +

    +Here we show an +example of the functionality of scikit-learn. +

    + + +

    import numpy as np 
    +import matplotlib.pyplot as plt 
    +from sklearn.linear_model import LinearRegression 
    +from sklearn.metrics import mean_squared_error, r2_score, mean_squared_log_error, mean_absolute_error
    +
    +x = np.random.rand(100,1)
    +y = 2.0+ 5*x+0.5*np.random.randn(100,1)
    +linreg = LinearRegression()
    +linreg.fit(x,y)
    +ypredict = linreg.predict(x)
    +print('The intercept alpha: \n', linreg.intercept_)
    +print('Coefficient beta : \n', linreg.coef_)
    +# The mean squared error                               
    +print("Mean squared error: %.2f" % mean_squared_error(y, ypredict))
    +# Explained variance score: 1 is perfect prediction                                 
    +print('Variance score: %.2f' % r2_score(y, ypredict))
    +# Mean squared log error                                                        
    +print('Mean squared log error: %.2f' % mean_squared_log_error(y, ypredict) )
    +# Mean absolute error                                                           
    +print('Mean absolute error: %.2f' % mean_absolute_error(y, ypredict))
    +plt.plot(x, ypredict, "r-")
    +plt.plot(x, y ,'ro')
    +plt.axis([0.0,1.0,1.5, 7.0])
    +plt.xlabel(r'$x$')
    +plt.ylabel(r'$y$')
    +plt.title(r'Linear Regression fit ')
    +plt.show()
    +
    +

    +









    + +

    Functions in scikit-learn

    + +

    +The function coef gives us the parameter \( \beta \) of our fit while intercept yields +\( \alpha \). Depending on the constant in front of the normal distribution, we get values near or far from \( alpha =2 \) and \( \beta =5 \). Try to play around with different parameters in front of the normal distribution. The function meansquarederror gives us the mean square error, a risk metric corresponding to the expected value of the squared (quadratic) error or loss defined as +$$ MSE(\hat{y},\hat{\tilde{y}}) = \frac{1}{n} +\sum_{i=0}^{n-1}(y_i-\tilde{y}_i)^2, $$ -we have then the following expression for the parameters \( \beta_j \) (the matrix elements of \( \hat{H} \) are \( h_{ij} \)) -$$ -\beta_j = \sum_{k=0}^{p-1}h_{jk}\sum_{i=0}^{n-1}\frac{y_i}{\sigma_i}\frac{x_{ik}}{\sigma_i} = \sum_{k=0}^{p-1}h_{jk}\sum_{i=0}^{n-1}b_ia_{ik} -$$ - -We state without proof the expression for the uncertainty in the parameters \( \beta_j \) as -$$ -\sigma^2(\beta_j) = \sum_{i=0}^{n-1}\sigma_i^2\left( \frac{\partial \beta_j}{\partial y_i}\right)^2, -$$ - -resulting in -$$ -\sigma^2(\beta_j) = \left(\sum_{k=0}^{p-1}h_{jk}\sum_{i=0}^{n-1}a_{ik}\right)\left(\sum_{l=0}^{p-1}h_{jl}\sum_{m=0}^{n-1}a_{ml}\right) = h_{jj}! -$$ -

    - +

    +The smaller the value, the better the fit. Ideally we would like to +have an MSE equal zero. The attentive reader has probably recognized +this function as being similar to the \( \chi^2 \) function defined above.











    -

    The \( \chi^2 \) function

    -
    - +

    Other functions in scikit-learn

    +

    -The first step here is to approximate the function \( y \) with a first-order polynomial, that is we write +The r2score function computes \( R^2 \), the coefficient of +determination. It provides a measure of how well future samples are +likely to be predicted by the model. Best possible score is 1.0 and it +can be negative (because the model can be arbitrarily worse). A +constant model that always predicts the expected value of \( \hat{y} \), +disregarding the input features, would get a \( R^2 \) score of \( 0.0 \). + +

    +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 $$ -y=y(x) \rightarrow y(x_i) \approx \beta_0+\beta_1 x_i. +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}, $$ -By computing the derivatives of \( \chi^2 \) with respect to \( \beta_0 \) and \( \beta_1 \) show that these are given by +where we have defined the mean value of \( \hat{y} \) as $$ -\frac{\partial \chi^2(\hat{\beta})}{\partial \beta_0} = -2\left[ \sum_{i=0}^{1}\left(\frac{y_i-\beta_0-\beta_1x_{i}}{\sigma_i^2}\right)\right]=0, +\bar{y} = \frac{1}{n} \sum_{i=0}^{n - 1} y_i. $$ -and -$$ -\frac{\partial \chi^2(\hat{\beta})}{\partial \beta_0} = -2\left[ \sum_{i=0}^{1}x_i\left(\frac{y_i-\beta_0-\beta_1x_{i}}{\sigma_i^2}\right)\right]=0. -$$ -

    - -











    -

    The \( \chi^2 \) function

    -
    - -

    +

    The mean absolute error and other functions in scikit-learn

    -We define then +Another quantity will meet again in our discussions of regression analysis is + mean absolute error (MAE), a risk metric corresponding to the expected value of the absolute error loss or what we call the \( l1 \)-norm loss. In our discussion above we presented the relative error. +The MAE is defined as follows $$ -\gamma = \sum_{i=0}^{1}\frac{1}{\sigma_i^2}, +\text{MAE}(\hat{y}, \hat{\tilde{y}}) = \frac{1}{n} \sum_{i=0}^{n-1} \left| y_i - \tilde{y}_i \right|. $$ - +Finally we present the +squared logarithmic (quadratic) error $$ -\gamma_x = \sum_{i=0}^{1}\frac{x_{i}}{\sigma_i^2}, -$$ - -$$ -\gamma_y = \sum_{i=0}^{1}\left(\frac{y_i}{\sigma_i^2}\right), -$$ - -$$ -\gamma_{xx} = \sum_{i=0}^{1}\frac{x_ix_{i}}{\sigma_i^2}, -$$ - -$$ -\gamma_{xy} = \sum_{i=0}^{1}\frac{y_ix_{i}}{\sigma_i^2}, -$$ - -and show that -$$ -\beta_0 = \frac{\gamma_{xx}\gamma_y-\gamma_x\gamma_y}{\gamma\gamma_{xx}-\gamma_x^2}, -$$ - -$$ -\beta_1 = \frac{\gamma_{xy}\gamma-\gamma_x\gamma_y}{\gamma\gamma_{xx}-\gamma_x^2}. +\text{MSLE}(\hat{y}, \hat{\tilde{y}}) = \frac{1}{n} \sum_{i=0}^{n - 1} (\log_e (1 + y_i) - \log_e (1 + \tilde{y}_i) )^2, $$

    -The LSM suffers often from both being underdetermined and overdetermined in the unknown coefficients \( \beta_i \). A better approach is to use the Singular Value Decomposition (SVD) method discussed below. -

    - +where \( \log_e (x) \) stands for the natural logarithm of \( x \). This error +estimate is best to use when targets having exponential growth, such +as population counts, average sales of a commodity over a span of +years etc.











    -

    Simple regression model with gradient descent

    +

    Cubic polynomial in scikit-learn

    + +

    +We will discuss in more +detail these and other functions in the various lectures. We conclude this part with another example. Instead of +a linear \( x \)-dependence we study now a cubic polynomial and use the polynomial regression analysis tools of scikit-learn. + +

    + + +

    import matplotlib.pyplot as plt
    +import numpy as np
    +import random
    +from sklearn.linear_model import Ridge
    +from sklearn.preprocessing import PolynomialFeatures
    +from sklearn.pipeline import make_pipeline
    +from sklearn.linear_model import LinearRegression
    +
    +x=np.linspace(0.02,0.98,200)
    +noise = np.asarray(random.sample((range(200)),200))
    +y=x**3*noise
    +yn=x**3*100
    +poly3 = PolynomialFeatures(degree=3)
    +X = poly3.fit_transform(x[:,np.newaxis])
    +clf3 = LinearRegression()
    +clf3.fit(X,y)
    +
    +Xplot=poly3.fit_transform(x[:,np.newaxis])
    +poly3_plot=plt.plot(x, clf3.predict(Xplot), label='Cubic Fit')
    +plt.plot(x,yn, color='red', label="True Cubic")
    +plt.scatter(x, y, label='Data', color='orange', s=15)
    +plt.legend()
    +plt.show()
    +
    +def error(a):
    +    for i in y:
    +        err=(y-yn)/yn
    +    return abs(np.sum(err))/len(err)
    +
    +print (error(y))
    +
    +

    +Similarly, using R, we can perform similar studies. +(more details on R will be inserted later). + +

    +









    + +

    Simple regression model with gradient descent

    Add info about the equations, play around with different learning rates

    @@ -777,7 +1117,7 @@ plt.show()











    -

    Simple regression model with stochastic gradient descent

    +

    Simple regression model with stochastic gradient descent

    Add info about the equations, play around with different learning rates

    @@ -802,7 +1142,7 @@ sgdreg.fit(x,y.











    -

    Polynomial Regression

    +

    Polynomial Regression

    @@ -833,7 +1173,7 @@ plt.show()











    -

    Ridge and Lasso Regression

    +

    Ridge and Lasso Regression

    @@ -914,7 +1254,7 @@ plt.show()











    -

    The singular value decompostion

    +

    The singular value decompostion

    @@ -925,6 +1265,26 @@ $$

    +

    +Add codes and discuss this in connection with lasso and ridge, show example where the standard inversion of a matrix fails and where SVD comes to rescue + +

    +









    + +

    Lasso and Ridge regression

    + +

    +Discuss the mathematics here + +

    +









    + +

    Logistic regression

    +Add discussion about classification versus regression, show examples of more than two cases and why regression is not the best approach. Motivate for k-nearest neighbors + +

    +Add examples on classification problems +

    diff --git a/doc/pub/Regression/ipynb/ipynb-Regression-src.tar.gz b/doc/pub/Regression/ipynb/ipynb-Regression-src.tar.gz index 5cdf91404..ec5a99105 100644 Binary files a/doc/pub/Regression/ipynb/ipynb-Regression-src.tar.gz and b/doc/pub/Regression/ipynb/ipynb-Regression-src.tar.gz differ diff --git a/doc/pub/Regression/pdf/Regression-beamer-handouts2x3.pdf b/doc/pub/Regression/pdf/Regression-beamer-handouts2x3.pdf index ea384ecc2..2298c24e0 100644 Binary files a/doc/pub/Regression/pdf/Regression-beamer-handouts2x3.pdf and b/doc/pub/Regression/pdf/Regression-beamer-handouts2x3.pdf differ diff --git a/doc/pub/Regression/pdf/Regression-beamer.pdf b/doc/pub/Regression/pdf/Regression-beamer.pdf index af2cf8798..8c2c84883 100644 Binary files a/doc/pub/Regression/pdf/Regression-beamer.pdf and b/doc/pub/Regression/pdf/Regression-beamer.pdf differ diff --git a/doc/pub/Regression/pdf/Regression-minted.pdf b/doc/pub/Regression/pdf/Regression-minted.pdf index e99942d86..57767b543 100644 Binary files a/doc/pub/Regression/pdf/Regression-minted.pdf and b/doc/pub/Regression/pdf/Regression-minted.pdf differ diff --git a/doc/src/Regression/Regression.do.txt b/doc/src/Regression/Regression.do.txt index 5600d4ce2..783a0975e 100644 --- a/doc/src/Regression/Regression.do.txt +++ b/doc/src/Regression/Regression.do.txt @@ -267,89 +267,6 @@ meaning that the solution for $\hat{\beta}$ is the one which minimizes the resid !eblock -!split -===== Simple regression model ===== -We are now ready to write our first program which aims at solving the above linear regression equations. We start with data we have produced ourselves, in this case normally distributed random numbers along the $x$-axis. These numbers define then the value of a function $y(x)=4+3x+N(0,1)$. Thereafter we order the $x$ values and employ our linear regression algorithm to set up the best fit. Here we find it useful to use the numpy function $c\_$ arrays where arrays are stacked along their last axis after being upgraded to at least two dimensions with ones post-pended to the shape. The following examples help in understanding what happens -!bc pycod -import numpy as np -print(np.c_[np.array([1,2,3]), np.array([4,5,6])]) -print(np.c_[np.array([[1,2,3]]), 0, 0, np.array([[4,5,6]])]) -!ec - -!bc pycod -# Importing various packages -from random import random, seed -import numpy as np -import matplotlib.pyplot as plt - -x = 2*np.random.rand(100,1) -y = 4+3*x+np.random.randn(100,1) - -xb = np.c_[np.ones((100,1)), x] -theta = np.linalg.inv(xb.T.dot(xb)).dot(xb.T).dot(y) -xnew = np.array([[0],[2]]) -xbnew = np.c_[np.ones((2,1)), xnew] -ypredict = xbnew.dot(theta) - -plt.plot(xnew, ypredict, "r-") -plt.plot(x, y ,'ro') -plt.axis([0,2.0,0, 15.0]) -plt.xlabel(r'$x$') -plt.ylabel(r'$y$') -plt.title(r'Linear Regression') -plt.show() - -!ec - -We see that, as expected, a linear fit gives a seemingly (from the graph) good representation of the data. - - - - -!split -===== Simple regression model, now using _scikit-learn_ ===== - - -We can repeat the above algorithm using _scikit-learn_ as follows -!bc pycod -# Importing various packages -from random import random, seed -import numpy as np -import matplotlib.pyplot as plt -from sklearn.linear_model import LinearRegression - -x = 2*np.random.rand(100,1) -y = 4+3*x+np.random.randn(100,1) -linreg = LinearRegression() -linreg.fit(x,y) -xnew = np.array([[0],[2]]) -ypredict = linreg.predict(xnew) - -plt.plot(xnew, ypredict, "r-") -plt.plot(x, y ,'ro') -plt.axis([0,2.0,0, 15.0]) -plt.xlabel(r'$x$') -plt.ylabel(r'$y$') -plt.title(r'Random numbers ') -plt.show() -!ec - - -!split -===== Correlations and the quality of our results ===== -In order to test the quality of our fit, there are several measures which can be implemented. One is the so-called -correlation function defined as -!bt -\[ -\mathrm{Corr}(X,Y) = \frac{\sum_{i=1}^n(x_i-\overline{x})(y_i-\overline{y})}{\sqrt{\sum_{i=1}^n(x_i-\overline{x})^2}\sqrt{\sum_{i=1}^n(y_i-\overline{y})^2} } -\] -!et - -Another quantity is the autocorrelation function we discussed in our chapter on statistical analysis. -Let us now try to assess the quality of our fit by studying various measures. - -!split -===== Estimate of the error ===== !split ===== The $\chi^2$ function ===== @@ -423,7 +340,7 @@ and if the matrix $\hat{A}^T\hat{A}$ is invertible we have the solution If we then introduce the matrix !bt \[ -\hat{H} = \hat{A}^T\hat{A}, +\hat{H} = \left(\hat{A}^T\hat{A}\right)^{-1}, \] !et we have then the following expression for the parameters $\beta_j$ (the matrix elements of $\hat{H}$ are $h_{ij}$) @@ -432,7 +349,7 @@ we have then the following expression for the parameters $\beta_j$ (the matrix e \beta_j = \sum_{k=0}^{p-1}h_{jk}\sum_{i=0}^{n-1}\frac{y_i}{\sigma_i}\frac{x_{ik}}{\sigma_i} = \sum_{k=0}^{p-1}h_{jk}\sum_{i=0}^{n-1}b_ia_{ik} \] !et -We state without proof the expression for the uncertainty in the parameters $\beta_j$ as +We state without proof the expression for the uncertainty in the parameters $\beta_j$ as (we leave this as an exercise) !bt \[ \sigma^2(\beta_j) = \sum_{i=0}^{n-1}\sigma_i^2\left( \frac{\partial \beta_j}{\partial y_i}\right)^2, @@ -458,13 +375,13 @@ y=y(x) \rightarrow y(x_i) \approx \beta_0+\beta_1 x_i. By computing the derivatives of $\chi^2$ with respect to $\beta_0$ and $\beta_1$ show that these are given by !bt \[ -\frac{\partial \chi^2(\hat{\beta})}{\partial \beta_0} = -2\left[ \sum_{i=0}^{1}\left(\frac{y_i-\beta_0-\beta_1x_{i}}{\sigma_i^2}\right)\right]=0, +\frac{\partial \chi^2(\hat{\beta})}{\partial \beta_0} = -2\left[ \sum_{i=0}^{n-1}\left(\frac{y_i-\beta_0-\beta_1x_{i}}{\sigma_i^2}\right)\right]=0, \] !et and !bt \[ -\frac{\partial \chi^2(\hat{\beta})}{\partial \beta_0} = -2\left[ \sum_{i=0}^{1}x_i\left(\frac{y_i-\beta_0-\beta_1x_{i}}{\sigma_i^2}\right)\right]=0. +\frac{\partial \chi^2(\hat{\beta})}{\partial \beta_0} = -2\left[ \sum_{i=0}^{n-1}x_i\left(\frac{y_i-\beta_0-\beta_1x_{i}}{\sigma_i^2}\right)\right]=0. \] !et !eblock @@ -476,28 +393,28 @@ and We define then !bt \[ -\gamma = \sum_{i=0}^{1}\frac{1}{\sigma_i^2}, +\gamma = \sum_{i=0}^{1}\frac{n-1}{\sigma_i^2}, \] !et !bt \[ -\gamma_x = \sum_{i=0}^{1}\frac{x_{i}}{\sigma_i^2}, +\gamma_x = \sum_{i=0}^{n-1}\frac{x_{i}}{\sigma_i^2}, \] !et !bt \[ -\gamma_y = \sum_{i=0}^{1}\left(\frac{y_i}{\sigma_i^2}\right), +\gamma_y = \sum_{i=0}^{n-1}\left(\frac{y_i}{\sigma_i^2}\right), \] !et !bt \[ -\gamma_{xx} = \sum_{i=0}^{1}\frac{x_ix_{i}}{\sigma_i^2}, +\gamma_{xx} = \sum_{i=0}^{n-1}\frac{x_ix_{i}}{\sigma_i^2}, \] !et !bt \[ -\gamma_{xy} = \sum_{i=0}^{1}\frac{y_ix_{i}}{\sigma_i^2}, +\gamma_{xy} = \sum_{i=0}^{n-1}\frac{y_ix_{i}}{\sigma_i^2}, \] !et and show that @@ -518,6 +435,398 @@ The LSM suffers often from both being underdetermined and overdetermined in the +!split +===== Simple regression model ===== +We are now ready to write our first program which aims at solving the above linear regression equations. We start with data we have produced ourselves, in this case normally distributed random numbers along the $x$-axis. These numbers define then the value of a function $y(x)=4+3x+N(0,1)$. Thereafter we order the $x$ values and employ our linear regression algorithm to set up the best fit. Here we find it useful to use the numpy function $c\_$ arrays where arrays are stacked along their last axis after being upgraded to at least two dimensions with ones post-pended to the shape. The following examples help in understanding what happens +!bc pycod +import numpy as np +print(np.c_[np.array([1,2,3]), np.array([4,5,6])]) +print(np.c_[np.array([[1,2,3]]), 0, 0, np.array([[4,5,6]])]) +!ec + +!bc pycod +# Importing various packages +from random import random, seed +import numpy as np +import matplotlib.pyplot as plt + +x = 2*np.random.rand(100,1) +y = 4+3*x+np.random.randn(100,1) + +xb = np.c_[np.ones((100,1)), x] +beta = np.linalg.inv(xb.T.dot(xb)).dot(xb.T).dot(y) +xnew = np.array([[0],[2]]) +xbnew = np.c_[np.ones((2,1)), xnew] +ypredict = xbnew.dot(beta) + +plt.plot(xnew, ypredict, "r-") +plt.plot(x, y ,'ro') +plt.axis([0,2.0,0, 15.0]) +plt.xlabel(r'$x$') +plt.ylabel(r'$y$') +plt.title(r'Linear Regression') +plt.show() + +!ec + +We see that, as expected, a linear fit gives a seemingly (from the graph) good representation of the data. + + + + +!split +===== Simple regression model, now using _scikit-learn_ ===== + + +We can repeat the above algorithm using _scikit-learn_ as follows +!bc pycod +# Importing various packages +from random import random, seed +import numpy as np +import matplotlib.pyplot as plt +from sklearn.linear_model import LinearRegression + +x = 2*np.random.rand(100,1) +y = 4+3*x+np.random.randn(100,1) +linreg = LinearRegression() +linreg.fit(x,y) +xnew = np.array([[0],[2]]) +ypredict = linreg.predict(xnew) + +plt.plot(xnew, ypredict, "r-") +plt.plot(x, y ,'ro') +plt.axis([0,2.0,0, 15.0]) +plt.xlabel(r'$x$') +plt.ylabel(r'$y$') +plt.title(r'Random numbers ') +plt.show() +!ec + + +!split +===== Simple linear regression model using _scikit-learn_ ===== + +We start with perhaps our simplest possible example, using _scikit-learn_ to perform linear regression analysis on a data set produced by us. +What follows is a simple Python code where we have defined function $y$ in terms of the variable $x$. Both are defined as vectors of dimension $1\times 100$. The entries to the vector $\hat{x}$ are given by random numbers generated with a uniform distribution with entries $x_i \in [0,1]$ (more about probability distribution functions later). These values are then used to define a function $y(x)$ (tabulated again as a vector) with a linear dependence on $x$ plus a random noise added via the normal distribution. + + +The Numpy functions are imported used the _import numpy as np_ +statement and the random number generator for the uniform distribution +is called using the function _np.random.rand()_, where we specificy +that we want $100$ random variables. Using Numpy we define +automatically an array with the specified number of elements, $100$ in +our case. With the Numpy function _randn()_ we can compute random +numbers with the normal distribution (mean value $\mu$ equal to zero and +variance $\sigma^2$ set to one) and produce the values of $y$ assuming a linear +dependence as function of $x$ + +!bt +\[ +y = 2x+N(0,1), +\] +!et + +where $N(0,1)$ represents random numbers generated by the normal +distribution. From _scikit-learn_ we import then the +_LinearRegression_ functionality and make a prediction $\tilde{y} = +\alpha + \beta x$ using the function _fit(x,y)_. We call the set of +data $(\hat{x},\hat{y})$ for our training data. The Python package +_scikit-learn_ has also a functionality which extracts the above +fitting parameters $\alpha$ and $\beta$ (see below). Later we will +distinguish between training data and test data. + +For plotting we use the Python package +"matplotlib":"https://matplotlib.org/" which produces publication +quality figures. Feel free to explore the extensive +"gallery":"https://matplotlib.org/gallery/index.html" of examples. In +this example we plot our original values of $x$ and $y$ as well as the +prediction _ypredict_ ($\tilde{y}$), which attempts at fitting our +data with a straight line. + +The Python code follows here. +!bc pycod +# Importing various packages +import numpy as np +import matplotlib.pyplot as plt +from sklearn.linear_model import LinearRegression + +x = np.random.rand(100,1) +y = 2*x+np.random.randn(100,1) +linreg = LinearRegression() +linreg.fit(x,y) +xnew = np.array([[0],[1]]) +ypredict = linreg.predict(xnew) + +plt.plot(xnew, ypredict, "r-") +plt.plot(x, y ,'ro') +plt.axis([0,1.0,0, 5.0]) +plt.xlabel(r'$x$') +plt.ylabel(r'$y$') +plt.title(r'Simple Linear Regression') +plt.show() +!ec + + +!split +===== Simple linear regression model ===== + +This example serves several aims. It allows us to demonstrate several +aspects of data analysis and later machine learning algorithms. The +immediate visualization shows that our linear fit is not +impressive. It goes through the data points, but there are many +outliers which are not reproduced by our linear regression. We could +now play around with this small program and change for example the +factor in front of $x$ and the normal distribution. Try to change the +function $y$ to + +!bt +\[ +y = 10x+0.01 \times N(0,1), +\] +!et + +where $x$ is defined as before. + + +!split +===== Less noise ===== + +Does the fit look better? Indeed, by +reducing the role of the normal distribution we see immediately that +our linear prediction seemingly reproduces better the training +set. However, this testing 'by the eye' is obviouly not satisfactory in the +long run. Here we have only defined the training data and our model, and +have not discussed a more rigorous approach to the _cost_ function. + + +!split +===== How to study our fits ===== + +We need more rigorous criteria in defining whether we have succeeded or +not in modeling our training data. You will be surprised to see that +many scientists seldomly venture beyond this 'by the eye' approach. A +standard approach for the *cost* function is the so-called $\chi^2$ +function + +!bt +\[ \chi^2 = \frac{1}{n} +\sum_{i=0}^{n-1}\frac{(y_i-\tilde{y}_i)^2}{\sigma_i^2}, +\] +!et + +where $\sigma_i^2$ is the variance (to be defined later) of the entry +$y_i$. We may not know the explicit value of $\sigma_i^2$, it serves +however the aim of scaling the equations and make the cost function +dimensionless. + + +!split +===== Minimizing the cost function ===== + +Minimizing the cost function is a central aspect of +our discussions to come. Finding its minima as function of the model +parameters ($\alpha$ and $\beta$ in our case) will be a recurring +theme in these series of lectures. Essentially all machine learning +algorithms we will discuss center around the minimization of the +chosen cost function. This depends in turn on our specific +model for describing the data, a typical situation in supervised +learning. Automatizing the search for the minima of the cost function is a +central ingredient in all algorithms. Typical methods which are +employed are various variants of _gradient_ methods. These will be +discussed in more detail later. Again, you'll be surprised to hear that +many practitioners minimize the above function ''by the eye', popularly dubbed as +'chi by the eye'. That is, change a parameter and see (visually and numerically) that +the $\chi^2$ function becomes smaller. + +!split +===== Relative error ===== + +There are many ways to define the cost function. A simpler approach is to look at the relative difference between the training data and the predicted data, that is we define +the relative error as + +!bt +\[ +\epsilon_{\mathrm{relative}}= \frac{\vert \hat{y} -\hat{\tilde{y}}\vert}{\vert \hat{y}\vert}. +\] +!et +We can modify easily the above Python code and plot the relative error instead +!bc pycod +import numpy as np +import matplotlib.pyplot as plt +from sklearn.linear_model import LinearRegression + +x = np.random.rand(100,1) +y = 5*x+0.01*np.random.randn(100,1) +linreg = LinearRegression() +linreg.fit(x,y) +ypredict = linreg.predict(x) + +plt.plot(x, np.abs(ypredict-y)/abs(y), "ro") +plt.axis([0,1.0,0.0, 0.5]) +plt.xlabel(r'$x$') +plt.ylabel(r'$\epsilon_{\mathrm{relative}}$') +plt.title(r'Relative error') +plt.show() +!ec + +Depending on the parameter in front of the normal distribution, we may +have a small or larger relative error. Try to play around with +different training data sets and study (graphically) the value of the +relative error. + + +!split +===== The richness of _scikit-learn_ ===== + +As mentioned above, _scikit-learn_ has an impressive functionality. +We can for example extract the values of $\alpha$ and $\beta$ and +their error estimates, or the variance and standard deviation and many +other properties from the statistical data analysis. + +Here we show an +example of the functionality of scikit-learn. +!bc pycod +import numpy as np +import matplotlib.pyplot as plt +from sklearn.linear_model import LinearRegression +from sklearn.metrics import mean_squared_error, r2_score, mean_squared_log_error, mean_absolute_error + +x = np.random.rand(100,1) +y = 2.0+ 5*x+0.5*np.random.randn(100,1) +linreg = LinearRegression() +linreg.fit(x,y) +ypredict = linreg.predict(x) +print('The intercept alpha: \n', linreg.intercept_) +print('Coefficient beta : \n', linreg.coef_) +# The mean squared error +print("Mean squared error: %.2f" % mean_squared_error(y, ypredict)) +# Explained variance score: 1 is perfect prediction +print('Variance score: %.2f' % r2_score(y, ypredict)) +# Mean squared log error +print('Mean squared log error: %.2f' % mean_squared_log_error(y, ypredict) ) +# Mean absolute error +print('Mean absolute error: %.2f' % mean_absolute_error(y, ypredict)) +plt.plot(x, ypredict, "r-") +plt.plot(x, y ,'ro') +plt.axis([0.0,1.0,1.5, 7.0]) +plt.xlabel(r'$x$') +plt.ylabel(r'$y$') +plt.title(r'Linear Regression fit ') +plt.show() + +!ec + + +!split +===== Functions in _scikit-learn_ ===== + +The function _coef_ gives us the parameter $\beta$ of our fit while _intercept_ yields +$\alpha$. Depending on the constant in front of the normal distribution, we get values near or far from $alpha =2$ and $\beta =5$. Try to play around with different parameters in front of the normal distribution. The function _meansquarederror_ gives us the mean square error, a risk metric corresponding to the expected value of the squared (quadratic) error or loss defined as +!bt +\[ MSE(\hat{y},\hat{\tilde{y}}) = \frac{1}{n} +\sum_{i=0}^{n-1}(y_i-\tilde{y}_i)^2, +\] +!et + +The smaller the value, the better the fit. Ideally we would like to +have an MSE equal zero. The attentive reader has probably recognized +this function as being similar to the $\chi^2$ function defined above. + +!split +===== Other functions in _scikit-learn_ ===== + +The _r2score_ function computes $R^2$, the coefficient of +determination. It provides a measure of how well future samples are +likely to be predicted by the model. Best possible score is 1.0 and it +can be negative (because the model can be arbitrarily worse). A +constant model that always predicts the expected value of $\hat{y}$, +disregarding the input features, would get a $R^2$ score of $0.0$. + +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 +!bt +\[ +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}, +\] +!et +where we have defined the mean value of $\hat{y}$ as +!bt +\[ +\bar{y} = \frac{1}{n} \sum_{i=0}^{n - 1} y_i. +\] +!et + +!split +===== The mean absolute error and other functions in _scikit-learn_ ===== + +Another quantity will meet again in our discussions of regression analysis is + mean absolute error (MAE), a risk metric corresponding to the expected value of the absolute error loss or what we call the $l1$-norm loss. In our discussion above we presented the relative error. +The MAE is defined as follows +!bt +\[ +\text{MAE}(\hat{y}, \hat{\tilde{y}}) = \frac{1}{n} \sum_{i=0}^{n-1} \left| y_i - \tilde{y}_i \right|. +\] +!et +Finally we present the +squared logarithmic (quadratic) error +!bt +\[ +\text{MSLE}(\hat{y}, \hat{\tilde{y}}) = \frac{1}{n} \sum_{i=0}^{n - 1} (\log_e (1 + y_i) - \log_e (1 + \tilde{y}_i) )^2, +\] +!et + +where $\log_e (x)$ stands for the natural logarithm of $x$. This error +estimate is best to use when targets having exponential growth, such +as population counts, average sales of a commodity over a span of +years etc. + + +!split +===== Cubic polynomial in _scikit-learn_ ===== + +We will discuss in more +detail these and other functions in the various lectures. We conclude this part with another example. Instead of +a linear $x$-dependence we study now a cubic polynomial and use the polynomial regression analysis tools of scikit-learn. + +!bc pycod +import matplotlib.pyplot as plt +import numpy as np +import random +from sklearn.linear_model import Ridge +from sklearn.preprocessing import PolynomialFeatures +from sklearn.pipeline import make_pipeline +from sklearn.linear_model import LinearRegression + +x=np.linspace(0.02,0.98,200) +noise = np.asarray(random.sample((range(200)),200)) +y=x**3*noise +yn=x**3*100 +poly3 = PolynomialFeatures(degree=3) +X = poly3.fit_transform(x[:,np.newaxis]) +clf3 = LinearRegression() +clf3.fit(X,y) + +Xplot=poly3.fit_transform(x[:,np.newaxis]) +poly3_plot=plt.plot(x, clf3.predict(Xplot), label='Cubic Fit') +plt.plot(x,yn, color='red', label="True Cubic") +plt.scatter(x, y, label='Data', color='orange', s=15) +plt.legend() +plt.show() + +def error(a): + for i in y: + err=(y-yn)/yn + return abs(np.sum(err))/len(err) + +print (error(y)) +!ec + +Similarly, using _R_, we can perform similar studies. +(more details on _R_ will be inserted later). + + + + + @@ -728,3 +1037,5 @@ Add examples on classification problems + +