vivek vivek - 1 year ago 81
R Question

Extract approximate probability density function (pdf) in R from random sampling

I have got

independent continuous
Random Variables(RV)
. For example say I have
4 Uniform RVs
with different set of
Upper and lowers

W~U[-1,5], X~U[0,1], Y~[0,2], Z~[0.5,2]

I am trying to find out the approximate PDF for the sum of these RVs i.e. for
. As I don't need any closed form solution, I have sampled
1 million points
for each of them to get
1 million samples for T
. Is it possible in R to get the approximate PDF function or a way to get approximate probability of
from this samples I have drawn. For example is there a easy way I can calculate
in R. My priority here is to get probability first even if getting the density function is not possible.

Answer Source

Consider the ecdf function:

W <- runif(1e6, -1, 5)
X <- runif(1e6, 0, 1)
Y <- runif(1e6, 0, 2)
Z <- runif(1e6, 0.5, 2)

T <- Reduce(`+`, list(W, X, Y, Z))
cdfT <- ecdf(T)
1 - cdfT(0.5) # Pr(T > 0.5)
# [1] 0.997589

See How to calculate cumulative distribution in R? for more details.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download