SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
MonteCarloExperiment.java
1package umontreal.ssj.mcqmctools;
2
3import umontreal.ssj.rng.RandomStream;
4import umontreal.ssj.stat.*;
5import umontreal.ssj.stat.list.ListOfTallies;
6import umontreal.ssj.stat.list.lincv.*;
7import umontreal.ssj.util.Chrono;
8import umontreal.ssj.util.PrintfFormat;
9
19
21
26
27 public static void simulateRuns(MonteCarloModelDouble model, int n, RandomStream stream, Tally statValue) {
28 statValue.init();
29 for (int i = 0; i < n; i++) {
30 model.simulate(stream);
31 statValue.add(model.getPerformance());
32 stream.resetNextSubstream();
33 }
34 }
35
49 public static void simulateRuns(MonteCarloModelDoubleArray model, int n, RandomStream stream,
50 ListOfTallies<? extends Tally> statValueList) {
51 statValueList.init();
52 for (int i = 0; i < n; i++) {
53 model.simulate(stream);
54 statValueList.add(model.getPerformance());
55 stream.resetNextSubstream();
56 }
57 }
58
63 public static void simulateRunsCV(MonteCarloModelCV model, int n, RandomStream stream,
65 statWithCV.init();
66 for (int i = 0; i < n; i++) {
67 model.simulate(stream);
68 statWithCV.add(model.getPerformance(), model.getValuesCV());
69 stream.resetNextSubstream();
70 }
71 }
72
78 public static void simulateRunsCV(MonteCarloModelCV model, int n, RandomStream stream, TallyStore statX,
79 TallyStore statC) {
80 statX.init();
81 statC.init();
82 for (int i = 0; i < n; i++) {
83 model.simulate(stream);
84 statX.add(model.getPerformance());
85 statC.add(model.getValuesCV()[0]);
86 stream.resetNextSubstream();
87 }
88 }
89
97 public static void simulateRunsCV(MonteCarloModelCV model, int n, RandomStream stream, double[] mean,
98 double[] variance) {
99 TallyStore statX = new TallyStore(n);
100 TallyStore statC = new TallyStore(n);
101 simulateRunsCV(model, n, stream, statX, statC);
102 computeMeanVarCV(statX, statC, mean, variance);
103 }
104
112 public static void computeMeanVarCV(TallyStore statX, TallyStore statC, double[] mean, double[] variance) {
113 mean[0] = statX.average();
114 variance[0] = statX.variance();
115 double meanC = statC.average();
116 double varC = statC.variance();
117 double covCX = statC.covariance(statX);
118 double beta = covCX / varC;
119 mean[1] = mean[0] - beta * meanC; // CV has mean 0.
120 variance[1] = variance[0] + beta * beta * varC - 2 * beta * covCX;
121 }
122
134 public static void simulFDReplicatesCRN(MonteCarloModelDouble model1, MonteCarloModelDouble model2, double delta,
135 int n, RandomStream stream, Tally statDiff) {
136 statDiff.init();
137 double value1;
138 for (int i = 0; i < n; i++) {
139 stream.resetNextSubstream();
140 model1.simulate(stream);
141 value1 = model1.getPerformance();
142 stream.resetStartSubstream();
143 model2.simulate(stream);
144 statDiff.add((model2.getPerformance() - value1) / delta);
145 }
146 }
147
153 public static void simulFDReplicatesIRN(MonteCarloModelDouble model1, MonteCarloModelDouble model2, double delta,
154 int n, RandomStream stream, Tally statDiff) {
155 statDiff.init();
156 double value1;
157 for (int i = 0; i < n; i++) {
158 stream.resetNextSubstream();
159 model1.simulate(stream);
160 value1 = model1.getPerformance();
161 model2.simulate(stream);
162 statDiff.add((model2.getPerformance() - value1) / delta);
163 }
164 }
165
172 public static String simulateRunsDefaultReportStudent(MonteCarloModelDouble model, int n, RandomStream stream,
173 Tally statValue, double level, int d, Chrono timer) {
174 PrintfFormat str = new PrintfFormat();
175 timer.init();
176 simulateRuns(model, n, stream, statValue);
178 str.append(model.toString() + "\n");
179 str.append(statValue.report(level, d));
180 str.append("Variance per run: ");
181 str.append(10, d, d - 1, statValue.variance());
182 str.append("\n");
183 // str.append("\n");
184 str.append("Total CPU time: " + timer.format() + "\n");
185 return str.toString();
186 }
187
192 public static String simulateRunsDefaultReportStudent(MonteCarloModelDouble model, int n, RandomStream stream,
193 Tally statValue, double level, int d) {
194 return simulateRunsDefaultReportStudent(model, n, stream, statValue, level, d, new Chrono());
195 }
196
201 public static String simulateRunsDefaultReportStudent(MonteCarloModelDouble model, int n, RandomStream stream,
202 Tally statValue, Chrono timer) {
203 return simulateRunsDefaultReportStudent(model, n, stream, statValue, 0.95, 4, timer);
204 }
205
210 public static String simulateRunsDefaultReportStudent(MonteCarloModelDouble model, int n, RandomStream stream,
211 Tally statValue) {
212 return simulateRunsDefaultReportStudent(model, n, stream, statValue, 0.95, 4, new Chrono());
213 }
214
220 public static String simulateRunsDefaultReportCV(MonteCarloModelCV model, int n, RandomStream stream,
221 ListOfTalliesWithCV<TallyStore> statWithCV, double level, int d, Chrono timer) {
222 PrintfFormat str = new PrintfFormat();
223 timer.init();
224 simulateRunsCV(model, n, stream, statWithCV);
225 statWithCV.estimateBeta(); // Computes the variances and covariances!
226 // statWithCV.setConfidenceIntervalStudent();
227 str.append(model.toString() + "\n");
228 // str.append(statWithCV.report(0.95, 4));
229 double[] centerAndRadius = new double[2];
230 statWithCV.confidenceIntervalStudentWithCV(0, level, centerAndRadius);
231 str.append("Average: " + statWithCV.averageWithCV(0) + "\n");
232 str.append("Variance per run, no CV: " + statWithCV.covarianceWithCV(0, 0) + "\n");
233 double[] varCV = new double[1];
234 statWithCV.varianceWithCV(varCV); // Is this implemented for more than one CV ? ***********
235 str.append("Variance per run with CV: " + varCV[0] + "\n");
236 str.append("Center of CI: " + centerAndRadius[0] + "\n");
237 str.append("Radius of CI: " + centerAndRadius[1] + "\n");
238 str.append("Total CPU time: " + timer.format() + "\n");
239 return str.toString();
240 }
241
246 public static String simulateRunsDefaultReportCV(MonteCarloModelCV model, int n, RandomStream stream,
247 ListOfTalliesWithCV<TallyStore> statWithCV, double level, int d) {
248 return simulateRunsDefaultReportCV(model, n, stream, statWithCV, level, d, new Chrono());
249 }
250
254 public static String simulateRunsDefaultReportCV(MonteCarloModelCV model, int n, RandomStream stream, double[] mean,
255 double[] variance, double level, int d, Chrono timer) {
256 PrintfFormat str = new PrintfFormat();
257 timer.init();
258 simulateRunsCV(model, n, stream, mean, variance);
259 // statWithCV.setConfidenceIntervalStudent();
260 str.append(model.toString() + "\n");
261 // str.append(statWithCV.report(0.95, 4));
262 str.append("Average, no CV: " + mean[0] + "\n");
263 str.append("Average with CV: " + mean[1] + "\n");
264 str.append("Variance, no CV: " + variance[0] + "\n");
265 str.append("Variance with CV: " + variance[1] + "\n");
266 str.append("Total CPU time: " + timer.format() + "\n");
267 return str.toString();
268 }
269
270}
Provides generic tools to perform simple Monte Carlo experiments with a simulation model that impleme...
static void computeMeanVarCV(TallyStore statX, TallyStore statC, double[] mean, double[] variance)
Given statistics collected in statX and statC as with simulateRunsCV, this method computes the mean a...
static String simulateRunsDefaultReportCV(MonteCarloModelCV model, int n, RandomStream stream, ListOfTalliesWithCV< TallyStore > statWithCV, double level, int d, Chrono timer)
Similar to simulateRunsDefaultReport, but this one uses a vector of control variates.
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 String simulateRunsDefaultReportStudent(MonteCarloModelDouble model, int n, RandomStream stream, Tally statValue, Chrono timer)
A short-hand equivalent for simulateRunsDefaultReportStudent (model, n, stream, statValue,...
static String simulateRunsDefaultReportStudent(MonteCarloModelDouble model, int n, RandomStream stream, Tally statValue, double level, int d)
In this version, there is no need to provide a Chrono; it is created inside.
static void simulateRuns(MonteCarloModelDouble model, int n, RandomStream stream, Tally statValue)
Performs n simulation runs of model using stream and collects statistics in statValue.
static void simulFDReplicatesCRN(MonteCarloModelDouble model1, MonteCarloModelDouble model2, double delta, int n, RandomStream stream, Tally statDiff)
Performs n simulation runs to estimate the difference in performance between model2 and model1,...
static void simulateRunsCV(MonteCarloModelCV model, int n, RandomStream stream, TallyStore statX, TallyStore statC)
Performs n runs using stream and collects statistics for a model with a single real-valued control va...
static void simulFDReplicatesIRN(MonteCarloModelDouble model1, MonteCarloModelDouble model2, double delta, int n, RandomStream stream, Tally statDiff)
Similar to simulFDReplicatesCRN, but using independent random numbers (IRN) across the two models.
static void simulateRunsCV(MonteCarloModelCV model, int n, RandomStream stream, double[] mean, double[] variance)
Performs n runs using stream and collects statistics for a model with a single real-valued control va...
static String simulateRunsDefaultReportStudent(MonteCarloModelDouble model, int n, RandomStream stream, Tally statValue)
A short-hand equivalent for simulateRunsDefaultReportStudent (model, n, stream, statValue,...
static void simulateRunsCV(MonteCarloModelCV model, int n, RandomStream stream, ListOfTalliesWithCV< TallyStore > statWithCV)
Performs n runs of model using stream and collects statistics for a model with a vector of control va...
static String simulateRunsDefaultReportCV(MonteCarloModelCV model, int n, RandomStream stream, ListOfTalliesWithCV< TallyStore > statWithCV, double level, int d)
In this one the timer is created internally (and cannot be accessed externally).
static String simulateRunsDefaultReportCV(MonteCarloModelCV model, int n, RandomStream stream, double[] mean, double[] variance, double level, int d, Chrono timer)
This one uses a single real-valued CV, as in simulateRunsCV.
static void simulateRuns(MonteCarloModelDoubleArray model, int n, RandomStream stream, ListOfTallies<? extends Tally > statValueList)
Similar to simulateRuns(MonteCarloModelDouble, int, RandomStream, Tally) but for a model of type Mont...
This class is a variant of Tally for which the individual observations are stored in a list implement...
double covariance(TallyStore t2)
Returns the sample covariance of the observations contained in this tally, and the other tally t2.
void init()
Initializes the statistical collector.
void add(double x)
Adds one observation x to 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.
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.
void varianceWithCV(double[] v)
Fills the given array with the variance of each component of.
void covarianceWithCV(DoubleMatrix2D covCV)
Computes the sample covariance of by replacing ,.
double averageWithCV(int i)
Returns the average of the th component of.
void confidenceIntervalStudentWithCV(int i, double level, double[] centerAndRadius)
Computes a confidence interval for the th component of.
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...
void init()
Initializes this AbstractChrono to zero.
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.
An extension of MonteCarloModelDouble that also implements a vector of control variates.
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.
void simulate(RandomStream stream)
Simulates the model for one run.
double[] getPerformance()
Recovers and returns the realization of the vector 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 resetStartSubstream()
Reinitializes the stream to the beginning of its current substream: