0
$\begingroup$

I have the following data, and need to fit a binomial distribution to this data. I have already done so for a gaussian fit (this is the example code I have below), but I am having trouble using a binomial distribution.

For this data, n = 18 and p = 1/2.

data := {{1, 0}, {2, 0}, {3, 1}, {4, 1}, {5, 5}, {6, 4}, {7, 11}, {8, 
24}, {9, 18}, {10, 20}, {11, 9}, {12, 6}, {13, 0}, {14, 0}, {15, 
1}, {16, 0}, {17, 0}, {18, 0}}

model[x_] = ampl Evaluate[PDF[NormalDistribution[x0, sigma], x]];
fit = FindFit[data, model[x], {ampl, x0, sigma}, x]
Show[ListPlot[data], 
Plot[model[x] /. fit, {x, 0, 19}, PlotStyle -> Red], 
AxesLabel -> {"# of Heads", "# of Trials"}]

Any help would be great.

$\endgroup$
1
$\begingroup$

The first step is to obtain the estimate of the probability of a head ($p$):

data = {{0, 0}, {1, 0}, {2, 0}, {3, 1}, {4, 1}, {5, 5}, {6, 4}, {7, 11}, {8, 24},
  {9, 18}, {10, 20}, {11, 9}, {12, 6}, {13, 0}, {14, 0}, {15, 1}, {16, 0}, 
  {17, 0}, {18, 0}};
n = 18;
p = data[[All, 1]].data[[All, 2]]/(n Total[data[[All, 2]]])
(* 97/200 *)  (* Very close to 0.5 *)

I've added in a zero count for when there are zero heads.

One can plot the observed relative frequencies and the estimated probabilities for a binomial distribution:

DiscretePlot[{data[[All, 2]][[k + 1]]/Total[data[[All, 2]]], 
  PDF[BinomialDistribution[18, p], k]}, {k, 0, 18}, 
 PlotLegends -> {"Observed relative frequencies", "Estimated binomial distribution"},
 PlotRange -> All, PlotStyle -> {Blue, Red}, AxesLabel -> {"# of Heads", "Probability"}]

Binomial distributions

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.