 # Generate probability vectors

I am interested in generating a list of n random numbers that add up to 1 and are all non-negative, that is, a probability vector. As an example, I have tried the following simple approach:

``````n=10
v=rbeta(n,shape1=2,shape2=5)
v/sum(v)
``````

However, I don't think this approach is correct, given that the number of degrees of freedom is n-1, instead of n.

Does anyone know how can this be done more correctly?

Thanks in advance for any help!

The first thing that came to my mind was the dirichlet distribution which is a multivariate beta distribution. This package has a function `rdirichlet` to randomly generate the vectors.

1 Like

Oh and the dirichlet generation is equivalent to doing what you did - using the beta and dividing by the sum of independent beta draws. It will have `n-1` degrees of freedom because of the constraint it sums to 1.

1 Like

Thanks for the answer! It was really helpful. I didn't know about the Dirichlet distribution, pretty interesting

x <- seq( from = 1, to = 100, by = 1)
s <- sample(x, 5)
s
sum_s <- sum(s)
s <- s/sum_s
s

Thanks for the answer. I guess the method you propose is equivalent, just employing a uniform distribution instead of beta distributions

I was surprised to see the beta distribution here. You didn't mention anything about your data to suggest anything about the shape of the distribution.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.