SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
ListOfTalliesWithCV.java
1/*
2 * Class: ListOfTalliesWithCV
3 * Description: List of tallies with control variables
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 Eric Buist
9 * @since August 2007
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.list.lincv;
25
26import cern.colt.list.DoubleArrayList;
27import cern.colt.matrix.DoubleMatrix1D;
28import cern.colt.matrix.DoubleMatrix2D;
29import cern.colt.matrix.impl.DenseDoubleMatrix2D;
30import cern.colt.matrix.linalg.Algebra;
31
32import umontreal.ssj.probdist.StudentDist;
33import umontreal.ssj.stat.Tally;
34import umontreal.ssj.stat.TallyStore;
35import umontreal.ssj.stat.list.ListOfTallies;
36import umontreal.ssj.stat.list.ListOfTalliesWithCovariance;
37import umontreal.ssj.util.PrintfFormat;
38
103public class ListOfTalliesWithCV<E extends Tally> extends ListOfTalliesWithCovariance<E> {
104 private static Algebra alg = new Algebra();
105 private DoubleMatrix2D beta;
106 private double[] exp;
107 private double[] tmp;
108 private int q;
109
110 private DoubleMatrix2D tempPP;
111 private DoubleMatrix2D tempQQ;
112 private DoubleMatrix2D tempPQ;
113 private DoubleMatrix2D tempQP;
114
121 super();
122 }
123
131 public ListOfTalliesWithCV(String name) {
132 super(name);
133 }
134
144 public static ListOfTalliesWithCV<Tally> createWithTally(int p, int q) {
146 int size = p + q;
147 for (int i = 0; i < size; i++)
148 list.add(new Tally());
149 list.setNumControlVariables(q);
150 list.init();
151 return list;
152 }
153
165 int size = p + q;
166 for (int i = 0; i < size; i++)
167 list.add(new TallyStore());
168 list.setNumControlVariables(q);
169 list.init();
170 return list;
171 }
172
173 public void init() {
174 super.init();
175 internalInit();
176 }
177
178 private void internalInit() {
179 int p = sizeWithoutCV();
180 beta = new DenseDoubleMatrix2D(q, p);
181 exp = new double[q];
182 tmp = new double[p + q];
183
184 tempPP = new DenseDoubleMatrix2D(p, p);
185 tempQQ = new DenseDoubleMatrix2D(q, q);
186 tempPQ = new DenseDoubleMatrix2D(p, q);
187 tempQP = new DenseDoubleMatrix2D(q, p);
188 setUnmodifiable();
189 }
190
197 public DoubleMatrix2D getBeta() {
198 return beta;
199 }
200
207 public void setBeta(DoubleMatrix2D beta) {
208 if (beta.rows() != getNumControlVariables())
209 throw new IllegalArgumentException("The number of rows in beta must be equal to q");
210 if (beta.columns() != sizeWithoutCV())
211 throw new IllegalArgumentException("The number of columns in beta must be equal to p");
212 this.beta = beta;
213 }
214
222 public double getExpectedValue(int i) {
223 return exp[i];
224 }
225
232 public void setExpectedValue(int i, double e) {
233 exp[i] = e;
234 }
235
242 public double[] getExpectedValues() {
243 return exp;
244 }
245
252 public void setExpectedValues(double[] exp) {
253 if (exp.length != this.exp.length)
254 throw new IllegalArgumentException("Invalid length of exp");
255 this.exp = exp;
256 }
257
264 public int sizeWithoutCV() {
265 return size() - q;
266 }
267
274 return q;
275 }
276
284 public void setNumControlVariables(int q) {
285 if (beta != null)
286 throw new IllegalArgumentException("Cannot change the number of control variables");
287 if (q < 0 || q >= size())
288 throw new IllegalArgumentException("q is negative or greater than or equal to " + size());
289 this.q = q;
290 }
291
297 public void correlationX(DoubleMatrix2D c) {
298 int l = sizeWithoutCV();
299 if (c.rows() != l)
300 throw new IllegalArgumentException("Invalid number of rows in covariance matrix");
301 if (c.columns() != l)
302 throw new IllegalArgumentException("Invalid number of columns in covariance matrix");
303 for (int i1 = 0; i1 < l; i1++)
304 for (int i2 = 0; i2 < l; i2++)
305 c.setQuick(i1, i2, correlation(i1, i2));
306 }
307
313 public void covarianceX(DoubleMatrix2D c) {
314 int l = sizeWithoutCV();
315 if (c.rows() != l)
316 throw new IllegalArgumentException("Invalid number of rows in covariance matrix");
317 if (c.columns() != l)
318 throw new IllegalArgumentException("Invalid number of columns in covariance matrix");
319 for (int i1 = 0; i1 < l; i1++)
320 for (int i2 = 0; i2 < l; i2++)
321 c.setQuick(i1, i2, covariance(i1, i2));
322 }
323
329 public void correlationC(DoubleMatrix2D c) {
330 int p = sizeWithoutCV();
331 int q = getNumControlVariables();
332 if (c.rows() != q)
333 throw new IllegalArgumentException("Invalid number of rows in covariance matrix");
334 if (c.columns() != q)
335 throw new IllegalArgumentException("Invalid number of columns in covariance matrix");
336 for (int i1 = 0; i1 < q; i1++)
337 for (int i2 = 0; i2 < q; i2++)
338 c.setQuick(i1, i2, correlation(p + i1, p + i2));
339 }
340
346 public void covarianceC(DoubleMatrix2D c) {
347 int p = sizeWithoutCV();
348 int q = getNumControlVariables();
349 if (c.rows() != q)
350 throw new IllegalArgumentException("Invalid number of rows in covariance matrix");
351 if (c.columns() != q)
352 throw new IllegalArgumentException("Invalid number of columns in covariance matrix");
353 for (int i1 = 0; i1 < q; i1++)
354 for (int i2 = 0; i2 < q; i2++)
355 c.setQuick(i1, i2, covariance(p + i1, p + i2));
356 }
357
364 public void correlationCX(DoubleMatrix2D c) {
365 int p = sizeWithoutCV();
366 int q = getNumControlVariables();
367 if (c.rows() != q)
368 throw new IllegalArgumentException("Invalid number of rows in covariance matrix");
369 if (c.columns() != p)
370 throw new IllegalArgumentException("Invalid number of columns in covariance matrix");
371 for (int i1 = 0; i1 < q; i1++)
372 for (int i2 = 0; i2 < p; i2++)
373 c.setQuick(i1, i2, correlation(p + i1, i2));
374 }
375
382 public void covarianceCX(DoubleMatrix2D c) {
383 int p = sizeWithoutCV();
384 int q = getNumControlVariables();
385 if (c.rows() != q)
386 throw new IllegalArgumentException("Invalid number of rows in covariance matrix");
387 if (c.columns() != p)
388 throw new IllegalArgumentException("Invalid number of columns in covariance matrix");
389 for (int i1 = 0; i1 < q; i1++)
390 for (int i2 = 0; i2 < p; i2++)
391 c.setQuick(i1, i2, covariance(p + i1, i2));
392 }
393
402 public void add(double[] x, double[] c) {
403 if (x.length != sizeWithoutCV())
404 throw new IllegalArgumentException("Invalid length of x");
405 if (c.length != getNumControlVariables())
406 throw new IllegalArgumentException("Invalid length of c");
407 System.arraycopy(x, 0, tmp, 0, x.length);
408 System.arraycopy(c, 0, tmp, x.length, c.length);
409 add(tmp);
410 }
411
419 public void add(double x, double[] c) {
420 if (sizeWithoutCV() != 1)
421 throw new IllegalArgumentException("Cannot use this method if p != 1");
422 if (c.length != getNumControlVariables())
423 throw new IllegalArgumentException("Invalid length of c");
424 tmp[0] = x;
425 System.arraycopy(c, 0, tmp, 1, c.length);
426 add(tmp);
427 }
428
435 public void add(double x, double c) {
436 if (sizeWithoutCV() != 1)
437 throw new IllegalArgumentException("Cannot use this method if p != 1");
438 if (getNumControlVariables() != 1)
439 throw new IllegalArgumentException("Cannot use this method if q != 1");
440 tmp[0] = x;
441 tmp[1] = c;
442 add(tmp);
443 }
444
458 public double averageWithCV(int i) {
459 int p = sizeWithoutCV();
460 if (i >= p)
461 throw new ArrayIndexOutOfBoundsException(i);
462 Tally tally = get(i);
463 if (tally == null || tally.numberObs() == 0)
464 return Double.NaN;
465 else {
466 double avg = tally.average();
467 for (int j = 0; j < q; j++)
468 avg -= beta.getQuick(j, i) * (get(p + j).average() - exp[j]);
469 return avg;
470 }
471 }
472
490 public void covarianceWithCV(DoubleMatrix2D covCV) {
491 final int p = sizeWithoutCV();
492 final int q = getNumControlVariables();
493 if (covCV.rows() != p || covCV.columns() != p) // ???????????
494 throw new IllegalArgumentException("Invalid dimensions of covCV, (p,q) = " + p + " " + q);
495 DoubleMatrix2D covX = covCV;
496 covarianceX(covX);
497 DoubleMatrix2D covC = tempQQ;
498 covarianceC(covC);
499 DoubleMatrix2D covCX = tempQP;
500 covarianceCX(covCX);
501
502 // covCV contains covX
503 // Add beta^t*sigmaC*beta
504 beta.viewDice().zMult(covC, tempPQ).zMult(beta, covCV, 1, 1, false, false);
505 // Subtract 2beta^t*sigmaCX
506 beta.viewDice().zMult(covCX, covCV, -2, 1, false, false);
507 }
508
526 public double covarianceWithCV(int i, int j) {
527 final int p = sizeWithoutCV();
528 // final int q = getNumControlVariables();
529 if (i >= p || j >= p)
530 throw new IllegalArgumentException("i >= p or j >= p");
531 double cov = covariance(i, j);
532 DoubleMatrix2D covC = tempQQ;
533 covarianceC(covC);
534 DoubleMatrix2D covCX = tempQP;
535 covarianceCX(covCX);
536 // Add beta_.i^t*sigmaC*beta.j
537 cov += beta.viewColumn(i).zDotProduct(covC.zMult(beta.viewColumn(j), null));
538 // Subtract sigmaCX_.i*beta_.j
539 cov -= covCX.viewColumn(i).zDotProduct(beta.viewColumn(j));
540 // Subtract sigmaCX_.j*beta_.i
541 cov -= covCX.viewColumn(j).zDotProduct(beta.viewColumn(i));
542 return cov;
543 }
544
548 public void averageWithCV(double[] a) {
549 int p = sizeWithoutCV();
550 if (a.length != p)
551 throw new IllegalArgumentException(
552 "Invalid length of the given array: given length is " + a.length + ", required length is " + p);
553 for (int i = 0; i < p; i++)
554 a[i] = averageWithCV(i);
555 }
556
562 public void averageX(double[] a) {
563 int l = sizeWithoutCV();
564 if (a.length != l)
565 throw new IllegalArgumentException(
566 "Invalid length of the given array: given length is " + a.length + ", required length is " + l);
567 for (int i = 0; i < a.length; i++) {
568 Tally tally = get(i);
569 a[i] = tally == null ? Double.NaN : tally.average();
570 if (tally.numberObs() == 0)
571 a[i] = Double.NaN;
572 }
573 }
574
580 public void averageC(double[] a) {
581 int p = sizeWithoutCV();
582 int l = getNumControlVariables();
583 if (a.length != l)
584 throw new IllegalArgumentException(
585 "Invalid length of the given array: given length is " + a.length + ", required length is " + l);
586 for (int i = 0; i < a.length; i++) {
587 Tally tally = get(i + p);
588 a[i] = tally == null ? Double.NaN : tally.average();
589 if (tally.numberObs() == 0)
590 a[i] = Double.NaN;
591 }
592 }
593
598 public void standardDeviationWithCV(double[] std) {
599 final int l = sizeWithoutCV();
600 if (l != std.length)
601 throw new IllegalArgumentException("Invalid length of given array");
602 DoubleMatrix2D covCV = tempPP;
603 covarianceWithCV(covCV);
604 for (int i = 0; i < std.length; i++)
605 std[i] = Math.sqrt(covCV.getQuick(i, i));
606 }
607
613 public void varianceWithCV(double[] v) {
614 final int l = sizeWithoutCV();
615 if (l != v.length)
616 throw new IllegalArgumentException("Invalid length of given array");
617 DoubleMatrix2D covCV = tempPP;
618 covarianceWithCV(covCV);
619 for (int i = 0; i < v.length; i++)
620 v[i] = covCV.getQuick(i, i);
621 }
622
636 public void confidenceIntervalStudentWithCV(int i, double level, double[] centerAndRadius) {
637 // Must return an array object, cannot return 2 doubles directly
638 double t;
639 Tally tally = get(i);
640 int numObs = tally.numberObs();
641 if (numObs < 2)
642 throw new RuntimeException("Calling confidenceIntervalStudent with < 2 Observations");
643 centerAndRadius[0] = averageWithCV(i);
644 t = StudentDist.inverseF(numObs - 1, 0.5 * (level + 1.0));
645 centerAndRadius[1] = t * Math.sqrt(covarianceWithCV(i, i) / (double) numObs);
646 }
647
660 public void estimateBeta() {
661 DoubleMatrix2D covC = tempQQ;
662 covarianceC(covC);
663 DoubleMatrix2D covCX = tempQP;
664 covarianceCX(covCX);
665 beta = alg.solve(covC, covCX);
666 assert beta.rows() == getNumControlVariables() && beta.columns() == sizeWithoutCV();
667 }
668
683 /*
684 * public void addControlledObservations (ListOfTallies<? extends Tally> a) {
685 * int p = sizeWithoutCV(); if (a.size() != p) throw new
686 * IllegalArgumentException ("Invalid length of the given array of tallies"); if
687 * (p == 0) return; int numObs = numberObs(); double[] tmp = new double[p]; for
688 * (int obs = 0; obs < numObs; obs++) { for (int i = 0; i < p; i++) { TallyStore
689 * tallyStore = (TallyStore)get (i); double v = tallyStore.getArray().getQuick
690 * (obs); for (int j = 0; j < q; j++) { tallyStore = (TallyStore)get (j + p);
691 * double c = tallyStore.getArray().getQuick (obs); v -= beta.getQuick (j, i)*(c
692 * - exp[j]); } tmp[i] = v; } a.add (tmp); } }
693 */
694
702 /*
703 * public void addControlledObservations (Tally ta) { int p = sizeWithoutCV();
704 * int q = getNumControlVariables(); if (p != 1) throw new
705 * IllegalArgumentException ("This method can only be called with p=1"); int
706 * numObs = get (0).numberObs(); ta.init(); TallyStore tallyStore =
707 * (TallyStore)get (0); DoubleArrayList array = tallyStore.getArray(); for (int
708 * obs = 0; obs < numObs; obs++) { double v = array.getQuick (obs); for (int j =
709 * 0; j < q; j++) { tallyStore = (TallyStore)get (j + p); double c =
710 * tallyStore.getArray().getQuick (obs); v -= beta.getQuick (j, 0)*(c - exp[j]);
711 * } ta.add (v); } }
712 */
713
725 l.beta = (DoubleMatrix2D) beta.clone();
726 l.exp = exp.clone();
727 l.tmp = tmp.clone();
728 l.q = q;
729
730 l.tempPP = (DoubleMatrix2D) tempPP.clone();
731 l.tempQQ = (DoubleMatrix2D) tempQQ.clone();
732 l.tempPQ = (DoubleMatrix2D) tempPQ.clone();
733 l.tempQP = (DoubleMatrix2D) tempQP.clone();
734
735 return l;
736 }
737
738}
Extends the class ContinuousDistribution for the Student.
double inverseF(double u)
Returns the inverse distribution function .
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 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
ListOfTalliesWithCovariance()
Creates an empty list of tallies with covariance support.
void add(double[] x, double[] c)
Adds a new observation to this list of tallies.
void varianceWithCV(double[] v)
Fills the given array with the variance of each component of.
int sizeWithoutCV()
Returns the size of this list excluding the control variables.
void correlationC(DoubleMatrix2D c)
Fills c with the sample correlation matrix of .
void covarianceWithCV(DoubleMatrix2D covCV)
Computes the sample covariance of by replacing ,.
static ListOfTalliesWithCV< TallyStore > createWithTallyStore(int p, int q)
This factory method constructs and returns a list of tallies with p+q new instances of umontreal....
void averageC(double[] a)
Fills the given array with the averages of the control variables.
void standardDeviationWithCV(double[] std)
Fills the given array with the square root of the variance of each component of .
void correlationX(DoubleMatrix2D c)
Fills c with the sample correlation matrix of .
double averageWithCV(int i)
Returns the average of the th component of.
double[] getExpectedValues()
Returns , the expected value of the vector of control variables.
static ListOfTalliesWithCV< Tally > createWithTally(int p, int q)
This factory method constructs and returns a list of tallies with p+q new instances of umontreal....
void covarianceC(DoubleMatrix2D c)
Fills c with the sample covariance matrix of .
DoubleMatrix2D getBeta()
Returns the current matrix .
void covarianceX(DoubleMatrix2D c)
Fills c with the sample covariance matrix of .
void averageX(double[] a)
Fills the given array with the averages without control variables.
void add(double x, double c)
Variant of the add(double[],double[]) that can be used when.
void confidenceIntervalStudentWithCV(int i, double level, double[] centerAndRadius)
Computes a confidence interval for the th component of.
ListOfTalliesWithCV()
Constructs a new empty list of tallies with no control variable.
void covarianceCX(DoubleMatrix2D c)
Fills c with the sample covariance matrix of and.
void setExpectedValue(int i, double e)
Sets the expected value of the th control variable to e.
double covarianceWithCV(int i, int j)
Computes the covariance between component i and j of.
void add(double x, double[] c)
Variant of the add(double[],double[]) method that can be used when there is only one output variable.
void setNumControlVariables(int q)
Sets the number of control variables to q.
int getNumControlVariables()
Returns the number of control variables.
double getExpectedValue(int i)
Gets the expected value of the th control variable.
void setBeta(DoubleMatrix2D beta)
Sets the matrix to beta.
ListOfTalliesWithCV< E > clone()
Fills the given list of tallies with controlled observations.
void estimateBeta()
Estimates the matrix from the observations currently in this list of tallies.
ListOfTalliesWithCV(String name)
Constructs a new empty list of tallies with no control variable and name name.
void correlationCX(DoubleMatrix2D c)
Fills c with the sample correlation matrix of and.
void averageWithCV(double[] a)
Fills the given array with the controlled averages.
Provides support for lists of statistical probes.