---
title: "SmoothKernelDistribution"
language: "en"
type: "Symbol"
summary: "SmoothKernelDistribution[{x1, x2, ...}] represents a smooth kernel distribution based on the data values xi. SmoothKernelDistribution[{{x1, y1, ...}, {x2, y2, ...}, ...}] represents a multivariate smooth kernel distribution based on the data values {xi, yi, ...}. SmoothKernelDistribution[..., bw] represents a smooth kernel distribution with bandwidth bw. SmoothKernelDistribution[..., bw, ker] represents a smooth kernel distribution with bandwidth bw and smoothing kernel ker."
keywords: 
- kernel density estimate
- kernel density estimator
- kernel smoothing
- kernel smoother
- non-parametric kernel density estimate
- non-parametric kernel density estimator
- non-parametric kernel smoothing
- non-parametric kernel smoother
- Parzen window method
- Epanechnikov
- biweight kernel
- quartic kernel
- triweight kernel
- triangular kernel
- rectangular kernel
- uniform kernel
- Gaussian kernel
- exponential kernel
- cosine kernel
- opt cosine kernel
- smooth histogram
- non-parametric statistics
- kdensity
- ksmooth
- kde
- proc univariate
canonical_url: "https://reference.wolfram.com/language/ref/SmoothKernelDistribution.html"
source: "Wolfram Language Documentation"
related_guides: 
  - 
    title: "Probability & Statistics with Quantities"
    link: "https://reference.wolfram.com/language/guide/ProbabilityWithQuantities.en.md"
  - 
    title: "Nonparametric Statistical Distributions"
    link: "https://reference.wolfram.com/language/guide/NonparametricStatisticalDistributions.en.md"
  - 
    title: "Probability & Statistics"
    link: "https://reference.wolfram.com/language/guide/ProbabilityAndStatistics.en.md"
  - 
    title: "Random Variables"
    link: "https://reference.wolfram.com/language/guide/RandomVariables.en.md"
  - 
    title: "Tabular Modeling"
    link: "https://reference.wolfram.com/language/guide/TabularModeling.en.md"
  - 
    title: "Unsupervised Machine Learning"
    link: "https://reference.wolfram.com/language/guide/UnsupervisedMachineLearning.en.md"
  - 
    title: "Survival Analysis"
    link: "https://reference.wolfram.com/language/guide/SurvivalAnalysis.en.md"
related_functions: 
  - 
    title: "KernelMixtureDistribution"
    link: "https://reference.wolfram.com/language/ref/KernelMixtureDistribution.en.md"
  - 
    title: "HistogramDistribution"
    link: "https://reference.wolfram.com/language/ref/HistogramDistribution.en.md"
  - 
    title: "EmpiricalDistribution"
    link: "https://reference.wolfram.com/language/ref/EmpiricalDistribution.en.md"
  - 
    title: "SurvivalDistribution"
    link: "https://reference.wolfram.com/language/ref/SurvivalDistribution.en.md"
  - 
    title: "EstimatedDistribution"
    link: "https://reference.wolfram.com/language/ref/EstimatedDistribution.en.md"
  - 
    title: "FindDistribution"
    link: "https://reference.wolfram.com/language/ref/FindDistribution.en.md"
---
# SmoothKernelDistribution

SmoothKernelDistribution[{x1, x2, …}] represents a smooth kernel distribution based on the data values xi.

SmoothKernelDistribution[{{x1, y1, …}, {x2, y2, …}, …}] represents a multivariate smooth kernel distribution based on the data values {xi, yi, …}.

SmoothKernelDistribution[…, bw] represents a smooth kernel distribution with bandwidth bw.

SmoothKernelDistribution[…, bw, ker] represents a smooth kernel distribution with bandwidth bw and smoothing kernel ker.

## Details and Options

* ``SmoothKernelDistribution`` returns a ``DataDistribution`` object that can be used like any other probability distribution.

* The probability density function for ``SmoothKernelDistribution`` for a value $x$ is given by a linearly interpolated version of $\frac{1}{n h}\sum _{i=1}^n k\left(\frac{x-x_i}{h}\right)$ for a smoothing kernel $k(x)$ and bandwidth parameter $h$.

* The following bandwidth specifications ``bw`` can be given:

|                     |                                                      |
| ------------------- | ---------------------------------------------------- |
| h                   | bandwidth to use                                     |
| {"Standardized", h} | bandwidth in units of standard deviations            |
| {"Adaptive", h, s}  | adaptive with initial bandwidth h and sensitivity s  |
| Automatic           | automatically computed bandwidth                     |
| "name"              | use a named bandwidth selection method               |
| {bwx, bwy, …}       | separate bandwidth specifications for x, y, etc.     |

* For multivariate densities, ``h`` can be a positive definite symmetric matrix.

* For adaptive bandwidths, the sensitivity ``s`` must be a real number between 0 and 1 or ``Automatic``. If ``Automatic`` is used, ``s`` is set to $1/(2 p)$, where $p$ is the dimensionality of the data.

* Possible named bandwidth selection methods include:

|                               |                                                  |
| ----------------------------- | ------------------------------------------------ |
| "LeastSquaresCrossValidation" | use the method of least-squares cross-validation |
| "Oversmooth"                  | 1.08 times wider than the standard Gaussian      |
| "Scott"                       | use Scott's rule to determine bandwidth          |
| "SheatherJones"               | use the Sheather–Jones plugin estimator          |
| "Silverman"                   | use Silverman's rule to determine bandwidth      |
| "StandardDeviation"           | use the standard deviation as bandwidth          |
| "StandardGaussian"            | optimal bandwidth for standard normal data       |

* By default, the ``"Silverman"`` method is used.

* For automatic bandwidth computation, constant arrays are assumed to have unit variance.

* The following kernel specifications ``ker`` can be given:

|                |                                                                                    |                                                               |
| -------------- | ---------------------------------------------------------------------------------- | ------------------------------------------------------------- |
| "Biweight"     | $\frac{15}{16}\left(1 -- u^2\right)^2$               | $-1<u<1$                        |
| "Cosine"       | $\frac{\pi }{4}\cos \left(\frac{\pi  u}{2}\right)$   | $-1<u<1$                        |
| "Epanechnikov" | $\frac{3}{4\sqrt{5}}\left(1 -- \frac{u^2}{5}\right)$ | $-\sqrt{5}<u<\sqrt{5}$          |
| "Gaussian"     | $\frac{1}{\sqrt{2\pi }}e^{-\frac{u^2}{2}}$           | $u\in \mathbb{R}$               |
| "Rectangular"  | $\frac{1}{2}$                                        | $-1<u<1$                        |
| "SemiCircle"   | $\frac{2}{\pi }\sqrt{1 -- u^2}$                      | $-1<u<1$                        |
| "Triangular"   | $1 -- \| u\|$                                        | $-1<u<1$                        |
| "Triweight"    | $\frac{35}{32}\left(1 -- u^2\right)^3$               | $-1<u<1$                        |
| func           | $$\text{$}f_n\text{$}$$                                  | $$\text{$}u\in \mathbb{R}\text{$}$$ |

* In order for ``SmoothKernelDistribution`` to generate a true density estimate, the function ``fn`` should be a valid probability density function.

* By default, the ``"Gaussian"`` kernel is used.

* The kernel function ``ker`` can be specified to account for known bounding on the underlying density using ``{"Bounded", c, ker}``, where ``c`` can be any real number, a list ``{c1, c2}`` such that ``c1 < c2``, or a list ``{{c11, c12}, {c21, c22}, …}``, with length equal to the dimension of ``data``.

* For multivariate densities, the kernel function ``ker`` can be specified as product and radial types using ``{"Product", ker}`` and ``{"Radial", ker}``, respectively. Product-type kernels are used if no type is specified.

* The precision used for density estimation is the minimum precision given in the ``bw`` and data.

* The following options can be given:

|                      |           |                                               |
| -------------------- | --------- | --------------------------------------------- |
| InterpolationPoints  | Automatic | initial number of interpolation points to use |
| MaxMixtureKernels    | Automatic | max number of kernels to use                  |
| MaxRecursion         | Automatic | number of recursive subdivisions to allow     |
| PerformanceGoal      | Automatic | optimize for speed or quality                 |
| MaxExtraBandwidths   | Automatic | max bandwidths beyond data to use             |

* ``SmoothKernelDistribution`` can be used with such functions as ``Mean``, ``CDF``, and ``RandomVariate``.

---

## Examples (92)

### Basic Examples (2)

Create an interpolated version of a kernel density estimate for some univariate data:

```wl
In[1]:= data = RandomVariate[NormalDistribution[], 10 ^ 3];

In[2]:= \[ScriptCapitalD] = SmoothKernelDistribution[data];
```

Use the resulting distribution to perform analysis, including visualizing distribution functions:

```wl
In[3]:= Table[Plot[f[\[ScriptCapitalD], x], {x, -4, 4}, PlotLabel -> f], {f, {PDF, CDF}}]

Out[3]= {[image], [image]}
```

Compute moments and quantiles:

```wl
In[4]:= Moment[\[ScriptCapitalD], 2]

Out[4]= 1.10239

In[5]:= Quantile[\[ScriptCapitalD], 0.95]

Out[5]= 1.72748
```

---

Create an interpolated version of a kernel density estimate of some bivariate data:

```wl
In[1]:= data = RandomVariate[BinormalDistribution[.75], 10];

In[2]:= \[ScriptCapitalD] = SmoothKernelDistribution[data];
```

Visualize the estimated PDF and CDF:

```wl
In[3]:= Table[ContourPlot[f[\[ScriptCapitalD], {x, y}], {x, -3, 3}, {y, -3, 3}, PlotLabel -> f, PlotRange -> All], {f, {PDF, CDF}}]

Out[3]= {[image], [image]}
```

Compute covariance and general moments:

```wl
In[4]:= Covariance[\[ScriptCapitalD]]//MatrixForm

Out[4]//MatrixForm=
(⁠|          |          |
| -------- | -------- |
| 1.20064  | 0.706679 |
| 0.706679 | 1.41701  |⁠)

In[5]:= Moment[\[ScriptCapitalD], {1, 2}]

Out[5]= -1.37949
```

### Scope (37)

#### Basic Uses (7)

Create an interpolated smooth density estimate for some data:

```wl
In[1]:= data = RandomVariate[NormalDistribution[], 10^4];

In[2]:= \[ScriptCapitalD] = SmoothKernelDistribution[data];

In[3]:= Show[Histogram[data, Automatic, "PDF"], Plot[PDF[\[ScriptCapitalD], x], {x, -4, 4}]]

Out[3]= [image]
```

Compute probabilities from the distribution:

```wl
In[4]:= Probability[x > 3, x\[Distributed]\[ScriptCapitalD]]

Out[4]= 0.00159408
```

---

Create an interpolated version of a kernel density estimate for data with quantities:

```wl
In[1]:= data = QuantityArray[RandomVariate[NormalDistribution[1, 2.5], 10 ^ 4], "Meters"]

Out[1]=
StructuredArray[QuantityArray, {10000}, StructuredArray`StructuredData[QuantityArray, 
  CompressedData["«108960»"], "Meters", {{1}}]]

In[2]:= \[ScriptCapitalD] = SmoothKernelDistribution[data]

Out[2]=
DataDistribution["SmoothKernel", {CompressedData["«5566»"], 
  CompressedData["«5558»"], 0.35573500399767344}, 1, 10000, "Meters"]
```

Find moments:

```wl
In[3]:= #[\[ScriptCapitalD]]& /@ {Mean, Variance, Skewness, Kurtosis}

Out[3]= {Quantity[0.9991393497188261, "Meters"], Quantity[6.3466558333625995, "Meters"^2], 0.0107567, 3.01791}
```

---

Increase the bandwidth for smoother estimates:

```wl
In[1]:= data = RandomVariate[NormalDistribution[], 5];

In[2]:= bw = {.1, .2, .5, 1.0};

In[3]:= Table[Plot[PDF[SmoothKernelDistribution[data, i], x]//Evaluate, {x, -4, 4}, PlotLabel -> Row[{"bandwidth = ", i}], PlotRange -> {0, 1.6}, Filling -> Axis], {i, bw}]

Out[3]= [image]
```

---

Allow the bandwidth to vary adaptively with local density:

```wl
In[1]:= data = RandomVariate[\[ScriptD] = MixtureDistribution[{1, 2}, {NormalDistribution[], NormalDistribution[2, 1 / 2]}], 10^4];

In[2]:= \[ScriptCapitalD] = SmoothKernelDistribution[data, {"Adaptive", Automatic, .5}];

In[3]:= Plot[{PDF[\[ScriptD], x], PDF[\[ScriptCapitalD], x]}, {x, -4, 4}, PlotLegends -> {"\[ScriptD]", "\[ScriptCapitalD]"}]

Out[3]= [image]
```

---

Interpolate kernel density estimates in higher dimensions:

```wl
In[1]:= data = RandomVariate[NormalDistribution[], {10, 3}];

In[2]:= \[ScriptCapitalD] = SmoothKernelDistribution[data, .5];
```

Plot the univariate marginal PDFs:

```wl
In[3]:= Table[Plot[Evaluate[PDF[MarginalDistribution[\[ScriptCapitalD], i], x]], {x, -4, 4}, PlotLabel -> i], {i, {1, 2, 3}}]

Out[3]= {[image], [image], [image]}
```

Plot the bivariate marginal PDFs:

```wl
In[4]:= Table[Plot3D[Evaluate[PDF[MarginalDistribution[\[ScriptCapitalD], i], {x, y}]], {x, -4, 4}, {y, -4, 4}, PlotRange -> All, PlotLabel -> i], {i, Subsets[{1, 2, 3}, {2}]}]

Out[4]= {[image], [image], [image]}
```

---

Select from built-in kernel functions or build a custom one:

```wl
In[1]:= data = RandomVariate[NormalDistribution[], 100];

In[2]:= kerns = {"Gaussian", "Rectangular", "Triangular"};

In[3]:= \[ScriptCapitalD]s = Table[SmoothKernelDistribution[data, Automatic, k], {k, kerns}];

In[4]:= MapThread[Plot[PDF[#1, x], {x, -4, 4}, Exclusions -> None, PlotLabel -> #2]&, {\[ScriptCapitalD]s, kerns}]

Out[4]= {[image], [image], [image]}
```

A custom kernel function:

```wl
In[5]:= \[ScriptCapitalD]custom = SmoothKernelDistribution[data, Automatic, PDF[NormalDistribution[], #]&];

In[6]:= Plot[PDF[\[ScriptCapitalD]custom, x], {x, -4, 4}]

Out[6]= [image]
```

---

Specify radial- or product-type kernels for multivariate estimates:

```wl
In[1]:= data = RandomVariate[BinormalDistribution[2 / 5], 250];

In[2]:= kernel = PDF[CauchyDistribution[0, 1], #]&;

In[3]:= \[ScriptCapitalD] = Table[SmoothKernelDistribution[data, {{1, .25}, {.25, 1}}, k], {k, {{"Radial", kernel}, {"Product", kernel}}}];

In[4]:= Table[Plot3D[PDF[i, {x, y}]//Evaluate, {x, -5, 5}, {y, -5, 5}, PlotRange -> All, Exclusions -> None, MeshFunctions -> (#3&)], {i, \[ScriptCapitalD]}]

Out[4]= {[image], [image]}
```

#### Distribution Properties (8)

Estimate distribution functions:

```wl
In[1]:= \[ScriptCapitalD] = SmoothKernelDistribution[RandomVariate[NormalDistribution[], 1000]];

In[2]:= Table[Plot[f[\[ScriptCapitalD], x], {x, -4, 4}, Filling -> Axis, PlotLabel -> f, Exclusions -> None], {f, {PDF, CDF, HazardFunction, SurvivalFunction}}]

Out[2]= [image]
```

---

Compute moments of the distribution:

```wl
In[1]:= \[ScriptCapitalD] = SmoothKernelDistribution[RandomReal[NormalDistribution[], 1000]];
```

Special moments:

```wl
In[2]:= {Mean[\[ScriptCapitalD]], Variance[\[ScriptCapitalD]], Skewness[\[ScriptCapitalD]], Kurtosis[\[ScriptCapitalD]]}

Out[2]= {-0.020898, 1.12084, 0.0693346, 3.12694}
```

General moments:

```wl
In[3]:= Table[Moment[\[ScriptCapitalD], k], {k, 4}]

Out[3]= {-0.020898, 1.12127, 0.0119955, 3.92436}

In[4]:= Table[CentralMoment[\[ScriptCapitalD], k], {k, 4}]

Out[4]= {0, 1.12084, 0.0822743, 3.9283}

In[5]:= Table[Cumulant[\[ScriptCapitalD], k], {k, 4}]

Out[5]= {-0.020898, 1.12084, 0.0822743, 0.15947}

In[6]:= Table[FactorialMoment[\[ScriptCapitalD], k], {k, 4}]

Out[6]= {-0.020898, 1.14217, -3.39362, 16.3118}
```

---

Quantile function:

```wl
In[1]:= \[ScriptCapitalD] = SmoothKernelDistribution[RandomReal[NormalDistribution[], 1000]];

In[2]:= Plot[Quantile[\[ScriptCapitalD], p], {p, 0, 1}, Filling -> Axis, Exclusions -> None]

Out[2]= [image]
```

Special quantile values:

```wl
In[3]:= Quartiles[\[ScriptCapitalD]]

Out[3]= {-0.708134, -0.00247263, 0.684967}

In[4]:= InterquartileRange[\[ScriptCapitalD]]

Out[4]= 1.3931

In[5]:= Quantile[\[ScriptCapitalD], {0.05, 0.95}]

Out[5]= {-1.64729, 1.52832}

In[6]:= Median[\[ScriptCapitalD]]

Out[6]= -0.00247263
```

---

Generate random numbers:

```wl
In[1]:= \[ScriptCapitalD] = SmoothKernelDistribution[RandomVariate[NormalDistribution[], 1000]];

In[2]:= RandomVariate[\[ScriptCapitalD], 10]

Out[2]= {0.360321, -1.63424, -0.0625419, 0.784392, 0.92129, -0.306278, -0.456075, -1.22149, -0.380103, 1.17629}
```

Compare with ``SmoothKernelDistribution`` :

```wl
In[3]:=
Show[Histogram[RandomVariate[\[ScriptCapitalD], 10^4], Automatic, "PDF"], 
	Plot[PDF[\[ScriptCapitalD], x], {x, -4, 4}]]

Out[3]= [image]
```

---

Compute probabilities and expectations:

```wl
In[1]:= data = RandomReal[NormalDistribution[], 1000];

In[2]:= \[ScriptCapitalD] = SmoothKernelDistribution[data];

In[3]:= NProbability[x > 2, x\[Distributed]\[ScriptCapitalD]]

Out[3]= 0.0227562

In[4]:= NExpectation[x ^ 2 + 3x  + 5, x\[Distributed]\[ScriptCapitalD]]

Out[4]= 6.06316
```

---

Estimate bivariate distribution functions:

```wl
In[1]:= \[ScriptCapitalD] = SmoothKernelDistribution[RandomVariate[BinormalDistribution[.75], 100]];

In[2]:=
Table[DiscretePlot3D[Evaluate[f[\[ScriptCapitalD], {x, y}]], {x, -4, 4, .5}, {y, -4, 4, .5}, PlotLabel -> f, 
	ExtentSize -> 1 / 2], {f, {PDF, CDF, HazardFunction, SurvivalFunction}}]

Out[2]= {[image], [image], [image], [image]}
```

---

Compute moments of a bivariate distribution:

```wl
In[1]:= \[ScriptCapitalD] = SmoothKernelDistribution[RandomReal[BinormalDistribution[.75], 1000]];
```

Special moments:

```wl
In[2]:= {Mean[\[ScriptCapitalD]], Variance[\[ScriptCapitalD]]}

Out[2]= {{0.0596306, 0.0205309}, {1.10877, 1.15505}}

In[3]:= Covariance[\[ScriptCapitalD]]//MatrixForm

Out[3]//MatrixForm=
(⁠|          |          |
| -------- | -------- |
| 1.10877  | 0.805948 |
| 0.805948 | 1.15505  |⁠)

In[4]:= Correlation[\[ScriptCapitalD]]//MatrixForm

Out[4]//MatrixForm=
(⁠|          |          |
| -------- | -------- |
| 1.       | 0.712172 |
| 0.712172 | 1.       |⁠)
```

General moments:

```wl
In[5]:= Moment[\[ScriptCapitalD], {1, 2}]

Out[5]= 0.0531729

In[6]:= CentralMoment[\[ScriptCapitalD], {1, 2}]

Out[6]= -0.0483038

In[7]:= Cumulant[\[ScriptCapitalD], {1, 2}]

Out[7]= -0.0483038

In[8]:= FactorialMoment[\[ScriptCapitalD], {1, 2}]

Out[8]= -0.75348
```

---

Generate random numbers:

```wl
In[1]:= \[ScriptCapitalD] = SmoothKernelDistribution[RandomReal[BinormalDistribution[.5], 10 ^ 2]];

In[2]:= RandomVariate[\[ScriptCapitalD], 6]

Out[2]= {{0.810211, -0.222298}, {0.145888, 0.4226}, {-1.73356, 1.86449}, {-0.721635, 0.410728}, {-2.31875, -2.293}, {0.984894, 0.00623788}}
```

Show the point distribution:

```wl
In[3]:= ListPlot[RandomVariate[\[ScriptCapitalD], 10^4], PlotRange -> {{-4, 4}, {-4, 4}}]

Out[3]= [image]
```

#### Bandwidth Selection (12)

Automatically select the bandwidth to use:

```wl
In[1]:=
data1 = RandomVariate[\[ScriptD] = NormalDistribution[], 10];
data2 = RandomVariate[\[ScriptD], 10 ^ 4];

In[2]:=
\[ScriptCapitalD]1 = SmoothKernelDistribution[data1];
\[ScriptCapitalD]2 = SmoothKernelDistribution[data2];
```

More data yields better approximations to the underlying distribution:

```wl
In[3]:= Table[Plot[{PDF[\[ScriptCapitalD], x], PDF[\[ScriptD], x]}, {x, -4, 4}, Filling -> Axis, Exclusions -> None], {\[ScriptCapitalD], {\[ScriptCapitalD]1, \[ScriptCapitalD]2}}]

Out[3]= [image]
```

---

Explicitly specify the bandwidth to use:

```wl
In[1]:= data = RandomVariate[NormalDistribution[], 10 ^ 3];
```

Use bandwidths of 0.1 and 1:

```wl
In[2]:=
\[ScriptCapitalD]1 = SmoothKernelDistribution[data, 0.1];
\[ScriptCapitalD]2 = SmoothKernelDistribution[data, 1.0];
```

Larger bandwidths yield smoother estimates:

```wl
In[3]:= Table[Plot[PDF[\[ScriptCapitalD], x], {x, -4, 4}, Filling -> Axis, Exclusions -> None], {\[ScriptCapitalD], {\[ScriptCapitalD]1, \[ScriptCapitalD]2}}]

Out[3]= [image]
```

---

Specify bandwidths in units of standard deviation:

```wl
In[1]:= data = RandomVariate[NormalDistribution[], 10 ^ 3];
```

Use bandwidths of $\frac{1}{2}$ and $\frac{1}{8}$ the standard deviation:

```wl
In[2]:=
\[ScriptCapitalD]1 = SmoothKernelDistribution[data, {"Standardized", 1 / 2}];
\[ScriptCapitalD]2 = SmoothKernelDistribution[data, {"Standardized", 1 / 8}];

In[3]:= Table[Plot[PDF[\[ScriptCapitalD], x], {x, -4, 4}, Filling -> Axis, Exclusions -> None], {\[ScriptCapitalD], {\[ScriptCapitalD]1, \[ScriptCapitalD]2}}]

Out[3]= [image]
```

---

Allow the bandwidth to vary adaptively with local density:

```wl
In[1]:= data = RandomVariate[NormalDistribution[], 10 ^ 3];
```

Vary the local sensitivity from ``0`` (none) to ``1`` (full):

```wl
In[2]:= Table[Plot[Evaluate[PDF[SmoothKernelDistribution[data, {"Adaptive", Automatic, s}], x]], {x, -4, 4}, PlotLabel -> Row[{"s = ", s}]], {s, {0, .25, .75, 1}}]

Out[2]= [image]
```

---

Vary the initial bandwidth for an adaptive estimate:

```wl
In[1]:= data = RandomVariate[NormalDistribution[], 10 ^ 3];
```

Specify an initial bandwidth of ``1.0`` and ``0.1``, respectively:

```wl
In[2]:=
\[ScriptCapitalD]1 = SmoothKernelDistribution[data, {"Adaptive", 1.0, .5}];
\[ScriptCapitalD]2 = SmoothKernelDistribution[data, {"Adaptive", 0.1, .5}];

In[3]:= Table[Plot[PDF[\[ScriptCapitalD], x], {x, -4, 4}, Filling -> Axis, Exclusions -> None], {\[ScriptCapitalD], {\[ScriptCapitalD]1, \[ScriptCapitalD]2}}]

Out[3]= [image]
```

---

Use any of several automatic bandwidth selection methods:

```wl
In[1]:= data = RandomVariate[NormalDistribution[], 100];

In[2]:= Table[Plot[Evaluate[PDF[SmoothKernelDistribution[data, name], x]], {x, -4, 4}, Filling -> Axis, Exclusions -> None, PlotLabel -> name], {name, {"LeastSquaresCrossValidation", "Oversmooth", "Scott", "SheatherJones", "StandardDeviation", "StandardGaussian"}}]

Out[2]= [image]
```

---

Silverman's method is used by default:

```wl
In[1]:= data = RandomVariate[NormalDistribution[], 10 ^ 3];

In[2]:=
\[ScriptCapitalD]1 = SmoothKernelDistribution[data, "Silverman"];
\[ScriptCapitalD]2 = SmoothKernelDistribution[data, Automatic];
```

The PDFs are equivalent:

```wl
In[3]:= Table[Plot[PDF[\[ScriptCapitalD], x], {x, -4, 4}, Filling -> Axis, Exclusions -> None], {\[ScriptCapitalD], {\[ScriptCapitalD]1, \[ScriptCapitalD]2}}]

Out[3]= [image]
```

---

By default, Silverman's method is used to independently select bandwidths in each dimension:

```wl
In[1]:= data = RandomVariate[BinormalDistribution[.75], 25];

In[2]:= Table[Plot3D[Evaluate[PDF[SmoothKernelDistribution[data, name], {x, y}]], {x, -3, 3}, {y, -3, 3}, PlotRange -> All, PlotLabel -> name, Exclusions -> None], {name, {Automatic, "Silverman"}}]

Out[2]= [image]
```

---

Any automated method can be used to independently select diagonal bandwidth elements:

```wl
In[1]:= data = RandomVariate[BinormalDistribution[.75], 25];

In[2]:= Table[Plot3D[Evaluate[PDF[SmoothKernelDistribution[data, name], {x, y}]], {x, -3, 3}, {y, -3, 3}, PlotRange -> All, PlotLabel -> name, Exclusions -> None], {name, {"LeastSquaresCrossValidation", "Oversmooth", "Scott", "SheatherJones", "StandardDeviation", "StandardGaussian"}}]

Out[2]= [image]
```

---

Methods used to estimate the bandwidth diagonal need not be the same:

```wl
In[1]:= BlockRandom[SeedRandom[6];data = RandomVariate[NormalDistribution[], {10, 3}]];
```

Use adaptive, oversmoothed, and constant bandwidths in the respective dimensions:

```wl
In[2]:= \[ScriptCapitalD] = SmoothKernelDistribution[data, {{"Adaptive", .05, 1}, "Silverman", 2}];
```

Plot the univariate marginal PDFs:

```wl
In[3]:= Table[Plot[Evaluate[PDF[MarginalDistribution[\[ScriptCapitalD], i], x]], {x, -4, 4}, PlotRange -> All, PlotLabel -> i], {i, {1, 2, 3}}]

Out[3]= {[image], [image], [image]}
```

---

Give a scalar value to use the same bandwidth in all dimensions:

```wl
In[1]:= data = RandomVariate[BinormalDistribution[.75], 25];

In[2]:= bands = {.25, .5, 1.0};

In[3]:= Table[Plot3D[Evaluate[PDF[SmoothKernelDistribution[data, bw], {x, y}]], {x, -3, 3}, {y, -3, 3}, PlotRange -> All, PlotLabel -> Row[{"bandwidth = ", bw * IdentityMatrix[2]}], Exclusions -> None], {bw, bands}]

Out[3]= [image]
```

---

To use nonzero off-diagonal elements, give a fully specified bandwidth matrix:

```wl
In[1]:= bw = {{1 / 2, 1 / 4}, {1 / 4, 1 / 2}};

In[2]:= \[ScriptCapitalD] = SmoothKernelDistribution[RandomVariate[NormalDistribution[], {100, 2}], bw];

In[3]:= ContourPlot[Evaluate[PDF[\[ScriptCapitalD], {x, y}]], {x, -3, 3}, {y, -3, 3}, PlotRange -> All]

Out[3]= [image]
```

#### Kernel Functions (6)

Specify any one of several kernel functions:

```wl
In[1]:= data = RandomVariate[NormalDistribution[], 100];

In[2]:= Table[Plot[Evaluate[PDF[SmoothKernelDistribution[data, Automatic, i], x]], {x, -4, 4}, Filling -> Axis, Ticks -> None, PlotLabel -> i, Exclusions -> None], {i, {"Biweight", "Cosine", "Epanechnikov", "Gaussian", "Rectangular", "SemiCircle", "Triangular", "Triweight"}}]

Out[2]= [image]
```

---

Define the kernel function as a pure function:

```wl
In[1]:= data = RandomVariate[NormalDistribution[], 10];

In[2]:=
\[ScriptCapitalD]1 = SmoothKernelDistribution[data, Automatic];
\[ScriptCapitalD]2 = SmoothKernelDistribution[data, Automatic, (1/π (1 + #1^2))&];

In[3]:= Table[Plot[Evaluate[PDF[\[ScriptCapitalD], x]], {x, -4, 4}, Filling -> Axis, Exclusions -> None], {\[ScriptCapitalD], {\[ScriptCapitalD]1, \[ScriptCapitalD]2}}]

Out[3]= [image]
```

---

By default, the Gaussian kernel is used:

```wl
In[1]:= data = RandomVariate[NormalDistribution[], 10];

In[2]:=
\[ScriptCapitalD]1 = SmoothKernelDistribution[data, Automatic];
\[ScriptCapitalD]2 = SmoothKernelDistribution[data, Automatic, "Gaussian"];

In[3]:= Table[Plot[Evaluate[PDF[\[ScriptCapitalD], x]], {x, -4, 4}, Filling -> Axis, Exclusions -> None], {\[ScriptCapitalD], {\[ScriptCapitalD]1, \[ScriptCapitalD]2}}]

Out[3]= [image]
```

This is equivalent to using the PDF of a ``NormalDistribution[0, 1]`` :

```wl
In[4]:= \[ScriptCapitalD]3 = SmoothKernelDistribution[data, Automatic, PDF[NormalDistribution[], #]&];

In[5]:= Plot[PDF[\[ScriptCapitalD]3, x], {x, -4, 4}, Filling -> Axis, Exclusions -> None]

Out[5]= [image]
```

---

Shapes of some univariate kernel functions:

```wl
In[1]:= kernels = {"Biweight", "Cosine", "Epanechnikov", "Gaussian", "Rectangular", "SemiCircle", "Triangular", "Custom"};

In[2]:= dists = Table[SmoothKernelDistribution[{0}, 1, i], {i, (kernels /. {"Custom" -> (PDF[CauchyDistribution[0, 1], #]&)})}];

In[3]:= Table[Plot[Evaluate[PDF[dists[[i]], x]], {x, -3, 3}, PlotRange -> All, PlotLabel -> kernels[[i]], Filling -> Axis, Ticks -> None], {i, Length[kernels]}]

Out[3]= [image]
```

---

Specify any one of several kernel functions for multivariate data:

```wl
In[1]:= data = RandomVariate[BinormalDistribution[.75], 10];

In[2]:= Table[Plot3D[Evaluate[PDF[SmoothKernelDistribution[data, Automatic, i], {x, y}]], {x, -4, 4}, {y, -4, 4}, Ticks -> None, PlotLabel -> i, PlotRange -> All, MeshFunctions -> (#3&)], {i, {"Biweight", "Cosine", "Epanechnikov", "Gaussian", "Rectangular", "SemiCircle", "Triangular", "Triweight"}}]

Out[2]= [image]
```

---

Choose between product- and radial-type kernel functions for multivariate data:

```wl
In[1]:= data = {{0, 0}};

In[2]:= kerns = {{"Product", "Biweight"}, {"Radial", "Biweight"}};

In[3]:= Table[Plot3D[Evaluate[PDF[ SmoothKernelDistribution[data, 1, i], {x, y}]], {x, -1.5, 1.5}, {y, -1.5, 1.5}, Ticks -> None, PlotLabel -> i, PlotRange -> All, MeshFunctions -> (#3 &), Boxed -> False], {i, kerns}]

Out[3]= [image]
```

#### Estimation with Fixed Domain (4)

Use bounding to stay within the domain:

```wl
In[1]:= data = RandomVariate[BetaDistribution[2, 3], 10 ^ 4];
```

Define the smooth kernel distribution with a Gaussian kernel:

```wl
In[2]:= \[ScriptCapitalD]1 = SmoothKernelDistribution[data, Automatic, "Gaussian"];
```

The support for PDF extends beyond the data support:

```wl
In[3]:= Plot[PDF[\[ScriptCapitalD]1, x]//Evaluate, {x, -0.1, 1.1}]

Out[3]= [image]
```

Impose bounds:

```wl
In[4]:= \[ScriptCapitalD]2 = SmoothKernelDistribution[data, Automatic, {"Bounded", {0, 1}, "Gaussian"}];

In[5]:= Plot[PDF[\[ScriptCapitalD]2, x]//Evaluate, {x, -.1, 1.1}]

Out[5]= [image]
```

Compare to the original ``BetaDistribution`` :

```wl
In[6]:= Plot[{PDF[\[ScriptCapitalD]2, x], PDF[BetaDistribution[2, 3], x]}//Evaluate, {x, -.1, 1.1}, PlotLegends -> {"Bounded smooth kernel density estimate", "Beta distribution density"}]

Out[6]= [image]
```

---

Compare smooth kernel distributions with a bounded Epanechnikov kernel:

```wl
In[1]:= sample = RandomReal[{-8, 8}, 512];

In[2]:= skd[c_] := SmoothKernelDistribution[sample, .25, {"Bounded", {-c, c}, "Epanechnikov"}]

In[3]:= Table[Plot[Evaluate[PDF[skd[c], x]], {x, -10, 10}, Filling -> Axis, PlotRange -> {0, .2}, PlotLabel -> {-c, c}], {c, {∞, 8}}]

Out[3]= [image]
```

The bounded smooth kernel density estimate is more accurate at the boundaries:

```wl
In[4]:= QuantilePlot[{skd[Infinity], skd[8]}, UniformDistribution[{-8, 8}], PlotRange -> {{-8.5, -5}, {-8.5, -5}}, PlotLegends -> {"Unbounded", "Bounded to (-8,8)"}]

Out[4]= [image]
```

---

Use a bounded cosine kernel for two-dimensional data:

```wl
In[1]:= cpd = CopulaDistribution[{"Binormal", .75}, {TriangularDistribution[{0, 1}], WignerSemicircleDistribution[1]}];

In[2]:= data = RandomVariate[cpd, 10 ^ 4];

In[3]:= skd = SmoothKernelDistribution[data, .2, {"Bounded", {{0, 1}, {-1, 1}}, "Cosine"}]

Out[3]=
DataDistribution["SmoothKernel", {CompressedData["«2384»"], {{0, 0.06182268990940143, 0.14110590516489196, 0.22038912042038206, 
    0.2996723356758726, 0.3789555509313631, 0.4582387661868532, 0.5375219814423438, 
    0.6168051966978343, 0.69608841 ... , -0.31773852809103165, -0.19081516498053164, -0.06389180187003163, 
    0.06303156124046838, 0.18995492435096795, 0.3168782874614684, 0.44380165057196796, 
    0.5707250136824684, 0.697648376792968, 0.8245717399034684, 1}}, {0.2, 0.2}}, 2, 10000]
```

Compare the estimated density to the population distribution density:

```wl
In[4]:= Table[Plot3D[pdf, {x, -0.5, 1.5}, {y, -1.5, 1.5}, PlotRange -> All, AxesLabel -> Automatic, ImageSize -> 220, PlotTheme -> "CoolColors", Exclusions -> {{(1 - y ^ 2), 0 < x < 1}, {x(1 - x) == 0, -1 < y < 1}}], {pdf, {PDF[skd, {x, y}], PDF[cpd, {x, y}]}}]

Out[4]= {[image], [image]}
```

---

Bounded Gaussian kernel:

```wl
In[1]:= data = RandomReal[{-3, 5}, 10 ^ 3];

In[2]:= \[ScriptCapitalD] = SmoothKernelDistribution[data, .5, {"Bounded", {-3, 5}, "Gaussian"}];
```

Truncating the ordinary smooth kernel distribution yields a different result:

```wl
In[3]:= \[ScriptCapitalD]2 = TruncatedDistribution[{-3, 5}, SmoothKernelDistribution[data, .5, "Gaussian"]];

In[4]:= Plot[{PDF[\[ScriptCapitalD], x], PDF[\[ScriptCapitalD]2, x]}//Evaluate, {x, -4, 6}, PlotLegends -> {"Bounded", "Truncated"}]

Out[4]= [image]
```

### Options (25)

#### InterpolationPoints (6)

By default, nonuniform interpolation is used to create a smooth estimate:

```wl
In[1]:= data = RandomVariate[\[ScriptD] = CauchyDistribution[0, 1], 10 ^ 3];

In[2]:= \[ScriptCapitalD] = SmoothKernelDistribution[data, InterpolationPoints -> Automatic];

In[3]:= Plot[{PDF[\[ScriptCapitalD], x], PDF[\[ScriptD], x]}, {x, -10, 10}, PlotRange -> All, PlotLegends -> {"\[ScriptCapitalD]", "\[ScriptD]"}]

Out[3]= [image]
```

---

Specify the initial number of sample points to use:

```wl
In[1]:= data = RandomVariate[NormalDistribution[], 10];
```

Use 4 interpolation points:

```wl
In[2]:= \[ScriptCapitalD] = SmoothKernelDistribution[data, InterpolationPoints -> 4];

In[3]:= Plot[PDF[\[ScriptCapitalD], x], {x, -6, 6}, Filling -> Axis, PlotRange -> All]

Out[3]= [image]
```

---

A larger number of points yields a smoother estimate:

```wl
In[1]:= data = RandomVariate[NormalDistribution[], 10];

In[2]:= estimates = Table[SmoothKernelDistribution[data, InterpolationPoints -> i, MaxRecursion -> 3], {i, {2, 4, 6, 8}}];

In[3]:= Table[Plot[PDF[i, x], {x, -4, 4}, Filling -> Axis], {i, estimates}]

Out[3]= [image]
```

---

Specify the number of interpolating points to use for bivariate data:

```wl
In[1]:= data = RandomVariate[BinormalDistribution[.8], 100];
```

Use 5 and 50 interpolation points in each dimension:

```wl
In[2]:= estimates = Table[SmoothKernelDistribution[data, InterpolationPoints -> i], {i, {5, 50}}];

In[3]:= Table[ContourPlot[PDF[i, {x, y}]//Evaluate, {x, -3, 3}, {y, -3, 3}, PlotRange -> All], {i, estimates}]

Out[3]= {[image], [image]}
```

---

Use different numbers of interpolation points in each dimension:

```wl
In[1]:= data = RandomVariate[BinormalDistribution[.8], 100];
```

Specify 3 and 30 points or 30 and 3:

```wl
In[2]:= estimates = Table[SmoothKernelDistribution[data, InterpolationPoints -> i, MaxRecursion -> 3], {i, {{3, 30}, {30, 3}}}];

In[3]:= Table[ContourPlot[PDF[i, {x, y}]//Evaluate, {x, -4, 4}, {y, -4, 4}, PlotRange -> All], {i, estimates}]

Out[3]= {[image], [image]}
```

---

A smooth result does not imply a high-quality estimate:

```wl
In[1]:= \[ScriptCapitalD]h = HistogramDistribution[RandomVariate[NormalDistribution[], 100], 5];
```

Using 1000 interpolation points creates a very smooth estimate in this case:

```wl
In[2]:= \[ScriptCapitalD]s = SmoothKernelDistribution[RandomVariate[\[ScriptCapitalD]h, 100], InterpolationPoints -> 1000];

In[3]:= Plot[{PDF[\[ScriptCapitalD]s, x], PDF[\[ScriptCapitalD]h, x]}, {x, -4, 4}, PlotRange -> All, Exclusions -> None, PlotLegends -> {"\[ScriptCapitalD]s", "\[ScriptCapitalD]h"}]

Out[3]= [image]
```

#### MaxExtraBandwidths (6)

By default, the estimate extends at most 12 bandwidths beyond the data:

```wl
In[1]:= data = RandomVariate[\[ScriptD] = ParetoDistribution[1, 2], 10 ^ 3];

In[2]:=
\[ScriptCapitalD]1 = SmoothKernelDistribution[data, MaxExtraBandwidths -> 12];
\[ScriptCapitalD]2 = SmoothKernelDistribution[data, MaxExtraBandwidths -> Automatic];

In[3]:= Table[Plot[PDF[\[ScriptCapitalD], x], {x, 0, 10}, PlotRange -> All], {\[ScriptCapitalD], {\[ScriptCapitalD]1, \[ScriptCapitalD]2}}]

Out[3]= [image]
```

---

Set the maximum number of bandwidths to use:

```wl
In[1]:= data = RandomVariate[\[ScriptD] = ParetoDistribution[1, 2], 10 ^ 3];

In[2]:=
\[ScriptCapitalD]1 = SmoothKernelDistribution[data, MaxExtraBandwidths -> 0];
\[ScriptCapitalD]2 = SmoothKernelDistribution[data, MaxExtraBandwidths -> 12];
```

Use 0 and 12 bandwidths, respectively:

```wl
In[3]:= Table[Plot[PDF[\[ScriptCapitalD], x], {x, 0, 10}, PlotRange -> All, Exclusions -> None], {\[ScriptCapitalD], {\[ScriptCapitalD]1, \[ScriptCapitalD]2}}]

Out[3]= [image]
```

---

Set a different number for each endpoint:

```wl
In[1]:= data = RandomVariate[NormalDistribution[], 10];

In[2]:= mebw = {{0, 0}, {0, 12}, {12, 0}, {12, 12}};

In[3]:=
\[ScriptCapitalD] = Table[SmoothKernelDistribution[data, MaxExtraBandwidths -> i], 
	{i, mebw}];

In[4]:= Table[Plot[PDF[\[ScriptCapitalD][[i]], x], {x, -6, 6}, PlotRange -> All, Filling -> Axis, PlotLabel -> mebw[[i]], Ticks -> None], {i, 4}]

Out[4]= [image]
```

---

Specify the number of extra bandwidths to use for multivariate data:

```wl
In[1]:= data = RandomVariate[BinormalDistribution[.5], 10];

In[2]:=
\[ScriptCapitalD]1 = SmoothKernelDistribution[data, MaxExtraBandwidths -> 0];
\[ScriptCapitalD]2 = SmoothKernelDistribution[data, MaxExtraBandwidths -> 12];
```

Use 0 and 12 bandwidths, respectively:

```wl
In[3]:= Table[ContourPlot[PDF[\[ScriptCapitalD], {x, y}], {x, -4, 4}, {y, -4, 4}, PlotRange -> All, Exclusions -> None], {\[ScriptCapitalD], {\[ScriptCapitalD]1, \[ScriptCapitalD]2}}]

Out[3]= [image]
```

---

Specify the number of extra bandwidths to use in each dimension:

```wl
In[1]:= data = RandomVariate[BinormalDistribution[.5], 10];

In[2]:=
\[ScriptCapitalD]1 = SmoothKernelDistribution[data, MaxExtraBandwidths -> {0, 12}];
\[ScriptCapitalD]2 = SmoothKernelDistribution[data, MaxExtraBandwidths -> {12, 0}];
```

Use 0 and 12 bandwidths or 12 and 0 bandwidths, respectively:

```wl
In[3]:= Table[ContourPlot[PDF[\[ScriptCapitalD], {x, y}], {x, -4, 4}, {y, -4, 4}, PlotRange -> All, Exclusions -> None], {\[ScriptCapitalD], {\[ScriptCapitalD]1, \[ScriptCapitalD]2}}]

Out[3]= [image]
```

---

Set a different number for each endpoint in each dimension:

```wl
In[1]:= data = RandomVariate[BinormalDistribution[.5], 10];

In[2]:=
\[ScriptCapitalD]1 = SmoothKernelDistribution[data, MaxExtraBandwidths -> {{0, 12}, {12, 0}}];
\[ScriptCapitalD]2 = SmoothKernelDistribution[data, MaxExtraBandwidths -> {{12, 0}, {0, 12}}];

In[3]:= Table[ContourPlot[PDF[\[ScriptCapitalD], {x, y}], {x, -4, 4}, {y, -4, 4}, PlotRange -> All, Exclusions -> None], {\[ScriptCapitalD], {\[ScriptCapitalD]1, \[ScriptCapitalD]2}}]

Out[3]= {[image], [image]}
```

#### MaxMixtureKernels (6)

By default, the number of kernels is generally optimal:

```wl
In[1]:= data = RandomVariate[\[ScriptD] = ParetoDistribution[1, 2], 10 ^ 4];

In[2]:= \[ScriptCapitalD] = SmoothKernelDistribution[data, MaxMixtureKernels -> Automatic];

In[3]:= Plot[{PDF[\[ScriptCapitalD], x], PDF[\[ScriptD], x]}, {x, 0, 6}, PlotRange -> {0, 1.3}, PlotLegends -> {"\[ScriptCapitalD]", "\[ScriptD]"}]

Out[3]= [image]
```

---

Specify the maximum number of kernels to use in the estimate:

```wl
In[1]:= data = RandomVariate[NormalDistribution[], 10];
```

Place at most 5 kernels:

```wl
In[2]:= \[ScriptCapitalD] = SmoothKernelDistribution[data, MaxMixtureKernels -> 5];

In[3]:= Plot[PDF[\[ScriptCapitalD], x], {x, -4, 4}, Filling -> Axis, PlotRange -> All]

Out[3]= [image]
```

---

A larger number of kernels gives a better estimate of the underlying distribution:

```wl
In[1]:= data = RandomVariate[NormalDistribution[], 10 ^ 4];

In[2]:= estimates = Table[SmoothKernelDistribution[data, MaxMixtureKernels -> i], {i, {10, 15, 25, 100}}];

In[3]:= Table[Plot[{PDF[i, x], PDF[NormalDistribution[], x]}, {x, -4, 4}, Filling -> Axis, PlotRange -> All], {i, estimates}]

Out[3]= [image]
```

---

Place a kernel at each data point:

```wl
In[1]:= data = {-2, 0, 2};

In[2]:= \[ScriptCapitalD] = SmoothKernelDistribution[data, .25, MaxMixtureKernels -> All];

In[3]:= Plot[PDF[\[ScriptCapitalD], x], {x, -4, 4}, Filling -> Axis]

Out[3]= [image]
```

Vary the bandwidth used for the same number of kernels:

```wl
In[4]:= Table[Plot[PDF[SmoothKernelDistribution[data, bw, MaxMixtureKernels -> All], x]//Evaluate, {x, -4, 4}, Filling -> Axis, PlotRange -> {0, .6}, PlotLabel -> Row[{"bandwidth = ", bw}]], {bw, {0.25, 0.5, 0.75, 1.0}}]

Out[4]= [image]
```

---

Specify the maximum number of kernels to use in each dimension for bivariate data:

```wl
In[1]:= data = RandomVariate[BinormalDistribution[.8], 100];
```

Place at most 10 and 100 kernels, respectively:

```wl
In[2]:= estimates = Table[SmoothKernelDistribution[data, .2, MaxMixtureKernels -> i], {i, {10, 100}}];

In[3]:= Table[DensityPlot[PDF[i, {x, y}]//Evaluate, {x, -4, 4}, {y, -4, 4}, PlotPoints -> 100, PlotRange -> All, ColorFunction -> "TemperatureMap"], {i, estimates}]

Out[3]= [image]
```

---

Set the maximum number of kernels in each dimension:

```wl
In[1]:= data = RandomVariate[BinormalDistribution[.8], 100];
```

Specify a maximum of 5 and 50 kernels or 50 and 5:

```wl
In[2]:= estimates = Table[SmoothKernelDistribution[data, MaxMixtureKernels -> i], {i, {{5, 50}, {50, 5}}}];

In[3]:= Table[Plot3D[Evaluate[PDF[i, {x, y}]], {x, -4, 4}, {y, -4, 4}, PlotPoints -> 50, PlotRange -> All], {i, estimates}]

Out[3]= [image]
```

#### MaxRecursion (4)

A smooth estimate will usually be returned by default:

```wl
In[1]:= data = RandomVariate[\[ScriptD] = NormalDistribution[], 10 ^ 3];

In[2]:= \[ScriptCapitalD] = SmoothKernelDistribution[data, MaxRecursion -> Automatic];

In[3]:= Plot[{PDF[\[ScriptCapitalD], x], PDF[\[ScriptD], x]}, {x, -5, 5}, PlotLegends -> {"\[ScriptCapitalD]", "\[ScriptD]"}]

Out[3]= [image]
```

---

Specify the maximum number of recursive subdivisions to use:

```wl
In[1]:= data = RandomVariate[NormalDistribution[], 10];

In[2]:= \[ScriptCapitalD] = Table[SmoothKernelDistribution[data, InterpolationPoints -> 3, MaxRecursion -> i], {i, {0, 2, 4, 6}}];

In[3]:= Table[Plot[PDF[i, x], {x, -5, 5}, Filling -> Axis, PlotRange -> All], {i, \[ScriptCapitalD]}]

Out[3]= [image]
```

---

Give the maximum number of recursive subdivisions for bivariate data:

```wl
In[1]:= data = RandomVariate[BinormalDistribution[.8], 100];
```

Use at most 2 and 6 subdivisions, respectively:

```wl
In[2]:= estimates = Table[SmoothKernelDistribution[data, MaxRecursion -> i, InterpolationPoints -> 3], {i, {2, 6}}];

In[3]:= Table[Plot3D[PDF[i, {x, y}]//Evaluate, {x, -4, 4}, {y, -4, 4}, PlotRange -> All, ColorFunction -> "TemperatureMap"], {i, estimates}]

Out[3]= {[image], [image]}
```

---

Set the maximum number of recursive subdivisions in each dimension:

```wl
In[1]:= data = RandomVariate[BinormalDistribution[.8], 100];
```

Specify a maximum of 0 and 3 subdivisions or 3 and 0:

```wl
In[2]:= estimates = Table[SmoothKernelDistribution[data, MaxRecursion -> i, InterpolationPoints -> 6], {i, {{0, 3}, {3, 0}}}];

In[3]:= Table[Plot3D[PDF[i, {x, y}]//Evaluate, {x, -4, 4}, {y, -4, 4}, PlotRange -> All], {i, estimates}]

Out[3]= {[image], [image]}
```

#### PerformanceGoal (3)

By default, estimates are optimized for a balance between speed and quality:

```wl
In[1]:= data = RandomVariate[\[ScriptD] = NormalDistribution[], 10 ^ 5];

In[2]:= (\[ScriptCapitalD] = SmoothKernelDistribution[data, PerformanceGoal -> Automatic])//Timing

Out[2]=
{0.052856, DataDistribution["SmoothKernel", {CompressedData["«5564»"], 
  CompressedData["«5642»"], 0.08955159847392712}, 1, 100000]}

In[3]:= Plot[{PDF[\[ScriptCapitalD], x], PDF[\[ScriptD], x]}, {x, -5, 5}, PlotLegends -> {"\[ScriptCapitalD]", "\[ScriptD]"}]

Out[3]= [image]
```

---

Set ``PerformanceGoal`` for speed or quality or use ``Automatic`` to balance the two:

```wl
In[1]:= data = RandomVariate[\[ScriptD] = MixtureDistribution[{1, 1}, {MultinormalDistribution[{-1, -1}, IdentityMatrix[2]], MultinormalDistribution[{1, 1}, .5IdentityMatrix[2]]}], 10 ^ 2];
```

More time is spent with ``PerformanceGoal`` set to ``"Quality"`` :

```wl
In[2]:= \[ScriptCapitalD]1 = SmoothKernelDistribution[data, PerformanceGoal -> "Speed"];//Timing

Out[2]= {0.015625, Null}

In[3]:= \[ScriptCapitalD]2 = SmoothKernelDistribution[data, PerformanceGoal -> Automatic];//Timing

Out[3]= {0.015625, Null}

In[4]:= \[ScriptCapitalD]3 = SmoothKernelDistribution[data, PerformanceGoal -> "Quality"];//Timing

Out[4]= {0.03125, Null}

In[5]:= Table[ContourPlot[Evaluate[PDF[\[ScriptCapitalD], {x, y}]], {x, -4, 4}, {y, -4, 4}, PlotPoints -> 35, PlotRange -> All], {\[ScriptCapitalD], {\[ScriptCapitalD]1, \[ScriptCapitalD]2, \[ScriptCapitalD]3}}]

Out[5]= [image]
```

---

Use with ``ControlActive`` to vary ``PerformanceGoal`` dynamically:

```wl
In[1]:= data = RandomVariate[MixtureDistribution[{1 / 2, 1 / 10, 1 / 10, 1 / 10, 1 / 10, 1 / 10}, Join[{NormalDistribution[]}, Table[NormalDistribution[i / 2 - 1, 1 / 10], {i, 0, 4}]]], 3 * 10 ^ 3];

In[2]:= Manipulate[Plot[PDF[SmoothKernelDistribution[data, PerformanceGoal -> ControlActive["Speed", "Quality"]], x]//Evaluate, {x, a, b}, PlotRange -> {Automatic, {0, .55}}], {a, -3, 0.1}, {b, 3, 0, -.1}, SaveDefinitions -> True]

Out[2]= DynamicModule[«9»]
```

### Applications (14)

Compare an estimated density to a theoretical model:

```wl
In[1]:= \[ScriptD] = MixtureDistribution[{1 / 2, 1 / 10, 1 / 10, 1 / 10, 1 / 10, 1 / 10}, Join[{NormalDistribution[]}, Table[NormalDistribution[i / 2 - 1, 1 / 10], {i, 0, 4}]]];

In[2]:= data = RandomVariate[\[ScriptD], 10 ^ 5];
```

Use adaptive bandwidths for highly oscillatory densities:

```wl
In[3]:= \[ScriptCapitalD] = SmoothKernelDistribution[data, {"Adaptive", Automatic, .25}, PerformanceGoal -> "Quality"];

In[4]:= Plot[{PDF[\[ScriptCapitalD], x], PDF[\[ScriptD], x]}, {x, -4, 4}, PlotLegends -> {"\[ScriptCapitalD]", "\[ScriptD]"}]

Out[4]= [image]
```

The moments of the model and the estimate are similar:

```wl
In[5]:= {Mean[\[ScriptD]], Variance[\[ScriptD]], Skewness[\[ScriptD]], Kurtosis[\[ScriptD]]}//N

Out[5]= {0., 0.755, 0., 3.03083}

In[6]:= {Mean[\[ScriptCapitalD]], Variance[\[ScriptCapitalD]], Skewness[\[ScriptCapitalD]], Kurtosis[\[ScriptCapitalD]]}

Out[6]= {-0.00259476, 0.754367, -0.0126747, 3.03306}
```

---

Use ``TruncatedDistribution`` to restrict the domain after smoothing:

```wl
In[1]:= data = RandomVariate[ExponentialDistribution[2], 10 ^ 4];

In[2]:= dist = SmoothKernelDistribution[data];

In[3]:= \[ScriptCapitalD] = TruncatedDistribution[{0, Infinity}, dist];
```

The estimate is restricted to positive values:

```wl
In[4]:= Plot[PDF[\[ScriptCapitalD], x]//Evaluate, {x, -1, 5}, PlotRange -> All, Filling -> Axis]

Out[4]= [image]
```

Verify that the distribution is bound by the truncation region:

```wl
In[5]:= NProbability[x < 0, x\[Distributed]\[ScriptCapitalD]]

Out[5]= 0.
```

---

Use with ``Cases`` to restrict the data domain before smoothing:

```wl
In[1]:= data = Cases[RandomVariate[NormalDistribution[], 10 ^ 5], x_ /; x > 0];

In[2]:= \[ScriptCapitalD] = SmoothKernelDistribution[data];
```

The estimate goes beyond the data on the left, but the data is restricted to positive values:

```wl
In[3]:= Plot[PDF[\[ScriptCapitalD], x], {x, -1, 5}, PlotRange -> All, Filling -> Axis]

Out[3]= [image]
```

The probability that the data falls below zero is not zero:

```wl
In[4]:= NProbability[x < 0, x\[Distributed]\[ScriptCapitalD]]

Out[4]= 0.0194042
```

---

Use ``MaxExtraBandwidths`` to restrict the domain without dropping data:

```wl
In[1]:= data = RandomVariate[ExponentialDistribution[2], 10 ^ 6];

In[2]:= \[ScriptCapitalD] = SmoothKernelDistribution[data, MaxExtraBandwidths -> {0, 12}];
```

The estimate stops at the minimum data value, which is restricted to positive values:

```wl
In[3]:= Plot[PDF[\[ScriptCapitalD], x], {x, -3, 3}, PlotRange -> All, Filling -> Axis]

Out[3]= [image]

In[4]:= NProbability[x < 0, x\[Distributed]\[ScriptCapitalD]]

Out[4]= 0.
```

---

Estimate the distribution of the lengths of human chromosomes:

```wl
In[1]:= \[ScriptCapitalD] = SmoothKernelDistribution[ N[Log[Table[GenomeData[i, "SequenceLength"], {i, 41}] ]]];

In[2]:= Plot[PDF[\[ScriptCapitalD], x], {x, 0, 30}, Filling -> Axis, Frame -> True]

Out[2]= [image]
```

The expected chromosome length, given that the length is greater than the mean:

```wl
In[3]:= NExpectation[x\[Conditioned]x > Mean[\[ScriptCapitalD]], x\[Distributed]\[ScriptCapitalD]]

Out[3]= 18.6638
```

---

Smooth the discrete distribution of the differences of successive primes:

```wl
In[1]:= data = Table[Prime[i] - Prime[i - 1], {i, 2, 10 ^ 4}];

In[2]:= \[ScriptCapitalD] = SmoothKernelDistribution[data];

In[3]:= Plot[PDF[\[ScriptCapitalD], x], {x, -10, 50}, Filling -> Axis, Frame -> True, PlotRange -> All, Axes -> False]

Out[3]= [image]

In[4]:= NExpectation[13 + x, x\[Distributed]\[ScriptCapitalD]]

Out[4]= 23.4737
```

---

Investigate the distribution of differenced daily returns on the S&P 500 during the 1990s:

```wl
In[1]:= sp500 = ExampleData[{"Statistics", "SP500"}];

In[2]:= Subscript[\[ScriptCapitalD], SP500] = SmoothKernelDistribution[sp500, {"Adaptive", Automatic, .25}];
```

Compare the smoothed distribution to a fitted model:

```wl
In[3]:= dfit = EstimatedDistribution[sp500, LaplaceDistribution[μ, β]];
```

In[4]:= Plot[{PDF[dfit,x],PDF[Subscript[\[ScriptCapitalD], SP500],x]},{x,-5,5},PlotRange->All,AxesOrigin->{-5,0},PlotLegends->{"dfit","Subscript[\[ScriptCapitalD], SP500]"}]

```wl
Out[4]= [image]
```

---

Compare the distribution of salaries from two university departments:

```wl
In[1]:= ExampleData[{"Statistics", "UniversitySalaries"}, "ColumnDescriptions"]

Out[1]= {"Department", "Estimated percentage of full time employment.", "Projected 2010 salary.", "Campus location"}

In[2]:= fullData = ExampleData[{"Statistics", "UniversitySalaries"}][[All, {1, 3}]];
```

Select salaries for two departments and attach currency units:

```wl
In[3]:=
statSalaries = Cases[fullData, {"Statistics", salary_} :> salary];
chemSalaries = Cases[fullData, {"Chemistry", salary_} :> salary];

In[4]:=
Subscript[\[ScriptCapitalD], Stat] = SmoothKernelDistribution[QuantityArray[statSalaries, "USDollars"]];
Subscript[\[ScriptCapitalD], Chem] = SmoothKernelDistribution[QuantityArray[chemSalaries, "USDollars"]];

In[5]:= Table[Plot[PDF[\[ScriptCapitalD], Quantity[x, "USDollars"]], {x, 0, 250000}, Ticks -> {{{70000, "70K"}, {125000, "125K"}, {200000, "200K"}}, None}, PlotRange -> All, Filling -> Axis, AxesLabel -> {"$"}], {\[ScriptCapitalD], {Subscript[\[ScriptCapitalD], Stat], Subscript[\[ScriptCapitalD], Chem]}}]

Out[5]= [image]
```

---

Estimate the joint distribution of Old Faithful eruption durations and waiting times:

```wl
In[1]:= oldF = QuantityArray[ExampleData[{"Statistics", "OldFaithful"}], "Minutes"];

In[2]:= Subscript[\[ScriptCapitalD], OldF] = SmoothKernelDistribution[oldF, "SheatherJones"];

In[3]:= Plot3D[PDF[Subscript[\[ScriptCapitalD], OldF], Quantity[{x, y}, "Minutes"]]//Evaluate, {x, 1, 6}, {y, 35, 105}, PlotRange -> All, Exclusions -> None, AspectRatio -> 1, PlotPoints -> 50, AxesLabel -> {"Duration", "Waiting", None}]

Out[3]= [image]
```

Probability an eruption lasts more than two minutes and the waiting time is less than one hour:

```wl
In[4]:= NProbability[x > Quantity[2, "Minutes"] && y < Quantity[60, "Minutes"], {x, y}\[Distributed]Subscript[\[ScriptCapitalD], OldF], PrecisionGoal -> 3]

Out[4]= 0.125255
```

---

Smooth a histogram:

```wl
In[1]:= data = RandomVariate[NormalDistribution[], 10 ^ 4];

In[2]:= hist = HistogramDistribution[data];
```

Generate random numbers from the histogram for smoothing:

```wl
In[3]:= samples = RandomVariate[hist, 10 ^ 5];

In[4]:= {DiscretePlot[PDF[hist, x], {x, -4, 4, .05}], Plot[PDF[SmoothKernelDistribution[samples, "Oversmooth"], x]//Evaluate, {x, -4, 4}, Filling -> Axis]}

Out[4]= [image]
```

---

Smooth an estimate returned from ``SurvivalDistribution`` :

```wl
In[1]:= data = RandomInteger[{10, 40}, 50];

In[2]:= censoring = RandomInteger[{0, 1}, 50];

In[3]:= \[ScriptCapitalD] = SurvivalDistribution[EventData[data, censoring]];

In[4]:= samples = RandomVariate[\[ScriptCapitalD], 10 ^ 4];

In[5]:= Show[DiscretePlot[SurvivalFunction[\[ScriptCapitalD], x], {x, data}], Plot[SurvivalFunction[\[ScriptD] = SmoothKernelDistribution[samples], x]//Evaluate, {x, 0, 50}, PlotStyle -> RGBColor[0.87058837853668, 0.6048248459110492, 0.4922838688297661], PlotRange -> All], PlotRange -> {{5, 45}, All}, Axes -> False, Frame -> True]

Out[5]= [image]
```

Compute the probability of survival beyond 25, given that the survival time is greater than 10:

```wl
In[6]:= NProbability[x > 25\[Conditioned]x > 10, x\[Distributed]\[ScriptD]]

Out[6]= 0.708577
```

---

Create a confidence band for the PDF of snowfall accumulations in Buffalo, New York:

```wl
In[1]:= ExampleData[{"Statistics", "BuffaloSnow"}, "Description"]

Out[1]= "Snowfall records for Buffalo, New York from 1910 to 1972."

In[2]:= data = QuantityArray[ExampleData[{"Statistics", "BuffaloSnow"}]//N, "Inches"];

In[3]:= \[ScriptCapitalD] = SmoothKernelDistribution[data];

In[4]:= bSamp = RandomChoice[Normal[data], {250, Length[data]}];
```

Smooth over each bootstrapped sample and obtain the confidence estimates:

```wl
In[5]:= Subscript[\[ScriptCapitalD], B] = SmoothKernelDistribution[#]& /@ bSamp;

In[6]:=
rng = Range[10., 150];
pdf = Table[PDF[i, Quantity[rng, "Inches"]], {i, Subscript[\[ScriptCapitalD], B]}];
High = Table[Quantile[i, .975], {i, Transpose[pdf]}];
Low = Table[Quantile[i, .025], {i, Transpose[pdf]}];
```

Visualize the estimate of the PDF with the 95% confidence band:

```wl
In[7]:= Show[Plot[PDF[\[ScriptCapitalD], Quantity[x, "Inches"]], {x, 0, 150}, PlotRange -> All, AxesLabel -> {"in"}], ListLinePlot[{Transpose[{rng, High}], Transpose[{rng, Low}]}, PlotStyle -> {{Dashed, Gray}}, Filling -> {1 -> {2}}]]

Out[7]= [image]
```

---

Confirm that the Mahalanobis distance has an asymptotic ``ChiSquareDistribution[p]``, given ``p``-dimensional multivariate normal data:

```wl
In[1]:= data = Table[RandomVariate[MultinormalDistribution[ConstantArray[0, i], IdentityMatrix[i]], 10^3], {i, rng = Range[2, 5]}];

In[2]:= MahalanobisDistance[data_] := Block[{m = Mean[data], cov = Inverse[Covariance[data]], temp}, temp = (#1 - m&) /@ data;Diagonal[temp.cov.Transpose[temp]]]

In[3]:= \[ScriptCapitalD]s = Table[SmoothKernelDistribution[MahalanobisDistance[i]], {i, data}];

In[4]:= Table[Plot[{PDF[ChiSquareDistribution[rng[[i]]], x], PDF[\[ScriptCapitalD]s[[i]], x]}, {x, 0, 30}, PlotRange -> All, PlotLabel -> Row[{"Dimension: ", rng[[i]]}]], {i, Length[rng]}]

Out[4]= {[image], [image], [image], [image]}
```

The probability that the Mahalanobis distance will exceed 10, given four-dimensional normal data:

```wl
In[5]:= NProbability[x > 10, x\[Distributed]\[ScriptCapitalD]s[[3]]]

Out[5]= 0.0411518
```

---

Estimate a heavy-tailed density using parametric tail models:

```wl
In[1]:= data = BlockRandom[SeedRandom[11];RandomVariate[\[ScriptD] = CauchyDistribution[0, .1], 10 ^ 3]];
```

The body is estimated well, but the tails are undersmoothed due to lack of data:

```wl
In[2]:= Show[SmoothHistogram[data, PlotRange -> All], Plot[PDF[\[ScriptD], x], {x, -10, 10}, PlotStyle -> {Dashed, RGBColor[0.87058837853668, 0.6048248459110492, 0.4922838688297661]}, PlotRange -> All], PlotRange -> {{-10, 10}, {0, .2}}]

Out[2]= [image]
```

Create a mixture of the kernel density estimate and estimated tail models:

```wl
In[3]:= q10 = Quantile[data, .1];q90 = Quantile[data, .9];

In[4]:= left = EstimatedDistribution[Select[data, # < q10&], TruncatedDistribution[{-∞, q10}, CauchyDistribution[a, b]]];

In[5]:= right = EstimatedDistribution[Select[data, # > q90&], TruncatedDistribution[{q90, ∞}, CauchyDistribution[a, b]]];

In[6]:= body = TruncatedDistribution[{q10, q90}, SmoothKernelDistribution[Select[data, q10 - 1 ≤ # ≤ q90 + 1&]]];

In[7]:= \[ScriptCapitalD] = MixtureDistribution[{.1, .8, .1}, {left, body, right}];
```

The entire estimate is smooth:

```wl
In[8]:= Plot[{PDF[\[ScriptCapitalD], x], PDF[\[ScriptD], x]}, {x, -20, 20}, PlotStyle -> {Automatic, {Dashed, RGBColor[0.87058837853668, 0.6048248459110492, 0.4922838688297661]}}, Exclusions -> None, PlotRange -> {0, .2}, PlotLegends -> {"\[ScriptCapitalD]", "\[ScriptD]"}]

Out[8]= [image]
```

### Properties & Relations (8)

The resulting density estimate integrates to unity:

```wl
In[1]:= data = RandomVariate[NormalDistribution[], 100];

In[2]:= \[ScriptCapitalD] = SmoothKernelDistribution[data];

In[3]:= Integrate[PDF[\[ScriptCapitalD], x], {x, -Infinity, Infinity}]

Out[3]= 1.
```

---

By default, machine estimates are used:

```wl
In[1]:= data = Range[10];

In[2]:= \[ScriptCapitalD] = SmoothKernelDistribution[data, 1];

In[3]:= Precision[\[ScriptCapitalD]]

Out[3]= MachinePrecision
```

Use high-precision data to get high-precision estimates:

```wl
In[4]:= data = N[Range[10], 100];

In[5]:= \[ScriptCapitalD] = SmoothKernelDistribution[data, 1];

In[6]:= Precision[\[ScriptCapitalD]]

Out[6]= 97.5246
```

---

The PDF is piecewise linear:

```wl
In[1]:= \[ScriptCapitalD] = SmoothKernelDistribution[RandomVariate[NormalDistribution[], 100], InterpolationPoints -> 20, MaxRecursion -> 0];

In[2]:= Plot[PDF[\[ScriptCapitalD], x], {x, -4, 4}]

Out[2]= [image]
```

The CDF and ``SurvivalFunction`` are piecewise quadratic:

```wl
In[3]:= {Plot[CDF[\[ScriptCapitalD], x], {x, -4, 4}], Plot[SurvivalFunction[\[ScriptCapitalD], x], {x, -4, 4}]}

Out[3]= {[image], [image]}
```

The ``HazardFunction`` is piecewise rational with linear over quadratic:

```wl
In[4]:= Plot[HazardFunction[\[ScriptCapitalD], x], {x, -4, 4}]

Out[4]= [image]
```

---

``SmoothKernelDistribution`` is a consistent estimator of the underlying distribution:

```wl
In[1]:= data = Table[RandomVariate[NormalDistribution[], 10^i], {i, Range[4]}];

In[2]:= \[ScriptCapitalD]l = Table[SmoothKernelDistribution[i], {i, data}];

In[3]:= Table[Plot[{PDF[\[ScriptCapitalD]l[[i]], x], PDF[NormalDistribution[], x]}, {x, -5, 5}, PlotRange -> All, Exclusions -> None, PlotLabel -> Row[{"n=", Superscript[10, i]}]], {i, Range[4]}]

Out[3]= [image]
```

---

As the bandwidth approaches infinity, the estimate approaches the shape of the kernel:

```wl
In[1]:= data = {-.5, 0, .5};

In[2]:= \[ScriptCapitalK] = {"Gaussian", "Epanechnikov", "Biweight", "Triweight", "Rectangular", "Triangular", "Cosine", "SemiCircle"};

In[3]:= Table[Plot[PDF[SmoothKernelDistribution[data, 200, i], x]//Evaluate, {x, -500, 500}, PlotRange -> All, Ticks -> None, AxesOrigin -> {0, 0}, PlotLabel -> i], {i, \[ScriptCapitalK]}]

Out[3]= {[image], [image], [image], [image], [image], [image], [image], [image]}
```

---

``SmoothKernelDistribution`` is a linear interpolation of ``KernelMixtureDistribution`` :

```wl
In[1]:= data = {-1.5, -1.0, -.2, -.1, 0., 1.5};

In[2]:= pts = Table[{i, PDF[KernelMixtureDistribution[data], i]}, {i, Range[-4, 4, .1]}];

In[3]:= {ListLinePlot[pts], Plot[PDF[SmoothKernelDistribution[data], x]//Evaluate, {x, -4, 4}]}

Out[3]= {[image], [image]}
```

---

``SmoothKernelDistribution`` works on the values only when the input is a ``TimeSeries`` or an ``EventSeries`` :

```wl
In[1]:=
ts = TemporalData[TimeSeries, {{{1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 
    0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 0, 
    0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 0, 
    1, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1}}, {{0, 99, 1}}, 1, {"Continuous", 1}, {"Discrete", 1}, 1, 
  {ResamplingMethod -> {"Interpolation", InterpolationOrder -> 1}}}, False, 10.1];

In[2]:= SmoothKernelDistribution[ts]

Out[2]= DataDistribution["SmoothKernel", {CompressedData["«5546»"], CompressedData["«5594»"], 0.17874967625149996}, 1, 100]
```

The same as:

```wl
In[3]:= SmoothKernelDistribution[ts["Values"]]

Out[3]= DataDistribution["SmoothKernel", {CompressedData["«5546»"], CompressedData["«5594»"], 0.17874967625149996}, 1, 100]

In[4]:= % == %%

Out[4]= True
```

---

``SmoothKernelDistribution`` works with all the values together when the input is a ``TemporalData`` :

```wl
In[1]:=
td = TemporalData[Automatic, {CompressedData["«110034»"], {{0, 1, 0.1}}, 1000, {"Continuous", 1000}, {"Continuous", 1}, 1, 
  {ValueDimensions -> 1, ResamplingMethod -> {"Interpolation", InterpolationOrder -> 1}}}, False, 
 10.1];

In[2]:= SmoothKernelDistribution[td]

Out[2]= DataDistribution["SmoothKernel", {CompressedData["«5546»"], CompressedData["«5644»"], 0.08250504431273302}, 1, 11000]
```

The same as:

```wl
In[3]:= SmoothKernelDistribution[td["ValueList"]//Flatten]

Out[3]= DataDistribution["SmoothKernel", {CompressedData["«5546»"], CompressedData["«5644»"], 0.08250504431273302}, 1, 11000]

In[4]:= %% == %

Out[4]= True
```

### Possible Issues (4)

The kernel function needs to be a PDF:

```wl
In[1]:= data = {-1.5, -1.0, -.2, -.1, 0., 1.5};

In[2]:= \[ScriptCapitalD] = SmoothKernelDistribution[data, 1, Cos[#]&];
```

The resulting density estimate is not a PDF:

```wl
In[3]:= Plot[PDF[\[ScriptCapitalD], x], {x, -4, 4}, Filling -> Axis]

Out[3]= [image]
```

---

Automatic adaptive bandwidths may be too small with large samples:

```wl
In[1]:= data = RandomVariate[NormalDistribution[], 10 ^ 6];

In[2]:= dist = SmoothKernelDistribution[data, {"Adaptive", Automatic, 1}];

In[3]:= Plot[PDF[dist, x]//Evaluate, {x, -4, 4}, PlotRange -> All]

Out[3]= [image]
```

Try increasing the initial bandwidth, ``MaxMixtureKernels``, or decreasing the sensitivity:

```wl
In[4]:= dist2 = SmoothKernelDistribution[data, {"Adaptive", 1.5, 1}];

In[5]:= Plot[PDF[dist2, x]//Evaluate, {x, -4, 4}, PlotRange -> All]

Out[5]= [image]
```

---

``SmoothKernelDistribution`` does not know the domain of the underlying distribution:

```wl
In[1]:= data = RandomVariate[\[ScriptD] = BinomialDistribution[30, .5], 10 ^ 4];
```

The estimated PDF is continuous, although the underlying distribution is discrete:

```wl
In[2]:= {DiscretePlot[PDF[\[ScriptD], x], {x, 5, 25}], Plot[PDF[SmoothKernelDistribution[data], x]//Evaluate, {x, 5, 25}]}

Out[2]= [image]

In[3]:= data2 = RandomVariate[\[ScriptD]2 = TruncatedDistribution[{-1, 1}, NormalDistribution[]], 10 ^ 4];
```

The estimated PDF is not bound on $[-1,1]$ :

```wl
In[4]:= {Plot[PDF[\[ScriptD]2, x], {x, -2, 2}, Exclusions -> None], Plot[PDF[SmoothKernelDistribution[data2], x]//Evaluate, {x, -2, 2}]}

Out[4]= {[image], [image]}
```

With heavily adaptive bandwidths, these issues may be less obvious:

```wl
In[5]:=
data3 = RandomVariate[\[ScriptD], 10 ^ 4];
data4 = RandomVariate[\[ScriptD]2, 10 ^ 4];

In[6]:=
{Plot[PDF[SmoothKernelDistribution[data3, {"Adaptive", Automatic, 1}], x]//Evaluate, {x, 5, 25}, PlotRange -> All], 
	Plot[PDF[SmoothKernelDistribution[data4, {"Adaptive", Automatic, 1}], x]//Evaluate, {x, -2, 2}]}

Out[6]= [image]
```

---

The tails of some distributions are too heavy to interpolate effectively:

```wl
In[1]:= SeedRandom[1];data = RandomVariate[\[ScriptD] = StableDistribution[0, 1 / 9, 1 / 15, 1 / 15, 1], 10 ^ 3];

In[2]:= \[ScriptCapitalD] = SmoothKernelDistribution[data, 10 ^ -5];
```

SmoothKernelDistribution::hvtl: The data contains extreme outliers. Density estimation may fail with automatic settings.

```wl
In[3]:= {Plot[PDF[\[ScriptD], x], {x, .054, .056}, PlotRange -> All], Plot[PDF[\[ScriptCapitalD], x], {x, .054, .056}, PlotRange -> All, Exclusions -> None]}

Out[3]= {[image], [image]}
```

``KernelMixtureDistribution`` uses symbolic methods that do not rely on interpolation:

```wl
In[4]:= \[ScriptCapitalD] = KernelMixtureDistribution[data, 10 ^ -5, MaxMixtureKernels -> All]//Quiet;

In[5]:= {Plot[PDF[\[ScriptD], x], {x, .054, .056}, PlotRange -> All], Plot[PDF[\[ScriptCapitalD], x], {x, .054, .056}, PlotRange -> All, Exclusions -> None]}

Out[5]= [image]
```

### Neat Examples (2)

Compute the distribution of temperature readings near your location:

```wl
In[1]:= data = WeatherData[loc = FindGeoLocation[], "Temperature", {{2001, 1, 1}, {2009, 1, 1}}, "NonMetricValue"];

In[2]:= data = Extract[data, Position[data, x_ /; NumericQ[x]]];

In[3]:= \[ScriptCapitalD] = SmoothKernelDistribution[data]

Out[3]=
DataDistribution["SmoothKernel", {CompressedData["«5576»"], 
  CompressedData["«5566»"], 1.8446486953551935}, 1, 84739]

In[4]:= Show[Plot[PDF[\[ScriptCapitalD], x], {x, -10, 110}, Frame -> True, Filling -> Axis, PlotLabel -> Row[{"Temperature Distribution: Lat ", Latitude[loc], " Lon ", Longitude[loc]}], FrameLabel -> {"°F", "Density"}, Axes -> False, PlotRange -> All, ColorFunction -> Function[{x, y}, ColorData["TemperatureMap"][x]]], Plot[PDF[\[ScriptCapitalD], x], {x, -10, 110}, PlotStyle -> Black]]

Out[4]= [image]
```

---

Estimate the density of volcanic craters in western Uganda:

```wl
In[1]:=
dat = ExampleData[{"Statistics", "UgandaVolcanoes"}];
poly = ExampleData[{"Statistics", "WesternUgandaBorder"}];

In[2]:= \[ScriptCapitalD] = SmoothKernelDistribution[dat, "SheatherJones"];
```

A region function for a bounding polygon using winding numbers:

```wl
In[3]:=
inPolyQ = Compile[{{poly, _Real, 2}, {x, _Real}, {y, _Real}}, Block[{Xi, Yi, Xip1, Yip1, u, v, w}, {Xi, Yi} = Transpose[poly];
	Xip1 = RotateLeft[Xi];
	Yip1 = RotateLeft[Yi];
	u = UnitStep[y - Yi];
	v = RotateLeft[u];
	w = UnitStep[-((Xip1 - Xi) (y - Yi) - (x - Xi) (Yip1 - Yi))];
	Total[(u (1 - v) (1 - w) - (1 - u) v w)] ≠ 0]];;

In[4]:= dens = ContourPlot[PDF[\[ScriptCapitalD], {x, y}]//Evaluate, {x, 0, 3000}, {y, 550, 4500}, PlotRange -> All, PlotPoints -> 50, Contours -> 15, ColorFunction -> "SolarColors", RegionFunction -> (inPolyQ[poly, #1, #2]&)];

In[5]:= Show[dens, ListLinePlot[poly, PlotStyle -> Thick], ListPlot[dat, PlotMarkers -> {Automatic, 5}, PlotStyle -> Black], PlotLabel -> Text[Style["Volcanic Crater Density", {Bold, 18}]]]

Out[5]= [image]
```

## See Also

* [`KernelMixtureDistribution`](https://reference.wolfram.com/language/ref/KernelMixtureDistribution.en.md)
* [`HistogramDistribution`](https://reference.wolfram.com/language/ref/HistogramDistribution.en.md)
* [`EmpiricalDistribution`](https://reference.wolfram.com/language/ref/EmpiricalDistribution.en.md)
* [`SurvivalDistribution`](https://reference.wolfram.com/language/ref/SurvivalDistribution.en.md)
* [`EstimatedDistribution`](https://reference.wolfram.com/language/ref/EstimatedDistribution.en.md)
* [`FindDistribution`](https://reference.wolfram.com/language/ref/FindDistribution.en.md)

## Related Guides

* [Probability & Statistics with Quantities](https://reference.wolfram.com/language/guide/ProbabilityWithQuantities.en.md)
* [Nonparametric Statistical Distributions](https://reference.wolfram.com/language/guide/NonparametricStatisticalDistributions.en.md)
* [Probability & Statistics](https://reference.wolfram.com/language/guide/ProbabilityAndStatistics.en.md)
* [Random Variables](https://reference.wolfram.com/language/guide/RandomVariables.en.md)
* [Tabular Modeling](https://reference.wolfram.com/language/guide/TabularModeling.en.md)
* [Unsupervised Machine Learning](https://reference.wolfram.com/language/guide/UnsupervisedMachineLearning.en.md)
* [Survival Analysis](https://reference.wolfram.com/language/guide/SurvivalAnalysis.en.md)

## History

* [Introduced in 2010 (8.0)](https://reference.wolfram.com/language/guide/SummaryOfNewFeaturesIn80.en.md) \| [Updated in 2014 (10.0)](https://reference.wolfram.com/language/guide/SummaryOfNewFeaturesIn100.en.md) ▪ [2016 (10.4)](https://reference.wolfram.com/language/guide/SummaryOfNewFeaturesIn104.en.md)