Skip to content

Commit ebe5100

Browse files
guilhermebodinraphaelsaavedra
authored andcommitted
added logit link (#38)
1 parent 1d30a87 commit ebe5100

File tree

2 files changed

+31
-11
lines changed

2 files changed

+31
-11
lines changed

src/link_functions.jl

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ struct IdentityLink <: Link end
3030

3131
link(::Type{IdentityLink}, param::T) where T = param
3232
unlink(::Type{IdentityLink}, param_tilde::T) where T = param_tilde
33-
jacobian_link(::Type{IdentityLink}, param_tilde::T) where T = one(T)
33+
jacobian_link(::Type{IdentityLink}, param::T) where T = one(T)
3434

3535
"""
3636
LogLink <: Link
@@ -41,11 +41,27 @@ struct LogLink <: Link end
4141

4242
link(::Type{LogLink}, param::T, lower_bound::T) where T = log(param - lower_bound)
4343
unlink(::Type{LogLink}, param_tilde::T, lower_bound::T) where T = exp(param_tilde) + lower_bound
44-
jacobian_link(::Type{LogLink}, param_tilde::T, lower_bound::T) where T = 1/(param_tilde - lower_bound)
44+
jacobian_link(::Type{LogLink}, param::T, lower_bound::T) where T = 1/(param - lower_bound)
4545

46+
"""
47+
LogitLink <: Link
48+
49+
Define the map ``\\tilde{f} = \\-ln(\\frac{b - a}{f + a} - 1)`` where ``f \\in [a, b], a, b \\in \\mathbb{R}`` and ``\\tilde{f} \\in \\mathbb{R}``
50+
"""
4651
struct LogitLink <: Link end
4752

53+
function link(::Type{LogitLink}, param::T, lower_bound::T, upper_bound::T) where T
54+
return log((param - lower_bound)/(upper_bound - param))
55+
end
56+
function unlink(::Type{LogitLink}, param_tilde::T, lower_bound::T, upper_bound::T) where T
57+
return lower_bound + ((upper_bound - lower_bound)/(1 + exp(-param_tilde)))
58+
end
59+
function jacobian_link(::Type{LogitLink}, param::T, lower_bound::T, upper_bound::T) where T
60+
return (upper_bound + lower_bound)/((upper_bound - param) * (param - lower_bound))
61+
end
62+
4863
const LINKS = [
4964
IdentityLink;
50-
LogLink
65+
LogLink;
66+
LogitLink
5167
]

test/test_links.jl

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
@testset "Links" begin
22
Random.seed!(10)
3-
x = rand()
43
atol = 1e-7
54
rtol = 1e-7
65

7-
link = SDM.IdentityLink
8-
@test SDM.link(link, SDM.unlink(link, x)) x atol = atol rtol = rtol
6+
for x in rand(10)
7+
link = SDM.IdentityLink
8+
@test SDM.link(link, SDM.unlink(link, x)) x atol = atol rtol = rtol
99

10-
link = SDM.LogLink
11-
lb = 1.0
12-
@test SDM.link(link, SDM.unlink(link, x, lb), lb) x atol = atol rtol = rtol
13-
lb = 0.0
14-
@test SDM.link(link, SDM.unlink(link, x, lb), lb) x atol = atol rtol = rtol
10+
link = SDM.LogLink
11+
lb = rand()
12+
@test SDM.link(link, SDM.unlink(link, x, lb), lb) x atol = atol rtol = rtol
13+
14+
link = SDM.LogitLink
15+
lb = rand()
16+
ub = rand() + lb
17+
@test SDM.link(link, SDM.unlink(link, x, lb, ub), lb, ub) x atol = atol rtol = rtol
18+
end
1519
end

0 commit comments

Comments
 (0)