Continualization of Probabilistic Programs With Correction

Probabilistic Programming offers a concise way to represent stochastic models and perform automated statistical inference. However, many real-world models have discrete or hybrid discrete-continuous distributions, for which existing tools may suffer non-trivial limitations. Inference and parameter estimation can be exceedingly slow for these models because many inference algorithms compute results faster (or exclusively) when the distributions being inferred are continuous. To address this discrepancy, this paper presents Leios. Leios is the first approach for systematically approximating arbitrary probabilistic programs that have discrete, or hybrid discrete-continuous random variables. The approximate programs have all their variables fully continualized. We show that once we have the fully continuous approximate program, we can perform inference and parameter estimation faster by exploiting the existing support that many languages offer for continuous distributions. Furthermore, we show that the estimates obtained when performing inference and parameter estimation on the continuous approximation are still comparably close to both the true parameter values and the estimates obtained when performing inference on the original model.


Introduction
Probabilistic programming languages (PPLs) offer an intuitive way to model uncertainty by representing complex probability models as simple programs [28]. A probabilistic programming system then performs fully automated statistical inference on this program by conditioning on observed data, to obtain a posterior distribution, all while hiding the intricate details of this inference process.
Probabilistic inference is a computationally hard task, even for programs containing only Bernoulli distributions (#P-complete [18]), but prior work has shown that for many inference algorithms, continuous and smooth distributions (such as Gaussians) can be significantly easier to handle than the distributions having discrete components or discontinuities in their densities [15,53,52,9,56]. However, many popular Bayesian models can have distributions which are discrete or hybrid discrete-continuous mixtures (denoted simply as "hybrid") leading to computationally inefficient inference for much the same reason. Particularly when the observed variable is a discrete-continuous mixture, inference may fail altogether [65]. Likewise even if the observed variable and likelihood are continuous, the prior or important latent variables, may be discrete (e.g., Binomial) leading to an equally difficult discrete inference problem [61,50].
In fact, a number of popular inference algorithms such as Hamiltonian Monte Carlo [48], NUTS [31,50], or versions of Variational Inference (VI) [9] only work for restricted classes of programs (e.g. by requiring each latent be continuous) to avoid these problems. Furthermore, we cannot always marginalize away the program's discrete component since it is often precisely the one we are interested in. Even if the parameter was one which could be safely marginalized out, doing so may require the programmer to use advanced domain knowledge to analytically solve and obtain a new model and re-write the program completely, which can be well beyond the abilities of the average PPL user. Problem statement: We address the question of how to accurately approximate the semantics of a probabilistic program P whose prior or likelihood is either discrete or hybrid, with a new program P C , where all variables follow continuous distributions, so that we can exploit the aforementioned inference algorithms to improve inference in an easy, off-the-shelf fashion.
While a programmer could manually rewrite the probabilistic program or model and apply approximations in an ad hoc manner, such as simply adding Gaussian noise to each variable, this would be neither sufficient nor wise. For instance, it has been shown that when a model contains Gaussians, how they are programatically written and parametrized can impact the inference time and quality [29,5]. Also, by not correcting for continuity in the program's branch conditions, one could significantly alter the probability of executing a particular program branch, and hence alter the overall distribution represented by the probabilistic program. Leios: We introduce a fully automated program analysis framework to continualize probabilistic programs for significantly improved inference performance, especially in cases where inference was originally intractable or prohibitively slow.
An input to Leios is a probabilistic program, which consists of (1) model that specifies the prior distributions and how the latent variables are related, (2) specifications of observable variables, and (3) specifications of data sets. Leios transforms the model, given the set of the observable variables. This model is then substituted back into the original program to produce a fully continuous probabilistic program leading to greatly improved inference. Furthermore the approximated program can easily be reused with different, unseen data. Figure 1 presents the main workflow of Leios : -Distribution transformer and Boolean predicate correction: Leios first finds individual discrete distribution sample statements to replace with continuous approximations based on known convergence theorems that specifically match the distributions' first moments [23]. Leios then performs a dataflow analysis to identify and then correct Boolean predicates in branches to best preserve the original program's probabilistic control flow. To correct Boolean predicates, we convert the program to a sketch and fill in the predicates with holes that will then be synthesized with the optimal values. We ensure that the distribution of the model's observed variables is fully continuous with a differentiable density function, by transforming it using an approach that adapts Smooth Interpretation [14] to probabilistic programs. We describe the transformations in Section 4. -Parameter Synthesizer: Leios determines the optimal parameters which minimize a numerical approximation of the Wasserstein Distance to fill in the holes in the program sketch. This step of the algorithm can be thought of as a "training phase" much like in machine learning, and we need only perform it once for a given program, regardless of the number of times we will later perform inference on different data sets. These parameters correspond to continuity correction factors in classical probability theory [23]. We describe the synthesizer in Section 5.
Contributions: This paper makes the following main contributions: -Concept: To the best of our knowledge, Leios is the first technique to automate program transformations that approximate discrete or hybrid discretecontinuous probabilistic programs with fully continuous ones to improve inference. It combines insights from probability theory, program analysis, compiler autotuning, and machine learning. -Program Transformation: Leios implements a set of transformations on distributions and the conditional statements that can produce provably continuous probabilistic programs that approximate the original ones. -Parameter Synthesis: We present a synthesis algorithm that corrects the probabilities of taking specific branches in the probabilistic program and improves the overall inference accuracy. -Evaluation: We evaluated Leios on a set of ten benchmarks from existing literature and two systems, WebPPL (using MCMC sampling) and Pyro (using stochastic variational inference). The results demonstrate that Leios can achieve a substantial decrease in inference time compared to the original model, while still achieving high inference accuracy. We also show how a continualized program allows for easy off-the-shelf inference that is not always readily available to discrete or hybrid models.    Figure 2 (a) presents a program that infers the parameters of the distribution modeling the number of recruiters coming to a recruiting fair given both the number of offers multiple students receive (line 1). As the number of recruiters may vary year to year, we model this count as a Poisson distribution (line 5). However, to accurately quantify how much this count varies year to year, we want to estimate the unknown parameter of this Poisson variable. We thus place a uniform prior over this parameter (line 4).

Example
The example represents the student GPAs in lines 7-9: it is either a perfect 4.0 score or any number between 0 and 4. We model the perfect GPA with a discrete distribution that has all the probability mass at 4.0 (line 7). To model the imperfect GPA, we use a Beta distribution (line 8), scaled by 4 to lie in the range [0.0, 4.0]. Finally, the distribution of the GPAs is a mixture of these two components (line 9). Our mixture assumes that 5% of students obtain perfect GPAs.
Because the GPA impacts the number of interviews a student receives, our model incorporates control flow where each branch captures the distribution of interviews received, conditioned on the GPA being in a certain range (lines [11][12][13][14][15][16][17]. Each student's resume is available to all recruiters and each recruiter can request an interview or not, hence all three of the Interviews distributions follow a Binomial distribution (here denoted as bin) with the same n (number of recruiters) but with different probabilities (higher probabilities for higher GPAs). From the factor statement (line 23) we see that the Offers variable governs the distribution of the observed data, hence it is the observed variable. Furthermore, given the values of all latent variables, Offers follows a Binomial distribution (line 19), hence the likelihood function of this program is discrete.
This program poses several challenges for inference. First, it contains discrete latent variables (such as the Binomials), which are expensive to sample from or rule out certain inference methods [26]. Second, it contains a hybrid discrete-continuous distribution governing the student GPA, and such hybrid distributions are challenging for inference algorithms [65]. Third, the model has complex control flow introduced by the if statements, making the observable data follow a (potentially multimodal) mixture distribution, which is yet another obstacle to efficient inference [43,17]. Lastly, the discrete distribution of the observed data and likelihood also hinder the inference efficiency [61,50,59].

Continualization
Our approach starts from the observation that inference with continuous distributions is often more efficient for several inference algorithms [53,52,56]. Leios first continualizes discrete and hybrid distributions in the original model. Starting in line 5 in Figure 2 (b), we approximate the Poisson variable with a Gaussian using a classical result [16], hence relaxing the constraint that the number of recruiters be an integer. (For ease of presentation we created new variables mu p and sigma p corresponding to the parameters of the approximation; Leios simply inlines these.) We next approximate the discrete component of the GPA hybrid mixture distribution by a Gaussian centered at 4 and small tunable standard deviation β (line 7). The GPA is now a mixture of two continuous distributions. We then transform all of the Binomials to Gaussians (lines 14, 18, 22, and 26) using another classic approximation [23].
Finally, Leios smooths the observed variables by a Gaussian to ensure the likelihood function is both fully continuous and differentiable. In this example we see that the approximation of the Binomial already makes the distribution of Offers (given all latent values) a Gaussian, hence this final step is not needed.
After continualization, the GPA cannot be exactly 4.0, thus we need to repair the first conditional branch of the continualized program. In line 11, we replace the exact equality predicate with the interval predicate 4-θ 1 < GPA < 4+θ 2 where each θ is a hole whose value Leios will synthesize. Leios finds all such branching predicates by tracking transitive data dependencies of all continualized variables.

Parameter Synthesis
Our continuous approximation should be close enough to the original model such that upon performing inference on the approximation, the estimations obtained will also be close to the ground-truth values. Hence Leios needs to ensure that the values synthesized for each θ are such that for every conditional statement, the probability of executing the true branch in the continualized program roughly matches the original (ensuring similar likelihoods). In probability theory, this value has a natural interpretation as a continuity correction factor as   it "corrects' the probability of a predicate being true after applying continuous approximations. For the (GPA == 4) condition, we might think about using a typical continuity correction factor of 0.5 [23], and transform it to 4-0.5 < GPA < 4+0.5. However, in that case, the second else if (GPA > 3.5) branch would never execute, thus significantly changing the program's semantics (and thus the likelihood function). Experimentally, such an error can lead to highly inaccurate inference results.
Hence we must synthesize a better continuity correction factor that makes the approximated model "closest" to the original program's with respect to a welldefined distance metric between probability distributions. In this paper, we will use the common Wasserstein distance, which we describe later in Section 5. The objective function aims to find the continuity correction factors that minimize the Wasserstein distance between the original and continualized models. Figure 3 (a) shows the continualized model. Leios calculated that the optimal values for the first branch are θ 1 = 0.00001 (hence the lower bound is 3.99999) and θ 2 = 0.95208 (hence the upper bound is 4.95208) in line 11, and θ 3 = 0.00012 (hence the lower bound is 3.500122) for the branch in line 15. Intuitively the synthesizer found the upper bound 4.95208 so that any sample larger than 4 (which must have come from the right tail of the continualized perfect GPA) is consumed by the first branch, instead of accidentally being consumed by the second branch. Another part of the synthesis step is to make sure that approximations do not introduce run-time errors. Since Interviews is now sampled from Gaussian, there is a small possibility that it could become negative, thus causing a runtime error (since we later take its square root). By dynamically sampling the continualized model during the parameter synthesis, as part of a light-weight auto-tuning step, Leios checks if such an error exists. If it does, Leios can instead use a Gamma approximation (which is always non-negative).
While continualization incurs additional computational cost, this cost is typically amortized. In particular, continualization needs to be performed only once. The continualized model can be then be used multiple times for inference on different data-sets. Further, we experimentally observed that our synthesis step is fast. In this example, for all the values of β we evaluated, this step required only a few hundred iterations to converge to the optimal continuity correction factors, as shown in Figure 3 (b).

Improving Inference
Upon constructing the continuous approximation of the model, we now wish to perform inference by conditioning upon the outcomes of 25 sampled students. To make a fair comparison, we compile both the original and continuous versions down to Webppl [26] and run MCMC inference (with 3500 samples and a burnin of 700). We also seek to understand how smoothing latent variables improves inference, thus we also compare against a naively continualized version where only the observed variable was smoothed using the same β, number of MCMC samples and burn-in. Figure 4 presents the distribution of the Offers variable in the original model, naively smoothed model, and the Leios-optimized model. The continuous approximation achieved by Leios is smooth and unimodal, unlike the naively smoothed approximation, which is highly multimodal. However all models have similar means Using these three models for inference, Figure 5 (a) presents the posterior distribution of the variable param for each approach. We finally take the mean as the point-estimate, τ est , of the parameter's true value τ . Figure 5 (b) presents the run time and the error ratio, | τ −τest τ |, for each approach (for the given true value of 37). It shows that our continualized version leads to the fastest inference.

Syntax and Semantics of Programs
We present the syntax and semantics of the probabilistic programming language on which our analyses is defined  The syntax is similar to the ones used in [24,51]. Unlike [51], our syntax does include exact equality predicates, which introduce difficulties during the approximation. To give the developer the flexibility in selecting which parts of the program to continualize, we add the CONST annotation. It indicates that the variable's distribution should not be continualized. Until explicitly noted, we will not use this annotation in the rest of the paper. For simplicity of exposition, we present only a single DataBlock and ObserveBlock, but our approach naturally extends to the cases with multiple data and observed variables.

Measure Theory Preliminaries
Though various semantics have been proposed [44,36,7], we adapt the sub-probability measure transformer semantics of Dahlqvist et al. [19]. We will use the terms distribution and measure interchangeably.

Definition 1.
A program state σ ∈ S is a n-tuple of real numbers: S = R n where the i th tuple element corresponds to the i th program variable's value.

Definition 3.
A measure μ over R n is a mapping from B{R n } to [0, +∞) such that μ(∅) = 0 and μ( i∈N Xi) = i∈N μ(Xi) when all Xi are mutually disjoint. A probability measure is a measure that satisfies μ(R n ) = 1 and a sub-probability measure is one satisfying μ(R n ) ≤ 1. The simplest measure is the Dirac measure denoted as δa i (S) = 1 if ai in S else 0. We denote the set of all sub-probability measures as M(R n ).

Semantics
Expression Level Semantics Arithmetic Expression semantics are standard, they map states σ ∈ R n to values, equivalently Expr : R n → R. Boolean Expression Semantics, denoted BExpr , simply return the set of states Bi ∈ B{R n } satisfying the Boolean conditional.

Distribution Semantics
The interpretation of a distribution is a kernel, κ, mapping a state to the measure associated with the specific parametrization of the distribution in that state. Since measures are set functions we will represent them as λ abstractions. The signature is Dist : Where fCont and fDisc are the density and mass functions, respectively, of the primitive distribution being sampled from (e.g., Supp is the distribution's support.

Statement Level Semantics
The statement-level semantics are shown in Figure   6. We interpret each statement as a (sub) measure transformer, hence the semantic signature is Statement : M(R n ) → M(R n ) . The skip statement returns the original measure and the abort statement transforms any measure to the 0 sub-measure. The condition statement removes measure from regions not satisfying the Boolean guard B. The factor statement can be seen as a "smoothed" version of condition that uses g, a function of the observed data and its distribution, to re-weight the measure associated with a set by some real value in [0, 1] (as opposed to strictly 0 or 1). Deterministic assignment transforms the measure into one which assigns to any set of states S the same value that the original measure μ would have assigned to all states that end up in S after executing the assignment statement. Probabilistic Assignment updates the measure so that x i's marginal is the measure associated with Dist, but with the parameters governed by μ.
An if else statement can be decomposed into the sum of the true branch's measure and the false branch's measure. The while loop semantics are the solution to the standard least fixed point equation [19], but can also be viewed as a mixture distribution where each mixture component corresponds to going through the loop k times. A for loop is just syntactic sugar for a sequencing of a fixed number of statements. We note that the Data block does not affect the measure (it is also syntactic sugar, and could simply be inlined in the Observe block). The program can be thought of as starting in some initial input measure μ 0 where each variable is undefined (which could simply mean initialized to some special value or even just zero), and as each variable gets defined, that variable's marginal (and hence the joint measure μ) gets updated.

Continualizing Probabilistic Programs
Our goal is to synthesize a new continuous approximation of the original program P . We formally define this via a transformation operator T β P [•]: P rogram → P rogram. Our approach operates in two main steps: (1) We first locally approximate the program's prior and latent variables using a series of program transformations to best preserve the local structural properties of the program and then apply smoothing globally to ensure that the likelihood function is both fully continuous and differentiable.

Fig. 6: Denotational Semantics of Probabilistic Programs
(2) We next synthesize a set of parameters that (approximately) minimize the distance metric between the distributions of the original and continualized models and we use light-weight auto-tuning to ensure the approximations do not introduce runtime errors.

Overview of the Algorithm
Algorithm 1 presents the technique for continualizing programs. It takes as input a program P containing a prior or observed variable that is discrete (or hybrid) and returns T β P [P ], a probabilistic program representing a fully continuous random variable with a differentiable likelihood function. The algorithm uses a tunable hyper-parameter β ∈ (0, ∞) to control the amount of smoothing (like in [14]). A smaller β leads to less smoothing, while a larger β leads to more smoothing, however the smallest β does not always lead to the best inference, and vice-versa, as can be seen in section 7.
In line 3 of Algorithm 1 Leios constructs a standard control flow graph (CFG) to represent the program, using a method called GetCFG(). This data structure will form the basis of Leios's future analyses. Each CFG node corresponds to a single statement and contains all relevant attributes of that statement. Leios then uses this CFG to build a data dependency graph (line 4) which will be used for checking which variables are tainted by the approximations. In line 5 Leios then applies T β P [•] to obtain a continualized sketch, PC . Lastly, Leios synthesizes the optimal continuity correction parameters (line 7), and in doing so, samples the program to detect if a runtime error occurred, also returning a Boolean flag success to convey this information. If a runtime error did occur we find the expression causing it (line 9) and then in lines 10-12 reapply the safer transformations (e.g., Gamma instead of Gaussian) to all possible dependencies which could have contributed to the runtime error.

Distribution and Expression Transformations
To continualize each variable, Leios mutates the individual distributions and expressions assigned to latent variables within the program. We use a transform operator for expressions and distributions T β E [•]: Expr ∪Dist → Expr ∪Dist, which we define next.
The rationale for this definition is that these approximations all preserve key structural properties of the distributions' shape (e.g., the number of modes) which have been shown to strongly affect the quality of inference [25,45,17]. Second, these continuous approximations all match the first moment of their corresponding discrete distributions, which is another important feature that affects the quality of approximation [53]. We refer the reader to [54] to see that for each distribution on the left, the corresponding continuous distribution on the right has the same mean. These approximations are best when certain limit conditions are satisfied, e.g. λ ≥ 10 for approximating a Poisson distribution with Gaussian, hence the values in the program itself do affect the overall approximation accuracy.
However, if we are not careful, a statement level transformation could introduce runtime errors. For example, a Binomial is always non-negative, but its Gaussian approximation could be negative. This is why T β E [•] has multiple transformations for the same distribution. For example, in addition to using a Gaussian to approximate both a Binomial and a Poisson, we also have a Gamma approximation since a Gamma distribution is always non-negative. Likewise we have a Beta approximation to a Bernoulli if we require that the approximation also have support in the range [0, 1]. Leios uses auto-tuning to safeguard against such errors during the synthesis phase, whereby when sampling the transformed program, if we encounter a run-time error of this nature, we simply go back and try a safer (but possibly slower) alternative (Algorithm 1 line 12). Since there are only finitely many variables and (safer) transformations to apply, this process will eventually terminate. For discrete distributions not supported by the specific approximations, but with fixed parameters, we empirically sample them to get a set of samples and then use a Kernel Density Estimate (KDE) [62] with a Gaussian kernel (the KDE bandwidth is precisely β) as the approximation.
Lastly, by default all discrete random variables become approximated with continuous versions, however we leave the option to the user to manually specify CONST in front of a variable if they do not wish for it to be approximated (in which case we no longer make any theoretical guarantees about continuity).

Influence Analysis and Control-Flow Correction of Predicates
Simply changing all instances of discrete distributions in the program to continuous ones is not enough to closely approximate the semantics of the original program. We additionally need to ensure that such changes do not introduce control flow errors into the program, in the sense that quantitative properties such as the probability of taking a particular branch need to be reasonably preserved.
Avoiding Zero Probability Events A major concern of the approximation is to ensure that no zero-probability events are introduced, such as when we have an exact equality "==" predicate in an if, observe or while statement and the variable being checked was transformed from a discrete to a continuous type. For example, discrete programs commonly have a statement like x := Poisson(1) followed by a conditional such as if (x==4), because the probability that a discrete random variable is exactly equal to a value can be non-zero. However upon applying our distribution transformations and transforming the distribution of x from a discrete Poisson to a continuous Gaussian, the conditional statement "if (x==4)" now corresponds to a zero probability (or measure zero) event, as the probability that an absolutely continuous probability measure assigns to the singleton set {4} is by definition zero. Thus, if not corrected for, we could significantly change the probabilities of taking certain branches and hence the overall distribution of the program.
The converse can also be true: applying approximations can make a zero probability event in the original program now have non-zero probability. For example, in x := DiscUniform(1,5); if (x<3 and x>2) the true branch has probability zero of executing but this becomes non-zero after approximations are applied. However, the branch paths like these in the original model could be identified by symbolic analysis (e.g., [24]) and removed via dead code elimination during pre-processing.

Correcting Control Flow Probabilities via Static Analysis
To prevent zero-probability events and ensure that the branch execution probabilities of the continualized program closely matches the original's, we use data dependence analysis to track which if, while or condition statements have logical comparisons with variables "tainted" by the approximations. A variable v is "tainted" if it has a transitive data dependence on an approximated variable, and we use reaching definitions analysis [35] on the program's CFG to identify these.
As shown in Algorithm 1 line 4, to compute the reaching definitions analysis we use a method called ComputeDataFlow() as part of a pre-transformation pass whereby for each program point in the CFG, each variable is marked with all the other variables on which it has a data-dependence. These annotations are stored in a data structure called DataDepGraph which maps nodes (program points) to sets of tuples where each tuple contains a variable, the other variables it depends on (and where they are assigned), and lastly, whether it will become tainted. Note that in the algorithm this step is done before the previously discussed expression-level transformations, hence why ComputeDataFlow() marks which variables will become continualized and which ones will not (i.e if a variable already defines a continuous random variable or was annotated with CONST). Furthermore, though we are computing the data dependencies before the approximations, because the approximations do not re-order or remove statements, all data dependencies will be the same before and after applying the approximations.

Transform Operator For Boolean Expressions
We take all such control predicates that contain an exact equality "==" comparison with a tainted variable and transform these predicates from exact equality predicates to interval-style predicates. Thus if we originally had a predicate of the form if(x==4) we will mutate this into a predicate of the form if(x>4-θ 1 && x<4+θ2) where θ are now placeholder values that will need to be filled with a concrete value during the synthesis phase (Section 5). Hence checking for exact equality gets relaxed to checking for containment within the interval (4 − θ 1, 4 + θ2). We also need to correct < and <= predicates if one of the variables was approximated or transitively affected by an approximation.
Hence we also define our transform operator T β B [•] : BExpr → BExpr at the level of Boolean expressions: CONST x and CONST y specified Because we have already pre-computed DataDepGraph one can check if a variable in a given statement or expression is tainted (or marked as CONST) in constant time. This correction has a natural interpretation in classical probability theory. It is well known that to approximate a discrete distribution X with a continuous oneX, we need a continuity correction factor, θ, such that P (X < x) ≈ P (X < x + θ) (hence why T β B [•] also corrects < and <= predicates). For simple approximations (i.e Binomial to Gaussian), the canonical correction factor is known (θ = 0.5) [23], however for the general case, it is not. Furthermore, it has been shown that in many cases, 0.5 is not the best correction factor [3].

Bringing it all together: Full Program Transformations
Having defined the transformation for distributions, arithmetic and Boolean expressions, we now define the program transformation operator T β P [•]: P rogram → P rogram inductively: The abort, factor and skip statements and the DataBlock remain the same after applying the transformation operator T β P [•].

Ensuring Smoothness Upon applying the statement-level transformations and
performing both dataflow analysis and predicate mutations, Leios ensures each latent variable comes from a continuous distribution. However a continuous distribution may still have jump discontinuities or non-differentiable regions in its density function (such as a uniform distribution), which can make inference difficult [66]. Furthermore it is known that performing parameter estimation on data that is distributed according to a discontinuous or non-smooth density function, or on distributions with a nonsmooth likelihoods can be just as challenging [50,1,59]. Thus to make the Program's likelihood function and density function of the observed data fully smooth, we need to apply additional Gaussian smoothing.
Since it would be redundant to apply smoothing if we already knew this variable came from a smooth distribution (as in the example) hence we make this simple check first. The following transformation performs this on the observed variables (which appear in the factor statement). We could perform additional smoothing for every variable to ensure each has a differentiable density, however we empirically observed that the variance added up enough to where inference quality deteriorated, hence we only apply the additional smoothing to observed variables.
Having defined the statement-level transformations we now state a theorem about T β P [•] preserving continuity. As many applications may invoke inference at any point in the program [46,60], it is important that absolute continuity of each marginal hold at every point. Proof. (sketch) To prove the theorem we will show that when any variable xi is initially defined, it comes from an absolutely continuous distribution and furthermore that the semantics of each statement in T β P [P ] preserves the absolute continuity of each marginal measure (where μx i ≡ μ(R × ... × Bi × R... × R)), equivalently for any statement, any (already defined) variable xi and any Borel set Bi ∈ B{R}: Case 1. skip and abort: Since skip is the identity measure transformer of each defined marginal measure μx i was A.C. before, then they will trivially be so afterward since they are unchanged. abort sends each marginal to the 0 sub-measure (which is trivially A.C.).

Synthesis of Continuity Correction Parameters
We now present our procedure for synthesizing optimal continuity correction parameters which covers lines 6 to 15 in Algorithm 1. This can be thought of as a "training" step which fits the continualized model to the original one. It is important to note that this step is agnostic to the observed data (it only fits to the Model ), hence it need only be done once off-line, regardless of how many times we perform inference on new data sets. Furthermore, even if we do not have parameters to synthesize, this step is still useful for catching runtime errors caused by the approximations, so that we can go back and apply safer approximations if necessary.

Optimization Framework
Ideally the posteriors of our approximated program T β P [P ] and the original P , should be reasonably close. However a specific posterior is induced by the corresponding dataset, if our optimization objective tries to minimize the statistical distance from T β P [P ] to P , we would simply be over-fitting to the data and we would not be able to re-use T β P [P ] for new data sets with different true parameters. Instead our objective is to minimize the distance between the original model M , which is simply the fragment of P that does not contain the data or observe block (and hence only defines the prior, likelihood and latent variables), and the corresponding continualized approximation, T β P [M ]. To do so, we need to choose the best possible continuity correction factors, θ, for T β P [M ]. Thus we define the "optimal" parameters as those which minimize a distance metric d between probability measures d : We also need to ensure that the metric can (a) compute the distance between discrete and continuous distributions and (b) is such that if models or likelihoods are close with respect to d, the posteriors should be as well.
Wasserstein Distance We choose to use the Wasserstein distance primarily because (1) it can measure the distance between a continuous and discrete distribution (unlike KL-Divergence or Total Variation Distance) and (2) prior work has shown that when performing inference, if using the Wasserstein distance as the chosen metric to approximate a likelihood, the (approximate) posteriors induced are comparable to the true posteriors (obtainable if one used the true likelihood) [49]. Additionally, unlike other metrics, the Wasserstein metric incorporates the underlying difference in geometry of the distributions (which strongly affects inference accuracy [37,59]).
Let M (μ 0) represent the renormalized measure associated to the observed variables of the original model and let T β P [M θ ] (μ0) represent the observed variables of the continualized model, but where a given continuity correction factor θ has been substituted in (both measures start in initial distribution μ 0). Furthermore, let J ⊆ M(R 2 ) represent the set of all joint measures with marginal measures M (μ0) and T β P [M θ ] (μ0). Hence we now define the 1-Wasserstein Distance: We also provide further justification why the Wasserstein Distance is a sensible metric to use. It is well known that a mixture of Gaussians can converge in distribution to any continuous random variable, however existing work has shown that a mixture of Gaussians can approximate any discrete distribution in the Wasserstein Distance arbitrarily well [20].

Objective Function
We now formulate our optimization approach as follows, wherê θ is the parameter vector minimizing the Wasserstein Distance with respect to the original model M , and d is the number of parameters to synthesize.
To restrict the search space we follow common practice [23,3] by requiring each θi ∈ (0, 1). Such optimization problem lacks a closed form solution. Symbolically computing the Wasserstein Distance is intractable, hence we numerically approximate it via the empirical Wasserstein Distance (EWD) between observed samples of M and T β P [M θ ]. Because this step is fully dynamic (we run and sample the model), the samples are conditioned upon successfully terminating, and hence the model's sub-measure has been implicitly renormalized to a full probability measure, thus justifying the use of a fully renormalized measure in equations (1)   Though intuitively we would expect that as we apply less smoothing (i.e. β < 1), the optimal θi should also be smaller (less need for correction) and the continualized program should become closer to the original, a simple negative result illustrates this is not always the case and that the dependence between the smoothing and continuity correction must be non-linear.

Remark 1.θ cannot be linearly proportional to β.
Proof. Let X be the constant random variable that is 0 with probability 1 and let X ∼ Gaussian(0, β). Furthermore, let I := (X == 0) and Ic := (cβ ≤ X ≤ cβ) be two indicator random variables. Intuitively we want Ic to have the same probability of being true as I for any β. However if c is constant (such as 1) then P r(cβ ≤ X ≤ cβ) will always be the same regardless of β (when c = 1, the probability is always 0.68).

Optimization Algorithm
Algorithm 2 presents our approximate synthesis algorithm, which is called as a subroutine in the main algorithm. As seen in line 2, if there are no parameters to be synthesized (d == 0) we still sample the continualized program in hopes of uncovering a possible runtime error (or gaining statistical confidence that one does not occur). We check for such an error in line 4 and if one exists, we return immediately, with the flag variable set to false (line 5).
To evaluate the EWD objective function (when there are parameters to synthesize), Algorithm 2 follows a technique from [14] and uses a Nelder-Mead search (line 11), due to Nelder-Mead's well known success in solving non-convex program synthesis problems. We first extract the fragment of the programs corresponding to the models, . This process continues until the search converges to a minimizing parameter, p, that is within the stopping threshold > 0 or encounters a runtime error during the sampling (which is checked in line 12). As before, if we encounter such an error we immediately return with the flag set to false (line 13). Following [14], we successively restart the Nelder-Mead search from k evenly spaced grid points in [0, 1] d (hence the loop in line 10), to find the globally optimal parameter (hence our approach is robust to local minima), which we successively update in lines 15-16. If no runtime error was ever encountered, we substitute in the parameters with the minimum EWD over all runs,θ, to the fully continuous program T β P [P ] and return (line 20). Though it can be argued this sampling is potentially as difficult as the original inference, we reiterate that we need only do this once offline, hence the cost is easily amortized. Table 1 presents the benchmarks. For each benchmark, Columns 2 and 3 present the original prior and likelihood type, respectively. Column 4 presents whether the continuity correction was applied. Column 5 presents the time to continualize the program, T Cont.. As can be seen in Columns 4 and 5 the total continualization time, TCont., depends on whether parameters had to be synthesized. GPAExample had the longest T Cont. at 3.6s, due to the complexity of the multiple predicates, however these times are amortized as our synthesis step is done only once.

Benchmarks
As our problem has received little attention, no standard benchmark suites exist. In fact, to make inference tractable, for many models, developers would construct continuous approximations by hand, in an ad hoc fashion. However we wanted a benchmark suite that showcased all 3 inference scenarios that our approach works for: (1) discrete/hybrid prior and discrete/hybrid likelihood (2) continuous prior but discrete/hybrid likelihood and (3) discrete/hybrid prior but a continuous likelihood. Therefore, we obtained the benchmarks in two ways. First, we looked at variations of the mixed distributions benchmarks previously published in the machine learning community, e.g., [65,58], which served as the inspiration for our GPAExample. Second, we took existing benchmarks [27,30] for which designers modeled certain distributions with continuous approximations, and we retro-fitted these models with the corresponding discrete distributions. This step was done for Election, Fairness, SVMfairness, SVE, and TrueSkill. These discretizations were only applied where they made sense, e.g., the Gauss(np,np(1-p)) in the original Election program became discretized as Binomial(n,p). We also took popular Bayesian models from Cognitive Science literature which use multiple discrete latent variables [39] and these models We will refer to these as simply "Original" and "Naive" respectively.

Inference Accuracy Comparison using Ground Truth Our experimental
design compares the respective inference estimates with the ground truth. We set the experiments as follows: For each of the original discrete or hybrid programs P , we replace the program variable corresponding to the prior distribution with a fixed value τ (the ground-truth) to obtain P (τ ). We then sample P (τ ) to obtain 25 observed data points, which will be used to test inference performance on P , P NS, and PLeios respectively. To test inference performance we then score P (original program), PNS (naively smoothed program), and PLeios against the observed data points to infer the posterior over the ground truth parameter τ . Note the programs only have access to the data samples, but not τ . For each of the 3 versions: P , PNS, and PLeios, we take the inferred posterior means as the estimates of the value, and then compare it with the ground-truth value τ to measure the error ratio E = τ −τ est τ . This entire procedure is repeated for 10 different values of τ to get a representative average of inference performance over a wide range of true parameter values.

Inference Time Measurement
We measure the time taken for inference for each version using built-in timers (which exclude file reading and warm-up). A timeout of 10 minutes was used for the inference step. We used this same procedure for both MCMC-based sampling in WebPPL and Variational Inference in Pyro.

Evaluation
We study the following three research questions:

RQ1
Can program continualization make inference faster, while still maintaining a high degree of accuracy, compared to the original program and naive smoothing?
RQ2 How do performance and accuracy vary for different smoothing factors β? RQ3 Can program continualization enable running transformed programs with offthe-shelf inference algorithms that cannot execute the original programs?  Table 2 we can see that on average, Leios leads to faster inference than both the Original (no approximations) and Naive (0.584s vs 2.797s and 0.894s, respectively). The Naive version was also faster than the original, giving more evidence that continuous models (even when just the observed variable is continualized) yield faster inference.  For accuracy, inference performed via Leios was on average more accurate than Naive (E = 0.079 vs. 0.098, respectively). Both were slightly less accurate than inference performed on Original (E = 0.043). This is not unreasonable as Original has no approximations applied (which are the main source of inference error). However the Original failed on Election, SVE, and SVMfairness. For Election, a large Binomial latent led to a timeout, and it also slowed the Naive version relative to Leios (3.23s vs 0.61s). The Original failed on SVE since it is a hybrid discrete-continuous model (which can make inference intractable [65,6]). SVMfairness is a non-linear model where many latent variables have high variances, leading to inference on the Original failing to converge; Leios and Naive had higher error on this benchmark, for much the same reason (though Leios was still significantly better than Naive, E = 0.261 vs 0.454).

RQ1: Benefits of Continualization
Although Leios was faster than Original in all cases, for TrueSkill and SVMfairness, Leios was somewhat slower than Naive. This is likely because the discrete latent variables in these benchmarks had small enough parameters (Binomial with small n). Similarly, for Fairness, Leios was slightly less accurate than Naive because the Gaussian approximation can be less accurate for smaller n. Figure 7 presents the average inference times and ERs for different smoothing factors β. In both cases, X-axes represent smoothing factors. The Y-Axis of the left subfigure presents time, and Y-Axis of the right presents error ratio compared to the ground truth (less is better). Figure 7 (a) shows that Inference on the programs constructed by Leios is nontrivially faster than inference done on the naively smoothed version, regardless of the β used (which has negligible affect on the inference time for the β we examined). Figure 7 (b) presents how accuracy directly depends on β. The Error Ratio for Leios reaches a local minimum when β = 0.1. Because Leios achieves "global" smoothing by approximating each latent, a larger value for β is not needed (unlike Naive). We also noticed for many benchmarks, smaller β led to better continuity correction parameters which also leads to better inference. Naive's performance suffers for smaller β, which we attribute to small β creating a highly multimodal observed variable distribution (also presented in Section 2) which hampers inference [37,59]. Consequently, Naive performs best when β = 0.5, however this β introduces non-trivially higher variance, which may often negatively affect the precision of inference.  Table 3 presents the results for running translated programs in Pyro. Columns 2-5 present the inference times and result errors for the original and naively smoothed program. These columns are "-" when Pyro cannot successfully perform inference (i.e. the model contains a discrete variable that is unsupported by the auto guide). Columns 6-11 present Leios' time and error for each model, for three different smoothing parameters. Fully-automated Variational Inference failed on all but one of the examples for both the Original and Naive. This is because in both cases the program still contains latent or observed discrete random variables. For most of the benchmarks (Election, GPA, TrueSkill) the program optimized with Leios had errors comparable to those computed previously with MCMC in WebPPL. For some the error was over 0.5 for all β (BetaBinomial, Fairness), which is in part a consequence of limitations of automatic VI, and hence for certain models manual fine-tuning may be unavoidable. These results illustrate that Leios can be used to create an efficient program in situations when the original language does not easily support non-continuous distributions.

Related Work
Probabilistic Program Synthesis To the best of our knowledge, we are the first to study program transformations that approximate discrete or hybrid discretecontinuous probabilistic programs with fully continuous ones to improve inference. Probabilistic program synthesis takes a more ambitious task of generating probabilistic programs with certain properties directly from data. For instance, Nori et al. [51] aim to synthesize a probabilistic program given a program sketch and a data-set to fit the program to. However, it merely fits the distribution parameters to the sketch. Furthermore their language lacks '==' comparisons. Chasins et al. [11] takes a similar approach but only apply continuous approximations to already continuous variables.

Probabilistic Inference with Discrete and Hybrid Distributions
Recent work [65,66] has explored developing languages and semantics to encode discretecontinuous mixtures, however these all restrict the types of programs that can be expressed and require specialized inference algorithms. In contrast, Leios can work with a variety of off-the-shelf inference algorithms that operate on arbitrary models and does not need to define its own inference algorithm. In [66] the authors explored a restricted programming language that can statically detect which parameters the program's density is discontinuous in. However they did not address the question of continuous approximation, rather their approach was to develop a custom inference scheme and restrict the language so that pathological models cannot be written (they also disallow '==' predicates). In [65], Wu et al. develop a custom inference method for discrete-continuous mixtures but only for models encodeable as a Bayesian network, furthermore as pointed out by [47], the specialized inference method of Wu et al. is restrictive since it cannot be composed with other program transformations.
Additionally, Machine Learning researchers have developed other continuous relaxation techniques to address the inherent problems of non-differentiable models. One other popular method is to reparametrize the gradient estimator during Variational Inference (VI) computation, commonly called the "reparameterization trick" [42,61]. However, this approach suffers from the fact that not all distributions support such gradient reparameterizations, and also this method is only limited to Variational Inference. Conversely our approach allows one to still use any inference scheme. Further, even though these techniques have been attempted in the probabilistic programming setting, [40], such work still inherits the aforementioned weaknesses.
We also draw upon Kernel Density Estimation (KDE) [62], a common approximation scheme in statistics. KDE fits a Kernel density to each observed data point, hence constructing a smooth approximation. Naive Smoothing is essentially a KDE (with a Gaussian Kernel) of the original while Leios employs additional continualizations. Furthermore, our smoothing factor β is analogous to the bandwidth of a KDE.
Program Analysis for Probabilistic Programs Multiple Program Analysis frameworks and systems have been developed for Probabilistic Programming [57,33,63,32,22]. Additionally these analyses make use of a rich set of semantics [44,36,7,64,19], however of particular note is recent work by Lew et al. [41], which provides a type system for reasoning about variational approximations; however they focus on continuous approximations of already continuous variables.

Benefits of Continuity in Conventional Programs
The idea of smoothing and working with continuous functions in non-probabilistic programs has found success in a variety of applications [21,12,34,13]. Our work derives inspiration mainly from Smooth interpretation [14], which provides a semantics for smoothing deterministic programs encoding a discontinuous or discrete function.

Conclusion
We presented Leios as a method for approximating probabilistic programs with fully continuous versions. Our approach shows that by continualizing probabilistic programs, it is possible to achieve substantial speed-ups in inference performance whilst still preserving a high degree of accuracy. To this effect we combined two key techniques: statement level program transformations to continualize latent variables and a novel continuity correction synthesis procedure to correct branch conditions.