# getting the types right for transformed distributions

**URL:** https://discourse.julialang.org/t/getting-the-types-right-for-transformed-distributions/10042
**Category:** General Usage
**Created:** [March 28, 2018, 10:24pm UTC](https://discourse.julialang.org/t/getting-the-types-right-for-transformed-distributions/10042 "2018-03-28T22:24:47Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![atteson](https://avatars.discourse-cdn.com/v4/letter/a/91b2a8/32.png) [@atteson](https://discourse.julialang.org/u/atteson)
#### Post date: [March 28, 2018, 10:24pm UTC](https://discourse.julialang.org/t/getting-the-types-right-for-transformed-distributions/10042/1 "2018-03-28T22:24:47Z")

</div>

I have a question about how to structure certain types. Even though the application is statistics, the question is really about types.

I’d like to build types that take a Distribution (from the Distributions package) and transform it into another Distribution. I’d like this to maintain the VariateForm of the Distribution. Below is a simplified version of what I’d like. Is there a way to do this which works?

using Distributions

type Translation{S,T\<:UnivariateDistribution{S}} \<: UnivariateDistribution{S}  
distribution::T  
translation::Float64  
scale::Float64  
end

Translation( distribution::UnivariateDistribution, x::Float64 ) =  
Translation( distribution, x, 1/x )

t = Translation( Normal(), 2.0 )

---

<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: [March 29, 2018, 6:02am UTC](https://discourse.julialang.org/t/getting-the-types-right-for-transformed-distributions/10042/2 "2018-03-29T06:02:13Z")

</div>

(Please [quote your code with backticks](https://discourse.julialang.org/t/psa-how-to-quote-code-with-backticks/7530)).

In general, yes, this is the way to wrap some other object. In the specific example, I am not sure why you have a type parameter `S`, you could just have `T <: UnivariateDistribution` (perhaps you are using `S` somewhere? but that is not in the example).

Also, there is no reason to restrict to `Float64`, use some `R <: Real`, so that your code would work with eg [ForwardDiff.jl](https://github.com/JuliaDiff/ForwardDiff.jl) at zero performance cost.

---

<div class="post-metadata">

### Author: ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)
#### Post date: [March 29, 2018, 6:36am UTC](https://discourse.julialang.org/t/getting-the-types-right-for-transformed-distributions/10042/3 "2018-03-29T06:36:50Z")

</div>

I think the `S` parameter is necessary so that the resulting type can itself be `<: UnivariateDistribution{S}`.

---

<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: [March 29, 2018, 7:16am UTC](https://discourse.julialang.org/t/getting-the-types-right-for-transformed-distributions/10042/4 "2018-03-29T07:16:13Z")

</div>

I must be missing something about the type hierarchy of `Distributions.jl`, but it is unclear to me how that is used. The only parametric usage of that type I could find is [the definition](https://github.com/JuliaStats/Distributions.jl/blob/b785e287984caf8b2192d814cfd4f0acc04f6429/src/common.jl#L67), the rest dispatch on `UnivariateDistribution`, and then subtypes just use a non-parametric subtype (eg [`Normal`](https://github.com/JuliaStats/Distributions.jl/blob/b785e287984caf8b2192d814cfd4f0acc04f6429/src/univariate/continuous/normal.jl#L26)).

---

<div class="post-metadata">

### Author: ![atteson](https://avatars.discourse-cdn.com/v4/letter/a/91b2a8/32.png) [@atteson](https://discourse.julialang.org/u/atteson)
#### Post date: [March 29, 2018, 3:49pm UTC](https://discourse.julialang.org/t/getting-the-types-right-for-transformed-distributions/10042/5 "2018-03-29T15:49:21Z")

</div>

I ended up solving my problem soon after posting it. Template parameters were needed for the function:

`  
using Distributions

type Translation{S,T\<:UnivariateDistribution{S}} \<: UnivariateDistribution{S}  
distribution::T  
translation::Float64  
scale::Float64  
end

Translation{S,T\<:UnivariateDistribution{S}}( distribution::T, x::Float64 ) =  
Translation{S,T}( distribution, x, 1/x )

t = Translation( Normal(), 2.0 )  
`
