# Restricting Kernel Density Estimate to n-sided polygon

Given some arbitrarily distributed data, in this case data generated by two normal distributions,

$$\mathcal{N_1}(\mu_1, \Sigma_1)$$ and $$\mathcal{N_2}(\mu_2, \Sigma_2)$$ where $$\mu_1 = \begin{bmatrix}0 & 0\end{bmatrix}^T, \Sigma_1 = \begin{bmatrix}10 & 0 \\ 0 & 100\end{bmatrix}$$ and $$\mu_2 = \begin{bmatrix}20 & 10\end{bmatrix}^T, \Sigma_2 = \begin{bmatrix}45 & 0 \\ 0 & 35\end{bmatrix}$$

A multivariate KDE is computed, in this case using Python and KDEpy, with a Gaussian kernel and bandwidth $$3I$$. An arbitrary n-sided polygon is given, which can be considered as a sequence of piece-wise linear segments. The particular polygon is defined by the tuples $$[(-10, -10), (22, 3), (30, 27), (-12, 30), (-20, 0)]$$.

My question is then as follows: Is it possible to perform boundary correction to adhere towards such an arbitrary polygon? I would like to compute the KDE such that the PDF is contained within the polygon, without necessarily "filling" it up completely.

I'm aware that for the 1D case and "simple" geometries, boundary correction can be achieved by techniques such as mirroring the data. However, I'm unsure what is classified a simple geometries. For 2D and higher dimensions, I have seen boundary correction can be achieved by analytical boundaries described by shapes such circles or ellipses.

• Sure it's possible. I have performed this correction simply through truncation: zero out the tails beyond the polygon and renormalize the remainder. This is not universally applicable, though: whether it makes sense depends on what you are using your KDE for.
– whuber
Aug 24 '21 at 22:01
• Would you clarify as to what "without necessarily 'filling' it up completely" means? And what @whuber suggests is exactly what one would do if you had the "true" bivariate density function.
– JimB
Aug 25 '21 at 0:03
• @whuber Thank you for your comment. Should the proposed truncation occur at the distribution of each data point? One thing to mention is that my is not necessarily described by n normal distributions, this detail was included if someone wanted to recreate the above plots. Aug 25 '21 at 7:05
• @Jimb Thank you for your comment. My comment regarding "filling" refers to that the polygon should simply act as a boundary, It is not the intention that the KDE is re-distributed to fit the entire shape. The polygon is simply a bound on the feasible 2D states. I apologise if this is worded poorly. Aug 25 '21 at 7:10
• Yes, that sounds like the correct grid operation. Once again, I am assuming you are trying to truncate the estimated distribution to the polygon rather than performing some other operation.
– whuber
Aug 26 '21 at 17:13