# How to sample a conditional PDF

Assuming I have two kind of balls, red and blue ones, and I pick randomly (with replacement) always a bunch of balls where the number of balls I pick is distributed according to the Poisson distribution. Now, I'd like to sample from the probability that I pick in total $z$ balls under the condition that I picked at least $k$ blue balls.

So, when denoted $Z$ as the random variable denoting the number of balls I pick in total, $X$ as the red balls I pick and $Y$ as the blue balls I pick. Then, I know that $Pr[ Z=X+Y ] \sim Poisson(\lambda)$ and I'd like to get samples of $Pr[Z\vert Y \geq k]$.

How can I do this?

I tried with rejection sampling, meaning throwing away all samples where $Y<k$, but this corresponds to samples of $Pr[Z,Y\geq k]$, right?

• I do not understand why you are complicating your question by introducing $X$, because it seems that all you are asking is how to sample $Y$ from a truncated Poisson distribution: to obtain $Z$, independently sample $X$ from a Poisson (or whatever distribution you please) and add it to $Y$. Is $X$ intended to play a more important role than that? – whuber Jul 23 '15 at 17:54
• Actually, I'm trying to verify some analytic formula I derived for the expected value $E\{ Z| Y \geq k \}$ (the case with only the red and blue balls is some simplicfication of my acutal problem, but I'm not even sure how to simulate this easier case). So I thought that I need to draw samples from $Pr[ Z \vert Y \geq k ]$, is that right? – bonanza Jul 23 '15 at 18:23
• Because expectation is linear and $X$ is independent of $Y$, $$\mathbb{E}(X+Y\,|\,Y\ge k)=\mathbb{E}(X\,|\, Y \ge k) + \mathbb{E}(Y\,|\, Y \ge k) = \mathbb{E}(X) + \mathbb{E}(Y\,|\, Y \ge k).$$ Therefore you can deal with $X$ separately -- and you already know $\mathbb{E}(X)=\lambda$. The conditional expectation of $Y$ has a closed expression in terms of the regularized Gamma function ($Q$) and exponentials: $$\mathbb{E}(Y\,|\,Y\ge k) = \lambda\left(1-Q(k,\lambda )+\frac{e^{-\lambda } \lambda ^{k-1}}{(k-1)!}\right).$$ – whuber Jul 23 '15 at 19:02
• It may also be useful to explicitly state the the population of balls here is infinite, or that you're sampling with replacement. The way it's worded makes it sound like you're taking a sample from a finite set of balls, which wouldn't actually be possible if $Y$ is Poisson distributed. – dsaxton Jul 23 '15 at 20:38
• @whuber that for this formula. From the formula and the definition of the expected value we $\mathbb{E}[Y]=\mathbb{E}[Y|Y\geq 1]=\lambda$, right? But from (my) intuition that result is somehow weird. Does it mean, that in the case I'd like to estimate the average number of red and blue balls I pick, which is then $\mathbb{E}[Z]$, I have no 'gain' when I already know that I already have picked at least one blue ball ($Y\geq k=1$) ? Or is this due to the fact that I draw with replacement? – bonanza Jul 24 '15 at 8:16

Throwing away samples where $Y < k$ is perfectly reasonable, as long as $k$ isn't too large (which might make this approach a bit inefficient). Of course you are drawing samples of $\{ Z, Y \geq k \}$, but the act of throwing the other cases out turns $\{ Y \geq k \}$ into the entire space, which is what conditioning means.
• Thanks for your answer! Just for my understanding, how would I then sample from $\{Z,Y\geq k\}$ if rejection sampling results in the conditional proability? – bonanza Jul 23 '15 at 17:22
• I'm not clear on the distinction you're making. What do you mean sample from $\{Z , Y \geq k \}$? If you're requiring that $Y \geq k$ in your sampling scheme, then you're sampling from the conditional distribution of $Z \mid Y \geq k$ regardless of whether or not you're using rejection sampling. – dsaxton Jul 23 '15 at 18:28
As I understand this, you are trying to sample $Z=X+Y$ given $Y \ge k$. You say $Z \sim Poisson (\lambda)$ and I assume you intend for $Y|Z \sim Binomial(Z,p)$ where $p$ is the probability of a blue ball. $X$ is trivial because $X=Z-Y$. Note: You can work out that $Y$ is marginally distributed as $Poisson(\lambda p)$. You can sample from $Z\space|\space Y \ge k$ by first sampling $Z$ and then sampling $Y$ using $Z$ as the number of Bernouli trials. If $Y$ is not as big as you like, then discard $Z$ and otherwise keep it. In this way you will only retain realizations of $Z$ that have at least $k$ blue balls.
• Since $Y$ is not independent of $Z$, you need to provide details of your proposal to sample from $Z$ first: the next draw must be from $Y|Z$, not the marginal of $Y$. – whuber Jul 23 '15 at 19:23