在MySQL中实现NORMSINV函数

前端之家收集整理的这篇文章主要介绍了在MySQL中实现NORMSINV函数 前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我需要在MySQL中使用逆正态累积分布函数(Excel中的是NORMSINV函数),但是那里没有这样的函数.也许你们中的任何一个都为MysqL实现了它?

非常感谢您的宝贵时间.

最佳答案
好吧,我终于找到了这个.它不是完美的,但是很好的替代.该代码不是我的,其作者是Geoffrey C. Barnes.我刚刚将其从VB.NET转换为MysqL.

DROP FUNCTION IF EXISTS NORMSINV;
DELIMITER //

CREATE FUNCTION NORMSINV (p DOUBLE) RETURNS DOUBLE
BEGIN

DECLARE q,r DOUBLE;
DECLARE A1,A2,A3,A4,A5,A6 DOUBLE;
DECLARE B1,B2,B3,B4,B5 DOUBLE;
DECLARE C1,C2,C3,C4,C5,C6 DOUBLE;
DECLARE D1,D2,D3,D4 DOUBLE;
DECLARE P_LOW,P_HIGH DOUBLE;

/* coefficients in rational approximations */
SET A1 = -39.696830286653757;
SET A2 = 220.9460984245205;
SET A3 = -275.92851044696869;
SET A4 = 138.357751867269;
SET A5 = -30.66479806614716;
SET A6 = 2.5066282774592392;

SET B1 = -54.476098798224058;
SET B2 = 161.58583685804089;
SET B3 = -155.69897985988661;
SET B4 = 66.80131188771972;
SET B5 = -13.280681552885721;

SET C1 = -0.0077848940024302926;
SET C2 = -0.32239645804113648;
SET C3 = -2.4007582771618381;
SET C4 = -2.5497325393437338;
SET C5 = 4.3746641414649678;
SET C6 = 2.9381639826987831;

SET D1 = 0.0077846957090414622;
SET D2 = 0.32246712907003983;
SET D3 = 2.445134137142996;
SET D4 = 3.7544086619074162;

/* define break points */
SET P_LOW = 0.02425;
SET P_HIGH = 1 - P_LOW;

IF (p > 0 AND p < P_LOW) THEN
/* rational approximation for lower region */
SET q = SQRT(-2 * LOG(p));
RETURN (((((C1 * q + C2) * q + C3) * q + C4) * q + C5) * q + C6) /  
       ((((D1 * q + D2) * q + D3) * q + D4) * q + 1);

ELSEIF (p >= P_LOW AND p <= P_HIGH) THEN
/* rational approximation for central region */
SET q = p - 0.5;
SET r = q * q;
RETURN (((((A1 * r + A2) * r + A3) * r + A4) * r + A5) * r + A6) * q / 
       (((((B1 * r + B2) * r + B3) * r + B4) * r + B5) * r + 1);

ELSEIF (p > P_HIGH AND p < 1) THEN
/* rational approximation for upper region */
SET q = SQRT(-2 * LOG(1 - p));
RETURN -(((((C1 * q + C2) * q + C3) * q + C4) * q + C5) * q + C6) / 
       ((((D1 * q + D2) * q + D3) * q + D4) * q + 1);

/* on error returning 0 */           
ELSE
RETURN 0;
END IF;

END//
DELIMITER ;
原文链接:https://www.f2er.com/mysql/532185.html

猜你在找的MySQL相关文章