rcontinuous-integrationconvolutionweibull

Integration issue regarding the convolution of Weibull distribution


Hi I was trying to compute the convolution of Weibull distribution as follows with parameter t, lambda(scale parameter) and k:

f.x <- function(x,lambda,k){
  dweibull(x,k,lambda)
}
cum <- function(T,lambda,k)
{ 
  return(1 - exp(-(T/lambda)^k))
}

F1_t = function(t,lambda,k){
  cum(t,lambda,k)
}
F2_t = function(t,lambda,k){
  integrate(function(x) F1_t(t-x,lambda,k)*f.x(x,lambda,k),0,t)$value

}
F3_t = function(t, lambda ,k){
  integrate(function(x) F2_t(t-x,lambda,k)*f.x(x,lambda,k),0,t)$value
}

Where F2_t and F3_t are the convolution definition of the second and third convolution,F2_t works fine but as I evaluated F3_t with

F3_t(1,1,2)

I got the following warning from R:

 Error in integrate(function(x) F1_t(t - x, lambda, k) * f.x(x, lambda,  : 
  length(upper) == 1 is not TRUE 

I am wondering why this problem appears and how to solve this issue.


Solution

  • The problem is that, unlike F1_t, F2is not vectorized, meaning it does not return a vector of results for a vector of t.

    Observe that

    F1_t(c(1,2), 1, 1)
    

    works fine, while

    F2_t(c(1,2), 1, 1)
    

    fails because you cannot provide a vector of upper integration limits to integrate.

    So, the solution is to vectorize F2_t:

    F2_t = function(t,lambda,k){
      sapply(t, function(y) integrate(function(x) F1_t(t-x,lambda,k)*f.x(x,lambda,k), lower = 0, upper = y)$value)
    }
    

    With the code above for F2_t, F3_t(1,1,2) runs fine.