3 naive Bayes - Virginia Tech

25
Probability and Naive Bayes Machine Learning CS5824/ECE5424 Bert Huang Virginia Tech

Transcript of 3 naive Bayes - Virginia Tech

Page 1: 3 naive Bayes - Virginia Tech

Probability and Naive Bayes

Machine Learning CS5824/ECE5424

Bert Huang Virginia Tech

Page 2: 3 naive Bayes - Virginia Tech

Outline

• Probabilistic identities

• Independence and conditional independence

• Naive Bayes

• Log tricks

Page 3: 3 naive Bayes - Virginia Tech

Probability Identities• Random variables in caps (A)

• values in lowercase: A = a or just a for shorthand

• P(a | b) = P(a, b) / P(b)

• P(a, b) = P(a | b) P(b)

• P(b | a) = P(a | b) P(b) / P(a)

conditional probability

joint probability

Page 4: 3 naive Bayes - Virginia Tech

Probability via Counting

Page 5: 3 naive Bayes - Virginia Tech

Probability via CountingP(circle, red)

2

8

=2/8 = 0.25

Page 6: 3 naive Bayes - Virginia Tech

Probability via Counting

2

8

12

3

35

P(circle | red) = P(circle, red) / P(red)

2/3 2/8 3/8

Page 7: 3 naive Bayes - Virginia Tech

Probability via Counting

2

8

12

3

35

P(circle | red) P(red) = P(circle, red)

2/3 2/83/8

Page 8: 3 naive Bayes - Virginia Tech

Probability Identities• Random variables in caps (A)

• values in lowercase: A = a or just a for shorthand

• P(a | b) = P(a, b) / P(b)

• P(a, b) = P(a | b) P(b)

• P(b | a) = P(a | b) P(b) / P(a)

Page 9: 3 naive Bayes - Virginia Tech

Bayes Rule

• P(b | a)

• P(b | a) = P(a, b) / P(a)

• P(b | a) = P(a | b) P(b) / P(a)

Page 10: 3 naive Bayes - Virginia Tech

Classification• x ∈ {0,1}d, y ∈ {0,1}

• f(x) ∈ {0,1}

• Accuracy: E[f(x) = y]

• Bayes optimal classifier: f(x) = arg max y p(y | x)

• Seems natural, but why is this optimal?

Page 11: 3 naive Bayes - Virginia Tech

Back-of-Envelope for Bayes Optimal

• For each unique x, p(y | x) is a coin flip

• Assume we need n samples to accurately estimate a coin flip

• How many unique x’s?

• |{0,1}d| = 2d

• Need n2d samples

n = 100

d = 100

Need 1.2676506 x 1032 samples

Page 12: 3 naive Bayes - Virginia Tech

1.2676506 x 10,000,000,000,000,000,000,000,000,000,000

Page 13: 3 naive Bayes - Virginia Tech

How Many Samples?• Concentration bounds

Page 14: 3 naive Bayes - Virginia Tech
Page 15: 3 naive Bayes - Virginia Tech

• A and B are independent iff p(A, B) = p(A) p(B)

• A and B are conditionally independent given C iff p(A, B | C) = p(A | C) p(B | C)

Independence

Page 16: 3 naive Bayes - Virginia Tech

Naive Bayes• Assume dimensions of x are conditionally independent given y

• Bag of words: p(“virginia”, “tech” | y) = p(“virginia” | y) p(“tech” | y)

• f(x) = arg max y p(y | x)

• = arg max y p(x | y) p(y) / p(x)

• = arg max y p(x | y) p(y)

• = arg max y p(y) ∏j p(xj | y)

Page 17: 3 naive Bayes - Virginia Tech

Bernoulli Maximum Likelihood

• p(y) ∏j p(xj | y)

• p(Y = y) ← (# examples where Y = y) / (# examples)

• p( Xj = xj | y) ← (# ex. where Y = y and Xj = xj) / (# ex. where Y=y)

• Learning by counting!

Page 18: 3 naive Bayes - Virginia Tech

Breaking Maximum Likelihood• Happy: “Great!”

Happy: “Had a great day”Sad: “:-( Bad day”Sad: “:-(”

• ???: “Had a bad day :-(”

• p(y) = 0.5

• p(happy | …) ∝ 0.5 x 0.0 …

p(sad | …) ∝ 0.5 x 1.0 x 0.0 x …

great had a bad day :-(1 0 0 0 0 01 1 1 0 1 00 0 0 1 1 10 0 0 0 0 1

great had a bad day :-(Happy 1.0 0.5 0.5 0.0 0.5 0

Sad 0.0 0.0 0.0 0.5 0.5 1.0

Page 19: 3 naive Bayes - Virginia Tech

Breaking Maximum Likelihood• Happy: “Great!”

Happy: “Had a great day”Sad: “:-( Bad day”Sad: “:-(”

• ???: “Had a bad day :-(”

• p(y) = 0.5

• p(happy | …) ∝ 0.5 x 0.0 …

p(sad | …) ∝ 0.5 x 1.0 x 0.0 x …

great had a bad day :-(1 0 0 0 0 01 1 1 0 1 00 0 0 1 1 10 0 0 0 0 1

great had a bad day :-(Happy 1.0 0.5 0.5 0.0 0.5 0

Sad 0.0 0.0 0.0 0.5 0.5 1.0

Page 20: 3 naive Bayes - Virginia Tech

Fixing Maximum Likelihood

• p(Z = z) ← (# examples where Z = z + ɑ) / (# examples + 2ɑ)

• E.g., ɑ = 1

• ɑ vanishes as # of examples grows toward infinity

• When # is small, ɑ prevents 1.0 or 0.0 estimates

Page 21: 3 naive Bayes - Virginia Tech

Breaking Maximum Likelihood• Happy: “Great!”

Happy: “Had a great day”Sad: “:-( Bad day”Sad: “:-(”

• ???: “Had a bad day :-(”

• p(y) = 0.5

• p(happy | …) ∝ 0.5 x 0.25 x 0.5 x 0.5 x 0.25 x 0.5 x 0.75

p(sad | …) ∝ 0.5 x 0.75 x 0.25 x 0.25 x 0.5 x 0.5 x 0.75

great had a bad day :-(1 0 0 0 0 01 1 1 0 1 00 0 0 1 1 10 0 0 0 0 1

great had a bad day :-(Happy 0.75 0.5 0.5 0.25 0.5 0.25

Sad 0.25 0.25 0.25 0.5 0.5 0.75

= 0.0029

= 0.0044

2 + 12 + 2 (1)

= 3/4

Page 22: 3 naive Bayes - Virginia Tech

Maximum a Posteriori• Bernoulli: p(Z | θ) = θZ (1 - θ)(1-Z)

• Maximum likelihood: θ ← arg maxθ’ p(Z | θ’)

• Maximum a posteriori = maximize posterior: θ ← arg maxθ’ p(θ’ | Z)

• p(θ’ | Z) = p(Z | θ’) p(θ’) / p(Z)

• MAP: θ ← arg maxθ’ p(Z | θ’) p(θ’)

• Previous trick equiv. to setting p(θ’) to a Beta distribution

Page 23: 3 naive Bayes - Virginia Tech

Continuous Data

• Conditional feature independence with continuous data?

• E.g., use normal distribution for p(xj | y)

• p(y) is the same as before

• p(xj | y) is the MLE for univariate normal

Page 24: 3 naive Bayes - Virginia Tech

Log Tricks• Each p(xj | y) is in [0,1]

• Multiplying d of them quickly goes to numerical zero

• E.g., 0.9256 = 1.932334983E-12

• Instead, use log probabilities: log ∏j p(xj | y) = ∑j log p(xj | y)

• E.g., log 0.9256 = 256 log 0.9 = -11.71

Page 25: 3 naive Bayes - Virginia Tech

Summary

• Probabilistic identities

• Independence and conditional independence

• Naive Bayes

• Log tricks