# Introduction to Regression

## Introduction

Regression is a method in machine learning that focuses on predicting numerical values based on input features. For example, consider a scenario where we predict the sales price of a house based on its features, such as the square footage, number of bedrooms, and location. We can train a regression model to learn the relationship between these variables by collecting data on past house sales, including the features and the corresponding sale prices. Once trained, the model can be used to estimate the price of a new house by providing its features as input.

Regression enables us to uncover patterns and make informed predictions in various domains, from predicting sales figures in business to estimating medical outcomes in healthcare.

## Definition

Regression is a supervised learning technique that aims to learn a function that maps input features to a continuous target variable. The goal is to find the best-fit line or curve that minimizes the difference between predicted and actual values.

The key components of a regression problem are:

- Input Features: The independent variables used to make predictions are denoted as $X$.
- Target Variable: The continuous dependent variable we aim to predict is $y$.
- Model: The mathematical function maps the input features to the target variable, denoted as $f(X)$.
- Parameters: The coefficients or weights that define the model's behavior learned during training.
- Loss Function: A measure of how well the model fits the training data, typically mean squared error (MSE) for regression.

Given a dataset $D = {(x_1, y_1), (x_2, y_2), ..., (x_n, y_n)}$ consisting of $n$ data points, where $x_i$ represents the inputs and $y_i$ represents the output, the goal of regression is to learn a function $f(X)$ that minimizes the loss function. The most common loss function for regression is the mean squared error (MSE):

$\text{MSE} = \frac{1}{n} \sum_{i=1}^{n} (y_i - f(x_i))^2$Where:

- $n$ is the number of data points,
- $y_i$ is the actual value of the target variable for the $i$-th data point,
- $f(x_i)$ is the predicted value of the target variable for the $i$-th data point.
- Mean Squared Error (MSE) is a commonly used performance metric in machine learning, particularly for regression problems. It measures the average squared difference between the predicted values and the actual values. MSE provides a way to quantify the amount of error or deviation in the predictions made by a model.

The goal is to find the function $f(X)$ that minimizes the MSE, making the predictions as close as possible to the actual values.

## Types of Regression

Some popular regression types are:

### Linear Regression

Linear regression is a simple and widely used method for predicting an output based on one or more input features. It assumes a linear relationship between the inputs and the output. In other words, Linear regression assumes the relationship between inputs and output is a straight line. The equation looks like this:

$y = \beta_0 + \beta_1x_1 + ... + \beta_nx_n$Where:

- $y$ is the output we want to predict,
- $x_1, ..., x_n$ are the inputs (information we have),
- $\beta_0, \beta_1, ..., \beta_n$ are numbers that the regression learns to make the line fit the data best.

**Example:** Now, let us consider an example of predicting a person's salary based on their years of experience and education level:

In this equation:

`Salary`

is the target variable (the salary we want to predict),`experience`

is the first input feature (the number of years of work experience),`education`

is the second input feature (the education level, which can be represented as a numeric value, e.g., 1 for high school, 2 for bachelor's degree, 3 for master's degree, etc.),- $\beta_0$ is the intercept (the predicted salary when a person has zero years of experience and no education),
- $\beta_1$ is the coefficient for experience (the change in salary for a one-year increase in experience, holding education level constant),
- $\beta_2$ is the coefficient for education (the change in salary for a one-level increase in education, holding years of experience constant).

Linear regression finds the best values for $\beta_0$, $\beta_1$, and $\beta_2$ that minimize the difference between the predicted and actual salaries in the training data.

### Polynomial Regression

Polynomial regression is an extension of linear regression that allows for modeling non-linear relationships between the input features and the target variable. It achieves this by including polynomial terms of the input features in the regression equation. In short, it uses the same idea as linear regression but includes squared, cubed, or higher powers of the inputs.

The general equation for polynomial regression with one input feature is:

$y = \beta_0 + \beta_1 x + \beta_2 x^2 + ... + \beta_n x^n$Where:

- $y$ is the target variable (the value we want to predict),
- $x$ is the input feature,
- $\beta_0$ is the intercept (the value of $y$ when $x$ is 0),
- $\beta_1, \beta_2, ..., \beta_n$ are the coefficients (the weights associated with each polynomial term),
- $n$ is the degree of the polynomial (a positive integer determining the highest power of $x$ in the equation).

**Example:** Let us consider an example of predicting a company's sales based on its advertising expenditure:

In this equation:

`sales`

is the target variable (the company's sales in thousands of dollars),`advertising`

is the input feature (the company's advertising expenditure in thousands of dollars),- $\beta_0$ is the intercept (the predicted sales when the advertising expenditure is 0),
- $\beta_1$ is the coefficient for the linear term (the change in sales for every additional dollar in advertising expenditure),
- $\beta_2$ is the coefficient for the quadratic term (the change in sales for a one-unit increase in the square of advertising expenditure). In other words, it captures the non-linear effect of every dollar spent on sales.

Polynomial regression finds the best values for $\beta_0$, $\beta_1$, and $\beta_2$ that minimize the difference between the predicted and actual sales in the training data.

## Applications

Regression has a wide range of applications across various domains:

**Dynamic Rates for Lending Pools**: Regression can assist in setting dynamic lending rates for isolated lending pools based on volatility, volume and LTV (Loan to value ratio) of the pool.**Medical Diagnosis**: Regression can assist in predicting patient outcomes, such as the likelihood of a disease or the expected recovery time, based on medical records, symptoms, and test results. These models supports healthcare professionals in making informed treatment decisions.**Stock Market Analysis**: Regression models can analyze the relationship between stock prices and various economic indicators, such as interest rates, GDP growth, and company financials. Such models aid in making investment decisions and risk assessments.

## Conclusion

Regression is a powerful method in an ML practitioner's toolkit to model quantifiable relations between various features. Choosing the appropriate regression algorithm depends on the nature of the problem, the assumptions about the data, and the desired interpretability of the model. Linear regression is simple and interpretable but assumes a linear relationship, while polynomial regression can capture non-linear patterns.