(*
 This is an example of histogramming  a gaussian distribution and then fitting it and plotting it together with the fit. It is intended to illustrate the basic methods for histograming, plotting, fitting and error bars. 
Harry Nelson from David_Stuart, January 2017
*)


(* 
 First I have some data which was generated externally and imported. I save it in a list called "a". Note that all of the variables I define start with lower case to distinguish them from Mathematica's built in functions that start with uppercase. But, I use camelcase to make the meaning of the variable names easier to remember. 
*)

a={0.0843775,0.061503,0.409863,0.282153,-0.236256,-1.70081,0.512925,-1.43355,0.429107,0.2688,2.16987,-0.229467,-0.92813,-1.61038,-0.988034,1.35624,-1.05393,-1.46079,-0.224995,1.69438,-0.531889,-0.307471,0.857624,0.361985,0.68736,1.79077,0.816336,-0.63192,0.817537,-0.277326,1.09466,-0.50137,0.621087,0.416547,0.229475,1.55992,0.803946,-1.35141,-1.08053,1.25988,0.233548,0.279898,-0.697694,-1.32566,0.816096,1.54668,-0.561437,0.279094,1.24989,1.01229,0.247803,0.797462,0.699245,0.0728633,1.43391,-0.169465,-0.325376,-1.76505,-0.11512,0.71597,1.06084,-0.756829,-0.528592,0.63316,-0.415525,1.07287,0.131959,1.26254,-1.77797,0.471602,0.898317,-0.238625,-1.32151,-0.250271,-0.308945,-0.302075,-0.612283,0.504638,0.321457,-0.490041,-0.0253785,0.152111,2.00734,-1.55613,-0.656362,-2.43659,-1.92372,-0.115187,-0.243433,0.784128,-1.67851,-1.55855,0.753839,0.568461,-0.916136,-0.279345,-0.376604,-0.159966,-1.70316,-0.657261,1.41904,1.43569,0.785343,-0.899746,0.162603,0.149056,0.571744,-0.766888,-0.523724,1.69772,-0.35669,-1.22672,0.775705,-0.620141,1.90284,1.88594,-0.169099,0.408571,-1.2807,1.55731,1.63649,-1.44657,-0.461263,0.0989473,-1.05822,-0.388162,1.0768,0.0652598,2.15069,0.310726,-0.364368,0.00119642,0.0129168,1.20236,0.741057,-0.0734605,-0.500334,0.965814,-0.095531,0.0590791,-0.899654,0.909275,-0.142735,-0.361375,-1.97618,-1.79593,0.981545,0.050485,-1.84093,-0.423376,-1.14508,-0.5141,-0.629542,0.350635,-1.23233,0.437411,0.0301431,0.0568879,-0.626177,0.26273,-0.665909,0.563533,1.29507,0.29491,1.39385,-2.15716,-0.450645,-0.585571,0.183563,-1.54912,1.00884,-1.92081,-0.511642,0.443471,0.925502,-0.0249285,0.656773,0.581,-2.66343,-0.36146,0.805579,1.53628,-0.593441,0.938864,-1.10035,-0.0391876,-0.387087,-0.230383,-0.747098,-0.059423,-1.10017,0.419265,1.01963,-1.20532,-0.169923,0.409924,0.466245,-1.22469,0.811558,0.279147,-1.19001,-1.14549,0.0757257,-0.766616,0.412469,1.10709,0.275098,0.888008,0.268041,-0.476903,-0.929225,2.16818,1.20661,-0.545802,0.54839,0.530438,0.614322,1.5854,0.493526,-0.945294,2.70847,1.70302,1.25519,-0.0328593,0.217243,1.5652,-1.89403,0.0120586,1.61617,-1.53282,0.770934,0.937929,-0.243369,-0.0866216,-0.60427,1.2343,-0.735281,-0.902218,0.380214,-0.687698,-0.87678,1.83213,1.55412,0.377792,1.22878,1.62585,-0.357461,-0.586306,1.14568,-0.64076,-1.3529,-1.43753,-0.459514,0.232331,-0.482869,-0.533575,1.70788,0.0301234,-1.59631,0.163732,0.110323,0.0435086,-1.25117,0.885467,-0.270064,0.45562,-0.816457,0.482134,-0.0463413,2.0081,-1.33072,-0.228199,0.282539,1.03055,2.29942,1.4622,0.850806,1.44398,-0.19694,-0.172486,0.223721,-0.349607,-0.301804,1.21207,-1.45187,0.0379911,-0.213835,-2.16724,0.019911,-0.136839,3.02034,-0.578514,0.971138,-1.86583,-0.447551,-0.605723,-0.557649,0.855907,-0.079813,0.904869,1.14858,0.33316,1.03602,-0.147544,-0.675243,0.486806,0.807856,0.203812,0.307627,-0.608779,-0.713202,-0.0176125,1.15051,-1.07862,-2.37788,0.945081,0.784206,-1.00458,-0.968926,-0.448098,-0.0432274,1.9077,-0.439328,1.29327,1.41993,1.21076,0.0984996,0.264982,0.558642,0.042634,-0.643856,0.997992,-0.175994,0.00653985,-1.04145,-0.706468,0.567096,0.735428,-1.65528,-0.750624,-1.22244,0.0412443,-0.580439,0.0108623,1.45139,-0.0960795,-0.614318,-0.650696,1.02822,1.02091,-0.829005,1.69347,0.967337,0.919289,0.760947,1.26549,-1.02005,-1.62479,0.38776,1.78339,-0.939629,-0.851972,1.65576,-1.46663,-0.0423529,1.39456,-0.941306,0.117577,0.364909,-0.301796,0.306622,0.839593,-0.980793,0.318846,0.408468,-1.4434,0.526703,1.43702,-1.19571,-0.734276,0.443211,0.135701,0.521916,-0.297874,0.308981,1.62213,-0.0926631,0.306196,-0.991324,-0.699927,0.433105,-0.153651,-0.813181,0.0923833,0.907719,0.610655,0.937475,-0.312065,-0.416638,-0.901219};

(* 
 We can calculate simple statistical quantities from this list as follows: 
*)
Mean[a]

StandardDeviation[a]

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

(* 
If we want a particular range and bin size we can specify that with the second parameter. For example here I make the range go from -5 to +5 with bin size of 0.5. 
*)
Histogram[a,{-5,5,0.5}]

(* 
 If we want to make the y axis be a log scale you put "LogCount" as the third parameter. Note however that it doesn't display the bins with one entry for
some reason. 
*)
Histogram[a, {-5,5,0.5},"LogCount"]

(* 
 If we want to put a title (or label) on the top of the plot we use PlotLabel as below. We can also put labels on the x and y axes. Note that here I switch back to a linear y axis rather than log using Count instead of LogCount. And finally AxisOrign specifies where the origin of the drawn axes sits.
*)
Histogram[a,{-5,5,0.5},"Count",PlotLabel->"An example of a Gaussian histogram", AxesLabel->{"x","y"}, AxesOrigin->{-4,0}, ChartStyle->Red]

(* 
 Next I want to fit the histogram to a Gaussian to extract a more precise measure of the mean and sigma. To do this I extract the histogram contents into a list of bin position and bin contents using HistogramList. I save the output into a new variable that I call aHistList. 
*)

aHistList = HistogramList[a,{-5,5,0.5}]

(* 
 Then I rearrange this into a vector of positions and contents that I save as a Table or "List". The semicolons at the end prevent the contents from being dumped. I don't care to look at them. I'll just use them later.
*)

aBinPositions = aHistList[[1]];
aBinContents = aHistList[[2]];
aHistVector = Table[{aBinPositions[[i]],aBinContents[[i]]},{i,1,Length[aBinContents]}];

(* 
 Now make a graph (or plot) of the contents of that List. I do this with Listplot 
*)

ListPlot[aHistVector, AxesOrigin->{-5,0}]

(* 
 Now I want to fit this to a Gaussian. I do that by explicitly writing out the Gaussian function in the NonlineModelFit command. The uncertainties on the bin contents are the square root of the contents except when the value of the contents is small then Poisson fluctuations makes it larger. So I approximate this as sqrt(aBinContents + 1) to make it never go below 1. These uncertainties are handled in the fit using the "Weights" specification. The "Weight" of each point is 1/error^2. Since the error is sqrt(aBinContents+1) the weight is 1/(aBinContents+1). Finally I save the result of the fit into a variable called aFitResult.
Note that I gave the parameters being fit appropriate names but starting with lowercase so they are not reserved names. And I specified approximate values for the fit to use as a starting point to speed convergence. 
*)

aFitResult = NonlinearModelFit[aHistVector,amplitude * Exp[{-(mean-x)^2}/(2 sigma^2)],{{amplitude,10.},{mean,0.},{sigma,1.}}, x, Weights->1/(aBinContents+1)]

(* 
I then dump the fitted parameters in a table including the uncertainty on each of the parameters as follows: 
*)

aFitResult["ParameterTable"]

(* 
 Now that I have everything let's make a final pretty plot of it. I do that by first drawing the fitted function with various specifications like a plot title and axis labels. Then I over lay the data points. This overlaying of two plots is done using the Show comment. It will show all the things in its list of arguments.
*)

Show[Plot[aFitResult[x],{x,-4,4},PlotLabel->"Fit compared to histogram contents", PlotRange->{0,100},PlotStyle->Red, AxesOrigin->{-5,0}, AxesLabel->{"time (s)","Number of occurences"}], ListPlot[aHistVector, PlotStyle->Blue]];

(* 
 The only thing missing here is that there are no uncertainties on the points. I can show uncertaintes (or error bars) using the ErrorListPlot command. That requires defining a table of data that specifies the error bars. I can do that similarly to the definition of aHistVector above. However that command needs to have access to the ErrorBarPlots package which I load using the Needs command. 
*)
Needs["ErrorBarPlots`"];

(* 
Define the table including error bars.
*)

aHistVectorWithErrors = Table[{{aBinPositions[[i]],aBinContents[[i]]},ErrorBar[1+Sqrt[aBinContents[[i]]]] },{i,1,Length[aBinContents]}];

(* 
 Now I can show a plot with the fit and the data points with error bars. 
*)
Show[Plot[aFitResult[x],{x,-4,4},PlotLabel->"Fit compared to histogram contents", PlotRange->{0,100},PlotStyle->Red, AxesOrigin->{-5,0}, AxesLabel->{"time (s)","Number of occurences"}], ErrorListPlot[aHistVectorWithErrors, PlotStyle->Blue]]