/// <summary> /// Basic constructor /// </summary> /// <param name="sample">A collection of the sample values</param> /// <param name="confIntType">Type of 95-% confidence interval to be computed</param> /// <param name="quantiles">A list of extra quantile values to be computed</param> public StatisticsSample(ICollection <double> sample, ConfidenceIntervalTypes confIntType, List <double> quantiles = null) { // for sample sizes smaller than or equal to 1 statistics cannot be computed if (sample.Count <= 1) { return; } List <double> sortedSample = sample.ToList(); // sort sample sortedSample.Sort(); // compute mean _mean = sortedSample.Sum() / sortedSample.Count; // compute variance foreach (double val in sortedSample) { _variance += 1d / (sortedSample.Count - 1) * Math.Pow(val - _mean, 2); } // end foreach // compute median _median = sortedSample[(int)Math.Ceiling(sample.Count() / 2d)]; // confidence computed via standard deviation if (confIntType == ConfidenceIntervalTypes.StandardDeviation) { _cI95Lower = _mean - 1.96 * Math.Sqrt(_variance / sample.Count); _cI95Upper = _mean + 1.96 * Math.Sqrt(_variance / sample.Count); } // confidence intercal via quantiles else if (confIntType == ConfidenceIntervalTypes.Quantiles) { int sortLength = sortedSample.Count; _cI95Lower = sortedSample[(int)(sortLength * 0.025)]; _cI95Upper = sortedSample[(int)(sortLength * 0.975)]; } _quantiles = new Dictionary <double, double>(); // compute specified quantiles if (quantiles != null) { for (int i = 0; i < quantiles.Count; i++) { _quantiles.Add(quantiles[i], sortedSample[(int)(sortedSample.Count * quantiles[i])]); } // end for } // end if } // end of StatisticsSample
} // end of RandomNumberGenerator #endregion #region GetStatistics /// <summary> /// Produces statistical measures of a set of double values /// </summary> /// <param name="sample">Sample values</param> /// <param name="confIntType">Type of confidence interval to be used</param> /// <param name="quantList">A list of quantiles that should be computed for the sample</param> /// <returns>A set of statistical measures for the sample</returns> static public StatisticsSample GetStatistics(ICollection <double> sample, ConfidenceIntervalTypes confIntType = ConfidenceIntervalTypes.StandardDeviation, List <double> quantList = null) { return(new StatisticsSample(sample, confIntType, quantList)); } // end of GetMeanOfSample