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