# How to reproduce SmoothKernelDistribution[] for the bivariate case

I am trying to reproduce the results of SmoothKernelDistribution[] for the bivariate case.

pts = RandomVariate[BinormalDistribution[0.8], 100];
Histogram3D[pts, ColorFunction -> "TemperatureMap"]


p1 = Plot3D[
Evaluate@PDF[SmoothKernelDistribution[pts], {x, y}],
{x, -3, 3}, {y, -3, 3}, PlotRange -> All]

k[u_] := 1/(2 \[Pi]) * Exp[-u.u/2]
k[h_, u_] := Det[h]^(-1/2)*k[MatrixPower[h, -1/2].u]
f[x_, y_, h_] :=
With[{n = Length@pts}, (1/n) *
Sum[k[h, {x - First@pts[[i]], y - Last@pts[[i]]}], {i, 1, n}]] // N

h = SmoothKernelDistribution[pts]["Bandwidth"];
bw = {{First@h, 0}, {0, Last@h}};
p2 = Plot3D[f[x, y, bw], {x, -3, 3}, {y, -3, 3}, PlotRange -> All]

Style[Grid[{{p1, p2}}], ImageSizeMultipliers -> 1]


The plots differ a bit. It is as if my kernel density estimation has a larger bandwidth and things get smoothed out more than they should. Would you be so kind as to point me what I'm missing?

Relevant ref: What do the options of SmoothKernelDistribution do?

• this might explain some of differences between the two pictures: your f places a kernel at every sample point, but SmoothKernelDesitribution default value for the option MaxMixtureKernels not All, i.e it does not place a kernel at every data point.
– kglr
Dec 9 '20 at 3:19
• Thank you @kglr for taking the time to comment. I tried with SmoothKernelDistribution[pts, MaxMixtureKernels -> All], but the result is the same. Actually, it seems as if this is the default option: SmoothKernelDistribution[pts, MaxMixtureKernels -> All] == SmoothKernelDistribution[pts] returns True. Dec 9 '20 at 4:39
• stathsik, you are right. I mis-interpreted the remark before the first example in SmoothKernelDistribution>> Options>>MaxMixtureKernels.
– kglr
Dec 9 '20 at 5:00
• However, your comment was in the right direction; the options MaxRecursion and InterpolationPoints imply that SmoothKernelDistribution[] calculation is more sophisticated. Relevant: mathematica.stackexchange.com/questions/4862/… Dec 9 '20 at 5:14