-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmaxima.jl
58 lines (45 loc) · 1.21 KB
/
maxima.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
53
54
55
56
57
58
# ------------------------------------------------------------------
# Licensed under the MIT License. See LICENCE in the project root.
# ------------------------------------------------------------------
"""
AbstractMaxima
A collection of maxima (e.g. block maxima)
"""
abstract type AbstractMaxima end
"""
collect(maxima)
Collect `maxima` values using appropriate model.
"""
Base.collect(m::AbstractMaxima) = error("not implemented")
#------------------
# IMPLEMENTATIONS
#------------------
"""
BlockMaxima(x, n)
Maxima obtained by splitting the data `x` into blocks of size `n`.
"""
struct BlockMaxima{V<:AbstractVector} <: AbstractMaxima
x::V
n::Int
end
function Base.collect(m::BlockMaxima)
result = Vector{Float64}()
for i in 1:(m.n):(length(m.x) - m.n + 1)
xs = skipmissing(view(m.x, i:(i + m.n - 1)))
xmax = -Inf
for x in xs
x > xmax && (xmax = x)
end
isinf(xmax) || push!(result, xmax)
end
result
end
"""
PeakOverThreshold(x, u)
Maxima obtained by thresholding the data `x` with threshold `u`.
"""
struct PeakOverThreshold{V<:AbstractVector} <: AbstractMaxima
x::V
u::Float64
end
Base.collect(m::PeakOverThreshold) = filter(x -> !ismissing(x) && x > m.u, m.x)