Unbiased estimation of log normalizing constants with applications to Bayesian cross-validation
Maxime Rischard, Pierre E. Jacob, Natesh Pillai
Setting
In this article we propose a new estimator of , which combines unbiased Markov chain Monte Carlo (Jacob et al., 2017) with the path sampling identity (Gelman and Meng, 1998; see also Chapter 5 of Chen et al., 2000), also known as thermodynamic integration (Kirkwood, 1935; Neal, 2005; Calderhead and Girolami, 2009). The specificity of the proposed estimator is its unbiasedness for the logarithm of , i.e. the expectation of the proposed estimator is exactly . Existing estimators based on Markov chain Monte Carlo (Chen et al., 1997) are only asymptotically unbiased, while existing estimators based on annealed importance samplers (Neal, 2001) and sequential Monte Carlo samplers (Del Moral et al., 2006) are unbiased for and not for .
Leveraging unbiasedness for , we consider a Bayesian cross-validation (CV) criterion based on the logarithmic scoring rule (Alqallaf and Gustafson, 2001; Bornn et al., 2010; Vehtari et al., 2017, e.g). In cross-validation, one randomly splits the available data into training and validation, then the posterior distribution given the training data is numerically approximated, and finally the predictive performance on the validation data is assessed e.g. with the logarithmic scoring rule (Parry et al., 2012). We propose an estimator that is directly unbiased for these Bayesian cross-validation objectives, which can be averaged over independent copies to obtain consistent estimators and asymptotically exact confidence intervals from the central limit theorem for i. i. d. variables.
The rest of the document is structured as follows. Section 2 introduces the proposed estimators, and their tuning parameters are discussed. Numerical experiments in simple examples can be found in Section 3. Section 4 discusses our findings and future directions. The code to reproduce the experiments of the article is available at https://github.com/pierrejacob/unbiasedpathsampling.
Proposed estimators
We propose an unbiased estimator of in Section 2.1, and obtain an unbiased estimator of a Bayesian cross-validation criterion in Section 2.2. Our implementation relies on the unbiased MCMC estimators of Jacob et al., 2017, which are briefly reviewed in Section 2.3, while Section 2.4 discusses tuning choices.
The thermodynamic integration or path sampling identity relies on the following interchange between differentiation and integration (Kirkwood, 1935),
By introducing an arbitrary density , strictly positive on , we obtain the path sampling identity:
This is useful if we can approximate integrals with respect to by Monte Carlo or numerical integration, and if we can approximate the inside expectation by Markov chain Monte Carlo (MCMC, Robert and Casella, 2004), for instance.
Here we denote by the inner expectation in (3), and we introduce , an unbiased estimator of that we can generate for any ; we defer the construction of such estimators to Section 2.3. We can then define an estimator of with the following procedure.
Draw , a distribution supported on $$.
Given , generate a variable with expectation .
Return .
The random variable has expectation by the law of iterated expectations, and we refer to it as an unbiased path sampling estimator (UPS). Note that sequential Monte Carlo samplers and related methods (Del Moral et al., 2006) would provide unbiased estimators of and not of . Thus these estimators will not be unbiased for . We will now see that the lack of bias on the logarithmic scale can be exploited to propose new estimators of Bayesian cross-validation criteria.
2 Unbiased Bayesian cross-validation
A number of articles discuss the computational difficulties associated with Bayesian cross-validation, e.g. Alqallaf and Gustafson, 2001; Bhattacharya and Haslett, 2007; Bornn et al., 2010; Lamnisos et al., 2012; McVinish et al., 2013; Vehtari et al., 2017. We first define the object of interest, before presenting our estimator. Let denote an unknown parameter with prior density , and let denote the data composed of units. The likelihood function is denoted by . Cross-validation consists in randomly splitting into and , where stands for training and for validation. The sets form a partition of , and . Denote by and the numbers of elements in and ; for instance, if , the procedure is termed “leave-one-out” cross-validation. Given a split of the data , we introduce a measure of accuracy in predicting using the training data . A typical choice is the logarithmic score (see Parry et al., 2012, for a discussion on the choice of scoring rule) where is the posterior predictive density given and evaluated on . Note that simplifies to if the data are modeled as conditionally independent given . The cross-validation objective, “CV” below, is defined as an average over all splits of size ,
Given a split , we can estimate using the path sampling identity and the unbiased estimators of the previous section. Indeed, that quantity is a log-ratio of the normalizing constants and . By introducing the path
This motivates the following strategy: sample a split uniformly from , and then obtain an unbiased estimator of given . The resulting estimator is directly unbiased for CV in (4), by the law of iterated expectations. We summarize the procedure below.
Sample index sets uniformly at random over , the set of partitions of into a set of size and a set of size .
Note how the lack of bias on the logarithmic scale is important for the above procedure to produce an unbiased estimator of CV. We could also extend the above procedure to allow for non-uniform sampling of the partitions from .
3 Reminders on unbiased MCMC
4 Tuning choices
A number of choices have to be made for the proposed estimators to be operational. The first choice is that of a path of distributions. There are generic choices such as the geometric path, and choices motivated by algorithmic considerations on a case-by-case basis. We will discuss the choice of paths through examples, in Section 3.
Given a path of distributions , algorithms approximating expectations with respect to typically involve tuning parameters. We describe the tuning of unbiased MCMC in Section 2.4.1. Then we discuss choices of distribution in Section 2.4.2.
The unbiased MCMC estimators described in Section Section 2.3 require the specification of a Markov kernel , a coupled kernel , and an initial distribution for the chains. Specifying these objects is typically difficult, but not specific to the setting of normalizing constant estimation. Therefore we defer to the large literature on MCMC algorithms (Robert and Casella, 2004; Brooks et al., 2011), as well as the relevant discussions in Jacob et al., 2017 in the context of unbiased MCMC. Ultimately we will care about the expected cost and the variance of the proposed unbiased estimators, in order to maximize the efficiency of the proposed estimators, as discussed in the next section.
4.2 Tuning of the distribution q(dλ)q(\mathop{d\lambda})
If is constant over , then the solution of the above minimization problem is given by . In Gelman and Meng, 1998 that solution is given, and then the verification that this is indeed a solution is done via Cauchy-Schwarz. Here we provide an informal derivation of the solution, in the case where is constant over . We write the function to minimize as , and introduce the Lagrangian
We would like to differentiate with respect to and set the derivative to zero. Introduce the directional derivative where is a function. Replacing by and differentiating with respect to in the Lagrangian yields
Setting to zero yields , and trying to set that expression to zero simultaneously for every choice of , we obtain , i.e. . This gives the candidate solution.
4.3 Proposed tuning procedure
We now combine the above sections into practical guidelines for the proposed estimators.
For in a grid of values , construct and tune an unbiased MCMC (initial distribution, Markov kernel , and coupled kernel ) targeting , and draw independent samples of the associated meeting times .
Use these estimates to define a distribution , such that is approximately proportional to for all in $$.
We describe a concrete way of performing step 5, for completeness. Given a grid of values and associated estimates of for obtained in step 4, we can define a distribution that is piecewise uniform on the intervals , and such that
Sampling from such a distribution can be done in order operations, by first selecting an interval with probability , and then sampling uniformly from that interval.
After the preliminary phase described in the five steps above, the generation of estimators can proceed as follows. First, is drawn from obtained in step 5 above. We then find the nearest value in the grid, with index . We can look up tuning parameters corresponding to for the unbiased MCMC estimators, stored during step 2 above, and the values of and stored during step 3 above. Using these tuning values we can generate an unbiased estimator of . The estimator is finally returned.
Numerical experiments
The numerical experiments are structured as follows. Section 3.1 contains toy examples of unbiased path sampling estimators. Section 3.2 considers logistic regressions with different choices of paths and of unbiased MCMC estimators, and an example taken from Epifani et al., 2008; Vehtari et al., 2017. Section 3.3 considers linear regressions with examples taken from Alqallaf and Gustafson, 2001; Peruggia, 1997; Vehtari et al., 2017. Throughout the experiments, 95% confidence intervals for an estimand are obtained as , where is the mean of independent unbiased estimators of and is their sample standard deviation. These confidence intervals are justified asymptotically as by the central limit theorem for i. i. d. random variables, provided that the variance of the unbiased estimators is finite. On parallel machines and under budget constraints, valid confidence intervals can be constructed following Glynn and Heidelberger, 1991; see also related remarks in Jacob et al., 2017.
We start with a grid of values of : for , with . For each , we run coupled MH chains until they meet, 100 times independently. We obtain a distribution of meeting times for each , represented on 1(a). The overlaid full line represents the quantiles, which we denote by . We also compute the average meeting times for each , which we denote . We then define
Given values of and , for each in the grid of values defined above, we approximate the first and second moments of with independent estimators. We use these moments to redefine the initial distribution of the Markov chains, which we set to a Normal distribution adapted to , and to tune the proposal standard deviation, which we set to be the estimated standard deviation of . At this point we could sample meeting times again and choose new values for and , but we omit this here. Next, we estimate for each in the grid, and define accordingly, following step 5 in Section 2.4. The estimates of are shown in 1(b). This completes the tuning phase, and we can now generate unbiased estimators of . We show these estimates against in 1(c). These are generated times independently. Concretely, they yield the confidence interval for the estimand at level .
1.2 Double-well example
We perform similar experiments on a path of two-dimensional distributions linking the potential , corresponding to a Normal distribution centered at and with diagonal variances , to the potential . The latter is a double-well potential, with modes around and . By numerical integration we find to be approximately . We introduce the geometric path . For each , we start chains from a Normal centered at and with covariance matrix , the identity matrix of size . We consider random walk MH schemes with Normal proposal, with covariance ; the coupled version relies on maximal couplings of the proposals, as in the previous section.
We draw meeting times independently, for with and . The distributions are shown in violin plots in 2(a). We observe much larger meeting times for close to one, which corresponds to the MH chains struggling to explore both modes of the double-well potential. We thus conservatively set to be twice the quantiles of the meeting times, instead of the quantiles themselves.
We follow the same heuristics as in Section 3.1.1 for the choice of . Without modifying the initial distribution nor the proposal distribution of the MH chains, we estimate for each in the grid, based on 100 independent copies, and define following again step 5 in Section 2.4. The estimates of are shown in 2(b). Finally we generate unbiased estimators of , and represent these estimates against in 2(c). These are generated times independently and result in the confidence interval for the estimand .
2 Logistic regression
With basic manipulations this is equivalent to the following simpler form
The Pólya-Gamma Gibbs (PGG) sampler (Polson et al., 2013; Choi and Hobert, 2013) is a Gibbs sampler that targets through the introduction of auxiliary variables . First, we recall that the Pólya-Gamma distribution with parameters , denoted by , has a density defined for all , as
Introduce auxiliary variables , independent of each other given , such that follows PG for all . An extended target distribution is defined as , where denotes a realization of , and . The appeal of this extension is that we can write the target as
and therefore the conditional of given simplifies to
given , draw ,
draw , independently for all .
In the experiments below, we initialize the chains from the prior distribution .
We first remark that the above reasoning holds when replacing the covariates by for any . This corresponds to the likelihood , for . In the case , the likelihood is equal to for all , while with , we retrieve the original likelihood. For all , we can introduce Pólya-Gamma variables following PG for all , and obtain a corresponding PGG sampler.
This enables normalizing constant estimators for the logistic regression model with little tuning, since the PGG sampler itself has no tuning parameters. Here, for all , we define
We consider a synthetic data set with rows and columns. The covariates are generated from a standard Normal distribution and the outcome is generated from the model with . The prior mean is set to zero and the covariance to a diagonal matrix with entries equal to . We start by gridding the interval $\lambda^{[l]}=l/LL=10k99\%m\lambda\hat{E}(\lambda)\lambdaq(\mathop{d\lambda})$ following step 5 in Section 2.4, and obtain the estimates of 3(a).
We obtain the independent estimators shown in 3(b), leading to a confidence interval of $r_{01}=\log(Z_{1}/Z_{0})70\lambda\lambdaq(\mathop{d\lambda})$.
Therefore we consider a grid of values of equispaced on the logarithmic scale: for with . Going through the exact same tuning steps, we obtain the estimators of 3(c), leading to the narrower confidence interval at level (with a width of 10 instead of 36 for the previous one). This illustrates the potential gains obtained by carefully choosing the distribution .
We conclude this section by noting that more dramatic gains can be obtained by changing the path of distributions. In the context of logistic regression with , the Laplace approximation of the posterior, defined as where is the maximum likelihood estimator and is the inverse of minus the Hessian of the log-likelihood evaluated at , seems to be very accurate. We thus introduce a geometric path between the Laplace approximation and the posterior distribution. We use a random walk MH algorithm to target for all , with proposal covariance matrix equal to where the dimension is equal to . To couple the MH algorithms, we use strategy that combines reflection and maximal couplings, as described in Jacob et al., 2017. The initial distribution of the chains is chosen to be the Laplace approximation. For , we obtain as the quantile of the meeting times, and we set . We use these values of and for all , and we choose to be uniform on $100[70.24,70.26]95\%\log Z_{1}+n\log 2$. This is orders of magnitude narrower than the previous intervals, for a smaller computational cost. The choice of paths can thus play a critical role in the efficiency of the proposed estimators, and approximations of the posterior distribution can be used to construct such paths.
2.2 Cross-validation
We now consider the approximation of CV in (4). We consider a leave-one-out criterion, with and . We thus construct paths between the posterior given the training data , with normalizing constant , and the posterior given all the data , with normalizing constant .
Our first path follows the reasoning of the previous section: we can multiply the covariates in the validation set by to preserve the original structure of the likelihood and thus to enable a similar PGG sampler. The unnormalized densities are then
Again we see that this is essentially a linear function of and thus its moments under are finite for all .
To tune the procedure, we obtain meeting times for the coupled PGG sampler based on the full data set, and choose as a quantile (here equal to ), and . Recall that the PGG sampler itself has no tuning parameters. Then, drawing a validation set at random time independently, generating uniformly on $\hat{E}(\lambda)95\%[-0.62,-0.56]$.
Alternatively, we introduce a geometric path between the posterior given and given , which corresponds to the unnormalized densities
For this path, we use random walk MH as in the previous section, with initial distribution and proposal covariance tuned using a Laplace approximation of the posterior distribution. We obtain a quantile of meetings at and set . Over independent experiments we obtain unbiased estimators of the CV objective shown in 4(b). The associated confidence interval for CV is . Thus, this second approach appears to be marginally more efficient than the first one; the cost comparison is made slightly difficult by the fact that PGG and MH have different costs per iteration.
2.3 Leukemia survival data
We follow Vehtari et al., 2017 and consider the leukemia data presented in Feigl and Zelen, 1965 and used as illustration in Epifani et al., 2008. We use the data formatted as in the package BGPhazard, see Garcıa-Bueno and Nieto-Barajas, 2016. The outcome is taken to be one if the survival time (column time of leukemiaFZ) is larger or equal to weeks, zero otherwise, and the two covariates are the columns wbc and AG, corresponding to counts of white blood cells and the outcome of a test related to white blood cell characteristics. There are 31 patients in the sample, so , and we consider leave-one-out cross-validation, i.e. and .
We introduce a path of distributions amenable to PGG sampling, as in the previous sections. Sampling uniformly the index of the observation to be left out, then sampling uniformly in $\pi_{\lambda}$, we record the meeting times. We do so 1,000 times independently, and show the results as a function of the index of the observation left out in 5(a).
Based on this plot we select , conservatively, and for all runs. We then generate unbiased estimators of CV. We plot the estimators against the index of the left-out observation in 5(b), and we note that the values are very different for one particular index, here equal to 17. In 5(c) we plot a histogram of the estimates of the CV objective, putting all the indices together. From these estimates we obtain a confidence interval for the leave-one-out CV objective. Thus we see that the proposed estimators can have a larger variance for certain splits of the data compared to others. Investigating further the behavior of the estimators for certain splits, one might be able to reduce the variance, for instance by tuning the proposal distribution , or by changing the path. Our estimators of CV might also be considered satisfactory as they stand. In any case, they do not suffer from infinite variance issues typically associated with importance sampling, when using a proposal distribution that has lighter tails than the target distribution.
3 Linear regressions
We next consider linear regressions, which have been used to illustrate Bayesian cross-validation e.g. in Alqallaf and Gustafson, 2001; Peruggia, 1997; Vehtari et al., 2017.
The first example is taken from Alqallaf and Gustafson, 2001. The data comprise of observations, each corresponding to an animal (arctic fox, owl monkey, etc). For each animal, the data set contains the body weight and the brain weight. The covariate of animal is a vector, with first entry equal to and second entry equal to the logarithm of body weight, while the outcome is the logarithm of brain weight. As before we write for the vector of outcomes and for the matrix of covariates, on which we condition throughout. The model is given by
To get the conditional distribution of given under the posterior distribution, note that
where . Thus, the conditional distribution is Normal with mean and covariance matrix . The distribution of given is inverse Gamma, where recall that
Then given is inverse Gamma with and . Coupling this algorithm can be done by maximal coupling of each of the conditional update of a Gibbs sampler.
In Alqallaf and Gustafson, 2001, the predictive performance in this example is measured by the mean squared error, defined conditional on a split as
We implement this procedure and draw independent coupled chains. We observe meeting times between and . Thus we set , , and draw independent unbiased estimators of CV. We obtain a confidence interval of , and standard error of . By comparison, Alqallaf and Gustafson, 2001 use 200 splits, and run 125 iterations of MCMC for each split, discarding the first 100. The total number of Gibbs iterations performed is approximately the same, and Alqallaf and Gustafson, 2001 obtain standard errors that are similar. An advantage of our method is in its simplicity: if we want more precise results, we simply generate more independent estimators.
We now consider the criterion , instead of the point-prediction mean squared error as above. The sequence of distributions defined in (5) is still amenable to a Gibbs sampling strategy and we need to work out the conditional distributions. The joint posterior density is
so that given the rest is . On the other hand given the rest is inverse Gamma with
3.2 Stack loss data
We consider the stack loss data example, which was considered in Peruggia, 1997; Vehtari et al., 2017. In the former article, it is shown that importance sampling from the posterior given all the data to the posterior leaving one data point out can lead to infinite variance estimators. Here we use the stackloss data set of (R Core Team, 2015), with the outcome set to be the column stack.loss, and the covariates Air.Flow, Water.Temp, Acid.Conc., and a column of ones. The data are shown in 6(a). We consider leave-one-out cross-validation, with here. For simplicity we use the same model as in the previous section, with a flat prior on given , instead of the proper prior given in Peruggia, 1997.
Using the coupled Gibbs sampler described in the previous section, we find meeting times to be less than with large probability, thus we set and . We obtain the CV estimators shown in 6(b), based on independent replicates, plotted against the index of the left-out observation. As in Section 3.2.3, we can see that the variance of the CV estimators varies across the different ways of partitioning the data into training and validation sets. These CV estimators yield the confidence interval .
Discussion
Further work will be needed to compare the proposed estimators with state-of-the-art methods such as sequential Monte Carlo samplers for normalizing constant estimation (Lee and Whiteley, 2015; Zhou et al., 2016; Andrieu et al., 2016, e.g.), with alternative approaches such as the ones described in Chen et al., 1997; Johnson, 1999; Neal, 2005; Salomone et al., 2018 and references therein, and with the different existing approaches for Bayesian cross-validation (Alqallaf and Gustafson, 2001; Bornn et al., 2010; Vehtari et al., 2017, e.g.).
Our estimators combine the path sampling identity with unbiased estimators of intractable integrals. As such, they are expected to break if either path sampling or the unbiased estimators break. Path sampling can give poor results if the path of distributions is ill-chosen, thus the design of these paths remains crucial. We have seen in Section 3.2 that different paths can give orders of magnitude differences in efficiencies. We have also seen that the paths can benefit from approximations of the posterior distribution, such as Laplace approximations. Mixtures of distributions fitted on MCMC samples or variational approximations could also be considered. Conditional on a path, the choice of distribution is also important and can be guided by preliminary runs. Unbiased MCMC estimators themselves break either if the underlying MCMC algorithms mix poorly, or if the coupling strategy is ineffective; we defer to Jacob et al., 2017 for related discussions, and to Heng and Jacob, 2018 for the case of Hamiltonian Monte Carlo algorithms.
We note that the path sampling identity (3) is an instance of a nested Monte Carlo (MC) problem, as defined and discussed in Rainforth et al., 2016. The target of nested MC is an expectation of the form
where the functions , and the joint distribution of are problem-dependent choices. In the case of path sampling, we obtain by choosing:
In this case is linear in its second argument, thus, given , unbiased estimators of directly translate into unbiased estimators of . We remark that unbiased estimators could also be obtained for functions that are nonlinear in the second argument. For instance we can get an unbiased estimator of , by sampling independent estimators of and taking their product. More generally we can obtain unbiased estimators of given for functions that are polynomials in the second argument.
Finally it is possible to adapt the proposed approach to estimate the Bayesian cross-validation objective associated with some other scoring rules, such as the one proposed in Hyvärinen, 2005, and considered in the setting of model comparison in e.g. Dawid and Musio, 2015; Shao et al., 2018.
The authors are grateful to Jeremy Heng and Stephane Shao for helpful discussions.