四川成都最新新聞事件今天深圳谷歌seo推廣
1 分位數(shù)Quantile
分位數(shù)(Quantile),亦稱分位點,是指將一個隨機變量的概率分布范圍分為幾個等份的數(shù)值點,常用的有中位數(shù)(即二分位數(shù))、四分位數(shù)、百分位數(shù)等。
2 常見各類分位數(shù)
2.1 二分位數(shù)
對于有限的數(shù)集,可以通過把所有觀察值高低排序后找出正中間的一個作為中位數(shù)。如果觀察值有偶數(shù)個,則中位數(shù)不唯一,通常取最中間的兩個數(shù)值的平均數(shù)作為中位數(shù),即二分位數(shù)。
一個數(shù)集中最多有一半的數(shù)值小于中位數(shù),也最多有一半的數(shù)值大于中位數(shù)。如果大于和小于中位數(shù)的數(shù)值個數(shù)均少于一半,那么數(shù)集中必有若干值等同于中位數(shù)。
計算有限個數(shù)的數(shù)據(jù)的二分位數(shù)的方法是:把所有的同類數(shù)據(jù)按照大小的順序排列。如果數(shù)據(jù)的個數(shù)是奇數(shù),則中間那個數(shù)據(jù)就是這群數(shù)據(jù)的中位數(shù);如果數(shù)據(jù)的個數(shù)是偶數(shù),則中間那2個數(shù)據(jù)的算術(shù)平均值就是這群數(shù)據(jù)的中位數(shù)。
2.2 四分位數(shù)
四分位數(shù)(Quartile)是統(tǒng)計學(xué)中分位數(shù)的一種,即把所有數(shù)值由小到大排列并分成四等份,處于三個分割點位置的數(shù)值就是四分位數(shù)。
1)第一四分位數(shù)(Q1),又稱“較小四分位數(shù)”,等于該樣本中所有數(shù)值由小到大排列后第25%的數(shù)字;
2)第二四分位數(shù)(Q2),又稱“中位數(shù)”,等于該樣本中所有數(shù)值由小到大排列后第50%的數(shù)字;
3)第三四分位數(shù)(Q3),又稱“較大四分位數(shù)”,等于該樣本中所有數(shù)值由小到大排列后第75%的數(shù)字。
第三四分位數(shù)與第一四分位數(shù)的差距又稱四分位距。
2.3 百分位數(shù)
百分位數(shù),統(tǒng)計學(xué)術(shù)語,如果將一組數(shù)據(jù)從小到大排序,并計算相應(yīng)的累計百分位,則某一百分位所對應(yīng)數(shù)據(jù)的值就稱為這一百分位的百分位數(shù)。運用在教育統(tǒng)計學(xué)中,例如表現(xiàn)測驗成績時,稱PR值。
3 分位數(shù)的應(yīng)用
分位數(shù)回歸思想的提出至今已經(jīng)有近30多年了,經(jīng)過這近30多年的發(fā)展,分位數(shù)回歸在理論和方法上都越來越成熟,并被廣泛應(yīng)用于多種學(xué)科中。它對于實際問題能提供更加全面的分析,無論是線性模型還是非線性模型,分位數(shù)回歸都是一種很好的工具,它對一般回歸模型做了有益的補充。
分位數(shù)回歸是對以古典條件均值模型為基礎(chǔ)的最小二乘法的延伸,它用幾個分位函數(shù)來估計整體模型。分位數(shù)回歸法的特殊情況就是中位數(shù)回歸(最小一乘回歸),用對稱權(quán)重解決殘差最小化問題,而其他條件分位數(shù)回歸則需要用非對稱權(quán)重解決殘差最小化 [2] 。
分位數(shù)回歸采用加權(quán)殘差絕對值之和的方法估計參數(shù),其優(yōu)點體現(xiàn)在以下幾方面:首先,它對模型中的隨機擾動項不需做任何分布的假定,這樣整個回歸模型就具有很強的穩(wěn)健性;其次,分位數(shù)回歸本身沒有使用一個連接函數(shù)來描述因變量的均值和方差的相互關(guān)系,因此分位數(shù)回歸有著比較好的彈性性質(zhì);第三,分位數(shù)回歸由于是對所有分位數(shù)進行回歸,因此對于數(shù)據(jù)中出現(xiàn)的異常點具有耐抗性;第四,不同于普通的最小二乘回歸,分位數(shù)回歸對于因變量具有單調(diào)變換性;最后,分位數(shù)回歸估計出來的參數(shù)具有在大樣本理論下的漸進優(yōu)良性。
4 計算分位數(shù)的C#源程序
using System;
namespace Legalsoft.Truffer
{
? ? /// <summary>
? ? /// Object for estimating arbitrary quantile values?
? ? /// from a continuing stream of data values.
? ? /// </summary>
? ? public class IQagent
? ? {
? ? ? ? public const int nbuf = 1000;
? ? ? ? private int nq { get; set; }
? ? ? ? private int nt { get; set; }
? ? ? ? private int nd { get; set; }
? ? ? ? private double[] pval { get; set; }
? ? ? ? private double[] dbuf;
? ? ? ? private double[] qile { get; set; }
? ? ? ? private double q0 { get; set; }
? ? ? ? private double qm { get; set; }
? ? ? ? public IQagent()
? ? ? ? {
? ? ? ? ? ? this.nq = 251;
? ? ? ? ? ? this.nt = 0;
? ? ? ? ? ? this.nd = 0;
? ? ? ? ? ? this.pval = new double[nq];
? ? ? ? ? ? this.dbuf = new double[nbuf];
? ? ? ? ? ? this.qile = new double[nq];
? ? ? ? ? ? this.q0 = 1.0e99;
? ? ? ? ? ? this.qm = -1.0e99;
? ? ? ? ? ? for (int j = 85; j <= 165; j++)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? pval[j] = (j - 75.0) / 100.0;
? ? ? ? ? ? }
? ? ? ? ? ? // Set general purpose array of p - values ranging from 1.0e-6 to 1~1.0e-6.
? ? ? ? ? ? // You can change this if you want.
? ? ? ? ? ? for (int j = 84; j >= 0; j--)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? pval[j] = 0.87191909 * pval[j + 1];
? ? ? ? ? ? ? ? pval[250 - j] = 1.0 - pval[j];
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? /// <summary>
? ? ? ? /// Assimilate a new value from the stream.
? ? ? ? /// </summary>
? ? ? ? /// <param name="datum"></param>
? ? ? ? public void add(double datum)
? ? ? ? {
? ? ? ? ? ? dbuf[nd++] = datum;
? ? ? ? ? ? if (datum < q0)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? q0 = datum;
? ? ? ? ? ? }
? ? ? ? ? ? if (datum > qm)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? qm = datum;
? ? ? ? ? ? }
? ? ? ? ? ? if (nd == nbuf)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? update();
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? /// <summary>
? ? ? ? /// Batch update.
? ? ? ? /// This function is called by add or report and should not be called directly
? ? ? ? /// by the user.
? ? ? ? /// </summary>
? ? ? ? public void update()
? ? ? ? {
? ? ? ? ? ? int jd = 0;
? ? ? ? ? ? int jq = 1;
? ? ? ? ? ? double told = 0.0;
? ? ? ? ? ? double tnew = 0.0;
? ? ? ? ? ? double[] newqile = new double[nq];
? ? ? ? ? ? Sorter.sort(dbuf, nd);
? ? ? ? ? ? double qold = q0;
? ? ? ? ? ? double qnew = q0;
? ? ? ? ? ? qile[0] = newqile[0] = q0;
? ? ? ? ? ? qile[nq - 1] = newqile[nq - 1] = qm;
? ? ? ? ? ? pval[0] = Math.Min(0.5 / (nt + nd), 0.5 * pval[1]);
? ? ? ? ? ? pval[nq - 1] = Math.Max(1.0 - 0.5 / (nt + nd), 0.5 * (1.0 + pval[nq - 2]));
? ? ? ? ? ? for (int iq = 1; iq < nq - 1; iq++)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? double target = (nt + nd) * pval[iq];
? ? ? ? ? ? ? ? if (tnew < target)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? for (; ; )
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? if (jq < nq && (jd >= nd || qile[jq] < dbuf[jd]))
? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? qnew = qile[jq];
? ? ? ? ? ? ? ? ? ? ? ? ? ? tnew = jd + nt * pval[jq++];
? ? ? ? ? ? ? ? ? ? ? ? ? ? if (tnew >= target)
? ? ? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? else
? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? qnew = dbuf[jd];
? ? ? ? ? ? ? ? ? ? ? ? ? ? tnew = told;
? ? ? ? ? ? ? ? ? ? ? ? ? ? if (qile[jq] > qile[jq - 1])
? ? ? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? tnew += nt * (pval[jq] - pval[jq - 1]) * (qnew - qold) / (qile[jq] - qile[jq - 1]);
? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? ? ? jd++;
? ? ? ? ? ? ? ? ? ? ? ? ? ? if (tnew >= target)
? ? ? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? ? ? told = tnew++;
? ? ? ? ? ? ? ? ? ? ? ? ? ? qold = qnew;
? ? ? ? ? ? ? ? ? ? ? ? ? ? if (tnew >= target)
? ? ? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? told = tnew;
? ? ? ? ? ? ? ? ? ? ? ? qold = qnew;
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? //if (tnew == told)
? ? ? ? ? ? ? ? if (Math.Abs(tnew - told) <= float.Epsilon)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? newqile[iq] = 0.5 * (qold + qnew);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? else
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? newqile[iq] = qold + (qnew - qold) * (target - told) / (tnew - told);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? told = tnew;
? ? ? ? ? ? ? ? qold = qnew;
? ? ? ? ? ? }
? ? ? ? ? ? // qile = newqile;
? ? ? ? ? ? qile = Globals.CopyFrom(newqile);
? ? ? ? ? ? nt += nd;
? ? ? ? ? ? nd = 0;
? ? ? ? }
? ? ? ? /// <summary>
? ? ? ? /// Return estimated p-quantile for?
? ? ? ? /// the data seen so far. (E.g., p D 0:5 for median.)
? ? ? ? /// </summary>
? ? ? ? /// <param name="p"></param>
? ? ? ? /// <returns></returns>
? ? ? ? public double report(double p)
? ? ? ? {
? ? ? ? ? ? if (nd > 0)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? update();
? ? ? ? ? ? }
? ? ? ? ? ? int jl = 0;
? ? ? ? ? ? int jh = nq - 1;
? ? ? ? ? ? int j;
? ? ? ? ? ? while (jh - jl > 1)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? j = (jh + jl) >> 1;
? ? ? ? ? ? ? ? if (p > pval[j])
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? jl = j;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? else
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? jh = j;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? j = jl;
? ? ? ? ? ? double q = qile[j] + (qile[j + 1] - qile[j]) * (p - pval[j]) / (pval[j + 1] - pval[j]);
? ? ? ? ? ? return Math.Max(qile[0], Math.Min(qile[nq - 1], q));
? ? ? ? }
? ? }
}
?