1 16 package org.apache.commons.math.distribution; 17 18 import java.io.Serializable ; 19 20 import org.apache.commons.math.MathException; 21 import org.apache.commons.math.special.Gamma; 22 23 28 public class GammaDistributionImpl extends AbstractContinuousDistribution 29 implements GammaDistribution, Serializable { 30 31 32 static final long serialVersionUID = -3239549463135430361L; 33 34 35 private double alpha; 36 37 38 private double beta; 39 40 45 public GammaDistributionImpl(double alpha, double beta) { 46 super(); 47 setAlpha(alpha); 48 setBeta(beta); 49 } 50 51 68 public double cumulativeProbability(double x) throws MathException{ 69 double ret; 70 71 if (x <= 0.0) { 72 ret = 0.0; 73 } else { 74 ret = Gamma.regularizedGammaP(getAlpha(), x / getBeta()); 75 } 76 77 return ret; 78 } 79 80 93 public double inverseCumulativeProbability(final double p) 94 throws MathException { 95 if (p == 0) { 96 return 0d; 97 } 98 if (p == 1) { 99 return Double.POSITIVE_INFINITY; 100 } 101 return super.inverseCumulativeProbability(p); 102 } 103 104 109 public void setAlpha(double alpha) { 110 if (alpha <= 0.0) { 111 throw new IllegalArgumentException ("alpha must be positive"); 112 } 113 this.alpha = alpha; 114 } 115 116 120 public double getAlpha() { 121 return alpha; 122 } 123 124 129 public void setBeta(double beta) { 130 if (beta <= 0.0) { 131 throw new IllegalArgumentException ("beta must be positive"); 132 } 133 this.beta = beta; 134 } 135 136 140 public double getBeta() { 141 return beta; 142 } 143 144 153 protected double getDomainLowerBound(double p) { 154 return Double.MIN_VALUE; 156 } 157 158 167 protected double getDomainUpperBound(double p) { 168 172 double ret; 173 174 if (p < .5) { 175 ret = getAlpha() * getBeta(); 177 } else { 178 ret = Double.MAX_VALUE; 180 } 181 182 return ret; 183 } 184 185 193 protected double getInitialDomain(double p) { 194 197 double ret; 198 199 if (p < .5) { 200 ret = getAlpha() * getBeta() * .5; 202 } else { 203 ret = getAlpha() * getBeta(); 205 } 206 207 return ret; 208 } 209 } 210 | Popular Tags |