# Create Confidence Envelopes about Nonparametric Density Estimates

Use several bandwidth-selection methods to determine a proper bandwidth. Estimate a bootstrapped 95% pointwise confidence band for the kernel density estimate.
 In[1]:= Xdata = BlockRandom[SeedRandom[3]; RandomVariate[ dist = MixtureDistribution[{1/2, 3, 1}, {NormalDistribution[-4, .4], NormalDistribution[0, 2], NormalDistribution[2.5, .6]}], 500]];
 In[2]:= X\[ScriptCapitalD] = SmoothKernelDistribution[data]; bSamp = RandomChoice[data, {250, Length[data]}]; Subscript[\[ScriptCapitalD], B] = SmoothKernelDistribution[#] & /@ bSamp;
 In[3]:= Xpdf = Table[ PDF[i, rng = Range[-7., 7, .05]], {i, Subscript[\[ScriptCapitalD], B]}]; High = Table[Quantile[i, .975], {i, Transpose[pdf]}]; Low = Table[Quantile[i, .025], {i, Transpose[pdf]}]; p1 = Show[ ListLinePlot[{Transpose[{rng, High}], Transpose[{rng, Low}]}, Filling -> {1 -> {{2}, Automatic}}, PlotRange -> {0, .25}, PlotStyle -> Dashed], Plot[PDF[\[ScriptCapitalD], x], {x, -7, 7}, PlotStyle -> {Thick, Blue}, PlotRange -> {0, .25}], Frame -> True, Axes -> None, ImageSize -> 570]; dists2 = Table[ SmoothKernelDistribution[data, i], {i, {"Oversmooth", "Silverman", "SheatherJones"}}]; p2 = Plot[Evaluate[PDF[#, x] & /@ dists2], {x, -8, 8}, Frame -> True, Axes -> None, ImageSize -> 570]; Grid[{{p2}, {p1}}]
 Out[3]=