SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
FunctionOfMultipleMeansTally.java
1/*
2 * Class: FunctionOfMultipleMeansTally
3 * Description: statistical collector for a function of multiple means
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 * SSJ is free software: you can redistribute it and/or modify it under
12 * the terms of the GNU General Public License (GPL) as published by the
13 * Free Software Foundation, either version 3 of the License, or
14 * any later version.
15
16 * SSJ is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20
21 * A copy of the GNU General Public License is available at
22 <a href="http://www.gnu.org/licenses">GPL licence site</a>.
23 */
24package umontreal.ssj.stat;
25
26import umontreal.ssj.util.MultivariateFunction;
27import umontreal.ssj.stat.list.ListOfTalliesWithCovariance;
28import umontreal.ssj.util.PrintfFormat;
29import umontreal.ssj.probdist.NormalDist;
30import java.util.logging.Level;
31import java.util.logging.Logger;
32
68public class FunctionOfMultipleMeansTally extends StatProbe implements Cloneable {
70 protected MultivariateFunction func;
71 private double[] temp;
72 private double[] delta;
73
74 private Logger log = Logger.getLogger("umontreal.ssj.stat");
75
76 private static enum CIType {
77 CI_NONE, CI_DELTA
78 };
79
80 protected CIType confidenceInterval = CIType.CI_NONE;
81 protected double level = 0.95;
82
93 ta.setUnmodifiable();
94 this.func = func;
95 internalInit();
96 }
97
108 public FunctionOfMultipleMeansTally(MultivariateFunction func, String name, int d) {
110 ta.setName(name);
111 ta.setUnmodifiable();
112 this.func = func;
113 this.name = name;
114 internalInit();
115 }
116
127 if (ta == null)
128 throw new NullPointerException("The list of tallies cannot be null");
129 this.ta = ta;
130 this.func = func;
131 ta.setUnmodifiable();
132 name = ta.getName();
133 internalInit();
134 }
135
136 public void setName(String name) {
137 super.setName(name);
138 ta.setName(name);
139 }
140
153
160 return func;
161 }
162
169 public int getDimension() {
170 return ta.size();
171 }
172
192 public void add(double... x) {
193 if (x.length != ta.size())
194 throw new IllegalArgumentException("Incompatible length of vectors of observations: given length is "
195 + x.length + ", but required length is " + ta.size());
196 if (collect)
197 ta.add(x);
198 }
199
206 public int numberObs() {
207 return ta.numberObs();
208 }
209
218 public double average() {
219 if (temp.length != ta.size())
220 // The dimension of the internal tally could (rarely) change.
221 temp = new double[ta.size()];
222 ta.average(temp);
223 return func.evaluate(temp);
224 }
225
267 public double variance() {
268 if (ta.numberObs() < 2) {
269 // System.out.println
270 // ("******* FunctionOfMultipleMeansTally with name " + getName()
271 // + ": Calling variance() with "
272 // + ta.numberObs() + " observation");
273 log.logp(Level.WARNING, "FunctionOfMultipleMeansTally", "variance", "FunctionOfMultipleMeansTally " + name
274 + ": calling variance() with " + ta.numberObs() + " observation");
275 return Double.NaN;
276 }
277 if (temp.length != ta.size()) {
278 // The dimension of the internal tally could (rarely) change.
279 temp = new double[ta.size()];
280 delta = new double[ta.size()];
281 }
282 ta.average(temp);
283 for (int i = 0; i < delta.length; i++)
284 delta[i] = func.evaluateGradient(i, temp);
285 for (int i = 0; i < temp.length; i++) {
286 temp[i] = 0;
287 for (int j = 0; j < delta.length; j++)
288 temp[i] += delta[j] * ta.covariance(j, i);
289 }
290 double sigma = 0;
291 for (int i = 0; i < temp.length; i++)
292 sigma += temp[i] * delta[i];
293 // sigma could be negative but near 0, e.g., -1e-13, so round it
294 // to 0 if it is negative.
295 return sigma < 0 ? 0 : sigma;
296 }
297
303 public double standardDeviation() {
304 return Math.sqrt(variance());
305 }
306
357 public void confidenceIntervalDelta(double level, double[] centerAndRadius) {
358 // Must return an array object, cannot return 2 doubles directly.
359 centerAndRadius[0] = average();
360 double z = NormalDist.inverseF01(0.5 * (level + 1.0));
361 centerAndRadius[1] = z * Math.sqrt(variance() / (double) numberObs());
362 }
363
375 public String formatCIDelta(double level, int d) {
376 PrintfFormat str = new PrintfFormat();
377 double ci[] = new double[2];
378 confidenceIntervalDelta(level, ci);
379
380 str.append(" " + (100 * level) + "%");
381 str.append(" confidence interval for function of means: (");
382 str.append(7 + d, d, d - 1, ci[0] - ci[1]).append(',');
383 str.append(7 + d, d, d - 1, ci[0] + ci[1]).append(" )" + PrintfFormat.NEWLINE);
384 return str.toString();
385 }
386
390 public String formatCIDelta(double level) {
391 return formatCIDelta(level, 3);
392 }
393
401 public String report() {
402 PrintfFormat str = new PrintfFormat();
403 str.append("REPORT on Tally stat. collector ==> " + name);
404 str.append(PrintfFormat.NEWLINE + " func. of averages standard dev. ");
405 str.append("num. obs." + PrintfFormat.NEWLINE);
406 str.append(20, 3, 2, (double) average());
407 str.append(13, 3, 2, standardDeviation());
408 str.append(13, (int) numberObs()).append(PrintfFormat.NEWLINE);
409
410 if (confidenceInterval == CIType.CI_DELTA) {
411 str.append(formatCIDelta(level));
412 }
413
414 return str.toString();
415 }
416
426 public String reportAndCIDelta(double level, int d) {
427 CIType oldCIType = confidenceInterval;
428 double oldLevel = this.level;
429
430 try {
431 confidenceInterval = CIType.CI_DELTA;
432 this.level = level;
433 return report();
434 } finally {
435 confidenceInterval = oldCIType;
436 this.level = oldLevel;
437 }
438 }
439
447 public String reportAndCIDelta(double level) {
448 return reportAndCIDelta(level, 3);
449 }
450
451 public String shortReportHeader() {
452 PrintfFormat pf = new PrintfFormat();
453 pf.append(-20, " func. of averages").append(" ");
454 pf.append(-9, "std. dev.");
455 if (showNobs)
456 pf.append(" ").append(-5, "nobs.");
457 if (confidenceInterval != CIType.CI_NONE)
458 pf.append(" ").append(-25, "conf. int.");
459
460 return pf.toString();
461 }
462
473 public String shortReport() {
474 PrintfFormat pf = new PrintfFormat();
475 pf.append(20, 3, 2, average()).append(" ");
476 if (numberObs() >= 2)
477 pf.append(9, 3, 2, standardDeviation());
478 else
479 pf.append(9, "---");
480 if (showNobs)
481 pf.append(" ").append(5, numberObs());
482
483 if (confidenceInterval == CIType.CI_DELTA) {
484 double[] ci = new double[2];
485 confidenceIntervalDelta(level, ci);
486 pf.append(" ").append((100 * level) + "% (");
487 pf.append(9, 3, 2, ci[0] - ci[1]).append(',');
488 pf.append(9, 3, 2, ci[0] + ci[1]).append(")");
489 }
490
491 return pf.toString();
492 }
493
501 public double getConfidenceLevel() {
502 return level;
503 }
504
512 public void setConfidenceLevel(double level) {
513 if (level < 0.0)
514 throw new IllegalArgumentException("level < 0");
515 if (level >= 1.0)
516 throw new IllegalArgumentException("level >= 1");
517 this.level = level;
518 }
519
526 confidenceInterval = CIType.CI_NONE;
527 }
528
535 confidenceInterval = CIType.CI_DELTA;
536 }
537
544 public void setShowNumberObs(boolean showNumObs) {
545 showNobs = showNumObs;
546 }
547
551 public double min() {
552 return Double.NaN;
553 }
554
558 public double max() {
559 return Double.NaN;
560 }
561
565 public double sum() {
566 return Double.NaN;
567 }
568
569 private void internalInit() {
570 if ((func.getDimension() != ta.size()) && (func.getDimension() != -1))
571 throw new IllegalArgumentException("The dimension of the function must be equal to d, or equal to -1");
572 temp = new double[ta.size()];
573 delta = new double[ta.size()];
574 }
575
576 public void init() {
577 ta.init();
578 internalInit();
579 }
580
587 try {
588 mta = (FunctionOfMultipleMeansTally) super.clone();
589 } catch (CloneNotSupportedException cne) {
590 throw new IllegalStateException("CloneNotSupportedException for a class implementing Cloneable");
591 }
592
594 for (int i = 0; i < ta.size(); i++) {
595 mta.ta.add(ta.get(i).clone());
596 }
597
598 mta.temp = new double[ta.size()];
599 mta.delta = new double[ta.size()];
600 return mta;
601 }
602}
Extends the class ContinuousDistribution for the normal distribution (e.g., tjoh95a  (page 80)).
static double inverseF01(double u)
Same as inverseF(0, 1, u).
double getConfidenceLevel()
Returns the level of confidence for the intervals on the mean displayed in reports.
FunctionOfMultipleMeansTally clone()
Clones this object.
String shortReport()
Formats and returns a short statistical report for this function of multiple means tally.
void init()
Initializes the statistical collector.
FunctionOfMultipleMeansTally(MultivariateFunction func, String name, int d)
Constructs a function of multiple means tally with name name, dimension d, and function func.
String report()
Returns a string containing a formatted report on this probe.
FunctionOfMultipleMeansTally(MultivariateFunction func, int d)
Constructs a function of multiple means tally with dimension d, and function func.
int numberObs()
Returns the number of vectors of observations given to this probe since its last initialization.
void setConfidenceIntervalNone()
Indicates that no confidence interval needs to be printed in reports formatted by report,...
String reportAndCIDelta(double level, int d)
Returns a string containing a formatted report on this probe (as in report ), followed by a confidenc...
void setConfidenceIntervalDelta()
Indicates that a confidence interval on the true mean, based on the delta and central limit theorems,...
void setName(String name)
Sets the name of this statistical collector to name.
String formatCIDelta(double level)
Same as formatCIDelta(level, 3).
double average()
Computes , an estimate of the function of means .
void setConfidenceLevel(double level)
Sets the level of confidence for the intervals on the mean displayed in reports.
String shortReportHeader()
Returns a string containing the name of the values returned in the report strings.
void setShowNumberObs(boolean showNumObs)
Determines if the number of observations must be displayed in reports.
FunctionOfMultipleMeansTally(MultivariateFunction func, ListOfTalliesWithCovariance< Tally > ta)
Constructs a function of multiple means tally using the function func and the list of tallies ta for ...
MultivariateFunction getFunction()
Returns the function of multiple means used by this tally.
ListOfTalliesWithCovariance< Tally > getListOfTallies()
Returns the (unmodifiable) list of tallies internally used by this object.
String formatCIDelta(double level, int d)
Similar to confidenceIntervalDelta(double,double[]), but returns the confidence interval in a formatt...
int getDimension()
Returns the dimension of this tally, i.e., the size of any vector of observations.
String reportAndCIDelta(double level)
Same as reportAndCIDelta(level, 3).
void confidenceIntervalDelta(double level, double[] centerAndRadius)
Computes a confidence interval with confidence level level on.
double standardDeviation()
Returns the square root of variance.
double variance()
Estimates where is the number of vectors of observations given to this collector since the last ini...
The objects of this class are statistical probes or collectors, which are elementary devices for coll...
Extends ListOfTallies to add support for the computation of the sample covariance between each pair o...
static ListOfTalliesWithCovariance< Tally > createWithTally(int size)
This factory method constructs and returns a list of tallies with size instances of umontreal....
This class acts like a StringBuffer which defines new types of append methods.
String toString()
Converts the buffer into a String.
PrintfFormat append(String str)
Appends str to the buffer.
static final String NEWLINE
End-of-line symbol or line separator.
Represents a function of multiple variables.
int getDimension()
Returns , the dimension of the function computed by this implementation.