Rituparna Dey
Rituparna Dey

Reputation: 1

R code for sampling from a mixture of normal and exponential components

I am trying to generate a bivariate sample from a mixture of two component distributions viz., normal and gamma distributions. I am using the 'copula' package for that.

The code I wrote is as follows-

library("copula")

mycop = normalCopula(param=c(0), dim=2, dispstr="ex")
mymvd = mvdc(
  copula=mycop, 
  margins=c("norm", "norm"),
  paramMargins=list(list(mean=-1, sd=0.5),list(mean=1, sd=0.5)))

This gave a bivariate sample from Normal(-1,1,0.5^2,0.5^2,0). The same code with exponential will lead give a bivariate sample from that distribution. But I don't know how to generate from w*BivariateNormal + (1-w)*BivariateGamma. Please help. I searched everywhere in yt but couldn't find anything. Thanks in advance.

Upvotes: 0

Views: 364

Answers (1)

Peter O.
Peter O.

Reputation: 32888

In general, you sample from a mixture of two distributions by:

  • Generating a uniform(0, 1) variate (runif in R), then
  • Sampling from the first distribution if that variate is less than w, or
  • Sampling from the second distribution otherwise.

Similarly, you sample from a mixture of multiple distributions by generating a uniform, or non-uniform random integer, then sampling from a distribution depending on the integer chosen.

Upvotes: 1

Related Questions