samedi 8 février 2020

How to generate multivariate nonnormal random numbers in R?

Background

I want to generate multivariate distributed random numbers with a fixed correlation matrix. For example, I want to generate a 2 dimensional data with correlation value = 0.5. The first maginal of data is a norm distribution with mean = 0, sd = 1, and the next is a exponential distribution with rate = 2.

My attempt

My attempt is that we can generate a correlated multinormal distribution random numbers and then revised them to be any distribution by Inverse transform sampling.

In below, I give an example about transforming 2 dimensional normal distribution random numbers into a norm(0,1)+ exp(2) random number:

# generate a correlated multi-normal distribution, data[,1] and data[,2] are standard norm
data <- mvrnorm(n = 1000,mu = c(0,0), Sigma = matrix(c(1,0.5,0.5,1),2,2))
# calculate the cdf of dimension 2
exp_cdf = ecdf(data[,2])
Fn = exp_cdf(data[,2])  
# inverse transform sampling to get Exponetial distribution with rate = 2
x = -log(1-Fn + 10^(-5))/2
mean(x);cor(data[,1],x)

Out:

[1] 0.5035326
[1] 0.436236

From the outputs, the new x is a exponential 2 random numbers. Also, x and data[,1] are correlated with 0.43.

My question

As a statistics graduate, I know there exist 10+ methods to generate multivariate random numbers theoretical. In this post, I want to collect bunch of good package to do it automatically. And then, I will compare them from different aspects, like time consuming and quality of data etc. Any ideas is appreciated!

Sources:

  1. generating-correlated-random-variables, math

  2. use copulas to generate multivariate random numbers, stackoverflow

  3. Ross simulation, theoretical book




Aucun commentaire:

Enregistrer un commentaire