標準常態分佈函數和標準常態分佈分位元函數

來源:互聯網
上載者:User

標準常態分佈函數和分位元函數的數值演算法可參考高惠璿編著的《統計計算》

以下是C#版本的實現代碼:

/// <summary>
/// 標準常態分佈函數Phi(x)
/// </summary>
/// <param name="x">隨機變數</param>
/// <returns>標準常態分佈機率</returns>
static double NormDistFunc(double x)
{
  double x0 = (x >= 0 ? x : -x);
  double[] b = { 0.196854, 0.115194, 0.000344, 0.019527 };
  double erf = 0;
  for (int i = 1; i <= 4; i++)
  {
    erf += b[i - 1] * Math.Pow(x0, i);
  }
  erf = 1 - Math.Pow(1.0 + erf, -4);
  double phi = (x >= 0 ? 0.5 * (1 + erf) : 0.5 * (1 - erf));
  return phi;
}
/// <summary>
/// 標準常態分佈函數分位元函數
/// </summary>
/// <param name="p">機率</param>
/// <returns>分位元</returns>
static double NormDistributionQuantile(double p)
{
  Debug.Assert((0 < p) && (p < 1));
  if (p == 0.5)
    return 0;
  double[] b ={0.1570796288E1,   0.3706987906E-1,
              -0.8364353589E-3, -0.2250947176E-3,
                0.6841218299E-5,  0.5824238515E-5,
              -0.1045274970E-5,  0.8360937017E-7,
              -0.3231081277E-8,  0.3657763036E-10,
                0.6936233982E-12};
  double alpha = 0;
  if ((0 < p) && (p < 0.5))
    alpha = p;
  else if ((0.5 < p) && (p < 1))
    alpha = 1 - p;
  double y = -Math.Log(4 * alpha * (1 - alpha));
  double u = 0;
#if USE_TODA_FORMULA
  //Toda近似公式,最大誤差1.2e-8
  for (int i = 0; i < b.Length; i++)
  {
    u += b[i] * Math.Pow(y, i);
  }
  u = Math.Sqrt(y * u);
#else
  //山內近似公式,最大誤差4.9e-4
  u = Math.Sqrt(y * (2.0611786 - 5.7262204 / (y + 11.640595)));
#endif
  double up = 0;
  if ((0 < p) && (p < 0.5))
    up = -u;
  else if ((0.5 < p) && (p < 1))
    up = u;
  return up;
}
/// <summary>
/// 標準常態分佈機率密度函數
/// </summary>
/// <param name="x">隨機變數</param>
/// <returns>機率密度</returns>
static double NormDensityFunc(double x)
{
  return Math.Exp(-x * x * 0.5) / Math.Sqrt(2 * Math.PI);
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.