Skip to content Skip to sidebar Skip to footer

Implementation Of Theil Inequality Index In Python

I am trying to implement Theil's index (http://en.wikipedia.org/wiki/Theil_index) in Python to measure inequality of revenue in a list. The formula is basically Shannon's entropy,

Solution 1:

If I understand you correctly, the formula you are trying to implement is the following:

enter image description here

In this case, your problem is calculating the natural logarithm of Xi / mean(X), when Xi = 0.

However, since that has to be multiplied by Xi / mean(X) first, if Xi == 0 the value of ln(Xi / mean(X)) doesn't matter because it will be multiplied by zero. You can treat the value of the formula for that entry as zero, and skip calculating the logarithm entirely.

In the case that you are implementing Shannon's formula directly, the same holds:

enter image description here

In both the first and second form, calculating the log is not necessary if Pi == 0, because whatever value it is, it will have been multiplied by zero.

UPDATE:

Given the code you quoted, you can replace x_i*log(x_i) with a function as follows:

defGroup_negentropy(x_i):
    if x_i == 0:
        return0else:
        return x_i*log(x_i)

defH(x)
    n = len(x)
    entropy = 0.0sum = 0.0for x_i in x: # work on all x[i]print x_i
        error_if_not_in_range01(x_i)
        sum += x_i
        group_negentropy = Group_negentropy(x_i)
        entropy += group_negentropy
    error_if_not_1(sum)
    return -entropy

Post a Comment for "Implementation Of Theil Inequality Index In Python"