You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The $c$-values are the coefficients of the polynomial - these numbers are to be estimated!
23
+
The $a-$ and $b-$values are already given.
24
+
If we plug all the given values into the general form of the polynomial we get a system of linear equations which can be reformulated as a matrix multiplication:
where $\mathbf{A}_n^{\dagger}$ is the Pseudo-Inverse.
56
+
57
+
For linear regression, the polynomial will be of order n=2 and will look like this:
58
+
59
+
$$ f(a) = c_1 + c_2 \cdot a $$
60
+
61
+
This will just be a straight line and there are only two coefficients $c_1$ and $c_2$ that have to be estimated.
62
+
Very often, this line is too simple to explain the data sufficiently.
63
+
That is why we want to fit polynomials of higher order, so $n > 2$.
64
+
Unfortunately, the more complex the model gets (i.e. the higher the order of the polynomial gets), the more noise will be tracked. Here we can make use of regularization techniques.
65
+
In the first and following part, you will be given artificial data and in the second part you will make use of real data!
66
+
67
+
### Part 1: Proof of concept
68
+
The line `b = pandas.read_csv('./data/noisy_signal.tab')` is used to load a noisy signal.
69
+
The line `x_axis = np.linspace(0, 1, num=len(b_noise))` will provide you with corresponding x-values (these are the a-values from above and the lecture).
70
+
This is some artificial data that serves as a means to try out the concepts you have learned about in the lecture.
71
+
The first part will be concerned with modeling this signal using polynomials.
72
+
73
+
#### ⊙ Task 1.1: Regression
74
+
Linear regression is usually a good first step.
34
75
35
-
$$\mathbf{A}_2^{\dagger}\mathbf{b} = \mathbf{x}$$
76
+
1. Start by implementing the function `set_up_point_matrix` from the `src/regularization.py` module.
77
+
The function should produce polynomial-coordinate matrices $\mathbf{A}_n$ of the form:
78
+
$$
79
+
\mathbf{A}_n =
80
+
\begin{pmatrix}
81
+
1 & a_1^1 & a_1^2 & \dots & a_1^{n-1} \\\\
82
+
1 & a_2^1 & a_2^2 & \dots & a_2^{n-1} \\\\
83
+
1 & a_3^1 & a_3^2 & \dots & a_3^{n-1} \\\\
84
+
\vdots & \vdots & \vdots & \ddots & \vdots \\\\
85
+
1 & a_m^1 & a_m^2 & \dots & a_m^{n-1} \\\\
86
+
\end{pmatrix}
87
+
$$
36
88
37
-
will produce the coefficients for a straight line. Evaluate your first-degree polynomial via $ax+b$.
38
-
Plot the result using `matplotlib.pyplot`'s `plot` function.
89
+
2. Go to the main-function and use the function you just implemented to create the point-matrix A for n=2.
will produce the coefficients for a straight line.
40
97
41
-
#### Fitting a Polynomial to a function:
42
-
The straight line above is insufficient to model the data. Using your
43
-
implementation of `set_up_point_matrix` set $n=300$ (to set up a square matrix) and fit the polynomial
44
-
by computing
98
+
4. Evaluate your first-degree polynomial via $c_1 + c_2 \cdot x$ and plot the result as well as the original data using `matplotlib.pyplot`'s `plot` function.
computes the function values. Plot the original points and the function values using matplotlib.
105
+
#### ⊙Task 1.2: Fitting a Polynomial to a function
106
+
The straight line above is insufficient to model the data.
107
+
So perform the very same steps as above, but change the degree of the polynomial to n=300 (to set up a square matrix since we have 300 data-points):
108
+
1. Set up the point matrix by setting n=300.
109
+
2. Estimate the coefficients via $$\mathbf{A}^{\dagger}\mathbf{b} = \mathbf{x}_{\text{fit}}.$$
110
+
3. Having estimated the coefficients $$\mathbf{A} \mathbf{x}_{\text{fit}}$$ computes the function values.
111
+
Plot the original points and the function values using matplotlib.
53
112
What do you see?
54
113
55
114
115
+
Solution:
116
+
117
+

56
118
57
-
#### Regularization:
58
-
Unfortunately, the fit is not ideal. The polynomial tracks the noise.
119
+
120
+
#### ⊙Task 1.3: Regularization
121
+
Unfortunately, the fit is not ideal. The polynomial is too complex and tracks the noise.
59
122
The singular value decomposition (SVD) can help!
60
123
Recall that the SVD turns a matrix
61
124
62
125
$$\mathbf{A} \in \mathbb{R}^{m,n}$$
63
126
64
127
into the form:
65
128
66
-
$$ \mathbf{A} = \mathbf{U} \Sigma \mathbf{V}^T
67
-
$$
129
+
$$\mathbf{A} = \mathbf{U} \Sigma \mathbf{V}^T$$
68
130
69
-
In the SVD-Form computing, the inverse is simple. Swap $U$ and $V$ and replace every of the m singular values with it's inverse
131
+
In the SVD-Form, computing the pseudoinverse is simple! Swap U and V and replace every of the m singular values with it's inverse
70
132
71
133
$$1/\sigma_i .$$
72
134
@@ -75,26 +137,36 @@ This results in the matrix
75
137
\Sigma^\dagger = \begin{pmatrix}
76
138
\sigma_1^{-1} & & & \\\\
77
139
& \ddots & \\\\
78
-
& & \sigma_n^{-1} \\\\ \hline
140
+
& & \sigma_m^{-1} \\\\ \hline
79
141
& 0 &
80
-
\end{pmatrix}^T
142
+
\end{pmatrix}
81
143
```
82
144
83
-
A solution to the overfitting problem is to filter the singular values.
145
+
A solution to the overfitting problem is to filter the singular values.
146
+
The idea is, that small singular values often correspond to directions in the data where noise dominates. And now we want to create a filter matrix, that gets rid of singular values if they are too small (i.e. if they fall below a threshold $\epsilon$).
84
147
Compute a diagonal for a filter matrix by evaluating:
85
148
86
149
$$f_i = \sigma_i^2 / (\sigma_i^2 + \epsilon)$$
87
150
88
-
The idea is to compute a loop over $i$ for all of the m singular values.
151
+
89
152
Roughly speaking multiplication by $f_i$ will filter a singular value when
90
153
91
-
$$\sigma_i \lt \epsilon .$$
154
+
$$\sigma_i \lt \epsilon ,$$
155
+
since in this case, $f_i$ will be close to $0$.
156
+
If however
157
+
$$\sigma_i \geq \epsilon ,$$
158
+
$f_i$ will be closer to $1$ and the respective singular value will not be filtered out.
Setting $n=300$ turns $A$ into a square matrix. In this case, the zero block in the sigma-matrix disappears.
107
-
Plot the result for epsilon equal to 0.1, 1e-6, and 1e-12.
178
+
Setting n=300 turns A into a square matrix. In this case, the zero block in the sigma-matrix disappears and you don't have to worry about transposing $\sigma$ when computing the pseudoinverse.
108
179
109
-
#### Model Complexity (Optional):
110
-
Another solution to the overfitting problem is reducing the complexity of the model.
111
-
To assess the quality of polynomial fit to the data, compute and plot the Mean Squared Error (Mean Squared Error (MSE) measure how close the regression line is to data points) for every degree of polynomial up to 20.
180
+
To sum it up, your tasks are:
181
+
1. Compute the SVD of A.
112
182
113
-
MSE can be calculated using the following equation, where $N$ is the number of samples, $y_i$ is the original point and $\hat{y_i}$ is the predictied output.
183
+
Perform the following steps 2. - 4. for epsilon equal to 0.1, 1e-6, and 1e-12.
184
+
2. Compute the diagonal for the filter matrix and turn it into a matrix.
185
+
3. Estimate the regularized coefficients by applying the formula above.
186
+
4. Plot the result.
187
+
188
+
Solution:
189
+
190
+

191
+
192
+
#### ✪Task 1.4: Model Complexity (Optional):
193
+
Another solution to the overfitting problem is reducing the complexity of the model.
194
+
To assess the quality of polynomial fit to the data, compute and plot the Mean Squared Error (Mean Squared Error measure how close the regression line is to data points) for every degree of polynomial upto 20.
195
+
So as before:
196
+
1. Set up the point matrix for the current degree from 1 to 20.
197
+
2. Estimate the coefficients.
198
+
3. Compute the predictions.
199
+
4. Calculate the MSE.
200
+
MSE can be calculated using the following equation, where $N$ is the number of samples, $y_i$ is the original point and $\hat{y_i}$ is the predicted output.
Now we are ready to deal with real data! Feel free to use your favorite time series data or work with the Rhine level data we provide.
120
220
The file `./data/pegel.tab` contains the Rhine water levels measured in Bonn over the last 100 years.
121
221
Data source: https://pegel.bonn.de.
122
222
123
-
#### Regression:
223
+
#### ⊙Task 2.1 Regression
124
224
The `src/pegel_bonn.py` file already contains code to pre-load the data for you.
125
-
Make the Rhine level measurements your new vector $\mathbf{b}$.
225
+
The Rhine level measurements will be your new vector $\mathbf{b}$ from before.
226
+
Now we want to do the same as in Part 1 and start with linear regression!
227
+
1. Generate a matrix A with n=2 using the timestamps for the data set as your x-values.
228
+
2. Compute $$\mathbf{A}^{\dagger}\mathbf{b}$$ to estimate the coefficients of the line.
229
+
3. Evaluate your polynomial and plot the result.
230
+
4. Compute the zero-intercept with the y-axis. When do the regression line and the x-axis intersect?
231
+
Or in other words: On which day will the Rhine water level be at 0 cm?
232
+
> **Hint:** Plug in $y=0$ into the equation of your line with the estimated coefficients and solve for the date $x$.
126
233
127
-
Generate a matrix A with n=2 using the timestamps for the data set and compute
234
+
Solution:
128
235
129
-
$$\mathbf{A}^{\dagger}\mathbf{b}.$$
236
+

130
237
131
-
Plot the result. Compute the zero. When do the regression line and the x-axis intersect?
132
238
133
-
#### Fitting a higherorder Polynomial:
239
+
#### ⊙ Task 2.2: Fitting a higher-order Polynomial
134
240
135
241
Re-using the code you wrote for the proof of concept task, fit a polynomial of degree 20 to the data. Before plotting have a closer look at `datetime_stamps` and its values and scale the axis appropriately.
136
-
Plot the result.
137
242
138
-
#### Regularization:
243
+
1. Scale the x-axis.
244
+
2. Set up the point matrix for the scaled x-axis with n=20.
245
+
3. Compute the coefficients.
246
+
4. Evaluate the polynomial.
247
+
5. Plot the result.
248
+
249
+
Solution:
250
+
251
+

252
+
253
+
254
+
255
+
#### ⊙Task 2.3: Regularization
139
256
Focus on the data from the year 2000 onward and filter the singular values.
140
-
Matrix A is not square in this case. Consequently, a zero block must appear in your singular value matrix.
141
-
Plot filtered eigen-polynomials using epsilon equal to 0.1, 1e-3, 1e-9.
257
+
We will use again a degree of 20.
258
+
Matrix A is not square in this case, because the degree is smaller than the number of datapoints! Consequently, a zero block must appear in your singular value matrix and when computing the Pseudoinverse from the SVD, $\sigma$ has to be transposed!
259
+
Like in Part 1:
260
+
1. Compute the SVD of the point matrix from the previous task.
261
+
262
+
Perform the following steps 2. - 4. for epsilon equal to 0.1, 1e-3, and 1e-9.
263
+
2. Compute the filter matrix.
264
+
3. Estimate the regularized coefficients by applying the formula from before.
265
+
> **Hint:** Remember the zero-block! You need degree-many rows and number-of-datapoints-many columns!
266
+
4. Evaluate the regularized polynomial and plot the results.
0 commit comments