Title: | Convolution of Gamma Distributions |
---|---|
Description: | Evaluation for density and distribution function of convolution of gamma distributions in R. Two related exact methods and one approximate method are implemented with efficient algorithm and C++ code. A quick guide for choosing correct method and usage of this package is given in package vignette. For the detail of methods used in this package, we refer the user to Mathai(1982)<doi:10.1007/BF02481056>, Moschopoulos(1984)<doi:10.1007/BF02481123>, Barnabani(2017)<doi:10.1080/03610918.2014.963612>, Hu et al.(2020)<doi:10.1007/s00180-019-00924-9>. |
Authors: | Chaoran Hu [aut, cre], Vladimir Pozdnyakov [ths], Jun Yan [ths] |
Maintainer: | Chaoran Hu <[email protected]> |
License: | GPL (>= 3.0) |
Version: | 1.2.2.9000 |
Built: | 2024-11-05 04:12:14 UTC |
Source: | https://github.com/chaoranhu/coga |
Evaluation for density and distribution function of convolution of gamma distributions in R. Two related exact methods and one approximate method are implemented with efficient algorithm and C++ code. A quick guide for choosing correct method and usage of this package is given in package vignette.
dcoga, pcoga, rcoga, dcoga2dim, pcoga2dim, pcoga2dim_diff_shape, dcoga_approx, and pcoga_approx.
*maintainer, author* Chaoran Hu [email protected]
*advisor* Jun Yan [email protected]
*advisor* Vladimir Pozdnyakov [email protected]
Density, distribution function, and random generation for convolution
of gamma distributions. Convolution of independent Gamma random
variables is , where
,
are independent Gamma distributions with parameters shapes and rates.
The exact density function and distribution function can be calculated,
according to the formulas from Moschopoulos, Peter G. (1985).
**We mention that
dcoga
and pcoga
are recommended
for .**
dcoga(x, shape, rate) pcoga(x, shape, rate) rcoga(n, shape, rate)
dcoga(x, shape, rate) pcoga(x, shape, rate) rcoga(n, shape, rate)
x |
Quantiles. |
shape |
Numerical vector of shape parameters for each gamma distributions, all shape parameters should be larger than or equal to 0, with at least one non-zero. |
rate |
Numerical vector of rate parameters for each gamma distributions, all rate parameters should be larger than 0. |
n |
Number of sample points. |
Chaoran Hu
Moschopoulos, Peter G. "The distribution of the sum of independent gamma random variables." Annals of the Institute of Statistical Mathematics 37.1 (1985): 541-544.
## Example 1: Correctness check set.seed(123) ## do grid y <- rcoga(100000, c(3,4,5), c(2,3,4)) grid <- seq(0, 15, length.out=100) ## calculate pdf and cdf pdf <- dcoga(grid, shape=c(3,4,5), rate=c(2,3,4)) cdf <- pcoga(grid, shape=c(3,4,5), rate=c(2,3,4)) ## plot pdf plot(density(y), col="blue") lines(grid, pdf, col="red") ## plot cdf plot(ecdf(y), col="blue") lines(grid, cdf, col="red") ## Example 2: Show parameter recycling ## these pairs give us the same results dcoga(1:5, c(1, 2), c(1, 3, 4, 2, 5)) dcoga(1:5, c(1, 2, 1, 2, 1), c(1, 3, 4, 2, 5)) pcoga(1:5, c(1, 3, 5, 2, 2), c(3, 5)) pcoga(1:5, c(1, 3, 5, 2, 2), c(3, 5, 3, 5, 3))
## Example 1: Correctness check set.seed(123) ## do grid y <- rcoga(100000, c(3,4,5), c(2,3,4)) grid <- seq(0, 15, length.out=100) ## calculate pdf and cdf pdf <- dcoga(grid, shape=c(3,4,5), rate=c(2,3,4)) cdf <- pcoga(grid, shape=c(3,4,5), rate=c(2,3,4)) ## plot pdf plot(density(y), col="blue") lines(grid, pdf, col="red") ## plot cdf plot(ecdf(y), col="blue") lines(grid, cdf, col="red") ## Example 2: Show parameter recycling ## these pairs give us the same results dcoga(1:5, c(1, 2), c(1, 3, 4, 2, 5)) dcoga(1:5, c(1, 2, 1, 2, 1), c(1, 3, 4, 2, 5)) pcoga(1:5, c(1, 3, 5, 2, 2), c(3, 5)) pcoga(1:5, c(1, 3, 5, 2, 2), c(3, 5, 3, 5, 3))
Density and distribution function of convolution of gamma distributions
are calculated based on approximation method from Barnabani(2017), which
gives us the approximate result and faster evaluation than dcoga
and pcoga
during three or more variables case. **So, we recommend
these functions for three or more varibales case with approximate result.**
dcoga_approx(x, shape, rate) pcoga_approx(x, shape, rate)
dcoga_approx(x, shape, rate) pcoga_approx(x, shape, rate)
x |
Quantiles. |
shape |
Numerical vector of shape parameters for each gamma distributions, all shape parameters should be larger than or equal to 0, with at least three non-zero. |
rate |
Numerical vector of rate parameters for each gamma distributions, all rate parameters should be larger than 0. |
Chaoran Hu
Barnabani, M. (2017). An approximation to the convolution of gamma distributions. Communications in Statistics - Simulation and Computation 46(1), 331-343.
## Example 1: Correctness check set.seed(123) ## do grid y <- rcoga(100000, c(3,4,5), c(2,3,4)) grid <- seq(0, 15, length.out=100) ## calculate pdf and cdf pdf <- dcoga_approx(grid, shape=c(3,4,5), rate=c(2,3,4)) cdf <- pcoga_approx(grid, shape=c(3,4,5), rate=c(2,3,4)) ## plot pdf plot(density(y), col="blue") lines(grid, pdf, col="red") ## plot cdf plot(ecdf(y), col="blue") lines(grid, cdf, col="red") ## Example 2: Show parameter recycling ## these pairs give us the same results dcoga_approx(1:5, c(1, 2), c(1, 3, 4, 2, 5)) dcoga_approx(1:5, c(1, 2, 1, 2, 1), c(1, 3, 4, 2, 5)) pcoga_approx(1:5, c(1, 3, 5, 2, 2), c(3, 5)) pcoga_approx(1:5, c(1, 3, 5, 2, 2), c(3, 5, 3, 5, 3))
## Example 1: Correctness check set.seed(123) ## do grid y <- rcoga(100000, c(3,4,5), c(2,3,4)) grid <- seq(0, 15, length.out=100) ## calculate pdf and cdf pdf <- dcoga_approx(grid, shape=c(3,4,5), rate=c(2,3,4)) cdf <- pcoga_approx(grid, shape=c(3,4,5), rate=c(2,3,4)) ## plot pdf plot(density(y), col="blue") lines(grid, pdf, col="red") ## plot cdf plot(ecdf(y), col="blue") lines(grid, cdf, col="red") ## Example 2: Show parameter recycling ## these pairs give us the same results dcoga_approx(1:5, c(1, 2), c(1, 3, 4, 2, 5)) dcoga_approx(1:5, c(1, 2, 1, 2, 1), c(1, 3, 4, 2, 5)) pcoga_approx(1:5, c(1, 3, 5, 2, 2), c(3, 5)) pcoga_approx(1:5, c(1, 3, 5, 2, 2), c(3, 5, 3, 5, 3))
Density, and distribution function of convolution of *two* gamma
distributions. These two functions still give us the exact density and
distribution function value, but which are much faster than dcoga
and pcoga
. **So, we recommend these two functions for two variables
case.** The algorithm of these two functions comes from Mathai, A.M. (1982).
dcoga2dim(x, shape1, shape2, rate1, rate2) pcoga2dim(x, shape1, shape2, rate1, rate2)
dcoga2dim(x, shape1, shape2, rate1, rate2) pcoga2dim(x, shape1, shape2, rate1, rate2)
x |
Quantiles. |
shape1 , shape2
|
Shape parameters for the first and second gamma distributions, both shape parameters should be larger than or equal to 0, with at least one non-zero. |
rate1 , rate2
|
Rate parameters for the first and second gamma distributions, both rate parameters should be larger than 0. |
Chaoran Hu
Mathai, A.M.: Storage capacity of a dam with gamma type inputs. Ann. Inst. Statist.Math. 34, 591-597 (1982)
## Example 1: Correctness check set.seed(123) ## do grid y <- rcoga(100000, c(3,4), c(2,3)) grid <- seq(0, 15, length.out=100) ## calculate pdf and cdf pdf <- dcoga2dim(grid, 3, 4, 2, 3) cdf <- pcoga2dim(grid, 3, 4, 2, 3) ## plot pdf plot(density(y), col="blue") lines(grid, pdf, col="red") ## plot cdf plot(ecdf(y), col="blue") lines(grid, cdf, col="red") ## Example 2: Comparison with `dcoga` and `pcoga` ## these pairs give us the same results dcoga(1:5, c(1, 2), c(3, 4)) dcoga2dim(1:5, 1, 2, 3, 4) pcoga(1:5, c(1, 3), c(3, 5)) pcoga2dim(1:5, 1, 3, 3, 5)
## Example 1: Correctness check set.seed(123) ## do grid y <- rcoga(100000, c(3,4), c(2,3)) grid <- seq(0, 15, length.out=100) ## calculate pdf and cdf pdf <- dcoga2dim(grid, 3, 4, 2, 3) cdf <- pcoga2dim(grid, 3, 4, 2, 3) ## plot pdf plot(density(y), col="blue") lines(grid, pdf, col="red") ## plot cdf plot(ecdf(y), col="blue") lines(grid, cdf, col="red") ## Example 2: Comparison with `dcoga` and `pcoga` ## these pairs give us the same results dcoga(1:5, c(1, 2), c(3, 4)) dcoga2dim(1:5, 1, 2, 3, 4) pcoga(1:5, c(1, 3), c(3, 5)) pcoga2dim(1:5, 1, 3, 3, 5)
pcoga2dim
The difference of distribution functions of convolution of two gamma
distributions between consecutive neighbors of shape parameter. This
function evaluate the value of 'pcoga2dim(x, shape1, shape2, rate1, rate2)
- pcoga2dim(x, shape1 + 1, shape2, rate1, rate2)' with higher efficiency
(this function is much more faster than call pcoga2dim
two-times).
pcoga2dim_diff_shape(x, shape1, shape2, rate1, rate2)
pcoga2dim_diff_shape(x, shape1, shape2, rate1, rate2)
x |
Quantiles. |
shape1 , shape2
|
Shape parameters of the first and second gamma distributions, both shape parameters should be larger than or equal to 0. |
rate1 , rate2
|
Rate parameters of the first and second gamma distributions, both rate parameters should be larger than 0. |
Chaoran Hu
## these pairs give us the same results pcoga2dim_diff_shape(3,2,4,5,4) pcoga2dim(3,2,4,5,4) - pcoga2dim(3,3,4,5,4) pcoga2dim_diff_shape(3,0,4,3,5) pgamma(3,4,5) - pcoga2dim(3,1,4,3,5) pcoga2dim_diff_shape(3,6,0,5,4) pgamma(3,6,5) - pgamma(3,7,5) pcoga2dim_diff_shape(3,0,0,4,5) 1 - pgamma(3,1,4)
## these pairs give us the same results pcoga2dim_diff_shape(3,2,4,5,4) pcoga2dim(3,2,4,5,4) - pcoga2dim(3,3,4,5,4) pcoga2dim_diff_shape(3,0,4,3,5) pgamma(3,4,5) - pcoga2dim(3,1,4,3,5) pcoga2dim_diff_shape(3,6,0,5,4) pgamma(3,6,5) - pgamma(3,7,5) pcoga2dim_diff_shape(3,0,0,4,5) 1 - pgamma(3,1,4)