Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Precision issues with random numbers

Subject: Precision issues with random numbers

From: Mark Salomon

Date: 9 Dec, 2010 18:53:05

Message: 1 of 5

Hi,

I'm having some problems with random numbers generated by Matlab. I believe that precision issues are behind those problems but I'm not completely sure.

Specifically, the function gamrnd returns zeros very often when the shape parameter is low (e.g., less than 0.001). For example:

% call this many times (with that shape parameter or smaller)
% and you will get zero very often
gamrnd([0.0012999], [1])

Is there any way to get around this? For reasons that are too long to explain I cannot use a scale parameter greater than 1. I don't want to see zeros as often as gamrnd is returning them with the shape parameters I pass.

If this cannot be solved in Matlab, could you recommend a library (preferably in C or C++) that can use greater precision (e.g., extended, quadruple) or that doesn't exhibit this behavior (frequent generation of zeros)?

Thanks in advance

Subject: Precision issues with random numbers

From: Roger Stafford

Date: 9 Dec, 2010 20:25:09

Message: 2 of 5

"Mark Salomon" <answer_through@thenewsgroup.thank.you.com> wrote in message <idr8ih$o1u$1@fred.mathworks.com>...
> .......
> Specifically, the function gamrnd returns zeros very often when the shape parameter is low (e.g., less than 0.001). .......
- - - - - - - -
  With a shape parameter that small you can expect a tight clustering of 'gamrnd' near zero. However the odds are very heavily against ever getting a zero. You had better inspect your results more closely. Are you using the default display of "format short"? That only gives you four place accuracy. Try using "format long" or better yet one of the FEX routines that print numbers to very high accuracy. Try '%27.25f' in fprintf. Or try "format hex". Anything that will show you the actual numbers you have obtained.

Roger Stafford

Subject: Precision issues with random numbers

From: Mark

Date: 9 Dec, 2010 22:29:05

Message: 3 of 5

Roger,

Thanks for your answer. Yes, I have tried changing the format too, but it's not a visualization problem. In fact, I detected this issue when dividing numbers by the random number sampled from gamrnd and getting results of Inf. I don't think Matlab would indicate a result of Inf if the divisor wasn't really zero. For instance, try this

for i=1:10, rnd1=gamrnd([0.0003], [1]); 1/rnd1, end

You will get several Inf values

Thus, it doesn't seem a visualization issue




"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <idrdv5$5ej$1@fred.mathworks.com>...
> <answer_through@thenewsgroup.thank.you.com> wrote in message <idr8ih$o1u$1@fred.mathworks.com>...
> > .......
> > Specifically, the function gamrnd returns zeros very often when the shape parameter is low (e.g., less than 0.001). .......
> - - - - - - - -
> With a shape parameter that small you can expect a tight clustering of 'gamrnd' near zero. However the odds are very heavily against ever getting a zero. You had better inspect your results more closely. Are you using the default display of "format short"? That only gives you four place accuracy. Try using "format long" or better yet one of the FEX routines that print numbers to very high accuracy. Try '%27.25f' in fprintf. Or try "format hex". Anything that will show you the actual numbers you have obtained.
>
> Roger Stafford

Subject: Precision issues with random numbers

From: Peter Perkins

Date: 9 Dec, 2010 22:42:25

Message: 4 of 5

On 12/9/2010 3:25 PM, Roger Stafford wrote:
> "Mark Salomon" <answer_through@thenewsgroup.thank.you.com> wrote in
> message <idr8ih$o1u$1@fred.mathworks.com>...
>> .......
>> Specifically, the function gamrnd returns zeros very often when the
>> shape parameter is low (e.g., less than 0.001). .......
> - - - - - - - -
> With a shape parameter that small you can expect a tight clustering of
> 'gamrnd' near zero. However the odds are very heavily against ever
> getting a zero.

Roger, for once I have to disagree with you, at least practical terms.
With that shape parameter, there's something like a 50% chance of
getting a value smaller than the smallest (nondenormal) double precision
number.

 >> gamcdf(realmin,.001,1)
ans =
       0.49272

Mark, the problem is that gamrnd(.001,1,n,m) asks for numbers that can't
be represented in d.p., and so the values are rounded to either realmin
or zero.

 >> x = gamrnd(.001,1,1000000,1);
 >>
 >> sum(x <= realmin)
ans =
       492535

My question would be, is this really a physically reasonable
probablility distribution? If the answer is really yes, I think you'll
need to generate log-gamma random values. I don't know how to do that,
but Luc Devroye's very fine book

    http://cg.scs.carleton.ca/~luc/rnbookindex.html

might be a place to start.

Hope this helps.

Subject: Precision issues with random numbers

From: Roger Stafford

Date: 9 Dec, 2010 23:50:23

Message: 5 of 5

Peter Perkins <Peter.Perkins@MathRemoveThisWorks.com> wrote in message <idrm0h$3ng$1@fred.mathworks.com>...
> On 12/9/2010 3:25 PM, Roger Stafford wrote:
> > "Mark Salomon" <answer_through@thenewsgroup.thank.you.com> wrote in
> > message <idr8ih$o1u$1@fred.mathworks.com>...
> >> .......
> >> Specifically, the function gamrnd returns zeros very often when the
> >> shape parameter is low (e.g., less than 0.001). .......
> > - - - - - - - -
> > With a shape parameter that small you can expect a tight clustering of
> > 'gamrnd' near zero. However the odds are very heavily against ever
> > getting a zero.
>
> Roger, for once I have to disagree with you, at least practical terms.
- - - - - - - - - -
  My apologies. I made the claim without properly checking things out. I just looked at a few gamma pdf values for that shape parameter value and made a guess, a wrong one as it turned out.

Roger Stafford

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us