/*
 *  Draco Econometrics
 *  Copyright (C) 2006-2007 Approximatrix, LLC
 *  http://www.approximatrix.com
 *  
 *  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 com.approximatrix.draco.analysis.statistics;

import com.approximatrix.draco.math.FunctionSupport;

/** Class providing values of the Chi-Squared distribution
 * 
 * @author Jeffrey Armstrong
 * @see http://en.wikipedia.org/wiki/Chi-square_distribution
 *
 */
public class ChiSquared {
	
	public static double getPDF(int deg_freedom, double x) {
		double half_freedom = ((double)deg_freedom)/2.0;
		double first = Math.pow(0.5, half_freedom)/FunctionSupport.Gamma(half_freedom);
		
		double second = Math.pow(x, half_freedom-1.0)/Math.exp(x/2.0);
		
		return first*second;
	}
	
	public static double getCDF(int deg_freedom, double x) {
		double half_freedom = ((double)deg_freedom)/2.0;
		
		double a = FunctionSupport.IncompleteGamma(half_freedom, x);
		double b = FunctionSupport.Gamma(half_freedom);
		
		if(Double.isInfinite(a) && Double.isInfinite(b))
			return 1.0;
		else if(Double.isInfinite(a))
			return a;
		else if(Double.isInfinite(b))
			return 0;
		else
			return a/b;
	}
}
