Suppose there are N outcomes from x1, to xn, each with P(x_i) of happening. What is the gini index/coefficient that summarizes the level of inequality of the population that is described by this discrete random variable?

ff_dist_gini_random_var(ar_x, ar_prob_of_x, bl_sort_sum = TRUE)

Arguments

ar_x

array of sclaar values x1, x2, ..., xn-1, xn, sorted.

ar_prob_of_x

array of probability mass that sums to one for sequentially each element of ar_x.

bl_sort_sum

boolean if true then finds the unique values of ar_x, sort it, and sum up the probabilities contained in ar_prob_of_x for each one of the unique values in ar_x

Value

a scalar value of the gini coefficient

Author

Fan Wang, http://fanwangecon.github.io

Examples

# already sorted example
for (fl_binom_success_prob in seq(0.0001,0.9999,length.out=10)) {
    ar_x <- seq(0, 100, by=1)
    ar_prob_of_x <- dbinom(ar_x, 100, fl_binom_success_prob)
    fl_gini_index <- ff_dist_gini_random_var(ar_x, ar_prob_of_x, bl_sort_sum=FALSE)
    st_print <- paste0('binom p(success)=', fl_binom_success_prob ,
                       ', the fl_gini_index=', fl_gini_index)
    print(st_print)
}
#> [1] "binom p(success)=1e-04, the fl_gini_index=0.990048846889393"
#> [1] "binom p(success)=0.111188888888889, the fl_gini_index=0.147061101509638"
#> [1] "binom p(success)=0.222277777777778, the fl_gini_index=0.0989942681255604"
#> [1] "binom p(success)=0.333366666666667, the fl_gini_index=0.0753059593394789"
#> [1] "binom p(success)=0.444455555555556, the fl_gini_index=0.0596495299535176"
#> [1] "binom p(success)=0.555544444444444, the fl_gini_index=0.0476678493269222"
#> [1] "binom p(success)=0.666633333333333, the fl_gini_index=0.0375168214334586"
#> [1] "binom p(success)=0.777722222222222, the fl_gini_index=0.0280646430085938"
#> [1] "binom p(success)=0.888811111111111, the fl_gini_index=0.0180312757603542"
#> [1] "binom p(success)=0.9999, the fl_gini_index=9.90197372312816e-07"
# Example 2a, unsorted
set.seed(123)
ar_x <- c(1,2,3,5,2,1,3)
ar_prob_of_x <- runif(length(ar_x))
ar_prob_of_x <- ar_prob_of_x/sum(ar_prob_of_x)
ff_dist_gini_random_var(ar_x, ar_prob_of_x, bl_sort_sum=TRUE)
#> [1] 0.1974732
# Example 2b, sorted same
ar_x_sort <- c(1,2,3,5)
ar_prob_of_x_sort <- c(0,0,0,0)
ar_prob_of_x_sort[1] <- ar_prob_of_x[1] + ar_prob_of_x[6]
ar_prob_of_x_sort[2] <- ar_prob_of_x[2] + ar_prob_of_x[5]
ar_prob_of_x_sort[3] <- ar_prob_of_x[3] + ar_prob_of_x[7]
ar_prob_of_x_sort[4] <- ar_prob_of_x[4]
ff_dist_gini_random_var(ar_x_sort, ar_prob_of_x_sort, bl_sort_sum=FALSE)
#> [1] 0.1974732
ff_dist_gini_random_var(ar_x_sort, ar_prob_of_x_sort, bl_sort_sum=TRUE)
#> [1] 0.1974732