3

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

enter image description here

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.

Captain Kenpachi
  • 6,435
  • 6
  • 42
  • 64

3 Answers3

5

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]);
Dmitry Bychenko
  • 149,892
  • 16
  • 136
  • 186
0

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

MBo
  • 66,413
  • 3
  • 45
  • 68
0

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));
}
Community
  • 1
  • 1
Torben
  • 418
  • 1
  • 5
  • 18