How to generate multiple Vectors with random variables but sum 1
9 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Hello Folks, i want to create multiple Vectors with random variables (btw. 0 and 1) but sum 1
for example [0 1 0 0] or [0.7 0.2 0 0.1] and so on
i want to scatterplot a large number of random portfolios and therefore i need random weights.
i'm sure there is an easy way but since i'm new to matlab and after looking for a long time i figured i ask the community.
thanks for your help.
0 Kommentare
Akzeptierte Antwort
the cyclist
am 20 Jan. 2012
Use Roger Stafford's solution from the FEX: http://www.mathworks.com/matlabcentral/fileexchange/9700-random-vectors-with-fixed-sum
The method assures good statistical properties for the marginal probabilities, and a uniform distribution across the joint probability space. All good things.
If you download Roger's code, I suggest you run this test to compare methods. (I'll post a figure with the result later, if I can.)
% Roger Stafford's solution
B1 = randfixedsum(10,10000,1,0,1);
% "Easy method" [Please don't use!]
A = rand(100000,10);
B2 = bsxfun(@rdivide,A,sum(A,2));
% Compare the marginal distributions of one variable
figure
subplot(2,1,1),hist(B1(1,:),25)
subplot(2,1,2),hist(B2(:,1),25)
0 Kommentare
Weitere Antworten (1)
Sean de Wolski
am 20 Jan. 2012
Easiest way is to normalize by the sum:
A = rand(1,10);
B=A./sum(A);
sum(B)
However, there are some things that make this non-truly random. If you care:
3 Kommentare
Sean de Wolski
am 20 Jan. 2012
It really depends on the use case. For a homework problem, I would take the easy way out.
Siehe auch
Kategorien
Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!