计算MySQL中的正态分布

时间:2011-09-16 16:24:27

标签: mysql

我尝试使用正态分布计算mysql中的值,其中mean可以是零或其他。

编辑发现这但不确定如何改变不是MYSQL

create Function CND(@X float) 
returns float
as 
begin
declare @L float
declare @K float
declare @a1 float
declare @a2 float
declare @a3 float
declare @a4 float
declare @a5 float
set @a1 = 0.31938153
set @a2 = -0.356563782
set @a3 = 1.781477937
set @a4 = -1.821255978
set @a5 = 1.330274429
set @L = Abs(@X)
set @K = 1 / (1 + 0.2316419 * @L)
declare @CND1 float
set @CND1 = 1 - 1 / Sqrt(2 * Pi()) * Exp(-power(@L,2) / 2) * (@a1 * @K + 
@a2 * power(@K,2) + @a3 * power(@K,3) + @a4 * power(@K,4) + @a5 * 
power(@K,5))
If @X < 0 
begin
    set @CND1 = 1 - @CND1
End
return @CND1
End 

2 个答案:

答案 0 :(得分:1)

转换为mysql:

delimiter //
create Function CND (_X double) 
returns float
begin
    set @X = _X;
    set @a1 = 0.31938153;
    set @a2 = -0.356563782;
    set @a3 = 1.781477937;
    set @a4 = -1.821255978;
    set @a5 = 1.330274429;
    set @L = Abs(@X);
    set @K = 1 / (1 + 0.2316419 * @L);
    set @CND1 = 1 - 1 / Sqrt(2 * Pi()) * Exp(-power(@L,2) / 2) * (@a1 * @K + 
        @a2 * power(@K,2) + @a3 * power(@K,3) + @a4 * power(@K,4) + @a5 * 
        power(@K,5));

    if @X < 0 then
        set @CND1 = 1 - @CND1;
    end if;

    return @CND1;
end //
delimiter ;

答案 1 :(得分:-1)

您的错误可能在于分隔符。请参阅MySQL文档,了解用于函数创建的正确语法。