| 1 | package de.uka.ipd.sdq.sensorframework.visualisation.statistics.views; |
| 2 | |
| 3 | import java.util.Collection; |
| 4 | |
| 5 | import de.uka.ipd.sdq.sensorframework.entities.Measurement; |
| 6 | import de.uka.ipd.sdq.sensorframework.entities.Sensor; |
| 7 | import de.uka.ipd.sdq.sensorframework.entities.SensorAndMeasurements; |
| 8 | import de.uka.ipd.sdq.sensorframework.entities.TimeSpanMeasurement; |
| 9 | import de.uka.ipd.sdq.sensorframework.entities.TimeSpanSensor; |
| 10 | import de.uka.ipd.sdq.sensorframework.visualisation.rvisualisation.views.AbstractHtmlReportView; |
| 11 | import de.uka.ipd.sdq.statistics.IBatchAlgorithm; |
| 12 | import de.uka.ipd.sdq.statistics.PhiMixingBatchAlgorithm; |
| 13 | import de.uka.ipd.sdq.statistics.StaticBatchAlgorithm; |
| 14 | import de.uka.ipd.sdq.statistics.estimation.ConfidenceInterval; |
| 15 | import de.uka.ipd.sdq.statistics.estimation.SampleMeanEstimator; |
| 16 | import de.uka.ipd.sdq.statistics.independence.RunUpTest; |
| 17 | |
| 18 | /** |
| 19 | * Report that calls {@link PhiMixingBatchAlgorithm} to determine the confidence intervals, |
| 20 | * also considering independence of the observations. The {@link RunUpTest} is used |
| 21 | * as the default to test the data sequence for independence. |
| 22 | * @author martens |
| 23 | * @see PhiMixingBatchAlgorithm |
| 24 | * @see RunUpTest |
| 25 | */ |
| 26 | public class ConfidenceIntervalsHtmlReportView extends AbstractHtmlReportView { |
| 27 | |
| 28 | @Override |
| 29 | public void setInput(Collection<SensorAndMeasurements> c) { |
| 30 | |
| 31 | |
| 32 | int batcheSize = 100; |
| 33 | |
| 34 | if (c.isEmpty()) { |
| 35 | browser.setText("<html><body><h1>Error! </h1>At least " |
| 36 | + "the measurements for one sensor must be " |
| 37 | + "available!</body></html>"); |
| 38 | } else { |
| 39 | String browserText = "<html><body><h1>Confidence intervals for mean values of sensors</h1>"; |
| 40 | |
| 41 | // TODO: make alpha configurable. |
| 42 | double alpha = 0.9; |
| 43 | |
| 44 | |
| 45 | for (SensorAndMeasurements sensorAndMeasurements : c) { |
| 46 | Sensor sensor = sensorAndMeasurements.getSensor(); |
| 47 | if (sensor instanceof TimeSpanSensor){ |
| 48 | PhiMixingBatchAlgorithm statisticChecker = new PhiMixingBatchAlgorithm(); |
| 49 | |
| 50 | for (Measurement m : sensorAndMeasurements.getMeasurements()) { |
| 51 | TimeSpanMeasurement t = (TimeSpanMeasurement)m; |
| 52 | statisticChecker.offerSample(t.getTimeSpan()); |
| 53 | } |
| 54 | browserText += "<h2>Sensor "+sensor.getSensorName()+"</h2>"; |
| 55 | browserText += "<p>Number of observations: "+sensorAndMeasurements.getMeasurements().size()+"<br>"; |
| 56 | |
| 57 | browserText += "<h3>Results of PhiMixingBatchAlgorithm</h3>"; |
| 58 | browserText = evaluateBatchAlgorithm(browserText, alpha, |
| 59 | sensorAndMeasurements, statisticChecker); |
| 60 | |
| 61 | |
| 62 | StaticBatchAlgorithm staticStatisticChecker = new StaticBatchAlgorithm(batcheSize,0); |
| 63 | |
| 64 | for (Measurement m : sensorAndMeasurements.getMeasurements()) { |
| 65 | TimeSpanMeasurement t = (TimeSpanMeasurement)m; |
| 66 | staticStatisticChecker.offerSample(t.getTimeSpan()); |
| 67 | } |
| 68 | |
| 69 | browserText += "<h3>Results of Plain Batch Means Algorithm (batch size "+batcheSize+")</h3>"; |
| 70 | browserText = evaluateBatchAlgorithm(browserText, alpha, |
| 71 | sensorAndMeasurements, staticStatisticChecker); |
| 72 | |
| 73 | /*better not use this as it will load all results of this sensor in memory. |
| 74 | StaticBatchAlgorithm singleStatisticChecker = new StaticBatchAlgorithm(1,0); |
| 75 | |
| 76 | for (Measurement m : sensorAndMeasurements.getMeasurements()) { |
| 77 | TimeSpanMeasurement t = (TimeSpanMeasurement)m; |
| 78 | singleStatisticChecker.offerSample(t.getTimeSpan()); |
| 79 | } |
| 80 | |
| 81 | |
| 82 | * browserText += "<h3>Results of Plain Confidence Interval Analysis on Single Samples</h3>"; |
| 83 | browserText = evaluateBatchAlgorithm(browserText, alpha, |
| 84 | sensorAndMeasurements, statisticChecker);*/ |
| 85 | |
| 86 | } |
| 87 | } |
| 88 | browserText += "<h2>Explanations</h2>" + |
| 89 | "<h3>PhiMixingBatchAlgorithm</h3><small><p>Implements a batch means procedure based on phi-mixing conditions as described in [1]. " + |
| 90 | "Appropriate batch sizes and the number of batches are determined automatically.</p>" + |
| 91 | "<p>The procedure utilizes an independence test in order to build a so-called \"quasi " + |
| 92 | "independent\" (QI) sample sequence. By default the RunUpTest will be used. \"The aim " + |
| 93 | "of the QI method is to continue the simulation run until we have obtained a pre-specified " + |
| 94 | "number of essentially independent random samples by skipping highly correlated observations.\" [1] " + |
| 95 | "As soon as the QI sequence appears to be independent, the computed batches can be considered as valid. " + |
| 96 | "Samples in the QI sequence are only used to determine appropriate batch sizes. " + |
| 97 | "They are not used to compute the batch means! Instead, the batch means consist of all samples, " + |
| 98 | "regardless of statistical dependence.</p>"+ |
| 99 | "<p>The RunUpTest is implemented as described in [Donald E. Knuth: The Art of Computer Programming. Seminumerical Algorithms].</p>"+ |
| 100 | "<p>[1] E. Chen, W. Kelton: A Stopping Procedure based on Phi-Mixing Conditions. Proceedings of the 2000 Winter Simulation Conference.</p>" + |
| 101 | "</small>" + |
| 102 | "<h3>Simple Batch Means</h3>" + |
| 103 | "Simply takes batches of size " +batcheSize+" and calculates the confidence interval based on their means. Handle the results with care, as they may not be statistically valid (e.g. as a simulation stopping criterion), because this algorithm does not check the independence of single observations." + |
| 104 | " In particular, the reported mean itself may deviate because of rounding errors. "+ |
| 105 | "</body></html>"; |
| 106 | browser.setText(browserText); |
| 107 | } |
| 108 | |
| 109 | } |
| 110 | |
| 111 | private String evaluateBatchAlgorithm(String browserText, double alpha, |
| 112 | SensorAndMeasurements sensorAndMeasurements, |
| 113 | IBatchAlgorithm statisticChecker) { |
| 114 | ConfidenceInterval ci = null; |
| 115 | if (statisticChecker.hasValidBatches()){ |
| 116 | ci = new SampleMeanEstimator().estimateConfidence(statisticChecker.getBatchMeans(),alpha); |
| 117 | } |
| 118 | if (ci != null){ |
| 119 | browserText += "Mean value: "+ci.getMean() +"<br>"+ |
| 120 | " Confidence value alpha: "+ci.getLevel()+"<br>"+ |
| 121 | " Upper bound: "+ci.getUpperBound()+"<br>"+ |
| 122 | " Lower bound: "+ci.getLowerBound()+"<br>"+ |
| 123 | " </p>"; |
| 124 | } else { |
| 125 | // calculate mean manually |
| 126 | double sum = 0; |
| 127 | for (Measurement m : sensorAndMeasurements.getMeasurements()) { |
| 128 | TimeSpanMeasurement t = (TimeSpanMeasurement)m; |
| 129 | sum += t.getTimeSpan(); |
| 130 | } |
| 131 | double mean = sum / sensorAndMeasurements.getMeasurements().size(); |
| 132 | browserText += "Mean value: "+mean +"</p><p>"; |
| 133 | |
| 134 | browserText += "Not enough information to calulate confidence interval: No valid batches could be determined to calculate the confidence interval. Maybe warmup effects influence the results.</p>"; |
| 135 | } |
| 136 | return browserText; |
| 137 | } |
| 138 | |
| 139 | |
| 140 | |
| 141 | } |