# Can we sample from the wrapped normal distribution and evaluate the density of the sample simultaneously?

In a computer program (written in C++), given $$x\in[0,1)$$ and $$\sigma>0$$, I need to sample $$y$$ from the wrapped normal distribution $$\mathcal W_{x,\:\sigma^2}$$ with mean $$x$$ and variance $$\sigma^2$$ and evaluate the density $$q$$ of $$\mathcal W_{x,\:\sigma^2}$$ at $$y$$ at the same time.

If $$\varphi_{\sigma^2}$$ denotes the density of the normal distribution with mean $$0$$ and variance $$\sigma^2$$ and $$\psi_{\sigma^2}(y):=\sum_{k\in\mathbb Z}\varphi_{\sigma^2}(k+y)\;\;\;\text{for }y\in\mathbb R,$$ then $$q(y):=\psi_{\sigma^2}(y-x)\;\;\;\text{for }y\in[0,1)$$ is the density of $$\mathcal W_{x,\:\sigma^2}$$ with respect to the Lebesgue measure.

We can sample $$y$$ by drawing $$\xi\sim\mathcal N_{0,\:1}$$, setting $$z:=x+\sigma\xi$$ and $$y:=z-\lfloor z\rfloor$$. But now I would need to evaluate $$q(y)$$ separately.

So, my question is: Can we somehow sample $$y$$ in a clever way such that we obtain $$q(y)$$ as a byproduct? If not, how should we evaluate $$q(y)$$ (the problematic thing being that it's an infinite sum)?

• It's an infinite sum, but for $\sigma$ not much larger than $1$, it will converge pretty quickly. Consider $\sigma = 2$ and $y = 1/2$; the 10th term is $\sim 2.5\times 10^{-6}$, and subsequent terms decrease by more than a factor of $10$. With $\sigma = 1$, the sixth term is $\sim 10^{-7}$ and the seventh $\sim 3\times 10^{-10}$, for example. – jbowman Jan 8 at 16:56
• @jbowman So, should I sum from $k=0,1,2,\ldots$ adding up $a_k:=\varphi_{\sigma^2}(y+k)$ and $b_k:=\varphi_{\sigma^2}(y-k)$ and stop once $a_k<\epsilon$ for some suitable $\epsilon$ (maybe $\epsilon=10^{-6}$?)? Or should I check $b_k$ as well? – 0xbadf00d Jan 8 at 20:00
• When $\sigma$ is only a little larger than $1,$ for all practical computational purposes this density is uniform: see the last method at stats.stackexchange.com/a/117711/919. For smaller $\sigma,$ as @jbowman indicates, the sum converges very rapidly. It is a theta function; some software includes procedures to evaluate it (buried, for instance, in the KS test). – whuber Jan 8 at 20:13
• @whuber Thank you for your comment. The $\sigma$ I've got in mind is smaller than $1$, e.g. $\sigma=0.01$. – 0xbadf00d Jan 8 at 20:18
• Then you can evaluate the density anywhere with a single term of the series! (At many points--more than about $0.05$ from the mean--you don't need any terms at all, because the density is practically zero there.) – whuber Jan 8 at 20:20