# MVgauss: Multivariate Gaussian densities In RTMB: 'R' Bindings for 'TMB'

 MVgauss R Documentation

## Multivariate Gaussian densities

### Description

Multivariate Gaussian densities

### Usage

``````dmvnorm(x, mu = 0, Sigma, log = FALSE, scale = 1)

dgmrf(x, mu = 0, Q, log = FALSE, scale = 1)

dautoreg(x, mu = 0, phi, log = FALSE, scale = 1)

dseparable(...)

unstructured(k)
``````

### Arguments

 `x` Density evaluation point `mu` Mean parameter vector `Sigma` Covariance matrix `log` Logical; Return log density? `scale` Extra scale parameter - see section 'Scaling'. `Q` Sparse precision matrix `phi` Autoregressive parameters `...` Log densities `k` Dimension

### Details

Multivariate normal density evaluation is done using `dmvnorm()`. This is meant for dense covariance matrices. If many evaluations are needed for the same covariance matrix please note that you can pass matrix arguments: When `x` is a matrix the density is applied to each row of `x` and the return value will be a vector (length = `nrow(x)`) of densities.

The function `dgmrf()` is essentially identical to `dmvnorm()` with the only difference that `dgmrf()` is specified via the precision matrix (inverse covariance) assuming that this matrix is sparse.

Autoregressive density evaluation is implemented for all orders via `dautoreg()` (including the simplest AR1). We note that this variant is for a stationary, mean zero and variance one process. FIXME: Provide parameterization via partial correlations.

Separable extension can be constructed for an unlimited number of inputs. Each input must be a function returning a gaussian mean zero log density. The output of `dseparable` is another log density which can be evaluated for array arguments. For example `dseparable(f1,f2,f3)` takes as input a 3D array `x`. `f1` acts in 1st array dimension of `x`, `f2` in 2nd dimension and so on. In addition to `x`, parameters `mu` and `scale` can be supplied - see below.

### Value

Vector of densities.

### Functions

• `dmvnorm()`: Multivariate normal distribution. OSA-residuals can be used for argument `x`.

• `dgmrf()`: Multivariate normal distribution. OSA is not implemented.

• `dautoreg()`: Gaussian stationary mean zero AR(k) density

• `dseparable()`: Separable extension of Gaussian log-densities

• `unstructured()`: Helper to generate an unstructured correlation matrix to use with `dmvnorm`

### Scaling

All the densities accept a `scale` argument which replaces `SCALE` and `VECSCALE` functionality of TMB. Scaling is applied elementwise on the residual `x-mu`. This works as expected when `scale` is a scalar or a vector object of the same length as `x`. In addition, `dmvnorm` and `dgmrf` can be scaled by a vector of length equal to the covariance/precision dimension. In this case the `scale` parameter is recycled by row to meet the special row-wise vectorization of these densities.

### Unstructured correlation

Replacement of `UNSTRUCTURED_CORR` functionality of TMB. Constuct object using `us <- unstructured(k)`. Now `us` has two methods: `x <- us\$parms()` gives the parameter vector used as input to the objective function, and `us\$corr(x)` turns the parameter vector into an unstructured correlation matrix.

### Examples

``````func <- function(x, sd, parm, phi) {
## IID N(0, sd^2)
f1 <- function(x)sum(dnorm(x, sd=sd, log=TRUE))
Sigma <- diag(2) + parm
## MVNORM(0, Sigma)
f2 <- function(x)dmvnorm(x, Sigma=Sigma, log=TRUE)
## AR(2) process
f3 <- function(x)dautoreg(x, phi=phi, log=TRUE)
## Separable extension (implicit log=TRUE)
-dseparable(f1, f2, f3)(x)
}
parameters <- list(x = array(0, c(10, 2, 10)), sd=2, parm=1, phi=c(.9, -.2))
obj <- MakeADFun(function(p)do.call(func, p), parameters, random="x")
## Check that density integrates to 1
obj\$fn()
## Check that integral is independent of the outer parameters
obj\$gr()
## Check that we can simulate from this density
s <- obj\$simulate()
``````

RTMB documentation built on May 29, 2024, 8:45 a.m.