Last updated on 5/27/20

## Assess Linearity

Now that you have a better understanding of what linear regression is, and how it applies to companies across industries and departments, let's dive into the first important concept behind it: linearity.

Linearity is, of course, at the heart of linear regression and its variants. It is a simple and elegant property widely used in forecastingneural networks, and many other complex machine learning algorithms. As you will see, it is a very robust element of data analysis.

You probably already have a good idea of what linearity is. When asking to define it, the answer usually involves the idea of a straight line that best fits the samples. For instance, plotting the weight versus the height of a sample of school children gives us the figure below on the left. The goal of linear interpolation is to find the straight line that is closest to all the points in the graph.

And to refresh your memory, the general equation of a line is:

$\\left[f\left(x\right) = ax + b\\right]$

$\\left(f\left(x\right) = ax + b\\right)$ is the classic equation for a line in the two-dimensional (2D) plane. $\\left(a\\right)$ is the slope, $\\left(b\\right)$ is the constant, and for different values of $\\left(a\\right)$ and $\\left(b\\right)$ we obtain different lines as illustrated by the figure below.

### The True Definition of Linearity - and a Catch

Let's go back to the mathematical definition of linearity of a function. Consider a function $\\left(f\left(x\right)\\right)$ where $\\left(x\\right)$ is a real number (also called a scalar). The exact mathematical definition of a linear function is the following:

$\\left(f\left(x\right)\\right)$ is a linear function if, and only if, it verifies these two conditions:

The first condition is called the multiplicative property:

•  $\\left(f\left(\lambda * x\right) = \lambda * f\left(x\right)\\right)$ for whatever number $\\left(\lambda\\right)$

The second condition is called the additive property:

•  $\\left(f\left(x+ y\right) = f\left(x\right) + f\left(y\right)\\right)$

That's it! It is quite a simple but powerful definition.

You can also verify that a line corresponds to a linear function. Let's start with the equation of a line that goes through the origin point (0,0):

$\\left[f\left(x\right) = a x\\right]$

When the slope $\\left(a \gt 0\\right)$ , the line goes up, and when $\\left(a \lt 0\\right)$ , the line goes down. And since $\\left(f\left(0\right) = 0\\right)$ , all the lines generated by   $\\left(f\left(x\right) = ax\\right)$ go through the origin point with coordinates $\\left(\left(0,0\right)\\right)$ .

You can verify the multiplicative property:

$\\left[f\left(\lambda x\right) = a \left(\lambda x\right) = \lambda \left( a x\right) = \lambda f\left(x\right)\\right]$

$\\left[f\left(x+y\right) = a\left(x + y\right) = ax + ay = f\left(x\right) + f\left(y\right)\\right]$

#### The Catch

What about lines that do not go through the point of origin? Lines whose equation is:

$\\left[f\left(x\right) = ax + b\\right]$

with $\\left(b \neq 0\\right)$

In that case, you can verify that the function is not linear, at least not according to the strictest definition of linearity. You have:

$\\left[f\left(\lambda x \right) = a \lambda x + b = \lambda \left(ax\right) + b= \lambda f\left(x\right) + \left(1 - \lambda\right) b \neq f\left(\lambda x \right)\\right]$

You can also verify that $\\left(f\left(x+y\right) \neq f\left(x\right) + f\left(y\right)\\right)$ as an exercise. :lol:

The presence of the constant $\\left(b\\right)$ breaks the two conditions required to make the function linear.

Linear functions that respect the additive and multiplicative properties are called homogeneous linear functions.

And now for some nonlinear functions!

### Nonlinear Functions

What is a nonlinear function?
Any function that does not rely only on multiplication and addition is nonlinear

Examples of nonlinear functions include:

•  $\\left(f\left(x\right) = x^2\\right)$

This function does not verify the additive or the multiplicative property. Let's verify the additive property: $\\left(f\left(x+y\right) = \left(x+y\right)^2 = x^2 + y^2 + 2xy \neq f\left(x\right) + f\left(y\right)\\right)$

•  $\\left(f\left(x\right) = log\left(x\right)\\right)$ is nonlinear since $\\left(log\left(x+y\right) \neq log\left(x\right) + log\left(y\right)\\right)$

• similarly $\\left(f\left(x\right) = sin\left(x\right)\\right)$ is also nonlinear for the same reason: $\\left(sin\left(x+y\right) \neq sin\left(x\right)+ sin\left(y\right)\\right)$

You get the drift.

The next function is also nonlinear, although it is linear on each segment defined by $\\left(x\\right)$ being negative or positive.

$\\left[f\left(x\right)=\max\left(0,x\right)\\right]$

The function spanning negative and positive values of x is nonlinear. This function is called a rectified linear unit (ReLU) is it an activation function widely used in deep learning to introduce nonlinearities in the neural network.

### Multi-Dimensions

So far, we have only worked with real numbers. The linearity property of a function can extend to vectors.

A vector of dimension N is simply a set of N numbers taken together as a single object.

In the 2D plane, a vector $\\left(\bf\left\{x\right\}\\right)$ has 2 numbers $\\left(\bf\left\{x\right\} = \left[x_0,x_1\right]\\right)$ . In a 3D volume, a vector $\\left(\bf\left\{x\right\}\\right)$ has 3 numbers $\\left(\bf\left\{x\right\} = \left[x_0,x_1,x_2\right]\\right)$ and so forth. Generalizing to N dimensions, a vector $\\left(\bf\left\{x\right\} \\right)$ has N numbers $\\left(\bf\left\{x\right\} = \left[x_0,x_1, ..., x_\left\{N-1\right\}\right]\\right)$

To multiply two vectors  $\\left(\bf\left\{a\right\} = \left[ a_\left\{0\right\}, a_\left\{1\right\}, \dots, a_\left\{N-1\right\} \right]\\right)$  with $\\left(\bf\left\{x\right\} = \left[ x_\left\{0\right\}, x_\left\{1\right\}, \dots, x_\left\{N-1\right\} \right]\\right)$ , simply write:

$\\left[\bf\left\{a\right\} \bf\left\{x\right\}^T = a_\left\{0\right\} x_\left\{0\right\} + ... + a_\left\{N-1\right\} x_\left\{N-1\right\}\\right]$

The function defined as  $\\left(f\left(\bf\left\{x\right\}\right) = \bf\left\{ax\right\}^T\\right)$  where $\\left(\bf\left\{a\right\}\\right)$ and $\\left(\bf\left\{x\right\}\\right)$ are both $\\left(N\\right)$ dimensional vectors is a linear function in the strictest sense of the definition.

### Application to Linear Regression

Consider the relations between $\\left(y\\right)$, our target variable, and two predictors, $\\left(x_0\\right)$ and $\\left(x_1\\right)$ .
In this example, $\\left(y\\right)$ is the weight of a middle school student, $\\left(x_0\\right)$ is the age, and $\\left(x_1\\right)$ the height. You can imagine that there's a linear relationship between the weight of the student and his/her age and height and write:

$\\left[weight = a_0 * age + a_1 * height\\right]$

Where $\\left(a_0, a_1\\right)$ are coefficients we would like to estimate.

You can rewrite the above equation as a vector equation:

$\\left[weight = \bf\left\{a\right\} \times \bf\left\{x\right\}^T\\right]$

With the vector $\\left(x = \left[age, height\right]\\right)$

Here, you have a linear function with two variables.

If there were more than two predictors of the student's weight but, instead $\\left(N\\right)$ predictors, the equation of the weight as a linear function of all the N predictors would still take the form  $\\left(weight = \bf\left\{a\right\} \times \bf\left\{x\right\}^T\\right)$ where $\\left(\bf\left\{a\right\}\\right)$ would now be a vector of $\\left(N\\right)$ coefficients and $\\left(\bf\left\{x\right\}\\right)$ a vector of $\\left(N\\right)$ predictors.

### Why is Linearity so Important?

What's interesting about linear function is that the linear feature is conserved by linear transformations:

• The sum of two linear functions is still a linear function.

• The multiplication of a linear function is also a linear function.

• The composition of two linear functions is a linear function.

The last point means that if two linear functions are defined by $\\left(f\left(x\right) = ax\\right)$ and $\\left(g\left(x\right) = cx\\right)$ , the composition of g and f : $\\left(g o f\left(x\right) = g\left(f\left(x\right)\right) = ac x\\right)$ is still linear! Linearity is very meta. :)

This means that if you apply linear regression to a dataset, you can transform your variables and still be in the same context of linear functions. Even when your dataset shows some strong nonlinearities.

#### Linear Modeling of Nonlinearities

The linear model can also extend to model nonlinearities in the data.

Let's say you want to include the height as a predictor of the school children's weight as well as the square of the height. Although the square of the height is not a linear function of the height, you can still build a linear model as such:

$\\left[weight = a_0 \times height + a_1 \times height^2\\right]$

This is a linear model with respect to two variables: $\\left(height\\right)$  and $\\left(height^2\\right)$ . We have simply created a new predictor $\\left(height^2 \\right)$ and used it in the linear model.

### Is My Data Linear?

How do you know if the relation between your variables is linear? The question is whether there is a linear relationship between one or several of your predictors and the target variable.

One way to check the linearity is to plot the target versus the predictors for each of the predictors in the dataset.

If the plot shows a distinct trend, you can conclude that there is some amount of linearity between the two variables. When the plot shows a different pattern, the relation is not linear.

The following plot shows the relation between the weight and the height in school children. The straight line is the linear interpolation of the data.

As expected, the weight increases with the height. Although not perfect, the relationship between the two variables shows some degree of linearity.

If you look at the height versus the age, the linearity of the relationship is less obvious as shown by the following figure:

The auto-mpg dataset is another dataset we're going to work with throughout the course. The original dataset comes from UCI Machine Learning repository. The auto-mpg dataset lists the fuel consumption in miles per gallon for over 300 cars from the 80s. Each sample has information on acceleration, displacement, weight, and some categorical variables. We'll come back to that dataset later in the course.

For now, let's look at the variables weight and acceleration with respect to the fuel efficiency/ miles per gallon (mpg) target variable.

The plot below on the left shows the mpg versus weight and on the right the mpg versus acceleration. As expected, the heavier the car, the worse its fuel consumption (lower number of miles per gallon).

• The plot on the left shows a distinct decreasing relation between mpg and weight. Some degree of linearity exists between the two variables.

• The plot on the right is hardly interpretable as a linear relation. The points form a sort of central blob, and it would be unwise to infer anything from it.

Looking at plots to assess the linearity of the relation between two variables is fine, but it does not give you enough information on the degree of that linearity. How close is the relation to a single straight line?

At this point several important questions are left unanswered:

• How can you effectively measure the degree of linearity?

• How can you be certain that a linear model properly captures the information contained in the dataset?

To illustrate that last point, let's look at the Height vs. Age plot above. If you trace a linear interpolation and a quadratic one (the curved line) on the same graph, how do you know which interpolation is better? Furthermore, the point on the right seems to have a big impact on the quadratic line, but by how much? What would happen if we removed it?

### Summary

Linearity is central to this course as well as a wide range of techniques with many diverse application domains, from forecasting and time series to neural networkssignal processing, and modeling of natural phenomena.

In this chapter, we covered the following key elements:

• A strict and mathematical definition of linearity based on additive and multiplicative properties.

• That linear functions deal with scalars as well as vectors with the same simple expression  $\\left(f\left(x\right) = \bf\left\{ax\right\}^T\\right)$

• That assessing the linearity between two variables can be done visually, but the conclusion is not always obvious or unique.

Looking back at the plots in this chapter, two types of relations between variables emerge:

• One where the plot shows a swarm of points, with no distinguishable patterns; a blob of point. This was the case for the Mpg vs. Acceleration plot.

• Other plots where some patterns can be distinctively observed, such as the Weight vs. Height in schoolchildren plot.