ocbe-uio / TruncExpFam

R package to generate data related to the Truncated Exponential Family
https://ocbe-uio.github.io/TruncExpFam/
GNU General Public License v3.0
0 stars 1 forks source link

Add probability and quantile functions #54

Open wleoncio opened 3 years ago

wleoncio commented 3 years ago

After we've implemented the r* and d* functions, work on p* and q*. Both would probably be numerically determined (though analytical solutions are of course preferred).

Maybe one function can do the job for all distributions, since the procedure is similar see sketch below)?

prtrunclnorm <- function(q, meanlog = 0, sdlog = 1, lower.tail = TRUE, log.p = FALSE) {
  y <- rtrunc.lognormal(n = 1e3, ...)
  # bootstrap y
  # get value of y_0 of y corresponding to q
  # return probability from -Inf to y_0
}

and q* is basically the other way around.

wleoncio commented 3 years ago

A simpler solution, supposing prtrunclnorm(q, a) is the same as plnorm(q) - plnorm(a) is to do

prtrunclnorm <- function(..., a, b) {
  if (lower.tail) {
    return(plnorm(q) - plnorm(a))
  } else {
    return(plnorm(b) - plnorm(q))
  }
}

And the same goes for the other distros.

rho62 commented 3 years ago

Yes, definitely a good suggestion. The only limitation I see, is if we have distributions, where the corresponing untruncated dist is not part of Base R (continuous Bernouilli, inverse Gamma)

  1. jun. 2021 kl. 10:03 skrev Waldir Leoncio @.***>:

A simpler solution, supposing prtrunclnorm(q, a) is the same as plnorm(q) - plnorm(a) is to do

prtrunclnorm <- function(..., a, b ) {

if (lower.tail ) {

return(plnorm(q) - plnorm(a )) } else {

return(plnorm(b) - plnorm(q )) } }

And the same goes for the other distros.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or unsubscribe.

wleoncio commented 2 years ago

Removed this from 1.0.0 because it can be part of the MVP and, thus, a publication of a 0.1.0 version of the package.

Adding the prob and quantile functions might take a while, so I think it's a good idea to publish 0.1.0 as a milestone before adding these to an eventual 1.0.0.

wleoncio commented 2 years ago

Removed from MVP as agreed in our 2021-11-04 meeting. These functions are important, but not essential for the 1.0.0 launch.

wleoncio commented 2 years ago

Quantile functions

These will be calculated numerically. Use the CDF of a distribution (which increases monotonically) and find out which quantile gives the correct P(Y < y).

Probability functions

These should also be relatively straightforward, they are just a proportion C of their untruncated versions. See image below, with C in pink and the probability function in green. bilde

Keep in mind that the integral limits, Lo and Hi, should in practice be the min/max between the truncation limit and the distribution support.

C is also used in all the density functions in this package, so it should probably be an auxiliary function to reduce code footprint.

wleoncio commented 2 years ago

Some relevant suggestions/comments can be found here and here.

wleoncio commented 9 months ago

Easy implementation from https://github.com/ocbe-uio/TruncExpFam/issues/80#issue-1145631708 (adapted, where X is a distribution name):

ptruncX(x; \theta) = (pX(x) - pX(a)) / pX(b) - pX(a)
qtruncX(x; \theta) = qX(p * (p * pX(b) + (1 - p) * pX(a)))

⚠️ Edit:

ptruncX(x; \theta) = (pX(x) - pX(a)) / (pX(b) - pX(a))
qtruncX(x; \theta) = qX(p * pX(b) + (1 - p) * pX(a))
wleoncio commented 5 months ago

Progress for ptrunc():

wleoncio commented 1 month ago

Checklist for implementing qtrunc()

rho62 commented 4 weeks ago

Hi Waldir,

For those distributions that are implemented in base R it is quite simple to find the qtrunc() functions: So lets call the distribution function for the regular (not-truncated) distribution F (eg pnorm or pgamma) and the corresponding distribution function q. Then qtrunc(p)=q(p(F(b)-F(a))+F(a), where a and b are lower and upper truncation limits. For example: qtrunc(p;theta)=q(p(pgamma(b,theta)-pgamma(a,theta))+pgamma(a,theta))

/René

Fra: Waldir Leoncio @.> Svar til: ocbe-uio/TruncExpFam @.> Dato: onsdag 5. juni 2024 kl. 15:16 Til: ocbe-uio/TruncExpFam @.> Kopi: Rene Holst @.>, Comment @.***> Emne: Re: [ocbe-uio/TruncExpFam] Add probability and quantile functions (#54)

Checklist for implementing qtrunc()

— Reply to this email directly, view it on GitHubhttps://github.com/ocbe-uio/TruncExpFam/issues/54#issuecomment-2149873449, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AFPRUPVBIYWLDG4MIQVEYXLZF4FTNAVCNFSM465E5F3KU5DIOJSWCZC7NNSXTN2JONZXKZKDN5WW2ZLOOQ5TEMJUHE4DOMZUGQ4Q. You are receiving this because you commented.Message ID: @.***>

wleoncio commented 4 weeks ago

Thank you for the suggestion René, I'll give the analytical solution one more try. I tried this one, but it didn't work and I couldn't figure out why, so I implemented a numerical solution for the normal. It's rather fast (bisection method), but definitely worse than an analytical solution.

rho62 commented 4 weeks ago

Hmm, that’s odd. Have you tried with both double truncation (meaning both a and b being defined/ inner points), with only right truncation (so F(a)=0) and with only left truncation, (so F(b)=1)?


Fra: Waldir Leoncio @.***> Sendt: fredag 7. juni 2024 12:55:05 Til: ocbe-uio/TruncExpFam Kopi: Rene Holst; Comment Emne: Re: [ocbe-uio/TruncExpFam] Add probability and quantile functions (#54)

Thank you for the suggestion René, I'll give the analytical solution one more try. I tried this onehttps://github.com/ocbe-uio/TruncExpFam/issues/54#issuecomment-1733603789, but it didn't work and I couldn't figure out why, so I implemented a numerical solution for the normal. It's rather fast (bisection method), but definitely worse than an analytical solution.

— Reply to this email directly, view it on GitHubhttps://github.com/ocbe-uio/TruncExpFam/issues/54#issuecomment-2154594005, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AFPRUPW63TS6KUOJD63GW6DZGGGQTAVCNFSM465E5F3KU5DIOJSWCZC7NNSXTN2JONZXKZKDN5WW2ZLOOQ5TEMJVGQ2TSNBQGA2Q. You are receiving this because you commented.Message ID: @.***>

wleoncio commented 2 days ago

Hi René,

Thanks again for providing me with the formulas. The one I was using previously (here, with corrections posted after "Edit") was wrong, which explains the issues I was having. The latest one you posted (here) have been implemented and seem to be working fine for Normal. I'll continue work on the remaining distros.