Homework 04
- Overall Info
- Due date: 2027-06-21
- Submit a
*.pyfile with comments for the code - In groups of 5-6 students
1. ODE implementation
Harmonic Oscillator:
- setting \(v = x^\prime\), convert this second-order ODE into a two-dimensional first-order ODE \((x, v)\).
- Use the Euler scheme to compute the solution with \(\omega=1\), \(x(0) = 1\), \(v(0) = 1\)
- plot the curve \(t\mapsto (x(t), v(t))\) for \(0\leq t \leq 10\).
Consider the Damped harmonic oscillator:
- With the same notation, convert this equation into a first-order ODE in two dimensions.
- Use 4th-order Runge-Kutta to compute the solution with \(\omega = 1\), \(\gamma = 0.1\) and \(x(0) = v(0) = 1\).
- plot the solution and compare with the previous solution
2. Forward vs Backward (explicit vs implicit).
Explicit Euler
Implicit Euler
This second method involves solving a root problem to get \(y(t+h)\).
- Using
rootfromscipy.optimize, implement the implicit Euler
Consider the ODE
with solution \(y(t) = e^{-\lambda t}\)
- Use explicit Euler and 4th-order Runge-Kutta to compare with implicit Euler (precision and speed)
3. Gradient Descent
We implement the gradient descent on the blurred \(0\) and \(1\) images from sklearn package.
import numpy as np
import plotly.express as px
import plotly.graph_objs as go
from plotly.subplots import make_subplots
import sklearn.datasets as dt
from sklearn.model_selection import train_test_split
# load the images and their features (whether it is a 0 or a 1)
digits, target = dt.load_digits(n_class=2, return_X_y=True)
# the shape of the dataset
print(digits.shape)
# We plot a short sample
px.imshow(digits.reshape(360, 8, 8)[:10, :, :], facet_col=0, binary_string=True)
The goal is to perform a simple linear regression but using gradient descent by minimizing among all \(\mathbf{w} = [w_0, \ldots, w_{64}]^\top\) the objective function
where \(y_n\) is either \(0\) or \(1\) for the label of the \(n\)-th image and \(\bar{\mathbf{x}}_n = [1, \mathbf{x}_n]\) is the array of the image augmented with a \(1\).
- Modify the objective function and its gradient so that they take as input \(\mathbf{w}\), \(\mathbf{x}\) and \(\mathbf{y}\), and modify the gradient descent function.
Since our goal is to find a correct specification to further classify data, we calibrate the model on a training set and evaluate its accuracy on a testing set. To split the two sets of data we use sklearn
x_train, x_test, y_train, y_test = train_test_split(
digits,
target,
test_size=0.2,
random_state=10
)
- Using gradient descent, calibrate the optimal value of \(\mathbf{w}\) on the training set
(x_train, y_train). Plot the value off_history(choose a maximum of 100 iterations and a threshold of 0.1, and tweak momentum and learning rate until you reach a satisfactory convergence rate and error)
Now we want to be able to classify the data. We proceed as follows
If \(\mathbf{w}^\top \bar{\mathbf{x}} \geq 0.5\) it is classified as \(1\) otherwise it is classified as \(0\).
-
Given \(\mathbf{w}\), \(N\) samples \((\mathbf{x}_1, y_1), \ldots,(\mathbf{x}_N, y_N)\), design a function
accuracy(w, x, y)that returns the percentage of correctly classified values. -
Using this function, provide the accuracy of your solution on the training set
(x_train, y_train)and on the testing set(x_test, y_test). -
Optional Question: compare the gradient descent method with the traditional OLS linear regression method.