Debian Codegraph @

src/dr/math/distributions/GammaKDEDistribution.java

raw symbols SCIP
1 /*
2 * GammaKDEDistribution.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.stats.DiscreteStatistics;
29
30 import java.util.Random;
31
32
33 /**
34 * @author Jennifer Tom
35 * Based on S. X. Chen. Probability density function estimation using gamma kernels.
36 * Annals of the Institute of Statistical Mathematics, 52(3):471-480, 2000.
37 * Use to create KDE for positive valued functions
38 * Assumes limits are (0, inf)
39 * Must provide with a bandwidth, or defaults to Scott's Rule
40 * Univariate distribution only
41 */
42 public class GammaKDEDistribution extends KernelDensityEstimatorDistribution {
43
44
45 public GammaKDEDistribution(Double[] sample) {
46 this(sample, null);
47 }
48
49 public GammaKDEDistribution(Double[] sample, Double bandWidth) {
50 super(sample, 0.0, Double.POSITIVE_INFINITY, bandWidth);
51
52 }
53
54 @Override
55 public double getFromPoint() {
56 throw new RuntimeException("Not yet implemented");
57 }
58
59 @Override
60 public double getToPoint() {
61 throw new RuntimeException("Not yet implemented");
62 }
63
64 protected void processBounds(Double lowerBound, Double upperBound) {
65 if (lowerBound > DiscreteStatistics.min(sample)) {
66 throw new RuntimeException("Sample min out of bounds. Gamma kernel for use with positive data only: " + DiscreteStatistics.min(sample));
67 } else if (upperBound < DiscreteStatistics.max(sample)) {
68 throw new RuntimeException("Sample max out of bounds" + DiscreteStatistics.max(sample));
69 }
70 this.lowerBound = lowerBound;
71 this.upperBound = upperBound;
72
73 }
74
75 protected void setBandWidth(Double bandWidth) {
76 if (bandWidth == null) {
77 double sigma = DiscreteStatistics.stdev(sample);
78 //Scott's rule (Hardle, 2004, Nonparametric and Semiparameteric Models)
79 this.bandWidth = sigma * Math.pow(N, -0.2);
80 } else
81 this.bandWidth = bandWidth;
82
83 }
84
85 protected double evaluateKernel(double x) {
86
87 double shape;
88 double scale;
89
90 if (x >= 2 * bandWidth) {
91 shape = x / bandWidth;
92 } else {
93 shape = .25 * Math.pow(x / bandWidth, 2) + 1;
94 }
95 scale = bandWidth;
96 double pdf = 0;
97 for (int i = 0; i < N; i++) {
98 pdf +=
99 Math.pow(sample[i], shape - 1) * Math.exp(-sample[i] / scale) / (Math.pow(scale, shape) * gamma(shape));
100 }
101 return pdf / N;
102 }
103
104 private double gamma(double value) {
105 return cern.jet.stat.Gamma.gamma(value);
106
107 }
108
109 public static void main(String[] args) {
110
111 long start = System.currentTimeMillis();
112
113 Random random = new Random(1234);
114
115 Double[] samples = new Double[10000000];
116 for (int i = 0; i < samples.length; i++) {
117 samples[i] = random.nextDouble();
118 }
119 GammaKDEDistribution nKDE = new GammaKDEDistribution(samples);
120
121 for (int i = 0; i < 100; i++) {
122 nKDE.evaluateKernel(random.nextDouble());
123 }
124
125 long end = System.currentTimeMillis();
126
127 System.out.println("Time: " + (end-start));
128
129 }
130
131 // private double sampleMean() {return DiscreteStatistics.mean(sample);}
132
133
134 // public static void main(String[] args) {
135 // String fileName = "/Users/jen/School/Programs/BEAST/kdeTest/simulUExp.txt";
136 // //String fileName = "/Users/jen/School/Programs/BEAST/kdeTest/simulUNorm.txt";
137 // double[] values = null; //vector of the training set
138 // try{
139 // BufferedReader br = new BufferedReader(new FileReader(fileName));
140 // StringBuilder AllDataSb = new StringBuilder();
141 // String s;
142 // String myLineSeparator = "\r";
143 // while (( s = br.readLine()) != null){
144 // AllDataSb.append(s);
145 // AllDataSb.append(myLineSeparator);
146 // }
147 // //convert from stringbuilder to string
148 // String AllDataS = AllDataSb.toString();
149 // String[] result = AllDataS.split("\t|\r|,");
150 //
151 // //convert string to double
152 // values = new double[result.length];
153 // for (int i = 0; i < result.length; i++) {values[i] = Double.parseDouble(result[i]);}
154 //
155 // } //close try
156 // catch(Exception e){
157 // System.out.println(e.getMessage());
158 // System.exit(-1);
159 // }
160 //
161 //
162 // GammaKDEDistribution kde;
163 //
164 //
165 // //expecting 2.02177 -> .073; .4046729 -> 1.0257; 0.1502078 -> 1.4021
166 // //normal-reference bandwidth 0.1939
167 // kde = new GammaKDEDistribution(values, null);
168 // System.err.println("prediction: at 2.02: "+kde.pdf(2.02177)+" at 0.405: "+kde.pdf(0.4046729)+" at 0.15: "+kde.pdf(0.1502078));
169 // System.err.println("sm: "+kde.sampleMean());
170 // }
171 }