diff --git a/doc/Projects/2019/Project1/html/._Project1-bs000.html b/doc/Projects/2019/Project1/html/._Project1-bs000.html index 5589bbbbb..12a5c36b1 100644 --- a/doc/Projects/2019/Project1/html/._Project1-bs000.html +++ b/doc/Projects/2019/Project1/html/._Project1-bs000.html @@ -50,7 +50,10 @@ Automatically generated HTML file from DocOnce source 3, None, '___sec1'), - ('Part b) Resampling techniques', 3, None, '___sec2'), + ('Part b) Resampling techniques, adding more complexity', + 3, + None, + '___sec2'), ('Part c): Bias-variance tradeoff', 3, None, '___sec3'), ('Part d): Ridge Regression on the Franke function with ' 'resampling', @@ -113,7 +116,7 @@ MathJax.Hub.Config({
Perform a resampling of the data where you split the data in training @@ -311,7 +314,7 @@ approximately \( 2/3 \) to \( 4/5 \) of the data as training data.
Implement the \( k \)-fold cross-validation algorithm (write your own -code) and and evaluate again the MSE and the \( R^2 \) functions resulting +code) and evaluate again the MSE and the \( R^2 \) functions resulting from the test data. You can compare your own code with that from Scikit-Learn if needed. @@ -351,16 +354,13 @@ The parameters \( \boldsymbol{\beta} \) are in turn found by optimizing the mean squared error via the so-called cost function $$ -C(\boldsymbol{X},\boldsymbol{\beta}) =\frac{1}{n}\sum_{i=0}^{n-1}(y_i-\tilde{y}_i)^2=\mathbb{E}\left[(\boldsymbol{\ -y}-\boldsymbol{\tilde{y}})^2\right]. +C(\boldsymbol{X},\boldsymbol{\beta}) =\frac{1}{n}\sum_{i=0}^{n-1}(y_i-\tilde{y}_i)^2=\mathbb{E}\left[(\boldsymbol{y}-\boldsymbol{\tilde{y}})^2\right]. $$
Show that you can rewrite this as $$ -\mathbb{E}\left[(\boldsymbol{y}-\boldsymbol{\tilde{y}})^2\right]=\frac{1}{n}\sum_i(f_i-\mathbb{E}\left[\bm\ -{\tilde{y}}\right])^2+\frac{1}{n}\sum_i(\tilde{y}_i-\mathbb{E}\left[\boldsymbol{\tilde{y}}\right])\ -^2+\sigma^2. +\mathbb{E}\left[(\boldsymbol{y}-\boldsymbol{\tilde{y}})^2\right]=\frac{1}{n}\sum_i(f_i-\mathbb{E}\left[\boldsymbol{\tilde{y}}\right])^2+\frac{1}{n}\sum_i(\tilde{y}_i-\mathbb{E}\left[\boldsymbol{\tilde{y}}\right])^2+\sigma^2. $$
diff --git a/doc/Projects/2019/Project1/html/Project1-bs.html b/doc/Projects/2019/Project1/html/Project1-bs.html index 5589bbbbb..12a5c36b1 100644 --- a/doc/Projects/2019/Project1/html/Project1-bs.html +++ b/doc/Projects/2019/Project1/html/Project1-bs.html @@ -50,7 +50,10 @@ Automatically generated HTML file from DocOnce source 3, None, '___sec1'), - ('Part b) Resampling techniques', 3, None, '___sec2'), + ('Part b) Resampling techniques, adding more complexity', + 3, + None, + '___sec2'), ('Part c): Bias-variance tradeoff', 3, None, '___sec3'), ('Part d): Ridge Regression on the Franke function with ' 'resampling', @@ -113,7 +116,7 @@ MathJax.Hub.Config({
Perform a resampling of the data where you split the data in training @@ -311,7 +314,7 @@ approximately \( 2/3 \) to \( 4/5 \) of the data as training data.
Implement the \( k \)-fold cross-validation algorithm (write your own -code) and and evaluate again the MSE and the \( R^2 \) functions resulting +code) and evaluate again the MSE and the \( R^2 \) functions resulting from the test data. You can compare your own code with that from Scikit-Learn if needed. @@ -351,16 +354,13 @@ The parameters \( \boldsymbol{\beta} \) are in turn found by optimizing the mean squared error via the so-called cost function $$ -C(\boldsymbol{X},\boldsymbol{\beta}) =\frac{1}{n}\sum_{i=0}^{n-1}(y_i-\tilde{y}_i)^2=\mathbb{E}\left[(\boldsymbol{\ -y}-\boldsymbol{\tilde{y}})^2\right]. +C(\boldsymbol{X},\boldsymbol{\beta}) =\frac{1}{n}\sum_{i=0}^{n-1}(y_i-\tilde{y}_i)^2=\mathbb{E}\left[(\boldsymbol{y}-\boldsymbol{\tilde{y}})^2\right]. $$
Show that you can rewrite this as $$ -\mathbb{E}\left[(\boldsymbol{y}-\boldsymbol{\tilde{y}})^2\right]=\frac{1}{n}\sum_i(f_i-\mathbb{E}\left[\bm\ -{\tilde{y}}\right])^2+\frac{1}{n}\sum_i(\tilde{y}_i-\mathbb{E}\left[\boldsymbol{\tilde{y}}\right])\ -^2+\sigma^2. +\mathbb{E}\left[(\boldsymbol{y}-\boldsymbol{\tilde{y}})^2\right]=\frac{1}{n}\sum_i(f_i-\mathbb{E}\left[\boldsymbol{\tilde{y}}\right])^2+\frac{1}{n}\sum_i(\tilde{y}_i-\mathbb{E}\left[\boldsymbol{\tilde{y}}\right])^2+\sigma^2. $$
diff --git a/doc/Projects/2019/Project1/html/Project1.html b/doc/Projects/2019/Project1/html/Project1.html index e71047065..be362272d 100644 --- a/doc/Projects/2019/Project1/html/Project1.html +++ b/doc/Projects/2019/Project1/html/Project1.html @@ -49,7 +49,10 @@ div { text-align: justify; text-justify: inter-word; } 3, None, '___sec1'), - ('Part b) Resampling techniques', 3, None, '___sec2'), + ('Part b) Resampling techniques, adding more complexity', + 3, + None, + '___sec2'), ('Part c): Bias-variance tradeoff', 3, None, '___sec3'), ('Part d): Ridge Regression on the Franke function with ' 'resampling', @@ -248,7 +251,7 @@ $$ \bar{y} = \frac{1}{n} \sum_{i=0}^{n - 1} y_i. $$ -
Perform a resampling of the data where you split the data in training @@ -266,7 +269,7 @@ approximately \( 2/3 \) to \( 4/5 \) of the data as training data.
Implement the \( k \)-fold cross-validation algorithm (write your own -code) and and evaluate again the MSE and the \( R^2 \) functions resulting +code) and evaluate again the MSE and the \( R^2 \) functions resulting from the test data. You can compare your own code with that from Scikit-Learn if needed. @@ -306,16 +309,13 @@ The parameters \( \boldsymbol{\beta} \) are in turn found by optimizing the mean squared error via the so-called cost function $$ -C(\boldsymbol{X},\boldsymbol{\beta}) =\frac{1}{n}\sum_{i=0}^{n-1}(y_i-\tilde{y}_i)^2=\mathbb{E}\left[(\boldsymbol{\ -y}-\boldsymbol{\tilde{y}})^2\right]. +C(\boldsymbol{X},\boldsymbol{\beta}) =\frac{1}{n}\sum_{i=0}^{n-1}(y_i-\tilde{y}_i)^2=\mathbb{E}\left[(\boldsymbol{y}-\boldsymbol{\tilde{y}})^2\right]. $$
Show that you can rewrite this as $$ -\mathbb{E}\left[(\boldsymbol{y}-\boldsymbol{\tilde{y}})^2\right]=\frac{1}{n}\sum_i(f_i-\mathbb{E}\left[\bm\ -{\tilde{y}}\right])^2+\frac{1}{n}\sum_i(\tilde{y}_i-\mathbb{E}\left[\boldsymbol{\tilde{y}}\right])\ -^2+\sigma^2. +\mathbb{E}\left[(\boldsymbol{y}-\boldsymbol{\tilde{y}})^2\right]=\frac{1}{n}\sum_i(f_i-\mathbb{E}\left[\boldsymbol{\tilde{y}}\right])^2+\frac{1}{n}\sum_i(\tilde{y}_i-\mathbb{E}\left[\boldsymbol{\tilde{y}}\right])^2+\sigma^2. $$
diff --git a/doc/Projects/2019/Project1/ipynb/ipynb-Project1-src.tar.gz b/doc/Projects/2019/Project1/ipynb/ipynb-Project1-src.tar.gz index 8d8c1d065..3498e5f42 100644 Binary files a/doc/Projects/2019/Project1/ipynb/ipynb-Project1-src.tar.gz and b/doc/Projects/2019/Project1/ipynb/ipynb-Project1-src.tar.gz differ diff --git a/doc/Projects/2019/Project1/pdf/Project1.p.tex b/doc/Projects/2019/Project1/pdf/Project1.p.tex index da45b59e4..3d4e28d4e 100644 --- a/doc/Projects/2019/Project1/pdf/Project1.p.tex +++ b/doc/Projects/2019/Project1/pdf/Project1.p.tex @@ -283,7 +283,7 @@ where we have defined the mean value of $\hat{y}$ as \bar{y} = \frac{1}{n} \sum_{i=0}^{n - 1} y_i. \] -\paragraph{Part b) Resampling techniques.} +\paragraph{Part b) Resampling techniques, adding more complexity.} Perform a resampling of the data where you split the data in training data and test data. Here you can write your own function or use the function for splitting training data provided by \textbf{Scikit-Learn}. @@ -298,7 +298,7 @@ approximately $2/3$ to $4/5$ of the data as training data. Implement the $k$-fold cross-validation algorithm (write your own -code) and and evaluate again the MSE and the $R^2$ functions resulting +code) and evaluate again the MSE and the $R^2$ functions resulting from the test data. You can compare your own code with that from \textbf{Scikit-Learn} if needed. @@ -334,15 +334,12 @@ The parameters $\bm{\beta}$ are in turn found by optimizing the means squared error via the so-called cost function \[ -C(\bm{X},\bm{\beta}) =\frac{1}{n}\sum_{i=0}^{n-1}(y_i-\tilde{y}_i)^2=\mathbb{E}\left[(\bm{\ -y}-\bm{\tilde{y}})^2\right]. +C(\bm{X},\bm{\beta}) =\frac{1}{n}\sum_{i=0}^{n-1}(y_i-\tilde{y}_i)^2=\mathbb{E}\left[(\bm{y}-\bm{\tilde{y}})^2\right]. \] Show that you can rewrite this as \[ -\mathbb{E}\left[(\bm{y}-\bm{\tilde{y}})^2\right]=\frac{1}{n}\sum_i(f_i-\mathbb{E}\left[\bm\ -{\tilde{y}}\right])^2+\frac{1}{n}\sum_i(\tilde{y}_i-\mathbb{E}\left[\bm{\tilde{y}}\right])\ -^2+\sigma^2. +\mathbb{E}\left[(\bm{y}-\bm{\tilde{y}})^2\right]=\frac{1}{n}\sum_i(f_i-\mathbb{E}\left[\bm{\tilde{y}}\right])^2+\frac{1}{n}\sum_i(\tilde{y}_i-\mathbb{E}\left[\bm{\tilde{y}}\right])^2+\sigma^2. \] Explain what the terms mean, which one is the bias and which one is diff --git a/doc/Projects/2019/Project1/pdf/Project1.pdf b/doc/Projects/2019/Project1/pdf/Project1.pdf index 906633c04..134041fb0 100644 Binary files a/doc/Projects/2019/Project1/pdf/Project1.pdf and b/doc/Projects/2019/Project1/pdf/Project1.pdf differ diff --git a/doc/Projects/2019/Project1/pdf/Project1.tex b/doc/Projects/2019/Project1/pdf/Project1.tex index 8f56713af..ff83c2665 100644 --- a/doc/Projects/2019/Project1/pdf/Project1.tex +++ b/doc/Projects/2019/Project1/pdf/Project1.tex @@ -253,7 +253,7 @@ where we have defined the mean value of $\hat{y}$ as \bar{y} = \frac{1}{n} \sum_{i=0}^{n - 1} y_i. \] -\paragraph{Part b) Resampling techniques.} +\paragraph{Part b) Resampling techniques, adding more complexity.} Perform a resampling of the data where you split the data in training data and test data. Here you can write your own function or use the function for splitting training data provided by \textbf{Scikit-Learn}. @@ -268,7 +268,7 @@ approximately $2/3$ to $4/5$ of the data as training data. Implement the $k$-fold cross-validation algorithm (write your own -code) and and evaluate again the MSE and the $R^2$ functions resulting +code) and evaluate again the MSE and the $R^2$ functions resulting from the test data. You can compare your own code with that from \textbf{Scikit-Learn} if needed. @@ -304,15 +304,12 @@ The parameters $\bm{\beta}$ are in turn found by optimizing the means squared error via the so-called cost function \[ -C(\bm{X},\bm{\beta}) =\frac{1}{n}\sum_{i=0}^{n-1}(y_i-\tilde{y}_i)^2=\mathbb{E}\left[(\bm{\ -y}-\bm{\tilde{y}})^2\right]. +C(\bm{X},\bm{\beta}) =\frac{1}{n}\sum_{i=0}^{n-1}(y_i-\tilde{y}_i)^2=\mathbb{E}\left[(\bm{y}-\bm{\tilde{y}})^2\right]. \] Show that you can rewrite this as \[ -\mathbb{E}\left[(\bm{y}-\bm{\tilde{y}})^2\right]=\frac{1}{n}\sum_i(f_i-\mathbb{E}\left[\bm\ -{\tilde{y}}\right])^2+\frac{1}{n}\sum_i(\tilde{y}_i-\mathbb{E}\left[\bm{\tilde{y}}\right])\ -^2+\sigma^2. +\mathbb{E}\left[(\bm{y}-\bm{\tilde{y}})^2\right]=\frac{1}{n}\sum_i(f_i-\mathbb{E}\left[\bm{\tilde{y}}\right])^2+\frac{1}{n}\sum_i(\tilde{y}_i-\mathbb{E}\left[\bm{\tilde{y}}\right])^2+\sigma^2. \] Explain what the terms mean, which one is the bias and which one is diff --git a/doc/Projects/2019/Project1/pdf/Project1.tex~ b/doc/Projects/2019/Project1/pdf/Project1.tex~ index 926bebcdc..625b611c3 100644 --- a/doc/Projects/2019/Project1/pdf/Project1.tex~ +++ b/doc/Projects/2019/Project1/pdf/Project1.tex~ @@ -253,7 +253,7 @@ where we have defined the mean value of $\hat{y}$ as \bar{y} = \frac{1}{n} \sum_{i=0}^{n - 1} y_i. \] -\paragraph{Part b) Resampling techniques.} +\paragraph{Part b) Resampling techniques, adding more complexity.} Perform a resampling of the data where you split the data in training data and test data. Here you can write your own function or use the function for splitting training data provided by \textbf{Scikit-Learn}. @@ -268,7 +268,7 @@ approximately $2/3$ to $4/5$ of the data as training data. Implement the $k$-fold cross-validation algorithm (write your own -code) and and evaluate again the MSE and the $R^2$ functions resulting +code) and evaluate again the MSE and the $R^2$ functions resulting from the test data. You can compare your own code with that from \textbf{Scikit-Learn} if needed. @@ -304,15 +304,12 @@ The parameters $\bm{\beta}$ are in turn found by optimizing the means squared error via the so-called cost function \[ -C(\bm{X},\bm{\beta}) =\frac{1}{n}\sum_{i=0}^{n-1}(y_i-\tilde{y}_i)^2=\mathbb{E}\left[(\bm{\ -y}-\bm{\tilde{y}})^2\right]. +C(\bm{X},\bm{\beta}) =\frac{1}{n}\sum_{i=0}^{n-1}(y_i-\tilde{y}_i)^2=\mathbb{E}\left[(\bm{y}-\bm{\tilde{y}})^2\right]. \] Show that you can rewrite this as \[ -\mathbb{E}\left[(\bm{y}-\bm{\tilde{y}})^2\right]=\frac{1}{n}\sum_i(f_i-\mathbb{E}\left[\bm\ -{\tilde{y}}\right])^2+\frac{1}{n}\sum_i(\tilde{y}_i-\mathbb{E}\left[\bm{\tilde{y}}\right])\ -^2+\sigma^2. +\mathbb{E}\left[(\bm{y}-\bm{\tilde{y}})^2\right]=\frac{1}{n}\sum_i(f_i-\mathbb{E}\left[\bm{\tilde{y}}\right])^2+\frac{1}{n}\sum_i(\tilde{y}_i-\mathbb{E}\left[\bm{\tilde{y}}\right])^2+\sigma^2. \] Explain what the terms mean, which one is the bias and which one is diff --git a/doc/src/Projects/2019/Project1/Project1-bs.html b/doc/src/Projects/2019/Project1/Project1-bs.html new file mode 100644 index 000000000..12a5c36b1 --- /dev/null +++ b/doc/src/Projects/2019/Project1/Project1-bs.html @@ -0,0 +1,598 @@ + + +
+ + + + + +
+ + + + + + +
+ + +
+ + +
+
+
+The main aim of this project is to study in more detail various +regression methods, including the Ordinary Least Squares (OLS) method, +Ridge regression and finally Lasso regression. +The methods are in turn combined with resampling techniques. + +
+We will first study how to fit polynomials to a specific +two-dimensional function called Franke's +function. This +is a function which has been widely used when testing various +interpolation and fitting algorithms. Furthermore, after having +established the model and the method, we will employ resamling +techniques such as cross-validation in order to perform a +proper assessment of our models. We will also study in detail the +so-called Bias-Variance trade off. + +
+The Franke function, which is a weighted sum of four exponentials reads as follows +$$ +\begin{align*} +f(x,y) &= \frac{3}{4}\exp{\left(-\frac{(9x-2)^2}{4} - \frac{(9y-2)^2}{4}\right)}+\frac{3}{4}\exp{\left(-\frac{(9x+1)^2}{49}- \frac{(9y+1)}{10}\right)} \\ +&+\frac{1}{2}\exp{\left(-\frac{(9x-7)^2}{4} - \frac{(9y-3)^2}{4}\right)} -\frac{1}{5}\exp{\left(-(9x-4)^2 - (9y-7)^2\right) }. +\end{align*} +$$ + +
+The function will be defined for \( x,y\in [0,1] \). Our first step will +be to perform an OLS regression analysis of this function, trying out +a polynomial fit with an \( x \) and \( y \) dependence of the form \( [x, y, +x^2, y^2, xy, \dots] \). We will also include cross-validation as +resampling technique. As in homeworks 1 and 2, we can use a uniform +distribution to set up the arrays of values for \( x \) and \( y \), or as in +the example below just a set of fixed +values for \( x \) and \( y \) with a given step +size. We will fit a +function (for example a polynomial) of \( x \) and \( y \). Thereafter we +will repeat much of the same procedure using the Ridge and Lasso +regression methods, introducing thus a dependence on the bias +(penalty) \( \lambda \). + +
+Finally we are going to use (real) digital terrain data and try to +reproduce these data using the same methods. We will also try to go +beyond the second-order polynomials metioned above and explore +which polynomial fits the data best. + +
+The Python fucntion for the Franke function is included here (it performs also a three-dimensional plot of it) +
+ + +
from mpl_toolkits.mplot3d import Axes3D
+import matplotlib.pyplot as plt
+from matplotlib import cm
+from matplotlib.ticker import LinearLocator, FormatStrFormatter
+import numpy as np
+from random import random, seed
+
+fig = plt.figure()
+ax = fig.gca(projection='3d')
+
+# Make data.
+x = np.arange(0, 1, 0.05)
+y = np.arange(0, 1, 0.05)
+x, y = np.meshgrid(x,y)
+
+
+def FrankeFunction(x,y):
+ term1 = 0.75*np.exp(-(0.25*(9*x-2)**2) - 0.25*((9*y-2)**2))
+ term2 = 0.75*np.exp(-((9*x+1)**2)/49.0 - 0.1*(9*y+1))
+ term3 = 0.5*np.exp(-(9*x-7)**2/4.0 - 0.25*((9*y-3)**2))
+ term4 = -0.2*np.exp(-(9*x-4)**2 - (9*y-7)**2)
+ return term1 + term2 + term3 + term4
+
+
+z = FrankeFunction(x, y)
+
+# Plot the surface.
+surf = ax.plot_surface(x, y, z, cmap=cm.coolwarm,
+ linewidth=0, antialiased=False)
+
+# Customize the z axis.
+ax.set_zlim(-0.10, 1.40)
+ax.zaxis.set_major_locator(LinearLocator(10))
+ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f'))
+
+# Add a color bar which maps values to colors.
+fig.colorbar(surf, shrink=0.5, aspect=5)
+
+plt.show()
++We will generate our own dataset for a function +\( \mathrm{FrankeFunction}(x,y) \) with \( x,y \in [0,1] \). The function +\( f(x,y) \) is the Franke function. You should explore also the addition +an added stochastic noise to this function using the normal +distribution \( \cal{N}(0,1) \). + +
+Write your own code (using either a matrix inversion or a singular +value decomposition from e.g., numpy ) or use your code from +homeworks 1 and 2 and perform a standard least square regression +analysis using polynomials in \( x \) and \( y \) up to fifth order. Find the +confidence intervals of the parameters \( \beta \) by computing their +variances, evaluate the Mean Squared error (MSE) + +$$ MSE(\hat{y},\hat{\tilde{y}}) = \frac{1}{n} +\sum_{i=0}^{n-1}(y_i-\tilde{y}_i)^2, +$$ + +
+and the \( R^2 \) score function. If \( \tilde{\hat{y}}_i \) is the predicted +value of the \( i-th \) sample and \( y_i \) is the corresponding true value, +then the score \( R^2 \) is defined as + +$$ +R^2(\hat{y}, \tilde{\hat{y}}) = 1 - \frac{\sum_{i=0}^{n - 1} (y_i - \tilde{y}_i)^2}{\sum_{i=0}^{n - 1} (y_i - \bar{y})^2}, +$$ + +
+where we have defined the mean value of \( \hat{y} \) as + +$$ +\bar{y} = \frac{1}{n} \sum_{i=0}^{n - 1} y_i. +$$ + +
+Perform a resampling of the data where you split the data in training +data and test data. Here you can write your own function or use the +function for splitting training data provided by Scikit-Learn. +This function is called \( train\_test\_split \). + +
+It is normal in essentially all Machine Learning studies to split the +data in a training set and a test set (sometimes also an additional +validation set). There +is no explicit recipe for how much data should be included as training +data and say test data. An accepted rule of thumb is to use +approximately \( 2/3 \) to \( 4/5 \) of the data as training data. + +
+Implement the \( k \)-fold cross-validation algorithm (write your own +code) and evaluate again the MSE and the \( R^2 \) functions resulting +from the test data. You can compare your own code with that from +Scikit-Learn if needed. + +
+With a code which does OLS and includes resampling techniques, +we will now discuss the bias-variance tradeoff in the context of +continuous predictions such as regression. However, many of the +intuitions and ideas discussed here also carry over to classification +tasks and basically all Machine Learning algorithms. + +
+Consider a +dataset \( \mathcal{L} \) consisting of the data +\( \mathbf{X}_\mathcal{L}=\{(y_j, \boldsymbol{x}_j), j=0\ldots n-1\} \). + +
+Let us assume that the true data is generated from a noisy model + +$$ +\boldsymbol{y}=f(\boldsymbol{x}) + \boldsymbol{\epsilon}. +$$ + +
+Here \( \epsilon \) is normally distributed with mean zero and standard +deviation \( \sigma^2 \). + +
+In our derivation of the ordinary least squares method we defined then +an approximation to the function \( f \) in terms of the parameters +\( \boldsymbol{\beta} \) and the design matrix \( \boldsymbol{X} \) which embody our model, +that is \( \boldsymbol{\tilde{y}}=\boldsymbol{X}\boldsymbol{\beta} \). + +
+The parameters \( \boldsymbol{\beta} \) are in turn found by optimizing the means +squared error via the so-called cost function + +$$ +C(\boldsymbol{X},\boldsymbol{\beta}) =\frac{1}{n}\sum_{i=0}^{n-1}(y_i-\tilde{y}_i)^2=\mathbb{E}\left[(\boldsymbol{y}-\boldsymbol{\tilde{y}})^2\right]. +$$ + +
+Show that you can rewrite this as +$$ +\mathbb{E}\left[(\boldsymbol{y}-\boldsymbol{\tilde{y}})^2\right]=\frac{1}{n}\sum_i(f_i-\mathbb{E}\left[\boldsymbol{\tilde{y}}\right])^2+\frac{1}{n}\sum_i(\tilde{y}_i-\mathbb{E}\left[\boldsymbol{\tilde{y}}\right])^2+\sigma^2. +$$ + +
+Explain what the terms mean, which one is the bias and which one is +the variance and discuss their interpretations. + +
+Discuss the bias and variance tradeoff as function +of your model complexity (the degree of the polynomial) and the number +of data points, and possibly also your training and test data. + +
+Try to make a figure similar to Fig. 2.11 of Hastie, Tibshirani, and +Friedman, see the references below. You will most likely not get an +equally smooth curve! + +
+Write your own code for the Ridge method, either using matrix +inversion or the singular value decomposition as done in the previous +exercise or howework 2 (see also chapter 3.4 of Hastie et al., +equations (3.43) and (3.44)). Perform the same analysis as in the +previous exercises (for the same polynomials and include resampling +techniques) but now for different values of \( \lambda \). Compare and +analyze your results with those obtained in parts a-c). Study the +dependence on \( \lambda \). + +
+Study also the bias-variance tradeoff as function of various values of +the parameter \( \lambda \). Comment your results. + +
+This part is essentially a repeat of the previous two ones, but now +with Lasso regression. Write either your own code or, in this case, +you can also use the functionalities of Scikit-Learn (recommended). +Give a +critical discussion of the three methods and a judgement of which +model fits the data best. + +
+With our codes functioning and having been tested properly on a +simpler function we are now ready to look at real data. We will +essentially repeat in part g) what was done in parts a-e). However, we +need first to download the data and prepare properly the inputs to our +codes. We are going to download digital terrain data from the website +https://earthexplorer.usgs.gov/, + +
+In order to obtain data for a specific region, you need to register as +a user (free) at this website and then decide upon which area you want +to fetch the digital terrain data from. In order to be able to read +the data properly, you need to specify that the format should be SRTM +Arc-Second Global and download the data as a GeoTIF file. The +files are then stored in tif format which can be imported into a +Python program using + +
+ + +
scipy.misc.imread
++Here is a simple part of a Python code which reads and plots the data +from such files + +
+ + +
import numpy as np
+from imageio import imread
+import matplotlib.pyplot as plt
+from mpl_toolkits.mplot3d import Axes3D
+from matplotlib import cm
+
+# Load the terrain
+terrain1 = imread('SRTM_data_Norway_1.tif')
+# Show the terrain
+plt.figure()
+plt.title('Terrain over Norway 1')
+plt.imshow(terrain1, cmap='gray')
+plt.xlabel('X')
+plt.ylabel('Y')
+plt.show()
++If you should have problems in downloading the digital terrain data, +we provide two examples under the data folder of project 1. One is +from a region close to Stavanger in Norway and the other Møsvatn +Austfjell, again in Norway. +Feel free to produce your own terrain data. + +
+Our final part deals with the parameterization of your digital terrain +data. We will apply all three methods for linear regression as in +parts a-c), the same type (or higher order) of polynomial +approximation and the same resampling techniques to evaluate which +model fits the data best. + +
+At the end, you should pesent a critical evaluation of your results +and discuss the applicability of these regression methods to the type +of data presented here. + +
+Here follows a brief recipe and recommendation on how to write a report for each +project. + +
+The preferred format for the report is a PDF file. You can also use DOC or postscript formats or as an ipython notebook file. As programming language we prefer that you choose between C/C++, Fortran2008 or Python. The following prescription should be followed when preparing the report: + +
+If you have Python installed (we recommend Python3) and you feel pretty familiar with installing different packages, +we recommend that you install the following Python packages via pip as + +
+See below for a discussion of tensorflow and scikit-learn. + +
+For OSX users we recommend also, after having installed Xcode, to install brew. Brew allows +for a seamless installation of additional software via for example + +
+If you don't want to install various Python packages with their dependencies separately, we recommend two widely used distrubutions which set up all relevant dependencies for Python, namely + +
+ +
+ +
+ + +
+ + +
+
+The main aim of this project is to study in more detail various +regression methods, including the Ordinary Least Squares (OLS) method, +Ridge regression and finally Lasso regression. +The methods are in turn combined with resampling techniques. + +
+We will first study how to fit polynomials to a specific +two-dimensional function called Franke's +function. This +is a function which has been widely used when testing various +interpolation and fitting algorithms. Furthermore, after having +established the model and the method, we will employ resamling +techniques such as cross-validation in order to perform a +proper assessment of our models. We will also study in detail the +so-called Bias-Variance trade off. + +
+The Franke function, which is a weighted sum of four exponentials reads as follows +$$ +\begin{align*} +f(x,y) &= \frac{3}{4}\exp{\left(-\frac{(9x-2)^2}{4} - \frac{(9y-2)^2}{4}\right)}+\frac{3}{4}\exp{\left(-\frac{(9x+1)^2}{49}- \frac{(9y+1)}{10}\right)} \\ +&+\frac{1}{2}\exp{\left(-\frac{(9x-7)^2}{4} - \frac{(9y-3)^2}{4}\right)} -\frac{1}{5}\exp{\left(-(9x-4)^2 - (9y-7)^2\right) }. +\end{align*} +$$ + +
+The function will be defined for \( x,y\in [0,1] \). Our first step will +be to perform an OLS regression analysis of this function, trying out +a polynomial fit with an \( x \) and \( y \) dependence of the form \( [x, y, +x^2, y^2, xy, \dots] \). We will also include cross-validation as +resampling technique. As in homeworks 1 and 2, we can use a uniform +distribution to set up the arrays of values for \( x \) and \( y \), or as in +the example below just a set of fixed +values for \( x \) and \( y \) with a given step +size. We will fit a +function (for example a polynomial) of \( x \) and \( y \). Thereafter we +will repeat much of the same procedure using the Ridge and Lasso +regression methods, introducing thus a dependence on the bias +(penalty) \( \lambda \). + +
+Finally we are going to use (real) digital terrain data and try to +reproduce these data using the same methods. We will also try to go +beyond the second-order polynomials metioned above and explore +which polynomial fits the data best. + +
+The Python fucntion for the Franke function is included here (it performs also a three-dimensional plot of it) +
+ + +
from mpl_toolkits.mplot3d import Axes3D
+import matplotlib.pyplot as plt
+from matplotlib import cm
+from matplotlib.ticker import LinearLocator, FormatStrFormatter
+import numpy as np
+from random import random, seed
+
+fig = plt.figure()
+ax = fig.gca(projection='3d')
+
+# Make data.
+x = np.arange(0, 1, 0.05)
+y = np.arange(0, 1, 0.05)
+x, y = np.meshgrid(x,y)
+
+
+def FrankeFunction(x,y):
+ term1 = 0.75*np.exp(-(0.25*(9*x-2)**2) - 0.25*((9*y-2)**2))
+ term2 = 0.75*np.exp(-((9*x+1)**2)/49.0 - 0.1*(9*y+1))
+ term3 = 0.5*np.exp(-(9*x-7)**2/4.0 - 0.25*((9*y-3)**2))
+ term4 = -0.2*np.exp(-(9*x-4)**2 - (9*y-7)**2)
+ return term1 + term2 + term3 + term4
+
+
+z = FrankeFunction(x, y)
+
+# Plot the surface.
+surf = ax.plot_surface(x, y, z, cmap=cm.coolwarm,
+ linewidth=0, antialiased=False)
+
+# Customize the z axis.
+ax.set_zlim(-0.10, 1.40)
+ax.zaxis.set_major_locator(LinearLocator(10))
+ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f'))
+
+# Add a color bar which maps values to colors.
+fig.colorbar(surf, shrink=0.5, aspect=5)
+
+plt.show()
++We will generate our own dataset for a function +\( \mathrm{FrankeFunction}(x,y) \) with \( x,y \in [0,1] \). The function +\( f(x,y) \) is the Franke function. You should explore also the addition +an added stochastic noise to this function using the normal +distribution \( \cal{N}(0,1) \). + +
+Write your own code (using either a matrix inversion or a singular +value decomposition from e.g., numpy ) or use your code from +homeworks 1 and 2 and perform a standard least square regression +analysis using polynomials in \( x \) and \( y \) up to fifth order. Find the +confidence intervals of the parameters \( \beta \) by computing their +variances, evaluate the Mean Squared error (MSE) + +$$ MSE(\hat{y},\hat{\tilde{y}}) = \frac{1}{n} +\sum_{i=0}^{n-1}(y_i-\tilde{y}_i)^2, +$$ + +
+and the \( R^2 \) score function. If \( \tilde{\hat{y}}_i \) is the predicted +value of the \( i-th \) sample and \( y_i \) is the corresponding true value, +then the score \( R^2 \) is defined as + +$$ +R^2(\hat{y}, \tilde{\hat{y}}) = 1 - \frac{\sum_{i=0}^{n - 1} (y_i - \tilde{y}_i)^2}{\sum_{i=0}^{n - 1} (y_i - \bar{y})^2}, +$$ + +
+where we have defined the mean value of \( \hat{y} \) as + +$$ +\bar{y} = \frac{1}{n} \sum_{i=0}^{n - 1} y_i. +$$ + +
+Perform a resampling of the data where you split the data in training +data and test data. Here you can write your own function or use the +function for splitting training data provided by Scikit-Learn. +This function is called \( train\_test\_split \). + +
+It is normal in essentially all Machine Learning studies to split the +data in a training set and a test set (sometimes also an additional +validation set). There +is no explicit recipe for how much data should be included as training +data and say test data. An accepted rule of thumb is to use +approximately \( 2/3 \) to \( 4/5 \) of the data as training data. + +
+Implement the \( k \)-fold cross-validation algorithm (write your own +code) and evaluate again the MSE and the \( R^2 \) functions resulting +from the test data. You can compare your own code with that from +Scikit-Learn if needed. + +
+With a code which does OLS and includes resampling techniques, +we will now discuss the bias-variance tradeoff in the context of +continuous predictions such as regression. However, many of the +intuitions and ideas discussed here also carry over to classification +tasks and basically all Machine Learning algorithms. + +
+Consider a +dataset \( \mathcal{L} \) consisting of the data +\( \mathbf{X}_\mathcal{L}=\{(y_j, \boldsymbol{x}_j), j=0\ldots n-1\} \). + +
+Let us assume that the true data is generated from a noisy model + +$$ +\boldsymbol{y}=f(\boldsymbol{x}) + \boldsymbol{\epsilon}. +$$ + +
+Here \( \epsilon \) is normally distributed with mean zero and standard +deviation \( \sigma^2 \). + +
+In our derivation of the ordinary least squares method we defined then +an approximation to the function \( f \) in terms of the parameters +\( \boldsymbol{\beta} \) and the design matrix \( \boldsymbol{X} \) which embody our model, +that is \( \boldsymbol{\tilde{y}}=\boldsymbol{X}\boldsymbol{\beta} \). + +
+The parameters \( \boldsymbol{\beta} \) are in turn found by optimizing the means +squared error via the so-called cost function + +$$ +C(\boldsymbol{X},\boldsymbol{\beta}) =\frac{1}{n}\sum_{i=0}^{n-1}(y_i-\tilde{y}_i)^2=\mathbb{E}\left[(\boldsymbol{y}-\boldsymbol{\tilde{y}})^2\right]. +$$ + +
+Show that you can rewrite this as +$$ +\mathbb{E}\left[(\boldsymbol{y}-\boldsymbol{\tilde{y}})^2\right]=\frac{1}{n}\sum_i(f_i-\mathbb{E}\left[\boldsymbol{\tilde{y}}\right])^2+\frac{1}{n}\sum_i(\tilde{y}_i-\mathbb{E}\left[\boldsymbol{\tilde{y}}\right])^2+\sigma^2. +$$ + +
+Explain what the terms mean, which one is the bias and which one is +the variance and discuss their interpretations. + +
+Discuss the bias and variance tradeoff as function +of your model complexity (the degree of the polynomial) and the number +of data points, and possibly also your training and test data. + +
+Try to make a figure similar to Fig. 2.11 of Hastie, Tibshirani, and +Friedman, see the references below. You will most likely not get an +equally smooth curve! + +
+Write your own code for the Ridge method, either using matrix +inversion or the singular value decomposition as done in the previous +exercise or howework 2 (see also chapter 3.4 of Hastie et al., +equations (3.43) and (3.44)). Perform the same analysis as in the +previous exercises (for the same polynomials and include resampling +techniques) but now for different values of \( \lambda \). Compare and +analyze your results with those obtained in parts a-c). Study the +dependence on \( \lambda \). + +
+Study also the bias-variance tradeoff as function of various values of +the parameter \( \lambda \). Comment your results. + +
+This part is essentially a repeat of the previous two ones, but now +with Lasso regression. Write either your own code or, in this case, +you can also use the functionalities of Scikit-Learn (recommended). +Give a +critical discussion of the three methods and a judgement of which +model fits the data best. + +
+With our codes functioning and having been tested properly on a +simpler function we are now ready to look at real data. We will +essentially repeat in part g) what was done in parts a-e). However, we +need first to download the data and prepare properly the inputs to our +codes. We are going to download digital terrain data from the website +https://earthexplorer.usgs.gov/, + +
+In order to obtain data for a specific region, you need to register as +a user (free) at this website and then decide upon which area you want +to fetch the digital terrain data from. In order to be able to read +the data properly, you need to specify that the format should be SRTM +Arc-Second Global and download the data as a GeoTIF file. The +files are then stored in tif format which can be imported into a +Python program using + +
+ + +
scipy.misc.imread
++Here is a simple part of a Python code which reads and plots the data +from such files + +
+ + +
import numpy as np
+from imageio import imread
+import matplotlib.pyplot as plt
+from mpl_toolkits.mplot3d import Axes3D
+from matplotlib import cm
+
+# Load the terrain
+terrain1 = imread('SRTM_data_Norway_1.tif')
+# Show the terrain
+plt.figure()
+plt.title('Terrain over Norway 1')
+plt.imshow(terrain1, cmap='gray')
+plt.xlabel('X')
+plt.ylabel('Y')
+plt.show()
++If you should have problems in downloading the digital terrain data, +we provide two examples under the data folder of project 1. One is +from a region close to Stavanger in Norway and the other Møsvatn +Austfjell, again in Norway. +Feel free to produce your own terrain data. + +
+Our final part deals with the parameterization of your digital terrain +data. We will apply all three methods for linear regression as in +parts a-c), the same type (or higher order) of polynomial +approximation and the same resampling techniques to evaluate which +model fits the data best. + +
+At the end, you should pesent a critical evaluation of your results +and discuss the applicability of these regression methods to the type +of data presented here. + +
+Here follows a brief recipe and recommendation on how to write a report for each +project. + +
+The preferred format for the report is a PDF file. You can also use DOC or postscript formats or as an ipython notebook file. As programming language we prefer that you choose between C/C++, Fortran2008 or Python. The following prescription should be followed when preparing the report: + +
+If you have Python installed (we recommend Python3) and you feel pretty familiar with installing different packages, +we recommend that you install the following Python packages via pip as + +
+See below for a discussion of tensorflow and scikit-learn. + +
+For OSX users we recommend also, after having installed Xcode, to install brew. Brew allows +for a seamless installation of additional software via for example + +
+If you don't want to install various Python packages with their dependencies separately, we recommend two widely used distrubutions which set up all relevant dependencies for Python, namely + +
+ + + + +