Debian Codegraph @

src/dr/evomodel/coalescent/CovariateGMRFSkylineLikelihood.java

raw symbols SCIP
1 /*
2 * CovariateGMRFSkylineLikelihood.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.coalescent;
27
28 import dr.evolution.tree.NodeRef;
29 import dr.evolution.tree.Tree;
30 import dr.inference.model.MatrixParameter;
31 import dr.inference.model.Parameter;
32
33 import java.util.ArrayList;
34 import java.util.logging.Logger;
35
36 /**
37 * @author Erik Bloomquist
38 * @author Marc A. Suchard
39 */
40 public class CovariateGMRFSkylineLikelihood extends GMRFSkyrideLikelihood {
41
42
43 private Parameter covariateData;
44 private Parameter covariateTimes;
45
46 private ArrayList<CoalescentIntervalWithData> intervals;
47 private ArrayList<CoalescentIntervalWithData> storedIntervals;
48
49
50 public CovariateGMRFSkylineLikelihood(Tree tree, Parameter popParameter, Parameter precParameter,
51 Parameter lambda, Parameter beta, MatrixParameter dMatrix,
52 Parameter data, Parameter times) {
53 super(tree, popParameter, null, precParameter, lambda, beta, dMatrix, false, true);
54
55 covariateData = data;
56 covariateTimes = times;
57
58 fieldLength += covariateData.getDimension();
59
60 addVariable(covariateData); // this can have missing values for imputation
61
62 }
63
64 // @Override
65 public void sSetupIntervals() {
66
67 intervals.clear();
68 intervals.ensureCapacity(fieldLength);
69
70 NodeRef x;
71 for (int i = 0; i < tree.getInternalNodeCount(); i++) {
72 x = tree.getInternalNode(i);
73 intervals.add(new CoalescentIntervalWithData(tree.getNodeHeight(x), Double.NaN, CoalescentEventType.COALESCENT));
74 }
75 for (int i = 0; i < tree.getExternalNodeCount(); i++) {
76 x = tree.getExternalNode(i);
77 if (tree.getNodeHeight(x) > 0.0) {
78 intervals.add(new CoalescentIntervalWithData(tree.getNodeHeight(x), Double.NaN, CoalescentEventType.NEW_SAMPLE));
79 }
80 }
81 for (int i = 0; i < covariateTimes.getDimension(); i++) {
82 intervals.add(new CoalescentIntervalWithData(covariateTimes.getParameterValue(i),
83 covariateData.getParameterValue(i), CoalescentEventType.NOTHING));
84 }
85 dr.util.HeapSort.sort(intervals);
86
87
88 double a;
89 for (int i = 0; i < intervals.size() - 1; i++) {
90 a = intervals.get(i).length;
91 intervals.get(i).length = a - intervals.get(i + 1).length;
92 }
93 intervals.remove(intervals.size() - 1);
94 intervalsKnown = true;
95
96 }
97
98 public void setupGMRFWeights() {
99 super.setupGMRFWeights();
100 }
101
102 public void storeState() {
103 storedIntervals = new ArrayList<CoalescentIntervalWithData>(intervals.size());
104 for (CoalescentIntervalWithData interval : intervals) {
105 storedIntervals.add(interval.clone());
106 }
107 }
108
109 public void restoreState() {
110 intervals = storedIntervals;
111 storedIntervals.clear();
112 }
113
114 private class CoalescentIntervalWithData implements Comparable<CoalescentIntervalWithData>, Cloneable {
115 public final CoalescentEventType type;
116 public double length;
117 public final double datum;
118
119 public CoalescentIntervalWithData(double length, double datum, CoalescentEventType type) {
120 this.length = length;
121 this.type = type;
122 this.datum = datum;
123 }
124
125 public int compareTo(CoalescentIntervalWithData a) {
126
127 if (a.length < this.length) {
128 return -1;
129 } else if (a.length == this.length) {
130 Logger.getLogger("dr.evomodel.coalescent").severe("The current model " +
131 "has 2 internal nodes or 1 node and 1 covariate at the same height");
132 return 0;
133 }
134 return 1;
135 }
136
137 public String toString() {
138 return "(" + length + "," + type + "," + datum + ")";
139 }
140
141 public CoalescentIntervalWithData clone() {
142 return new CoalescentIntervalWithData(length, datum, type);
143 }
144
145 }
146
147 }