7. Simulate random variable with
given cumulative distribution function
- The cumulative distribution function of a random variable
\(X\) on the interval \([0,2]\) is
\[
F_X(x) =
\begin{cases}
2x^2/9, &\text{if \(x\in[0,1.5]\),}\\
-x^2+4x-3, &\text{if \(x\in(1.5,2]\).}
\end{cases}
\]
Write a Python function that
simulates a random variable with such a cumulative distribution
function \(F_X\). Write a program that generates \(1000\) such
random numbers and prints two numbers in the first line: their
average and the relative frequency of the number
\(1.5\).
- In this task you have to make a little calculation: determine
the exact values that come out from the simulation, that is
print out in the second line the exact value of
\(\mathbb{E}(X)\), and \(\mathbb{P}(X=1.5)\). Calculating
\(\mathbb{E}(X)\) use
module sympy
or scipy.
- Help: to calculate the expected value from CDF but without the
probability distribution function is possible. Namely, if
\(X\ge0\), then
\(\mathbb{E}(X)=\int_0^\infty 1-F_X(x)\,\mathrm{d}x\) (see
e.g. in stackexchange
the second and last answer).
- Help: for solving this problem you have to understand, how to
generate a random variable from uniform distribution using the inverse
CDF
(see
Wikipedia). So first calculate the inverse of \(F_X\), and
think about what shoud be done at the discontinuities (you
have to modify the inverse a littlebit).
- The aim of the task is to understand the function of a random
variable, for example to understand the random variable
\(F_X(X)\), and to use this to simulate a given random
variable. The random variable in this task is neither
continuous nor discrete!