Before starting this series of articles on Machine Learning, I thought it might be a good idea to go through some Statistical recalls. This first article is an introduction to some more detailed articles on statistics. I will be illustrating some concepts using Python codes.

This is the part 2/2 of our series on Linear Regression.

# Random Design Matrix

So far, a hidden hypothesis was set without being explicitly defined: should be deterministic. Indeed, we should be able to have full control over how is measured. This is what we call a fixed design matrix. Let me quickly introduce an extension of what has been covered so far.

## Random Design

In real-life situations, is rarely fixed. Indeed, many external components might influence the measures you are currently making. For example, some geological data collected over time could depend on the temperature, the atmospheric pressure or other external factors… And it might not be taken into account.

For this reason, we introduce the random design matrices where : is a random variable. The hypothesis on slightly change :

## Consequences

The minimization problem remains the same as previously : .

The difference relies in the associated theoretical results. Recall the Gram matrix that we previously defined : . It should now be written as follows : .

The limit results become : tends as n goes to infinity to

Notice that it is now a limit result.

## What changes?

What should be remembered from this small article? Well, the direct consequence we face in this case is a change in the confidence interval as the limit distribution is not the same as previously.

Previously, we had :

, being the quantile of the student distribution

This result is constrained to the gaussian hypothesis, which is quite restrictive.

Now that we introduced the random design matrix, the confidence interval becomes :

, being the quantile of the Normal distribution.

# Normal Regression Model

## Concept

Recall that in a multi-dimensional linear regression, we have :

And the following conditions on :

- , i.e a white noise condition
- for all i = 1,…,n, i.e a homoskedasticity condition

Notice also that we never specified any distribution for . This is where the Normal law comes in. This time, we make the hypothesis that : .

Independence implies that :

Joint normality is not required in the normal regression model. We simply want to have that the conditional distribution of given is normal.

## Maximum Likelihood Estimation (MLE)

First, we should observe that the previous condition on the conditional distribution of can be translated into :

Under the assumption that the observations are independent, the conditional density becomes :

is called the likelihood function. Our aim is to find the Maximum Likelihood Estimation (MLE), i.e the values of such that the likelihood function is maximal. A natural interpretation is to identify the values of that are the most likely. We can sum up the maximization problem as follows :

You might have noticed that it is not always simple to work with products in a likelihood function. Therefore, we introduce the log-likelihood function :

The maximization problem can be re-expressed as :

## First Order conditions

The MLE is usually identified numerically. In our case, we can explore it algebraically, by identifying that jointly solve the First Order Conditions :

It can be pretty easily shown that the MLE will give us the same results as the OLS procedure. Indeed,

And :

The last step is to plug-in the estimators in the initial problem :

# Pseudo Least Squares

Do you remember when we defined the Gram matrix as ? To define the OLS estimator, we defined the Gram matrix as invertible. In other words, . But what happens when it is not the case?

## When does it happen?

It might happen that the Gram matrix is not invertible. But what would that mean? And how does it happen?

- it arises from a non-unique OLS solution
- it typically is the case when the dimension of our design matrix is larger than the number of observations itself, e.g collecting a lot of data per patient in a small medical study
- it implies that the pseudo-inverse should be used instead of the inverse of the Gram matrix

## Singular Value Decomposition

If we cannot invert the Gram matrix, we are stuck in the algebraic derivation of the OLS model at the following expression :

We do not have a unique solution, but a whole set of solutions defined by : .

In order to compute the inverse of , we need to use the Singular Value Decomposition (SVD). The SVD is a matrix decomposition theorem that states the following :

Any matrix can be de decomposed as Where :

- is a matrix, and , contains left singular vectors
- is a matrix that contains the eigen values of X on the diagonal, and 0s elsewhere.
- is a matrix, and , contains right singular vectors

We can express as :

The whole point of performing an SVD is to define : . is called the pseudo-inverse of a matrix. This pseudo-inverse will allow us to compute the pseudo inverse of the Gram matrix :

The estimator has now a different form. This means that we can compute again the bias and the variance of this new estimator.

The bias becomes : where . The estimator will be biaised, except if which means .

The variance becomes :

# Transformations

Let’s get back to the simple problem in which we wanted to assess the demand for ice cream depending on the outside temperature. The model we built looked like this :

where :

- is the icecream demand on a given day
- a constant parameter
- the parameter that assesses the impact of temperature on icecream demand
- the average temparature for a given day
- the residuals

However, the assumption that the model is purely linear is strong, that is pretty much never met in practice. We can apply some transformations to the model to make it more flexible.

## 1. Log

This also means that 1% change of : is equal to .

## 2. Log-Log

## 3. Square Root

## 4. Quadratic

This implies that :

## 5. Non-linear

This is a simple example of a non-linear transform.

# Boolean and Categorical Variables

Up to now, we mostly considered cases in which the variables were continuous (temperature for example). But the variety of data you might deal with might include categorical or boolean variables!

## I. Binary Variables

Let’s get back to our icecream demand forecasting problem :

where :

- is the icecream demand on a given day
- a constant parameter
- the parameter that assesses the impact of temperature on icecream demand
- the average temparature for a given day
- the residuals

A binary variable that might be interesting to predict icecream’s consumption is the fact that there is public holiday on the day considered or not :

where :

- is there is public holiday this day
- otherwise

Therefore, we can see as :

Public holiday has an impact on icecream demand if is significantly different from 0. This can be seen as a classical hypothesis testing :

- Null hypothesis
- and

## II. Categorical Variables

We might also have more than 2 categories. For example, regarding our public holiday variable, we’ll now be interested in which state/region of France is on a public holiday (there are 3 overall, to avoid have everybody sharing the same week of holidays).

where :

- is zone A is on holiday
- is zone B is on holiday
- is zone C is on holiday

The reference case we implicitly consider here is the case in which there is no public holiday. The three terms are here to evaluate a difference with the reference case.

We can run individual or joint tests on the different coefficients of the regression.

## III. Interaction variable

The intuition behind the interaction variable is quite simple. Suppose we want to test the effect of temperature on consumption if zone A is on holiday.

The problem can be formulated as follows :

Here, measures the effect of temperature on icecream consumption when zone A is on holiday.

We can run student tests on directly.

We can now enrich our model and add several interaction terms :

If we want to test the difference between the groups, we should make a Fisher test with a null hypothesis .

# Dealing with heteroscedasticity

Heteroscedasticity might be an issue when conducting hypothesis tests. How can we define heteroscedasticity? How can we detect it? How can we overcome this issue?

## I. What is heteroscedasticity?

Heteroscedasticity (or heteroskedasticity) refers to the case in which the variability is unequal across the range of values.

Recall that in the linear regression framework : . The fundamental hypothesis is that : . Under this hypothesis, the OLS estimator is the Best Linear Unbiaised Estimator (BLUE).

By normality hypothesis, under homoscedasticity, and .

For example, if we try to predict the income in terms of the age of a person :

- in case of homoscedasticity, the variance is constant over the age of the person, i.e
- in case of heteroscedasticity, the variance is increasing over the age of the person

Under heteroscedasticity, .

## II. Transform the variables in `log`

In most cases, to control for heteroscedasticity, there is an easy trick: transform the variables in log.

Most of the time, this will do the trick!

For example, in Matlab :

```
y = log(hprice1(:,1));
[n,k] = size(hprice1);
X = [ones(n,1), hprice1(:,[3,4,5])]
beta = inv(X'*X)*X'*y
```

## III. Robust inference to heteroscedasticity

### Concept

*Idea *: Build a standard error robust to any kind of heteroscedasticity.

The estimators take the following form :

This estimator is robust to any kind of heteroscedasticity. The variance of the estimator is defined by :

This requires to be known and too. We can define the residual of the estimation. Using White’s formula:

is robust to any kind of heteroscedasticity.

Therefore, if is the residual of the regression of on the other independant variables, we have :

It is sufficient to modify the reference standard error during further tests.

### Detect heteroscedasticity

How can we detect heteroscedasticity ? A simple test hypothesis can be used :

## IV. Linear form residuals

The residuals in case of heteroscedasticity might depend on :

- time, with an index , as we have seen up to now
- other features

In the second case, the residuals should have the following form :

We can test the hypothesis

## V. Heteroscedasticity with a constant shift

This time, the hypothesis is the following :

Therefore, we might need to modify the residuals :

Now, let’s use the transformed model to fit our regression :

We obtain a weighted least squares problem :

In practice, it is hard to find those weights. For this reason, we usually apply what we call generalized least squares (GLS).

# Generalized Least Squares

In the previous sections, we highlighted the need for models and estimators that handle heteroscedasticity. We’ll introduce Generalized Least Squares, a more general framework.

Recall that in the OLS framework, the Best Linear Unbiaised Estimator was defined by :

We have seen previously that under heteroscedasticity, this estimator was no longer the best. It remained, however, a Linear Unbiased Estimator.

Suppose that we know the covariance of the errors such that it can be split the following way by Cholesky decomposition: , a triangular matrix.

In this case, we can build the GLS estimator by scaling the initial regression problem :

We can define new shifted variables :

The resulting estimator is therefore modified too :

The variance of the estimator becomes :

## 1. Weighted Least Squares

The Weighted Least Squares is a special case of the GLS framework.

Under heteroscedasticity, we might face :

In such case, we can specify , which has diagnoal elements of the type

If we replace the next value of in the GLS estimator :

The estimator is consistent whatever the weights being used.

## 2. Feasible Least Squares

Usually, we don’t know the covariance of the errors, but it usually depends on some parameters that can be estimated. This is the main idea behind Feasible Least Squares.

We can use this estimator in the estimator’s formula :

The Github repository of this article can be found here.

Like it? Buy me a coffee

## Leave a comment