1
$\begingroup$

I want to use KernelMixtureDistribution to deal with a large amount of data points. For example, the following codes shows how I use it:

data = RandomReal[{0, 1}, {10000, 2}];
d1 = KernelMixtureDistribution[data, MaxMixtureKernels -> All]
ContourPlot[PDF[d1, {x, y}], {x, 0, 1}, {y, 0, 1}, PlotRange -> All, ColorFunction -> "TemperatureMap", Contours -> 20]

and it works fine. However, I want to specify the bandwidth manually, like this:

d2 = KernelMixtureDistribution[data, 0.01, MaxMixtureKernels -> All]
ContourPlot[PDF[d2, {x, y}], {x, 0, 1}, {y, 0, 1}, PlotRange -> All, ColorFunction -> "TemperatureMap", Contours -> 20]

then it gives me a lot of error message and gets stuck:

Error message

To specify a bandwidth manually is important for me. So my question is: how to fix that?

$\endgroup$

2 Answers 2

0
$\begingroup$

With even a moderate sample size you're going to get pretty much the same resulting contour plot no matter what kernel type you use (even if the kernel is the "Rectangular" kernel). So avoid the "Gaussian" kernel for large sample sizes and you won't see those warning messages.

data = RandomReal[{0, 1}, {10000, 2}];
d2 = KernelMixtureDistribution[data, 0.01, "Epanechnikov", MaxMixtureKernels -> All];
ContourPlot[PDF[d2, {x, y}], {x, 0, 1}, {y, 0, 1}, PlotRange -> All, 
 ColorFunction -> "TemperatureMap", Contours -> 20]

Contour plot of kernel density

$\endgroup$
0
$\begingroup$
SeedRandom[1234];

data = RandomReal[{0, 1}, {10000, 2}];

Use Manipulate to investigate changes in the bandwidth and/or MaxMixtureKernels.

Manipulate[
 d2 = KernelMixtureDistribution[data, bw,
   MaxMixtureKernels -> mmk];
 cp = ContourPlot[
   PDF[d2, {x, y}], {x, 0, 1}, {y, 0, 1},
   PlotRange -> All,
   ColorFunction -> "TemperatureMap",
   Contours -> 20,
   ImageSize -> 324,
   WorkingPrecision -> 15],
 Row[{Control[{{bw, 0.05, "bandwidth"}, 0.01, 0.2, 0.01, 
     Appearance -> "Labeled",
     ImageSize -> Small}],
   Spacer[15],
   Control[{{mmk, All, "MaxMixtureKernels"}, {Automatic, All, 10, 15, 
      20}}]}],
 TrackedSymbols -> True,
 SynchronousUpdating -> False]

enter image description here

As stated in the error messages, when the bandwidth is made too small some of the exponential terms cannot be represented. Specifying a WorkingPrecision helps some, but this slows down the calculations.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.