• API
• FAQ
• Tools
• Archive
daily pastebin goal
17%
SHARE
TWEET

# Untitled

a guest May 16th, 2018 216 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. void Main()
2. {
3.     GetPassFailRateWithConfidence(10, 2, 0.95).ToString().Dump();
4.     GetPassFailRateWithConfidence(100, 20, 0.95).ToString().Dump();
5.     GetPassFailRateWithConfidence(1000, 200, 0.95).ToString().Dump();
6.     GetPassFailRateWithConfidence(10000, 2000, 0.95).ToString().Dump();
7. }
8.
9. public ErrorRate GetPassFailRateWithConfidence(long passes, long fails, double confidence)
10. {
11.     double fact(long n) => Math.Round(Math.Sqrt(Math.PI * (2 * n + 1.0 / 3)) * Math.Pow(n / Math.E, n));
12.
13.     double nCr(long n, long r) =>
14.         fact(n).Dump() / (fact(r) * fact(n - r));
15.
16.     long gcd(long a, long b) => b > 0
17.         ? gcd(b, a % b)
18.         : a;
19.
20.     double nCr2(long n, long r, double factor)
21.     {
22.         if (r > n / 2) r = n - r;
23.
24.         double ans = factor;
25.         for (long i = 1; i <= r; i++)
26.         {
27.             var num = (n - r + i);
28.             var divisor = gcd(num, i);
29.             ans *= (num / divisor);
30.             ans /= (i / divisor);
31.         }
32.
33.         return ans;
34.     }
35.
36.     var total = passes + fails;
37.     var θ = (passes + 0.5) / (total + 1);
38.
39.     double P(long p) =>
40.         nCr2(total, p, Math.Pow(θ, p) * Math.Pow(1 - θ, total - p));
41.
42.     var minThreshold = (1 - confidence) / 2;
43.     var maxThreshold = 1 - minThreshold;
44.     var sum = 0.0;
45.     double? min = default;
46.     double? max = default;
47.     for (var i = 0; i <= total; i++)
48.     {
49.         if (sum >= maxThreshold && max == null)
50.         {
51.             max = (double)i / total;
52.             break;
53.         }
54.
55.         sum += P(i);
56.         //sum.Dump();
57.
58.         if (sum >= minThreshold && min == null)
59.         {
60.             min = (double)i / total;
61.         }
62.     }
63.
64.     if (max == null)
65.     {
66.         max = 1;
67.     }
68.
69.     return new ErrorRate(mean: (double)passes / total, min: min.Value, max: max.Value, confidence: confidence);
70. }
71.
72. public class ErrorRate
73. {
74.     public ErrorRate(double mean, double min, double max, double confidence)
75.     {
76.         this.Mean = mean;
77.         this.Min = min;
78.         this.Max = max;
79.         this.Confidence = confidence;
80.     }
81.
82.     public double Mean { get; }
83.     public double Min { get; }
84.     public double Max { get; }
85.     public double Confidence { get; }
86.
87.     public override string ToString()
88.     {
89.         return \$"{this.Mean:P} ({this.Min:P}-{this.Max:P} @{this.Confidence:P})";
90.     }
91. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.

Top