Docsity

Understanding Maximum A Posteriori & Maximum Likelihood in Bayesian AI - Prof. Christopher, Study notes of Computer Science

Toggle sidebar
bg1
Artificial Intelligence Programming
Bayesian Learning
Chris Brooks
Department of Computer Science
University of San Francisco
Departmentof Computer Science — University of San Francisco – p. 1/??
19-2: Learning and Classification
•An important sort of learning problem is the classification
problem.
•This involves placing examples into one of two or more classes.
◦Should/shouldn’t get credit
◦Categories of documents.
◦Golf-playing/non-golf-playing days
•This requires access to a set of labeled training examples,
which allow us to induce a hypothesis that describes how to
decide what class an example should be in.
Departmentof Computer Science — University of San Francisco – p. 2/??
19-3: Bayes’ Theorem
•Recall the definition of Bayes’ Theorem
•P(b|a) = P(a|b)P(b)
P(a)
•Let’s rewrite this a bit.
•Let Dbe the data we’ve seen so far.
•Let hbe a possible hypothesis
•P(h|D) = P(D|h)P(h)
P(D)
Departmentof Computer Science — University of San Francisco
19-4: MAP Hypothesis
•Usually, we won’t be so interested in the particular probabilities
for each hypothesis.
•Instead, we want to know: Which hypothesis is most likely,
given the data?
◦Which classification is the most probable?
◦Is Pl ayT ennis or ¬Pl ayT ennis more likely?
•We call this the maximum a posteriori hypothesis (MAP
hypothesis).
•In this case, we can ignore the denominator in Bayes’ Theorem,
since it will be the same for all h.
•hMAP =argmaxh∈HP(D|h)P(h)
•Advantages:
◦Simpler calculation
◦No need to have a prior for P(D)
Departmentof Computer Science — University of San Francisco – p. 4/??
19-5: ML Hypothesis
•In some cases, we can simplify things even further.
•What are the priors P(h)for each hypothesis?
•Without any other information, we’ll often assume that they’re
equally possible.
◦Each has probability 1
H
•In this case, we can just consider the conditional probability
P(D|h).
•We call the hypothesis that maximizes this conditional
probability the maximum likelihood hypothesis.
•hML =argmaxh∈HP(D|h)
Departmentof Computer Science — University of San Francisco – p. 5/??
19-6: Example
•Imagine that we have a large bag of candy. We want to know
the ratio of cherry to lime in the bag.
•We start with 5 hypotheses:
1. h1: 100% cherry
2. h275% cherry, 25% lime.
3. h350% cherry, 50% lime
4. h425% cherry, 75% lime
5. h5100% lime
•Our agent repeatedly draws pieces of candy.
•We want it to correctly pick the type of the next piece of candy.
Departmentof Computer Science — University of San Francisco
pf3
pf4
pf5

Partial preview of the text

Download Understanding Maximum A Posteriori & Maximum Likelihood in Bayesian AI - Prof. Christopher and more Study notes Computer Science in PDF only on Docsity!

Artificial Intelligence Programming

Bayesian Learning

Chris Brooks Department of Computer ScienceUniversity of San Francisco

Department of Computer Science — University of San Francisco – p. 1/

??

19-2: Learning and Classification^ •^

An important sort of learning problem is the

classification

problem. • This involves placing examples into one of two or more classes.^ ◦^

Should/shouldn’t get credit ◦ Categories of documents. ◦ Golf-playing/non-golf-playing days

-^ This requires access to a set of

labeled

training examples,

which allow us to induce a hypothesis that describes how todecide what class an example should be in.

Department of Computer Science — University of San Francisco – p. 2/

??

19-3: Bayes’ Theorem^ •^

Recall the definition of Bayes’ Theorem • P^ (b

|a) =

P^ (a

|b)P

(b) P^ (a

)

-^ Let’s rewrite this a bit. •^ Let

D^

be the data we’ve seen so far.

-^ Let

h^ be a possible hypothesis

-^ P

(h|

D) =

P^ (D

|h)P

(h) P^ (D

)

Department of Computer Science — University of San Fra

19-4: MAP Hypothesis^ •^

Usually, we won’t be so interested in the particular probabilitiesfor each hypothesis. • Instead, we want to know: Which hypothesis is most likely,given the data?^ ◦^

Which classification is the most probable? ◦ Is^ P layT ennis

or^

¬P layT ennis

more likely?

-^ We call this the

maximum a posteriori hypothesis

(MAP

hypothesis). • In this case, we can ignore the denominator in Bayes’ Theorem,since it will be the same for all

h.

-^ h

M AP

=^

argmax

h∈H

P^ (D

|h)P

(h)

-^ Advantages:

◦^ Simpler calculation ◦^ No need to have a prior for

P^ (

D)

Department of Computer Science — University of San Francisco – p. 4/

??

19-5: ML Hypothesis^ •^

In some cases, we can simplify things even further. • What are the priors

P^ (

h)^ for each hypothesis?

-^ Without any other information, we’ll often assume that they’reequally possible.

◦^ Each has probability

(^1) H

-^ In this case, we can just consider the conditional probability^ P

(D

|h).

-^ We call the hypothesis that maximizes this conditionalprobability the

maximum likelihood

hypothesis.

-^ h

M L

=^

argmax

h∈H

P^ (D

|h)

Department of Computer Science — University of San Francisco – p. 5/

??

19-6: Example^ •^

Imagine that we have a large bag of candy. We want to knowthe ratio of cherry to lime in the bag. • We start with 5 hypotheses:1.^

h: 100% cherry^1

2.^

h75% cherry, 25% lime.^2

3.^

h50% cherry, 50% lime^3

4.^

h25% cherry, 75% lime^4

5.^

h100% lime^5

-^ Our agent repeatedly draws pieces of candy. •^ We want it to correctly pick the type of the next piece of candy.

Department of Computer Science — University of San Fra

19-7: Example^ •^

Let’s assume our priors for the different hypotheses are: • (0.^1

,^0.^2

,^0.^4

,^0.^2

,^0 .1)

-^ Also, we assume that the observations are i.i.d.

◦^ This means that each choice is independent of the others.(order doesn’t matter)

-^ In that case, we can multiply probabilities. •^ P

(D

|h) = Πi

P^ (j d|hj^

)i

-^ Suppose we draw 10 limes in a row.

P^ (

D|h

)^ is 3

10 , since the

probability of drawing a lime under

h^3

(^1) is 2

. Department of Computer Science — University of San Francisco – p. 7/

??

19-8: Example^ •^

How do the hypotheses change as data is observed? • Initially, we start with the priors:

1 ,^0

.^2 ,^0

.^4 ,^0

.^2 ,^0

-^ Then we draw a lime.

◦^ P

(h^1

|lime

αP

(lime

|h)^1

P^ (h

◦^ P

(h^2

|lime

αP

(lime

|h)^2

P^ (h

(^1) α 4

∗^0

α^0

.^05.

◦^ P

(h^3

|lime

αP

(lime

|h)^3

P^ (h

(^1) α 2

∗^0

α^0

.^2

◦^ P

(h^4

|lime

αP

(lime

|h)^4

P^ (h

(^3) α 4

∗^0

α^0

.^15.

◦^ P

(h^5

|lime

αP

(lime

|h)^5

P^ (h

α^1

∗^0

α^0

.^1.

◦^ α

Department of Computer Science — University of San Francisco – p. 8/

??

19-9: Example^ •^

Then we draw a second lime.^ ◦^

P^ (h

|lime, lime 1

αP

(lime, lime

|h)^1

P^ (h

◦^ P

(h^2

|lime, lime

αP

(lime, lime

|h)^2

P^ (h

(^1) α 4

1 ∗^4

α^0.

◦^ P

(h^3

|lime, lime

αP

(lime, lime

|h)^3

P^ (h

(^1) α 2

1 ∗^2

α^0.

◦^ P

(h^4

|lime, lime

αP

(lime, lime

|h)^4

P^ (h

(^3) α 4

3 ∗^4

α^0.

◦^ P

(h^5

|lime

αP

(lime

|h^5

)P^ (

h) =^5

α^1

∗^0

α^0

.^1.

◦^ α

.^07.

-^ Strictly speaking, we don’t really care what

α^ is.

-^ We can just select the MAP hypothesis, since we just want toknow the most likely hypothesis.

Department of Computer Science — University of San Fra

19-10: Bayesian Learning^ •^

Eventually, the true hypothesis will dominate all others.^ ◦^

Caveat: assuming the data is noise-free, or noise isuniformly distributed.

-^ Notice that we can use Bayesian learning (in this case) eitheras a batch algorithm or as an incremental algorithm. •^ We can always easily update our hypotheses to incorporatenew evidence.

◦^ This depends on the assumption that our observations areindependent.

Department of Computer Science — University of San Francisco – p. 10/

??

19-11: Learning bias^ •^

What sort of bias does Bayesian Learning use? • Typically, simpler hypotheses will have larger priors. • More complex hypotheses will fit data more exactly (but there’smany more of them).^ ◦^

Under these assumptions,

hM AP

will be the simplest

hypothesis that fits the data. ◦ This is Occam’s razor, again. ◦ Think about the deterministic case, where

P^ (

h|Di

)^ is either

1 or 0.

Department of Computer Science — University of San Francisco – p. 11/

??

19-12: Bayesian Concept Learning^ •^

Bayesian Learning involves estimating the likelihood of eachhypothesis. • In a more complex world where observations are notindependent, this could be difficult. • Our first cut at doing this might be a brute force approach:1. For each

h^ in

H, calculate

P^ (

h|D

P^ ( D|h)

P^ (h

) P^ (D

)

  1. From this, output the hypothesis

hM AP

with the highest

posterior probability.

-^ This is what we did in the example.

◦^ Challenge - Bayes’ Theorem can be computationallyexpensive to use in the case where observations are noti.i.d. ◦^ P

(h|

o, o^1

P^ (o

1 |h,o

2 )P^ (

h|o^2

)

P^ (o^1

|o)^2

Department of Computer Science — University of San Fran

19-19: Naive Bayes assumption^ •^

Naive Bayes assumes that all attributes are conditionallyindependent of each other. • In this case,

P^ (

a, a^1

, ..., a 2

|vn ) = Πi

P^ (i a|vi

).i

-^ This can be estimated from the training data. •^ The classifier then picks the class with the highest probabilityaccording to this equation. •^ Interestingly, Naive Bayes performs well even in cases wherethe conditional independence assumption fails.

Department of Computer Science — University of San Francisco – p. 19/

??

19-20: Example^ •^

Recall your tennis-playing problem from the decision treehomework. • We want to use the training data and a Naive Bayes classifier toclassify the following instance: • Outlook = Sunny, Temperature = Cool, Humidity = high, Wind =Strong.

Department of Computer Science — University of San Francisco – p. 20/

??

19-21: Example

Day^

Outlook

Temperature

Humidity

Wind

PlayTennis

D^

Sunny

Hot^

High

Weak

No

D^

Sunny

Hot^

High

Strong

No

D^

Overcast

Hot^

High

Weak

Yes

D^

Rain

Mild

High

Weak

Yes

D^

Rain

Cool

Normal

Weak

Yes

D^

Rain

Cool

Normal

Strong

No

D^

Overcast

Cool

Normal

Strong

Yes

D^

Sunny

Mild

High

Weak

No

D^

Sunny

Cool

Normal

Weak

Yes

D^

Rain

Mild

Normal

Weak

Yes

D^

Sunny

Mild

Normal

Strong

Yes

D^

Overcast

Mild

High

Strong

Yes

D^

Overcast

Hot^

Normal

Weak

Yes

D^

Rain

Mild

High

Strong

No Department of Computer Science — University of San Fran

19-22: Example^ •^

Our priors are:^ ◦^

P^ (P layT ennis

=^ yes

.^64

◦^ P

(P layT ennis

=^ no

.^36

-^ We can estimate:

◦^ P

(wind

=^

strong

|P layT ennis

=^

yes

.^33

◦^ P

(wind

=^

strong

|P layT ennis

=^

no) = 3

.^6

◦^ P

(humidity

=^

high

|P layT ennis

=^ yes

.^33

◦^ P

(humidity

=^

high

|P layT ennis

=^ no

.^8

◦^ P

(outlook

=^

sunny

|P layT ennis

=^

yes

.^22

◦^ P

(outlook

=^

sunny

|P layT ennis

=^

no) = 3

.^6

◦^ P

(temp

=^

cool

|P layT ennis

=^

yes

.^33

◦^ P

(temp

=^

cool

|P layT ennis

=^

no) = 1

.^2

Department of Computer Science — University of San Francisco – p. 22/

??

19-23: Example^ •^

vyes

P^ (yes

)P^ (

sunny

|yes

)P^ (

cool

|yes

)P^ (

high

|yes

)P^ (

strong

|yes

0.^005 • vno

=^

P^ (no

)P^ (

sunny

|no

)P^ (

cool

|no

)P^ (

high

|no

)P^ (

strong

|no

0.^0206 • So we see that not playing tennis is the maximum likelihoodhypothesis. • Further, by normalizing, we see that the classifier predicts a

0.^02060 .005+

.^0206

.^80

probability of not playing tennis.

Department of Computer Science — University of San Francisco – p. 23/

??

19-24: Estimating Probabilities^ •^

As we can see from this example, estimating probabilitiesthrough frequency is risky when our data set is small. • We only have 5 negative examples, so we may not have anaccurate estimate. • A better approach is to use the following formula, called an m-estimate:n+c^ •

mp n+m

-^ Where

nc^

is the number of individuals with the characteristic of

interest (say Wind = strong),

n^ is the total number of

positive/negative examples,

p^ is our prior estimate, and

m^

is a

constant called the

equivalent sample size

. Department of Computer Science — University of San Fran

19-25: Estimating Probabilities^ •^

m^ determines how heavily to weight

p.

-^ p

is assumed to be uniform.

-^ So, in the Tennis example,^ P

(wind

=^

strong

|playT ennis

=^

no) =

3+

.^2 m 5+m -^ We’ll determine an

m^

based on sample size.

◦^ If

m^

is zero, we just use observed data. ◦^ If

m >> n

, we use the prior.

◦^ Otherwise

m^

lets us weight these parameters’ relative

influence.

Department of Computer Science — University of San Francisco – p. 25/

??

19-26: Using Naive Bayes to classify text^ •^

One are where Naive Bayes has been very successful is in textclassification.^ ◦^

Despite the violation of independence assumptions.

-^ Problem - given some text documents labeled as to category,determine the category of new and unseen documents. •^ Our features will be the words that appear in a document. •^ Based on this, we’ll predict a category.

Department of Computer Science — University of San Francisco – p. 26/

??

19-27: Using Naive Bayes to classify text^ •^

The first thing we need is some prior probabilities for words. • We get this by scanning the entire training set and counting thefrequency of each word.^ ◦^

Common, content-free words such as a, an, the, he, she,etc. are often left out. (these are called

stop words

-^ This word frequency count gives us an estimate of the priors foreach word.

Department of Computer Science — University of San Fran

19-28: Using Naive Bayes to classify text^ •^

We then need priors for each category.^ ◦^

We get this by counting the fraction of the training set thatbelong to a given category.

-^ Finally, we need to determine the conditional probability^ P

(wk

|v)j^

◦^ We do this by concatenating all documents in class

v. Callj^

this the Text. ◦ Then, for each word in the vocabulary, we count how often itoccurs in the Text. Call this

nk

◦^ Let

n^ be the total number of words in Text. ◦^ P

(wk

|v) =j^

n+1k^ n+|V ocabulary

|

◦^ m

is the size of the vocabulary.

Department of Computer Science — University of San Francisco – p. 28/

??

19-29: Classifying text^ •^

Once we’ve computed the conditional probabilities, we canclassify text. • For a new document, find all distinct words in the document. • For each class of document, compute the posterior probabilityof that class, given the words present in the document. • Seems too simple, but it actually works. (Up to 90% accurate insome cases.) • There are

a lot

of details that can drastically affect

performance. You will get to see these firsthand in project 3.

Department of Computer Science — University of San Francisco – p. 29/

??