random numbers between a and b with specified mean and variance

72 views (last 30 days)
rihab
rihab on 2 Sep 2015
Answered: Greig on 3 Sep 2015
I am using a + b*rand() to create uniformly distributed variables between [a,b]. But I want to create the same with a specified mean and variance. Is there a way to do so in matlab?

Accepted Answer

James Tursa
James Tursa on 2 Sep 2015
1) a + b*rand() creates a value between a and a+b, not between a and b.
2) The values will have a uniform distribution with mean and variance fixed depending on a and b. If you want other mean and variance values then you either need a different a and b or you need a different distribution. Please specify what exactly you want.
  2 Comments
James Tursa
James Tursa on 2 Sep 2015
a + (b-a)*rand() will give uniform random values in the interval [a,b] with mean (b+a)/2 and variance ((b-a)^2)/12. So as long as c = (b+a)/2 and d = (b-a)/sqrt(12) then you will get a uniform distribution in the interval [a,b] with mean c and standard deviation d. If c and/or d do not match the formulas above, then (as I wrote before) you will need a different distribution ... not a uniform distribution. You can't specify a uniform distribution and then at the same time specify arbitrary mean and variance. You can specify a and b (and then get the resulting c and d), or you can specify c and d (and then get the resulting a and b), but you can't specify all four arbitrarily at the same time ... the math just doesn't work.

Sign in to comment.

More Answers (2)

Walter Roberson
Walter Roberson on 2 Sep 2015
If you require your distribution to be probability zero outside of [a,b], the way that uniform random has all of its probability within a pre-defined range, then you need to be using a Beta distribution. Uniform random distribution is one case of the Beta distribution.
However, the mean and standard deviation of a beta distribution cannot be chosen arbitrarily: they are fixed by the choice of a and b. http://www.mathworks.com/help/stats/betastat.html
There is no other distribution that is 0 probability outside of the given interval. If [a, b] are intended to be the complete bounds with no chance of a random number outside [a,b] then you need a Beta (or the special Beta that is Uniform random).
You can truncate an infinite random distribution to [a,b], but if you generate from the result, the samples you generate will not have mean c or standard deviation d. For example,
function x = truncrandn(a,b,c,d,m,n)
x = c + d * randn(m,n);
x = min(b,max(a,x));
end
This will produce random numbers in the specified range but exactly a and exactly b will both be over-represented, and the distribution of the result will probably not have the correct mean and will definitely not have the correct standard deviation.
  1 Comment
Greig
Greig on 3 Sep 2015
Just to clarify your comments on the Beta distribution....
The standard Beta distribution is bound to the range [0,1], not [a,b]. a and b control the shape of the distribution, but regardless of their choice (both must be > 0), the distribution is alway bound to [0,1].
There is a generalized Beta distribution, B(a, b, c, d), which is bound to [c,d]. This is not currently supported by MATLAB, but the rescaling is a simple linear transformation, so it should be easy to do.
Nevertheless, the mean and variance of B(a,b,c,d) depend on a,b,c, and d. So, as you correctly point out for the standard Beta distribution, they are not independent and cannot be arbitrarily defined.

Sign in to comment.


Greig
Greig on 3 Sep 2015
As far as I aware there is no bounded probability distribution where the mean and variance are independent of each other, and independent of the distribution parameterization. I don't believe it is mathematically possible. The bounds impose constraints the variance and, if the distribution is not symmetric about the mean, they also influence the mean.
Perhaps if you give us some more information about your application, we might be able to help with some approximation that fits your use. That is, something similar to James Tursa's answer and comment above.

Tags

Community Treasure Hunt

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

Start Hunting!