SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
ListOfTalliesWithCovariance.java
1/*
2 * Class: ListOfTalliesWithCovariance
3 * Description: List of tallies with covariance
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 umontreal.ssj.stat.Tally;
28import umontreal.ssj.stat.TallyStore;
29import java.util.logging.Level;
30import java.util.logging.Logger;
31import cern.colt.matrix.DoubleMatrix1D;
32
89public class ListOfTalliesWithCovariance<E extends Tally> extends ListOfTallies<E> {
90 private double[] tempArray;
91 private double[][] sxy;
92
93 // Determines if we use a numerically stable covariance
94 // formula.
95 private boolean isStable = true;
96
97 // The average of the first observations, for each tally
98 private double[] curAverages;
99
100 // The sum (xi - average)(yi - average) of the first observations
101 private double[][] curSum2;
102 private Logger log = Logger.getLogger("umontreal.ssj.stat.list");
103
109 super();
110 }
111
119 public ListOfTalliesWithCovariance(String name) {
120 super(name);
121 }
122
132 for (int i = 0; i < size; i++)
133 list.add(new Tally());
134 list.init();
135 return list;
136 }
137
147 for (int i = 0; i < size; i++)
148 list.add(new TallyStore());
149 list.init();
150 return list;
151 }
152
153 private void createSxy() {
154 int l = size();
155 if (isStable) {
156 curAverages = new double[l];
157 curSum2 = new double[l - 1][];
158 for (int i = 0; i < l - 1; i++)
159 curSum2[i] = new double[l - 1 - i];
160 } else {
161 sxy = new double[l - 1][];
162 for (int i = 0; i < l - 1; i++)
163 sxy[i] = new double[l - 1 - i];
164 }
165 tempArray = new double[l];
166 }
167
168 public void init() {
169 super.init();
170
171 if (isModifiable()) {
172 setUnmodifiable();
173 createSxy();
174 }
175 if (isStable) {
176 for (int i = 0; i < curAverages.length; i++)
177 curAverages[i] = 0;
178 for (int i = 0; i < curSum2.length; i++)
179 for (int j = 0; j < curSum2[i].length; j++)
180 curSum2[i][j] = 0;
181 } else
182 for (int i = 0; i < sxy.length; i++)
183 for (int j = 0; j < sxy[i].length; j++)
184 sxy[i][j] = 0;
185 }
186
195 public void add(double[] x) {
196 int l = size();
197
198 int structSize = 0;
199 structSize = (isStable) ? curSum2.length : sxy.length;
200 if (structSize != l - 1)
201 throw new IllegalArgumentException("The structure's size mismatches the list's size");
202
203 super.add(x);
204 if (isStable) {
205 int numObs = get(0).numberObs();
206 // get (i1).average() would return the average over the n
207 // observations, but we need the average over the last n-1 observations.
208 for (int i1 = 0; i1 < l - 1; i1++)
209 for (int i2 = i1 + 1; i2 < l; i2++)
210 curSum2[i1][i2 - i1 - 1] += (numObs - 1) * (x[i1] - curAverages[i1]) * (x[i2] - curAverages[i2])
211 / numObs;
212 for (int i = 0; i < l; i++)
213 curAverages[i] += (x[i] - curAverages[i]) / numObs;
214 // Now, curAverages[i] == get (i).average()
215 } else
216 for (int i1 = 0; i1 < l - 1; i1++)
217 for (int i2 = i1 + 1; i2 < l; i2++)
218 sxy[i1][i2 - i1 - 1] += x[i1] * x[i2];
219 }
220
221 public void add(DoubleMatrix1D x) {
222 x.toArray(tempArray);
223 add(tempArray);
224 }
225
226 public double covariance(int i, int j) {
227 if (i == j)
228 return get(i).variance();
229 if (i > j) {
230 // Make sure that i1 < i2, to have a single case
231 int tmp = i;
232 i = j;
233 j = tmp;
234 }
235
236 Tally tallyi = get(i);
237 Tally tallyj = get(j);
238 if (tallyi == null || tallyj == null)
239 return Double.NaN;
240 int n = tallyi.numberObs();
241 if (n != tallyj.numberObs()) {
242 log.logp(Level.WARNING, "ListOfTalliesWithCovariance", "covariance",
243 "Tally " + i + ", with name " + tallyi.getName() + ", contains " + n + " observations while " + "tally "
244 + j + ", with name " + tallyj.getName() + ", contains " + tallyj.numberObs() + "observations");
245 return Double.NaN;
246 }
247
248 if (n < 2) {
249 log.logp(Level.WARNING, "ListOfTalliesWithCovariance", "covariance",
250 "Tally " + i + ", with name " + tallyi.getName() + ", contains " + n + " observation");
251 return Double.NaN;
252 }
253 if (tallyi instanceof TallyStore && tallyj instanceof TallyStore)
254 return ((TallyStore) tallyi).covariance((TallyStore) tallyj);
255 else if (isStable)
256 return curSum2[i][j - i - 1] / (n - 1);
257 else {
258 double sum1 = tallyi.sum();
259 double sum2 = tallyj.sum();
260 double sum12 = sxy[i][j - i - 1];
261 return (sum12 - sum1 * sum2 / n) / (n - 1);
262 }
263 }
264
272 ta.tempArray = new double[size()];
273 if (curAverages != null)
274 ta.curAverages = curAverages.clone();
275 if (sxy != null) {
276 ta.sxy = new double[sxy.length][];
277 for (int i = 0; i < sxy.length; i++)
278 ta.sxy[i] = sxy[i].clone();
279 }
280 if (curSum2 != null) {
281 ta.curSum2 = new double[curSum2.length][];
282 for (int i = 0; i < curSum2.length; i++)
283 ta.curSum2[i] = curSum2[i].clone();
284 }
285 return ta;
286 }
287}
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
static ListOfTalliesWithCovariance< TallyStore > createWithTallyStore(int size)
This factory method constructs and returns a list of tallies with size instances of umontreal....
ListOfTalliesWithCovariance()
Creates an empty list of tallies with covariance support.
ListOfTalliesWithCovariance(String name)
Creates an empty list of tallies with covariance support and name name.
void add(double[] x)
Adds a new vector of observations x to this list of tallies, and updates the internal data structures...
ListOfTalliesWithCovariance< E > clone()
Clones this object.
static ListOfTalliesWithCovariance< Tally > createWithTally(int size)
This factory method constructs and returns a list of tallies with size instances of umontreal....
Provides support for lists of statistical probes.