Julia布尔比较

时间:2018-02-02 22:04:44

标签: julia logistic-regression boolean-expression

我测试了逻辑函数的CDF的不同参数化,并比较了结果和对不同参数的曲线的影响。

using Distributions

# Vector of x to test the different functions
x = collect(0:20)

Logis = Logistic(10, 1)  # PDF of Logistic function in Julia
y = cdf(Logis, x)       # CDF of Logistic function in Julia

# This is a standard representation of the CDF for Logistic
LogisticV1(x, μ=10, θ=1) = 1 / ( 1 + e^-((x-μ)/θ))   
y1 = LogisticV1.(x)

# This is another representation of the CDF for Logistic
LogisticV2(x, μ=10, θ=1) = 1/2 + 1/2 * tanh((x-μ)/2*θ)
y2 = LogisticV2.(x)

正如预期的那样,所有三个功能的图都是相同的。所有三个y向量的类型也是相同的(Array {Float64,1}),并且三个y向量也看起来是相同的。

show(y)

[4.53979e-5,0.000123395,0.00033535,0.000911051,0.00247262,0.00669285,0.0179862,0.0474259,0.119203,0.268941,0.5%,0.731059,0.880797,0.952574,0.982014,0.993307,0.997527,0.999089,0.999665,0.999877,0.999955] < / p>

show(y1)

[4.53979e-5,0.000123395,0.00033535,0.000911051,0.00247262,0.00669285,0.0179862,0.0474259,0.119203,0.268941,0.5%,0.731059,0.880797,0.952574,0.982014,0.993307,0.997527,0.999089,0.999665,0.999877,0.999955] < / p>

show(y2)

[4.53979e-5,0.000123395,0.00033535,0.000911051,0.00247262,0.00669285,0.0179862,0.0474259,0.119203,0.268941,0.5%,0.731059,0.880797,0.952574,0.982014,0.993307,0.997527,0.999089,0.999665,0.999877,0.999955] < / p>

然而:

y == y1    # true
y == y2    # false
y1 == y2   # false

为什么会这样?我认为这与LogisticV2中tanh函数引入的浮点变化有关,但我不确定。我很欣赏对此的任何见解。

编辑:修正了一些拼写错误,使代码可以运行

2 个答案:

答案 0 :(得分:0)

要比较浮点数,请使用isapprox而不是==

在您的情况下,您会看到isapprox(y,y1) == isapprox(y,y2) == isapprox(y1,y2) == true。此外,您可以检查maximum(abs.(y-y2))以查看差异是浮点精度的顺序(我找到1.1102230246251565e-16)。 (但请注意,isapprox默认检查相对偏差)

答案 1 :(得分:0)

  

我认为这与LogisticV2中的tanh函数引入的浮点变化有关。

你是对的:

julia> (y .== y1)'
1×21 RowVector{Bool,BitArray{1}}:
 true  true  true  true  true  true  true  true  true  true  true  true  true  true  true  true  true  true  true  true  true

julia> (y .== y2)'
1×21 RowVector{Bool,BitArray{1}}:
 false  false  false  false  false  false  false  false  false  true  true  true  false  false  true  false  false  true  false  false  false

可是:

julia> y ≈ y2    # \approx<TAB> for: ≈ symbol
true

isapprox的Unicode别名:

  

帮助&GT?; ≈

     

“≈”可以通过\ about

输入      

搜索:≈

     

isapprox(x, y; rtol::Real=sqrt(eps), atol::Real=0, nans::Bool=false, norm::Function)

     

不精确的平等比较:true如果norm(x-y) <= atol + tol*max(norm(x), norm(y))。默认atol为零,默认rtol取决于xy的类型。关键字参数nans确定NaN值是否被视为相等(默认为false)。

     

对于实数或复数浮点值,rtol默认为    sqrt(eps(typeof(real(x-y))))。这相当于要求平等    约有一半的有效数字。对于其他类型,rtol    默认为零。

     

xy也可能是数字数组,在这种情况下norm默认值   到vecnorm但可以通过传递norm::Function关键字进行更改   论点。 (对于数字,normabs相同。)xy时   是数组,如果norm(x-y)不是有限的(即±InfNaN),那么   比较回溯到检查xy的所有元素   在组件方面大致相等。

     

二元运算符相当于isapprox的默认参数,而x ≉ y相当于!isapprox(x,y)

julia> 0.1 ≈ (0.1 - 1e-10)   
true

julia> isapprox(10, 11; atol = 2)
true

julia> isapprox([10.0^9, 1.0], [10.0^9, 2.0])   
true