Debian Codegraph @

src/dr/math/MultivariateMonteCarloIntegral.java

raw symbols SCIP
1 /*
2 * MultivariateMonteCarloIntegral.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 java.util.Arrays;
29 import java.util.HashMap;
30
31 /**
32 * Approximates the integral of a given function using Monte Carlo integration; possibly stratified
33 *
34 * @author Alexei Drummond
35 *
36 * @version $Id: MonteCarloIntegral.java,v 1.5 2005/05/24 20:26:01 rambaut Exp $
37 */
38 public class MultivariateMonteCarloIntegral implements MultivariateIntegral {
39
40 // bins is the number of divisions that each axis it split into (so the full number of bins is this to the
41 // power of the dimension of the function
42 // sampleSize is the number of samples PER BIN
43
44 public MultivariateMonteCarloIntegral(int sampleSize, int bins) {
45 this.sampleSize = sampleSize;
46 this.bins = bins;
47 }
48
49 public MultivariateMonteCarloIntegral(int sampleSize) {
50 this(sampleSize, 1);
51 }
52
53 /**
54 * @return the approximate integral of the given function
55 * within the given range using simple monte carlo integration.
56 * @param f the function whose integral is of interest
57 * @param mins the minimum value of the function
58 * @param maxes the upper limit of the function
59 */
60 public double integrate(MultivariateFunction f, double[] mins, double[] maxes) {
61
62 int dim = f.getNumArguments();
63 int totalBins = bins*dim;
64 double[] steps = new double[dim];
65 double totalArea=1;
66
67
68 for(int i=0; i<dim; i++){
69 totalArea *= (maxes[i]-mins[i]);
70 }
71
72 HashMap<Integer, double[]> binCorners = new HashMap<Integer, double[]>();
73 double[] currentCorner = new double[dim];
74
75
76 for(int index=0; index<totalBins; index++){
77 binCorners.put(index, Arrays.copyOf(currentCorner, dim));
78
79 int dimToCheck = 0;
80 while(dimToCheck<dim){
81 if(currentCorner[dimToCheck]+steps[dimToCheck]<maxes[dimToCheck]){
82 currentCorner[dimToCheck] += steps[dimToCheck];
83 break;
84 } else {
85 currentCorner[dimToCheck] = mins[dimToCheck];
86 }
87 dimToCheck++;
88 }
89 }
90
91 double integral = 0.0;
92
93 for(int i=0; i<totalBins; i++){
94
95 for (int j=1; j <= sampleSize; j++) {
96
97 double[] sample = new double[dim];
98 for(int k=0; k<sample.length; k++){
99 sample[k] = binCorners.get(i)[k] + MathUtils.nextDouble()*(steps[k]);
100 }
101
102 integral += f.evaluate(sample);
103 }
104
105 }
106 integral *= totalArea/((double)sampleSize*totalBins);
107 return integral;
108 }
109
110 protected int getSampleSize(){
111 return sampleSize;
112 }
113 protected int getBins(){
114 return bins;
115 }
116
117 private int sampleSize;
118 private int bins;
119 }