SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
MonteCarloSampler.java
1package umontreal.ssj.mcqmctools.anova;
2
3import umontreal.ssj.stat.Tally;
4import umontreal.ssj.rng.RandomStream;
5import umontreal.ssj.hups.PointSetIterator;
6import umontreal.ssj.mcqmctools.*;
7
15public class MonteCarloSampler implements RandomSampler {
16
17 protected int nSamples;
18 protected RandomStream stream;
19
20 public MonteCarloSampler(int nSamples) {
21 this.nSamples = nSamples;
22 }
23
24 public MonteCarloSampler(int nSamples, RandomStream stream) {
25 this.nSamples = nSamples;
26 this.stream = stream;
27 }
28
31 return stream;
32 }
33
36 return 1;
37 }
38
40 public int getNumSamples() {
41 return nSamples;
42 }
43
48 public void setNumSamples(int nSamples) {
49 this.nSamples = nSamples;
50 }
51
58 public void setStream(RandomStream stream) {
59 this.stream = stream;
60 }
61
67
68 PointSetIterator psit = null;
69 if (stream instanceof PointSetIterator)
70 psit = (PointSetIterator) stream;
71
72 for (int i = 0; i < nSamples; i++) {
73 model.simulate(stream);
74 collector.observe(model.getPerformance());
75 if (psit != null)
76 psit.resetToNextPoint();
77 }
78 }
79
84 public void simulateRuns(MonteCarloModelDouble model, Tally collector) {
85
86 boolean isPointSet = (stream instanceof PointSetIterator);
87
88 for (int i = 0; i < nSamples; i++) {
89 model.simulate(stream);
90 collector.add(model.getPerformance());
91 if (isPointSet)
92 ((PointSetIterator) stream).resetToNextPoint();
93 }
94 }
95
96 @Override
97 public String toString() {
98 String s = "Monte Carlo Sampler [samples=" + getNumSamples() + "]";
99 if (getStream() != null)
100 s += " [stream=" + getStream().getClass().getSimpleName() + "]";
101 return s;
102 }
103}
void setNumSamples(int nSamples)
Sets the number of samples.
public< E > void simulateRuns(MonteCarloModel<? extends E > model, ObservationCollector< E > collector)
void simulateRuns(MonteCarloModelDouble model, Tally collector)
int getNumSimulationsPerSample()
Returns the number of times the model is simulated each time a sample is produced.
int getNumSamples()
Returns the number of samples produced each time the simulateRuns() method is called.
RandomStream getStream()
Returns the currently used random stream.
void setStream(RandomStream stream)
Use stream to produce random points.
A subclass of StatProbe.
Definition Tally.java:47
void add(double x)
Gives a new observation x to the statistical collector.
Definition Tally.java:109
This is the interface for iterators that permit one to go through the points of a PointSet and the su...
int resetToNextPoint()
Increases the current point index by 1 and returns its new value.
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.
double getPerformance()
Recovers and returns the realization of the performance measure, of type double.
An interface for a simple simulation model for which Monte Carlo (MC) or RQMC experiments are to be p...
void simulate(RandomStream stream)
Simulates the model for one run, using the given stream.
E getPerformance()
Recovers and returns the realization of the performance measure, of type E.
This interface defines the basic structures to handle multiple streams of uniform (pseudo)random numb...