Skip to content

Commit

Permalink
Rebase
Browse files Browse the repository at this point in the history
  • Loading branch information
Raphael Saavedra committed Nov 27, 2022
2 parents 91b4ce3 + 04456d6 commit feb92dc
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 40 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ jobs:
- version: '1'
os: ubuntu-latest
arch: x64
- version: '1.0'
- version: '1.6'
os: ubuntu-latest
arch: x64
- version: '1.0'
- version: '1.6'
os: ubuntu-latest
arch: x86
steps:
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/documentation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ jobs:
- uses: actions/checkout@v2
- uses: julia-actions/setup-julia@latest
with:
# Build documentation on Julia 1.0
version: '1.0'
# Build documentation on Julia 1.6
version: '1.6'
- name: Install dependencies
run: julia --project=docs/ -e 'using Pkg; Pkg.develop(PackageSpec(path=pwd())); Pkg.instantiate()'
- name: Build and deploy
Expand Down
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ SpecialFunctions = "276daf66-3868-5448-9aa4-cd146d93841b"
StatsBase = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91"

[compat]
Distributions = "0.23, 0.24, 0.25"
Distributions = "0.25"
HypothesisTests = "0.10"
Optim = "1.2"
RecipesBase = "1"
Expand Down
11 changes: 9 additions & 2 deletions src/ScoreDrivenModels.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
module ScoreDrivenModels

using Distributions, Optim, SpecialFunctions, HypothesisTests, RecipesBase, StatsBase
using LinearAlgebra, Printf
using Distributions
using Distributions: AffineDistribution, value_support
using HypothesisTests
using LinearAlgebra
using Optim
using Printf
using RecipesBase
using SpecialFunctions
using StatsBase

import Base: length, deepcopy, show

Expand Down
27 changes: 11 additions & 16 deletions src/distributions/betafour.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* `time_varying_params` map.
* Default link
Right now when estimating a BetaFourParameters model is recomended to provide fixed parameters a and c, dont
Right now when estimating a BetaFourParameters model is recomended to provide fixed parameters a and c, dont
with the code:
```julia
Expand All @@ -15,49 +15,49 @@ gas_beta_4.ω[1] = minimum(y) - 0.1*std(y) # parameter a
gas_beta_4.ω[2] = maximum(y) + 0.1*std(y) # parameter c
```
This code is a simple and not very accurate heuristic on how to estimate the a and c parameters. If you have estimated
This code is a simple and not very accurate heuristic on how to estimate the a and c parameters. If you have estimated
using maximum likelihood it will be probably better
The fact occurs because if in the optimization the gradient makes `c < maximum(y)` or `a > minimum(y)` then it
The fact occurs because if in the optimization the gradient makes `c < maximum(y)` or `a > minimum(y)` then it
is impossible that y comes from the BetaFourParameters distribution leading to DomainErrors
"""
BetaFourParameters

function score!(score_til::Matrix{T}, y::T, ::Type{BetaFourParameters}, param::Matrix{T}, t::Int) where T
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])
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])
score_til[t, 3] = log(y - param[t, 1]) - log(param[t, 2] - param[t, 1]) +
score_til[t, 3] = log(y - param[t, 1]) - log(param[t, 2] - param[t, 1]) +
digamma(param[t, 3] + param[t, 4]) - digamma(param[t, 3])
score_til[t, 4] = log(param[t, 2] - y) - log(param[t, 2] - param[t, 1]) +
score_til[t, 4] = log(param[t, 2] - y) - log(param[t, 2] - param[t, 1]) +
digamma(param[t, 3] + param[t, 4]) - digamma(param[t, 4])
return
end

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

# Links
function link!(param_tilde::Matrix{T}, ::Type{BetaFourParameters}, param::Matrix{T}, t::Int) where T
function link!(param_tilde::Matrix{T}, ::Type{BetaFourParameters}, param::Matrix{T}, t::Int) where T
param_tilde[t, 1] = link(IdentityLink, param[t, 1])
param_tilde[t, 2] = link(IdentityLink, param[t, 2])
param_tilde[t, 3] = link(LogLink, param[t, 3], zero(T))
param_tilde[t, 4] = link(LogLink, param[t, 4], zero(T))
return
end
function unlink!(param::Matrix{T}, ::Type{BetaFourParameters}, param_tilde::Matrix{T}, t::Int) where T
function unlink!(param::Matrix{T}, ::Type{BetaFourParameters}, param_tilde::Matrix{T}, t::Int) where T
param[t, 1] = unlink(IdentityLink, param_tilde[t, 1])
param[t, 2] = unlink(IdentityLink, param_tilde[t, 2])
param[t, 3] = unlink(LogLink, param_tilde[t, 3], zero(T))
param[t, 4] = unlink(LogLink, param_tilde[t, 4], zero(T))
return
end
function jacobian_link!(aux::AuxiliaryLinAlg{T}, ::Type{BetaFourParameters}, param::Matrix{T}, t::Int) where T
function jacobian_link!(aux::AuxiliaryLinAlg{T}, ::Type{BetaFourParameters}, param::Matrix{T}, t::Int) where T
aux.jac[1] = jacobian_link(IdentityLink, param[t, 1])
aux.jac[2] = jacobian_link(IdentityLink, param[t, 2])
aux.jac[3] = jacobian_link(LogLink, param[t, 3], zero(T))
Expand All @@ -69,8 +69,8 @@ end
function update_dist(::Type{BetaFourParameters}, param::Matrix{T}, t::Int) where T
small_threshold!(param[:, 3:4], SMALL_NUM, t)
beta = Beta(param[t, 3], param[t, 4])
return LocationScale(param[t, 1], param[t, 2] - param[t, 1], beta)
end
return AffineDistribution(param[t, 1], param[t, 2] - param[t, 1], beta)
end

function params_sdm(d::BetaFourParameters)
return (d.μ, d.σ + d.μ, Distributions.params(d.ρ)...)
Expand All @@ -79,8 +79,3 @@ end
function num_params(::Type{BetaFourParameters})
return 4
end

# d = Beta(1, 1)
# d2 = LocationScale(-1, 3, d)
# maximum(d2)
# minimum(d2)
4 changes: 2 additions & 2 deletions src/distributions/non_native_dists.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Define TDistLocationScale as the location scale transformation of the Distributions.jl TDist
export TDistLocationScale
TDistLocationScale = LocationScale{Float64,TDist{Float64}}
TDistLocationScale = AffineDistribution{Float64,value_support(TDist),TDist{Float64}}

export BetaFourParameters
BetaFourParameters = LocationScale{Float64,Beta{Float64}}
BetaFourParameters = AffineDistribution{Float64,value_support(Beta),Beta{Float64}}
20 changes: 10 additions & 10 deletions src/distributions/tdistlocationscale.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ TDistLocationScale

function score!(score_til::Matrix{T}, y::T, ::Type{TDistLocationScale}, param::Matrix{T}, t::Int) where T
score_til[t, 1] = ((param[t, 3] + 1) * (y - param[t, 1])) / ((y - param[t, 1])^2 + param[t, 2] * param[t, 3])
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] +
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] +
(y - param[t, 1])^2))
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)) -
1/param[t, 3] -
log(((y - param[t, 1])^2)/(param[t, 2] * param[t, 3]) + 1) -
1/param[t, 3] -
log(((y - param[t, 1])^2)/(param[t, 2] * param[t, 3]) + 1) -
digamma(param[t, 3] / 2) + digamma((param[t, 3] + 1) / 2)) / 2
return
end
Expand Down Expand Up @@ -49,35 +49,35 @@ function log_likelihood(::Type{TDistLocationScale}, y::Vector{T}, param::Matrix{
end

# Links
function link!(param_tilde::Matrix{T}, ::Type{TDistLocationScale}, param::Matrix{T}, t::Int) where T
function link!(param_tilde::Matrix{T}, ::Type{TDistLocationScale}, param::Matrix{T}, t::Int) where T
param_tilde[t, 1] = link(IdentityLink, param[t, 1])
param_tilde[t, 2] = link(LogLink, param[t, 2], zero(T))
param_tilde[t, 3] = link(LogLink, param[t, 3], zero(T))
return
end
function unlink!(param::Matrix{T}, ::Type{TDistLocationScale}, param_tilde::Matrix{T}, t::Int) where T
function unlink!(param::Matrix{T}, ::Type{TDistLocationScale}, param_tilde::Matrix{T}, t::Int) where T
param[t, 1] = unlink(IdentityLink, param_tilde[t, 1])
param[t, 2] = unlink(LogLink, param_tilde[t, 2], zero(T))
param[t, 3] = unlink(LogLink, param_tilde[t, 3], zero(T))
return
end
function jacobian_link!(aux::AuxiliaryLinAlg{T}, ::Type{TDistLocationScale}, param::Matrix{T}, t::Int) where T
function jacobian_link!(aux::AuxiliaryLinAlg{T}, ::Type{TDistLocationScale}, param::Matrix{T}, t::Int) where T
aux.jac[1] = jacobian_link(IdentityLink, param[t, 1])
aux.jac[2] = jacobian_link(LogLink, param[t, 2], zero(T))
aux.jac[3] = jacobian_link(LogLink, param[t, 3], zero(T))
return
end

# utils
# utils
function update_dist(::Type{TDistLocationScale}, param::Matrix{T}, t::Int) where T
tdist = TDist(param[t, 3])
return LocationScale(param[t, 1], sqrt(param[t, 2]), tdist)
end
return AffineDistribution(param[t, 1], sqrt(param[t, 2]), tdist)
end

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

function num_params(::Type{TDistLocationScale})
return 3
end
end
10 changes: 5 additions & 5 deletions test/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ function instantiate_dist(D::Type{<:Distribution})
end
end

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

# Some distributions might not have the fisher information yet available
if D in FI_NOT_IMPLEMENTED
return
return
else
ScoreDrivenModels.fisher_information!(aux_lin_alg, D, pars, 1)
end
Expand Down Expand Up @@ -94,10 +94,10 @@ end

function test_dist_utils(D::Type{<:Distribution})
# num_params
dist = instantiate_dist(D)
dist = instantiate_dist(D)
n_pars = ScoreDrivenModels.num_params(D)
@test n_pars == length(ScoreDrivenModels.params_sdm(dist))

# update_dist
t = 1
pars = permutedims([ScoreDrivenModels.params_sdm(dist)...])
Expand Down

2 comments on commit feb92dc

@raphaelsaavedra
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/72949

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.1.11 -m "<description of version>" feb92dc11294b7ff16f725778d1f7d050ee25292
git push origin v0.1.11

Please sign in to comment.