| 1 | package de.uka.ipd.sdq.statistics.estimation; |
| 2 | |
| 3 | import java.util.List; |
| 4 | |
| 5 | import de.uka.ipd.sdq.probfunction.math.IContinousPDFFactory; |
| 6 | import de.uka.ipd.sdq.probfunction.math.IStudentTDistribution; |
| 7 | import de.uka.ipd.sdq.probfunction.math.apache.impl.PDFFactory; |
| 8 | |
| 9 | /** |
| 10 | * Estimator for the sample mean. |
| 11 | * |
| 12 | * @author Philipp Merkle |
| 13 | * |
| 14 | */ |
| 15 | public class SampleMeanEstimator implements IPointEstimator, IConfidenceEstimator { |
| 16 | |
| 17 | private IContinousPDFFactory pdfFactory; |
| 18 | |
| 19 | public SampleMeanEstimator() { |
| 20 | this(new PDFFactory()); |
| 21 | } |
| 22 | |
| 23 | public SampleMeanEstimator(IContinousPDFFactory pdfFactory) { |
| 24 | assert pdfFactory != null : "The passed PDF factory may not be null."; |
| 25 | this.pdfFactory = pdfFactory; |
| 26 | } |
| 27 | |
| 28 | @Override |
| 29 | public ConfidenceInterval estimateConfidence(List<Double> samples, |
| 30 | double level) { |
| 31 | int degreesOfFreedom = samples.size() - 1; |
| 32 | if (degreesOfFreedom > 0){ |
| 33 | IStudentTDistribution dist = this.pdfFactory.createStudentTDistribution(degreesOfFreedom); |
| 34 | double upperQuantile = dist.inverseF(level); |
| 35 | |
| 36 | // calculate sample standard deviation |
| 37 | double stdDev = Math.sqrt(new SampleVarianceEstimator() |
| 38 | .estimatePoint(samples)); |
| 39 | |
| 40 | // calculate sample mean |
| 41 | double mean = estimatePoint(samples); |
| 42 | |
| 43 | // calculate confidence interval |
| 44 | double lowerBound = mean - upperQuantile * stdDev |
| 45 | / Math.sqrt(samples.size()); |
| 46 | double upperBound = mean + upperQuantile * stdDev |
| 47 | / Math.sqrt(samples.size()); |
| 48 | |
| 49 | return new ConfidenceInterval(mean, lowerBound, upperBound, level); |
| 50 | } else { |
| 51 | return null; |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | @Override |
| 56 | public double estimatePoint(List<Double> samples) { |
| 57 | double sum = 0; |
| 58 | for (Double sample : samples) { |
| 59 | sum += sample; |
| 60 | } |
| 61 | return sum / samples.size(); |
| 62 | } |
| 63 | } |