vivek vivek - 3 months ago 15
R Question

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

I have got

n>2
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
T=W+X+Y+Z
. 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
P(t<T)
from this samples I have drawn. For example is there a easy way I can calculate
P(0.5<T)
in R. My priority here is to get probability first even if getting the density function is not possible.
Thanks

Answer

Consider the ecdf function:

set.seed(123)
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.

Comments