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!
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
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>,
>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}]]
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
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
--
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.
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
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
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