File Exchange

image thumbnail

myBinomTest(s,n,p,S​ided)

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

23 Downloads

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:
% http://en.wikipedia.org/wiki/Binomial_test
% http://www.graphpad.com/guides/prism/6/statistics/index.htm?stat_binomial.htm
%
% by Matthew Nelson July 21st, 2009
%
% Last Updated by Matthew Nelson May 23, 2015
% matthew.nelson.neuro@gmail.com

Comments and Ratings (6)

Zoltan

Zoltan (view profile)

Adam

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 =
1

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

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:

myBinomTest(7,14,.5,'two')
ans =
1.2095

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.

Updates

2.0.0.0

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.

1.7.0.0

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

1.3.0.0

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

1.2.0.0

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