Debian Codegraph @

src/dr/evomodel/epidemiology/casetocase/periodpriors/AbstractPeriodPriorDistribution.java

raw symbols SCIP
1 /*
2 * AbstractPeriodPriorDistribution.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.epidemiology.casetocase.periodpriors;
27
28 import dr.inference.loggers.LogColumn;
29 import dr.inference.loggers.Loggable;
30 import dr.inference.model.AbstractModel;
31 import dr.inference.model.Model;
32 import dr.inference.model.Parameter;
33 import dr.inference.model.Variable;
34
35 import java.util.ArrayList;
36
37
38 /**
39 * Abstract class for the probability of a set of latent or infectious periods being drawn from an unknown probability
40 * distribution, given hyperpriors on the parameters of that distribution.
41 */
42
43 public abstract class AbstractPeriodPriorDistribution extends AbstractModel implements Loggable {
44
45 // are we working on the logarithms of the values?
46 protected boolean log;
47
48 protected double logL;
49 protected double storedLogL;
50
51 public AbstractPeriodPriorDistribution(String name, boolean log) {
52 super(name);
53 this.log = log;
54 }
55
56 public double getLogLikelihood(double[] values){
57 if(!log){
58 return calculateLogLikelihood(values);
59 } else {
60 double[] logValues = new double[values.length];
61 for(int i=0; i<values.length; i++){
62 logValues[i] = Math.log(values[i]);
63 }
64 return calculateLogLikelihood(logValues);
65 }
66 }
67
68 public double getLogPosteriorProbability(double newValue, double minValue){
69 if(!log){
70 return calculateLogPosteriorProbability(newValue, minValue);
71 } else {
72 return calculateLogPosteriorProbability(Math.log(newValue), Math.log(minValue));
73 }
74 }
75
76 public double getLogPosteriorCDF(double limit, boolean upper){
77 if(!log){
78 return calculateLogPosteriorCDF(limit, upper);
79 } else {
80 return calculateLogPosteriorCDF(Math.log(limit), upper);
81 }
82 }
83
84 protected void handleModelChangedEvent(Model model, Object object, int index) {
85 //generally nothing to do
86 }
87
88 protected void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) {
89 //generally nothing to do
90 }
91
92 protected void storeState() {
93 storedLogL = logL;
94
95 }
96
97 protected void restoreState() {
98 logL = storedLogL;
99 }
100
101 protected void acceptState() {
102 //generally nothing to do
103 }
104
105
106 public LogColumn[] getColumns() {
107 ArrayList<LogColumn> columns = new ArrayList<LogColumn>();
108
109 columns.add(new LogColumn.Abstract(getModelName()+"_LL"){
110 protected String getFormattedValue() {
111 return String.valueOf(logL);
112 }
113 });
114 return columns.toArray(new LogColumn[columns.size()]);
115 }
116
117 public abstract void reset();
118
119 public abstract double calculateLogPosteriorProbability(double newValue, double minValue);
120
121 public abstract double calculateLogPosteriorCDF(double limit, boolean upper);
122
123 public abstract double calculateLogLikelihood(double[] values);
124
125
126
127
128 }