SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
MarkovChain.java
1package umontreal.ssj.markovchainrqmc;
2
3import umontreal.ssj.stat.Tally;
4import umontreal.ssj.rng.*;
5import umontreal.ssj.util.Chrono;
6import umontreal.ssj.util.PrintfFormat;
7import umontreal.ssj.hups.*;
8
34public abstract class MarkovChain implements Cloneable {
35
37 public int numSteps; // Number of steps
38 protected boolean stopped = false;
39
43
49 public abstract void initialState();
50
56 public abstract void nextStep(RandomStream stream);
57
62 public abstract double getPerformance();
63
67
71
75 public Object clone() throws CloneNotSupportedException {
76 MarkovChain o = null;
77 try {
78 o = (MarkovChain) super.clone();
79 } catch (CloneNotSupportedException e) {
80 System.err.println("This MarkovChain cannot be cloned");
81 }
82 return o;
83 }
84
88 public boolean hasStopped() {
89 return stopped;
90 }
91
96 public void simulSteps(int numSteps, RandomStream stream) {
98 this.numSteps = numSteps;
99 int step = 0;
100 while (step < numSteps && !hasStopped()) {
101 nextStep(stream);
102 ++step;
103 }
104 }
105
111 public void simulSteps(RandomStream stream) {
112 simulSteps(Integer.MAX_VALUE, stream);
113 }
114
120 public void simulRuns(int n, int numSteps, RandomStream stream, Tally statRuns) {
121 statRuns.init();
122 for (int i = 0; i < n; i++) {
123 simulSteps(numSteps, stream);
124 statRuns.add(getPerformance());
125 }
126 }
127
133 public void simulRunsWithSubstreams(int n, int numSteps, RandomStream stream, Tally statRuns) {
134 statRuns.init();
135 stream.resetStartStream();
136 for (int i = 0; i < n; i++) {
137 simulSteps(numSteps, stream);
138 statRuns.add(getPerformance());
139 stream.resetNextSubstream();
140 }
141 }
142
147 public double simulMC(int n, int numSteps) {
148 Tally statRuns = new Tally();
149 simulRunsWithSubstreams(n, numSteps, new MRG32k3a(), statRuns);
150 return statRuns.average();
151 }
152
156 public double simulMC(int n) {
157 return simulMC(n, Integer.MAX_VALUE);
158 }
159
164 public void simulRepMC(int n, int numSteps, int m, Tally t) {
165 for (int rep = 0; rep < m; ++rep) {
166 t.add(simulMC(n, numSteps));
167 }
168 }
169
173 public void simulRepMC(int n, int m, Tally t) {
174 simulRepMC(n, Integer.MAX_VALUE, m, t);
175 }
176
187 public void simulRQMC(PointSet p, int m, int numSteps, PointSetRandomization rand, Tally statReps) {
188 statReps.init();
189 Tally statRuns = new Tally(); // Used within the runs.
190 int n = p.getNumPoints(); // Number of points.
191 RandomStream stream = p.iterator();
192 for (int rep = 0; rep < m; rep++) {
193 p.randomize(rand);
194 simulRunsWithSubstreams(n, numSteps, stream, statRuns);
195 statReps.add(statRuns.average());
196 }
197 }
198
202 public String simulRunsFormat(int n, int numSteps, RandomStream stream, Tally statRuns) {
203 timer.init();
204 simulRuns(n, numSteps, stream, statRuns);
205 StringBuffer sb = new StringBuffer("----------------------------------------------" + PrintfFormat.NEWLINE);
206 sb.append("MC simulations:" + PrintfFormat.NEWLINE);
207 sb.append(" Number of runs n = " + n + PrintfFormat.NEWLINE);
208 sb.append(formatResults(statRuns));
209 sb.append(" CPU Time = " + timer.format() + PrintfFormat.NEWLINE);
210 return sb.toString();
211 }
212
217 public String simulRunsWithSubstreamsFormat(int n, int numSteps, RandomStream stream, Tally statRuns) {
218 timer.init();
219 simulRunsWithSubstreams(n, numSteps, stream, statRuns);
220 StringBuffer sb = new StringBuffer("----------------------------------------------" + PrintfFormat.NEWLINE);
221 sb.append("MC simulations with substreams:" + PrintfFormat.NEWLINE);
222 sb.append(" Number of runs n = " + n + PrintfFormat.NEWLINE);
223 sb.append(formatResults(statRuns));
224 sb.append(" CPU Time = " + timer.format() + PrintfFormat.NEWLINE);
225 return sb.toString();
226 }
227
231 public String simulRQMCFormat(PointSet p, int m, int numSteps, PointSetRandomization rand, Tally statReps) {
232 timer.init();
233 simulRQMC(p, m, numSteps, rand, statReps);
234 int n = p.getNumPoints();
235 StringBuffer sb = new StringBuffer("----------------------------------------------" + PrintfFormat.NEWLINE);
236 sb.append("RQMC simulations:" + PrintfFormat.NEWLINE + PrintfFormat.NEWLINE);
237 sb.append(p.toString());
238 sb.append(PrintfFormat.NEWLINE + " Number of indep. randomization, m = " + m + PrintfFormat.NEWLINE);
239 sb.append(" Number of points n = " + n + PrintfFormat.NEWLINE);
240 sb.append(formatResultsRQMC(statReps, n));
241 sb.append(" CPU Time = " + timer.format() + PrintfFormat.NEWLINE);
242 return sb.toString();
243 }
244
249 public String testImprovementRQMCFormat(PointSet p, int m, int numSteps, PointSetRandomization rand, double varMC,
250 Tally statReps) {
251 // Removed next line because numSteps may be infinite!
252 // p.randomize (0, numSteps * dimPerStep, noise);
253 StringBuffer sb = new StringBuffer(simulRQMCFormat(p, m, numSteps, rand, statReps));
254 double var = p.getNumPoints() * statReps.variance();
255 sb.append(" Variance ratio: " + PrintfFormat.format(15, 10, 4, varMC / var) + PrintfFormat.NEWLINE);
256 return sb.toString();
257 }
258
263 public String formatResults(Tally stat) {
264 StringBuffer sb = new StringBuffer(" Average value = ");
265 sb.append(PrintfFormat.format(12, 9, 5, stat.average()) + PrintfFormat.NEWLINE);
266 sb.append(" Variance = ");
267 sb.append(PrintfFormat.format(12, 9, 5, stat.variance()) + PrintfFormat.NEWLINE);
268 sb.append(stat.formatCIStudent(0.9, 7));
269 return sb.toString();
270 }
271
276 public String formatResultsRQMC(Tally stat, int numPoints) {
277 StringBuffer sb = new StringBuffer(" Average value = ");
278 sb.append(PrintfFormat.format(12, 9, 5, stat.average()) + PrintfFormat.NEWLINE);
279 sb.append(" Variance * numPoints = ");
280 sb.append(PrintfFormat.format(12, 9, 5, numPoints * stat.variance()) + PrintfFormat.NEWLINE);
281 sb.append(stat.formatCIStudent(0.9, 7));
282 return sb.toString();
283 }
284
285}
286
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 defines a generic Markov chain and provides basic tools to simulate it for a given number ...
boolean hasStopped()
Tells if the chain has stopped.
abstract void nextStep(RandomStream stream)
Simulates one more step of the chain, from its current state, using stream for the randomness.
abstract void initialState()
Sets the Markov chain to its (deterministic) initial state and initializes the collectors for the per...
void simulRepMC(int n, int numSteps, int m, Tally t)
Perform n runs and compute the average, reapeat m times and return the stats in t.
String formatResultsRQMC(Tally stat, int numPoints)
Returns a string containing the mean, the variance multiplied by numPoints, and a 90% confidence inte...
abstract double getPerformance()
Returns the performance measure (total or average cost or gain) so far, for the current simulation ru...
String simulRQMCFormat(PointSet p, int m, int numSteps, PointSetRandomization rand, Tally statReps)
Same as simulRQMC but also returns the results as a formatted string.
void simulSteps(RandomStream stream)
Starts a new simulation run and simulates until the stopping time is reached, using the given stream.
double simulMC(int n)
Perform n runs, each one until the chain stops.
String simulRunsFormat(int n, int numSteps, RandomStream stream, Tally statRuns)
Same as simulRuns but also returns the results as a formatted string.
double simulMC(int n, int numSteps)
Perform n simulation runs of the chain, each for numSteps steps, and returns average.
void simulRepMC(int n, int m, Tally t)
Same as previous one, but run the chains until they stop.
void simulSteps(int numSteps, RandomStream stream)
Starts a new simulation run and simulates numSteps steps of the Markov chain or until the chain stops...
void simulRQMC(PointSet p, int m, int numSteps, PointSetRandomization rand, Tally statReps)
Performs m independent replicates of simulation runs of the chain using a RQMC point set,...
String formatResults(Tally stat)
Returns a string containing the mean, the variance, and a 90% confidence interval for stat.
String simulRunsWithSubstreamsFormat(int n, int numSteps, RandomStream stream, Tally statRuns)
Same as simulRunsWithSubstreams but also returns the results as a formatted string.
void simulRunsWithSubstreams(int n, int numSteps, RandomStream stream, Tally statRuns)
Same as simulRuns, except that the stream is first reset to its initial seed and then reset to the fi...
void simulRuns(int n, int numSteps, RandomStream stream, Tally statRuns)
Performs n simulation runs of the chain, for numSteps steps per run, using the given stream.
Object clone()
Returns a clone of the chain.
String testImprovementRQMCFormat(PointSet p, int m, int numSteps, PointSetRandomization rand, double varMC, Tally statReps)
Similar to simulRQMCFormat, but also gives the variance improvement factor with respect to MC.
Extends the abstract class RandomStreamBase by using as a backbone (or main) generator the combined m...
Definition MRG32k3a.java:46
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
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
The Chrono class extends the umontreal.ssj.util.AbstractChrono class and computes the CPU time for th...
Definition Chrono.java:37
static Chrono createForSingleThread()
Creates a Chrono instance adapted for a program using a single thread.
Definition Chrono.java:60
This class acts like a StringBuffer which defines new types of append methods.
static final String NEWLINE
End-of-line symbol or line separator.
static String format(long x)
Same as d(0, 1, x).
This interface is for a randomization that can be used to randomize a umontreal.ssj....
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 .
Tools for Collecting Statistics and computing estimators and confidence intervals.