Friday 3 May 2013

Runs Above Below Test for Random Numbers with Java Program


  • The previous test for up runs and down runs are important. But they are not adquate to assure that the sequence is random.
  • Check the sequence of numbers at the top of page 306, where they pass the runs up and down test. But it display the phenomenon that the first 20 numbers are above the mean, while the last 20 are below the mean.
  • Let $n_1$ and $n_2$ be the number of individual observations above and below the mean, let b the total number of runs.
  • For a given $n_1$ and $n_2$, the mean and variance of b can be expressed as

    \begin{displaymath}\mu_b = \frac{2n_1 n_2}{N} + \frac{1}{2} \end{displaymath}


    and

    \begin{displaymath}\sigma_b^2 = \frac{2n_1 n_2 (2n_1 n_2 - N)} {N^2 (N-1)} \end{displaymath}


  • For either $n_1$ or $n_2$ greater than 20, b is approximately normally distributed

    \begin{displaymath}Z_0 = \frac{b - (2n_1 n_2 / N) - 1/2} { [\frac{2n_1 n_2 (2 n_1 n_2 - N)}
{N^2 (N-1)} ]^{1/2}} \end{displaymath}


  • Failure to reject the hypothesis of independence occurs when $-z_{\alpha/2} \le Z_0 \le z_{\alpha/2}$, where $\alpha$ is the level of significance.
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;

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

    public Test_RunsAboveBelow() {
        randomNumbers = new ArrayList<Double>();
        
        acceptRandomNumbers();
        performTest();
        printResult();
    }

    
    public Test_RunsAboveBelow(ArrayList<Double> randomNumbers) {
        
        this.randomNumbers = randomNumbers;
        
        acceptRandomNumbers();
        performTest();
        printResult();
    }
    
    private void performTest()
    {
        doRunCount();
        
        double b = runCount;
        double N = randomNumbers.size();
        
        double twoInton1n2 = (2.0*n1*n2);
        
        mew = (twoInton1n2/N)+0.5;
        sigma_sqr = twoInton1n2*(twoInton1n2-N)/(Math.pow(N, 2)*(N-1));
        Z = (b-mew)/Math.sqrt(sigma_sqr);
    }
    
    private void doRunCount()
    {
        for(double r : randomNumbers)
        {
            if(sign!=null)
            {
                if(r>0.495)
                {
                    n1++;
                    
                    if(!sign)
                    {
                        sign=true;
                        runCount++;
                    }
                }
                else
                {
                    n2++;
                    
                    if(sign)
                    {
                        sign=false;
                        runCount++;
                    }
                }
            }
            else
            {
                if(r>0.495)
                {
                    n1++;
                    sign=true;
                }
                else
                {
                    n2++;
                    sign=false;
                }
                runCount++;
            }
        }
    }
    
    private  void printResult()
    {
        System.out.println("Result -");
        System.out.println("b = " + runCount);
        System.out.println("n1 = " + n1);
        System.out.println("n2 = " + n2);
        System.out.println("Mew = " + mew);
        System.out.println("Sigma^2 = " + sigma_sqr);
        System.out.println("Z = " + Z);
    }
    
    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_RunsAboveBelow test_RAB = new Test_RunsAboveBelow();
    }      
}

No comments:

Post a Comment

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

Do you like this article?