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

**Contents**hide

#### Best Answer

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!)

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:

- Solved – Does there exist any univariate distribution that we can’t sample from
- Solved – Inverse Transformation Sampling with Gaussian
- Solved – Generating Data from Arbitrary Distribution
- Solved – Generating Data from Arbitrary Distribution
- Solved – How to determine the best fit normal distribution from this information