1package umontreal.ssj.markovchainrqmc;
3import umontreal.ssj.stat.PgfDataTable;
4import umontreal.ssj.stat.Tally;
5import umontreal.ssj.util.*;
6import umontreal.ssj.util.multidimsort.*;
7import umontreal.ssj.hups.*;
8import umontreal.ssj.charts.*;
9import umontreal.ssj.functionfit.LeastSquares;
10import java.io.IOException;
12import java.io.FileWriter;
13import java.lang.reflect.Array;
75 protected T baseChain;
79 protected double[] performances;
82 protected int sortCoordPts = 0;
95 this.baseChain = baseChain;
106 this.baseChain = baseChain;
108 randomization = rand;
116 @SuppressWarnings(
"unchecked")
119 final T[] c = (T[]) Array.newInstance(baseChain.getClass(), n);
123 performances =
new double[n];
124 for (
int i = 0; i < n; i++) {
126 chains[i] = (T) baseChain.clone();
127 }
catch (CloneNotSupportedException e) {
128 System.err.println(
"ArrayOfComparableChains:");
142 for (T mc : chains) {
144 performances[i] = mc.getPerformance();
167 randomization = rand;
174 return randomization;
211 if (sortCoordPts > 0) {
213 throw new IllegalArgumentException(
"p is not a CachedPointSet.");
214 if (sortCoordPts > 1)
222 for (T mc : chains) {
223 if (mc.hasStopped()) {
232 performances[i] = mc.getPerformance();
269 int numNotStopped = n;
272 while (step < numSteps && numNotStopped > 0) {
273 if (numNotStopped == n)
274 sort.
sort(chains, 0, n);
278 if (sortCoordPts > 0) {
280 throw new IllegalArgumentException(
"p is not a CachedPointSet.");
281 if (sortCoordPts > 1)
289 for (T mc : chains) {
290 if (mc.hasStopped()) {
299 performances[i] = mc.getPerformance();
334 double sumPerf = 0.0;
335 for (
int i = 0; i < n; ++i) {
336 sumPerf += performances[i];
347 int numSteps,
int m,
Tally statReps) {
350 for (
int rep = 0; rep < m; rep++) {
362 int sortCoordPts,
int numSteps,
int m,
Tally statReps) {
367 for (
int rep = 0; rep < m; rep++) {
370 StringBuffer sb =
new StringBuffer(
"----------------------------------------------" +
PrintfFormat.
NEWLINE);
373 sb.append(
" Number of indep copies m = " + m);
375 sb.append(baseChain.formatResultsRQMC(statReps, n));
377 return sb.toString();
386 StringBuffer sb =
new StringBuffer(
388 return sb.toString();
405 int sortCoordPts,
int numSteps,
int m,
double varMC, String filenamePlot, String methodLabel) {
407 int numSets = pointSets.length;
409 double[] logn =
new double[numSets];
410 double[] variance =
new double[numSets];
411 double[] logVariance =
new double[numSets];
414 StringBuffer str =
new StringBuffer(
"\n\n --------------------------");
415 str.append(methodLabel +
"\n MC Variance : " + varMC +
"\n\n");
418 for (
int i = 0; i < numSets; ++i) {
419 initTime = System.currentTimeMillis();
421 str.append(
"n = " + n +
"\n");
425 logVariance[i] =
Num.
log2(variance[i]);
426 str.append(
" Average = " + statPerf.
average() +
"\n");
427 str.append(
" VRF = " + varMC / (n * variance[i]) +
"\n");
428 str.append(formatTime((System.currentTimeMillis() - initTime) / 1000.) +
"\n");
431 double regSlope = slope(logn, logVariance, numSets);
432 str.append(
"Regression slope (log) for variance = " + regSlope +
"\n\n");
435 if (filenamePlot !=
null)
437 Writer file =
new FileWriter(filenamePlot +
".tex");
440 chart.add(logn, logVariance);
441 file.write(chart.toLatex(12, 8));
443 }
catch (IOException e) {
446 return str.toString();
464 int m,
double varMC, String filenamePlot, String methodLabel) {
465 int numSets = rqmcPts.length;
467 double[] logn =
new double[numSets];
468 double[] variance =
new double[numSets];
469 double[] logVariance =
new double[numSets];
472 StringBuffer str =
new StringBuffer(
"\n\n --------------------------");
473 str.append(methodLabel +
"\n MC Variance per Run : " + varMC / (
double) n +
"\n\n");
476 for (
int i = 0; i < numSets; ++i) {
479 for (
int j = 0; j < numSteps; ++j)
481 initTime = System.currentTimeMillis();
483 str.append(
"n = " + n +
"\n");
488 logVariance[i] =
Num.
log2(variance[i]);
489 str.append(
" Average = " + statPerf.
average() +
"\n");
490 str.append(
" RQMC Variance : " + variance[i] +
"\n\n");
491 str.append(
" VRF = " + varMC / (n * variance[i]) +
"\n");
492 str.append(formatTime((System.currentTimeMillis() - initTime) / 1000.) +
"\n");
495 double regSlope = slope(logn, logVariance, numSets);
496 str.append(
"Regression slope (log) for variance = " + regSlope +
"\n\n");
498 String[] tableField = {
"log(n)",
"log(Var)" };
499 double[][] data =
new double[numSets][2];
500 for (
int s = 0; s < numSets; s++) {
501 data[s][0] = logn[s];
502 data[s][1] = logVariance[s];
506 if (filenamePlot !=
null)
513 FileWriter fileIV =
new FileWriter(filenamePlot +
"_" +
"VAr.tex");
514 fileIV.write(plotIV);
516 }
catch (IOException e) {
520 tableField =
new String[] {
"step",
"average",
"variance" };
521 data =
new double[numSteps][3];
522 for (
int s = 0; s < numSteps; s++) {
528 if (filenamePlot !=
null)
538 FileWriter fileIV =
new FileWriter(filenamePlot +
"_" +
"MeanPerStep.tex");
539 fileIV.write(plotMean);
542 fileIV =
new FileWriter(filenamePlot +
"_" +
"VariancePerStep.tex");
543 fileIV.write(plotVar);
545 }
catch (IOException e) {
549 return str.toString();
562 while (j >= 0 && chains[j].hasStopped())
564 while (i < n && !chains[i].hasStopped())
567 while (!chains[i].hasStopped())
569 while (chains[j].hasStopped())
572 chains[i] = chains[j];
575 sort.
sort(chains, 0, i);
584 savedSort.sort(chains, 0, n);
588 public String formatTime(
double time) {
589 int second, hour, min, centieme;
590 hour = (int) (time / 3600.0);
592 time -= ((double) hour * 3600.0);
594 min = (int) (time / 60.0);
596 time -= ((double) min * 60.0);
599 centieme = (int) (100.0 * (time - (
double) second) + 0.5);
600 return String.valueOf(hour) +
":" + min +
":" + second +
"." + centieme;
605 public double slope(
double[] x,
double[] y,
int n) {
609 double[] x2 =
new double[n], y2 =
new double[n];
610 for (
int i = 0; i < n; ++i) {
614 return LeastSquares.calcCoefficients(x2, y2, 1)[1];
Provides tools to create and manage curve plots.
This container class caches a point set by precomputing and storing all the points locally in an arra...
This abstract class represents a general point set.
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.
int getNumPoints()
Returns the number of points in the associated point set.
PointSetRandomization getRandomization()
Returns the internal umontreal.ssj.hups.PointSetRandomization.
double[] getPerformances()
Returns the vector for performances for the chains.
String varianceImprovementFormat(double varRQMC, double varMC)
Returns a string that reports the the ratio of MC variance per run varMC over the RQMC variance per r...
int simulOneStepArrayRQMC(PointSet p, PointSetRandomization rand, MultiDimSort< T > sort, int sortCoordPts)
Randomized the point set p and Simulates the copies of the chain, one step for each copy,...
int simulOneStepArrayRQMC(PointSet p)
This version uses the preselected randomization and sort, with sortCoordPts = 0.
Tally[] performancePerRun
Performance measure at each step of the chain.
double calcMeanPerf()
Computes and returns the mean performance of the chains.
double simulArrayRQMC(PointSet p, PointSetRandomization rand, MultiDimSort< T > sort, int numSteps)
This version assumes that sortCoordPts = 0, so that there is no need to sort the points at each step.
int getN()
Returns the number n of chains.
ArrayOfComparableChains(T baseChain, PointSetRandomization rand, MultiDimSort< T > sort)
Creates an array of the comparable chain baseChain.
void sortChains()
Sorts the chains using the stored.
void setRandomization(PointSetRandomization rand)
Sets the internal umontreal.ssj.hups.PointSetRandomization to rand.
void sortNotStoppedChains(MultiDimSort< T > sort)
Sorts the chains that have not stopped yet using the stored.
void setSort(MultiDimSort< T > sort)
Sets the internal umontreal.ssj.util.MultiDimSort to sort.
void initialStates()
Initializes the n copies (clones) of the chain baseChain to their initial state by calling initialSta...
void simulReplicatesArrayRQMC(PointSet p, PointSetRandomization rand, MultiDimSort< T > sort, int sortCoordPts, int numSteps, int m, Tally statReps)
Performs m independent replications of an array-RQMC simulation as in simulArrayRQMC.
ArrayOfComparableChains(T baseChain)
Creates an array of the comparable chain baseChain.
String testVarianceRateFormat(PointSet[] pointSets, PointSetRandomization rand, MultiDimSort< T > sort, int sortCoordPts, int numSteps, int m, double varMC, String filenamePlot, String methodLabel)
Performs an experiment to estimate the convergence rate of the RQMC variance as a function of ,...
double simulArrayRQMC(PointSet p, PointSetRandomization rand, MultiDimSort< T > sort, int sortCoordPts, int numSteps)
Simulates the copies of the chain, numSteps steps for each copy, using umontreal....
String simulReplicatesArrayRQMCFormat(PointSet p, PointSetRandomization rand, MultiDimSort< T > sort, int sortCoordPts, int numSteps, int m, Tally statReps)
Performs m independent replications of an array-RQMC simulation as in simulFormatArrayRQMC.
void makeCopies(int n)
Creates n copies (clones) of the chain baseChain and puts them in an array, ready for the array RQMC ...
String testVarianceRateFormat(RQMCPointSet[] rqmcPts, MultiDimSort< T > sort, int sortCoordPts, int numSteps, int m, double varMC, String filenamePlot, String methodLabel)
Same as above, but produces a plot of the mean and the variance as a function of the step.
double simulArrayRQMC(PointSet p, int numSteps)
This version assumes that sortCoordPts = 0 and uses the preset randomization and sort for the chains.
MultiDimSort< T > getSort()
Returns the saved umontreal.ssj.util.MultiDimSort.
T[] getChains()
Returns the underlying array of n MarkovChainComparable.
A subclass of MarkovChain for which there is a total ordering between the states, induced by the impl...
Represents a data table which has a name, a number of observations (rows), a number of fields (column...
String drawPgfPlotSingleCurve(String title, String axistype, String xaxis, String yaxis, int logbasis, String axisoptions, String plotoptions)
Returns a String that contains a complete tikzpicture for the pgfplot package, showing the field j2 a...
double average()
Returns the average value of the observations since the last initialization.
double variance()
Returns the sample variance of the observations since the last initialization.
void init()
Initializes the statistical collector.
void add(double x)
Gives a new observation x to the statistical collector.
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...
static Chrono createForSingleThread()
Creates a Chrono instance adapted for a program using a single thread.
This class provides various constants and methods to compute numerical quantities such as factorials,...
static double log2(double x)
Returns x .
This is the interface for iterators that permit one to go through the points of a PointSet and the su...
void resetCurPointIndex()
Equivalent to setCurPointIndex(0).
void setCurCoordIndex(int j)
Sets the current coordinate index to , so that the next calls to nextCoordinate or nextCoordinates wi...
This interface is for a randomization that can be used to randomize a umontreal.ssj....
void resetNextSubstream()
Reinitializes the stream to the beginning of its next substream:
This interface is meant to be implemented by certain multivariate sorting algorithms that sort object...
void sort(T[] a, int iMin, int iMax)
Sorts the subarray of a made of the elements with indices from iMin to iMax-1.