user3203275
user3203275

Reputation: 305

Generate random weights vector with fixed sum

What I need: I want to create a 10 size vector with random weights. The sum of weights should be equal to 1.

Why I need that: I want to finally calculate the returns of stocks of a randomly diversified portfolio of assets.

Mock Solution: [0.01 0.02 0.07 0.10 0.10 0.05 0.05 .......]. Sum of all elements is 1.

Upvotes: 0

Views: 710

Answers (2)

Stéphane Laurent
Stéphane Laurent

Reputation: 84529

The Dirichlet distribution exactly does what you want.

igraph::sample_dirichlet(1, alpha = rep(1, 10L))

Upvotes: 2

Anita
Anita

Reputation: 64

Try something like this

x <- runif(10)
weights <- x/sum(x)

Upvotes: 1

Related Questions