vendredi 4 octobre 2019

Generating groups of skewed size but whose elements add to a fixed sum

I have some fixed number of people (e.g. 1000). I would like to split these 1000 people into some random number of classes Y (e.g. 5), but not equally. I want them to be distributed unevenly, according to some probability distribution that is heavily skewed (something like a power-law distribution).

My intuition is that I need to generate a distribution of probabilities that is (1) skewed and (2) which also adds up to 1.

My ad hoc solution was to generate random numbers from a power law distribution, multiply these by some scalar that ensures these add up to something close to my target number, adjust my target number to that new number, and then split accordingly.

But it seems awfully ad hoc. What's a better approach?

require(poweRlaw)
x<-1000
y<-10
y_sizes<-rpldis(10,xmin=5,alpha=2,discrete_max=x)
y_sizes<-round(y_sizes * x/sum(y_sizes))
newx<-y_sizes #newx approx = x
people<-1:x
groups<-cut(
  people,
  c(0,cumsum(y_sizes))
) %>% as.numeric
data.frame(
  people=people,
  group=groups
)



Aucun commentaire:

Enregistrer un commentaire