Quantcast
Channel: ClosedXML - The easy way to OpenXML
Viewing all articles
Browse latest Browse all 1877

Commented Unassigned: Inserting the MEDIAN and QUARTILE functions [9431]

$
0
0
Inserting more statistical functions.
Comments: I have implemented the functions in this way: In Statistical.cs ``` //=morraf= static object Percentile(List<Expression> p) { var range = p[0] as IEnumerable; // the k parameter of the percentile var k = (double)p[1].Evaluate(); // populate the tally object var tally = new Tally(); foreach (var value in range) { System.Diagnostics.Debug.Print(value.ToString()); tally.AddValue(value); } // evaluate return tally.Percentile(k); } //=morraf= static object Quartile(List<Expression> p) { // convert the quartile to percentile var k = (double)p[1].Evaluate() / 4; p[1] = new Expression(k); //p.Add(new Expression(k)); return Percentile(p); } //=morraf= static object Median(List<Expression> p) { // it is percentile 0.5 p.Add(new Expression(0.5)); return Percentile(p); } ``` where tally.Percentile ``` //http://stackoverflow.com/questions/8137391/percentile-calculation public double Percentile(double excelPercentile) { var values = Numerics().ToArray(); Array.Sort(values); double ret = 0; int arrayCount = values.Count(); if (arrayCount > 0) { double n = (arrayCount - 1) * excelPercentile + 1; // Another method: double n = (N + 1) * excelPercentile; if (n == 1d) return values[0]; else if (n == arrayCount) return values[arrayCount - 1]; else { int k = (int)n; double d = n - k; ret = values[k - 1] + d * (values[k] - values[k - 1]); } } return ret; } ```

Viewing all articles
Browse latest Browse all 1877

Trending Articles