For a simulation, I need to compute the distribution of the product of two discrete random variables. Although I could implement it from scratch, I thought that the distr
package already provides this functionality.
The operatorsIimplemented in the package do not seem to be documented, but, by trying, I have figured out that +
stands for the convolution operation (sum of independent random variables):
> X <- DiscreteDistribution(supp=c(-1,0,1), prob=c(0.25,0.5,0.25))
> Y <- X + X
> attr(Y, "support")
[1] -2 -1 0 1 2
> d(Y)(attr(Y, "support"))
[1] 0.0625 0.2500 0.3750 0.2500 0.0625
Although there seems to be a multiplication operator (at least methods(class="DiscreteDistribution")
reports one), it does not work as expected (actually, it does not work at all):
> Y <- X * X
Error in apply(mixDistr.dfs, 1, all) : dim(X) must have a positive length
Does someone know how to compute the distribution of the product of two discrete independent random variables with distr
? Or does the package lack this functionality?
It turned out that this was a bug in distr
.
As @allan-cameron points out, the operators for distributions are documented with ?'operators-methods'
. There is a multiplication operator listed for arbitrary distributions (class AcDcLcDistribution
), and this indeed works for most cases:
> X <- DiscreteDistribution(1:2)
> Y <- DiscreteDistribution(1:2)
> Z <- X*Y
> support(Z)
[1] 1 2 4
> prob(Z)
1 2 4
0.25 0.50 0.25
My example did not work due to a bug in distr
. I have reported it to the maintainers of distr
, and the bug will be fixed in the upcoming release 2.9.3.