Debian Codegraph @

src/dr/math/distributions/KernelDensityEstimatorDistribution.java

raw symbols SCIP
1 /*
2 * KernelDensityEstimatorDistribution.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.math.distributions;
27
28 import dr.math.UnivariateFunction;
29 import org.apache.commons.math.FunctionEvaluationException;
30 import org.apache.commons.math.MaxIterationsExceededException;
31 import org.apache.commons.math.analysis.DifferentiableUnivariateRealFunction;
32 import org.apache.commons.math.analysis.UnivariateRealFunction;
33 import org.apache.commons.math.analysis.integration.SimpsonIntegrator;
34 import org.apache.commons.math.analysis.solvers.NewtonSolver;
35
36 /**
37 * @author Marc Suchard
38 */
39 public abstract class KernelDensityEstimatorDistribution implements Distribution {
40
41 KernelDensityEstimatorDistribution(Double[] sample, Double lowerBound, Double upperBound, Double bandWidth) {
42
43 this.sample = new double[sample.length];
44 for (int i = 0; i < sample.length; i++) {
45 this.sample[i] = sample[i];
46 }
47 this.N = sample.length;
48 processBounds(lowerBound, upperBound);
49 setBandWidth(bandWidth);
50 }
51
52 abstract protected double evaluateKernel(double x);
53
54 abstract protected void processBounds(Double lowerBound, Double upperBound);
55
56 abstract protected void setBandWidth(Double bandWidth);
57
58 abstract public double getFromPoint();
59
60 abstract public double getToPoint();
61
62 /**
63 * probability density function of the distribution
64 *
65 * @param x argument
66 * @return pdf value
67 */
68 public double pdf(double x) {
69 return evaluateKernel(x);
70 }
71
72 /**
73 * the natural log of the probability density function of the distribution
74 *
75 * @param x argument
76 * @return log pdf value
77 */
78 public double logPdf(double x) {
79 return Math.log(pdf(x));
80 }
81
82 /**
83 * cumulative density function of the distribution
84 *
85 * @param x argument
86 * @return cdf value
87 */
88 public double cdf(double x) {
89
90 double cdf;
91 try {
92 cdf = cdfFunction.value(x);
93 } catch (FunctionEvaluationException e) {
94 throw new RuntimeException(e.getMessage());
95 }
96
97 return cdf;
98 }
99
100 /**
101 * quantile (inverse cumulative density function) of the distribution
102 *
103 * @param y argument
104 * @return cdf value
105 */
106 public double quantile(final double y) {
107
108 final DifferentiableUnivariateRealFunction root =
109 new DifferentiableUnivariateRealFunction() {
110
111 @Override
112 public UnivariateRealFunction derivative() {
113 return pdfFunction;
114 }
115
116 @Override
117 public double value(double x) throws FunctionEvaluationException {
118
119 return cdfFunction.value(x) - y;
120 }
121 };
122
123 NewtonSolver solver = new NewtonSolver(root);
124
125 double q;
126 try {
127 q = solver.solve(getFromPoint(), getToPoint());
128 } catch (MaxIterationsExceededException e) {
129 throw new RuntimeException(e.getMessage());
130 } catch (FunctionEvaluationException e) {
131 throw new RuntimeException(e.getMessage());
132 }
133
134 return q;
135 }
136
137 /**
138 * mean of the distribution
139 *
140 * @return mean
141 */
142 public double mean() {
143 throw new RuntimeException("Not Implemented.");
144 }
145
146 /**
147 * variance of the distribution
148 *
149 * @return variance
150 */
151 public double variance() {
152 throw new RuntimeException("Not Implemented.");
153 }
154
155 /**
156 * @return a probability density function representing this distribution
157 */
158 public UnivariateFunction getProbabilityDensityFunction() {
159 throw new RuntimeException("Not Implemented.");
160 }
161
162 public double getBandWidth() {
163 return bandWidth;
164 }
165
166 public enum Type {
167 GAUSSIAN("Gaussian"),
168 GAMMA("Gamma"),
169 LOG_TRANSFORMED_GAUSSIAN("LogTransformedGaussian"),
170 BETA("Beta");
171
172 Type(String text) {
173 this.text = text;
174 }
175
176 public String getText() {
177 return text;
178 }
179
180 public static Type parseFromString(String text) {
181 for (Type format : Type.values()) {
182 if (format.getText().compareToIgnoreCase(text) == 0)
183 return format;
184 }
185 return null;
186 }
187
188 private final String text;
189 }
190
191 protected int N;
192 protected double lowerBound;
193 protected double upperBound;
194 double bandWidth;
195 protected double[] sample;
196
197 private final SimpsonIntegrator integrator = new SimpsonIntegrator();
198
199 private final UnivariateRealFunction pdfFunction = new UnivariateRealFunction() {
200
201 @Override
202 public double value(double x) throws FunctionEvaluationException {
203 return pdf(x);
204 }
205 };
206
207 private final UnivariateRealFunction cdfFunction = new UnivariateRealFunction() {
208
209 @Override
210 public double value(double x) throws FunctionEvaluationException {
211 double rangeMin = getFromPoint();
212
213 double cdf;
214 try {
215 cdf = integrator.integrate(pdfFunction, rangeMin, x);
216 } catch (MaxIterationsExceededException e) {
217 throw new RuntimeException(e.getMessage());
218 }
219
220 return cdf;
221 }
222 };
223 }