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 truncation arguments to univariate distributions #46

Open
joethorley opened this issue Feb 1, 2023 · 6 comments
Open

Add truncation arguments to univariate distributions #46

joethorley opened this issue Feb 1, 2023 · 6 comments

Comments

@joethorley
Copy link
Member

log_lik_norm(x, mean, sd, tlower = -Inf, tupper = -Inf)
res_norm(x, mean, sd, tlower = -Inf, tupper = -Inf)
ran_norm(x, mean, sd, tlower = -Inf, tupper = -Inf)

if outside limits then log_lik = -Inf
res I guess is -Inf or Inf
and ran_norm should resample.

@joethorley
Copy link
Member Author

the ran_norm might be problematic if narrow range cos either very few not NA or repeats many times.

@joethorley
Copy link
Member Author

This is a stab at one

ran_student2 <- function(n = 1, mean = 0, sd = 1, theta = 0, tlower = -Inf, tupper = Inf) {
  chk_number(tlower)
  chk_number(tupper)
  chk_lt(tlower, tupper)

  ran <- extras::ran_student(n, mean, sd, theta = theta)
  ran <- ran[ran > tlower & ran < tupper]
  nran <- length(ran)

  if(nran == n) {
    return(ran)
  }

  ran %>%
    c(ran_student2(n - nran, mean = mean, sd = sd, theta = theta, tlower, tupper))
}

@joethorley
Copy link
Member Author

Might not need to check proportion of ran as just get stack overflow.
Or perhaps check to give better error message.

@nehill197
Copy link
Member

  • This issue is more complex than we initially thought.
  • The log likelihoods need to incorporate the additional density from the truncated sections such the area under the PDF sums to 1.
  • This means we need either distribution specific truncated PDFs or perhaps a general solution to incorporate the truncated density.
  • for Normal dist. see wikipedia page for PDF; msm package has dtnorm() and rtnorm() for comparison.

@joethorley
Copy link
Member Author

Yes I think we are going to need to use the equivalent of pnorm and qnorm for each of our functions.

I guess we could call them something like prob_norm and quant_norm(). These should also have tlower and tupper arguments.

@nehill197
Copy link
Member

Potentially useful information in the stan reference manual section 7.4
https://mc-stan.org/docs/reference-manual/sampling-statements.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants