using System; using System.Collections.Generic; using System.Linq; namespace Unity.PerformanceTesting.Statistics { static class ConfidenceLevelExtensions { private static readonly Dictionary k_ConfidenceLevelDetails = CreateConfidenceLevelMapping(); /// /// Calculates Z value (z-star) for confidence interval /// /// ConfidenceLevel for a confidence interval /// Sample size (n >= 3) public static double GetZValue(this ConfidenceLevel level, int n) { if (n <= 1) throw new ArgumentOutOfRangeException(nameof(n), "n should be >= 2"); return StudentDistributionHelper.InverseTwoTailedStudent(1 - level.ToPercent(), n - 1); } static double ToPercent(this ConfidenceLevel level) { (int value, int digits) = k_ConfidenceLevelDetails[level]; return value / Math.Pow(10, digits); } static Dictionary CreateConfidenceLevelMapping() { return Enum.GetValues(typeof(ConfidenceLevel)) .Cast() .ToDictionary( confidenceLevel => confidenceLevel, confidenceLevel => { var textRepresentation = confidenceLevel.ToString().Substring(1); return (int.Parse(textRepresentation), textRepresentation.Length); }); } } }