SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
TallyStore.java
1/*
2 * Class: TallyStore
3 * Description:
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
9 * @since
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;
26
27import cern.colt.list.DoubleArrayList;
28import java.util.logging.Level;
29import java.util.logging.Logger;
30import umontreal.ssj.util.PrintfFormat;
31
57public class TallyStore extends Tally {
58
59 private DoubleArrayList array = null; // Where the observations are stored.
60 private Logger log = Logger.getLogger("umontreal.ssj.stat");
61
65 public TallyStore() {
66 super();
67 array = new DoubleArrayList();
68 }
69
75 public TallyStore(String name) {
76 super(name);
77 array = new DoubleArrayList();
78 }
79
86 public TallyStore(int capacity) {
87 super();
88 array = new DoubleArrayList(capacity);
89 }
90
98 public TallyStore(String name, int capacity) {
99 super(name);
100 array = new DoubleArrayList(capacity);
101 }
102
109 public TallyStore(DoubleArrayList a) {
110 super();
111 array = a;
112 array.clear();
113 }
114
115 public void init() {
116 super.init();
117 // We must call super before any actions inside constructors.
118 // Unfortunately, the base class calls init, which would
119 // result in a NullPointerException.
120 if (array != null)
121 array.clear();
122 }
123
127 public void add(double x) {
128 if (collect)
129 array.add(x);
130 super.add(x);
131 }
132
138 public double[] getArray() {
139 array.trimToSize();
140 return array.elements();
141 }
142
149 public DoubleArrayList getDoubleArrayList() {
150 array.trimToSize();
151 return array;
152 }
153
157 public void quickSort() {
158 array.quickSort();
159 }
160
170 public double covariance(TallyStore t2) {
171 if (numberObs() != t2.numberObs()) {
172 // System.err.println ("******* TallyStore.covariance(): " +
173 // "Tally's with different number of observations");
174 log.logp(Level.WARNING, "TallyStore", "covariance",
175 "This tally, with name " + getName() + ", contains " + numberObs() + " observations while "
176 + "the given tally, with name " + t2.getName() + ", contains " + t2.numberObs() + "observations");
177 return Double.NaN;
178 }
179
180 if (numberObs() < 2 || t2.numberObs() < 2) {
181 // System.err.println ("******* TallyStore.covariance() with " +
182 // numberObs() + " Observation");
183 log.logp(Level.WARNING, "TallyStore", "covariance",
184 "This tally, with name " + getName() + ", contains " + numberObs() + " observation");
185 return Double.NaN;
186 }
187
188 return cern.jet.stat.Descriptive.covariance(getDoubleArrayList(), t2.getDoubleArrayList());
189 }
190
194 public TallyStore clone() {
195 TallyStore t = (TallyStore) super.clone();
196 t.array = (DoubleArrayList) array.clone();
197 return t;
198 }
199
207 public TallyStore extractSubrange(double a, double b) {
208 int numObs = this.numberObs();
209 double[] obs = this.getArray();
210 double x;
211 TallyStore t = new TallyStore();
212 for (int i = 0; i < numObs; i++) {
213 x = obs[i];
214 if ((x > a) & (x < b))
215 t.add(x);
216 }
217 return t;
218 }
219
235 public TallyStore aggregate(int gsize) {
236 int numObs = this.numberObs();
237 int numGroups = numObs / gsize;
238 double[] obs = this.getArray();
239 double sum;
240 TallyStore t = new TallyStore(numGroups);
241 for (int i = 0; i < numGroups; i++) {
242 sum = 0.0;
243 for (int j = 0; j < gsize; j++)
244 sum += obs[gsize * i + j];
245 sum /= gsize;
246 t.add(sum);
247 }
248 // This is if gsize does not divide numObs.
249 int rest = numObs - numGroups * gsize;
250 if (rest > 0) {
251 sum = 0.0;
252 for (int j = 0; j < rest; j++)
253 sum += obs[gsize * numGroups + j];
254 sum /= rest;
255 t.add(sum);
256 }
257 return t;
258 }
259
263 public String toString() {
264 StringBuffer sb = new StringBuffer();
265 for (int i = 0; i < numberObs(); i++)
266 sb.append(i + " " + array.getQuick(i) + PrintfFormat.NEWLINE);
267 return sb.toString();
268 }
269
270}
String getName()
Returns the name associated with this probe, or null if no name was specified upon construction.
void quickSort()
Sorts the elements of this probe using the quicksort from Colt.
DoubleArrayList getDoubleArrayList()
Returns the DoubleArrayList object that contains the observations for this probe.
TallyStore aggregate(int gsize)
Returns a new TallyStore instance that contains aggregate observations from this TallyStore.
double[] getArray()
Returns the observations stored in this probe.
double covariance(TallyStore t2)
Returns the sample covariance of the observations contained in this tally, and the other tally t2.
TallyStore(String name, int capacity)
Constructs a new TallyStore statistical probe with name name and given initial capacity capacity for ...
void init()
Initializes the statistical collector.
TallyStore(String name)
Constructs a new TallyStore statistical probe with name name.
String toString()
Returns the observations stored in this object as a String.
TallyStore(int capacity)
Constructs a new TallyStore statistical probe with given initial capacity capacity for its associated...
TallyStore clone()
Clones this object and the array which stores the observations.
TallyStore extractSubrange(double a, double b)
Returns a new TallyStore instance that contains all the observations of this TallyStore than are in t...
void add(double x)
Adds one observation x to this probe.
TallyStore(DoubleArrayList a)
Constructs a new TallyStore statistical probe with given associated array.
TallyStore()
Constructs a new TallyStore statistical probe.
double sum()
Returns the sum cumulated so far for this probe.
Definition Tally.java:148
int numberObs()
Returns the number of observations given to this probe since its last initialization.
Definition Tally.java:143
Tally()
Constructs a new unnamed Tally statistical probe.
Definition Tally.java:66
This class acts like a StringBuffer which defines new types of append methods.
static final String NEWLINE
End-of-line symbol or line separator.