-
Notifications
You must be signed in to change notification settings - Fork 16
Change UnitVector transform to use normalization #138
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 11 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
4401070
Load norm and rmul! from LinearAlgebra
sethaxen 423bc8c
Make dimension of transform equal to output size
sethaxen 629f4a2
Update unit vector transform to use normalization
sethaxen 54b3882
Update inverse transform to be identity
sethaxen 0ddf68d
Require unit vectors be length >2
sethaxen 41ac480
Update unit vector docstring
sethaxen aae6c51
Update UnitVector tests
sethaxen 6cd99b8
Update UnitVector dimension tests
sethaxen 2ab58bb
Test that inverse is a "right inverse"
sethaxen 340deb9
Avoid test at singularity of UnitVector
sethaxen a7c1e73
Update show test
sethaxen b9f36ed
Apply suggestions from code review
sethaxen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -194,20 +194,40 @@ end | |
|
||
@testset "to unit vector" begin | ||
@testset "dimension checks" begin | ||
U = UnitVector(3) | ||
@test_throws ArgumentError UnitVector(0) | ||
@test_throws ArgumentError UnitVector(1) | ||
U = UnitVector(2) | ||
x = zeros(3) # incorrect | ||
@test_throws ArgumentError transform(U, x) | ||
@test_throws ArgumentError transform_and_logjac(U, x) | ||
end | ||
|
||
@testset "consistency checks" begin | ||
for K in 1:10 | ||
for K in 2:11 | ||
t = UnitVector(K) | ||
@test dimension(t) == K - 1 | ||
if K > 1 | ||
test_transformation(t, y -> sum(abs2, y) ≈ 1, | ||
vec_y = y -> y[1:(end-1)]) | ||
end | ||
@test dimension(t) == K | ||
test_transformation(t, y -> sum(abs2, y) ≈ 1, test_inverse=false, jac=false) | ||
|
||
# because transform is non-bijective, we need to manually test inverse and jac | ||
x = normalize(randn(K)) # if already normalized, inverse is the identity | ||
y = transform(t, x) | ||
@test inverse(t, y) ≈ y ≈ x | ||
|
||
# test "jacobian", here lj is the sum of the log jacobian of the transform and | ||
# the log-density of the prior on the discarded parameter (the norm of the vector) | ||
x = randn(K) | ||
r = norm(x) | ||
_, lj = transform_and_logjac(t, x) | ||
J = ForwardDiff.jacobian(x -> vcat(normalize(x), norm(x)), x) | ||
# log of generalized Jacobian determinant: | ||
# - https://encyclopediaofmath.org/wiki/Jacobian#Generalizations_of_the_Jacobian_determinant | ||
# - https://en.wikipedia.org/wiki/Area_formula_(geometric_measure_theory) | ||
lj_transform = logdet(J' * J) / 2 | ||
# lp_prior | ||
# un-normalized Chi distribution prior on r | ||
lp_prior = (K - 1) * log(r) - r^2 / 2 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you please explain why this correction is needed? |
||
lj_manual = lj_transform + lp_prior | ||
@test lj ≈ lj_manual | ||
end | ||
end | ||
end | ||
|
@@ -358,7 +378,8 @@ end | |
x = randn(dimension(tn)) | ||
y = @inferred transform(tn, x) | ||
@test y isa NamedTuple{(:a,:b,:c)} | ||
@test inverse(tn, y) ≈ x | ||
x′ = inverse(tn, y) | ||
@test inverse(tn, transform(tn, x′)) ≈ x′ | ||
index = 0 | ||
ljacc = 0.0 | ||
for (i, t) in enumerate((t1, t2, t3)) | ||
|
@@ -396,11 +417,13 @@ end | |
for _ in 1:10 | ||
N = rand(3:7) | ||
tt = as((a = as(Tuple(as(Vector, asℝ₊, 2) for _ in 1:N)), | ||
b = as(Tuple(UnitVector(n) for n in 1:N)))) | ||
b = as(Tuple(UnitVector(n) for n in 2:N)))) | ||
x = randn(dimension(tt)) | ||
y = transform(tt, x) | ||
x′ = inverse(tt, y) | ||
@test x ≈ x′ | ||
m = sum(2:N) | ||
@test x[1:end-m] ≈ x′[1:end-m] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We might as well remove these two lines, the one below is sufficient. |
||
@test inverse(tt, transform(tt, x′)) ≈ x′ | ||
end | ||
end | ||
|
||
|
@@ -506,7 +529,7 @@ end | |
-(abs2(μ) + abs2(σ) + abs2(β) + α + δ[1] + δ[2]) | ||
end | ||
P = TransformedLogDensities.TransformedLogDensity(t, f) | ||
x = zeros(dimension(t)) | ||
x = randn(dimension(t)) | ||
v = logdensity(P, x) | ||
g = ForwardDiff.gradient(x -> logdensity(P, x), x) | ||
|
||
|
@@ -619,7 +642,7 @@ end | |
|
||
t = UnitVector(3) | ||
d = dimension(t) | ||
x = [zeros(d), zeros(d)] | ||
x = [randn(d), randn(d)] | ||
@test transform.(t, x) == map(x -> transform(t, x), x) | ||
end | ||
|
||
|
@@ -733,7 +756,7 @@ end | |
[98:98] 1 → asℝ | ||
[108:110] 2 → SMatrix{3,3} correlation cholesky factor | ||
[120:121] 3 → 3 element unit simplex transformation | ||
[131:133] 4 → 4 element unit vector transformation""" | ||
[131:134] 4 → 4 element unit vector transformation""" | ||
repr(MIME("text/plain"), t) == repr_t | ||
end | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.