Skip to content

Conversation

@abhro
Copy link
Contributor

@abhro abhro commented Oct 21, 2025

Solves #968

src/moments.jl Outdated
function _moment2(v::AbstractArray{<:Real}, m::Real; corrected=false)
n = length(v)
s = 0.0
s = zero(m * m)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general, s does not have the correct/desired type here as this initialization purely depends on m and neglects v.

Copy link
Contributor Author

@abhro abhro Oct 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was making the assumption that m and v would have compatible/identical types, since m is calculated from v and the function is only called internally. Would an implementation involving v be something like zero(v[begin] * v[begin])?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO we shouldn't make that assumption. For instance, in var(...; mean=...) users can easily provide a mean that is of different type.

Would an implementation involving v be something like zero(v[begin] * v[begin])

No, that would error if the array is empty and would neglect the type of m.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would the most recent commit (6a236d4) work for this?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, not in general.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll have to say, I'm confused where it would fail 😅

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For instance if v::AbstractVector{Bool} and m::Bool:

julia> v = [true, true];

julia> m = true;

julia> zero(promote_type(eltype(v), typeof(m)))
false

julia> (v[1] - m)^2
0

I also noticed that one actually has to make a few more changes to ensure that e.g. Float32 inputs don't yield Float64 outputs. I opened #973 to address this issue more generally.

@abhro abhro marked this pull request as ready for review October 27, 2025 18:23
@abhro
Copy link
Contributor Author

abhro commented Oct 30, 2025

Made obsolete by #973 :D

@abhro abhro closed this Oct 30, 2025
@abhro abhro deleted the moment-types branch October 30, 2025 12:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants