Debian Codegraph @

src/dr/inference/distribution/ExponentialMarkovModel.java

raw symbols SCIP
1 /*
2 * ExponentialMarkovModel.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.AbstractModelLikelihood;
29 import dr.inference.model.Model;
30 import dr.inference.model.Parameter;
31 import dr.inference.model.Variable;
32 import dr.math.distributions.GammaDistribution;
33 import org.w3c.dom.Document;
34 import org.w3c.dom.Element;
35
36 /**
37 * A class that acts as a model for exponentially distributed data.
38 *
39 * @author Andrew Rambaut
40 * @author Alexei Drummond
41 * @author Gerton Lunter
42 * @version $Id: ExponentialMarkovModel.java,v 1.8 2005/05/24 20:25:59 rambaut Exp $
43 */
44
45 public class ExponentialMarkovModel extends AbstractModelLikelihood {
46
47 public static final String EXPONENTIAL_MARKOV_MODEL = "exponentialMarkovLikelihood";
48
49 /**
50 * Constructor.
51 */
52 public ExponentialMarkovModel(Parameter chainParameter, boolean jeffreys, boolean reverse, double shape) {
53
54 super(EXPONENTIAL_MARKOV_MODEL);
55
56 this.chainParameter = chainParameter;
57 this.jeffreys = jeffreys;
58 this.reverse = reverse;
59 this.shape = shape;
60
61 addVariable(chainParameter);
62 chainParameter.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0, chainParameter.getDimension()));
63 }
64
65 public Parameter getChainParameter() {
66 return (Parameter)getVariable(0);
67 }
68
69 // *****************************************************************
70 // Interface Model
71 // *****************************************************************
72
73 public void handleModelChangedEvent(Model model, Object object, int index) {
74 // no intermediates need to be recalculated...
75 }
76
77 protected final void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) {
78 // no intermediates need to be recalculated...
79 }
80
81 protected void storeState() {
82 } // no additional state needs storing
83
84 protected void restoreState() {
85 } // no additional state needs restoring
86
87 protected void acceptState() {
88 } // no additional state needs accepting
89
90 // **************************************************************
91 // XMLElement IMPLEMENTATION
92 // **************************************************************
93
94 public Element createElement(Document document) {
95 throw new RuntimeException("Not implemented!");
96 }
97
98 // **************************************************************
99 // Likelihood
100 // **************************************************************
101
102 /**
103 * Get the model.
104 *
105 * @return the model.
106 */
107 public Model getModel() {
108 return this;
109 }
110
111 private int index(int i) {
112 if (reverse)
113 return chainParameter.getDimension() - i - 1;
114 else
115 return i;
116 }
117
118 /**
119 * Get the log likelihood.
120 *
121 * @return the log likelihood.
122 */
123 public double getLogLikelihood() {
124
125 double logL = 0.0;
126 // jeffreys Prior!
127 if (jeffreys) {
128 logL += -Math.log(chainParameter.getParameterValue(index(0)));
129 }
130 for (int i = 1; i < chainParameter.getDimension(); i++) {
131 final double mean = chainParameter.getParameterValue(index(i - 1));
132 final double x = chainParameter.getParameterValue(index(i));
133 //logL += dr.math.distributions.ExponentialDistribution.logPdf(x, 1.0/mean);
134
135 final double scale = mean / shape;
136 logL += GammaDistribution.logPdf(x, shape, scale);
137 }
138 return logL;
139 }
140
141 /**
142 * Forces a complete recalculation of the likelihood next time getLikelihood is called
143 */
144 public void makeDirty() {
145 }
146
147 // **************************************************************
148 // Identifiable IMPLEMENTATION
149 // **************************************************************
150
151 private String id = null;
152
153 public void setId(String id) {
154 this.id = id;
155 }
156
157 public String getId() {
158 return id;
159 }
160
161 // **************************************************************
162 // Private instance variables
163 // **************************************************************
164
165 private Parameter chainParameter = null;
166 private boolean jeffreys = false;
167 private boolean reverse = false;
168 private double shape = 1.0;
169
170 }
171