We can use FindDistribution
as shown below. Before applying that function though, I am going to find the empirical distribution based on the image of the histogram.
Get the image points
Crop the image (using Mathematica's interactive image manipulation tools):
Binarize and edge detect:
img2 = EdgeDetect[Binarize[img], 3];
img2
Get the corresponding “white” points:
lsPoints = Position[ImageData[img2], 1];
lsPoints2 = Map[{#[[1]], ImageDimensions[img2][[2]] - #[[2]]} &, Reverse /@ lsPoints];
ListPlot[lsPoints2]
Rescale into the histogram image ranges:
lsPoints3 = N@Transpose[{Rescale[lsPoints2[[All, 1]], MinMax[lsPoints2[[All, 1]]], {-60, -35}], Rescale[lsPoints2[[All, 2]], MinMax[lsPoints2[[All, 2]]], {0, 610}]}];
Length[lsPoints3]
ListPlot[lsPoints3]
(*2043*)
Estimate the number of points
Aggregate by the x-coordinate and find the means of the corresponding y-coordinates:
lsPoints4 = Sort@Values[GroupBy[lsPoints3, First, {#[[1, 1]], Mean[#[[All, 2]]]} &]];
Length[lsPoints4]
(*622*)
Make an interpolation function:
ifunc = Interpolation[lsPoints4, InterpolationOrder -> 1]
Plot[ifunc[x], {x, lsPoints4[[1, 1]], lsPoints4[[-1, 1]]}]
Find the width for each bin:
Tally[Differences[lsPoints4[[All, 1]]]]
(*{{0.0402576, 559}, {0.0402576, 62}}*)
binWidth = Mean[Differences[lsPoints4[[All, 1]]]]
(*0.0402576*)
Well, this bin width estimation is more of an artifact of EdgeDetect
; the histogram bin seems to be a few times wider.
Here is the estimate for total sum of points:
nPoints = Ceiling[Total[lsPoints4[[All, 2]]]/4]
(* 31350 *)
Generate “histogram image estimated” points
Generate the points:
lsIntervals = RandomChoice[lsPoints4[[All, 2]] -> lsPoints4[[All, 1]], Floor[nPoints/10]];
ResourceFunction["RecordsSummary"][lsIntervals]
lsGeneratedPoints = RandomReal[{#, # + binWidth}] & /@ lsIntervals;
Histogram[lsGeneratedPoints, {lsPoints4[[1, 1]], lsPoints4[[-1, 1]], 10 binWidth}]
Here is the original image with the histogram (looks close enough):
img0
Find the distribution
Over the generated points above we apply
FindDistribution
:
FindDistribution[lsGeneratedPoints]
(*NormalDistribution[-46.7283, 3.68475]*)
Verification
SeedRandom[6];
Histogram[
RandomVariate[
NormalDistribution[-46.728321499135916`, 3.684750636542824`], 10000], 50]
histx
in theNonlinearModelFit
. When posting questions, include a minimal working example to include a representative (but not necessarily complete) data set that reproduces the problem that you are experiencing. $\endgroup$NonlinearModelFit
) to fit a histogram. If you really have a normal distribution, all you need to do is calculate the mean and standard deviation. $\endgroup$