function plotLorenzCurve(p, w)
% The function plots the Lorenz Curve, where populations from the poorer to the richer
% are on the x-axis and associated wealth is on the y-axis
% 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).
% p is the vector of populations and w is the vector of wealth
%
% 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: 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)
% plotLorenzCurve(p, w)
%
% -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
% -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
%
% 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);
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);
pw = pw ./ repmat(pw(end, :), length(p), 1); % Cumulative p & w, normalized to 1
figure('Position', [10 50 1280 600]);
plot([0; pw(:, 1)], [0; pw(:, 2)], 'LineWidth', 2) % Lorenz Curve
hold on
plot([0, 1], [0, 1], 'r', 'LineWidth', 2); % Perfect Equality Line (45 Degree Slope)
plot((0.01:0.01:1), (0.01:0.01:1) .^ 2, 'g', 'LineWidth', 2); % Uniform Random Distribution
title('Lorenz Curve', 'FontWeight', 'bold', 'FontSize', 16);
xlabel('Cumulative Percentage of Population from the poorer to the richer', 'FontWeight', 'bold', 'FontSize', 16);
ylabel('Cumulative Percentage of Wealth', 'FontWeight', 'bold', 'FontSize', 16);
set(gca, 'FontWeight', 'bold');
set(gca, 'FontSize', 16);
set(gca, 'LineWidth', 1);
set(gca, 'Color', [1 1 0.9]);
set(gca, 'Box', 'on');
set(gcf, 'Color', [1 1 0.9]);
h = legend('Lorenz Curve', 'Perfect Equality Line (45 Degree Slope)', 'Uniform Random Distribution', 1);
set(h, 'FontSize', 12, 'EdgeColor', 'none', 'Location', 'NorthWest')