Debian Codegraph @

src/dr/evomodel/coalescent/EmpiricalPiecewiseModel.java

raw symbols SCIP
1 /*
2 * EmpiricalPiecewiseModel.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.EmpiricalPiecewiseConstant;
30 import dr.evomodelxml.coalescent.EmpiricalPiecewiseModelParser;
31 import dr.inference.model.Model;
32 import dr.inference.model.Parameter;
33
34 /**
35 * @author Alexei Drummond
36 * @author Andrew Rambaut
37 * @version $Id: EmpiricalPiecewiseModel.java,v 1.4 2005/04/11 11:24:39 alexei Exp $
38 */
39 public class EmpiricalPiecewiseModel extends DemographicModel {
40 //
41 // Public stuff
42 //
43 /**
44 * Construct demographic model with default settings
45 */
46 public EmpiricalPiecewiseModel(double[] intervalWidths, Parameter populationSizesParameter, Parameter tauParameter, Parameter bParameter, Parameter lagParameter, Type units) {
47
48 this(EmpiricalPiecewiseModelParser.EMPIRICAL_PIECEWISE, intervalWidths, populationSizesParameter, tauParameter, bParameter, lagParameter, units);
49
50 }
51
52 /**
53 * Construct demographic model with default settings
54 */
55 public EmpiricalPiecewiseModel(String name, double[] intervalWidths, Parameter populationSizesParameter, Parameter tauParameter, Parameter bParameter, Parameter lagParameter, Type units) {
56
57 super(name);
58
59 //System.out.println("intervalWidths.length=" + intervalWidths.length);
60 //System.out.println("populationSizes.dimension=" + populationSizesParameter.getDimension());
61
62 if (intervalWidths.length == 1) {
63 double[] newIntervalWidths = new double[populationSizesParameter.getDimension() - 1];
64 for (int i = 0; i < newIntervalWidths.length; i++) {
65 newIntervalWidths[i] = intervalWidths[0];
66 }
67 intervalWidths = newIntervalWidths;
68 }
69 //System.out.println("new intervalWidths.length=" + intervalWidths.length);
70
71 if (populationSizesParameter.getDimension() != (intervalWidths.length + 1)) {
72 throw new IllegalArgumentException(
73 "interval widths array must have either 1 or " + (populationSizesParameter.getDimension() - 1) +
74 " elements, but instead it has " + intervalWidths.length + "."
75 );
76 }
77
78 this.tauParameter = tauParameter;
79 this.lagParameter = lagParameter;
80 this.bParameter = bParameter;
81
82 this.intervalWidths = intervalWidths;
83 this.populationSizesParameter = populationSizesParameter;
84
85 addVariable(tauParameter);
86 addVariable(lagParameter);
87 addVariable(bParameter);
88 addVariable(populationSizesParameter);
89 tauParameter.addBounds(new Parameter.DefaultBounds(Double.MAX_VALUE, 0.0, tauParameter.getDimension()));
90 lagParameter.addBounds(new Parameter.DefaultBounds(Double.MAX_VALUE, 0.0, lagParameter.getDimension()));
91 bParameter.addBounds(new Parameter.DefaultBounds(Double.MAX_VALUE, 0.0, bParameter.getDimension()));
92 populationSizesParameter.addBounds(new Parameter.DefaultBounds(Double.MAX_VALUE, 0.0, populationSizesParameter.getDimension()));
93
94 setUnits(units);
95
96 piecewiseFunction = new EmpiricalPiecewiseConstant(intervalWidths, calculatePopSizes(),
97 lagParameter.getParameterValue(0), units);
98 }
99
100 /**
101 *
102 */
103 public DemographicFunction getDemographicFunction() {
104 piecewiseFunction.setLag(lagParameter.getParameterValue(0));
105 piecewiseFunction.setPopulationSizes(calculatePopSizes());
106
107 return piecewiseFunction;
108 }
109
110 private double[] calculatePopSizes() {
111 double m = tauParameter.getParameterValue(0);
112 double c = bParameter.getParameterValue(0);
113
114 double[] popSizes = new double[populationSizesParameter.getDimension()];
115 for (int i = 0; i < popSizes.length; i++) {
116 popSizes[i] = m * populationSizesParameter.getParameterValue(i) + c;
117 }
118 return popSizes;
119 }
120
121 // **************************************************************
122 // Model IMPLEMENTATION
123 // **************************************************************
124
125 protected void handleModelChangedEvent(Model model, Object object, int index) {
126 // no intermediates need to be recalculated...
127 }
128
129 // protected void handleVariableChangedEvent(Parameter parameter, int index) {
130 //
131 // // no intermediates need to be recalculated...
132 // }
133 //
134
135 // todo: why override?
136
137 protected void storeState() {
138 } // no additional state needs storing
139
140 protected void restoreState() {
141 } // no additional state needs restoring
142
143 protected void acceptState() {
144 } // no additional state needs accepting
145
146 //
147 // protected stuff
148 //
149
150 Parameter tauParameter;
151 Parameter lagParameter;
152 Parameter bParameter;
153 Parameter populationSizesParameter;
154 double[] intervalWidths;
155 EmpiricalPiecewiseConstant piecewiseFunction = null;
156 }