









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!










Today we will continue to explore probabilistic approaches to machine learn- ing. First, we will introduce the very popular but simplistic Naive Bayes clas- sifier, in which an extreme independence assumption is made about feature values. Second, we will use EM to develop the Autoclass clustering algorithm for discrete features. Third, we will explore the difference between maximum likelihood (ML), maximum a posteriori (MAP) and full Bayesian learning. Both MAP and a full Bayesian approach incorporate a prior, which can help to avoid overfitting. The tradeoff is added computational complexity, although this can be avoided by adopting conjugate distributions to represent priors. We also offer a brief view into a Bayesian view point on model selection– how complex of a model and thus hypothesis should be adopted in a domain?
A classifier can be constructed by learning a parameterized, joint distribution,
P (X, Y | θ) (1)
on attribute values X and class Y. It is typical to define this as P (X, Y | θ) = P (Y | θ)P (X | Y, θ). Consider a domain with a discrete feature space. In particular, to keep things simple, suppose Boolean attributes X 1 ,... , Xm, such that Xj ∈ { 0 , 1 }, and a binary classification problem such that Y = { 0 , 1 }. Given this, then without any additional assumptions the probabilistic model would require 2(2m^ − 1) + 1 parameters. For each class, there would be a pa- rameter to define, for each distinct feature vector minus one (since they add up to 1 and so one number is redundant), the probability of that feature vector conditioned on the class value. Then, one additional parameter would be re- quired to define the prior that the target class is 1. The number of parameters is O(2m+1) and exponential in the number of attributes. The Naive Bayes classifier avoids this blow-up in the number of parameters, which would preclude learning except with a suitably massive amount of training data, by making a strong independence assumption. Specifically, we assume in the Naive Bayes model (some people call it “stupid Bayes”) that all attributes are independent of each other, conditioned on a target class. Because the Naive
Bayes model has so few parameters it scales well to high dimensions. Of course it is only a reasonable approach if the data in the domain approximates this conditional independence assumption, and this scaling comes at the expense of considering a limited hypothesis space. Typically used for discrete attributes, the Naive Bayes model is valid for attributes that are Boolean, discrete, or continuous. We can treat a continuous attribute as discrete by dividing it into a finite number of ranges, and assuming a uniform density within each range. For each class y, we assume that each pair of features Xi and Xj are condi- tionally independent given y. Mathematically, this means that:
P (Xi, Xj | Y) = P (Xi | Y)P (Xj | Y) (2)
Given this, the joint distribution over the attributes and the class can be decomposed as follows:
P (X 1 ,... , Xm, Y) = P (Y)P (X 1 ,... , Xm | Y) (3)
= P (Y)
∏^ m
j=
P (Xj | Y) (4)
Assume that the attributes and target classes are discrete. Let dom(Xj ) denote the domain of attribute Xj , similarly for dom(Y ). We need to estimate P (Y), and each of the terms P (Xj | Y). P (Y) has |dom(Y )| − 1 independent parameters, while for a class y, P (Xj | Y = y) has |dom(Xj )| − 1 indepen- dent parameters. The total number of independent parameters is therefore |dom(Y )| − 1 + |dom(Y )| ·
∑m j=1(|dom(Xj^ )| −^ 1). Special case: for^ m^ Boolean attributes and a Boolean target class, this is 2 − 1 + 2(m(2 − 1)) = 1 + 2m parameters. Given parameters θ, then classification with a naive Bayes model is very simple. We want to choose the class y ∈ Y that maximizes P (Y = y | X = x, θ) for x = (x 1 ,... , xm). By Bayes’ theorem, and as seen previously, this is just
arg max y∈Y P (Y = y | θ)P (X = x | Y = y, θ) (5)
= arg max y∈Y P (Y = y | θ)
∏^ m
j=
P (Xj = xj | Y = y, θ) (6)
where the second equality comes from the Naive Bayes model. This expression has two components. The first, P (Y = y | θ), is the prior probability of class y, without considering any of the feature values in x. The second component indicates the probability of the particular features x given class y.
In order to gain more understanding of the naive Bayes classifier, let’s look at its hypothesis space. Let’s just consider the case of m Boolean attributes and
The important thing here is not the specific formulas for the weights, but the basic form of the model. Eq. 13 shows that a Naive Bayes classifier is just a lin- ear separator with a particular method to determine weights! It can be compared to other linear separators, for example the perceptron model. When we consider the independence assumption, that different attributes are conditionally inde- pendent of each other given the classification, this is not that surprising. The assumption tells us that different attributes can only influence the classification individually— they cannot be combined in funky ways, as in XOR.
While the Naive Bayes model is very simple, it is very easy to implement, very quick to train, and performs surprisingly well in many domains. Therefore, it is often used as a baseline against which to compare other learning algorithms. The training data D = {(x 1 , y 1 ),... , (xn, yn)}. We will use the maximum likelihood approach to estimate the parameters of the Naive Bayes model. As usual, we want to maximize the log likelihood
L(D, θ) = ln P (D | θ) =
∑^ n
i=
ln P (X = xi, Y = yi | θ). (14)
To keep things simple, we will consider only the case a Naive Bayes classifier with Boolean attributes and a Boolean classification problem. We saw above that the model can be characterized by the parameters
θc = P (Y = T ) (15) θTj = P (Xj = T | Y = T ) (16) θjF = P (Xj = T | Y = F ) (17)
We also saw that if an instance x has label true, then the probability of x is
θc
∏m
j=
(θjT )xj^ (1 − θjT )^1 −xj^. (18)
(Recall that we use 1 and 0 for true and false.) Similarly, if the classification is false, the probability of x is
(1 − θc)
∏^ m
j=
(θFj )xj^ (1 − θFj )^1 −xj^. (19)
Splitting up the data into sets with positive and negative labels, we get the
following expression for the likelihood of the data given parameters θ:
L(D, θ) =
i:yi=T
ln θc +
∑^ m
j=
(xij ln θTj + (1 − xij ) ln(1 − θjT ))
i:yi=F
ln(1 − θc) +
∑^ m
j=
(xij ln θjF + (1 − xij ) ln(1 − θFj ))
To find values of the parameters that maximize this likelihood, we differen- tiate with respect to each of the parameters. Let’s start with θjT. Only the first half of the above expression, for the examples with positive classification, in- volves this parameter. Also, only a single term in the summation over j applies. We get
∂L(D, θ) ∂θjT
i:yi=T
xij θTj
1 − xij 1 − θjT
Equating to zero and solving, we obtain ∑
i:yi=T
(xij (1 − θT ML,j ) − (1 − xij )θT ML,j ) = 0 (22)
Given the data D, let’s define
n = total number of instances NT = number of instances i such that yi is T N (^) jTT = number of instances i such that yi is T and xij = 1 N (^) jTF = number of instances i such that yi is F and xij = 1
We can similarly define NF , N (^) jFT and N (^) jFF , which can be calculated as NF = n − NT , N (^) jFT = N T^ − N (^) jTT and N (^) jFF = N F^ − N (^) jTF. Now we can replace Eq. (22) with
N (^) jTT (1 − θT ML,j ) − N (^) jFT θT ML,j = 0 (23)
Solving, we get
θ MLT ,j =
N (^) jTT N (^) jTT + N (^) jFT
N (^) jTT NT
It is easy to check by inspection that this indeed maximizes the likelihood. Similarly, we find that
θ MLF ,j =
N (^) jTF NF
θjF )(1−xij^ ). An example xi will be ultimately classified to class T if P (Y = T | X = xi) ≥ 0 .5 and to class F otherwise. To run EM on this problem, notice that the solution to the ML problem with known classes is given by equations (24)–(26), and that these depend on the data only through the sufficient statistics as represented by the N -values. To maximize the expected log likelihood in the (M) step, we can compute expected values of these sufficient statistics and substitute them into these same equations. The EM algorithm for this problem becomes
E step: Determine posterior probability γi = P (Y = T | X = xi) for each xi, using θ(old). M step: Using the probabilities γi, compute expected values of each sufficient statistic, and substitute these into Eqs. (24)–(26) to determine θ(new). Then make θ(old)^ ← θ(new).
For example, to compute E[NT ], the expected number of instances with positive classification, this is just
∑n i=1 γi.^ Similarly,^ E[N^
T jT ] is the expected number of instances with positive classification where the jth attribute Xj is true, and is equal to
i:xij =T γi. For completeness, here is more explicit pseudocode for the EM algorithm applied to Naive Bayes, and used for clustering. This is the AutoClass algorithm:
Set θc, θTi and θFi to initial values Repeat until convergence /* Expectation Step */ E[NT ] := 0 E[N (^) jTT ] := 0 for each j E[N (^) jTF ] := 0 for each j For each instance xi pT := θc
j (θ
T j ) xij (^) (1 − θT j ) (1−xij ) pF := (1 − θc)
j (θ
F j )
xij (^) (1 − θF j )
(1−xij ) P (Y = T | X = xi) := (^) pTp +TpF E[NT ] += P (Y = T | X = xi) For each attribute j If (xij = T ) E[N (^) jTT ] += P (Y = T | X = xi) E[N (^) jTF ] += 1 − P (Y = T | X = xi)
/* Maximization Step */ θc := E[N nT^ ] For each attribute j θjT := E[N (^) jTT ] E[NT ]
θjF := E[N (^) jTF ] n−E[NT ] As always with iterative improvement algorithms, local optima may be a problem. We can use standard techniques such as random restarts to deal with this. One point about the initial parameter values: to ensure that progress is made and prevent getting stuck at a bad local solution it is best to start with random, asymmetric parameter values. For example, we can adopt θc = 0.5, and make θTj and θFj be uniform on [0,1].
3 Maximum A Posteriori Method
First, recall that we are generally interested in modeling the distribution
P (X = x | θ), (29)
with parameters determined from the data and then used to perform tasks such as classification. (We could have also included a target value Y here, but leave it out to keep things simple.) In the maximum likelihood (ML) approach, we learn by finding parameters θ that solve
θML = arg max θ P (D | θ) (30)
Although fundamental and valued for its simplicity and power, the ML ap- proach can suffer from overfitting, because it focuses completely on the data D and finds parameters to maximize the observed data. If we have a small amount of data, we can come to some quick conclusions due to small example size. Consider baseball players’ averages at the beginning of the season. After one game, a player might well be “batting 1.000”, meaning that they achieved a hit in every “at bat” in the game. Using a ML approach, we would estimate that the probability they would get a hit in an “at bat” is 1. But clearly, we do not believe that any player is going to hit 1.000 for an entire season. What is happening here is that we have some prior idea about what a base- ball player might hit. We should try to incorporate this prior knowledge when interpreting observed data. What if we also have a prior P (Θ = θ) on param- eters, where Θ is a random variable corresponding to the model parameters, representing all the knowledge about the parameters except for that contained in training data D? How could we incorporate this additional knowledge into our framework? This is where the maximum a posteriori (MAP) approach can be used.
Definition 1 (MAP) In the MAP approach, then given data D and a prior P (Θ) on parameters, the result of training is a parameter vector
θMAP = arg max θ
P (θ | D) (31)
= arg max θ
P (D | θ)P (Θ = θ) (32)
The answer turns out to be the Beta distribution. The Beta distribution has two parameters α, β > 0, and is written
P (Θ = θ) = Beta(Θ = θ | α, β) =
θα−^1 (1 − θ)β−^1 , (37)
where Z is the normalization constant to make
θ P^ (Θ^ =^ θ)dθ^ = 1. Parameters α, β are called hyperparameters because they are parameters on the distribution that provides the prior on the parameters in our Bernoulli model that we want to learn. As is appropriate for θ, which recall is the probability of a win in the Bernoulli model of the data, the Beta distribution generates random variables between [0, 1]. This is good! We have E[θ] = (^) αα+β , with the distribution shifting towards 1 for α > β and becoming more peaked as α + β increases. The Beta(1,1) distribution is just the Uniform[0,1] distribution. What is very helpful for our purposes is the conjugate property of Beta with respect to the parameter of the Bernoulli distribution. Recall that
P (Θ = θ | D) ∝ P (D | θ)P (Θ = θ), (38)
and substituting (35) and (37) we have
P (Θ = θ | D) ∝ θNt^ (1 − θ)NF^ θα−^1 (1 − θ)β−^1 = θNt+α−^1 (1 − θ)NF^ +β−^1 , (39)
and so we have
P (θ | D) = Beta(Θ = θ | NT + α, NF + β) (40)
This is the conjugate property in action! We can now interpret the hyper- parameters α and β, so that α is 1 + the number of prior observations of x = 1 and β is 1+ the number of prior observations of x = 0. For Beta(1,1), which is a uniform prior on θ, this is interpreted as having seen zero prior observations. We can now solve for θMAP. For this, it is easy to check that the mode of Beta(α, β), i.e. the θ for which P (Θ = θ) = Beta(Θ = θ | α, β) is maximized is α− 1 α+β− 2. This is exactly what we need to know to find the MAP estimate for^ θ, and so:
θMAP = arg max θ
P (Θ = θ | D) = α + Nt − 1 α + β + n − 2
Let us suppose that the prior in the soccer example is P (Θ = θ) = Beta(Θ = θ | 5 , 3), which can be interpreted as though more wins than losses have been observed in the past. Given this, we solve and find:
θMAP =
This is more optimistic than the ML estimate because the prior is more optimistic than suggested by the additional three examples.
Consider again the simple example of data D = {x 1 ,... , xn}, with scalar xi ∈ R, and the idea of fitting a univariate Gaussian model. We have already seen that the maximum likelihood approach provides mean and variance:
μML =
n
∑^ n
i=
xi (43)
and
σML^2 =
n
∑^ n
i=
(xi − μML)^2 (44)
We will now find a MAP estimate of the mean μ, assuming for simplicity that σ^2 is already known. (This can also be derived as a MAP estimate but it is a bit messy.^1 ) So, the parameter to learn is just θMAP = μMAP. Given data D, the likelihood is
P (D | μ) =
∏^ n
i=
P (X = xi | μ) =
(2πσ^2 )n/^2
exp
2 σ^2
∑^ n
i=
(xi − μ)^2
Looking for the conjugate property, we seek a prior on parameter Θ (repre- senting the mean of the Normal distribution defining the likelihood of data) for which the posterior on Θ, given some data, has the same functional form as the prior. In this case, the appropriate distribution for the conjugate prior is itself a Gaussian prior (hence the title of this subsection.) We adopt prior:
P (Θ = θ) = N (Θ = θ | μ 0 , σ^20 ) (46)
where μ 0 , σ^20 are the hyperparameters. This provides the conjugate property because the likelihood function is an exponential of a quadratic form in μ, and so if we choose a prior P (Θ) given by a Gaussian, the corresponding posterior on the parameter will itself be a product of two exponentials of quadratic functions of μ, and also Gaussian. By algebra, we have:
P (Θ = θ | D) = P (D | θ)N (Θ = θ | μ 0 , σ^20 ) = N (Θ = θ | μn, σ n^2 ), (47)
where
μn =
σ^2 nσ^20 + σ^2
μ 0 +
nσ 02 nσ^20 + σ^2
μML (48)
σ^2 n = σ^2 σ 02 nσ^20 + σ^2
(^1) It is possible to do the same analysis to obtain a MAP estimate of the variance σ (^2) MAP on the parameterized model of data D. The details are beyond the scope of this course, but one adopts the Gamma distribution for the prior on the precision, which is λ ≡ (^) σ^12 because Gamma is conjugate to the precision of a Gaussian.
Based on this, the ML approach selects weights to solve, ∑^ n
i=
(yi − f (xi, w))^2 , (54)
and so seek to minimize squared empirical error. We now see something else interesting. When we compare the ML solu- tion to the MAP solution we obtain a justification for a particular method of regularization, in which overfitting to a very complex hypothesis is avoided. For this we adopt a prior on the weights w. The appropriate conjugate prior is a multivariate Gaussian with mean just the vector of zeros,
P (W = w | α) = N (W = w | 0 , α−^1 I), (55)
with hyperparameter α > 0 representing the precision of this Gaussian prior, so that the entry along the diagonal in the covariance matrix is 1/α = σ^2 (matrix I is the identity matrix with 1’s on the diagonal and 0’s everywhere else.) For a large enough α, this prior on the weights represents knowledge, before seeing the data, that weights are likely to be small with zero mean and some variance around zero. Given this, the MAP estimate is
θMAP = arg max θ P (Θ = θ | D) = arg max θ P (D | θ)P (Θ = θ) (56)
Focusing on the MAP estimate of weights w, and making parameter β and hyperparameter α explicit, we seek to maximize
P (W = w|D, α, β) ∝ P (D | W = w, β)P (W = w | α) (57)
Evaluating the density function for the multivariate Gaussian that defines the prior on weights, we have
P (W = w | α) = N (W = w | 0 , α−^1 I) =
( (^) α 2 π
) m 2 + exp
α 2
wT^ w
where we use the fact that determinant |α−^1 I| = (^) αm^1 +1. We can now substitute this expression along with Eq. (53) for P (D | W = w, β) into Eq. (57), and taking logs and ignoring terms that don’t depend on w, we have
β 2
∑^ n
i=
(yi − f (xi, w))^2 + αwT^ w (59)
Maximizing this is equivalent to the following minimization problem:
wMAP = min
∑^ n
i=
(yi − f (xi, w))^2 +
α β
wT^ w
and we see this is exactly the method of regularized least squares, where the goal of finding a polynomial to minimize the total squared error is tempered by the goal of avoiding weights that are too large!
The effective regularization parameter is λ = α β , and we see that as the prior on w increases in strength, α and so λ increases (preferring less complexity), and as the noise on the data increases and so β decreases then λ increases (preferring less complexity). We see that by adopting MAP and an explicit prior on weights we have an interpretation for the regularization approach that is popular for fitting higher degree polynomials to data. It happened here by adopting a prior that placed more probability on weights that are close to zero than away from zero.
4 Full Bayesian Learning
In a fully Bayesian approach we do not make the same heavy commitment towards a single set of parameter values that we see in both ML and MAP. All parameter values are considered possible, even after training. No single set of parameter values is selected. Instead, a probability distribution is maintained over the set of parameter values. The Bayesian approach starts as in the MAP approach, by considering
P (Θ = θ | D) =
P (D | θ)P (Θ = θ) (61)
where Z is a normalizing factor and equal to
θ
P (D | θ)P (Θ = θ)dθ. (62)
Because the normalizing factor contains an integration over all parameter values, it can be difficult to compute. After seeing n examples, we want to be able to predict the probability of further examples. In the full Bayesian approach, this also requires an integration over possible parameter values:
P (X = x | D) =
θ
P (X = x, Θ = θ | D)dθ (63)
θ
P (X = x | Θ = θ, D)P (Θ = θ | D)dθ (64)
θ
P (X = x | Θ = θ)P (Θ = θ | D)dθ (65)
The first equality is due to the addition rule of probability, the second the product rule, and the third recognizes that x is conditionally independent of D, when conditioning on θ. Again, this integration can be difficult to perform in general. Similarly, if our goal is to classify some new instance x, then we compute the probability of class value y as
P (Y = y | X = x, D) =
θ
P (Y = y | X = x, Θ = θ)P (Θ = θ | D)dθ (66)
5 Bayesian Model Fitting
One recurring theme has been one of the complexity of a hypothesis space and the possibility of over-fitting. For example, in a clustering problem we might ask how it would be possible to determine the right number of clusters K automatically? For K-means, we could do something heuristic and compare the squared error as K is increased from 2, 3, 4, ... 10, ... and look for a value of K at which the marginal returns are decreasing (or a “knee” in the curve.) Of course we know that if K gets very large the error goes to zero because every example is its own cluster! For the mixture-of-Gaussians and Naive Bayes methods, coupled with max- imum likelihood estimation, a similar approach would compute the likelihood P (D | θML) for the ML parameters under different numbers of components in the model. But we can in fact do a little better by thinking about this from a Bayesian perspective. This at least adds some conceptual clarity even if it is not always a tractable approach. Suppose that we have a collection of models {Mℓ} where a single model Mℓ specifies
Given this, we can look to evaluate the following quantity
P (M = Mℓ | D) ∝ P (D | M = Mℓ)P (M = Mℓ) (74)
where the first term is evaluated as,
P (D | M = Mℓ) =
θ
P (D | Θ = θ)P (Θ = θ | M = Mℓ)dθ, (75)
and is called the model evidence. The second term P (M = Mℓ) allows for a prior on models, for example it would allow for a model in which the number of components is larger to be given less prior probability. In the above, we adopt M for the random variable denoting the model from which the data is generated. Given this, a simplistic (but interesting) approach is ignore the possibility for an informed prior on models (and just assume P (M = Mℓ) is uniform), and look for a model to maximize the model evidence P (D | M = Mℓ). Roughly speaking, this makes a tradeoff between the probability of the data given the parameters (the likelihood of the data) and the probability of the parameters given the model (the likelihood of the parameters.) A complex model, that allows for a wide range of parameterizations and hypotheses, will tend to increase P (D | Θ = θ) while decreasing P (Θ = θ | M = Mℓ). Thus, we see that consulting the model evidence allows for a principled approach to deciding on the appropriate model; for clustering this would allow the number of clusters to be decided.
Under certain assumptions (beyond the scope of this course) the model evi- dence can be approximated to the Bayesian Information Criterion,
BIC (D, Mℓ) = ln P (D | Θ = θMAP) − log n 2
where θMAP is the MAP point estimate given the model, n the number of examples, and |Mℓ| the number of parameters for the model.