Reputation: 1571
I have some data containing information about a variable called ``wealth".
I want to calculate the shares of those being in the top in the distribution, in the middle and in the bottom. That is how much of wealth is owned by the rich, middle, and poor.
The analogue example is to draw 10000 random variables from a gamma distribution, So suppose the distribution is this one:
wealth = gamrnd(shape,scale,n,1);
So how I can calculate how much of this variable go to say the top 10%, bottom 90% etc ...
Can someone help me how I can do that in Matlab ?
Upvotes: 0
Views: 1349
Reputation: 627
To calculate percentiles you can use matlab's function prctile
. One of the ways to call the function is
prctile(X,p)
where X is your vector and p is a percentage in the range [0-100]. Note that this would be what you call "bottom percentage"
In your case, you can get the bottom n% as follows:
ninetyPercentBottom = prctile(X,n)
ninetyPercentBottomShare = sum(X(X<ninetyPercentBottom))/sum(X)
If you want the "top percentage", note that "bottom percentage" n% is the same as "top percentage" 100-n%, so you can use that idea to get the share of the top n%
topPercentile = 10
tenPercentTop = prctile(X,100-topPercentile)
tenPercentTopShare = sum(X(X>tenPercentTop))/sum(X)
Upvotes: 1
Reputation: 5822
You can use the following function, which is based on sorting your data:
function [ topVals, bottomVals ] = calcPercentile( x, percentile )
sortedX = sort(x,'descend');
m = int16(percentile*length(x));
topVals = sortedX(1:m);
bottomVals = sortedX(m+1:end);
end
Usage example:
%getting top 10% and bottom 90%
[ topVals, bottomVals ] = calcPercentile(x,0.1);
%getting top 40% and bottom 60%
[ topVals, bottomVals ] = calcPercentile(x,0.4);
Results:
topVals = 10
bottomVals = 9 8 7 6 5 4 3 2 1
topVals = 10 9 8 7
bottomVals = 6 5 4 3 2 1
Upvotes: 1