SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
Nonuniform.java
1package tutorial;
2
3import umontreal.ssj.rng.*;
4import java.io.*;
5import umontreal.ssj.charts.HistogramChart;
6import umontreal.ssj.probdist.*;
7import umontreal.ssj.randvar.*;
8import umontreal.ssj.stat.*;
9
10public class Nonuniform {
11 // The parameter values are hardcoded here to simplify the program.
12 double lambda = 5.0;
13 double p = 0.2;
14 double alpha = 2.0;
15 double beta = 1.0;
16 double mu = 5.0;
17 double sigma = 0.5;
18
19 RandomStream stream = new LFSR113();
20 RandomVariateGenInt genN = new RandomVariateGenInt(stream, new PoissonDist(lambda)); // For N
21 RandomVariateGen genY = new GammaAcceptanceRejectionGen(stream, new GammaDist(alpha, beta)); // For Y_j
22 RandomVariateGen genW = new RandomVariateGen(stream, new LognormalDist(mu, sigma)); // For W_j
23
24 // Generates and returns X.
25 public double simulate() {
26 int N;
27 int M;
28 int j;
29 double X = 0.0;
30 N = genN.nextInt();
31 M = GeometricDist.inverseF(p, stream.nextDouble()); // Uses static method
32 for (j = 0; j < N; j++)
33 X += genY.nextDouble();
34 for (j = 0; j < M; j++)
35 X += genW.nextDouble();
36 return X;
37 }
38
39 // Performs n indep. runs and collects statistics in statX.
40 public void simulateRuns(int n, TallyStore statX) {
41 for (int i = 0; i < n; i++)
42 statX.add(simulate());
43 }
44
45 public static void main(String[] args) throws IOException {
46 int n = 100000;
47 TallyStore statX = new TallyStore(n); // To store the n observations of X.
48 (new Nonuniform()).simulateRuns(n, statX); // Simulate X n times.
49 System.out.println(statX.report(0.95, 1));
50
51 // Compute and print the empirical quantiles.
52 statX.quickSort();
53 double[] data = statX.getArray(); // The sorted observations.
54 System.out.printf(" 0.10 quantile: %9.1f%n", data[(int) (0.10 * n)]);
55 System.out.printf(" 0.50 quantile: %9.1f%n", data[(int) (0.50 * n)]);
56 System.out.printf(" 0.90 quantile: %9.1f%n", data[(int) (0.90 * n)]);
57 System.out.printf(" 0.99 quantile: %9.1f%n", data[(int) (0.99 * n)]);
58
59 // Make a histogram of the empirical distribution of X.
60 HistogramChart hist = new HistogramChart("Histogram of distribution of $X$", "Values of $X$", "Frequency",
61 statX.getArray(), n);
62 double[] bounds = { 0, 4000, 0, 25000 }; // Range of x and y to be displayed.
63 hist.setManualRange(bounds);
64 (hist.getSeriesCollection()).setBins(0, 40, 0, 4000); // 40 bins over [0, 4000].
65 hist.view(800, 500); // View on screen.
66
67 // Make a Latex file that contains the histogram.
68 String histLatex = hist.toLatex(12.0, 8.0); // Width and height of plot in cm.
69 Writer file = new FileWriter("src/main/docs/examples/tutorial/NonuniformHist.tex");
70 file.write(histLatex);
71 file.close();
72 }
73}
Provides tools to create and manage histograms.
HistogramSeriesCollection getSeriesCollection()
Returns the chart’s dataset.
JFrame view(int width, int height)
Displays chart on the screen using Swing.
String toLatex(double width, double height)
Exports the chart to a LaTeX source code using PGF/TikZ.
void setManualRange(double[] range)
Sets the and ranges of the chart using the format: range = [xmin, xmax, ymin, ymax].
Definition XYChart.java:261
Extends the class ContinuousDistribution for the gamma distribution tjoh95a  (page 337) with shape pa...
Extends the class DiscreteDistributionInt for the geometric distribution slaw00a  (page 322) with par...
static int inverseF(double p, double u)
Computes the inverse of the geometric distribution, given by ( FInvgeom ).
Extends the class ContinuousDistribution for the lognormal distribution tjoh95a .
Extends the class DiscreteDistributionInt for the Poisson distribution slaw00a  (page 325) with mean.
This class implements gamma random variate generators using a method that combines acceptance-rejecti...
This is the base class for all generators of discrete random variates over the set of integers.
This is the base class for all random variate generators over the real line.
Extends RandomStreamBase using a composite linear feedback shift register (LFSR) (or Tausworthe) RNG ...
Definition LFSR113.java:47
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.
void add(double x)
Adds one observation x to this probe.
String report()
Returns a formatted string that contains a report on this probe.
Definition Tally.java:397
This interface defines the basic structures to handle multiple streams of uniform (pseudo)random numb...