Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add probability and quantile functions #54

Closed
2 tasks done
wleoncio opened this issue Jun 18, 2021 · 13 comments
Closed
2 tasks done

Add probability and quantile functions #54

wleoncio opened this issue Jun 18, 2021 · 13 comments
Assignees
Labels
enhancement New feature or request
Milestone

Comments

@wleoncio
Copy link
Member

wleoncio commented Jun 18, 2021

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.

  • Implement ptrunc*
  • Implement qtrunc*
@wleoncio wleoncio added the enhancement New feature or request label Jun 18, 2021
@wleoncio wleoncio added this to the MVP feature milestone Jun 18, 2021
@wleoncio
Copy link
Member Author

wleoncio commented Jun 18, 2021

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
Copy link
Contributor

rho62 commented Jun 19, 2021 via email

@wleoncio
Copy link
Member Author

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 wleoncio removed this from the MVP feature milestone Nov 4, 2021
@wleoncio
Copy link
Member Author

wleoncio commented Nov 4, 2021

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
Copy link
Member Author

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
Copy link
Member Author

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

@wleoncio wleoncio added this to the MVP 1.3.0 milestone Apr 13, 2023
@wleoncio
Copy link
Member Author

wleoncio commented Sep 25, 2023

Easy implementation from #80 (comment) (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 wleoncio self-assigned this Jan 18, 2024
@wleoncio
Copy link
Member Author

wleoncio commented Jan 18, 2024

Progress for ptrunc():

  • Beta
  • Binomial
  • Chisq
  • Contbernoulli
  • Exp
  • Gamma
  • InvGamma
  • InvGauss
  • Log-Normal
  • Negative Binomial
  • Normal
  • Poisson

wleoncio added a commit that referenced this issue Jan 18, 2024
wleoncio added a commit that referenced this issue Jan 18, 2024
wleoncio added a commit that referenced this issue Jan 18, 2024
`q_T` equation in #78 (comment) suggested that `p = [q - F(a)] / [F(b) - F(a)]`.
wleoncio added a commit that referenced this issue Jan 18, 2024
* issue-54:
  Updated NEWS.md
  Updated docs
  Added test for truncated `ptrunc.normal()` (#54)
  Fixed calculation of `ptrunc()`(#54)
  Vectorized `ptrunc.normal()` (#54)
  Fixed code and links in documentation
  Added `ptrunc()` for normal (#54)
  Reorganized generic files
  Increment version number to 1.1.0.9000
wleoncio added a commit that referenced this issue Feb 1, 2024
wleoncio added a commit that referenced this issue Feb 1, 2024
wleoncio added a commit that referenced this issue Feb 1, 2024
wleoncio added a commit that referenced this issue Feb 1, 2024
As a consequence, `lower.tail` and `log.p` must now be named. On the plus side, since these are usually unused, `ptrunc()` calls are more sensible (distribution args come first), but they also follow the same order as their `stats` counterparts (again, distro parms come first)
wleoncio added a commit that referenced this issue Feb 1, 2024
wleoncio added a commit that referenced this issue Feb 1, 2024
wleoncio added a commit that referenced this issue Feb 1, 2024
In some low-probability cases (`a` and/or `qt` in the tail of the distro), `pbeta()` would give out a warning. The proposed solution avoids this.
wleoncio added a commit that referenced this issue Feb 1, 2024
wleoncio added a commit that referenced this issue Feb 1, 2024
* issue-54:
  Resolved linting
  Improved validation a <= q <= b (#54)
  Worked around extreme cases (#54)
  Added more tests to `ptrunc.beta()` (#54)
  Added validation for `ptrunc.beta()` (#54)
  Fixed docs
  Moved `...` to middle of arg list (#54)
  Isolated repeated calculation of `p` (#54)
  Fixed docs
  Increment version number to 1.1.0.9001
  Implemented `ptrunc.beta()` (#54)
  Restructured `ptrunc()` generic (#54)
  Added comments
wleoncio added a commit that referenced this issue Jul 3, 2024
wleoncio added a commit that referenced this issue Jul 3, 2024
wleoncio added a commit that referenced this issue Jul 3, 2024
* issue-54:
  Increment version number to 1.1.1.9015
  Removed numerical computation of `qtrunc() (#54)`
  Adjusted unit tests (#54)
  Added analytical solution (#54)
@wleoncio
Copy link
Member Author

wleoncio commented Jul 3, 2024

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.

wleoncio added a commit that referenced this issue Jul 3, 2024
wleoncio added a commit that referenced this issue Jul 3, 2024
* issue-54:
  Increment version number to 1.1.1.9016
  Implemented `qtrunc.beta()` (#54)
wleoncio added a commit that referenced this issue Jul 5, 2024
wleoncio added a commit that referenced this issue Jul 5, 2024
wleoncio added a commit that referenced this issue Jul 5, 2024
* issue-54:
  Increment version number to 1.1.1.9017
  Implemented unit tests for `qtrunc.binom()` (#54)
  Fixed unit tests for #54
  Implemented `qtrunc.binom()` (#54)
  Updated RoxygenNote version
wleoncio added a commit that referenced this issue Jul 5, 2024
wleoncio added a commit that referenced this issue Jul 5, 2024
* issue-54:
  Increment version number to 1.1.1.9018
  Implemented `qtrunc.chisq()` (#54)
wleoncio added a commit that referenced this issue Aug 6, 2024
wleoncio added a commit that referenced this issue Aug 6, 2024
* issue-54:
  Increment version number to 1.1.1.9019
  Implemented `qtrunc.contbern()` (#54)
wleoncio added a commit that referenced this issue Aug 13, 2024
wleoncio added a commit that referenced this issue Aug 13, 2024
wleoncio added a commit that referenced this issue Aug 13, 2024
wleoncio added a commit that referenced this issue Aug 13, 2024
wleoncio added a commit that referenced this issue Aug 13, 2024
wleoncio added a commit that referenced this issue Aug 13, 2024
wleoncio added a commit that referenced this issue Aug 13, 2024
wleoncio added a commit that referenced this issue Aug 13, 2024
wleoncio added a commit that referenced this issue Aug 21, 2024
wleoncio added a commit that referenced this issue Aug 21, 2024
* pqtrunc-alias:
  Increment version number to 1.1.1.9022
  Added alias and tests for qtrunc (#54)
  Properly documented ptrunc aliases (#54)
  Added unit tests for ptrunc aliases (#54)
  Added aliases for #54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants