Skip to content

Commit 4a45fb2

Browse files
Add beta, remove beta location scale and add te (#120)
1 parent 45f75c4 commit 4a45fb2

File tree

9 files changed

+74
-85
lines changed

9 files changed

+74
-85
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "ScoreDrivenModels"
22
uuid = "4a87933e-d659-11e9-0e65-7f40dedd4a3a"
33
authors = ["guilhermebodin <[email protected]>, raphaelsaavedra <[email protected]>"]
4-
version = "0.1.4"
4+
version = "0.1.5"
55

66
[deps]
77
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"

docs/src/manual.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,6 @@ this is handled internally.
9595
| Distribution | Identity scaling | Inverse and inverse square root scalings |
9696
| :------- |:---------------:|:--------------------:|
9797
|[`Beta`](@ref)|||
98-
|[`BetaLocationScale`](@ref)|| x |
9998
|[`Exponential`](@ref)|||
10099
|[`Gamma`](@ref)|||
101100
|[`LogitNormal`](@ref)|||
@@ -110,7 +109,6 @@ this is handled internally.
110109

111110
```@docs
112111
ScoreDrivenModels.Beta
113-
ScoreDrivenModels.BetaLocationScale
114112
ScoreDrivenModels.Exponential
115113
ScoreDrivenModels.Gamma
116114
ScoreDrivenModels.LogitNormal

src/MLE.jl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,8 @@ end
9494
function fit(gas::Model{D, T}, y::Vector{T};
9595
initial_params::Matrix{T} = DEFAULT_INITIAL_PARAM,
9696
opt_method::AbstractOptimizationMethod = NelderMead(gas, DEFAULT_NUM_SEEDS),
97-
verbose::Int = DEFAULT_VERBOSE) where {D, T}
97+
verbose::Int = DEFAULT_VERBOSE,
98+
throw_errors::Bool = false) where {D, T}
9899

99100
verbose in [0, 1, 2, 3] || throw(ErrorException, "verbose argument must be in [0, 1, 2, 3]")
100101
# Number of initial_points and number of params to estimate
@@ -123,6 +124,9 @@ function fit(gas::Model{D, T}, y::Vector{T};
123124
verbose >= 1 && println("Round $i of $n_initial_points - Log-likelihood: $(-opt_result.minimum * length(y))")
124125
catch err
125126
println(err)
127+
if throw_errors
128+
throw(err)
129+
end
126130
verbose >= 1 && println("Round $i diverged")
127131
end
128132
end

src/ScoreDrivenModels.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ include("opt_methods/NelderMead.jl")
3131
# Distributions
3232
include("distributions/non_native_dists.jl")
3333
include("distributions/common_interface.jl")
34-
include("distributions/betalocationscale.jl")
34+
include("distributions/beta.jl")
3535
include("distributions/exponential.jl")
3636
include("distributions/gamma.jl")
3737
include("distributions/logitnormal.jl")

src/distributions/beta.jl

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
"""
2+
Beta
3+
* Parametrization
4+
* Score
5+
* Fisher Information
6+
* `time_varying_params` map.
7+
* Default link
8+
"""
9+
Beta
10+
11+
function score!(score_til::Matrix{T}, y::T, ::Type{Beta}, param::Matrix{T}, t::Int) where T
12+
score_til[t, 1] = log(y) + digamma(param[t, 1] + param[t, 2]) - digamma(param[t, 1])
13+
score_til[t, 2] = log(1 - y) + digamma(param[t, 1] + param[t, 2]) - digamma(param[t, 2])
14+
return
15+
end
16+
17+
function fisher_information!(aux::AuxiliaryLinAlg{T}, ::Type{Beta}, param::Matrix{T}, t::Int) where T
18+
minus_trigamma_a_b = -trigamma(param[t, 1] + param[t, 2])
19+
aux.fisher[1, 1] = trigamma(param[t, 1]) + minus_trigamma_a_b
20+
aux.fisher[2, 2] = trigamma(param[t, 2]) + minus_trigamma_a_b
21+
aux.fisher[2, 1] = minus_trigamma_a_b
22+
aux.fisher[1, 2] = minus_trigamma_a_b
23+
return
24+
end
25+
26+
function log_likelihood(::Type{Beta}, y::Vector{T}, param::Matrix{T}, n::Int) where T
27+
loglik = 0.0
28+
for t in 1:n
29+
loglik += (param[t, 1] - 1)*log(y[t]) + (param[t, 2] - 1)*log(1 - y[t]) - logbeta(param[t, 1], param[t, 2])
30+
end
31+
return -loglik
32+
end
33+
34+
# Links
35+
function link!(param_tilde::Matrix{T}, ::Type{Beta}, param::Matrix{T}, t::Int) where T
36+
param_tilde[t, 1] = link(LogLink, param[t, 1], zero(T))
37+
param_tilde[t, 2] = link(LogLink, param[t, 2], zero(T))
38+
return
39+
end
40+
function unlink!(param::Matrix{T}, ::Type{Beta}, param_tilde::Matrix{T}, t::Int) where T
41+
param[t, 1] = unlink(LogLink, param_tilde[t, 1], zero(T))
42+
param[t, 2] = unlink(LogLink, param_tilde[t, 2], zero(T))
43+
return
44+
end
45+
function jacobian_link!(aux::AuxiliaryLinAlg{T}, ::Type{Beta}, param::Matrix{T}, t::Int) where T
46+
aux.jac[1] = jacobian_link(LogLink, param[t, 1], zero(T))
47+
aux.jac[2] = jacobian_link(LogLink, param[t, 2], zero(T))
48+
return
49+
end
50+
51+
# utils
52+
function update_dist(::Type{Beta}, param::Matrix{T}, t::Int) where T
53+
small_threshold!(param, SMALL_NUM, t)
54+
return Beta(param[t, 1], param[t, 2])
55+
end
56+
57+
function params_sdm(d::Beta)
58+
return Distributions.params(d)
59+
end
60+
61+
function num_params(::Type{Beta})
62+
return 2
63+
end

src/distributions/betalocationscale.jl

Lines changed: 0 additions & 69 deletions
This file was deleted.

src/distributions/common_interface.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Currently supported distributions
22
const DISTS = [
3-
BetaLocationScale,
3+
Beta,
44
Exponential,
55
Gamma,
66
LogitNormal,
@@ -13,7 +13,7 @@ const DISTS = [
1313
Weibull
1414
]
1515

16-
export BetaLocationScale,
16+
export Beta,
1717
Exponential,
1818
Gamma,
1919
LogitNormal,
Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
11
# Define TDistLocationScale as the location scale transformation of the Distributions.jl TDist
22
export TDistLocationScale
3-
TDistLocationScale = LocationScale{Float64,TDist{Float64}}
4-
5-
# Define BetaLocationScale as the location scale transformation of the Distributions.jl TDist
6-
export BetaLocationScale
7-
BetaLocationScale = LocationScale{Float64,Beta{Float64}}
3+
TDistLocationScale = LocationScale{Float64,TDist{Float64}}

test/utils.jl

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const PARAMETER_REQUIRED_DISTS = [Chisq; Chi; TDist]
2-
const FI_NOT_IMPLEMENTED = [Weibull; BetaLocationScale; NegativeBinomial]
2+
const FI_NOT_IMPLEMENTED = [Weibull; NegativeBinomial]
33

44
struct FakeDist{T<:Real} <: Distributions.ContinuousUnivariateDistribution
55
foo::T
@@ -9,9 +9,6 @@ function instantiate_dist(D::Type{<:Distribution})
99
if D == TDistLocationScale
1010
params = [1.0 2.0 10]
1111
return ScoreDrivenModels.update_dist(D, params, 1)
12-
elseif D == BetaLocationScale
13-
params = [-2.0 5.0 1.5 3.4]
14-
return ScoreDrivenModels.update_dist(D, params, 1)
1512
elseif D in PARAMETER_REQUIRED_DISTS
1613
params = [10][:, :]
1714
return ScoreDrivenModels.update_dist(D, params, 1)

0 commit comments

Comments
 (0)