1 16 package org.apache.commons.math.distribution; 17 18 import java.io.Serializable ; 19 20 import org.apache.commons.math.MathException; 21 22 27 public class ChiSquaredDistributionImpl 28 extends AbstractContinuousDistribution 29 implements ChiSquaredDistribution, Serializable { 30 31 32 static final long serialVersionUID = -8352658048349159782L; 33 34 35 private GammaDistribution gamma; 36 37 41 public ChiSquaredDistributionImpl(double degreesOfFreedom) { 42 super(); 43 setGamma(DistributionFactory.newInstance().createGammaDistribution( 44 degreesOfFreedom / 2.0, 2.0)); 45 } 46 47 51 public void setDegreesOfFreedom(double degreesOfFreedom) { 52 getGamma().setAlpha(degreesOfFreedom / 2.0); 53 } 54 55 59 public double getDegreesOfFreedom() { 60 return getGamma().getAlpha() * 2.0; 61 } 62 63 70 public double cumulativeProbability(double x) throws MathException { 71 return getGamma().cumulativeProbability(x); 72 } 73 74 87 public double inverseCumulativeProbability(final double p) 88 throws MathException { 89 if (p == 0) { 90 return 0d; 91 } 92 if (p == 1) { 93 return Double.POSITIVE_INFINITY; 94 } 95 return super.inverseCumulativeProbability(p); 96 } 97 98 107 protected double getDomainLowerBound(double p) { 108 return Double.MIN_VALUE * getGamma().getBeta(); 109 } 110 111 120 protected double getDomainUpperBound(double p) { 121 124 double ret; 125 126 if (p < .5) { 127 ret = getDegreesOfFreedom(); 129 } else { 130 ret = Double.MAX_VALUE; 132 } 133 134 return ret; 135 } 136 137 145 protected double getInitialDomain(double p) { 146 149 double ret; 150 151 if (p < .5) { 152 ret = getDegreesOfFreedom() * .5; 154 } else { 155 ret = getDegreesOfFreedom(); 157 } 158 159 return ret; 160 } 161 162 166 private void setGamma(GammaDistribution gamma) { 167 this.gamma = gamma; 168 } 169 170 174 private GammaDistribution getGamma() { 175 return gamma; 176 } 177 } 178 | Popular Tags |