April – Frequentist Inference in Mathematica

Mathematica provides a package called “Hypothesis Testing” for analyzing random data. Package includes handy objects like “Around” which represents a quantity and the uncertainty around it. The following example code returns an Around object representing the $90 \%$ confidence interval of the estimated bias of a coin after a repeated trial. Like shown in the code, the ListPlot plots Around objects as such.

(*Load the Hypothesis Testing Package.*)
Needs["HypothesisTesting`"];

coin[flips_, bias_] :=
 (*Make the experiment and collect the data*) 
 Module[{index, realizations, mean, conf, around},
  realizations = {};
  For[index = 1, index <= flips, index++,
   realizations =  
     Append[realizations, 
      RandomVariate[BernoulliDistribution[bias]]];
   ];
  
  (*Mean*)
  mean = Mean[realizations[[All]]];
  (*Confidence interval*)
  conf = MeanCI[realizations[[All]]];
  (*Around object*)
  around = Around[mean, Abs[conf[[1]] - conf[[2]]]]
  ]
around = coin[1000, 0.5]
ListPlot[{around}]