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

1 or 2 sided binomial test with arbitrary binomial probabilities
5.2K 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

Cite As

Matthew Nelson (2024). myBinomTest(s,n,p,Sided) (https://www.mathworks.com/matlabcentral/fileexchange/24813-mybinomtest-s-n-p-sided), MATLAB Central File Exchange. Retrieved .

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

Community Treasure Hunt

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

Start Hunting!
Version Published Release Notes
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.

1.0.0.0