24package umontreal.ssj.stat.list.lincv;
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;
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;
104 private static Algebra alg =
new Algebra();
105 private DoubleMatrix2D beta;
106 private double[] exp;
107 private double[] tmp;
110 private DoubleMatrix2D tempPP;
111 private DoubleMatrix2D tempQQ;
112 private DoubleMatrix2D tempPQ;
113 private DoubleMatrix2D tempQP;
147 for (
int i = 0; i < size; i++)
149 list.setNumControlVariables(q);
166 for (
int i = 0; i < size; i++)
168 list.setNumControlVariables(q);
178 private void internalInit() {
180 beta =
new DenseDoubleMatrix2D(q, p);
182 tmp =
new double[p + q];
184 tempPP =
new DenseDoubleMatrix2D(p, p);
185 tempQQ =
new DenseDoubleMatrix2D(q, q);
186 tempPQ =
new DenseDoubleMatrix2D(p, q);
187 tempQP =
new DenseDoubleMatrix2D(q, p);
209 throw new IllegalArgumentException(
"The number of rows in beta must be equal to q");
211 throw new IllegalArgumentException(
"The number of columns in beta must be equal to p");
253 if (exp.length !=
this.exp.length)
254 throw new IllegalArgumentException(
"Invalid length of exp");
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());
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++)
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));
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++)
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));
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++)
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));
402 public void add(
double[] x,
double[] c) {
404 throw new IllegalArgumentException(
"Invalid length of x");
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);
419 public void add(
double x,
double[] c) {
421 throw new IllegalArgumentException(
"Cannot use this method if p != 1");
423 throw new IllegalArgumentException(
"Invalid length of c");
425 System.arraycopy(c, 0, tmp, 1, c.length);
435 public void add(
double x,
double c) {
437 throw new IllegalArgumentException(
"Cannot use this method if p != 1");
439 throw new IllegalArgumentException(
"Cannot use this method if q != 1");
461 throw new ArrayIndexOutOfBoundsException(i);
462 Tally tally =
get(i);
463 if (tally ==
null || tally.
numberObs() == 0)
467 for (
int j = 0; j < q; j++)
468 avg -= beta.getQuick(j, i) * (
get(p + j).average() - exp[j]);
493 if (covCV.rows() != p || covCV.columns() != p)
494 throw new IllegalArgumentException(
"Invalid dimensions of covCV, (p,q) = " + p +
" " + q);
495 DoubleMatrix2D covX = covCV;
497 DoubleMatrix2D covC = tempQQ;
499 DoubleMatrix2D covCX = tempQP;
504 beta.viewDice().zMult(covC, tempPQ).zMult(beta, covCV, 1, 1,
false,
false);
506 beta.viewDice().zMult(covCX, covCV, -2, 1,
false,
false);
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;
534 DoubleMatrix2D covCX = tempQP;
537 cov += beta.viewColumn(i).zDotProduct(covC.zMult(beta.viewColumn(j),
null));
539 cov -= covCX.viewColumn(i).zDotProduct(beta.viewColumn(j));
541 cov -= covCX.viewColumn(j).zDotProduct(beta.viewColumn(i));
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++)
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();
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();
601 throw new IllegalArgumentException(
"Invalid length of given array");
602 DoubleMatrix2D covCV = tempPP;
604 for (
int i = 0; i < std.length; i++)
605 std[i] = Math.sqrt(covCV.getQuick(i, i));
616 throw new IllegalArgumentException(
"Invalid length of given array");
617 DoubleMatrix2D covCV = tempPP;
619 for (
int i = 0; i < v.length; i++)
620 v[i] = covCV.getQuick(i, i);
639 Tally tally =
get(i);
642 throw new RuntimeException(
"Calling confidenceIntervalStudent with < 2 Observations");
645 centerAndRadius[1] = t * Math.sqrt(
covarianceWithCV(i, i) / (
double) numObs);
661 DoubleMatrix2D covC = tempQQ;
663 DoubleMatrix2D covCX = tempQP;
665 beta = alg.solve(covC, covCX);
725 l.beta = (DoubleMatrix2D) beta.clone();
730 l.tempPP = (DoubleMatrix2D) tempPP.clone();
731 l.tempQQ = (DoubleMatrix2D) tempQQ.clone();
732 l.tempPQ = (DoubleMatrix2D) tempPQ.clone();
733 l.tempQP = (DoubleMatrix2D) tempQP.clone();
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...
double average()
Returns the average value of the observations since the last initialization.
int numberObs()
Returns the number of observations given to this probe since its last initialization.
ListOfTalliesWithCovariance()
Creates an empty list of tallies with covariance support.
double correlation(int i, int j)
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 setExpectedValues(double[] exp)
Sets to exp.
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.