SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
DensityEstimator.java
1
2package umontreal.ssj.stat.density;
3
4import java.util.ArrayList;
5import umontreal.ssj.probdist.ContinuousDistribution;
6import umontreal.ssj.stat.PgfDataTable;
7
61
62public abstract class DensityEstimator {
63
67 protected double[] data;
68
75 public abstract void setData(double[] data);
76
82 public double[] getData() {
83 return data;
84 }
85
93
94 public abstract double evalDensity(double x);
95
105 public double[] evalDensity(double[] evalPoints) {
106 int k = evalPoints.length;
107 double[] dens = new double[k];
108 for (int j = 0; j < k; j++)
109 dens[j] = evalDensity(evalPoints[j]);
110 return dens;
111 }
112
122 public double[] evalDensity(double[] evalPoints, double[] data) {
123 setData(data);
124 return evalDensity(evalPoints);
125 }
126
154 public double[][] evalDensity(double[] evalPoints, double[][] data) {
155 int m = data.length;
156 double[][] density = new double[m][];
157 for (int r = 0; r < m; r++)
158 density[r] = evalDensity(evalPoints, data[r]);
159
160 return density;
161 }
162
187 public static void evalDensity(ArrayList<DensityEstimator> listDE, double[] evalPoints, double[][] data,
188 ArrayList<double[][]> listDensity) {
189 for (DensityEstimator de : listDE)
190 listDensity.add(de.evalDensity(evalPoints, data));
191 }
192
207 public static double[] computeVariance(double[][] density) {
208 int m = density.length; // number of indep. replications
209 int k = density[0].length; // number of evaluation points
210
211 double x, y;
212
213 double meanDens[] = new double[k]; // Average value over the rep replicates
214 double varDens[] = new double[k]; // Variance at each evaluation point
215 // Arrays.fill(meanDens, 0.0);
216 // Arrays.fill(varDens, 0.0);
217 for (int r = 0; r < m; r++) {
218 // Update the empirical mean and variance at each evaluation point.
219 for (int j = 0; j < k; j++) {
220 x = density[r][j];
221 y = x - meanDens[j];
222 meanDens[j] += y / (double) (r + 1);
223 varDens[j] += y * (x - meanDens[j]);
224 }
225 }
226
227 for (int j = 0; j < k; j++) // normalize
228 varDens[j] /= (double) (m - 1);
229
230 return varDens;
231 }
232
272 public static double computeIV(double[][] density, double a, double b, double[] variance) {
273 variance = computeVariance(density);
274 int k = density[0].length;
275 double iv = 0.0;
276 for (double var : variance)
277 iv += var;
278 return iv * (b - a) / (double) k;
279 }
280
306 public static void computeIV(ArrayList<double[][]> listDensity, double a, double b, ArrayList<Double> listIV) {
307
308 // ArrayList<Double> returnList = new ArrayList<Double>();
309 int k = (listDensity.get(0))[0].length;
310 double[] variance = new double[k];
311 for (double[][] density : listDensity) {
312 // returnList.add(computeIV(density, a, b, variance));
313 listIV.add(computeIV(density, a, b, variance));
314 }
315 }
316
365 public static double[] computeMISE(ContinuousDistribution dist, double[] evalPoints, double[][] density, double a,
366 double b, double[] variance, double[] sqBias, double[] mse) {
367 int m = density.length;
368 int k = evalPoints.length;
369
370 double x, y, z;
371 double trueDensity;
372
373 double meanDens[] = new double[k]; // Average value over m
374 // replicates
375
376 for (int r = 0; r < m; r++) {
377 // Update the empirical mean, sum of squares, and mse of
378 // observations at each evaluation point.
379 for (int j = 0; j < k; j++) {
380 x = density[r][j];
381 y = x - meanDens[j];
382 trueDensity = dist.density(evalPoints[j]);
383 z = x - trueDensity;
384
385 meanDens[j] += y / (double) (r + 1);
386 variance[j] += y * (x - meanDens[j]);
387 mse[j] += z * z;
388 }
389
390 }
391
392 double iv = 0.0;
393 double mise = 0.0;
394 for (int j = 0; j < k; j++) {
395 variance[j] /= (double) (m - 1.0);
396 mse[j] /= (double) m;
397 sqBias[j] = mse[j] - variance[j];
398
399 iv += variance[j];
400 mise += mse[j];
401 }
402
403 double fact = (b - a) / (double) k;
404 iv *= fact;
405 mise *= (b - a) / ((double) k);
406
407 double[] res = { iv, mise - iv, mise };
408 return res;
409
410 }
411
444
445 public static void computeMISE(ContinuousDistribution dist, double[] evalPoints, ArrayList<double[][]> listDensity,
446 double a, double b, ArrayList<double[]> listMISE) {
447 // ArrayList<double[]> returnList = new ArrayList<double[]>();
448 int k = evalPoints.length;
449 // double[] variance = new double[k];
450 // double[] sqBias = new double[k];
451 // double[] mse = new double[k];
452 double[] tmp = new double[k];
453 for (double[][] density : listDensity) {
454 // returnList.add(computeMISE(dist, evalPoints, density, a, b, tmp, tmp, tmp));
455 listMISE.add(computeMISE(dist, evalPoints, density, a, b, tmp, tmp, tmp));
456 }
457 // return returnList;
458 }
459
465 public abstract String toString();
466
488 public static String plotDensity(double[] evalPoints, double[] density, String plotTitle, String[] axisTitles) {
489 double[][] plotData = new double[evalPoints.length][];
490 for (int i = 0; i < evalPoints.length; i++) {
491 plotData[i] = new double[2];
492 plotData[i][0] = evalPoints[i];
493 plotData[i][1] = density[i];
494 }
495 PgfDataTable table = new PgfDataTable(plotTitle, "", axisTitles, plotData);
496 StringBuffer sb = new StringBuffer("");
497 sb.append(PgfDataTable.pgfplotFileHeader());
498 sb.append(table.drawPgfPlotSingleCurve(plotTitle, "axis", 0, 1, 2, "", ""));
499 sb.append(PgfDataTable.pgfplotEndDocument());
500 return sb.toString();
501 }
502
517 public static double roughnessFunctional(double[] density, double a, double b) {
518 double fac = (b - a) / (double) density.length;
519 double sum = 0.0;
520 for (double d : density) {
521 sum += d * d;
522 }
523 return fac * sum;
524 }
525
526}
Classes implementing continuous distributions should inherit from this base class.
abstract double density(double x)
Returns , the density evaluated at .
Represents a data table which has a name, a number of observations (rows), a number of fields (column...
String drawPgfPlotSingleCurve(String title, String axistype, String xaxis, String yaxis, int logbasis, String axisoptions, String plotoptions)
Returns a String that contains a complete tikzpicture for the pgfplot package, showing the field j2 a...
This abstract class represents a univariate density estimator (DE).
static double computeIV(double[][] density, double a, double b, double[] variance)
This method estimates the empirical IV over the interval .
static double[] computeVariance(double[][] density)
This method computes the empirical variance based on the values given in data.
static void evalDensity(ArrayList< DensityEstimator > listDE, double[] evalPoints, double[][] data, ArrayList< double[][]> listDensity)
This function is particularly designed for experiments with many different types of density estimator...
double[] data
The data associated with this DensityEstimator object, if any.
abstract void setData(double[] data)
Sets the observations for the density estimator do data.
static double[] computeMISE(ContinuousDistribution dist, double[] evalPoints, double[][] density, double a, double b, double[] variance, double[] sqBias, double[] mse)
In situations where the true density is known this method can estimate the empirical MISE over the in...
static double roughnessFunctional(double[] density, double a, double b)
Estimates the roughness functional.
abstract String toString()
Gives a short description of the estimator.
static void computeMISE(ContinuousDistribution dist, double[] evalPoints, ArrayList< double[][]> listDensity, double a, double b, ArrayList< double[]> listMISE)
This method estimates the empirical MISE over the interval for a collection of different estimators.
double[][] evalDensity(double[] evalPoints, double[][] data)
This method is particularly designed to evaluate the density estimator in such a way that the result ...
static String plotDensity(double[] evalPoints, double[] density, String plotTitle, String[] axisTitles)
Gives a plot of the estimated density.
double[] getData()
Gives the observations for this density estimator, if any.
abstract double evalDensity(double x)
Evaluates the density estimator at x.
double[] evalDensity(double[] evalPoints)
Evaluates the density estimator at the points in evalPoints.
double[] evalDensity(double[] evalPoints, double[] data)
Sets the observations for the density estimator to data and evaluates the density at each point in ev...
static void computeIV(ArrayList< double[][]> listDensity, double a, double b, ArrayList< Double > listIV)
This method estimates the empirical IV over the interval for a collection of different estimators.
Tools for univariate density estimation.