3
$\begingroup$

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

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

enter image description here

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]

enter image description here

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?

$\endgroup$
4
  • 1
    $\begingroup$ 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. $\endgroup$
    – kglr
    Dec 9 '20 at 3:19
  • 1
    $\begingroup$ 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. $\endgroup$
    – stathisk
    Dec 9 '20 at 4:39
  • 1
    $\begingroup$ stathsik, you are right. I mis-interpreted the remark before the first example in SmoothKernelDistribution>> Options>>MaxMixtureKernels. $\endgroup$
    – kglr
    Dec 9 '20 at 5:00
  • 1
    $\begingroup$ 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/… $\endgroup$
    – stathisk
    Dec 9 '20 at 5:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.