# Constrained kernel density estimation

Suppose you are trying to estimate the joint density $p(x,y)$ based on observed $(X,Y)$. However, you know that the marginal density $p(x)$ is uniform. How can you use this information to improve your density estimate?

• homework? See stats.stackexchange.com/faq – Rob Hyndman Jun 2 '11 at 5:01
• Not homework. This is for the purpose of creating a "conditional density plot" for simulation results. – charles.y.zheng Jun 2 '11 at 15:03
• It looks like the question is incomplete. What are the ranges of $X$ and $Y$, what about their distribution, i.e., whether they are discrete or continuous or can be anything, about their dependence and what about the marginal distribution of $Y$? – Ashok Jun 4 '11 at 4:59

My answer: I assume $(X,Y)$ is abs cont, i.e. has a density $f_{(X,Y)}$ with respect to Lebesgue measure in $\mathbb{R}^2$. Everything can be done by using parameter "weights" of function density in R, i.e. with a call to

$$\text{density}\left ((Y_1,\dots,Y_n),\; \text{ weights}=(e^{|x-X_i|^2/h^2})_{i=1,\dots,n}\right )$$
to estimate $\hat{f}_{Y}^{|X=x}(y)$ (for a given $x$) and a proper use of conditioning formulae (given at the end of the post (you also need to tune the window parameter $h$).

Developpment: I assume $X\leadsto \mathcal{U}[0,1]$. Since you know $f_{X}$ you only need to estimate $\hat{f}_{Y}^{|X=x}(y)$ the conditional density.

Depending on how much observation you have, there might be different strategies. The simplest one is to do a regular binning of $[0,1]$, say with $p$ bins and estimate $\hat{f}_{Y}^{|X=x_i}(y)$ ($x_i$ stands for the center of bin $i$) with an histogram (or with function density of R) for each $i=1,\dots,p$. Your final estimate is $$\hat{f}_{(X,Y)}(x,y)=\frac{1}{p}\sum_{i}1(x\in B_i)\hat{f}_{Y}^{|X=x_i}(y)$$

$1(x\in B_i)$ is the indicator function that $x$ is in bin $i$.

Obviously you can turn the zero-one weigthing scheme ($1(x\in B_i)$) into a smoother one (you need to adapt the calculation of $\hat{f}_{Y}^{|X=x_i}(y)$ but it is easy since function "density" of R allows for a parameter "weigths"). Your weights have to keep the invariance by translation which characterize the uniform distribution. For example, with exponential weight of windows parameter $h>0$ this will give, for a given $x\in [0,1]$, something like:

$$\hat{f}_{(X,Y)}(x,y)=1(x\in [0,1])\frac{1}{\sqrt{2\pi}h}\int_{t}e^{|x-t|^2/h^2}\hat{f}_{Y}^{|X=x}(y) dt$$

where $\hat{f}_{Y}^{|X=x}(y)$ is obtained with function "density" of R and parameter weights=$(e^{|x-X_i|^2/h^2})_{i=1,\dots,n}$.