Discrete Order Statistics

Introduction
Suppose that you roll N dice, and you are concerned with the value of the r-th lowest dice roll. The statistic X(r), which is the r-th lowest roll, is called an order statistic.

For simplicity, assume that the support of the random variable X has finite support. Without any loss of generality, call these values {1,2...Y}.

To calculate the distribution of the r-th order statistic, you use the following code:

Java
F(N,R,x) { // This functions takes the following parameters: // N : The total number of rolls // R : The order statistic that you want to evaluate // x : The particular value that you want the probability of. E.g. F(N,R,2) Gives the probability that the r-th order statistic of the N rolls is exactly 2. if (x == Y)  { for (int i = 0 ; i < R ; i ++) {        ans += com(N,i)*Math.pow(CDF[Y-1],i)*Math.pow(prob[Y],N-i) ; } } else if (x == 1) {    for (int i = 0 ; i <= N-R ; i ++) {        ans += com(N,i)*Math.pow(1-CDF[1],i)*Math.pow(prob[1],N-i) ; } } else {    for (int i = 0 ; i < R ; i ++) for (int j = 0 ; j <= N - R ; j ++) {        ans += Math.exp(lfact[N]-lfact[i]-lfact[j]-lfact[N-i-j])*Math.pow(CDF[x-1],i)*Math.pow(prob[x],N-i-j)*Math.pow(1-CDF[x],j) ; }    } } // Needed functions: // com(X,Y) returns "X choose Y", that is: X!/(Y!*(X-Y)!) // lfact[X] returns the logfactorial of X // prob[X] returns the pmf at [X] (the probability that a single roll gives X as the outcome) // CDF[X] returns the CDF at [X] (the sum from 1 to X of the probabilities; also equal to the probability that a single roll gives a value less than or equal to X) // To calculate expected value, i.e. E[F(N,R)], use the following: double result = 0 ; for (int i = 1 ; i <= Y ; i ++) result += F(N,R,i)*i ; System.out.println(result) ; // For variance, use the definition of variance: V[X] = E[X^2]-E[X]^2. In this case, use the following code: double res1, res2 ; res1 = res2 = 0 ; for (int i = 1 ; i <= Y; i ++) {   double t = F(N,R,i) ; res1 += t*i ; res2 += t*i*i ; } res1 *= res1 ; double variance = res2 - res1 ; System.out.println(variance) ;