User Tools

Site Tools


How do I compute the median of a parameter's values?

The code fragment below was contributed by Tom Rutherford to theGAMS User List, an example for multidimensional data is available on his web site

$title  Finding the Median of a Distribution in GAMS

set     i       Individuals /i1*i1000/;
alias (i,j);
parameter       income(i)       Personal income,
                rank(i)         Income rank,
                median          Median income;
*       Make it random for the purpose of illustration:
income(i) = uniform(0,1);
*       This is ugly and slow, but it works:
rank(i) = sum(j$(income(j) gt income(i)), 1) + 1;

* In this example there will be no ties, there could be a tie in an arbitrary
* application.  Just to be safe,  divide through by the number of
* median individuals:

median = sum(i$(rank(i) eq round(card(i)/2)), income(i))
            / sum(i$(rank(i) eq round(card(i)/2)), 1);

display median;
gams/compute_the_median_of_a_parameter_s_values.txt · Last modified: 2009/05/29 10:52 by support