-
-
Notifications
You must be signed in to change notification settings - Fork 192
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
Create native Dirichlet multinomial family #1729
base: master
Are you sure you want to change the base?
Changes from all commits
e12a8d6
031980e
85bd4fb
4194f03
04874c4
4ade4f0
3b99c9d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -837,6 +837,17 @@ log_lik_multinomial <- function(i, prep) { | |
log_lik_weight(out, i = i, prep = prep) | ||
} | ||
|
||
log_lik_dirichlet_multinomial <- function(i, prep) { | ||
stopifnot(prep$family$link == "logit") | ||
eta <- get_Mu(prep, i = i) | ||
eta <- insert_refcat(eta, refcat = prep$refcat) | ||
phi <- get_dpar(prep, "phi", i = i) | ||
cats <- seq_len(prep$data$ncat) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the variables cats and alpha seem redundant here. Leftovers from previous versions? |
||
alpha <- dcategorical(cats, eta = eta) * phi | ||
out <- ddirichletmultinomial(prep$data$Y[i, ], eta = eta, phi = phi, log = TRUE) | ||
log_lik_weight(out, i = i, prep = prep) | ||
} | ||
|
||
log_lik_dirichlet <- function(i, prep) { | ||
stopifnot(prep$family$link == "logit") | ||
eta <- get_Mu(prep, i = i) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -587,6 +587,21 @@ posterior_epred_multinomial <- function(prep) { | |
out | ||
} | ||
|
||
posterior_epred_dirichlet_multinomial <- function(prep) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since this is now the same as the one for multinomial (right?), perhaps it makes sense to just call it directly with a comment saying why this is valid? |
||
get_counts <- function(i) { | ||
eta <- insert_refcat(slice_col(eta, i), refcat = prep$refcat) | ||
dcategorical(cats, eta = eta) * trials[i] | ||
} | ||
# dirichlet part included in mu | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't understand this comment |
||
eta <- get_Mu(prep) | ||
cats <- seq_len(prep$data$ncat) | ||
trials <- prep$data$trials | ||
out <- abind(lapply(seq_cols(eta), get_counts), along = 3) | ||
out <- aperm(out, perm = c(1, 3, 2)) | ||
dimnames(out)[[3]] <- prep$cats | ||
out | ||
} | ||
|
||
posterior_epred_dirichlet <- function(prep) { | ||
get_probs <- function(i) { | ||
eta <- insert_refcat(slice_col(eta, i), refcat = prep$refcat) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/* dirichlet-multinomial-logit log-PMF | ||
* Args: | ||
* y: array of integer response values | ||
* mu: vector of category logit probabilities | ||
* phi: precision parameter (sum of Dirichlet alphas) | ||
* Returns: | ||
* a scalar to be added to the log posterior | ||
*/ | ||
real dirichlet_multinomial_logit2_lpmf(array[] int y, vector mu, real phi) { | ||
// get Dirichlet alphas | ||
int N = num_elements(mu); | ||
vector[N] alpha = phi * softmax(mu); | ||
|
||
// get trials from y | ||
real T = sum(y); | ||
|
||
return lgamma(phi) + lgamma(T + 1.0) - lgamma(T + phi) + | ||
sum(lgamma(to_vector(y) + alpha)) - sum(lgamma(alpha)) - sum(lgamma(to_vector(y) + 1)); | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
does this accept the same kinds of inputs as brms::dmultinomial (just with an additional phi?). E.g. is the right dimensionality for x and eta taken care of?