SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
FunctionOfMultipleMeansTallyWithCV.java
1/*
2 * Class: FunctionOfMultipleMeansTallyWithCV
3 * Description: function of multiple means tally 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 Éric 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.matrix.DoubleMatrix2D;
27
28import umontreal.ssj.util.MultivariateFunction;
29import umontreal.ssj.stat.FunctionOfMultipleMeansTally;
30import umontreal.ssj.stat.Tally;
31
55 private MultivariateFunction funcNoCV;
56 private double[] beta;
57
68 // Assumes that the superclass' constructor do not evaluate the function
69 super(new LinCVFunction(p + q), ListOfTalliesWithCV.createWithTally(p, q));
70 this.funcNoCV = funcNoCV;
71 beta = new double[q];
72 ((LinCVFunction) getFunction()).initFunctionOfMultipleMeansTallyWithCV(this);
73 }
74
83 // Assumes that the superclass' constructor do not evaluate the function
84 super(new LinCVFunction(l.size()), l);
85 this.funcNoCV = funcNoCV;
86 beta = new double[l.getNumControlVariables()];
87 ((LinCVFunction) getFunction()).initFunctionOfMultipleMeansTallyWithCV(this);
88 }
89
100 return funcNoCV;
101 }
102
109 return beta.length;
110 }
111
121 return getDimension() - beta.length;
122 }
123
130 public double getBeta(int i) {
131 return beta[i];
132 }
133
140 public void setBeta(int i, double b) {
141 beta[i] = b;
142 }
143
150 public double[] getBeta() {
151 return beta;
152 }
153
160 public void setBeta(double[] beta) {
161 if (beta.length != this.beta.length)
162 throw new IllegalArgumentException("Invalid length of beta");
163 this.beta = beta;
164 }
165
170 return (ListOfTalliesWithCV<Tally>) super.getListOfTallies();
171 }
172
182 public double getExpectedValue(int i) {
183 return getListOfTalliesWithCV().getExpectedValue(i);
184 }
185
194 public void setExpectedValue(int i, double e) {
195 getListOfTalliesWithCV().setExpectedValue(i, e);
196 }
197
205 public double[] getExpectedValues() {
206 return getListOfTalliesWithCV().getExpectedValues();
207 }
208
216 public void setExpectedValues(double[] exp) {
217 getListOfTalliesWithCV().setExpectedValues(exp);
218 }
219
248
259 public void estimateBetaFromMatrix(DoubleMatrix2D mbeta) {
261 int p = getDimension() - beta.length;
262 int q = beta.length;
263 double[] avg = new double[p];
264 double[] gradient = new double[p];
265 for (int i = 0; i < p; i++)
266 avg[i] = a.get(i).average();
267 for (int i = 0; i < p; i++)
268 gradient[i] = funcNoCV.evaluateGradient(i, avg);
269 for (int i = 0; i < q; i++) {
270 beta[i] = 0;
271 for (int j = 0; j < p; j++)
272 beta[i] += mbeta.getQuick(i, j) * gradient[j];
273 }
274 }
275
282 mta.beta = beta.clone();
283
284 LinCVFunction fct = new LinCVFunction(getDimension());
285 mta.func = fct;
286 fct.initFunctionOfMultipleMeansTallyWithCV(mta);
287
288 return mta;
289 }
290
291 private static class LinCVFunction implements MultivariateFunction {
293 private int pplusq;
294 private double[] tmp;
295
296 public LinCVFunction(int pplusq) {
297 this.pplusq = pplusq;
298 }
299
300 public void initFunctionOfMultipleMeansTallyWithCV(FunctionOfMultipleMeansTallyWithCV fcv) {
301 this.fcv = fcv;
302 tmp = new double[fcv.getListOfTalliesWithCV().sizeWithoutCV()];
303 }
304
305 public int getDimension() {
306 return pplusq;
307 }
308
309 public double evaluate(double... x) {
310 MultivariateFunction funcNoCV = fcv.getFunctionWithoutCV();
311
312 if (x.length != getDimension())
313 throw new IllegalArgumentException(
314 "x has length " + x.length + ", which differs from the dimension " + getDimension());
315 int d = getDimension() - fcv.beta.length;
316 System.arraycopy(x, 0, tmp, 0, d);
317 double gValue = funcNoCV.evaluate(tmp);
318 ListOfTalliesWithCV<?> a = fcv.getListOfTalliesWithCV();
319 for (int i = 0; i < fcv.beta.length; i++)
320 gValue -= fcv.beta[i] * (x[d + i] - a.getExpectedValue(i));
321 return gValue;
322 }
323
324 public double evaluateGradient(int i, double... x) {
325 MultivariateFunction funcNoCV = fcv.getFunctionWithoutCV();
326
327 if (x.length != getDimension())
328 throw new IllegalArgumentException(
329 "x has length " + x.length + ", which differs from the dimension " + getDimension());
330 int d = getDimension() - fcv.beta.length;
331 if (i < d) {
332 System.arraycopy(x, 0, tmp, 0, d);
333 return funcNoCV.evaluateGradient(i, tmp);
334 }
335 return -fcv.beta[i - d];
336 }
337 }
338}
FunctionOfMultipleMeansTally(MultivariateFunction func, int d)
Constructs a function of multiple means tally with dimension d, and function func.
MultivariateFunction getFunction()
Returns the function of multiple means used by this tally.
int getDimension()
Returns the dimension of this tally, i.e., the size of any vector of observations.
void average(double[] r)
Computes the average for each tally in this list, and stores the averages in the array r.
Represents a function of multiple means tally for an estimator with linear control variables.
int getNumControlVariables()
Returns the number of control variables being used.
FunctionOfMultipleMeansTallyWithCV(MultivariateFunction funcNoCV, ListOfTalliesWithCV< Tally > l)
Constructs a new function of multiple means tally with control variables from the list of tallies l,...
int getDimensionWithoutCV()
Returns the dimension of this tally excluding the control variables.
void setExpectedValues(double[] exp)
Sets the vector to to given array exp.
void setExpectedValue(int i, double e)
Sets the expected value of the th component of.
FunctionOfMultipleMeansTallyWithCV(MultivariateFunction funcNoCV, int p, int q)
Creates a new function of multiple means tally for a function funcNoCV of p variables,...
double getExpectedValue(int i)
Gets the expected value of the th component of.
MultivariateFunction getFunctionWithoutCV()
Returns the implementation computing the function.
ListOfTalliesWithCV< Tally > getListOfTalliesWithCV()
Returns the list of tallies with control variables used by this object.
void estimateBetaFromMatrix(DoubleMatrix2D mbeta)
Multiples the given matrix by the gradient.
void estimateBeta()
Uses the sample averages and covariances obtained from the internal list of tallies to estimate the.
FunctionOfMultipleMeansTallyWithCV clone()
Clones this object and the function which is stored inside.
Represents a list of tallies with control variables that inherits the functionalities of a list of ta...
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....
DoubleMatrix2D getBeta()
Returns the current matrix .
int getNumControlVariables()
Returns the number of control variables.
void estimateBeta()
Estimates the matrix from the observations currently in this list of tallies.
Represents a function of multiple variables.
int getDimension()
Returns , the dimension of the function computed by this implementation.