Debian Codegraph @

src/dr/evomodel/continuous/BivariateDiscreteDiffusionModel.java

raw symbols SCIP
1 /*
2 * BivariateDiscreteDiffusionModel.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.xml.*;
30
31 import java.io.File;
32 import java.io.FileNotFoundException;
33 import java.io.FileReader;
34 import java.io.IOException;
35 import java.util.HashMap;
36
37 /**
38 * Created by IntelliJ IDEA.
39 * User: msuchard
40 * Date: Jul 14, 2007
41 * Time: 7:01:32 PM
42 * To change this template use File | Settings | File Templates.
43 */
44 public class BivariateDiscreteDiffusionModel extends MultivariateDiffusionModel {
45
46 public static final String DISCRETE_DIFFUSION_PROCESS = "multivariateDiscreteDiffusionModel";
47 public static final String GRID_X_DIMENSION = "xGridDimension";
48 public static final String GRID_Y_DIMENSION = "yGridDimension";
49 // public static final String EIGEN_FILE = "eigensystemFileName";
50 public static final String EVEC_NAME = "eigenvectorsFileName";
51 public static final String EVAL_NAME = "eigenvaluesFileName";
52
53 /**
54 * Construct a discrete diffusion model.
55 */
56 public BivariateDiscreteDiffusionModel(Parameter graphRate,
57 int xDim, int yDim,
58 double[] eVal, double[][] eVec) {
59 super();
60 // super(diffusionPrecisionMatrixParameter);
61 this.graphRate = graphRate;
62 this.xDim = xDim;
63 this.yDim = yDim;
64 this.totalDim = xDim * yDim;
65 this.eVal = eVal;
66 this.eVec = eVec;
67 // probabilityCache = new HashMap<Integer,Double>();
68
69 addVariable(graphRate);
70
71 System.err.println("TEST00 = " + getCTMCProbability(0, 0, 0.0));
72 System.err.println("TEST01 = " + getCTMCProbability(0, 1, 0.0));
73 }
74
75 private int getIndex(int x, int y) {
76 return x * yDim + y;
77 }
78
79 private int[] getXY(int index) {
80 int[] xy = new int[2];
81 xy[0] = index / yDim;
82 xy[1] = index - xy[0] * yDim;
83 return xy;
84 }
85
86 private double getProbability(int I, int J, double time) {
87 double probability = 0;
88 for (int k = 0; k < totalDim; k++) {
89 probability += eVec[I][k] * Math.exp(time * eVal[k]) * eVec[J][k];
90 }
91 return probability;
92
93 }
94
95 public void handleParameterChangedEvent(Parameter parameter, int index) {
96 // Clear cached probabilities
97 // probabilityCache.clear();
98 }
99
100 private double getCTMCProbability(int I, int J, double time) {
101 double probability = 0;
102 for (int k = 0; k < totalDim; k++) {
103 probability += eVec[I][k] * Math.exp(time * eVal[k]) * eVec[J][k];
104 }
105 return probability;
106
107 }
108
109 /**
110 * @return the log likelihood of going from start to stop in the given time
111 */
112 public double getLogLikelihood(double[] start, double[] stop, double time) {
113 // System.err.println("xy0 = "+start[0]+":"+start[1]);
114 // System.err.println("xy1 = "+ stop[0]+":"+ stop[1]);
115
116
117 int startIndex = getIndex((int) start[0], (int) start[1]);
118 int stopIndex = getIndex((int) stop[0], (int) stop[1]);
119
120 // System.err.println("i0 = "+startIndex);
121 // System.err.println("i1 = "+stopIndex);
122 // System.err.println("");
123
124 return Math.log(getCTMCProbability(startIndex, stopIndex, time));
125
126 }
127
128 public static XMLObjectParser PARSER = new AbstractXMLObjectParser() {
129
130 public String getParserName() {
131 return DISCRETE_DIFFUSION_PROCESS;
132 }
133
134 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
135
136 int xDim = xo.getIntegerAttribute(GRID_X_DIMENSION);
137 int yDim = xo.getIntegerAttribute(GRID_Y_DIMENSION);
138
139 String evecFileName = xo.getStringAttribute(EVEC_NAME);
140 String evalFileName = xo.getStringAttribute(EVAL_NAME);
141
142 File evecFile;
143 File evalFile;
144
145 try {
146 File file = new File(evecFileName);
147 String name = file.getName();
148 String parent = file.getParent();
149
150 if (!file.isAbsolute()) {
151 parent = System.getProperty("user.dir");
152 }
153 evecFile = new File(parent, name);
154 new FileReader(evecFile);
155 } catch (FileNotFoundException fnfe) {
156 throw new XMLParseException("File '" + evecFileName + "' can not be opened for " + getParserName() + " element.");
157 }
158
159 try {
160 File file = new File(evalFileName);
161 String name = file.getName();
162 String parent = file.getParent();
163
164 if (!file.isAbsolute()) {
165 parent = System.getProperty("user.dir");
166 }
167 evalFile = new File(parent, name);
168 new FileReader(evalFile);
169 } catch (FileNotFoundException fnfe) {
170 throw new XMLParseException("File '" + evalFileName + "' can not be opened for " + getParserName() + " element.");
171 }
172
173 double[] eval = null;
174 double[][] evec = null;
175
176 try {
177 eval = TopographicalMap.readEigenvalues(evalFile.getAbsolutePath());
178 evec = TopographicalMap.readEigenvectors(evecFile.getAbsolutePath());
179 } catch (IOException e) {
180 e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
181 assert false;
182 }
183
184 // XMLObject cxo = (XMLObject) xo.getChild(DIFFUSION_CONSTANT);
185 // MatrixParameter diffusionParam = (MatrixParameter) cxo.getChild(MatrixParameter.class);
186 // MatrixParameter diffusionParam = null;
187 Parameter graphRate = (Parameter) xo.getChild(Parameter.class);
188
189 // if (diffusionParam.getRowDimension()>1 && diffusionParam.getColumnDimension()>1)
190 // throw new XMLParseException("The bivariate discrete diffusion model currently uses a single rate.");
191
192 final int totalDim = xDim * yDim;
193 if (totalDim != evec.length || totalDim != eval.length)
194 throw new XMLParseException("Number of eigenvalues and eigenvectors must match the map grid dimensions");
195
196 return new BivariateDiscreteDiffusionModel(graphRate, xDim, yDim, eval, evec);
197 }
198
199 //************************************************************************
200 // AbstractXMLObjectParser implementation
201 //************************************************************************
202
203 public String getParserDescription() {
204 return "Describes a multivariate discrete diffusion process.";
205 }
206
207 public XMLSyntaxRule[] getSyntaxRules() {
208 return rules;
209 }
210
211 private final XMLSyntaxRule[] rules = {
212 AttributeRule.newStringRule(EVEC_NAME),
213 AttributeRule.newStringRule(EVAL_NAME),
214 AttributeRule.newIntegerRule(GRID_X_DIMENSION),
215 AttributeRule.newIntegerRule(GRID_Y_DIMENSION),
216 new ElementRule(Parameter.class),
217 // new ElementRule(DIFFUSION_CONSTANT,
218 // new XMLSyntaxRule[]{new ElementRule(MatrixParameter.class)}),
219 };
220
221 public Class getReturnType() {
222 return BivariateDiscreteDiffusionModel.class;
223 }
224
225 };
226
227 private final Parameter graphRate;
228 private final int xDim;
229 private final int yDim;
230 private final int totalDim;
231 HashMap<Integer, Double> probabilityCache;
232
233 private final double[] eVal;
234 private final double[][] eVec;
235
236 }