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 ax+b.
Plot the result using matplotlib.pyplot
's plot
function.
The straight line above is insufficient to model the data. Using your
implementation of set_up_point_matrix
, set n=300 (to set up a square matrix) and fit the polynomial
by computing
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 U and V and replace every of the m singular values with it's inverse
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 i for all of the m singular values. Roughly speaking, multiplication by f underscore i will filter a singular value when
Apply the regularization by computing:
with
Setting n=300 turns A into a square matrix. In this case, the zero block in the sigma-matrix disappears. Plot the result for epsilon equal to 0.1, 1e-6, and 1e-12.
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 N is the number of samples,
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. Study the scaling of the timestamp values. Is the range suitable for stable numerical computations? 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.