Inference algorithms for probabilistic programming are complex imperative programs with many moving parts. Efficient inference often requires customising an algorithm to a particular probabilistic model or problem, sometimes called inference programming. Most inference frameworks are implemented in languages that lack a disciplined approach to side effects, which can result in monolithic implementations where the structure of the algorithms is obscured and inference programming is hard. Functional programming with typed effects offers a more structured and modular foundation for programmable inference, with monad transformers being the primary structuring mechanism explored to date. This paper presents an alternative approach to programmable inference, based on algebraic effects, building on recent work that used algebraic effects to represent probabilistic models. Using effect signatures to specify the key operations of the algorithms, and effect handlers to modularly interpret those operations for specific variants, we develop three abstract algorithms, or inference patterns, representing three important classes of inference: Metropolis-Hastings, particle filtering, and guided optimisation. We show how our approach reveals the algorithms' high-level structure, and makes it easy to tailor and recombine their parts into new variants. We implement the three inference patterns as a Haskell library, and discuss the pros and cons of algebraic effects vis-a-vis monad transformers as a structuring mechanism for modular imperative algorithm design. It should be possible to reimplement our library in any typed functional language able to emulate effects and effect handlers.
翻译:概率编程中的推理算法是包含众多活动部件的复杂命令式程序。高效的推理通常需要针对特定概率模型或问题定制算法,这有时被称为推理编程。大多数推理框架在缺乏处理副作用规范方法的语言中实现,这可能导致算法的结构被掩盖、推理编程变得困难的整体式实现。带有类型化效果(typed effects)的函数式编程为可编程推理提供了更具结构性和模块化的基础,其中单子变换器(monad transformers)是迄今探索的主要结构化机制。本文提出了一种基于代数效应(algebraic effects)的可编程推理替代方案,建立在近期使用代数效应表示概率模型的工作之上。通过使用效果签名(effect signatures)来指定算法的关键操作,并利用效果处理器(effect handlers)模块化地解释这些操作以适配特定变体,我们开发了三种抽象算法(或称推理模式),它们代表了三个重要的推理类别:Metropolis-Hastings、粒子滤波(particle filtering)和引导优化(guided optimisation)。我们展示了我们的方法如何揭示算法的高级结构,并使得轻松定制和重组其组件以形成新变体成为可能。我们将这三种推理模式实现为一个Haskell库,并讨论了代数效应与单子变换器作为模块化命令式算法设计结构化机制的优缺点。我们的库应能在任何能够模拟效果和效果处理器的类型化函数式语言中重新实现。