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]

enter image description here

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!

  • $\begingroup$ Maybe this can help to share your data. :) $\endgroup$
    – yode
    Oct 27, 2016 at 13:15
  • 1
    $\begingroup$ Look at ScalingFunctions to get a log histogram. How do you want to obtain analytically a line drawn by hand? Look at SmoothHistogram. $\endgroup$
    – corey979
    Oct 27, 2016 at 13:26
  • $\begingroup$ @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). $\endgroup$
    – Vaggelis_Z
    Oct 27, 2016 at 13:37
  • $\begingroup$ Look at FindDistribution and related commands, like FindDistributionParameters. $\endgroup$
    – corey979
    Oct 27, 2016 at 14:19
  • $\begingroup$ @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. $\endgroup$
    – Vaggelis_Z
    Oct 27, 2016 at 14:22

1 Answer 1


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]]

enter image description here

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.

  • $\begingroup$ 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? $\endgroup$
    – Vaggelis_Z
    Oct 27, 2016 at 16:11
  • $\begingroup$ 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! $\endgroup$
    – bill s
    Oct 27, 2016 at 16:41
  • $\begingroup$ 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? $\endgroup$
    – Vaggelis_Z
    Oct 27, 2016 at 16:44
  • $\begingroup$ 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? $\endgroup$
    – Vaggelis_Z
    Oct 27, 2016 at 17:21
  • $\begingroup$ 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[]]] $\endgroup$
    – bill s
    Oct 27, 2016 at 18:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.