Measuring entropy using AIF360 metrics on Watson machine learning model predictions
By: Ravi Chamarthy
Tags: Machine Learning, Ibm Watson, Entropy

“The increase of disorder or entropy is what distinguishes the past from the future, giving a direction to time.” Stephen Hawking, A Brief History of Time

Quick introduction to ML

So, let’s keep it simple.

Instead of we taking a decision, we have built a machine learning model and asking it to take a decision on our behalf. But, can we trust that decision? Well, why this question first of all. Why because, the so called machine learning model is nothing but a piece of program or code that is trained by us (humans) by teaching this is This and that is That (give loans to this set of population and do not give loans to this set, or issue library card to these set of students and do not issue to this set, or .. well, you see, more such examples). As part of the teaching the model, we (The Humans, To err is Human) can make mistakes knowingly or unknowingly and teach the model wrongly, say biased teaching or training. So it is quite possible that the model can give biased or wrong outcomes.

Before we go any further, typically building machine learning model comprising of following steps:

Gather data and build the training data along with ground truth manual labels

Build the model, which includes

Identify an algorithm to use for the model

Find out the features for the model

Train the model

And finally we have the trained classifier, sorry the model.

To this model we send a payload or records to give predictions.

Lifecycle of machine learning model building

Entropy is the measure of uncertainty

Coming back to the model making wrong predictions, there are two obvious questions — is the model giving “accurate” predictions?, and what is the “uncertainty” in the predictions as compared to the ground truth labels?.

At here, to set the right context of this post, this post does not talk much on the accuracy measurement of the model, but would try to explain the measurement of uncertainty aspect of the model.

Now, let’s try to define what entropy is in the context of machine learning. Imagine the government has launched a new benefit scheme for farmers, and machine learning models being used to classify farmers as who would get benefits from these schemes. A bias would result in either some eligible farmers not getting the benefits (false positives) or some ineligible farmers getting the benefits (false negatives). See, this is like measuring the fairness of the model. On the other hand, entropy here is measure of the disorder or uncertainity of the data — are the outcomes distributed equally among all farmers, and if not, what is the disorder rate. The formula for general entropy is as follows:

Genralized Entropy

For simplicity sake, let’s say the model is predicting two (N) label classes — a positive class (government benefit scheme to be awarded to the farmer) and a negative class (not to be given). So, in the above formula “i” would be either positive (+) or negative (-). And let’s say we have total 100 data points in the data set with 30 belonging to positive class and 70 belonging to negative class. The model predicted these 30, 70 outcomes based on the features it is trained on, and one such feature, let’s say, is Race of the farmer. From an human intuition point of view there is a clear disorder in the predictions between the positive and negative classes — 30 versus 70. And we want to quantify this disorder value.

So yi/ybar is defined as frequentist probability of a class “i” in the data. Meaning, y+ would be 30/100 and y- would be 70/100. And alpha is the parameter that regulates the weight given to distances between values at different parts of the distribution.

So GE(0) = -1/2 * ( ln(0.7) + ln(0.3) ) = -0.5 * (-0.36 -1.2 ) = ~0.78

The entropy here is approximately 0.78. This is considered a high entropy, a high level of disorder. Entropy is measured between 0 and 1. In an ideal prediction distribution we should expect the entropy should be close to be 0.

Measure entropy using IBM AIF360

If you notice here, we manually calculated the entropy value using a formula. To automate these calculations IBM provides an AI fairness toolkit (AIF360 — https://aif360.mybluemix.net) using which we can compute a varied range of fairness measures. And one such measure would be calculating entropy. There are other forms of entropy calculation that can be done using AIF360.

Let’s try to demonstrate entropy detection using AIF360 python utilities. Continuing with the same example of government benefit scheme to farmers, lets encode the race feature as 0, 1, 2, the manual label data and the model predicted label data is as shown in the below table. Here the favorable value is 1 and unfavorable value is 0. If you notice the groups {race:0} and {race:2} for all favorable values and the group {race:1} got all unfavorable values. So clearly there is a disorder in the prediction label among the groups, and this is what we plan to measure using AIF360 among the groups [{race:0}, {race:1}, {race:2}].

Sample data

Following the related code.

Calculating Genralized Entropy using AIF360

The value is almost close of 0.2 which tells that there is disorder of favorable predictions between the groups [{race:0}, {race:1}, {race:2}].

This code is captured here in this notebook: https://github.com/ravichamarthy/AIF360Experiments/blob/master/Entropy%20Identifier.ipynb