My question is very related to the general sum of Gamma RVs question found in the following link:
[The sum of two independent gamma random variables
There is some helpful R code there for generating a distribution function for a sum of Gamma random variables. However, I am wondering if there is a more general form that allows for arbitrary correlations between the variables.
Specifically, suppose I have:
$X_i$ ~ $Gamma(\alpha_i,\beta_i), \;\;\;\;\;\; i=1,...,n$
but $cov(X_i,X_j)=\rho_{ij}\neq 0$
Can the R code below be augmented to account for this in simulating the empirical distribution of $\Sigma_{i=1}^n X_i$? If these were normally distributed it would be easy, but I cannot find any generalization for Gamma random variables.
shape <- 1:3 #ki
scale <- 1:3 # thetai
make_cumgenfun <- function(shape, scale) {
# we return list(shape, scale, K, K', K'')
n <- length(shape)
m <- length(scale)
stopifnot( n == m, shape > 0, scale > 0 )
return( list( shape=shape, scale=scale,
Vectorize(function(s) {-sum(shape * log(1-scale * s) ) }),
Vectorize(function(s) {sum((shape*scale)/(1-s*scale))}) ,
Vectorize(function(s) { sum(shape*scale*scale/ (1-s*scale)) })) )
}
solve_speq <- function(x, cumgenfun) {
# Returns saddle point!
shape <- cumgenfun[[1]]
scale <- cumgenfun[[2]]
Kd <- cumgenfun[[4]]
uniroot(function(s) Kd(s)-x,lower=-100,
upper = 0.3333,
extendInt = "upX")$root
}
make_fhat <- function(shape, scale) {
cgf1 <- make_cumgenfun(shape, scale)
K <- cgf1[[3]]
Kd <- cgf1[[4]]
Kdd <- cgf1[[5]]
# Function finding fhat for one specific x:
fhat0 <- function(x) {
# Solve saddlepoint equation:
s <- solve_speq(x, cgf1)
# Calculating saddlepoint density value:
(1/sqrt(2*pi*Kdd(s)))*exp(K(s)-s*x)
}
# Returning a vectorized version:
return(Vectorize(fhat0))
} #end make_fhat
fhat <- make_fhat(shape, scale)
plot(fhat, from=0.01, to=40, col="red", main="unnormalized saddlepoint approximation\nto sum of three gamma variables")