-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #87 from mrc-ide/negative-binomial
Add Negative Binomial distribution
- Loading branch information
Showing
11 changed files
with
244 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"), | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
#pragma once | ||
|
||
#include <cmath> | ||
|
||
#include "monty/random/gamma.hpp" | ||
#include "monty/random/poisson.hpp" | ||
#include "monty/random/generator.hpp" | ||
|
||
namespace monty { | ||
namespace random { | ||
|
||
namespace { | ||
|
||
template <typename real_type> | ||
void negative_binomial_validate(real_type size, real_type prob) { | ||
if(!R_FINITE(size) || !R_FINITE(prob) || size <= 0 || prob <= 0 || prob > 1) { | ||
char buffer[256]; | ||
snprintf(buffer, 256, | ||
"Invalid call to negative_binomial with size = %g, prob = %g", | ||
size, prob); | ||
monty::utils::fatal_error(buffer); | ||
} | ||
} | ||
|
||
} | ||
|
||
template <typename real_type, typename rng_state_type> | ||
real_type negative_binomial_prob(rng_state_type& rng_state, real_type size, real_type prob) { | ||
#ifdef __CUDA_ARCH__ | ||
static_assert("negative_binomial_prob() not implemented for GPU targets"); | ||
#endif | ||
negative_binomial_validate(size, prob); | ||
|
||
if (rng_state.deterministic) { | ||
return (1 - prob) * size / prob; | ||
} | ||
return (prob == 1) ? 0 : poisson(rng_state, gamma_scale(rng_state, size, (1 - prob) / prob)); | ||
} | ||
|
||
template <typename real_type, typename rng_state_type> | ||
real_type negative_binomial_mu(rng_state_type& rng_state, real_type size, real_type mu) { | ||
#ifdef __CUDA_ARCH__ | ||
static_assert("negative_binomial_mu() not implemented for GPU targets"); | ||
#endif | ||
const auto prob = size / (size + mu); | ||
negative_binomial_validate(size, prob); | ||
|
||
return negative_binomial_prob(rng_state, size, prob); | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.