Make a lecture like this from your own PDF
Drop a PDF, slides or notes here. You'll sign up, then yours starts.
0:00 / 10:26
Shared by Joseph Karki
Linear regression is all about predicting a continuous number by finding a line that best fits your data. You represent your data in a matrix X (samples as rows, features as columns) and use a weight vector w to make predictions. The "best" fit is found by minimizing the sum of squared errors, which is mathematically powerful because, if your data noise follows a normal distribution, minimizing this error is actually the statistically "correct" way to find the most likely weights.
If your data isn't a straight line, you can use basis functions to transform your inputs into higher-dimensional space. This lets you fit complex curves (like parabolas or sine waves) while keeping the math linear. However, if you add too many features, you run into overfitting, where the model starts chasing noise instead of the actual pattern.
To fix overfitting, we use regularization, specifically ridge regression. By adding a penalty term that discourages your weights from getting too large, you keep the model simple and smooth. Interestingly, this isn't just a trick to make models work better; from a Bayesian perspective, it’s a way of saying you believe your model weights should start out small.
Finally, all of this relies on matrix calculus. Because we represent everything as matrix equations, finding the optimal weights simplifies down to a neat formula called the normal equation. As long as your features aren't redundant and you have enough data points, you can solve for the perfect weights in one clean step.
0:00Let's start with a question. You have some data — house prices, temperature readings, stock values — and you want to predict a continuous number from it. That's regression. And the simplest, most powerful place to start is linear regression. The idea is straightforward: we assume the output is roughly a linear function of the input, plus some noise. So our model looks like y equals w transpose x, plus epsilon — where w is a vector of weights we need to learn, and epsilon is random noise we can't predict. The goal is to find the weights that make our line fit the data as closely as possible. But what does "as closely as possible" actually mean? That's where the squared error comes in — and we'll see exactly why it's the right choice.
0:52Before we can derive anything, we need to get comfortable with how machine learning represents data. Typically, we arrange our data into a matrix X, where each row is one data sample — one house, one patient, one measurement — and each column is a feature — like square footage, number of bedrooms, age of the property. So X is an N by d matrix, where N is the number of samples and d is the number of features. Our targets, the values we want to predict, go into a vector y of length N. And the weights we're trying to learn form a vector w of length d. The prediction for all data points at once is just X times w — a matrix-vector product. This compact notation is going to make everything that follows much cleaner.
1:48What if your data isn't linear? A straight line won't fit a parabola or a sine wave. But here's the clever trick: we can keep the linear algebra and just transform the inputs. Instead of using x directly, we apply basis functions — phi of x — which can be polynomials, Gaussians, sigmoids, or any fixed nonlinear transformation. The model becomes w transpose times phi of x. It's still linear in the parameters w, so all our previous math still works. We just replace the data matrix X with a feature matrix Phi, where each column is one basis function evaluated at all data points. The solution becomes w star equals Phi transpose Phi inverse, times Phi transpose y. This is incredibly powerful — we can fit curves, surfaces, and complex patterns while using nothing more than linear algebra.
2:53There's a catch. The formula w star equals X transpose X inverse times X transpose y only works if X transpose X is actually invertible. When is it not invertible? Two common cases. First, if you have fewer data points than features — N is less than d — then X transpose X is a d by d matrix but its rank is at most N, so it's singular. You simply don't have enough information to uniquely determine all the weights. Second, if two features are perfectly correlated — say, you include both height in centimeters and height in inches — then the columns of X are linearly dependent, and again X transpose X is singular. In practice, as long as you have more data points than features and your features aren't redundant, the solution exists and is unique.
3:57To minimize our error, we need derivatives — but not just ordinary derivatives. We need to take derivatives of scalar functions with respect to vectors. This is called matrix calculus, and there are just two rules you need to remember. First, the linear rule: the derivative of a transpose x with respect to x is just the vector a. Think of it as the vector version of the scalar rule — the derivative of a times x is a. Second, the quadratic rule: the derivative of x transpose A x with respect to x is A plus A transpose, times x. If A is symmetric, which it almost always will be in our case, this simplifies to 2 A x. That's it. With these two rules, we can derive the entire least squares solution in just a few lines.
4:56We can take the probabilistic view one step further. What if we also have a prior belief about the weights — say, that they should be small and close to zero? This is the Bayesian approach. We place a Gaussian prior on w, with mean zero and variance sigma_w squared. Using Bayes' theorem, the posterior distribution over w given the data is proportional to the likelihood times the prior. If we maximize the log of this posterior — the MAP, or maximum a posteriori estimate — we get a familiar objective. It's the sum of squared errors plus an extra term: sigma squared over sigma_w squared, times w transpose w. And that is exactly ridge regression, where the regularization parameter lambda equals sigma squared divided by sigma_w squared. So ridge regression isn't just a hack to prevent overfitting — it's the principled Bayesian way of incorporating prior knowledge that weights should be small. This connection between probability and regularization is one of the most elegant ideas in machine learning.
6:09With basis functions, we can fit very complex functions. But there's a danger: if we use too many basis functions, we'll start fitting the noise in the data, not just the signal. This is called overfitting. The solution is regularization — adding a penalty that discourages the weights from becoming too large. Ridge regression adds a penalty term, lambda times the squared norm of w, to the sum of squared errors. The new objective is SSE plus lambda times w transpose w. Taking the derivative and setting to zero gives us a modified solution: w star equals X transpose X plus lambda I, inverse, times X transpose y. Notice that lambda I is added to X transpose X, which makes the matrix invertible even when X transpose X was singular. Lambda is a tuning parameter — larger values shrink the weights more, giving simpler, smoother models that generalize better to new data.
7:22So far, we've justified the squared error by saying it's easy to work with. But there's a deeper reason — a probabilistic one. Let's assume that the noise in our data follows a Gaussian, or normal, distribution with mean zero and variance sigma squared. This means that for a given input x, the probability of observing a particular target y follows a bell-shaped curve centered at w transpose x. The likelihood of all our data is the product of these probabilities across all data points. If we take the log and maximize it with respect to w, something beautiful happens. The log of the Gaussian simplifies to a sum of squared errors, plus some constant terms that don't depend on w. Maximizing the log-likelihood is exactly the same as minimizing the sum of squared errors. This is the maximum likelihood estimate — MLE — and it tells us that using squared error isn't just convenient, it's the statistically correct thing to do when the noise is Gaussian.
8:34Now let's derive the least squares solution step by step. Our objective is the sum of squared errors — SSE — which measures how far off our predictions are from the true targets. In matrix form, this is y minus X w, transpose, times y minus X w. Let's expand this. We get y transpose y, minus 2 w transpose X transpose y, plus w transpose X transpose X w. Now we take the derivative with respect to w. The first term, y transpose y, doesn't depend on w, so it vanishes. For the second term, using the linear rule, the derivative of minus 2 w transpose X transpose y is minus 2 X transpose y. For the third term, using the quadratic rule with A equals X transpose X, which is symmetric, the derivative is 2 X transpose X w. We set the whole gradient to zero, and after rearranging, we get the normal equations: X transpose X w equals X transpose y. Finally, if X transpose X is invertible, we multiply both sides by its inverse and get the closed-form solution: w star equals X transpose X inverse, times X transpose y. This is the ordinary least squares solution — one of the most important formulas in all of machine learning.