# Language impediments to inference of real-time streaming models

**URL:** https://discourse.julialang.org/t/language-impediments-to-inference-of-real-time-streaming-models/39731
**Category:** Probabilistic Programming
**Created:** [May 19, 2020, 1:06am UTC](https://discourse.julialang.org/t/language-impediments-to-inference-of-real-time-streaming-models/39731 "2020-05-19T01:06:16Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![marty0801](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marty0801/32/12715_2.png) [@marty0801](https://discourse.julialang.org/u/marty0801)
#### Post date: [May 19, 2020, 1:06am UTC](https://discourse.julialang.org/t/language-impediments-to-inference-of-real-time-streaming-models/39731/1 "2020-05-19T01:06:16Z")

</div>

I’m debating whether to attempt a Julia solution to a particular “streaming” inference problem in which a global data model is constantly being updated with new data. I’m new to the language, but there seems to be language-related complications; Julia seems to favor statically-defined, rather than dynamically-generated, functions. I am aware Julia’s metaprogramming facilities, but would rather not delve into that if I can avoid it.

Here’s the problem. I want to perform inference on a global model (a parametric likelihood function) of real-time streaming data which is being modified in real time as follows. Incoming data is clustered; each cluster defines a parametric likelihood function of its constituent data; the global likelihood function is updated by multiplication by the likelihoods of the new data clusters. For example: Initial global model f\_0(x|\theta); new-data likelihood g(y|\theta); updated likelihood is f(x,y|\theta)=f\_0(x|\theta)\cdot g(y|\theta).

Questions:

1. How to do inference on the current model? This seems problematic since the global likelihood function cannot be recompiled in the same Julia session. (True?) How then do I take code that performed inference against f\_0 and point it at f(x,y)? What is the Julian approach? Use Revise? Rename the global model whenever I update with new data? My concern there is having potentially thousands of global models sitting around in memory indefinitely.
2. Should I be worried about the latency of compiling the updated model? In the example above, f\_0(x|\theta) may be very complex; g is relatively simple by comparison. When Julia compiles the new global model, f(x,y), does it efficiently reuse the previously compiled f\_0, or is the latter recompiled along with g?
3. Does anyone know of an existing Julia project doing something like this already?

Thanks!

---

<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: [May 19, 2020, 5:53am UTC](https://discourse.julialang.org/t/language-impediments-to-inference-of-real-time-streaming-models/39731/2 "2020-05-19T05:53:25Z")

</div>

> [@marty0801](#):
>
> Julia seems to favor statically-defined, rather than dynamically-generated, functions. I am aware Julia’s metaprogramming facilities, but would rather not delve into that if I can avoid it.

I would use an approach with higher order functions, chaining together the likelihood incrementally.

---

<div class="post-metadata">

### Author: ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)
#### Post date: [May 19, 2020, 6:03am UTC](https://discourse.julialang.org/t/language-impediments-to-inference-of-real-time-streaming-models/39731/3 "2020-05-19T06:03:28Z")

</div>

You can define `X` as `[x, y1, y2, y3]` and define `f(X) = f0(X[1]) * prod(g, X[2:end])`. If you have more data, just `push!` it to `X` and call `f` again on `X`.

Edit: although if you are multiplying many such terms, I would work in terms of the `log` and add instead to avoid underflow.

Edit 2: to avoid re-computing terms, you can make `f` a callable struct with a cache field and save the results of `f0(X[1])` and every computed value in the cache field in `f`. Alternatively, there is Memoization.jl to memoize the functions automatically.

---

<div class="post-metadata">

### Author: ![trappmartin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/trappmartin/32/1165_2.png) [@trappmartin](https://discourse.julialang.org/u/trappmartin)
#### Post date: [May 19, 2020, 6:36am UTC](https://discourse.julialang.org/t/language-impediments-to-inference-of-real-time-streaming-models/39731/4 "2020-05-19T06:36:14Z")

</div>

I did several projects related to BNP and dynamic compositional likelihoods. In my cases those compositions usually get quite complicated as they are also nested and, therefore, I always used an approach similar to what @mohamed82008 suggested for you in (2). But there are many approaches in Julia to do this and I in my experience Julia is one of the more suited language for this.

---

<div class="post-metadata">

### Author: ![marty0801](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marty0801/32/12715_2.png) [@marty0801](https://discourse.julialang.org/u/marty0801)
#### Post date: [May 19, 2020, 11:22am UTC](https://discourse.julialang.org/t/language-impediments-to-inference-of-real-time-streaming-models/39731/5 "2020-05-19T11:22:42Z")

</div>

Thank you! Have you, by chance, used this approach with automatic-differentiation packages, in particular ReverseDiff? Do they play nicely together?

---

<div class="post-metadata">

### Author: ![marty0801](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marty0801/32/12715_2.png) [@marty0801](https://discourse.julialang.org/u/marty0801)
#### Post date: [May 19, 2020, 11:28am UTC](https://discourse.julialang.org/t/language-impediments-to-inference-of-real-time-streaming-models/39731/6 "2020-05-19T11:28:07Z")

</div>

Thanks very much. Can you describe or point me to examples of the other approaches you alluded to?

---

<div class="post-metadata">

### Author: ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)
#### Post date: [May 19, 2020, 1:42pm UTC](https://discourse.julialang.org/t/language-impediments-to-inference-of-real-time-streaming-models/39731/7 "2020-05-19T13:42:15Z")

</div>

ReverseDiff should be fine yes. If it is not, please open an issue with a minimal working example.
