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

	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.twoSample.wald_WolfowitzTest;

import java.util.Arrays;

import javanpst.data.DataDefinitions;
import javanpst.data.structures.dataTable.DataTable;
import javanpst.data.structures.sequence.StringSequence;
import javanpst.distributions.DistributionDefinitions;
import javanpst.distributions.tests.TotalNumberOfRunsDistribution;
import javanpst.tests.StatisticalTest;

/**
 * The Wald-Wolfowitz test.
 * 
 * This tests maps the values of the ordered combined sample
 * into a string sequence (e.g. XXYXYY, where X are the values 
 * of the first sample and Y are the values of the second sample).
 * 
 * Then a Total Number of Runs test is carried out, where the
 * alternative hypothesis (distinct distributions) is accepted
 * when there are too few runs.
 * 
 * The exact and the asymptotic p-values of the Total Number 
 * of Runs test is provided.
 * 
 * @author Joaqu�n Derrac Rus (jderrac@decsai.ugr.es)
 * @version 1.0
 */
public class Wald_WolfowitzTest extends StatisticalTest{

	private TotalNumberOfRunsDistribution distribution;
	
	/**
	 * Data to analyze
	 */
	private DataTable data;
	
	/**
	 * First sample
	 */
	private double sample1 [];
	
	/**
	 * Second sample
	 */
	private double sample2 [];
	
	/**
	 * Sequence of combined values
	 */
	private StringSequence sequence;
	
	/**
	 * Test statistic (number of runs)
	 */
	private int R;
	
	/**
	 * Exact p-value
	 */
	private double exactPValue;
	
	/**
	 * Asymptotic p-value
	 */
	private double asymptoticPValue;

	/**
	 * Default builder
	 */
	public Wald_WolfowitzTest(){
		
		distribution = TotalNumberOfRunsDistribution.getInstance();
		setReportFormat();
		clearData();
		
	}//end-method
	
	/**
	 * Clears the data stored in the test
	 */
	public void clearData(){
		
		data=new DataTable();
		sequence= new StringSequence();
		
		sample1=null;
		sample2=null;
		
		performed=false;
		dataReady=false;
		
		R=0;
		
		exactPValue=-1.0;
		asymptoticPValue=-1.0;

	}//end-method
	
	/**
	 * Builder. Load data to test.
	 * 
	 * @param newData data to test.
	 */
	public Wald_WolfowitzTest(DataTable newData){

		int nulls;
		int counter1, counter2;
		double value;
		int pointer1,pointer2;
		double value1,value2;
		boolean last=false;
		
		distribution = TotalNumberOfRunsDistribution.getInstance();
		setReportFormat();
		
		data=DataTable.newInstance(newData);
		
		if(data.getColumns()!=2){
			System.out.println("Wald-Wolfowitz test only can be employed with two samples");
			clearData();
			return;
		}
		
		nulls=data.getColumnNulls(0);
		sample1=new double [data.getRows()-nulls];
		nulls=data.getColumnNulls(1);
		sample2=new double [data.getRows()-nulls];
		
		counter1=0;
		counter2=0;
		
		for(int i=0;ivalue2){
				sequence.append("Y");
				pointer2++;
				last=false;
			}
			
			if(value1==value2){
				if(last){
					sequence.append("Y");
					pointer2++;
					last=false;
				}else{
					sequence.append("X");
					pointer1++;
					last=true;
				}

			}
		}
		
		while(pointer1value2){
				sequence.append("Y");
				pointer2++;
				last=false;
			}
			
			if(value1==value2){
				if(last){
					sequence.append("Y");
					pointer2++;
					last=false;
				}else{
					sequence.append("X");
					pointer1++;
					last=true;
				}

			}
		}
		
		while(pointer1 

Ads help maintain this website.