Debian Codegraph @

src/dr/evomodel/continuous/BifractionalDiffusionModel.java

raw symbols SCIP
1 /*
2 * BifractionalDiffusionModel.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.evomodel.continuous;
27
28 import dr.inference.model.Parameter;
29 import dr.inference.model.Variable;
30 import dr.math.distributions.BifractionalDiffusionDensity;
31
32 /**
33 * @author Marc Suchard
34 *
35 *
36 * Follows a bifractional diffusion model developed in:
37 * Brockman D, Hufnagel L and Geisel T (2006) The scaling laws of human travel. Nature 439, 462 - 465
38 *
39 *
40 */
41 public class BifractionalDiffusionModel extends MultivariateDiffusionModel {
42
43 public BifractionalDiffusionModel(Parameter alpha, Parameter beta) {
44 super();
45 this.alpha = alpha;
46 this.beta = beta;
47 addVariable(alpha);
48 addVariable(beta);
49 densityKnown = false;
50 }
51
52 protected double calculateLogDensity(double[] start, double[] stop, double time) {
53 // Compute finite-time transition probability
54
55 // Equation (2) from Brockman, Hufnagel and Geisel (2006)
56 // final double ratio = alpha.getParameterValue(0) / beta.getParameterValue(0);
57 // final double r = distanceEuclidean(start, stop);
58 // final double scaledTime = Math.pow(time,ratio);
59 // return -ratio * Math.log(time) + logUniversalScalingFunction(r / scaledTime);
60 double a = alpha.getParameterValue(0);
61 double b = beta.getParameterValue(0);
62 double r = distanceEuclidean(start, stop) * scalarDistance;
63
64
65 // a = 2.0
66 //b = 1.0
67 //r = 0.3597864505508788
68 //t = 3.290985204423155E-5
69 // a = 2.0;
70 //b = 1.0;
71 //r = 0.3597864505508788;
72 //time = 3.290985204423155E-5;
73 // time = 1;
74 // System.err.println("a = " + a);
75 // System.err.println("b = " + b);
76 // System.err.println("r = " + r);
77 // System.err.println("t = " + time);
78
79 if (!densityKnown) {
80 setupDensity();
81 densityKnown = true;
82 }
83
84 // return BifractionalDiffusionDensity.logPdf(r, time, a, b);
85
86 double pdf = bifractionalDensity.pdf(r, time * scalarTime);
87 double logPdf = Math.log(pdf);
88 if (Double.isNaN(logPdf)) {
89 System.err.println("a = " + a);
90 System.err.println("b = " + b);
91 System.err.println("r = " + r);
92 System.err.println("t = " + time * scalarTime);
93 System.err.println("pdf = " + pdf);
94 System.err.println("logPdf = " + logPdf);
95 }
96 return logPdf;
97 }
98
99 private void setupDensity() {
100 final double a = alpha.getParameterValue(0);
101 final double b = beta.getParameterValue(0);
102 bifractionalDensity = new BifractionalDiffusionDensity(a, b);
103 }
104
105 protected void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) {
106 super.handleVariableChangedEvent(variable, index, type);
107 if (variable == alpha || variable == beta) {
108 densityKnown = false;
109 }
110 }
111
112 private double distanceEuclidean(double[] start, double[] stop) {
113 final int dim = start.length;
114 double total = 0;
115 for(int i=0; i<dim; i++) {
116 final double dX = stop[i] - start[i];
117 total += dX*dX;
118 }
119 return Math.sqrt(total);
120 }
121
122 // private double logUniversalScalingFunction(double x) {
123 // return x - x;
124 // }
125
126 protected void calculatePrecisionInfo() {
127 // Precompute normalizing constants if necessary
128 }
129
130 protected void restoreState() {
131 super.restoreState();
132 densityKnown = false;
133 }
134
135 private Parameter alpha;
136 private Parameter beta;
137 private BifractionalDiffusionDensity bifractionalDensity;
138 private boolean densityKnown;
139 private double scalarDistance = 10E-3;
140 private double scalarTime = 10E+3;
141 }