0
$\begingroup$

I'm wondering how to set up the calculation for a double integral to solve for the value of c for the problem below.

Consider the joint probability density function $f_{XY} (x, y) = c(x+y)$ over the range $0 < x < 3$ and $x < y < x + 2$.

I know how to do this for a joint discrete distribution, e.g.

Consider the joint probability mass function $f_{XY} (x, y) = c(x + y)$ over the nine points with $x = 1, 2, 3$ and $y = 1, 2, 3$.

For this, I'd create two variables so that a function $f_{XY}$ would return all possible values of $x + y$:

x = c(rep(1,3), rep(2,3), rep(3,3))
y = rep(c(1:3),3) 
f_XY = x+y
f_XY
c = 1/sum(f_XY)

I'm not sure how to create the variables for x and y when they're continuous, so I'd really appreciate any help!

$\endgroup$
5
  • 2
    $\begingroup$ Why would you do that in R? It's simple algebra. If you want something to do algebra for you R's probably not the right tool. There's various ways to approximate it, but I presume you want an essentially exact answer. $\endgroup$
    – Glen_b
    Commented Sep 26, 2021 at 6:57
  • $\begingroup$ I'm required to do it in R for this class, unfortunately :/ would be much easier to just do on paper $\endgroup$ Commented Sep 26, 2021 at 6:59
  • $\begingroup$ What method are they looking for? $\endgroup$
    – Glen_b
    Commented Sep 26, 2021 at 7:00
  • 2
    $\begingroup$ Oh, since this appears to be for a class exercise, see stats.stackexchange.com/tags/self-study/info add the tag and modify the question accordingly. $\endgroup$
    – Glen_b
    Commented Sep 26, 2021 at 7:03
  • $\begingroup$ I don't think there's any limitation on the method, as long as it works? Sorry, I'm a beginner in R and a little out of practice with stat so I'm not sure if that answers the question. The problem above was the only reference I had for this. $\endgroup$ Commented Sep 26, 2021 at 7:03

1 Answer 1

1
$\begingroup$

The integral2 function of the pracma package is a possibility:

library(pracma)

f <- function(x ,y) x + y

integral2(f, xmin = 0, xmax = 3, ymin = function(x) x, ymax = function(x) x+2)

If you are not allowed to use a package, you can nest the integrate function:

inner <- function(x) integrate(function(y) x+y, lower = x, upper = x + 2)$value
integrate(Vectorize(inner), lower = 0, upper = 3)

Both methods give 24, an approximate value of the double integral.


Finally, you can use the SimplicialCubature package after noticing that the region of integration can be split into two triangles (= simplicies). Moreover, the integrand is a polynomial function, and then SimplicialCubature offers the possibility to get the exact value of the integral.

library(SimplicialCubature)

S1 <- cbind(c(0,0), c(3,3), c(0,2)) # first triangle
S2 <- cbind(c(0,2), c(3,3), c(3,5)) # second triangle
S <- array(c(S1, S2), dim = c(2, 3, 2))

P <- definePoly(coef = c(1,1), k = cbind(c(1,0), c(0,1)))
printPoly(P) # x + y

integrateSimplexPolynomial(P, S)
# 24
$\endgroup$
3
  • $\begingroup$ Why the downvote ? :-/ $\endgroup$ Commented Sep 26, 2021 at 13:00
  • $\begingroup$ This worked, thank you so much! I wasn't aware of the pracma package $\endgroup$ Commented Sep 26, 2021 at 20:24
  • $\begingroup$ @stat-lilili-894 Glad to help. Please mark my answer as accepted (by clicking the checkmark). $\endgroup$ Commented Sep 27, 2021 at 10:40

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.