function y = GiniCoeff(p, w)
% The function computes the Gini Coefficient for populations p associated to wealth w.
% p and w must be vectors of same size, only positive values are allowed for p, only
% non-negative values are allowed for w (with at least one i such that w(i) > 0).
% The Gini Coefficient is a measure of inequality, i.e. a measure of wealth concentration.
%
% http://en.wikipedia.org/wiki/Lorenz_curve
% http://en.wikipedia.org/wiki/Gini_coefficient
% http://en.wikipedia.org/wiki/Theil_index
% http://en.wikipedia.org/wiki/Atkinson_index
%
% -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
% -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
%
% % Example 1: Uniform U(0, 1)
% N = 1000; % Number of populations
% p = rand(N, 1); w = rand(N, 1); % Population and Wealth extracted from a Uniform U(0, 1)
% y = GiniCoeff(p, w)
% y =
%
% 0.3339
%
% -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
% -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
%
% % Example 2: Standard Normal z(0, 1)
% N = 1000; % Number of populations
% p = rand(N, 1); % Population extracted from a Uniform U(0, 1)
% mu = 4;
% w = mu + randn(N, 1); % Wealth extracted from a Normal N(mu, 1)
% w = abs(w); % Be careful: all values must be strictly positive!!!
% y = GiniCoeff(p, w)
% y =
%
% 0.1412
%
% -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
% -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
%
% % Example 3: LogNormal logN(0, 1)
% N = 1000; % Number of populations
% p = rand(N, 1); % Population extracted from a Uniform U(0, 1)
% w = exp(randn(N, 1)); % Wealth extracted from a LogNormal logN(0, 1)
% y = GiniCoeff(p, w)
% y =
%
% 0.5200
%
% -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
% -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
%
% % Example 4: Power Law PL(kappaPL, alphaPL)
% N = 1000; % Number of populations
% p = rand(N, 1); % Population extracted from a Uniform U(0, 1)
% kappaPL = 1;
% alphaPL = 2;
% w = kappaPL * (rand(N, 1) .^ (-1/alphaPL)); % Wealth extracted from a Power Law PL(kappaPL, alphaPL)
% y = GiniCoeff(p, w)
% y =
%
% 0.3317
%
% -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
% -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
%
% % Example 5: Mixture of LogNormal logN(0, 1) and Power Law PL(kappaPL, alphaPL)
% N = 1000; % Number of populations
% p = rand(N, 1); % Population extracted from a Uniform U(0, 1)
% alpha = floor(0.92 * N); % Number of populations whose wealth is extracted from a LogNormal logN(0, 1)
% w = exp(randn(alpha, 1)); % Wealth extracted from a LogNormal logN(0, 1)
% kappaPL = 1;
% alphaPL = 2;
% w((alpha + 1):N) = kappaPL * (rand(N - alpha, 1) .^ (-1/alphaPL)); % Wealth extracted from a Power Law PL(kappaPL, alphaPL)
% y = GiniCoeff(p, w)
% y =
%
% 0.5076
%
% -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
% -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
%
% Written by
% Francesco Pozzi
% 19 May 2008
%
ctrl = isvector(p) & isnumeric(p) & isreal(p) & isvector(w) & isnumeric(w) & isreal(w);
if ctrl
p = p(:);
w = w(:);
ind = ~isnan(p) & ~isinf(p) & ~isnan(w) & ~isinf(w);
p = p(ind);
w = w(ind);
else
error('Population and/or Wealth Values incorrect!')
end
ctrl = all(p > 0) & all(w >= 0) & any(w > 0);
ctrl = ctrl & (length(p) == length(w));
if ~ctrl
error('Population and/or Wealth Values incorrect!')
end
pw = [p, p .* w, w];
pw = sortrows(pw, 3); % Sort with respect to Total Wealth
pw = cumsum(pw);
minpop = min(p) / pw(end, 1); % Keep the smallest population
pw = pw ./ repmat(pw(end, :), length(p), 1); % Cumulative p & w, normalized to 1
height = [pw(1); pw(2:end, 1) - pw(1:(end - 1), 1)];
base = pw(:, 2);
base = [base(1); base(1:(end - 1)) + base(2:end)] / 2;
y = (1 - 2 * sum(height .* base)) / (1 - minpop); % The Gini Coefficient is normalized with respect to its
% highest possible value which is obtained if the smallest
% population owns all the existing wealth