Skip to content
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

Redundant computation in Affine #22

Closed
cscherrer opened this issue Sep 20, 2021 · 2 comments
Closed

Redundant computation in Affine #22

cscherrer opened this issue Sep 20, 2021 · 2 comments
Assignees

Comments

@cscherrer
Copy link
Collaborator

Say we change logpdf to print some things along the way,

function logpdf(d::AbstractMeasure, x)
    _logpdf(d, basemeasure(d), x, zero(Float64))
end

@inline function _logpdf(d::AbstractMeasure, β::AbstractMeasure, x, ℓ::Float64)
    @show d
    @show x
    d === β && return ℓ
    Δℓ = logdensity(d, x)
    _logpdf(β, basemeasure(β), x, ℓ + Δℓ)
end

and similarly with logdensity(::Affine, x):

function logdensity(d::Affine{(:σ,)}, x)
    z = d.σ \ x
    @show z
    println()
    logdensity(d.parent, z)
end

Then we can compute

julia> μ = Affine((σ=[3 4]',), Normal()^1);

julia> logpdf(μ, [1,2])
d = Affine{(,),PowerMeasure{typeof(identity), Normal{(), Tuple{}}, 1, Tuple{Base.OneTo{Int64}}},Tuple{LinearAlgebra.Adjoint{Int64, Matrix{Int64}}}}(AffineTransform{(,),Tuple{LinearAlgebra.Adjoint{Int64, Matrix{Int64}}}}((σ = [3; 4;;],)), Normal() ^ 1)
x = [1, 2]
z = [0.44000000000000006]

d = 0.398942 * Affine{(,),PowerMeasure{typeof(identity), Lebesgue{ℝ}, 1, Tuple{Base.OneTo{Int64}}},Tuple{LinearAlgebra.Adjoint{Int64, Matrix{Int64}}}}(AffineTransform{(,),Tuple{LinearAlgebra.Adjoint{Int64, Matrix{Int64}}}}((σ = [3; 4;;],)), Lebesgue(ℝ) ^ 1)
x = [1, 2]
z = [0.44000000000000006]

d = Affine{(,),PowerMeasure{typeof(identity), Lebesgue{ℝ}, 1, Tuple{Base.OneTo{Int64}}},Tuple{LinearAlgebra.Adjoint{Int64, Matrix{Int64}}}}(AffineTransform{(,),Tuple{LinearAlgebra.Adjoint{Int64, Matrix{Int64}}}}((σ = [3; 4;;],)), Lebesgue(ℝ) ^ 1)
x = [1, 2]
z = [0.44000000000000006]

d = 0.2 * Lebesgue(ℝ) ^ 1
x = [1, 2]
d = Lebesgue(ℝ) ^ 1
x = [1, 2]
-2.625176445638773

Each z shown above is the result of a separate z = d.σ \ x. That's very inefficient, so we need to fix it.

@cscherrer cscherrer self-assigned this Sep 20, 2021
@cscherrer
Copy link
Collaborator Author

I've thought about some options for fixing this, but I think I see a better one now. @mschauer has suggested basemeasure should be able to take the point as an argument, for cases where we might want the base measure to be the tangent space. So we can have a new function (probably with a different name than this)

function logdensity_tuple(d, x)
    return (logdensity(d, x), basemeasure(d, x), x)
end

In cases like Affine where we end up with a transformation, the third argument can be the MapsTo object z ↦ x. The recursion step that finally eliminates the transformation can then work with z from that point on.

This is probably still a little vague, so I'll get a prototype together and see how it goes.

@cscherrer
Copy link
Collaborator Author

Closing this outdated issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant