# What is a generalized linear model?

To understand what a generalized linear model does, let’s look back at linear models.

## Typical linear model setup

In the typical setup for a linear model, we have a random input vector $\rve{X}$ and a random output variable $\rva{Y}$ whose mean $\mu = \expectation\brak{\rva{Y}}$ is a linear function of $\rve{X}$.

For instance, a linear least squares regression amounts to:

Similarly, a linear regression with MAE loss amounts to:

Hence, with tradidional linear models, we attempt to predict the mean $\mu$ of $\rva{Y}$. When this mean has a non-linear dependence with $\rve{X}$, we can try to predict another parameter $\eta$ instead and then use a non linear function to tranform this parameter into the mean.

## Exponential family

A family of distribution for which this approach works particularly well is the exponential family. Which is good news because the most familiar distributions are part of this family (normal, exponential, Bernoulli, Poisson, geometric, etc.)

A density belongs to the exponential family if it can be writen as:

The parameter $\vec{\eta}$ is called the natural parameter of the density and $A(\vec{\eta})$ is named the cumulant.

This family of distribution is even more interesting since $\Phi(y)$ is a sufficient statistic!

Generalized linear models’ aim is to estimate the natural parameter $\vec{\eta}$ on the basis of the dataset. Before we dive into more details, let’s convince ourselves that exponential families are THE thing.

Estimating $\vec{\eta}$ using the sufficient statistic $\Phi(y)$, is made possible through the link function $g$ such that:

for $\mu = \expectation\brak{\Phi(y)}$.

This link function always exists because $\Phi(y)$ is a sufficient statistic.

## Usual distributions are members of the exponential family

The $Bernoulli(\mu)$ distribution is a member of the exponential family with parameters:

A generalized linear model for this distribution is called a logistic regression.

The $Poisson(\mu)$ distribution is a member of the exponential family with parameters:

The normal distribution $\gaussian(\mu, \sigma^2)$ is a member of the exponential family with parameters:

## Generalized linear models

### Setup

Let $\rve{X}$ a random vector and $\rva{Y}$ a random variable. Assume our dataset $S$ is made of $\ndataset$ i.i.d. samples from $(\rve{X}, \rva{Y})$:

We suppose the distribution of $\rva{Y}$ given $\rve{X}$ is member of the exponential family:

Where the natural parameter $\eta$ linearly depends on $\inputvec$:

### Loss function

We will used a maximum likelihood estimation method for $\eta$. Our goal is thus to maximize the likelihood:

Which amounts to maximizing the log-likelihood. In other words, our loss function is:

This loss function is convex.

Let’s note $\inputmatrix \in \realset^{\ndataset\times \inputdim}$ the matrix whose $\idataset$-th row is the vector $\ninputvec{\idataset}$:

And this loss function can be minimized using gradient descent to find $\bestmodel$.