The first part of Grubbs' test is finding the test statistic. Recall that



Our test statistic-finding function will take value as a Number (the value we are testing) and an array of Numbers (the data sample from which our value to be tested resides).

// Find the test statistic
public static function findTestStat(val:Number, array:Array):Number {
    return (Math.abs(findMean(array)-val))/findSD(array);
}

Notice that we declared this a 'static' function and gave it the return type of Number. Our goals is that when we call this function with a value and our data sample, it will return the test statistic as a Number

You will also notice that in the return statement, I make calls to the functions findMean() and findSD(). If you are wondering where they come from, the answer is that have yet to write them! Yes, we could no doubt find the mean and standard deviation within the function. However, long and complex functions are hard to read, and it is good programming practice to break complicated tasks apart into smaller, reusable functions. (There is a chance that we might want to compute the mean or standard deviation by itself.)

Here are the functions for finding the mean and standard deviation. I chose to make both methods public, as opposed to private, in the case that we wanted to call Grubbs.findMean(some parameter) by itself.

Mean:

// Find a mean of an array of numbers
public static function findMean(array:Array):Number {
    var entries:int = array.length;
    var sum:Number = 0;
    for each(var a:Number in array) {
    sum += a;
    }
    return sum/entries;
}


Standard Deviation:

// Find standard deviation
public static function findSD(array:Array):Number {
    var entries:int = array.length;
    var mean:Number;
    var sumOfSquares = 0;
    mean = findMean(array);
    for each(var a:Number in array) {
    sumOfSquares += (a-mean)*(a-mean);
    }
    return Math.sqrt(sumOfSquares/entries);
}

(If you need a little help remembering how to find the standard deviation, please visit this article.)

Here is out Grubbs class so far:

package {
    public class Grubbs {

        // Find the test statistic
        public static function findTestStat(val:Number, array:Array):Number {
            return (Math.abs(findMean(array)-val))/findSD(array);
        }

        // Find a mean of an array of numbers
        public static function findMean(array:Array):Number {
            var entries:int = array.length;
            var sum:Number = 0;
            for each(var a:Number in array) {
            sum += a;
            }
        return sum/entries;
        }

        // Find standard deviation
        public static function findSD(array:Array):Number {
            var entries:int = array.length;
            var mean:Number;
            var sumOfSquares = 0;

            mean = findMean(array);
            for each(var a:Number in array) {
            sumOfSquares += (a-mean)*(a-mean);
            }
            return Math.sqrt(sumOfSquares/entries);
        }


    }
}


Okay, now that we have our function for finding the test statistic, we need to determine the critical region of Grubbs' test.