Why Neighborhood Size Is a Geometric Question
A nearest-neighbor method finds training observations close to a query point. The number of observations requested, \(k\), does not by itself say how close those observations will be. If training data are dense near the query, the kth neighbor may be very close; if observations are sparse, the same \(k\) can require a much larger radius. Predictor dimension matters too: a ball's volume grows at a different rate with radius in one dimension than in several dimensions.
This tutorial describes that relationship using geometry and probability mass. The calculations concern the distance needed to cover a neighborhood, not how the responses of its observations are combined. Unless stated otherwise, distances are Euclidean distances after any required feature scaling, and \(p\) denotes the number of continuous predictor dimensions being considered.
The training predictor vectors are assumed to be independent draws from a distribution with density \(f\), unless an example says otherwise. A density describes probability per unit volume; probability is obtained by integrating it over a region. When the distribution has support \(\mathcal{D}\), observations can occur only in \(\mathcal{D}\).
From Probability Mass to a Neighborhood Radius
Write \(q_x(r)=P(X\in B(x,r))\). For a fixed radius \(r\), the number \(N_r\) of training observations in the ball has a binomial distribution:
The event \(R_k(x)\le r\) occurs exactly when at least \(k\) observations fall inside the ball. Thus, when the predictor distribution is continuous so that ties in distance have probability zero,
This is the exact distributional statement. A useful characteristic-radius approximation sets the expected count \(nq_x(r)\) equal to \(k\). It locates a radius at which the ball contains about the requested number of observations on average. It is not, in general, an exact formula for \(E[R_k(x)]\); randomness in the count and the nonlinear conversion from probability mass to distance matter.
For an interior point \(x\) where \(f\) is continuous and positive, the density is approximately \(f(x)\) throughout a sufficiently small ball. In \(p\) dimensions, the volume of a Euclidean ball of radius \(r\) is \(c_p r^p\), where \(c_p\) is the volume of the unit ball. Consequently, for a small interior ball,
Substitute this expression into \(nq_x(r_k)\approx k\), then solve for the radius:
Why the Dimension Changes the Radius
The formula follows from matching the expected count in a local ball to \(k\). With density held fixed, the ball must have volume about \(k/\{nf(x)\}\). Since its volume is \(c_p r^p\), taking the \(p\)th root converts that required volume to a radius. This is the reason for the exponent \(1/p\): the radius is not proportional to the required volume when \(p>1\).
The unit-ball constants for the first three dimensions are \(c_1=2\), \(c_2=\pi\), and \(c_3=4\pi/3\). In one dimension, a ball is an interval of length \(2r\). In two dimensions it is a disk, and in three dimensions it is a sphere. More generally, if the density and sample size are fixed, increasing \(k\) increases the characteristic radius by the factor \(k^{1/p}\). The radius grows with \(k\), but the rate depends on dimension.
Worked Example: Increasing k in a One-Dimensional Neighborhood
A hypothetical portfolio study has \(n=500\) independent observations of one continuous, scaled predictor. Assume the predictor is uniformly distributed on \([0,1]\), and the query point is \(x=0.50\). Find the characteristic radii for \(k=5\) and \(k=20\). Both neighborhoods are assumed to remain within the interval.
Method. The density is \(f(x)=1\), and an interior one-dimensional ball has length \(2r\). Use the expected-count approximation \(n(2r)\approx k\). This method avoids treating \(k\) itself as a distance or overlooking that the interval extends on both sides of the query.
For \(k=5\), the approximate probability mass is \(5/500=0.01\). The interval length must therefore be \(0.01\), so \(2r_5=0.01\) and \(r_5=0.005\). Check: \(500(2)(0.005)=5\) expected observations.
For \(k=20\), the approximate probability mass is \(20/500=0.04\). Thus \(2r_{20}=0.04\) and \(r_{20}=0.020\). Check: \(500(2)(0.020)=20\) expected observations. The radius is four times as large when \(k\) is four times as large in one dimension.
In higher dimensions, the radius can be much larger even when the required probability mass is unchanged. A fixed volume is spread across more directions; the radius has to expand enough for the \(p\)-dimensional ball to enclose that volume.
Worked Example: Comparing Coverage in One, Two, and Three Dimensions
Suppose \(n=10{,}000\) hypothetical training predictor vectors are uniformly distributed on a unit interval, square, or cube, as appropriate. The query is at the center of each domain, the requested neighborhood size is \(k=10\), and the distance is Euclidean. Calculate the characteristic radius in each dimension.
Method. In each domain the density is 1, so the required ball volume is \(k/n=10/10{,}000=0.001\). Use \(c_p r^p=0.001\). The resulting radii are all below \(0.5\), so the balls centered in the domains do not reach their boundaries.
In one dimension, \(2r=0.001\), giving \(r=0.0005\). Check: \(10{,}000(2)(0.0005)=10\).
In two dimensions, \(\pi r^2=0.001\), so \(r=\sqrt{0.001/\pi}=\sqrt{0.00031831}\approx 0.01784\). Check using the unrounded expression: \(10{,}000\pi(0.001/\pi)=10\).
In three dimensions, \((4\pi/3)r^3=0.001\), so \(r=\{0.001/(4\pi/3)\}^{1/3}\approx 0.06204\). Check using the unrounded expression: \(10{,}000(4\pi/3)\{0.001/(4\pi/3)\}=10\). The radii are rounded to four significant figures where needed. For this same sample size and \(k\), the required radius grows from \(0.0005\) to about \(0.06204\) across these dimensions.
Common mistake avoided. Comparing the numerical radii without accounting for dimension is misleading: the ball volumes, not the radii, were held equal. The different radii are precisely what allows the ball in each dimension to cover the same probability mass.
Boundaries and Uneven Density
The interior approximation \(f(x)c_p r^p\) requires care near the edge of the predictor domain. In general, the probability mass is the integral over the part of the ball where observations can occur:
If the density is approximately constant near \(x\), this integral is approximately \(f(x)\) times the volume of \(B(x,r)\cap\mathcal{D}\). A boundary can make that intersection smaller than the full ball. But when density varies within the region, it is not generally valid to claim that the mass is less than \(f(x)c_p r^p\): higher density elsewhere in the ball can change the comparison. In that setting, use the integral rather than a full-ball calculation.
Worked Example: A Neighborhood Near a Domain Boundary
A hypothetical predictor vector is uniform on the unit square \([0,1]^2\), so its density is 1 inside the square. A query point is \(x=(0.02,0.50)\), and consider a ball of radius \(r=0.10\). With \(n=1{,}000\) training observations, calculate the expected number inside this ball and compare it with the full-disk calculation.
Method. The disk reaches the left boundary but no other boundary. Let \(a=0.02\) be the query's distance from the left edge. The part of the disk outside the square is a circular cap with area \(r^2\arccos(a/r)-a\sqrt{r^2-a^2}\). This follows by taking the sector with angle \(2\arccos(a/r)\), whose area is \(r^2\arccos(a/r)\), and subtracting the triangle area \(a\sqrt{r^2-a^2}\).
Here \(a/r=0.2\), \(\arccos(0.2)\approx1.369438\), and \(\sqrt{r^2-a^2}=\sqrt{0.01-0.0004}=\sqrt{0.0096}\approx0.097980\). The outside cap area is therefore \(0.01(1.369438)-0.02(0.097980)=0.013694-0.001960=0.011735\), to six decimal places.
The full disk area is \(\pi(0.10)^2\approx0.031416\). The area inside the square is \(0.031416-0.011735=0.019681\). Since the density is 1, this is also the probability mass. The expected number of observations is \(1{,}000(0.019681)=19.681\), or about \(19.68\).
For comparison, the interior full-disk approximation would predict \(1{,}000(0.031416)=31.416\) observations. It overstates coverage here because the density is constant and part of the disk lies outside the support. A ball containing an expected 20 observations must have radius slightly greater than \(0.10\), since the radius-\(0.10\) ball has expected count below 20. The check is consistent with the boundary-adjusted mass.
Interpreting Coverage and Avoiding Traps
A neighborhood radius summarizes how far one must search to gather a specified number of observations. A large radius at a given \(k\) signals that those observations cover a wider predictor region; it does not mean the data are uniformly distributed within that region. Local density, domain shape, and distance scaling all affect what the radius represents.
- Do not confuse a count with a mass. The expected count is \(nq_x(r)\); \(q_x(r)\) is a probability. Set \(nq_x(r)\approx k\), not \(q_x(r)\approx k\).
- Do not call the approximation an exact expected distance. The binomial count is random, and converting probability mass to radius is nonlinear. The exact distribution is given by \(P(R_k(x)\le r)=P(N_r\ge k)\).
- Do not assume a full ball fits near an edge. Integrate over the intersection of the ball with the support. Under approximately constant density, the boundary-adjusted volume is appropriate; under varying density, use the density integral.
- Do not use the same radius formula blindly across dimensions. The exponent and unit-ball volume depend on \(p\), and the assumptions of local constancy and an interior point must be checked.
These calculations are useful for judging whether a nominal neighborhood is genuinely local or instead spans a broad region of predictor space. They also explain why the same \(k\) need not provide comparable local coverage across datasets with different dimensions or predictor densities. This geometric perspective is aligned with the SOA Exam SRM (Statistics for Risk Modeling) syllabus.
Check Your Understanding
Use the expected-count approximation unless a question asks for a boundary-adjusted calculation. Give radii to four significant figures when needed.
- A predictor is uniform on \([0,1]\). For \(n=800\), an interior query, and \(k=8\), find the characteristic radius. (Answer: \(0.005\))
- In the same one-dimensional setting, compare \(k=8\) and \(k=32\) with the same \(n=800\). What is the ratio of the characteristic radii? (Answer: \(4\))
- Predictors are uniform on the unit square, \(n=5{,}000\), and the query is at the center. Find the characteristic radius for \(k=5\). (Answer: \(\sqrt{1/(1{,}000\pi)}\approx0.01784\))
- Predictors are uniform on the unit cube, \(n=4{,}000\), and \(k=8\). Find the characteristic radius for a central query. (Answer: \(\{8/[4{,}000(4\pi/3)]\}^{1/3}\approx0.07816\))
- Predictors are uniform on the unit square, \(n=2{,}000\), and the query is \(0.01\) units from one edge and centered relative to the other edges. For \(r=0.05\), find the expected number in the ball, accounting for the boundary. (Answer: approximately \(9.8406\))