A machine reports two continuous sensor readings, and an engineer wants the probability that it is running in operating mode A rather than mode B. Binary word features suited the naive Bayes example of the previous lesson, but continuous readings need a density for each mode, and the two readings may vary together. Gaussian discriminant analysis gives each class a multivariate Gaussian density and applies Bayes’ rule. This lesson shows why one shared covariance matrix leads to a straight boundary, why separate covariance matrices allow a curved one, and what the extra flexibility costs in data.
Key terms
Class-conditional density
Definition For continuous features, the class-conditional density of class is the probability density of the feature vector given , so that for every region of feature space. With class priors for , the posterior probability of class at is
wherever the denominator is positive. A density value is not a probability. It can be larger than , and the probability of any single exact vector is .
In plain words Each class gets its own description of where its feature vectors tend to fall. Bayes’ rule compares how densely each class populates the neighborhood of , weighted by how common the class is. Comparing densities works because the small neighborhood around has the same size for every class, so its size cancels from the ratio.
Example Two dimensionless, preprocessed sensor readings describe each machine. Four constructed training machines in mode A read , , and , and four in mode B read , , and . The Gaussian densities fitted in the cards below give the reading the values and . With equal priors,
Multivariate Gaussian distribution
Definition A random vector in has the multivariate Gaussian, or normal, distribution with mean vector in and a symmetric positive-definite covariance matrix if it has the density
where , for every in . Then and the covariance matrix of is . Its diagonal entries are the variances of the individual features, and its off-diagonal entries are the covariances, which say how deviations of two features move together. The number is the squared Mahalanobis distance from to . The density is constant on each set where is constant, an ellipse for and an ellipsoid in general, with axes along the eigenvectors of .
In plain words The mean is the center of the cloud of points, and the covariance matrix sets its spread and its tilt. Mahalanobis distance measures how far a point lies from the center in units of the spread in that direction, so a step along a direction of high variance counts for less than the same step along a direction of low variance. Probabilities come from integrating the density over a region. The density itself can exceed , and for the two-dimensional Gaussian with mean and covariance it is about at the center.
Example The Gaussian fitted to mode B has
The points and both lie at Euclidean distance from the mean. Their squared Mahalanobis distances are and , so their densities are and , compared with at the mean. The second point is much less typical of mode B, because the second sensor varies much less.
Positive-definite covariance matrix
Definition A real symmetric matrix is positive definite if for every nonzero vector in , which holds exactly when all eigenvalues of are positive. Every covariance matrix is symmetric and positive semidefinite, because . It is positive definite exactly when no nonzero linear combination of the features has variance . A positive-definite matrix is invertible with , which the Gaussian density formula needs. A symmetric matrix with entries , , , is positive definite exactly when and . The maximum likelihood covariance estimate from points in has rank at most , so it is singular whenever .
In plain words Positive definite means the data spread out in every direction. If some direction has no spread at all, the cloud is flat, the density formula would divide by zero, and Mahalanobis distance in that direction has no meaning. Software that inverts such a matrix either fails or returns numbers dominated by rounding error.
Example The diagonal matrix with entries and has those two numbers as eigenvalues, so it is positive definite. If the two sensors were exact copies of each other, each with variance , their covariance matrix would be
with eigenvalues and and determinant . The direction gives , since the difference of the two readings is always . Adding gives eigenvalues and and a positive-definite matrix, but that regularization changes the model, and its size should be chosen on validation data. The symmetric matrix with rows and has eigenvalues and and cannot be a covariance matrix at all.
Gaussian discriminant analysis
Definition Gaussian discriminant analysis is the generative classifier whose class-conditional densities are multivariate Gaussian, with each positive definite, combined with class priors . Taking the logarithm of and dropping the term , which is the same for every class, gives the discriminant score
where is the squared Mahalanobis distance from to under . The classifier predicts the class with the largest , the posterior probabilities are , and with two classes with the sigmoid . The maximum likelihood estimates are , the class mean and
where . Many libraries divide by instead, which changes the numbers slightly.
In plain words The method fits an elliptical cloud to each class and asks which cloud makes a new point most plausible, after weighting by how common each class is. The score has three parts, the prior, a penalty for a widely spread class, and the Mahalanobis distance to the class center. A widely spread class has a lower density near its center but loses density more slowly as points move away.
Example Both modes have four training machines, so . The means are and . The deviations from the mean in mode B are , , and , whose outer products add up to the scatter matrix with diagonal entries and . The cross-products cancel in both modes, so
With the divisor instead of , the diagonals would be and .
Linear discriminant analysis
Definition Linear discriminant analysis, LDA in this lesson and not latent Dirichlet allocation, is Gaussian discriminant analysis with a shared covariance matrix. Every class has with the same positive-definite . The terms and are then common to all classes and drop out, which leaves the linear discriminant
For two classes with
so when the boundary is a hyperplane, a line when . The shared matrix is estimated by the pooled within-class covariance , the sum of all within-class scatter divided by , which is its maximum likelihood estimate. Dividing the scatter by instead gives the usual unbiased version. However many classes there are, the shared matrix has free entries.
In plain words LDA assumes every class has a cloud of the same shape and orientation, only centered in different places. The quadratic parts of the scores are then identical and cancel, so two classes are compared through a single linear combination of the features. Pooling all classes to estimate one matrix keeps the estimate stable with modest data, but it misses real differences in spread. The posterior has the same logistic form as logistic regression, yet its coefficients come from class means and a pooled covariance instead of from maximizing the conditional likelihood, so the two methods usually give different fits to the same data.
Example Pooling the two fitted matrices gives
Then and , so . The boundary is the vertical line , halfway between the means because the priors are equal. The reading gets , and the reading gets .
Quadratic discriminant analysis
Definition Quadratic discriminant analysis, QDA, is Gaussian discriminant analysis with a separate positive-definite covariance matrix for each class. Each discriminant keeps the term , which differs between classes whenever their covariance matrices differ. The difference is then a quadratic function of , and the boundary where it equals is a quadratic surface, in two dimensions a conic section, possibly degenerate, such as an ellipse, a parabola, a hyperbola or a pair of lines. Each has distinct entries estimated from the points of class alone, and its maximum likelihood estimate is singular when . Common remedies are fewer features and shrinkage, for example toward the pooled matrix with , or adding a positive multiple of the identity, with the amount chosen on validation data.
In plain words QDA lets each class keep its own shape, so a widely spread class can claim points far out on both sides of a tight class, and the boundary can bend around the tight class. The flexibility costs data, because every class must supply enough points to estimate its own covariance, and with few points the estimates are noisy and the boundary unstable. Whether QDA predicts better than LDA is an empirical question, settled by comparing both on data not used for fitting.
Example With and from above and equal priors,
The second sensor drops out because both modes have variance there, and comes from the determinants and . At the difference is , so , as in the first card. At it is , giving against under LDA, because a large first reading is far less surprising for the widely spread mode B. Setting the difference to gives , with roots and . QDA therefore assigns mode B to readings with and also to readings with , a boundary of two vertical lines that a single LDA line cannot express. This shows what the covariance assumption does. Eight constructed points cannot show which model predicts better.
Check yourself
- Keep the fitted LDA model, with under equal priors, but suppose the machines in service run in mode B with probability and in mode A with probability , with the class-conditional densities unchanged. Find the new boundary and say which way it moved.
Show the answer
The prior odds of B are , so the score difference gains and becomes . Setting it to gives
The boundary moved from toward the mean of mode A, so the region assigned to the more common mode B grew. The answer assumes that the new prior is right and that the sensor distributions within each mode did not change. A new prior does not correct for drift in the sensors themselves.
- A machine reads . Find the probability of mode B under the fitted LDA model and under the fitted QDA model, both with equal priors.
Show the answer
Under LDA the score difference is , so the reading lies on the boundary and . A rule for ties is needed to give it a label. Under QDA the difference is
so and the equal-cost decision is mode B. The QDA boundary at lies just to the left of this reading.
- A data set has sensors, with training machines in mode A and in mode B. Can QDA be fitted as defined here? Can LDA? What can be done?
Show the answer
QDA needs a positive-definite estimate of each class covariance. The estimate for mode A comes from points in , so its rank is at most , and it is singular. Its inverse and its log determinant do not exist, so QDA as defined cannot be fitted. The pooled estimate for LDA uses the deviations of all machines from their class means and has rank at most , so it can be positive definite, and with sensors that are not exact linear combinations of each other it usually is. LDA estimates covariance entries, while QDA needs , of which the for mode A would come from only points. The options are to use LDA, to shrink the mode A covariance toward the pooled matrix or toward a positive multiple of the identity, or to reduce the number of sensors, with the choice and the amount of shrinkage made on validation data. If two sensors were exact copies, even the pooled matrix would be singular, and one of them would have to be removed or the matrix regularized.