Skip to content

Commit 571e665

Browse files
Add Weibull distribution (#25)
* add Weibull * return error if scaling is required * error update * bug fix
1 parent ca8af57 commit 571e665

File tree

7 files changed

+85
-5
lines changed

7 files changed

+85
-5
lines changed

src/ScoreDrivenModels.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,6 @@ include("distributions/poisson.jl")
2727
include("distributions/beta.jl")
2828
include("distributions/log_normal.jl")
2929
include("distributions/gamma.jl")
30+
include("distributions/weibull.jl")
3031

3132
end

src/distributions/common_interface.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ const DISTS = [
44
Beta;
55
Poisson;
66
LogNormal;
7-
Gamma
7+
Gamma;
8+
Weibull
89
]
910

1011
function score(y::T, D::Type{<:Distribution}, param::Vector{T}) where T

src/distributions/poisson.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ end
1616
Proof somewhere
1717
"""
1818
function log_likelihood(::Type{Poisson}, y::Vector{Int}, param::Vector{Vector{T}}, n::Int) where T
19-
loglik = 0.0
19+
loglik = zero(T)
2020
for i in 1:n
2121
loglik += y[i]*log(param[i][1]) - param[i][1] - lfactorial(y[i])
2222
end

src/distributions/weibull.jl

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
"""
2+
Proof somewhere
3+
parametrized in \\alpha and \\theta
4+
"""
5+
function score(y::T, ::Type{Weibull}, param::Vector{T}) where T
6+
return [
7+
(1/param[1]) + log(y/param[2]) * (1 - (y/param[2])^param[1]) ;
8+
(param[1]/param[2]) * (((y/param[2])^param[1]) - 1)
9+
]
10+
end
11+
12+
"""
13+
Proof somewhere
14+
"""
15+
function fisher_information(::Type{Weibull}, param::Vector{T}) where T
16+
return error("Fisher information not implemented for Weibull distribution.")
17+
end
18+
19+
"""
20+
Proof somewhere
21+
"""
22+
function log_likelihood(::Type{Weibull}, y::Vector{T}, param::Vector{Vector{T}}, n::Int) where T
23+
loglik = zero(T)
24+
for i in 1:n
25+
loglik += log(param[i][1]) + (param[i][1] - 1) * log(y[i]) - param[i][1] * log(param[i][2]) - (y[i]/param[i][2])^param[i][1]
26+
end
27+
return -loglik
28+
end
29+
30+
# Links
31+
function param_to_param_tilde(::Type{Weibull}, param::Vector{T}) where T
32+
return [
33+
param_to_param_tilde(ExponentialLink, param[1], zero(T));
34+
param_to_param_tilde(ExponentialLink, param[2], zero(T))
35+
]
36+
end
37+
function param_tilde_to_param(::Type{Weibull}, param_tilde::Vector{T}) where T
38+
return [
39+
param_tilde_to_param(ExponentialLink, param_tilde[1], zero(T));
40+
param_tilde_to_param(ExponentialLink, param_tilde[2], zero(T))
41+
]
42+
end
43+
function jacobian_param_tilde(::Type{Weibull}, param_tilde::Vector{T}) where T
44+
return Diagonal([
45+
jacobian_param_tilde(ExponentialLink, param_tilde[1], zero(T));
46+
jacobian_param_tilde(ExponentialLink, param_tilde[2], zero(T))
47+
])
48+
end
49+
50+
# utils
51+
function update_dist(::Type{Weibull}, param::Vector{T}) where T
52+
# normal here is parametrized as sigma^2
53+
return Weibull(param[1], param[2])
54+
end
55+
56+
function num_params(::Type{Weibull})
57+
return 2
58+
end

src/link_functions.jl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@ struct ExponentialLink <: Link end
1010

1111
param_to_param_tilde(::Type{ExponentialLink}, param::T, lower_bound::T) where T = log(param - lower_bound)
1212
param_tilde_to_param(::Type{ExponentialLink}, param_tilde::T, lower_bound::T) where T = exp(param_tilde) + lower_bound
13-
jacobian_param_tilde(::Type{ExponentialLink}, param_tilde::T, lower_bound::T) where T = exp(param_tilde) + lower_bound
13+
jacobian_param_tilde(::Type{ExponentialLink}, param_tilde::T, lower_bound::T) where T = exp(param_tilde)
1414

1515
struct LogitLink <: Link end
1616

17+
const LINKS = [
18+
IdentityLink;
19+
ExponentialLink
20+
]

test/runtests.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ using Test, Random, HypothesisTests
55
const SDM = ScoreDrivenModels
66

77
include("test_recursion.jl")
8-
include("test_estimate.jl")
8+
include("test_links.jl")
99
include("test_distributions.jl")
1010
include("test_initial_params.jl")
11-
include("test_diagnostics.jl")
11+
include("test_diagnostics.jl")
12+
include("test_estimate.jl")

test/test_links.jl

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
@testset "Links" begin
2+
Random.seed!(10)
3+
x = rand()
4+
atol = 1e-7
5+
rtol = 1e-7
6+
7+
link = SDM.IdentityLink
8+
@test SDM.param_to_param_tilde(link, SDM.param_tilde_to_param(link, x)) x atol = atol rtol = rtol
9+
10+
link = SDM.ExponentialLink
11+
lb = 1.0
12+
@test SDM.param_to_param_tilde(link, SDM.param_tilde_to_param(link, x, lb), lb) x atol = atol rtol = rtol
13+
lb = 0.0
14+
@test SDM.param_to_param_tilde(link, SDM.param_tilde_to_param(link, x, lb), lb) x atol = atol rtol = rtol
15+
end

0 commit comments

Comments
 (0)