Given the probability density function
\begin{equation} f(x)=\begin{cases} kx, & \text{$0 \leq x \leq 1$}.\\ 0, & \text{otherwise}. \end{cases} \end{equation}
I've found that k = 2, so $$E(X) = \int_{-\infty}^{\infty} xf(x)\,dx = \int_{0}^{1} 2x^2\,dx = \frac{2}{3}$$
$$E(X^2) = \int_{-\infty}^{\infty} x^2f(x)\,dx = \int_{0}^{1} 2x^3\,dx = \frac{1}{2}$$
$$Var(X) = E(X^2) - E(X)^2 = \frac{1}{2} - \frac{2}{3}^2 = \frac{1}{18}$$
But when I generated some random variables using python, this is what I have with expectation:
import numpy as np
N = 100_000
X = np.random.uniform(size=N, low=0, high=1)
Y = [2*x for x in X]
np.mean(Y) # 1.00221 <- not equal to 2/3
np.var(Y) # 0.3323 <- not equal to 1/18
What am I doing wrong here?
Monte Carlo Simulation
To approximate the integral of some function of $x$, say, $g(x)$, over $S = [0, 1]$, using Monte Carlo simulation, you
The result of step 2 is the approximation of the integral.
Definitions of Expected Value and Variance
Given a continuous random variable $X$ with pdf $f(x)$ and set of possible values $S$,
Approximating Expected Value and Variance using Monte Carlo Simulation
Code
Adapting your method:
Output
Instead of making
YandElists, a much better approach isY,Ein this case arenumpyarrays. This approach is much more efficient. Try makingN = 100_000_000and compare the execution times of both methods. The second should be much faster.