Integrate over an integral in R

前端 未结 2 1665
一向
一向 2021-01-13 09:54

I want to solve the following in R:

0H [π(t) ∫tH A(x) dx ] dt <

2条回答
  •  醉酒成梦
    2021-01-13 10:43

    In this particular case, you don't need to Vectorize since the integral of dbeta is already implemented in R through pbeta. Try this:

    prior <- function(t) dbeta(t, 1, 24)
    #define the integral of the A function instead
    Aint     <- function(x,H) pbeta(H, 1, 4) - pbeta(x,1,4)
    expected_loss <- function(H){
      integrand<-function(x) Aint(x,H)*prior(x)
      loss          <- integrate(integrand, lower = 0, upper = H)$value
      return(loss)
    }
    expected_loss(.5)
    #[1] 0.7946429
    expected_loss(1)
    #[1] 0.8571429
    

提交回复
热议问题