# Implementation¶

## ExpFam¶

struct ExpFam

Exponential family of distributions.

f(y; θ, 𝜙) = exp{(yθ - b(θ))/a(𝜙) + c(y,𝜙)},


for which::

y     : random variable value.
θ     : canonical parameter.
𝜙     : nuisance parameter.
a(𝜙)  :
b(θ)  : log-partition function.
c(y,𝜙): normaliser.


The mean and variance are given by::

E[y]   = b'(θ)
Var[y] = b''(θ)a(𝜙)


In order to define a generalised linear mixed model (GLMM) we use the so-called natural parameter η. Given a link function g(.), the natural parameter relates to the canonical parameter as follows::

η = g(E[y]) = g(b'(θ)).


Every member of the exponential family has a canonical link function, which greatly simplifies the relationship::

η = θ


Public Members

double y

Random variable value

double a

a(𝜙)

double loga

log(a(𝜙))

double c

c(y,𝜙)

log_partition *lp

b(θ)

log_partition_fderivative *lpfd

log(b'(θ))

log_partition_derivatives *lpd

log(b''(θ))

## Likelihood¶

We assume the canonical link function for every likelihood.

### Bernoulli¶

y assumes 1 or 0 for failure. We make use of the Binomial implementation. So, please, refer to the next section for details.

static double bernoulli_log_partition(const double theta)

Bernoulli log-partition function.

Please, refer to the binomial_log_partition() function.

static double bernoulli_log_partition_fderivative(const double theta)

First derivative of the Bernoulli log-partition function.

Please, refer to the binomial_log_partition_fderivative() function.

static void bernoulli_log_partition_derivatives(const double theta, double *b0, double *logb1, double *logb2)

Zeroth, first, and second derivatives of the Bernoulli log-partition function.

Please, refer to the bernoulli_log_partition_fderivative() function.

### Binomial¶

The random variable is given by y = k/n. The support is therefore y ϵ {0/n, 1/n, ..., r/n}. The exponential family functions are:

𝜙      = n
a(𝜙)   = 1/𝜙
b(θ)   = log(1 + exp(θ))
c(y,𝜙) = log(binom(n, y𝜙))


Let us define:

𝜇 = E[y] = p.


The canonical link function and its inverse are given by:

canonical(𝜇)     = log(𝜇/(1+𝜇)) = η
canonical_inv(η) = 1/(1 + exp(-η))

double binomial_log_partition(const double theta)

Binomial log-partition function.

Definition:

b(𝜃) = log(1 + exp(𝜃)).


double binomial_log_partition_fderivative(const double theta)

First derivative of the Binomial log-partition function.

Definition:

log(b'(𝜃)) = 𝜃 - log(1 + exp(𝜃))


void binomial_log_partition_derivatives(const double theta, double *b0, double *logb1, double *logb2)

Zeroth, first, and second derivatives of the Binomial log-partition function.

Implements b(𝜃), log(b'(𝜃)), and:

log(b''(𝜃)) = 𝜃 - 2log(1 + exp(𝜃))


### Negative Binomial¶

The random variable is given by y = k/r. The support is therefore y ϵ {0/r, 1/r, ..., r/r}. The exponential family functions are:

𝜙 = r
a(𝜙) = 1/𝜙
b(θ) = -log(1 - exp(θ))
c(y,𝜙) = log(binom(y𝜙 + 𝜙 - 1, y𝜙))


Let us define:

𝜇 = E[y] = p / (1 - p)


The canonical link function and its inverse are given by:

canonical(𝜇)     = log(𝜇 / (1 + 𝜇)) = η
canonical_inv(η) = exp(η) / (1 - exp(η))

double nbinomial_log_partition(const double theta)

Negative binomial log-partition function.

Definition:

b(𝜃) = -log(1 - exp(𝜃)).


double nbinomial_log_partition_fderivative(const double theta)

First derivative of the Negative Binomial log-partition function.

Definition:

log(b'(𝜃)) = 𝜃 - log(1 - exp(𝜃)).


void nbinomial_log_partition_derivatives(const double theta, double *b0, double *logb1, double *logb2)

Zeroth, first, and second derivatives of the Negative Binomial log-partition func.

Implements b(𝜃), log(b'(𝜃)), and:

log(b''(𝜃)) = 𝜃 - 2log(1 - exp(𝜃))


### Poisson¶

The support is y ϵ {0, 1, ...}. The exponential family functions are:

𝜙      = 1
a(𝜙)   = 𝜙
b(𝜃)   = exp(𝜃)
b'(𝜃)  = exp(𝜃)
b'(𝜃)  = exp(𝜃)
c(y,𝜙) = -log(y!)


Let us define:

𝜇 = E[y] = λ,


for which λ is the Poisson distribution parameter. The canonical link function and its inverse are given by:

canonical(𝜇)     = log(𝜇 / (1 + 𝜇)) = η
canonical_inv(η) = exp(η) / (1 - exp(η))

double poisson_log_partition(const double theta)

Poisson log-partition function.

Definition:

b(𝜃) = exp(𝜃)


double poisson_log_partition_fderivative(const double theta)

Log of the first derivative of the Poisson log-partition function.

Definition:

log(b'(𝜃)) = 𝜃


void poisson_log_partition_derivatives(const double theta, double *b0, double *logb1, double *logb2)

Log of the derivatives of the Poisson log-partition function.

Implements b(𝜃), log(b'(𝜃)), and:

log(b''(𝜃)) = 𝜃