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

	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.oneSample.wilcoxonTest;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;

import javanpst.data.structures.dataTable.DataTable;
import javanpst.distributions.DistributionDefinitions;
import javanpst.distributions.tests.WilcoxonDistribution;
import javanpst.tests.StatisticalTest;

/**
 * The Wilcoxon Signed-Ranks test
 * 
 * This test is proposed for use with paired-sample data 
 * in making inferences concerning the value of the median 
 * of the population of differences.
 * 
 * Zero differences are shared between both samples. If there 
 * is a odd number of them, one is discarded. Ties are handled
 * by the midrank method.
 * 
 * An exact p-value (for p-values equal or lower than 0.2, and
 * sample size equal or lower than 50) is provided. An
 * asymptotic approximation to the normal distribution is 
 * also provided.
 * 
 * Furthermore, this version of the test also provides two
 * confidence intervals (at 0.90 and 0.95 levels) for the
 * location of the median of the differences, through the use of
 * Walsh averages.
 * 
 * @author Joaqu�n Derrac Rus (jderrac@decsai.ugr.es)
 * @version 1.0
 */
public class WilcoxonTest extends StatisticalTest{

	/**
	 * Distribution of the test
	 */
	private WilcoxonDistribution distribution;
	
	/**
	 * Data to analyze
	 */
	private DataTable data;
	
	/**
	 * Wilcoxon ranks
	 */
	private double rPlus, rMinus;

	/**
	 * 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;
	
	/**
	 * Upper limit of confidence intervals
	 */
	double upper90, upper95;
	
	/**
	 * Upper limit of confidence intervals
	 */
	double lower90, lower95;
	
	/**
	 * Exact confidence
	 */
	double exactConfidence90,exactConfidence95;
	
	/**
	 * Default builder
	 */
	public WilcoxonTest(){
		
		distribution=WilcoxonDistribution.getInstance();
		setReportFormat();
		clearData();
		
	}//end-method
	
	/**
	 * Clears the data stored in the test
	 */
	public void clearData(){
		
		rPlus=0.0;
		rMinus=0.0;
		
		upper90=0.0;
		upper95=0.0;
		lower90=0.0;
		lower95=0.0;
		
		exactLeftTail=-1.0;
		exactRightTail=-1.0;
		exactDoubleTail=-1.0;
		asymptoticLeftTail=-1.0;
		asymptoticRightTail=-1.0;
		asymptoticDoubleTail=-1.0;
		
		data = new DataTable();
		
		dataReady=false;
		performed=false;
		
	}//end-method
	
	/**
	 * Builder. Load data to test.
	 * 
	 * @param newData data to test.
	 */
	public WilcoxonTest(DataTable newData){
		
		distribution=WilcoxonDistribution.getInstance();	
		setReportFormat();
		
		data=DataTable.newInstance(newData);
		
		if(data.getColumns()!=2){
			System.out.println("Wilcoxon test only can be employed with two samples");
			clearData();
			return;
		}
		
		for(int i=0;i0){
				System.out.println("No null values allowed in this test.");
				clearData();
				return;
			}
		}
		
		dataReady=true;
		performed=false;
		
	}//end-method
	
	/**
	 * Load data to test.
	 * 
	 * @param newData data to test.
	 */
	public void setData(DataTable newData){
		
		data=DataTable.newInstance(newData);
		
		if(data.getColumns()!=2){
			System.out.println("Wilcoxon test only can be employed with two samples");
			clearData();
			return;
		}
		
		for(int i=0;i0){
				System.out.println("No null values allowed in this test.");
				clearData();
				return;
			}
		}
		
		dataReady=true;
		performed=false;
		
	}//end-method
	
	/**
	 * Performs the test
	 */
	public void doTest(){
		
		double AOld [],A [];
		double BOld [],B [];
		double diffOld [], diff [];
		int zeroDifferences,N,pointer;
		boolean sign[];
		double ranks[];
		ArrayList walsh;
		double tiesWeight;
		int criticalN;
		
		if(!dataReady){		
			System.out.println("Data is not ready");
			return;	
		}
		
		AOld=new double[data.getRows()];
		BOld=new double[data.getRows()];
		diffOld=new double[data.getRows()];
		
		zeroDifferences=0;
		tiesWeight=0.0;

		//compute population of differences
		for(int i=0;i0.0){
					sign[pointer]=true;
				}
				else{
					sign[pointer]=false;
				}
				pointer++;
			}
		
		}

		//compute ranks
		double min;
		double points;
		int tied;
		
		Arrays.fill(ranks, -1.0);

		for(int rank=1;rank<=N;){
			
			min=Double.MAX_VALUE;
			tied=1;
			
			for(int i=0;i1){
			//If there is a odd number of them, one is discarded.
			if(zeroDifferences%2==1){
				increment=zeroDifferences-1.0;
			}
			else{
				increment=zeroDifferences;
			}
			
			//Addition of ranks due to zero differences
			//(e.g if 2 zero differences were added, the sum of these ranks is:
			//((2+1)*(2))/2 = 3, thus 1.5 points are added to each statistic)
			sum0=(((double)zeroDifferences+1.0)*(double)zeroDifferences)/2.0;
			sum0/=2.0;
			
			rPlus+=sum0;
			rMinus+=sum0;
			
			//Scaling the rest of ranks 
			//(e.g if 2 zero differences were added, each rank must be increased by 2)
			for(int i=0;i();
		
		double aux;
		for(int i=0;i 

Ads help maintain this website.