贝叶斯
贝叶斯分类器是一种概率框架下的统计学习分类器,对分类任务而言,假设在相关概率都已知
的情况下,贝叶斯分类器考虑如何基于这些概率为样本判定最优的类标。在开始介绍贝叶斯决
策论之前,我们首先来回顾下贝叶斯公式。
贝叶斯决策论
若将上述定义中样本空间的划分 Bi 看做为类标,A 看做为一个新的样本,则很容易将条件概
率理解为样本 A 是类别 Bi 的概率。在机器学习训练模型的过程中,往往我们都试图去优化一
个风险函数,因此在概率框架下我们也可以为贝叶斯定义“条件风险”(conditional risk)
若损失函数λ取 0-1 损失,则
[推导]:由公式(7.1)和公式(7.4)可得:
又$\sum_{j=1}^{N}P(c_j|\boldsymbol x)=1$,则:
此即为公式(7.5)
我们的任务就是寻找一个判定准则最小化所有样本的条件风险总和,因此就有了贝叶斯判定准
则(Bayes decision rule):为最小化总体风险,只需在每个样本上选择那个使得条件风险最小的
类标。
对于给定的样本 x,P(x)与类标无关,P(c)称为类先验概率,p(x | c )称为类条件概率。
这时估计后验概率 P(c | x)就变成为估计类先验概率和类条件概率的问题。对于先验概率和
后验概率,普及一下它们的基本概念。
- 先验概率: 根据以往经验和分析得到的概率。
- 后验概率:后验概率是基于新的信息,修正原来的先验概率后所获得的更接近实际情况的概
率估计。
实际上先验概率就是在没有任何结果出来的情况下估计的概率,而后验概率则是在有一定依据
后的重新估计,直观意义上后验概率就是条件概率
极大似然法
回归正题,对于类先验概率 P(c),p(c)就是样本空间中各类样本所占的比例,根据大数
定理(当样本足够多时,频率趋于稳定等于其概率),这样当训练样本充足时,p(c)可以使用
各类出现的频率来代替。因此只剩下类条件概率 p(x | c ),它表达的意思是在类别 c 中出现
x 的概率,它涉及到属性的联合概率问题,若只有一个离散属性还好,当属性多时采用频率估
计起来就十分困难,因此这里一般采用极大似然法进行估计。
极大似然估计(Maximum Likelihood Estimation,简称 MLE),是一种根据数据采样来估计概率
分布的经典方法。常用的策略是先假定总体具有某种确定的概率分布,再基于训练样本对概率
分布的参数进行估计。运用到类条件概率 p(x | c )中,假设 p(x | c )服从一个参数为θ的
分布,问题就变为根据已知的训练样本来估计θ。极大似然法的核心思想就是:估计出的参数
使得已知样本出现的概率最大,即使得训练数据的似然最大。
所以,贝叶斯分类器的训练过程就是参数估计。总结最大似然法估计参数的过程,一般分为以
下四个步骤:
- 1.写出似然函数;
- 2.对似然函数取对数,并整理;
- 3.求导数,令偏导数为 0,得到似然方程组;
- 4.解似然方程组,得到所有参数即为所求。
7.12
[推导]:参见公式(7.13)
7.13
[推导]:根据公式(7.11)和公式(7.10)可知参数求解公式为
由西瓜书上下文可知,此时假设概率密度函数$p(\boldsymbol{x} | c) \sim \mathcal{N}\left(\boldsymbol{\mu}_{c}, \boldsymbol{\sigma}_{c}^{2}\right)$,其等价于假设
其中,$d$表示$\boldsymbol{x}$的维数,$\boldsymbol{\Sigma}_c=\boldsymbol{\sigma}_{c}^{2}$为对称正定协方差矩阵,$|\boldsymbol{\Sigma}_c|$表示$\boldsymbol{\Sigma}_c$的行列式。将其代入参数求解公式可得
假设此时数据集$D_c$中的样本个数为$n$,也即$|D_c|=n$,则上式可以改写为
为了便于分别求解$\hat{\boldsymbol{\mu}}_{c}$和$\hat{\boldsymbol{\Sigma}}_{c}$,在这里我们根据公式$\boldsymbol{x}^{\mathrm{T}}\mathbf{A}\boldsymbol{x}=\operatorname{tr}(\mathbf{A}\boldsymbol{x}\boldsymbol{x}^{\mathrm{T}}),\bar{\boldsymbol{x}}=\frac{1}{n}\sum_{i=1}^{n}\boldsymbol{x}_i$将上式中的最后一项作如下恒等变形
所以
观察上式可知,由于此时$\boldsymbol{\Sigma}_c^{-1}$和$\boldsymbol{\Sigma}_c$一样均为正定矩阵,所以当$\boldsymbol{\mu}_c-\bar{\boldsymbol{x}}\neq\boldsymbol{0}$时,上式最后一项为正定二次型。根据正定二次型的性质可知,上式最后一项取值的大小此时仅与$\boldsymbol{\mu}_c-\bar{\boldsymbol{x}}$相关,而且当且仅当$\boldsymbol{\mu}_c-\bar{\boldsymbol{x}}=\boldsymbol{0}$时,上式最后一项取到最小值0,此时可以解得
将求解出来的$\hat{\boldsymbol{\mu}}_{c}$代回参数求解公式可得新的参数求解公式为
此时的参数求解公式是仅与$\boldsymbol{\Sigma}_c$相关的函数。为了求解$\hat{\boldsymbol{\Sigma}}_{c}$,在这里我们不加证明地给出一个引理(具体证明参见参考文献[8]):设$\mathbf{B}$为$p$阶正定矩阵,$n>0$为实数,在对所有$p$阶正定矩阵$\boldsymbol{\Sigma}$有
当且仅当$\boldsymbol{\Sigma}=\frac{1}{n}\mathbf{B}$时等号成立。所以根据此引理可知,当且仅当$\boldsymbol{\Sigma}_c=\frac{1}{n}\sum_{i=1}^{n}(\boldsymbol{x}_i-\bar{\boldsymbol{x}})(\boldsymbol{x}_i-\bar{\boldsymbol{x}})^{\mathrm{T}}$
时,上述参数求解公式中$\arg \min$后面的式子取到最小值,那么此时的$\boldsymbol{\Sigma}_c$即为我们想要求解的$\hat{\boldsymbol{\Sigma}}_{c}$。