Machine Learning Hamid Beigyce.sharif.edu/.../root/Slides/40717-5to8-handout.pdfWhen C >2, the...

91
Classification based on Bayes decision theory Machine Learning Hamid Beigy Sharif University of Technology Fall 1394 Hamid Beigy (Sharif University of Technology) Classification based on Bayes decision theory Fall 1394 1 / 70

Transcript of Machine Learning Hamid Beigyce.sharif.edu/.../root/Slides/40717-5to8-handout.pdfWhen C >2, the...

Page 1: Machine Learning Hamid Beigyce.sharif.edu/.../root/Slides/40717-5to8-handout.pdfWhen C >2, the problem is calledmulti-class classi cation. Hamid Beigy (Sharif University of Technology)

Classification based on Bayes decision theoryMachine Learning

Hamid Beigy

Sharif University of Technology

Fall 1394

Hamid Beigy (Sharif University of Technology) Classification based on Bayes decision theory Fall 1394 1 / 70

Page 2: Machine Learning Hamid Beigyce.sharif.edu/.../root/Slides/40717-5to8-handout.pdfWhen C >2, the problem is calledmulti-class classi cation. Hamid Beigy (Sharif University of Technology)

1 Introduction

2 Bayes decision theoryMinimizing the classification error probabilityMinimizing the average riskDiscriminant function and decision surfaceBayesian classifiers for Normally distributed classesMinimum distance classifierBayesian classifiers for independent binary features

3 Supervised learning of the Bayesian classifiersParametric methods for density estimation

Maximum likelihood parameter estimationMaximum a posteriori estimationBayesian estimationMixture models for density estimation

Nonparametric methods for density estimationHistogram estimatorNaive estimatorKernel estimatork−Nearest neighbor estimatork−Nearest neighbor classifier

4 Naive Bayes classifier

Hamid Beigy (Sharif University of Technology) Classification based on Bayes decision theory Fall 1394 2 / 70

Page 3: Machine Learning Hamid Beigyce.sharif.edu/.../root/Slides/40717-5to8-handout.pdfWhen C >2, the problem is calledmulti-class classi cation. Hamid Beigy (Sharif University of Technology)

Outline

1 Introduction

2 Bayes decision theoryMinimizing the classification error probabilityMinimizing the average riskDiscriminant function and decision surfaceBayesian classifiers for Normally distributed classesMinimum distance classifierBayesian classifiers for independent binary features

3 Supervised learning of the Bayesian classifiersParametric methods for density estimation

Maximum likelihood parameter estimationMaximum a posteriori estimationBayesian estimationMixture models for density estimation

Nonparametric methods for density estimationHistogram estimatorNaive estimatorKernel estimatork−Nearest neighbor estimatork−Nearest neighbor classifier

4 Naive Bayes classifier

Hamid Beigy (Sharif University of Technology) Classification based on Bayes decision theory Fall 1394 3 / 70

Page 4: Machine Learning Hamid Beigyce.sharif.edu/.../root/Slides/40717-5to8-handout.pdfWhen C >2, the problem is calledmulti-class classi cation. Hamid Beigy (Sharif University of Technology)

Introduction

In classification, the goal is to find a mapping from inputs X to outputs t given a labeledset of input-output pairs

S = {(x1, t1), (x2, t2), . . . , (xN , tN)}.

S is called training set.

In the simplest setting, each training input x is a D−dimensional vector of numbers.

Each component of x is called feature, attribute, or variable and x is called feature vector.

The goal is to find a mapping from inputs X to outputs t, where t ∈ {1, 2, . . . ,C} with Cbeing the number of classes.

When C = 2, the problem is called binary classification. In this case, we often assumethat t ∈ {−1,+1} or t ∈ {0, 1}.When C > 2, the problem is called multi-class classification.

Hamid Beigy (Sharif University of Technology) Classification based on Bayes decision theory Fall 1394 3 / 70

Page 5: Machine Learning Hamid Beigyce.sharif.edu/.../root/Slides/40717-5to8-handout.pdfWhen C >2, the problem is calledmulti-class classi cation. Hamid Beigy (Sharif University of Technology)

Introduction (cont.)

Bayes theorem

p(Ck |X ) =P(X |Ck)P(Ck)

P(X )

=P(X |Ck)P(Ck)∑Y p(X |Ck)p(Ck)

p(Ck) is called prior of Ck .

p(X |Ck) is called likelihood of data .

p(Ck |X ) is called posterior probability.

Since p(X ) is the same for all classes, we can write as

p(Ck |X ) = P(X |Ck)P(Ck)

Approaches for building a classifier.

Generative approach: This approach first creates a joint model of the form of p(x ,Ck) andthen to condition on x , deriving p(Ck |x).Discriminative approach: This approach creates a model of the form of p(Ck |x) directly.

Hamid Beigy (Sharif University of Technology) Classification based on Bayes decision theory Fall 1394 4 / 70

Page 6: Machine Learning Hamid Beigyce.sharif.edu/.../root/Slides/40717-5to8-handout.pdfWhen C >2, the problem is calledmulti-class classi cation. Hamid Beigy (Sharif University of Technology)

Outline

1 Introduction

2 Bayes decision theoryMinimizing the classification error probabilityMinimizing the average riskDiscriminant function and decision surfaceBayesian classifiers for Normally distributed classesMinimum distance classifierBayesian classifiers for independent binary features

3 Supervised learning of the Bayesian classifiersParametric methods for density estimation

Maximum likelihood parameter estimationMaximum a posteriori estimationBayesian estimationMixture models for density estimation

Nonparametric methods for density estimationHistogram estimatorNaive estimatorKernel estimatork−Nearest neighbor estimatork−Nearest neighbor classifier

4 Naive Bayes classifier

Hamid Beigy (Sharif University of Technology) Classification based on Bayes decision theory Fall 1394 5 / 70

Page 7: Machine Learning Hamid Beigyce.sharif.edu/.../root/Slides/40717-5to8-handout.pdfWhen C >2, the problem is calledmulti-class classi cation. Hamid Beigy (Sharif University of Technology)

Bayes decision theory

Given a classification task of M classes, C1,C2, . . . ,CM , and an input vector x , we canform M conditional probabilities

p(Ck |x) ∀k = 1, 2, . . . ,M

Without loss of generality, consider two class classification problem. From the Bayestheorem, we have

p(Ck |x) = P(x |Ck)P(Ck)

The base classification rule is

if p(C1|x) > p(C2|x) then x is classified to C1

if p(C1|x) < p(C2|x) then x is classified to C2

if p(C1|x) = p(C2|x) then x is classified to either C1 or C2

Since p(x) is same for all classes, then it can be removed. Hence

p(x |C1)p(C1) ≶ p(x |C2)p(C2)

If p(C1) = p(C2) = 12 , then we have

p(x |C1) ≶ p(x |C2)

Hamid Beigy (Sharif University of Technology) Classification based on Bayes decision theory Fall 1394 5 / 70

Page 8: Machine Learning Hamid Beigyce.sharif.edu/.../root/Slides/40717-5to8-handout.pdfWhen C >2, the problem is calledmulti-class classi cation. Hamid Beigy (Sharif University of Technology)

Bayes decision theory

If p(C1) = p(C2) = 12 , then we have

The coloured region may produce error. The probability of error equals to

Pe = p(mistake) = p(x ∈ R1,C2) + p(x ∈ R2,C1)

=1

2

∫R1

p(x |C2)dx +1

2

∫R2

p(x |C1)dx

Hamid Beigy (Sharif University of Technology) Classification based on Bayes decision theory Fall 1394 6 / 70

Page 9: Machine Learning Hamid Beigyce.sharif.edu/.../root/Slides/40717-5to8-handout.pdfWhen C >2, the problem is calledmulti-class classi cation. Hamid Beigy (Sharif University of Technology)

Outline

1 Introduction

2 Bayes decision theoryMinimizing the classification error probabilityMinimizing the average riskDiscriminant function and decision surfaceBayesian classifiers for Normally distributed classesMinimum distance classifierBayesian classifiers for independent binary features

3 Supervised learning of the Bayesian classifiersParametric methods for density estimation

Maximum likelihood parameter estimationMaximum a posteriori estimationBayesian estimationMixture models for density estimation

Nonparametric methods for density estimationHistogram estimatorNaive estimatorKernel estimatork−Nearest neighbor estimatork−Nearest neighbor classifier

4 Naive Bayes classifier

Page 10: Machine Learning Hamid Beigyce.sharif.edu/.../root/Slides/40717-5to8-handout.pdfWhen C >2, the problem is calledmulti-class classi cation. Hamid Beigy (Sharif University of Technology)

Minimizing the classification error probability (cont.)

We now show that the Bayesian classifier is optimal with respect to minimizing theclassification probability.Let R1(R2) be the region in the feature space in which we decide in favor of C1 (C2).Then error is made if x ∈ R1 although it belongs to C2, or if x ∈ R2 but it may belongsto C1. That is

Pe = p(x ∈ R2,C1) + p(x ∈ R1,C2)

= p(x ∈ R2|C1)p(C1) + p(x ∈ R1|C2)p(C2)

= p(C1)

∫R2

p(x ∈ R2|C1) + p(C2)

∫R1

p(x ∈ R1|C2)

=

∫R2

p(C1|x)p(x)dx +

∫R1

p(C2|x)p(x)dx

Since R1 ∪R2 covers all the feature space, from the definition of probability densityfunction, we have

p(C1) =

∫R1

p(C1|x)p(x)dx +

∫R2

p(C1|x)p(x)dx

By combining these two equation, we obtain

Pe = p(C1)−∫R1

[p(C1|x)− p(C2|x)] p(x)dx

Hamid Beigy (Sharif University of Technology) Classification based on Bayes decision theory Fall 1394 7 / 70

Page 11: Machine Learning Hamid Beigyce.sharif.edu/.../root/Slides/40717-5to8-handout.pdfWhen C >2, the problem is calledmulti-class classi cation. Hamid Beigy (Sharif University of Technology)

Minimizing the classification error probability (cont.)

The probability of error equals to

Pe = p(C1)−∫R1

[p(C1|x)− p(C2|x)] p(x)dx

The probability of error is minimized if R1 is the region of the space in which

[p(C1|x)− p(C2|x)] > 0

Then R2 becomes the region where the reverse is true, i.e. is the region of the space inwhich

[p(C1|x)− p(C2|x)] < 0

For classification task with M classes, x is assigned to class Ck with the following rule

if p(Ck |x) > p(Cj |x) ∀j 6= k

Show that this rule also minimizes the classification error probability for classification taskwith M classes.

Hamid Beigy (Sharif University of Technology) Classification based on Bayes decision theory Fall 1394 8 / 70

Page 12: Machine Learning Hamid Beigyce.sharif.edu/.../root/Slides/40717-5to8-handout.pdfWhen C >2, the problem is calledmulti-class classi cation. Hamid Beigy (Sharif University of Technology)

Outline

1 Introduction

2 Bayes decision theoryMinimizing the classification error probabilityMinimizing the average riskDiscriminant function and decision surfaceBayesian classifiers for Normally distributed classesMinimum distance classifierBayesian classifiers for independent binary features

3 Supervised learning of the Bayesian classifiersParametric methods for density estimation

Maximum likelihood parameter estimationMaximum a posteriori estimationBayesian estimationMixture models for density estimation

Nonparametric methods for density estimationHistogram estimatorNaive estimatorKernel estimatork−Nearest neighbor estimatork−Nearest neighbor classifier

4 Naive Bayes classifier

Page 13: Machine Learning Hamid Beigyce.sharif.edu/.../root/Slides/40717-5to8-handout.pdfWhen C >2, the problem is calledmulti-class classi cation. Hamid Beigy (Sharif University of Technology)

Minimizing the average risk

The classification error probability is not always the best criterion to be adopted forminimization. Why?

This because it assigns the same importance to all errors.

In some applications such as IDS, patient classification, and spam filtering some wrongdecisions may have more serious implications than others.

In some cases, it is more appropriate to assign a penalty term to weight each error.

In such case, we try to minimize the following risk.

r = λ12p(C1)

∫R2

p(x |C1)dx + λ21

∫R2

p(x |C1)dx

In general, the risk/loss associated to class Ck is defined as

rk =M∑i=1

λki

∫Ri

p(x |Ck)dx

The goal is to partition the feature space so that the average risk is minimized.

r =M∑k=1

rkp(Ck)

=M∑i=1

∫Ri

(M∑k=1

λkip(x |Ck)p(Ck)

)dx

Hamid Beigy (Sharif University of Technology) Classification based on Bayes decision theory Fall 1394 9 / 70

Page 14: Machine Learning Hamid Beigyce.sharif.edu/.../root/Slides/40717-5to8-handout.pdfWhen C >2, the problem is calledmulti-class classi cation. Hamid Beigy (Sharif University of Technology)

Minimizing the average risk (cont.)

The average risk is equal to

r =M∑i=1

∫Ri

(M∑k=1

λkip(x |Ck)p(Ck)

)dx

This is achieved if each integral is minimized, so that

x ∈ R if ri =M∑k=1

λkip(x |Ck)p(Ck) < rj =M∑k=1

λkjp(x |Ck)p(Ck) ∀j 6= i

When λki = 1 (for k 6= i), minimizing the average risk is equivalent to minimizing theclassification error probability.

In two–class case, we have

r1 = λ11p(x |C1)p(C1) + λ21p(x |C2)p(C2)

r2 = λ12p(x |C1)p(C1) + λ22p(x |C2)p(C2)

We assign x to C1 if r1 < r2, that is

(λ21 − λ22) p(x |C2)p(C2) < (λ12 − λ11) p(x |C1)p(C1)

Hamid Beigy (Sharif University of Technology) Classification based on Bayes decision theory Fall 1394 10 / 70

Page 15: Machine Learning Hamid Beigyce.sharif.edu/.../root/Slides/40717-5to8-handout.pdfWhen C >2, the problem is calledmulti-class classi cation. Hamid Beigy (Sharif University of Technology)

Minimizing the average risk (cont.)

In other words,

x ∈ C1(C2) ifp(x |C1)

p(x |C2)> (<)

p(C2)

p(C1)

λ21 − λ22

λ12 − λ11

Assume that the loss matrix is in the form of

Λ =

[0 λ12

λ21 0

].

Then, we have

x ∈ C1(C2) ifp(x |C1)

p(x |C2)> (<)

p(C2)

p(C1)

λ21

λ12

When p(C1) = p(C2) = 12 , we have

x ∈ C1(C2) if p(x |C1) > (<)p(x |C2)λ21

λ12

If λ21 > λ12, then x is assigned to C2 if

p(x |C2) > p(x |C1)λ12

λ21

That is, p(x |C1) is multiplied by a factor less than one and the effect is the movement ofthe threshold to left of x0.

Hamid Beigy (Sharif University of Technology) Classification based on Bayes decision theory Fall 1394 11 / 70

Page 16: Machine Learning Hamid Beigyce.sharif.edu/.../root/Slides/40717-5to8-handout.pdfWhen C >2, the problem is calledmulti-class classi cation. Hamid Beigy (Sharif University of Technology)

Minimizing the average risk (example)

In a two class problem with a single feature x , distributions of two classes are

p(x |C1) =1√πexp

(−x2

)p(x |C2) =

1√πexp

(−(x − 1)2

)The prior probabilities of two class are p(C1) = p(C2) = 1

2 .Compute x0 for minimum error probability classifier. x0 is the solution of

1√πexp

(−x2

0

)=

1√πexp

(−(x0 − 1)2

)x0 = 1

2 is the solution of the above equation.If the following loss matrix is given, compute x0 for the minimum average risk classifier.

Λ =

[0 0.5

1.0 0

].

x0 must satisfy the following equation.

1√πexp

(−x2

0

)=

2√πexp

(−(x0 − 1)2

)x0 = 1−ln 2

2 < 12 is the solution of the above equation.

Hamid Beigy (Sharif University of Technology) Classification based on Bayes decision theory Fall 1394 12 / 70

Page 17: Machine Learning Hamid Beigyce.sharif.edu/.../root/Slides/40717-5to8-handout.pdfWhen C >2, the problem is calledmulti-class classi cation. Hamid Beigy (Sharif University of Technology)

Outline

1 Introduction

2 Bayes decision theoryMinimizing the classification error probabilityMinimizing the average riskDiscriminant function and decision surfaceBayesian classifiers for Normally distributed classesMinimum distance classifierBayesian classifiers for independent binary features

3 Supervised learning of the Bayesian classifiersParametric methods for density estimation

Maximum likelihood parameter estimationMaximum a posteriori estimationBayesian estimationMixture models for density estimation

Nonparametric methods for density estimationHistogram estimatorNaive estimatorKernel estimatork−Nearest neighbor estimatork−Nearest neighbor classifier

4 Naive Bayes classifier

Page 18: Machine Learning Hamid Beigyce.sharif.edu/.../root/Slides/40717-5to8-handout.pdfWhen C >2, the problem is calledmulti-class classi cation. Hamid Beigy (Sharif University of Technology)

Discriminant function and decision surface

As discussed, minimizing either the risk or the error probability is equivalent topartitioning the feature space into M regions for M classes.

If two regions Ri and Rj happen to be continuous, then they are separated by a decisionsurface in the multi-dimensional feature space.

For the minimum error probability case, this surface described by equation

p(Ci |x)− p(Cj |x) = 0.

From the one side of the surface this difference is positive and from the other side, it isnegative.

Sometimes, instead of working directly with probabilities (or risks), it is more convenientto work with an equivalent function of them such as

gi (x) = f (p(Ci |x))

f (.) is a monotonically increasing function. (why?)

Function gi (x) is known as a discriminant function.

Now, the decision test is stated as

Classify x in Ci if gi (x) > gj(x) ∀j 6= i

The decision surfaces, separating continuous regions are stated as

gij(x) = gi (x)− gj(x) ∀i , j = 1, 2, . . . ,M, and j 6= i

Hamid Beigy (Sharif University of Technology) Classification based on Bayes decision theory Fall 1394 13 / 70

Page 19: Machine Learning Hamid Beigyce.sharif.edu/.../root/Slides/40717-5to8-handout.pdfWhen C >2, the problem is calledmulti-class classi cation. Hamid Beigy (Sharif University of Technology)

Outline

1 Introduction

2 Bayes decision theoryMinimizing the classification error probabilityMinimizing the average riskDiscriminant function and decision surfaceBayesian classifiers for Normally distributed classesMinimum distance classifierBayesian classifiers for independent binary features

3 Supervised learning of the Bayesian classifiersParametric methods for density estimation

Maximum likelihood parameter estimationMaximum a posteriori estimationBayesian estimationMixture models for density estimation

Nonparametric methods for density estimationHistogram estimatorNaive estimatorKernel estimatork−Nearest neighbor estimatork−Nearest neighbor classifier

4 Naive Bayes classifier

Page 20: Machine Learning Hamid Beigyce.sharif.edu/.../root/Slides/40717-5to8-handout.pdfWhen C >2, the problem is calledmulti-class classi cation. Hamid Beigy (Sharif University of Technology)

Discriminant function for Normally distributed classes

The one dimensional Gaussian distribution with mean of µ and variance σ2 is given by

p(x) = N (µ, σ2) =1

σ√

2πexp

(−(x − µ)2

2σ2

)The D−dimensional Gaussian distribution with mean of µ and covariance matrix Σ is

p(x) = N (µ,Σ) =1

|Σ|D/2(2π)D/2exp

(−1

2(x − µ)TΣ−1(x − µ)

)What is the optimal classifier when the involved pdfs are N (µ,Σ)?Because of the exponential form of the involved densities, it is preferable to work with thefollowing discriminant functions.

gi (x) = ln[p(x |ci )p(Ci )]

= ln p(x |ci ) + ln p(Ci )

Or

gi (x) = −1

2(x − µi )TΣ−1

i (x − µi ) + wi0

wi0 = −D

2ln(2π)− D

2ln |Σi |+ ln p(Ci )

By expanding the above equation, we obtain the following quadratic form.

gi (x) = −1

2xTΣ−1

i x +1

2xTΣ−1

i µi −1

2µTi Σ−1

i µi +1

2µTi Σ−1

i x + wi0

Hamid Beigy (Sharif University of Technology) Classification based on Bayes decision theory Fall 1394 14 / 70

Page 21: Machine Learning Hamid Beigyce.sharif.edu/.../root/Slides/40717-5to8-handout.pdfWhen C >2, the problem is calledmulti-class classi cation. Hamid Beigy (Sharif University of Technology)

Discriminant function for Normally distributed classes(example)

For Normally distributed classes, we have the following quadratic form classifier.

gi (x) = −1

2xTΣ−1

i x +1

2xTΣ−1

i µi −1

2µTi Σ−1

i µi +1

2µTi Σ−1

i x + wi0

Assume

Σi =

[σ2i 0

0 σ2i

]Thus we have

gi (x) = − 1

2σ2i

(x2

1 + x22

)+

1

2σ2i

(µi1x1 + µi2x2)− 1

2σ2i

(µ2i1 + µ2

i2

)+ wi0

Obviously the associated decision curves gi (x)− gj(x) = 0 are quadratics.

In this case the Bayesian classifier is a quadratic classifier, i.e. the partition of the featurespace is performed via quadratic decision surfaces.

Hamid Beigy (Sharif University of Technology) Classification based on Bayes decision theory Fall 1394 15 / 70

Page 22: Machine Learning Hamid Beigyce.sharif.edu/.../root/Slides/40717-5to8-handout.pdfWhen C >2, the problem is calledmulti-class classi cation. Hamid Beigy (Sharif University of Technology)

Discriminant function for Normally distributed classes (cont.)

The discriminant functions for optimal classifier when the involved pdfs are N (µ,Σ) havethe following form

gi (x) = −1

2(x − µi )TΣ−1

i (x − µi ) + wi0

wi0 = −1

2ln(2π)− 1

2ln |Σi |+ ln p(Ci )

By expanding the above equation, we obtain the following quadratic form.

gi (x) = −1

2xTΣ−1

i x +1

2xTΣ−1

i µi −1

2µTi Σ−1

i µi +1

2µTi Σ−1

i x + wi0

Based on the above equations, We distinguish three distinct cases:

When Σi = σ2I , where σ2 is a scalar and I is the identity matrix;Σi = Σ, i.e. all classes have equal covariance matrices;Σi is arbitrary.

Hamid Beigy (Sharif University of Technology) Classification based on Bayes decision theory Fall 1394 16 / 70

Page 23: Machine Learning Hamid Beigyce.sharif.edu/.../root/Slides/40717-5to8-handout.pdfWhen C >2, the problem is calledmulti-class classi cation. Hamid Beigy (Sharif University of Technology)

Discriminant function for Normally distributed classes (when Σi = σ2I )

The discriminant functions for optimal classifier when the involved pdfs are N (µ,Σ) havethe following form

gi (x) = −1

2(x − µi )TΣ−1

i (x − µi ) + wi0

By replacing Σi = σ2I in the above equation, we obtain

gi (x) = −1

2(x − µi )T (σ2)−1(x − µi ) + wi0

= −||x − µi ||2

2σ2+ wi0

= − 1

2σ2

(xT x − 2µTi x + µTi µi

)+ wi0

Terms xT x and other constants are equal for all classes so they can be dropped.

gi (x) =1

σ2

(µTi x −

1

2µTi µi

)+ wi0

This is a linear discriminant function with

wi =µiσ2

w ′i0 = −µTi µi2σ2

+ ln p(Ci )

Hamid Beigy (Sharif University of Technology) Classification based on Bayes decision theory Fall 1394 17 / 70

Page 24: Machine Learning Hamid Beigyce.sharif.edu/.../root/Slides/40717-5to8-handout.pdfWhen C >2, the problem is calledmulti-class classi cation. Hamid Beigy (Sharif University of Technology)

Discriminant function for Normally distributed classes (Σi = σ2I )(cont.)

For this case, the discriminant functions are equal to

gi (x) =1

σ2µTi x + wi0

The corresponding hyperplanes can be written as

gij(x) = gi (x)− gj(x) =1

σ2µTi x + wi0 −

1

σ2µTj x + wj0

=1

σ2(µi − µj)T x + wi0 − wj0

= wT (x − x0) = 0

w = µi − µj

x0 =1

2(µi + µj)− σ2 ln

p(Ci )

p(Cj)

µi − µj||µi − µj ||2

This implies that the decision surface is a hyperplane passing through the point x0.

For any x on the decision hyperplane, vector (x − x0) also lies on the hyperplane andhence (µi − µj) is orthogonal to the decision hyperplane.

Hamid Beigy (Sharif University of Technology) Classification based on Bayes decision theory Fall 1394 18 / 70

Page 25: Machine Learning Hamid Beigyce.sharif.edu/.../root/Slides/40717-5to8-handout.pdfWhen C >2, the problem is calledmulti-class classi cation. Hamid Beigy (Sharif University of Technology)

Discriminant function for Normally distributed classes( Σi = σ2I )(cont.)

When p(Ci ) = p(Cj), then x0 = 12 (µi + µj) and the hyperplane passes through the

average of µi and µj .When p(Ci ) < p(Cj), the hyperplane located closer to µi .When p(Ci ) > p(Cj), the hyperplane located closer to µj .

If σ2 is small with respect to ||µi − µj ||, the location of the hyperplane is insensitive tothe values of p(Ci ) and p(Cj).

Hamid Beigy (Sharif University of Technology) Classification based on Bayes decision theory Fall 1394 19 / 70

Page 26: Machine Learning Hamid Beigyce.sharif.edu/.../root/Slides/40717-5to8-handout.pdfWhen C >2, the problem is calledmulti-class classi cation. Hamid Beigy (Sharif University of Technology)

Discriminant function for Normally distributed classes (Σi = Σ)

The discriminant functions for optimal classifier when the involved pdfs are N (µ,Σ) havethe following form

gi (x) = −1

2(x − µi )TΣ−1

i (x − µi ) + wi0

By replacing Σi = Σ in the above equation, we obtain

gi (x) = −1

2(x − µi )TΣ−1(x − µi ) + wi0

= −1

2xTΣ−1x +

1

2xTΣ−1µi −

1

2µTi Σ−1µi +

1

2µTi Σ−1x + wi0

= −1

2xTΣ−1x + µTi Σ−1x − 1

2µTi Σ−1µi + wi0

Terms xT x and other constants are equal for all classes and can be dropped. This gives

gi (x) =1

2

(2µTi Σ−1x − µTi Σ−1µi

)+ ln p(Ci )

This is a linear discriminant function with

gi (x) = wTi x + w ′i0

With the following parameters

wi = µiΣ−1

w ′i0 = −1

2µTi Σ−1µi + ln p(Ci )

Hamid Beigy (Sharif University of Technology) Classification based on Bayes decision theory Fall 1394 20 / 70

Page 27: Machine Learning Hamid Beigyce.sharif.edu/.../root/Slides/40717-5to8-handout.pdfWhen C >2, the problem is calledmulti-class classi cation. Hamid Beigy (Sharif University of Technology)

Discriminant function for Normally distributed classes ( Σi = Σ) (Cont.)

For this case, the discriminant functions are equal to

gi (x) =1

2

(2µTi Σ−1x − µTi Σ−1µi

)+ ln p(Ci )

The corresponding hyperplanes can be written as

gij(x) = gi (x)− gj(x) = wT (x − x0) = 0

w = Σ−1 (µi − µj)

x0 =1

2(µi + µj)− ln

p(Ci )

p(Cj)

µi − µj||µi − µj ||2Σ−1

=1

2(µi + µj)− ln

p(Ci )

p(Cj)

µi − µj(µi − µj)TΣ−1(µi − µj)

The decision function is no longer orthogonal to vector (µi − µj) but to its lineartransformation Σ−1(µi − µj).

Hamid Beigy (Sharif University of Technology) Classification based on Bayes decision theory Fall 1394 21 / 70

Page 28: Machine Learning Hamid Beigyce.sharif.edu/.../root/Slides/40717-5to8-handout.pdfWhen C >2, the problem is calledmulti-class classi cation. Hamid Beigy (Sharif University of Technology)

Discriminant function for Normally distributed classes (arbitrary Σi)

The discriminant functions for optimal classifier when the involved pdfs are N (µ,Σ) havethe following form

gi (x) = −1

2(x − µi )TΣ−1

i (x − µi ) + ln p(Ci )−D

2ln(2π)− D

2ln |Σi |

The discriminant functions cannot be simplified much further. Only the constant termD2 ln(2π) can be dropped.Discriminant functions are not linear but quadratic.They have much more complicated decision regions than the linear classifiers of the twoprevious cases.Now, decision surfaces are also quadratic and the decision regions do not have to be evenconnected sets.

Hamid Beigy (Sharif University of Technology) Classification based on Bayes decision theory Fall 1394 22 / 70

Page 29: Machine Learning Hamid Beigyce.sharif.edu/.../root/Slides/40717-5to8-handout.pdfWhen C >2, the problem is calledmulti-class classi cation. Hamid Beigy (Sharif University of Technology)

Outline

1 Introduction

2 Bayes decision theoryMinimizing the classification error probabilityMinimizing the average riskDiscriminant function and decision surfaceBayesian classifiers for Normally distributed classesMinimum distance classifierBayesian classifiers for independent binary features

3 Supervised learning of the Bayesian classifiersParametric methods for density estimation

Maximum likelihood parameter estimationMaximum a posteriori estimationBayesian estimationMixture models for density estimation

Nonparametric methods for density estimationHistogram estimatorNaive estimatorKernel estimatork−Nearest neighbor estimatork−Nearest neighbor classifier

4 Naive Bayes classifier

Page 30: Machine Learning Hamid Beigyce.sharif.edu/.../root/Slides/40717-5to8-handout.pdfWhen C >2, the problem is calledmulti-class classi cation. Hamid Beigy (Sharif University of Technology)

Minimum distance classifier

Assume that we have p(Ci ) = p(Cj) with the same covariance matrix, then gi (x) equals to

gi (x) = −1

2(x − µi )TΣ−1(x − µi )

For diagonal covariance matrix (Σ = σ2I ), the maximum gi (x) implies minimumEuclidean distance.

dε = ||x − µi ||Feature vectors are assigned to classes according to their Euclidean distance from theirrespective mean points.For non-diagonal covariance matrix, the maximum gi (x) is equivalent to minimizingMahalanobis distance (Σ−1−norm).

dm = (x − µi )TΣ−1(x − µi )

Hamid Beigy (Sharif University of Technology) Classification based on Bayes decision theory Fall 1394 23 / 70

Page 31: Machine Learning Hamid Beigyce.sharif.edu/.../root/Slides/40717-5to8-handout.pdfWhen C >2, the problem is calledmulti-class classi cation. Hamid Beigy (Sharif University of Technology)

Outline

1 Introduction

2 Bayes decision theoryMinimizing the classification error probabilityMinimizing the average riskDiscriminant function and decision surfaceBayesian classifiers for Normally distributed classesMinimum distance classifierBayesian classifiers for independent binary features

3 Supervised learning of the Bayesian classifiersParametric methods for density estimation

Maximum likelihood parameter estimationMaximum a posteriori estimationBayesian estimationMixture models for density estimation

Nonparametric methods for density estimationHistogram estimatorNaive estimatorKernel estimatork−Nearest neighbor estimatork−Nearest neighbor classifier

4 Naive Bayes classifier

Page 32: Machine Learning Hamid Beigyce.sharif.edu/.../root/Slides/40717-5to8-handout.pdfWhen C >2, the problem is calledmulti-class classi cation. Hamid Beigy (Sharif University of Technology)

Bayesian classifiers for independent binary features

We consider the case where features are binary-valued and independent and the classconditional density for each feature is the Bernoulli distribution. This yields

p(x |Ci ) =D∏j=1

qxjij (1− qij)

(1−xj )

qij (for j = 1, 2, . . . ,D) are parameters for the class conditional density of the class Ci .The discriminant function is

gi (x) = ln p(Ci |x) = ln p(x |Ci )p(Ci ) = lnD∏j=1

qxjij (1− qij)

(1−xj ) p(Ci )

=D∑j=1

[xj ln qij + ln (1− qij)− xj ln (1− qij)] + ln p(Ci )

These are linear discriminant functions

gi (x) = W Ti x + wi0 =

D∑j=1

wijxj + wi0

wi0 = ln qij − ln(1− qij)

wij =D∑j=1

ln(1− qij) + ln p(Ci )

Hamid Beigy (Sharif University of Technology) Classification based on Bayes decision theory Fall 1394 24 / 70

Page 33: Machine Learning Hamid Beigyce.sharif.edu/.../root/Slides/40717-5to8-handout.pdfWhen C >2, the problem is calledmulti-class classi cation. Hamid Beigy (Sharif University of Technology)

Outline

1 Introduction

2 Bayes decision theoryMinimizing the classification error probabilityMinimizing the average riskDiscriminant function and decision surfaceBayesian classifiers for Normally distributed classesMinimum distance classifierBayesian classifiers for independent binary features

3 Supervised learning of the Bayesian classifiersParametric methods for density estimation

Maximum likelihood parameter estimationMaximum a posteriori estimationBayesian estimationMixture models for density estimation

Nonparametric methods for density estimationHistogram estimatorNaive estimatorKernel estimatork−Nearest neighbor estimatork−Nearest neighbor classifier

4 Naive Bayes classifier

Hamid Beigy (Sharif University of Technology) Classification based on Bayes decision theory Fall 1394 25 / 70

Page 34: Machine Learning Hamid Beigyce.sharif.edu/.../root/Slides/40717-5to8-handout.pdfWhen C >2, the problem is calledmulti-class classi cation. Hamid Beigy (Sharif University of Technology)

Supervised learning of the Bayesian classifiers

We assumed that the class conditional pdfs p(x |Ci ) and the prior probabilities p(Ci ) wereknown. In practice, this is never the case and we study supervised learning of classconditional pdfs.

For supervised learning we need training samples. In the training set there are featurevectors from each class and we re-arrange training samples based on their classes.

Si = {(xi1, ti1) , (xi2, ti2) , . . . , (xiNi, tiNi

)}

Ni is the number of training samples from the class Ci .

We assume that the training samples in the sets Si are occurrences of the independentrandom variables.

The training data may be collected in two distinct ways. These are meaningful when weneed to learn the prior probabilities.

In mixture sampling, a set of objects are randomly selected, their feature vectors extractedand then they hand-classified to the most appropriate classes. The prior probability of eachclass is estimated as

p(Ci ) =Ni

N

In separate sampling, the training data for each class is collected separately. In this case, theprior probabilities cannot be deduced and it is most reasonable to assume that they are known(If they are unknown, we usually assume that the prior probabilities for all classes are equal.)

Hamid Beigy (Sharif University of Technology) Classification based on Bayes decision theory Fall 1394 25 / 70

Page 35: Machine Learning Hamid Beigyce.sharif.edu/.../root/Slides/40717-5to8-handout.pdfWhen C >2, the problem is calledmulti-class classi cation. Hamid Beigy (Sharif University of Technology)

Supervised learning of the Bayesian classifiers (Cont.)

We assumed that the probability density functions are known. In most cases, theseprobability density functions are not known and the underlying pdf will be estimated fromthe available data.

There are various ways to estimate the probability density functions.

If we know the type of of the pdf, we can estimate the parameters of the pdf such as meanand variance from the available data. These methods are known as parametric methods.

In the estimative approach to parametric density estimation, we use an estimate of theparameter θj in the parametric density.

p(x |Cj) = p(x |θ̂j)

θ̂j is an estimate of the parameter θj based on the data samples.In the Bayesian/predictive approach, we assume that we don’t know the true value ofparameter θj . This approach treats θj as an unknown random variable.

In many cases, we may not have the information about the type of the pdf, but we mayknow certain statistical parameters such as the mean and the variance. These methodsare known as nonparametric methods.

Hamid Beigy (Sharif University of Technology) Classification based on Bayes decision theory Fall 1394 26 / 70

Page 36: Machine Learning Hamid Beigyce.sharif.edu/.../root/Slides/40717-5to8-handout.pdfWhen C >2, the problem is calledmulti-class classi cation. Hamid Beigy (Sharif University of Technology)

Outline

1 Introduction

2 Bayes decision theoryMinimizing the classification error probabilityMinimizing the average riskDiscriminant function and decision surfaceBayesian classifiers for Normally distributed classesMinimum distance classifierBayesian classifiers for independent binary features

3 Supervised learning of the Bayesian classifiersParametric methods for density estimation

Maximum likelihood parameter estimationMaximum a posteriori estimationBayesian estimationMixture models for density estimation

Nonparametric methods for density estimationHistogram estimatorNaive estimatorKernel estimatork−Nearest neighbor estimatork−Nearest neighbor classifier

4 Naive Bayes classifier

Page 37: Machine Learning Hamid Beigyce.sharif.edu/.../root/Slides/40717-5to8-handout.pdfWhen C >2, the problem is calledmulti-class classi cation. Hamid Beigy (Sharif University of Technology)

Parametric methods for density estimation

In parametric methods, we assume that the sample is drawn from some knowndistribution (for example Gaussian). But the parameters of this distribution is not knownand our goal is to estimate these parameters from the data.

The main advantage of the parametric methods is the model is defined up to a smallnumber of parameters and when these parameters are estimated, the whole distribution isknown.

The following methods usually are used to estimate the parameters of the distribution

Maximum likelihood estimationBayesian estimationMaximum a posteriori probability estimationMaximum entropy estimationMixture Models

Hamid Beigy (Sharif University of Technology) Classification based on Bayes decision theory Fall 1394 27 / 70

Page 38: Machine Learning Hamid Beigyce.sharif.edu/.../root/Slides/40717-5to8-handout.pdfWhen C >2, the problem is calledmulti-class classi cation. Hamid Beigy (Sharif University of Technology)

Outline

1 Introduction

2 Bayes decision theoryMinimizing the classification error probabilityMinimizing the average riskDiscriminant function and decision surfaceBayesian classifiers for Normally distributed classesMinimum distance classifierBayesian classifiers for independent binary features

3 Supervised learning of the Bayesian classifiersParametric methods for density estimation

Maximum likelihood parameter estimationMaximum a posteriori estimationBayesian estimationMixture models for density estimation

Nonparametric methods for density estimationHistogram estimatorNaive estimatorKernel estimatork−Nearest neighbor estimatork−Nearest neighbor classifier

4 Naive Bayes classifier

Page 39: Machine Learning Hamid Beigyce.sharif.edu/.../root/Slides/40717-5to8-handout.pdfWhen C >2, the problem is calledmulti-class classi cation. Hamid Beigy (Sharif University of Technology)

Maximum likelihood parameter estimation

Consider an M−class problem with feature vectors distributed according to p(x |Ci ) (fori = 1, 2, . . . ,M).

We assume that p(x |Ci ) belongs to some family of parametric distributions. For example,we assume that p(x |Ci ) is a normal density with unknown parameters θi = (µi ,Σi ).

To show the dependence on θi , we denote p(x |Ci ) = p(x |Ci ; θi ). The class Ci defines theparametric family, and the parameter vector θi defines the member of that parametricfamily.

The parametric families do not need to be same for all classes.

Our goal is to estimate the unknown parameters using a set of known feature vectors ineach class.

If we assume that data from one class do not affect the parameter estimation of theothers,we can formulate the problem independent of classes and simplify our notation(p(x ; θ)). Then solve the problem for each class independently.

Let X = {x1, x2, . . . , xN} be random samples drawn from pdf p(x ; θ). We form the jointpdf p(X ; θ).

Assuming statistical independence between the different samples, we have

p(X ; θ) = p(x1, x2, . . . , xN ; θi ) =N∏

k=1

p(xk ; θ)

Hamid Beigy (Sharif University of Technology) Classification based on Bayes decision theory Fall 1394 28 / 70

Page 40: Machine Learning Hamid Beigyce.sharif.edu/.../root/Slides/40717-5to8-handout.pdfWhen C >2, the problem is calledmulti-class classi cation. Hamid Beigy (Sharif University of Technology)

Maximum likelihood parameter estimation (cont.)

p(X ; θ) is a function of θ and is known as likelihood function.

The maximum likelihood (ML) method estimates θ so that the likelihood function takesits maximum value, that is,

θ̂ML = argmaxθ

N∏k=1

p(xk ; θ)

A necessary condition that θ̂ML must satisfy in order to be a maximum is the gradient ofthe likelihood function with respect to θ to be zero.

∂∏N

k=1 p(xk ; θ)

∂θ= 0

It is more convenient to work with the logarithm of the likelihood function than with thelikelihood function itself. Hence, we define the log likelihood function as

LL(θ) = lnN∏

k=1

p(xk ; θ)

=N∑

k=1

ln p(xk ; θ)

Hamid Beigy (Sharif University of Technology) Classification based on Bayes decision theory Fall 1394 29 / 70

Page 41: Machine Learning Hamid Beigyce.sharif.edu/.../root/Slides/40717-5to8-handout.pdfWhen C >2, the problem is calledmulti-class classi cation. Hamid Beigy (Sharif University of Technology)

Maximum likelihood parameter estimation (cont.)

In order to find θ̂ML, it must satisfy

∂LL(θ)

∂θ=

∑Nk=1 ∂ ln p(xk ; θ)

∂θ

=N∑

k=1

1

p(xk ; θ)

∂p(xk ; θ)

∂θ

= 0

The single unknown parameter case

Hamid Beigy (Sharif University of Technology) Classification based on Bayes decision theory Fall 1394 30 / 70

Page 42: Machine Learning Hamid Beigyce.sharif.edu/.../root/Slides/40717-5to8-handout.pdfWhen C >2, the problem is calledmulti-class classi cation. Hamid Beigy (Sharif University of Technology)

Maximum likelihood estimation for normal distribution

Let x1, x2, . . . , xN be vectors sampled from a normal distribution with known covariancematrix and unknown mean, that is,

p(x ;µ) = N (µ,Σ) =1

|Σ|D/2(2π)D/2exp

(−1

2(x − µ)TΣ−1(x − µ)

)Obtain ML-estimate of the unknown mean vector.

For N available samples, we have

LL(µ) = lnN∏

k=1

p(xk ;µ) = −N

2ln[(2π)D |Σ|]− 1

2

N∑k=1

(xk − µ)TΣ−1(xk − µ)

Taking the gradient with respect to µ, we obtain

∂LL(µ)

∂µ=

∂LL(µ)∂µ1

∂LL(µ)∂µ2

...∂LL(µ)∂µD

=N∑

k=1

Σ−1(xk − µ) = 0

or

µ̂ML =1

N

N∑k=1

xk

That is, the ML estimate of the mean, for Gaussian densities, is the sample average.

Hamid Beigy (Sharif University of Technology) Classification based on Bayes decision theory Fall 1394 31 / 70

Page 43: Machine Learning Hamid Beigyce.sharif.edu/.../root/Slides/40717-5to8-handout.pdfWhen C >2, the problem is calledmulti-class classi cation. Hamid Beigy (Sharif University of Technology)

Maximum likelihood estimation for normal distribution

Assume x1, x2, . . . , xN have been generated by a one-dimensional Gaussian pdf of knownmean, µ, but of unknown variance, that is,

p(x ;σ2) =1

σ√

2πexp

(−(x − µ)2

2σ2

)Obtain ML-estimate of the unknown variance.

For N available samples, we have

LL(σ2) = lnN∏

k=1

p(xk ;σ2) = −N

2ln(2πσ2)− 1

2σ2

N∑k=1

(xk − µ)2

Taking the derivative of the above with respect to σ2 and equating to zero, we obtain

dLL(σ2)

dσ2= − N

2σ2+

1

2σ4

N∑k=1

(xk − µ)2 = 0

Solving the above equation with respect to σ2, results in

σ̂2ML =

1

N

N∑k=1

(xk − µ)2

Hamid Beigy (Sharif University of Technology) Classification based on Bayes decision theory Fall 1394 32 / 70

Page 44: Machine Learning Hamid Beigyce.sharif.edu/.../root/Slides/40717-5to8-handout.pdfWhen C >2, the problem is calledmulti-class classi cation. Hamid Beigy (Sharif University of Technology)

Evaluating an estimator

Let x be a sample from a pdf with parameter θ, and θ̂ be an estimator of θ.

To evaluate the quality of this estimator, we can measure how much it is different from θ,that is (θ̂ − θ)2.

But since it is a random variable (it depends on the sample), we need to average meansquare error this over possible x and consider r(θ̂, θ), the mean square error of theestimator θ̂ defined as

r(θ̂, θ) = E [(θ̂ − θ)2]

The bias of an estimator is given as

biasθ(θ̂) = E [θ̂]− θ

If biasθ(θ̂) = 0 for all values of θ, then we say that θ̂ is an unbiased estimator.

Hamid Beigy (Sharif University of Technology) Classification based on Bayes decision theory Fall 1394 33 / 70

Page 45: Machine Learning Hamid Beigyce.sharif.edu/.../root/Slides/40717-5to8-handout.pdfWhen C >2, the problem is calledmulti-class classi cation. Hamid Beigy (Sharif University of Technology)

Evaluating an estimator: bias and variance (cont.)

If biasθ(θ̂) = 0 for all values of θ, then we say that θ̂ is an unbiased estimator.

Example (Sample average)

Assume that N samples xk are drawn from some density with mean µ, the sample average, µ̂,is an unbiased estimator of the mean, µ, because

E [µ̂] = E

[∑k xkN

]=

1

N

∑k

E [xk ] =Nµ

N= µ

An estimator θ̂ is consistent estimator if

limN→∞

Var(θ̂)→ 0

Example (Sample average)

Assume that N samples xk are drawn from some density with mean µ, the sample average, µ̂,is a consistent estimator of the mean, µ, because

Var(µ̂) = Var

(∑k xkN

)=

1

N2

∑k

Var [xk ] =Nσ2

N2=σ2

N

As N gets larger, µ̂ deviates less from µ.

Hamid Beigy (Sharif University of Technology) Classification based on Bayes decision theory Fall 1394 34 / 70

Page 46: Machine Learning Hamid Beigyce.sharif.edu/.../root/Slides/40717-5to8-handout.pdfWhen C >2, the problem is calledmulti-class classi cation. Hamid Beigy (Sharif University of Technology)

Evaluating an estimator (cont.)

Example (Sample variance)

Assume that N samples xk are drawn from some density with variance σ2, the samplevariance, σ̂2, is a biased estimator of the variance, σ2, because

σ̂2 =

∑k(xk − µ̂)2

N=

∑k x

2k − Nµ̂2

N

E [σ̂2] =

∑k E [x2

k ]− NE [µ̂2]

N

Given that E [x2] = Var(x) + E [x ]2, we can write

E [x2k ] = σ2 + µ2

E [µ̂2] =σ2

N+ µ2

Replacing back, we obtain

E [σ̂2] =N(σ2 + µ2) + N(σ2/N + µ2)

N=

(N − 1

N

)σ2 6= σ2

This is an example of an asymptotically unbiased estimator whose bias goes to 0 as N goes to∞.Hamid Beigy (Sharif University of Technology) Classification based on Bayes decision theory Fall 1394 35 / 70

Page 47: Machine Learning Hamid Beigyce.sharif.edu/.../root/Slides/40717-5to8-handout.pdfWhen C >2, the problem is calledmulti-class classi cation. Hamid Beigy (Sharif University of Technology)

Properties of maximum likelihood estimation

If θ0 is the true value of the unknown parameter in p(x ; θ), it can be shown that undergenerally valid conditions the following are true

The ML estimate is asymptotically unbiased, that is

limN→∞

E [θ̂ML] = θ0

The ML estimate is asymptotically consistent, that is, it satisfies

limN→∞

Prob[||θ̂ML − θ0||2 ≤ ε

]= 1

for arbitrarily small ε. A stronger condition for consistency is also true

limN→∞

E[||θ̂ML − θ0||2

]= 0

The ML estimate is asymptotically efficient; that is, this is the lowest value of variance,which any estimate can achieve (Cramer-Rao lower bound).The pdf of the ML estimate as N →∞ approaches the Gaussian distribution with mean θ0.

In summary, the ML estimator is unbiased, is normally distributed, and has the minimumpossible variance. However, all properties are valid only for large values of N.

If N is small, little can be said about the ML-estimates in general.

Hamid Beigy (Sharif University of Technology) Classification based on Bayes decision theory Fall 1394 36 / 70

Page 48: Machine Learning Hamid Beigyce.sharif.edu/.../root/Slides/40717-5to8-handout.pdfWhen C >2, the problem is calledmulti-class classi cation. Hamid Beigy (Sharif University of Technology)

Outline

1 Introduction

2 Bayes decision theoryMinimizing the classification error probabilityMinimizing the average riskDiscriminant function and decision surfaceBayesian classifiers for Normally distributed classesMinimum distance classifierBayesian classifiers for independent binary features

3 Supervised learning of the Bayesian classifiersParametric methods for density estimation

Maximum likelihood parameter estimationMaximum a posteriori estimationBayesian estimationMixture models for density estimation

Nonparametric methods for density estimationHistogram estimatorNaive estimatorKernel estimatork−Nearest neighbor estimatork−Nearest neighbor classifier

4 Naive Bayes classifier

Page 49: Machine Learning Hamid Beigyce.sharif.edu/.../root/Slides/40717-5to8-handout.pdfWhen C >2, the problem is calledmulti-class classi cation. Hamid Beigy (Sharif University of Technology)

Maximum a posteriori estimation

In the maximum likelihood estimate, we considered θ as an unknown parameter.

In maximum a posteriori estimation, we consider θ as a random vector, and we willestimate its value based on sample X .

From the Bayes theorem, we have

p(θ|X ) =p(X |θ)p(θ)

p(X )

The maximum a posteriori estimation (MAP) θ̂MAP is defined at the point where p(θ|X )becomes maximum.

A necessary condition that θ̂MAP must satisfy in order to be a maximum is its gradientwith respect to θ to be zero.

∂p(θ|X )

∂θ= 0

or

∂p(X |θ)p(θ)

∂θ= 0

The difference between ML and MAP estimates lies in the involvement of p(θ) in theMAP.

If p(θ) is uniform, then both estimates yield identical results.

Hamid Beigy (Sharif University of Technology) Classification based on Bayes decision theory Fall 1394 37 / 70

Page 50: Machine Learning Hamid Beigyce.sharif.edu/.../root/Slides/40717-5to8-handout.pdfWhen C >2, the problem is calledmulti-class classi cation. Hamid Beigy (Sharif University of Technology)

Maximum a posteriori estimation (example)

Let x1, x2, . . . , xN be vectors drawn from a normal distribution with known covariancematrix and unknown mean, that is

p(xk ;µ) =1

(2π)D/2|Σ|D/2exp

(−1

2(xk − µ)Tσ−1(xk − µ)

)Assume that the unknown mean vector µ is known to be normally distributed as

p(µ) =1

(2π)D/2σDµexp

(−1

2

||µ− µ0||2

σ2µ

)The MAP estimate is given by the solution of

∂µln

(N∏

k=1

p(xk |µ)p(µ)

)= 0

For Σ = σ2I , we obtain

µ̂MAP =µ0 +

σ2µ

σ2

∑Nk=1 xk

1 +σ2µ

σ2 N

Whenσ2µ

σ2 � 1, then µ̂MAP ≈ µ̂ML.

Hamid Beigy (Sharif University of Technology) Classification based on Bayes decision theory Fall 1394 38 / 70

Page 51: Machine Learning Hamid Beigyce.sharif.edu/.../root/Slides/40717-5to8-handout.pdfWhen C >2, the problem is calledmulti-class classi cation. Hamid Beigy (Sharif University of Technology)

Outline

1 Introduction

2 Bayes decision theoryMinimizing the classification error probabilityMinimizing the average riskDiscriminant function and decision surfaceBayesian classifiers for Normally distributed classesMinimum distance classifierBayesian classifiers for independent binary features

3 Supervised learning of the Bayesian classifiersParametric methods for density estimation

Maximum likelihood parameter estimationMaximum a posteriori estimationBayesian estimationMixture models for density estimation

Nonparametric methods for density estimationHistogram estimatorNaive estimatorKernel estimatork−Nearest neighbor estimatork−Nearest neighbor classifier

4 Naive Bayes classifier

Page 52: Machine Learning Hamid Beigyce.sharif.edu/.../root/Slides/40717-5to8-handout.pdfWhen C >2, the problem is calledmulti-class classi cation. Hamid Beigy (Sharif University of Technology)

Bayesian estimation

Both MLE and MAP compute a specific estimate of the unknown parameter vector θ.

Sometimes, before looking at a sample, we (or experts of the application) may have someprior information on the possible value range that a parameter, θ, may take. Thisinformation is quite useful and should be used, especially when the sample is small.

The prior information does not tell us exactly what the parameter value is (otherwise wewould not need the sample), and we model this uncertainty by viewing θ as a randomvariable and by defining a prior density for it, p(θ).

For example, we are told that θ is approximately normal and with 90 percent confidence,θ lies between 5 and 9, symmetrically around 7.

The prior density p(θ) tells the likely values that θ may take before looking at the sample.

This is combined with what the sample data tells (p(X |θ)) using Bayes rule and get theposterior density of θ, which tells the θ values after looking at the sample.

p(θ|X ) =p(X |θ)p(θ)

p(X )

=p(X |θ)p(θ)∫

p(X |θ′)p(θ′)dθ′

Given the set of X and the prior information p(θ), the goal is to compute the conditionalpdf p(x |X ).

Hamid Beigy (Sharif University of Technology) Classification based on Bayes decision theory Fall 1394 39 / 70

Page 53: Machine Learning Hamid Beigyce.sharif.edu/.../root/Slides/40717-5to8-handout.pdfWhen C >2, the problem is calledmulti-class classi cation. Hamid Beigy (Sharif University of Technology)

Bayesian estimation (cont.)

For estimating the density at x , we have

p(x |X ) =

∫p(x , θ|X )dθ

=

∫p(x |θ,X )p(θ|X )dθ

=

∫p(x |θ)p(θ|X )dθ

p(x |θ,X ) = p(x |θ), because once we know θ, the sufficient statics, we know everythingabout the distribution.

Evaluating the integrals may be quite difficult, except in case where the posterior has anice form.

When the full integration is not feasible, we reduce it to a single point.

If we can assume that p(θ|X ) has a narrow peak around its mode, then using maximumposteriori (MAP) estimate will make the calculation easier.

If p(θ|X ) is known, then p(x |X ) is average of p(x |θ) with respect to θ, that is

p(x |X ) = Eθ[p(x |θ)]

Hamid Beigy (Sharif University of Technology) Classification based on Bayes decision theory Fall 1394 40 / 70

Page 54: Machine Learning Hamid Beigyce.sharif.edu/.../root/Slides/40717-5to8-handout.pdfWhen C >2, the problem is calledmulti-class classi cation. Hamid Beigy (Sharif University of Technology)

Bayesian estimation (example)

Let p(x |µ) be a univariate Gaussian N (µ, σ2) with unknown parameter mean, which isalso assumed to follow a Gaussian N (µ0, σ

20). From the previous slide, we have

p(µ|X ) =p(X |µ)p(µ)

p(X )=

1

α

N∏k=1

p(xk |µ)p(µ)

p(X ) is a constant denoted as α, or

p(µ|X ) =1

α

N∏k=1

1√2πσ

exp

(−(xk − µ)2

2σ2

)1√

2πσ0

exp

(−(µ− µ0)2

2σ20

)When N samples are given, p(µ|X ) turns out to be a Gaussian (show it), that is

p(µ|X ) =1√

2πσNexp

(−1

2

(µ− µN)2

σ2N

)µN =

Nσ20 x̄N + σ2µ0

Nσ20 + σ2

σ2N =

σ2σ20

Nσ20 + σ2

By some algebraic simplification, we obtain the following Gaussian pdf

p(x |X ) =1√

2π(σ2 + σ2N)

exp

(−1

2

(x − µN)2

σ2 + σ2N

)Hamid Beigy (Sharif University of Technology) Classification based on Bayes decision theory Fall 1394 41 / 70

Page 55: Machine Learning Hamid Beigyce.sharif.edu/.../root/Slides/40717-5to8-handout.pdfWhen C >2, the problem is calledmulti-class classi cation. Hamid Beigy (Sharif University of Technology)

Outline

1 Introduction

2 Bayes decision theoryMinimizing the classification error probabilityMinimizing the average riskDiscriminant function and decision surfaceBayesian classifiers for Normally distributed classesMinimum distance classifierBayesian classifiers for independent binary features

3 Supervised learning of the Bayesian classifiersParametric methods for density estimation

Maximum likelihood parameter estimationMaximum a posteriori estimationBayesian estimationMixture models for density estimation

Nonparametric methods for density estimationHistogram estimatorNaive estimatorKernel estimatork−Nearest neighbor estimatork−Nearest neighbor classifier

4 Naive Bayes classifier

Page 56: Machine Learning Hamid Beigyce.sharif.edu/.../root/Slides/40717-5to8-handout.pdfWhen C >2, the problem is calledmulti-class classi cation. Hamid Beigy (Sharif University of Technology)

Mixture models for density estimation

An alternative way to model an unknown density function p(x) is via linear combinationof M density functions in the form of

p(x) =M∑

m=1

πmp(x |m)

where

M∑m=1

πm = 1∫xp(x |m)dx = 1

This modeling implicitly assumes that each point x may be drawn from any M modeldistributions with probability πm (for m = 1, 2, . . . ,M).

It can be shown that this modeling can approximate closely any continuous densityfunction for a sufficient number mixtures M and appropriate model parameters.

Hamid Beigy (Sharif University of Technology) Classification based on Bayes decision theory Fall 1394 42 / 70

Page 57: Machine Learning Hamid Beigyce.sharif.edu/.../root/Slides/40717-5to8-handout.pdfWhen C >2, the problem is calledmulti-class classi cation. Hamid Beigy (Sharif University of Technology)

Mixture models(cont.)

The first step of the procedure involves the choice of the set of density componentsp(x |m) in the parametric form p(x |m, θ).Thus we have

p(x ; θ) =M∑

m=1

πmp(x |θm)

The second step is the computation of the unknown parameters θ and π1, π2, . . . , πMbased on the set of available training data.The parameter set is defined as θ = {π1, π2, . . . , πM, θ1, θ2, . . . , θM} and

∑i π1 = 1.

Given data X = {x1, x2, . . . , xN}, and assuming mixture model

p(x ; θ) =M∑

m=1

πmp(x |θm)

we want to estimate parameters.In order to estimate each πm, we can count how many points from X coming from eachof M components then normalize by N.

π̂m =Nm

N

Each Nm can be obtained from Nm =∑N

n=1 zmn and

zmn =

{1 if the nth point was drawn from component m0 otherwise.

Hamid Beigy (Sharif University of Technology) Classification based on Bayes decision theory Fall 1394 43 / 70

Page 58: Machine Learning Hamid Beigyce.sharif.edu/.../root/Slides/40717-5to8-handout.pdfWhen C >2, the problem is calledmulti-class classi cation. Hamid Beigy (Sharif University of Technology)

Mixture models(cont.)

What of the specific parameters of each of the components θ̂m?

We need to obtain the estimates θ̂m which maximize the likelihood of the data pointswhich were drawn from component m under the parametric form p(x |θm).

If the mixture components were Gaussian, then the Maximum-Likelihood estimate for thecomponent mean vectors would be

µ̂m =

∑Nn=1 zmnxn∑Nn=1 zmn

The estimation for covariance matrix for each component would be

Σ̂m =1∑N

n=1 zmn

N∑n=1

zmn(xn − µ̂m)(xn − µ̂m)T

The difficulty is that we do not know zmn. This is a major difficulty because the variableszmn are hidden or latent then our ML estimates cannot follow in the straightforwardmanner we had anticipated.

The problem is that we assumed knowledge of the values for indicator variables zmn.

Hamid Beigy (Sharif University of Technology) Classification based on Bayes decision theory Fall 1394 44 / 70

Page 59: Machine Learning Hamid Beigyce.sharif.edu/.../root/Slides/40717-5to8-handout.pdfWhen C >2, the problem is calledmulti-class classi cation. Hamid Beigy (Sharif University of Technology)

Mixture models(cont.)

We need the joint likelihood of data X = {x1, x2, . . . , xN} and indicator variablesZ = {z1, z2, . . . , zM} where each zm = {zm1, zm2, . . . , zMN}.Given θ = {θ1, θ2, . . . , θM}, we can marginalize over all possible component allocations.

p(X |θ) =∑Z

p(X ,Z |θ)

The summation is over all possible values which Z may take on.Then log p(X |θ) equals to

log p(X |θ) = log∑Z

p(X ,Z |θ)

= log∑Z

p(Z |X )p(X ,Z |θ)

p(Z |X )

Using inequality log E [x ] ≥ E [log x ], we can write

log∑Z

p(Z |X )p(X ,Z |θ)

p(Z |X )≥

∑Z

p(Z |X ) logp(X ,Z |θ)

p(Z |X )

≥∑Z

p(Z |X ) log p(X ,Z |θ)

−∑Z

p(Z |X ) log p(Z |X )

Hamid Beigy (Sharif University of Technology) Classification based on Bayes decision theory Fall 1394 45 / 70

Page 60: Machine Learning Hamid Beigyce.sharif.edu/.../root/Slides/40717-5to8-handout.pdfWhen C >2, the problem is calledmulti-class classi cation. Hamid Beigy (Sharif University of Technology)

Mixture models(cont.)

Since xn drawn i.i.d. from m distributions exclusively, then summation over all Z equalsto a summation over all n and m i.e. log–likelihood (LL) equals (drive it.)

∑Z

p(Z |X ) logp(X ,Z |θ)

p(Z |X )=

M,N∑m,n

p(m|xn) logp(xn|θm)p(m)

p(m|xn)

=M∑

m=1

N∑n=1

p(m|xn) log p(xn|θm)p(m)

−M∑

m=1

N∑n=1

p(m|xn) log p(m|xn)

p(m) is the probability that zmn = 1 for any n.

The Expectation Maximization (EM) algorithm is a general purpose method to maximizethe likelihood of the complete data (X&Z ) so as to obtain estimates of the componentparameters θm.

Before performing the Maximization step we require to obtain the Expected values of aset of latent variables zmn.

Once we have obtained the Expected values of the latent variables we then perform theMaximization step to obtain our current parameter estimates.

Hamid Beigy (Sharif University of Technology) Classification based on Bayes decision theory Fall 1394 46 / 70

Page 61: Machine Learning Hamid Beigyce.sharif.edu/.../root/Slides/40717-5to8-handout.pdfWhen C >2, the problem is calledmulti-class classi cation. Hamid Beigy (Sharif University of Technology)

Mixture models(cont.)

This EM interleaving is continued until some convergence criterion is achieved.

Taking derivatives of the LL with respect to p(m|xn) then

∂LL

∂p(m|xn)= log p(m|xn)− log p(xn|θm)p(m)− 1

Setting to zero we see that p(m|xn) ∝ p(xn|θm)p(m) and then normalizing appropriatelyyields the distribution of the form

p(m|xn) =p(xn|θm)p(m)∑M

m′=1 p(xn|θm′)p(m′)

You should now be able to see that this is the posterior distribution over the mixturecomponents m which generated xn, or the expected value of the binary variable zmn.

We have maximized the bound with respect to the Expected value of the indicatorvariable we need to Maximize the bound with respect to the parameter values.

The only terms in LL which are dependent on the component parameters are

M∑m=1

N∑n=1

p(m|xn) log p(xn|θm)p(m).

We maximize the above with respect to each θm.

Hamid Beigy (Sharif University of Technology) Classification based on Bayes decision theory Fall 1394 47 / 70

Page 62: Machine Learning Hamid Beigyce.sharif.edu/.../root/Slides/40717-5to8-handout.pdfWhen C >2, the problem is calledmulti-class classi cation. Hamid Beigy (Sharif University of Technology)

Expectation maximization (cont.)

Assume that each p(xn|θm) is a multivariate Gaussian, then expanding and retaining theelements dependent on the parameters we obtain

∂L

∂p(m|xn)= −1

2

M∑m=1

N∑n=1

p(m|xn) ln |Σm|

− 1

2

M∑m=1

N∑n=1

p(m|xn)(xn − µm)TΣ−1m (xn − µm)

+1

2

M∑m=1

N∑n=1

p(m|xn) log p(m)

Taking Taking derivatives with respect to µm and solving them, we have

µ̂m =

∑Nn=1 p(m|xn)xn∑Nn=1 p(m|xn)

Compare with the estimator when we have perfect knowledge of the hidden variables zmn

i.e.

µ̂m =

∑Nn=1 zmnxn∑Nn=1 zmn

So in the absence of the values zmn, we employ the expected values, or the posteriorprobabilities p(m|xn) which are obtained in the Expectation step.

Hamid Beigy (Sharif University of Technology) Classification based on Bayes decision theory Fall 1394 48 / 70

Page 63: Machine Learning Hamid Beigyce.sharif.edu/.../root/Slides/40717-5to8-handout.pdfWhen C >2, the problem is calledmulti-class classi cation. Hamid Beigy (Sharif University of Technology)

Expectation maximization (cont.)

The estimator for covariance matrices

Σ̂m =

∑Nn=1 p(m|xn)(xn − µ̂m)T (xn − µ̂m)∑N

n=1 p(m|xn)

We also see that we have replaced perfect knowledge of the allocation variables with ourcurrent estimates of the posteriors p(m|xn).

We also need an estimate for p(m), taking derivatives we observe that

p(m) ∝N∑

n=1

p(m|xn)

Then normalizing the above equality results in

p(m) =1

N

N∑n=1

p(m|xn)

Hamid Beigy (Sharif University of Technology) Classification based on Bayes decision theory Fall 1394 49 / 70

Page 64: Machine Learning Hamid Beigyce.sharif.edu/.../root/Slides/40717-5to8-handout.pdfWhen C >2, the problem is calledmulti-class classi cation. Hamid Beigy (Sharif University of Technology)

Expectation maximization (cont.)

EM algorithm has two steps: Expectation & Maximization

In Expectation step

p(m|xn) =p(xn|θm)p(m)∑M

m′=1 p(xn|θ′m)p(m′)

In Maximization step

µ̂m =

∑Nn=1 zmnxn∑Nn=1 zmn

Σ̂m =

∑Nn=1 p(m|xn)(xn − µ̂m)T (xn − µ̂m)∑N

n=1 p(m|xn)

p(m) =1

N

N∑n=1

p(m|xn)

Hamid Beigy (Sharif University of Technology) Classification based on Bayes decision theory Fall 1394 50 / 70

Page 65: Machine Learning Hamid Beigyce.sharif.edu/.../root/Slides/40717-5to8-handout.pdfWhen C >2, the problem is calledmulti-class classi cation. Hamid Beigy (Sharif University of Technology)

Outline

1 Introduction

2 Bayes decision theoryMinimizing the classification error probabilityMinimizing the average riskDiscriminant function and decision surfaceBayesian classifiers for Normally distributed classesMinimum distance classifierBayesian classifiers for independent binary features

3 Supervised learning of the Bayesian classifiersParametric methods for density estimation

Maximum likelihood parameter estimationMaximum a posteriori estimationBayesian estimationMixture models for density estimation

Nonparametric methods for density estimationHistogram estimatorNaive estimatorKernel estimatork−Nearest neighbor estimatork−Nearest neighbor classifier

4 Naive Bayes classifier

Page 66: Machine Learning Hamid Beigyce.sharif.edu/.../root/Slides/40717-5to8-handout.pdfWhen C >2, the problem is calledmulti-class classi cation. Hamid Beigy (Sharif University of Technology)

Nonparametric methods for density estimation

In parametric methods, we assume that the sample is drawn from some knowndistribution (for example Gaussian). But the parameters of this distribution is not knownand our goal is to estimate these parameters from the data.

The main advantage of the parametric methods is the model is defined up to a smallnumber of parameters and when these parameters are estimated, the whole distribution isknown.

The method used to estimate the parameters of the distribution is maximum likelihoodestimation and Bayesian estimation.

Why nonparametric methods for density estimation?

Common parametric forms do not always fit the densities encountered in practice.Most of the classical parametric densities are unimodal, whereas many practical problemsinvolve multi-modal densities.Non-parametric methods can be used with arbitrary distributions and without the assumptionthat the forms of the underlying densities are known.

In nonparametric estimation, we assume is that similar inputs have similar outputs. Thisis a reasonable assumption because the world is smooth and functions, whether they aredensities, discriminants, or regression functions, change slowly.

Hamid Beigy (Sharif University of Technology) Classification based on Bayes decision theory Fall 1394 51 / 70

Page 67: Machine Learning Hamid Beigyce.sharif.edu/.../root/Slides/40717-5to8-handout.pdfWhen C >2, the problem is calledmulti-class classi cation. Hamid Beigy (Sharif University of Technology)

Nonparametric methods for density estimation (cont.)

Assume X = {x1, x2, . . . , xN} be random samples drawn i.i.d. (independently andidentically distributed) from probability density function p(x).

The probability PR is probability that a vector x will fall in a region R and is given by

PR =

∫x∈R

p(x)dx

The probability that k of N samples will fall in R is given by the binomial law.

P(k)R =

(Nk

)PkR (1− PR)N−k

The expected value of k is equal to E [k] = NPR and MLE for PR equals to kN .

If p(x) is continuous and R is small enough so that p(x) does not vary significantly in it,then for all x ∈ R, we can approximate PR with

PR =

∫x ′∈R

p(x ′)dx ′ ≈ p(x)V

V is the volume of RThen the density function can be estimated as

p(x) ≈ k/N

V

Hamid Beigy (Sharif University of Technology) Classification based on Bayes decision theory Fall 1394 52 / 70

Page 68: Machine Learning Hamid Beigyce.sharif.edu/.../root/Slides/40717-5to8-handout.pdfWhen C >2, the problem is calledmulti-class classi cation. Hamid Beigy (Sharif University of Technology)

Nonparametric methods for density estimation (example)

Let x be a univariate feature vector and R(x) be the region given by

R(x) = {x ′|x′ ≤ x}

The nonparametric estimator for the cumulative distribution function, P(x), at point x isthe proportion of sample points that are less than or equal to x .

P̂(x) =|R(x)|N

The nonparametric estimate for the density function can be calculated as

p̂(x) =1

h

[|R(x + h)| − |R(x)|

N

]h is the length of the interval and instances x that fall in this interval are assumed to beclose enough.

Different heuristics are used to determine the instances that are close and their effects onthe estimate.

Hamid Beigy (Sharif University of Technology) Classification based on Bayes decision theory Fall 1394 53 / 70

Page 69: Machine Learning Hamid Beigyce.sharif.edu/.../root/Slides/40717-5to8-handout.pdfWhen C >2, the problem is calledmulti-class classi cation. Hamid Beigy (Sharif University of Technology)

Outline

1 Introduction

2 Bayes decision theoryMinimizing the classification error probabilityMinimizing the average riskDiscriminant function and decision surfaceBayesian classifiers for Normally distributed classesMinimum distance classifierBayesian classifiers for independent binary features

3 Supervised learning of the Bayesian classifiersParametric methods for density estimation

Maximum likelihood parameter estimationMaximum a posteriori estimationBayesian estimationMixture models for density estimation

Nonparametric methods for density estimationHistogram estimatorNaive estimatorKernel estimatork−Nearest neighbor estimatork−Nearest neighbor classifier

4 Naive Bayes classifier

Page 70: Machine Learning Hamid Beigyce.sharif.edu/.../root/Slides/40717-5to8-handout.pdfWhen C >2, the problem is calledmulti-class classi cation. Hamid Beigy (Sharif University of Technology)

Histogram estimator

The oldest and most popular method is thehistogram where the input space is dividedinto equal-sized intervals called bins.Given an origin x0 and a bin width h, the mth bins denoted by Rm(x) is the interval[x0 + mh, x0 + (m + 1)h) for positive and negative integers m and the estimate is given as

p̂(x) =|Rm(x)|

NhIn constructing the histogram, we have to choose both an origin and a bin width.The choice of origin affects the estimate near boundaries of bins, but it is mainly the binwidth that has an effect on the estimate

When bins are small, the estimate is spiky.When bins become larger, the estimate becomes smoother.

The estimate is 0 if no instance falls in a bin and there are discontinuities at binboundaries.One advantage of the histogram is that once the bin estimates are calculated and stored,we do not need to retain the training set.

Hamid Beigy (Sharif University of Technology) Classification based on Bayes decision theory Fall 1394 54 / 70

Page 71: Machine Learning Hamid Beigyce.sharif.edu/.../root/Slides/40717-5to8-handout.pdfWhen C >2, the problem is calledmulti-class classi cation. Hamid Beigy (Sharif University of Technology)

Outline

1 Introduction

2 Bayes decision theoryMinimizing the classification error probabilityMinimizing the average riskDiscriminant function and decision surfaceBayesian classifiers for Normally distributed classesMinimum distance classifierBayesian classifiers for independent binary features

3 Supervised learning of the Bayesian classifiersParametric methods for density estimation

Maximum likelihood parameter estimationMaximum a posteriori estimationBayesian estimationMixture models for density estimation

Nonparametric methods for density estimationHistogram estimatorNaive estimatorKernel estimatork−Nearest neighbor estimatork−Nearest neighbor classifier

4 Naive Bayes classifier

Page 72: Machine Learning Hamid Beigyce.sharif.edu/.../root/Slides/40717-5to8-handout.pdfWhen C >2, the problem is calledmulti-class classi cation. Hamid Beigy (Sharif University of Technology)

Naive estimator

A generalization of the histogram method called the nave estimator, addresses the choiceof bin locations.

The main idea behind this method is to use the estimation point to adaptively determinethe bin locations, thereby eliminating it as an extra parameter. Thus the naive estimatorfrees us from setting an origin.

Given a bin width h, the bin denoted by R(x) is the interval [x − h2 , x + h

2 ) and theestimate is given as

p̂(x) =|R(x)|Nh

This equals to the histogram estimate where x is always at the center of a bin of size h.

The estimator can also be written as

p̂(x) =1

Nh

N∑k=1

w

(x − xk

h

)w is weight function and defined as

w(u) =

{1 if |u| ≤ 1

20 otherwise

Hamid Beigy (Sharif University of Technology) Classification based on Bayes decision theory Fall 1394 55 / 70

Page 73: Machine Learning Hamid Beigyce.sharif.edu/.../root/Slides/40717-5to8-handout.pdfWhen C >2, the problem is calledmulti-class classi cation. Hamid Beigy (Sharif University of Technology)

Properties of Histogram (cont.)

Histogram density model has some drawbacks.

If the process generated the data is multi-modal, this aspect of the distribution can neverbe captured by unimodal distributions such as Gaussian distribution.

A histogram density model is dependent on the choice of the origin x0. This is typicallymuch less significant than the value of h.

A histogram density model may has discontinuities due to the bin edge rather thanproperties of the data.

A major limitation of the histogram approach is its scalability with dimensionality.

Histogram density model has some advantages.

Histogram density model has the property that once histogram has been computed, thedataset itself can be discarded. It is an advantage if the dataset is large.

Lessons from histogram approach to density density estimation

For the estimation of the probability at any point, we should consider the data points thatlie within some local neighborhood of that data. For histogram, this neighborhoodproperty was defined by the bins. There is a natural smoothing parameter describes binlocality.

This smoothing parameter should be neither too large nor too small.

Hamid Beigy (Sharif University of Technology) Classification based on Bayes decision theory Fall 1394 56 / 70

Page 74: Machine Learning Hamid Beigyce.sharif.edu/.../root/Slides/40717-5to8-handout.pdfWhen C >2, the problem is calledmulti-class classi cation. Hamid Beigy (Sharif University of Technology)

Outline

1 Introduction

2 Bayes decision theoryMinimizing the classification error probabilityMinimizing the average riskDiscriminant function and decision surfaceBayesian classifiers for Normally distributed classesMinimum distance classifierBayesian classifiers for independent binary features

3 Supervised learning of the Bayesian classifiersParametric methods for density estimation

Maximum likelihood parameter estimationMaximum a posteriori estimationBayesian estimationMixture models for density estimation

Nonparametric methods for density estimationHistogram estimatorNaive estimatorKernel estimatork−Nearest neighbor estimatork−Nearest neighbor classifier

4 Naive Bayes classifier

Page 75: Machine Learning Hamid Beigyce.sharif.edu/.../root/Slides/40717-5to8-handout.pdfWhen C >2, the problem is calledmulti-class classi cation. Hamid Beigy (Sharif University of Technology)

Kernel estimator

In order to get a smooth estimate, we use a smooth weight function, called kernelfunction, and in this context is also called Parzen window.

p̂(x) =1

Nh

N∑i=1

K

(x − xi

h

)K (.) is some kernel (window) function and h is the bandwidth (smoothing parameter).The most popular kernel function is Gaussian kernel function with mean 0 and variance 1.

K (u) =1√2π

exp

(−u2

2

)Function K (.) determines the shape of influences and h determines the window width.The kernel estimator can be generalized to D−dimensional data.

p̂(x) =1

NhD

N∑k=1

K

(x − xk

h

)

K (u) =

(1√2π

)D

exp

(−||u||

2

2

)The total number of data points lying in this window (cube) equals to (drive it.)

k =N∑i=1

K

(x − xi

h

)Hamid Beigy (Sharif University of Technology) Classification based on Bayes decision theory Fall 1394 57 / 70

Page 76: Machine Learning Hamid Beigyce.sharif.edu/.../root/Slides/40717-5to8-handout.pdfWhen C >2, the problem is calledmulti-class classi cation. Hamid Beigy (Sharif University of Technology)

Outline

1 Introduction

2 Bayes decision theoryMinimizing the classification error probabilityMinimizing the average riskDiscriminant function and decision surfaceBayesian classifiers for Normally distributed classesMinimum distance classifierBayesian classifiers for independent binary features

3 Supervised learning of the Bayesian classifiersParametric methods for density estimation

Maximum likelihood parameter estimationMaximum a posteriori estimationBayesian estimationMixture models for density estimation

Nonparametric methods for density estimationHistogram estimatorNaive estimatorKernel estimatork−Nearest neighbor estimatork−Nearest neighbor classifier

4 Naive Bayes classifier

Page 77: Machine Learning Hamid Beigyce.sharif.edu/.../root/Slides/40717-5to8-handout.pdfWhen C >2, the problem is calledmulti-class classi cation. Hamid Beigy (Sharif University of Technology)

k−Nearest neighbor estimator

One of the difficulties with the kernel approach is that the parameter h is fixed for allkernels.

Large value of h may lead to over-smoothing.

Reducing value of h may lead to noisy estimates.

The optimal choice of h may be dependent on location within the data space.

Hamid Beigy (Sharif University of Technology) Classification based on Bayes decision theory Fall 1394 58 / 70

Page 78: Machine Learning Hamid Beigyce.sharif.edu/.../root/Slides/40717-5to8-handout.pdfWhen C >2, the problem is calledmulti-class classi cation. Hamid Beigy (Sharif University of Technology)

k−Nearest neighbor estimator (cont.)

Instead of fixing h and determining the value of k from the data, we fix the value of kand use the data to find an appropriate value of h.

To do this, we consider a small sphere centered on the point x at which we wish toestimate the density p(x) and allow the radius of the sphere to grow until it containsprecisely k data points.

p̂(x) =k

NV

V is the volume of the resulting sphere.

Value of k determines the degree of smoothing and there is an optimum choice for k thatis neither too large nor too small.

Note that: The model produced by k nearest neighborhood is not a true density modelbecause the integral over all space diverges.

Hamid Beigy (Sharif University of Technology) Classification based on Bayes decision theory Fall 1394 59 / 70

Page 79: Machine Learning Hamid Beigyce.sharif.edu/.../root/Slides/40717-5to8-handout.pdfWhen C >2, the problem is calledmulti-class classi cation. Hamid Beigy (Sharif University of Technology)

Outline

1 Introduction

2 Bayes decision theoryMinimizing the classification error probabilityMinimizing the average riskDiscriminant function and decision surfaceBayesian classifiers for Normally distributed classesMinimum distance classifierBayesian classifiers for independent binary features

3 Supervised learning of the Bayesian classifiersParametric methods for density estimation

Maximum likelihood parameter estimationMaximum a posteriori estimationBayesian estimationMixture models for density estimation

Nonparametric methods for density estimationHistogram estimatorNaive estimatorKernel estimatork−Nearest neighbor estimatork−Nearest neighbor classifier

4 Naive Bayes classifier

Page 80: Machine Learning Hamid Beigyce.sharif.edu/.../root/Slides/40717-5to8-handout.pdfWhen C >2, the problem is calledmulti-class classi cation. Hamid Beigy (Sharif University of Technology)

k−Nearest neighbor classifier

In k−Nearest neighbor classifier, we apply the k−Nearest neighbor density estimationestimation technique to each class separately and then make use of Bayes theorem.

Suppose that we have a data set with Ni points in class Ci with N points in total, so that∑i Nk = N.

To classify a new point x , we draw a sphere centered on x containing precisely k pointsirrespective of their class.

Suppose this sphere has volume V and contains ki points from class Ci .

An estimate of the density associated with each class equals to

p(x |Ci ) =ki

NiV

The unconditional density is given by

p(x) =k

NV

The class priors equal to

p(Ci ) =Ni

N

Combining the above equations using Bayes theorem will results in

p(Ci |x) =p(x |Ci )p(Ci )

p(x)=

kiNiV

NiN

kNV

=kik

Hamid Beigy (Sharif University of Technology) Classification based on Bayes decision theory Fall 1394 60 / 70

Page 81: Machine Learning Hamid Beigyce.sharif.edu/.../root/Slides/40717-5to8-handout.pdfWhen C >2, the problem is calledmulti-class classi cation. Hamid Beigy (Sharif University of Technology)

k−Nearest neighbor classifier (cont.)

In k−Nearest neighbor classifier, the posterior probability of each class equals to

p(Ci |x) =p(x |Ci )p(Ci )

p(x)=

kiNiV

NiN

kNV

=kik

If we wish to minimize the probability of misclassification, this is done by assigning thetest point x to the class having the largest posterior probability, corresponding to thelargest value of ki/k.

Thus to classify a new point, we identify the k nearest points from the training data setand then assign the new point to the class having the largest number of representativesamongst this set.

The particular case of k = 1 is called the nearest-neighbor rule, because a test point issimply assigned to the same class as the nearest point from the training set.

An interesting property of the nearest-neighbor (k = 1) classifier is that, in the limitN →∞, the error rate is never more than twice the minimum achievable error rate of anoptimal classifier.

Hamid Beigy (Sharif University of Technology) Classification based on Bayes decision theory Fall 1394 61 / 70

Page 82: Machine Learning Hamid Beigyce.sharif.edu/.../root/Slides/40717-5to8-handout.pdfWhen C >2, the problem is calledmulti-class classi cation. Hamid Beigy (Sharif University of Technology)

k−Nearest neighbor classifier (example)

The parameter k controls the degree of smoothing, i.e. small k produces many smallregions of each class and large k leads to a fewer larger regions.

Hamid Beigy (Sharif University of Technology) Classification based on Bayes decision theory Fall 1394 62 / 70

Page 83: Machine Learning Hamid Beigyce.sharif.edu/.../root/Slides/40717-5to8-handout.pdfWhen C >2, the problem is calledmulti-class classi cation. Hamid Beigy (Sharif University of Technology)

k−Nearest neighbor classifier (cont.)

k−Nearest neighbor classifier relies on a metric or a distance function, between points

For all points , x , y , and z , a metric d(., .) must satisfy the following properties

Non–negativity: d(x , y) ≥ 0.Reflexivity: d(x , y) = 0⇐⇒ x = y .Symmetry: d(x , y) = d(y , x).Triangle inequality : d(x , y) + d(y , z) ≥ d(x , z).

A general class of metrics for D−dimensional feature vectors is Minkowski metric (alsoreferred to as Lp−norm)

Lp(x , y) =

(D∑i=1

|xi − yi |p) 1

p

When p = 1, the metric called Manhattan or city–block distance and is L1−norm .

When p = 2, the metric called Euclidean distance and is L2−norm .

When p =∞, the L∞−norm is the maximum of distance along individual coordinatesaxes.

L∞(x , y) = maxi|xi − yi |

Hamid Beigy (Sharif University of Technology) Classification based on Bayes decision theory Fall 1394 63 / 70

Page 84: Machine Learning Hamid Beigyce.sharif.edu/.../root/Slides/40717-5to8-handout.pdfWhen C >2, the problem is calledmulti-class classi cation. Hamid Beigy (Sharif University of Technology)

k−Nearest neighbor classifier (cont.)

k−Nearest neighbor (k-NN) is considered a lazy learning algorithm.

It defers data processing until a test example arrives.Replies to request by combining its stored data.discards the constructed answer.

Other names for lazy algorithms

Memory–based.Instance–based.Example–based.Case–based.Experience–based.

This strategy is opposed to an eager learning algorithm which

Analyzes the data and builds a model.Uses the constructed model to classify the test example.

Read chapter 8 of T. Mitchel’s book for other models of instance based algorithms suchas locally weighted regression and case–based reasoning.

Hamid Beigy (Sharif University of Technology) Classification based on Bayes decision theory Fall 1394 64 / 70

Page 85: Machine Learning Hamid Beigyce.sharif.edu/.../root/Slides/40717-5to8-handout.pdfWhen C >2, the problem is calledmulti-class classi cation. Hamid Beigy (Sharif University of Technology)

Conclusions

Both the k−nearest-neighbor method, and the kernel density estimator, require the entiretraining data set to be stored, leading to expensive computation if the data set is large.

This effect can be offset, at the expense of some additional one-off computation, byconstructing tree-based search structures such as KD-tree to allow(approximate) nearestneighbors to be found efficiently without doing an exhaustive search of the data set.

These nonparametric methods are still severely limited.

On the other hand, simple parametric models are very restricted in terms of the forms ofdistribution that they can represent.

We therefore need to find density models that are very flexible and yet for which thecomplexity of the models can be controlled independently of the size of the training set,and we shall see in subsequent chapters how to achieve this.

Hamid Beigy (Sharif University of Technology) Classification based on Bayes decision theory Fall 1394 65 / 70

Page 86: Machine Learning Hamid Beigyce.sharif.edu/.../root/Slides/40717-5to8-handout.pdfWhen C >2, the problem is calledmulti-class classi cation. Hamid Beigy (Sharif University of Technology)

Outline

1 Introduction

2 Bayes decision theoryMinimizing the classification error probabilityMinimizing the average riskDiscriminant function and decision surfaceBayesian classifiers for Normally distributed classesMinimum distance classifierBayesian classifiers for independent binary features

3 Supervised learning of the Bayesian classifiersParametric methods for density estimation

Maximum likelihood parameter estimationMaximum a posteriori estimationBayesian estimationMixture models for density estimation

Nonparametric methods for density estimationHistogram estimatorNaive estimatorKernel estimatork−Nearest neighbor estimatork−Nearest neighbor classifier

4 Naive Bayes classifier

Hamid Beigy (Sharif University of Technology) Classification based on Bayes decision theory Fall 1394 66 / 70

Page 87: Machine Learning Hamid Beigyce.sharif.edu/.../root/Slides/40717-5to8-handout.pdfWhen C >2, the problem is calledmulti-class classi cation. Hamid Beigy (Sharif University of Technology)

Naive Bayes classifier

Bayesian classifiers estimate posterior probabilities based likelihood, prior, and evidence.

These classifiers first estimate p(x |Ci ) and p(Ci ) and then classify the given instance.

How much training data will be required to obtain reliable estimates of thesedistributions?

Consider the number of parameters that must be estimated when C = 2 and x is a vectorof D boolean features.

In this case, we need to estimate a set of parameters

θij = p(xi |Cj)

Index i takes on 2D possible values, and j takes on 2 possible values.

Therefore, we will need to estimate exactly 2(2D − 1) of such θij parameters.

Unfortunately, this corresponds to two distinct parameters for each of the distinctinstances in the instance space for x .

In order to obtain reliable estimates of each of these parameters, we will need to observeeach of these distinct instances multiple times! This is clearly unrealistic in most practicallearning domains.

For example, if x is a vector containing 30 boolean features, then we will need to estimatemore than 3 billion parameters.

Hamid Beigy (Sharif University of Technology) Classification based on Bayes decision theory Fall 1394 66 / 70

Page 88: Machine Learning Hamid Beigyce.sharif.edu/.../root/Slides/40717-5to8-handout.pdfWhen C >2, the problem is calledmulti-class classi cation. Hamid Beigy (Sharif University of Technology)

Naive Bayes classifier (cont.)

Given the intractable sample complexity for learning Bayesian classifiers, we must look forways to reduce this complexity.

The Naive Bayes classifier does this by making a conditional independence assumptionthat dramatically reduces the number of parameters to be estimated when modellingP(xi |Cj), from our original 2(2D − 1) to just 2D.

Definition (Conditional Independence)

Given random variables x , y and z , we say x is conditionally independent of y given z , if andonly if the probability distribution governing x is independent of the value of y given z ; that is

p(xi |yj , zk) = p(xi |zk) ∀i , j , k

The Naive Bayes algorithm is a classification algorithm based on Bayes rule, that assumesthe features x1, x2, . . . , xD are all conditionally independent of one another, given theclass label Ci . Thus we have

px1, x2, . . . , xD |Cj =D∏i=1

p(xi |Cj)

Note that when C and the xi are boolean variables, we need only 2D parameters to definep(xik |Cj) for the necessary i , j , and k .

Hamid Beigy (Sharif University of Technology) Classification based on Bayes decision theory Fall 1394 67 / 70

Page 89: Machine Learning Hamid Beigyce.sharif.edu/.../root/Slides/40717-5to8-handout.pdfWhen C >2, the problem is calledmulti-class classi cation. Hamid Beigy (Sharif University of Technology)

Naive Bayes classifier (cont.)

We derive the Naive Bayes algorithm, assuming in general that C is any discrete-valuedvariable, and features x1, x2, . . . , xD are any discrete or real-valued features.

Our goal is to train a classifier that will output the probability distribution over possiblevalues of C , for each new instance x that we ask it to classify.

The probability that C will take on its kth possible value equals to

p(Ck |x1, x2, . . . , xD) =p(Ck)p(x1, x2, . . . , xD |Ck)∑j p(Cj)p(x1, x2, . . . , xD |Cj)

Now, assuming the xi are conditionally independent given Ck , we can rewrite as

p(Ck |x1, x2, . . . , xD) =p(Cj)

∏i p(xi |Cj)∑

j p(Cj)∏

i p(xi |Ck)

The Naive Bayes classification rule is

C = argmaxCj

p(Cj)∏

i p(xi |Cj)∑j p(Cj)

∏i p(xi |Ck)

Since the denominator does not depend on C , it simplifies to the following

C = argmaxCj

p(Cj)∏i

p(xi |Cj)

Hamid Beigy (Sharif University of Technology) Classification based on Bayes decision theory Fall 1394 68 / 70

Page 90: Machine Learning Hamid Beigyce.sharif.edu/.../root/Slides/40717-5to8-handout.pdfWhen C >2, the problem is calledmulti-class classi cation. Hamid Beigy (Sharif University of Technology)

Naive Bayes for discrete-valued inputs

When the D input features xi each take on J possible discrete values, and C is a discretevariable taking on M possible values, then our learning task is to estimate two sets ofparameters.

θijk = p(xi = x ′ij |C = Ck) Feature xi takes value xij

πk = p(C = Ck)

We can estimate these parameters using either ML estimates or Bayesian/MAP estimates.

θijk =|xi = x ′ij

∧C = Ck |

|Ck |This maximum likelihood estimate sometimes results in θ estimates of zero, if the datadoes not happen to contain any training examples satisfying the condition in thenumerator. To avoid this, it is common to use a smoothed estimate.

θijk =|xi = x ′ij

∧C = Ck |+ l

|Ck |+ lJ

Value of l determines the strength of this smoothing.Maximum likelihood estimates for πk are

πk =|Ck |N

=|Ck |+ l

N + lMHamid Beigy (Sharif University of Technology) Classification based on Bayes decision theory Fall 1394 69 / 70

Page 91: Machine Learning Hamid Beigyce.sharif.edu/.../root/Slides/40717-5to8-handout.pdfWhen C >2, the problem is calledmulti-class classi cation. Hamid Beigy (Sharif University of Technology)

Naive Bayes for continuous inputs

When features are continuous, we must choose some other way to represent thedistributions p(xi |Ck).One common approach is to assume that for each possible Ck , the distribution of eachfeature xi is Gaussian defined by mean and variance specific to xi and Ck .In order to train such a Naive Bayes classifier, we must therefore estimate the mean andstandard deviation of each of these distributions.

µik = E [xi |Ck ]

σ2ik = E [(xik − µik)2|Ck ]

We must also estimate the prior on C .

πk = p(C = Ck)

we can use either maximum likelihood estimates (MLE) or maximum a posteriori (MAP)estimates for these parameters.The maximum likelihood estimator for µik is

µ̂ik =

∑j xijδ(tj = Ck)∑j δ(tj = Ck)

The maximum likelihood estimator for σ2ik is

σ̂2ik =

∑j(xij − µ̂ik)2δ(tj = Ck)∑

j δ(tj = Ck)

Hamid Beigy (Sharif University of Technology) Classification based on Bayes decision theory Fall 1394 70 / 70