Maximum likelihood estimation, test and Naive Bayes classifier algorithm to estimate the maximum

Maximum likelihood estimation, test and Naive Bayes classifier algorithm to estimate the maximum

table of Contents

  I. Introduction

  Second, the probability theory foundation

  Third, the maximum likelihood estimate

  Fourth, the maximum a posteriori estimation

  V. Bayesian classifier

  VI. References

 

I. Introduction

  The main content of this article is the author of a review of the probability theory foundation of content, and personal interpretation of some of the knowledge points. In addition, these knowledge on the basis of the above, review the basics of probability inference maximum likelihood estimation and the maximum a posteriori estimation. Finally, the end of the article reviews the basic flow of naive Bayes classification method, and with a small case to help readers better understand the basic process of the method.

Second, the probability theory foundation

(1) the probability of

  Definitions [1]: Let E be randomized trials, S is its sample space. For each event E A gives a real number, referred to as P (A), called the probability of A, if the function is set, and P satisfy the following conditions (.):

  (1) a non-negative: for each event A, there is P (A)> = 0;

  (2) Specifications: the inevitable event for S, with a p (S) = 1;

  (3) the column may be additive: Let A1, A2, ... pairwise mutually exclusive events, i.e. for AiAj = Ø, i ≠ j, i, j = 1,2, ..., there :

  P(A1∪A2∪A3...)=P(A1)+P(A2)+P(A3)+....

(2) random variable

  Refers to a random variable can take multiple values of a random variable, capital letters used herein, denotes a random variable, its value is represented by lower case letters, such as: random variable X, X can be a value of { . 1 , X 2 , X . 3 , ...}. Random variable is just a representation of all possible states of a random phenomenon, its value is not necessarily a real number, and random variables need to match exactly express the probability of each state that may occur will be mentioned later in the probability distribution. For example, throw a coin, the result of the coin last fall that may occur is a random event, we can use a random variable X to represent the final result may occur, then the possible values for the random variable {positive, negative}; throw a dice, after landing will be positive values represented by the random variable Y, the possible values of {1,2,3,4,5,6}.

Probability (3) distributed random variables

  The probability distribution of random variables used to describe all the possible values ​​of the random variable probability size. Since the value of the random variable may be discrete, as described above, and a coin toss dice example; may be continuous, i.e., all possible values ​​of the random variable not individually listed, which may be a value within the interval within the real axis any one point [2], for example: a person in double eleven amount spent on Taobao, and so long a day when a sports person. For discrete random variables and continuous random variable, respectively, using the probability mass function and a probability density function to describe the probability distribution.

  Probability mass function : the probability that a random variable X is the probability mass function for each value of a random variable is mapped to the random variable corresponding to the value taken, we use X ~ P (X) is expressed "obey the random variable X P (X) distribution. " X is a random variable probability mass function P (X) needs to satisfy the following properties:

  (1) must be a random variable domain of all possible values ​​of X;

  (2) x for any value of X, P (x)> = 0 and P (x) <= 1

  (3)Σx∈X P(X)=1

  Or to toss a coin as an example, we use the random variable X to represent the final outcome might toss a coin, all possible values ​​of the random variable {positive, negative}, according to our experience, a coin toss result is positive or 0.5 are negative probability, then the random variable X obey probability mass distribution: P (X = positive) = 0.5, P (X = negative) = 0.5.

  Probability density function : Since all possible values of continuous random variable is not individually recited, describe different ways and discrete probability distribution of random variables, using the probability density function. For a probability density function F (X), needs to satisfy the following properties:

  (1) it must be a range of all possible values ​​of the random variable and set

  (2) x for any value of X, F (x)> = 0

  (3)∫F(x)dx=1

  For the continuous random variable X, it takes a certain probability value is 0, i.e., P (X = X . 1 ) = 0. Common probability density function of a uniform distribution and normal distribution.

(4) the joint probability distribution

  联合分布指的是两个或者多个随机变量同时取某些值的概率分布。例如,对于随机变量X和Y而言,当X=x1,同时Y=y1时,其联合概率分布为P(X=x1,Y=y1)。下面以一个更为具体的例子来说明一下联合概率分布。一个盒子中有两红一白的三个球,现在从中不放回取出两个球,求下面的概率:

  (1)第一次取出红球的概率;

  (2)第二次取出红球的概率;

  (3)两次同时取出红球的概率。

  解答:

  (1)以随机变量X来表示第一次取球的结果,显然P(X=红球)=2/3;

  (2)以随机变量Y来表示第二次取球的结果,本问求的时P(Y=红球)。需要注意的是采取的是“不放回”的取球方式,第一次的取球结果会影响到盒子中红白球的比例,也就影响到了第二次取球的概率,因此需要根据第一次的取球结果来分情况探讨第二次取红球的所有可能的情况。如果第一次从两个红球中取出一个红球的话,那么第二次取出红球的所有可能情况为{红红,红红};如果第一次取出白球的话,那么第二次就可以从剩余的两个红球中取,因此所有可能的情况为{白红,白红}。总体的样本空间为{红白,红红,红白,红红,白红,白红},共6种情况,那么P(Y=红球)=2/3;

  (3)第三问求的是当X=红球时,同时Y=红球的概率,是一个联合分布。根据上一问中所得出的样本空间的情况,可知满足条件的情况只有两种,因此P(X=红球,Y=红球)=1/3。

  如果两个随机变量相互之间是独立的,那么我们就可以得到如下结论:对任意的x∈X,y∈Y,有P(X=x, Y=y)=P(X=x)*P(Y=Y)。借用上面的例子,这一次变为有放回的抽取方式,再求第三问。由于第一次抽样的结果不会影响第二次抽样的时盒子中球的比例,因此可以将两个随机变量视为独立的,根据上述结论,可以求得P(X=红球,Y=红球)=(2/3)*(2/3)=4/9。现在使用传统的分析样本空间的方式来求这个问题,两次有放回抽样的所有可能情况为{红红,红红,红白,红红,红红,红白,白红,白红,白白},其中满足条件的样本点数来为四个,所以所求概率为4/9,这一结果与根据独立性所得出的结果是一致的。

(5)条件概率

  条件概率指的是在某一个事件发生的基础之上,另一个事件发生的概率。条件概率的符号表达为P(Y=y1|X=x1),可解释为在随机变量X取x1的情况下,Y取y1的概率。笔者以为条件概率其实是根据这一条件对总的样本空间取了一个子集,然后在这一子集中再讨论另外一个事件发生的概率。还是以上述取球的题目为例子,现在添加一个问题:在第一次取出红球的情况下,第二次取出红球的概率。本题所要求的概率为P(Y=红球|X=红球),总的样本空间为{红白,红红,红白,红红,白红,白红},根据条件“X=红球“从总体的样本空间中划分出一个满足条件的子集为{红白,红红,红白,红红},其中第二次为红球的样本数为2,因此P(Y=红球|X=红球)=1/2。求解条件概率还可以使用条件概率的定义,即:如果P(X=x)≠0的话,P(Y=y|X=x)=P(Y=y, X=x)/P(X=x),读者们可以尝试一下应用定义计算的结果也是1/2。

(6)全概率公式

   如果B1,B2,B3...是对样本空间S的一个划分,即B1,B2,B3...这些事件不为空,两两不相交,且B1∪B2∪B3∪...=S,那么对于事件A,有:

P(A)=P(A|B1)P(B1)+P(A|B2)P(B2)+P(A|B3)P(B3)...。

  这个式子被称为全概率公式。结合条件概率的定义,该公式还可以变为:P(A)=P(A,B1)+P(A,B2)+P(A,B3)...。现在我们用全概率公式来求取球问题中的第二问。第一次取球的随机变量可取的值为:X=红球 或者 白球,可以将第一次取球视为对样本空间的一种划分,那么P(Y=红球)=P(X=红球,Y=红球)+P(X=白球,Y=红球)。其中P(X=红球,Y=红球)的答案在第三问中已经解答过了,为1/3。样本空间为{红白,红红,红白,红红,白红,白红},而第一次取白球,第二次取红球的样本点只有两个,因此P(X=白球,Y=红球)=1/3,所以P(Y=红球)=2/3。

(6)贝叶斯法则

  贝叶斯法则在笔者看来,其实就是将条件概率与全概率公式组合在一起了。其定义如下:如果B1,B2,B3...是对样本空间S的一个划分,且P(A)>0,对任意的i,P(Bi)>0,则有P(Bi|A)=P(A|Bi)*P(Bi)/P(A)=P(A|Bi)*P(Bi)/∑P(A|Bi)。

三、最大似然估计

  最大似然估计是统计推断的一部分内容,主要目的为通过有限的样本来估计整体的分布情况。接下来,先给出一个应用最大似然估计的例子,然后再说明更普遍意义上的最大似然估计。假设现在有一枚不均匀的硬币,由于硬币是不均匀的,所以其正反面出现的概率是不一样的,现在将硬币抛掷10次,其结果为{正,正,正,负,负,正,正,正,负,正},现在需要求抛掷该硬币所出现结果的概率分布。现在以随机变量Xi来代表第i次抛掷硬币可能出现的结果,依据我们的经验可知,抛掷一枚硬币的可能的结果只有正面和反面,因此可以假设Xi服从贝努利分布,即: P(Xi=正面; θ)=θ;P(Xi=反面; θ)=1-θ,θ>0 且 θ<1,并且每一次抛掷都互不相关。设x={X1=正,X2=正,X3=正,X4=负,...,X10=正},根据我们所假设的分布,十次实验要得到我们现有结果的可能性为:P(x)=θ7(1-θ)3,这个函数也被称为似然函数,可记为L(θ|x)。最大似然估计所需要求的θ值就是使得似然函数最大的θ值。以θ为自变量,绘制L(θ|x)函数的图像,如下图所示。我们可以用微积分的知识来求得θ=argmax(L(θ|x))。似然函数在一阶导数为0的位置取得最值,似然函数对参数θ的导数为:7*θ6(1-θ)3-3*θ7(1-θ)2=0,解得θ=0.7。

 

  下面摘录一段对最大似然估计的概括性描述[3]:

  假设X1,X2,X3,...Xn是n个独立同分布(两两之间相互独立,并且都服从统一概率分布)的随机变量,所服从的概率分布为f(Xi;θ),i=1,2,...,n,以θ作为概率分布的参数。现在假设x1, x2,..., xn,是上述n个随机变量所取的值,设x=(x1, x2,..., xn)。似然函数为L(θ|x)=∏f(xi;θ),i=1,2,...,n,使得似然函数最大的θ被称之为θ的最大似然估计。

  对于所假设的分布是贝努利分布的最大似然估计而言,在计算最大似然估计的时候可以对似然函数取自然底数的对数,设t为n个样本中为正例数量,得到:In(L(θ|x))=t*In(θ)+(n-t)*In(1-θ),通过求导,可以得到一般结论即其最大似然估计为:θ=t/n。

四、最大后验估计

  在最大似然估计中,我们将概率分布的参数视为一个固定的常数值,然而在最大后验估计中,该参数也被视为一个随机变量。以最大似然估计中所说的硬币为例,不同于在最大似然估计中所假设的第i次抛掷的结果是随机变量Xi,在θ为随机变量的情况下,第i次抛掷的结果变为服从在θ取某一个值的条件下的条件概率分布。依旧假设该条件概率分布服从贝努利分布:P(Xi=正面|θ)=θ;P(Xi=反面|θ)=1-θ,θ>0 且 θ<1,并且假设每次抛掷的结果是以θ为条件相互独立的,设硬币为正面的概率θ服从分布:P(θ)。在现有样本所出现结果的情况下,θ取某个值的概率为:P(θ|x),使得这个概率最大的θ就是所需要求的。根据条件概率公式可得:P(θ|x)=P(x, θ)/P(x)=P(x|θ)*P(θ)/P(x),其中P(x)为抛掷结果在不考虑θ的情况下的先验概率。由于P(x)和θ无关,在计算最大后验估计的时候可以不考虑,因此所要求的结果为:θ=argmax P(x|θ)*P(θ)。现在假设P(θ)为μ为0.5,σ为0.1的正态分布,即:10/√2π*exp(-50(θ-0.5)2)。P(x|θ)*P(θ)=P(X1=正|θ)*P(X2=正|θ)*P(X3=正|θ)*...*P(X10=正|θ)*P(θ)=θ7(1-θ)3*10/√2π*exp(-50(θ-0.5)2),应用类似于最大似然估计中的求最值的方法,可以得到最终的θ的值。

五、朴素贝叶斯算法

  下面的内容会通过一个案例来介绍朴素贝叶斯算法在分类中的应用,该样例摘自参考文献4。

  现有如下样本:

 

  求样本{2,S}属于某一类别的概率?

  将特征1视作视为随机变量X1,取值范围为{1,2,3};将特征2视为随机变量X2,取值范围为{S,M,L};类别视为随机变量Y,取值范围为{-1,1}。根据题意,可知所需要求概率为P(Y=-1|X1=2,X2=S)及P(Y=1|X1=2,X2=S),下面我们来求第一个概率,第二个概率的求法与第一个是一样。

  根据条件概率公式,上述概率可转化为:P(Y=-1|X1=2,X2=S)= P(X1=2,X2=S|Y=-1)*P(Y=-1)/P(X1=2,X2=S)。在应用朴素贝叶斯方法的时候,有一个很重要的假设,那就是条件独立假设:在表示类别特征的随机变量取某个值的条件下,表示各个特征的随机变量是相互独立的。于是就有:

  P(Y=-1|X1=2,X2=S)= P(X1=2,X2=S|Y=-1)*P(Y=-1)/P(X1=2,X2=S)=P(X1=2|Y=-1)*P(X2=S|Y=-1)*P(Y=-1)/P(X1=2,X2=S)。(1)

  我们可以将类别作为条件,对总体的样本空间进行一次完备划分,根据全概率公式和条件独立假设,可将等式(1)中的分母转为:

  P(X1=2,X2=S)=P(X1=2,X2=S|Y=-1)*P(Y=-1)+P(X1=2,X2=S|Y=1)*P(Y=1)=P(X1=2|Y=-1)*P(X2=S|Y=-1)*P(Y=-1)+P(X1=2|Y=1)*P(X2=S|Y=1)*P(Y=1)。   (2)

  接下来我们只需要求出:P(X1=2|Y=-1),P(X2=S|Y=-1),P(Y=-1),P(Y=1),P(X1=2|Y=1),P(X2=S|Y=1)。

  直觉上,我们会通过计算现有样本中所有符合条件的样本所占的比例来计算上述六个概率值,虽然最后得到的结果是对的,但是李航老师在[4]中提到了使用最大似然估计来求解上面这四个概率的方法,具体读者可以参考该书所对应的的内容。笔者以为书上的结论,是基于P(X1|Y),P(X2|Y),P(Y)这三个分布是贝努利分布这一假设得来的。接下来直接使用该结论,计算上述四个概率值。

  P(X1=2|Y=-1)可理解为:当类别为-1的样本时,X1为2的可能性,结果为:P(X1=2|Y=-1)=2/6;同理,P(X1=2|Y=1)=3/9

  P(X2=S|Y=-1)可理解为:当类别为-1的样本时,X2为S的可能性,结果为:P(X2=S|Y=-1)=3/6;同理,P(X1=S|Y=1)=1/9

  P(Y=-1)可理解为:样本为-1类的概率,结果为:P(Y=-1)=6/15;

  P(Y=1)可理解为:样本为1类的概率,结果为:P(Y=1)=9/15;

  最终得到:P(Y=-1|X1=2,X2=S)=3/4。

  以上就是笔者对最大似然估计、最大后验概率及贝叶斯分类的一个简单总结。笔者水平有限,有错误的地方还请各位读者批评指正。

六、参考文献

[1]概率论与数理统计(第四版),浙江大学;

[2]https://baike.baidu.com/item/%E8%BF%9E%E7%BB%AD%E5%9E%8B%E9%9A%8F%E6%9C%BA%E5%8F%98%E9%87%8F

[3]《Introduction to Probability and Statistical Inference》 by George Roussas;

[4]《统计学习方法》,李航著。

Guess you like

Origin www.cnblogs.com/AlgrithmsRookie/p/11749113.html