Debian Codegraph @

src/dr/evomodel/continuous/MultivariateDiffusionModel.java

raw symbols SCIP
1 /*
2 * MultivariateDiffusionModel.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.evolution.tree.Tree;
29 import dr.evolution.tree.TreeAttributeProvider;
30 import dr.inference.model.*;
31 import dr.math.distributions.MultivariateNormalDistribution;
32 import dr.xml.*;
33 import org.w3c.dom.Document;
34 import org.w3c.dom.Element;
35
36 /**
37 * @author Marc Suchard
38 */
39
40
41 public class MultivariateDiffusionModel extends AbstractModel implements TreeAttributeProvider {
42
43 public static final String DIFFUSION_PROCESS = "multivariateDiffusionModel";
44 public static final String DIFFUSION_CONSTANT = "precisionMatrix";
45 public static final String PRECISION_TREE_ATTRIBUTE = "precision";
46
47 public static final double LOG2PI = Math.log(2*Math.PI);
48
49 /**
50 * Construct a diffusion model.
51 */
52
53 public MultivariateDiffusionModel(MatrixParameterInterface diffusionPrecisionMatrixParameter) {
54
55 super(DIFFUSION_PROCESS);
56
57 this.diffusionPrecisionMatrixParameter = diffusionPrecisionMatrixParameter;
58 calculatePrecisionInfo();
59 addVariable(diffusionPrecisionMatrixParameter);
60
61 }
62
63 public MultivariateDiffusionModel() {
64 super(DIFFUSION_PROCESS);
65 }
66
67
68 // public void randomize(Parameter trait) {
69 // }
70
71 public void check(Parameter trait) throws XMLParseException {
72 assert trait != null;
73 }
74
75 public MatrixParameterInterface getPrecisionParameter() {
76 checkVariableChanged();
77 return diffusionPrecisionMatrixParameter;
78 }
79
80 public double[][] getPrecisionmatrix() {
81 if (diffusionPrecisionMatrixParameter != null) {
82 checkVariableChanged();
83 return diffusionPrecisionMatrixParameter.getParameterAsMatrix();
84 }
85 return null;
86 }
87
88 public double getDeterminantPrecisionMatrix() {
89 checkVariableChanged();
90 return determinatePrecisionMatrix;
91 }
92
93 /**
94 * @return the log likelihood of going from start to stop in the given time
95 */
96 public double getLogLikelihood(double[] start, double[] stop, double time) {
97
98 if (time == 0) {
99 boolean equal = true;
100 for(int i=0; i<start.length; i++) {
101 if( start[i] != stop[i] ) {
102 equal = false;
103 break;
104 }
105 }
106 if (equal)
107 return 0.0;
108 return Double.NEGATIVE_INFINITY;
109 }
110
111 return calculateLogDensity(start, stop, time);
112 }
113
114 protected void checkVariableChanged(){
115 if(variableChanged){
116 calculatePrecisionInfo();
117 variableChanged=false;
118 }
119 }
120
121 protected double calculateLogDensity(double[] start, double[] stop, double time) {
122 checkVariableChanged();
123 final double logDet = Math.log(determinatePrecisionMatrix);
124 return MultivariateNormalDistribution.logPdf(stop, start, diffusionPrecisionMatrix, logDet, time);
125 }
126
127 // todo should be a test, no?
128 public static void main(String[] args) {
129 double[] start = {1, 2};
130 double[] stop = {0, 0};
131 double[][] precision = {{2, 0.5}, {0.5, 1}};
132 double scale = 0.2;
133 MatrixParameter precMatrix = new MatrixParameter("Hello");
134 precMatrix.addParameter(new Parameter.Default(precision[0]));
135 precMatrix.addParameter(new Parameter.Default(precision[1]));
136 MultivariateDiffusionModel model = new MultivariateDiffusionModel(precMatrix);
137 System.err.println("logPDF = " + model.calculateLogDensity(start, stop, scale));
138 System.err.println("Should be -19.948");
139 }
140
141 protected void calculatePrecisionInfo() {
142 diffusionPrecisionMatrix = diffusionPrecisionMatrixParameter.getParameterAsMatrix();
143 determinatePrecisionMatrix =
144 MultivariateNormalDistribution.calculatePrecisionMatrixDeterminate(
145 diffusionPrecisionMatrix);
146 }
147
148 // *****************************************************************
149 // Interface Model
150 // *****************************************************************
151 public void handleModelChangedEvent(Model model, Object object, int index) {
152 // no intermediates need to be recalculated...
153 }
154
155 protected void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) {
156 variableChanged=true;
157 // calculatePrecisionInfo();
158 }
159
160 protected void storeState() {
161 savedDeterminatePrecisionMatrix = determinatePrecisionMatrix;
162 savedDiffusionPrecisionMatrix = diffusionPrecisionMatrix;
163 storedVariableChanged=variableChanged;
164 }
165
166 protected void restoreState() {
167 determinatePrecisionMatrix = savedDeterminatePrecisionMatrix;
168 diffusionPrecisionMatrix = savedDiffusionPrecisionMatrix;
169 variableChanged=storedVariableChanged;
170 }
171
172 protected void acceptState() {
173 } // no additional state needs accepting
174
175 public String[] getTreeAttributeLabel() {
176 return new String[] {PRECISION_TREE_ATTRIBUTE};
177 }
178
179 public String[] getAttributeForTree(Tree tree) {
180 if (diffusionPrecisionMatrixParameter != null) {
181 return new String[] {diffusionPrecisionMatrixParameter.toSymmetricString()};
182 }
183
184 diffusionPrecisionMatrixParameter.toString();
185 return new String[] { "null" };
186 }
187
188 // **************************************************************
189 // XMLElement IMPLEMENTATION
190 // **************************************************************
191
192 public Element createElement(Document document) {
193 throw new RuntimeException("Not implemented!");
194 }
195
196 // **************************************************************
197 // XMLObjectParser
198 // **************************************************************
199
200 public static XMLObjectParser PARSER = new AbstractXMLObjectParser() {
201
202 public String getParserName() {
203 return DIFFUSION_PROCESS;
204 }
205
206 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
207
208 XMLObject cxo = xo.getChild(DIFFUSION_CONSTANT);
209 MatrixParameterInterface diffusionParam = (MatrixParameterInterface)
210 cxo.getChild(MatrixParameterInterface.class);
211
212 return new MultivariateDiffusionModel(diffusionParam);
213 }
214
215 //************************************************************************
216 // AbstractXMLObjectParser implementation
217 //************************************************************************
218
219 public String getParserDescription() {
220 return "Describes a multivariate normal diffusion process.";
221 }
222
223 public XMLSyntaxRule[] getSyntaxRules() {
224 return rules;
225 }
226
227 private final XMLSyntaxRule[] rules = {
228 new ElementRule(DIFFUSION_CONSTANT,
229 new XMLSyntaxRule[]{new ElementRule(MatrixParameterInterface.class)}),
230 };
231
232 public Class getReturnType() {
233 return MultivariateDiffusionModel.class;
234 }
235 };
236
237 // **************************************************************
238 // Private instance variables
239 // **************************************************************
240
241 protected MatrixParameterInterface diffusionPrecisionMatrixParameter;
242 private double determinatePrecisionMatrix;
243 private double savedDeterminatePrecisionMatrix;
244 private double[][] diffusionPrecisionMatrix;
245 private double[][] savedDiffusionPrecisionMatrix;
246
247 private boolean variableChanged=true;
248 private boolean storedVariableChanged;
249
250 }
251