Solved – How to sample from Cantor distribution

What would be the best way to sample from Cantor distribution? It only has cdf and we can't invert it.

Easy: sample from a Uniform$(0,1)$ distribution and recode from binary to ternary, interpreting each "1" as a "2". (This is the inverse probability transform approach: it does indeed invert the CDF!)

Figure

Here is an R implementation, written in a way that ought to port readily to almost any computing environment.

binary.to.ternary <- function(x) {   y <- 0   x <- round(2^52 * x)   for (i in 1:52) {     y <- y + 2*(x %% 2)     y <- y/3     x <- floor(x/2)   }   y }  n <- 1000 x <- runif(n) y <- binary.to.ternary(x) plot(ecdf(y), pch=".") 

Similar Posts:

Rate this post

Leave a Comment