Debian Codegraph @

src/dr/evomodel/coalescent/PiecewisePopulationModel.java

raw symbols SCIP
1 /*
2 * PiecewisePopulationModel.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.coalescent.DemographicFunction;
29 import dr.evolution.coalescent.PiecewiseConstantPopulation;
30 import dr.evolution.coalescent.PiecewiseExponentialPopulation;
31 import dr.evolution.coalescent.PiecewiseLinearPopulation;
32 import dr.inference.model.Model;
33 import dr.inference.model.Parameter;
34 import dr.inference.model.Statistic;
35
36 /**
37 * @author Alexei Drummond
38 * @author Andrew Rambaut
39 * @version $Id: PiecewisePopulationModel.java,v 1.13 2005/05/24 20:25:57 rambaut Exp $
40 */
41 public class PiecewisePopulationModel extends DemographicModel {
42
43 //
44 // Public stuff
45 //
46
47 /**
48 * Construct demographic model with default settings
49 */
50 public PiecewisePopulationModel(String name, Parameter N0Parameter, double[] epochLengths, boolean isLinear, Type units) {
51
52 super(name);
53
54 this.epochCount = epochLengths.length + 1;
55
56 if (N0Parameter.getDimension() != epochCount) {
57 throw new IllegalArgumentException(
58 "epochSize parameter must have the same dimensions as the number of epochs: (" + epochCount +
59 ") but instead has " + N0Parameter.getDimension() + "!"
60 );
61 }
62
63 this.N0Parameter = N0Parameter;
64 addVariable(N0Parameter);
65 N0Parameter.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0, N0Parameter.getDimension()));
66
67 //addVariable(epochLengths);
68 //epochLengths.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0, epochLengths.getDimension()));
69
70 setUnits(units);
71
72 if (isLinear) {
73 piecewiseFunction = new PiecewiseLinearPopulation(epochLengths, new double[N0Parameter.getDimension()], units);
74 } else {
75 piecewiseFunction = new PiecewiseConstantPopulation(epochLengths, new double[N0Parameter.getDimension()], units);
76 }
77 }
78
79 /**
80 * Construct demographic model with default settings
81 */
82 public PiecewisePopulationModel(String name, Parameter N0Parameter, Parameter growthRatesParameter,
83 double[] epochLengths, Type units) {
84
85 super(name);
86
87 this.epochCount = epochLengths.length + 1;
88
89 this.N0Parameter = N0Parameter;
90 this.growthRatesParameter = growthRatesParameter;
91 addVariable(N0Parameter);
92 N0Parameter.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0, N0Parameter.getDimension()));
93
94 addVariable(growthRatesParameter);
95 growthRatesParameter.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, growthRatesParameter.getDimension()));
96
97 setUnits(units);
98
99 int popSizeCount = N0Parameter.getDimension();
100 int growthRateCount = growthRatesParameter.getDimension();
101
102 if (popSizeCount == epochCount && growthRateCount == 1) {
103 piecewiseFunction = new PiecewiseExponentialPopulation(epochLengths,
104 new double[N0Parameter.getDimension()],
105 growthRatesParameter.getParameterValue(0),
106 units);
107 } else if (popSizeCount == 1 && growthRateCount == epochCount) {
108 piecewiseFunction = new PiecewiseExponentialPopulation(epochLengths,
109 N0Parameter.getParameterValue(0),
110 new double[growthRatesParameter.getDimension()], units);
111 } else {
112 if (growthRatesParameter.getDimension() != epochCount) {
113 throw new IllegalArgumentException(
114 "growthRate parameter must have the same dimension as the number of epochs: (" + epochCount +
115 ") but instead has " + N0Parameter.getDimension() + "!"
116 );
117 }
118 }
119
120
121 addStatistic(new GrowthRateStatistic());
122 }
123
124 public DemographicFunction getDemographicFunction() {
125 if (growthRatesParameter != null) {
126 // exponential growth
127 for (int i = 0; i < N0Parameter.getDimension(); i++) {
128 piecewiseFunction.setArgument(i, N0Parameter.getParameterValue(i));
129 }
130 for (int i = 0; i < growthRatesParameter.getDimension(); i++) {
131 piecewiseFunction.setArgument(i + N0Parameter.getDimension(), growthRatesParameter.getParameterValue(i));
132 }
133 } else {
134 // constant or linear growth
135 for (int i = 0; i < N0Parameter.getDimension(); i++) {
136 piecewiseFunction.setArgument(i, N0Parameter.getParameterValue(i));
137 }
138 }
139 return piecewiseFunction;
140 }
141
142 // **************************************************************
143 // Model IMPLEMENTATION
144 // **************************************************************
145
146 protected void handleModelChangedEvent(Model model, Object object, int index) {
147 // no intermediates need to be recalculated...
148 }
149
150 protected void handleParameterChangedEvent(Parameter parameter, int index) {
151
152 if (parameter == N0Parameter) {
153 //System.out.println("popSize parameter changed..");
154 }
155
156 // no intermediates need to be recalculated...
157 }
158
159 protected void storeState() {
160 } // no additional state needs storing
161
162 protected void restoreState() {
163 } // no additional state needs restoring
164
165 protected void acceptState() {
166 } // no additional state needs accepting
167
168 public class GrowthRateStatistic extends Statistic.Abstract {
169
170 public GrowthRateStatistic() {
171 super("growthRate");
172 }
173
174 public int getDimension() {
175 return ((PiecewiseExponentialPopulation) piecewiseFunction).getEpochCount();
176 }
177
178 public double getStatisticValue(int i) {
179 return ((PiecewiseExponentialPopulation) piecewiseFunction).getEpochGrowthRate(i);
180 }
181
182 }
183
184 //
185 // private stuff
186 //
187
188 private Parameter N0Parameter;
189 private Parameter growthRatesParameter;
190 private DemographicFunction piecewiseFunction = null;
191
192 private final int epochCount;
193 }