0
$\begingroup$

Suppose I have $50$ training points $x_1$, $x_2,\ldots,x_{50}$ and they are distributed via bimodal Gaussian on real line. Now, given a new point, for $1NN$, I am trying to find a interval around $x$ so that it contains exactly 1 point from my training points.

From some online lecture notes, i.e., this one, I know the unconditioned density can be computed as $$ P(x)=\frac{1}{50V} $$ where $V$ is the length of the interval around $x$ which contains 1 point from training set.

I am trying to compute $P(x)$ but got confused on how to compute $V$.

$\endgroup$

1 Answer 1

1
$\begingroup$

If you refer back to the slides you linked to you'll see that $V$ is not a constant. It is a function of $x$. As it states you compute the distance from $x$ to it's $k^{th}$ closest neighbour, we'll call this distance $r(x)$. Then in order to compute $V(x)$, find the volume of the $d$-dimensional sphere with radius $r(x)$. For instance if you are in two dimensions, $V(x) = \pi \cdot (r(x))^2.

Intuitively, the further you are away from the training points, the larger $r(x)$ will be, the larger $V(x)$ will be, and the smaller our estimate for $P(x)$ will be.

Any questions?

import numpy as np
import matplotlib.pyplot as plt

d = 1
k = 5000
N = 100000

data = np.random.normal(size=N)
grid = np.arange(-3.0, 3.0, 0.1)
D = [[np.linalg.norm(x-y) for y in data] for x in grid]
_ = [l.sort() for l in D]
R = [l[k-1] for l in D]
P = [k/(N*2*r) for r in R]

plt.plot(grid, P)
plt.show()
$\endgroup$
6
  • $\begingroup$ Yes, I understand $V$ is indeed a function, not a constant. But my question is how to really compute it when the distribution of training set is known, i.e., to have a explicit equation so that I can do some future compute of $P(x)$. For example, determine if I increase the data set, what happened to $P$, and also, would $P$ a really density function or not. Thank you! $\endgroup$
    – JumpJump
    Commented Oct 1, 2015 at 17:49
  • $\begingroup$ I was quite explicit about how to compute $V(x)$ and therefore $P(x)$. If you increase the dataset $P(x)$ can go up or down depending on whether or not a point is added closer to $x$ and if so, how much closer. It is not obvious to me that $P$ is a density, in fact, I would be surprised if it were. $\endgroup$ Commented Oct 1, 2015 at 17:51
  • $\begingroup$ Hmm, if is not too much, could you write done the function for $r(x)$ for me? Sorry I am new to stats and ML, and here I really want to understand, if given the data set has normal distribution on really line, how should I compute the distance function by using this normal distribution... $\endgroup$
    – JumpJump
    Commented Oct 1, 2015 at 17:56
  • $\begingroup$ I want to construct a explicit example to help myself understand the unconditional density, and future, the conditional density for different size of data. $\endgroup$
    – JumpJump
    Commented Oct 1, 2015 at 17:58
  • $\begingroup$ I'm sorry that you sensed my impatience there. I can't write an explicit function for $r(x)$ but I can give specific instructions. Given you have $\{x_1 \dots, x_n\}$ samples from your distribution. Compute distances $\|x-x_i\|$ for every $i$, and sort them from least to greatest. $r(x)$ is the $k^{th}$ of these. $\endgroup$ Commented Oct 1, 2015 at 17:59

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.