Debian Codegraph @

src/dr/evolution/continuous/DiffusionLikelihood.java

raw symbols SCIP
1 /*
2 * DiffusionLikelihood.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.evolution.continuous;
27
28 import dr.math.*;
29 import dr.evolution.tree.Tree;
30 import dr.evolution.tree.NodeRef;
31
32 /**
33 *
34 *
35 */
36 public class DiffusionLikelihood implements UnivariateFunction, MultivariateFunction {
37
38 Tree tree;
39 String traitName;
40
41 public DiffusionLikelihood(Tree tree, String traitName) {
42 this.tree = tree;
43 this.traitName = traitName;
44 }
45
46 public double optimize(double[] p) {
47 //
48
49 if (p.length > 1) {
50 p[0] = 1.0;
51 p[1] = 0.01;
52
53 MultivariateMinimum optimizer = new ConjugateDirectionSearch();
54 //MultivariateMinimum optimizer = new DifferentialEvolution(2,5);
55 optimizer.optimize(this,p,1e-6, 1e-6);
56 return -evaluate(p);
57 } else if (p.length == 1) {
58 UnivariateMinimum optimizer = new UnivariateMinimum();
59 p[0] = optimizer.optimize(this,1e-6);
60 return -evaluate(p[0]);
61 } else throw new IllegalArgumentException("");
62 }
63
64 /**
65 * @return the log likelihood of going from start to stop in the given time
66 */
67 public double getLogLikelihood(double distance, double time, double D, double bias) {
68
69 // expected variance of distances of given time
70 double Dtime = D * time;
71
72 double d = distance - (bias*time);
73
74
75 //System.out.println("distance=" + unbiasedDistance + " time=" + time);
76
77 // the log likelihood of travelling distance d, in time t given diffusion rate D
78 return -(Math.log(Math.sqrt(Dtime*2*Math.PI))) - ((d*d)/(2*Dtime));
79 }
80
81
82 public double evaluate(double[] argument) {
83
84 double D = argument[0];
85 double bias = argument[1];
86
87 double logLkl = 0.0;
88 for (int i = 0; i < tree.getNodeCount(); i++) {
89
90 NodeRef child = tree.getNode(i);
91
92 if (!tree.isRoot(child)) {
93
94 NodeRef parent = tree.getParent(child);
95
96 Contrastable parentValue = (Contrastable)tree.getNodeAttribute(parent,traitName);
97 Contrastable childValue = (Contrastable)tree.getNodeAttribute(child,traitName);
98
99 double distance = parentValue.getDifference(childValue);
100 //System.out.println(distance);
101
102 logLkl += getLogLikelihood(distance,tree.getBranchLength(child),D, bias);
103
104 }
105 }
106
107 return -logLkl;
108 }
109
110
111
112
113 public int getNumArguments() {
114 return 2;
115 }
116
117 public double getLowerBound(int n) {
118 if (n == 0) return 1e-12;
119 return -Double.MAX_VALUE;
120 }
121
122 public double getUpperBound(int n) {
123 return 2000;
124 }
125
126 public double evaluate(double argument) {
127 return evaluate(new double[] {argument, 0.0});
128 }
129
130 public double getLowerBound() {
131 return getLowerBound(0);
132 }
133
134 public double getUpperBound() {
135 return getUpperBound(0);
136 }
137 }