| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
package dr.math.distributions; |
| 27 |
|
| 28 |
import dr.math.UnivariateFunction; |
| 29 |
import dr.util.DataTable; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
public class UniformDistribution implements Distribution { |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
public UniformDistribution(double lower, double upper) { |
| 48 |
this.lower = lower; |
| 49 |
this.upper = upper; |
| 50 |
|
| 51 |
assert !Double.isInfinite(lower) : "bounds on a uniform distribution cannot be infinite"; |
| 52 |
assert !Double.isInfinite(upper) : "bounds on a uniform distribution cannot be infinite"; |
| 53 |
} |
| 54 |
|
| 55 |
public double pdf(double x) { |
| 56 |
return pdf(x, lower, upper); |
| 57 |
} |
| 58 |
|
| 59 |
public double logPdf(double x) { |
| 60 |
return logPdf(x, lower, upper); |
| 61 |
} |
| 62 |
|
| 63 |
public double cdf(double x) { |
| 64 |
return cdf(x, lower, upper); |
| 65 |
} |
| 66 |
|
| 67 |
public double quantile(double y) { |
| 68 |
return quantile(y, lower, upper); |
| 69 |
} |
| 70 |
|
| 71 |
public double mean() { |
| 72 |
return mean(lower, upper); |
| 73 |
} |
| 74 |
|
| 75 |
public double variance() { |
| 76 |
return variance(lower, upper); |
| 77 |
} |
| 78 |
|
| 79 |
public final UnivariateFunction getProbabilityDensityFunction() { |
| 80 |
return pdfFunction; |
| 81 |
} |
| 82 |
|
| 83 |
private final UnivariateFunction pdfFunction = new UnivariateFunction() { |
| 84 |
public final double evaluate(double x) { |
| 85 |
return pdf(x); |
| 86 |
} |
| 87 |
|
| 88 |
public final double getLowerBound() { |
| 89 |
return lower; |
| 90 |
} |
| 91 |
|
| 92 |
public final double getUpperBound() { |
| 93 |
return upper; |
| 94 |
} |
| 95 |
}; |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
public static double pdf(double x, double lower, double upper) { |
| 107 |
return (x >= lower && x <= upper ? 1.0 / (upper - lower) : 0.0); |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
public static double logPdf(double x, double lower, double upper) { |
| 119 |
if (x < lower || x > upper) return Double.NEGATIVE_INFINITY; |
| 120 |
|
| 121 |
|
| 122 |
return - Math.log(upper - lower); |
| 123 |
|
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
public static double cdf(double x, double lower, double upper) { |
| 135 |
if (x < lower) return 0.0; |
| 136 |
if (x > upper) return 1.0; |
| 137 |
return (x - lower) / (upper - lower); |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
public static double quantile(double y, double lower, double upper) { |
| 150 |
if (!(y >= 0.0 && y <= 1.0)) throw new IllegalArgumentException("y must in range [0,1]"); |
| 151 |
return (y * (upper - lower)) + lower; |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
public static double mean(double lower, double upper) { |
| 162 |
return (upper + lower) / 2; |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
public static double variance(double lower, double upper) { |
| 173 |
return (upper - lower) * (upper - lower) / 12; |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
private final double upper; |
| 179 |
private final double lower; |
| 180 |
} |