Friday, 3 May 2013

Runs Up down Test with Java Program


  • The runs test examines the arrangement of numbers in a sequence to test the hypothesis of independence.
  • See the tables on page 303. With a closer look, the numbers in the first table go from small to large with certain pattern. Though these numbers will pass the uniform tests in previous section, they are not quite independent.
  • A run is defined as a succession of similar events proceded and followed by a different event.E.g. in a sequence of tosses of a coin, we may have
      H T T H H T T T H T
    
    The first toss is proceded and the last toss is followed by a "no event". This sequence has six runs, first with a length of one, second and third with length two, fourth length three, fifth and sixth length one.
  • A few features of a run
    • two characteristics: number of runs and the length of run
    • an up run is a sequence of numbers each of which is succeeded by a larger number; a down run is a squence of numbers each of which is succeeded by a smaller number
  • If a sequence of numbers have too few runs, it is unlikely a real random sequence. E.g. 0.08, 0.18, 0.23, 0.36, 0.42, 0.55, 0.63, 0.72, 0.89, 0.91, the sequence has one run, an up run. It is not likely a random sequence.
  • If a sequence of numbers have too many runs, it is unlikely a real random sequence. E.g. 0.08, 0.93, 0.15, 0.96, 0.26, 0.84, 0.28, 0.79, 0.36, 0.57. It has nine runs, five up and four down. It is not likely a random sequence.
  • If a is the total number of runs in a truly random sequence, the mean and variance of a is given by

    \begin{displaymath}\mu_a = \frac{2N - 1}{3} \end{displaymath}


    and

    \begin{displaymath}\sigma^2 = \frac{16N - 29}{90} \end{displaymath}


  • For $ N > 20$, the distribution of a is reasonably approximated by a normal distribution, $N(\mu_a, \sigma_a^2$. Converting it to a standardized normal distribution by

    \begin{displaymath}Z_0 = \frac{a - \mu_a}{\sigma_a} \end{displaymath}


    that is

    \begin{displaymath}Z_0 = \frac{a - [(2N-1)/3]} {\sqrt{(16N - 29)/90}} \end{displaymath}


  • Failure to reject the hypothesis of independence occurs when $-z_{\alpha/2} \le Z_0 \le z_{\alpha/2}$, where the $\alpha$ is the level of significance.


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;

public class Test_RunsUpDown {
    ArrayList<Double> randomNumbers;
    
    int runCount=0;
    
    Boolean sign = null;
    
    double mew, sigma_sqr, Z;

    public Test_RunsUpDown() {
        
        randomNumbers = new ArrayList<Double>();
        
        acceptRandomNumbers();
        performTest();
        printResult();
    }
    
    
    public Test_RunsUpDown(ArrayList<Double> randomNumbers) {
        this.randomNumbers = randomNumbers;
        
        performTest();
        printResult();
    }
    
    
    private void performTest()
    {
        doRunCount();
        
        double a = runCount;
        double N = randomNumbers.size();
        
        mew = (2.0*N-1.0)/3.0;
        sigma_sqr = (16.0*N-29.0)/90.0;
        Z = (a-mew)/Math.sqrt(sigma_sqr);
    }
    
    private  void printResult()
    {
        System.out.println("Result -");
        System.out.println("a = " + runCount);
        System.out.println("Mew = " + mew);
        System.out.println("Sigma^2 = " + sigma_sqr);
        System.out.println("Z = " + Z);
    }
    
    private void doRunCount()
    {
        for(int i=1; i<randomNumbers.size(); i++)
        {
            double r = randomNumbers.get(i);
            
            if(sign!=null)
            {
                if(r>randomNumbers.get(i-1))
                {
                    if(!sign)
                    {
                        sign=true;
                        runCount++;
                    }
                }
                else
                {
                    if(sign)
                    {
                        sign=false;
                        runCount++;
                    }
                }
            }
            else
            {
                if(r>randomNumbers.get(i-1))
                    sign=true;
                else 
                    sign=false;
                runCount++;
            }
            
        }
    }
    
    public void acceptRandomNumbers()
    {
        try
        {
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            
            System.out.print("Sample Space (N) = ");
            int count = Integer.parseInt(br.readLine());
            
            System.out.println("\nEnter Random Numbers: ");
            
            for(int i=0; i<count; i++)
            {
                randomNumbers.add(Double.parseDouble(br.readLine()));
            }
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }
    
    
    public static void main(String[] args)
    {
        Test_RunsUpDown test_RUD = new Test_RunsUpDown();
    }
}

No comments:

Post a Comment

Your comments are very much valuable for us. Thanks for giving your precious time.

Do you like this article?