MLE/MAP + Nave Bayes MLE / MAP Readings: Nave Bayes Readings: - - PowerPoint PPT Presentation

mle map na ve bayes
SMART_READER_LITE
LIVE PREVIEW

MLE/MAP + Nave Bayes MLE / MAP Readings: Nave Bayes Readings: - - PowerPoint PPT Presentation

10-601 Introduction to Machine Learning Machine Learning Department School of Computer Science Carnegie Mellon University MLE/MAP + Nave Bayes MLE / MAP Readings: Nave Bayes Readings: Matt


slide-1
SLIDE 1

MLE/MAP + Naïve ¡Bayes

1

10-­‑601 ¡Introduction ¡to ¡Machine ¡Learning

Matt ¡Gormley Lecture ¡5 February ¡1, ¡2016

Machine ¡Learning ¡Department School ¡of ¡Computer ¡Science Carnegie ¡Mellon ¡University

MLE ¡/ ¡MAP ¡Readings: “Estimating ¡Probabilities” ¡ (Mitchell, ¡2016) Naïve ¡Bayes ¡Readings: “Generative ¡and ¡Discriminative ¡ Classifiers: ¡Naive ¡Bayes ¡and ¡Logistic ¡ Regression” ¡ (Mitchell, ¡2016) Murphy ¡3 Bishop ¡-­‑-­‑ HTF ¡-­‑-­‑ Mitchell ¡6.1-­‑6.10

slide-2
SLIDE 2

Reminders

  • Background Exercises (Homework 1)

– Release: ¡Wed, ¡Jan. ¡25 – Due: ¡Wed, ¡Feb. ¡1 ¡at ¡5:30pm – ONLY ¡HW1: ¡Collaboration questions not required

  • Homework 2: ¡Naive Bayes

– Release: ¡Wed, ¡Feb. ¡1 – Due: ¡Mon, ¡Feb. ¡13 ¡at ¡5:30pm

2

slide-3
SLIDE 3

MLE ¡/ ¡MAP ¡Outline

  • Generating ¡Data

– Natural ¡(stochastic) ¡data – Synthetic ¡data – Why ¡synthetic ¡data? – Examples: ¡Multinomial, ¡Bernoulli, ¡Gaussian

  • Data ¡Likelihood

– Independent ¡and ¡Identically ¡Distributed ¡(i.i.d.) – Example: ¡Dice ¡Rolls

  • Learning ¡from ¡Data ¡(Frequentist)

– Principle ¡of ¡Maximum ¡Likelihood ¡Estimation ¡(MLE) – Optimization ¡for ¡MLE – Examples: ¡1D ¡and ¡2D ¡optimization – Example: ¡MLE ¡of ¡Multinomial – Aside: ¡Method ¡of ¡Lagrange ¡Multipliers

  • Learning ¡from ¡Data ¡(Bayesian)

– maximum ¡a ¡posteriori ¡(MAP) ¡estimation – Optimization ¡for ¡MAP – Example: ¡MAP ¡of ¡Bernoulli—Beta ¡

3

This ¡Lecture Last ¡Lecture

slide-4
SLIDE 4

Learning ¡from ¡Data ¡(Frequentist)

Whiteboard

– Aside: ¡Method ¡of ¡Langrange Multipliers – Example: ¡MLE ¡of ¡Multinomial

4

slide-5
SLIDE 5

Learning ¡from ¡Data ¡(Bayesian)

Whiteboard

– maximum ¡a ¡posteriori ¡(MAP) ¡estimation – Optimization ¡for ¡MAP – Example: ¡MAP ¡of ¡Bernoulli—Beta ¡

5

slide-6
SLIDE 6

Takeaways

  • One ¡view ¡of ¡what ¡ML ¡is ¡trying ¡to ¡accomplish ¡is ¡

function ¡approximation

  • The ¡principle ¡of ¡maximum ¡likelihood ¡

estimation ¡provides ¡an ¡alternate ¡view ¡of ¡ learning

  • Synthetic ¡data ¡can ¡help ¡debug ML ¡algorithms
  • Probability ¡distributions ¡can ¡be ¡used ¡to ¡model

real ¡data ¡that ¡occurs ¡in ¡the ¡world (don’t ¡worry ¡we’ll ¡make ¡our ¡distributions ¡more ¡ interesting ¡soon!)

6

slide-7
SLIDE 7

Naïve ¡Bayes ¡Outline

  • Probabilistic ¡(Generative) ¡View ¡of ¡

Classification

– Decision ¡rule ¡for ¡probability ¡model

  • Real-­‑world ¡Dataset

– Economist ¡vs. ¡Onion ¡articles – Document ¡à bag-­‑of-­‑words ¡à binary ¡feature ¡ vector

  • Naive ¡Bayes: ¡Model

– Generating ¡synthetic ¡"labeled ¡documents" – Definition ¡of ¡model – Naive ¡Bayes ¡assumption – Counting ¡# ¡of ¡parameters ¡with ¡/ ¡without ¡NB ¡ assumption

  • Naïve ¡Bayes: ¡Learning ¡from ¡Data

– Data ¡likelihood – MLE ¡for ¡Naive ¡Bayes – MAP ¡for ¡Naive ¡Bayes

  • Visualizing ¡Gaussian ¡Naive ¡Bayes

7

slide-8
SLIDE 8

Today’s ¡Goal

To ¡define ¡a ¡generative ¡model ¡

  • f ¡emails ¡of ¡two ¡different ¡

classes ¡ (e.g. ¡spam ¡vs. ¡not ¡spam)

8

slide-9
SLIDE 9

Spam ¡News

The ¡Economist The ¡Onion

9

slide-10
SLIDE 10

Real-­‑world ¡Dataset

Whiteboard

– Economist ¡vs. ¡Onion ¡articles – Document ¡à bag-­‑of-­‑words ¡à binary ¡feature ¡ vector

10

slide-11
SLIDE 11

Naive ¡Bayes: ¡Model

Whiteboard

– Generating ¡synthetic ¡"labeled ¡documents" – Definition ¡of ¡model – Naive ¡Bayes ¡assumption – Counting ¡# ¡of ¡parameters ¡with ¡/ ¡without ¡NB ¡ assumption

11

slide-12
SLIDE 12

Model ¡1: ¡Bernoulli ¡Naïve ¡Bayes

12

If ¡HEADS, ¡flip ¡ each ¡red ¡coin Flip ¡weighted ¡coin If ¡TAILS, ¡flip ¡ each ¡blue ¡coin 1 1 … 1 y x1 x2 x3 … xM 1 1 … 1 1 1 1 1 … 1 1 … 1 1 1 … 1 1 1 … Each ¡red ¡coin ¡ corresponds ¡to ¡ an ¡xm … … We ¡can ¡generate data ¡in ¡ this ¡fashion. ¡Though ¡in ¡ practice ¡we ¡never ¡would ¡ since ¡our ¡data ¡is ¡given. ¡ Instead, ¡this ¡provides ¡an ¡ explanation ¡of ¡how the ¡ data ¡was ¡generated ¡ (albeit ¡a ¡terrible ¡one).

slide-13
SLIDE 13

Naive ¡Bayes: ¡Model

Whiteboard

– Generating ¡synthetic ¡"labeled ¡documents" – Definition ¡of ¡model – Naive ¡Bayes ¡assumption – Counting ¡# ¡of ¡parameters ¡with ¡/ ¡without ¡NB ¡ assumption

13

slide-14
SLIDE 14

What’s ¡wrong ¡with ¡the ¡ Naïve ¡Bayes ¡Assumption?

The ¡features ¡might ¡not ¡be ¡independent!!

14

  • Example ¡1:

– If ¡a ¡document ¡contains ¡the ¡word ¡ “Donald”, ¡it’s ¡extremely ¡likely ¡to ¡ contain ¡the ¡word ¡“Trump” – These ¡are ¡not ¡independent!

  • Example ¡2:

– If ¡the ¡petal ¡width ¡is ¡very ¡high, ¡ the ¡petal ¡length ¡is ¡also ¡likely ¡to ¡ be ¡very ¡high

slide-15
SLIDE 15

Naïve ¡Bayes: ¡Learning ¡from ¡Data

Whiteboard

– Data ¡likelihood – MLE ¡for ¡Naive ¡Bayes – MAP ¡for ¡Naive ¡Bayes

15

slide-16
SLIDE 16

VISUALIZING ¡NAÏVE ¡BAYES

16

Slides ¡in ¡this ¡section ¡from ¡William ¡Cohen ¡(10-­‑601B, ¡Spring ¡2016)

slide-17
SLIDE 17
slide-18
SLIDE 18

Fisher ¡Iris ¡Dataset

Fisher ¡(1936) ¡used ¡150 ¡measurements ¡of ¡flowers ¡ from ¡3 ¡different ¡species: ¡Iris ¡setosa (0), ¡Iris ¡ virginica (1), ¡Iris ¡versicolor (2) ¡collected ¡by ¡ Anderson ¡(1936)

18

Full ¡dataset: ¡https://en.wikipedia.org/wiki/Iris_flower_data_set Species Sepal ¡ Length Sepal ¡ Width Petal ¡ Length Petal ¡ Width 4.3 3.0 1.1 0.1 4.9 3.6 1.4 0.1 5.3 3.7 1.5 0.2 1 4.9 2.4 3.3 1.0 1 5.7 2.8 4.1 1.3 1 6.3 3.3 4.7 1.6 1 6.7 3.0 5.0 1.7

slide-19
SLIDE 19

Slide ¡from ¡William ¡Cohen

slide-20
SLIDE 20

Slide ¡from ¡William ¡Cohen

slide-21
SLIDE 21

Plot ¡the ¡difference ¡of ¡the ¡probabilities

Slide ¡from ¡William ¡Cohen

slide-22
SLIDE 22

Naïve ¡Bayes ¡has ¡a ¡linear decision ¡ boundary

Slide ¡from ¡William ¡Cohen ¡(10-­‑601B, ¡Spring ¡2016)

slide-23
SLIDE 23

Figure ¡from ¡William ¡Cohen ¡(10-­‑601B, ¡Spring ¡2016)

slide-24
SLIDE 24

Why ¡don’t ¡we ¡drop ¡the ¡ generative ¡model ¡and ¡ try ¡to ¡learn ¡this ¡ hyperplane directly?

Figure ¡from ¡William ¡Cohen ¡(10-­‑601B, ¡Spring ¡2016)

slide-25
SLIDE 25

Beyond ¡the ¡Scope ¡of ¡this ¡Lecture

  • Multinomial Naïve ¡Bayes ¡can ¡be ¡used ¡for ¡

integer features

  • Multi-­‑class ¡Naïve ¡Bayes ¡can ¡be ¡used ¡if ¡your ¡

classification ¡problem ¡has ¡> ¡2 ¡classes

25

slide-26
SLIDE 26

Summary

  • 1. Naïve ¡Bayes ¡provides ¡a ¡framework ¡for ¡

generative ¡modeling

  • 2. Choose ¡p(xm | ¡y) ¡appropriate ¡to ¡the ¡data

(e.g. ¡Bernoulli ¡for ¡binary ¡features, ¡ Gaussian ¡for ¡continuous ¡features)

  • 3. Train ¡by ¡MLE or ¡MAP
  • 4. Classify ¡by ¡maximizing ¡the ¡posterior

26

slide-27
SLIDE 27

EXTRA ¡SLIDES

27

slide-28
SLIDE 28

Model: ¡Product ¡of ¡prior and ¡the ¡event ¡model

Naïve ¡Bayes ¡Model

28

Generic

P(, Y ) = P(Y )

K

  • k=1

P(Xk|Y )

Support: Depends ¡on ¡the ¡choice ¡of ¡event ¡model, ¡P(Xk|Y) Training: ¡Find ¡the ¡class-­‑conditional ¡MLE ¡parameters For ¡P(Y), ¡we ¡find ¡the ¡MLE ¡using ¡all ¡the ¡data. ¡For ¡each ¡ P(Xk|Y) we ¡condition ¡on ¡the ¡data ¡with ¡the ¡corresponding ¡ class. Classification: ¡Find ¡the ¡class ¡that ¡maximizes ¡the ¡posterior

ˆ y =

y

p(y|)

slide-29
SLIDE 29

Naïve ¡Bayes ¡Model

29

Generic

Classification:

ˆ y =

y

p(y|) (posterior) =

y

p(|y)p(y) p(x) (by Bayes’ rule) =

y

p(|y)p(y)

slide-30
SLIDE 30

Model ¡1: ¡Bernoulli ¡Naïve ¡Bayes

30

Support: ¡Binary ¡vectors ¡of ¡length ¡K

∈ {0, 1}K

Generative ¡Story:

Y ∼ Bernoulli(φ) Xk ∼ Bernoulli(θk,Y ) ∀k ∈ {1, . . . , K}

Model:

pφ,θ(x, y) = pφ,θ(x1, . . . , xK, y) = pφ(y)

K

  • k=1

pθk(xk|y) = (φ)y(1 − φ)(1−y)

K

  • k=1

(θk,y)xk(1 − θk,y)(1−xk)

slide-31
SLIDE 31

Model ¡1: ¡Bernoulli ¡Naïve ¡Bayes

31

Support: ¡Binary ¡vectors ¡of ¡length ¡K

∈ {0, 1}K

Generative ¡Story:

Y ∼ Bernoulli(φ) Xk ∼ Bernoulli(θk,Y ) ∀k ∈ {1, . . . , K}

Model:

pφ,θ(x, y) =

Classification: ¡Find ¡the ¡class ¡that ¡maximizes ¡the ¡posterior

ˆ y =

y

p(y|)

= (φ)y(1 − φ)(1−y)

K

  • k=1

(θk,y)xk(1 − θk,y)(1−xk)

Same ¡as ¡Generic ¡ Naïve ¡Bayes

slide-32
SLIDE 32

Model ¡1: ¡Bernoulli ¡Naïve ¡Bayes

32

Training: ¡Find ¡the ¡class-­‑conditional ¡MLE ¡parameters For ¡P(Y), ¡we ¡find ¡the ¡MLE ¡using ¡all ¡the ¡data. ¡For ¡each ¡ P(Xk|Y) we ¡condition ¡on ¡the ¡data ¡with ¡the ¡corresponding ¡ class.

φ = N

i=1 I(y(i) = 1)

N θk,0 = N

i=1 I(y(i) = 0 ∧ x(i) k

= 1) N

i=1 I(y(i) = 0)

θk,1 = N

i=1 I(y(i) = 1 ∧ x(i) k

= 1) N

i=1 I(y(i) = 1)

∀k ∈ {1, . . . , K}

slide-33
SLIDE 33

Model ¡1: ¡Bernoulli ¡Naïve ¡Bayes

33

Training: ¡Find ¡the ¡class-­‑conditional ¡MLE ¡parameters For ¡P(Y), ¡we ¡find ¡the ¡MLE ¡using ¡all ¡the ¡data. ¡For ¡each ¡ P(Xk|Y) we ¡condition ¡on ¡the ¡data ¡with ¡the ¡corresponding ¡ class.

φ = N

i=1 I(y(i) = 1)

N θk,0 = N

i=1 I(y(i) = 0 ∧ x(i) k

= 1) N

i=1 I(y(i) = 0)

θk,1 = N

i=1 I(y(i) = 1 ∧ x(i) k

= 1) N

i=1 I(y(i) = 1)

∀k ∈ {1, . . . , K}

Data:

1 1 … 1 y x1 x2 x3 … xK 1 1 … 1 1 1 1 1 … 1 1 … 1 1 1 … 1 1 1 …

slide-34
SLIDE 34

Model ¡2: ¡Multinomial ¡Naïve ¡Bayes

34

Option ¡1: ¡Integer ¡vector ¡(word ¡IDs)

= [x1, x2, . . . , xM] where xm ∈ {1, . . . , K} a word id.

Support: Generative ¡Story:

for i ∈ {1, . . . , N}: y(i) ∼ Bernoulli(φ) for j ∈ {1, . . . , Mi}: x(i)

j

∼ Multinomial(θy(i), 1)

Model:

pφ,θ(x, y) = pφ(y)

K

  • k=1

pθk(xk|y) = (φ)y(1 − φ)(1−y)

Mi

  • j=1

θy,xj

slide-35
SLIDE 35

Model ¡3: ¡Gaussian ¡Naïve ¡Bayes

35

Model: ¡Product ¡of ¡prior and ¡the ¡event ¡model Support: ¡

p(x, y) = p(x1, . . . , xK, y) = p(y)

K

  • k=1

p(xk|y) ∈ RK

Gaussian Naive Bayes assumes that p(xk|y) is given by a Normal distribution.

slide-36
SLIDE 36

Model ¡4: ¡Multiclass ¡Naïve ¡Bayes

36

Model:

p(x, y) = p(x1, . . . , xK, y) = p(y)

K

  • k=1

p(xk|y)

Now, y ∼ Multinomial(φ, 1) and we have a sepa- rate conditional distribution p(xk|y) for each of the C classes. The only change is that we permit y to range over C classes.

slide-37
SLIDE 37

Smoothing

  • 1. Add-­‑1 ¡Smoothing
  • 2. Add-­‑λ Smoothing
  • 3. MAP ¡Estimation ¡(Beta ¡Prior)

37

slide-38
SLIDE 38

MLE

What ¡does ¡maximizing ¡likelihood ¡accomplish?

  • There ¡is ¡only ¡a ¡finite ¡amount ¡of ¡probability ¡

mass ¡(i.e. ¡sum-­‑to-­‑one ¡constraint)

  • MLE ¡tries ¡to ¡allocate ¡as ¡much ¡probability ¡

mass ¡as ¡possible ¡to ¡the ¡things ¡we ¡have ¡

  • bserved…

…at ¡the ¡expense of ¡the ¡things ¡we ¡have ¡not

  • bserved

38

slide-39
SLIDE 39

MLE

For ¡Naïve ¡Bayes, ¡suppose ¡we ¡never ¡observe ¡ the ¡word ¡“serious” ¡in ¡an ¡Onion ¡article. In ¡this ¡case, ¡what ¡is ¡the ¡MLE ¡of ¡p(xk | ¡y)?

39

θk,0 = N

i=1 I(y(i) = 0 ∧ x(i) k

= 1) N

i=1 I(y(i) = 0)

Now ¡suppose ¡we ¡observe ¡the ¡word ¡“serious” ¡ at ¡test ¡time. ¡What ¡is ¡the ¡posterior ¡probability ¡ that ¡the ¡article ¡was ¡an ¡Onion ¡article?

p(y|x) = p(x|y)p(y) p(x)

slide-40
SLIDE 40
  • 1. ¡Add-­‑1 ¡Smoothing

The simplest setting for smoothing simply adds a single pseudo-observation to the data. This converts the true

  • bservations D into a new dataset D from we derive

the MLEs. D = {((i), y(i))}N

i=1

(1) D = D ∪ {(, 0), (, 1), (, 0), (, 1)} (2) where is the vector of all zeros and is the vector of all ones. This has the effect of pretending that we observed each feature xk with each class y.

40

slide-41
SLIDE 41
  • 1. ¡Add-­‑1 ¡Smoothing

41

What if we write the MLEs in terms of the original dataset D?

φ = N

i=1 I(y(i) = 1)

N θk,0 = 1 + N

i=1 I(y(i) = 0 ∧ x(i) k

= 1) 2 + N

i=1 I(y(i) = 0)

θk,1 = 1 + N

i=1 I(y(i) = 1 ∧ x(i) k

= 1) 2 + N

i=1 I(y(i) = 1)

∀k ∈ {1, . . . , K}

slide-42
SLIDE 42
  • 2. ¡Add-­‑λ Smoothing

42

Suppose we have a dataset obtained by repeatedly rolling a K-sided (weighted) die. Given data D = {x(i)}N

i=1 where x(i) ∈ {1, . . . , K}, we have the fol-

lowing MLE: φk = N

i=1 I(x(i) = k)

N Withadd-λsmoothing, weaddpseudo-observationsas before to obtain a smoothed estimate: φk = λ + N

i=1 I(x(i) = k)

kλ + N

For ¡the ¡Categorical ¡Distribution

slide-43
SLIDE 43

MLE ¡vs. ¡MAP

43

Suppose we have data D = {x(i)}N

i=1 MLE

  • MAP
  • θMAP =

θ N

  • i=1

p((i)|θ)p(θ)

Prior

θMLE =

θ N

  • i=1

p((i)|θ)

Maximum ¡Likelihood ¡ Estimate ¡(MLE) Maximum ¡a ¡posteriori (MAP) ¡estimate

slide-44
SLIDE 44
  • 3. ¡MAP ¡Estimation ¡(Beta ¡Prior)

44

Generative ¡Story: The ¡parameters ¡are ¡ drawn ¡once ¡for ¡the ¡ entire ¡dataset.

for k ∈ {1, . . . , K}: for y ∈ {0, 1}: θk,y ∼ Beta(α, β) for i ∈ {1, . . . , N}: y(i) ∼ Bernoulli(φ) for k ∈ {1, . . . , K}: x(i)

k

∼ Bernoulli(θk,y(i))

Training: ¡Find ¡the ¡class-­‑conditional ¡ MAP ¡parameters

φ = N

i=1 I(y(i) = 1)

N θk,0 = (α − 1) + N

i=1 I(y(i) = 0 ∧ x(i) k

= 1) (α − 1) + (β − 1) + N

i=1 I(y(i) = 0)

θk,1 = (α − 1) + N

i=1 I(y(i) = 1 ∧ x(i) k

= 1) (α − 1) + (β − 1) + N

i=1 I(y(i) = 1)

∀k ∈ {1, . . . , K}