Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

Views expressed in these public forums are not endorsed by Drexel University or The Math Forum.


Math Forum » Discussions » Software » comp.soft-sys.matlab

Topic: Generate random numbers with fixed sum and different constraints
Replies: 25   Last Post: Sep 17, 2013 2:28 AM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
Bruno Luong

Posts: 8,830
Registered: 7/26/08
Re: Generate random numbers with fixed sum and different constraints
Posted: Nov 19, 2012 1:39 AM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

Sorry, I miss the definition of the variable 'tol' in my previous posted code; here we go again:

% lower + upper bounds
lo = [1 0 2 1 0];
up = [5 6 4 4 2];
% target sum
s = 15;
% number of samples
n = 1000;

%% Engine
m = length(lo);
slo = sum(lo);
sup = sum(up);

% A particular solution, x0
x0 = interp1([slo; sup], [lo; up], s);
if any(isnan(x0))
error('no solution exists')
end

% feasible set: S = { x=x0+A*y : A*y <= b }
x0 = x0(:);
B = null(ones(size(lo)));
A = [-B; B];
b = [x0-lo(:); up(:)-x0];

% FEX: http://www.mathworks.com/matlabcentral/fileexchange/30892
tol = 1e-6;
[V,nr,nre] = lcon2vert(A,b,[],[],tol);

% Split S in simplex
T = delaunayn(V);
P = V(T,:);
P = reshape(P, [size(T) m-1]);
P = permute(P, [3 2 1]);
np = size(P,3);

% Compute the volume of simplexes
Q = bsxfun(@minus, P, P(:,end,:));
Q(:,end,:) = [];
vol = arrayfun(@(k) det(Q(:,:,k)), 1:np);
vol = abs(vol);

% generate n samples (i) of v with probability ~ vol
vol = vol / sum(vol);
c = cumsum(vol); c(end)=1;
[~, i] = histc(rand(1,n),[0 c]);

% Random rarycentric coordinates
% FEX: http://www.mathworks.com/matlabcentral/fileexchange/9700
w = randfixedsum(m,n,1,0,1);

% Put together
V = P(:,:,i);
y = zeros(m-1, n);
for k=1:n
y(:,k) = V(:,:,k)*w(:,k);
end
% final result, m x n array of random vector
x = bsxfun(@plus, x0, B*y);

% Bruno


Date Subject Author
11/14/12
Read Generate random numbers with fixed sum and different constraints
Dmitrey Yershov
11/17/12
Read Re: Generate random numbers with fixed sum and different constraints
Greg Heath
11/17/12
Read Re: Generate random numbers with fixed sum and different constraints
Roger Stafford
11/17/12
Read Re: Generate random numbers with fixed sum and different constraints
Bruno Luong
11/17/12
Read Re: Generate random numbers with fixed sum and different constraints
Roger Stafford
11/18/12
Read Re: Generate random numbers with fixed sum and different constraints
Dmitrey Yershov
11/18/12
Read Re: Generate random numbers with fixed sum and different constraints
Bruno Luong
11/19/12
Read Re: Generate random numbers with fixed sum and different constraints
Bruno Luong
11/19/12
Read Re: Generate random numbers with fixed sum and different constraints
Dmitrey Yershov
11/19/12
Read Re: Generate random numbers with fixed sum and different constraints
Bruno Luong
11/19/12
Read Re: Generate random numbers with fixed sum and different constraints
Dmitrey Yershov
11/19/12
Read Re: Generate random numbers with fixed sum and different constraints
Bruno Luong
11/19/12
Read Re: Generate random numbers with fixed sum and different constraints
Bruno Luong
11/19/12
Read Re: Generate random numbers with fixed sum and different constraints
Roger Stafford
11/20/12
Read Re: Generate random numbers with fixed sum and different constraints
Bruno Luong
11/20/12
Read Re: Generate random numbers with fixed sum and different constraints
Bruno Luong
11/20/12
Read Re: Generate random numbers with fixed sum and different constraints
Roger Stafford
11/21/12
Read Re: Generate random numbers with fixed sum and different constraints
Bruno Luong
11/21/12
Read Re: Generate random numbers with fixed sum and different constraints
james bejon
11/20/12
Read Re: Generate random numbers with fixed sum and different constraints
james bejon
9/17/13
Read Re: Generate random numbers with fixed sum and different constraints
JS Hong
9/17/13
Read Re: Generate random numbers with fixed sum and different constraints
JS Hong
11/19/12
Read Re: Generate random numbers with fixed sum and different constraints
David Epstein
11/19/12
Read Re: Generate random numbers with fixed sum and different constraints
Roger Stafford
11/20/12
Read Re: Generate random numbers with fixed sum and different constraints
Bruno Luong
11/17/12
Read Re: Generate random numbers with fixed sum and different constraints
Greg Heath

Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.