Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Fit Gaussian function to histogram

2,806 views
Skip to first unread message

cubsfan334

unread,
Mar 29, 2011, 7:54:26 AM3/29/11
to
Hi,

I realize that I can generate a histogram from a data set using the
Histogram[{data},bin size] command, yet this only seems to create a
graphic. Is there anyway to fit a function (preferably Gaussian) to
the histogram Mathematica creates?

Thanks!

Richard Hofler

unread,
Mar 30, 2011, 5:08:39 AM3/30/11
to
Hello,

Try this:

data=Table[Random[NormalDistribution[10,2]],{10000}];
histo=Histogram[data];
plt=Plot[2000 PDF[NormalDistribution[10,2]][x],{x,4,16}];
Show[histo,plt]

Richard

Barrie Stokes

unread,
Mar 30, 2011, 5:14:42 AM3/30/11
to
Hi

The first thing I would do would be to find m=Mean[ data ] and sd=StandardDeviation[ data ], and then plot together the histogram and something like

Plot[ PDF[ NormalDistribution[ m, sd ], x ], {x, Min [data ], Max[ data] ],

and see if the Gaussian is at all a reasonable fit.

Fitting distributions to data is a big area, and there are tests for closeness of fit. Have a look through the Mathematica documentation.

There is

DistributionFitTest[ data, Automatic, "HypothesisTestData" ] in the documentation for DistributionFitTest.

(I found this by searching the online documentation for "Kolmogorov", because I know that the Kolmogorov-Smirnov test is such a test.)

Have a look also at AndersonDarlingTest[].

If you enter "fitting Normal distribution to data" into the search box for the online documentation, the second screen will lead you to

FindDistributionParameters[], and it will mention also EstimatedDistribution[].

Mathematica can give you a lot of power to attack a problem like this. There is a lot of example code in these documentation pages, too.

Cheers

Barrie

>>> On 29/03/2011 at 10:54 pm, in message <2011032911...@smc.vnet.net>,

Bill Rowe

unread,
Mar 30, 2011, 5:16:23 AM3/30/11
to
On 3/29/11 at 6:54 AM, cubsf...@gmail.com (cubsfan334) wrote:

>I realize that I can generate a histogram from a data set using the
>Histogram[{data},bin size] command, yet this only seems to create a
>graphic. Is there anyway to fit a function (preferably Gaussian) to
>the histogram Mathematica creates?

Is this something like what you want to do?

data = RandomReal[NormalDistribution[], 1000];

Show[Histogram[data, Automatic, "PDF"],
Plot[PDF[NormalDistribution[], x], {x, -3, 3}]]


Kevin J. McCann

unread,
Mar 31, 2011, 5:02:28 AM3/31/11
to
Histogram gives the plot, but if you want the actual bin counts, use
BinCounts. The mma help will give you the options to call it; I usually use

BinCounts[data,dx] in which dx is the bin width. You can also specify
all the bins with BinCounts[data,{xmin,xmax,dx}]. It is then easy to
convert the counts to probability densities.

Kevin

Alexei Boulbitch

unread,
Mar 31, 2011, 7:04:33 AM3/31/11
to
Hi,

try this:


data = RandomVariate[NormalDistribution[], 1000];
hist = Histogram[data, "Wand", "PDF", Frame -> True];
dist = EstimatedDistribution[data,
NormalDistribution[\[Mu], \[Sigma]]];
distPlot = Plot[PDF[dist, x], {x, -4, 4}, PlotStyle -> {Thick, Red}];
Show[{hist, unimodalDistPlot1}, Epilog -> Inset[
Panel@Column[{
Row[{Style["\[Mu] \[TildeTilde]", 12, Blue], Spacer[5],
Style[NumberForm[dist[[1]], {4, 2}], 12, Blue]
}],

Row[{Style["\[Sigma] \[TildeTilde]", 12, Blue], Spacer[5],
Style[NumberForm[dist[[2]], {4, 2}], 12, Blue]
}],
Row[{Style["Test: ", 10, Blue], Spacer[5],

Style[NumberForm[DistributionFitTest[data, dist],
{4, 3}], 12, Blue]
}]
}], Scaled[{0.15, 0.7}]
]]

Fave fun, Alexei

-----Original Message-----
From: cubsfan334 [mailto:cubsfan334 at gmail.com]
Sent: Tuesday, March 29, 2011 7:55 AM
To: mathgroup at smc.vnet.net
Subject: Fit Gaussian function to histogram

Hi,

I realize that I can generate a histogram from a data set using the
Histogram[{data},bin size] command, yet this only seems to create a
graphic. Is there anyway to fit a function (preferably Gaussian) to
the histogram Mathematica creates?

Thanks!


--
Alexei Boulbitch, Dr. habil.
Senior Scientist
Material Development

IEE S.A.
ZAE Weiergewan
11, rue Edmond Reuter
L-5326 CONTERN
Luxembourg

Tel: +352 2454 2566
Fax: +352 2454 3566
Mobile: +49 (0) 151 52 40 66 44

e-mail: alexei.b...@iee.lu

www.iee.lu

--

This e-mail may contain trade secrets or privileged, undisclosed or
otherwise confidential information. If you are not the intended
recipient and have received this e-mail in error, you are hereby
notified that any review, copying or distribution of it is strictly
prohibited. Please inform us immediately and destroy the original
transmittal from your system. Thank you for your co-operation.


dr DanW

unread,
Apr 1, 2011, 3:35:50 AM4/1/11
to
I find it easier to fit to the CDF. The shape of the histogram depends on bin widths, so some of the decisions you have to make to get a good quality fit are arbitrary. The cumulative frequency of your data, however, is unambiguous.

CumulativeFrequency[dat_] := With[{tsd = Tally[Sort[dat]]},
Transpose[{tsd[[All,1]], Accumulate[tsd[[All,2]]]/(Length[dat] + 1)}]]

FitCDF[d_] := Module[{mean, sd, cf, param}, mean = Mean[d]; sd = StandardDeviation[d];
cf = CumulativeFrequency[d]; param = FindFit[cf, CDF[NormalDistribution[\[Mu], \[Sigma]],
x], {{\[Mu], mean}, {\[Sigma], sd}}, x];
Print[Show[{ListPlot[cf], Plot[Evaluate[CDF[NormalDistribution[\[Mu], \[Sigma]], x] /.
param], Evaluate[{x, \[Mu] - 3*\[Sigma], \[Mu] + 3*\[Sigma]} /. param]]}]]; param]

Notice that this technique works with any distribution (with suitable modification of FitCDF).

Mathematica 8 has a lot of new tools for statistics and probability, many of which I do not understand. Perhaps you are looking for HypothesisTesting`?

Enjoy,
Daniel

Mark Fisher

unread,
Apr 2, 2011, 6:05:41 PM4/2/11
to

Maybe you're interesting in something like this:

data = RandomReal[NormalDistribution[], 100];
histo = Histogram[data, 10, "PDF"];
hdata = Cases[histo,
RectangleBox[{x0_, _}, {x1_, y_}, _] :> {.5 (x0 + x1),
y}, \[Infinity]];
fun = PDF[NormalDistribution[m, s], x] /.
FindFit[hdata, PDF[NormalDistribution[m, s], x], {m, s}, {x}];
Show[histo, Plot[fun, {x, Min[data], Max[data]}]]

--Mark

Ray Koopman

unread,
Apr 11, 2011, 7:07:56 AM4/11/11
to

There are several ways to do it, depending on how you use the data.
See the thread "Fitting a normal distribution to a histogram",
Jun 19-23, 2008, at
https://groups.google.com/group/sci.stat.math/browse_frm/thread/ca471d2c3a09a620/4581b219f3ddea2b?hl=en

0 new messages