| 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 org.apache.commons.math.ConvergenceException; |
| 30 |
import org.apache.commons.math.FunctionEvaluationException; |
| 31 |
import org.apache.commons.math.analysis.UnivariateRealFunction; |
| 32 |
import org.apache.commons.math.analysis.integration.RombergIntegrator; |
| 33 |
import org.apache.commons.math.analysis.integration.UnivariateRealIntegrator; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
public class ReflectedNormalDistribution implements Distribution { |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
double lower; |
| 47 |
double upper; |
| 48 |
double precision; |
| 49 |
double m; |
| 50 |
double sd; |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
public ReflectedNormalDistribution(double mean, double sd, double lower, double upper, double precision) { |
| 56 |
this.m = mean; |
| 57 |
this.sd = sd; |
| 58 |
this.lower = lower; |
| 59 |
this.upper = upper; |
| 60 |
this.precision = precision; |
| 61 |
} |
| 62 |
|
| 63 |
public double getMean() { |
| 64 |
return m; |
| 65 |
} |
| 66 |
|
| 67 |
public void setMean(double value) { |
| 68 |
m = value; |
| 69 |
} |
| 70 |
|
| 71 |
public double getSD() { |
| 72 |
return sd; |
| 73 |
} |
| 74 |
|
| 75 |
public void setSD(double value) { |
| 76 |
sd = value; |
| 77 |
} |
| 78 |
|
| 79 |
public double pdf(double x) { |
| 80 |
|
| 81 |
if (x < lower) return 0; |
| 82 |
if (x > upper) return 0; |
| 83 |
|
| 84 |
double pdf = NormalDistribution.pdf(x, m, sd); |
| 85 |
double newPDF = pdf; |
| 86 |
|
| 87 |
System.out.println("N(" + x + ",m,sd)=" + pdf); |
| 88 |
|
| 89 |
int i = 1; |
| 90 |
do { |
| 91 |
pdf = newPDF; |
| 92 |
|
| 93 |
int A = 2 * ((i + 1) / 2); |
| 94 |
int B = 2 * (i / 2); |
| 95 |
int C = i % 2 == 0 ? -1 : 1; |
| 96 |
|
| 97 |
double leftPDF = NormalDistribution.pdf(A * lower - C * x - B * upper, m, sd); |
| 98 |
double rightPDF = NormalDistribution.pdf(A * upper - C * x - B * lower, m, sd); |
| 99 |
newPDF = leftPDF + rightPDF + pdf; |
| 100 |
|
| 101 |
i += 1; |
| 102 |
|
| 103 |
System.out.println("newPDF=" + newPDF + " A=" + A + " B=" + B + " C=" + C); |
| 104 |
|
| 105 |
|
| 106 |
} while (newPDF - pdf > precision); |
| 107 |
|
| 108 |
return newPDF; |
| 109 |
} |
| 110 |
|
| 111 |
public double logPdf(double x) { |
| 112 |
throw new RuntimeException("Not implemented!"); |
| 113 |
} |
| 114 |
|
| 115 |
public double cdf(double x) { |
| 116 |
throw new RuntimeException("Not implemented!"); |
| 117 |
} |
| 118 |
|
| 119 |
public double quantile(double y) { |
| 120 |
throw new RuntimeException("Not implemented!"); |
| 121 |
} |
| 122 |
|
| 123 |
public double mean() { |
| 124 |
throw new RuntimeException("Not implemented!"); |
| 125 |
} |
| 126 |
|
| 127 |
public double variance() { |
| 128 |
throw new RuntimeException("Not implemented!"); |
| 129 |
} |
| 130 |
|
| 131 |
public final UnivariateFunction getProbabilityDensityFunction() { |
| 132 |
return pdfFunction; |
| 133 |
} |
| 134 |
|
| 135 |
private final UnivariateFunction pdfFunction = new UnivariateFunction() { |
| 136 |
public final double evaluate(double x) { |
| 137 |
return pdf(x); |
| 138 |
} |
| 139 |
|
| 140 |
public final double getLowerBound() { |
| 141 |
return lower; |
| 142 |
} |
| 143 |
|
| 144 |
public final double getUpperBound() { |
| 145 |
return upper; |
| 146 |
} |
| 147 |
}; |
| 148 |
|
| 149 |
public static void main(String[] args) { |
| 150 |
|
| 151 |
|
| 152 |
final ReflectedNormalDistribution rnd = new ReflectedNormalDistribution(2, 2, 1, 2, 1e-6); |
| 153 |
|
| 154 |
rnd.pdf(1); |
| 155 |
|
| 156 |
UnivariateRealFunction f = new UnivariateRealFunction() { |
| 157 |
public double value(double v) throws FunctionEvaluationException { |
| 158 |
return rnd.pdf(v); |
| 159 |
} |
| 160 |
}; |
| 161 |
|
| 162 |
final UnivariateRealIntegrator integrator = new RombergIntegrator(); |
| 163 |
integrator.setAbsoluteAccuracy(1e-14); |
| 164 |
integrator.setMaximalIterationCount(16); |
| 165 |
|
| 166 |
double x; |
| 167 |
try { |
| 168 |
x = integrator.integrate(f, rnd.lower, rnd.upper); |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
System.out.println("Integrated pdf = " + x); |
| 173 |
|
| 174 |
|
| 175 |
} catch (ConvergenceException e) { |
| 176 |
|
| 177 |
|
| 178 |
} catch (FunctionEvaluationException e) { |
| 179 |
|
| 180 |
throw new RuntimeException("I have no idea why I am here."); |
| 181 |
|
| 182 |
} |
| 183 |
|
| 184 |
} |
| 185 |
|
| 186 |
} |