# Obtaining a fitting curve from a histogram

Here is a data file containing a small sample of data for play.

Now let's create the corresponding histogram

data = Flatten[Import["hist.dat", "Table"],1];

P00 = Histogram[data, Automatic, "Probability", ChartStyle -> Gray,
ChartBaseStyle -> EdgeForm[None], Frame -> True,
FrameLabel -> {"N", "P"}, RotateLabel -> False,
LabelStyle -> Directive[FontFamily -> "Helvetica", 20],
Epilog -> {Red, Thick, Dashed,
Line[{{#, 0}, {#, 1}} &@Last@Commonest[data]]}, PlotRange -> All,
PlotRangePadding -> 0.001, AspectRatio -> 1, ImageSize -> 550] The red vertical dashed line denotes the most probable value.

Now I want the following: Compute the best fit of the analytical equation $P = f(N)$ of the tail (drawn by hand in the above plot) and plot it on top of the histogram. The best fitting equation of the tail should start from the most probable value (all the data before the most probable value should not be taken into consideration) and proceed to all the other values tending asymptotically to zero. The fitting line should pass either from the upper middle (as in the plot) of each rectangle or even more preferably from the upper right corner of each rectangle. I suppose that this line should be an exponential-like function.

Any ideas?

Many thanks in advance!

• Maybe this can help to share your data. :) – yode Oct 27 '16 at 13:15
• Look at ScalingFunctions to get a log histogram. How do you want to obtain analytically a line drawn by hand? Look at SmoothHistogram. – corey979 Oct 27 '16 at 13:26
• @corey979 The fitting line should start from the most probable value and then pass from all the other values, tending asymptotically to zero (see the plot). – Vaggelis_Z Oct 27 '16 at 13:37
• Look at FindDistribution and related commands, like FindDistributionParameters. – corey979 Oct 27 '16 at 14:19
• @corey979 I don't know beforehand if the best fit is exponential, or 1/x , or 1/x^2, etc. I know that the fitting line should pass through each rectangle (upper right point) tending asymptotic to zero. – Vaggelis_Z Oct 27 '16 at 14:22

One approach is to use a smoothing kernel. Using the data from your file:

dist = SmoothKernelDistribution[Flatten@data, 0.5];
trunc = TruncatedDistribution[{0, 100}, dist];
Show[Histogram[Flatten[data], 10, "PDF"],
Plot[PDF[trunc, x] // Evaluate, {x, 0, 20}, PlotRange -> All,
Filling -> Axis]] If you want to assume a form for the distribution, then you can do:

FindDistributionParameters[Flatten@data, LaplaceDistribution[mu, sigme]]


which returns the best mu and sigma for the LaplaceDistribution. Choose whatever distribution you want.

• Nice, but I want the analytic equation regarding the best fit. Is there a way to obtain the best fit for the blue curve starting from N = 6 up to N = 20? – Vaggelis_Z Oct 27 '16 at 16:11
• Look at PDF[trunc, x] and you will see that it is an interpolating function. What else can you expect from a curve created from points? To get an analytic form for the curve you will need to assume a model structure, which you told @corey979 you did not want to do! – bill s Oct 27 '16 at 16:41
• You are right! OK, let's assume that the best fitting curve has the form a+bLog_10(cx). How can we derive the best values for a, b and c? – Vaggelis_Z Oct 27 '16 at 16:44
• Now we are on the right track! Could you please edit your post and replace the plot of trunc, on top of the histogram, with the plot of the Laplace distribution for the best values of mu and sigma? – Vaggelis_Z Oct 27 '16 at 17:21
• You don't actually want the Laplace Distribution -- it's double sided and your data is single sided. I suggest giving a little thought to what makes your data the way it is, and what distribution would make sense for your data. Maybe you want a RayleighDistribution? A MaxwellDistribution? Maybe a PoissonDistribution make the most sense... then just Plot[PDF[PoissonDistribution[]]] – bill s Oct 27 '16 at 18:17