(*A Simple Histogram Example in Mathematica*)
(*Created June 23, 2013 by Wing To UCSB Physics*)
(* Insert the data here *)
data={1,1.1,1.3,0.8,0.9,1.2,1,1.1,0.8,0.7,1,1.1,0.9,1};

(* Now make a simple histogram. With no additional arguments, it will autoscale things. *)
histo = Histogram[data]

(*   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,*)
histo = Histogram[data,{0.5,1.5,0.1},"Count",PlotLabel->"An example of a histogram", AxesLabel->{"time (s)","occurence"}, ChartStyle->Red]
Mean[data] (* Easy to compute the mean *)
StandardDeviation[data] (* And the StandardDeviation *)

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

DataList = HistogramList[data,{0.5,1.5,0.1}]
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->{0.5,1.5}]

(* 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->{0.5,1.5}, PlotRange->{0,8}]

(* 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,0.5,1.5},PlotLabel->"Fit compared to histogram contents", PlotRange->{0,8},PlotStyle->Red, AxesLabel->{"time (s)","Number of occurences"}], ErrorListPlot[DataTableWithErrors, PlotStyle->Blue], AxesOrigin->{0.5,0}]