| 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 test.dr.math; |
| 27 |
|
| 28 |
import dr.evomodel.branchratemodel.LatentStateBranchRateModel; |
| 29 |
import dr.inference.markovjumps.MarkovReward; |
| 30 |
import dr.inference.markovjumps.SericolaSeriesMarkovReward; |
| 31 |
import dr.inference.markovjumps.TwoStateOccupancyMarkovReward; |
| 32 |
import dr.math.IntegrableUnivariateFunction; |
| 33 |
import dr.math.distributions.GeneralizedIntegerGammaDistribution; |
| 34 |
import dr.math.matrixAlgebra.Vector; |
| 35 |
import org.apache.commons.math.FunctionEvaluationException; |
| 36 |
import org.apache.commons.math.MaxIterationsExceededException; |
| 37 |
import org.apache.commons.math.analysis.UnivariateRealFunction; |
| 38 |
import org.apache.commons.math.analysis.integration.TrapezoidIntegrator; |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
public class GeneralizedIntegerGammaTest extends MathTestCase { |
| 45 |
|
| 46 |
private static final double tolerance = 10E-6; |
| 47 |
|
| 48 |
private double sum(double[] v) { |
| 49 |
double sum = 0.0; |
| 50 |
for (double x : v) { |
| 51 |
sum += x; |
| 52 |
} |
| 53 |
return sum; |
| 54 |
} |
| 55 |
|
| 56 |
int[] shape1s = new int[] { 10, 6, 2 }; |
| 57 |
int[] shape2s = new int[] { 1, 3, 4 }; |
| 58 |
double[] rate1s = new double[] { 0.1, 2, 3 }; |
| 59 |
double[] rate2s = new double[] { 2, 0.1, 0.2 }; |
| 60 |
double[] xs = new double[] { 0.1, 0.5, 1.2 }; |
| 61 |
|
| 62 |
public void testGeneratingFunction() { |
| 63 |
|
| 64 |
for (int i = 0; i < shape1s.length; ++i) { |
| 65 |
GeneralizedIntegerGammaDistribution gig = new GeneralizedIntegerGammaDistribution( |
| 66 |
shape1s[i], shape2s[i], rate1s[i], rate2s[i] |
| 67 |
); |
| 68 |
|
| 69 |
double a = gig.generatingFunction(xs[i]); |
| 70 |
double b = gig.generatingFunctionPartialFraction(xs[i]); |
| 71 |
assertEquals(a, b, tolerance); |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
public void testPdf() { |
| 76 |
|
| 77 |
for (int i = 0; i < shape1s.length; ++i) { |
| 78 |
final GeneralizedIntegerGammaDistribution gig = new GeneralizedIntegerGammaDistribution( |
| 79 |
shape1s[i], shape2s[i], rate1s[i], rate2s[i] |
| 80 |
); |
| 81 |
|
| 82 |
TrapezoidIntegrator integrator = new TrapezoidIntegrator(); |
| 83 |
double m0 = 0.0; |
| 84 |
double m1 = 0.0; |
| 85 |
double m2 = 0.0; |
| 86 |
try { |
| 87 |
m0 = integrator.integrate( |
| 88 |
new UnivariateRealFunction() { |
| 89 |
@Override |
| 90 |
public double value(double x) throws FunctionEvaluationException { |
| 91 |
final double pdf = gig.pdf(x); |
| 92 |
return pdf; |
| 93 |
} |
| 94 |
}, 0.0, 1000.0); |
| 95 |
|
| 96 |
m1 = integrator.integrate( |
| 97 |
new UnivariateRealFunction() { |
| 98 |
@Override |
| 99 |
public double value(double x) throws FunctionEvaluationException { |
| 100 |
final double pdf = gig.pdf(x); |
| 101 |
return x * pdf; |
| 102 |
} |
| 103 |
}, 0.0, 1000.0); |
| 104 |
|
| 105 |
|
| 106 |
m2 = integrator.integrate( |
| 107 |
new UnivariateRealFunction() { |
| 108 |
@Override |
| 109 |
public double value(double x) throws FunctionEvaluationException { |
| 110 |
final double pdf = gig.pdf(x); |
| 111 |
return x * x * pdf; |
| 112 |
} |
| 113 |
}, 0.0, 1000.0); |
| 114 |
|
| 115 |
} catch (MaxIterationsExceededException e) { |
| 116 |
e.printStackTrace(); |
| 117 |
} catch (FunctionEvaluationException e) { |
| 118 |
e.printStackTrace(); |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
assertEquals(1.0, m0, tolerance); |
| 123 |
|
| 124 |
|
| 125 |
double mean = shape1s[i] / rate1s[i] + shape2s[i] / rate2s[i]; |
| 126 |
assertEquals(mean, m1, tolerance); |
| 127 |
|
| 128 |
|
| 129 |
m2 -= m1 * m1; |
| 130 |
double variance = shape1s[i] / rate1s[i] / rate1s[i] + shape2s[i] / rate2s[i] / rate2s[i]; |
| 131 |
assertEquals(variance, m2, tolerance * 10); |
| 132 |
} |
| 133 |
} |
| 134 |
} |