6

我想要做的是得到我数字的每一半的中间。所以我已经创建的是一种在此处获取数字中间值(数学中的中位数)的方法;

    public static String Find_Median()
    {
        double Size = list.Count;
        double Final_Number = 0;
        if (Size % 2 == 0)
        {
            int HalfWay = list.Count / 2;
            double Value1 = Convert.ToDouble(list[HalfWay - 1].ToString());
            double Value2 = Convert.ToDouble(list[HalfWay - 1 + 1].ToString());
            double Number = Value1 + Value2;
            Final_Number = Number / 2;
        }
        else
        {
            int HalfWay = list.Count / 2;
            double Value1 = Convert.ToDouble(list[HalfWay].ToString());
            Final_Number = Value1;
        }
        return Convert.ToString(Final_Number);
    }

这得到了列表中所有数字的确切中间数字,即使它到达中间它也会做那个数学。我想在双方都这样做;这是一个例子;

3 2 1 4 5 6

该列表的中间值(中位数)是 3.5。我想用数学来找到 2,它位于等式的开始和中间。在 IQR 中也称为 Q1。我还想知道如何找到中位数(中间)和结尾之间的中间数,即 5。

在此处输入图像描述

IE 所以我可以找到 70,80 和 90 的代码。

4

4 回答 4

10

我刚刚遇到了同样的问题,并检查了Quartile 的维基百科条目,它比最初出现的要复杂一些。

我的方法如下:(这似乎适用于所有情况,N=1 起)...

 /// <summary>
/// Return the quartile values of an ordered set of doubles
///   assume the sorting has already been done.
///   
/// This actually turns out to be a bit of a PITA, because there is no universal agreement 
///   on choosing the quartile values. In the case of odd values, some count the median value
///   in finding the 1st and 3rd quartile and some discard the median value. 
///   the two different methods result in two different answers.
///   The below method produces the arithmatic mean of the two methods, and insures the median
///   is given it's correct weight so that the median changes as smoothly as possible as 
///   more data ppints are added.
///    
/// This method uses the following logic:
/// 
/// ===If there are an even number of data points:
///    Use the median to divide the ordered data set into two halves. 
///    The lower quartile value is the median of the lower half of the data. 
///    The upper quartile value is the median of the upper half of the data.
///    
/// ===If there are (4n+1) data points:
///    The lower quartile is 25% of the nth data value plus 75% of the (n+1)th data value.
///    The upper quartile is 75% of the (3n+1)th data point plus 25% of the (3n+2)th data point.
///    
///===If there are (4n+3) data points:
///   The lower quartile is 75% of the (n+1)th data value plus 25% of the (n+2)th data value.
///   The upper quartile is 25% of the (3n+2)th data point plus 75% of the (3n+3)th data point.
/// 
/// </summary>
internal Tuple<double, double, double> Quartiles(double[] afVal)
{
    int iSize = afVal.Length;
    int iMid = iSize / 2; //this is the mid from a zero based index, eg mid of 7 = 3;

    double fQ1 = 0;
    double fQ2 = 0;
    double fQ3 = 0;

    if (iSize % 2 == 0)
    {
        //================ EVEN NUMBER OF POINTS: =====================
        //even between low and high point
        fQ2 = (afVal[iMid - 1] + afVal[iMid]) / 2;

        int iMidMid = iMid / 2;

        //easy split 
        if (iMid % 2 == 0)
        {
            fQ1 = (afVal[iMidMid - 1] + afVal[iMidMid]) / 2;
            fQ3 = (afVal[iMid + iMidMid - 1] + afVal[iMid + iMidMid]) / 2;
        }
        else
        {
            fQ1 = afVal[iMidMid];
            fQ3 = afVal[iMidMid + iMid];
        }
    }
    else if (iSize == 1)
    {
        //================= special case, sorry ================
        fQ1 = afVal[0];
        fQ2 = afVal[0];
        fQ3 = afVal[0];
    }
    else
    {
        //odd number so the median is just the midpoint in the array.
        fQ2 = afVal[iMid];

        if ((iSize - 1) % 4 == 0)
        {
            //======================(4n-1) POINTS =========================
            int n = (iSize - 1) / 4;
            fQ1 = (afVal[n - 1] * .25) + (afVal[n] * .75);
            fQ3 = (afVal[3 * n] * .75) + (afVal[3 * n + 1] * .25);
        }
        else if ((iSize - 3) % 4 == 0)
        {
            //======================(4n-3) POINTS =========================
            int n = (iSize - 3) / 4;

            fQ1 = (afVal[n] * .75) + (afVal[n + 1] * .25);
            fQ3 = (afVal[3 * n + 1] * .25) + (afVal[3 * n + 2] * .75);
        }
    }

    return new Tuple<double, double, double>(fQ1, fQ2, fQ3);
}

有很多方法可以计算四分位数:

Quartile(array, type=8)我在这里尽我所能来实现R 文档中描述为 type = 8 的四分位数版本: https ://www.rdocumentation.org/packages/stats/versions/3.5.1/topics/quantile 。此处描述的 R 函数的作者更喜欢这种方法,因为它可以在值之间产生更平滑的过渡。但是,R 默认使用方法 7,这与 S 和 Excel 使用的函数相同。

如果您只是在谷歌上搜索答案,而不考虑输出的含义或您想要达到的结果,这可能会给您带来惊喜。

于 2014-01-09T00:31:17.543 回答
3

在以下列表中运行相同的方法:

list1 = list.Where(x => x < Median)
list2 = list.Where(x => x > Median) 

Find_Median(list1)将返回第一个四分位数, Find_Median(list2)将返回第三个四分位数

于 2013-02-04T09:20:49.517 回答
3

我知道这是一个老问题,所以我争论了一会儿是否应该添加下面的答案,并且由于投票最多的答案与 Excel 四分位数的数字不匹配,我决定在下面发布答案。

当我试图绘制直方图并创建 bin 宽度和范围时,我还需要找到第一和第三四分位数,我使用的是 Freedman–Diaconis 规则,该规则需要知道第一和第三四分位数。我从迈克的回答开始。

但是在数据验证过程中,我注意到结果与 Excel 中计算四分位数的方式和使用Ploltly创建的直方图不匹配,所以我进一步挖掘并偶然发现了以下两个链接:

第二个链接中的幻灯片 12 状态“第 P 个百分位数的位置由 给出(n + 1)P/100,其中 n 是集合中的观察数。”

因此,来自C# Descriptive Statistic Class的等效 C# 代码是:

    /// <summary>
    /// Calculate percentile of a sorted data set
    /// </summary>
    /// <param name="sortedData"></param>
    /// <param name="p"></param>
    /// <returns></returns>
    internal static double Percentile(double[] sortedData, double p)
    {
        // algo derived from Aczel pg 15 bottom
        if (p >= 100.0d) return sortedData[sortedData.Length - 1];

        double position = (sortedData.Length + 1) * p / 100.0;
        double leftNumber = 0.0d, rightNumber = 0.0d;

        double n = p / 100.0d * (sortedData.Length - 1) + 1.0d;

        if (position >= 1)
        {
            leftNumber = sortedData[(int)Math.Floor(n) - 1];
            rightNumber = sortedData[(int)Math.Floor(n)];
        }
        else
        {
            leftNumber = sortedData[0]; // first data
            rightNumber = sortedData[1]; // first data
        }

        //if (leftNumber == rightNumber)
        if (Equals(leftNumber, rightNumber))
            return leftNumber;
        double part = n - Math.Floor(n);
        return leftNumber + part * (rightNumber - leftNumber);
    } // end of internal function percentile

测试用例(用 Visual Studio 2017 编写):

    static void Main()
    {
        double[] x = { 18, 18, 18, 18, 19, 20, 20, 20, 21, 22, 22, 23, 24, 26, 27, 32, 33, 49, 52, 56 };
        var q1 = Percentile(x, 25);
        var q2 = Percentile(x, 50);
        var q3 = Percentile(x, 75);
        var iqr = q3 - q1;

        var (q1_mike, q2_mike, q3_mike) = Quartiles(x); //Uses named tuples instead of regular Tuple
        var iqr_mike = q3_mike - q1_mike;
    }

结果比较:

您会注意到结果与幻灯片 12中提到的统计理论相匹配。

  • 从代码:

四分位数比较

  • 来自 excel(匹配 q1、q2 和 q3 值)

excel中的结果相同

于 2018-07-10T11:59:18.190 回答
0

一种可以调整的快捷方式

var q3 = table.Skip(table.Length * 3 / 4).Take(1);
var q1 = table.Skip(table.Length * 1 / 4).Take(1);
于 2018-09-11T18:02:58.137 回答