Debian Codegraph @

src/dr/evomodel/coalescent/BMPriorLikelihood.java

raw symbols SCIP
1 /*
2 * BMPriorLikelihood.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.evomodelxml.coalescent.BMPriorLikelihoodParser;
29 import dr.inference.distribution.ParametricDistributionModel;
30 import dr.inference.model.AbstractModelLikelihood;
31 import dr.inference.model.Model;
32 import dr.inference.model.Parameter;
33 import dr.inference.model.Variable;
34
35 /**
36 * @author Joseph Heled
37 * Date: 25/04/2008
38 */
39
40 // It should be a model since it may be the only user of parameter sigma
41
42 public class BMPriorLikelihood extends AbstractModelLikelihood {
43 // private final Parameter mean;
44 private final Parameter sigma;
45 //private final Parameter lambda;
46 private final boolean logSpace;
47 //private final boolean normalize;
48 // private Parameter data;
49 // private Parameter times;
50 private ParametricDistributionModel popMeanPrior = null;
51 private final VariableDemographicModel m;
52
53 private BMPriorLikelihood(Parameter sigma, boolean logSpace, VariableDemographicModel m) {
54 super(BMPriorLikelihoodParser.BM);
55
56 this.logSpace = logSpace;
57 //this.normalize = normalize;
58
59 //this.mean = mean;
60 // if( mean != null ) {
61 // addVariable(mean);
62 // }
63
64 this.m = m;
65 this.sigma = sigma;
66 addVariable(sigma);
67
68 // this.lambda = lambda;
69 // if (lambda != null) {
70 // addVariable( lambda );
71 // }
72 }
73
74 public BMPriorLikelihood(Parameter sigma, VariableDemographicModel demographicModel, boolean logSpace,
75 ParametricDistributionModel popMeanPrior) {
76 this(sigma, logSpace, demographicModel);
77 // this.m = demographicModel;
78 addModel(demographicModel);
79
80 // this.data = this.times = null;
81 this.popMeanPrior = popMeanPrior;
82 }
83
84 // public BMPriorLikelihood(Parameter sigma,
85 // Parameter dataParameter, Parameter timesParameter, boolean logSpace, boolean normalize) {
86 // this(sigma, logSpace);
87 // dataParameter.addParameterListener(this);
88 // timesParameter.addParameterListener(this);
89 // this.data = dataParameter;
90 // this.times = timesParameter;
91 // }
92
93 // log of normal distribution coeffcient.
94 final private double logNormalCoef = -0.5 * Math.log(2 * Math.PI);
95
96 // A specialized version where everything is normalized. Time is normalized to 1. Data moved to mean zero and rescaled
97 // according to time.
98
99 private double reNormalize(VariableDemographicModel m) {
100
101 final double[] tps = m.getDemographicFunction().allTimePoints();
102 // get a copy since we re-scale data
103 final double[] vals = m.getPopulationValues().getParameterValues();
104
105 //assert ! logSpace : "not implemented yet";
106
107 final double tMax = tps[tps.length - 1];
108
109
110 if (false) {
111 return -Math.log(tMax / m.getDemographicFunction().getIntegral(0, tMax));
112 } else {
113 // compute mean
114 double popMean = tMax / m.getDemographicFunction().getIntegral(0, tMax);
115 // todo not correct when using midpoints
116 // if( m.getType() == VariableDemographicModel.Type.LINEAR ) {
117 // for(int k = 0; k < tps.length; ++k) {
118 // final double dt = (tps[k] - (k > 0 ? tps[k - 1] : 0));
119 // popMean += dt * (vals[k+1] + vals[k]);
120 // }
121 // popMean /= (2* tMax);
122 // } else {
123 // for(int k = 0; k < tps.length; ++k) {
124 // final double dt = (tps[k] - (k > 0 ? tps[k - 1] : 0));
125 // popMean += dt * vals[k];
126 // }
127 // popMean /= tMax;
128 // }
129
130 // Normalize to time interval = 1 and mean = 0
131 final double sigma = this.sigma.getStatisticValue(0);
132 // final double lam = 0.5 * tMax;
133 // for(int k = 0; k < vals.length; ++k) {
134 // vals[k] = vals[k]/tMax;
135 // }
136
137 // optimized version of the code in getLogLikelihood.
138 // get factors out when possible. logpdf of a normal is -x^2/2, when mean is 0
139 double ll = 0.0;
140
141 final double s2 = 2 * sigma * sigma;
142 for (int k = 0; k < tps.length; ++k) {
143 final double dt = ((tps[k] - (k > 0 ? tps[k - 1] : 0)) / tMax);
144
145 //final double d = (vals[k+1] - vals[k]);
146 final double r = logSpace ? (vals[k + 1] - vals[k]) : Math.log(vals[k + 1] / vals[k]);
147 final double d = r;
148 ll -= (d * d / (s2 * dt));
149 ll -= 0.5 * Math.log(dt);
150 }
151 ll += tps.length * (logNormalCoef - Math.log(sigma));
152 //ll /= tps.length;
153 if (popMeanPrior != null) {
154 ll += popMeanPrior.logPdf(popMean);
155 } else {
156 // default Jeffrey's
157 ll -= logSpace ? popMean : Math.log(popMean);
158 }
159
160 return ll;
161 }
162 }
163
164 public double getLogLikelihood() {
165 // if( lastValue != Double.NEGATIVE_INFINITY ) {
166 // return lastValue;
167 // }
168
169 assert m != null;
170 lastValue = reNormalize(m);
171 return lastValue;
172 }
173
174 public void makeDirty() {
175
176 lastValue = Double.NEGATIVE_INFINITY;
177 }
178
179 // simply saves last value
180 double lastValue = Double.NEGATIVE_INFINITY;
181
182 public Model getModel() {
183 return this;
184 }
185
186 protected void handleModelChangedEvent(Model model, Object object, int index) {
187 makeDirty();
188 }
189
190 protected void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) {
191 makeDirty();
192 }
193
194 protected void storeState() {
195 }
196
197 protected void restoreState() {
198 makeDirty();
199 }
200
201 protected void acceptState() {
202 }
203 }