# Probability Distributions

The distributions available for use in PoPy models are shown in Table 60:-

Table 60 Probability Distributions
Name | Syntax | Type
--- | --- | ---
Uniform | x ~ unif(min_x, max_x) init_x | continuous univariate
Normal | y ~ norm(mean, var) | continuous univariate
Multi Normal | y_vec ~ mnorm(mean_vec, var_mat) | continuous multivariate
Bernoulli | y ~ bernoulli(p) | discrete univariate
Poisson | y ~ poisson(p) | discrete univariate
Negative Binomial | y ~ negbinomial(p, r) | discrete univariate

## Uniform Distribution

Uniform is a continuous univariate distribution, written as:-

    x ~ unif(min_x, max_x) init_x

The uniform distribution is used to define a range of values for an unknown scalar that you wish PoPy to estimate.

The input parameters are:-

- min_x - the minimum value that variable ‘x’ is allowed to take during estimation.
- max_x - the maximum value that variable ‘x’ is allowed to take during estimation.
- init_x - the initial value that variable ‘x’ takes at the start of estimation.

The output ‘x’ and inputs ‘min_x’, ‘max_x’, ‘init_x’ are all continuous values.

For more information see Uniform Distribution on Wikipedia.

### Uniform Distribution Examples

You use the Uniform Distribution in the LEVEL_PARAMS section of a PoPy Fit Script as follows:-

    f[KE] ~ unif(0.001, 100) 0.05

The above expressions limits the `f[KE]` variable to the range [0.001, 100] with an initial starting value of 0.05.

Alternatively you can do:-

    f[KE] ~ unif(0.001, +inf) 0.05

Which limits `f[KE]` to be greater than 0.001. Note the an equivalent shortcut is available as follows:-

    f[KE] ~ P 0.05

Where ‘P’ stands for +ve. You can also have an unconstrained variable as follows:-

    f[KE] ~ U 0.05

Where ‘U’ stands for unlimited. The equivalent long form is:-

    f[KE] ~ unif(-inf, +inf) 0.05

## Normal Distribution

The Normal distribution is used for continuous variables and written in PoPy as:-

    x ~ norm(mean, var)

The Normal models a Gaussian distribution with two parameters ‘mean’ and ‘var’.

The input parameters are:-

- mean - the expected value of the Normal
- var - the variance of the Normal

The output ‘x’ and inputs ‘mean’, ‘var’ are all continuous values

For more information see Normal Distribution on Wikipedia.

### Normal Random Effect Example

You can use the Normal Distribution in the LEVEL_PARAMS section of a PoPy script, to define a `r[X]` random effect variable as follows:-

    LEVEL_PARAMS:
        INDIV:
            params: |
                r[KE] ~ norm(0, f[KE_isv])

Here the `r[KE]` scalar variable is defined as a normal with mean zero and positive scalar variance `f[KE_isv]`.

`r[KE]` is defined at the ‘INDIV’ level, so each individual in the population has an independent sample of this normal distribution.

### Normal Likelihood Example

You can use the Normal Distribution in the PREDICTIONS section of a PoPy Fit Script as follows:-

    PREDICTIONS:
        p[DV_CENTRAL] = s[CENTRAL]/m[V1]
        var = m[ANOISE]**2 + m[PNOISE]**2 * p[DV_CENTRAL]**2
        c[DV_CENTRAL] ~ norm(p[DV_CENTRAL], var)

The above syntax in a Fit Script specifies the likelihood of the observed `c[DV_CENTRAL]` observation from the data file, when modelled as a Normal variable, with mean p[DV_CENTRAL] and variance ‘var’.

## Multivariate Normal Distribution

Multivariate-Normal distribution is used for vectors of continuous variables and written like this:-

    output_vector ~ mnorm(mean_vector, covariance_matrix)

The Multivariate Normal is a generalisation of the Normal Distribution with two parameters ‘mean_vector’ and ‘covariance_matrix’, as follows:-

- mean_vector - the mean of the ‘output_vector’
- covariance_matrix - the covariance of the ‘output_vector’ elements

The ‘output_vector’ must have the same number of dimensions as the ‘mean_vector’. Also the ‘covariance_matrix’ needs to be symmetric positive definite with a matching dimensionality. See Matrices for examples of how to define the covariance matrix.

For more information see Multivariate Normal Distribution on Wikipedia.

### Multivariate Normal Random Effect Example

You can use the Multivariate Normal Distribution in the LEVEL_PARAMS section of a PoPy script, to define a vector of `r[X]` random effects variables as follows:-

    LEVEL_PARAMS:
        INDIV:
            params: |
                r[KA,CL,V] ~ mnorm([0, 0, 0], f[KA_isv,CL_isv,V_isv])

Here the `r[KA,CL,V]` variable is defined as a 3 element vector with mean zero. `[0,0,0]` is a 3 element ‘mean_vector’ and `f[KA_isv,CL_isv,V_isv]` is a 3x3 ‘covariance_matrix’. The `f[KA_isv,CL_isv,V_isv]` matrix can be a diagonal or square symmetric matrix, see Matrices.

The `r[KA,CL,V]` is defined at the ‘INDIV’ level, so each individual in the population has an independent sample of this multivariate normal distribution.

## Bernoulli Distribution

The Bernoulli is univariate discrete distribution used to model binary variables, and written in PoPy as:-

    y ~ bernoulli(prob_success)

The Bernoulli models the distribution of a single Bernoulli trial.

The input parameters are:-

- prob_success - probability of success of the bernouilli trial

The output ‘y’ is a binary value, i.e. either 1 for success or 0 for failure. ‘prob_success’ is a real valued number in the range [0,1].

For more information see Bernoulli Distribution on Wikipedia.

### Bernoulli Likelihood Example

You can use the Bernoulli Distribution in the PREDICTIONS section of a PoPy Fit Script as follows:-

    PREDICTIONS:
        conc = s[X]/m[V]
        p[DV_BERN] = 1.0 / (1.0+ exp(-conc))
        c[DV_BERN] ~ bernoulli(p[DV_BERN])

The above syntax in a Fit Script specifies the likelihood of the observed `c[DV_BERN]` binary observation from the data file, when modelled as a Bernoulli variable, with success rate dependent on ‘conc’ via a logistic transform.

## Poisson Distribution

The Poisson is a discrete univariate distribution, to model discrete count variables, written in PoPy as:-

    y ~ poisson(lambda)

The Poisson models the distribution of the number of events occurring within a fixed time interval, if each individual event occurs independently and at constant rate ‘lambda’.

The input parameters are:-

- lambda - the expected number of occurrences within the time interval

The output ‘y’ is the observed count, i.e. a non-negative integer value. ‘lambda’ is a positive real valued number, which represents the mean rate of event occurrence.

For more information see Poisson Distribution on Wikipedia.

### Poisson Likelihood Example

You can use the Poisson Distribution in the PREDICTIONS section of a PoPy Fit Script as follows:-

    PREDICTIONS:
        c[COUNT] ~ poisson(m[LAMBDA])

The above syntax in a Fit Script specifies the likelihood of the observed `c[COUNT]` count observations from the data file, when modelled as a Poisson process with estimated rate parameter `m[LAMBDA]`.

## Negative Binomial Distribution

The negative binomial is a univarite discrete distribution, written in PoPy as:-

    num_succeses ~ negbinomial(prob_success, num_of_fails)

The negative binomial models the distribution of the number of successes for a series of independent Bernoulli trials until the failure count reaches ‘num_of_fails’.

The input parameters are:-

- prob_success - probability of success of each bernouilli trial
- num_of_fails - number of unsuccessful bernouilli trials before num_successes output

Here the output ‘num_successes’ is an integer. ‘num_of_fails’ is also an integer and ‘prob_success’ is a real valued number in the range [0,1].

For more information see Negative Binomial Distribution on Wikipedia.

### Negative Binomial Likelihood Example

You can use the Negative Binomial Distribution in PREDICTIONS section of a PoPy Fit Script as follows:-

    PREDICTIONS:
        conc = s[X]/m[V]
        p[DV_NB] = 1.0 / (1.0 + exp(-conc))
        c[DV_NB] ~ negbinomial(p[DV_NB], 1)

The above syntax in a Fit Script specifies the likelihood of the observed `c[DV_NB]` count data from the data file when modelled as the number of successes of a Bernoulli variable (with success rate dependent on ‘conc’ via a logistic transform) until occurrence of first failure.