SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
MatrixOfTallies.java
1/*
2 * Class: MatrixOfTallies
3 * Description: Matrix 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 2006
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.matrix;
25
26import umontreal.ssj.stat.Tally;
27import umontreal.ssj.stat.TallyStore;
28import cern.colt.matrix.DoubleMatrix2D;
29import umontreal.ssj.util.PrintfFormat;
30import cern.colt.list.DoubleArrayList;
31import cern.colt.matrix.impl.DenseDoubleMatrix2D;
32import umontreal.ssj.stat.list.ListOfTallies;
33
43public class MatrixOfTallies<E extends Tally> extends MatrixOfStatProbes<E> {
44
54 public MatrixOfTallies(int numRows, int numColumns) {
55 super(numRows, numColumns);
56 }
57
68 public MatrixOfTallies(String name, int numRows, int numColumns) {
69 super(name, numRows, numColumns);
70 }
71
81 public static MatrixOfTallies<Tally> createWithTally(int numRows, int numColumns) {
82 MatrixOfTallies<Tally> matrix = new MatrixOfTallies<Tally>(numRows, numColumns);
83 for (int r = 0; r < numRows; r++)
84 for (int c = 0; c < numColumns; c++)
85 matrix.set(r, c, new Tally());
86 return matrix;
87 }
88
98 public static MatrixOfTallies<TallyStore> createWithTallyStore(int numRows, int numColumns) {
100 for (int r = 0; r < numRows; r++)
101 for (int c = 0; c < numColumns; c++)
102 matrix.set(r, c, new TallyStore());
103 return matrix;
104 }
105
122 public void add(DoubleMatrix2D x) {
123 int rows = rows();
124 int columns = columns();
125 if (x.rows() != rows || x.columns() != columns)
126 throw new IllegalArgumentException("Incompatible matrix dimensions: given " + x.rows() + "x" + x.columns()
127 + ", required " + rows() + "x" + columns());
128 if (collect)
129 for (int r = 0; r < rows; r++)
130 for (int c = 0; c < columns; c++) {
131 double v = x.getQuick(r, c);
132 Tally ta = get(r, c);
133 if (!Double.isNaN(v) && ta != null)
134 ta.add(v);
135 }
137 }
138
142 public void add(double[][] x) {
143 int rows = rows();
144 int columns = columns();
145 if (x.length != rows)
146 throw new IllegalArgumentException("Incompatible number of rows: given " + x.length + ", required " + rows);
147 if (collect)
148 for (int r = 0; r < rows; r++) {
149 if (x[r].length != columns)
150 throw new IllegalArgumentException("Incompatible number of columns in row " + r + ": given "
151 + x[r].length + ", but required " + columns);
152 for (int c = 0; c < columns; c++) {
153 double v = x[r][c];
154 Tally ta = get(r, c);
155 if (!Double.isNaN(v) && ta != null)
156 ta.add(v);
157 }
158 }
159 notifyListeners(new DenseDoubleMatrix2D(x));
160 }
161
169 public int numberObs() {
170 if (rows() == 0 || columns() == 0)
171 return 0;
172 Tally t0 = get(0, 0);
173 return t0 == null ? 0 : t0.numberObs();
174 }
175
183 public boolean areAllNumberObsEqual() {
184 final int nr = rows();
185 final int nc = columns();
186 int n = numberObs();
187 for (int r = 0; r < nr; r++)
188 for (int c = 0; c < nc; c++) {
189 Tally t = get(r, c);
190 if (t.numberObs() != n)
191 return false;
192 }
193 return true;
194 }
195
201 public void average(DoubleMatrix2D m) {
202 for (int i = 0; i < m.rows(); i++)
203 for (int j = 0; j < m.columns(); j++) {
204 Tally ta = get(i, j);
205 double v = ta == null || ta.numberObs() == 0 ? Double.NaN : ta.average();
206 m.setQuick(i, j, v);
207 }
208 }
209
227 public void variance(DoubleMatrix2D m) {
228 if (m.rows() != rows())
229 throw new IllegalArgumentException(
230 "Invalid number of rows in the given matrix: required " + rows() + " but found " + m.rows());
231 if (m.columns() != columns())
232 throw new IllegalArgumentException(
233 "Invalid number of columns in the given matrix: required " + columns() + " but found " + m.columns());
234 for (int r = 0; r < rows(); r++)
235 for (int c = 0; c < columns(); c++) {
236 Tally tally = get(r, c);
237 m.setQuick(r, c, tally != null && tally.numberObs() >= 2 ? tally.variance() : Double.NaN);
238 }
239 }
240
254 public void standardDeviation(DoubleMatrix2D m) {
255 if (m.rows() != rows())
256 throw new IllegalArgumentException(
257 "Invalid number of rows in the given matrix: required " + rows() + " but found " + m.rows());
258 if (m.columns() != columns())
259 throw new IllegalArgumentException(
260 "Invalid number of columns in the given matrix: required " + columns() + " but found " + m.columns());
261 for (int r = 0; r < rows(); r++)
262 for (int c = 0; c < columns(); c++) {
263 Tally tally = get(r, c);
264 m.setQuick(r, c, tally != null && tally.numberObs() >= 2 ? tally.standardDeviation() : Double.NaN);
265 }
266 }
267
272 return (MatrixOfTallies<E>) super.clone();
273 }
274}
This class is a variant of Tally for which the individual observations are stored in a list implement...
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
void standardDeviation(DoubleMatrix2D m)
For each tally in the matrix, computes the standard deviation, and stores it into the given matrix.
void add(DoubleMatrix2D x)
Adds the observation x.get(r, c) in the tally whose row is r and column is c, for r = 0,...
boolean areAllNumberObsEqual()
Tests that every tally in this matrix contains the same number of observations.
MatrixOfTallies(int numRows, int numColumns)
Constructs a new unnamed matrix of tallies with numRows rows, and numColumns columns,...
static MatrixOfTallies< Tally > createWithTally(int numRows, int numColumns)
This factory method constructs and returns a matrix of tallies with numRows rows, numColumns columns,...
MatrixOfTallies(String name, int numRows, int numColumns)
Constructs a new matrix of tallies with name name, numRows rows, and numColumns columns,...
int numberObs()
Assuming that each tally in this matrix contains the same number of observations, returns the number ...
static MatrixOfTallies< TallyStore > createWithTallyStore(int numRows, int numColumns)
This factory method constructs and returns a matrix of tallies with numRows rows, numColumns columns,...
void average(DoubleMatrix2D m)
Computes the average for each tally in the matrix.
void add(double[][] x)
Same as add(DoubleMatrix2D) for a 2D array.
MatrixOfTallies< E > clone()
Clones this object.
void variance(DoubleMatrix2D m)
For each tally in the matrix, computes the sample variance, and stores it into the given matrix.
Provides facilities to construct and manage rectangular 2D arrays of statistical probes.