Debian Codegraph @

src/dr/evomodel/continuous/DiffusionModel.java

raw symbols SCIP
1 /*
2 * DiffusionModel.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.AbstractModel;
29 import dr.inference.model.Model;
30 import dr.inference.model.Parameter;
31 import dr.inference.model.Variable;
32 import dr.math.MathUtils;
33 import dr.xml.*;
34 import org.w3c.dom.Document;
35 import org.w3c.dom.Element;
36
37 /**
38 * A class that can calculate the likelihood of a diffusion rate given a distance and time.
39 *
40 * @author Alexei Drummond
41 * @version $Id: DiffusionModel.java,v 1.5 2005/01/06 14:46:36 rambaut Exp $
42 */
43
44 public class DiffusionModel extends AbstractModel {
45
46 public static final String DIFFUSION_PROCESS = "diffusionProcess";
47 public static final String DIFFUSION_CONSTANT = "D";
48 public static final String BIAS = "mu";
49
50 /**
51 * Construct a diffusion model.
52 */
53 public DiffusionModel(Parameter diffusionRateParameter) {
54
55 super(DIFFUSION_PROCESS);
56
57 this.diffusionRateParameter = diffusionRateParameter;
58 addVariable(diffusionRateParameter);
59 diffusionRateParameter.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0, 1));
60 }
61
62 /**
63 * Construct a diffusion model.
64 */
65 public DiffusionModel(Parameter diffusionRateParameter, Parameter biasParameter) {
66
67 super(DIFFUSION_PROCESS);
68
69 this.diffusionRateParameter = diffusionRateParameter;
70 this.biasParameter = biasParameter;
71 addVariable(diffusionRateParameter);
72 addVariable(biasParameter);
73 diffusionRateParameter.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0, 1));
74 }
75
76 public double getD() {
77 return diffusionRateParameter.getParameterValue(0);
78 }
79
80 /**
81 * @return the log likelihood of going from start to stop in the given time
82 */
83 public double getLogLikelihood(double start, double stop, double time) {
84
85 double D = diffusionRateParameter.getParameterValue(0);
86 double bias = getBias();
87
88 // expected variance of distances of given time
89 double Dtime = D * time;
90
91 double unbiasedDistance = (stop - start) - (bias * time);
92
93 //System.out.println("distance=" + unbiasedDistance + " time=" + time);
94
95 // the log likelihood of travelling distance d, in time t given diffusion rate D
96 return -0.5 * Math.log(Dtime) - ((unbiasedDistance * unbiasedDistance) / (Dtime));
97 }
98
99 /**
100 * simulate the diffusion process forward in time.
101 *
102 * @return the new value of the trait after given time.
103 */
104 public double simulateForward(double value, double time) {
105 double D = diffusionRateParameter.getParameterValue(0);
106 double delta = MathUtils.nextGaussian();
107
108 delta *= Math.sqrt(D * time);
109 delta += getBias() * time;
110
111 return value + delta;
112 }
113
114
115 /**
116 * @return the bias of this diffusion process.
117 */
118 private double getBias() {
119 if (biasParameter == null) return 0.0;
120 return biasParameter.getParameterValue(0);
121 }
122
123 // *****************************************************************
124 // Interface Model
125 // *****************************************************************
126
127 public void handleModelChangedEvent(Model model, Object object, int index) {
128 // no intermediates need to be recalculated...
129 }
130
131 public void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) {
132 // no intermediates need to be recalculated...
133 }
134
135 protected void storeState() {
136 } // no additional state needs storing
137
138 protected void restoreState() {
139 } // no additional state needs restoring
140
141 protected void acceptState() {
142 } // no additional state needs accepting
143
144 // **************************************************************
145 // XMLElement IMPLEMENTATION
146 // **************************************************************
147
148 public Element createElement(Document document) {
149 throw new RuntimeException("Not implemented!");
150 }
151
152 // **************************************************************
153 // XMLObjectParser
154 // **************************************************************
155
156 public static XMLObjectParser PARSER = new AbstractXMLObjectParser() {
157
158 public String getParserName() {
159 return DIFFUSION_PROCESS;
160 }
161
162 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
163
164 XMLObject cxo = xo.getChild(DIFFUSION_CONSTANT);
165 Parameter diffusionParam = (Parameter) cxo.getChild(Parameter.class);
166
167 Parameter biasParam = null;
168 if (xo.hasAttribute(BIAS)) {
169 cxo = xo.getChild(BIAS);
170 biasParam = (Parameter) cxo.getChild(Parameter.class);
171 }
172
173 if (biasParam == null) {
174 return new DiffusionModel(diffusionParam);
175 }
176 return new DiffusionModel(diffusionParam, biasParam);
177 }
178
179 //************************************************************************
180 // AbstractXMLObjectParser implementation
181 //************************************************************************
182
183 public String getParserDescription() {
184 return "Describes a diffusion process.";
185 }
186
187 public XMLSyntaxRule[] getSyntaxRules() {
188 return rules;
189 }
190
191 private final XMLSyntaxRule[] rules = {
192 new ElementRule(DIFFUSION_CONSTANT,
193 new XMLSyntaxRule[]{new ElementRule(Parameter.class)}),
194 new ElementRule(BIAS,
195 new XMLSyntaxRule[]{new ElementRule(Parameter.class)})
196 };
197
198 public Class getReturnType() {
199 return DiffusionModel.class;
200 }
201 };
202
203 // **************************************************************
204 // Private instance variables
205 // **************************************************************
206
207 private final Parameter diffusionRateParameter;
208 private Parameter biasParameter;
209 }