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