问题
I am required to perform a number of statistical calculations on a number set and one of the things I need to calculate is the Median Absolute Deviation. I was supplied with an ISO standard and all it tells me is
I have no idea what to do with that info as I do not have any statistical math training. As such, I can't translate the above into a C# function.
回答1:
Median is a middle element of the sorted array (or average of two middle items if the array has even items):
double[] source = new double[] { 1, 2, 3, 4, 5 };
Array.Sort(source);
double med = source.Length % 2 == 0
? (source[source.Length / 2 - 1] + source[source.Length / 2]) / 2.0
: source[source.Length / 2];
double[] d = source
.Select(x => Math.Abs(x - med))
.OrderBy(x => x)
.ToArray();
double MADe = 1.483 * (d.Length % 2 == 0
? (d[d.Length / 2 - 1] + d[d.Length / 2]) / 2.0
: d[d.Length / 2]);
回答2:
Make simple steps:
1. Find median med of x[] array (you can just sort array and get middle value, but there are more effective methods)
2. Build array d[] of absolute differences with median
3. Find median of d[] array
4. Calc MADe
回答3:
How to program a function, which will calculate the median, you will find here
then your function can look like this
var arrOfValues = new int[] { 1, 3, 5, 7, 9 };
var di = new List<int>(); //List where all di will be stored
var median = calcMedian(); //See the link how to write it
foreach(var elem in arrOfValues)
{
di.Add(Math.Abs(elem - median));
}
来源:https://stackoverflow.com/questions/43610304/calculating-median-absolute-deviation-in-c-sharp