Debian Codegraph @

src/dr/math/distributions/BetaKDEDistribution.java

raw symbols SCIP
1 /*
2 * BetaKDEDistribution.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.GammaFunction;
29 import dr.stats.DiscreteStatistics;
30
31 //import java.io.BufferedReader;
32 //import java.io.FileReader;
33 //import java.io.FileNotFoundException;
34 //import java.io.IOException;
35 //import java.util.StringTokenizer;
36
37 /**
38 * @author Marc Suchard
39 */
40 public class BetaKDEDistribution extends KernelDensityEstimatorDistribution {
41
42 public BetaKDEDistribution(Double[] sample, Double lowerBound, Double upperBound, Double bandWidth) {
43 super(sample, lowerBound, upperBound, bandWidth);
44
45 }
46
47 @Override
48 public double getFromPoint() {
49 throw new RuntimeException("Not yet implemented");
50 }
51
52 @Override
53 public double getToPoint() {
54 throw new RuntimeException("Not yet implemented");
55 }
56
57 protected void processBounds(Double lowerBound, Double upperBound) {
58 if (lowerBound == null || upperBound == null || upperBound - lowerBound <= 0) {
59 throw new RuntimeException("BetaKDEDistribution must be bounded");
60 }
61 if (lowerBound > DiscreteStatistics.min(sample) || upperBound < DiscreteStatistics.max(sample))
62 throw new RuntimeException("Sample range outside bounds: "+DiscreteStatistics.min(sample)+" -> "+DiscreteStatistics.max(sample));
63
64 this.lowerBound = lowerBound;
65 this.upperBound = upperBound;
66
67 // Make a copy because we are translating values into [0,1)
68 double[] oldSample = sample;
69 sample = new double[sample.length];
70
71 range = upperBound - lowerBound;
72 for(int i=0; i<N; i++)
73 sample[i] = (oldSample[i] - this.lowerBound) / range;
74 }
75
76 protected void setBandWidth(Double bandWidth) {
77 if (bandWidth == null) {
78 // Default bandwidth
79 double sigma = DiscreteStatistics.stdev(sample);
80 this.bandWidth = sigma * Math.pow(N,-0.4); // Renault & Scaillet (2004); Chen (1999)
81 } else
82 this.bandWidth = bandWidth;
83 }
84
85 /* Beta kernel density estimator based on:
86 * Chen SX (1999) Beta kernel estimators for density functions. Comp. Statist. Data. Anal. 31, 131-145.
87 *
88 */
89 protected double evaluateKernel(double x) {
90
91 double xPrime = (x - lowerBound) / range;
92 double alphaMinus1 = xPrime/bandWidth - 1.0;
93 double betaMinus1 = (1.0 - xPrime)/bandWidth - 1.0;
94
95 if (xPrime < 2*bandWidth) // Removing these two cases reduces the kernel to C1
96 alphaMinus1 = getRho(xPrime,bandWidth) - 1.0;
97 else if (xPrime > 1 - 2*bandWidth)
98 betaMinus1 = getRho(1.0-xPrime,bandWidth) - 1.0;
99
100 double logK = GammaFunction.lnGamma(alphaMinus1+betaMinus1+2.0) - GammaFunction.lnGamma(alphaMinus1+1.0) - GammaFunction.lnGamma(betaMinus1+1.0);
101
102 double pdf = 0;
103 for(int i=0; i<N; i++)
104 pdf += Math.pow(sample[i],alphaMinus1) * Math.pow(1.0-sample[i],betaMinus1);
105
106 return pdf * Math.exp(logK) / (double)N / range;
107 }
108
109 private double getRho(double x, double bandWidth) {
110 return 2*bandWidth*bandWidth + 2.5 - Math.sqrt(4*bandWidth*bandWidth*bandWidth*bandWidth
111 + 6*bandWidth*bandWidth
112 + 2.25
113 - x*x
114 - x/bandWidth);
115 }
116
117 public double sampleMean() { return DiscreteStatistics.mean(sample); }
118
119 private double range;
120
121 // public static void main(String[] args) {
122 //
123 // String fileName = "out.txt";
124 // double[] values = null;
125 // try {
126 //
127 // BufferedReader reader = new BufferedReader(new FileReader(fileName));
128 //
129 // String line1 = reader.readLine();
130 // StringTokenizer st = new StringTokenizer(line1," ");
131 // values = new double[st.countTokens()];
132 // for(int i=0; i<values.length; i++)
133 // values[i] = Double.valueOf(st.nextToken());
134 //
135 // reader.close();
136 //
137 // } catch (FileNotFoundException e) {
138 // System.err.println("File not found: "+fileName);
139 // System.exit(-1);
140 // } catch (IOException e) {
141 // System.err.println("IO exception reading: "+fileName);
142 // System.exit(-1);
143 // }
144 //
145 //// System.err.println("v: "+new Vector(values));
146 // BetaKDEDistribution kde;
147 // kde = new BetaKDEDistribution(values,0.0, 10.0, 0.1);
148 // System.err.println("r: "+kde.pdf(3.0));
149 //
150 // kde = new BetaKDEDistribution(values,1.0, 9.0, 0.25);
151 // System.err.println("r: "+kde.pdf(7.0));
152 //
153 // System.err.println("sm: "+kde.sampleMean());
154 //
155 // }
156 }