import java.io.*; import umontreal.ssj.charts.HistogramChart; import umontreal.ssj.charts.EmpiricalChart; import umontreal.ssj.rng.*; import umontreal.ssj.stat.TallyStore; /** * Here we compute the empirical distribution of the shortest path lengths and * we construct a histogram. */ public class San13Dist extends San13 { // The constructor reads link length distributions in a file. public San13Dist(String fileName) throws IOException { super(0.0, fileName); } public double getValue() { return maxPath; } public String toString() { return "SAN network with 9 nodes and 13 links, from Elmaghraby (1977)\n" + "Estimate distribution of length of longest path.\n"; } // Aggregates the observations of t1 into gsize groups, takes the average // in each group, and these averages become the observations of the new // returned TallyStore t2. public TallyStore aggregate (TallyStore t1, int gsize) { int numObs = t1.numberObs(); int numGroups = numObs / gsize; double sum; TallyStore t2 = new TallyStore (numGroups); for (int i = 0; i < numGroups; i++) { sum = 0.0; for (int j = 0; j < gsize; j++) sum += t1.getArray()[gsize * i + j]; sum /= gsize; t2.add(sum); } // This is if gsize does not divide numObs. int rest = numObs - numGroups * gsize; if (rest > 0) { sum = 0.0; for (int j = 0; j < rest; j++) sum += t1.getArray()[gsize * numGroups + j]; sum /= rest; t2.add(sum); } return t2; } public static void main(String[] args) throws IOException { int n = 100000; int groupSize = 100; San13Dist san = new San13Dist("san13a.dat"); TallyStore statT = new TallyStore("TallyStore for SAN13 example"); MonteCarloExperiment.simulateRunsDefaultReport(san, n, new LFSR113(), statT); statT.quickSort(); Writer file; TallyStore statTaggregated = statT; if (groupSize > 1) statTaggregated = san.aggregate (statT, groupSize); double [] aggreg = statTaggregated.getArray(); EmpiricalChart cdf = new EmpiricalChart("Empirical cdf of $T$", "Values of $T$", "cdf", aggreg, statTaggregated.numberObs()); double[] bounds2 = { 0, 200, 0, 1.0 }; cdf.setManualRange(bounds2); cdf.view(800, 500); String cdfLatex = cdf.toLatex(12.0, 8.0); file = new FileWriter("san13cdf.tex"); file.write(cdfLatex); file.close(); HistogramChart hist = new HistogramChart("Distribution of $T$", "Values of $T$", "Frequency", statT.getArray(), n); double[] bounds = { 0, 200, 0, 12000 }; hist.setManualRange(bounds); (hist.getSeriesCollection()).setBins(0, 40, 0, 200); hist.view(800, 500); String histLatex = hist.toLatex(12.0, 8.0); file = new FileWriter("san13chart.tex"); file.write(histLatex); file.close(); // Print p-th quantile double p = 0.99; int index = (int)Math.round (p * n); double xip = statT.getArray()[index]; System.out.printf("%5.3g -th quantile: %9.6g \n", p, xip); } }