Skip to content

Commit

Permalink
include ridge2f
Browse files Browse the repository at this point in the history
  • Loading branch information
thierrymoudiki committed Jan 12, 2024
1 parent b5f8941 commit 099de76
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 4 deletions.
23 changes: 22 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,29 @@
# Ahead

[![Build Status](https://github.com/thierrymoudiki/Ahead.jl/actions/workflows/CI.yml/badge.svg?branch=main)](https://github.com/thierrymoudiki/Ahead.jl/actions/workflows/CI.yml?query=branch%3Amain)
[![Build Status](https://github.com/Techtonique/Ahead.jl/actions/workflows/CI.yml/badge.svg?branch=main)](https://github.com/Techtonique/Ahead.jl/actions/workflows/CI.yml?query=branch%3Amain) [![HitCount](https://hits.dwyl.com/Techtonique/Aheadjl.svg?style=flat-square)](http://hits.dwyl.com/Techtonique/Aheadjl)

**Univariate and multivariate time series forecasting, with uncertainty quantification**.

## Installation

From GitHub (for now):

```julia
julia> using Pkg; Pkg.add("https://github.com/Techtonique/Ahead.jl")
````

Ultimately:

```julia
julia> using Pkg; Pkg.add("Ahead")
```

## Usage

```julia
julia> using Ahead
julia> print(Ahead.dynrmf([1,2,3,4,5,6,7,8,9,10]))
```

## Note to self or developers

Expand Down
110 changes: 107 additions & 3 deletions src/Ahead.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module Ahead

# exports
export dynrmf
export ridge2f

if Sys.islinux()
run(`ls -la`)
Expand Down Expand Up @@ -52,10 +53,113 @@ module Ahead

R"load_ahead <- try(library(ahead), silent = TRUE)"

function dynrmf(x)
# https://juliainterop.github.io/RCall.jl/stable/custom/#Nested-conversion
return rcopy(R"try(ahead::dynrmf($x), silent = TRUE)")
# univariate time series forecasting models ----------------------------------------------

"""
Dynamic regression model (see https://techtonique.r-universe.dev/ahead/doc/manual.html#dynrmf)
# Details
For now, the function uses only Ridge regression with automatic selection of the regularization parameter.
# Examples
```julia
using Ahead
val = Ahead.dynrmf([1,2,3,4,5,6,7,8,9,10], h = 5, level = 95)
println(val)
````
"""
function dynrmf(y, h = 5, level = 95)
r_code = """
ahead::dynrmf(y = $y, h = $h, level = $level)
"""
return rcopy(R"$r_code")
end

# multivariate time series forecasting models ----------------------------------------------

"""
Random Vector functional link (RVFL) nnetwork model with 2 regularization parameters (see https://techtonique.r-universe.dev/ahead/doc/manual.html#ridge2f)
# Details
The model is based on an RVFL model (see https://www.mdpi.com/2227-9091/6/1/22) with 2 regularization parameters, and provides some uncertainty quantification.
# Examples
```julia
using Ahead
using Distributions
import Random
Random.seed!(123)
y = rand(100, 5)
print(ridge2f(y, h = 5, level = 95))
```
"""
function ridge2f(
y,
xreg = nothing,
h = 5,
level = 95,
lags = 1,
nb_hidden = 5,
nodes_sim = "sobol",
activ = "relu",
a = 0.01,
lambda_1 = 0.1,
lambda_2 = 0.1,
dropout = 0,
type_forecast = "recursive",
type_pi = "gaussian",
block_length = nothing,
margins = "gaussian",
seed = 1,
B = 100,
type_aggregation = "mean",
centers = nothing,
type_clustering = "kmeans",
ym = nothing,
cl = 1,
show_progress = true,
args...
)

if xreg == nothing
xreg = RNULL()
end

if block_length == nothing
block_length = RNULL()
end

if centers == nothing
centers = RNULL()
end

if ym == nothing
ym = RNULL()
end

r_code = """
ahead::ridge2f(y = $y, xreg = $xreg, h = $h,
level = $level, lags = $lags, nb_hidden = $nb_hidden,
nodes_sim = $nodes_sim, activ = $activ, a = $a,
lambda_1 = $lambda_1, lambda_2 = $lambda_2,
dropout = $dropout, type_forecast = $type_forecast,
type_pi = $type_pi, block_length = $block_length,
margins = $margins, seed = $seed, B = $B,
type_aggregation = $type_aggregation,
centers = $centers, type_clustering = $type_clustering,
ym = $ym, cl = $cl, show_progress = $show_progress,
$args...)
"""

return rcopy(R"$r_code")

end

end

13 changes: 13 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,19 @@ using Ahead
using Test

@testset "Ahead.jl" begin

using Ahead
using Distributions
import Random

# Test Ahead.dynrmf ----------------------------
val = Ahead.dynrmf([1,2,3,4,5,6,7,8,9,10])
@test isapprox(round(val[:residuals][1]), 0)

# Test Ahead.ridge2f ----------------------------
Random.seed!(123)
y = rand(100, 5)
val = Ahead.ridge2f(y, h = 5, level = 95)
@test val[:x] == y

end

0 comments on commit 099de76

Please sign in to comment.