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

	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.goodness.chiSquareTest;

import javanpst.data.structures.dataTable.DataTable;
import javanpst.distributions.DistributionDefinitions;
import javanpst.distributions.common.continuous.ChiSquareDistribution;
import javanpst.distributions.common.discrete.BinomialDistribution;
import javanpst.distributions.common.discrete.GeometricDistribution;
import javanpst.distributions.common.discrete.PoissonDistribution;
import javanpst.distributions.common.discrete.UniformDistribution;
import javanpst.tests.StatisticalTest;

/**
 * The Chi-square test.
 * 
 * This goodness of fit can be used to test if a given sample follows
 * a discrete distribution. This implementation allow to adjust the data
 * to a uniform, binomial, poisson or geometric distribution.
 * 
 * @author Joaqu�n Derrac Rus (jderrac@decsai.ugr.es)
 * @version 1.0
 */
public class ChiSquareTest extends StatisticalTest{

	/**
	 * Data to fit
	 */
	private DataTable data;
	
	/**
	 * Type of distribution to fit
	 */
	private int typeDist;
	
	/**
	 * Number of degrees of the chi-square distribution
	 */
	private int freedomDegree; 
	
	/**
	 * Number of estimated parameters
	 */
	private int nEstimated;
	
	/**
	 * Number of values in the sample
	 */
	private int nCount;
	
	/**
	 * Error obtained in each category
	 */
	private double errorTable [][];
	
	/**
	 * distribution defined by data
	 */
	private double distribution [][];
	
	/**
	 * Tests if the distribution to adjust is completely defined
	 */
	private boolean distributionReady;
	
	/**
	 * Q statistic
	 */
	private double Q;
	
	/**
	 * P-value of the test
	 */
	private double pValue;
	
	/**
	 * Uniform distribution
	 */
	private UniformDistribution uniform;
	
	/**
	 * Binomial distribution
	 */
	private BinomialDistribution binomial;
	
	/**
	 * Poisson distribution
	 */
	private PoissonDistribution poisson;
	
	/**
	 * Geometric distribution
	 */
	private GeometricDistribution geometric;
	
	/**
	 * Chi-square distribution
	 */
	private ChiSquareDistribution chiSquare;
	
	/**
	 * Default builder
	 */
	public ChiSquareTest(){
		
		setReportFormat();
		clearData();
		
	}//end-method
	
	/**
	 * Clears the data stored in the test
	 */
	public void clearData(){
		
		data=new DataTable();
		
		performed=false;
		dataReady=false;
		distributionReady=false;
		
		typeDist=-1;
		freedomDegree=0;
		nEstimated=0;
		
		errorTable=null;
		
		pValue=-1.0;
		
	}//end-method
	
	/**
	 * Builder. Load data to test.
	 * 
	 * @param newData data to test.
	 */
	public ChiSquareTest(DataTable newData){
		
		setReportFormat();
		
		data=DataTable.newInstance(newData);
		
		if(data.getColumns()!=2){
			System.out.println("Chi-square test only accept tables in the following format:");
			System.out.println(" ");
			clearData();
			return;
		}
		
		for(int i=0;i0){
				System.out.println("No null values allowed in this test.");
				clearData();
				return;
			}
		}
	
		errorTable = new double [data.getRows()][2];
		distribution = new double [data.getRows()][2];
		
		nCount=0;
		for(int i=0;i ");
			clearData();
			return;
		}
		
		for(int i=0;i0){
				System.out.println("No null values allowed in this test.");
				clearData();
				return;
			}
		}
		
		errorTable = new double [data.getRows()][2];
		distribution = new double [data.getRows()][2];
		
		nCount=0;
		for(int i=0;i0 && less=1.0 && pointerdistribution[j][0]){
					swapRow(distribution[i][0],distribution[i][1],distribution[j][0],distribution[j][1]);
				}
			}
		}
		
	}//end-method
	
	/**
	 * Swaps two rows of the distribution table
	 * 
	 * @param a1 first row, first column value
	 * @param a2 first row, second column value
	 * @param b1 second row, first column value
	 * @param b2 second row, second column value
	 */
	private void swapRow(double a1,double a2,double b1, double b2){
		
		double aux1,aux2;
		
		aux1=a1;
		a1=b1;
		b1=aux1;
		
		aux2=a2;
		a2=b2;
		b2=aux2;
		
	}//end-method

	/**
	 * Get Q statistic 
	 * 
	 * @return Q Statistic
	 */
	public double getQ(){
		
		return Q;
		
	}//end-method
	
	/**
	 * Get p-value of the test
	 * 
	 * @return p-value computed
	 */
	public double getPValue(){
		
		return pValue;
		
	}//end-method
	
	/**
	 * Prints the data stored in the test
	 * 
	 * @return Data stored
	 */
	public String printData(){
		
		String text="";
		
		text+="\n"+data;
		
		return text;
		
	}//end-method
	
	/**
	 * Prints a report with the results of the test
	 * 
	 * @return Output report
	 */
	public String printReport(){
		
		String report="";
		
		if(!performed){
			report+="The test has not been performed.\n";
			return report;
		}
		
		report+="\n************\n";
		report+="Chi-square goodness of fit test\n";
		report+="**************\n\n";
		
		switch(typeDist){
		
			case DistributionDefinitions.UNIFORM:
				report+="Fitting data to Uniform distribution (N="+nf6.format(uniform.getN())+")\n\n";
				break;
			
			case DistributionDefinitions.BINOMIAL:
				report+="Fitting data to Binomial distribution (N="+nf6.format(binomial.getN())+" , P="+nf6.format(binomial.getP())+")\n\n";
				break;
				
			case DistributionDefinitions.POISSON:
				report+="Fitting data to Poisson distribution (Mean="+nf6.format(poisson.getMean())+")\n\n";
				break;
				
			case DistributionDefinitions.GEOMETRIC:
				report+="Fitting data to Geometric distribution (P="+nf6.format(geometric.getP())+")\n\n";
				break;
	
		}

		report+="Q Statistic: "+nf6.format(Q)+"\n\n";
		
		report+="P-Value : "+nf6.format(pValue)+"\n";

		return report;
		
	}//end-method
	
}//end-class

 

Ads help maintain this website.