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. Prints their
average in the first line with 5 decimal digit precision and the relative frequency of the number
\(1.5\) in the second line with 3-digit precision .
- 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 the third line the exact value of
\(\mathbb{E}(X)\) with 5-digit precision, and \(\mathbb{P}(X=1.5)\) in the fourth line as a fraction (Help on converting to fraction). For calculating
\(\mathbb{E}(X)\) use
module sympy
or scipy.
- The file to be uploaded should be called CDF.py.
- 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!