(*A Simple Histogram Example in Mathematica*)
(*Created June 23, 2013 by Wing To UCSB Physics*)
(* Insert the data here *)
dataLeft={1.06,1.19,1.21,1.16,1.31,1.19,1.19,1.28,1.31,1.25,1.16,1.38,1.32,1.28,1.16,1.28,1.28,1.25,1.31,1.34,1.35,1.25,1.50,1.41,1.44,1.60,1.35,1.35,1.29,1.44,1.70,1.40,1.30,1.20,1.16,1.13,1.18,1.12,1.19,1.25,1.35,1.22,1.31,1.25,1.33,1.35,1.32,1.29,1.31};
dataRight={1.06,1.00,1.38,0.94,1.00,1.25,1.22,1.44,1.35,1.31,1.28,1.26,1.50,1.44,1.24,1.20,1.06,1.50,1.44,1.24,1.20};

(* Now make a simple histogram. With no additional arguments, it will autoscale things. *)
histoLeft = Histogram[dataLeft]
histoRight = Histogram[dataRight]

(*   The histogram doesn't look very good and does not display all the data points. The binning is too BIG!*)
(* Googled Mathematica histograms. Found more options! http://reference.wolfram.com/mathematica/ref/Histogram.html *)
(* Arg runs as following: data, range/bin, binheight by count, Label,*)
histoLeft = Histogram[dataLeft,{0.9,2.0,0.05},"Count",PlotLabel->"An example of a histogram", AxesLabel->{"time (s)","occurence"}, ChartStyle->Red]
histoRight= Histogram[dataRight,{0.9,2.0,0.1},"Count",PlotLabel->"An example of a histogram", AxesLabel->{"time (s)","occurence"}, ChartStyle->Red]
Mean[dataLeft] (* Easy to compute the mean *)
StandardDeviation[dataLeft] (* And the StandardDeviation *)

(* HistogramList can also list the contents of each bin for easy analysis.*)

DataList = HistogramList[dataLeft,{1.0,2.0,0.05}];
BinPositions = DataList[[1]];
BinContents= DataList[[2]];
DataTable = Table[{BinPositions[[i]],BinContents[[i]]},{i,1,Length[BinContents]}]

(* Now make a graph (or plot) of the contents of that List. I do this with Listplot *)
(*  http://reference.wolfram.com/mathematica/ref/ListPlot.html *)

ListPlot[DataTable, AxesOrigin->{1.0,2.0}]

(* The list plots doesn't have the uncertainties within each bin *)
(* We assign the uncertainties using ErrorBar function *)
(* For large number of occurrence bin error ~ sqrt (bin content) *)
(* For small number of occurrence bin error ~ which I approximate here with (0.5+sqrt(0.25+bin content))*)
(* This function has a nice proper that approximates the poisson errors.
n low high cred.
0 0.000000 1.000000 0.632121
1 0.381966 2.618034 0.679295
2 1.000000 4.000000 0.681595
3 1.697224 5.302776 0.682159
4 2.438447 6.561553 0.682378
5 3.208712 7.791288 0.682485
6 4.000000 9.000000 0.682545
7 4.807418 10.192582 0.682582
8 5.627719 11.372281 0.682607
9 6.458619 12.541381 0.682624*)
Needs["ErrorBarPlots`"];
DiscretePlot[Evaluate@Table[CDF[PoissonDistribution[λ],k],{λ,{5,10,20}}],{k,0,30},PlotRange->All,PlotMarkers->Automatic]

DataTableWithErrors=Table[{{BinPositions[[i]],BinContents[[i]]},ErrorBar[0.5+Sqrt[0.25+BinContents[[i]]]]},{i,1,Length[BinContents]}];

ErrorListPlot[DataTableWithErrors,PlotStyle->Blue, DataRange->{1.0,2.0}, PlotRange->{0,15}]

(* If we want more information about the data than the mean and width *)
(* can fit the data to what we "think" the functional form should be.*)
(* The Fitter does not like Errors but allows us to put weights. Weights = 1/ Error^2 *)

FitResult = NonlinearModelFit[DataTable,AMP * Exp[{-(MEAN-x)^2}/(2  SIGMA^2)],{{AMP,5.},{MEAN,1.},{SIGMA,1.}}, x, Weights->1/(0.5 + Sqrt[BinContents+0.25 ])^2];
FitResult["ParameterTable"]

(*  Now let's put everything together with errors and fit functions *)
Show[Plot[FitResult[x],{x,1.0,2.0},PlotLabel->"Fit compared to histogram contents", PlotRange->{0,15},PlotStyle->Red, AxesLabel->{"time (s)","Number of occurences"}], ErrorListPlot[DataTableWithErrors, PlotStyle->Blue], AxesOrigin->{1.0,0}]