Debian Codegraph @

src/dr/math/RiemannApproximation.java

raw symbols SCIP
1 /*
2 * RiemannApproximation.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;
27
28 import dr.math.distributions.NormalDistribution;
29
30 /**
31 * Approximates the integral of a given function using Riemann integration
32 *
33 * @author Alexei Drummond
34 * @version $Id: RiemannApproximation.java,v 1.3 2005/05/24 20:26:01 rambaut Exp $
35 */
36 public class RiemannApproximation implements Integral {
37
38 public enum variant {
39 UPPER,
40 LOWER,
41 MIDPOINT,
42 TRAPEZOID
43 }
44
45 public RiemannApproximation(int sampleSize, variant mode) {
46 this.mode = mode;
47 this.sampleSize = sampleSize;
48 }
49
50 public RiemannApproximation(int sampleSize) {
51 this(sampleSize, variant.UPPER);
52 }
53
54 /**
55 * @param f the function whose integral is of interest
56 * @param min the minimum value of the function
57 * @param max the upper limit of the function
58 * @return the approximate integral of the given function
59 * within the given range.
60 */
61 public double integrate(UnivariateFunction f, double min, double max) {
62
63 double integral = 0.0;
64
65 double gridpoint = min;
66 double step = (max - min) / sampleSize;
67
68 switch (mode){
69 case UPPER:
70 for (int i = 1; i <= sampleSize; i++) {
71 gridpoint += step;
72 integral += f.evaluate(gridpoint);
73 }
74 break;
75 case LOWER:
76 for (int i = 1; i <= sampleSize; i++) {
77 integral += f.evaluate(gridpoint);
78 gridpoint += step;
79 }
80 break;
81 case MIDPOINT:
82 for (int i = 1; i <= sampleSize; i++) {
83 integral += f.evaluate(gridpoint + step/2);
84 gridpoint += step;
85 }
86 break;
87 case TRAPEZOID:
88 for (int i = 1; i <= sampleSize; i++) {
89 integral += (f.evaluate(gridpoint) + f.evaluate(gridpoint+step))/2;
90 gridpoint += step;
91 }
92 break;
93 }
94 integral *= (max - min) / (double) sampleSize;
95 return integral;
96 }
97
98 // this does the addition on a log scale, in case of underflow
99
100 public double logIntegrate(UnivariateFunction f, double min, double max){
101 double logIntegral = Double.NEGATIVE_INFINITY;
102
103 double gridpoint = min;
104 double step = (max - min) / sampleSize;
105
106 switch (mode){
107 case UPPER:
108 for (int i = 1; i <= sampleSize; i++) {
109 gridpoint += step;
110 logIntegral = LogTricks.logSum(logIntegral, Math.log(f.evaluate(gridpoint)));
111 }
112 break;
113 case LOWER:
114 for (int i = 1; i <= sampleSize; i++) {
115 logIntegral = LogTricks.logSum(logIntegral, Math.log(f.evaluate(gridpoint)));
116 gridpoint += step;
117 }
118 break;
119 case MIDPOINT:
120 for (int i = 1; i <= sampleSize; i++) {
121 logIntegral = LogTricks.logSum(logIntegral, Math.log(f.evaluate(gridpoint+step/2)));
122 gridpoint += step;
123 }
124 break;
125 case TRAPEZOID:
126 for (int i = 1; i <= sampleSize; i++) {
127 logIntegral = LogTricks.logSum(logIntegral,
128 Math.log((f.evaluate(gridpoint) + f.evaluate(gridpoint+step))/2));
129 gridpoint += step;
130 }
131 break;
132 }
133 logIntegral += Math.log((max - min) / (double) sampleSize);
134 return logIntegral;
135 }
136
137 public variant getMode() {
138 return mode;
139 }
140
141
142 public static void main(String[] args) {
143
144 UnivariateFunction normalPDF = new NormalDistribution(0.0, 1.0).getProbabilityDensityFunction();
145 UnivariateFunction normalPDF2 = new NormalDistribution(0.0, 1.0).getProbabilityDensityFunction();
146 UnivariateFunction normalPDF3 = new NormalDistribution(0.0, 1.0).getProbabilityDensityFunction();
147
148 double Z = 1.0;
149 //double Z = Math.sqrt(2*Math.PI);
150 CompoundFunction threeNormals = new CompoundFunction(new UnivariateFunction[]{normalPDF, normalPDF2, normalPDF3}, Z);
151
152 System.out.println("Riemann approximation to the integral of a three normal distribution:");
153 RiemannApproximation integrator = new RiemannApproximation(100000);
154 System.out.println("integral(N(0.0, 1.0))=" + integrator.integrate(normalPDF, -4.0, 4.0));
155 System.out.println("integral(N(1.0, 2.0))=" + integrator.integrate(normalPDF2, -8.0, 8.0));
156 System.out.println("integral(N(2.0, 3.0))=" + integrator.integrate(normalPDF3, -16.0, 16.0));
157
158
159 double integral = integrator.integrate(threeNormals, -16.0, 16.0);
160 System.out.println("Riemann approximation to the integral of the compound of three normal distribution:");
161 System.out.println("integral(N(0.0, 1.0)*N(1.0, 2.0)*N(2.0, 3.0))=" + integral);
162 System.out.println("Estimate normalizing constant is " + (1.0 / integral));
163
164 /*System.out.println("Ten monte carlo approximations to the integral of a normal distribution:");
165 MonteCarloIntegral integrator2 = new MonteCarloIntegral(10000);
166 for (int i = 0; i < 10; i++) {
167 System.out.println(integrator2.integrate(normalPDF, -4.0, 4.0));
168 }*/
169 }
170
171 public int getSampleSize(){
172 return sampleSize;
173 }
174
175 private int sampleSize;
176
177 private final variant mode;
178 }