Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * IndividualPrior.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.evomodel.epidemiology.casetocase.AbstractCase;
29 import dr.inference.distribution.LogNormalDistributionModel;
30 import dr.inference.distribution.NormalDistributionModel;
31 import dr.inference.distribution.ParametricDistributionModel;
32 import dr.math.distributions.LogNormalDistribution;
33 import dr.math.distributions.NormalDistribution;
34 import dr.xml.*;
35
36 import java.util.HashMap;
37
38 /**
39 * Created by mhall on 03/06/2014.
40 */
41 public class IndividualPrior extends AbstractPeriodPriorDistribution {
42
43 ParametricDistributionModel distribution;
44
45 public static final String INDIVIDUAL_PRIOR = "individualPrior";
46 public static final String ID = "id";
47 public static final String DISTRIBUTION = "distribution";
48
49 public IndividualPrior(String name, ParametricDistributionModel distribution){
50 super(name, false);
51 this.distribution = distribution;
52 }
53
54 public void reset() {
55 // nothing to do.
56 }
57
58 public double calculateLogPosteriorProbability(double newValue, double minValue) {
59 double logNumerator = distribution.logPdf(newValue);
60 double logDenominator = Math.log(1-distribution.cdf(minValue));
61
62 if(logDenominator == Double.NEGATIVE_INFINITY){
63 if(distribution instanceof LogNormalDistributionModel){
64 double mean = ((LogNormalDistributionModel)distribution).getMu();
65 double stdev = ((LogNormalDistributionModel)distribution).getSigma();
66
67 double scaledValue = (Math.log(minValue)-mean)/stdev;
68
69 logDenominator = NormalDistribution.standardCDF(-scaledValue, true);
70
71 } else if (distribution instanceof NormalDistributionModel){
72 double mean = ((NormalDistributionModel)distribution).getMean().getValue(0);
73 double stdev = ((NormalDistributionModel)distribution).getStdev();
74
75 double scaledValue = (minValue-mean)/stdev;
76
77 logDenominator = NormalDistribution.standardCDF(-scaledValue, true);
78 }
79 }
80 return logNumerator - logDenominator;
81 }
82
83 public double calculateLogPosteriorCDF(double limit, boolean upper) {
84 double out;
85 if(upper) {
86 out = Math.log(1-distribution.cdf(limit));
87 } else {
88 out = Math.log(distribution.cdf(limit));
89 }
90 if(out == Double.NEGATIVE_INFINITY){
91 if(distribution instanceof LogNormalDistributionModel){
92 double mean = ((LogNormalDistributionModel)distribution).getMu();
93 double stdev = ((LogNormalDistributionModel)distribution).getSigma();
94
95 double scaledValue = (Math.log(limit)-mean)/stdev;
96
97 out = NormalDistribution.standardCDF(-scaledValue, true);
98
99 } else if (distribution instanceof NormalDistributionModel){
100 double mean = ((NormalDistributionModel)distribution).getMean().getValue(0);
101 double stdev = ((NormalDistributionModel)distribution).getStdev();
102
103 double scaledValue = (limit-mean)/stdev;
104
105 out = NormalDistribution.standardCDF(-scaledValue, true);
106 }
107 }
108 return out;
109 }
110
111 public double calculateLogLikelihood(double[] values) {
112 double out = 0;
113
114 for (double value : values) {
115 out += distribution.logPdf(value);
116 }
117
118 return out;
119 }
120
121 public static XMLObjectParser PARSER = new AbstractXMLObjectParser() {
122
123 public String getParserName() {
124 return INDIVIDUAL_PRIOR;
125 }
126
127 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
128 String id = (String) xo.getAttribute(ID);
129 ParametricDistributionModel distribution =
130 (ParametricDistributionModel)xo.getElementFirstChild(DISTRIBUTION);
131
132 return new IndividualPrior(id, distribution);
133
134 }
135
136 public XMLSyntaxRule[] getSyntaxRules() {
137 return rules;
138 }
139
140 private final XMLSyntaxRule[] rules = {
141 AttributeRule.newStringRule(ID, false),
142 new ElementRule(DISTRIBUTION, ParametricDistributionModel.class)
143 };
144
145 public String getParserDescription() {
146 return "Calculates the probability of a set of doubles all being drawn from the specified prior " +
147 "distribution";
148 }
149
150 public Class getReturnType() {
151 return IndividualPrior.class;
152 }
153 };
154 }