How to generate a random data vector that follows a constraint

조회 수: 2 (최근 30일)
I want to generate random data vector where that follows this constrainst. where and is a constant. I would appreciate any help.

채택된 답변

Bruno Luong
Bruno Luong 2019년 9월 29일
편집: Bruno Luong 2019년 9월 29일
This will generate uniform distribution (within to polytope)
N = 10;
Etotal = 0;
E = -log(rand(N+1,1));
E = E(1:N,:)./sum(E,1);
  댓글 수: 2
Bruno Luong
Bruno Luong 2019년 9월 29일
편집: Bruno Luong 2019년 9월 29일
Here is a comparison of distribution with the two other methods proposed below to show the issue of non-uniformity if one doesn't pay attention
N = 2;
Etotal = 1;
p = 3e3;
Ebias = rand(1,p) .* randfixedsum(N,p,Etotal,0,Etotal);
E = rand(1,p).^(1/N)*Etotal .* randfixedsum(N,p,1,0,1);
E2 = -log(rand(N+1,p)); E2 = E2(1:N,:)./sum(E2,1);
subplot(2,2,1)
plot(Ebias(1,:),Ebias(2,:),'.');
title('rand * randfixedsum')
axis equal
subplot(2,2,2)
plot(E(1,:),E(2,:),'.');
axis equal
title('sqrt(rand) * randfixedsum')
subplot(2,2,3)
plot(E2(1,:),E2(2,:),'.');
axis equal
title('exponential method')
Saifullah Khalid
Saifullah Khalid 2019년 9월 30일
Bruno Luong, thank very much for detailed answer and the insight.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Walter Roberson
Walter Roberson 2019년 9월 29일
Look in the File Exchange for Roger's randfixedsum(). Generate a vector with fixed sum . Multiply all of the elements by rand() to implement the <= part. (Though you might want to worry about the difficulty that rand() is never exactly 1, so if you generate a sum exactly equal to and multiply by rand() then the result can never exactly total
  댓글 수: 3
Walter Roberson
Walter Roberson 2019년 9월 29일
I was thinking of
rand() * randfixedsum(N,1,Etotal,0,Etotal)
but your comment might still apply.
Bruno Luong
Bruno Luong 2019년 9월 29일
편집: Bruno Luong 2019년 9월 29일
Both give the same non-uniform pdf
The "correct" one is
E = rand()^(1/N)*Etotal * randfixedsum(N,1,1,0,1);

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Solver Outputs and Iterative Display에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by