Welcome to the exercise repository for day 3 of our course. To help you, we have prepared unit-tests. Use:
nox -s test
While coding, use nox -s lint
, and nox -s typing
to check your code.
Autoformatting help is available via nox -s format
.
Feel free to read more about nox at https://nox.thea.codes/en/stable/ .
Use b = pandas.read_csv('./data/noisy_signal.tab')
to load a noisy signal.
The first part will be concerned with modelling this signal using polynomials.
Linear regression is usually a good first step. Start by implementing the function
set_up_point_matrix
from the src/regularization.py
module.
The function should produce polynomial-coordinate matrices
With n=2,
will produce the coefficients for a straight line. Evaluate your first-degree polynomial via matplotlib.pyplot
's plot
function.
The straight line above is insufficient to model the data. Using your
implementation of set_up_point_matrix
set
Having estimated the coefficients
computes the function values. Plot the original points and the function values using matplotlib. What do you see?
Unfortunately, the fit is not ideal. The polynomial tracks the noise. The singular value decomposition (SVD) can help! Recall that the SVD turns a matrix
into the form:
In the SVD-Form computing, the inverse is simple. Swap
This results in the matrix
A solution to the overfitting problem is to filter the singular values. Compute a diagonal for a filter matrix by evaluating:
The idea is to compute a loop over
Apply the regularization by computing:
with
Setting
Another solution to the overfitting problem is reducing the complexity of the model. 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.
MSE can be calculated using the following equation, where
Are the degree of the polynomial and the MSE linked?
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.
The file ./data/pegel.tab
contains the Rhine water levels measured in Bonn over the last 100 years.
Data source: https://pegel.bonn.de.
The src/pegel_bonn.py
file already contains code to pre-load the data for you.
Make the Rhine level measurements your new vector
Generate a matrix A with n=2 using the timestamps for the data set and compute
Plot the result. Compute the zero. When do the regression line and the x-axis intersect?
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.
Plot the result.
Focus on the data from the year 2000 onward and filter the singular values. Matrix A is not square in this case. Consequently, a zero block must appear in your singular value matrix. Plot filtered eigen-polynomials using epsilon equal to 0.1, 1e-3, 1e-9.