Debian Codegraph @

src/test/dr/math/GeneralizedIntegerGammaTest.java

raw symbols SCIP
1 /*
2 * GeneralizedIntegerGammaTest.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 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 * @author Marc A. Suchard
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 // Check normalization
122 assertEquals(1.0, m0, tolerance);
123
124 // Check mean
125 double mean = shape1s[i] / rate1s[i] + shape2s[i] / rate2s[i];
126 assertEquals(mean, m1, tolerance);
127
128 // Check variance
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); // Harder to approximate
132 }
133 }
134 }