/***********************************************************************

	This file is part of JavaNPST, a Java library of NonParametric
	Statistical Tests.

	Copyright (C) 2011
	
	J. Derrac (jderrac@decsai.ugr.es)
	S. Garcia (sglopez@ujaen.es)
	F. Herrera (herrera@decsai.ugr.es)

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see http://www.gnu.org/licenses/
  
**********************************************************************/

package javanpst.tests.randomness.vonNeumannTest;

import java.util.Arrays;

import javanpst.data.structures.sequence.NumericSequence;
import javanpst.distributions.common.continuous.NormalDistribution;
import javanpst.distributions.tests.NMDistribution;
import javanpst.distributions.tests.RVNDistribution;
import javanpst.tests.StatisticalTest;

/**
 * The Von Neumann test
 * 
 * This tests check randomness of a numeric sequence by 
 * assigning ranks to each value.
 * 
 * A normal approximation is also provided. 
 * 
 * @author Joaqu�n Derrac Rus (jderrac@decsai.ugr.es)
 * @version 1.0
 */
public class VonNeumannTest extends StatisticalTest{

	/**
	 * NM distribution
	 */
	private NMDistribution distributionNM;
	
	/**
	 * RVN distribution
	 */
	private RVNDistribution distributionRVN;
	
	/**
	 * Sequence to test
	 */
	private NumericSequence sequence;
	
	/**
	 * Left tail p-value
	 */
	private double exactLeftTail;
	
	/**
	 * Right tail p-value
	 */
	private double exactRightTail;
	
	/**
	 * Double tail p-value
	 */
	private double exactDoubleTail;
	
	/**
	 * Left tail asymptotic p-value
	 */
	private double asymptoticLeftTail;
	
	/**
	 * Right tail asymptotic p-value
	 */
	private double asymptoticRightTail;
	
	/**
	 * Double tail asymptotic p-value
	 */
	private double asymptoticDoubleTail;
	
	/**
	 * Ranks of the sequence
	 */
	private double [] ranks;
	
	/**
	 * NM statistic
	 */
	private double NM;
	
	/**
	 * RVN Statistic
	 */
	private double RVN;

	/**
	 * Default builder
	 */
	public VonNeumannTest(){
		
		distributionNM=NMDistribution.getInstance();
		distributionRVN=RVNDistribution.getInstance();
		setReportFormat();
		clearData();
		
	}//end-method
	
	/**
	 * Clears the data stored in the test
	 */
	public void clearData(){
		
		sequence=new NumericSequence();
		
		performed=false;
		dataReady=false;

		NM=0.0;
		RVN=0.0;
		
		ranks=null;
		
		exactLeftTail=-1.0;
		exactRightTail=-1.0;
		exactDoubleTail=-1.0;
		
		asymptoticLeftTail=-1.0;
		asymptoticRightTail=-1.0;
		asymptoticDoubleTail=-1.0;
		
	}//end-method

	/**
	 * Builder. Load data to test.
	 * 
	 * @param newSequence data to test.
	 */
	public VonNeumannTest(NumericSequence newSequence){

		distributionNM=NMDistribution.getInstance();
		distributionRVN=RVNDistribution.getInstance();
		setReportFormat();
		
		sequence=new NumericSequence(newSequence);
		
		dataReady=true;
		performed=false;
		
	}//end-method
	
	/**
	 * Load data to test.
	 * 
	 * @param newSequence data to test.
	 */
	public void setData(NumericSequence newSequence){
		
		sequence=new NumericSequence(newSequence);
		
		dataReady=true;
		performed=false;
		
	}//end-method
	
	/**
	 * Perform the test
	 */
	public void doTest(){
	
		if(!dataReady){		
			System.out.println("Data is not ready");
			return;	
		}
		
		computeRanks();
		
		double denominator=0.0;
		double z;
		double variance;
		double n=(double)sequence.size();
		
		//compute test statistics
		NM=0.0;
		
		for(int i=0;i 

Ads help maintain this website.