Discrete Probability Simulations in R

Probability
Simulations
Author

Francois de Ryckel

Published

November 14, 2023

Modified

November 14, 2022

The idea behind this post is to collect various numerical methods to simulate discrete probability problems.

Expectation of a uniform variable.

Question: what is the expectation if one square a number that is picked at random out of a hat (with replacement) that contains the numbers 1 to 100.

expec <- mean(sample(1:100, size = 1000000, replace = TRUE)^2)
print(expec)
[1] 3384.936

The calculated expectation should be: \[\sum_{x=1}^{100} x^2 P(X=x) = \sum_{x=1}^{100} x^2 \frac{1}{n} = \frac{101 \cdot 201}{6} = 3383.5\]

We could connect this to the Jensen’s inequality (as we are dealing with a convex function) and show that indeed the expectation of the function is greater than the function of the expectation: \(\mathbb{E}[f(X)] \geq f(\mathbb{E}[X])\)

exp_square <- mean(sample(1:100, 1000000, replace = TRUE)^2)
square_exp <- (mean(sample(1:100, 1000000, replace = TRUE)))^2

print(exp_square)
[1] 3389.425
print(square_exp)
[1] 2551.491