-
Notifications
You must be signed in to change notification settings - Fork 1
/
mean_return.jl
52 lines (39 loc) · 1.07 KB
/
mean_return.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
abstract type AbstractMeanReturn{T} <: PortfolioAnalyticsSingleOutput{T} end
@doc """
$(TYPEDEF)
ArithmeticMeanReturn{T}()
The `ArithmeticMeanReturn` type implements arithmetic mean returns calculations.
"""
mutable struct ArithmeticMeanReturn{T} <: AbstractMeanReturn{T}
value::T
n::Int
sum::Sum
function ArithmeticMeanReturn{T}() where {T}
s = Sum()
new{T}(zero(T), 0, s)
end
end
function OnlineStatsBase._fit!(stat::ArithmeticMeanReturn, data)
fit!(stat.sum, data)
stat.n += 1
stat.value = value(stat.sum) / stat.n
end
@doc """
$(TYPEDEF)
GeometricMeanReturn{T}()
The `GeometricMeanReturn` type implements geometric mean returns calculations.
"""
mutable struct GeometricMeanReturn{T} <: AbstractMeanReturn{T}
value::T
n::Int
prod::Prod
function GeometricMeanReturn{T}() where {T}
p = Prod(T)
new{T}(zero(T), 0, p)
end
end
function OnlineStatsBase._fit!(stat::GeometricMeanReturn, data)
fit!(stat.prod, 1 + data)
stat.n += 1
stat.value = value(stat.prod)^(1 / stat.n) - 1
end