File Exchange

image thumbnail


version (12.5 KB) by Matthew Nelson
1 or 2 sided binomial test with arbitrary binomial probabilities


Updated 26 May 2015

View License

%function pout=myBinomTest(s,n,p,Sided)
% Performs a binomial test of the number of successes given a total number
% of outcomes and a probability of success. Can be one or two-sided.
% Inputs:
% s- (Scalar or Array) The observed numebr of successful outcomes
% n- (Scalar or Array) The total number of outcomes (successful or not)
% p- (Scalar or Array) The proposed probability of a successful outcome
% Sided- (String) can be 'one', 'two' (the default), or 'two, equal
% counts'. A value of 'one' will perform a one-sided test to
% determine if the observed number of successes are either
% significantly greater than or less than the expected number
% of successes, depending on whether s is greater than or less
% than the observed number of successes. 'Two' will use the
% method of small p-values (see reference below) to perform a
% two-tailed test to calculate the probability of observing
% any equally unlikely or more unlikely value greater than or
% less than the expected number of successes (ie with the
% same cdf value of the distribution. 'Two, equal counts'
% will perform a two-sided test that the that the actual
% number of success is different from the expected number of
% successes in any direction.
% Outputs:
% pout- The probability of observing the resulting value of s or
% another value more extreme (the precise meaning of which
% depends on the value of Sided) given n total outcomes with
% a probability of success of p.
% s, n and p can be scalars or arrays of the same size. The
% dimensions and size of pout will match that of these inputs.
% For example, the signtest is a special case of this where the value of p
% is equal to 0.5 (and a 'success' is dfeined by whether or not a given
% sample is of a particular sign.), but the binomial test and this code is
% more general allowing the value of p to be any value between 0 and 1.
% The results when Sided='two' and when Sided='two, equal counts' are
% identical only when p=0.5, but are different otherwise. For more
% description, see the second reference below.
% References:
% by Matthew Nelson July 21st, 2009
% Last Updated by Matthew Nelson May 23, 2015

Comments and Ratings (6)


Zoltan (view profile)


Adam (view profile)

Probably related to the above problem, but it is still possible to get results greater than 1 when the input is a vector:

binotest([11],22,0.5, 'Two')
ans =

binotest([11;12],22,0.5, 'Two')
ans =

Thanks for calling attention to that. The problem was that when using the two-sided test in the special case when the expected value was precisely equaled by the number of successes, the code would add the probability at that discrete value twice. That is now fixed in the update. The probability is always 1 in this case anyway, which can be interpreted as the probability that the absolute difference in the outcome from the expected value is greater than or equal to 0. (A certainty) In all other cases the code is and has been accurate.

Nelson Lau

There might be a slight bug with the 'two' sided tests. See the following:

ans =

Summit Suen

Trevor Agus

Thanks for posting it. It seems to be working fine. One improvement might be to include a working example in the help section, just to get us started faster.


Updated use of array inputs.
Sided accepts ‘one’ as input instead of 'greater' or 'lesser'.
Uses method of small p-values for default two-sided p-value. The previous method can still be done by inputting ‘two, equal counts’ as the variable Sided.

Fixed bug that occurs when the expected value is equaled by the number of successes, caught by Nelson Lau in the comments.

Fixed a bug in the previous update to allow the program to accept arrays as well as scalars in the inputs.

Allowed for the inputs to be arrays of the same dimensions and sizes rather than just scalars.

MATLAB Release Compatibility
Created with R2009a
Compatible with any release
Platform Compatibility
Windows macOS Linux

Discover Live Editor

Create scripts with code, output, and formatted text in a single executable document.

Learn About Live Editor