Skip to content

Commit 4623d07

Browse files
Major improvements in performance (#42)
* checkpoint * picked series in csv * fixed estimation * estimation working for identity * working * not working for LogNormal 1 12 * version working for all scalings * all tests are passing * deleted unecessary csv * changes in all dists * fix gamma bug * updated benchmarks * fix bugs found on ENA * take out Delimited Files * remove unecessary line
1 parent 2856227 commit 4623d07

22 files changed

+413
-331
lines changed

bench/benchmark_estimate.jl

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@ num_seeds = 3
1717
estimate!(gas, $simulation; verbose = $verbose, opt_method = opt_method)
1818
end
1919
# BenchmarkTools.Trial:
20-
# memory estimate: 7.33 GiB
21-
# allocs estimate: 95576498
20+
# memory estimate: 466.69 MiB
21+
# allocs estimate: 40237
2222
# --------------
23-
# minimum time: 5.632 s (15.93% GC)
24-
# median time: 5.632 s (15.93% GC)
25-
# mean time: 5.632 s (15.93% GC)
26-
# maximum time: 5.632 s (15.93% GC)
23+
# minimum time: 2.553 s (0.57% GC)
24+
# median time: 2.655 s (0.59% GC)
25+
# mean time: 2.655 s (0.59% GC)
26+
# maximum time: 2.756 s (0.60% GC)
2727
# --------------
28-
# samples: 1
28+
# samples: 2
2929
# evals/sample: 1
3030

3131
scaling = 0.0
@@ -43,15 +43,15 @@ num_seeds = 3
4343
estimate!(gas, $simulation; verbose = $verbose, opt_method = opt_method)
4444
end
4545
# BenchmarkTools.Trial:
46-
# memory estimate: 8.63 GiB
47-
# allocs estimate: 112481074
46+
# memory estimate: 353.02 MiB
47+
# allocs estimate: 30598
4848
# --------------
49-
# minimum time: 5.418 s (19.27% GC)
50-
# median time: 5.418 s (19.27% GC)
51-
# mean time: 5.418 s (19.27% GC)
52-
# maximum time: 5.418 s (19.27% GC)
49+
# minimum time: 1.008 s (1.41% GC)
50+
# median time: 1.265 s (1.36% GC)
51+
# mean time: 1.281 s (1.35% GC)
52+
# maximum time: 1.587 s (1.28% GC)
5353
# --------------
54-
# samples: 1
54+
# samples: 4
5555
# evals/sample: 1
5656

5757

@@ -62,13 +62,13 @@ scaling = 0.5
6262
estimate!(gas, $simulation; verbose = $verbose, opt_method = opt_method)
6363
end
6464
# BenchmarkTools.Trial:
65-
# memory estimate: 17.39 GiB
66-
# allocs estimate: 325680001
65+
# memory estimate: 8.26 GiB
66+
# allocs estimate: 159942290
6767
# --------------
68-
# minimum time: 9.760 s (23.28% GC)
69-
# median time: 9.760 s (23.28% GC)
70-
# mean time: 9.760 s (23.28% GC)
71-
# maximum time: 9.760 s (23.28% GC)
68+
# minimum time: 7.361 s (18.19% GC)
69+
# median time: 7.361 s (18.19% GC)
70+
# mean time: 7.361 s (18.19% GC)
71+
# maximum time: 7.361 s (18.19% GC)
7272
# --------------
7373
# samples: 1
7474
# evals/sample: 1
@@ -80,13 +80,13 @@ scaling = 1.0
8080
estimate!(gas, $simulation; verbose = $verbose, opt_method = opt_method)
8181
end
8282
# BenchmarkTools.Trial:
83-
# memory estimate: 11.99 GiB
84-
# allocs estimate: 172023821
83+
# memory estimate: 666.69 MiB
84+
# allocs estimate: 8744603
8585
# --------------
86-
# minimum time: 8.502 s (20.62% GC)
87-
# median time: 8.502 s (20.62% GC)
88-
# mean time: 8.502 s (20.62% GC)
89-
# maximum time: 8.502 s (20.62% GC)
86+
# minimum time: 2.908 s (2.08% GC)
87+
# median time: 3.382 s (2.07% GC)
88+
# mean time: 3.382 s (2.07% GC)
89+
# maximum time: 3.855 s (2.07% GC)
9090
# --------------
91-
# samples: 1
91+
# samples: 2
9292
# evals/sample: 1

src/MLE.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
export estimate!
22

3+
const DEFAULT_INITIAL_PARAM = NaN.*ones(1, 1)
4+
35
function log_lik(psitilde::Vector{T}, y::Vector{T}, sdm::SDM{D, T},
46
initial_params::Vector{Vector{T}}, unknowns::Unknowns_SDM, n::Int) where {D, T}
57
return error("log_lik not defined for a model of type ", typeof(sdm))
68
end
79

810
function estimate!(sdm::SDM{D, T}, y::Vector{T};
9-
initial_params::Vector{Vector{Float64}} = [[NaN]], # Means default initializations
11+
initial_params::Matrix{T} = DEFAULT_INITIAL_PARAM,
1012
opt_method::AbstractOptimizationMethod = LBFGS(sdm, 3),
1113
verbose::Int = 0) where {D, T}
1214

src/ScoreDrivenModels.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ using LinearAlgebra
55

66
import Base.length
77

8+
const SCALINGS = [0.0, 1/2, 1.0]
9+
const BIG_NUM = 1e8
10+
const SMALL_NUM = 1e-8
11+
812
# Core files
913
include("abstracts.jl")
1014
include("utils.jl")

src/distributions/beta.jl

Lines changed: 29 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,61 +2,56 @@
22
Proof somewhere
33
parametrized in \\alpha and \\beta
44
"""
5-
function score(y::T, ::Type{Beta}, param::Vector{T}) where T
6-
digamma_a_b = digamma(param[1] + param[2])
7-
return [
8-
log(y) + digamma_a_b - digamma(param[1]);
9-
log(1 - y) + digamma_a_b - digamma(param[2])
10-
]
5+
function score!(score_til::Matrix{T}, y::T, ::Type{Beta}, param::Matrix{T}, t::Int) where T
6+
score_til[t, 1] = log(y) + digamma(param[t, 1] + param[t, 2]) - digamma(param[t, 1])
7+
score_til[t, 2] = log(1 - y) + digamma(param[t, 1] + param[t, 2]) - digamma(param[t, 2])
8+
return
119
end
1210

1311
"""
1412
Proof somewhere
1513
"""
16-
function fisher_information(::Type{Beta}, param::Vector{T}) where T
17-
minus_trigamma_a_b = -trigamma(param[1] + param[2])
18-
19-
return [
20-
trigamma(param[1]) + minus_trigamma_a_b minus_trigamma_a_b;
21-
minus_trigamma_a_b trigamma(param[2]) + minus_trigamma_a_b
22-
]
14+
function fisher_information!(aux::AuxiliaryLinAlg{T}, ::Type{Beta}, param::Matrix{T}, t::Int) where T
15+
minus_trigamma_a_b = -trigamma(param[t, 1] + param[t, 2])
16+
aux.fisher[1, 1] = trigamma(param[t, 1]) + minus_trigamma_a_b
17+
aux.fisher[2, 2] = trigamma(param[t, 2]) + minus_trigamma_a_b
18+
aux.fisher[2, 1] = minus_trigamma_a_b
19+
aux.fisher[1, 2] = minus_trigamma_a_b
20+
return
2321
end
2422

2523
"""
2624
Proof somewhere
2725
"""
28-
function log_likelihood(::Type{Beta}, y::Vector{T}, param::Vector{Vector{T}}, n::Int) where T
26+
function log_likelihood(::Type{Beta}, y::Vector{T}, param::Matrix{T}, n::Int) where T
2927
loglik = 0.0
30-
for i in 1:n
31-
loglik += (param[i][1] - 1)*log(y[i]) + (param[i][2] - 1)*log(1 - y[i]) - logbeta(param[i][1], param[i][2])
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])
3230
end
3331
return -loglik
3432
end
3533

3634
# Links
37-
function link(::Type{Beta}, param::Vector{T}) where T
38-
return [
39-
link(LogLink, param[1], zero(T));
40-
link(LogLink, param[2], zero(T))
41-
]
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
4239
end
43-
function unlink(::Type{Beta}, param_tilde::Vector{T}) where T
44-
return [
45-
unlink(LogLink, param_tilde[1], zero(T));
46-
unlink(LogLink, param_tilde[2], zero(T))
47-
]
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
4844
end
49-
function jacobian_link(::Type{Beta}, param_tilde::Vector{T}) where T
50-
return Diagonal([
51-
jacobian_link(LogLink, param_tilde[1], zero(T));
52-
jacobian_link(LogLink, param_tilde[2], zero(T))
53-
])
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
5449
end
5550

5651
# utils
57-
function update_dist(::Type{Beta}, param::Vector{T}) where T
58-
small_threshold!(param, T(1e-8))
59-
return Beta(param[1], param[2])
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])
6055
end
6156

6257
function num_params(::Type{Beta})

src/distributions/common_interface.jl

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,28 @@ const DISTS = [
88
Weibull
99
]
1010

11-
function score(y::T, D::Type{<:Distribution}, param::Vector{T}) where T
12-
return error("score not implemented for $D distribution")
11+
function score!(score_til::Matrix{T}, y::T, ::Type{<:Distribution}, param::Matrix{T}, t::Int) where T
12+
return error("score! not implemented for $D distribution")
1313
end
14-
function score(y::Int, D::Type{<:Distribution}, param::Vector{T}) where T
15-
return error("score not implemented for $D distribution")
14+
function score!(score_til::Matrix{T}, y::Int, ::Type{<:Distribution}, param::Matrix{T}, t::Int) where T
15+
return error("score! not implemented for $D distribution")
1616
end
17-
function fisher_information(D::Type{<:Distribution}, param::Vector{T}) where T
18-
return error("fisher_information not implemented for $D distribution")
17+
function fisher_information!(aux::AuxiliaryLinAlg{T},::Type{<:Distribution}, param::Matrix{T}, t::Int) where T
18+
return error("fisher_information! not implemented for $D distribution")
1919
end
20-
function link(D::Type{<:Distribution}, param::Vector{T}) where T
21-
return error("link not implemented for $D distribution")
20+
function log_likelihood(::Type{<:Distribution}, y::Vector{T}, param::Matrix{T}, n::Int) where T
21+
return error("log_likelihood not implemented for $D distribution")
2222
end
23-
function unlink(D::Type{<:Distribution}, param_tilde::Vector{T}) where T
24-
return error("unlink not implemented for $D distribution")
23+
function link!(param_tilde::Matrix{T}, ::Type{<:Distribution}, param::Matrix{T}, t::Int) where T
24+
return error("link! not implemented for $D distribution")
2525
end
26-
function jacobian_link(D::Type{<:Distribution}, param_tilde::Vector{T}) where T
27-
return error("jacobian_link not implemented for $D distribution")
26+
function unlink!(param::Matrix{T}, ::Type{<:Distribution}, param_tilde::Matrix{T}, t::Int) where T
27+
return error("unlink! not implemented for $D distribution")
2828
end
29-
function update_dist(D::Type{<:Distribution}, param::Vector{T}) where T
29+
function jacobian_link!(aux::AuxiliaryLinAlg{T}, ::Type{<:Distribution}, param::Matrix{T}, t::Int) where T
30+
return error("jacobian_link! not implemented for $D distribution")
31+
end
32+
function update_dist(::Type{<:Distribution}, param::Matrix{T}, t::Int) where T
3033
return error("update_dist not implemented for $D distribution")
3134
end
3235
function num_params(D::Type{<:Distribution})

src/distributions/gamma.jl

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,57 +2,56 @@
22
Proof somewhere
33
parametrized in \\alpha and \\theta
44
"""
5-
function score(y::T, ::Type{Gamma}, param::Vector{T}) where T
6-
return [
7-
log(y) - digamma(param[1]) - log(param[2]);
8-
y/param[2]^2 - param[1]/param[2]
9-
]
5+
function score!(score_til::Matrix{T}, y::T, ::Type{Gamma}, param::Matrix{T}, t::Int) where T
6+
score_til[t, 1] = log(y) - digamma(param[t, 1]) - log(param[t, 2])
7+
score_til[t, 2] = y/param[t, 2]^2 - param[t, 1]/param[t, 2]
8+
return
109
end
1110

1211
"""
1312
Proof somewhere
1413
"""
15-
function fisher_information(::Type{Gamma}, param::Vector{T}) where T
16-
[
17-
-trigamma(param[1]) -1/param[2];
18-
-1/param[2] -2*y/param[2]^3 + param[1]/param[2]^2
19-
]
14+
function fisher_information!(aux::AuxiliaryLinAlg{T}, ::Type{Gamma}, param::Matrix{T}, t::Int) where T
15+
error("Fisher information not implemented for Gamma distribution.")
16+
aux.fisher[1, 1] = -trigamma(param[t, 1])
17+
aux.fisher[2, 2] = -2 * y / param[t, 2] ^ 3 + param[t, 1] / param[t, 2]^2
18+
aux.fisher[2, 1] = -1/param[t, 2]
19+
aux.fisher[1, 2] = -1/param[t, 2]
20+
return
2021
end
2122

2223
"""
2324
Proof somewhere
2425
"""
25-
function log_likelihood(::Type{Gamma}, y::Vector{T}, param::Vector{Vector{T}}, n::Int) where T
26+
function log_likelihood(::Type{Gamma}, y::Vector{T}, param::Matrix{T}, n::Int) where T
2627
loglik = 0.0
27-
for i in 1:n
28-
loglik += (param[i][1] - 1)*log(y[i]) - y[i]/param[i][2] - loggamma(param[i][1]) - param[i][1]*log(param[i][2])
28+
for t in 1:n
29+
loglik += (param[t, 1] - 1)*log(y[t]) - y[t]/param[t, 2] - loggamma(param[t, 1]) - param[t, 1]*log(param[t, 2])
2930
end
3031
return -loglik
3132
end
3233

3334
# Links
34-
function link(::Type{Gamma}, param::Vector{T}) where T
35-
return [
36-
link(LogLink, param[1], zero(T));
37-
link(LogLink, param[2], zero(T))
38-
]
35+
function link!(param_tilde::Matrix{T}, ::Type{Gamma}, 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
3939
end
40-
function unlink(::Type{Gamma}, param_tilde::Vector{T}) where T
41-
return [
42-
unlink(LogLink, param_tilde[1], zero(T));
43-
unlink(LogLink, param_tilde[2], zero(T))
44-
]
40+
function unlink!(param::Matrix{T}, ::Type{Gamma}, 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
4544
end
46-
function jacobian_link(::Type{Gamma}, param_tilde::Vector{T}) where T
47-
return Diagonal([
48-
jacobian_link(LogLink, param_tilde[1], zero(T));
49-
jacobian_link(LogLink, param_tilde[2], zero(T))
50-
])
45+
function jacobian_link!(aux::AuxiliaryLinAlg{T}, ::Type{Gamma}, 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
5149
end
5250

5351
# utils
54-
function update_dist(::Type{Gamma}, param::Vector{T}) where T
55-
return Gamma(param[1], param[2])
52+
function update_dist(::Type{Gamma}, param::Matrix{T}, t::Int) where T
53+
small_threshold!(param, SMALL_NUM, t)
54+
return Gamma(param[t, 1], param[t, 2])
5655
end
5756

5857
function num_params(::Type{Gamma})

0 commit comments

Comments
 (0)