25package umontreal.ssj.stat;
27import umontreal.ssj.util.PrintfFormat;
28import umontreal.ssj.probdist.StudentDist;
29import umontreal.ssj.probdist.NormalDist;
30import umontreal.ssj.probdist.ChiSquareDist;
31import java.util.logging.Level;
32import java.util.logging.Logger;
50 private double curAverage;
51 private double curSum2;
53 private Logger log = Logger.getLogger(
"umontreal.ssj.stat");
56 CI_NONE, CI_NORMAL, CI_STUDENT
60 protected double level = 0.95;
61 protected int digits = 3;
92 maxValue = Double.NEGATIVE_INFINITY;
93 minValue = Double.POSITIVE_INFINITY;
109 public void add(
double x) {
118 double y = x - curAverage;
119 curAverage += y / numObs;
120 curSum2 += y * (x - curAverage);
131 public void add(
double[] x,
int number) {
133 for (
int i = 0; i < number; i++)
149 return numObs * curAverage;
160 log.logp(Level.WARNING,
"Tally",
"average",
161 "Tally " + name +
": calling average() with " + numObs +
" observation");
181 log.logp(Level.WARNING,
"Tally",
"variance",
182 "Tally " + name +
": calling variance() with " + numObs +
" observation");
185 return curSum2 / (numObs - 1);
225 throw new RuntimeException(
"Tally " + name +
": Calling confidenceIntervalStudent with < 2 Observations");
226 centerAndRadius[0] =
average();
228 centerAndRadius[1] = z * Math.sqrt(
variance() / (
double) numObs);
258 throw new RuntimeException(
"Tally " + name +
": Calling confidenceIntervalStudent with < 2 Observations");
259 centerAndRadius[0] =
average();
261 centerAndRadius[1] = t * Math.sqrt(
variance() / (
double) numObs);
278 double ci[] =
new double[2];
280 str.
append(
" " + (100 * level) +
"%");
281 str.
append(
" conf. interval for the mean (normal approx.): (");
312 double ci[] =
new double[2];
314 str.
append(
" " + (100 * level) +
"%");
315 str.
append(
" conf. interval for the mean (Student approx.): (");
360 throw new RuntimeException(
361 "Tally " + name +
": calling confidenceIntervalVarianceChi2 with < 2 observations");
362 double w = (numObs - 1) *
variance();
365 interval[0] = w / x2;
366 interval[1] = w / x1;
383 double ci[] =
new double[2];
385 str.
append(
" " + (100 * level) +
"%");
386 str.
append(
" conf. interval for the variance (chi2 approx.): (");
398 return report(level, digits);
410 public String
report(
double level,
int d) {
412 str.
append(
"REPORT on Tally stat. collector ==> " + name);
416 str.
append(7 + d, (
int) numObs);
418 str.
append(9 + d, d, d - 1, (
double) minValue);
420 str.
append(9 + d, d, d - 1, (
double) maxValue);
429 switch (confidenceInterval) {
451 pf.
append(-8,
" std. dev.");
452 if (confidenceInterval !=
CIType.CI_NONE)
480 if (confidenceInterval !=
CIType.CI_NONE) {
481 double[] ci =
new double[2];
482 switch (confidenceInterval) {
511 CIType oldCIType = confidenceInterval;
514 confidenceInterval =
CIType.CI_STUDENT;
517 confidenceInterval = oldCIType;
553 throw new IllegalArgumentException(
"level < 0");
555 throw new IllegalArgumentException(
"level >= 1");
565 confidenceInterval =
CIType.CI_NONE;
574 confidenceInterval =
CIType.CI_NORMAL;
583 confidenceInterval =
CIType.CI_STUDENT;
593 showNobs = showNumObs;
601 return (
Tally) super.clone();
602 }
catch (CloneNotSupportedException e) {
603 throw new IllegalStateException(
"This Tally cannot be cloned");
Extends the class ContinuousDistribution for the chi-square distribution with degrees of freedom,...
double inverseF(double u)
Returns the inverse distribution function .
Extends the class ContinuousDistribution for the normal distribution (e.g., tjoh95a (page 80)).
static double inverseF01(double u)
Same as inverseF(0, 1, u).
Extends the class ContinuousDistribution for the Student.
double inverseF(double u)
Returns the inverse distribution function .
The objects of this class are statistical probes or collectors, which are elementary devices for coll...
void notifyListeners(double x)
Notifies the observation x to all registered observers if broadcasting is ON.
double min()
Returns the smallest value taken by the variable since the last initialization of this probe.
double max()
Returns the largest value taken by the variable since the last initialization of this probe.
double standardDeviation()
Returns the sample standard deviation of the observations since the last initialization.
String reportAndCIStudent(double level, int d)
Returns a formatted string that contains a report on this probe (as in report ), followed by a confid...
String formatCIStudent(double level)
Equivalent to formatCIStudent (level, 3).
String formatCINormal(double level, int d)
Similar to confidenceIntervalNormal.
double sum()
Returns the sum cumulated so far for this probe.
double average()
Returns the average value of the observations since the last initialization.
void setName(String name)
Set the name of this Tally to name.
int numberObs()
Returns the number of observations given to this probe since its last initialization.
void setConfidenceIntervalStudent()
Indicates that a confidence interval on the true mean, based on the normality assumption,...
String formatCINormal(double level)
Equivalent to formatCINormal (level, 3).
double variance()
Returns the sample variance of the observations since the last initialization.
void init()
Initializes the statistical collector.
String formatCIVarianceChi2(double level, int d)
Similar to confidenceIntervalVarianceChi2.
String reportAndCIStudent(double level)
Same as reportAndCIStudent(level, 3).
String shortReport()
Formats and returns a short statistical report for this tally.
void confidenceIntervalStudent(double level, double[] centerAndRadius)
Computes a confidence interval on the mean.
void setShowNumberObs(boolean showNumObs)
Determines if the number of observations must be displayed in reports.
void setConfidenceLevel(double level)
Sets the level of confidence for the intervals on the mean displayed in reports.
void add(double[] x, int number)
Adds the first number observations from the array x to this probe.
Tally()
Constructs a new unnamed Tally statistical probe.
void setConfidenceIntervalNormal()
Indicates that a confidence interval on the true mean, based on the central limit theorem,...
Tally(String name)
Constructs a new Tally statistical probe with name name.
String report(double level, int d)
Returns a formatted string that contains a report on this probe with a confidence interval level leve...
void setConfidenceIntervalNone()
Indicates that no confidence interval needs to be printed in reports formatted by report,...
void confidenceIntervalVarianceChi2(double level, double[] interval)
Computes a confidence interval on the variance.
void confidenceIntervalNormal(double level, double[] centerAndRadius)
Computes a confidence interval on the mean.
double getConfidenceLevel()
Returns the level of confidence for the intervals on the mean displayed in reports.
Tally clone()
Clones this object.
void add(double x)
Gives a new observation x to the statistical collector.
String shortReportHeader()
Returns a string containing the name of the values returned in the report strings.
String formatCIStudent(double level, int d)
Similar to confidenceIntervalStudent.
String report()
Returns a formatted string that contains a report on this probe.