Debian Codegraph @

src/dr/evomodel/coalescent/structure/MetaPopulationModel.java

raw symbols SCIP
1 /*
2 * MetaPopulationModel.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.structure;
27
28 import dr.evolution.coalescent.structure.MetaPopulation;
29 import dr.evomodel.coalescent.DemographicModel;
30 import dr.inference.model.*;
31 import dr.xml.*;
32
33 import java.util.ArrayList;
34
35 /**
36 * A wrapper for ConstantPopulation.
37 *
38 * @author Andrew Rambaut
39 * @author Alexei Drummond
40 * @version $Id: MetaPopulationModel.java,v 1.3 2006/09/11 09:33:01 gerton Exp $
41 */
42 @Deprecated
43 public class MetaPopulationModel extends AbstractModel implements MetaPopulation {
44 //
45 // Public stuff
46 //
47
48 public static final String META_POPULATION_MODEL = "metaPopulationModel";
49
50 /**
51 * Construct demographic model with default settings
52 */
53 public MetaPopulationModel(ArrayList<DemographicModel> demographicModels, Parameter populationProportions) {
54
55 this(META_POPULATION_MODEL, demographicModels, populationProportions);
56 }
57
58 /**
59 * Construct demographic model with default settings
60 */
61 public MetaPopulationModel(String name, ArrayList<DemographicModel> demographicModelList, Parameter populationProportions) {
62
63 super(name);
64
65 this.populationProportions = populationProportions;
66
67 if (populationProportions != null) {
68
69 // Single demographic, each with a different weight
70 addVariable(populationProportions);
71 populationProportions.addBounds(new Parameter.DefaultBounds(1.0, 0.0, populationProportions.getDimension()));
72 populationCount = populationProportions.getDimension() + 1;
73 // Add copies of the demographicModel to the array
74 for (int i = 1; i < populationCount; i++) {
75 demographicModelList.add(demographicModelList.get(0));
76 }
77 demographicModels = demographicModelList.toArray(new DemographicModel[0]);
78 addModel(demographicModels[0]);
79
80 } else {
81
82 // Several demographic models
83 populationCount = demographicModelList.size();
84 demographicModels = demographicModelList.toArray(new DemographicModel[0]);
85 for (int i = 0; i < populationCount; i++) {
86 addModel(demographicModels[i]);
87 }
88
89 }
90
91 addStatistic(populationSizesStatistic);
92 }
93
94 // general functions
95
96 private double getProportion(int population) {
97
98 if (populationProportions == null) {
99 return 1.0;
100 }
101 if (population > 0) {
102 return populationProportions.getParameterValue(population - 1);
103 }
104 double proportion = 1.0;
105 for (int i = 1; i < populationCount; i++) {
106 proportion -= populationProportions.getParameterValue(i - 1);
107 }
108 return proportion;
109 }
110
111
112 public int getPopulationCount() {
113
114 return populationCount;
115
116 }
117
118 public double[] getPopulationSizes(double time) {
119
120 // make population size array
121 double[] N = new double[populationCount];
122 for (int i = 0; i < populationCount; i++) {
123 N[i] = demographicModels[i].getDemographicFunction().getDemographic(time) * getProportion(i);
124 }
125
126 return N;
127 }
128
129 /* returns value of demographic function at time t (population size; one entry of double[] getPopulationSizes)
130 * (This function mirrors an equivalent function in DemographicFunction)
131 */
132 public double getDemographic(double time, int population) {
133
134 return demographicModels[population].getDemographicFunction().getDemographic(time) * getProportion(population);
135
136 }
137
138 /* calculates the integral 1/N(x) dx from start to finish, for one of the populations
139 * (This function mirrors an equivalent function in DemographicFunction)
140 */
141 public double getIntegral(double start, double finish, int population) {
142
143 double integral = demographicModels[population].getDemographicFunction().getIntegral(start, finish);
144 return integral / getProportion(population);
145
146 }
147
148 // **************************************************************
149 // Model IMPLEMENTATION
150 // **************************************************************
151
152 protected void handleModelChangedEvent(Model model, Object object, int index) {
153 fireModelChanged();
154 }
155
156 protected void storeState() {
157 } // no additional state needs storing
158
159 protected void restoreState() {
160 fireModelChanged();
161 } // no additional state needs restoring
162
163 protected void acceptState() {
164 } // no additional state needs accepting
165
166 public void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) {
167 // nothing to do
168 }
169
170 private Statistic populationSizesStatistic = new Statistic.Abstract() {
171
172 public String getStatisticName() {
173 return "populationSizes";
174 }
175
176 public int getDimension() {
177 return populationCount;
178 }
179
180 public double getStatisticValue(int dim) {
181
182 double metaN0 = demographicModels[dim].getDemographicFunction().getDemographic(0);
183 return metaN0 * getProportion(dim);
184 }
185
186 };
187
188 /**
189 * Parses an element from an DOM document into a ConstantPopulation.
190 */
191 public static XMLObjectParser PARSER = new AbstractXMLObjectParser() {
192
193 public String getParserName() {
194 return MetaPopulationModel.META_POPULATION_MODEL;
195 }
196
197 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
198
199 ArrayList<DemographicModel> demographics = new ArrayList<DemographicModel>(0);
200 Parameter populationProportions = null;
201
202 for (int i = 0; i < xo.getChildCount(); ++i) {
203 final Object o = xo.getChild(i);
204 if (o instanceof DemographicModel) {
205 demographics.add((DemographicModel) o);
206 } else if (o instanceof Parameter) {
207 if (populationProportions != null) {
208 throw new Error("Allowed at most one Parameter in a MetaPopulationModel");
209 }
210 populationProportions = (Parameter) o;
211 } else {
212 throw new Error("A MetaPopulationModel may only have children of type Parameter or DemographicModel");
213 }
214 }
215
216 // Do sanity checking.
217 if (populationProportions == null) {
218 if (demographics.size() < 2) {
219 throw new Error("A MetaPopulationModel must have at least 2 DemographicModels (or a Parameter)");
220 }
221 } else {
222 if (demographics.size() != 1) {
223 throw new Error("A MetaPopulationModel with a Parameter must have exactly one DemographicModel");
224 }
225 }
226
227 return new MetaPopulationModel(demographics, populationProportions);
228 }
229
230 //************************************************************************
231 // AbstractXMLObjectParser implementation
232 //************************************************************************
233
234 public String getParserDescription() {
235 return "A model that represents a subdivided population.";
236 }
237
238 public Class getReturnType() {
239 return MetaPopulationModel.class;
240 }
241
242 public XMLSyntaxRule[] getSyntaxRules() {
243 return rules;
244 }
245
246 private XMLSyntaxRule[] rules = new XMLSyntaxRule[]{
247 new ElementRule(DemographicModel.class, 1, 999), // at least one required
248 new ElementRule(Parameter.class, true) // optional
249 };
250 };
251
252 //
253 // protected stuff
254 //
255
256 private int populationCount;
257 private Parameter populationProportions;
258 private DemographicModel[] demographicModels;
259 }