97 lines
2.4 KiB
C#
97 lines
2.4 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
|
|
namespace CordonelPreadjustmentUi.Helper.Extensions
|
|
{
|
|
public static class ListDoubleCalculationExt
|
|
{
|
|
public static double Mean(this List<double> values)
|
|
{
|
|
#region CA1062
|
|
if (values == null)
|
|
return 0;
|
|
#endregion
|
|
|
|
return values.Count == 0 ? 0 : values.Mean(0, values.Count);
|
|
}
|
|
|
|
public static double Mean(this List<double> values, int start, int end)
|
|
{
|
|
#region CA1062
|
|
if (values == null)
|
|
return 0;
|
|
#endregion
|
|
double s = 0;
|
|
|
|
for (int i = start; i < end; i++)
|
|
{
|
|
s += values[i];
|
|
}
|
|
|
|
return s / (end - start);
|
|
}
|
|
|
|
public static double Variance(this List<double> values)
|
|
{
|
|
#region CA1062
|
|
if (values == null)
|
|
return 0;
|
|
#endregion
|
|
|
|
return values.Variance(values.Mean(), 0, values.Count);
|
|
}
|
|
|
|
public static double Variance(this List<double> values, double mean)
|
|
{
|
|
#region CA1062
|
|
if (values == null)
|
|
return 0;
|
|
#endregion
|
|
|
|
return values.Variance(mean, 0, values.Count);
|
|
}
|
|
|
|
public static double Variance(this List<double> values, double mean, int start, int end)
|
|
{
|
|
#region CA1062
|
|
if (values == null)
|
|
return 0;
|
|
#endregion
|
|
|
|
double variance = 0;
|
|
|
|
|
|
for (int i = start; i < end; i++)
|
|
{
|
|
variance += (double)Math.Pow((values[i] - mean), 2);
|
|
}
|
|
|
|
int n = end - start;
|
|
if (start > 0) n -= 1;
|
|
|
|
return variance / (n);
|
|
}
|
|
|
|
public static double StandardDeviation(this List<double> values)
|
|
{
|
|
#region CA1062
|
|
if (values == null)
|
|
return 0;
|
|
#endregion
|
|
|
|
return values.Count == 0 ? 0 : values.StandardDeviation(0, values.Count);
|
|
}
|
|
|
|
public static double StandardDeviation(this List<double> values, int start, int end)
|
|
{
|
|
double mean = values.Mean(start, end);
|
|
double variance = values.Variance(mean, start, end);
|
|
|
|
return (double)Math.Sqrt(variance);
|
|
}
|
|
|
|
}
|
|
}
|