Go back to the Main.ipynb
notebook.
We consider a discrete distribution of variables $\{y_i\}_{i=1,\ldots,n}$ with probabilities $\{p_{y_i}\}_{i=1,\ldots,n}$. The variables $\{y_i\}$ are positive and sorted in increasing order ($0<y_i<y_{i+1}$). The Gini coefficient $G$ is then defined as: $$G = 1- \frac{\sum_{i=1}^n p_{y_i}(S_{i-1}+S_{i})}{S_n},$$ where $S_0=0$ and: $$S_i = \sum_{j=1}^i y_jp_{y_j}.$$
The function we define is Gini(ys, pys)
,where ys
corresponds to $\{y_i\}_{i=1,\ldots,n}$, and pys
to $\{p_{y_i}\}_{i=1,\ldots,n}$. The inputs ys
and pys
can be either of type Vector
or Matrix
(but both of the same type).
The input ys
is not required to be sorted.
function Gini(ys::Vector{T}, pys::Vector{T})::T where{T<:Real}
@assert size(ys)==size(pys)
iys = sortperm(ys)
ys .= ys[iys]
pys .= pys[iys]
Ss = [zero(T); cumsum(ys.*pys)]
return one(T) - sum(pys.*(Ss[1:end-1].+Ss[2:end]))/Ss[end]
end
function Gini(ys::Matrix{T}, pys::Matrix{T})::T where{T<:Real}
@assert size(ys)==size(pys)
return Gini(ys[:],pys[:])
end;
The function format(x, prec)
returns the number x
formatted with the precision prec
. If x
is an Integer, no modification is done.
function fmt(x::I, prec::J=2) where{I<:Integer,J<:Integer}
return x
end
function fmt(x::T, prec::J=2) where{T<:Real,J<:Integer}
return round(x,digits=prec)
end;