1
$\begingroup$

Possible duplicate, but not sure: Generating random numbers with skewed distribution

So, I would like to generate integers bewteen $x$ and $y$ with a skewed distribution of $n$. What is the best way to do this?

I believe it is different to the link above, because it only chooses between two numbers, $1$ and $46$, but I want a larger range (i.e. between 1-100), but I don't know how.

$\endgroup$
  • $\begingroup$ Generally the idea suggested in that link is about the probability integral transformation, which at least in principle can be used to generate any distribution you want, provided that you have the cumulative distribution function given and a source of uniform random numbers. If you have something else given, like the probability mass function, then you need to construct the cumulative distribution function in order to apply the probability integral transformation. $\endgroup$ – Ian Oct 19 '17 at 19:03
  • $\begingroup$ @Ian, it may sound stupid, but apart from the words "the", that","link" and other simple words, I did not get an inkling on what you were on about. I am very sorry. I think you are saying that that link has the structure of the question I am writing. However, I need to construct my own algorithm like that in order to fulfill my needs $\endgroup$ – VortexYT Oct 19 '17 at 19:20
  • $\begingroup$ If you don't know what a "cumulative distribution function" is, then you have some studying to do on basic probability before getting into any kind of programming with it. If you know that much, then you can look up the probability integral transformation. It has a perfectly good Wikipedia article. $\endgroup$ – Ian Oct 19 '17 at 19:42
4
$\begingroup$

Just to get you started:

Let $X$ have density function $f(x) = 2(1-x),$ for $0 < x < 1.$. That is a right-skewed continuous distribution taking values in $(0,1).$ The distribution of $X$ is called $\mathsf{Beta}(1,2);$ you can look at the Wikipedia article to see the general form of the family of distributions.

Then $Y = 100X$ is a continuous distribution taking values in $(0,100).$ You can use the ceiling function to get a discrete distribution taking integer values $1, 2, \dots, 100.$

How to generate values of $X:$ The CDF of $X$ is $F(x) = (1-x)^2,$ for $0 < x < 1.$ Suppose you have a pseudorandom generator that essentially give you random observations from $U \sim \mathsf{UNIF}(0,1).$ Then you can set $U = F(X)$ and solve for $X$ in terms of $U$ to get $X = F^{-1}(U) = 1 - \sqrt{U}.$

In R statistical software the function runif(10^6) generates a vector of a million observations that are essentially sampled from $\mathsf{Unif}(0,1)$ so the program below generates and plots a histogram of a million pseudo-realizations of $X.$ [In R this could be done more easily by using the built-in function for generating beta random variables: rbeta(m, 1, 2).]

m = 10^6;  u = runif(m)
x = 1 - sqrt(u)          

par(mfrow=c(1,2))  # enables 2-panel plots
 hist(u, prob=T, col="skyblue2", main="Realizations of UNIF(0,1)")
   curve(dunif(x), -.2, 1.2, col="blue", lwd=2, n=1001, add=T)
 hist(x, prob=T, col="skyblue2", main="Realizations of BETA(1,2)")
   curve(dbeta(x,1,2), -.2, 1.2, col="blue", lwd=2, n=1001, add=T)
par(mfrow=c(1,1))  # returns to single panel plots

enter image description here

Then a discrete distribution can be obtained by taking ceilings, as mentioned above. For a graph that will display clearly at the resulution available here, I have used values 1 through 20 (instead of 1 through 100).

k = 20
y = ceiling(k*x)
cutp=seq(0, k, by=.1)
hist(y, prob=T, br=cutp, main="Right-Skewed Discrete Distribution")

enter image description here

If you want a more extreme skew, you can use a different member of the beta family of distributions. [But then the relationship to uniform random variables is not so transparent.]

m = 10^6;  x = rbeta(m, 1, 4);  y = ceiling(20*x);  cutp=seq(0, 20, by=.1)
hist(y, prob=T, br=cutp, main="Right-Skewed Discrete Distribution")

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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