You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Consider the following code for computing partial correlations:
functionpartialcor(X::AbstractVector, Y::AbstractVector, Z::AbstractMatrix)
p =size(Z, 2)
if p ==1returnpartialcor(X, Y, vec(Z))
end
Z₀ =view(Z, :, 1)
ZmZ₀ =view(Z, :, 2:p)
rxy =partialcor(X, Y, ZmZ₀)
rxz =partialcor(X, Z₀, ZmZ₀)
rzy =partialcor(Z₀, Y, ZmZ₀)
return (rxy - rxz * rzy) / (sqrt(1- rxz^2) *sqrt(1- rzy^2))
endfunctionpartialcor(X::AbstractVector, Y::AbstractVector, Z::AbstractVector)
rxy =cor(X, Y)
rxz =cor(X, Z)
rzy =cor(Z, Y)
return (rxy - rxz * rzy) / (sqrt(1- rxz^2) *sqrt(1- rzy^2))
end
On 0.6, this is inferred just fine:
julia> using Base.Test
julia> X = rand(10, 4);
julia> @inferred partialcor(X[:,1], X[:,2], X[:,3:4])
-0.2837674355125281
On current master, things aren't so peachy:
julia> using Test
julia> X = rand(10, 4);
julia> @inferred partialcor(X[:,1], X[:,2], X[:,3:4])
ERROR: return type Float64 does not match inferred return type Any
Stacktrace:
[1] error(::String) at ./error.jl:33
[2] top-level scope
Now, what's particularly intriguing about this example is what exactly isn't getting inferred:
The variable rxy is inferred to be Any, rather than Float64 like its r* buddies. This inferential confusion propagates through and the result is then inferred as Any. But if we rearrange the calls in the first method and add a type assert, things are okay again:
functionpartialcor(X::AbstractVector, Y::AbstractVector, Z::AbstractMatrix)
# Same stuff as before
rxz =partialcor(X, Z₀, ZmZ₀)
rzy =partialcor(Z₀, Y, ZmZ₀)
rxy =partialcor(X, Y, ZmZ₀)::typeof(rxz)
# ...
Then both rxy and the final result are correctly inferred.
The text was updated successfully, but these errors were encountered:
I don't know about this specific case, but yes, inferred types can sometimes get weaker. I often bring this up in debates on return_type and nobody seems to believe me, but it's true.
Not only that, tighter inferred types are not always better: the optimal inferrer would know exactly when concrete types matter and when they don't and not infer anything tighter than Any in the latter case.
Consider the following code for computing partial correlations:
On 0.6, this is inferred just fine:
On current master, things aren't so peachy:
Now, what's particularly intriguing about this example is what exactly isn't getting inferred:
The variable
rxy
is inferred to beAny
, rather thanFloat64
like itsr*
buddies. This inferential confusion propagates through and the result is then inferred asAny
. But if we rearrange the calls in the first method and add a type assert, things are okay again:Then both
rxy
and the final result are correctly inferred.The text was updated successfully, but these errors were encountered: