Is there a way to compute and plot the transformed random variable in Python or R?

Let's say if $X_1$ and $X_2$ are independent and identically distributed random variable with PDF defined over non-negative values of $x$ as:

$$ f(x) = \frac{x^2e^{-x}}{2} $$

Another random variable $Y$ is defined as $Y = X_1 + X_2$.

I know that for computing the PDF of $Y$, I can define a transformed tandom variable $T = \left(X_1+X_2, X_2\right)$ and use conservation of probability and Jacobian of the transformation. However, what I am interested in is to know if there is a way to do this transformation and compute the PDF using any existing packages/modules of R/Python.

  • 3
    $\begingroup$ As far as I know, R has not this symbolic computation capability. Numerical computation through simulation is the only solution I can think of. $\endgroup$
    – utobi
    Jan 2, 2023 at 13:39
  • $\begingroup$ @utobi, I am not aware of numerical computation through simulation. How could I use it? $\endgroup$ Jan 2, 2023 at 13:55
  • 1
    $\begingroup$ Distributions of the sums of independent variables are computed through convolution, which is most efficiently carried out with the Fast Fourier Transform as explained in several threads here on CV. Other transformations require different methods. Could you therefore please be more specific about what transformation you are interested in? $\endgroup$
    – whuber
    Jan 3, 2023 at 17:01

1 Answer 1


As far as I can tell, R doesn't have such symbolic computation capabilities; though R does have some limited symbolic computation capabilities (e.g. the command D computes symbolically the derivative of a user-provided function).

Nevertheless, when the analytical computation is hard/long, or you are sceptical about your computations, you can get an approximation via the Monte Carlo (MC) approach. The idea is pretty simple. Instead of computing the analytical PDF of $Z$, draw random numbers from its distribution. You can then use these draws to approximate whatever quantity of $Z$ you are interested in.

Example. Suppose $X_1,X_2\sim \text{Exp}(1)$, independently. Then $Z = X_1+X_2 \sim \text{Gamma}(2, 1)$. The MC approach would be to draw $N$ values (i.e. $N\times 1$ vector) from the distribution of $X_1$ and other $N$ values (i.e. $N\times 1$ vector) from the distribution of $X_2$. The element-wise sum of these vectors is another $N\times 1$ vector drawn from $\text{Gamma}(2,1)$.

Here is the R code for this.

# number of samples, the larger the better
N <- 1e4

# draw from X1
x1 <- rexp(N)

# draw from X2 (using the same N!)
x2 <- rexp(N)

# apply your funtion; here is the sum
z = x1+x2

# Distribution of Z computed(approximated) via
# Monte Carlo
hist(z, breaks = 30, probability = T, ylim=c(0,0.4))

# The TRUE distribution of Z
plot(function(x) dgamma(x, shape = 2, rate = 1), 
     n=100, xlim=c(0,12), lwd=2, add=TRUE)

enter image description here

Side note 1. For the MC method to be applicable, you must be able to take random draws from $X_1,X_2$. Sometimes, this is trivial (as in the example above) but sometimes it may be less trivial. This happens when $X_1$ and $X_2$ do not have known distributions (e.g. normal, gamma, Cauchy, etc.) not there are no standard methods for generating random numbers.

Side note 2. The MC method delivers a stochastic approximation. This means that, if you run the approximation again, you will likely get slightly different answers. Analytical computation doesn't have this issue, thus my advice is to use MC as last resort or as a method for double-checking your analytical calculations.

  • 2
    $\begingroup$ Thanks a lot for your answer! There may not be a package to verify the transformed variable as you said but Monte Carlo approach really helped me in double checking my analytical solution. Kudos! $\endgroup$ Jan 2, 2023 at 17:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.