Binornd draws '1' every time

I'm running a script in which I use binornd(1,0.5) within a for loop, and the output is '1' every time. When I copy paste to the command window, it behaves as expected (I haven't done a full statistical test, but at least I get both '0's and '1's as outputs). In the same script, I use randi, which seems to be behaving normally..
Moving the binornd line to different positions in the script does not make a difference, and neither does adding rng('shuffle') just above. Finally, I use binornd(1,p) in another loop, where p varies on each iteration, and I also get only '1's as outputs. So it seems to be a problem specific to binornd.

5 Comments

Hard to say without the script.
This is part of the script (embedded in a for loop), but the problem exists both for the sub-iterations of the while loop and the main iterations of the larger for loop.
The culprit is at line 5 (the 2nd time binornd is called in the script it works ok - my mistake).
As I replied below, making p very low does not help.
while count<limit
matrix=fastest_matrix;
a(count)=randi([1 order]);
b(count)=randi([1 order]);
t(count)=binornd(1,0.5);
if a(count)==b(count)
continue
elseif t(count)==1
prob_link(count)=exppdf(matrix(a(count),b(count)),mu)/exppdf(0,mu);
rng('shuffle')
g(count)=binornd(1,prob_link(count));
if g(count)==1
matrix(a(count),b(count))=matrix(a(count),b(count))+1;
I=find(matrix(:,b(count))>=1);
if sum(matrix(:,b(count)))>cat_limit && numel(I)>1
matrix(I,b(count))=matrix(I,b(count))-(1/(numel(I)-1));
matrix(a(count),b(count))=matrix(a(count),b(count))+...
...(1/(numel(I)-1));
end
AdjMatrix(:,:,count)=matrix;
count=count+1;
else continue
end
else
if matrix(a(count),b(count))>=1
matrix(a(count),b(count))=matrix(a(count),b(count))-1;
matrix(:,b(count))=matrix(:,b(count))+(1/(order-1));
matrix(a(count),b(count))=matrix(a(count),b(count))-...
...(1/(order-1));
else continue
end
end
end
John D'Errico
John D'Errico on 8 Jul 2014
Edited: John D'Errico on 8 Jul 2014
Why are you using rng inside the loop?
Don't do a shuffle before every call! It does not make things more random. In fact, it will make things less random and it will slow down your code.
This is a common mistake people make. They think because they have a tool, that they must use it.
Apologies, this was not supposed to be included - I used it as a test, but it didn't resolve the problem (although it did slow things down as you say).
I've now found another clue - even randi([0 1]) is causing the same problem, although randi with non-binary arguments is working fine, and again randi([0 1]) works fine from the command line! Very strange...
Can't see why, but problem resolved after making some changes elsewhere in the code. Thanks everyone.

Sign in to comment.

Answers (2)

what if you execute
which binornd
just before the calls? Do the outputs differ based on the situation (script vs. command line). It could be that you have two different versions ...

1 Comment

Thanks for the response. I tried this and I get the same version both times: C:\Program Files\MATLAB\R2014a\toolbox\stats\stats\binornd.m
But yes the outputs do differ between these 2 situations. I've now noticed that the 2nd time I call it, it is indeed working (just that the p values were high), but the first time is still failing (line 5 of the code I entered above). And changing p to any non-zero value below 1 does not change the situation.

Sign in to comment.

Shashank Prasanna
Shashank Prasanna on 8 Jul 2014
Edited: Shashank Prasanna on 8 Jul 2014
Why don't you generate all of your random numbers before hand and just use index into the array at each loop iteration? It should be faster and cleaner:
randBin = binornd(limit,0.5);
while count<limit
matrix=fastest_matrix;
a(count)=randi([1 order]);
b(count)=randi([1 order]);
t(count)=randBin(count);
...

3 Comments

The first argument is the number of trials, so the result of this is the mean of the binomial distribution with n=limit and p=0.5 (not an array of independent Bernoulli trial results).
My bad, I meant to say:
randBin = binornd(1,0.5,limit,1);
OK thanks. And the problem resolved itself a few minutes ago when I made some changes somewhere else in the code. It's still not at all clear to me why, but at least it's working!

Sign in to comment.

Tags

Asked:

on 8 Jul 2014

Commented:

on 8 Jul 2014

Community Treasure Hunt

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

Start Hunting!