SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
RQMCExperiment.java
1package umontreal.ssj.mcqmctools;
2
3import umontreal.ssj.hups.*;
4import umontreal.ssj.rng.RandomStream;
5import umontreal.ssj.stat.*;
6import umontreal.ssj.stat.list.ListOfTallies;
7import umontreal.ssj.stat.list.lincv.ListOfTalliesWithCV;
8import umontreal.ssj.util.Chrono;
9import umontreal.ssj.util.PrintfFormat;
10
15
21
27 public static void simulReplicatesRQMC(MonteCarloModelDouble model, RQMCPointSet prqmc, int m, Tally statReps) {
28 simulReplicatesRQMC(model, prqmc.getPointSet(), prqmc.getRandomization(), m, statReps);
29 }
30
37 Tally statReps) {
38 statReps.init();
39 int n = p.getNumPoints();
40 // Internal collector for stats on the n outputs X, for each replication.
41 Tally statValue = new Tally();
42 PointSetIterator stream = p.iterator();
43 for (int rep = 0; rep < m; rep++) {
44 rand.randomize(p);
45 stream.resetStartStream();
46 simulateRuns(model, n, stream, statValue);
47 statReps.add(statValue.average()); // For the estimator of the mean.
48 }
49 }
50
59 public static void simulReplicatesRQMC(MonteCarloModelDouble model, RQMCPointSet prqmc, int m, Tally statReps,
60 double[][] data) {
61 simulReplicatesRQMC(model, prqmc.getPointSet(), prqmc.getRandomization(), m, statReps, data);
62 }
63
69 Tally statReps, double[][] data) {
70 int n = p.getNumPoints();
71 data = new double[m][]; // ????
72 // Internal collector for stats on the n outputs X, for each replication.
73 TallyStore statSave = new TallyStore(n);
74 PointSetIterator stream = p.iterator();
75 for (int rep = 0; rep < m; rep++) {
76 rand.randomize(p);
77 stream.resetStartStream();
78 simulateRuns(model, n, stream, statSave);
79 statReps.add(statSave.average()); // For the estimator of the mean.
80 statSave.quickSort();
81 data[rep] = statSave.getArray(); // Instead of copy, just exchange pointers! Good?
82 statSave = new TallyStore(n);
83 }
84 }
85
90 PointSetRandomization rand, int m, Tally statRQMC) {
91 PrintfFormat str = new PrintfFormat();
92 Chrono timer = new Chrono();
93 simulReplicatesRQMC(model, p, rand, m, statRQMC);
95 str.append(model.toString() + "\n");
96 str.append("QMC point set: " + p.toString() + "\n");
97 str.append("Randomization: " + rand.toString() + "\n");
98 str.append(statRQMC.report());
99 str.append("Total CPU time: " + timer.format() + "\n");
100 return str.toString();
101 }
102
111 PointSetRandomization rand, int m, Tally statRQMC, double variancePerRunMC, double secondsPerRunMC) {
112 PrintfFormat str = new PrintfFormat();
113 Chrono timer = new Chrono();
114 simulReplicatesRQMC(model, p, rand, m, statRQMC);
115 double secondsRQMC = timer.getSeconds() / (m * p.getNumPoints());
116 double varianceRQMC = p.getNumPoints() * statRQMC.variance();
118 str.append(model.toString() + "\n");
119 str.append("QMC point set: " + p.toString() + "\n");
120 str.append("Randomization: " + rand.toString() + "\n");
121 str.append(statRQMC.report());
122 str.append("Total CPU time: " + timer.format() + "\n\n");
123 str.append("MC Variance per run: ");
124 str.append(12, 5, 4, variancePerRunMC);
125 str.append("\n");
126 str.append("RQMC Variance per run: ");
127 str.append(10, 5, 4, varianceRQMC);
128 str.append("\n");
129 str.append("Variance ratio: ");
130 str.append(12, 3, 4, variancePerRunMC / varianceRQMC);
131 str.append("\n");
132 str.append("Efficiency ratio: ");
133 str.append(12, 3, 4, (variancePerRunMC * secondsPerRunMC) / (varianceRQMC * secondsRQMC));
134 str.append("\n---------------------------------------------------------------\n");
135 return str.toString();
136 }
137
138 // Makes a comparison between MC and RQMC, pass and recover the two Tally
139 // probes.
146 PointSetRandomization rand, int n, int m, Tally statMC, Tally statRQMC) {
147 Chrono timer = new Chrono();
148 PrintfFormat pf = new PrintfFormat();
149 pf.append(MonteCarloExperiment.simulateRunsDefaultReportStudent(model, n, stream, statMC, timer) + "\n");
150 double secPerRunMC = timer.getSeconds() / n;
151 pf.append(RQMCExperiment.simulReplicatesRQMCDefaultReportCompare(model, p, rand, m, statRQMC, statMC.variance(),
152 secPerRunMC));
153 return pf.toString();
154 }
155
161 PointSetRandomization rand, int n, int m) {
162 Tally statMC = new Tally("Statistics with MC");
163 Tally statRQMC = new Tally("Statistics on RQMC averages");
164 return makeComparisonExperimentMCvsRQMC(model, stream, p, rand, n, m, statMC, statRQMC);
165 }
166
180 public static void simulReplicatesRQMC(MonteCarloModelDoubleArray model, RQMCPointSet prqmc, int m,
181 ListOfTallies<Tally> statRepsList) {
182 simulReplicatesRQMC(model, prqmc.getPointSet(), prqmc.getRandomization(), m, statRepsList);
183 }
184
197 int m, ListOfTallies<Tally> statRepsList) {
198 statRepsList.init();
199 int n = p.getNumPoints();
200 int t = model.getPerformanceDim();
201 // Internal collector for stats on the n outputs X, for each
202 // replication.
204 PointSetIterator stream = p.iterator();
205 for (int rep = 0; rep < m; rep++) {
206 rand.randomize(p);
207 stream.resetStartStream();
208 simulateRuns(model, n, stream, statValue);
209 double[] means = new double[t];
210 statValue.average(means);
211 statRepsList.add(means); // For the estimator of the mean.
212 }
213 }
214
231 public static void simulReplicatesRQMC(MonteCarloModelDoubleArray model, RQMCPointSet prqmc, int m,
232 ListOfTallies<Tally> statRepsList, double[][][] data) {
233 simulReplicatesRQMC(model, prqmc.getPointSet(), prqmc.getRandomization(), m, statRepsList, data);
234
235 }
236
249
251 int m, ListOfTallies<Tally> statRepsList, double[][][] data) {
252 int n = p.getNumPoints();
253 int t = model.getPerformanceDim();
254 // Internal collector for stats on the n outputs X, for each
255 // replication.
257 PointSetIterator stream = p.iterator();
258 for (int rep = 0; rep < m; rep++) {
259 p.randomize(rand);
260 stream.resetStartStream();
261 simulateRuns(model, n, stream, statSave);
262 // TODO: should we keep the possibility to sort the data?
263 double[] means = new double[t];
264 statSave.average(means);
265 statRepsList.add(means); // For the estimator of the mean.
266 for (int i = 0; i < t; i++)
267 // This allows to just exchange pointers instead of copying
268 // entries. But the array will be [m][t][n]
269 // which is less practical than [m][n][t]
270 // data[rep][i] = statSave.get(i).getArray();
271 for (int k = 0; k < n; k++)
272 // creates [m][n][t]-array
273 data[rep][k][i] = statSave.get(i).getArray()[k];
274 statSave = ListOfTallies.createWithTallyStore(n, t);
275
276 }
277
278 }
279
290 public static void simulReplicatesRQMCCV(MonteCarloModelCV model, RQMCPointSet prqmc, int m,
291 ListOfTalliesWithCV<Tally> statWithCV) {
292 simulReplicatesRQMCCV(model, prqmc.getPointSet(), prqmc.getRandomization(), m, statWithCV);
293 }
294
300 ListOfTalliesWithCV<Tally> statWithCV) {
301 statWithCV.init();
302 int n = p.getNumPoints();
303 int numCV = model.getNumberCV();
304 PointSetIterator stream = p.iterator();
305 for (int rep = 0; rep < m; rep++) {
306 rand.randomize(p);
307 stream.resetStartStream();
308 double sumValues = 0.0;
309 double[] sumValuesCV = new double[numCV];
310 double[] curValuesCV = new double[numCV]; // Current value of the CV
311 for (int i = 0; i < n; i++) {
312 model.simulate(stream);
313 sumValues += model.getPerformance();
314 curValuesCV = model.getValuesCV();
315 for (int k = 0; k < numCV; k++)
316 sumValuesCV[k] += curValuesCV[k];
317 stream.resetNextSubstream();
318 }
319 for (int k = 0; k < numCV; k++)
320 sumValuesCV[k] /= (double) n;
321 statWithCV.add(sumValues / (double) n, sumValuesCV);
322 }
323 }
324
328 public static void simulFDReplicatesRQMC(MonteCarloModelDouble model1, MonteCarloModelDouble model2, double delta,
329 PointSet p, PointSetRandomization rand, int m, Tally statDiffRQMC) {
330 Tally statValue = new Tally("stat on value");
331 statDiffRQMC.init();
332 double average1;
333 PointSetIterator stream = p.iterator();
334 for (int j = 0; j < m; j++) {
335 rand.randomize(p);
336 stream.resetStartStream();
337 simulateRuns(model1, p.getNumPoints(), stream, statValue);
338 average1 = statValue.average();
339 stream.resetStartStream();
340 simulateRuns(model2, p.getNumPoints(), stream, statValue);
341 statDiffRQMC.add((statValue.average() - average1) / delta);
342 }
343 }
344}
This abstract class represents a general point set.
Definition PointSet.java:99
void randomize(PointSetRandomization rand)
Randomizes this point set using the given rand.
PointSetIterator iterator()
Constructs and returns a point set iterator.
int getNumPoints()
Returns the number of points.
String toString()
Formats a string that contains information about the point set.
This class is used for randomized quasi-Monte Carlo (RQMC) simulations.
PointSet getPointSet()
Returns the point set associated to this object.
PointSetRandomization getRandomization()
Returns the randomization associated to this object.
Provides generic tools to perform simple Monte Carlo experiments with a simulation model that impleme...
static String simulateRunsDefaultReportStudent(MonteCarloModelDouble model, int n, RandomStream stream, Tally statValue, double level, int d, Chrono timer)
Performs n independent runs using n substreams of stream, collects statistics in statValue,...
static void simulateRuns(MonteCarloModelDouble model, int n, RandomStream stream, Tally statValue)
Performs n simulation runs of model using stream and collects statistics in statValue.
Provides basic generic tools to perform RQMC experiments with a simulation model that implements the ...
static String simulReplicatesRQMCDefaultReportCompare(MonteCarloModelDouble model, PointSet p, PointSetRandomization rand, int m, Tally statRQMC, double variancePerRunMC, double secondsPerRunMC)
Similar to simulReplicatesRQMCDefaultReport, but this one makes a comparison with another estimator w...
static String makeComparisonExperimentMCvsRQMC(MonteCarloModelDouble model, RandomStream stream, PointSet p, PointSetRandomization rand, int n, int m, Tally statMC, Tally statRQMC)
This method first performs a MC experiment to estimate the variance and CPU time per run,...
static void simulReplicatesRQMC(MonteCarloModelDoubleArray model, RQMCPointSet prqmc, int m, ListOfTallies< Tally > statRepsList, double[][][] data)
Same as simulReplicatesRQMC(MonteCarloModelDoubleArray, RQMCPointSet, int, ListOfTallies),...
static String simulReplicatesRQMCDefaultReport(MonteCarloModelDouble model, PointSet p, PointSetRandomization rand, int m, Tally statRQMC)
Similar to simulReplicatesRQMC, but also returns a report as a String.
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 simulReplicatesRQMC(MonteCarloModelDoubleArray model, PointSet p, PointSetRandomization rand, int m, ListOfTallies< Tally > statRepsList)
Same as simulReplicatesRQMC(MonteCarloModelArrayOfDoubles, RQMCPointSet, int, ListOfTallies) but with...
static void simulFDReplicatesRQMC(MonteCarloModelDouble model1, MonteCarloModelDouble model2, double delta, PointSet p, PointSetRandomization rand, int m, Tally statDiffRQMC)
To estimate a derivative via a finite difference.
static void simulReplicatesRQMCCV(MonteCarloModelCV model, PointSet p, PointSetRandomization rand, int m, ListOfTalliesWithCV< Tally > statWithCV)
Same as simulReplicatesRQMCCV above, except that here the point set p and the randomization rand are ...
static void simulReplicatesRQMC(MonteCarloModelDouble model, PointSet p, PointSetRandomization rand, int m, Tally statReps, double[][] data)
Same as simulReplicatesRQMC, except that all the n observations for each the m replications are saved...
static String makeComparisonExperimentMCvsRQMC(MonteCarloModelDouble model, RandomStream stream, PointSet p, PointSetRandomization rand, int n, int m)
In this version, the statistical collectors are created internally, so they cannot be accessed extern...
static void simulReplicatesRQMC(MonteCarloModelDoubleArray model, RQMCPointSet prqmc, int m, ListOfTallies< Tally > statRepsList)
Similar to simulReplicatesRQMC(MonteCarloModelDouble, RQMCPointSet, int, Tally) but for a model of ty...
static void simulReplicatesRQMC(MonteCarloModelDouble model, PointSet p, PointSetRandomization rand, int m, Tally statReps)
Simulate m replications 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.
static void simulReplicatesRQMC(MonteCarloModelDoubleArray model, PointSet p, PointSetRandomization rand, int m, ListOfTallies< Tally > statRepsList, double[][][] data)
Same as simulReplicatesRQMC(MonteCarloModelDoubleArray, RQMCPointSet, int, ListOfTallies,...
This class is a variant of Tally for which the individual observations are stored in a list implement...
void quickSort()
Sorts the elements of this probe using the quicksort from Colt.
double[] getArray()
Returns the observations stored in this probe.
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
void setConfidenceIntervalStudent()
Indicates that a confidence interval on the true mean, based on the normality assumption,...
Definition Tally.java:582
double variance()
Returns the sample variance of the observations since the last initialization.
Definition Tally.java:174
void init()
Initializes the statistical collector.
Definition Tally.java:91
void add(double x)
Gives a new observation x to the statistical collector.
Definition Tally.java:109
String report()
Returns a formatted string that contains a report on this probe.
Definition Tally.java:397
void init()
Initializes this list of statistical probes by calling umontreal.ssj.stat.StatProbe....
Represents a list of tally statistical collectors.
static ListOfTallies< Tally > createWithTally(int size)
This factory method constructs and returns a list of tallies with size instances of umontreal....
void average(double[] r)
Computes the average for each tally in this list, and stores the averages in the array r.
static ListOfTallies< TallyStore > createWithTallyStore(int size)
This factory method constructs and returns a list of tallies with size instances of umontreal....
void add(double[] x)
Adds the observation x[i] in tally i of this list, for i = 0,…, size() - 1.
Represents a list of tallies with control variables that inherits the functionalities of a list of ta...
void add(double[] x, double[] c)
Adds a new observation to 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...
double getSeconds()
Returns the CPU time in seconds used by the program since the last call to init for this AbstractChro...
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.
String toString()
Converts the buffer into a String.
PrintfFormat append(String str)
Appends str to the buffer.
This is the interface for iterators that permit one to go through the points of a PointSet and the su...
This interface is for a randomization that can be used to randomize a umontreal.ssj....
void randomize(PointSet p)
This method must randomize p.
An extension of MonteCarloModelDouble that also implements a vector of control variates.
int getNumberCV()
TO DO: Recovers the realizations of the control variates from the last run.
double[] getValuesCV()
Recovers the realizations of the control variates for the last run.
Similar to MonteCarloModelDouble except that the returned performance is an array of real numbers.
int getPerformanceDim()
Returns the dimension of the array of performance measures.
An interface for a very simple simulation model for which Monte Carlo (MC) and RQMC experiments are t...
void simulate(RandomStream stream)
Simulates the model for one run.
String toString()
Returns a description of the model and its parameters.
double getPerformance()
Recovers and returns the realization of the performance measure, of type double.
This interface defines the basic structures to handle multiple streams of uniform (pseudo)random numb...
void resetNextSubstream()
Reinitializes the stream to the beginning of its next substream:
void resetStartStream()
Reinitializes the stream to its initial state : and are set to .