2
$\begingroup$

I would like to model some time series. For this purpose I have the marginal distribution and want to use a gaussian copula to build the dependency. Following a tutorial the following should give me the desired dependency structure with correct marginals

library(mvtnorm)
S <- matrix(c(0.025,0.0075,0.0075,0.007),2,2)
normal <- rmvnorm(n = 1000,mean = c(4.1,2.08),sigma = S)
uniform <- pnorm(normal)
hist(uniform[,1])
hist(uniform[,2])

However, the histograms look not at all like a uniform distribution. What is wrong with the above code? The help of "rmvnorm" says sigma is the covariance matrix. Looking at other example in the web I see that often they use the correlation matrix. So what is expected by rmvnorm? Moreover, the covariance of uniform should stay the same. However, I get the following result:

cov(uniform)
             [,1]         [,2]
[1,] 3.481586e-10 3.626536e-08
[2,] 3.626536e-08 1.440411e-05
> 

which varies a lot from the input matrix $S$. So whats going wrong here?

$\endgroup$
2
  • $\begingroup$ Can you share the link for the tutorial or anywhere that shows that the result should be a uniform distribution? As it stands, this looks like a coding question and thus not appropriate for CV. $\endgroup$ May 15, 2016 at 15:40
  • $\begingroup$ The code is from this blog r-bloggers.com/copulas-made-easy if its not appropriate for CV feel free to move it to overflow $\endgroup$
    – math
    May 15, 2016 at 16:09

1 Answer 1

1
$\begingroup$

Your error is in the usage of pnorm. pnorm is used to calculate the CDF (or by default, 1- CDF) $F(q)$ for a standard normal. That is pnorm(q) is going to calculate

$$u = P(X \geq q) $$

for when $X \sim N(0,1)$. However, in your example, your normal distributions are not mean 0 and variance 1. So you need to adjust your pnorms, like

uniform <- matrix(NA, nrow = 1000, ncol = 2)
S <- matrix(c(0.025,0.0075,0.0075,0.007),2,2)
normal <- rmvnorm(n = 1000, mean = c(4.1,2.08),sigma = S)
uniform[,1] <- pnorm(normal[,1], mean = 4.1, sd = sqrt(.025))
uniform[,2] <- pnorm(normal[,2], mean = 2.08, sd = sqrt(.007))
hist(uniform[,1])
hist(uniform[,2])

enter image description here

The r-bloggers code worked because they used mean 0 and unit variance in their rmvnorm. It has nothing to do with correlation matrix.

$\endgroup$
2
  • $\begingroup$ One small question regarding your answer. Shouldnt we take $1-CDF$, i.e. $1-pnorm$ given your comment at the beginning? $\endgroup$
    – math
    May 15, 2016 at 22:15
  • $\begingroup$ Yes, ideally we should, but it doesn't matter because 1-U(0,1) is also a U(0,1) $\endgroup$ May 15, 2016 at 22:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.