Statistics: binofit

### Function Reference: `binofit`

statistics: pshat = binofit (x, n)
statistics: [pshat, psci] = binofit (x, n)
statistics: [pshat, psci] = binofit (x, n, alpha)

Estimate parameter and confidence intervals for the binomial distribution.

`pshat = binofit (x, n)` returns the maximum likelihood estimate (MLE) of the probability of success for the binomial distribution. x and n are scalars containing the number of successes and the number of trials, respectively. If x and n are vectors, `binofit` returns a vector of estimates whose i-th element is the parameter estimate for x(i) and n(i). A scalar value for x or n is expanded to the same size as the other input.

`[pshat, psci] = binofit (x, n, alpha)` also returns the `100 * (1 - alpha)` percent confidence intervals of the estimated parameter. By default, the optional argument alpha is 0.05 corresponding to 95% confidence intervals.

`binofit` treats a vector x as a collection of measurements from separate samples, and returns a vector of estimates. If you want to treat x as a single sample and compute a single parameter estimate and confidence interval, use `binofit (sum (x), sum (n))` when n is a vector, and `binofit (sum (x), n * length (x))` when n is a scalar.

Further information about the binomial distribution can be found at https://en.wikipedia.org/wiki/Binomial_distribution

See also: binocdf, binoinv, binopdf, binornd, binolike, binostat

Source Code: binofit

### Example: 1

     ## Sample 2 populations from different binomial distibutions
     rand ("seed", 1);    # for reproducibility
     r1 = binornd (50, 0.15, 1000, 1);
     rand ("seed", 2);    # for reproducibility
     r2 = binornd (100, 0.5, 1000, 1);
     r = [r1, r2];

     ## Plot them normalized and fix their colors
     hist (r, 23, 0.35);
     h = findobj (gca, "Type", "patch");
     set (h(1), "facecolor", "c");
     set (h(2), "facecolor", "g");
     hold on

     ## Estimate their probability of success
     pshatA = binofit (r(:,1), 50);
     pshatB = binofit (r(:,2), 100);

     ## Plot their estimated PDFs
     x = [min(r(:,1)):max(r(:,1))];
     y = binopdf (x, 50, mean (pshatA));
     plot (x, y, "-pg");
     x = [min(r(:,2)):max(r(:,2))];
     y = binopdf (x, 100, mean (pshatB));
     plot (x, y, "-sc");
     ylim ([0, 0.2])
     legend ({"Normalized HIST of sample 1 with ps=0.15", ...
              "Normalized HIST of sample 2 with ps=0.50", ...
              sprintf("PDF for sample 1 with estimated ps=%0.2f", ...
                      mean (pshatA)), ...
              sprintf("PDF for sample 2 with estimated ps=%0.2f", ...
                      mean (pshatB))})
     title ("Two population samples from different binomial distibutions")
     hold off