核密度估计(KDE)
Author: Rotch
Date: 2025-08-28
1. 算法简介
1.1 核密度估计算法介绍
设 <math xmlns="http://www.w3.org/1998/Math/MathML"> S = ( x ( 1 ) , x ( 2 ) , . . . , x ( n ) ) \mathcal{S} = (x^{(1)}, x^{(2)}, ..., x^{(n)}) </math>S=(x(1),x(2),...,x(n)) 是从总体中抽取的一组独立同分布(i.i.d.)样本数据,在统计分析中,我们通常需要通过这组有限样本,推断出总体的概率分布特征,而核密度估计正是实现这一目标的非参数方法.
1.2 核密度估计算法基本思路
核密度估计的核心思想是:每个样本点都会对其周围区域的概率密度产生一定的 "影响",将所有样本点的贡献叠加起来,就能得到整个数据空间的概率密度分布,具体可拆解为以下两个步骤:
-
为每个样本点分配一个"影响衰减函数":它用于描述单个样本点对周围区域密度的"影响范围"和"影响强度". 距离样本点越近的位置受到的影响越强,函数值越大;反之受到的影响越弱,函数值越小.
-
叠加各样本的"影响衰减函数":将每个样本点对应的缩放核函数在整个数据空间中进行叠加,叠加后的结果就是核密度估计得到的总体概率密度函数.
上述"影响衰减函数"通常用核函数来定义,因此本算法称为"核密度估计". 从直观上理解,核密度估计相当于用无数个 "小的概率密度峰"(每个样本点对应的核函数)共同构建出一个 "整体的概率密度山",这个 "山" 的形状完全由样本点的分布和核函数的特性决定,既能避免参数估计对分布假设的依赖,又能有效平滑数据中的噪声.
2. 核函数
2.1 核函数的定义
在介绍核密度估计前我们先引入对核函数的介绍.
<math xmlns="http://www.w3.org/1998/Math/MathML"> Def 2.1 核函数: \color{blue}{\textbf{Def 2.1 核函数: }} </math>Def 2.1 核函数: 一个核函数 <math xmlns="http://www.w3.org/1998/Math/MathML"> K : R → [ 0 , + ∞ ) K: \mathbb{R} \to [0, +\infty) </math>K:R→[0,+∞) 是一个实值函数,满足以下三条性质:
- 非负性 : <math xmlns="http://www.w3.org/1998/Math/MathML"> K ( u ) ≥ 0 K(u) \geq 0 </math>K(u)≥0 对任意的 <math xmlns="http://www.w3.org/1998/Math/MathML"> u ∈ R u \in \mathbb{R} </math>u∈R;
- 归一性 : <math xmlns="http://www.w3.org/1998/Math/MathML"> ∫ R K ( u ) d u = 1 \int_{\mathbb{R}} K(u) \mathrm{d}u = 1 </math>∫RK(u)du=1;
- 对称性 : <math xmlns="http://www.w3.org/1998/Math/MathML"> K ( − u ) = K ( u ) K(-u) = K(u) </math>K(−u)=K(u) 对任意的 <math xmlns="http://www.w3.org/1998/Math/MathML"> u ∈ R u \in \mathbb{R} </math>u∈R.
非负性确保核函数对周围区域的密度贡献是正向的;归一性保证单个样本点对整体密度的总贡献为 <math xmlns="http://www.w3.org/1998/Math/MathML"> 1 1 </math>1;对称性则确保样本点对其左侧、右侧区域的密度贡献对称,避免引入方向偏差.
以下给出了常见的核函数及其公式:
核函数 | 公式 |
---|---|
高斯核(Gaussian) | <math xmlns="http://www.w3.org/1998/Math/MathML"> K ( u ) = 1 2 π exp ( − u 2 2 ) K(u) = \frac{1}{\sqrt{2 \pi}} \exp(-\frac{u^2}{2}) </math>K(u)=2π 1exp(−2u2) |
Epanechnikov 核 | <math xmlns="http://www.w3.org/1998/Math/MathML"> K ( u ) = { 3 4 ( 1 − u 2 ) , if ∣ u ∣ < 1 0 , otherwise K(u) = \begin{cases} \frac{3}{4}(1 - u^2), & \text{if } \vert u \vert < 1 \\ 0, & \text{otherwise}\end{cases} </math>K(u)={43(1−u2),0,if ∣u∣<1otherwise |
均匀核(Uniform / Rectangular) | <math xmlns="http://www.w3.org/1998/Math/MathML"> K ( u ) = { 1 2 , if ∣ u ∣ < 1 0 , otherwise K(u) = \begin{cases} \frac{1}{2}, & \text{if } \vert u \vert < 1 \\ 0, & \text{otherwise}\end{cases} </math>K(u)={21,0,if ∣u∣<1otherwise |
三角核(Triangular) | <math xmlns="http://www.w3.org/1998/Math/MathML"> K ( u ) = { 1 − ∣ u ∣ , if ∣ u ∣ < 1 0 , otherwise K(u) = \begin{cases} 1 - \vert u \vert, & \text{if } \vert u \vert < 1 \\ 0, & \text{otherwise}\end{cases} </math>K(u)={1−∣u∣,0,if ∣u∣<1otherwise |
双权核(Biweight / Quartic) | <math xmlns="http://www.w3.org/1998/Math/MathML"> K ( u ) = { 15 16 ( 1 − u 2 ) 2 , if ∣ u ∣ < 1 0 , otherwise K(u) = \begin{cases}\frac{15}{16}(1 - u^2)^2, & \text{if } \vert u \vert < 1 \\ 0, & \text{otherwise}\end{cases} </math>K(u)={1615(1−u2)2,0,if ∣u∣<1otherwise |
三权核(Triweight) | <math xmlns="http://www.w3.org/1998/Math/MathML"> K ( u ) = { 35 32 ( 1 − u 2 ) 3 , if ∣ u ∣ < 1 0 , otherwise K(u) = \begin{cases} \frac{35}{32}(1 - u^2)^3, & \text{if } \vert u \vert < 1 \\ 0, & \text{otherwise}\end{cases} </math>K(u)={3235(1−u2)3,0,if ∣u∣<1otherwise |
2.2 缩放核函数的定义与应用
在上述核函数中存在带宽的概念. 以 Epanechnikov 核为例,其默认带宽 (bandwidth)为 <math xmlns="http://www.w3.org/1998/Math/MathML"> h = 1 h = 1 </math>h=1,即对 <math xmlns="http://www.w3.org/1998/Math/MathML"> ∣ u ∣ < 1 \vert u \vert < 1 </math>∣u∣<1 的区域产生影响,对 <math xmlns="http://www.w3.org/1998/Math/MathML"> ∣ u ∣ ≥ 1 \vert u \vert \geq 1 </math>∣u∣≥1 的区域不产生影响. 这在实际应用中会产生"尺度不匹配"的问题,即样本的分布范围可能与带宽不符.
例如:某身高样本的相邻间隔约为 <math xmlns="http://www.w3.org/1998/Math/MathML"> 5 5 </math>5(单位:厘米,下同),而 Epanechnikov 核函数的带宽默认为 <math xmlns="http://www.w3.org/1998/Math/MathML"> 1 1 </math>1,导致核函数的影响范围无法覆盖相邻样本,进而使密度曲线"断裂";为解决这一问题,我们可以缩放带宽至 <math xmlns="http://www.w3.org/1998/Math/MathML"> h = 5 h = 5 </math>h=5,将核函数影响范围扩展到 <math xmlns="http://www.w3.org/1998/Math/MathML"> ± 5 \pm 5 </math>±5,从而有效衔接相邻样本的贡献.
<math xmlns="http://www.w3.org/1998/Math/MathML"> Def 2.2 缩放核函数: \color{blue}{\textbf{Def 2.2 缩放核函数: }} </math>Def 2.2 缩放核函数: 设 <math xmlns="http://www.w3.org/1998/Math/MathML"> K : R → [ 0 , + ∞ ) K: \mathbb{R} \to [0, +\infty) </math>K:R→[0,+∞) 是一给定的核函数,对带宽 <math xmlns="http://www.w3.org/1998/Math/MathML"> h > 0 h > 0 </math>h>0,定义其缩放核函数:
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"> K h ( u ) = 1 h K ( u h ) . \begin{equation} K_h(u) = \frac{1}{h}K(\frac{u}{h}). \end{equation} </math>Kh(u)=h1K(hu).
显然, <math xmlns="http://www.w3.org/1998/Math/MathML"> ∫ R K h ( u ) d u = 1 h ∫ R K ( u h ) d u = 1 \int_\mathbb{R} K_h(u) \mathrm{d}u = \frac{1}{h} \int_\mathbb{R} K(\frac{u}{h}) \mathrm{d}u = 1 </math>∫RKh(u)du=h1∫RK(hu)du=1,即缩放后的核函数不影响核函数的基本性质.
3. 核密度估计算法
3.1 从直方图估计到核密度估计
对于该类问题,最值观的解法是"直方图法". 例如,设有 <math xmlns="http://www.w3.org/1998/Math/MathML"> n n </math>n 个 <math xmlns="http://www.w3.org/1998/Math/MathML"> 1 1 </math>1 维样本点 <math xmlns="http://www.w3.org/1998/Math/MathML"> x ( 1 ) , x ( 2 ) , ... , x ( n ) x^{(1)}, x^{(2)}, \dots, x^{(n)} </math>x(1),x(2),...,x(n), <math xmlns="http://www.w3.org/1998/Math/MathML"> x ( i ) ∈ [ a , b ] x^{(i)} \in [a, b] </math>x(i)∈[a,b]. 作 <math xmlns="http://www.w3.org/1998/Math/MathML"> [ a , b ] [a, b] </math>[a,b] 的等距分划 <math xmlns="http://www.w3.org/1998/Math/MathML"> T : a = a 0 < a 1 < a 2 < ⋯ < a n = b T: a = a_0 < a_1 < a_2 < \dots < a_n = b </math>T:a=a0<a1<a2<⋯<an=b, <math xmlns="http://www.w3.org/1998/Math/MathML"> a i = a 0 + i n a_i = a_0 + \frac{i}{n} </math>ai=a0+ni, <math xmlns="http://www.w3.org/1998/Math/MathML"> ∥ T ∥ = a k − a k − 1 = b − a n \parallel T \parallel = a_k - a_{k - 1} = \frac{b - a}{n} </math>∥T∥=ak−ak−1=nb−a. 则可以估计密度函数:
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"> p ^ ( x ) = 1 n ∥ T ∥ ∑ i = 1 n 1 ( x ( i ) ∈ [ a k − 1 , a k ) ) . \begin{equation} \hat{p}(x) = \frac{1}{n \parallel T \parallel} \sum\limits_{i = 1}^{n} \mathbb{1}\left( x^{(i)} \in [a_{k - 1}, a_k) \right). \end{equation} </math>p^(x)=n∥T∥1i=1∑n1(x(i)∈[ak−1,ak)).
显然,该方法存在以下缺陷:
- 区间的位置和宽度会严重影响估计结果 :例如,改变区间的端点或改变分划 <math xmlns="http://www.w3.org/1998/Math/MathML"> T T </math>T 都会对结果产生影响;
- 密度函数不连续 : <math xmlns="http://www.w3.org/1998/Math/MathML"> p ^ ( x ) \hat{p}(x) </math>p^(x) 不连续,无法反映数据分布的平滑性;
- 难以推广到高维空间 :当数据维度增加时,区间数量会呈指数级增长;且大多数区间内无样本点,致使其密度估计为 <math xmlns="http://www.w3.org/1998/Math/MathML"> 0 0 </math>0.
核密度估计正是为解决这些问题而提出的:它将直方图的 "区间贡献" 替换为 "样本点的核函数贡献",用连续的核函数替代离散的区间,从而实现平滑、连续的密度估计.
3.2 核密度估计算法推导
设总体的随机变量为 <math xmlns="http://www.w3.org/1998/Math/MathML"> X X </math>X,其概率分布函数为 <math xmlns="http://www.w3.org/1998/Math/MathML"> F ( x ) = P ( X ≤ x ) F(x) = P(X \leq x) </math>F(x)=P(X≤x),概率密度函数为 <math xmlns="http://www.w3.org/1998/Math/MathML"> p ( x ) p(x) </math>p(x),则有:
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"> p ( x ) = d d x F ( x ) = F ( x + h ) − F ( x − h ) 2 h + ο ( h ) = P ( x − h < X ≤ x + h ) 2 h + ο ( h ) . ( h → 0 + ) \begin{align} p(x) = \frac{\mathrm{d}}{\mathrm{d}x} F(x) &= \frac{F(x + h) - F(x - h)}{2h} + \omicron(h) \nonumber \\ &= \frac{P(x - h < X \leq x + h)}{2h} + \omicron(h). \space (h \rightarrow 0^+) \end{align} </math>p(x)=dxdF(x)=2hF(x+h)−F(x−h)+ο(h)=2hP(x−h<X≤x+h)+ο(h). (h→0+)
根据大数定律,当样本空间足够大时,可以用频率近似概率,得到:
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"> P ( x − h < X ≤ x + h ) ≈ 1 n ⋅ ∑ i = 1 n 1 ( ∣ x − x ( i ) ∣ h ≤ 1 ) \begin{equation} P(x - h < X \leq x + h) \approx \frac{1}{n} \cdot \sum\limits_{i = 1}^{n} \mathbb{1} \left( \frac{\vert x - x^{(i)} \vert}{h} \leq 1 \right) \end{equation} </math>P(x−h<X≤x+h)≈n1⋅i=1∑n1(h∣x−x(i)∣≤1)
将 <math xmlns="http://www.w3.org/1998/Math/MathML"> ( 4 ) (4) </math>(4) 带入 <math xmlns="http://www.w3.org/1998/Math/MathML"> ( 3 ) (3) </math>(3),得:
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"> p ( x ) ≈ 1 2 n h ⋅ ∑ i = 1 n 1 ( ∣ x − x ( i ) ∣ h ≤ 1 ) = 1 n ⋅ ∑ i = 1 n 1 h ⋅ K ( x − x ( i ) h ) , \begin{align} p(x) &\approx \frac{1}{2nh} \cdot \sum\limits_{i = 1}^{n} \mathbb{1} \left( \frac{\vert x - x^{(i)} \vert}{h} \leq 1 \right) \nonumber \\ &= \frac{1}{n} \cdot \sum\limits_{i = 1}^{n} \frac{1}{h} \cdot K \left(\frac{x - x^{(i)}}{h} \right), \end{align} </math>p(x)≈2nh1⋅i=1∑n1(h∣x−x(i)∣≤1)=n1⋅i=1∑nh1⋅K(hx−x(i)),
其中 <math xmlns="http://www.w3.org/1998/Math/MathML"> K ( u ) = 1 2 1 ( ∣ u ∣ ≤ 1 ) K(u) = \frac{1}{2} \mathbb{1} ( \vert u \vert \leq 1) </math>K(u)=211(∣u∣≤1),即:均匀核函数. 下面我们验证 <math xmlns="http://www.w3.org/1998/Math/MathML"> p ( x ) p(x) </math>p(x) 是概率密度函数,事实上:
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"> ∫ R p ( x ) d x = 1 n ∑ i = 1 n ∫ R 1 h K ( x − x ( i ) h ) d x = 1. \begin{equation} \int_\mathbb{R} p(x) \mathrm{d}x = \frac{1}{n} \sum\limits_{i = 1}^{n} \int_\mathbb{R} \frac{1}{h} K\left(\frac{x - x^{(i)}}{h} \right) \mathrm{d}x = 1. \end{equation} </math>∫Rp(x)dx=n1i=1∑n∫Rh1K(hx−x(i))dx=1.
若将上式中的 <math xmlns="http://www.w3.org/1998/Math/MathML"> K ( x ) K(x) </math>K(x) 替换为其它核函数,得到的 <math xmlns="http://www.w3.org/1998/Math/MathML"> p ( x ) p(x) </math>p(x) 仍为概率密度函数.
3.3 核密度估计算法
<math xmlns="http://www.w3.org/1998/Math/MathML"> Algorithm: Kernel Density Estimation \color{blue}{\textbf{Algorithm: Kernel Density Estimation}} </math>Algorithm: Kernel Density Estimation 设 <math xmlns="http://www.w3.org/1998/Math/MathML"> ( x ( 1 ) , x ( 2 ) , ... , x ( n ) ) \left( x^{(1)}, x^{(2)}, \dots, x^{(n)} \right) </math>(x(1),x(2),...,x(n)) 是一组给定的独立同分布的简单随机样本, <math xmlns="http://www.w3.org/1998/Math/MathML"> K ( x ) K(x) </math>K(x) 是给定的核函数, <math xmlns="http://www.w3.org/1998/Math/MathML"> h > 0 h > 0 </math>h>0 为给定带宽,则核密度估计给出的概率密度估计函数为:
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"> p ^ ( x ) = 1 n h ∑ i = 1 n K ( x − x ( i ) h ) = 1 n ∑ i = 1 n K h ( x − x ( i ) ) . \begin{equation} \hat{p}(x) = \frac{1}{nh} \sum\limits_{i = 1}^{n} K \left( \frac{x - x^{(i)}}{h} \right) = \frac{1}{n} \sum\limits_{i = 1}^{n}K_h(x - x^{(i)}). \end{equation} </math>p^(x)=nh1i=1∑nK(hx−x(i))=n1i=1∑nKh(x−x(i)).
其中核函数 <math xmlns="http://www.w3.org/1998/Math/MathML"> K ( x ) K(x) </math>K(x) 即为样本点的"影响衰减函数",不同样本点的影响衰减函数彼此相同. 通过选取不同的核函数和带宽,可以改变样本点的影响衰减效应,从而改变概率密度估计. 本文剩余内容将介绍如何选取合适的核函数与带宽.
4. 带宽的选择
4.1 评价指标
要选取合适的带宽,首先要给出带宽的评价指标. 我们发现当带宽 <math xmlns="http://www.w3.org/1998/Math/MathML"> h h </math>h 增大时,影响衰减函数图像越"扁"、曲线越平滑,此时方差减小、偏差增大;反之当带宽 <math xmlns="http://www.w3.org/1998/Math/MathML"> h h </math>h 减小时,影响衰减函数图像越"尖"、曲线越陡峭,此时方差增大、偏差减小. 因此,要评价带宽,必须给出对方差和偏差的综合评价指标.
<math xmlns="http://www.w3.org/1998/Math/MathML"> Def 4.1 均方误差: \color{blue}{\textbf{Def 4.1 均方误差: }} </math>Def 4.1 均方误差: 函数 <math xmlns="http://www.w3.org/1998/Math/MathML"> M S E ( f ^ ( x ) ) = E [ ( f ^ ( x ) − f ( x ) ) 2 ] \mathrm{MSE} \left( \hat{f}(x) \right) = \mathbb{E}\left[ \left( \hat{f}(x) - f(x) \right)^2 \right] </math>MSE(f^(x))=E[(f^(x)−f(x))2] 称为均方误差(Mean Squared Error).
<math xmlns="http://www.w3.org/1998/Math/MathML"> Prop 4.2: \color{blue}{\textbf{Prop 4.2: }} </math>Prop 4.2: <math xmlns="http://www.w3.org/1998/Math/MathML"> M S E ( f ^ ( x ) ) = V a r ( f ^ ( x ) ) + B i a s 2 ( f ^ ( x ) ) \mathrm{MSE} \left( \hat{f}(x) \right) = \mathrm{Var} \left( \hat{f}(x) \right) + \mathrm{Bias}^2 \left( \hat{f}(x) \right) </math>MSE(f^(x))=Var(f^(x))+Bias2(f^(x)).
<math xmlns="http://www.w3.org/1998/Math/MathML"> Proof: \color{brown}{\textbf{Proof: }} </math>Proof: 易知 <math xmlns="http://www.w3.org/1998/Math/MathML"> E [ f ^ ( x ) − E ( f ^ ( x ) ) ] = 0 \mathbb{E} \left[ \hat{f}(x) - \mathbb{E} \left( \hat{f}(x) \right) \right] = 0 </math>E[f^(x)−E(f^(x))]=0,则有:
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"> M S E ( f ^ ( x ) ) = E [ ( f ^ ( x ) − f ( x ) ) 2 ] = E [ [ ( f ^ ( x ) − E [ f ^ ( x ) ] ) + ( E [ f ^ ( x ) ] − f ( x ) ) ] 2 ] = E [ ( f ^ ( x ) − E [ f ^ ( x ) ] ) 2 ] + 2 E [ ( f ^ ( x ) − E [ f ^ ( x ) ] ) ( E [ f ^ ( x ) ] − f ( x ) ) ] + E [ ( E [ f ^ ( x ) ] − f ( x ) ) 2 ] = V a r ( f ^ ( x ) ) + 2 ( E [ f ^ ( x ) ] − f ( x ) ) E [ f ^ ( x ) − E [ f ^ ( x ) ] ] + ( E [ f ^ ( x ) ] − f ( x ) ) 2 = V a r ( f ^ ( x ) ) + B i a s 2 ( f ^ ( x ) ) . □ \begin{align} \mathrm{MSE} \left( \hat{f}(x) \right) &= \mathbb{E}\left[ \left( \hat{f}(x) - f(x) \right)^2 \right] \nonumber \\ &= \mathbb{E} \left[ \left[ \left( \hat{f}(x) - \mathbb{E} \left[ \hat{f}(x) \right] \right) + \left( \mathbb{E} \left[ \hat{f}(x) \right] - f(x) \right) \right]^2 \right] \nonumber \\ &= \mathbb{E} \left[ \left( \hat{f}(x) - \mathbb{E} \left[ \hat{f}(x) \right] \right)^2 \right] + 2 \mathbb{E} \left[ \left( \hat{f}(x) - \mathbb{E} \left[ \hat{f}(x) \right] \right) \left( \mathbb{E} \left[ \hat{f}(x) \right] - f(x) \right) \right] \nonumber \\ & \quad + \mathbb{E} \left[ \left(\mathbb{E} \left[ \hat{f}(x) \right] - f(x)\right)^2 \right]\nonumber \\ &= \mathrm{Var} \left( \hat{f}(x) \right) + 2 \left( \mathbb{E} \left[ \hat{f}(x) \right] - f(x) \right) \mathbb{E} \left[ \hat{f}(x) - \mathbb{E} \left[ \hat{f}(x) \right] \right] + \left(\mathbb{E} \left[ \hat{f}(x) \right] - f(x)\right)^2 \nonumber \\ &= \mathrm{Var} \left( \hat{f}(x) \right) + \mathrm{Bias}^2 \left( \hat{f}(x) \right). \square \end{align} </math>MSE(f^(x))=E[(f^(x)−f(x))2]=E[[(f^(x)−E[f^(x)])+(E[f^(x)]−f(x))]2]=E[(f^(x)−E[f^(x)])2]+2E[(f^(x)−E[f^(x)])(E[f^(x)]−f(x))]+E[(E[f^(x)]−f(x))2]=Var(f^(x))+2(E[f^(x)]−f(x))E[f^(x)−E[f^(x)]]+(E[f^(x)]−f(x))2=Var(f^(x))+Bias2(f^(x)).□
<math xmlns="http://www.w3.org/1998/Math/MathML"> Def 4.3 均方积分误差: \color{blue}{\textbf{Def 4.3 均方积分误差: }} </math>Def 4.3 均方积分误差: 积分值 <math xmlns="http://www.w3.org/1998/Math/MathML"> M I S E ( f ^ ( x ) ) = ∫ R M S E ( f ^ ( x ) ) d x \mathrm{MISE} \left( \hat{f}(x) \right) = \int_\mathbb{R} \mathrm{MSE} \left( \hat{f}(x) \right) \mathrm{d}x </math>MISE(f^(x))=∫RMSE(f^(x))dx 称为均方积分误差(Mean Integrated Squared Error).
最优带宽 <math xmlns="http://www.w3.org/1998/Math/MathML"> h o p t h_{\mathrm{opt}} </math>hopt 理论上是使均方积分误差最小的带宽,即:
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"> h o p t = arg min h M I S E ( f ^ ( x ) ) \begin{equation} h_{\mathrm{opt}} = \arg \mathop{\min}\limits_{h} \mathrm{MISE}\left( \hat{f}(x) \right) \end{equation} </math>hopt=arghminMISE(f^(x))
4.2 Silverman 方法
Silverman 方法是计算最优带宽的一种简单方法,适用于数据整体近似正态分布的情况,具体公式如下:
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"> h o p t = C ⋅ σ ⋅ n − 1 / 5 . \begin{equation} h_{\mathrm{opt}} = C \cdot \sigma \cdot n^{-1/5}. \end{equation} </math>hopt=C⋅σ⋅n−1/5.
其中 <math xmlns="http://www.w3.org/1998/Math/MathML"> C C </math>C 是依赖于核函数的常数; <math xmlns="http://www.w3.org/1998/Math/MathML"> σ \sigma </math>σ 是数据的标准差; <math xmlns="http://www.w3.org/1998/Math/MathML"> n n </math>n 是样本量.
Silverman 方法的基本思想是通过渐进均方积分误差 代替均方积分误差,进而得到最优带宽. 首先计算 <math xmlns="http://www.w3.org/1998/Math/MathML"> E ( f ^ ( x ) ) \mathbb{E}\left( \hat{f}(x) \right) </math>E(f^(x)):
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"> E ( f ^ ( x ) ) = E [ 1 n h ∑ i = 1 n K ( x − x ( i ) h ) ] = 1 n h ∑ i = 1 n E [ K ( x − x ( i ) h ) ] = 1 h ∫ R K ( x − t h ) f ( t ) d t = = = u = x − t h ∫ R K ( u ) f ( x − h u ) d u . \begin{align} \mathbb{E}\left( \hat{f}(x) \right) &= \mathbb{E} \left[ \frac{1}{nh} \sum\limits_{i = 1}^{n} K \left( \frac{x - x^{(i)}}{h} \right) \right] \nonumber \\ &= \frac{1}{nh} \sum\limits_{i = 1}^{n} \mathbb{E} \left[ K \left( \frac{x - x^{(i)}}{h} \right) \right] \nonumber \\ &= \frac{1}{h} \int_{\mathbb{R}} K \left( \frac{x - t}{h} \right) f(t) \mathrm{d}t \nonumber \\ &\overset{u = \frac{x - t}{h}}{=\!=\!=} \int_{\mathbb{R}} K(u) f(x - hu) \mathrm{d}u. \end{align} </math>E(f^(x))=E[nh1i=1∑nK(hx−x(i))]=nh1i=1∑nE[K(hx−x(i))]=h1∫RK(hx−t)f(t)dt===u=hx−t∫RK(u)f(x−hu)du.
假设真实分布函数 <math xmlns="http://www.w3.org/1998/Math/MathML"> f ( x ) f(x) </math>f(x) 二阶可导,对 <math xmlns="http://www.w3.org/1998/Math/MathML"> f ( x − h u ) f(x - hu) </math>f(x−hu) 作二阶 Taylor 展开,得:
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"> f ( x − h u ) = f ( x ) − h u f ′ ( x ) + 1 2 ( h u ) 2 f ′ ′ ( x ) + ο ( h 2 ) . \begin{equation} f(x - hu) = f(x) - hu f'(x) + \frac{1}{2} (hu)^2 f''(x) + \omicron(h^2). \end{equation} </math>f(x−hu)=f(x)−huf′(x)+21(hu)2f′′(x)+ο(h2).
将 <math xmlns="http://www.w3.org/1998/Math/MathML"> ( 12 ) (12) </math>(12) 带入 <math xmlns="http://www.w3.org/1998/Math/MathML"> ( 11 ) (11) </math>(11) 得:
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"> E ( f ^ ( x ) ) = ∫ R K ( u ) [ f ( x ) − h u f ′ ( x ) + 1 2 ( h u ) 2 f ′ ′ ( x ) + ο ( h 2 ) ] d u = f ( x ) ∫ R K ( u ) d u − h f ′ ( x ) ∫ R u K ( u ) d u + 1 2 h 2 f ′ ′ ( x ) ∫ R u 2 K ( u ) d u + ο ( h 2 ) = f ( x ) + 1 2 h 2 f ′ ′ ( x ) μ 2 ( K ) + ο ( h 2 ) . \begin{align} \mathbb{E}\left( \hat{f}(x) \right) &= \int_{\mathbb{R}} K(u) \left[ f(x) - hu f'(x) + \frac{1}{2} (hu)^2 f''(x) + \omicron(h^2)\right] \mathrm{d}u \nonumber \\ &= f(x) \int_\mathbb{R} K(u) \mathrm{d}u - h f'(x) \int_\mathbb{R} u K(u) \mathrm{d}u + \frac{1}{2}h^2 f''(x) \int_\mathbb{R} u^2 K(u) \mathrm{d}u + \omicron(h^2)\nonumber \\ &= f(x) + \frac{1}{2}h^2 f''(x) \mu_2(K) + \omicron(h^2). \end{align} </math>E(f^(x))=∫RK(u)[f(x)−huf′(x)+21(hu)2f′′(x)+ο(h2)]du=f(x)∫RK(u)du−hf′(x)∫RuK(u)du+21h2f′′(x)∫Ru2K(u)du+ο(h2)=f(x)+21h2f′′(x)μ2(K)+ο(h2).
上式中最后一个等号用到了核函数的归一性和对称性,其中 <math xmlns="http://www.w3.org/1998/Math/MathML"> μ 2 ( K ) = ∫ R u 2 K ( u ) d u \mu_2(K) = \int_{\mathbb{R}} u^2 K(u) \mathrm{d}u </math>μ2(K)=∫Ru2K(u)du 是核函数 <math xmlns="http://www.w3.org/1998/Math/MathML"> K K </math>K 的二阶矩 . 接下来计算 <math xmlns="http://www.w3.org/1998/Math/MathML"> B i a s ( f ^ ( x ) ) \mathrm{Bias} \left( \hat{f}(x) \right) </math>Bias(f^(x)) 渐进等式:
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"> B i a s ( f ^ ( x ) ) = E ( f ^ ( x ) ) − f ( x ) = 1 2 h 2 f ′ ′ ( x ) μ 2 ( K ) + ο ( h 2 ) . B i a s 2 ( f ^ ( x ) ) = 1 4 h 4 [ f ′ ′ ( x ) ] 2 μ 2 2 ( K ) + ο ( h 4 ) . \begin{align} \mathrm{Bias} \left( \hat{f}(x) \right) = \mathbb{E} \left( \hat{f}(x) \right) - f(x) = \frac{1}{2}h^2 f''(x) \mu_2(K) + \omicron(h^2). \\ \mathrm{Bias}^2 \left( \hat{f}(x) \right) = \frac{1}{4}h^4 [f''(x)]^2 \mu_2^2(K) + \omicron(h^4). \end{align} </math>Bias(f^(x))=E(f^(x))−f(x)=21h2f′′(x)μ2(K)+ο(h2).Bias2(f^(x))=41h4[f′′(x)]2μ22(K)+ο(h4).
再计算 <math xmlns="http://www.w3.org/1998/Math/MathML"> V a r ( f ^ ( x ) ) \mathrm{Var} \left( \hat{f}(x) \right) </math>Var(f^(x)) 的渐进等式:
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"> V a r ( f ^ ( x ) ) = 1 n h 2 ( E [ K 2 ( x − x ( i ) h ) ] − E 2 [ K ( x − x ( i ) h ) ] ) = 1 n h 2 [ h ∫ R K 2 ( u ) f ( x − h u ) d u − ( h ∫ R K ( u ) f ( x − h u ) d u ) 2 ] . \begin{align} \mathrm{Var} \left( \hat{f}(x) \right) &= \frac{1}{nh^2} \left( \mathbb{E}\left[ K^2 \left( \frac{x - x^{(i)}}{h} \right) \right] - \mathbb{E}^2 \left[ K \left( \frac{x - x^{(i)}}{h} \right) \right] \right) \nonumber \\ &= \frac{1}{nh^2} \left[ h\int_{\mathbb{R}} K^2(u) f(x - hu) \mathrm{d}u - \left( h \int_{\mathbb{R}} K(u) f(x - hu) \mathrm{d}u \right)^2 \right]. \end{align} </math>Var(f^(x))=nh21(E[K2(hx−x(i))]−E2[K(hx−x(i))])=nh21[h∫RK2(u)f(x−hu)du−(h∫RK(u)f(x−hu)du)2].
这里只对 <math xmlns="http://www.w3.org/1998/Math/MathML"> f ( x − h u ) f(x - hu) </math>f(x−hu) Taylor 展开至一阶:
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"> f ( x − h u ) = f ( x ) + ο ( 1 ) . \begin{equation} f(x - hu) = f(x) + \omicron(1). \end{equation} </math>f(x−hu)=f(x)+ο(1).
将 <math xmlns="http://www.w3.org/1998/Math/MathML"> ( 17 ) (17) </math>(17) 带入 <math xmlns="http://www.w3.org/1998/Math/MathML"> ( 16 ) (16) </math>(16),得:
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"> V a r ( f ^ ( x ) ) = 1 n h 2 ( h f ( x ) ∫ R K 2 ( u ) d u + ο ( h ) − h 2 f 2 ( x ) − ο ( h 2 ) ) = f ( x ) n h R ( K ) + ο ( 1 n h ) . \begin{align} \mathrm{Var} \left( \hat{f}(x) \right) &= \frac{1}{nh^2} \left( h f(x) \int_{\mathbb{R}} K^2(u) \mathrm{d}u + \omicron(h) - h^2 f^2(x) - \omicron(h^2) \right) \nonumber \\ &= \frac{f(x)}{nh} R(K) + \omicron \left( \frac{1}{nh} \right). \end{align} </math>Var(f^(x))=nh21(hf(x)∫RK2(u)du+ο(h)−h2f2(x)−ο(h2))=nhf(x)R(K)+ο(nh1).
上式中最后一个等号直接将 <math xmlns="http://www.w3.org/1998/Math/MathML"> h 2 f 2 ( x ) h^2f^2(x) </math>h2f2(x) 视作 <math xmlns="http://www.w3.org/1998/Math/MathML"> ο ( h ) \omicron(h) </math>ο(h),其中 <math xmlns="http://www.w3.org/1998/Math/MathML"> R ( K ) = ∫ R K 2 ( u ) d u R(K) = \int_{\mathbb{R}} K^2(u) \mathrm{d}u </math>R(K)=∫RK2(u)du 是核函数 <math xmlns="http://www.w3.org/1998/Math/MathML"> K K </math>K 的平方积分 . 于是得到均方误差的渐进形式,即渐进均方误差(Asymptotic MSE):
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"> A M S E ( f ^ ( x ) ) = 1 4 h 4 [ f ′ ′ ( x ) ] 2 μ 2 2 ( K ) + f ( x ) n h R ( K ) . \begin{equation} \mathrm{AMSE} \left( \hat{f}(x) \right) = \frac{1}{4}h^4 [f''(x)]^2 \mu_2^2(K) + \frac{f(x)}{nh} R(K). \end{equation} </math>AMSE(f^(x))=41h4[f′′(x)]2μ22(K)+nhf(x)R(K).
进而有渐进均方积分误差(Asymptotic MISE):
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"> A M I S E ( f ^ ( x ) ) = ∫ R A M S E ( x ) d x = 1 4 h 4 μ 2 2 ( K ) J ( f ) + R ( K ) n h . \begin{equation} \mathrm{AMISE} \left( \hat{f}(x) \right) = \int_{\mathbb{R}} \mathrm{AMSE}(x) \mathrm{d}x = \frac{1}{4}h^4 \mu_2^2(K) J(f) + \frac{R(K)}{nh}. \end{equation} </math>AMISE(f^(x))=∫RAMSE(x)dx=41h4μ22(K)J(f)+nhR(K).
其中 <math xmlns="http://www.w3.org/1998/Math/MathML"> J ( f ) = ∫ R [ f ′ ′ ( x ) ] 2 d x J(f) = \int_\mathbb{R} \left [ f''(x) \right]^2 \mathrm{d}x </math>J(f)=∫R[f′′(x)]2dx 是密度函数的二阶导数平方积分 . 对 <math xmlns="http://www.w3.org/1998/Math/MathML"> A M I S E ( f ^ ( x ) ) \mathrm{AMISE} \left( \hat{f}(x) \right) </math>AMISE(f^(x)) 关于 <math xmlns="http://www.w3.org/1998/Math/MathML"> h h </math>h 求导,得:
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"> d d h A M I S E ( f ^ ( x ) ) = h 3 μ 2 2 ( K ) J ( f ) − R ( K ) n h 2 . \begin{equation} \frac{\mathrm{d}}{\mathrm{d}h} \mathrm{AMISE} \left( \hat{f}(x) \right) = h^3 \mu_2^2(K) J(f) - \frac{R(K)}{nh^2}. \end{equation} </math>dhdAMISE(f^(x))=h3μ22(K)J(f)−nh2R(K).
令 <math xmlns="http://www.w3.org/1998/Math/MathML"> d d h A M I S E ( f ^ ( x ) ) = 0 \frac{\mathrm{d}}{\mathrm{d}h} \mathrm{AMISE} \left( \hat{f}(x) \right) = 0 </math>dhdAMISE(f^(x))=0,有:
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"> h o p t = R ( K ) μ 2 2 ( K ) J ( f ) ⋅ 1 n 5 . \begin{equation} h_{\mathrm{opt}} = \sqrt[5]{\frac{R(K)}{\mu_2^2(K)J(f)} \cdot \frac{1}{n}}. \end{equation} </math>hopt=5μ22(K)J(f)R(K)⋅n1 .
假设样本近似服从正态分布 <math xmlns="http://www.w3.org/1998/Math/MathML"> N ( μ , σ 2 ) N(\mu, \sigma^2) </math>N(μ,σ2),此时可用正态分布密度函数替换 <math xmlns="http://www.w3.org/1998/Math/MathML"> f f </math>f. 令 <math xmlns="http://www.w3.org/1998/Math/MathML"> f ( x ) = 1 2 π σ exp ( − ( x − μ ) 2 2 σ 2 ) f(x) = \frac{1}{\sqrt{2 \pi} \sigma} \exp(-\frac{(x - \mu)^2}{2\sigma^2}) </math>f(x)=2π σ1exp(−2σ2(x−μ)2),计算其二阶导数:
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"> f ′ ′ ( x ) = f ( x ) ⋅ ( ( x − μ ) 2 σ 4 − 1 σ 2 ) . \begin{equation} f''(x) = f(x) \cdot \left( \frac{(x - \mu)^2}{\sigma^4} - \frac{1}{\sigma^2} \right). \end{equation} </math>f′′(x)=f(x)⋅(σ4(x−μ)2−σ21).
计算 <math xmlns="http://www.w3.org/1998/Math/MathML"> J ( f ) J(f) </math>J(f):
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"> J ( f ) = ∫ R [ f ′ ′ ( x ) ] 2 d x = 1 2 π σ 10 [ ∫ R ( x − μ ) 4 e − ( x − μ ) 2 σ 2 d x − 2 σ 2 ∫ R ( x − μ ) 2 e − ( x − μ ) 2 σ 2 d x + σ 4 ∫ R e − ( x − μ ) 2 σ 2 d x ] = 1 2 π σ 10 [ 3 π 4 σ 5 − 2 ⋅ π 2 σ 5 + π σ 5 ] = 3 8 π σ 5 . \begin{align} J(f) &= \int_{\mathbb{R}} \left[ f''(x) \right]^2 \mathrm{d}x \nonumber \\ &= \frac{1}{2 \pi \sigma^{10}} \left[ \int_{\mathbb{R}} (x - \mu)^4 e^{-\frac{(x - \mu)^2}{\sigma^2}} \mathrm{d}x - 2\sigma^2 \int_{\mathbb{R}} (x - \mu)^2 e^{-\frac{(x - \mu)^2}{\sigma^2}} \mathrm{d}x + \sigma^4 \int_{\mathbb{R}} e^{-\frac{(x - \mu)^2}{\sigma^2}} \mathrm{d}x \right] \nonumber \\ &= \frac{1}{2 \pi \sigma^{10}} \left[ \frac{3 \sqrt{\pi}}{4} \sigma^5 - 2 \cdot \frac{\sqrt{\pi}}{2} \sigma^5 + \sqrt{\pi} \sigma^5 \nonumber \right] \\ &= \frac{3}{8\sqrt{\pi} \sigma^5}. \end{align} </math>J(f)=∫R[f′′(x)]2dx=2πσ101[∫R(x−μ)4e−σ2(x−μ)2dx−2σ2∫R(x−μ)2e−σ2(x−μ)2dx+σ4∫Re−σ2(x−μ)2dx]=2πσ101[43π σ5−2⋅2π σ5+π σ5]=8π σ53.
记 <math xmlns="http://www.w3.org/1998/Math/MathML"> C = 8 π R ( K ) 3 μ 2 2 ( K ) 5 C = \sqrt[5]{\frac{8 \sqrt{\pi} R(K)}{3 \mu_2^2(K)}} </math>C=53μ22(K)8π R(K) ,于是有:
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"> h o p t = C ⋅ σ ⋅ n − 1 / 5 . \begin{equation} h_{\mathrm{opt}} = C \cdot \sigma \cdot n^{-1/5}. \end{equation} </math>hopt=C⋅σ⋅n−1/5.
若选择 Gauss 核,则 <math xmlns="http://www.w3.org/1998/Math/MathML"> C G ≈ 1.06 C_\mathrm{G} \approx 1.06 </math>CG≈1.06;若选择 Epanechnikov 核,则 <math xmlns="http://www.w3.org/1998/Math/MathML"> C E ≈ 2.34 C_\mathrm{E} \approx 2.34 </math>CE≈2.34;若选择均匀核,则 <math xmlns="http://www.w3.org/1998/Math/MathML"> C U ≈ 1 / 36 C_{\mathrm{U}} \approx 1/36 </math>CU≈1/36. 具体计算过程略.
4.3 留一交叉验证法(Leave-One-Out CV, LOO-CV)
留一交叉验证法同样使用 <math xmlns="http://www.w3.org/1998/Math/MathML"> M I S E \mathrm{MISE} </math>MISE 作为最优带宽的选择标准. 不同的是,留一交叉验证法构建了 <math xmlns="http://www.w3.org/1998/Math/MathML"> L O O C V \mathrm{LOOCV} </math>LOOCV 函数,
首先,回顾 <math xmlns="http://www.w3.org/1998/Math/MathML"> M I S E \mathrm{MISE} </math>MISE 的公式:
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"> M I S E ( f ^ ( x ) ) = ∫ R E [ ( f ^ ( x ) − f ( x ) ) 2 ] d x = E [ ∫ R f ^ 2 ( x ) d x ] − 2 E [ ∫ R f ^ ( x ) f ( x ) d x ] + ∫ R f 2 ( x ) d x . \begin{align} \mathrm{MISE}(\hat{f}(x)) &= \int_{\mathbb{R}} \mathbb{E}\left[ \left( \hat{f}(x) - f(x) \right)^2 \right] \mathrm{d}x \nonumber \\ &= \mathbb{E}\left[ \int_{\mathbb{R}} \hat{f}^2 (x) \mathrm{d}x \right] - 2 \mathbb{E}\left[ \int_{\mathbb{R}} \hat{f}(x) f(x) \mathrm{d}x \right] + \int_{\mathbb{R}} f^2(x) \mathrm{d}x. \end{align} </math>MISE(f^(x))=∫RE[(f^(x)−f(x))2]dx=E[∫Rf^2(x)dx]−2E[∫Rf^(x)f(x)dx]+∫Rf2(x)dx.
其中 <math xmlns="http://www.w3.org/1998/Math/MathML"> ∫ R f 2 ( x ) d x \int_{\mathbb{R}} f^2(x) \mathrm{d}x </math>∫Rf2(x)dx 与 <math xmlns="http://www.w3.org/1998/Math/MathML"> h h </math>h 无关. 因此最小化 <math xmlns="http://www.w3.org/1998/Math/MathML"> M I S E \mathrm{MISE} </math>MISE 等价于最小化 <math xmlns="http://www.w3.org/1998/Math/MathML"> E [ ∫ R f ^ 2 ( x ) d x ] − 2 E [ ∫ R f ^ ( x ) f ( x ) d x ] \mathbb{E}\left[ \int_{\mathbb{R}} \hat{f}^2 (x) \mathrm{d}x \right] - 2 \mathbb{E}\left[ \int_{\mathbb{R}} \hat{f}(x) f(x) \mathrm{d}x \right] </math>E[∫Rf^2(x)dx]−2E[∫Rf^(x)f(x)dx]. 由于 <math xmlns="http://www.w3.org/1998/Math/MathML"> f ( x ) f(x) </math>f(x) 未知,无法直接求解. 下面采用 Monte-Carlo 算法的思想,对其进行近似.
对于每个 <math xmlns="http://www.w3.org/1998/Math/MathML"> j = 1 , 2 , ... , n j = 1, 2, \dots, n </math>j=1,2,...,n,作训练集 <math xmlns="http://www.w3.org/1998/Math/MathML"> S − j = S ∖ { x ( j ) } \mathcal{S}{-j} = \mathcal{S} \setminus \{x^{(j)}\} </math>S−j=S∖{x(j)},利用 <math xmlns="http://www.w3.org/1998/Math/MathML"> S − j \mathcal{S}{-j} </math>S−j 作函数 <math xmlns="http://www.w3.org/1998/Math/MathML"> f ^ h , − j ( x ) \hat{f}{h, -j}(x) </math>f^h,−j(x):
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"> f ^ h , − j ( x ) = 1 ( n − 1 ) h ∑ i = 1 , i ≠ j n K ( x − x ( i ) h ) . \begin{equation} \hat{f}{h, -j}(x) = \frac{1}{(n - 1)h} \sum\limits_{i = 1, i \not= j}^{n} K \left( \frac{x - x^{(i)}}{h} \right). \end{equation} </math>f^h,−j(x)=(n−1)h1i=1,i=j∑nK(hx−x(i)).
显然有:
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"> ∫ R f ^ ( x ) f ( x ) d x ≈ 1 n ∑ j = 1 n f ^ h , − j ( x ( j ) ) \begin{equation} \int_{\mathbb{R}} \hat{f}(x) f(x) \mathrm{d}x \approx \frac{1}{n} \sum\limits_{j = 1}^{n} \hat{f}_{h, -j}(x^{(j)}) \end{equation} </math>∫Rf^(x)f(x)dx≈n1j=1∑nf^h,−j(x(j))
定义 <math xmlns="http://www.w3.org/1998/Math/MathML"> L O O C V \mathrm{LOOCV} </math>LOOCV 函数:
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"> L O O C V ( h ) = ∫ R f ^ 2 ( x ) d x − 2 n ∑ j = 1 n f ^ h , − j ( x ( j ) ) . \begin{equation} \mathrm{LOOCV}(h) = \int_{\mathbb{R}} \hat{f}^2 (x) \mathrm{d}x - \frac{2}{n} \sum\limits_{j = 1}^{n} \hat{f}_{h, -j}(x^{(j)}). \end{equation} </math>LOOCV(h)=∫Rf^2(x)dx−n2j=1∑nf^h,−j(x(j)).
记常数 <math xmlns="http://www.w3.org/1998/Math/MathML"> C = ∫ R f 2 ( x ) d x C = \int_{\mathbb{R}} f^2(x) \mathrm{d}x </math>C=∫Rf2(x)dx,有:
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"> E [ L O O C V ( h ) ] ≈ M I S E ( f ^ ( x ) ) + C \begin{equation} \mathbb{E} \left[ \mathrm{LOOCV}(h) \right] \approx \mathrm{MISE}(\hat{f}(x)) + C \end{equation} </math>E[LOOCV(h)]≈MISE(f^(x))+C
对于固定带宽,当样本量足够大时,成立:
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"> L O O C V ( h ) → p E [ L O O C V ( h ) ] \begin{equation} \mathrm{LOOCV}(h) \xrightarrow{p} \mathbb{E} \left[ \mathrm{LOOCV}(h) \right] \end{equation} </math>LOOCV(h)p E[LOOCV(h)]
因此最小化 <math xmlns="http://www.w3.org/1998/Math/MathML"> L O O C V ( h ) \mathrm{LOOCV}(h) </math>LOOCV(h) 等价于最小化 <math xmlns="http://www.w3.org/1998/Math/MathML"> M I S E ( f ^ ( x ) ) \mathrm{MISE}(\hat{f}(x)) </math>MISE(f^(x)). 即:
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"> h L O O C V = arg min h L O O C V ( h ) . \begin{equation} h_{\mathrm{LOOCV}} = \arg \mathop{\min}\limits_{h} \mathrm{LOOCV}\left( h \right). \end{equation} </math>hLOOCV=arghminLOOCV(h).
下面补充 <math xmlns="http://www.w3.org/1998/Math/MathML"> L O O C V \mathrm{LOOCV} </math>LOOCV 中 <math xmlns="http://www.w3.org/1998/Math/MathML"> ∫ R f ^ 2 ( x ) d x \int_{\mathbb{R}} \hat{f}^2 (x) \mathrm{d}x </math>∫Rf^2(x)dx 的计算方法:
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"> ∫ R f ^ h ( x ) 2 d x = 1 n 2 h 2 ∑ i = 1 n ∑ j = 1 n ∫ R K ( x − x ( i ) h ) K ( x − x ( j ) h ) d x = = = u = x − x ( i ) h 1 n 2 h 2 ∑ i = 1 n ∑ j = 1 n ∫ R K ( u ) K ( u + x ( i ) − x ( j ) h ) h d u . = 1 n 2 h ∑ i = 1 n ∑ j = 1 n ( K ∗ K ) ( x ( i ) − x ( j ) h ) . \begin{align} \int_{\mathbb{R}} \hat{f}h(x)^2 dx &= \frac{1}{n^2 h^2} \sum{i = 1}^{n} \sum_{j = 1}^{n} \int_{\mathbb{R}} K\left( \frac{x - x^{(i)}}{h} \right) K\left( \frac{x - x^{(j)}}{h} \right) \mathrm{d}x \nonumber \\ &\overset{u = \frac{x - x^{(i)}}{h}}{=\!=\!=} \frac{1}{n^2 h^2} \sum_{i = 1}^{n} \sum_{j = 1}^{n} \int_{\mathbb{R}} K(u) K \left( u + \frac{x^{(i)} - x^{(j)}}{h} \right) h \mathrm{d}u. \nonumber \\ &= \frac{1}{n^2 h} \sum_{i = 1}^{n} \sum_{j = 1}^{n} (K * K) \left( \frac{x^{(i)} - x^{(j)}}{h} \right) \end{align}. </math>∫Rf^h(x)2dx=n2h21i=1∑nj=1∑n∫RK(hx−x(i))K(hx−x(j))dx===u=hx−x(i)n2h21i=1∑nj=1∑n∫RK(u)K(u+hx(i)−x(j))hdu.=n2h1i=1∑nj=1∑n(K∗K)(hx(i)−x(j)).
例如对于 Gauss 核,可直接利用其卷积公式 <math xmlns="http://www.w3.org/1998/Math/MathML"> ( K G ∗ K G ) ( t ) = 1 2 π e − t 2 / 4 (K_{\mathrm{G}} * K_{\mathrm{G}} )(t) = \frac{1}{2\sqrt{\pi}}e^{-t^2/4} </math>(KG∗KG)(t)=2π 1e−t2/4 快速计算.
4.4 直观法
直观法是核密度估计中带宽选择的 "经验驱动型方法",核心思路是通过可视化密度曲线形态 和主观判断平滑性与细节的平衡选择带宽,无需复杂的数学推导或迭代计算,适用于对估计精度要求不高、数据结构简单或需快速探索的场景.
直观法一般给出一系列可选择的带宽 <math xmlns="http://www.w3.org/1998/Math/MathML"> H = { h k ∣ k = 1 , 2 , ... , m } \mathcal{H} = \{h_k \mid k = 1, 2, \dots, m\} </math>H={hk∣k=1,2,...,m},生成多组密度函数,进而选取最优带宽(此处的最优指最适合展示或计算的带宽,非理论最优带宽). 例如,在处理小样本问题过程中,Silverman 方法和 LOO-CV 法都有较大误差. 此时可以使用直观法,确定较为合适的带宽. 此外,在数据分析初期,也可通过直观法生成多组带宽的密度曲线,辅助分析数据分布趋势.
5. 核函数的选择
5.1 Epanechnikov 核:理论最优核
在讨论最优核之前,我们先对核函数补充两个限制:
- 要求任意核函数 <math xmlns="http://www.w3.org/1998/Math/MathML"> K K </math>K 必须有紧支集 ,即 <math xmlns="http://www.w3.org/1998/Math/MathML"> s u p p ( K ) = K − 1 ( R ∖ { 0 } ) ‾ \mathrm{supp}(K) = \overline{K^{-1}(\mathbb{R} \setminus \{0\})} </math>supp(K)=K−1(R∖{0}) 有界,不妨设 <math xmlns="http://www.w3.org/1998/Math/MathML"> s u p p ( K ) = [ − 1 , 1 ] \mathrm{supp}(K) = [-1, 1] </math>supp(K)=[−1,1].
- 核函数连续.
不符合上述两个限制的核包含 Gauss 核及均匀核,我们将在后面进行讨论. 而在选择最优带宽下,满足上述两个限制的最优核函数是 Epanechnikov 核. 我们将继续在最小化 <math xmlns="http://www.w3.org/1998/Math/MathML"> A M I S E \mathrm{AMISE} </math>AMISE 下讨论.
重新回顾 Silverman 方法中引入的 <math xmlns="http://www.w3.org/1998/Math/MathML"> A M I S E \mathrm{AMISE} </math>AMISE 及 <math xmlns="http://www.w3.org/1998/Math/MathML"> h o p t h_{\mathrm{opt}} </math>hopt:
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"> A M I S E ( f ^ ( x ) ) = 1 4 h 4 μ 2 2 ( K ) J ( f ) + R ( K ) n h , h o p t = R ( K ) μ 2 2 ( K ) J ( f ) ⋅ 1 n 5 . \begin{align} \mathrm{AMISE} \left( \hat{f}(x) \right) &= \frac{1}{4}h^4 \mu_2^2(K) J(f) + \frac{R(K)}{nh}, \\ h_{\mathrm{opt}} &= \sqrt[5]{\frac{R(K)}{\mu_2^2(K)J(f)} \cdot \frac{1}{n}}. \end{align} </math>AMISE(f^(x))hopt=41h4μ22(K)J(f)+nhR(K),=5μ22(K)J(f)R(K)⋅n1 .
将 <math xmlns="http://www.w3.org/1998/Math/MathML"> ( 35 ) (35) </math>(35) 带入 <math xmlns="http://www.w3.org/1998/Math/MathML"> ( 34 ) (34) </math>(34),得:
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"> A M I S E ( f ^ ( x ) ) = 5 4 ( R 4 ( K ) ⋅ μ 2 2 ( K ) ⋅ J ( f ) ⋅ n − 4 ) 1 / 5 . \begin{equation} \mathrm{AMISE} \left( \hat{f}(x) \right) = \frac{5}{4} \left( R^4(K) \cdot \mu_2^2(K) \cdot J(f) \cdot n^{-4} \right)^{1/5}. \end{equation} </math>AMISE(f^(x))=45(R4(K)⋅μ22(K)⋅J(f)⋅n−4)1/5.
上式表明在最优化带宽下最小化 <math xmlns="http://www.w3.org/1998/Math/MathML"> A M I S E \mathrm{AMISE} </math>AMISE 等价于最小化 <math xmlns="http://www.w3.org/1998/Math/MathML"> R 2 ( K ) μ 2 ( K ) R^2(K) \mu_2(K) </math>R2(K)μ2(K). 接下来我们使用变分法求解.
由于核函数 <math xmlns="http://www.w3.org/1998/Math/MathML"> K K </math>K 具有紧支集,故对 <math xmlns="http://www.w3.org/1998/Math/MathML"> K K </math>K 的各种积分的界限都可以改为 <math xmlns="http://www.w3.org/1998/Math/MathML"> [ − 1 , 1 ] [-1, 1] </math>[−1,1],仍记为原符号. 设 <math xmlns="http://www.w3.org/1998/Math/MathML"> μ 2 ( K ) = A \mu_2(K) = A </math>μ2(K)=A,在 <math xmlns="http://www.w3.org/1998/Math/MathML"> μ 2 ( K ) = A \mu_2(K) = A </math>μ2(K)=A 及 <math xmlns="http://www.w3.org/1998/Math/MathML"> ∫ [ − 1 , 1 ] K ( u ) d u \int_{[-1, 1]} K(u) \mathrm{d}u </math>∫[−1,1]K(u)du 的约束下,最小化 <math xmlns="http://www.w3.org/1998/Math/MathML"> R ( K ) R(K) </math>R(K),可以建立 Lagrange 函数:
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"> L ( K , λ 1 , λ 2 ) = ∫ [ − 1 , 1 ] K 2 ( u ) d u + λ 1 ( 1 − ∫ [ − 1 , 1 ] K ( u ) d u ) + λ 2 ( A − ∫ [ − 1 , 1 ] u 2 K ( u ) d u ) . \begin{equation} \mathcal{L}(K,\lambda_1,\lambda_2)=\int_{[-1, 1]} K^2(u) \mathrm{d}u + \lambda_1 \left( 1 - \int_{[-1, 1]} K(u) \mathrm{d}u \right) + \lambda_2 \left( A - \int_{[-1, 1]} u^2K(u) \mathrm{d}u \right). \end{equation} </math>L(K,λ1,λ2)=∫[−1,1]K2(u)du+λ1(1−∫[−1,1]K(u)du)+λ2(A−∫[−1,1]u2K(u)du).
对 <math xmlns="http://www.w3.org/1998/Math/MathML"> L \mathcal{L} </math>L 关于 <math xmlns="http://www.w3.org/1998/Math/MathML"> K K </math>K 求变分:
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"> δ L δ K ( u ) = 2 K ( u ) − λ 1 − λ 2 u 2 . \begin{equation} \frac{\delta \mathcal{L}}{\delta K(u)} = 2K(u) - \lambda_1 - \lambda_2 u^2. \end{equation} </math>δK(u)δL=2K(u)−λ1−λ2u2.
令其泛函导数为 <math xmlns="http://www.w3.org/1998/Math/MathML"> 0 0 </math>0,解得:
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"> K ( u ) = 1 2 λ 1 + 1 2 λ 2 u 2 . \begin{equation} K(u) = \frac{1}{2} \lambda_1 + \frac{1}{2} \lambda_2 u^2. \end{equation} </math>K(u)=21λ1+21λ2u2.
考虑到约束 <math xmlns="http://www.w3.org/1998/Math/MathML"> K ( − 1 ) = K ( 1 ) = 0 K(-1) = K(1) = 0 </math>K(−1)=K(1)=0,得:
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"> λ 1 = − λ 2 . \begin{equation} \lambda_1 = -\lambda_2. \end{equation} </math>λ1=−λ2.
再考虑到 <math xmlns="http://www.w3.org/1998/Math/MathML"> K K </math>K 的归一性,有:
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"> ∫ [ − 1 , 1 ] K ( u ) d u = λ 1 2 ∫ [ − 1 , 1 ] ( 1 − u 2 ) d u = 1 , ⟹ λ 1 = 3 2 , K ( u ) = 3 4 ( 1 − u 2 ) . \begin{align} \int_{[-1, 1]} K(u) \mathrm{d}u = \frac{\lambda_1}{2} \int_{[-1, 1]} \left( 1 - u^2 \right) \mathrm{d}u = 1, \\ \implies \lambda_1 = \frac{3}{2}, \space K(u) = \frac{3}{4} \left( 1 - u^2 \right). \end{align} </math>∫[−1,1]K(u)du=2λ1∫[−1,1](1−u2)du=1,⟹λ1=23, K(u)=43(1−u2).
上式中 <math xmlns="http://www.w3.org/1998/Math/MathML"> K ( u ) K(u) </math>K(u) 即为 Epanechnikov 核.
5.2 Gauss 核:实践最优核
Gauss 核因适配多数实践场景需求,被广泛视为'实践最优核".
首先我们讨论 Gauss 核的有效区域. 虽然 Gauss 核不具有紧支集,但是对于 <math xmlns="http://www.w3.org/1998/Math/MathML"> h > 0 h > 0 </math>h>0:
- 当 <math xmlns="http://www.w3.org/1998/Math/MathML"> ∣ x ∣ > 3 h \vert x \vert > 3h </math>∣x∣>3h 时, <math xmlns="http://www.w3.org/1998/Math/MathML"> exp ( − x 2 2 h 2 ) < exp ( − 9 2 ) ≈ 0.011 \exp\left(-\frac{x^2}{2h^2}\right) < \exp\left(-\frac{9}{2}\right) \approx 0.011 </math>exp(−2h2x2)<exp(−29)≈0.011,贡献已不足 1%;
- 当 <math xmlns="http://www.w3.org/1998/Math/MathML"> ∣ x ∣ > 5 h \vert x \vert > 5h </math>∣x∣>5h 时, <math xmlns="http://www.w3.org/1998/Math/MathML"> exp ( − x 2 2 h 2 ) < exp ( − 25 2 ) ≈ 3 × 1 0 − 6 \exp\left(-\frac{x^2}{2h^2}\right) < \exp\left(-\frac{25}{2}\right) \approx 3 \times 10^{-6} </math>exp(−2h2x2)<exp(−225)≈3×10−6,贡献几乎可忽略.
即:Gauss 核的实际有效区域是 <math xmlns="http://www.w3.org/1998/Math/MathML"> [ − 5 h , 5 h ] [-5h, 5h] </math>[−5h,5h],在实际计算中完全可将有效区域外样本的权重视为 <math xmlns="http://www.w3.org/1998/Math/MathML"> 0 0 </math>0,计算效率与紧支集核几乎无差异. Gauss 核的广泛应用还离不开以下三个个关键特质:
- 无线光滑性 : <math xmlns="http://www.w3.org/1998/Math/MathML"> K G ∈ C ∞ ( R ) K_{\mathrm{G}} \in C^{\infty}(\mathbb{R}) </math>KG∈C∞(R),其生成的密度函数 <math xmlns="http://www.w3.org/1998/Math/MathML"> f ^ \hat{f} </math>f^ 也继承了这一优良数学性质,能完美匹配真实密度的光滑特性. 而其它核(如:Epanechnikov 核)在需要高阶导数条件下(如:峰值分析、梯度分析)表现不佳.
- 抗干扰性:由于 Gauss 核的权重是连续平滑的,且对远离待估计点的 "异常值样本" 赋予极低的权重,因此在数据存在少量噪声或异常值时,Gauss 核的密度估计结果不易被干扰. 而其它核若恰好将异常值纳入支集内,会直接影响局部密度估计,抗干扰性相对较弱.
- 高维稳定性 :高维数据中,"距离" 的定义会因维度灾难导致样本分布稀疏,紧支集核可能因支集内样本过少而无法有效估计密度(甚至支集内无样本,导致估计值为 <math xmlns="http://www.w3.org/1998/Math/MathML"> 0 0 </math>0). 而 Gauss 核的指数衰减特性能通过带宽调整,灵活适配高维数据的稀疏性------即使局部样本少,也能通过平滑的权重分配维持密度估计的连续性,避免出现 "零密度区域" 的不合理结果.
此外,在其它分析中,Gauss 核还具有严格正定性等优良性质,是核分析中的常用函数.
5.3 均匀核
均匀核既非理论最优核,也没有 Gauss 核的优良数学性质,反而缺点重重. 但是均匀核在特定情况下有其作用. 例如,在认为"某点周围 <math xmlns="http://www.w3.org/1998/Math/MathML"> K K </math>K 个样本的类别完全同等重要"时,可以使用均匀核. 此外,在一些低计算成本场景下(如:嵌入式开发、实时数据处理),均匀核因其计算简单而受青睐.
总而言之,不同核有其各自的优缺点,实际应用中要根据应用场景的特点,选择合适的核函数.