| 1 | package de.uka.ipd.sdq.probfunction.math.apache.impl; |
| 2 | |
| 3 | import org.apache.commons.math.MathException; |
| 4 | import org.apache.commons.math.distribution.AbstractContinuousDistribution; |
| 5 | import de.uka.ipd.sdq.probfunction.math.IContinousPDF; |
| 6 | import de.uka.ipd.sdq.probfunction.math.IProbabilityDensityFunction; |
| 7 | import de.uka.ipd.sdq.probfunction.math.IRandomGenerator; |
| 8 | import de.uka.ipd.sdq.probfunction.math.IUnit; |
| 9 | import de.uka.ipd.sdq.probfunction.math.exception.DomainNotNumbersException; |
| 10 | import de.uka.ipd.sdq.probfunction.math.exception.FunctionNotInFrequencyDomainException; |
| 11 | import de.uka.ipd.sdq.probfunction.math.exception.FunctionNotInTimeDomainException; |
| 12 | import de.uka.ipd.sdq.probfunction.math.exception.FunctionsInDifferenDomainsException; |
| 13 | import de.uka.ipd.sdq.probfunction.math.exception.IncompatibleUnitsException; |
| 14 | import de.uka.ipd.sdq.probfunction.math.exception.InvalidSampleValueException; |
| 15 | import de.uka.ipd.sdq.probfunction.math.exception.NegativeDistanceException; |
| 16 | import de.uka.ipd.sdq.probfunction.math.exception.ProbabilityFunctionException; |
| 17 | import de.uka.ipd.sdq.probfunction.math.exception.ProbabilitySumNotOneException; |
| 18 | import de.uka.ipd.sdq.probfunction.math.exception.UnitNameNotSetException; |
| 19 | import de.uka.ipd.sdq.probfunction.math.exception.UnitNotSetException; |
| 20 | import de.uka.ipd.sdq.probfunction.math.exception.UnknownPDFTypeException; |
| 21 | |
| 22 | |
| 23 | public abstract class AbstractContinousPDF implements IContinousPDF { |
| 24 | |
| 25 | protected AbstractContinuousDistribution internalFunction = null; |
| 26 | protected IRandomGenerator sampleDrawer = null; |
| 27 | |
| 28 | |
| 29 | public AbstractContinousPDF() { |
| 30 | super(); |
| 31 | } |
| 32 | |
| 33 | |
| 34 | |
| 35 | public double getCoefficientOfVariance() { |
| 36 | return this.getStandardDeviation() / this.getArithmeticMeanValue(); |
| 37 | } |
| 38 | |
| 39 | |
| 40 | public double getVariance() { |
| 41 | double std = this.getStandardDeviation(); |
| 42 | return std * std; |
| 43 | } |
| 44 | |
| 45 | public double getProbabilitySum() { |
| 46 | return 1; |
| 47 | } |
| 48 | |
| 49 | public boolean isInFrequencyDomain() { |
| 50 | return false; |
| 51 | } |
| 52 | |
| 53 | public boolean isInTimeDomain() { |
| 54 | return true; |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Returns the distribution function F(x) as defined in |
| 59 | * {@link umontreal.iro.lecuyer.probdist.Distribution#cdf(double)} |
| 60 | * @param x |
| 61 | * @return F(x) |
| 62 | */ |
| 63 | public double cdf(double x){ |
| 64 | try { |
| 65 | return this.internalFunction.cumulativeProbability(x); |
| 66 | } catch (MathException e) { |
| 67 | throw new ProbabilityFunctionException(e.getLocalizedMessage()); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | |
| 72 | /** |
| 73 | * Returns f(x), the density evaluated at x, as defined in |
| 74 | * {@link umontreal.iro.lecuyer.probdist.ContinousDistribution#density(double)} |
| 75 | * @param x |
| 76 | * @return F(x) |
| 77 | */ |
| 78 | public double density(double x){ |
| 79 | return this.internalFunction.density(x); |
| 80 | } |
| 81 | |
| 82 | public double drawSample() { |
| 83 | if (sampleDrawer == null) { |
| 84 | throw new ProbabilityFunctionException("RNG was not set!"); |
| 85 | |
| 86 | } |
| 87 | try { |
| 88 | return internalFunction.inverseCumulativeProbability(sampleDrawer.random()); |
| 89 | } catch (MathException e) { |
| 90 | throw new ProbabilityFunctionException(e.getLocalizedMessage()); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | |
| 95 | |
| 96 | @Override |
| 97 | public IProbabilityDensityFunction add(IProbabilityDensityFunction pdf) |
| 98 | throws FunctionsInDifferenDomainsException, |
| 99 | UnknownPDFTypeException, IncompatibleUnitsException { |
| 100 | throw new UnsupportedOperationException(); |
| 101 | } |
| 102 | |
| 103 | |
| 104 | |
| 105 | @Override |
| 106 | public IProbabilityDensityFunction div(IProbabilityDensityFunction pdf) |
| 107 | throws FunctionsInDifferenDomainsException, |
| 108 | UnknownPDFTypeException, IncompatibleUnitsException { |
| 109 | throw new UnsupportedOperationException(); |
| 110 | |
| 111 | } |
| 112 | |
| 113 | |
| 114 | |
| 115 | @Override |
| 116 | public IProbabilityDensityFunction getCumulativeFunction() |
| 117 | throws FunctionNotInTimeDomainException { |
| 118 | throw new UnsupportedOperationException(); |
| 119 | } |
| 120 | |
| 121 | |
| 122 | |
| 123 | @Override |
| 124 | public IProbabilityDensityFunction getFourierTransform() |
| 125 | throws FunctionNotInTimeDomainException { |
| 126 | throw new UnsupportedOperationException(); |
| 127 | } |
| 128 | |
| 129 | |
| 130 | |
| 131 | @Override |
| 132 | public IProbabilityDensityFunction getInverseFourierTransform() |
| 133 | throws FunctionNotInFrequencyDomainException { |
| 134 | throw new UnsupportedOperationException(); |
| 135 | } |
| 136 | |
| 137 | |
| 138 | |
| 139 | |
| 140 | |
| 141 | |
| 142 | |
| 143 | @Override |
| 144 | public double greaterThan(IProbabilityDensityFunction pdf) |
| 145 | throws ProbabilityFunctionException { |
| 146 | throw new UnsupportedOperationException(); |
| 147 | } |
| 148 | |
| 149 | |
| 150 | |
| 151 | @Override |
| 152 | public double lessThan(IProbabilityDensityFunction pdf) |
| 153 | throws ProbabilityFunctionException { |
| 154 | throw new UnsupportedOperationException(); |
| 155 | } |
| 156 | |
| 157 | |
| 158 | |
| 159 | @Override |
| 160 | public IProbabilityDensityFunction mult(IProbabilityDensityFunction pdf) |
| 161 | throws FunctionsInDifferenDomainsException, |
| 162 | UnknownPDFTypeException, IncompatibleUnitsException { |
| 163 | throw new UnsupportedOperationException(); |
| 164 | } |
| 165 | |
| 166 | |
| 167 | |
| 168 | @Override |
| 169 | public double probabilisticEquals(IProbabilityDensityFunction pdf) |
| 170 | throws ProbabilityFunctionException { |
| 171 | throw new UnsupportedOperationException(); |
| 172 | } |
| 173 | |
| 174 | |
| 175 | |
| 176 | @Override |
| 177 | public IProbabilityDensityFunction scale(double scalar) { |
| 178 | throw new UnsupportedOperationException(); |
| 179 | } |
| 180 | |
| 181 | |
| 182 | |
| 183 | @Override |
| 184 | public IProbabilityDensityFunction shiftDomain(double scalar) |
| 185 | throws DomainNotNumbersException { |
| 186 | throw new UnsupportedOperationException(); |
| 187 | } |
| 188 | |
| 189 | |
| 190 | |
| 191 | @Override |
| 192 | public IProbabilityDensityFunction stretchDomain(double scalar) { |
| 193 | throw new UnsupportedOperationException(); |
| 194 | } |
| 195 | |
| 196 | |
| 197 | |
| 198 | @Override |
| 199 | public IProbabilityDensityFunction sub(IProbabilityDensityFunction pdf) |
| 200 | throws FunctionsInDifferenDomainsException, |
| 201 | UnknownPDFTypeException, IncompatibleUnitsException { |
| 202 | throw new UnsupportedOperationException(); |
| 203 | } |
| 204 | |
| 205 | |
| 206 | |
| 207 | @Override |
| 208 | public void checkConstrains() throws NegativeDistanceException, |
| 209 | ProbabilitySumNotOneException, FunctionNotInTimeDomainException, |
| 210 | UnitNotSetException, UnitNameNotSetException, |
| 211 | InvalidSampleValueException { |
| 212 | throw new UnsupportedOperationException(); |
| 213 | |
| 214 | } |
| 215 | |
| 216 | |
| 217 | |
| 218 | |
| 219 | |
| 220 | |
| 221 | |
| 222 | /** |
| 223 | * Computes and returns the inverse distribution function x = F^{-1}(u). |
| 224 | * This can be used to get the quantiles of the distribution. |
| 225 | * Pass 0.9 to get the x value of this function for which this.cdf(x) = 0.9 holds. |
| 226 | * Uses the implementation of the concrete function in the {@link umontreal.iro.lecuyer.probdist} package. |
| 227 | * @param u - value in the interval [0, 1] for which the inverse distribution function is evaluated |
| 228 | * @return the inverse distribution function evaluated at u |
| 229 | */ |
| 230 | public double inverseF(double u) { |
| 231 | |
| 232 | try { |
| 233 | return this.internalFunction.inverseCumulativeProbability(u); |
| 234 | } catch (MathException e) { |
| 235 | e.printStackTrace(); |
| 236 | throw new ProbabilityFunctionException(e.getLocalizedMessage()); |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | |
| 241 | public IUnit getUnit() { |
| 242 | return null; |
| 243 | } |
| 244 | |
| 245 | |
| 246 | |
| 247 | |
| 248 | |
| 249 | |
| 250 | } |