mitchelloharawild / distributional

Vectorised distributions for R
https://pkg.mitchelloharawild.com/distributional
GNU General Public License v3.0
94 stars 15 forks source link

Inverse exponential distribution has inconsistent parametrisation #108

Open Fuco1 opened 2 months ago

Fuco1 commented 2 months ago

Given a cdf F defined on positive numbers for a random variable X, we can find the reciprocal random variable Y = 1/X and its cdf G as follows:

G(y) = Pr(Y <= y) // definition

substitute Y = 1/X and rearrange

Pr(Y <= y) = Pr(X >= 1/y) = 1 - Pr(X < 1/y) = 1 - F(1/y)

Therefore G(y) = 1 - F(1/y)

In this package however:

## this is G(y) in terms of original exponential F(x) cdf
> 1 - cdf(dist_exponential(0.5), 1 / 1:10)[[1]]      
 [1] 0.6065 0.7788 0.8465 0.8825 0.9048 0.9200 0.9311 0.9394 0.9460 0.9512

## using inverse exponential with the same rate gives different results
> cdf(dist_inverse_exponential(0.5), 1:10)[[1]]    
 [1] 0.1353 0.3679 0.5134 0.6065 0.6703 0.7165 0.7515 0.7788 0.8007 0.8187

## using inverse exponential with the reciprocal of the rate gives correct results
> cdf(dist_inverse_exponential(1 / 0.5), 1:10)[[1]]  
 [1] 0.6065 0.7788 0.8465 0.8825 0.9048 0.9200 0.9311 0.9394 0.9460 0.9512

So you can see that we need to parametrize the inverse function with the reciprocal rate to get the same result.

In my opinion this is a confusing API and it caught me a bit off guard while I realized where I was getting weird numbers from :)

Edit:

Using the dist_transformed we get another interesting interplay, notice the quantiles being first 0.8 and then 0.2

> dist_transformed(dist_exponential(1/0.3), \(x) 1/x, \(x) 1/x) %>% quantile(0.8)
[1] 2.071
> dist_inverse_exponential(0.3) %>% quantile(0.2)
[1] 2.071
> 

I'm quite confused to be honest :D

Fuco1 commented 2 months ago

Another thing is that Weibull distribution with shape = 1 is the same as exponential, but also uses reciprocal "rate" argument (called scale in this package).

I don't know if it makes sense to unify this or what are the usual conventions there.