-
Notifications
You must be signed in to change notification settings - Fork 3
/
CLUSLOG.jl
97 lines (76 loc) · 3.11 KB
/
CLUSLOG.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
"""
cluslog(
rel_pr::AbstractMatrix{<:AbstractFloat},
horizon::Int,
TW::Int,
clus_mod::Type{<:ClusLogVariant},
nclusters::Int,
nclustering::Int,
boundries::NTuple{2, AbstractFloat};
progress::Bool=true
)
Run KMNLOG, KMDLOG, etc., algorithms on the given data.
!!! note "Important note"
In order to use this function, you have to install the \
[Clustering.jl](https://github.com/JuliaStats/Clustering.jl) package first, and then \
import it along with the OnlinePortfolioSelection.jl package:
```julia
julia> using Pkg; Pkg.add(name="Clustering", version="0.15.2")
julia> using OnlinePortfolioSelection, Clustering
```
# Arguments
- `rel_pr::AbstractMatrix{<:AbstractFloat}`: Relative prices of assets. Each column \
represents the price of an asset at a given time.
- `horizon::Int`: Number of trading days.
- `TW::Int`: Maximum time window length to be examined.
- `clus_mod::Type{<:ClusLogVariant}`: Clustering model to be used. Currently, only \
[`KMNLOG`](@ref) and [`KMDLOG`](@ref) are supported.
- `nclusters::Int`: The maximum number of clusters to be examined.
- `nclustering::Int`: The number of times clustering algorithm is run for optimal \
number of clusters.
- `boundries::NTuple{2, AbstractFloat}`: The lower and upper boundries for the \
weights of assets in the portfolio.
# Keyword Arguments
- `progress::Bool=true`: Whether to log the progress or not.
!!! warning "Beware!"
`rel_pr` should be a matrix of size `n_assets` × `n_periods`.
# Returns
- `::OPSAlgorithm`: An [`OPSAlgorithm`](@ref) object.
# Example
Two clustering model is available as of now: [`KMNLOG`](@ref), and [`KMDLOG`](@ref). \
The first example utilizes [`KMNLOG`](@ref):
```julia
julia> using OnlinePortfolioSelection, Clustering
julia> adj_close = [
1.5464 1.5852 1.6532 1.7245 1.5251 1.4185 1.2156 1.3231 1.3585 1.4563 1.4456
1.2411 1.2854 1.3456 1.4123 1.5212 1.5015 1.4913 1.5212 1.5015 1.4913 1.5015
1.3212 1.3315 1.3213 1.3153 1.3031 1.2913 1.2950 1.2953 1.3315 1.3213 1.3315
]
julia> rel_pr = adj_close[:, 2:end]./adj_close[:, 1:end-1]
julia> horizon = 3; TW = 3; nclusters_ = 3; nclustering = 10; lb, ub = 0.0, 1.;
julia> model = cluslog(rel_pr, horizon, TW, KMNLOG, nclusters_, nclustering, (lb, ub));
julia> model.b
3×3 Matrix{Float64}:
0.00264911 0.00317815 0.148012
0.973581 0.971728 0.848037
0.02377 0.0250939 0.00395028
julia> sum(model.b , dims=1) .|> isapprox(1.) |> all
true
```
The same approach works for [`KMDLOG`](@ref) as well:
```julia
julia> using OnlinePortfolioSelection, Clustering
julia> model = cluslog(rel_pr, horizon, TW, KMDLOG, nclusters_, nclustering, (lb, ub));
julia> model.b
3×3 Matrix{Float64}:
4.59938e-7 4.96421e-7 4.89426e-7
0.999998 0.999997 0.999997
2.02964e-6 2.02787e-6 2.02964e-6
julia> sum(model.b , dims=1) .|> isapprox(1.) |> all
true
```
See also [`KMNLOG`](@ref), and [`KMDLOG`](@ref).
# Reference
> [An online portfolio selection algorithm using clustering approaches and considering transaction costs](https://doi.org/10.1016/j.eswa.2020.113546)
"""
function cluslog end