1 | /** |
2 | * |
3 | */ |
4 | package de.uka.ipd.sdq.probfunction.math.apache.distribution; |
5 | |
6 | import org.apache.commons.math.MathException; |
7 | import org.apache.commons.math.distribution.AbstractContinuousDistribution; |
8 | |
9 | |
10 | /** |
11 | * @author joerg |
12 | * |
13 | */ |
14 | public class UniformDistributionImpl extends AbstractContinuousDistribution { |
15 | |
16 | |
17 | /** |
18 | * |
19 | */ |
20 | private static final long serialVersionUID = 4771624459254238355L; |
21 | double a,b; |
22 | |
23 | |
24 | |
25 | public double getA() { |
26 | return a; |
27 | } |
28 | |
29 | |
30 | public double getB() { |
31 | return b; |
32 | } |
33 | |
34 | |
35 | |
36 | public UniformDistributionImpl(double a, double b) throws MathException |
37 | { |
38 | if(b<a) |
39 | throw new MathException("Second value has to be greater than first value of interval"); |
40 | |
41 | this.a = a; |
42 | this.b = b; |
43 | |
44 | |
45 | |
46 | } |
47 | |
48 | |
49 | /* (non-Javadoc) |
50 | * @see org.apache.commons.math.distribution.AbstractIntegerDistribution#cumulativeProbability(int) |
51 | */ |
52 | @Override |
53 | public double cumulativeProbability(double x) throws MathException { |
54 | if(x<a) |
55 | return 0; |
56 | else if(x>b) |
57 | return 1; |
58 | |
59 | return (x-a)/(b-a); |
60 | } |
61 | |
62 | /* (non-Javadoc) |
63 | * @see org.apache.commons.math.distribution.AbstractIntegerDistribution#getDomainLowerBound(double) |
64 | */ |
65 | @Override |
66 | protected double getDomainLowerBound(double p) { |
67 | return a; |
68 | } |
69 | |
70 | /* (non-Javadoc) |
71 | * @see org.apache.commons.math.distribution.AbstractIntegerDistribution#getDomainUpperBound(double) |
72 | */ |
73 | @Override |
74 | protected double getDomainUpperBound(double p) { |
75 | return b; |
76 | } |
77 | |
78 | |
79 | |
80 | public double getMean() |
81 | { |
82 | return (a+b)/2; |
83 | } |
84 | |
85 | public double getVariance() |
86 | { |
87 | return 1/12.0*(b-a)*(b-a); |
88 | } |
89 | |
90 | |
91 | @Override |
92 | protected double getInitialDomain(double p) { |
93 | return getMean(); |
94 | } |
95 | |
96 | } |