Files
FYS-STK4155/doc/LectureNotes/exercisesweek37.ipynb
T
Morten Hjorth-Jensen f202dd1b7c updated exercises week37
2025-09-07 14:54:43 +02:00

424 lines
14 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
{
"cells": [
{
"cell_type": "markdown",
"id": "8e6632a0",
"metadata": {
"editable": true
},
"source": [
"<!-- HTML file automatically generated from DocOnce source (https://github.com/doconce/doconce/)\n",
"doconce format html exercisesweek37.do.txt -->\n",
"<!-- dom:TITLE: Exercises week 37 -->"
]
},
{
"cell_type": "markdown",
"id": "82705c4f",
"metadata": {
"editable": true
},
"source": [
"# Exercises week 37\n",
"**Implementing gradient descent for Ridge and ordinary Least Squares Regression**\n",
"\n",
"Date: **September 8-12, 2025**"
]
},
{
"cell_type": "markdown",
"id": "921bf331",
"metadata": {
"editable": true
},
"source": [
"## Learning goals\n",
"\n",
"After having completed these exercises you will have:\n",
"1. Your own code for the implementation of the simplest gradient descent approach applied to ordinary least squares (OLS) and Ridge regression\n",
"\n",
"2. Be able to compare the analytical expressions for OLS and Ridge regression with the gradient descent approach\n",
"\n",
"3. Explore the role of the learning rate in the gradient descent approach and the hyperparameter $\\lambda$ in Ridge regression\n",
"\n",
"4. Scale the data properly"
]
},
{
"cell_type": "markdown",
"id": "adff65d5",
"metadata": {
"editable": true
},
"source": [
"## Simple one-dimensional second-order polynomial\n",
"\n",
"We start with a very simple function"
]
},
{
"cell_type": "markdown",
"id": "70418b3d",
"metadata": {
"editable": true
},
"source": [
"$$\n",
"f(x)= 2-x+5x^2,\n",
"$$"
]
},
{
"cell_type": "markdown",
"id": "11a3cf73",
"metadata": {
"editable": true
},
"source": [
"defined for $x\\in [-2,2]$. You can add noise if you wish. \n",
"\n",
"We are going to fit this function with a polynomial ansatz. The easiest thing is to set up a second-order polynomial and see if you can fit the above function.\n",
"Feel free to play around with higher-order polynomials."
]
},
{
"cell_type": "markdown",
"id": "04a06b51",
"metadata": {
"editable": true
},
"source": [
"## Exercise 1, scale your data\n",
"\n",
"Before fitting a regression model, it is good practice to normalize or\n",
"standardize the features. This ensures all features are on a\n",
"comparable scale, which is especially important when using\n",
"regularization. Here we will perform standardization, scaling each\n",
"feature to have mean 0 and standard deviation 1."
]
},
{
"cell_type": "markdown",
"id": "408db3d9",
"metadata": {
"editable": true
},
"source": [
"### 1a)\n",
"\n",
"Compute the mean and standard deviation of each column (feature) in your design/feature matrix $\\boldsymbol{X}$.\n",
"Subtract the mean and divide by the standard deviation for each feature.\n",
"\n",
"We will also center the target $\\boldsymbol{y}$ to mean $0$. Centering $\\boldsymbol{y}$\n",
"(and each feature) means the model does not require a separate intercept\n",
"term, the data is shifted such that the intercept is effectively 0\n",
". (In practice, one could include an intercept in the model and not\n",
"penalize it, but here we simplify by centering.)\n",
"Choose $n=100$ data points and set up $\\boldsymbol{x}, $\\boldsymbol{y}$ and the design matrix $\\boldsymbol{X}$."
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "37fb732c",
"metadata": {
"collapsed": false,
"editable": true
},
"outputs": [],
"source": [
"# Standardize features (zero mean, unit variance for each feature)\n",
"X_mean = X.mean(axis=0)\n",
"X_std = X.std(axis=0)\n",
"X_std[X_std == 0] = 1 # safeguard to avoid division by zero for constant features\n",
"X_norm = (X - X_mean) / X_std\n",
"\n",
"# Center the target to zero mean (optional, to simplify intercept handling)\n",
"y_mean = ?\n",
"y_centered = ?"
]
},
{
"cell_type": "markdown",
"id": "d861e1e3",
"metadata": {
"editable": true
},
"source": [
"Fill in the necessary details. Do we need to center the $y$-values? \n",
"\n",
"After this preprocessing, each column of $\\boldsymbol{X}_{\\mathrm{norm}}$ has mean zero and standard deviation $1$\n",
"and $\\boldsymbol{y}_{\\mathrm{centered}}$ has mean 0. This makes the optimization landscape\n",
"nicer and ensures the regularization penalty $\\lambda \\sum_j\n",
"\\theta_j^2$ in Ridge regression treats each coefficient fairly (since features are on the\n",
"same scale)."
]
},
{
"cell_type": "markdown",
"id": "b3e774d0",
"metadata": {
"editable": true
},
"source": [
"## Exercise 2, calculate the gradients\n",
"\n",
"Find the gradients for OLS and Ridge regression using the mean-squared error as cost/loss function."
]
},
{
"cell_type": "markdown",
"id": "d5dc7708",
"metadata": {
"editable": true
},
"source": [
"## Exercise 3, using the analytical formulae for OLS and Ridge regression to find the optimal paramters $\\boldsymbol{\\theta}$"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "4c9c86ac",
"metadata": {
"collapsed": false,
"editable": true
},
"outputs": [],
"source": [
"# Set regularization parameter, either a single value or a vector of values\n",
"# Note that lambda is a python keyword. The lambda keyword is used to create small, single-expression functions without a formal name. These are often called \"anonymous functions\" or \"lambda functions.\"\n",
"lam = ?\n",
"\n",
"\n",
"# Analytical form for OLS and Ridge solution: theta_Ridge = (X^T X + lambda * I)^{-1} X^T y and theta_OLS = (X^T X)^{-1} X^T y\n",
"I = np.eye(n_features)\n",
"theta_closed_formRidge = ?\n",
"theta_closed_formOLS = ?\n",
"\n",
"print(\"Closed-form Ridge coefficients:\", theta_closed_form)\n",
"print(\"Closed-form OLS coefficients:\", theta_closed_form)"
]
},
{
"cell_type": "markdown",
"id": "eeae00fd",
"metadata": {
"editable": true
},
"source": [
"This computes the Ridge and OLS regression coefficients directly. The identity\n",
"matrix $I$ has the same size as $X^T X$. It adds $\\lambda$ to the diagonal of $X^T X$ for Ridge regression. We\n",
"then invert this matrix and multiply by $X^T y$. The result\n",
"for $\\boldsymbol{\\theta}$ is a NumPy array of shape (n$\\_$features,) containing the\n",
"fitted parameters $\\boldsymbol{\\theta}$."
]
},
{
"cell_type": "markdown",
"id": "e1c215d5",
"metadata": {
"editable": true
},
"source": [
"### 3a)\n",
"\n",
"Finalize, in the above code, the OLS and Ridge regression determination of the optimal parameters $\\boldsymbol{\\theta}$."
]
},
{
"cell_type": "markdown",
"id": "587dd3dc",
"metadata": {
"editable": true
},
"source": [
"### 3b)\n",
"\n",
"Explore the results as function of different values of the hyperparameter $\\lambda$. See for example exercise 4 from week 36."
]
},
{
"cell_type": "markdown",
"id": "bfa34697",
"metadata": {
"editable": true
},
"source": [
"## Exercise 4, Implementing the simplest form for gradient descent\n",
"\n",
"Alternatively, we can fit the ridge regression model using gradient\n",
"descent. This is useful to visualize the iterative convergence and is\n",
"necessary if $n$ and $p$ are so large that the closed-form might be\n",
"too slow or memory-intensive. We derive the gradients from the cost\n",
"functions defined above. Use the gradients of the Ridge and OLS cost functions with respect to\n",
"the parameters $\\boldsymbol{\\theta}$ and set up (using the template below) your own gradient descent code for OLS and Ridge regression.\n",
"\n",
"Below is a template code for gradient descent implementation of ridge:"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "49245f55",
"metadata": {
"collapsed": false,
"editable": true
},
"outputs": [],
"source": [
"# Gradient descent parameters, learning rate eta first\n",
"eta = 0.1\n",
"# Then number of iterations\n",
"num_iters = 1000\n",
"\n",
"# Initialize weights for gradient descent\n",
"theta = np.zeros(n_features)\n",
"\n",
"# Gradient descent loop\n",
"for t in range(num_iters):\n",
" # Compute gradients for OSL and Ridge\n",
" grad_OLS = ?\n",
" grad_Ridge = ?\n",
" # Update parameters theta\n",
" theta_gdOLS = ?\n",
" theta_gdRidge = ? \n",
"\n",
"# After the loop, theta contains the fitted coefficients\n",
"theta_gdOLS = ?\n",
"theta_gdRidge = ?\n",
"print(\"Gradient Descent OLS coefficients:\", theta_gdOLS)\n",
"print(\"Gradient Descent Ridge coefficients:\", theta_gdRidge)"
]
},
{
"cell_type": "markdown",
"id": "f3f43f2c",
"metadata": {
"editable": true
},
"source": [
"### 4a)\n",
"\n",
"Write first a gradient descent code for OLS only using the above template.\n",
"Discuss the results as function of the learning rate parameters and the number of iterations"
]
},
{
"cell_type": "markdown",
"id": "9ba303be",
"metadata": {
"editable": true
},
"source": [
"### 4b)\n",
"\n",
"Write then a similar code for Ridge regression using the above template.\n",
"Try to add a stopping parameter as function of the number iterations and the difference between the new and old $\\theta$ values. How would you define a stopping criterion?"
]
},
{
"cell_type": "markdown",
"id": "78362c6c",
"metadata": {
"editable": true
},
"source": [
"## Exercise 5, Ridge regression and a new Synthetic Dataset\n",
"\n",
"We create a synthetic linear regression dataset with a sparse\n",
"underlying relationship. This means we have many features but only a\n",
"few of them actually contribute to the target. In our example, well\n",
"use 10 features with only 3 non-zero weights in the true model. This\n",
"way, the target is generated as a linear combination of a few features\n",
"(with known coefficients) plus some random noise. The steps we include are:\n",
"\n",
"Decide on the number of samples and features (e.g. 100 samples, 10 features).\n",
"Define the **true** coefficient vector with mostly zeros (for sparsity). For example, we set $\\hat{\\boldsymbol{\\theta}} = [5.0, -3.0, 0.0, 0.0, 0.0, 0.0, 2.0, 0.0, 0.0, 0.0]$, meaning only features 0, 1, and 6 have a real effect on y.\n",
"\n",
"Then we sample feature values for $\\boldsymbol{X}$ randomly (e.g. from a normal distribution). We use a normal distribution so features are roughly centered around 0.\n",
"Then we compute the target values $y$ using the linear combination $\\boldsymbol{X}\\hat{\\boldsymbol{\\theta}}$ and add some noise (to simulate measurement error or unexplained variance).\n",
"\n",
"Below is the code to generate the dataset:"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "8be1cebe",
"metadata": {
"collapsed": false,
"editable": true
},
"outputs": [],
"source": [
"import numpy as np\n",
"\n",
"# Set random seed for reproducibility\n",
"np.random.seed(0)\n",
"\n",
"# Define dataset size\n",
"n_samples = 100\n",
"n_features = 10\n",
"\n",
"# Define true coefficients (sparse linear relationship)\n",
"theta_true = np.array([5.0, -3.0, 0.0, 0.0, 0.0, 0.0, 2.0, 0.0, 0.0, 0.0])\n",
"\n",
"# Generate feature matrix X (n_samples x n_features) with random values\n",
"X = np.random.randn(n_samples, n_features) # standard normal distribution\n",
"\n",
"# Generate target values y with a linear combination of X and theta_true, plus noise\n",
"noise = 0.5 * np.random.randn(n_samples) # Gaussian noise\n",
"y = X.dot @ theta_true + noise"
]
},
{
"cell_type": "markdown",
"id": "e2693666",
"metadata": {
"editable": true
},
"source": [
"This code produces a dataset where only features 0, 1, and 6\n",
"significantly influence $\\boldsymbol{y}$. The rest of the features have zero true\n",
"coefficient. For example, feature 0 has\n",
"a true weight of 5.0, feature 1 has -3.0, and feature 6 has 2.0, so\n",
"the expected relationship is:"
]
},
{
"cell_type": "markdown",
"id": "bc954d12",
"metadata": {
"editable": true
},
"source": [
"$$\n",
"y \\approx 5 \\times x_0 \\;-\\; 3 \\times x_1 \\;+\\; 2 \\times x_6 \\;+\\; \\text{noise}.\n",
"$$"
]
},
{
"cell_type": "markdown",
"id": "6534b610",
"metadata": {
"editable": true
},
"source": [
"You can remove the noise if you wish to. \n",
"\n",
"Try to fit the above data set using OLS and Ridge regression with the analytical expressions and your own gradient descent codes.\n",
"\n",
"If everything worked correctly, the learned coefficients should be\n",
"close to the true values [5.0, -3.0, 0.0, …, 2.0, …] that we used to\n",
"generate the data. Keep in mind that due to regularization and noise,\n",
"the learned values will not exactly equal the true ones, but they\n",
"should be in the same ballpark. Which method (OLS or Ridge) gives the best results?"
]
}
],
"metadata": {},
"nbformat": 4,
"nbformat_minor": 5
}