Multivariate Normal Variance Mixtures

Erik Hintz, Marius Hofert and Christiane Lemieux

2019-11-13

library(nvmix)
library(RColorBrewer)
library(lattice)
doPDF <- FALSE

1 Introduction

The R package nvmix provides functionality for (multivariate) normal variance mixture distributions, including normal and Student’s t distributions; see also Hintz et al. (2019, “Normal variance mixtures: Distribution, density and parameter estimation”). A random vector \(\mathbf{X}=(X_1,\dots,X_d)\) follows a normal variance mixture, in notation \(\mathbf{X}\sim NVM_d(\mathbf{\mu},\Sigma,F_W)\), if, in distribution, \[ \mathbf{X}=\mathbf{\mu}+\sqrt{W}A\mathbf{Z}, \] where \(\mathbf{\mu}\in\mathbb{R}^d\) denotes the location (vector), \(\Sigma=AA^\top\) for \(A\in\mathbb{R}^{d\times k}\) denotes the scale (matrix) (a covariance matrix), and the mixture variable \(W\sim F_W\) is a non-negative random variable independent of \(\mathbf{Z}\sim N_k(\mathbf{0},I_k)\) (where \(I_k\in\mathbb{R}^{k\times k}\) denotes the identity matrix). Note that both the Student’s \(t\) distribution with degrees of freedom parameter \(\nu>0\) and the normal distribution are normal variance mixtures; in the former case, \(W\sim IG(\nu/2, \nu/2)\) (inverse gamma) and in the latter case \(W\) is almost surely constant (taken as \(1\) so that \(\Sigma\) is the covariance matrix of \(\mathbf{X}\) in this case).

Note that the density of \(\mathbf{X}\) exists if and only if \(\Sigma\) is positive definite and \(P(W=0)=0\). In this case one can take \(A\in\mathbb{R}^{d\times d}\) to be the (lower triangular) Cholesky factor \(A\) of \(\Sigma\) such that \(AA^\top=\Sigma\). This corresponds to the argument factor in those functions. In rnvmix(), factor is of the general form as \(A\) above. The function pnvmix() accepts a singular scale matrix \(\Sigma\) as input and then estimates the distribution function correctly (that is, the distribution function of the underlying singular normal variance mixture).

For most functions in the package, the quantile function of \(W\) needs to be provided which is (here) defined as \[ F_W^\leftarrow(u)=\inf\{w\in[0,\infty):F_W(w)\ge u\},\quad u\in[0,1]. \]

2 Evaluating the distribution function

An important but difficult task is to evaluate the (cumulative) distribution function of a normal variance mixture distribution, so \[ F(\mathbf{x})=P(\mathbf{X}\le\mathbf{x})=P(X_1\le x_1,\dots,X_d\le x_d),\quad \mathbf{x}\in\mathbb{R}^d. \] In fact, the function pnvmix() can be used to estimate more general probabilities of the form \[ F(\mathbf{a},\mathbf{a} )=P(\mathbf{a} < \mathbf{X}\le\mathbf{b})=P(a_1 < X_1\le b_1,\dots, a_d < X_d\le b_d),\quad \mathbf{a},\mathbf{b}\in\mathbb{R}^d \] where \(\mathbf{a}<\mathbf{b}\) (interpreted componentwise) and entries of \(\mathbf{a},\mathbf{b}\) are allowed to be \(\pm\infty\). To this end, the function pnvmix() internally approximates the \(d\)-dimensional integral using a randomized Quasi Monte Carlo (RQMC) method. Due to the random nature, the result depends (slightly) on the seed .Random.seed.

2.1 Exponential mixture distribution

As a first example, consider a normal variance mixture with exponential mixture variable \(W\). We illustrate two approaches how to use pnvmix() to approximate \(P(\mathbf{a} < \mathbf{X} \le \mathbf{b})\) for randomly chosen \(\mathbf{a}\le\mathbf{b}\).

## [1] 0.5211573
## attr(,"error")
## [1] 0.0005840082
## attr(,"numiter")
## [1] 2
## [1] 0.5211573
## attr(,"error")
## [1] 0.0005840082
## attr(,"numiter")
## [1] 2

We see that the results coincide.

If higher precision of the computed probabilities is desired, this can be accomplished by changing the argument pnvmix.abstol (which defaults to 1e-3) in the control argument at the expense of a higher run time.

## [1] 0.5211832
## attr(,"error")
## [1] 9.166613e-06
## attr(,"numiter")
## [1] 9

2.2 Three-point mixture distribution

As a next example, consider a normal variance mixture where \(W\) is discrete. This time, we are interested in computing the one-sided probabability \(P(\mathbf{X}\le\mathbf{b})=F(\mathbf{b})\) for \(\mathbf{b}\) as constructed before.

## [1] 0.8968077
## attr(,"error")
## [1] 0.0004163609
## attr(,"numiter")
## [1] 1

This could have also been obtained as follows but we would have called pNorm() (so pnvmix()) three times then.

2.3 The wrappers pNorm() and pStudent()

For the two special cases of Student’s \(t\) distribution and the normal distribution, pNorm() and pStudent() are user-friendly wrappers of pnvmix(). Note that pStudent() works for any degree of freedom parameter \(\nu>0\) (not necessarily integer) – to the best of our knowledge, this functionality was not available in R at the time of development of this package.

2.4 The effect of algorithm-specific parameters

The function pnvmix() (and thus the wrappers pStudent() and pNorm()) give the user the possibility to change algorithm-specific parameters via the control argument. A few of them are (for others see ?get.set.parameters):

Let us now illustrate the effect of method and precond on the performance of pnvmix() with mix = 'inverse.gamma'. To this end we use the wrapper pStudent(). We set pnvmix.abstol = NULL so that the algorithm runs until the number of function evaluations exceeds fun.eval[2]. We do this for different values of fun.eval[2] in order to get an idea of the speed of convergence. We also compute the regression coefficients which act as a summary measure of the speed of convergence.

## Setup
df <- 2.1 # degrees of freedom
maxiter <- 9 # note: i iterations require 3 * 2^8 * 2^i function evaluations
max.fun.evals <- 3 * 2^8 * 2^seq(from = 2, to = maxiter, by = 1)
errors <- matrix(, ncol = length(max.fun.evals), nrow = 4)
nms <- c("Sobol  with preconditioning", "Sobol  w/o  preconditioning",
         "PRNG with preconditioning", "PRNG w/o  preconditioning")
rownames(errors) <- nms

## Computing the errors
## Note:
## - resetting the seed leads to a fairer comparison here
## - set 'verbose' to 0 or FALSE to avoid warnings which inevitably occur
##   due to 'pnvmix.abstol = NULL'
for(i in seq_along(max.fun.evals)) {
    N.max <- max.fun.evals[i]
    ## Sobol with preconditioning
    set.seed(42)
    errors[nms[1],i] <-
        attr(pStudent(b, lower = a, scale = P,  df = df,
                      control = list(pnvmix.abstol = NULL, fun.eval = c(2^6, N.max)),
                      verbose = FALSE), "error")
    ## Sobol without preconditioning
    set.seed(42)
    errors[nms[2],i] <-
        attr(pStudent(b, lower = a, scale = P,  df = df,
                      control = list(precond = FALSE, pnvmix.abstol = NULL,
                                     fun.eval = c(2^6, N.max)),
                      verbose = FALSE), "error")
    ## PRNG with preconditioning
    set.seed(42)
    errors[nms[3],i] <-
        attr(pStudent(b, lower = a, scale = P,  df = df,
                      control = list(method = "PRNG", pnvmix.abstol = NULL,
                                     fun.eval = c(2^6, N.max)),
                      verbose = FALSE), "error")
    ## PRNG without preconditioning
    set.seed(42)
    errors[nms[4],i] <-
        attr(pStudent(b, lower = a, scale = P,  df = df,
                      control = list(method = "PRNG", precond = FALSE,
                                     pnvmix.abstol = NULL, fun.eval = c(2^6, N.max)),
                      verbose = FALSE), "error")
}

## Computing the regression coefficients
coeff <- apply(errors, 1, function(y) lm(log(y) ~ log(max.fun.evals))$coeff[2])
names(coeff) <- nms

## Plot
if(doPDF) pdf(file = (file <- "fig_pnvmix_error_comparison.pdf"),
              width = 7, height = 7)
pal <- colorRampPalette(c("#000000", brewer.pal(8, name = "Dark2")[c(7, 3, 5)]))
cols <- pal(4) # colors
plot(NA, log = "xy", xlim = range(max.fun.evals), ylim = range(errors),
     xlab = "Number of function evaluations", ylab = "Estimated error")
lgnd <- character(4)
for(k in 1:4) {
    lines(max.fun.evals, errors[k,], col = cols[k])
    lgnd[k] <- paste0(nms[k]," (",round(coeff[k], 2),")")
}
legend("topright", bty = "n", lty = rep(1, 4), col = rev(cols), legend = rev(lgnd))
if(doPDF) dev.off()

We can see that in this example Sobol’ outperforms PRNG and that the preconditioning helps significantly in reducing the error.

3 Evaluating the density function

Another important task is to evaluate the density function of a normal variance mixture. This is particularly important for likelihood-based methods. In general, the density is given in terms of a univariate integral which the function dnvmix() internally approximates using a randomized Quasi Monte Carlo (RQMC) method. Due to the random nature, the result slightly varies with .Random.seed. Note that if \(\Sigma\) is singular, the density does not exist.

Note the argument log in dnvmix(): Rather than estimating the density, the logarithmic density is estimated. Only if log = FALSE (the default), the actual density is returned. This is usually numerically more stable than estimating the density and then applying the logarithm to the computed density. Also note that for many applications, the log-density is the actual quantity of interest, for example, when computing the log-likelihood.

We give two small examples:

3.1 3-point mixture

## [1] 7.301123e-15 6.122256e-15 4.975161e-15
## attr(,"error")
## [1] 6.837077e-12 6.227079e-12 5.578180e-12
## attr(,"numiter")
## [1] 1 1 1
## [1] -32.55075 -32.72685 -32.93432
## attr(,"error")
## [1] 6.837077e-12 6.227079e-12 5.578180e-12
## attr(,"numiter")
## [1] 1 1 1

3.2 Inverse-gamma mixture

In the case of an inverse-gamma mixture (so that \(\mathbf{X}\) is multivariate \(t\)), the density is known. This can be used to accurately estimate the error in our estimation procedure, as illustrated here:

4 (Quasi-)random number generation

The function rnvmix() provides a flexible tool to sample from (multivariate) normal variance mixtures. The structure is similar to the one of dnvmix() and pnvmix() (but also different in some aspects; see ?dnvmix). The user can specify the argument qmix which, as usual, corresponds to the quantile function \(F_W^\leftarrow\) of \(W\) or, alternatively, the argument rmix, which corresponds to a random number generator for \(W\). This is due to the fact that there are distributions for which it is hard to find the quantile function, but for which sampling procedures exist (for example, for stable distributions). As an example call of rnvmix(), let us revisit Section 2.1 where \(W\sim\mathrm{Exp}(1.9)\).

## Sampling
n <- 500 # sample size
set.seed(42)
r1 <- rnvmix(n, rmix = list("exp", rate = rate)) # uses the default P = diag(2)

## Plot
if(doPDF) pdf(file = (file <- paste0("fig_rnvmix_W_exp.pdf")),
              width = 6, height = 6)
plot(r1, xlab = expression(X[1]), ylab = expression(X[2]))
if(doPDF) dev.off()

An important argument of rnvmix() is method. This can be either "PRNG" (classical pseudo-random sampling) or "sobol" or "ghalton" (for the inversion method based on the corresponding low-discrepancy point set). If method is not "PRNG", qmix must be provided. As an example, let us revisit Section 2.2 where \(W\) was following a three-point distribution.

## Sampling
set.seed(42)
r1 <- rnvmix(n, qmix = qW)
r2 <- rnvmix(n, qmix = qW, method = "ghalton")

## Plot
if(doPDF) pdf(file = (file <- paste0("fig_rnvmix_W_three-point.pdf")),
              width = 9, height = 6)
ran <- range(r1, r2)
opar <- par(pty = "s")
layout(t(1:2))
plot(r1, xlab = expression(X[1]), ylab = expression(X[2]),
     main = "Pseudo-random sample", xlim = ran, ylim = ran)
plot(r2, xlab = expression(X[1]), ylab = expression(X[2]),
     main = "Quasi-random sample", xlim = ran, ylim = ran)
layout(1)
par(opar)
if(doPDF) dev.off()

When \(W\) is discrete and has finite support, one can also easily sample from the corresponding normal variance mixture using rNorm().

## Sampling
set.seed(42)
r <- lapply(1:3, function(k) rNorm(p[k] * n, scale = diag(x[k], 2)))

## Plot
if(doPDF) pdf(file = (file <- paste0("fig_rnvmix_W_three-point_via_rNorm.pdf")),
              width = 6, height = 6)
ran  <- range(r)
cols <- pal(4)
opar <- par(pty = "s")
plot(NA, xlim = ran, ylim = ran, xlab = expression(X[1]), ylab = expression(X[2]))
for(k in 1:3) points(r[[k]], col = cols[k+1])
par(opar)
if(doPDF) dev.off() 

This examples helps understanding normal variance mixtures. Note that the brown points come from \(N(\mathbf{0}, I_2)\), the blue ones from \(N(\mathbf{0}, 3I_2)\) and the green ones from \(N(\mathbf{0}, 5I_2)\) and that their frequencies correspond to the probabilities \(P(W=1)\), \(P(W=3)\) and \(P(W=5)\).

Unlike dnvmix(), rnvmix() can handle singular normal variance mixtures. In this case, the matrix factor (which is a matrix \(A\in\mathbb{R}^{d\times k}\) such that \(AA^\top=\Sigma\)) has to be provided. In the following example, we consider a Student’s \(t\) distribution via the wrapper rStudent(). As expected in the singular case, all points lie on a plane which is visible after a suitable rotation of the cloud plot.

## Sampling
df <- 3.9 # degrees of freedom
factor <- matrix(c(1,0, 0,1, 0,1), ncol = 2, byrow = TRUE) # (3,2)-matrix 'factor'
Sigma <- tcrossprod(factor) # the 'scale' corresponding to factor
stopifnot(Sigma == factor %*% t(factor))
set.seed(42)
r <- rStudent(n, df = df, factor = factor) # sample

## Plot
if(doPDF) pdf(file = (file <- paste0("fig_rnvmix_singular.pdf")),
              width = 6, height = 6)
cloud(r[,3] ~ r[,1] * r[,2], screen = list(z = 115, x = -68),
      xlab = expression(X[1]), ylab = expression(X[2]), zlab = expression(X[3]),
      scales = list(arrows = FALSE, col = "black"),
      par.settings = modifyList(standard.theme(color = FALSE),
                                list(axis.line = list(col = "transparent"),
                                     clip = list(panel = "off"))))
if(doPDF) dev.off()

5 Parameter estimation

The function fitnvmix() can be used to fit any multivariate normal variance mixture distribution to data so long as the quantile function of the mixing variable \(W\) is available. Internally, an ECME (Expectation/Conditional Maximization Either) algorithm is used to estimate the mixing parameters of \(W\), the location vector \(\mathbf{\mu}\) and the scale matrix \(\Sigma\). The specification of \(W\) is passed to fitnvmix() via the argument qmix, see also the documentation for further details. Here, qmix can be either a function of \(u\) and \(\nu\) (where \(\nu\) corresponds to the parameters of the mixing random variable \(W\)) or a string (currently allowed are qmix = "constant", qmix = "inverse.gamma" and qmix = "pareto"); note that in the latter case, analytical formulas for densities and weights are used where as in the former case, all densities and weights are estimated via RQMC methods. The following example illustrates the problem of fitting data to a Pareto-mixture.

set.seed(42) # for reproducibility 
## Define 'qmix' as the quantile function of a Par(nu, 1) distribution
qmix <- function(u, nu) (1-u)^(-1/nu)
## Parameters for sampling
n         <- 50    
d         <- 3
loc       <- rep(0, d) # true location vector
A         <- matrix(runif(d * d), ncol = d)
scale     <- cov2cor(A %*% t(A)) # true scale matrix
nu        <- 2.4 # true mixing parameter 
mix.param.bounds  <- c(1, 10) # nu in [1, 10]
## Sample data using 'rnvmix()':
x <- rnvmix(n, qmix = qmix, nu = nu, loc = loc, scale = scale)
## Call 'fitvnmix()' with 'qmix' as function (so all densities/weights are estimated)
(MyFit21 <- fitnvmix(x, qmix = qmix, mix.param.bounds = mix.param.bounds))
## Step 1: Initial estimate for 'nu' by optimizing log-likelihood 
## Step 2: ECME iteration.
##   Iteration 1
##   Iteration 2
##   Iteration 3
##   Iteration 4
##   Iteration 5
##   Iteration 6
## RETURN.
## $nu
## [1] 2.202002
## 
## $loc
## [1] -0.12046456 -0.04306758 -0.17286223
## 
## $scale
##          [,1]      [,2]      [,3]
## [1,] 1.345332 1.2791244 1.1673244
## [2,] 1.279124 1.3988674 0.9456172
## [3,] 1.167324 0.9456172 1.1696439
## 
## $iter
## [1] 6
## 
## $max.ll
## [1] -100.7721
## Call 'fitnvmix()' with 'qmix = "pareto"' in which case an analytical formula
## for the density is used
(MyFit22 <- fitnvmix(x, qmix = "pareto", mix.param.bounds = mix.param.bounds))
## Step 1: Initial estimate for 'nu' by optimizing log-likelihood 
## Step 2: ECME iteration.
##   Iteration 1
##   Iteration 2
##   Iteration 3
##   Iteration 4
##   Iteration 5
##   Iteration 6
## RETURN.
## $nu
## [1] 2.215732
## 
## $loc
## [1] -0.12048830 -0.04301149 -0.17295004
## 
## $scale
##          [,1]      [,2]      [,3]
## [1,] 1.347588 1.2812305 1.1693110
## [2,] 1.281231 1.4010124 0.9473019
## [3,] 1.169311 0.9473019 1.1715663
## 
## $iter
## [1] 6
## 
## $max.ll
## [1] -100.7536
stopifnot(all.equal(MyFit21$nu, MyFit22$nu, tol = 5e-2))
## Produce a Q-Q-Plot of the sampled mahalanobis distance versus their theoretical
## quantiles with parameters estimated in 'MyFit21' 
if(doPDF) pdf(file = (file <- paste0("fig_fitnvmix_qqplot.pdf")),
              width = 6, height = 6)
qqplot.maha(x, qmix = "pareto", loc = MyFit21$loc, scale = MyFit21$scale, 
            alpha = MyFit21$nu)
if(doPDF) dev.off()