Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prod - Track the overall prod. #41

Closed
femtotrader opened this issue Apr 23, 2024 · 1 comment
Closed

Prod - Track the overall prod. #41

femtotrader opened this issue Apr 23, 2024 · 1 comment

Comments

@femtotrader
Copy link

femtotrader commented Apr 23, 2024

Hello,

quite similar to Sum... Prod could track the overall product.

I haven't find such a statistics.

Implementing it could be interesting.

Kind regards

PS: here is an implementation idea inspired by Sum implementation

"""
    Prod(T::Type = Float64)

Track the overall prod.
"""
mutable struct Prod{T} <: OnlineStat{Number}
    prod::T
    n::Int
end
Prod(T::Type = Float64) = Prod(one(T), 0)
Base.prod(o::Prod) = o.prod
OnlineStatsBase._fit!(o::Prod{T}, x::Real) where {T<:AbstractFloat} = (o.prod *= convert(T, x); o.n += 1)
OnlineStatsBase._fit!(o::Prod{T}, x::Real) where {T<:Integer} =       (o.prod *= round(T, x); o.n += 1)
OnlineStatsBase._fit!(o::Prod{T}, x::Real, n) where {T<:AbstractFloat} = (o.prod *= convert(T, x * n); o.n += n)
OnlineStatsBase._fit!(o::Prod{T}, x::Real, n) where {T<:Integer} =       (o.prod *= round(T, x * n); o.n += n)
OnlineStatsBase._merge!(o::T, o2::T) where {T <: Prod} = (o.prod *= o2.prod; o.n += o2.n; o)

PS2: here is my use cases
https://github.com/femtotrader/OnlinePortfolioAnalytics.jl/blob/9ab6010cfb604c1484cd338609c47b643a001750/src/mean_return.jl#L40
https://github.com/femtotrader/OnlinePortfolioAnalytics.jl/blob/9ab6010cfb604c1484cd338609c47b643a001750/src/drawdowns.jl#L14
https://github.com/femtotrader/OnlinePortfolioAnalytics.jl/blob/9ab6010cfb604c1484cd338609c47b643a001750/src/cumulative_return.jl#L12

@joshday
Copy link
Owner

joshday commented May 21, 2024

I'm hesitant to add this since there would need to be a lot of special care to handle overflow/underflow.

@joshday joshday closed this as completed May 28, 2024
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

No branches or pull requests

2 participants