Efficient multivariate normal distribution calculations in StataMichael Grayling
Introduction ConclusionMethods Results
Efficient multivariate normal distribution calculations in Stata
Michael Grayling
Supervisor: Adrian Mander
MRC Biostatistics Unit
2015 UK Stata Users Group Meeting10/09/15
1/21
Why and what?
Why do we need to be able to work with the Multivariate Normal Distribution?
Efficient multivariate normal distribution calculations in StataMichael Grayling
Introduction ConclusionMethods Results
2/21
β’ The normal distribution has significant importance in statistics.
β’ Much real world data either is, or is assumed to be, normally distributed.
β’ Whilst the central limit theorem tells us the mean of many random variables drawn independently from the samedistribution will be approximately normally distributed.
β’ Today however a considerable amount of statistical analysis performed is not univariate, but multivariate in nature.
β’ Consequently the generalisation of the normal distribution to higher dimensions; the multivariate normaldistribution, is of increasing importance.
Why and what?
Definition
Efficient multivariate normal distribution calculations in StataMichael Grayling
ConclusionMethods ResultsIntroduction
3/21
β’ Consider a π-dimensional random variable π. If π has a (non-degenerate) MVN distribution with location parameter(mean vector) π β βπ and positive definite covariance matrix Ξ£ β βπΓπ , denoted π ~ ππ π, Ξ£ , then itsdistribution has density ππ π± for π± = π₯1, β¦ , π₯π β βπ given by:
ππ π± = ππ π, Ξ£ =1
Ξ£ 2π πexp β
1
2π± β π β€Ξ£β1 π± β π β β,
where Ξ£ = det Ξ£ .
β’ In this instance we have:
πΌ π = π,Var π = Ξ£,
β ππ β€ π₯π β€ ππ βΆ π = 1,β¦ ,π = π π, π, π, Ξ£ = π1
π1
β¦ ππ
ππ
ππ π, Ξ£ ππ± .
The multivariate normal distribution in Stata
Whatβs available?
Efficient multivariate normal distribution calculations in StataMichael Grayling
ConclusionMethods ResultsIntroduction
4/21
β’ drawnorm allows random samples to be drawn from the multivariate normal distribution.
β’ binormal allows the computation of cumulative bivariate normal probabilities.
β’ mvnp allows the computation of cumulative multivariate normal probabilities through simulation using the GHK simulator.
. set obs 1000
. matrix R = (1, .25 \ .25, 1)
. drawnorm v1 v2, corr(R) seed(13131313)
. matrix C = cholesky(R)
. ge x_b = binormal(v1,v2,.25)
. mdraws, neq(2) dr(500) prefix(p)
. egen x_s = mvnp(v1 v2), dr(500) chol(C) prefix(p) adoonly
. su x_b x_s
Variable | Obs Mean Std. Dev. Min Max
-------------+--------------------------------------------------------
x_b | 1000 .2911515 .238888 6.76e-06 .9953722
x_s | 1000 .2911539 .2388902 6.76e-06 .9953699
The multivariate normal distribution in Stata
The new commands
Efficient multivariate normal distribution calculations in StataMichael Grayling
ConclusionMethods ResultsIntroduction
5/21
β’ Utilise Mata and one of the new efficient algorithms that has been developed to quickly compute probabilities over any range of integration.
β’ Additionally, thereβs currently no easy means to compute equi-coordinate quantiles which have a range of applications:
π = ββ
π
β¦ ββ
π
ππ π, Ξ£ ππ,
so use interval bisection to search for π, employing the former algorithm for probabilities to evaluate the RHS.
β’ Final commands named mvnormalden, mvnormal, invmvnormal and rmvnormal, with all four using Mata.
β’ mvnormal in particular makes use of a recently developed Quasi-Monte Carlo Randomised Lattice algorithm forperforming the required integration.
β’ All four are easy to use with little user input required.
The multivariate normal distribution in Stata
Talk outline
Efficient multivariate normal distribution calculations in StataMichael Grayling
ConclusionMethods ResultsIntroduction
6/21
β’ Discuss the transformations and algorithm that allows the distribution function to be worked with efficiently.
β’ Detail how this code can then be used to compute equi-coordinate quantiles.
β’ Compare the performance of mvnormal to mvnp.
β’ Demonstrate how mvnormal can be used to determine the operating characteristics of a group sequential clinical trial.
Working with the distribution function
Transforming the integral
Efficient multivariate normal distribution calculations in StataMichael Grayling
Introduction ConclusionMethods Results
7/21
β’ First we use a Cholesky decomposition transformation: π = πΆπ², where πΆπΆβ€ = Ξ£:
π π, π, π, Ξ£ =1
Ξ£ 2π π π1
π1
β¦ ππ
ππ
πβ12π
β€Ξ£β1πππ,
, , , =1
2π π π1β²
π1β²
πβπ¦12/2
ππβ²
ππβ²
πβπ¦π2 /2ππ².
β’ Next transform each of the π¦πβs separately using π¦π = Ξ¦β1 π§π :
π π, π, π, Ξ£ = π1
π1
β¦ ππ π§1,β¦,π§πβ1
ππ π§1,β¦,π§πβ1
ππ³.
β’ Turn the problem in to a constant limit form using π§π = ππ + π€π ππ β ππ :
π π, π, π, Ξ£ = π1 β π1 0
1
π2 β π2 β¦ 0
1
ππ β ππ 0
1
ππ°.
Working with the distribution function
Quasi Monte Carlo Randomised Lattice Algorithm
Efficient multivariate normal distribution calculations in StataMichael Grayling
Introduction ConclusionResultsMethods
8/21
β’ Specify a number of shifts of the Monte Carlo algorithm π, a number of samples for each shift π, and a Monte Carloconfidence factor πΌ . Set πΌ = π = 0 , π = π1, β¦ , ππ = π = π1, β¦ , ππ = 0,β¦ , 0 and π² = π¦1, β¦ , π¦πβ1 =
0,β¦ , 0 . Compute the Cholesky factor πΆ = πππ .
β’ For π = 1,β¦ ,π:β’ Set πΌπ = 0 and generate uniform random π« = Ξ1, β¦ , Ξπβ1 β 0,1 πβ1.β’ For π = 1,β¦ , π:
β’ Set π° = 2 Γmod π π© + π«, 1 β 1 , where π© is a vector of the first π β 1 prime numbers.
β’ Set π1 = Ξ¦ π1/π11 , π1 = Ξ¦ π1/π11 and π1 = π1 β π1.β’ For π = 2,β¦ ,π:
β’ Set π¦πβ1 = Ξ¦β1 ππβ1 + π€πβ1 ππβ1 β ππβ1 , ππ = Ξ¦ ππ β π=1πβ1 ππππ¦π /πππ , ππ = Ξ¦ ππ β
Equi-coordinate quantiles
Computing equi-coordinate quantiles
Efficient multivariate normal distribution calculations in StataMichael Grayling
Introduction ConclusionResultsMethods
9/21
β’ Recall the definition of an equi-coordinate quantile:
π = π π = ββ
π
β¦ ββ
π
Ξ¦π π, Ξ£ ππ.
β’ We can compute π for any π efficiently using the algorithm discussed previously to evaluate the RHS for any π, andmodified interval bisection to search for the correct π.
β’ Optimize does not work well because of the small errors present when you evaluate the RHS.
β’ Choose a maximum number of interactions πmax, and a tolerance π.β’ Initialise π = β106, π = 106 and π = 1. Compute π π and π π .β’ While π β€ πmax:
β’ Set π = π β π β π / π π β π π π π and compute π π .
β’ If π π = 0 or π β π /2 < π break. Else:β’ If π π , π π < 0 or π π , π π > 0 set π = π and π π = π π . Else set π = π and π(π) = π(π).
β’ Set π = π + 1.β’ Return π = π.
Syntax
mvnormal
Efficient multivariate normal distribution calculations in StataMichael Grayling
Introduction ConclusionMethods Results
10/21
mvnormal, LOWer(numlist miss) UPPer(numlist miss) MEan(numlist) Sigma(string) [SHIfts(integer 12) ///
SAMples(integer 1000) ALPha(real 3)]
invmvnormal, p(real) MEan(numlist) Sigma(string) [Tail(string) SHIfts(integer 12) SAMples(integer 1000) ///
ALPha(real 3) Itermax(integer 1000000) TOLerance(real 0.000001)]
π Ξ£π π
πΌ
π
π
Syntax
invmvnormal
Efficient multivariate normal distribution calculations in StataMichael Grayling
Introduction ConclusionMethods Results
10/21
mvnormal, LOWer(numlist miss) UPPer(numlist miss) MEan(numlist) Sigma(string) [SHIfts(integer 12) ///
SAMples(integer 1000) ALPha(real 3)]
invmvnormal, p(real) MEan(numlist) Sigma(string) [Tail(string) SHIfts(integer 12) SAMples(integer 1000) ///
ALPha(real 3) Itermax(integer 1000000) TOLerance(real 0.000001)]
π π
πmax π
π π Ξ£
πΌ
Syntax
invmvnormal
Efficient multivariate normal distribution calculations in StataMichael Grayling
Introduction ConclusionMethods Results
10/21
mvnormal, LOWer(numlist miss) UPPer(numlist miss) MEan(numlist) Sigma(string) [SHIfts(integer 12) ///
SAMples(integer 1000) ALPha(real 3)]
invmvnormal, p(real) MEan(numlist) Sigma(string) [Tail(string) SHIfts(integer 12) SAMples(integer 1000) ///
ALPha(real 3) Itermax(integer 1000000) TOLerance(real 0.000001)]
π π
πmax π
π π Ξ£
πΌ
lower, upper, or both
Performance Comparison
Set-up
Efficient multivariate normal distribution calculations in StataMichael Grayling
Introduction ConclusionMethods Results
11/21
β’ Compare the average time required to compute a single particular integral, and the associated average absolute errorby mvnp for different numbers of draws and across different dimensions, in comparison to mvnormal.
β’ Take the case Ξ£ππ = 1, Ξ£ππ = 0.5 for π β π, with ππ = 0 for all π.
β’ First determine the 95% both tailed quantile about π using invmvnormal, then assess how close the value returnedby mvnp and mvnormal is to 0.95 on average, across 100 replicates.
β’ Do this for the 3, 5, 7 and 10 dimensional problems, with draws set to 5 (default), 10, 25, 50, 75, 100 and 200.
β’ Caveats:
β’ This is the case when you desire the value to only one integral.β’ mvnormal will soon be changed to become more efficient through variable re-ordering methods and
parallelisation.
Performance Comparison
Using invmvnormal and mvnormal
Efficient multivariate normal distribution calculations in StataMichael Grayling
Introduction ConclusionMethods Results
12/21
β’ First initialise the covariance matrix Sigma, then pass this and the other required characteristics to invmvnormal:
. mat Sigma = 0.5*I(3) + J(3, 3, 0.5)
. invmvnormal, p(0.95) mean(0, 0, 0) sigma(Sigma) tail(both)
Quantile = 2.3487841
Error = 1.257e-08
Flag = 0
fQuantile = 9.794e-06
Iterations = 185
β’ We can verify further the accuracy of this quantile value using mvnormal:
. mvnormal, lower(-2.3487841, -2.3487841, -2.3487841) upper(2.3487841, 2.3487841, 2.3487841) sigma(Sigma) mean(0, 0, 0)
Integral = .94999214
Error = .00006841
Performance Comparison
Mean Computation Time
Efficient multivariate normal distribution calculations in StataMichael Grayling
Introduction ConclusionMethods Results
13/21
Number of Draws
Mea
n C
om
pu
tati
on
Tim
e
Key
Dim = 3, mvnp
Dim = 5, mvnp
Dim = 7, mvnp
Dim = 10, mvnp
Dim = 3, mvnormal
Dim = 5, mvnormal
Dim = 7, mvnormal
Dim = 10, mvnormal
Performance Comparison
Mean Absolute Error
Efficient multivariate normal distribution calculations in StataMichael Grayling
Introduction ConclusionMethods Results
14/21
Key
Dim = 3, mvnp
Dim = 5, mvnp
Dim = 7, mvnp
Dim = 10, mvnp
Dim = 3, mvnormal
Dim = 5, mvnormal
Dim = 7, mvnormal
Dim = 10, mvnormalMea
n A
bso
lute
Err
or
Number of Draws
Performance Comparison
Relative Performance
Efficient multivariate normal distribution calculations in StataMichael Grayling
Introduction ConclusionMethods Results
15/21
DimensionDraws = 5 Draws = 50 Draws = 75 Draws = 150
Rel. Mean Error Rel. Time Req. Rel. Mean Error Rel. Time Req. Rel. Mean Error Rel. Time Req. Rel. Mean Error Rel. Time Req.
3 156.5 1.55 147.0 9.53 147.6 15.11 147.0 34.52
5 62.0 0.94 51.0 7.18 50.3 11.71 50.5 29.15
7 47.8 0.97 32.8 8.18 32.7 13.81 32.5 35.53
10 33.5 0.95 17.6 8.97 16.3 15.40 16.7 44.37
Group sequential clinical trial design
Triangular Test
Efficient multivariate normal distribution calculations in StataMichael Grayling
Introduction ConclusionMethods Results
16/21
β’ Suppose we wish to design a group sequential clinical trial to compare the performance of two drugs, π΄ and π΅, and ultimately to test the following hypotheses:
π»0 βΆ ππ΅ β ππ΄ β€ 0, π»1 βΆ ππ΅ β ππ΄ > 0.
β’ We plan to recruit π patients to each drug in each of a maximum of πΏ stages, and desire a type-I error of πΌ when ππ΅ βππ΄ = 0 and a type-II error of π½ when ππ΅ β ππ΄ = πΏ.
β’ We utilise the following standardised test statistics at each analysis:
ππ = ππ΅ β ππ΄ πΌπ1/2
,
and wish to determine early stopping efficacy and futility boundaries; ππ and ππ, π = 1, β¦ , πΏ in order to give the required operating characteristics.
β’ Additionally, information is linked to sample size by π = 2π2πΌ1 where π2 is the variance of the patient responses on treatment π΄ or π΅.
Group sequential clinical trial design
Triangular Test
Efficient multivariate normal distribution calculations in StataMichael Grayling
Introduction ConclusionMethods Results
17/21
β’ Whitehead and Stratton (1983) demonstrated this could be approximately achieved by taking:
ππ = πΌπβ1/2
β2
πΏlog
1
2πΌ+ 0.583
πΌπΏπΏ
+3 πΏ
4
π
πΏπΌπΏ ,
ππ = πΌπβ1/2 2
πΏlog
1
2πΌβ 0.583
πΌπΏπΏ
+ πΏ
4
π
πΏπΌπΏ ,
πΏ =2Ξ¦β1 1 β πΌ
Ξ¦β1 1 β πΌ + Ξ¦β1 1 β π½πΏ.
β’ Desiring ππΏ = ππΏ to ensure a decision is made at the final analysis, we have:
πΌπΏ =4 Γ 0.5832
πΏ+ 8 log
1
2πΌ
1/2
β2 Γ 0.583
πΏ1/21
πΏ2.
Group sequential clinical trial design
Computing the Designs Performance
Efficient multivariate normal distribution calculations in StataMichael Grayling
Introduction ConclusionMethods Results
18/21
β’ We can compute the expected sample size or power at any true treatment effect π = ππ΅ β ππ΄ using multivariateintegration and the following facts:
πΌ ππ = ππΌπ1/2
, π = 1, . . , πΏ,
Cov ππ1 , ππ2 = πΌπ1/πΌπ21/2
, 1 β€ π1 β€ π2 β€ πΏ.
β’ For example, define πππ π and πππ π to be the probabilities we stop for futility or efficacy at stage π respectively. Then
for example:
ππ3 π = π1
π1
π2
π2
ββ
π3
Ξ¦ π, Cov π ππ½ , , , for π = π,β¦ , π β€, π = π1, β¦ , π3β€.
β’ Then we have:
πΌ π|π =
π=1
πΏ
2π πππ π + πππ π and Power π =
π=1
πΏ
πππ π .
Group sequential clinical trial design
Power and expected sample size
Efficient multivariate normal distribution calculations in StataMichael Grayling
Introduction ConclusionMethods Results
19/21
ππ
π
True treatment effectFixed Sample Design Triangular Test
πΌ π|π Power π πΌ π|π Power π
π = 0 620 0.050 401.6 0.051
π = πΏ 620 0.808 469.3 0.801
β’ As an example, determine the design for πΏ = 3, πΏ = 0.2, πΌ = 0.05, π½ = 0.2, π = 1.
Conclusion
Complete and simple to use
Efficient multivariate normal distribution calculations in StataMichael Grayling
Introduction ConclusionMethods Results
20/21
β’ Created four easy to use commands that allow you to work with the multivariate normal distribution.
β’ Performance of these commands is seen to be very good.
β’ Complementary to mvnp with the relative efficiency dependent on the number of required integrals.
β’ Similarly, we have created commands for working with the multivariate π‘ distribution.
β’ Moving forward, we would like to add functionality to allow alternate specialist algorithms to be used.