foundations of computational agents
A common learning task is supervised learning, where there is a set of examples, described by features, which are partitioned into input features and target features. The aim is to predict the values of the target features from the values of the input features for each possible example.
A feature is a function from examples into a value. If is an example, and is a feature, is the value of feature for example . The domain of a feature is the set of values it can return. Note that this is the range of the function defining the feature, but becomes the domain of a function that takes features and makes predictions. A Boolean feature is one with domain .
A feature has exactly one value for each example. Suppose an example is a news article. The topic of an article can be a feature, with domain the set of possible topics (perhaps including “none” if it is possible there is no topic), if each article has a unique topic. If each article can have multiple topics, topic would not be a feature. Instead, there could be a Boolean feature for each possible topic. Alternatively, there could be a feature that maps each news article to the set of topics it covers.
In a supervised learning task, the learner is given
a set of input features,
a set of target features,
a bag of training examples, where each example is a pair , where is a tuple of a value for each input feature and is a tuple of a value for each target feature.
The output is a predictor, a function that predicts s from s. The aim is to predict the values of the target features for examples that the learner has not seen. For this book, consider only a single target feature, except where explicitly noted.
Supervised learning is called regression when the domain of the target is (a subset of) the real numbers. It is called classification when the domain of the target is a fixed finite set, for example, the domain could be Boolean , clothing sizes XS, S, M, L, XL, , or countries of birth (countries, or None for those people who were born outside of any country). Other forms of supervised learning include relational learning, such as predicting a person’s birth mother when test examples might be from a different population of people from training examples, and structured prediction, such as predicting the shape of a molecule.
Some examples of applications of supervised learning are
a smart watch predicting the activity of the wearer (e.g., sleeping, sitting, walking, running, driving) from their heart rate and movement
predicting how likely a location will be to flood in the next decade or century, based on the local topography, climate, geology, and land use, which might be used for insurance or for a decision about whether to build in the location
predicting the words that someone may have hand-written in a phone or tablet based on their writing, perhaps taking the order of the strokes into account
in a machine translation system, predicting Indonesian text that corresponds to some Swahili text (where the training examples might include text in other languages).
Before tackling sophisticated applications such as these, you need to understand the basics.
Figure 7.1 shows training examples typical of a classification task. The aim is to predict whether a person reads an article posted to a threaded discussion website given properties of the article. The input features are , , , and . There is one target feature, . The domain of is , the domain of is , and so on.
There are eighteen training examples, each of which has a value for all of the features. In this dataset, , , and .
There are two new cases, and , for which the model needs to predict the user action.
Example | ||
---|---|---|
0.7 | 1.7 | |
1.1 | 2.4 | |
1.3 | 2.5 | |
1.9 | 1.7 | |
2.6 | 2.1 | |
3.1 | 2.3 | |
3.9 | 7 | |
2.9 | ? | |
5.0 | ? |
Figure 7.2 shows some data for a regression task, where the aim is to predict the value of feature on examples for which the value of feature is provided. This is a regression task because is a real-valued feature. Predicting a value of for example is an interpolation problem, as its value for the input feature is between two of the values of the training examples. Predicting a value of for example is an extrapolation problem, because its value is outside the range of the training examples.
Suppose is a bag of examples and is a target feature. Given example , the actual value of for , the ground truth, is . is the number of examples in .
A predictor for target feature is a function from the domains of the input features into the domain of . A predictor for is written as , so is the predicted value for target feature on example . can only use the input features of an example for its prediction. is built using examples in , but should be applicable for all possible examples.
A point estimate for target feature on example is a prediction of that is a number if is real or Boolean, or can be a vector if has a finite set of possible values or is vector-valued. For example, if is Boolean, or has domain , a point prediction could be 1 or 0.7 or even 1.3 (although 1.3 would never be a good prediction). An example of a prediction that is not a point estimate is the prediction that the value of real-valued variable lies between 1.7 and 1.8. If is discrete with values red, yellow, and green, the prediction can be a vector containing a number for each of the three values.
The loss for example on feature is a measure of how close the prediction is to the actual value . The measures below define a nonnegative real-valued function that gives the loss for prediction on an example when the actual value is .
A common error function on a dataset is the mean loss, which for predictor on a dataset is
where is the number of examples in . An alternative error is the sum of losses (without ). When finding a predictor to minimize the error, either the mean or sum can be used, because they are a minimum for the same predictors. When implementing an algorithm, it is important to check whether the mean or the sum is used, particularly when this value is added to another one. The mean loss is typically reported as the error for a dataset because it can be interpreted without knowing the number of examples.
Real-valued features are used when the values are totally ordered, and the differences are meaningful. This covers cases where the values are all integers as well as cases where the values can be arbitrary reals. For example, height in centimeters, student marks, and number of children could all be real-valued features. Clothing sizes, when mapped to numbers, could also be considered real-valued.
For regression, when the target feature is real-valued, when both the actual and the prediction are numbers, the following losses are common:
The 0–1 loss, or L0 loss, has
This is sometimes written as , where is a function from Booleans into , defined by and .
The mean 0–1 loss of a dataset is the average number of predictions that are wrong. It does not take into account how wrong the predictions are, just whether they are correct or not. The accuracy of a predictor on a dataset is one minus the mean 0–1 loss, which is the number of correct predictions divided by the number of examples. Accuracy is maximized when 0–1 loss is minimized.
Testing equality for real or floating-point numbers is unreliable. This means that 0–1 loss may not be appropriate for the prediction of the mean, but is still applicable for predicting the mode or the median.
The absolute loss, or L1 loss, is
the absolute difference between the predicted and actual value. This is always nonnegative. The mean absolute loss of a dataset is only zero when the predictions exactly fit the observed values. Unlike for the 0–1 loss, close predictions are better than far-away predictions.
The squared loss, or L2 loss, is
This measure treats large losses as much worse than small losses. For example, a loss of 2 on an example is as bad as 4 losses of 1, and a loss of 10 on one example is as bad as 100 losses of 1. Minimizing mean squared loss is equivalent to minimizing the root-mean-square (RMS) error, the square root of the mean squared loss, because the square root function is a monotonically increasing function on nonnegative values.
Sometimes you might see squared loss as . The does not affect the minimization, but makes some calculations simpler (in particular, when taking derivatives).
The worst-case loss, or L loss, on examples is the maximum absolute difference
In this case, the learner is evaluated by its worst prediction. This is the only error covered that is not a mean or sum.
Consider the data of Figure 7.2. Figure 7.3 shows a plot of the training data (filled circles) and three lines, , , and , that predict the -value for all points. minimizes the mean absolute loss, minimizes the mean squared loss, and minimizes the mean worst-case loss of the training examples.
As no three points are collinear, any line through any pair of the points minimizes the 0–1, , loss.
Lines and give similar predictions for ; namely, predicts 1.805 and predicts 1.709, whereas the data contain a data point . predicts 0.7. They give predictions within 1.5 of each other when interpolating in the range . Their predictions diverge when extrapolating from the data. and give very different predictions for .
An outlier is an example that does not follow the pattern of the other examples. The difference between the lines that minimize the various error measures is most pronounced in how they handle outliers. The point can be seen as an outlier as the other points are approximately in a line.
The prediction with the lowest worse-case loss for this example, , only depends on three data points, , , and , each of which has the same worst-case loss for prediction . The other data points could be at different locations, as long as they are not farther away from than these three points.
A prediction that minimizes the absolute loss, , does not change as a function of the actual -value of the training examples, as long as the points above the line stay above the line, and those below the line stay below. For example, the prediction that minimizes the absolute loss would be the same, even if the last data point was instead of .
Prediction is sensitive to all of the data points; if the -value for any point changes, the line that minimizes the squared loss will change. Changes to outliers will have more effect on the line than changes to points close to the line.
A categorical feature is one where the domain is a fixed finite set. Examples include the main topic of a news article (when each news article only has one main topic), the species of an animal, the country of birth of a person (perhaps with the extra value for those who were not born in a country), or the letter of the alphabet for a hand-written letter.
Suppose target variable is categorical with domain . A point estimate is one of the following two forms:
A definitive prediction where the prediction is one of .
A probabilistic prediction , a function or dictionary that maps the domain into nonnegative real numbers such that , where is the value that prediction makes for value . Thus a probabilistic prediction makes a nonnegative prediction for each value in the domain, and the sum of the predictions is 1.
A definitive prediction of is equivalent to the probabilistic prediction with and the other numbers in the probabilistic prediction are all 0.
Whether a variable is real-valued or categorical is often a matter of data design – how the data is represented.
A trading agent wants to learn a person’s preference for the length of holidays. The holiday can be for 1, 2, 3, 4, 5, or 6 days. See Figure 7.4 for example data. Note that there are no input features, and one output feature in this example.
Example | |
---|---|
1 | |
6 | |
6 | |
2 | |
1 |
One representation is to treat the target as a real-valued variable that is the number of days in the holiday. A prediction for a new example can be any real number, such as .
The target could also be treated as categorical with domain . A definitive prediction for example might be the prediction . A probabilistic prediction for example might be the prediction represented by the dictionary with
which means that it is predicting the holiday is of length 1 with probability 0.25, length 2 with probability 0.2, and so on.
The losses used for real-valued target features can be applied to categorical features by using for an actual . For example, if the actual is , the squared loss of probabilistic prediction is .
It is common to report mean accuracy. Accuracy for a definitive prediction is 1 if the prediction is correct, and 0 otherwise. For probabilistic predictions, the accuracy is 1 when there is a unique mode (the with the highest corresponding ) that corresponds to the actual value. Accuracy is a crude measure of how accurate probabilistic predictions are, as it only depends on the mode.
A probabilistic prediction is typically optimized with categorical log loss or categorical cross entropy, often just known as log loss, where the loss for prediction for actual is
That is, when the actual is , log loss selects the corresponding prediction, , and returns its negative logarithm.
The log loss is always greater than or equal to 0. It is only 0 when and all other probabilities are 0.
For categorical and dataset , predictor has mean log loss
where is the value of the prediction evaluated for the actual for example .
The probabilistic prediction from Example 7.4 for the dataset of Figure 7.4, with four data points (, , , and ) having the prediction 0.25, and one () having the prediction 0.2 has mean log loss (where the logarithm is base 2):
The log loss of the prediction with and , with the others 0, has mean log loss (base 2)
Thus, the second prediction, which is using the proportion of the data for each value, is better than the other prediction.
There are two main justifications for log loss; one is in terms of probability, and one in terms of decision making.
You might want the model that best predicts the data; this is the model where the data is most likely given the model. The likelihood of the examples given model , assuming that the examples are independent, is the product of the prediction for each example:
Taking the logarithm gives the log-likelihood
The mean log loss is negative of the log-likelihood divided by the number of examples. Thus, log loss is minimized when the log likelihood – and so also the likelihood – is maximized.
Mean log loss is appropriate when the prediction is used as a probability for gambling and other reasoning under uncertainty (see Chapter 9). For example, under squared loss, and are very close; a prediction of will have a very similar error to a prediction of . However, as probabilities they are very different. An insurance company making its decisions based on a probability of will lose a lot of money if the correct probability is ; the event will occur 10 times as much as expected. This difference is reflected in the log loss, but not in the losses that only use the differences between the actual and predicted.
The logarithm of 0 is undefined. As gets closer to 0, gets larger. For this reason, log loss of the prediction of 0 is usually treated as infinity. Averaging infinity with any finite set of numbers is infinity. Log loss discourages any probabilistic prediction of 0 for a value that is possible.
Log loss is closely related to the notion of entropy. The log loss (when the base of the logarithm is 2) can be seen as the mean number of bits it will take to encode the data given a code that is based on treated as a probability. The base of the logarithm provides a constant difference, which doesn’t matter when the goal is to minimize the loss. However, when reporting results it is important to specify the base of the logarithm, where both 2 and (the base of the natural logarithm) are common.
Information Theory
A bit is a binary digit. Because a bit has two possible values (0 and 1), it can be used to distinguish two items. Two bits can distinguish four items, each associated with either , , , or . In general, bits can distinguish items. Thus, items can be distinguished with bits (or the smallest integer greater than or equal to this value). It may be surprising, but you can do better than this using probabilities.
Consider this code to distinguish the elements of the set , with , , , and :
This code sometimes uses one bit, sometimes two bits and sometimes three bits. On average, it uses
For example, the string with 8 characters has code , which uses 14 bits.
With this code, bit is required to distinguish from the other symbols. Distinguishing uses bits. Distinguishing or requires bits.
It is possible to build a code that, to identify , requires bits (or the smallest integer greater than this). Suppose there is a sequence of symbols you want to transmit or store and you know the probability distribution over the symbols. A symbol with probability can use bits. To transmit a sequence, each symbol requires, on average,
bits to send, where the sum is over each value in a domain. This is called the information content or entropy of the distribution.
Suppose and are both probability distributions. The expected number of bits to describe using a code optimized for is
This is the cross entropy of distribution relative to . For a fixed , cross entropy is minimized when . In machine learning, is typically the learned model and the distribution of the data. Log loss allows for a separate prediction for each example.
When the domain of is binary, one value can be associated with 0, the other value with 1. For Boolean features, with domain , it is traditional to associate 0 with and 1 with . Boolean variables are very common; for example, predicting the topics of a news article that can have multiple topics can be modeled using a Boolean variable for each topic.
A Boolean target variable can be treated either as a real-valued prediction or as a categorical prediction. The real-valued prediction for variable is equivalent to the categorical prediction where the prediction for 1 is and the prediction for 0 is .
The binary log loss, or binary cross-entropy loss, for prediction and actual value is defined by
and by convention, , even though is undefined (or infinity). Log loss is when and when , and so is the same as categorical log loss where the prediction for value 1 is and the prediction for value 0 is . The term log loss includes both categorical log loss and binary log loss; which is meant should be clear from the context.
A binary point prediction could be any real number or could be restricted to be 0 or 1. Here we assume that the prediction can be any real number, except where explicitly noted.
The simplest case for learning is when there is a single target feature and the input features are ignored (or there are no input features). In this case, a learning algorithm predicts a single value for the target feature for all of the examples. This forms a naive baseline that any more sophisticated algorithm should be able to beat. It is also the base case for some of the learning algorithms.
Suppose is a bag of examples, is a numeric feature, and the learner makes a point estimate, , for all examples.
Loss | Error for prediction | Optimal prediction |
---|---|---|
for training data | for training data | |
mean 0–1 loss | ||
mean absolute loss | ||
mean squared loss | ||
worst case | ||
Special case for domain of is , , . | ||
mean log loss | ||
For categorical with domain , value occurs times in training data, and prediction has . | ||
mean log loss |
The optimal prediction depends on the optimality criterion. Figure 7.5 gives the optimal prediction for various optimality criteria for the case with no input features and a single real-valued feature , where the training set consisting of examples, namely the numbers . The mode is the value that occurs most often. The median is the middle value after sorting the values; if there are two middle values, either of them, or any value between them, will minimize the absolute error. The mean is the sum of the values divided by the number of values. The best worst-case error is the mean of the minimum value and the maximum value. Exercise 7.1 asks about the proofs.
When the target feature has domain , the training examples can be summarized in two numbers: , the number of examples with value 1 and , the number of examples with value 0 (). The mean is the empirical frequency: the proportion of 1s in the training data, namely . This is also the maximum likelihood estimate, and the prediction with the minimum log loss. The 0–1 error and the absolute error are minimized by predicting which of 0 or 1 occurs more often.
When the target feature is categorical with domain , a dataset can be summarized by numbers , where is the number of occurrences of in the dataset. In this case, the prediction that minimizes log loss on the training set is the empirical frequency. The prediction that minimizes 0–1 error and maximizes accuracy is to predict the mode; a value that appears most often.
Consider the length of holiday data of Example 7.4. Suppose only the target is given, and there are no input features, so all of the examples get the same prediction.
In the first representation, the prediction of 1 and the prediction of 6 both minimize 0–1 error and so maximize accuracy on the training data. The prediction that minimizes the absolute loss is 2, with an total absolute loss of 10, and a mean absolute loss of 2. The prediction that minimizes the squared error on the training data is 3.2. The prediction the minimizes the worst-case error is 3.5.
Treating the feature as categorical, the prediction that minimizes log loss in training error is , which is the empirical distribution of the training data.
Thus, which prediction is preferred depends on how the prediction is represented and how it will be evaluated.
This analysis does not specify the optimal prediction for unseen examples. You should not expect the empirical frequency of the training data to be the optimal prediction for new examples when maximizing the likelihood or minimizing the log loss. If one , the corresponding does not appear in the training data. However, if just one test example has value , the likelihood would be 0 (its lowest possible value) and the log loss would be infinite (or undefined). This is an example of overfitting. See Exercise 7.1.
Optimality Criterion | Naive Prediction | Mean Loss |
---|---|---|
mean 0–1 loss | 1 | |
mean absolute loss | 0.5 | 0.5 |
mean squared loss | 0.5 | 0.25 |
worst case error | 0.5 | 0.5 |
mean log loss (base 2) | 0.5 | 1 |
mean log loss (base ) | 0.5 | 0.693… |
Figure 7.6 provides mean losses that any method for binary target domains should beat, as a function of the optimality criterion. The naive prediction is a prediction that can be made before seeing any data. These losses act as a quick check for determining if a method is not working. For example, if the mean squared loss of a predictor for 0/1 variable is greater than 0.25, the method is not working.
Given a dataset, for the optimality criterion used, you should compute the average loss for the optimal prediction on the training set – ignoring the input features – as a baseline to beat. Often you will find such a baseline is difficult to beat by a lot.
Not all errors are equal; the consequences of some errors may be much worse than others. For example, it may be much worse to predict a patient does not have a disease that they actually have, so that the patient does not get appropriate treatment, than it is to predict that a patient has a disease they do not actually have, which will force the patient to undergo further tests, but may result in more anxiety.
Consider a simple case where the domain of the target feature is Boolean (which you can consider as “positive” and “negative”) and the predictions are restricted to be Boolean. One way to evaluate a prediction independently of the decision is to consider the four cases between the predicted value and the actual value:
actual positive () | actual negative () | |
---|---|---|
predict positive () | true positive () | false positive () |
predict negative () | false negative () | true negative () |
A false-positive error or type I error is a positive prediction that is wrong (i.e., the predicted value is positive and the actual value is negative). A false-negative error or type II error is a negative prediction that is wrong (i.e., the predicted value is negative and the actual value is positive). For a given predictor for a given set of examples, suppose is the number of true positives, is the number of false positives, is the number of false negatives, and is the number of true negatives.
Different choices of learning algorithm or parameters can affect the number of false positives and false negatives. Suppose false positives are times as bad as false negatives. means false negatives are worse than false positives, means they are equally as bad, and means false positives are worse than false negatives. Assuming the costs of errors can be added, the cost of a prediction is proportional to
One would then select the predictor with the lowest cost.
Sometimes you want to evaluate a predictor without knowing anything about the relative costs of different types of errors.
The following measures are often used:
The recall or true-positive rate is , the proportion of actual positives that are predicted to be positive.
The false-positive rate is , the proportion of actual negatives predicted to be positive.
An agent should try to maximize the true-positive rate and minimize the false-positive rate; however, these goals are incompatible. An agent can maximize the true-positive rate by making positive predictions about all cases it is sure about (assuming it is correct about these). However, this choice maximizes the false-positive rate because more of the positives will be wrong.
Predictor dominates if has a higher true-positive rate and a lower false-positive rate than . If dominates then has a lower cost (and so is better) than for all cost functions that depend only on the number of false positives and false negatives (assuming the costs to be minimized are additive and nonnegative). See Exercise 7.3.
For a given set of examples where both the prediction and the actual are given, a receiver operating characteristic space, or an ROC space, plots the false-positive rate against the true-positive rate for various predictors. Each predictor becomes a point in the space.
Consider a case where there are 100 examples that are actually positive and 1000 examples that are actually negative . Figure 7.7 shows the performance of six possible predictors for these 1100 examples. Predictor (a) predicts 70 of the positive examples correctly and 850 of the negative examples correctly. Predictor (e) predicts every example as positive, and (f) predicts all examples as negative.
|
|
|
|
|
|
In the ROC space, any predictor lower and to the right of another predictor is dominated by the other predictor. For example, (d) is dominated by (c); there would be no reason to choose (d) if (c) were available as a predictor.
Consider predictions (a) and (c) in Figure 7.7. The true-positive rate of (a) is 0.7 and the false-positive rate is 0.15. Predictor (c) has a true-positive rate of 0.98 and a false-positive rate of 0.2. If false positives were much more important than false negatives, then (a) would be better than (c), as it has fewer false positives. If false negatives were much more important (much worse) than false positives, then (c) would be better than (a), as it has fewer false negatives. Neither dominates the other in the ROC space.
Any predictor that is below the upper envelope of predictors (shown with line segments in Figure 7.7) is dominated by the other predictors. For example, although (a) is not dominated by (b) or by (c), it is dominated by the randomized predictor: with probability 0.5 use the prediction of (b), else use the prediction of (c). This randomized predictor would expect to have 26 false negatives and 112.5 false positives. The line between (b) and (c) reflects all probabilistic mixes of (b) and (c).
For some algorithms, there is a parameter which, when varied, results in predictors with different false-positive and true-positive rates. For example, a model that returns a real value can predict true if the output value is greater than a threshold, and false if the output value is less than the threshold. As the threshold varies, so do the predictions. In such cases, a single algorithm can provide a curve in the ROC space. One algorithm is better than another algorithm if its curve is above and to the left of the other. Often two algorithms have different parts of the curve where they dominate, in which case the preferred algorithm depends on the relative costs of the two types of errors. The area under the ROC curve (AUROC) is a single number that can be used to compare algorithms across the whole parameter space. One predictor having a larger AUROC than another means it is better for some cost functions, but does not imply it is better for all cost functions.
Another common measure is precision, which is , the proportion of positive predictions that are actual positives. Some have suggested comparing algorithms just using precision and recall. However, it is possible that one algorithm might have a better (higher) recall and a better (higher) precision than another but be a worse algorithm for some cost functions. See Exercise 7.3.