I want to compute the quantile function of a Bin(4, 1/3) random variable and plot it with R, but I don't know where to start from.

Any help or hint is highly appreciated.

  • 1
    $\begingroup$ For just plotting quantile functions in R, note that the common distributions all have four functions (d...,p...,q..,r...) defined for them, the density/pmf, the cdf, the quantile function (i.e. the inverse cdf) and random variate generation. See the help ?qbinom, which identifies the specific function you seek and its arguments). $\endgroup$
    – Glen_b
    Commented Oct 30, 2022 at 5:30
  • 1
    $\begingroup$ The quantile function for discrete variables can be tricky. The quantile function is defined as the inverse of the cumulative distribution function (CDF), but for discrete variables this CDF is non-injective and non-surjetive and can not be inverted. You have problems like the function not having a continuous domain and a single value maps to many different ones. But there are alternative definitions: en.m.wikipedia.org/wiki/… $\endgroup$ Commented Oct 30, 2022 at 8:27
  • 1
    $\begingroup$ curve(qbinom(x,4,1/3), from=0, to=1) is quick and dirty, though the vertical steps would be better not shown so curve(qbinom(x,4,1/3), from=0, to=1, type="p") gives a different version $\endgroup$
    – Henry
    Commented Oct 30, 2022 at 17:47

1 Answer 1


Note that the probability mass function is

$$ f(x) = P(X = x) = \begin{cases} \frac{16}{81}&\text{if } x=0\\ \frac{32}{81}&\text{if } x=1\\ \frac{24}{81}&\text{if } x=2\\ \frac{8}{81}&\text{if } x=3\\ \frac{1}{81}&\text{if } x=4, \end{cases} $$ and the cumulative distribution function is

$$ F(x) = \begin{cases} 0 & \text{if } x<0\\ \frac{16}{81} &\text{if } 0\leq x < 1\\ \frac{48}{81} &\text{if } 1\leq x < 2\\ \frac{72}{81} &\text{if } 2\leq x < 3\\ \frac{80}{81} &\text{if } 3\leq x < 4\\ 1 &\text{if } 4\leq x. \end{cases} $$

The quantile function $Q$ is thus

$$ Q(p) = \begin{cases} 0 & \text{if } 0< p \leq \frac{16}{81}\\ \cdots & \cdots\\ 4 & \text{if } \frac{80}{81}< p. \end{cases} $$

I leave the rest, e.g. the $\cdots$, to you.

enter image description here

R code

plot(1, xlim=c(0,1), ylim=c(-1,5), type="n",
     xlab= "p", ylab= "Q(p)", main = "X ~ Bin(4,1/3)")
abline(v=c(0,1), lwd=1, lty=2, col="lightgray")
segments(x0=0,x1=16/81,y0=0,y1=0, lwd=2)
points(x=16/81,y=0, pch=20, cex=1.4)
segments(x0=16/81,x1=48/81,y0=1,y1=1, lwd=2)
points(x=48/81,y=1, pch=20, cex=1.4)
segments(x0=48/81,x1=72/81,y0=2,y1=2, lwd=2)
points(x=72/81,y=2, pch=20, cex=1.4)
segments(x0=72/81,x1=80/81,y0=3,y1=3, lwd=2)
points(x=80/81,y=3, pch=20, cex=1.4)
segments(x0=80/81,x1=81/81,y0=4,y1=4, lwd=2)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.