1 | package de.uka.ipd.sdq.probfunction.math.apache.distribution; |
2 | |
3 | import org.apache.commons.math.MathException; |
4 | |
5 | |
6 | /** |
7 | * SDQ implementation of LogNormalDistFromMoments based on apache.commons.math |
8 | * |
9 | * @author joerg |
10 | * |
11 | */ |
12 | public class LognormalDistributionFromMomentsImpl extends LognormalDistributionImpl |
13 | { |
14 | |
15 | /** Serializable version identifier */ |
16 | private static final long serialVersionUID = -7504569402218262740L; |
17 | |
18 | public LognormalDistributionFromMomentsImpl (double mean, double variance) throws MathException |
19 | { |
20 | super (calculateMu (mean, variance), calculateSigma (mean, variance)); |
21 | } |
22 | |
23 | /** |
24 | * Calculate mu from mean and variance. |
25 | * u=e^(M+S^2 /2) |
26 | * M=ln(u)-S^2/2 |
27 | * |
28 | * @param mean The mean value |
29 | * @param var The variance |
30 | * @return The value of mu |
31 | * @throws MathException |
32 | */ |
33 | private static double calculateMu (double mean, double var) throws MathException { |
34 | final double sigma2 = calculateSigma2 (mean, var); |
35 | return Math.log (mean) - sigma2 / 2.0; |
36 | } |
37 | |
38 | |
39 | /** |
40 | * Calculate sigma from mean and variance. |
41 | * |
42 | * @param mean The mean value |
43 | * @param var The variance |
44 | * @return The value of sigma |
45 | * @throws MathException |
46 | */ |
47 | private static double calculateSigma (double mean, double var) throws MathException { |
48 | final double sigma2 = calculateSigma2 (mean, var); |
49 | return Math.sqrt(sigma2); |
50 | } |
51 | |
52 | /** |
53 | * Calculate value of sigma^2 from mean and variance. |
54 | * |
55 | * for x = sigma^2 |
56 | * and M=ln(u)-S^2/2 |
57 | * |
58 | * o^2 = e^(x+2M)*(e^x-1) |
59 | * u^2*e^3x/2 = o^2+u^2*e^x/2 |
60 | * e^x*e^x/2 = o^2/u^2 + e^x/2 |
61 | * x=ln(o^2/u^2 +1) |
62 | * |
63 | * @param mean The mean value |
64 | * @param var The variance |
65 | * @return The value of sigma^2 |
66 | * @throws MathException |
67 | */ |
68 | private static double calculateSigma2 (double mean, double var) throws MathException |
69 | { |
70 | if (mean <= 0) |
71 | throw new MathException ("Mean has to be positive"); |
72 | if (var <= 0) |
73 | throw new MathException ("Variance has to be positive"); |
74 | |
75 | double o2 = var; |
76 | double u2 = (mean * mean); |
77 | return Math.log (o2 / u2 + 1); |
78 | } |
79 | |
80 | |
81 | } |