Skip to content

Commit feb92dc

Browse files
author
Raphael Saavedra
committed
Rebase
2 parents 91b4ce3 + 04456d6 commit feb92dc

File tree

8 files changed

+42
-40
lines changed

8 files changed

+42
-40
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ jobs:
1515
- version: '1'
1616
os: ubuntu-latest
1717
arch: x64
18-
- version: '1.0'
18+
- version: '1.6'
1919
os: ubuntu-latest
2020
arch: x64
21-
- version: '1.0'
21+
- version: '1.6'
2222
os: ubuntu-latest
2323
arch: x86
2424
steps:

.github/workflows/documentation.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ jobs:
1212
- uses: actions/checkout@v2
1313
- uses: julia-actions/setup-julia@latest
1414
with:
15-
# Build documentation on Julia 1.0
16-
version: '1.0'
15+
# Build documentation on Julia 1.6
16+
version: '1.6'
1717
- name: Install dependencies
1818
run: julia --project=docs/ -e 'using Pkg; Pkg.develop(PackageSpec(path=pwd())); Pkg.instantiate()'
1919
- name: Build and deploy

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ SpecialFunctions = "276daf66-3868-5448-9aa4-cd146d93841b"
1414
StatsBase = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91"
1515

1616
[compat]
17-
Distributions = "0.23, 0.24, 0.25"
17+
Distributions = "0.25"
1818
HypothesisTests = "0.10"
1919
Optim = "1.2"
2020
RecipesBase = "1"

src/ScoreDrivenModels.jl

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
module ScoreDrivenModels
22

3-
using Distributions, Optim, SpecialFunctions, HypothesisTests, RecipesBase, StatsBase
4-
using LinearAlgebra, Printf
3+
using Distributions
4+
using Distributions: AffineDistribution, value_support
5+
using HypothesisTests
6+
using LinearAlgebra
7+
using Optim
8+
using Printf
9+
using RecipesBase
10+
using SpecialFunctions
11+
using StatsBase
512

613
import Base: length, deepcopy, show
714

src/distributions/betafour.jl

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* `time_varying_params` map.
77
* Default link
88
9-
Right now when estimating a BetaFourParameters model is recomended to provide fixed parameters a and c, dont
9+
Right now when estimating a BetaFourParameters model is recomended to provide fixed parameters a and c, dont
1010
with the code:
1111
1212
```julia
@@ -15,49 +15,49 @@ gas_beta_4.ω[1] = minimum(y) - 0.1*std(y) # parameter a
1515
gas_beta_4.ω[2] = maximum(y) + 0.1*std(y) # parameter c
1616
```
1717
18-
This code is a simple and not very accurate heuristic on how to estimate the a and c parameters. If you have estimated
18+
This code is a simple and not very accurate heuristic on how to estimate the a and c parameters. If you have estimated
1919
using maximum likelihood it will be probably better
2020
21-
The fact occurs because if in the optimization the gradient makes `c < maximum(y)` or `a > minimum(y)` then it
21+
The fact occurs because if in the optimization the gradient makes `c < maximum(y)` or `a > minimum(y)` then it
2222
is impossible that y comes from the BetaFourParameters distribution leading to DomainErrors
2323
"""
2424
BetaFourParameters
2525

2626
function score!(score_til::Matrix{T}, y::T, ::Type{BetaFourParameters}, param::Matrix{T}, t::Int) where T
2727
score_til[t, 1] = -(param[t, 3] - 1)/(y - param[t, 1]) + (param[t, 4] + param[t, 3] - 1)/(param[t, 2] - param[t, 1])
2828
score_til[t, 2] = (param[t, 4] - 1)/(param[t, 2] - y) - (param[t, 4] + param[t, 3] - 1)/(param[t, 2] - param[t, 1])
29-
score_til[t, 3] = log(y - param[t, 1]) - log(param[t, 2] - param[t, 1]) +
29+
score_til[t, 3] = log(y - param[t, 1]) - log(param[t, 2] - param[t, 1]) +
3030
digamma(param[t, 3] + param[t, 4]) - digamma(param[t, 3])
31-
score_til[t, 4] = log(param[t, 2] - y) - log(param[t, 2] - param[t, 1]) +
31+
score_til[t, 4] = log(param[t, 2] - y) - log(param[t, 2] - param[t, 1]) +
3232
digamma(param[t, 3] + param[t, 4]) - digamma(param[t, 4])
3333
return
3434
end
3535

3636
function log_likelihood(::Type{BetaFourParameters}, y::Vector{T}, param::Matrix{T}, n::Int) where T
3737
loglik = 0.0
3838
for t in 1:n
39-
loglik += (param[t, 3] - 1)*log(y[t] - param[t, 1]) + (param[t, 4] - 1) * log(param[t, 2] - y[t]) -
39+
loglik += (param[t, 3] - 1)*log(y[t] - param[t, 1]) + (param[t, 4] - 1) * log(param[t, 2] - y[t]) -
4040
(param[t, 3] + param[t, 4] - 1) * log(param[t, 2] - param[t, 1]) - logbeta(param[t, 3], param[t, 4])
4141
end
4242
return -loglik
4343
end
4444

4545
# Links
46-
function link!(param_tilde::Matrix{T}, ::Type{BetaFourParameters}, param::Matrix{T}, t::Int) where T
46+
function link!(param_tilde::Matrix{T}, ::Type{BetaFourParameters}, param::Matrix{T}, t::Int) where T
4747
param_tilde[t, 1] = link(IdentityLink, param[t, 1])
4848
param_tilde[t, 2] = link(IdentityLink, param[t, 2])
4949
param_tilde[t, 3] = link(LogLink, param[t, 3], zero(T))
5050
param_tilde[t, 4] = link(LogLink, param[t, 4], zero(T))
5151
return
5252
end
53-
function unlink!(param::Matrix{T}, ::Type{BetaFourParameters}, param_tilde::Matrix{T}, t::Int) where T
53+
function unlink!(param::Matrix{T}, ::Type{BetaFourParameters}, param_tilde::Matrix{T}, t::Int) where T
5454
param[t, 1] = unlink(IdentityLink, param_tilde[t, 1])
5555
param[t, 2] = unlink(IdentityLink, param_tilde[t, 2])
5656
param[t, 3] = unlink(LogLink, param_tilde[t, 3], zero(T))
5757
param[t, 4] = unlink(LogLink, param_tilde[t, 4], zero(T))
5858
return
5959
end
60-
function jacobian_link!(aux::AuxiliaryLinAlg{T}, ::Type{BetaFourParameters}, param::Matrix{T}, t::Int) where T
60+
function jacobian_link!(aux::AuxiliaryLinAlg{T}, ::Type{BetaFourParameters}, param::Matrix{T}, t::Int) where T
6161
aux.jac[1] = jacobian_link(IdentityLink, param[t, 1])
6262
aux.jac[2] = jacobian_link(IdentityLink, param[t, 2])
6363
aux.jac[3] = jacobian_link(LogLink, param[t, 3], zero(T))
@@ -69,8 +69,8 @@ end
6969
function update_dist(::Type{BetaFourParameters}, param::Matrix{T}, t::Int) where T
7070
small_threshold!(param[:, 3:4], SMALL_NUM, t)
7171
beta = Beta(param[t, 3], param[t, 4])
72-
return LocationScale(param[t, 1], param[t, 2] - param[t, 1], beta)
73-
end
72+
return AffineDistribution(param[t, 1], param[t, 2] - param[t, 1], beta)
73+
end
7474

7575
function params_sdm(d::BetaFourParameters)
7676
return (d.μ, d.σ + d.μ, Distributions.params(d.ρ)...)
@@ -79,8 +79,3 @@ end
7979
function num_params(::Type{BetaFourParameters})
8080
return 4
8181
end
82-
83-
# d = Beta(1, 1)
84-
# d2 = LocationScale(-1, 3, d)
85-
# maximum(d2)
86-
# minimum(d2)
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Define TDistLocationScale as the location scale transformation of the Distributions.jl TDist
22
export TDistLocationScale
3-
TDistLocationScale = LocationScale{Float64,TDist{Float64}}
3+
TDistLocationScale = AffineDistribution{Float64,value_support(TDist),TDist{Float64}}
44

55
export BetaFourParameters
6-
BetaFourParameters = LocationScale{Float64,Beta{Float64}}
6+
BetaFourParameters = AffineDistribution{Float64,value_support(Beta),Beta{Float64}}

src/distributions/tdistlocationscale.jl

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ TDistLocationScale
1616

1717
function score!(score_til::Matrix{T}, y::T, ::Type{TDistLocationScale}, param::Matrix{T}, t::Int) where T
1818
score_til[t, 1] = ((param[t, 3] + 1) * (y - param[t, 1])) / ((y - param[t, 1])^2 + param[t, 2] * param[t, 3])
19-
score_til[t, 2] = -(param[t, 3] * (param[t, 2] - (y - param[t, 1])^2)) / (2 * param[t, 2] * (param[t, 3] * param[t, 2] +
19+
score_til[t, 2] = -(param[t, 3] * (param[t, 2] - (y - param[t, 1])^2)) / (2 * param[t, 2] * (param[t, 3] * param[t, 2] +
2020
(y - param[t, 1])^2))
2121
score_til[t, 3] = ((((y - param[t, 1])^2)*(param[t, 3] + 1)/(param[t, 3] * (y - param[t, 1])^2 + param[t, 2] * param[t, 3]^2)) -
22-
1/param[t, 3] -
23-
log(((y - param[t, 1])^2)/(param[t, 2] * param[t, 3]) + 1) -
22+
1/param[t, 3] -
23+
log(((y - param[t, 1])^2)/(param[t, 2] * param[t, 3]) + 1) -
2424
digamma(param[t, 3] / 2) + digamma((param[t, 3] + 1) / 2)) / 2
2525
return
2626
end
@@ -49,35 +49,35 @@ function log_likelihood(::Type{TDistLocationScale}, y::Vector{T}, param::Matrix{
4949
end
5050

5151
# Links
52-
function link!(param_tilde::Matrix{T}, ::Type{TDistLocationScale}, param::Matrix{T}, t::Int) where T
52+
function link!(param_tilde::Matrix{T}, ::Type{TDistLocationScale}, param::Matrix{T}, t::Int) where T
5353
param_tilde[t, 1] = link(IdentityLink, param[t, 1])
5454
param_tilde[t, 2] = link(LogLink, param[t, 2], zero(T))
5555
param_tilde[t, 3] = link(LogLink, param[t, 3], zero(T))
5656
return
5757
end
58-
function unlink!(param::Matrix{T}, ::Type{TDistLocationScale}, param_tilde::Matrix{T}, t::Int) where T
58+
function unlink!(param::Matrix{T}, ::Type{TDistLocationScale}, param_tilde::Matrix{T}, t::Int) where T
5959
param[t, 1] = unlink(IdentityLink, param_tilde[t, 1])
6060
param[t, 2] = unlink(LogLink, param_tilde[t, 2], zero(T))
6161
param[t, 3] = unlink(LogLink, param_tilde[t, 3], zero(T))
6262
return
6363
end
64-
function jacobian_link!(aux::AuxiliaryLinAlg{T}, ::Type{TDistLocationScale}, param::Matrix{T}, t::Int) where T
64+
function jacobian_link!(aux::AuxiliaryLinAlg{T}, ::Type{TDistLocationScale}, param::Matrix{T}, t::Int) where T
6565
aux.jac[1] = jacobian_link(IdentityLink, param[t, 1])
6666
aux.jac[2] = jacobian_link(LogLink, param[t, 2], zero(T))
6767
aux.jac[3] = jacobian_link(LogLink, param[t, 3], zero(T))
6868
return
6969
end
7070

71-
# utils
71+
# utils
7272
function update_dist(::Type{TDistLocationScale}, param::Matrix{T}, t::Int) where T
7373
tdist = TDist(param[t, 3])
74-
return LocationScale(param[t, 1], sqrt(param[t, 2]), tdist)
75-
end
74+
return AffineDistribution(param[t, 1], sqrt(param[t, 2]), tdist)
75+
end
7676

7777
function params_sdm(d::TDistLocationScale)
7878
return (d.μ, d.σ^2, Distributions.params(d.ρ)...)
7979
end
8080

8181
function num_params(::Type{TDistLocationScale})
8282
return 3
83-
end
83+
end

test/utils.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ function instantiate_dist(D::Type{<:Distribution})
2020
end
2121
end
2222

23-
function test_score_mean(D::Type{<:Distribution}; n::Int = 10^7, seed::Int = 10,
24-
atol::Float64 = 1e-3, rtol::Float64 = 1e-3)
23+
function test_score_mean(D::Type{<:Distribution}; n::Int = 10^6, seed::Int = 10,
24+
atol::Float64 = 1e-2, rtol::Float64 = 1e-2)
2525
Random.seed!(seed)
2626
dist = instantiate_dist(D)
2727
pars = permutedims([ScoreDrivenModels.params_sdm(dist)...])
@@ -55,7 +55,7 @@ function test_fisher_information(D::Type{<:Distribution}; n::Int = 10^6, seed::I
5555

5656
# Some distributions might not have the fisher information yet available
5757
if D in FI_NOT_IMPLEMENTED
58-
return
58+
return
5959
else
6060
ScoreDrivenModels.fisher_information!(aux_lin_alg, D, pars, 1)
6161
end
@@ -94,10 +94,10 @@ end
9494

9595
function test_dist_utils(D::Type{<:Distribution})
9696
# num_params
97-
dist = instantiate_dist(D)
97+
dist = instantiate_dist(D)
9898
n_pars = ScoreDrivenModels.num_params(D)
9999
@test n_pars == length(ScoreDrivenModels.params_sdm(dist))
100-
100+
101101
# update_dist
102102
t = 1
103103
pars = permutedims([ScoreDrivenModels.params_sdm(dist)...])

0 commit comments

Comments
 (0)