Basis Functions in Linear Regression Models, with an OLS example
I give an overview of how basis functions are used in linear regression models. Then, I derive the solution to an OLS problem with basis functions involved.
In the field of supervised learning, one of the most fundamental linear models is the Linear Regression model.
Let’s begin with a quick recap on how to mathematically set up a linear regression model:
Basic Linear Regression
Step 1: Define hypothesis space H
Whereby $w_0$ and $w_1$ are parameters of the model, and $w_0 , w_1 \space \epsilon \space R$
The goal is to find the optimal values of $w_0$ and $w_1$ that best fit the data.
Step 2: Define the loss function and ERM
Firstly, we need to define a loss function:
Then, the empirical risk minimization problem becomes:
Step 3: Solve for the best approximation
To minimize loss, we find the derivative of the empirical loss function, and recognize that loss is minimum at point where derivative value = 0.
So, we need to find the derivative of the loss function with respect to $w_0$ and $w_1$.
General Linear Basis Models
The simple linear regression we have seen is quite limited - it is only suitable for 1-dimensional inputs, and can only fit linear relationships. Now, we’ll see how linear basis models help us generalize the simple linear regression approach.
Consider an input $x$ that lies in a higher-dimensional space. We can apply a feature map $\phi (x)$ to transform this input into a new space where we use $\phi (x)$ to model the problem. This transformation allows us to handle non-linear relationships from the original input space while maintaining a linear relationship between the transformed features and target $y$.
Let's use a simple example to illustrate what this all means:
Let’s say the relationship between your $x$ and $y$ variables resembles something like $y = x^2$. whereby the relationship between $x$ and $y$ is non-linear. So, the $x$ here is considered as being in a ‘space’ where it is not straightforward to model $y$ linearly.
So, we use a feature map $\phi (x) = x^2$ to transform $x$ into a new feature space, where $\phi (x)$ and $y$ have a linear relationship.
In this example,
- Original relationship: $y = x^2$
- Feature map: $\phi (x)$ = $x^2$
- Transformed relationship: y = $\phi (x)$
In the transformed space $\phi (x)$, the relationship is now linear because $y$ is directly proportional to $\phi (x)$.
From a higher-level perspective, we can see that basis functions allow us to work with non-linear $x$ inputs while having linear parameters that are easy to compute.
Let's mathematically formulate the hypothesis space for the feature map
Considering $x \space \epsilon \space R^d$,
Whereby each $\phi_j : R^d \rightarrow R$ is called a basis function or a feature map. Our feature map can also generate multiple features, depending on the value of $M$.
For example, our feature map $\phi (x)$ could generate $\phi_1 (x) = x$, $\phi_2 (x) = x^2$, and more.
Applying Linear Basis Models to Ordinary Least Squares
Now, let’s see how the linear basis models can be used to solve an OLS problem.
Remember, due to the feature mapping, our $x$ input is modified to be $\phi_j (x_i)$, so the input to our ordinary least squares equation becomes $w_j \phi_j (x_i)$, where loss = $w_j \phi_j (x_i) - y_i$
We can also rewrite the end result in compact form:
Whereby
Now, let's solve for the least squares solution
Let $\vartriangle \text{Squared Error}$ = 0, then:
Here, we can see how linear basis models can model more complex relationships than simple linear regression models, and we have shown how the least squares solution can be derived relatively simply for the linear basis models too.