5.0

5.0 | 7 ratings Rate this file 31 Downloads (last 30 days) File Size: 8.22 KB File ID: #9700
image thumbnail

Random Vectors with Fixed Sum

by Roger Stafford

 

19 Jan 2006 (Updated 24 Jan 2006)

Randomly and uniformly generates vectors with a specified sum and values in a specified interval.

Editor's Notes:

This file was a File Exchange Pick of the Week

| Watch this File

File Information
Description

This generates m random n-element column vectors of values, [x1;x2;...;xn], each with a fixed sum, s, and subject to a restriction a<=xi<=b. The vectors are randomly and uniformly distributed in the n-1 dimensional space of solutions. This is accomplished by decomposing that space into a number of different types of simplexes (the many-dimensional generalizations of line segments, triangles, and tetrahedra.) The 'rand' function is used to distribute vectors within each simplex uniformly, and further calls on 'rand' serve to select different types of simplexes with probabilities proportional to their respective n-1 dimensional volumes. This algorithm does not perform any rejection of solutions - all are generated so as to already fit within the prescribed hypercube.

MATLAB release MATLAB 5.2 (R10)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (8)
30 Jan 2006 John D'Errico

This took a bit of work to verify uniformity in a slice of an n-dimensional hypercube. I'm now confident that Roger has done what he claimed, having checked samplings in several different dimensions, as well as having thought through the process he used to generate the sampling.

01 Sep 2006 Mike Edwards

very useful! beautiful code!

03 Apr 2007 Per-Anders Ekström

Excellent!

02 Apr 2010 Florian

Exactly what I was looking for!!! Many thanks for the great work!!!

10 Nov 2010 Vassilios Vassiliadis

Respect....

12 Jan 2011 Mate 2u

Hi when i try to use the function on a very large a array it gives me the following error...

??? Maximum variable size allowed by the program is exceeded.

Error in ==> randfixedsum at 58
w = zeros(n,n+1); w(1,2) = realmax; % Scale for full 'double' range

27 Feb 2011 Bruno Luong  
13 Oct 2011 Christophe Lauwerys

Nice. I'm trying to generate random data within a simplex defined by linear inequality constraints.

Lets say I already have the N vertices of the simplex defined by the inequalities. Is it then correct to first generate a random sample in the interval [0,1] with a sum equal to 1, and then take the inner product of this sample with the vector of vertices?

Something along the lines of:

X = rand(6,2);
k = convhull(X);
plot(X(k,1),X(k,2),'b'), hold on
nv = numel(k)-1; % Nmuber of vertices
X = X(k(1:end-1),:); % Remove repeated first vertex
L = randfixedsum(size(X,1),1000,1,0,1);
Y = L'*X;
plot(Y(:,1),Y(:,2),'r.'), hold off

Maybe I shouldn't trust my vision on this, but the samples don't really look uniformly spread within the simplex. For some reason they only seem to do for a triangle.

Any thoughts?

Thanks

Christophe

Please login to add a comment or rating.
Tag Activity for this File
Tag Applied By Date/Time
random Roger Stafford 22 Oct 2008 08:12:56
fixedsum Roger Stafford 22 Oct 2008 08:12:56
ndimensional Roger Stafford 22 Oct 2008 08:12:56
hypercube Roger Stafford 22 Oct 2008 08:12:56
simplex Roger Stafford 22 Oct 2008 08:12:56
fixedsum Federico Felizzi 12 Sep 2011 07:50:21

Contact us at files@mathworks.com