function coeff = gini(x,nosample)

// Gini coefficient

// x ... data vector
// nosample 0 ... sample correction (default)
//          1 ... population

// coeff ... Gini coefficient

// Literature: Basmann, R.L., Hayes, K.J., Slottje,
// D.J. (1993). Some new methods for measuring
// and describing economic inequality, JAI Press:
// Connecticut

// Copyright (C): Lukáš Malec and Jaroslav Poživil;
// University of Business in Prague, Czech Republic
// Date: $2014-02-01$

[lhs,rhs]=argn(0)

if x==[] then coeff=%nan, return, end

n = length(x), x = matrix(x,n,1)
x = gsort(x,'r','i')

freq = flipdim(cumsum(ones(n,1)),1)

tot = sum(x)

coeff = n+1-2*(sum(x.*freq)./tot)

if rhs==1
    coeff = coeff ./(n-1)
  elseif nosample==1 then
    coeff = coeff ./n
  elseif nosample==0 then
    coeff = coeff ./(n-1)

end

endfunction
