Open wleoncio opened 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.
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)
- 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.
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.
Removed from MVP as agreed in our 2021-11-04 meeting. These functions are important, but not essential for the 1.0.0 launch.
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).
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.
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.
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))
Progress for ptrunc()
:
Checklist for implementing qtrunc()
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: @.***>
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.
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: @.***>
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.
After we've implemented the
r*
andd*
functions, work onp*
andq*
. 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)?
and
q*
is basically the other way around.ptrunc*
qtrunc*