Re: Re: Gaussian PDF Overlay
- To: mathgroup at smc.vnet.net
- Subject: [mg19318] Re: [mg19283] Re: [mg19277] Gaussian PDF Overlay
- From: BobHanlon at aol.com
- Date: Sat, 14 Aug 1999 23:42:49 -0400
- Sender: owner-wri-mathgroup at wolfram.com
Lawrence, I had a mistake in the scaling for my earlier reply. This is a corrected version, formatted as a function. Needs["Statistics`ContinuousDistributions`"]; Needs["Statistics`DataManipulation`"]; Needs["Graphics`Graphics`"]; Needs["Utilities`FilterOptions`"]; Options[plotContDistData] = {nbrBins -> 10}; plotContDistData::usage = "plotContDistData[dist, data] overlays the PDF for the specified \ continuous distribution over a bar chart of the data list."; plotContDistData[dist_, data_List, opts___?OptionQ] := Module[{mu, sigma, xmin, xmax, pltPDF, x, nbrVal = Length[data], step, k, pltData, nBins, pltOpts}, nBins = (nbrBins /. Flatten[{opts}]) /. Options[plotContDistData]; pltOpts = FilterOptions[Plot, opts]; mu = N[Mean[dist]]; sigma = N[StandardDeviation[dist]]; xmin = Max[mu - 3sigma, Domain[dist][[1, 1]]]; xmax = Min[mu + 3sigma, Domain[dist][[1, 2]]]; pltPDF = Plot[PDF[dist, x], {x, xmin, xmax}, PlotStyle -> AbsoluteThickness[2], DisplayFunction -> Identity, Evaluate[pltOpts]]; step = (xmax - xmin)/nBins; pltData = GeneralizedBarChart[ Transpose[{Table[xmin + step(k - 1/2), {k, nBins}], BinCounts[data, {xmin, xmax, step}]/(step*nbrVal), Table[step, {nBins}]}], DisplayFunction -> Identity, Evaluate[pltOpts]]; Show[{pltData, pltPDF}, DisplayFunction -> $DisplayFunction]]; Examples dist = NormalDistribution[8, 2]; data = RandomArray[dist, 1000]; plotContDistData[dist, data, nbrBins -> 15]; dist = BetaDistribution[2, 2]; data = RandomArray[dist, 2000]; plotContDistData[dist, data, nbrBins -> 20]; dist = HalfNormalDistribution[5]; data = RandomArray[dist, 1000]; plotContDistData[dist, data]; dist = GammaDistribution[1, 5]; data = RandomArray[dist, 1000]; plotContDistData[dist, data]; Bob Hanlon In a message dated 8/14/99 5:14:28 AM, BobHanlon at aol.com writes: >Use a GeneralizedBarChart: > >Needs["Statistics`ContinuousDistributions`"] >Needs["Statistics`DataManipulation`"]; >Needs["Graphics`Graphics`"]; > >dist = NormalDistribution[mu = 4., sigma = 2.]; > >xmin = mu - 3sigma; xmax = mu + 3sigma; > >plt1 = Plot[PDF[dist, x], {x, xmin, xmax}, PlotStyle -> AbsoluteThickness[2], > > DisplayFunction -> Identity]; > >nbrSamples = 1000; >data = RandomArray[dist, nbrSamples]; >nbrBins = 10; >step = (xmax - xmin)/nbrBins; >freq = BinCounts[data, {xmin, xmax, step}]/nbrSamples; >midPoints = Table[xmin + step(k - 1/2), {k, nbrBins}]; > >plt2 = GeneralizedBarChart[Transpose[{midPoints, freq, Table[1, {nbrBins}]}], > > DisplayFunction -> Identity]; > >Show[{plt2, plt1}, DisplayFunction -> $DisplayFunction]; >