# Getting off the ground with Gen

**URL:** <https://discourse.julialang.org/t/getting-off-the-ground-with-gen/44211>\
**Category:** Probabilistic Programming\
**Created:** [August 3, 2020, 7:07pm UTC](https://discourse.julialang.org/t/getting-off-the-ground-with-gen/44211 "2020-08-03T19:07:20Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![terdmann](https://avatars.discourse-cdn.com/v4/letter/t/85e7bf/32.png) [@terdmann](https://discourse.julialang.org/u/terdmann)\
**Post date:** [August 3, 2020, 7:07pm UTC](https://discourse.julialang.org/t/getting-off-the-ground-with-gen/44211/1 "2020-08-03T19:07:20Z")

</div>

I am really interested in Gen.jl, but am somewhat confused from the documentation. That is, I could not find any example of the functionality I’m trying to use. Can someone help me with the first steps?

My first question is, how to just get the below code to work?  
My second question is, how do I implement custom proposals, say just a simple random walk proposal? I tried a couple of ways (for example using the update function), but none of them lead to any sensible inferences for my simple example.

The goal of this code is just to estimate the mean parameter of a normal distribution using MH.  
For me the chain just gets stuck at the initial value far from the mean of the data.

```julia
using Gen, Plots, Random

@gen function model(n::Int)
    y = Vector{Float64}(undef, n)
    m = @trace(normal(0, 10.0), :m)
    for i in 1:n
        y[i] = @trace(normal(m, 1.0), (:y, i)) 
    end
    y
end

function do_inference(y, num_iters)
    trace, = generate(model, (length(y),), choicemap([((:y, i), y[i]) for i in 1:n]))
    ms = Float64[]
    for i=1:num_iters
        trace, = mh(trace, select(:m))
        @show trace[:m], get_score(trace)
        push!(ms, trace[:m])
    end
    ms
end

Random.seed!(1)
n = 50
(trace, _) = Gen.generate(model, (n,))
m = trace[:m]
y = [trace[(:y, i)] for i in 1:n];
trace, chn = do_inference(y, 1000)
plot(y)
hline!([m])
plot!(chn)

```

---

<div class="post-metadata">

**Author:** ![Marco\_Cusumano-Towne](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marco_cusumano-towne/32/6697_2.png) [@Marco\_Cusumano-Towne](https://discourse.julialang.org/u/Marco_Cusumano-Towne)\
**Post date:** [September 14, 2020, 7:13pm UTC](https://discourse.julialang.org/t/getting-off-the-ground-with-gen/44211/2 "2020-09-14T19:13:48Z")

</div>

Re your first question: there was a bug in your code for constructing the choice map. It should be:

```julia
choicemap([((:y, i), y[i]) for i in 1:n]...)

```

I ran the code after making that fix, and it estimates the mean of the Gaussian as expected.

Re your section question, custom proposal distributions are implemented as generative functions. For a random walk on the mean in your model, you can use, e.g.:

```julia
@gen function random_walk(trace, stdev)
   m ~ normal(trace[:m], stdev)
   # note that ~ is a newish syntactic sugar in Gen; this line is equivalent to m = @trace(normal(trace[:m], stdev), :m
end

```

Then you can apply it using a different variant of `mh` that accepts the proposal instead of the selection, e.g.:

```julia
trace, = mh(trace, random_walk, (0.1,))

```

Hope that helps!
