Debian Codegraph @

src/dr/math/GeneralisedGaussLaguerreQuadrature.java

raw symbols SCIP
1 /*
2 * GeneralisedGaussLaguerreQuadrature.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 /**
29 * Approximate the integral from min to +INF of x^alpha * e^-Bx * f(x) by generalised Gauss-Riemann quadrature
30 *
31 * Adapted from Numerical Recipes
32 */
33 public class GeneralisedGaussLaguerreQuadrature implements Integral {
34
35 private double B;
36 private double alpha;
37 final private int noPoints;
38 final private double[] abscissae;
39 final private double[] coefficients;
40
41 public GeneralisedGaussLaguerreQuadrature(double B, double alpha, int noPoints){
42 this.B = B;
43 this.alpha = alpha;
44 this.noPoints = noPoints;
45 abscissae = new double[noPoints];
46 coefficients = new double[noPoints];
47 setupArrays();
48 }
49
50 public GeneralisedGaussLaguerreQuadrature(int noPoints){
51 this.noPoints = noPoints;
52 abscissae = new double[noPoints];
53 coefficients = new double[noPoints];
54 }
55
56 public void setB(double B){
57 this.B = B;
58 setupArrays();
59 }
60
61 public void setAlpha(double alpha){
62 this.alpha = alpha;
63 setupArrays();
64 }
65
66 public void setAlphaAndB(double alpha, double B){
67 this.alpha = alpha;
68 this.B = B;
69 setupArrays();
70 }
71
72 private void setupArrays(){
73 final int maxIterations = 10;
74 final double eps = 1E-14;
75
76 double z = 0;
77
78 double p1=0, p2=0, p3=0, pp=0;
79
80 for(int i=0; i<noPoints; i++){
81 if(i==0){
82 z = (1.0+alpha)*(3.0+0.92*alpha)/(1.0+2.4*noPoints+1.8*alpha);
83 } else if (i==1){
84 z += (15.0+6.25*alpha)/(1.0+0.9*alpha+2.5*noPoints);
85 } else {
86 double ai = i-1;
87 z += ((1.0+2.55*ai)/(1.9*ai) + 1.26*ai*alpha/(1.0+3.5*ai))*(z-abscissae[i-2])/(1.0+0.3*alpha);
88 }
89
90 boolean failed = true;
91
92 for(int its = 0 ; its<maxIterations; its++){
93 p1 = 1.0;
94 p2 = 0.0;
95 for(int j=0; j<noPoints; j++){
96 p3 = p2;
97 p2 = p1;
98 p1 = ((2*j+1+alpha-z)*p2-(j+alpha)*p3)/(j+1);
99 }
100 pp = (noPoints*p1-(noPoints+alpha)*p2)/z;
101 double z1=z;
102 z=z1-p1/pp;
103 if(Math.abs(z-z1) <= eps){
104 failed = false;
105 break;
106 }
107 }
108 if(failed){
109 throw new RuntimeException("Too many iterations");
110 }
111 abscissae[i] = z;
112
113 coefficients[i] = -Math.exp(GammaFunction.lnGamma(alpha+noPoints) - GammaFunction.lnGamma((double)noPoints))/
114 (pp*noPoints*p2);
115
116 }
117
118 }
119
120
121 public double integrate(UnivariateFunction f, double min, double max) {
122 if(max!=Double.POSITIVE_INFINITY){
123 throw new RuntimeException("Gauss-Laguerre quadrature is for integration with an infinite upper limit");
124 }
125 else{
126 return integrate(f, min);
127 }
128 }
129
130 public double logIntegrate(UnivariateFunction f, double min, double max) {
131 if(max!=Double.POSITIVE_INFINITY){
132 throw new RuntimeException("Gauss-Laguerre quadrature is for integration with an infinite upper limit");
133 }
134 else{
135 return logIntegrate(f, min);
136 }
137 }
138
139 public double integrate(UnivariateFunction f, double min){
140 double integral = 0;
141
142 for(int i=0; i<noPoints; i++){
143 integral += coefficients[i]*f.evaluate(abscissae[i]/B + min);
144 }
145
146 integral *= 1/Math.pow(B, alpha+1);
147
148 return integral;
149 }
150
151 public double logIntegrate(UnivariateFunction f, double min){
152 try {
153
154 double logIntegral = Double.NEGATIVE_INFINITY;
155
156 for (int i = 0; i < noPoints; i++) {
157 logIntegral = LogTricks.logSum(logIntegral, Math.log(coefficients[i]) +
158 ((UnivariateFunction.AbstractLogEvaluatableUnivariateFunction)f)
159 .logEvaluate(min + abscissae[i] / B));
160 }
161
162 logIntegral += -(alpha + 1) * Math.log(B);
163
164 return logIntegral;
165 } catch(ClassCastException e){
166 throw new RuntimeException(f.toString()+" has no logEvaluate method");
167 }
168
169 }
170
171
172 }