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

	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.location.wilcoxonRankSumTest;

import java.util.Arrays;

import javanpst.data.DataDefinitions;
import javanpst.data.structures.dataTable.DataTable;
import javanpst.distributions.DistributionDefinitions;
import javanpst.distributions.common.continuous.NormalDistribution;
import javanpst.distributions.tests.WilcoxonRankSumDistribution;
import javanpst.tests.StatisticalTest;

/**
 * The Wilcoxon Ranks-Sum test.
 * 
 * This test can be used for testing the null hypothesis 
 * of equal medians between two samples, assuming the location
 * model.
 * 
 * An exact and a normal approximation of p-values is provided.
 * (exact p-values are only computes if samples' size is equal or 
 * lower than 10). Moreover, confidence intervals for the 
 * differences of medians (at 0.90 and 0.95 level) are computed.
 * 
 * Asymptotic P-value is computed through a normal approximation.
 * 
 * Ties are handled by the midranks method, and an appropriate
 * reduction of variance is considered.
 * 
 * @author Joaqu�n Derrac Rus (jderrac@decsai.ugr.es)
 * @version 1.0
 */
public class WilcoxonRankSumTest extends StatisticalTest{

	/**
	 * Exact distribution of the test
	 */
	private WilcoxonRankSumDistribution distribution;
	
	/**
	 * Data to analyze
	 */
	private DataTable data;
	
	/**
	 * First sample
	 */
	private double sample1 [];
	
	/**
	 * Second sample
	 */
	private double sample2 [];
	
	/**
	 * Number of values of each sample
	 */
	private int values1,values2;
	
	/**
	 * Array of combined sample
	 */
	private double combined [];
	
	/**
	 * Ties array
	 */
	private boolean ties [];
	
	/**
	 * Wilcoxon statistic
	 */
	private double WRank;
	
	/**
	 * Left tail exact p-value
	 */
	private double exactLeftTail;
	
	/**
	 * Right tail exact p-value
	 */
	private double exactRightTail;
	
	/**
	 * Double tail exact 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;
	
	/**
	 * Confidence intervals at 0.90 and 0.95 levels
	 */
	String confidenceIntervals95, confidenceIntervals90;
	
	/**
	 * Exact confidence for both intervals
	 */
	double exactConfidence90,exactConfidence95;

	/**
	 * Default builder
	 */
	public WilcoxonRankSumTest(){

		distribution=WilcoxonRankSumDistribution.getInstance();
		setReportFormat();
		clearData();
		
	}//end-method
	
	/**
	 * Clears the data stored in the test
	 */
	public void clearData(){
		
		data=new DataTable();
		
		performed=false;
		dataReady=false;
		
		WRank=0.0;
		
		sample1=null;
		sample2=null;
		
		asymptoticLeftTail=-1.0;
		asymptoticRightTail=-1.0;
		asymptoticDoubleTail=-1.0;
		exactLeftTail=-1.0;
		exactRightTail=-1.0;
		exactDoubleTail=-1.0;
		
		confidenceIntervals95="";
		confidenceIntervals90="";
		
		exactConfidence90=-1.0;
		exactConfidence95=-1.0;
		
	}//end-method
	
	/**
	 * Builder. Load data to test.
	 * 
	 * @param newData data to test.
	 */
	public WilcoxonRankSumTest(DataTable newData){
		
		int nulls;
		int counter1, counter2;
		double value;
		
		distribution=WilcoxonRankSumDistribution.getInstance();
		setReportFormat();
		
		data=DataTable.newInstance(newData);
		
		if(data.getColumns()!=2){
			System.out.println("Wilcoxon Ranks-Sum test only can be employed with two samples");
			clearData();
			return;
		}
		
		nulls=data.getColumnNulls(0);
		values1=data.getRows()-nulls;
		sample1=new double [values1];
		nulls=data.getColumnNulls(1);
		values2=data.getRows()-nulls;
		sample2=new double [values2];
		
		counter1=0;
		counter2=0;
		
		for(int i=0;i0){
					sumTies+=actualTie*((actualTie*actualTie)-1);
					actualTie=0;
				}
			}
			pointer++;
		}
		if(ties[pointer-1]){
			sumTies+=actualTie*((actualTie*actualTie)-1);
		}
		
		//compute variance
		double denominator;
		
		denominator=(double)((double)(values1*values2*(values1+values2+1))/(double)12.0);
		denominator-=(double)((double)(values1*values2*sumTies)/(double)(12.0*(values1+values2)*(values1+values2-1)));
		
		denominator=Math.sqrt(denominator);
		double numerator;
		
		numerator=WRank-0.5-((double)values1*((double)values1+values2+1)/(double)2.0);
		
		double z;
		
		z=numerator/denominator;
		
		asymptoticRightTail=1.0-normal.getTipifiedProbability(z, false);
		
		numerator=WRank+0.5-((double)values1*((double)values2+values1+1)/(double)2.0);
		z=numerator/denominator;
		
		asymptoticLeftTail=normal.getTipifiedProbability(z, false);
		
		asymptoticDoubleTail=Math.min(Math.min(asymptoticLeftTail,asymptoticRightTail)*2.0,1.0);
	
		//confidence interval
		
		criticalN=distribution.findCriticalValue(values1,values2,0.1);
		criticalN=Math.max(criticalN,0);

		differences=new double [sample1.length*sample2.length];
		
		pointer=0;
		
		for(int i=0;i 

Ads help maintain this website.