# Working with ASTs

**URL:** <https://discourse.julialang.org/t/working-with-asts/19646>\
**Category:** General Usage\
**Tags:** question\
**Created:** [January 15, 2019, 4:18am UTC](https://discourse.julialang.org/t/working-with-asts/19646 "2019-01-15T04:18:14Z")\
**Posts on this page:** 6\
**Page:** 4

<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 21, 2019, 6:14pm UTC](https://discourse.julialang.org/t/working-with-asts/19646/61 "2019-01-21T18:14:51Z")

</div>

I am following your project with interest, but I have to admit that the mental model that works best for me is that _models are (log) density functions_ that map a bunch of numbers to a single number.

I recognize that there are benefits to the DAG representation (such as getting eg predicted quantities easily), but at the same time I find DAGs constraining and difficult to optimize, and the benefits rather trivial compared to the other programming costs of PPC.

Also, I think it is a misconception that `DynamicHMC` (or NUTS, or MCMC in general) provides a `rand`. Perhaps if you run it for infinite time, but the key aspect of `rand` for me is getting IID samples in O(1) time.

---

<div class="post-metadata">

**Author:** ![cscherrer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cscherrer/32/7631_2.png) [@cscherrer](https://discourse.julialang.org/u/cscherrer)\
**Post date:** [January 21, 2019, 6:51pm UTC](https://discourse.julialang.org/t/working-with-asts/19646/62 "2019-01-21T18:51:00Z")

</div>

> [@Tamas\_Papp](#):
>
> I am following your project with interest, but I have to admit that the mental model that works best for me is that _models are (log) density functions_ that map a bunch of numbers to a single number.

That’s great when you can get it, and it’s my usual focus as well. But there are plenty of other areas people are working in. Likelihood-free methods like ABC, particle filter algorithms like Turing uses, etc. Also, online algorithms like Kalman filtering have to maintain and update state, very different than function and gradient evaluation.

> [@Tamas\_Papp](#):
>
> I recognize that there are benefits to the DAG representation (such as getting eg predicted quantities easily), but at the same time I find DAGs constraining and difficult to optimize, and the benefits rather trivial compared to the other programming costs of PPC.

I’m not married to DAGs, but being able to get at one can be helpful for some algorithms. There are also tricks you can play to get at the Markov blanket, like expanding the functional form of the log-likelihood and checking variable co-occurrence within terms. Right now the biggest benefit I see of the dependency graph stuff is to understand the sparsity structure of the Hessian.

> [@Tamas\_Papp](#):
>
> Also, I think it is a misconception that `DynamicHMC` (or NUTS, or MCMC in general) provides a `rand` . Perhaps if you run it for infinite time, but the key aspect of `rand` for me is getting IID samples in O(1) time.

Once the data are fixed, everything is O(1) 😉

Seriously though, I see what you’re getting at, and I tend to agree. I would never advise “take this strongly-autocorrelated sampler and pretend it’s iid”. But that’s important to keep in mind for any sampling method.

Still, sometimes we need a sample as part of an algorithm. For example, each iteration of SVI samples from a given variational distribution. Maybe this is a proper `Distribution`, but it could also be a `Model`. It’s just a useful abstraction. This is exactly the approach Uber’s Pyro language takes.

Anyway, to be really pedantic about it, there’s no such thing as iid anyway. Any PRNG has state, and the implications of that always require care for more complex or high-dimensional sampling procedures 🙂

---

<div class="post-metadata">

**Author:** ![datnamer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/datnamer/32/3471_2.png) [@datnamer](https://discourse.julialang.org/u/datnamer)\
**Post date:** [January 21, 2019, 7:46pm UTC](https://discourse.julialang.org/t/working-with-asts/19646/63 "2019-01-21T19:46:35Z")

</div>

Have you seen [omega.jl’s](https://github.com/zenna/Omega.jl/issues/75) approach to inference and generative models? It’s intriguing.

---

<div class="post-metadata">

**Author:** ![cscherrer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cscherrer/32/7631_2.png) [@cscherrer](https://discourse.julialang.org/u/cscherrer)\
**Post date:** [January 21, 2019, 8:11pm UTC](https://discourse.julialang.org/t/working-with-asts/19646/64 "2019-01-21T20:11:57Z")

</div>

> [@datnamer](#):
>
> Have you seen [omega.jl’s](https://github.com/zenna/Omega.jl/issues/75) approach to inference and generative models? It’s intriguing.

Thanks, I hadn’t seen that one. I don’t know Zenna well, but I really enjoyed his ideas that I’ve seen - think it was at a poster session or two. I’ll check it out 🙂

---

<div class="post-metadata">

**Author:** ![cscherrer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cscherrer/32/7631_2.png) [@cscherrer](https://discourse.julialang.org/u/cscherrer)\
**Post date:** [January 29, 2019, 3:04pm UTC](https://discourse.julialang.org/t/working-with-asts/19646/65 "2019-01-29T15:04:13Z")

</div>

Thank you all for the helpful discussion. Here are some plans I have for a sprint on this work over my spring sabbatical:

> **[Soss.jl: Design Plans for Spring 2019](https://cscherrer.github.io/post/soss-update/)**
>
> If you've followed my work recently, you've probably heard of my probabilistic programming system Soss.jl. I recently had the pleasure of presenting these ideas at PyData Miami:
> \[N.B. Above is supposed to be an embedded copy of my slides from PyData...

---

<div class="post-metadata">

**Author:** ![c42f](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/c42f/32/52842_2.png) [@c42f](https://discourse.julialang.org/u/c42f)\
**Post date:** [January 30, 2019, 3:44am UTC](https://discourse.julialang.org/t/working-with-asts/19646/66 "2019-01-30T03:44:57Z")

</div>

> [@cscherrer](#):
>
> I’m sure there are plenty of details to work out, but it seems this could be a more principled way of organizing these concepts
> 
> - A _Distribution_ has methods `logpdf` , `rand` , etc
> - A `Model` may need additional information (or not) and then can be compiled into a `Distribution` . There will often be multiple ways of doing this compilation (it’s a form of inference), with corresponding tradeoffs.

This sounds fairly sensible to me. We didn’t resolve your original question but at this stage it might be best to try prototyping various of these compilation steps in something like your existing AST-based style. Then there will be a bunch of examples of the required source transformations and the semantics of the stuff inside `@model` might be clear by example.

I do think an AST based approach will run into various `eval` scoping-related composability issues as soon as you start using `Soss` from other modules and with custom `Distribution`s. As discussed up thread, there are some ways to mitigate this by carefully choosing the context for the eval.

[Previous page](https://discourse.julialang.org/t/working-with-asts/19646.md?page=3)
