3

I would like to ask if any of you guys can help me find how can I compute a double integral of a given joint distribution e.g. dnorm(x,m1,s1)*dnorm(y,m2,s2)*CopulaDensity with Ymin=-inf, Ymax=inf, and Xmin= x and Xmax = inf? I want to do this in R.

Thanks a lot .

Note! CopulaDensity is a numeric vector .

Giorgos
  • 31
  • 2

1 Answers1

2

Here is a solution inspired in this post.
But with no CopulaDensity.

f <- function(x, m1, s1, m2, s2) {
  dnorm(x, m1, s1) * integrate(function(y, m2, s2) {
    dnorm(y, m2, s2)
  }, x, Inf, m2 = m2, s2 = s2)$value
}

(res <- integrate(Vectorize(f), -Inf, Inf, m1 = 0, s1 = 1, m2 = 0, s2 = 1)$value)
#[1] 0.5
Rui Barradas
  • 70,273
  • 8
  • 34
  • 66
  • Thank you for answering. Although I need to multiply those 2 densities with a numeric vector as mentioned above. Any thoughts on how I can embed that ? – Giorgos Oct 23 '21 at 11:29
  • @Giorgos You can have an extra argument `vec` at the end of `f <- function(x, m1, s1, m2, s2, vec)` and then use it anywhere you want in the function body. If the vector is `v`, put it before the 1st `dnorm`. – Rui Barradas Oct 23 '21 at 13:42
  • `f <- function(x, m1, s1, m2, s2,v) { v*dnorm(x, m1, s1) * integrate(function(y, m2, s2) { dnorm(y, m2, s2) }, x, Inf, m2 = m2, s2 = s2)$value }` Something like this ? – Giorgos Oct 23 '21 at 17:09
  • @Giorgos Yes, for what I understand of the problem that should work. Can you try it with known input/output? – Rui Barradas Oct 23 '21 at 17:18
  • Unfortunately, I get the following error : `Error in integrate(Vectorize(f), -Inf, Inf, m1 = mean(x), copuladensity = Frank.Density, : evaluation of function gave a result of wrong length In addition: Warning message: In mapply(FUN = function (x, m1, s1, m2, s2, copuladensity) : longer argument not a multiple of length of shorter` – Giorgos Oct 24 '21 at 12:02
  • I think that there is something wrong with the fixed Vector values . – Giorgos Oct 24 '21 at 12:04