Skip to content

Commit a80f480

Browse files
Add table with distributions and docs for common interface (#90)
* add table with distributions and docs for common interface * fix docs * more appropriate notation
1 parent 81610b1 commit a80f480

File tree

3 files changed

+87
-23
lines changed

3 files changed

+87
-23
lines changed

docs/src/manual.md

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ All optimization methods can receive the following keyword arguments
1818
* `LB` - Lower bound of the initial points. Default is `0.0`.
1919
* `UB` - Upper bound of the initial points. Default is `0.6`.
2020

21-
The [ScoreDrivenModels.IPNewton](@ref) allows users to perform box-constrained optimization.
21+
[`ScoreDrivenModels.IPNewton`](@ref) allows users to perform box-constrained optimization.
2222

2323
```@docs
2424
ScoreDrivenModels.NelderMead
@@ -77,19 +77,38 @@ forecasts or ensembles of scenarios. Point forecasts are obtained using the func
7777
and ensembles of scenarios are obtained using the function `simulate`.
7878

7979
```@docs
80-
forecast
8180
forecast_quantiles
8281
simulate
8382
```
8483

8584
## ScoreDrivenModels distributions
8685

87-
The following section presents how every distribution is parametrized, its score, fisher information
86+
The following section presents how every distribution is parametrized, its score, Fisher information
8887
and the `time_varying_params` map. Every distribution is originally imported to ScoreDrivenModels.jl
89-
from [Distributions.jl](https://github.com/JuliaStats/Distributions.jl).
88+
from [Distributions.jl](https://github.com/JuliaStats/Distributions.jl). Some distributions may have different
89+
parametrizations from [Distributions.jl](https://github.com/JuliaStats/Distributions.jl),
90+
this is handled internally.
91+
92+
| Distribution | Identity scaling | Inverse and inverse square root scalings |
93+
| :------- |:---------------:|:--------------------:|
94+
|[`Beta`](@ref)|||
95+
|[`BetaLocationScale`](@ref)|| x |
96+
|[`Chi`](@ref)|||
97+
|[`Chisq`](@ref)|||
98+
|[`Exponential`](@ref)|||
99+
|[`Gamma`](@ref)|||
100+
|[`LogitNormal`](@ref)|||
101+
|[`LogNormal`](@ref)|||
102+
|[`Normal`](@ref)|||
103+
|[`Poisson`](@ref)|||
104+
|[`TDist`](@ref)|||
105+
|[`TDistLocationScale`](@ref)|||
106+
|[`Weibull`](@ref)|| x |
107+
90108

91109
```@docs
92110
ScoreDrivenModels.Beta
111+
ScoreDrivenModels.BetaLocationScale
93112
ScoreDrivenModels.Chi
94113
ScoreDrivenModels.Chisq
95114
ScoreDrivenModels.Exponential
@@ -108,21 +127,34 @@ ScoreDrivenModels.Weibull
108127
If you want to add a new distribution please feel free to make a pull request.
109128

110129
Each distribution must have the following methods:
111-
* `score`
112-
* `fisher information`
113-
* `log likelihood`
114-
* `link interface`
115-
* `link`
116-
* `unlink`
117-
* `jacobian_link`
118-
* `update_dist`
119-
* `num_params`
130+
* [`ScoreDrivenModels.score!`](@ref)
131+
* [`ScoreDrivenModels.fisher_information!`](@ref)
132+
* [`ScoreDrivenModels.log_likelihood`](@ref)
133+
* link interface
134+
* [`ScoreDrivenModels.link!`](@ref)
135+
* [`ScoreDrivenModels.unlink!`](@ref)
136+
* [`ScoreDrivenModels.jacobian_link!`](@ref)
137+
* [`ScoreDrivenModels.update_dist`](@ref)
138+
* [`ScoreDrivenModels.params_sdm`](@ref)
139+
* [`ScoreDrivenModels.num_params`](@ref)
120140

121141
The details of the new distribution must be documented following the example in
122142
[`Normal`](@ref) and added to the [ScoreDrivenModels distributions](@ref) section.
123143
The new implemented distribution must also be added to the constant `DISTS` and exported in the
124144
`distributions/common_interface.jl` file.
125145

146+
```@docs
147+
ScoreDrivenModels.score!
148+
ScoreDrivenModels.fisher_information!
149+
ScoreDrivenModels.log_likelihood
150+
ScoreDrivenModels.link!
151+
ScoreDrivenModels.unlink!
152+
ScoreDrivenModels.jacobian_link!
153+
ScoreDrivenModels.update_dist
154+
ScoreDrivenModels.params_sdm
155+
ScoreDrivenModels.num_params
156+
```
157+
126158
# Reference
127159

128160
```@docs

src/distributions/common_interface.jl

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ export Beta,
2929
TDistLocationScale,
3030
Weibull
3131

32+
"""
33+
score!(score_til::Matrix{T}, y::T, D::Type{<:Distribution}, param::Matrix{T}, t::Int) where T
34+
35+
Fill `score_til` with the score of distribution `D` with parameters `param[:, t]` considering
36+
the observation `y`.
37+
"""
3238
function score!(score_til::Matrix{T}, y::T, D::Type{<:Distribution}, param::Matrix{T}, t::Int) where T
3339
return error("score! not implemented for $D distribution")
3440
end
@@ -37,22 +43,48 @@ function score!(score_til::Matrix{T}, y::Int, D::Type{<:Distribution}, param::Ma
3743
return error("score! not implemented for $D distribution")
3844
end
3945

46+
"""
47+
fisher_information!(aux::AuxiliaryLinAlg{T}, D::Type{<:Distribution}, param::Matrix{T}, t::Int) where T
48+
49+
Fill `aux` with the fisher information of distribution `D` with parameters `param[:, t]`.
50+
"""
4051
function fisher_information!(aux::AuxiliaryLinAlg{T}, D::Type{<:Distribution}, param::Matrix{T}, t::Int) where T
4152
return error("fisher_information! not implemented for $D distribution")
4253
end
4354

55+
"""
56+
log_likelihood(D::Type{<:Distribution}, y::Vector{T}, param::Matrix{T}, n::Int) where T
57+
58+
Evaluate the log-likelihood of the distribution `D` considering the time-varying parameters
59+
`param` and the observations `y`.
60+
"""
4461
function log_likelihood(D::Type{<:Distribution}, y::Vector{T}, param::Matrix{T}, n::Int) where T
4562
return error("log_likelihood not implemented for $D distribution")
4663
end
4764

65+
"""
66+
link!(param_tilde::Matrix{T}, D::Type{<:Distribution}, param::Matrix{T}, t::Int) where T
67+
68+
Fill `param_tilde` after the unlinking procedure of `param`.
69+
"""
4870
function link!(param_tilde::Matrix{T}, D::Type{<:Distribution}, param::Matrix{T}, t::Int) where T
4971
return error("link! not implemented for $D distribution")
5072
end
5173

74+
"""
75+
unlink!(param::Matrix{T}, D::Type{<:Distribution}, param_tilde::Matrix{T}, t::Int) where T
76+
77+
Fill `param` after the unlinking procedure of `param_tilde`.
78+
"""
5279
function unlink!(param::Matrix{T}, D::Type{<:Distribution}, param_tilde::Matrix{T}, t::Int) where T
5380
return error("unlink! not implemented for $D distribution")
5481
end
5582

83+
"""
84+
jacobian_link!(aux::AuxiliaryLinAlg{T}, D::Type{<:Distribution}, param::Matrix{T}, t::Int) where T
85+
86+
Write the jacobian of the link map in `aux`.
87+
"""
5688
function jacobian_link!(aux::AuxiliaryLinAlg{T}, D::Type{<:Distribution}, param::Matrix{T}, t::Int) where T
5789
return error("jacobian_link! not implemented for $D distribution")
5890
end

src/simulate.jl

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
export simulate, forecast_quantiles
22

33
"""
4-
simulate(series::Vector{T}, gas::Model{D, T}, N::Int, S::Int, kwargs...) where {D, T}
4+
simulate(series::Vector{T}, gas::Model{D, T}, H::Int, S::Int, kwargs...) where {D, T}
55
6-
Generate scenarios for the future of a time series by updating the GAS recursion `N` times and taking
6+
Generate scenarios for the future of a time series by updating the GAS recursion `H` times and taking
77
a sample of the distribution until it generates `S` scenarios.
88
99
By default this method uses the `stationary_initial_params` method to perform the
1010
score driven recursion. If you estimated the model with a different set of `initial_params`
1111
use them here to maintain the coherence of your estimation.
1212
"""
13-
function simulate(series::Vector{T}, gas::Model{D, T}, N::Int, S::Int;
13+
function simulate(series::Vector{T}, gas::Model{D, T}, H::Int, S::Int;
1414
initial_params::Matrix{T} = stationary_initial_params(gas)) where {D, T}
1515
# Filter params estimated on the time series
1616
params = score_driven_recursion(gas, series; initial_params = initial_params)
@@ -19,9 +19,9 @@ function simulate(series::Vector{T}, gas::Model{D, T}, N::Int, S::Int;
1919

2020
params_simulation = params[(end - biggest_lag):(end - 1), :]
2121
# Create scenarios matrix
22-
scenarios = Matrix{T}(undef, N, S)
22+
scenarios = Matrix{T}(undef, H, S)
2323
for scenario in 1:S
24-
sim, param = simulate_recursion(gas, N + biggest_lag + 1; initial_params = params_simulation)
24+
sim, param = simulate_recursion(gas, H + biggest_lag + 1; initial_params = params_simulation)
2525
scenarios[:, scenario] = sim[biggest_lag + 2:end]
2626
end
2727

@@ -30,9 +30,9 @@ end
3030

3131

3232
"""
33-
forecast_quantiles(series::Vector{T}, gas::Model{D, T}, N::Int; kwargs...) where {D, T}
33+
forecast_quantiles(series::Vector{T}, gas::Model{D, T}, H::Int; kwargs...) where {D, T}
3434
35-
Forecast quantiles for future values of a time series by updating the GAS recursion `N` times and
35+
Forecast quantiles for future values of a time series by updating the GAS recursion `H` times and
3636
using Monte Carlo method as in Blasques, Francisco, Siem Jan Koopman,
3737
Katarzyna Lasak and Andre Lucas (2016): "In-Sample Confidence Bounds and Out-of-Sample Forecast
3838
Bands for Time-Varying Parameters in Observation Driven Models",
@@ -47,11 +47,11 @@ By default this method uses the `stationary_initial_params` method to perform th
4747
score driven recursion. If you estimated the model with a different set of `initial_params`
4848
use them here to maintain the coherence of your estimation.
4949
"""
50-
function forecast_quantiles(series::Vector{T}, gas::Model{D, T}, N::Int;
50+
function forecast_quantiles(series::Vector{T}, gas::Model{D, T}, H::Int;
5151
initial_params::Matrix{T} = stationary_initial_params(gas),
52-
quantiles::Vector{T} = T.([0.025, 0.5, 0.975]), S::Int = 1000) where {D, T}
52+
quantiles::Vector{T} = T.([0.025, 0.5, 0.975]), S::Int = 10_000) where {D, T}
5353

54-
scenarios = simulate(series, gas, N, S; initial_params = initial_params)
54+
scenarios = simulate(series, gas, H, S; initial_params = initial_params)
5555

5656
return get_quantiles(quantiles, scenarios)
5757
end

0 commit comments

Comments
 (0)