2
$\begingroup$

Let $X_1, X_2, \dots, X_n \sim U([1,3])$. Write a code in $\mathcal{R}$ to find an approximate value of $$\mathbb{P}\left( \sum_{k=1}^n X_k< a \right)$$

I have seen multiple questions here where people asked about the sum of two or three uniformly distributed variables. There was one more that suggested to use Irwin-Hall distribution, but this distribution only works for $X_k \sim U([0,1])$.

My attempt so far was this (an example with $a=3$) :

k = 5
for(i in 1:k){
  measurements <- runif(10, min = 1, max = 3) # take 10,000 measurements
  above.threshold <- sum((measurements) < 3) #count the number of values < 4
  rez <- above.threshold/length(measurements) # calculate the proportion of values < 4
}

This code doesn't work properly. I have calculated a couple of probabilities by hand:

$$\begin{equation} \begin{aligned} &\mathbb{P}(X_1+X_2<4)=\frac{1}{2}; \\ &\mathbb{P}(X_1+X_2+X_3<4)=\frac{1}{48}. \end{aligned} \end{equation}$$

Maybe there is someone who's familiar with $\mathcal{R}$ programming and has already faced this problem ? Thank you !

$\endgroup$
1
  • $\begingroup$ If $Y$ is $U[0,1]$ distributed, then $(b-a)X + a$ is $U[a,b]$ distributed. Conversely, if $Y$ is $U[a,b]$ distributed, then $(X - a)/(b-a)$ is $U[0,1]$ distributed. $\endgroup$
    – lmaosome
    Mar 6, 2021 at 12:59

1 Answer 1

1
$\begingroup$

Here is how you can run the simulation with R:

simulate.sum.uniform.iid <- function(k, a, min=1, max=3, N=10000) {
  set.seed(111)     # for reproducibility
  mean(replicate(N, # take N measurements
                 {
                   X <- runif(k, min = 1, max = 3) 
                   sum(X) < a
                 }))
}

$P(X_1 + X_2 < 4)$, where $X_1, X_2 \sim U(1, 3)$, prob. is $=\frac{1}{2}=0.5$

simulate.sum.uniform.iid(k = 2, a = 4)
# 0.5019

$P(X_1 + X_2 +X_3 < 4)$, where $X_1, X_2, X_3 \sim U(1, 3)$, prob. is $=\frac{1}{48}=0.02083333$

simulate.sum.uniform.iid(k = 3, a = 4)
# [1] 0.021
$\endgroup$
1
  • 1
    $\begingroup$ Thank you! Now I see where my problems were. $\endgroup$
    – Karagum
    Mar 7, 2021 at 7:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .