% 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.
% 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.
% 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.
% by Matthew Nelson July 21st, 2009
% Last Updated by Matthew Nelson May 23, 2015
Probably related to the above problem, but it is still possible to get results greater than 1 when the input is a vector:
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.
There might be a slight bug with the 'two' sided tests. See the following:
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.
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.
Create scripts with code, output, and formatted text in a single executable document.