Debian Codegraph @

src/dr/evomodel/coalescent/ScaledPiecewiseModel.java

raw symbols SCIP
1 /*
2 * ScaledPiecewiseModel.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.PiecewiseLinearPopulation;
31 import dr.evomodel.tree.TreeModel;
32 import dr.evomodelxml.coalescent.ScaledPiecewiseModelParser;
33 import dr.inference.model.Model;
34 import dr.inference.model.Parameter;
35
36 /**
37 * @author Alexei Drummond
38 * @author Andrew Rambaut
39 * @version $Id: ScaledPiecewiseModel.java,v 1.3 2005/04/11 11:24:39 alexei Exp $
40 */
41 public class ScaledPiecewiseModel extends DemographicModel {
42 //
43 // Public stuff
44 //
45 /**
46 * Construct demographic model with default settings
47 */
48 public ScaledPiecewiseModel(Parameter N0Parameter, TreeModel treeModel, boolean linear, Type units) {
49
50 this(ScaledPiecewiseModelParser.PIECEWISE_POPULATION, N0Parameter, treeModel, linear, units);
51
52 }
53
54 /**
55 * Construct demographic model with default settings
56 */
57 public ScaledPiecewiseModel(String name, Parameter N0Parameter, TreeModel treeModel, boolean linear, Type units) {
58
59 super(name);
60
61 if (N0Parameter.getDimension() < 2) {
62 throw new IllegalArgumentException("Must have at least 2 epochs");
63 }
64
65 this.N0Parameter = N0Parameter;
66 this.treeModel = treeModel;
67 addVariable(N0Parameter);
68 N0Parameter.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0, N0Parameter.getDimension()));
69
70 setUnits(units);
71
72 int epochs = N0Parameter.getDimension();
73
74 if (linear) {
75 piecewiseFunction = new PiecewiseLinearPopulation(new double[epochs - 1], new double[epochs], units);
76 } else {
77 piecewiseFunction = new PiecewiseConstantPopulation(new double[epochs - 1], new double[epochs], units);
78 }
79 }
80
81 /**
82 *
83 */
84 public DemographicFunction getDemographicFunction() {
85
86 double height = treeModel.getNodeHeight(treeModel.getRoot());
87 int epochs = N0Parameter.getDimension();
88
89 for (int i = 0; i < N0Parameter.getDimension(); i++) {
90 piecewiseFunction.setArgument(i, N0Parameter.getParameterValue(i));
91 if (i < epochs - 1) piecewiseFunction.setEpochDuration(i, height / (double) epochs);
92 }
93
94 return piecewiseFunction;
95 }
96
97 // **************************************************************
98 // Model IMPLEMENTATION
99 // **************************************************************
100
101 protected void handleModelChangedEvent(Model model, Object object, int index) {
102 // no intermediates need to be recalculated...
103 }
104
105 protected void handleParameterChangedEvent(Parameter parameter, int index) {
106
107 if (parameter == N0Parameter) {
108 //System.out.println("popSize parameter changed..");
109 }
110
111 // no intermediates need to be recalculated...
112 }
113
114 protected void storeState() {
115 } // no additional state needs storing
116
117 protected void restoreState() {
118 } // no additional state needs restoring
119
120 protected void acceptState() {
121 } // no additional state needs accepting
122
123 //
124 // protected stuff
125 //
126
127 Parameter N0Parameter;
128 TreeModel treeModel;
129 PiecewiseConstantPopulation piecewiseFunction = null;
130 }