diff --git a/src/Directory.Build.props b/src/Directory.Build.props index 0f64581..c9b5c2b 100644 --- a/src/Directory.Build.props +++ b/src/Directory.Build.props @@ -1,6 +1,6 @@ - 0.2.0 + 0.2.1 Tony Redondo, Grégory Léocadie net6.0;net7.0;net8.0;net9.0 enable diff --git a/src/TimeItSharp.Common/Utils.cs b/src/TimeItSharp.Common/Utils.cs index eb002ce..ac16753 100644 --- a/src/TimeItSharp.Common/Utils.cs +++ b/src/TimeItSharp.Common/Utils.cs @@ -1,5 +1,6 @@ using System.Text; using MathNet.Numerics.Distributions; +using Spectre.Console; using TimeItSharp.Common.Results; namespace TimeItSharp.Common; @@ -42,7 +43,7 @@ public static IEnumerable RemoveOutliers(IEnumerable data, doubl // If the data is empty, return an empty array if (lstData.Count == 0) { - return Array.Empty(); + return []; } // Calculate the standard deviation of the data @@ -187,7 +188,7 @@ public static OverheadResult[][] GetComparisonTableData(IReadOnlyList(); + return []; } // Initialize a 2D array to hold the comparison table data @@ -281,27 +282,42 @@ public static string ToDurationString(this TimeSpan timeSpan) public static double[] CalculateConfidenceInterval(double mean, double standardError, int sampleSize, double confidenceLevel) { - // Check if we should use the Student's t-distribution or the standard normal distribution - double criticalValue; - if (sampleSize < 30) - { - // Let's use the t-distribution - var degreesOfFreedom = sampleSize - 1; - criticalValue = StudentT.InvCDF(0, 1, degreesOfFreedom, 1 - (1 - confidenceLevel) / 2); - } - else + try { - // Let's use the standard normal distribution - criticalValue = Normal.InvCDF(0, 1, 1 - (1 - confidenceLevel) / 2); - } + // Check if we should use the Student's t-distribution or the standard normal distribution + double criticalValue; + if (sampleSize < 30) + { + // Let's use the t-distribution + var degreesOfFreedom = sampleSize - 1; + if (degreesOfFreedom > 0) + { + criticalValue = StudentT.InvCDF(0, 1, degreesOfFreedom, 1 - (1 - confidenceLevel) / 2); + } + else + { + return [mean, mean]; + } + } + else + { + // Let's use the standard normal distribution + criticalValue = Normal.InvCDF(0, 1, 1 - (1 - confidenceLevel) / 2); + } - // Calc the margin of error - var marginOfError = criticalValue * standardError; + // Calc the margin of error + var marginOfError = criticalValue * standardError; - // Create confidence interval - var lowerBound = mean - marginOfError; - var upperBound = mean + marginOfError; + // Create confidence interval + var lowerBound = mean - marginOfError; + var upperBound = mean + marginOfError; - return [lowerBound, upperBound]; + return [lowerBound, upperBound]; + } + catch (Exception ex) + { + AnsiConsole.WriteException(ex); + return [mean, mean]; + } } } \ No newline at end of file