Skip to content

Commit

Permalink
added coeficient of variation
Browse files Browse the repository at this point in the history
  • Loading branch information
ignatandrei committed Oct 12, 2024
1 parent 3290ad8 commit f1bbf39
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,28 @@
}
var mode= modes.First();
var stdDev = Math.Sqrt( Model.Variance);
var min = Model.Min;
var max = Model.Max;
var mean = Model.ArithmeticMean;
double? coefOfVar=null;
if(mean != 0)
{
coefOfVar = stdDev / Model.ArithmeticMean;
}

//var perc = Model.Percentile(95, (a, b) => ((double)a) * b);
}
<div>
Min Value: @min
;Max Value: @max
;Math average: @Model.ArithmeticMean
;Math median: @Model.Median
;Standard Dev: @(stdDev.ToString("0.00"))
@if(coefOfVar.HasValue){
<text>
;Coefficient of Variation: @(coefOfVar.Value.ToString("0.00"))
</text>
}
@if(displayMode){
<text>
<br />
Expand Down
4 changes: 3 additions & 1 deletion src/NetPackageAnalyzer/NetPackageAnalyzerObjects/many.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ public record NamePerCountArray(NamePerCount[]? NamePerCounts,bool Descending)
{
public Statistics<long> Statistics()
{
return new Statistics<long>(NamePerCounts?.Select(it => it.Count).ToArray() ?? []);
return new Statistics<long>(
this.DataOrdered().Select(it => it.Count).ToArray() ?? []
);
}
public NamePerCount? First1()
{
Expand Down
40 changes: 37 additions & 3 deletions src/NetPackageAnalyzer/Statistical/StatisticalNumbers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,23 @@
namespace Statistical;

public static class StatisticalNumbers<T>
where T : INumber<T>,IDivisionOperators<T, T, T>
where T : INumber<T>, IDivisionOperators<T, T, T>
{
public static T? Max(T[] values) {

if (values == null || values.Length == 0)
{
return default(T?);
}
return values.Max();
}
public static T? Min(T[] values) {
if (values == null || values.Length == 0)
{
return default(T?);
}
return values.Min();
}
public static T Median(T[]? values)
{
var size = values?.Length ?? 0;
Expand Down Expand Up @@ -103,7 +118,22 @@ public static T Variance(T[] values)
}
return variance / mid; // For sample variance
}

//public static T Percentile(T[] sequence, int percentile, Func<T,double,T> f)
//{
// double excelPercentile= ((double)percentile) / 100;
// Array.Sort(sequence);
// int N = sequence.Length;
// double n = (N - 1) * excelPercentile + 1;
// // Another method: double n = (N + 1) * excelPercentile;
// if (n == 1d) return sequence[0];
// else if (n == N) return sequence[N - 1];
// else
// {
// int k = (int)n;
// double d = n - k;
// return sequence[k - 1] + f(sequence[k] - sequence[k - 1],d);
// }
//}
}
public record Statistics<T>(T[] values)
where T : INumber<T>, IDivisionOperators<T, T, T>
Expand All @@ -112,5 +142,9 @@ public record Statistics<T>(T[] values)
public T ArithmeticMean => StatisticalNumbers<T>.ArithmeticMean(values);
public ModeResult<T>[] Mode => StatisticalNumbers<T>.Mode(values);
public T Variance => StatisticalNumbers<T>.Variance(values);
}

public T? Max => StatisticalNumbers<T>.Max(values);
public T? Min => StatisticalNumbers<T>.Min(values);
//public T Percentile(int percentile, Func<T,double,T> multiply) => StatisticalNumbers<T>.Percentile(values, percentile, multiply);
}

0 comments on commit f1bbf39

Please sign in to comment.