找到第一和第三个四分位数

时间:2013-02-04 09:08:38

标签: c# math

我想做的是获取我的号码的每一半的中间位置。 所以我已经创建了一种方法来获得数字的中间值(数学术语的中位数);

    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。

enter image description here

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

4 个答案:

答案 0 :(得分:7)

我刚遇到同样的问题,并检查wikipedia entry for 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);
}

有多种计算季数的方法:

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

如果你只是谷歌搜索答案而不考虑输出意味着什么,或者你想要达到什么结果,这可能会给你一个惊喜。

答案 1 :(得分:3)

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

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

Find_Medium(list1)将首先返回Quartile, Find_Medium(list2)将返回第三个Quartile

答案 2 :(得分:0)

我知道这是一个老问题,所以我辩论了一段时间是否应该添加下面的答案,并且由于投票最多的答案与Excel Quartile的数字不匹配,因此我决定在下面发布答案。 / p>

在尝试绘制直方图并创建bin宽度和范围时,我还需要找到第一和第三四分位数。我使用的是Freedman–Diaconis规则,该规则要求知道第一和第三四分位数。我从Mike's answer开始。

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

第二个链接中的幻灯片12表示“ Pth百分位的位置由(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;
    }

结果比较:

您会注意到excel的结果与幻灯片12中提到的Statistics的理论相符。

  • 来自代码:

Quartile value comparison

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

Same result in excel

答案 3 :(得分:0)

一种可以调整的快捷方式

var q3 = table.Skip(table.Length * 3 / 4).Take(1);
var q1 = table.Skip(table.Length * 1 / 4).Take(1);