| 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; |
| 27 |
|
| 28 |
import dr.math.distributions.NormalDistribution; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 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 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
} |
| 170 |
|
| 171 |
public int getSampleSize(){ |
| 172 |
return sampleSize; |
| 173 |
} |
| 174 |
|
| 175 |
private int sampleSize; |
| 176 |
|
| 177 |
private final variant mode; |
| 178 |
} |