Equally distributed multidimensional random values with boundaries - how to generate?
Show older comments
I have to generate a matrix that will have 100 columns. Every row represents a value that can change in defined range. For example, I can describe it by the array:
A=[1 5; 3 7; 1 10]
Where 1 to 5 is first row range, 3 to 7 is the second, and 1 to 10 is the last. If I want to generate the random distribution to cover the range, just for one line, I can do this as follow:
data = lb + rand(1,100) .* ( ub - lb );
Where ub and lb are upper and lower boundary. Now, I can reproduce this in simple for loop:
for i=1:size(A,1)
lb=A(i,1);
ub=A(i,2);
data2(i,:) = lb + rand(1,100) .* ( ub - lb );
end
But in this case, every single row is evaluated separately, So I don't have any guarantee that the distribution will be equal in the meaning of comibinations between rows, as every rows changes independent. For example I can encounter situation where I will not have any combination with Row 1 close to 1 and Row 2 close to 7, just because of RNG. Is there any way I can sovle my problem and ensure multidimensional equal random distribution?
7 Comments
William Rose
on 13 Feb 2023
Maybe I do not understand what you want to do. You wrote:
"I don't have any guarantee that the distribution will be equal in the meaning of comibinations between rows, as every rows changes independent."
The solution you propoesed will generate independent columns of uniformly discributed random numbers with the ranges you want. Each column of A is a uniformly distributed random point in a 3D rectangular prism with the bounds as specified in A. Since data2 has 100 columns, there are 100 random points in the 3D rectangular prism. Is that not satisfactory?
A uniformly distributed independent random sample does not require that two successive values have any relation to each other. I'll use rand to make an example.
rand()
So the first point I got lies above 0.5. Now, I'll sample a second point. There is NO reaon to expect that the next point will lie at some value less than 1/2, even if the first sample I generated was greater than 1/2.
rand()
Do you understand that? Likewise, flipping a fair coin twice in a row does not mean that if the first toss was a head, then the second toss MUST be a tail, or even that a tail is any more likely on the second coin toss.
But that is exactly what you are asking to have happen. You seem to think a uniformly distributed set of numbers has some sort of memory, so that future samples will in some way depend on the previous samples. I'm sorry, but that is not how independent random variables work. And each successive random sample (from rand) is independent from the previous ones, as much as is possible in the context of how a pseudo-random variable can be. And rand was designed to use a very good pseudo-random variable scheme, with very good statistical properties.
Yes, the laws of probability and statistics do apply. Over a long term, the sample mean of a distribution will tend to the population mean. So that eventually things will balance out.
Karol P.
on 13 Feb 2023
Say A = [1 5; 3 7].
Now say you generate random columns as
for i = 1:size(A,1)
lb = A(i,1);
ub = A(i,2);
data(i,:) = lb + rand(1,100)* ( ub - lb );
end
Then the column vectors of the matrix [data(1,:);data(2,:)] are uniformly distributed on [1 5] x [3 7] although (or better: because) both are generated independently, namely row 1 as uniformly distributed over [1 5] and row 2 as uniformly distributed over [3 7].
Thus what you want, namely
Now I want to gererate a random set of 100 columns, that will equally cover the area of allowed solutions.
is fulfilled by using this approach.
John D'Errico
on 13 Feb 2023
Edited: John D'Errico
on 13 Feb 2023
I'm sorry, but I think you still misunderstand random numbers, what a uniform distribution means, and, apparently the entire point of my comment.
That you have columns with different ranges is completely irrelevant. Each column will be filled with sets of numbers that are uniformly distributed. And they are independent of other columns, or of previous samples.
For example:
n = 25000;
X = [rand(n,1),rand(n,1)*2 + 1];
So the first column of X (thus X(:,1)) is uniformly distributed, on the open interval (0,1).
X(:,2) is niformly distributed on the open interval (1,3).
These points, if taken as points in the two dimensional box (0,1)x(1,3), will fill that space uniformly. Of course if the sampling is coarse enough, the box will be filled in very well.
plot(X(:,1),X(:,2),'.')
If I choose n a bit larger, then the figure turns completely blue, with white showing through at all. And if you count the number of points in any local region of the box, so essentially a 2-dimensional histogram, then you would find that locally the number of points in that region will be proportional to the area of the region you looked at.
For example, histcounts produces that 2-d histogram.
[N,XEDGES,YEDGES] = histcounts2(X(:,1),X(:,2))
And we would expect to see on average, with a 10x10 grid of bins on that domain, we would expect to see 1% of the samples falling in each bin. Indeed, that is what happens. If the sample size were larger, then the counts in each bin will more accurately approach that value of 1% in each bin. We expect to see some degree of variability of course in those bin counts, but as I have said, that will decrease with sample size.
surf(N)
That the different sets of variables live in different intervals is completely irrelevant. (Sorry, I forgot to scale the x and y axes in the 2-d hstogram plot.)
Karol P.
on 13 Feb 2023
Accepted Answer
More Answers (0)
Categories
Find more on Uniform Distribution (Continuous) in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

