# "Fancy" matrix variable transformations in TransformVariables.jl / DynamicHMC.jl

**URL:** <https://discourse.julialang.org/t/fancy-matrix-variable-transformations-in-transformvariables-jl-dynamichmc-jl/19618>\
**Category:** Statistics\
**Created:** [January 14, 2019, 12:55pm UTC](https://discourse.julialang.org/t/fancy-matrix-variable-transformations-in-transformvariables-jl-dynamichmc-jl/19618 "2019-01-14T12:55:21Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![hessammehr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hessammehr/32/3847_2.png) [@hessammehr](https://discourse.julialang.org/u/hessammehr)\
**Post date:** [January 14, 2019, 12:55pm UTC](https://discourse.julialang.org/t/fancy-matrix-variable-transformations-in-transformvariables-jl-dynamichmc-jl/19618/1 "2019-01-14T12:55:21Z")

</div>

Lately I’ve been using @Tamas_Papp’s excellent DynamicHMC package (thank you, Tamas!) and it’s been a lot of fun, but was hoping someone could point me in the right direction with a few more esoteric variable transformations using TransformVariables.jl, e.g.

- A matrix each column of which is L1 normalized (to be used with a Dirichlet prior)
- A symmetric matrix

I realise that similar examples to these exist in the `special_arrays.jl`, namely `UnitVector` and `CorrCholeskyFactor`. Does the following seem like the right approach?

- Declare a struct in each case, e.g.

```julia
TransformVariables.@calltrans struct UnitColumnMatrix <: TransformVariables.VectorTransform
    n_rows::Int
    n_cols::Int
end

```

- Implement `dimension`, `transform_with`, `inverse_eltype`, and `inverse!` for each type.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [January 14, 2019, 1:11pm UTC](https://discourse.julialang.org/t/fancy-matrix-variable-transformations-in-transformvariables-jl-dynamichmc-jl/19618/2 "2019-01-14T13:11:07Z")

</div>

I am happy to hear that you find the package useful.

You can use a stick-breaking transform to make L1-unit vectors, as described eg in the Stan docs. I am quite busy at the moment, but if you open an issue I am happy to add them in the medium rum.

As for symmetric matrices, just generate them from a vector of the upper or lower triangle. Since the transformation is trivially linear, there is no need for a Jacobian adjustment.

---

<div class="post-metadata">

**Author:** ![hessammehr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hessammehr/32/3847_2.png) [@hessammehr](https://discourse.julialang.org/u/hessammehr)\
**Post date:** [January 14, 2019, 1:22pm UTC](https://discourse.julialang.org/t/fancy-matrix-variable-transformations-in-transformvariables-jl-dynamichmc-jl/19618/3 "2019-01-14T13:22:32Z")

</div>

Thanks, Tamas! I’ll have a go at this and open an issue if I get stuck.
