SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
DEKernelDensity.java
1package umontreal.ssj.stat.density;
2
3import umontreal.ssj.probdist.ContinuousDistribution;
4
27
28public class DEKernelDensity extends DensityEstimator {
29
33 private ContinuousDistribution kernel;
34
38 private double h;
39
45 private double eps = 1.0E-10;
46
52 public DEKernelDensity(double[] data) {
53 this.data = data;
54 }
55
62 setKernel(kernel);
63 }
64
72 public DEKernelDensity(ContinuousDistribution kernel, double[] data) {
73 setKernel(kernel);
74 this.data = data;
75 }
76
82 public DEKernelDensity(double h) {
83 setH(h);
84 }
85
92 public DEKernelDensity(double h, double[] data) {
93 this(h);
94 this.data = data;
95 }
96
103 public DEKernelDensity(ContinuousDistribution kernel, double h) {
104 this(kernel);
105 setH(h);
106 }
107
116 public DEKernelDensity(ContinuousDistribution kernel, double h, double[] data) {
117 this(kernel, data);
118 setH(h);
119 }
120
126 public void setData(double[] data) {
127 this.data = data;
128 }
129
135 public void setH(double h) {
136 this.h = h;
137 }
138
144 public double getH() {
145 return h;
146 }
147
154 return kernel;
155 }
156
162 public void setKernel(ContinuousDistribution kernel) {
163 this.kernel = kernel;
164 }
165
174 public double getEps() {
175 return eps;
176 }
177
186 public void setEps(double eps) {
187 this.eps = eps;
188 }
189
199 public double evalDensity(double x) {
200 double density;
201 int n = data.length;
202 double invh = 1.0 / h;
203 double invhn = invh / (double) n;
204 double sum = 0.0;
205 double term; // A term to be added to the sum that defines the density
206 // estimate.
207 int imin = 0; // We know that the terms for i < imin do not contribute
208 // significantly.
209
210 term = kernel.density((x - data[imin]) * invh);
211 while ((term < eps) && (imin < n - 1) && (data[imin] < x))
212 term = kernel.density((x - data[++imin]) * invh);
213 sum = term; // The first significant term.
214 for (int i = imin + 1; (i < n) && ((term > eps) || (data[i] < x)); i++) // Data
215 // indexed
216 // by i.
217 sum += (term = kernel.density((x - data[i]) * invh));
218 density = sum * invhn;
219 return density;
220 }
221
238 @Override
239 public double[] evalDensity(double[] evalPoints) {
240 int k = evalPoints.length;
241 double[] density = new double[k];
242 int n = data.length;
243 double invh = 1.0 / h;
244 double invhn = invh / (double) n;
245 double y;
246 double sum = 0.0;
247 double term; // A term to be added to the sum that defines the density
248 // estimate.
249 int imin = 0; // We know that the terms for i < imin do not contribute
250 // significantly.
251 for (int j = 0; j < k; j++) { // Evaluation points are indexed by j.
252 y = evalPoints[j];
253 term = kernel.density((y - data[imin]) * invh);
254 while ((term < eps) && (imin < n - 1) && (data[imin] < y))
255 term = kernel.density((y - data[++imin]) * invh);
256 sum = term; // The first significant term.
257 for (int i = imin + 1; (i < n) && ((term > eps) || (data[i] < y)); i++)
258 // Data indexed by i.
259 sum += (term = kernel.density((y - data[i]) * invh));
260 density[j] = sum * invhn;
261 }
262
263 return density;
264 }
265
269 @Override
270 public String toString() {
271 String str = "KDE [h = " + h + ", Kernel: " + kernel.toString() + "]";
272 return str;
273 }
274
275 // STATIC METHODS:
276
292 public static double evalDensity(double x, ContinuousDistribution kernel, double h, double[] data, double eps) {
293 double density;
294 int n = data.length;
295 double invh = 1.0 / h;
296 double invhn = invh / (double) n;
297 double sum = 0.0;
298 double term; // A term to be added to the sum that defines the density
299 // estimate.
300 int imin = 0; // We know that the terms for i < imin do not contribute
301 // significantly.
302
303 term = kernel.density((x - data[imin]) * invh);
304 while ((term < eps) && (imin < n - 1) && (data[imin] < x))
305 term = kernel.density((x - data[++imin]) * invh);
306 // System.out.println(imin);
307 // pmin=imin;
308 sum = term; // The first significant term.
309 for (int i = imin + 1; (i < n) && ((term > eps) || (data[i] < x)); i++) // Data
310 // indexed
311 // by i.
312 sum += (term = kernel.density((x - data[i]) * invh));
313 density = sum * invhn;
314 // System.out.println(density);
315 return density;
316 }
317
339 public static double[] evalDensity(double[] evalPoints, ContinuousDistribution kernel, double h, double[] data,
340 double eps) {
341 int k = evalPoints.length;
342 double[] density = new double[k];
343 int n = data.length;
344 double invh = 1.0 / h;
345 double invhn = invh / (double) n;
346 double y;
347 double sum = 0.0;
348 double term; // A term to be added to the sum that defines the density
349 // estimate.
350 int imin = 0; // We know that the terms for i < imin do not contribute
351 // significantly.
352 for (int j = 0; j < k; j++) { // Evaluation points are indexed by j.
353 y = evalPoints[j];
354 term = kernel.density((y - data[imin]) * invh);
355 while ((term < eps) && (imin < n - 1) && (data[imin] < y))
356 term = kernel.density((y - data[++imin]) * invh);
357 sum = term; // The first significant term.
358 for (int i = imin + 1; (i < n) && ((term > eps) || (data[i] < y)); i++)
359 // Data indexed by i.
360 sum += (term = kernel.density((y - data[i]) * invh));
361 density[j] = sum * invhn;
362 }
363
364 return density;
365 }
366
394 public static double[][] evalDensity(double[] evalPoints, ContinuousDistribution kernel, double h, double[][] data,
395 double eps) {
396 int m = data.length;
397 int k = evalPoints.length;
398 double[][] density = new double[m][k];
399 for (int rep = 0; rep < m; rep++)
400 density[rep] = evalDensity(evalPoints, kernel, h, data[rep], eps);
401 return density;
402
403 }
404
405}
Classes implementing continuous distributions should inherit from this base class.
static double[][] evalDensity(double[] evalPoints, ContinuousDistribution kernel, double h, double[][] data, double eps)
Assume that we have independent realizations of the underlying model.
void setKernel(ContinuousDistribution kernel)
Sets the kernel density function to kernel.
DEKernelDensity(ContinuousDistribution kernel)
Constructs a KDE with the kernel function kernel.
DEKernelDensity(double h)
Constructs a KDE with bandwidth .
DEKernelDensity(ContinuousDistribution kernel, double h)
Constructs a KDE with the kernel function kernel and bandwidth h.
double getEps()
Gives the threshold-level for the evaluation of the density.
DEKernelDensity(ContinuousDistribution kernel, double h, double[] data)
Constructs a KDE with the kernel function kernel and bandwidth h from the observations data.
void setH(double h)
Sets the bandwidth to h.
static double evalDensity(double x, ContinuousDistribution kernel, double h, double[] data, double eps)
Evaluates the KDE with kernel density functionkernel, bandwidth h which is defined by the observation...
void setEps(double eps)
Sets the threshold-level for the evaluation of the density to eps.
DEKernelDensity(double[] data)
Constructs a KDE from the observations data.
String toString()
Gives a short description of the estimator.a short description.
void setData(double[] data)
Sets a new set of observations.
double evalDensity(double x)
Evaluates the KDE at the evaluation point x.
double[] evalDensity(double[] evalPoints)
Evaluates the KDE at each of the evaluation points evalPoints and returns the results in an array.
DEKernelDensity(double h, double[] data)
Constructs a KDE with bandwidth h from the observations data.
DEKernelDensity(ContinuousDistribution kernel, double[] data)
Constructs a KDE with the kernel function kernel from the observations in data.
static double[] evalDensity(double[] evalPoints, ContinuousDistribution kernel, double h, double[] data, double eps)
Evaluates the KDE with kernel density functionkernel, bandwidth h which is defined by the observation...
ContinuousDistribution getKernel()
Gives the kernel density function .
This abstract class represents a univariate density estimator (DE).
double[] data
The data associated with this DensityEstimator object, if any.
Tools for univariate density estimation.