SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
KernelDensity.java
1/*
2 * Class: KernelDensity
3 * Description: Kernel density estimators
4 * Environment: Java
5 * Software: SSJ
6 * Copyright (C) 2001 Pierre L'Ecuyer and Universite de Montreal
7 * Organization: DIRO, Universite de Montreal
8 * @author
9 * @since
10 *
11 *
12 * Licensed under the Apache License, Version 2.0 (the "License");
13 * you may not use this file except in compliance with the License.
14 * You may obtain a copy of the License at
15 *
16 * http://www.apache.org/licenses/LICENSE-2.0
17 *
18 * Unless required by applicable law or agreed to in writing, software
19 * distributed under the License is distributed on an "AS IS" BASIS,
20 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 * See the License for the specific language governing permissions and
22 * limitations under the License.
23 *
24 */
25package umontreal.ssj.gof;
26
27import umontreal.ssj.probdist.*;
28// import umontreal.ssj.randvar.KernelDensityGen;
29
42public class KernelDensity {
43
49 private static double estimate(EmpiricalDist dist, ContinuousDistribution kern, double h, double y) {
50 // Computes and returns the kernel density estimate at $y$, where the
51 // kernel is the density kern.density(x), and the bandwidth is $h$.
52 double z;
53 double a = kern.getXinf(); // lower limit of density
54 double b = kern.getXsup(); // upper limit of density
55 double sum = 0;
56 int n = dist.getN();
57 for (int i = 0; i < n; i++) {
58 z = (y - dist.getObs(i)) / h;
59 if ((z >= a) && (z <= b))
60 sum += kern.density(z);
61 }
62 sum /= (h * n);
63 return sum;
64 }
65
79 public static double[] computeDensity(EmpiricalDist dist, ContinuousDistribution kern, double h, double[] Y) {
80 int m = Y.length;
81 double[] u = new double[m];
82 for (int j = 0; j < m; j++)
83 u[j] = estimate(dist, kern, h, Y[j]);
84 return u;
85 }
86
97 public void evalDensity(double data[], ContinuousDistribution kern, double h, double[] evalPoints, double[] density,
98 double epsilon0) {
99 int m = evalPoints.length;
100 // density = new double[m]; // Maybe not needed, but perhaps safer.
101 int n = data.length;
102 double invhn = 1.0 / (h * n);
103 double invh = 1.0 / h;
104 double y;
105 double sum = 0.0;
106 double term; // A term to be added to the sum that defines the density estimate.
107 int imin = 0; // We know that the terms for i < imin do not contribute significantly.
108 for (int j = 0; j < m; j++) { // Evaluation points are indexed by j.
109 y = evalPoints[j];
110 term = kern.density((y - data[imin]) * invh);
111 while ((term < epsilon0) & (imin < n - 1) && (data[imin] < y))
112 term = kern.density((y - data[++imin]) * invh);
113 sum = term; // The first significant term.
114 for (int i = imin + 1; (i < n) && ((term > epsilon0) || (data[i] < y)); i++)
115 // Data indexed by i.
116 sum += (term = kern.density((y - data[i]) * invh));
117 density[j] = sum * invhn;
118 }
119 }
120
121}
This static class provides methods to compute a kernel density estimator from a set of individual ob...
static double[] computeDensity(EmpiricalDist dist, ContinuousDistribution kern, double h, double[] Y)
Given the empirical distribution dist, this method computes the kernel density estimate at each of th...
void evalDensity(double data[], ContinuousDistribution kern, double h, double[] evalPoints, double[] density, double epsilon0)
Similar to , but much more efficient for very large n.
Classes implementing continuous distributions should inherit from this base class.
double getXinf()
Returns such that the probability density is 0 everywhere outside the interval .
double getXsup()
Returns such that the probability density is 0 everywhere outside the interval .
abstract double density(double x)
Returns , the density evaluated at .
Extends DiscreteDistribution to an empirical distribution function, based on the observations (sorte...
double getObs(int i)
Returns the value of , for .
int getN()
Returns , the number of observations.