8

Let X and Y be independent and identical exponential random variables with parameter $\theta>0$. Compute $P[X \leq x | X+Y]$ for $x\geq0$.

I tried to solve this theoretically here (https://math.stackexchange.com/questions/4508874/conditional-distribution-function-of-one-random-variable-given-the-sum-of-two) and I wanted to verify it through simulation as I'm waiting a response.

Any suggestion?

Enrico
  • 211
  • I would like some sort of a runnable example that generically solves the problem. E.g. I would like to fix the sum of X+Y to 5, $\theta=1$ and obtain a result. Do you think it is possible to get the solution in an answer here? Thank you. – Enrico Aug 09 '22 at 13:11
  • This could be fine. Could you give me some references so I can write down some code? Thank you. – Enrico Aug 09 '22 at 13:26
  • Sorry for being not clear enough: imho one cannot verify the theoretical result since one need exploit the theoretical result to simulate from the conditional. The only verification one could make is to simulate from the marginal of $X+Y$ then $X$ given $X+Y$ and check that the marginals of $X$ and $Y$ are indeed both Exp$(\theta)$. – Xi'an Aug 09 '22 at 13:29
  • So there is no way to simulate two exponential distributed and independent samples and obtain an estimate for that probability in a "Monte Carlo" style? Probably I am a bit rusty in these topics after 5 years of not using them anymore. – Enrico Aug 09 '22 at 13:40
  • 1
    @Xi'an I am surprised at such a negative prognosis. I know you know of many efficient ways to generate $(X,Y)$ values conditional on some event. Any way that does not rely on the analysis used to compute the conditional distribution would serve as a useful check of that analysis. – whuber Aug 09 '22 at 14:01

2 Answers2

9

Unless the conditional density varies rapidly with the conditioning event, a brute-force rejection sample works.

Let's state the general problem. You wish to study the distribution of some function $f(X,Y)$ conditional on an event $\mathscr E$ which is either extremely rare or, as in this case, has zero probability (but positive density).

The idea is that with some care--there are geometric and probabilistic subtleties here (that are partially explored below)--you can "thicken" $\mathscr E$ by an amount $\delta$ to an event $\mathscr E^\prime = \mathscr E(\delta) \supset \mathscr E$ that has positive probability. Generate $(X,Y)$ from its joint distribution and reject any results not in $\mathscr E^\prime,$ then examine the empirical distribution of $f(X,Y)$ and compare it to what you expected theoretically.

This works provided the probability of $\mathscr E^\prime$ is not so small that it takes forever for randomly-generated $(X,Y)$ to fall within it. To obtain a sample of size $n,$ you will need (on average) to generate $N = n/\Pr(\mathscr E^\prime)$ values of $(X,Y).$ I recommend starting with a modest value of $N,$ generating values, and observing what $n$ turns out to be. Extrapolate from that to determine how many values you have time to generate. If it's too small, you will have to thicken $\mathscr E$ more -- and you can make a reasonable guess about how much more is needed based on this preliminary study.


Let's use your problem as an example. $X$ and $Y$ are independently Exponentially distributed. Because the parameter only establishes the unit of measurement, we may take it to be $1$ with no loss of generality. The function is $f(X,Y) = X+Y.$

A quick and dirty R implementation mirrors the strategy. This one examines the distribution of $X$ conditional on $X + Y \approx 0.5,$ using an amount $\delta = 0.05$ to thicken $\mathscr E:$

x <- rexp(1e4)
y <- rexp(1e4)
z <- x + y                                    # z = f(x,y)
i <- abs(z - 0.5) <= 0.05                     # Thicken E to E'
X <- data.frame(x = x[i], y = y[i], z = z[i]) # Reject results outside E'

Here is what the empirical $(x,y)$ scatterplot might look like:

with(X, plot(x, y))

Figure 1, showing (x,y) pairs along with the line x + y = 1/2

The line $x+y=0.5$ is plotted for reference: it corresponds to the conditioning event $\mathscr E.$ The gray area (comprised of thousands of generated points) more or less fills out $\mathscr E^\prime.$

With such a simulation in hand, you can explore the conditional distribution of any $f(X,Y)$ in all the familiar ways, such as with a histogram

Figure 2 histogram of x

(the dashed line is the theoretical value) or an empirical CDF (ECDF)

Figure 3 ECDF of x

The (faintly visible) dashed blue line is the theoretical distribution, shown for reference.

There are clear edge effects near $X = 0.5$ but those are understandable consequences of the fact that $Y \lt 0$ is not possible, thereby reducing the probability at that corner of $\mathscr E^\prime.$ (This is why examining the first scatterplot is helpful: it alerts you to such problem regions and helps you interpret the results.)


I mentioned the need for care. Let's illustrate that by showing what happens when the event $\mathscr E$ is thickened differently, using the same three figures.

Figure 1'

I have allowed $\mathscr E^\prime$ to be thicker at smaller $X$ values than at larger ones, thereby relatively oversampling the smaller $X$ values. (This has a rationale: it eliminates the edge effects near $X = 0.5$) The histogram

Figure 2'

and the ECDF

Figure 3'

reflect that. They clearly differ from the theoretical result.

The moral of this example is that you need to understand what you're doing when you compute distributions conditional on events with zero probability!

whuber
  • 322,774
  • 1
    I appreciate your answer whuber. If I am right, my theoretical result seems confirmed by what you found examining the ecdf of X|X+Y. – Enrico Aug 09 '22 at 15:25
  • 1
    I have plotted your theoretical result in the histograms and ECDFs. There is good agreement :-). – whuber Aug 09 '22 at 15:43
  • Good, I'm waiting someone to validate them from a mathematical point of view. One question: shouldn't the conditional density of X|X+Y be equal to 2 in the plot? Thank you. – Enrico Aug 09 '22 at 15:54
  • 1
    My dotted line is a little less than $2$ to account for the edge effects near $0.5:$ it represents the uniform distribution on the interval $[0, 0.5+0.05].$ The theoretical answer is given in several other threads here, including https://stats.stackexchange.com/questions/252692 (where yours is the special case $n=2$). Using uniform order statistics it's easy to show $X/(X+Y)$ has a Uniform distribution and that's exactly what your result says. – whuber Aug 09 '22 at 16:20
  • 3
    This is a great answer. I learned a lot from your post. – Eli Aug 09 '22 at 18:25
7

We could approach the problem using inverse transform sampling: generate random samples from the standard uniform distribution ($U$) and, given $X+Y$ that we specify, solve for $X$ and $Y$ for each $U$ using the hypothesized CDF. Then test whether $X$ and $Y$ are iid exponential variates.

Equivalently, we can reverse that approach by generating random $X$, $Y$ pairs, where the same exponential distribution is used within each pair. Then use the hypothesized CDF to transform them to $Z=\frac{X}{X+Y}$ (basically inverse transform sampling in reverse). Then simply test if $Z\sim U(0,1)$.

In R:

set.seed(94)
n <- 1e7L
X <- rexp(n)
Y <- rexp(n)
z <- X/(X + Y)
ks.test(z, "punif")
#> 
#>  One-sample Kolmogorov-Smirnov test
#> 
#> data:  z
#> D = 0.00029162, p-value = 0.3629
#> alternative hypothesis: two-sided

Check that the results hold while allowing the rate parameter to vary with each $X$, $Y$ pair:

theta <- runif(n)
X <- rexp(n, theta)
Y <- rexp(n, theta)
z <- X/(X + Y)
ks.test(z, "punif")
#> 
#>  One-sample Kolmogorov-Smirnov test
#> 
#> data:  z
#> D = 0.00037478, p-value = 0.1205
#> alternative hypothesis: two-sided

Or plot the ECDF:

set.seed(94)
n <- 1e6L
X <- rexp(n)
Y <- rexp(n)
plot(ecdf(X/(X + Y)))

enter image description here

theta <- runif(n)
X <- rexp(n, theta)
Y <- rexp(n, theta)
plot(ecdf(X/(X + Y)))

enter image description here

jblood94
  • 1,459
  • 4
    The initial idea is great (for this specific problem), but checking it with moments is poor: why not just explore the entire distribution of $X/(X+Y)$? If you want to be formal, conduct a KS test: it's simple and powerful (it's the analog of visually comparing the ECDF to the theoretical CDF). – whuber Aug 09 '22 at 16:39
  • 1
    Point taken. See updated answer. – jblood94 Aug 09 '22 at 17:00
  • 1
    Cool approach, thank you jblood94. – Enrico Aug 09 '22 at 17:29
  • My confusion is how to get from establishing empirically that $X/(X+Y)\sim\mathcal U(0,1)$ to deriving the conditional distribution of $X$ conditional on $X+Y$. – Xi'an Aug 10 '22 at 07:17
  • Yes, of course. This approach would not be useful for deriving the conditional distribution, only for testing an existing hypothesized distribution. The OP has a hypothesized distribution, so this approach seemed appropriate. – jblood94 Sep 09 '22 at 15:58