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

	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.countData.fisherTest;

import javanpst.data.structures.dataTable.DataTable;
import javanpst.distributions.tests.FisherDistribution;
import javanpst.tests.StatisticalTest;

/**
 * The Fisher test.
 * 
 * Fisher's exact test can be used to test the significance of the association (contingency) 
 * between two kinds of classifications (in 2 x 2 contingency matrixes).
 * 
 * A chi-square based approximation is also provided.
 * 
 * @author Joaqu�n Derrac Rus (jderrac@decsai.ugr.es)
 * @version 1.0
 */
public class FisherTest extends StatisticalTest{

	/**
	 * Distribution of the test
	 */
	private FisherDistribution distribution;
	
	/**
	 * Data to analyze
	 */
	private DataTable data;
	
	/**
	 * Contingency matrix
	 */
	private double contingencyTable [][];
	
	/**
	 * Number of samples
	 */
	private int N;
	 
	/**
	 * Sum of the samples of the first column
	 */
	private int Y;
	
	/**
	 * Samples in first column, first row
	 */
	private int n00;
	
	/**
	 * Sum of the samples of the first row
	 */
	private int n1;
	
	/**
	 * Sum of the samples of the second row
	 */
	private int n2;
	
	/**
	 * Exact left tail p-value
	 */
	private double exactLeftPValue;
	
	/**
	 * Exact right tail p-value
	 */
	private double exactRightPValue;
	
	/**
	 * Q statistic
	 */
	private double Q;
	
	/**
	 * Asymptotic p-value
	 */
	private double asymptoticPValue;
	
	/**
	 * Default builder
	 */
	public FisherTest(){

		distribution=FisherDistribution.getInstance();
		setReportFormat();
		clearData();
		
	}//end-method
	
	/**
	 * Clears the data stored in the test
	 */
	public void clearData(){
		
		data=new DataTable();
		
		performed=false;
		dataReady=false;
		
		Q=0.0;
		
		contingencyTable=null;
		
		asymptoticPValue=-1.0;
		exactLeftPValue=-1.0;
		exactRightPValue=-1.0;
		
	}//end-method
	
	/**
	 * Builder. Load data to test.
	 * 
	 * @param newData data to test.
	 */
	public FisherTest(DataTable newData){
		
		distribution=FisherDistribution.getInstance();
		setReportFormat();
		
		data=DataTable.newInstance(newData);
		
		if((data.getColumns()>2)||(data.getColumns()<2)||(data.getRows()>2)||(data.getRows()<2)){
			System.out.println("Data must be represented in two rows and two columns");
			clearData();
			return;
		}
		
		for(int i=0;i0){
				System.out.println("No null values allowed in this test.");
				clearData();
				return;
			}
		}

		contingencyTable=new double[data.getRows()][data.getColumns()];
		
		for(int i=0;i2)||(data.getColumns()<2)||(data.getRows()>2)||(data.getRows()<2)){
			System.out.println("Data must be represented in two rows and two columns");
			clearData();
			return;
		}
		
		for(int i=0;i0){
				System.out.println("No null values allowed in this test.");
				clearData();
				return;
			}
		}
		
		contingencyTable=new double[data.getRows()][data.getColumns()];
		
		for(int i=0;i 

Ads help maintain this website.