Debian Codegraph @

src/dr/inference/distribution/NegativeBinomialDistributionModel.java

raw symbols SCIP
1 /*
2 * NegativeBinomialDistributionModel.java
3 *
4 * Copyright (c) 2002-2016 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.AbstractModel;
29 import dr.inference.model.Model;
30 import dr.inference.model.Parameter;
31 import dr.inference.model.Variable;
32 import dr.inferencexml.distribution.NegativeBinomialDistributionModelParser;
33 import dr.math.UnivariateFunction;
34 import dr.math.distributions.NegativeBinomialDistribution;
35 import org.w3c.dom.Document;
36 import org.w3c.dom.Element;
37
38 /**
39 * A class that acts as a model for Negative Binomially distributed data.
40 *
41 * @author Andrew Rambaut
42 * @version $Id$
43 */
44
45 public class NegativeBinomialDistributionModel extends AbstractModel implements ParametricDistributionModel {
46
47 /**
48 * Constructor.
49 */
50 public NegativeBinomialDistributionModel(Variable<Double> mean, Variable<Double> alpha) {
51
52 super(NegativeBinomialDistributionModelParser.NEGATIVE_BINOMIAL_DISTRIBUTION_MODEL);
53
54 this.mean = mean;
55 addVariable(mean);
56 mean.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0, 1));
57
58 this.alpha = alpha;
59 addVariable(alpha);
60 alpha.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0, 1));
61 }
62
63 // *****************************************************************
64 // Interface Distribution
65 // *****************************************************************
66
67 public double pdf(double x) {
68 return NegativeBinomialDistribution.pdf(x, mean(), alpha());
69 }
70
71 public double logPdf(double x) {
72 return NegativeBinomialDistribution.logPdf(x, mean(), alpha());
73 }
74
75 public double cdf(double x) {
76 return NegativeBinomialDistribution.cdf(x, mean(), alpha());
77 }
78
79 public double quantile(double y) {
80 throw new RuntimeException("Not implemented.");
81 }
82
83 public double mean() {
84 return mean.getValue(0);
85 }
86
87 public double alpha() {
88 return alpha.getValue(0);
89 }
90
91 public double variance() {
92 throw new RuntimeException("Not implemented!");
93 }
94
95 public final UnivariateFunction getProbabilityDensityFunction() {
96 return pdfFunction;
97 }
98
99 private final UnivariateFunction pdfFunction = new UnivariateFunction() {
100 public final double evaluate(double x) {
101 return pdf(x);
102 }
103
104 public final double getLowerBound() {
105 return 0.0;
106 }
107
108 public final double getUpperBound() {
109 return Double.POSITIVE_INFINITY;
110 }
111 };
112
113 // *****************************************************************
114 // Interface DensityModel
115 // *****************************************************************
116
117 @Override
118 public double logPdf(double[] x) {
119 return logPdf(x[0]);
120 }
121
122 @Override
123 public Variable<Double> getLocationVariable() {
124 return mean;
125 }
126
127 // *****************************************************************
128 // Interface Model
129 // *****************************************************************
130
131 public void handleModelChangedEvent(Model model, Object object, int index) {
132 // no intermediates need to be recalculated...
133 }
134
135 protected final void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) {
136 fireModelChanged();
137 }
138
139 protected void storeState() {
140 } // no additional state needs storing
141
142 protected void restoreState() {
143 } // no additional state needs restoring
144
145 protected void acceptState() {
146 } // no additional state needs accepting
147
148 public Element createElement(Document document) {
149 throw new RuntimeException("Not implemented!");
150 }
151
152 // **************************************************************
153 // Private instance variables
154 // **************************************************************
155
156 private final Variable<Double> mean;
157 private final Variable<Double> alpha;
158
159 }