Debian Codegraph @

src/dr/math/distributions/ReflectedNormalDistribution.java

raw symbols SCIP
1 /*
2 * ReflectedNormalDistribution.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
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 * reflected normal distribution (pdf, cdf, quantile)
37 *
38 * @author Alexei Drummond
39 * @author Marc Suchard
40 */
41 public class ReflectedNormalDistribution implements Distribution {
42 //
43 // Public stuff
44 //
45
46 double lower;
47 double upper;
48 double precision;
49 double m;
50 double sd;
51
52 /**
53 * Constructor
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); // 2 2 4 4 6 6
94 int B = 2 * (i / 2); // 0 2 2 4 4 6
95 int C = i % 2 == 0 ? -1 : 1; // 1 -1 1 -1 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 // final ReflectedNormalDistribution rnd = new ReflectedNormalDistribution(2, 2, 0.5, 2, 1e-6);
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); // fail if it takes too much time
165
166 double x;
167 try {
168 x = integrator.integrate(f, rnd.lower, rnd.upper);
169 // ptotErr += cdf != 0.0 ? Math.abs(x-cdf)/cdf : x;
170 // np += 1;
171 //assertTrue("" + shape + "," + scale + "," + value + " " + Math.abs(x-cdf)/x + "> 1e-6", Math.abs(1-cdf/x) < 1e-6);
172 System.out.println("Integrated pdf = " + x);
173
174 //System.out.println(shape + "," + scale + " " + value);
175 } catch (ConvergenceException e) {
176 // can't integrate , skip test
177 // System.out.println(shape + "," + scale + " skipped");
178 } catch (FunctionEvaluationException e) {
179
180 throw new RuntimeException("I have no idea why I am here.");
181
182 }
183
184 }
185
186 }