SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
AnovaVarianceCollector.java
1package umontreal.ssj.mcqmctools.anova;
2
3import umontreal.ssj.stat.list.ListOfTallies;
4import umontreal.ssj.stat.Tally;
5
6import java.util.*;
7
12public class AnovaVarianceCollector extends ListOfTallies<PartialVariance> {
13
14 protected List<AnovaObserver> observers;
15 protected Tally meanCorrection;
16 protected Tally totalVar;
17 protected boolean sorted;
18
23 public AnovaVarianceCollector(Iterable<CoordinateSet> coordSets) {
24
25 sorted = false;
26 totalVar = new Tally("Total variance");
27 meanCorrection = new Tally("Correction to the mean");
28
29 for (CoordinateSet cs : coordSets)
30 add(new PartialVariance(cs, totalVar));
31
32 observers = new ArrayList<AnovaObserver>();
33 }
34
40 public void addObserver(AnovaObserver observer) {
41 observers.add(observer);
42 }
43
48 @Override
49 public void init() {
50 super.init();
51 meanCorrection.init();
52 totalVar.init();
53 }
54
61 @Override
62 public void add(double[] vars) {
63 if (sorted)
64 throw new IllegalStateException("AnovaVarianceCollector cannot collect data once sorted");
65 for (int i = 0; i < size(); i++)
66 get(i).add(vars[i]);
67 meanCorrection.add(vars[size()]);
68 totalVar.add(vars[size() + 1]);
69
70 // notify observers
71 for (AnovaObserver obs : observers)
72 obs.anovaUpdated(this);
73 }
74
81 public void sort() {
82 Collections.sort(this, Collections.reverseOrder());
83 sorted = true;
84 }
85
91 return meanCorrection;
92 }
93
99 return totalVar;
100 }
101
106 public double getVarianceForOrder(int order) {
107 double sum = 0;
108 for (PartialVariance c : this)
109 if (c.getCoordinates().cardinality() == order)
110 sum += c.average();
111 return sum;
112 }
113
119 public double getVarianceFractionForOrder(int order) {
120 double sum = 0;
121 for (PartialVariance c : this)
122 if (c.getCoordinates().cardinality() == order)
123 sum += c.sensitivityIndex();
124 return sum;
125 }
126
133 for (PartialVariance c : this)
134 if (c.getCoordinates().equals(coords))
135 return c.average();
136 return -1;
137 }
138
143 public double getTotalVarianceForCoordinate(int coord) {
144 double sum = 0;
145 for (PartialVariance c : this)
146 if (c.getCoordinates().contains(coord))
147 sum += c.average();
148 return sum;
149 }
150
155 public int getMaxOrder() {
156 int x = 0;
157 for (PartialVariance c : this)
158 x = Math.max(x, c.getCoordinates().cardinality());
159 return x;
160 }
161
166 @Override
167 public String toString() {
168 String s = "ANOVA Collector";
169 s += String.format(" [maxOrder=%d]", getMaxOrder());
170 return s;
171 }
172
177 public String report() {
178
179 String report = "";
180
181 report += String.format("%30s: %9.4g", "Correction to mean", getMeanCorrection().average());
182 if (getMeanCorrection().numberObs() > 1) {
183 double dvar = Math.sqrt(getMeanCorrection().variance() / getMeanCorrection().numberObs());
184 report += String.format(" ± %.2g", dvar);
185 }
186
187 report += umontreal.ssj.util.PrintfFormat.NEWLINE;
188 report += String.format("%30s: %9.4g", "Total variance", getTotalVariance().average());
189 if (getTotalVariance().numberObs() > 1) {
190 double dvar = Math.sqrt(getTotalVariance().variance() / getTotalVariance().numberObs());
191 report += String.format(" ± %.2g", dvar);
192 }
193
194 report += umontreal.ssj.util.PrintfFormat.NEWLINE;
195
196 report += umontreal.ssj.util.PrintfFormat.NEWLINE;
197
198 for (PartialVariance c : this)
199 report += c + umontreal.ssj.util.PrintfFormat.NEWLINE;
200
201 for (int i = 1; i <= getMaxOrder(); i++)
202 report += umontreal.ssj.util.PrintfFormat.NEWLINE + String.format("%30s: %9.4g (%.4g %%)",
203 String.format("order %d", i), getVarianceForOrder(i), getVarianceFractionForOrder(i) * 100);
204
205 return report;
206 }
207
208}
Tally getTotalVariance()
Returns the total variance collector.
String toString()
Returns a description of the ANOVA collector.
double getVarianceForOrder(int order)
Returns the total variance for all projections of dimension order.
String report()
Returns a report of the current state of estimation of the ANOVA variances.
void addObserver(AnovaObserver observer)
Add an observer, which is notified when the ANOVA variance collector is updated.
int getMaxOrder()
Returns the maximum dimension of the projections contained in the list.
double getVarianceFractionForOrder(int order)
Returns the total variance fraction for all projections of dimension order.
double getTotalVarianceForCoordinate(int coord)
Returns the total variance for all projections involving coordinate coord.
AnovaVarianceCollector(Iterable< CoordinateSet > coordSets)
Constructs an ANOVA collector for the coordinate sets coordSets.
void add(double[] vars)
Adds an observation of ANOVA variances.
Tally getMeanCorrection()
Returns the mean correction collector.
double getTotalVarianceForCoordinate(CoordinateSet coords)
Returns the total variance for a specific projection or a negative number if the projection coords ha...
int cardinality()
Returns the cardinality of the current coordinate set.
boolean contains(int coord)
Returns true if the current set contains coordinate coord.
Represents the partial variance of a function with respect to a given coordinate set.
double sensitivityIndex()
Returns the sensitivity index (fraction of the total variance) of the coordinate set under considerat...
CoordinateSet getCoordinates()
Returns the coordinate set associated with the current partial variance.
A subclass of StatProbe.
Definition Tally.java:47
double average()
Returns the average value of the observations since the last initialization.
Definition Tally.java:155
void average(double[] r)
Computes the average for each tally in this list, and stores the averages in the array r.
void variance(double[] v)
For each tally in this list, computes the sample variance, and stores the variances into the array v.
int numberObs()
Assuming that each tally in this list contains the same number of observations, returns the number of...
ListOfTallies()
Constructs a new empty list of tallies.