## Sunday, 3 June 2018

### Easy (Bayesian) multidimensional scaling with Stan

Multidimensional scaling (MDS) is a data visualization technique in which the dimension of the data is reduced in a non-linear way. The data is represented as a $$N\times N$$ distance matrix $$(d_{ij})_{ij}$$, and $$N$$ points $$x_i$$ in a $$D$$ dimensional space (typically $$D=2$$) are chosen such that the Euclidean distances $$\|x_i - x_j\|$$ resemble the input distances $$d_{ij}$$ "as good as possible".

In metric MDS, an objective function $$E(x) = \sum_{1\leq i < j \leq N} (d_{ij} - \|x_i - x_j\|)^2$$ is defined that needs to be minimized. For different flavors of MDS, this objective function is defined differently. In order to minimize the objective function, e.g. the conjugate gradient descent method is used. This method requires that one calculates the gradient $$\nabla E$$ of the objective function. Of course, this is not so difficult is the case of metric MDS, but more difficult objective functions might require more effort. Enter Stan.

Stan uses automatic differentiation for Hamiltonian Monte Carlo, but Stan can also be used for maximum likelihood. Hence, if we can formulate the MDS problem in terms of a likelihood function, we can let Stan do all the work. The parameters of the model are the $$x_i$$, the data is given by the distances $$d_{ij}$$. If we assume that given the parameters, the data is distributed as $d_{ij} \sim \mathcal{N}(\|x_i - x_j\|, \sigma^2)\,,$ then maximizing the (log) likelihood is equivalent to minimizing the function $$E$$. The parameter $$\sigma^2$$ is merely a nuisance parameter that needs to be estimated as well.

### An implementation of MDS in the Stan programming language

Implementing MDS in Stan is fairly straightforward, but there are a few snags that we should be aware of. First, if $$x$$ solves the MDS problem, then also any Euclidean transformation of $$x$$ is a solution. Hence, the model as stated above has too many parameters. We solve this by fixing the first point at the origin, restricting the next point to a $$1$$-dimensional half space, the third point to a $$2$$-dimensional half space et cetera. The last $$N-D-1$$ points are unrestricted. In Stan, we can accomplish this by using a  cholesky_factor_cov  matrix: A positive-definite lower-trangular matrix. We then use the  transformed parameters  block to concatenate the points together into a single matrix.

Secondly, the data that we use in the example below is highly censored. Many of the distances are missing, and some are right censored. In such a case MDS can be used to infer the missing distances, and not merely visualize the data. The data that is passed to Stan, therefore, is a list of edges, a list of distances, and a list of codes that determine the type of censoring.

Thirdly, as the title of this post suggests, we will use Stan to do some sort of Bayesian MDS. In this case, we will sample a collection of "maps" $$x$$ from a posterior distribution, that gives information about the location of each point, but also the uncertainty of this location. In this case, the fact that we restrict the first $$D+1$$ points, comes back to bite us, as the uncertainty of these points will be different than the unrestricted points. Furthermore, it might be hard to compare the individual maps to one another, and for instance compute sensible mean locations of the points, as some maps may be "twisted" more than others. Therefore, we use the  generated quantities  block to center and rotate (cf. PCA) the sampled maps.

### Example: Antigenic cartography of the influenza virus

An interesting application of MDS is antigenic cartography of the influenza virus. Influenza virus circumvents human antibody responses by continually evolving its surface proteins, in particular, hemagglutinin (HA). This is known as antigenic drift. In order to decide whether flu vaccines need to be updated, the hemagglutination inhibition (HI) assay is used to determine if the induced antibody response is still effective against future strains. The titers measured in the HI assay can be used to define "distances" between antisera and antigens. Using MDS, the antisera and antigens can be drawn into a "map", that shows the antigenic drift of the virus. This was done by Smith et al. in 2004. Conveniently, the data used for their map is available online. This table gives HI titers $$H_{ij}$$ of antigen $$i$$ and antiserum $$j$$. A small titer corresponds to a large distance, which are defined as $$d_{ij} = \log_2(H_{\max,j}) - \log_2(H_{ij})$$, where $$H_{\max,j} = \max_{k} H_{kj}$$. As an example, I recreated their antigenic map using the Stan model above, and a Python script below.

The white squares denote the relative positions of the antisera in the "antigenic space", while the colored circles represent the antigens. The colors map to the years in which the infuenza strains were isolated.

### Bayesian multidimensional scaling

For antigenic cartography of IAV, Bayesian MDS has been introduced by Bedford et al., who used multiple HI assay results per antigen/antiserum pair to incorporate the uncertainty of these measurements in their antigenic map. Moreover, they were able to use genetic and temporal information about the antigens (i.e. the RNA sequences of HA and their isolation dates) to inform the position of the antigens and antisera on the map. We will not go this far in this post, but since we have already formulated the MDS algorithm in Stan, we might as well make a "Bayesian" antigenic map. This can give some insight into the uncertainty of the positions of the antigens and antisera. This is not unlike the confidence areas as drawn by Smith et al. (the ellipsoid shapes). The result is given by the following figure.

Again, squares indicate antisera and colored circles the antigens. All the individual MCMC samples are represented by the grey dots. The MCMC samples for each antigen or antiserum are used to draw a two-dimensional error bar (i.e. ellipse) around the mean location.
A Python script for parsing the HI titer data, compiling and running the Stan model and drawing the maps is added below. For it to work, you will need to download the  mds_model.stan  file and make a csv file called  baselinemap.csv  with the HI table

## Tuesday, 6 February 2018

### Computing q-values with C++

When looking for associations between features $$i = 1,\dots, m$$ and some trait, it is often necessary to have some sort of multiple-testing correction. A very conservative method is the Bonferroni correction, that minimizes the family-wise error rate (FWER), but at the cost of many false negatives. This is not desirable when one wants to discover features or associations, and therefore other methods have been developed. One particularly intuitive method is based on the false discovery rate (FDR) and uses so-called $$q$$-values, which are (under certain conditions) elegantly analogous to $$p$$-values.

## False discovery rate

Let $$S$$ be the number of features called significant, and $$F$$ the number of false positives among the significant features (i.e. false discoveries). In an article by John D. Storey, The (positive) false discovery rate is defined as ${\rm pFDR} := \mathbb{E}\left[\left.\frac{F}{S}\right| S > 0 \right]\,.$ Hence, the pFDR is the expected fraction of false positives among the features that are called significant. The condition $$S > 0$$ ensures that it is well defined.

In the case of hypothesis testing, one typically has a test statistic $$T$$, and one wants to test if the null hypothesis is true ($$H = 0$$), or rather that the alternative hypothesis is true ($$H = 1$$). The statistical model specifies the distribution of $$T | H = 0$$, and the null hypothesis is rejected when the realization of $$T$$ falls into a pre-defined significance region $$\Gamma$$.
When testing multiple features, we typically have a sequence $$(T_i, H_i)_{i=1}^m$$, here assumed to be identically distributed and independent. The $${\rm pFDR}$$ then depends on $$\Gamma$$: ${\rm pFDR}(\Gamma) = \mathbb{E}\left[\left.\frac{F(\Gamma)}{S(\Gamma)}\right| S(\Gamma) > 0 \right] \,,$ where $$F(\Gamma) := \#\{i : T_i \in \Gamma \wedge H_i = 0 \}$$ and $$S(\Gamma) := \#\{i : T_i \in \Gamma\}$$. Storey derives that under certain conditions, we can write ${\rm pFDR}(\Gamma) = \mathbb{P}[H = 0 | T \in \Gamma] = \frac{\mathbb{E}[F(\Gamma)]}{\mathbb{E}[S(\Gamma)]}$

## The q-value

Let $$\{\Gamma_{\alpha}\}_{\alpha=0}^1$$ be a nested family of significance regions. That is $$\Gamma_{\alpha} \subseteq \Gamma_{\alpha'}$$ whenever $$\alpha \leq \alpha'$$ and $$\mathbb{P}[T \in \Gamma_{\alpha} | H=0] = \alpha$$. For instance, if $$T | H = 0 \sim \mathcal{N}(0,1)$$, then we could choose $$\Gamma_{\alpha} = [z_{\alpha}, \infty)$$, where $$z_{\alpha} = \Phi^{-1}(\alpha)$$, with $$\Phi$$ the CDF if $$\mathcal{N}(0,1)$$.
The $$q$$-value of a realization $$t$$ of $$T$$ is then defined as $q(t) = \inf_{\Gamma_{\alpha} | t \in \Gamma_{\alpha}} {\rm pFDR}(\Gamma_{\alpha})$ We can now give the above-mentioned analogy between $$p$$-values and $$q$$-values. The $$p$$-value is defined as: $p(t) = \inf_{\{\Gamma_{\alpha} : t \in \Gamma_{\alpha}\}} \mathbb{P}[T \in \Gamma_{\alpha} | H = 0]$ While under the right conditions, the $$q$$-value can be written as: $q(t) = \inf_{\{\Gamma_{\alpha} : t \in \Gamma_{\alpha}\}} \mathbb{P}[H = 0 | T \in \Gamma_{\alpha} ]$

## Computing q-values

In order to compute $$q$$-values, given a sequence of $$p$$-values, we follow the steps given in a paper by Storey and Tibshirani In this scenario, the $$p$$-value plays the role of the realization $$t$$ of the statistic $$T$$. Under the null hypothesis, these $$p$$-values are uniformly distributed. As a family of significance regions, we simply take $$\Gamma_{\alpha} = [0,\alpha]$$, and write for instance $$S(\alpha) := S(\Gamma_{\alpha})$$.

First, we have to estimate $$\mathbb{E}[S(\alpha)]$$, for which we use $$\#\{i : p_i \leq \alpha\}$$, and we estimate $$\mathbb{E}[F(\alpha)]$$ with $$m \cdot \hat{\pi}_0 \cdot \alpha$$, where $$\hat{\pi}_0$$ is an estimate for $$\pi_0 = \mathbb{P}[H=0]$$.
The most difficult part of computing $$q$$-values is estimating $$\pi_0$$. An often used estimate is the average $$p$$-value, but we can do a little bit better by making use of the empirical CDF of the $$p$$-values. The figure below shows a "typical" histogram of $$p$$-values, where the $$p$$-values sampled from the alternative distribution are given by the gray bars. The marginal distribution of $$p$$-values becomes relatively flat towards the right, where most $$p$$-values should come from the null distribution, which is $${\rm Uniform}(0,1)$$. The right panel of the figure shows a "rotated" empirical CDF of the $$p$$-values, i.e. $x \mapsto R(x) = 1-{\rm CDF}(1-x)\,,$ and the fact that large $$p$$-values should be uniformly distributed is resembled by the fact that $$R(x)$$ is a straight line for small $$x$$. The slope of this straight line is an estimate of $$\pi_0$$. In the C++ code below, I use GSL to fit a line though $$R$$ (indicated by the red line in the figure), using a weight function $$x \mapsto (1-x)^2$$ to give more precedence to small values of $$x$$, thereby mostly ignoring the non-straight part of $$R$$.

In this example, the marginal $$p$$-values are sampled from a mixture distribution $$\pi_0 {\rm Uniform}(0,1) + (1-\pi_0) {\rm Beta}(10,1)$$, where $$\pi_0 = 3/4$$.

Now we sort the $$p$$-values such that $$p_1 \leq p_2 \leq \dots \leq p_m$$, such that $$\#\{j : p_j \leq p_i\} = i$$ and first determine the $$q$$-value corresponding to $$p_m$$: $q_m = \hat{\pi}_0 \cdot p_m$ The $$q$$-value $$q_i$$ corresponding to $$p$$-value $$p_i$$ is computed as $q_i = \min(\hat{\pi}_0 p_i m/i, q_{i+1})$ Recently, I had to implement this algorithm in C++. The function is given in the following header file, and accepts a "keyed" list of $$p$$-values, where the key is used to identify the feature. The function returns a keyed list of $$q$$-values.

The following code gives an example of how to use qvalues.hpp, and can be used for testing:

After compiling and running the program, the result should be something like:
$g++ -O3 -std=c++11 qvalues_test.cpp -lgsl -lgslcblas -o qvaltest$ ./qvaltest 1728
true false
discoveries:   143    32
negatives:   712   113
realized FDR: 0.182857


## Tuesday, 18 April 2017

### How to calculate WBIC with Stan

In a previous post, I showed how to use Stan to calculate the Bayes marginal likelihood with the path sampling method. For large models, this method can be computationally expensive, and therefore estimates for the marginal likelihood have been developed. The most famous one is the Bayesian Information Criterion (BIC), an estimate for the Bayes free energy. Although the name suggests otherwise, the BIC is not a "true Bayesian" estimate, since it depends only on a point estimate, and not the entire posterior distribution: ${\sf BIC} = -2 L(\hat{\theta}) + k \cdot \log(n)\,,$ where $$L$$ is the log-likelihood function, $$\hat{\theta}$$ is the maximum likelihood estimate of the models parameters, $$k$$ is the dimension of $$\theta$$ and $$n$$ is the number of data points.
More importantly, the BIC does not work for singular models. As Watanabe points out, if a statistical model contains hierarchical layers or hidden variables then it is singular. Watanabe, therefore, proposes a generalization of the BIC, the WBIC, that works for singular models as well, and is truly Bayesian. This is not unlike AIC versus WAIC.

Suppose that you have some sort of MCMC algorithm to sample from the posterior distribution of your model, given your data. When you want to compute the WAIC, you will just have to store the likelihoods of each observation, given each sampled set of parameters. This can be done during an actual run of the MCMC algorithm. For the WBIC, however, you will need to do a separate run at a different "temperature". In fact, Watanabe gives the following formula: ${\sf WBIC} = -2\mathbb{E}_{\theta}^{\beta}[L(\theta)]\,,$ where $$\beta = 1/\log(n)$$, and the notation $$\mathbb{E}_{\theta}^{\beta}[f(\theta)]$$ expresses that the expectation of $$f$$ is taken with respect to the distribution with PDF $\theta \mapsto \frac{\exp(\beta L(\theta)) \pi(\theta)}{\int\exp(\beta L(\theta)) \pi(\theta)d\theta}$ which equals the posterior distribution when $$\beta = 1$$ and the prior distribution $$\pi$$ when $$\beta = 0$$.

Something very similar happens in the path sampling example, with the exception that the "path" is replaced by a single point. In a recent paper about another alternative to the BIC, the Singular BIC (sBIC), Drton and Plummer point out the similarity to the mean value theorem for integrals. Using the path sampling method, the log-marginal-likelihood is computed from the following integral $\log(P(D|M)) = \int_0^1 \mathbb{E}_{\theta}^{\beta}[L(\theta)]\, d\beta\,.$ Here, we write $$P(D|M)$$ for the marginal likelihood of model $$M$$, and data $$D$$. According to the mean value theorem $\exists \beta^{\ast} \in (0,1): \log(P(D|M)) = \mathbb{E}_{\theta}^{\beta^{\ast}}[L(\theta)]\,,$ and Watanabe argues that choosing $$1/\log(n)$$ for $$\beta^{\ast}$$ gives a good approximation. Notice that the mean value theorem does not provide a recipe to compute $$\beta^{\ast}$$, and that Watanabe uses rather advanced algebraic geometry to prove that his choice for $$\beta^{\ast}$$ is good; the mean value theorem only gives a minimal justification for Watanabe's result.

## Implementing WBIC in Stan

Let us start with a very simple example: the biased coin model. Suppose that we have $$N$$ coin flips, with outcomes $$x \in \{0,1\}^N$$ (heads = 1 and tails = 0), and let $$K$$ denote the number of heads. We compare the null model (the coin is fair) with the alternative model (the coin is biased) using $$\Delta {\sf WBIC}$$ and see if we get the "right" answer. Notice that for this simple example, we can compute the Bayes factor exactly.

In order to have a Stan model that can be used for both regular sampling, and estimating WBIC, a sampling mode is passed with the data to determine the desired behavior. In the transformed data block, a parameter watanabe_beta is then defined, determining the sampling "temperature".
The actual model is defined in the transformed parameters block, such that log_likes can be used in both the model block as the generated quantities block. In stead of a sampling statement (x[n] ~ bernoulli(p)), we have to use the bernoulli_lpmf function, which allows us to scale the log-likelihood with watanabe_beta (i.e. target += watanabe_beta * sum(log_likes)).
// coin_model.stan

data {
int<lower=3> N; // number of coin tosses
int<lower=0, upper=1> x[N]; // outcomes (heads = 1, tails = 0)
int<lower=0, upper=1> mode; // 0 = normal sampling, 1 = WBIC sampling
}
transformed data {
real<lower=0, upper=1> watanabe_beta; // WBIC parameter
if ( mode == 0 ) {
watanabe_beta = 1.0;
}
else { // mode == 1
watanabe_beta = 1.0/log(N);
}
}
parameters {
real<lower=0, upper=1> p; // probability of heads
}
transformed parameters {
vector[N] log_likes;
for ( n in 1:N ) {
log_likes[n] = bernoulli_lpmf(x[n] | p);
}
}
model {
p ~ beta(1, 1);
target += watanabe_beta * sum(log_likes);
}
generated quantities {
real log_like;
log_like = sum(log_likes);
}

Using the pystan module for Python (3), one could estimate $$p$$ from data $$x$$ as follows:
import pystan
import numpy as np
import scipy.stats as sts

## compile the model
sm = pystan.StanModel(file="coin_model.stan")

## create random data
N = 50
p_real = 0.75
x = sts.bernoulli.rvs(p_real, size=N)

## prepare data for Stan
data = {'N' : N, 'x' : x, 'mode' : 0}
pars = ['p', 'log_like']

## run the Stan model
fit = sm.sampling(data=data)
chain = fit.extract(permuted=True, pars=pars)
ps = chain['p']
print("p =", np.mean(ps), "+/-", np.std(ps))

Which should give output similar to
p = 0.67187824104 +/- 0.0650602081466

In order to calculate the WBIC, the mode has to be set to 1:
## import modules, compile the Stan model, and create data as before...

## prepare data for Stan
data = {'N' : N, 'x' : x, 'mode' : 1} ## notice the mode
pars = ['p', 'log_like']

## run the Stan model
fit = sm.sampling(data=data)
chain = fit.extract(permuted=True, pars=pars)
WBIC = -2*np.mean(chain["log_like"])

print("WBIC =", WBIC)

which should result in something similar to
WBIC = 66.036854275

In order to test if this result is any good, we first compute the WBIC of the null model. This is easy since the null model does not have any parameters ${\sf WBIC} = -2\cdot L(x) = -2 \cdot \log(\tfrac12^{K}(1-\tfrac12)^{N-K}) = 2N\cdot\log(2)$ and hence, when $$N=50$$, we get $${\sf WBIC} \approx 69.3$$. Hence, we find positive evidence against the null model, since $$\Delta{\sf WBIC} \approx 3.3$$.

For the coin-toss model, we know the posterior density explicitly, namely $P(x|M) = \int_0^1 p^K (1-p)^{N-K} dp = B(K+1, N-K+1)\,,$ where $$M$$ denotes the alternative (i.e. biased) model, and $$B$$ denotes the Beta function. In the above example, we had $$N = 50$$ and $$K = 34$$, and hence $$-2\cdot\log(P(x|M)) \approx 66.31$$, which is remarkably close to the $${\sf WBIC}$$ of the alternative model. Similar results hold for several values of $$p$$, as demonstrated by the following figure

## A non-trivial example

As the example above is non-singular, and WBIC is supposed to work also for singular models, I plan to present an example with a singular model here.

## Tuesday, 22 November 2016

### Using the "ones trick" to handle unbalanced missing data with JAGS

The so-called "ones trick" for JAGS and BUGS models allows the user to sample from distributions that are not in the standard list. Here I show another application for "unbalanced" missing data. More examples using the ones (or zeros) trick can be found in The BUGS Book, and in particular in Chapter 9.

Suppose that we want to find an association between a trait $$x$$ (possibly continuous) and another categorical trait $$k \in \{1, 2,\dots, K\}$$. We have $$N$$ observations $$x_i$$, however, some of the $$k_i$$ are missing. Whenever there is no information on the missing $$k_i$$ whatsoever, we can simply write the following JAGS model:
data {
/* alpha is the parameter for the Dirichlet prior of p,
* with p the estimated frequency of k
*/
for ( j in 1:K ) {
alpha[j] <- 1
}
}
model {
for ( i in 1:N ) {
/* Some model for x, given k */
x[i] ~ dnorm(xhat[k[i]], tau_x)
/* Sample missing values of k, and inform
* the distribution p with the known k's
*/
k[i] ~ dcat(p)
}
/* Model the distribution p_k of the trait k */
p ~ ddirch(alpha)

/* The precision of the likelihood for x */
tau_x ~ dgamma(0.01, 0.01)

/* Give the xhat's a shared prior to regularize them */
for ( j in 1:K ) {
xhat[j] ~ dnorm(mu_xhat, tau_xhat)
}
mu_xhat ~ dnorm(0, 0.01)
tau_xhat ~ dgamma(0.01, 0.01)
}

The data file must contain the vector $$k$$, with NA in the place of the missing values.

Suppose now that the missing $$k_i$$s are not completely unknown. In stead, suppose that we know that some of the $$k_i$$ must belong to a group $$g_i$$. The group $$g_i$$ is encoded as a binary vector, where a $$g_{i,j} = 1$$ indicates that the trait value $$j$$ is in the group, and $$0$$ that it is not. In particular, when $$k_i$$ is known, then $g_{i,j} = \left\{ \begin{array}{ll} 1 & {\rm if\ } j = k_i \\ 0 & {\rm otherwise} \end{array} \right.$ If $$k_i$$ is completely unknown, then we just let $$g_{i,j} = 1$$ for each $$j$$.
data {
for ( j in 1:K ) {
alpha[j] <- 1
}
/* for the "ones trick",
* we need a 1 for every observation
*/
for ( i in 1:N ) {
ones[i] <- 1
}
}
model {
for ( i in 1:N ) {
x[i] ~ dnorm(xhat[k[i]], tau_x)
/* sample missing k's using the group-vector g */
k[i] ~ dcat(g[i,])
/* in order to inform p correctly,
* we need to multiply the posterior with p[k[i]],
* which can be done by observing a 1,
* assumed to be Bernoulli(p[k[i]]) distributed
*/
ones[i] ~ dbern(p[k[i]])
}
p ~ ddirch(alpha)

tau_x ~ dgamma(0.01, 0.01)
for ( j in 1:K ) {
xhat[j] ~ dnorm(mu_xhat, tau_xhat)
}
mu_xhat ~ dnorm(0, 0.01)
tau_xhat ~ dgamma(0.01, 0.01)
}

In stead of using the "ones trick", it would have been more clear-cut if we were able to write
k[i] ~ dcat(g[i,]) /* sampling statement */
k[i] ~ dcat(p) /* statement to inform p */

but in this way JAGS can not tell that it is to sample from $${\rm Categorical}(g_i)$$, and not from $${\rm Categorical}(p)$$.
Similarly, it might have been tempting to write
k[i] ~ dcat(g[i,] * p) /* point-wise vector multiplication */

This would correctly prefer the common $$k\,$$s in group $$g_i$$ over the rare $$k\,$$s, but the fact that $$k_i$$ must come from the group $$g_i$$ is not used to inform $$p$$.

As an example, I generated some random $$k_i$$s sampled from $${\rm Categorical}(p)$$ (I did not bother to sample any $$x_i$$s). I have taken $$K = 15$$, $$N = 2000$$, and $$3$$ randomly chosen groups. For a $$1000$$ observations, I "forget" the actual $$k_i$$, and only know the group $$g_i$$. The goal is to recover $$p$$ and the missing $$k_i$$s.
Using a chain of length $$20000$$ (using the first $$10000$$ as a burn-in and $$1/10$$-th thinning) we get the following result:

## Thursday, 5 May 2016

### Computing Bayes factors with Stan and a path-sampling method

Stan is a great program for MCMC (or HMC, really). Vehtari et al. explain here how to use Stan to compute WAIC. For the Bayes factor, however, I have not found a method yet, and therefore I would like to demonstrate a possible method here. This will obviously not work well for every model; this is merely an experiment.
Recently, I was really intrigued by a paper by Gelman and Meng, where several methods for computing Bayes factors, or normalizing constants, are explained and connected (even the really bad ones). Here, I will use the path sampling method.
Let us implement a simple model in Stan, for which we can explicitly compute the marginal likelihood. Then we can try to estimate this marginal likelihood with the path-sampling method, and compare it with the exact value.
A very simple model is the 'fair coin' example (taken directly from wikipedia). The Bayes factor between a null-model $$M_0$$ and a model that incorporates a bias, $$M_1$$, can be computed directly as the quotient of the marginal likelihoods, and moreover, the null model does not have any parameters.
Let $$n$$ denote the number of coin tosses, and $$k$$ the number of 'heads'. Hence, the data $$D$$ is given by the pair $$(n,k)$$. Given a prior $$\theta \sim {\rm Beta}(\alpha, \beta)$$ on the probability of throwing heads, we get the posterior $$\theta \sim {\rm Beta}(\alpha + k, \beta +n-k)$$, and we can compute the marginal likelihood exactly:
$p(D|M_1) = \int_0^1 p(D|\theta)\pi(\theta) d\theta$ $= {n \choose k}\int_0^1 \theta^{k+\alpha-1}(1-\theta)^{n-k+\beta-1} d\theta = {n \choose k} B(k+\alpha, n-k+\beta)\,,$
where $$B$$ denotes the Beta function. Meanwhile,
$p(D | M_0) = {n \choose k} \left(\tfrac12\right)^k\left(1-\tfrac12\right)^{n-k}\,.$
In this instance of path sampling (and closely following Gelman and Meng), we consider a family of (un-normalized) distributions $$Q_T$$, indexed by a parameter $$T \in [0,1]$$, such that $$Q_0(\theta) = \pi(\theta)$$ and $$Q_1(\theta) = p(D|\theta)\pi(\theta)$$. The normalizing constants are denoted by $$z(T)$$. Notice that $$z(0) = 1$$ and $$z(1) = p(D|M_1)$$.
Let $$\Theta = [0,1]$$ denote the support of $$\theta$$. Since
$\frac{d}{dT} \log z(T) = \frac{1}{z(T)} \frac{d}{dT} z(T) = \frac{1}{z(T)} \frac{d}{dT} \int_{\Theta} Q_T(\theta) d\theta\,,$
we get that
$\frac{d}{dT} \log z(T) = \int_{\Theta} \frac{1}{z(T)} \frac{d}{dT} Q_T(\theta) d\theta\,,$
and hence
$\frac{d}{dT} \log z(T) = \int_{\Theta} \frac{Q_T(\theta)}{z(T)} \frac{d}{dT} \log(Q_T(\theta)) d\theta\,.$
When we denote by $$\mathbb{E}_T$$ the expectation under $$P_T$$, we get that
$\frac{d}{dT} \log z(T) = \int_{\Theta} P_T(\theta) \frac{d}{dT} \log(Q_T(\theta)) d\theta = \mathbb{E}_T\left[ \frac{d}{dT} \log(Q_T(\theta)) \right]\,.$
We can think of $$U(\theta, T) := \frac{d}{dT} \log(Q_T(\theta))$$ as 'potential energy', and we get
$\int_0^1 \mathbb{E}_T\left[U(\theta, T)\right] dT = \int_0^1 \frac{d}{dT} \log(z(T)) dT$ $= \log(z(1)) - \log(z(0)) = \log(z(1)/z(0)) =: \lambda\,.$
Notice that in our case $$\lambda = \log(P(D|M_1))$$. We can interpret $$\int_0^1 \mathbb{E}_T\left[U(\theta, T)\right] dT$$ as the expectation of $$U$$ over the joint probability density of $$T$$ (with a uniform prior) and $$\theta$$:
$\lambda = \mathbb{E}\left[ U(\theta, T)\right]\,.$
This suggests an estimator $$\hat{\lambda}$$ for $$\lambda$$:
$\hat{\lambda} = \frac{1}{N} \sum_{i=1}^N U(\theta_i, T_i)\,,$
where $$(\theta_i, T_i)_i$$ is a sample from the joint distribution of $$\theta$$ and $$T$$. A way of creating such a sample, is first sampling $$T_i$$ from its marginal (uniform) distribution, and then sampling $$\theta_i$$ from $$P_{T_i}$$. This last step might require some Monte Carlo sampling.
First, we need to choose $$1$$-parameter family of distributions. A simple choice is the geometric path:
$Q_T(\theta) = \pi(\theta)^{1-T} (\pi(\theta)p(D|\theta))^{T} = \pi(\theta) p(D|\theta)^T\,.$
In this case, the potential energy simply equals $$\frac{d}{dT}\log(Q_T(\theta)) = \log p(D|\theta)$$

## The Stan model

Using the pystan interface, we can implement the model as follows. The most important parts are the parameter $$T$$ (declared in the data section), and the "generated quantity" $$U$$.
## import some modules
import pystan
import scipy.stats as sts
import scipy.special as spl
import numpy as np
import multiprocessing

## define a Stan model
model = """
data {
int<lower=0> n;
int<lower=0, upper=n> k;
real<lower=0> alpha;
real<lower=0> beta;
real<lower=0, upper=1> T; // parameter for path sampling
}
parameters {
real<lower=0, upper=1> theta;
}
model {
theta ~ beta(alpha, beta);
increment_log_prob(T*binomial_log(k, n, theta));
// replaces sampling statement "k ~ binomial(n, theta)"
}
generated quantities {
real U;
U <- binomial_log(k, n, theta);
}
"""

## let Stan translate this into C++, and compile...
sm = pystan.StanModel(model_code=model)


## A parallel method

We need to generate samples $$T_i$$ from $${\rm Uniform}(0,1)$$, and then, given $$T_i$$ we generate a sample $$\theta_i$$ from $$P_{T_i}$$. The simplest way is just to make a partition $$T_i = \tfrac{i}{N}$$ of $$[0,1]$$ and then for each $$i=0,\dots,N$$, use the Stan model with $$T = T_i$$. Notice that for each $$i$$, we will generate multiple ($$K$$, say) samples from $$P_{T_i}$$. This method lends itself well for multi-processing, as all $$N+1$$ Stan sessions can run in parallel.
## choose some parameters
n = 100 ## coin tosses
alp = 1 ## determines prior for q
bet = 1 ## determines prior for q
K = 100 ## length of each chain
N = 1000 ## number of Ts

## a function that prepares a data dictionary,
## and then runs the Stan model
def runStanModel(T):
coin_data = {
'n' : n,
'k' : k,
'alpha' : alp,
'beta' : bet,
'T' : T
}
fit = sm.sampling(data=coin_data, iter=2*K,
warmup=K, chains=1)
la = fit.extract(permuted=True)
return la['U'] ## U is a "generated quantity"

## make a partition of [0,1]
Ts = np.linspace(0, 1, N+1)
## start a worker pool
pool = multiprocessing.Pool(4) ## 4 threads
## for each T in Ts, run the Stan model
Us = np.array(pool.map(runStanModel, Ts))

Let's have a look at the result. Notice that for $$\alpha=\beta=1$$, the marginal likelihood does not depend on $$k$$ as $P(D|M_1) = {n \choose k} \frac{\Gamma(k+1)\Gamma(n-k+1)}{\Gamma(n+2)} = {n \choose k} \frac{k! (n-k)!}{(n+1)!} = \frac{1}{n+1}$ We could take for $$\hat{\lambda}$$ the average of all $$(N+1)\cdot K$$ samples, but in my experience, the standard error is more realistic when I only take one sample per $$T_i$$.
## take one sample for each T
lamhat = np.mean(Us[:,-1])
## we can also compute a standard error!!
se_lamhat = sts.sem(Us[:,-1])

print "extimated lambda = %f +/- %f"%(lamhat, se_lamhat)
print "estimated p(D|M_1) = %f"%np.exp(lamhat)

exactMargLike = spl.beta(k+alp, n-k+bet) * spl.binom(n,k)
exactMargLoglike = np.log(exactMargLike)

print "exact lambda = %f"%exactMargLoglike
print "exact p(D|M_1) = %f"%exactMargLike

In my case, the result is
estimated lambda = -4.724850 +/- 0.340359
estimated p(D|M_1) = 0.008872
exact lambda = -4.615121
exact p(D|M_1) = 0.009901


## A serial method

Another method does not use parallel processing, but uses the fact that the distributions $$P_{T_i}$$ and $$P_{T_{i+1}}$$ are very similar when $$T_{i}-T_{i+1} = \frac{1}{N}$$ is small. When we have a sample from $$P_{T_i}$$, we can use it as the initial condition for the Stan run with $$T = T_{i+1}$$. We then only need very little burn-in (warm-up) time before we are actually sampling from $$P_{T_{i+1}}$$. We can specify the number of independent chains that Stan computes, and also separate initial parameters for each of the chains. Hence, we can take multiple samples $$P_{T_i}$$ as initial choices for the next chain. For this very simple model, this "serial" method is much slower than the parallel method, but my guess is that it could be a lot faster for more complicated models. I hope to prove this claim in a future post.
## choose some parameters
n = 100 ## coin tosses
alp = 1 ## determines prior for q
bet = 1 ## determines prior for q
K = 100 ## size initial chain
N = 200 ## number of Ts

## initially, do a longer run with T=0
coin_data = {
'n' : n,
'k' : k,
'alpha' : alp,
'beta' : bet,
'T' : 0
}
fit = sm.sampling(data=coin_data, iter=2*K,
warmup=K, chains=1)
la = fit.extract(permuted=True)

## in stead of length K,
## now use a much shorter chain (of length L)
L = 10
chains = 4
Us = np.zeros(shape=(N+1,L*chains))
Ts = np.linspace(0, 1, N+1)

## now run the 'chain of chains'
for i, Ti in enumerate(Ts):
coin_data['T'] = Ti ## take another T
## take some thetas from the previous sample
thetas = np.random.choice(la["theta"], chains)
initial_guesses = [{'theta' : theta} for theta in thetas]
fit = sm.sampling(data=coin_data, iter=2*L, warmup=L,
chains=chains, init=initial_guesses)
la = fit.extract(permuted=True)
Us[i,:] = la['U']

Ok, let us have another look at the result. For this, I used the same code as above:
estimated lambda = -5.277354 +/- 1.120274
estimated p(D|M_1) = 0.005106
exact lambda = -4.615121
exact p(D|M_1) = 0.009901

As you can see, the estimate is less precise, but this is due to the fact that $$N=200$$ instead of $$1000$$.
I've written in, and I ran the above code fragments from a Jupyter notebook. As compiling and sampling can take a lot of time, such an interface can be very convenient. Please let me know when this was somehow useful for you or if you have any questions, and also please tell me if I did something stupid...

## Monday, 2 May 2016

### A Sunset Colormap for Python

Room Z533 in the Kruyt building in Utrecht can have a wonderful view, especially at sunset. Not too long ago, I took the following photograph.

Having a love-hate relationship with colormaps, and in particular choosing a good one, I INSTANTLY noticed the beauty of the gradient of the sky, and hence, its application as a colormap.

In Python, it is easy to import figures as a matrix, and then extract the RGB values along a line. To make things easier, I first cropped out the part of the sky I wanted:
Then, I had a look at this website.
Let's import the "slice of sky" in Python and have a look:
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.colors import LinearSegmentedColormap
from PIL import Image

im = Image.open("/path/to/sunset.jpg")

xs = range(im.size[1])
y = im.size[0]/2
rs = [pix[y,x][0] for x in xs]
gs = [pix[y,x][1] for x in xs]
bs = [pix[y,x][2] for x in xs]

fig = plt.figure(figsize=(5,2))

ax.plot(xs, rs, color='red')
ax.plot(xs, gs, color='green')
ax.plot(xs, bs, color='blue')
ax.set_xlabel("pixel")
ax.set_ylabel('r/g/b value')

plt.savefig('rgb-plot.png', dpi=300, bboxinches='tight')


This results in the following figure:

Now let's make the actual colormap.
Dp = 1 ## determines the number of pixels between "nodes"
xs = np.linspace(0,1,im.size[1]/Dp + 1) ## points between 0 and 1
idxs = range(0,im.size[1],Dp) ## indices in the original picture matrix

redlist = [rs[idx]/255. for idx in idxs] + [rs[-1]/255., 0]
greenlist = [gs[idx]/255. for idx in idxs] + [gs[-1]/255., 0]
bluelist = [bs[idx]/255. for idx in idxs] + [bs[-1]/255., 0]

## LinearSegmentedColormap wants these weird triples,
## where some end points are ignored...

redtuples = [(x, redlist[i], redlist[i+1]) for i, x in enumerate(xs)]
greentuples = [(x, greenlist[i], greenlist[i+1]) for i, x in enumerate(xs)]
bluetuples = [(x, bluelist[i], bluelist[i+1]) for i, x in enumerate(xs)]

cdict = {'red' : redtuples, 'green' : greentuples, 'blue' : bluetuples}

cmap = LinearSegmentedColormap('tbz533', cdict) ## choose a name
plt.register_cmap(cmap=cmap)
## now we can use our new colormap!


OK. Let's make some 70s wallpaper to celebrate the new colormap:
xs = np.linspace(-5,5,1000)
ys = np.linspace(-5,5,1000)
zs = np.array([[np.sin(x)*np.cos(y) for x in xs] for y in ys])

fig = plt.figure(figsize=(5.5,5))
C = ax1.pcolormesh(xs, ys, zs, cmap='tbz533')
fig.colorbar(C)

fig.savefig('my70swallpaper.png', dpi=200, bboxinches='tight')


This results in the following figure:

## Friday, 6 November 2015

### ETA for C++

Simulations can take some time, and I'd like to know how long. This is easy, right? Yes, it is. I've done it lots of times, but every time I do, I curse myself for not using an old piece of code.
most likely, there is some standard, best way of doing this, but I haven't found it. Most recently, I did this: I made a simple object "EtaEstimator", that can be updated every (costly) time step and asked for an estimated time of "arrival" at any time. Here's the header:
// eta.hpp
#include <ctime>
#include <cmath> // floor
#include <iostream>

class EtaEstimator {
public:
EtaEstimator(int );
// constuction starts the clock. Pass the number of steps
void update();
void print(std::ostream & ) const;
private:
double ct, etl; // cumulative time, estimated time left
int n, N; // steps taken, total amount of steps
clock_t tick; // time after update ((c) matlab)
// statics...
static const int secperday = 86400;
static const int secperhour = 3600;
static const int secperminute = 60;
};

std::ostream & operator<<(std::ostream & , const EtaEstimator & );

The members are straight forward too:
// eta.cpp
#include "eta.hpp"

EtaEstimator::EtaEstimator(int N) :
ct(0.0), etl(0.0), n(0), N(N) {
tick = clock();
}

void EtaEstimator::update() {
clock_t dt = clock() - tick;
tick += dt;
ct += (double(dt)/CLOCKS_PER_SEC); // prevent integer division
// CLOCKS_PER_SEC is defined in ctime
++n;
etl = (ct/n) * (N-n);
}

void EtaEstimator::print(std::ostream & os) const {
double etlprime = etl;
int days = floor(etlprime / secperday);
etlprime -= days * secperday;
int hours = floor(etlprime / secperhour);
etlprime -= hours * secperhour;
int minutes = floor(etlprime / secperminute);
etlprime -= minutes * secperminute;
int seconds = floor(etlprime);
os << (days > 0 ? std::to_string(days) + " " : "")
<< hours << ":"
<< (minutes < 10 ? "0" : "") << minutes << ":"
<< (seconds < 10 ? "0" : "") << seconds;
}

std::ostream & operator<<(std::ostream & os,
const EtaEstimator & eta) {
eta.print(os);
return os;
}

Typical usage of EtaEstimator would be the following:
#include <iostream>
#include "eta.hpp"

// about to do lots of work...
int N = 1000;
EtaEstimator eta(N);
for ( int n = 0; n < N; ++n ) {
// do something very expensive
eta.update()
std::cout << "\rETA: " << eta << std::flush;
}
// ...

PS: std::to_string is a C++11 feature, and can be ignored by using something like
if ( days > 0 ) os << days << " "; // else nothing at all