# Using TransformVariables.jl to Numerically Handle Jacobian Adjustment of Simple Problem

**URL:** <https://discourse.julialang.org/t/using-transformvariables-jl-to-numerically-handle-jacobian-adjustment-of-simple-problem/60677>\
**Category:** Probabilistic Programming\
**Tags:** dynamichmc\
**Created:** [May 6, 2021, 7:25pm UTC](https://discourse.julialang.org/t/using-transformvariables-jl-to-numerically-handle-jacobian-adjustment-of-simple-problem/60677 "2021-05-06T19:25:56Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Adam\_Fleischhacker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adam_fleischhacker/32/24468_2.png) [@Adam\_Fleischhacker](https://discourse.julialang.org/u/Adam_Fleischhacker)\
**Post date:** [May 6, 2021, 7:25pm UTC](https://discourse.julialang.org/t/using-transformvariables-jl-to-numerically-handle-jacobian-adjustment-of-simple-problem/60677/1 "2021-05-06T19:25:57Z")

</div>

Here is a simple generative DAG where a Jacobian adjustment is required to get the `logpdf` of the posterior distribution for y.

 ![graphYW](https://global.discourse-cdn.com/julialang/original/3X/0/e/0e13e53a12b5074909d1b1fada874dec3d593202.png)

I can code this function to work with the DynamicHMC.jl suite of packages if I manually add the Jacobian adjustment - which is great. I cannot, however, figure out how to use TransformVariables.jl (or different package) so that I avoid the analytic calculation of the Jacobian function. I am pretty sure the computer will calculate the adjustment for me, I just can’t figure out which function (e.g. `TransformVariables.CustomTransform` or `transform_logdensity` ???) or how to use the function? I’ve tried many combos and can really use some help. Thanks!

Here is the working code where I want to replace the analytic Jacobian adjustment with one performed computationally:

```julia
using Distributions, TransformVariables, LogDensityProblems, DynamicHMC, DynamicHMC.Diagnostics, Parameters, Statistics, Random
## create problem type where winnings (w) is observed
struct spinProblem{T <: AbstractVector}
    w::T # winnings of previous players
end

## make function factory for this problem type
function (problem::spinProblem)(θ)
    @unpack y = θ # extract the parameter
    @unpack w = problem # extract the data

    # log likelihood accumulated in llSpinProb
    # prior on y
    llSpinProb = logpdf(Uniform(0,10),y)

    # likelihood
    for i in 1:length(w)
        ## let x_i = w_i/y be uniform(0,1)
        x_i = w[i] / y
        llSpinProb += logpdf(Uniform(0,1),x_i) #data
        llSpinProb += log(1/y) # Jacobian Adjustment - HOW TO REPLACE THIS WITH NUMERICAL APPROX
    end

    return(llSpinProb)
end

p2 = spinProblem([3.2,7.6,4.1,1.1,2.4]) ## fake data
p2((y = 7.5,)) # infeasible
p2((y = 8,)) # feasible
p2((y = 9.5,)) # feasible (less likely)

```

---

<div class="post-metadata">

**Author:** ![Adam\_Fleischhacker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adam_fleischhacker/32/24468_2.png) [@Adam\_Fleischhacker](https://discourse.julialang.org/u/Adam_Fleischhacker)\
**Post date:** [May 7, 2021, 1:12pm UTC](https://discourse.julialang.org/t/using-transformvariables-jl-to-numerically-handle-jacobian-adjustment-of-simple-problem/60677/2 "2021-05-07T13:12:19Z")

</div>

I am continuing to educate myself and now believe that `TransformVariables.jl` is used to go from the unconstrained space that an HMC sampler prefers to the constrained space of a probability model. It is not a general package for any old probability transform using a Jacobian. I now see `LogDensityProblems.jl` might be the right avenue for computationally computed Jacobian transforms; maybe? My search continues … any hints are appreciated 🙂
