Simple probabilities in R

R has functions to compute probabilities based on most common distributions.\\\\

If $X$ is a random variable with a known distribution, then R can typically compute values of the cumulative distribution function or:
$$F(x)=P[X \leq x]$$
Examples
\begin{xmpl}

If $X \sim b(n,p)$ has binomial distribution, i.e.
$$ P(X = x) = {n \choose x}p^x(1-p)^{n-x},$$
then cumulative probabilities can be computed with $pbinom$, e.g.
\begin{lstlisting}
pbinom(5,10,0.5)
\end{lstlisting}

gives $$P[X \leq 5] = 0.623$$
where $$X \sim b(n=10,p= \frac{1}{2}).$$

This can also be computed by hand. Here we have $n=10$, $p=1/2$ and the probability
$P[X \leq 5] $ is obtained by adding up the individual probabilities,
$P[X =0]+P[X =1]+P[X =2]+P[X =3]+P[X =4]+P[X =5]$

$$
P[X \leq 5] = \sum_{x=0}^5 {10\choose x} \frac{1}{2}^x\frac{1}{2}^{10-x}.
$$
This becomes
$$
P[X \leq 5] = {10\choose 0} \frac{1}{2}^0\frac{1}{2}^{10-0} +{10\choose 1} \frac{1}{2}^1\frac{1}{2}^{10-1}+{10\choose 1} \frac{1}{2}^2\frac{1}{2}^{10-2}+{10\choose 3} \frac{1}{2}^3\frac{1}{2}^{10-3}+{10\choose 4} \frac{1}{2}^4\frac{1}{2}^{10-4}+{10\choose 5} \frac{1}{2}^5\frac{1}{2}^{10-5}
$$
or
$$
P[X \leq 5] = {10\choose 0} \frac{1}{2}^{10} +{10\choose 1} \frac{1}{2}^{10}+{10\choose 1} \frac{1}{2}^{10}+{10\choose 3} \frac{1}{2}^{10}+{10\choose 4} \frac{1}{2}^{10}+{10\choose 5} \frac{1}{2}^{10}=\frac{1}{2}^{10}\left[1+10+45+...\right ].
$$


Furthermore,
\begin{lstlisting}
pbinom(10,10,0.5)
[1] 1
\end{lstlisting}
and
\begin{lstlisting}
pbinom(0,10,0.5)
[1] 0.0009765625
\end{lstlisting}



It is sometimes of interest to compute $P[X=x]$ in this case, and this is given by the $dbinom$ function, e.g.

\begin{lstlisting}
dbinom(1,10,0.5)
[1] 0.009765625
\end{lstlisting}

or $ \frac{10}{1024}$

\end{xmpl}
\begin{xmpl}

Suppose $X$ has a uniform distribution between $0$ and $1$, i.e. $X \sim U(0,1)$. Then the $punif$ function will return probabilities of the form $$ P[X \leq x]= \int_{-\infty}^{x} f(t)dt= \int_{0}^{x} f(t)dt$$ where $f(t)=1$ if $0 \leq t \leq 1$
and $f(t)=0$.
For example:
\begin{lstlisting}
punif(0.75)
[1] 0.75
\end{lstlisting}

To obtain $P[a \leq X \leq b],$ we use $punif$ twice, e.g.
\begin{lstlisting}
punif(0.75)-punif(0.25)
[1] 0.5
\end{lstlisting}
\end{xmpl}