Debian Codegraph @

src/dr/geo/GeoSpatialCollectionModel.java

raw symbols SCIP
1 /*
2 * GeoSpatialCollectionModel.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.geo;
27
28 import dr.inference.model.AbstractModelLikelihood;
29 import dr.inference.model.Model;
30 import dr.inference.model.Parameter;
31 import dr.inference.model.Variable;
32
33 import java.util.List;
34
35 /**
36 * @author Marc A. Suchard
37 * <p/>
38 * Provides a GeoSpatialDistribution over multiple points in multiple polygon.
39 * Uses AbstractModelLikelihood to cache 'contains' to reduce recalculations
40 * when only a single point is updated
41 */
42
43 public class GeoSpatialCollectionModel extends AbstractModelLikelihood {
44
45 public GeoSpatialCollectionModel(String name, Parameter points,
46 List<GeoSpatialDistribution> geoSpatialDistributions,
47 boolean isIntersection) {
48
49 super(name);
50 this.points = points;
51 this.geoSpatialDistributions = geoSpatialDistributions;
52
53 dim = points.getDimension() / GeoSpatialDistribution.dimPoint;
54 cachedPointLogLikelihood = new double[dim];
55 storedCachedPointLogLikelihood = new double[dim];
56 validPointLogLikelihood = new boolean[dim];
57 storedValidPointLogLikelihood = new boolean[dim];
58 likelihoodKnown = false;
59
60 addVariable(points);
61
62 this.isIntersection = isIntersection;
63 }
64
65 protected void handleModelChangedEvent(Model model, Object object, int index) {
66 // No submodels; do nothing
67 }
68
69 protected void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) {
70 // Mark appropriate dim as invalid
71 validPointLogLikelihood[index / GeoSpatialDistribution.dimPoint] = false;
72 likelihoodKnown = false;
73 }
74
75 protected void storeState() {
76
77 System.arraycopy(cachedPointLogLikelihood, 0, storedCachedPointLogLikelihood, 0, dim);
78 System.arraycopy(validPointLogLikelihood, 0, storedValidPointLogLikelihood, 0, dim);
79
80 storedLikelihoodKnown = likelihoodKnown;
81 storedLogLikelihood = logLikelihood;
82 }
83
84 protected void restoreState() {
85
86 double[] tmp1 = cachedPointLogLikelihood;
87 cachedPointLogLikelihood = storedCachedPointLogLikelihood;
88 storedCachedPointLogLikelihood = tmp1;
89
90 boolean[] tmp2 = validPointLogLikelihood;
91 validPointLogLikelihood = storedValidPointLogLikelihood;
92 storedValidPointLogLikelihood = tmp2;
93
94 likelihoodKnown = storedLikelihoodKnown;
95 logLikelihood = storedLogLikelihood;
96 }
97
98 protected void acceptState() {
99
100 }
101
102 public Model getModel() {
103 return this;
104 }
105
106 public double getLogLikelihood() {
107
108 if (likelihoodKnown)
109 return logLikelihood;
110
111 logLikelihood = 0.0;
112 final double[] point = new double[GeoSpatialDistribution.dimPoint];
113
114 for (int i = 0; i < dim; i++) {
115 if (!validPointLogLikelihood[i]) {
116 final int offset = i * GeoSpatialDistribution.dimPoint;
117 for (int j = 0; j < GeoSpatialDistribution.dimPoint; j++)
118 point[j] = points.getParameterValue(offset + j);
119
120 double pointLogLikelihood = 0;
121 for (GeoSpatialDistribution distribution : geoSpatialDistributions) {
122 //if we consider the union of polygons and the point must be inside, than it is good enough that the point is in one polygon
123 //so we look for a polygon that does not yield -inf
124 if (!isIntersection && !distribution.getOutside()) {
125 final double logPdf = distribution.logPdf(point);
126 if (logPdf != Double.NEGATIVE_INFINITY) {
127 pointLogLikelihood = logPdf;
128 break;
129 } else {
130 pointLogLikelihood = logPdf;
131 }
132 } else {
133 // Below is for intersections (or unions of complements)
134 pointLogLikelihood += distribution.logPdf(point);
135 if (pointLogLikelihood == Double.NEGATIVE_INFINITY)
136 break; // No need to finish
137 }
138 }
139 cachedPointLogLikelihood[i] = pointLogLikelihood;
140 validPointLogLikelihood[i] = true;
141 }
142 logLikelihood += cachedPointLogLikelihood[i];
143 if (logLikelihood == Double.NEGATIVE_INFINITY)
144 break; // No need to finish
145 }
146 likelihoodKnown = true;
147 return logLikelihood;
148 }
149
150 public void makeDirty() {
151 likelihoodKnown = false;
152 for (int i = 0; i < dim; i++)
153 validPointLogLikelihood[i] = false;
154 }
155
156 public Parameter getParameter() {
157 return points;
158 }
159
160 private Parameter points;
161 private List<GeoSpatialDistribution> geoSpatialDistributions;
162 private int dim;
163
164 private double[] cachedPointLogLikelihood;
165 private double[] storedCachedPointLogLikelihood;
166
167 private boolean likelihoodKnown;
168 private boolean storedLikelihoodKnown;
169
170 private double logLikelihood;
171 private double storedLogLikelihood;
172
173 private boolean[] validPointLogLikelihood;
174 private boolean[] storedValidPointLogLikelihood;
175
176 private final boolean isIntersection;
177 }