Debian Codegraph @

src/dr/evomodel/continuous/BiasedMultivariateDiffusionModel.java

raw symbols SCIP
1 /*
2 * BiasedMultivariateDiffusionModel.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.xml.*;
29 import dr.inference.model.MatrixParameter;
30 import dr.inference.model.Parameter;
31 import dr.inference.model.Variable;
32 import dr.evolution.tree.Tree;
33
34 /**
35 * @author Marc A. Suchard
36 */
37
38 public class BiasedMultivariateDiffusionModel extends MultivariateDiffusionModel {
39
40 public static final String BIASED_DIFFUSION_PROCESS = "biasedMultivariateDiffusionModel";
41 public static final String BIAS_PARAMETER = "biasParameter";
42 public static final String BIAS_TREE_ATTRIBUTE = "bias";
43
44 BiasedMultivariateDiffusionModel(Parameter biasParam, MatrixParameter diffusionParam) {
45 super(diffusionParam);
46 this.biasParam = biasParam;
47 addVariable(biasParam);
48 }
49
50 protected void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) {
51 if (variable == diffusionPrecisionMatrixParameter)
52 calculatePrecisionInfo();
53 // else is bias and do nothing
54 }
55
56 @Override
57 protected double calculateLogDensity(double[] start, double[] stop, double time) {
58
59 final int dim = stop.length;
60 double[] bias = biasParam.getParameterValues();
61 for (int i = 0; i < dim; i++) {
62 bias[i] *= time;
63 bias[i] += start[i];
64 }
65
66
67
68 return super.calculateLogDensity(bias, stop, time);
69 }
70
71 public String[] getTreeAttributeLabel() {
72 return new String[]{
73 PRECISION_TREE_ATTRIBUTE,
74 BIAS_TREE_ATTRIBUTE
75 };
76 }
77
78 public String[] getAttributeForTree(Tree tree) {
79 return new String[]{
80 diffusionPrecisionMatrixParameter.toSymmetricString(),
81 toParameterString(biasParam)
82 };
83 }
84
85 private String toParameterString(Parameter param) {
86 StringBuffer sb = new StringBuffer("{");
87 int dim = param.getDimension();
88 for (int i = 0; i < dim; i++) {
89 sb.append(String.format("%5.4e", param.getParameterValue(i)));
90 if (i < dim - 1)
91 sb.append(",");
92 }
93 sb.append("}");
94 return sb.toString();
95 }
96
97 public static XMLObjectParser PARSER = new AbstractXMLObjectParser() {
98
99 public String getParserName() {
100 return BIASED_DIFFUSION_PROCESS;
101 }
102
103 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
104
105 XMLObject cxo = xo.getChild(BIAS_PARAMETER);
106 Parameter biasParam = (Parameter) cxo.getChild(Parameter.class);
107
108 cxo = xo.getChild(DIFFUSION_CONSTANT);
109 MatrixParameter diffusionParam = (MatrixParameter) cxo.getChild(MatrixParameter.class);
110
111 return new BiasedMultivariateDiffusionModel(biasParam, diffusionParam);
112 }
113
114 //************************************************************************
115 // AbstractXMLObjectParser implementation
116 //************************************************************************
117
118 public String getParserDescription() {
119 return "Describes a multivariate normal diffusion process.";
120 }
121
122 public XMLSyntaxRule[] getSyntaxRules() {
123 return rules;
124 }
125
126 private final XMLSyntaxRule[] rules = {
127 new ElementRule(DIFFUSION_CONSTANT,
128 new XMLSyntaxRule[]{new ElementRule(MatrixParameter.class)}),
129 new ElementRule(BIAS_PARAMETER,
130 new XMLSyntaxRule[]{new ElementRule(Parameter.class)}),
131 };
132
133 public Class getReturnType() {
134 return MultivariateDiffusionModel.class;
135 }
136 };
137
138 private Parameter biasParam;
139
140 }