| 1 | package de.uka.ipd.sdq.probfunction.math.apache.impl; |
| 2 | |
| 3 | |
| 4 | import org.apache.commons.math.distribution.GammaDistributionImpl; |
| 5 | //import umontreal.iro.lecuyer.probdist.GammaDist; |
| 6 | //import umontreal.iro.lecuyer.probdist.GammaDistFromMoments; |
| 7 | import de.uka.ipd.sdq.probfunction.math.IGammaDistribution; |
| 8 | import de.uka.ipd.sdq.probfunction.math.IProbabilityDensityFunction; |
| 9 | import de.uka.ipd.sdq.probfunction.math.apache.distribution.GammaDistributionFromMomentsImpl; |
| 10 | import de.uka.ipd.sdq.probfunction.math.exception.DomainNotNumbersException; |
| 11 | import de.uka.ipd.sdq.probfunction.math.exception.FunctionNotInFrequencyDomainException; |
| 12 | import de.uka.ipd.sdq.probfunction.math.exception.FunctionNotInTimeDomainException; |
| 13 | import de.uka.ipd.sdq.probfunction.math.exception.FunctionsInDifferenDomainsException; |
| 14 | import de.uka.ipd.sdq.probfunction.math.exception.IncompatibleUnitsException; |
| 15 | import de.uka.ipd.sdq.probfunction.math.exception.InvalidSampleValueException; |
| 16 | import de.uka.ipd.sdq.probfunction.math.exception.NegativeDistanceException; |
| 17 | import de.uka.ipd.sdq.probfunction.math.exception.ProbabilityFunctionException; |
| 18 | import de.uka.ipd.sdq.probfunction.math.exception.ProbabilitySumNotOneException; |
| 19 | import de.uka.ipd.sdq.probfunction.math.exception.UnitNameNotSetException; |
| 20 | import de.uka.ipd.sdq.probfunction.math.exception.UnitNotSetException; |
| 21 | import de.uka.ipd.sdq.probfunction.math.exception.UnknownPDFTypeException; |
| 22 | import de.uka.ipd.sdq.probfunction.math.exception.UnorderedDomainException; |
| 23 | |
| 24 | /** |
| 25 | * @see IGammaDistribution |
| 26 | * @author martens |
| 27 | */ |
| 28 | public class GammaDistribution extends AbstractContinousPDF implements IGammaDistribution { |
| 29 | |
| 30 | /** |
| 31 | * Constructs a GammaDist object with parameters alpha and theta. |
| 32 | * @param alpha shape parameter |
| 33 | * @param theta scale parameter = 1 / rate parameter |
| 34 | */ |
| 35 | public GammaDistribution(double alpha, double theta) { |
| 36 | super(); |
| 37 | /* |
| 38 | * In contrast to SSJ apache common math uses beta as a direct replacement of theta! |
| 39 | * No 1/theta required! |
| 40 | */ |
| 41 | this.internalFunction = new GammaDistributionImpl(alpha, theta); |
| 42 | } |
| 43 | |
| 44 | @Override |
| 45 | public double density(double x) { |
| 46 | //SSJ compatibility |
| 47 | if(x == 0.0) |
| 48 | return 0.0; |
| 49 | |
| 50 | return super.density(x); |
| 51 | } |
| 52 | |
| 53 | protected GammaDistribution(){ |
| 54 | super(); |
| 55 | } |
| 56 | |
| 57 | private GammaDistribution(GammaDistributionImpl internal) { |
| 58 | super(); |
| 59 | this.internalFunction = internal; |
| 60 | } |
| 61 | |
| 62 | |
| 63 | public IProbabilityDensityFunction add(IProbabilityDensityFunction pdf) |
| 64 | throws FunctionsInDifferenDomainsException, |
| 65 | UnknownPDFTypeException, IncompatibleUnitsException { |
| 66 | throw new UnsupportedOperationException(); |
| 67 | } |
| 68 | |
| 69 | |
| 70 | public IProbabilityDensityFunction div(IProbabilityDensityFunction pdf) |
| 71 | throws FunctionsInDifferenDomainsException, |
| 72 | UnknownPDFTypeException, IncompatibleUnitsException { |
| 73 | throw new UnsupportedOperationException(); |
| 74 | } |
| 75 | |
| 76 | public IProbabilityDensityFunction getFourierTransform() |
| 77 | throws FunctionNotInTimeDomainException { |
| 78 | throw new UnsupportedOperationException(); |
| 79 | } |
| 80 | |
| 81 | |
| 82 | public IProbabilityDensityFunction getInverseFourierTransform() |
| 83 | throws FunctionNotInFrequencyDomainException { |
| 84 | throw new UnsupportedOperationException(); |
| 85 | } |
| 86 | |
| 87 | |
| 88 | public double getLowerDomainBorder() { |
| 89 | throw new UnsupportedOperationException(); |
| 90 | } |
| 91 | |
| 92 | |
| 93 | public double greaterThan(IProbabilityDensityFunction pdf) |
| 94 | throws ProbabilityFunctionException { |
| 95 | throw new UnsupportedOperationException(); |
| 96 | } |
| 97 | |
| 98 | |
| 99 | public double lessThan(IProbabilityDensityFunction pdf) |
| 100 | throws ProbabilityFunctionException { |
| 101 | throw new UnsupportedOperationException(); |
| 102 | } |
| 103 | |
| 104 | |
| 105 | public IProbabilityDensityFunction mult(IProbabilityDensityFunction pdf) |
| 106 | throws FunctionsInDifferenDomainsException, |
| 107 | UnknownPDFTypeException, IncompatibleUnitsException { |
| 108 | throw new UnsupportedOperationException(); |
| 109 | } |
| 110 | |
| 111 | |
| 112 | public double probabilisticEquals(IProbabilityDensityFunction pdf) |
| 113 | throws ProbabilityFunctionException { |
| 114 | throw new UnsupportedOperationException(); |
| 115 | } |
| 116 | |
| 117 | |
| 118 | public IProbabilityDensityFunction scale(double scalar) { |
| 119 | throw new UnsupportedOperationException(); |
| 120 | } |
| 121 | |
| 122 | |
| 123 | public IProbabilityDensityFunction sub(IProbabilityDensityFunction pdf) |
| 124 | throws FunctionsInDifferenDomainsException, |
| 125 | UnknownPDFTypeException, IncompatibleUnitsException { |
| 126 | throw new UnsupportedOperationException(); |
| 127 | } |
| 128 | |
| 129 | |
| 130 | public void checkConstrains() throws NegativeDistanceException, |
| 131 | ProbabilitySumNotOneException, FunctionNotInTimeDomainException, |
| 132 | UnitNotSetException, UnitNameNotSetException, |
| 133 | InvalidSampleValueException { |
| 134 | throw new UnsupportedOperationException(); |
| 135 | |
| 136 | } |
| 137 | |
| 138 | |
| 139 | |
| 140 | public Object getMedian() throws UnorderedDomainException { |
| 141 | throw new UnsupportedOperationException(); |
| 142 | } |
| 143 | |
| 144 | |
| 145 | public Object getPercentile(int p) throws IndexOutOfBoundsException, |
| 146 | UnorderedDomainException { |
| 147 | throw new UnsupportedOperationException(); |
| 148 | } |
| 149 | |
| 150 | public boolean hasOrderedDomain() { |
| 151 | throw new UnsupportedOperationException(); |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Shape parameter |
| 156 | */ |
| 157 | public double getAlpha() { |
| 158 | return ((GammaDistributionImpl)this.internalFunction).getAlpha(); |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Scale parameter beta |
| 163 | */ |
| 164 | public double getTheta() { |
| 165 | return ((GammaDistributionImpl)this.internalFunction).getBeta(); |
| 166 | } |
| 167 | |
| 168 | |
| 169 | public IProbabilityDensityFunction getCumulativeFunction() |
| 170 | throws FunctionNotInTimeDomainException { |
| 171 | throw new UnsupportedOperationException(); |
| 172 | } |
| 173 | |
| 174 | |
| 175 | public IProbabilityDensityFunction shiftDomain(double scalar) |
| 176 | throws DomainNotNumbersException { |
| 177 | double newMean = this.getArithmeticMeanValue() + scalar; |
| 178 | double newVariance = this.getVariance(); |
| 179 | return new GammaDistribution(new GammaDistributionFromMomentsImpl(newMean, newVariance)); |
| 180 | } |
| 181 | |
| 182 | |
| 183 | @Override |
| 184 | public double getVariance() |
| 185 | { |
| 186 | //variance is easier to calculate for gamma than sd |
| 187 | return this.getAlpha()*this.getTheta()*this.getTheta(); |
| 188 | } |
| 189 | |
| 190 | public IProbabilityDensityFunction stretchDomain(double scalar) { |
| 191 | double newMean = this.getArithmeticMeanValue() * scalar; |
| 192 | double newVariance = this.getVariance() * scalar * scalar; |
| 193 | return new GammaDistribution(new GammaDistributionFromMomentsImpl(newMean, newVariance)); |
| 194 | } |
| 195 | |
| 196 | @Override |
| 197 | public double getStandardDeviation() { |
| 198 | return Math.sqrt(this.getVariance()); |
| 199 | } |
| 200 | |
| 201 | @Override |
| 202 | public double getXinf() { |
| 203 | return 0.0; |
| 204 | } |
| 205 | |
| 206 | @Override |
| 207 | public double getXsup() { |
| 208 | return Double.POSITIVE_INFINITY; |
| 209 | } |
| 210 | |
| 211 | @Override |
| 212 | public double getArithmeticMeanValue() throws DomainNotNumbersException, |
| 213 | FunctionNotInTimeDomainException { |
| 214 | return this.getAlpha()*this.getTheta(); |
| 215 | } |
| 216 | } |