Package 'coga'

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

Help Index


coga: Convolution of independent 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.

coga functions

dcoga, pcoga, rcoga, dcoga2dim, pcoga2dim, pcoga2dim_diff_shape, dcoga_approx, and pcoga_approx.

Author(s)


Convolution of Gamma Distributions (Exact Method).

Description

Density, distribution function, and random generation for convolution of gamma distributions. Convolution of independent Gamma random variables is Y=X1+...+XnY = X_{1} + ... + X_{n}, where Xi,i=1,...,nX_{i}, i = 1, ..., n, 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 n>=3n >= 3.**

Usage

dcoga(x, shape, rate)

pcoga(x, shape, rate)

rcoga(n, shape, rate)

Arguments

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.

Author(s)

Chaoran Hu

References

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.

Examples

## 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))

Convolution of Gamma distribuitons (Approximation Method)

Description

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.**

Usage

dcoga_approx(x, shape, rate)

pcoga_approx(x, shape, rate)

Arguments

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.

Author(s)

Chaoran Hu

References

Barnabani, M. (2017). An approximation to the convolution of gamma distributions. Communications in Statistics - Simulation and Computation 46(1), 331-343.

Examples

## 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))

Convolution of Two Gamma Distributions (Exact Method).

Description

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).

Usage

dcoga2dim(x, shape1, shape2, rate1, rate2)

pcoga2dim(x, shape1, shape2, rate1, rate2)

Arguments

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.

Author(s)

Chaoran Hu

References

Mathai, A.M.: Storage capacity of a dam with gamma type inputs. Ann. Inst. Statist.Math. 34, 591-597 (1982)

Examples

## 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)

Recurrence Identity of Shape Parameter for pcoga2dim

Description

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).

Usage

pcoga2dim_diff_shape(x, shape1, shape2, rate1, rate2)

Arguments

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.

Author(s)

Chaoran Hu

Examples

## 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)