SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
RQMCExperimentSeries.java
1/*
2 * Class: RQMCExperimentSeries
3 * Description: randomized quasi-Monte Carlo simulations
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.mcqmctools;
26
27import umontreal.ssj.functionfit.LeastSquares;
28import umontreal.ssj.hups.*;
29import umontreal.ssj.stat.PgfDataTable;
30import umontreal.ssj.stat.Tally;
31import umontreal.ssj.stat.list.lincv.ListOfTalliesWithCV;
32import umontreal.ssj.util.Chrono;
33import umontreal.ssj.util.PrintfFormat;
34import java.util.ArrayList;
35
55
57 int numSets = 0; // Number of point sets in the series.
58 RQMCPointSet[] theSets;
59 double base = 2.0; // Base for the logs (in base 2 by default)
60 double logOfBase; // Math.log(base)
61 double[] size = new double[numSets]; // values of n
62 double[] mean = new double[numSets]; // average performance for each point set
63 double[] variance = new double[numSets]; // variance of the average for each point set
64 double[] logn = new double[numSets]; // log_base n
65 double[] logVar = new double[numSets]; // log_base (variance)
66 String[] tableFields = { "n", "mean", "variance", "log(n)", "log(variance)" };
67 // Names of fields for table.
68 boolean displayExec = false; // When true, prints a display of execution in real time
69 int numReplicates; // last value of m
71 // int numSkipRegression = 0; // Number of values of n that are skipped for the
72 // regression
73 String cpuTime; // time for last experiment\
74 String title;
75
82
89 public RQMCExperimentSeries(RQMCPointSet[] theSets, double base) {
90 init(theSets, base);
91 }
92
97 public void init(RQMCPointSet[] theSets, double base) {
98 this.base = base;
99 this.logOfBase = Math.log(base);
100 this.numSets = theSets.length;
101 this.theSets = theSets;
102 size = new double[numSets]; // n for each point set
103 mean = new double[numSets]; // average for each point set
104 variance = new double[numSets]; // variance for each point set
105 logn = new double[numSets]; // log n
106 logVar = new double[numSets]; // log (variance)
107 }
108
113 public void setExecutionDisplay(boolean display) {
114 this.displayExec = display;
115 }
116
120 public void setBase(double b) {
121 base = b;
122 logOfBase = Math.log(base);
123 }
124
128 public double getBase() {
129 return base;
130 }
131
137 public RQMCPointSet getSet(int i) {
138 return theSets[i];
139 }
140
144 public double[] getValuesn() {
145 return size;
146 }
147
151 public double[] getLogn() {
152 return logn;
153 }
154
158 public double[] getMeans() {
159 return mean;
160 }
161
167 public double[] getVariances() {
168 return variance;
169 }
170
174 public double[] getLogVariances() {
175 return logVar;
176 }
177
184 public void testVarianceRate(MonteCarloModelDouble model, int m) {
185 int n;
186 Tally statReps = new Tally();
187 Chrono timer = new Chrono();
188 numReplicates = m;
189 this.model = model;
190 if (displayExec) {
191 System.out.println("\n ============================================= ");
192 System.out.println("RQMC simulation for mean estimation: ");
193 System.out.println("Model: " + model.toString());
194 System.out.println(" Number of indep copies m = " + m);
195 System.out.println(" Point sets: " + theSets[0].toString() + "\n");
196 System.out.println(" n CPU time mean log(var) ");
197 }
198 for (int s = 0; s < numSets; s++) { // For each cardinality n
199 n = theSets[s].getNumPoints();
200 size[s] = n;
201 logn[s] = Math.log(n) / logOfBase;
202 // System.out.println(" n = " + n + ", log n = " + logn[s] + "\n"); // ****
203 // System.out.println(" " + n + " " + timer.format());
204 RQMCExperiment.simulReplicatesRQMC(model, theSets[s], m, statReps);
205 mean[s] = statReps.average();
206 variance[s] = statReps.variance();
207 logVar[s] = Math.log(variance[s]) / logOfBase;
208 if (displayExec) {
209 System.out.println(" " + n + " " + timer.format() + " " + PrintfFormat.f(10, 5, mean[s]) + " "
210 + PrintfFormat.f(7, 2, logVar[s]));
211 }
212 }
213 cpuTime = timer.format();
214 }
215
219 public void testVarianceRateCV(MonteCarloModelCV model, int m) {
220 int numCV = model.getNumberCV();
222 statWithCV.setExpectedValue(0, 0.0); // The CV is centered to 0.
223 int n;
224 Chrono timer = new Chrono();
225 numReplicates = m;
226 this.model = model;
227 if (displayExec) {
228 System.out.println("\n ============================================= ");
229 System.out.println("RQMC simulation for mean estimation with control variates: ");
230 System.out.println("Model: " + model.toString());
231 System.out.println(" Number of indep copies m = " + m);
232 System.out.println(" Point sets: " + theSets[0].toString() + "\n");
233 System.out.println(" n CPU time mean log(var) ");
234 }
235 for (int s = 0; s < numSets; s++) { // For each cardinality n
236 n = theSets[s].getNumPoints();
237 size[s] = n;
238 logn[s] = Math.log(n) / logOfBase;
239 // System.out.println(" n = " + n + ", log n = " + logn[s] + "\n"); // ****
240 // System.out.println(" " + n + " " + timer.format());
241 RQMCExperiment.simulReplicatesRQMCCV(model, theSets[s], m, statWithCV);
242 statWithCV.estimateBeta(); // This is where the var. and covar. are computed!
243 mean[s] = statWithCV.averageWithCV(0);
244 variance[s] = statWithCV.covarianceWithCV(0, 0);
245 logVar[s] = Math.log(variance[s]) / logOfBase;
246 if (displayExec) {
247 System.out.println(" " + n + " " + timer.format() + " " + PrintfFormat.f(10, 5, mean[s]) + " "
248 + PrintfFormat.f(7, 2, logVar[s]));
249 }
250 }
251 cpuTime = timer.format();
252 }
253
260 public double[] regressionLogVariance(int numSkip) {
261 double[] x2 = new double[numSets - numSkip], y2 = new double[numSets - numSkip];
262 for (int i = 0; i < numSets - numSkip; ++i) {
263 x2[i] = logn[i + numSkip];
264 y2[i] = logVar[i + numSkip];
265 }
266 return LeastSquares.calcCoefficients(x2, y2, 1);
267 }
268
276 public String formatRegression(double[] regCoeff) {
277 StringBuffer sb = new StringBuffer("");
278 // double[] regCoeff = regressionLogVariance (numSkipRegression);
279 sb.append(" Slope of log(var) = " + PrintfFormat.f(8, 5, regCoeff[1]) + "\n");
280 sb.append(" constant term = " + PrintfFormat.f(8, 5, regCoeff[0]) + "\n\n");
281 return sb.toString();
282 }
283
291 public String reportVarianceRate(int numSkip, boolean details) {
292 StringBuffer sb = new StringBuffer("");
293 sb.append("\n ============================================= \n");
294 sb.append("RQMC simulation for mean estimation: \n ");
295 sb.append("Model: " + model.toString() + "\n");
296 sb.append(" Number of indep copies m = " + numReplicates + "\n");
297 sb.append(" RQMC point sets: " + theSets[0].toString() + "\n\n");
298 sb.append("RQMC variance \n");
299 if (details)
300 sb.append(dataLogForPlot());
301 sb.append(formatRegression(regressionLogVariance(numSkip)));
302 // sb.append(" Slope of log(var) = " + PrintfFormat.f(8, 5, regCoeff[1]) +
303 // "\n");
304 // sb.append(" constant term = " + PrintfFormat.f(8, 5, regCoeff[0]) + "\n\n");
305 sb.append(" Total CPU Time = " + cpuTime + "\n");
306 sb.append("-----------------------------------------------------\n");
307 return sb.toString();
308 }
309
317 public PgfDataTable toPgfDataTable(String tableName, String tableLabel) {
318 double[][] data = new double[numSets][5];
319 for (int s = 0; s < numSets; s++) { // For each cardinality n
320 data[s][0] = size[s];
321 data[s][1] = mean[s];
322 data[s][2] = variance[s];
323 data[s][3] = logn[s];
324 data[s][4] = logVar[s];
325 }
326 return new PgfDataTable(tableName, tableLabel, tableFields, data);
327 }
328
329 public PgfDataTable toPgfDataTable(String tableLabel) {
330 return toPgfDataTable(title, tableLabel);
331 }
332
339 public String dataForPlot() {
340 StringBuffer sb = new StringBuffer("");
341 sb.append(" n mean variance \n");
342 for (int s = 0; s < numSets; s++) // For each cardinality n
343 sb.append(
344 " " + size[s] + " " + PrintfFormat.f(10, 5, mean[s]) + " " + PrintfFormat.f(10, 5, variance[s]) + "\n");
345 return sb.toString();
346 }
347
353 public String dataLogForPlot() {
354 StringBuffer sb = new StringBuffer("");
355 sb.append(" log(n) mean log(variance) \n");
356 for (int s = 0; s < numSets; s++) // For each cardinality n
357 sb.append(
358 " " + logn[s] + " " + PrintfFormat.f(10, 5, mean[s]) + " " + PrintfFormat.f(10, 5, logVar[s]) + "\n");
359 return sb.toString();
360 }
361
362 /*
363 * Returns the data on the mean and variance for each n, in an appropriate
364 * format to produce a plot with the pgfplot package. This is OBSOLETE.
365 *
366 * @return Report as a string.
367 *
368 * public String XformatPgfCurve (String curveName) { StringBuffer sb = new
369 * StringBuffer("");
370 * sb.append(" \\addplot+[no marks] table[x=n,y=variance] {" + "\n");
371 * sb.append( dataForPlot() + " } \n"); sb.append(" \\addlegendentry{" +
372 * curveName + "}\n"); sb.append(" % \n"); return sb.toString(); }
373 */
374
384 public String testVarianceRateManyPointTypes(MonteCarloModelDouble model, ArrayList<RQMCPointSet[]> list, int m,
385 int numSkip, boolean makePgfTable, boolean printReport, boolean details, ArrayList<PgfDataTable> listCurves) {
386 StringBuffer sb = new StringBuffer("");
387 // if (makePgfTable)
388 // listCurves = new ArrayList<PgfDataTable>();
389 for (RQMCPointSet[] ptSeries : list) {
390 init(ptSeries, base);
391 testVarianceRate(model, m);
392 if (printReport)
393 System.out.println(reportVarianceRate(numSkip, details));
394 if (printReport)
395 sb.append(reportVarianceRate(numSkip, details));
396 if (makePgfTable == true)
397 listCurves.add(toPgfDataTable(ptSeries[0].getLabel()));
398 }
399 return sb.toString();
400 }
401
406 public String toString() {
407 return ("RQMC Experiment:" + title); // theSets[0].toString();
408 }
409}
This class implements different linear regression models, using the least squares method to estimate ...
static double[] calcCoefficients(double[] X, double[] Y)
Computes the regression coefficients using the least squares method.
This class is used for randomized quasi-Monte Carlo (RQMC) simulations.
PgfDataTable toPgfDataTable(String tableName, String tableLabel)
Takes the data from the most recent experiment and returns it in a PgfDataTable.
void setExecutionDisplay(boolean display)
When set to true, a real-time display of execution results and CPU times will be printed on the defau...
RQMCExperimentSeries()
Constructor without RQMC point sets.
void init(RQMCPointSet[] theSets, double base)
Resets the array of RQMC point sets for this object, and initializes (re-creates) the arrays that wil...
double[] regressionLogVariance(int numSkip)
Performs a linear regression of log(variance) vs log(n), and returns the coefficients (constant and s...
String reportVarianceRate(int numSkip, boolean details)
Produces and returns a report on the last experiment.
RQMCPointSet getSet(int i)
Returns the point set number i associated to this object (starts at 0).
double getBase()
Returns the base used for the logs.
void setBase(double b)
Sets the base for the logs to b.
String dataForPlot()
Returns the data on the mean and variance for each n, in an appropriate format to produce a plot with...
double[] getVariances()
Returns the vector of variances from the last experiment.
void testVarianceRateCV(MonteCarloModelCV model, int m)
Similar to testVarianceRate, but with control variates, all centered at 0.
double[] getMeans()
Returns the vector of means from last experiment, after an experiment.
void testVarianceRate(MonteCarloModelDouble model, int m)
Performs an RQMC experiment with the given model, with this series of RQMC point sets.
String testVarianceRateManyPointTypes(MonteCarloModelDouble model, ArrayList< RQMCPointSet[]> list, int m, int numSkip, boolean makePgfTable, boolean printReport, boolean details, ArrayList< PgfDataTable > listCurves)
Performs an experiment (testVarianceRate) for each point set series in the given list,...
String dataLogForPlot()
Similar to dataForPlot, but for the log(variance) in terms of log n.
String toString()
Performs an experiment (testVarianceRate) for each point set series in the given list,...
RQMCExperimentSeries(RQMCPointSet[] theSets, double base)
Constructor with a given series of RQMC point sets.
String formatRegression(double[] regCoeff)
Takes the regression coefficients of log(variance) in regCoeff and returns a two-line string that rep...
double[] getValuesn()
Returns the vector of values of n, after an experiment.
double[] getLogn()
Returns the vector of log_base(n), after an experiment.
double[] getLogVariances()
Returns the vector of log_base of variances from the last experiment.
Provides basic generic tools to perform RQMC experiments with a simulation model that implements the ...
static void simulReplicatesRQMC(MonteCarloModelDouble model, RQMCPointSet prqmc, int m, Tally statReps)
Simulate m replications with prqmc and return the m RQMC average observations in statReps.
static void simulReplicatesRQMCCV(MonteCarloModelCV model, RQMCPointSet prqmc, int m, ListOfTalliesWithCV< Tally > statWithCV)
Same as simulReplicatesRQMC, except that this one uses control variates.
Represents a data table which has a name, a number of observations (rows), a number of fields (column...
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
double variance()
Returns the sample variance of the observations since the last initialization.
Definition Tally.java:174
Represents a list of tallies with control variables that inherits the functionalities of a list of ta...
void covarianceWithCV(DoubleMatrix2D covCV)
Computes the sample covariance of by replacing ,.
double averageWithCV(int i)
Returns the average of the th component of.
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 setExpectedValue(int i, double e)
Sets the expected value of the th control variable to e.
void estimateBeta()
Estimates the matrix from the observations currently in this list of tallies.
String format()
Converts the CPU time used by the program since its last call to init for this AbstractChrono to a St...
The Chrono class extends the umontreal.ssj.util.AbstractChrono class and computes the CPU time for th...
Definition Chrono.java:37
This class acts like a StringBuffer which defines new types of append methods.
static String f(double x)
Same as f(0, 6, x).
An extension of MonteCarloModelDouble that also implements a vector of control variates.
An interface for a very simple simulation model for which Monte Carlo (MC) and RQMC experiments are t...