Debian Codegraph @

src/dr/inference/distribution/RandomWalkModel.java

raw symbols SCIP
1 /*
2 * RandomWalkModel.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.inference.distribution;
27
28 import dr.inference.model.*;
29
30 import java.util.logging.Logger;
31
32 /**
33 * @author Marc A. Suchard
34 */
35 public class RandomWalkModel extends AbstractModelLikelihood {
36
37 public RandomWalkModel(
38 ParametricDistributionModel distribution,
39 Parameter data, boolean forwardOrder, boolean logScale) {
40 super(null);
41 this.distribution = distribution;
42 this.forwardOrder = forwardOrder;
43 this.logScale = logScale;
44 this.data = data;
45 if (distribution != null) {
46 addModel(distribution);
47 }
48
49 double lower = Double.NEGATIVE_INFINITY;
50 if (logScale)
51 lower = 0.0;
52
53 addVariable(data);
54 if (data instanceof CompoundParameter) {
55 CompoundParameter cp = (CompoundParameter) data;
56 for (int i = 0; i < cp.getParameterCount(); i++) {
57 Parameter p = cp.getParameter(i);
58 p.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, lower, p.getDimension()));
59 }
60 } else
61 data.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, lower, data.getDimension()));
62
63 Logger.getLogger("dr.inference").info("Setting up a first-order random walk:");
64 Logger.getLogger("dr.inference").info("\tData parameter: " + data.getId());
65 Logger.getLogger("dr.inference").info("\tOn scale: " + (logScale ? "log" : "real"));
66 Logger.getLogger("dr.inference").info("\tDistribution: " + distribution.getId());
67 Logger.getLogger("dr.inference").info("\tIf you publish results using this model, please cite Suchard and Lemey (in preparation)\n");
68 }
69
70 protected double calculateLogLikelihood() {
71
72 final int dim = data.getDimension();
73
74 double logLikelihood = 0;
75 double previous = data.getParameterValue(0);
76 if (logScale)
77 previous = Math.log(previous);
78 for (int i = 1; i < dim; i++) {
79 double current = data.getParameterValue(i);
80 if (logScale)
81 current = Math.log(current);
82 logLikelihood += distribution.logPdf(current - previous);
83 if (logScale)
84 logLikelihood -= current;
85 previous = current;
86 }
87
88 return logLikelihood;
89 }
90
91 protected void handleModelChangedEvent(Model model, Object object, int index) {
92 likelihoodKnown = false;
93 }
94
95 protected void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) {
96 likelihoodKnown = false;
97 }
98
99 protected void storeState() {
100 }
101
102 protected void restoreState() {
103 }
104
105 protected void acceptState() {
106 }
107
108 private final ParametricDistributionModel distribution;
109 private final boolean logScale;
110 private Parameter data;
111 protected boolean likelihoodKnown;
112 private boolean forwardOrder;
113
114 public Model getModel() {
115 return this;
116 }
117
118 public double getLogLikelihood() {
119 return calculateLogLikelihood();
120 }
121
122 public void makeDirty() {
123 }
124 }