How to build a probability function of the density function?

Suppose A follows an exponential distribution; The Gamma distribution follows. How to build a PDF file 0.5 * (A + B)

+6
r
source share
4 answers

This is pretty straight forward using the distr package:

library(distr) A <- Exp(rate=3) B <- Gammad(shape=2, scale=3) conv <- 0.5*(A+B) plot(conv) plot(conv, to.draw.arg=1) 

Edit JD Long

The resulting plot is as follows: alt text

+8
source share

If you're just looking for a quick schedule, I usually take a quick and dirty approach to simulation. I do some practical jokes, slam the gaussian density into draws and plot that a bad boy:

 numDraws <- 1e6 gammaDraws <- rgamma(numDraws, 2) expDraws <- rexp(numDraws) combined <- .5 * (gammaDraws + expDraws) plot(density(combined)) 

the output should look something like this:

alt text

+5
source share

Here is an attempt to convolution (which @Jim Lewis refers to) in R. Note that there are probably much more efficient ways to do this.

 lower <- 0 upper <- 20 t <- seq(lower,upper,0.01) fA <- dexp(t, rate = 0.4) fB <- dgamma(t,shape = 8, rate = 2) ## C has the same distribution as (A + B)/2 dC <- function(x, lower, upper, exp.rate, gamma.rate, gamma.shape){ integrand <- function(Y, X, exp.rate, gamma.rate, gamma.shape){ dexp(Y, rate = exp.rate)*dgamma(2*XY, rate = gamma.rate, shape = gamma.shape)*2 } out <- NULL for(ix in seq_along(x)){ out[ix] <- integrate(integrand, lower = lower, upper = upper, X = x[ix], exp.rate = exp.rate, gamma.rate = gamma.rate, gamma.shape = gamma.shape)$value } return(out) } fC <- dC(t, lower=lower, upper=upper, exp.rate=0.4, gamma.rate=2, gamma.shape=8) ## plot the resulting distribution plot(t,fA, ylim = range(fA,fB,na.rm=TRUE,finite = TRUE), xlab = 'x',ylab = 'f(x)',type = 'l') lines(t,fB,lty = 2) lines(t,fC,lty = 3) legend('topright', c('A ~ exp(0.4)','B ~ gamma(8,2)', 'C ~ (A+B)/2'),lty = 1:3) 
+2
source share

I am not an R programmer, but it may be useful to know that for independent random variables with PDF files f 1 (x) and f 2 (x), the PDF of the sum of two variables is given by the convolution f 1 * f 2 (x) of the two input PDF files.

+1
source share

All Articles