Debian Codegraph @

src/dr/evomodel/coalescent/OrnsteinUhlenbeckPriorLikelihood.java

raw symbols SCIP
1 /*
2 * OrnsteinUhlenbeckPriorLikelihood.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.OrnsteinUhlenbeckPriorLikelihoodParser;
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 import dr.math.distributions.NormalDistribution;
35
36 /**
37 * Ornstein-Uhlenbeck prior.
38 * <p/>
39 * A diffusion process - Basically a correlated sequence of normally distributed values, where correlation is time
40 * dependent.
41 * <p/>
42 * <p/>
43 * Very experimental and only slightly tested at this time
44 *
45 * @author Joseph Heled
46 * Date: 25/04/2008
47 */
48
49 // It should be a model since it may be the only user of parameter sigma
50
51 public class OrnsteinUhlenbeckPriorLikelihood extends AbstractModelLikelihood {
52 private final Parameter mean;
53 private final Parameter sigma;
54 private final Parameter lambda;
55 private final boolean logSpace;
56 private final boolean normalize;
57 private Parameter data;
58 private Parameter times;
59 private ParametricDistributionModel popMeanPrior = null;
60 private VariableDemographicModel m = null;
61
62 private OrnsteinUhlenbeckPriorLikelihood(Parameter mean, Parameter sigma, Parameter lambda,
63 boolean logSpace, boolean normalize) {
64 super(OrnsteinUhlenbeckPriorLikelihoodParser.OU);
65
66 this.logSpace = logSpace;
67 this.normalize = normalize;
68
69 this.mean = mean;
70 if (mean != null) {
71 addVariable(mean);
72 }
73
74 this.sigma = sigma;
75 addVariable(sigma);
76
77 this.lambda = lambda;
78 if (lambda != null) {
79 addVariable(lambda);
80 }
81 }
82
83 public OrnsteinUhlenbeckPriorLikelihood(Parameter mean, Parameter sigma, Parameter lambda,
84 VariableDemographicModel demographicModel, boolean logSpace, boolean normalize,
85 ParametricDistributionModel popMeanPrior) {
86 this(mean, sigma, lambda, logSpace, normalize);
87 this.m = demographicModel;
88
89 this.data = this.times = null;
90 this.popMeanPrior = popMeanPrior;
91 }
92
93 public OrnsteinUhlenbeckPriorLikelihood(Parameter mean, Parameter sigma, Parameter lambda,
94 Parameter dataParameter, Parameter timesParameter, boolean logSpace, boolean normalize) {
95 this(mean, sigma, lambda, logSpace, normalize);
96 dataParameter.addParameterListener(this);
97 timesParameter.addParameterListener(this);
98 this.data = dataParameter;
99 this.times = timesParameter;
100 }
101
102 // log of normal distribution coefficient.
103 final private double logNormalCoef = -0.5 * Math.log(2 * Math.PI);
104
105 // A specialized version where everything is normalized. Time is normalized to 1. Data moved to mean zero and rescaled
106 // according to time. Lambda is 0.5. The prior on mean is added.
107
108 private double reNormalize(VariableDemographicModel m) {
109 final double[] tps = m.getDemographicFunction().allTimePoints();
110 // get a copy since we re-scale data
111 final double[] vals = m.getPopulationValues().getParameterValues();
112
113 assert !logSpace : "not implemented yet";
114
115 final double len = tps[tps.length - 1];
116
117 // compute mean
118 double popMean = 0;
119 // todo not correct when using midpoints
120 if (m.getType() == VariableDemographicModel.Type.LINEAR) {
121 for (int k = 0; k < tps.length; ++k) {
122 final double dt = (tps[k] - (k > 0 ? tps[k - 1] : 0));
123 popMean += dt * (vals[k + 1] + vals[k]);
124 }
125 popMean /= (2 * len);
126 } else {
127 for (int k = 0; k < tps.length; ++k) {
128 final double dt = (tps[k] - (k > 0 ? tps[k - 1] : 0));
129 popMean += dt * vals[k];
130 }
131 popMean /= len;
132 }
133
134 // Normalize to time interval = 1 and mean = 0
135 final double sigma = this.sigma.getStatisticValue(0) / Math.sqrt(len);
136 final double lam = 0.5 * len;
137 for (int k = 0; k < vals.length; ++k) {
138 vals[k] = (vals[k] - popMean) / len;
139 }
140
141 // optimized version of the code in getLogLikelihood.
142 // get factors out when possible. logpdf of a normal is -x^2/2, when mean is 0
143 double ll = 0.0;
144
145 for (int k = 0; k < tps.length; ++k) {
146 final double dt = (tps[k] - (k > 0 ? tps[k - 1] : 0)) / len;
147
148 final double a = Math.exp(-lam * dt);
149 final double d = (vals[k + 1] - vals[k] * a);
150
151 final double c = 1 - a * a;
152 ll += (d * d / c) - 0.5 * Math.log(c);
153 }
154
155 final double f2 = (2 * lam) / (sigma * sigma);
156 ll = tps.length * (logNormalCoef - Math.log(sigma)) + ll * f2 / -2;
157
158 if (popMeanPrior != null) {
159 ll += popMeanPrior.logPdf(popMean);
160 } else {
161 // default Jeffreys
162 ll -= Math.log(popMean);
163 }
164 return ll;
165 }
166
167 public double getLogLikelihood() {
168 if (lastValue > 0) {
169 return lastValue;
170 }
171 double logL;
172
173 if (normalize) {
174 assert m != null;
175 logL = reNormalize(m);
176 } else {
177
178 // final double[] tps = m != null ? m.getDemographicFunction().allTimePoints() : times.getParameterValues();
179 //
180 // final double[] vals = m != null ? m.getPopulationValues().getParameterValues() : data.getParameterValues();
181
182 final double[] tps = times.getParameterValues();
183 final double[] vals = data.getParameterValues();
184
185 if (logSpace) {
186 for (int k = 0; k < vals.length; ++k) {
187 vals[k] = Math.log(vals[k]);
188 }
189 }
190
191 final double lambda = this.lambda.getStatisticValue(0);
192 final double mean = this.mean.getStatisticValue(0);
193
194 double sigma = this.sigma.getStatisticValue(0);
195 if (normalize) {
196 // make the process have a SD of sigma
197 sigma *= Math.sqrt(2 * lambda);
198 }
199
200 logL = NormalDistribution.logPdf(vals[0], mean, sigma);
201
202 final double xScale = -lambda * (normalize ? 1.0 / tps[tps.length - 1] : 1.0);
203
204 for (int k = 0; k < tps.length; ++k) {
205 double dt = tps[k] - (k > 0 ? tps[k - 1] : 0);
206 double a = Math.exp(xScale * dt);
207 double den = sigma * Math.sqrt((1 - a * a) / (2 * lambda));
208 double z = (vals[k + 1] - (vals[k] * a + mean * (1 - a))) / den;
209 logL += NormalDistribution.logPdf(z, 0, 1);
210 }
211 }
212
213 lastValue = logL;
214
215 return logL;
216 }
217
218 public void makeDirty() {
219 lastValue = -1;
220 }
221
222 // simply saves last value
223 double lastValue = -1;
224
225 public Model getModel() {
226 return this;
227 }
228
229 protected void handleModelChangedEvent(Model model, Object object, int index) {
230 makeDirty();
231 }
232
233 protected void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) {
234 makeDirty();
235 }
236
237 protected void storeState() {
238 }
239
240 protected void restoreState() {
241 }
242
243 protected void acceptState() {
244 }
245 }