SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
ListOfTallies.java
1/*
2 * Class: ListOfTallies
3 * Description: List of statistical collectors.
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 Éric Buist
9 * @since 2007
10 *
11 *
12 * Licensed under the Apache License, Version 2.0 (the "License");
13 * you may not use this file except in compliance with the License.
14 * You may obtain a copy of the License at
15 *
16 * http://www.apache.org/licenses/LICENSE-2.0
17 *
18 * Unless required by applicable law or agreed to in writing, software
19 * distributed under the License is distributed on an "AS IS" BASIS,
20 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 * See the License for the specific language governing permissions and
22 * limitations under the License.
23 *
24 */
25package umontreal.ssj.stat.list;
26
27import cern.colt.matrix.DoubleMatrix2D;
28import umontreal.ssj.stat.Tally;
29import umontreal.ssj.stat.TallyStore;
30
40public class ListOfTallies<E extends Tally> extends ListOfStatProbes<E> {
41
45 public ListOfTallies() {
46 super();
47 }
48
54 public ListOfTallies(String name) {
55 super(name);
56 }
57
65 public static ListOfTallies<Tally> createWithTally(int size) {
67 for (int i = 0; i < size; i++)
68 list.add(new Tally());
69 return list;
70 }
71
81 for (int i = 0; i < size; i++)
82 list.add(new TallyStore());
83 return list;
84 }
85
94
95 public static ListOfTallies<TallyStore> createWithTallyStore(int size, int t) {
97 for (int i = 0; i < t; i++)
98 list.add(new TallyStore(size));
99 return list;
100 }
101
115 public void add(double[] x) {
116 int l = size();
117 if (x.length != l)
118 throw new IllegalArgumentException("Incompatible array length: given " + x.length + ", required " + l);
119 if (collect)
120 for (int i = 0; i < l; i++) {
121 double v = x[i];
122 Tally ta = get(i);
123 if (!Double.isNaN(v) && ta != null)
124 ta.add(v);
125 }
127 }
128
136 public int numberObs() {
137 if (size() == 0)
138 return 0;
139 Tally t0 = get(0);
140 return t0 == null ? 0 : t0.numberObs();
141 }
142
153 public boolean areAllNumberObsEqual() {
154 final int l = size();
155 int n = numberObs();
156 for (int i = 1; i < l; i++) {
157 Tally t = get(i);
158 if (t.numberObs() != n)
159 return false;
160 }
161 return true;
162 }
163
169 public void average(double[] r) {
170 final int l = size();
171 for (int i = 0; i < l; i++) {
172 // Manual check to avoid repetitive logs when all tallies
173 // have 0 observation.
174 Tally ta = get(i);
175 double v = ta == null || ta.numberObs() == 0 ? Double.NaN : ta.average();
176 r[i] = v;
177 }
178 }
179
191 public void variance(double[] v) {
192 if (size() != v.length)
193 throw new IllegalArgumentException("Invalid length of given array");
194 for (int i = 0; i < v.length; i++) {
195 Tally tally = get(i);
196 if (tally == null || tally.numberObs() < 2)
197 v[i] = Double.NaN;
198 else
199 v[i] = tally.variance();
200 }
201 }
202
214 public void standardDeviation(double[] std) {
215 if (size() != std.length)
216 throw new IllegalArgumentException("Invalid length of given array");
217 for (int i = 0; i < std.length; i++) {
218 Tally tally = get(i);
219 if (tally == null || tally.numberObs() < 2)
220 std[i] = Double.NaN;
221 else
222 std[i] = tally.standardDeviation();
223 }
224 }
225
247 public double covariance(int i, int j) {
248 if (i == j)
249 return get(i).variance();
250
251 TallyStore tallyi = (TallyStore) get(i);
252 TallyStore tallyj = (TallyStore) get(j);
253 return tallyi.covariance(tallyj);
254 }
255
273 public double correlation(int i, int j) {
274 if (i == j)
275 return 1.0;
276 double cov = covariance(i, j);
277 Tally tallyi = get(i);
278 Tally tallyj = get(j);
279 if (tallyi == null || tallyj == null)
280 return Double.NaN;
281 return cov / Math.sqrt(tallyi.variance() * tallyj.variance());
282 }
283
296 public void covariance(DoubleMatrix2D c) {
297 int l = size();
298 if (c.rows() != l)
299 throw new IllegalArgumentException("Invalid number of rows in covariance matrix");
300 if (c.columns() != l)
301 throw new IllegalArgumentException("Invalid number of columns in covariance matrix");
302 for (int i1 = 0; i1 < l; i1++)
303 c.setQuick(i1, i1, get(i1).variance());
304 for (int i1 = 0; i1 < l - 1; i1++)
305 for (int i2 = i1 + 1; i2 < l; i2++) {
306 double cov = covariance(i1, i2);
307 c.setQuick(i1, i2, cov);
308 c.setQuick(i2, i1, cov);
309 }
310 }
311
322 public void correlation(DoubleMatrix2D c) {
323 int l = size();
324 if (c.rows() != l)
325 throw new IllegalArgumentException("Invalid number of rows in correlation matrix");
326 if (c.columns() != l)
327 throw new IllegalArgumentException("Invalid number of columns in correlation matrix");
328 for (int i1 = 0; i1 < l; i1++)
329 c.setQuick(i1, i1, 1.0);
330 for (int i1 = 0; i1 < l - 1; i1++)
331 for (int i2 = i1 + 1; i2 < l; i2++) {
332 double cor = correlation(i1, i2);
333 c.setQuick(i1, i2, cor);
334 c.setQuick(i2, i1, cor);
335 }
336 }
337
344 return (ListOfTallies<E>) super.clone();
345 }
346}
This class is a variant of Tally for which the individual observations are stored in a list implement...
double covariance(TallyStore t2)
Returns the sample covariance of the observations contained in this tally, and the other tally t2.
A subclass of StatProbe.
Definition Tally.java:47
double standardDeviation()
Returns the sample standard deviation of the observations since the last initialization.
Definition Tally.java:195
double average()
Returns the average value of the observations since the last initialization.
Definition Tally.java:155
int numberObs()
Returns the number of observations given to this probe since its last initialization.
Definition Tally.java:143
double variance()
Returns the sample variance of the observations since the last initialization.
Definition Tally.java:174
void add(double x)
Gives a new observation x to the statistical collector.
Definition Tally.java:109
ListOfTallies< E > clone()
Clones this object.
double correlation(int i, int j)
Returns the empirical correlation between the observations in tallies with indices i and j.
void correlation(DoubleMatrix2D c)
Similar to covariance(DoubleMatrix2D) for computing the sample correlation matrix.
boolean areAllNumberObsEqual()
Tests that every tally in this list contains the same number of observations.
static ListOfTallies< Tally > createWithTally(int size)
This factory method constructs and returns a list of tallies with size instances of umontreal....
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.
void covariance(DoubleMatrix2D c)
Constructs and returns the sample covariance matrix for the tallies in this list.
void standardDeviation(double[] std)
For each tally in this list, computes the sample standard deviation, and stores the standard deviatio...
int numberObs()
Assuming that each tally in this list contains the same number of observations, returns the number of...
static ListOfTallies< TallyStore > createWithTallyStore(int size)
This factory method constructs and returns a list of tallies with size instances of umontreal....
ListOfTallies()
Constructs a new empty list of tallies.
static ListOfTallies< TallyStore > createWithTallyStore(int size, int t)
This factory method constructs and returns a list of tallies with size instances of umontreal....
void add(double[] x)
Adds the observation x[i] in tally i of this list, for i = 0,…, size() - 1.
double covariance(int i, int j)
Returns the empirical covariance of the observations in tallies with indices i and j.
ListOfTallies(String name)
Constructs a new empty list of tallies with name name.
Provides support for lists of statistical probes.