Linear regression is a technique used to find the relationship between variables. If we have 2 variables and their relationship is linear, a function in the form of $y=mx+q$ can be found in machine learning by \textit{training} a model. Let's say we have the values shown in Table \ref{table:linear_regr_1}.

\begin{table}[h!]
\centering
\begin{tabular}{| c | c | c |}
	\hline
	Pounds in 1000s & Miles per gallon\\
	\hline
	3.5 & 18\\
	\hline
	3.69 & 15\\
	\hline
	3.44 & 18\\
	\hline
	3.43 & 16\\
	\hline
	4.34 & 15\\
	\hline
	4.42 & 14\\
	\hline
	2.37 & 24\\
	\hline
\end{tabular}
\caption{Relationship between miles per gallon and weight of a car.}
\label{table:linear_regr_1}
\end{table}

If we plot this data using the pounds as $x$ and the miles per gallon as $y$, we would get something like Figure \ref{fig:linear_regr_1}

\begin{figure}[h]
    \centering
    \begin{tikzpicture}
    \begin{axis}[%
        x=5cm,
        y=0.25cm,
        scatter/classes={%
          a={mark=o,draw=black}}]

    % 1. The Scatter Plot
    \addplot[scatter,only marks,%
        scatter src=explicit symbolic]%
    table[meta=label] {
    x y label
    3.5 18 a
    3.69 15 a
    3.44 18 a
    3.43 16 a
    3.34 15 a
    3.42 14 a
    2.37 24 a
    };

    \end{axis}
    \end{tikzpicture}
    \caption{Scatter plot with previously defined data}
    \label{fig:linear_regr_1}
\end{figure}

\begin{figure}[h]
    \centering
    \begin{tikzpicture}
    \begin{axis}[%
        x=5cm,
        y=0.25cm,
        scatter/classes={%
          a={mark=o,draw=black}}]

    % 1. The Scatter Plot
    \addplot[scatter,only marks,%
        scatter src=explicit symbolic]%
    table[meta=label] {
    x y label
    3.5 18 a
    3.69 15 a
    3.44 18 a
    3.43 16 a
    3.34 15 a
    3.42 14 a
    2.37 24 a
    };

    \addplot [red, thick, domain=2:4] {-6.80*x + 39.66};

    \end{axis}
    \end{tikzpicture}
    \caption{Scatter plot with linear regression line}
    \label{fig:scatter_regression}
\end{figure}

Our goal is to find the line equation that given the pounds, returns the \textit{most probable} amount of miles per gallon. In this case, the line would look something like Figure \ref{fig:scatter_regression}.
\subsection{Training}
The process used to find the $m$ and $q$ parameters is called training. In machine learning $m$ is usually referred to as $w$ (weight) and $q$ is usually referred to as $b$ (bias) so, in the end, our linear function will be $y = wx+b$. To find these values we go through an iterative process that checks how "wrong" our results are and updates the weight and bias accordingly, until we notice that changing it more doesn't change the result in a significant way. When this happens, we say that the model \textit{converged}.
\subsection{Loss}
We said that we have to find how "wrong" we are with our predictions, the "wrongness" is called \textit{loss} in machine learning. There are different ways to calculate the loss. In linear regression we can fallback to 2 different ones: mean absolute error and mean squared error.
\begin{equation}
	\text{MAE} = \frac{1}{N}\sum_{i=1}^{N} \rvert y_i - y \rvert
\end{equation}
\begin{equation}
	\text{MSE}= \frac{1}{N}\sum_{i=1}^{N} (y_i - y)^2
\end{equation}
where $N$ is the amount of samples, $y_i$ is the actual value in our dataset and $y$ is the value we predicted.

\subsection{Gradient descent}
After we getting the loss, we have to find the direction in which our function should be updated. To do this, we calculate the slope of the loss by taking the partial derivative of the loss function for both the weight and the bias.\\
\begin{center}
	First, we put $u=(y_i-y)^2$
\end{center}
\begin{equation}
	\frac{\partial \text{MSE}}{\partial b} = \frac{\partial \text{MSE}}{\partial u} \frac{\partial u}{\partial y} \frac{\partial y}{\partial b}
\end{equation}
\begin{center}
	for the bias and
\end{center}
\begin{equation}
	\frac{\partial \text{MSE}}{\partial w} = \frac{\partial \text{MSE}}{\partial u} \frac{\partial u}{\partial y} \frac{\partial y}{\partial w}
\end{equation}
\begin{center}
	for the weight which evaluate to
\end{center}
\begin{equation}
	\frac{\partial \text{MSE}}{\partial b} = \frac{2}{N}\sum_{i=1}^{N}(y_i - y)
\end{equation}
\begin{center}
and
\end{center}
\begin{equation}
	\frac{\partial \text{MSE}}{\partial w} = \frac{2}{N}\sum_{i=1}^{N}(y_i - y) * x_i
\end{equation}
Now that we know the slope of the loss, we can update our parameters by multiplying the slope by a small number called \textit{learning rate} and then subtracting the result to the weight and the bias.
\begin{equation}
	w = w - \frac{\partial MSE}{\partial w} * \beta
\end{equation}

\begin{equation}
	b = b - \frac{\partial MSE}{\partial b} * \beta
\end{equation}
We then repeat this process until the model \textit{converges}.

\subsection{Python implementation}
\lstinputlisting[language=Octave]{linear_regression.py}
