Friday, 3 May 2013

Autocorrelation Random Number Test with Java Program

What is autocorrelation?
Autocorrelation is a statistical test that determines whether a random number generator is producing independent random numbers in a sequence.


What does autocorrelation test?

  • The tests for autocorrelation are concerned with the dependence between numbers in a sequence.
  • The test computes the autocorrelation between every m numbers (m is also known as the lag) starting with the ith number (i is also known as the index).

What are the important variables to remember?

m - is the lag, the space between the numbers being tested. 

i - is the index, or the number in the sequence that you start with 

N - the number of numbers generated in a sequence 

M - is the largest integer such that  

How do you figure out the value of M?

To figure out the value of M, you must use some simple algebra. For example, the equation: must be solved using the given values i the index, N the number of elements in the sequence, and m the lag.
For example:


Since the value M must be an integer, the 0.8 is truncated and the 4 is saved as the value of M. Thus, M is equal to 4.

Using the Equations

To use the estimator equation, denoted  with a hat, you must understand the sequence and summation notations. The formula is actually quite easy to use.



After identifying the index in the sequence, you start there by taking that value and multiplying by the value of the next number (index (i) + the lag (m)). For the next number, you use the previous (i+m) and multiply that with the next value (i+m+m). This process continues iteratively resulting in a sequence. The results of each multiplication are added together (this represents the summation).
The next equation is even simpler to use. After solving for M, plug the numbers in and use the results.

Drawbacks when using autocorrelation
  • Autocorrelation is not very sensitive to small values of M, when the values being tested are on the low side. For example, if all the values were equal to zero, then the resulting value would be -1.95, which is not enough to reject the hypothesis.
  • Many sequences can be formed in a set of date (the sequence of all numbers, the sequence from the first, fifth, ..., numbers and so forth. If the alpha is equal to 0.05, then there is a possibility of rejecting a true hypothesis. For example, if these are 10 independent events, the possibility of finding of finding no autocorrelation alone is (0.95)10 or 0.60. Thus, 40% of the time signifigant autocorrelation would be detected when it does not exist.

The Conclusion

When using autocorrelation tests, be cautious. Autocorrelation may be detected after numerous tests even when no autocorrelation exists.

Java Code for Autocorrelation Random Number Test


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

public class Test_AutoCoRelation {
    
     ArrayList<Double> randomNumbers;
     int i, m, M;
     
     double rho, sigma, Z;

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

    public Test_AutoCoRelation(ArrayList<Double> randomNumbers) {
        this.randomNumbers = randomNumbers;
        
        performTest();
        printResult();
    }
     
     
     private void performTest()
     {
         int N = randomNumbers.size();
         M = ((N-i)/m)-1;
         
         rho = ((1.0/(M+1))*Summation())-0.25;
         sigma = Math.sqrt(13*M+7)/(12*(M+1));
         Z=rho/sigma;
         
     }
    
     private double Summation()
     {
         double sum=0;
         
         for(int j=((i-1)+m); j<randomNumbers.size(); j+=m)
             sum += randomNumbers.get(j-m)*randomNumbers.get(j);
         
         return sum;
     }
     
     
    private  void printResult()
    {
        System.out.println("Result -");
        System.out.println("M = " + M);
        System.out.println("Summation = " + Summation());
        System.out.println("Rho = " + rho);
        System.out.println("Sigma = " + sigma);
        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()));
            }
            
            System.out.print("Enter start point (i) = ");        
            i = Integer.parseInt(br.readLine());
            
            System.out.print("Gap (m) = ");
            m = Integer.parseInt(br.readLine());
        
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }
    
    
    public static void main(String[] args)
    {
        Test_AutoCoRelation test_ACT = new Test_AutoCoRelation();
    }
}

No comments:

Post a Comment

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