Skip to content

Commit

Permalink
setup negative binomial
Browse files Browse the repository at this point in the history
  • Loading branch information
edknock committed Oct 17, 2024
1 parent 75396f4 commit 4a30802
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: monty
Title: Monte Carlo Models
Version: 0.2.19
Version: 0.2.20
Authors@R: c(person("Rich", "FitzJohn", role = c("aut", "cre"),
email = "[email protected]"),
person("Wes", "Hinsley", role = "aut"),
Expand Down
30 changes: 30 additions & 0 deletions R/dsl-distributions.R
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,34 @@ distr_hypergeometric <- distribution(
mean = quote(k * n1 / (n1 + n2))),
cpp = list(density = "hypergeometric", sample = "hypergeometric"))

distr_negative_binomial_mu <- distribution(
name = "NegativeBinomial",
variant = "mu",
density = function(x, size, mu) {
dnbinom(x, size, mu = mu, log = TRUE)
},
domain = c(0, Inf),
sample = function(rng, size, mu) rng$negative_binomial_mu(1, size, mu),
expr = list(
density = quote(lchoose(x + size - 1, x) + size * log(size) +
x * log(mu) - (size + x) * log(size + mu)),
mean = quote(mu)),
cpp = list(density = "negative_binomial_mu", sample = "negative_binomial_mu"))

distr_negative_binomial_prob <- distribution(
name = "NegativeBinomial",
variant = "prob",
density = function(x, size, prob) {
dnbinom(x, size, prob = prob, log = TRUE)
},
domain = c(0, Inf),
sample = function(rng, size, prob) rng$negative_binomial_prob(1, size, prob),
expr = list(
density = quote(lchoose(x + size - 1, x) + x * log(1 - prob) +
size * log(prob)),
mean = quote(size * (1 - prob) / prob)),
cpp = list(density = "negative_binomial_prob", sample = "negative_binomial_prob"))

distr_normal <- distribution(
name = "Normal",
density = function(x, mean, sd) dnorm(x, mean, sd, log = TRUE),
Expand Down Expand Up @@ -166,6 +194,8 @@ dsl_distributions <- local({
distr_gamma_rate,
distr_gamma_scale,
distr_hypergeometric,
distr_negative_binomial_mu,
distr_negative_binomial_prob,
distr_normal,
distr_poisson,
distr_uniform)
Expand Down

0 comments on commit 4a30802

Please sign in to comment.