%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 twosided.
%
% 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 onesided 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 pvalues (see reference below) to perform a
% twotailed 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 twosided 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
2.0.0.0  Updated use of array inputs.


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. 
Create scripts with code, output, and formatted text in a single executable document.
Li He (view profile)
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 =
1
binotest([11;12],22,0.5, 'Two')
ans =
1.1682
0.8318
Matthew Nelson (view profile)
Thanks for calling attention to that. The problem was that when using the twosided 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 (view profile)
There might be a slight bug with the 'two' sided tests. See the following:
myBinomTest(7,14,.5,'two')
ans =
1.2095
Summit Suen (view profile)
Trevor Agus (view profile)
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.