No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ConfidenceLevelExtensions.cs 1.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. namespace Unity.PerformanceTesting.Statistics
  5. {
  6. static class ConfidenceLevelExtensions
  7. {
  8. private static readonly Dictionary<ConfidenceLevel, (int value, int digits)> k_ConfidenceLevelDetails = CreateConfidenceLevelMapping();
  9. /// <summary>
  10. /// Calculates Z value (z-star) for confidence interval
  11. /// </summary>
  12. /// <param name="level">ConfidenceLevel for a confidence interval</param>
  13. /// <param name="n">Sample size (n >= 3)</param>
  14. public static double GetZValue(this ConfidenceLevel level, int n)
  15. {
  16. if (n <= 1)
  17. throw new ArgumentOutOfRangeException(nameof(n), "n should be >= 2");
  18. return StudentDistributionHelper.InverseTwoTailedStudent(1 - level.ToPercent(), n - 1);
  19. }
  20. static double ToPercent(this ConfidenceLevel level)
  21. {
  22. (int value, int digits) = k_ConfidenceLevelDetails[level];
  23. return value / Math.Pow(10, digits);
  24. }
  25. static Dictionary<ConfidenceLevel, (int value, int length)> CreateConfidenceLevelMapping()
  26. {
  27. return Enum.GetValues(typeof(ConfidenceLevel))
  28. .Cast<ConfidenceLevel>()
  29. .ToDictionary(
  30. confidenceLevel => confidenceLevel,
  31. confidenceLevel =>
  32. {
  33. var textRepresentation = confidenceLevel.ToString().Substring(1);
  34. return (int.Parse(textRepresentation), textRepresentation.Length);
  35. });
  36. }
  37. }
  38. }