Skip to content

Instantly share code, notes, and snippets.

@ZacLN
Created January 28, 2017 10:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ZacLN/fd34052b6a17ea9274f1e92cf82d6472 to your computer and use it in GitHub Desktop.
Save ZacLN/fd34052b6a17ea9274f1e92cf82d6472 to your computer and use it in GitHub Desktop.
using Distributions, KernelDensity, QuadGK
function gini(x)
n = length(x)
xx = sort(x)
2*(sum(collect(1:n).*xx))/(n*sum(xx))-1
end
gini(d::Exponential) = 0.5
gini(d::Beta) = (2/d.α)*beta(d.α+d.β, d.α+d.β)/(beta(d.α,d.α)*beta(d.β, d.β))
gini(d::Gamma) = gamma(d.α+1/2)/(d.α*gamma(d.α)*sqrt(π))
gini(d::LogNormal) = erf(d.σ/2)
gini(d::Pareto) = d.α ≥ 1 ? 1/(2*d.α - 1) : 1.0
gini(d::Uniform) = (d.b-d.a)/(3*(d.b-d.a))
function gini(d::Distribution)
ul = 0.1
while cdf(d, ul)< 0.999
ul*=2
end
mean(d)*QuadGK.quadgk(x->cdf(d, x)*(1-cdf(d, x)), eps(Float64), ul)[1]
end
function atkinson(x::Vector,e::Real)
at =0.0
mu = mean(x)
N = length(x)
if e<0
error("e<0")
elseif e ==1
at = 1 -(1/mu)*(prod(x))^(1/N)
else
at = 1 -(1/mu)*(1/N*sum(x.^(1-e)))^(1/(1-e))
end
return at
end
function atkinson(d::Distribution, e)
ul = 0.1
while cdf(d, ul)< 0.999
ul*=2
end
m = mean(d)
1-1/m*QuadGK.quadgk(x->x^(1-e)*pdf(d, x), eps(Float64), ul)[1]^(1/(1-e))
end
theil(x) = 1.0/length(x)*sum((x/mean(x)).*log.(x/mean(x)))
theil(d::LogNormal) = d.σ^2/2
function atkinson(d::Distribution, e)
ul = 0.1
while cdf(d, ul)< 0.999
ul*=2
end
m = mean(d)
QuadGK.quadgk(x->x/m*log(x/m)*pdf(d,x), 0,ul)[1]
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment