Debian Codegraph @

src/dr/evomodel/treedatalikelihood/continuous/ContinuousTraitDataModel.java

raw symbols SCIP
1 /*
2 * ContinuousTraitData.java
3 *
4 * Copyright (c) 2002-2016 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.treedatalikelihood.continuous;
27
28 import dr.evomodel.treedatalikelihood.continuous.cdi.PrecisionType;
29 import dr.inference.model.*;
30
31 import java.util.*;
32
33 /**
34 * @author Marc A. Suchard
35 */
36 public class ContinuousTraitDataModel extends AbstractModel implements ContinuousTraitPartialsProvider {
37
38 private final CompoundParameter parameter;
39 private final List<Integer> missingIndices;
40 private final List<Integer> originalMissingIndices;
41
42 private final int numTraits;
43 private final int dimTrait;
44 private final PrecisionType precisionType;
45
46 public ContinuousTraitDataModel(String name,
47 CompoundParameter parameter,
48 List<Integer> missingIndices,
49 boolean useMissingIndices,
50 final int dimTrait, PrecisionType precisionType) {
51 super(name);
52 this.parameter = parameter;
53 this.originalMissingIndices = missingIndices;
54 this.missingIndices = (useMissingIndices? missingIndices : new ArrayList<Integer>());
55 addVariable(parameter);
56
57 this.dimTrait = dimTrait;
58 this.numTraits = getParameter().getParameter(0).getDimension() / dimTrait;
59 this.precisionType = precisionType;
60 }
61
62 public boolean bufferTips() { return true; }
63
64 public int getTraitCount() { return numTraits; }
65
66 public int getTraitDimension() { return dimTrait; }
67
68 public PrecisionType getPrecisionType() {
69 return precisionType;
70 }
71
72 public String getName() { return super.getModelName(); }
73
74 public CompoundParameter getParameter() { return parameter; }
75
76 public List<Integer> getMissingIndices() { return missingIndices; }
77
78 List<Integer> getOriginalMissingIndices() { return originalMissingIndices; }
79
80 @Override
81 protected void handleModelChangedEvent(Model model, Object object, int index) {
82 // No sub-models
83 }
84
85 @Override
86 protected void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) {
87 if (variable == parameter) {
88 if (type == Parameter.ChangeType.VALUE_CHANGED) {
89 fireModelChanged(this, getTaxonIndex(index));
90 } else if (type == Parameter.ChangeType.ALL_VALUES_CHANGED){
91 // if (!allDataChange) {
92 fireModelChanged(this);
93 // allDataChange = true;
94 // }
95 } else {
96 throw new RuntimeException("Unhandled parameter change type");
97 }
98 }
99 }
100
101 // private boolean allDataChange = false;
102
103 private int getTaxonIndex(int parameterIndex) {
104 return parameterIndex / (dimTrait * numTraits);
105 }
106
107 @Override
108 protected void storeState() {
109 // allDataChange = false;
110 }
111
112 @Override
113 protected void restoreState() { }
114
115 @Override
116 protected void acceptState() { }
117
118 private double[] getScalarTipPartial(int taxonIndex) {
119 double[] partial = new double[numTraits * (dimTrait + 1)];
120 final Parameter p = parameter.getParameter(taxonIndex);
121 int offset = 0;
122 for (int i = 0; i < numTraits; ++i) {
123 boolean missing = false;
124 for (int j = 0; j < dimTrait; ++j) {
125 final int index = i * dimTrait + j;
126 final int missingIndex = index + dimTrait * numTraits * taxonIndex;
127 partial[offset + j] = p.getParameterValue(index);
128 if (missingIndices != null && missingIndices.contains(missingIndex)) {
129 missing = true;
130 }
131 }
132 partial[offset + dimTrait] = missing ? 0.0 : Double.POSITIVE_INFINITY;
133 offset += dimTrait + 1;
134 }
135 return partial;
136 }
137
138 private static final boolean OLD = false;
139
140 public double[] getTipPartial(int taxonIndex, boolean fullyObserved) {
141 if (fullyObserved) {
142
143 final PrecisionType precisionType = PrecisionType.SCALAR;
144 final int offsetInc = dimTrait + precisionType.getMatrixLength(dimTrait);
145 final double precision = PrecisionType.getObservedPrecisionValue(false);
146
147 double[] tipPartial = getTipPartial(taxonIndex, precisionType);
148
149 for (int i = 0; i < numTraits; ++i) {
150 precisionType.fillPrecisionInPartials(tipPartial, i * offsetInc, 0, precision, dimTrait);
151 }
152
153 return tipPartial;
154 } else {
155 return getTipPartial(taxonIndex, precisionType);
156 }
157 }
158
159 private double[] getTipPartial(int taxonIndex, final PrecisionType precisionType) {
160
161 if (OLD) {
162 return getScalarTipPartial(taxonIndex);
163 }
164
165 final int offsetInc = dimTrait + precisionType.getMatrixLength(dimTrait);
166 final double[] partial = new double[numTraits * offsetInc];
167 final Parameter p = parameter.getParameter(taxonIndex);
168
169 int offset = 0;
170 for (int i = 0; i < numTraits; ++i) {
171 for (int j = 0; j < dimTrait; ++j) {
172
173 final int pIndex = i * dimTrait + j;
174 final int missingIndex = pIndex + dimTrait * numTraits * taxonIndex;
175
176 partial[offset + j] = p.getParameterValue(pIndex);
177
178 final boolean missing = missingIndices != null && missingIndices.contains(missingIndex);
179 final double precision = PrecisionType.getObservedPrecisionValue(missing);
180
181 precisionType.fillPrecisionInPartials(partial, offset, j, precision, dimTrait);
182 }
183
184 offset += offsetInc;
185 }
186
187 return partial;
188 }
189
190 double[] getTipObservation(int taxonIndex, final PrecisionType precisionType) {
191 final int offsetInc = dimTrait + precisionType.getMatrixLength(dimTrait);
192
193 final double[] partial = getTipPartial(taxonIndex, precisionType);
194 final double[] data = new double[numTraits * dimTrait];
195
196 for (int i = 0; i < numTraits; ++i) {
197 precisionType.copyObservation(partial, i * offsetInc, data, i * dimTrait, dimTrait);
198 }
199
200 return data;
201 }
202
203 /*
204 * For partially observed tips: (y_1, y_2)^t \sim N(\mu, \Sigma) where
205 *
206 * \mu = (\mu_1, \mu_2)^t
207 * \Sigma = ((\Sigma_{11}, \Sigma_{12}), (\Sigma_{21}, \Sigma_{22})^t
208 *
209 * then y_1 | y_2 \sim N (\bar{\mu}, \bar{\Sigma}), where
210 *
211 * \bar{\mu} = \mu_1 + \Sigma_{12}\Sigma_{22}^{-1}(y_2 - \mu_2), and
212 * \bar{\Sigma} = \Sigma_{11} - \Sigma_{12}\Sigma_{22}^1\Sigma{21}
213 *
214 */
215 }