Skip to content

Commit

Permalink
used clang-format to properly format the added files
Browse files Browse the repository at this point in the history
  • Loading branch information
chvandorp committed Nov 29, 2023
1 parent ae20c02 commit 28a29cb
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 18 deletions.
23 changes: 12 additions & 11 deletions stan/math/prim/prob/dirichlet_multinomial_lpmf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
#include <stan/math/prim/fun/to_ref.hpp>
#include <vector>


namespace stan {
namespace math {

Expand All @@ -17,29 +16,29 @@ namespace math {
* sizes, \f$\alpha\f$.
* Each element of \f$\alpha\f$ must be greater than 0.
* Each element of \f$n\f$ must be greater than or equal to 0.
*
*
* Suppose that \f$n = (n_1, \ldots, n_K)\f$ has a Dirichlet-multinomial
* distribution with prior sample size
* \f$\alpha = (\alpha_1,\ldots,\alpha_K)\f$ (also called the intensity):
*
* \f[
* (n_1,\ldots,n_K)\sim\mbox{DirMult}(\alpha_1,\ldots,\alpha_K)
* \f]
*
* Write \f$N = n_1 + \cdots + n_K\f$ and
*
* Write \f$N = n_1 + \cdots + n_K\f$ and
* \f$\alpha_0 = \alpha_1 + \cdots + \alpha_K\f$,
* then the log probability mass function is given by
*
*
* \f[
* \log(p(n_1, \ldots, n_K\,|\,\alpha_1,\ldots,\alpha_K))=\log\left(
* \frac{\Gamma(\alpha_0)\Gamma(N+1)}{\Gamma(\alpha_0 + N)}
* \prod_{k=1}^K \frac{\Gamma(n_k + \alpha_k)}{\Gamma(\alpha_k)
* \prod_{k=1}^K \frac{\Gamma(n_k + \alpha_k)}{\Gamma(\alpha_k)
* \Gamma(n_k+1)} \right)\\
* = \log(N) + \log(B(\alpha_0, N)) -
* = \log(N) + \log(B(\alpha_0, N)) -
* \sum_{k : n_k > 0} \bigl(\log(n_k) + \log(B(\alpha_k, n_k))\bigr)
* \f]
*
* The second identity is only valid for \f$N > 0\f$. For \f$N=0\f$,
*
* The second identity is only valid for \f$N > 0\f$. For \f$N=0\f$,
* we have \f$\log(p(n\,|\,\alpha)) = 0\f$.
*
* @tparam T_prior_size type of prior sample sizes
Expand All @@ -64,7 +63,8 @@ return_type_t<T_prior_size> dirichlet_multinomial_lpmf(

return_type_t<T_prior_size> lp(0.0);

if (!include_summand<propto, T_prior_size>::value) return lp;
if (!include_summand<propto, T_prior_size>::value)
return lp;

int sum_ns = 0;

Expand All @@ -75,7 +75,8 @@ return_type_t<T_prior_size> dirichlet_multinomial_lpmf(
}
}

if (sum_ns == 0) return lp;
if (sum_ns == 0)
return lp;

value_type_t<T_prior_size> sum_alpha = alpha_ref.sum();

Expand Down
8 changes: 4 additions & 4 deletions stan/math/prim/prob/dirichlet_multinomial_rng.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ namespace math {
/** \ingroup multivar_dists
* Return a draw from a Dirichlet-Multinomial distribution with specified
* parameters \f$\alpha\f$ and \f$N\f$ and pseudo-random number generator rng.
*
*
* The Dirichlet-Multinomial distribution is a continuous mixture of
* Multinomial distirbutions, where the mixing distribution is the Dirichlet
* distribution. This fact is used for generating DirMult random draws.
* First, we sample a probability vector
* First, we sample a probability vector
* \f$\theta \sim \mbox{Dirichlet}(\alpha)\f$.
* Then, we sample a \f$n \sim \mbox{Multinomial}(\theta, N)\f$,
* Then, we sample a \f$n \sim \mbox{Multinomial}(\theta, N)\f$,
* and return \f$n\f$.
*
*
* @tparam RNG type of pseudo-random number generator
* @param alpha Prior sample sizes (or intensity vector).
* @param N Number of trials.
Expand Down
7 changes: 4 additions & 3 deletions test/unit/math/prim/prob/dirichlet_multinomial_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,13 @@ TEST(ProbDistributionsDirichletMultinomial, rng) {

// PRNG samples should sum to n, and each element is non-negative
EXPECT_EQ(std::accumulate(a.begin(), a.end(), 0), n);
for (int k : a) EXPECT_GE(k, 0);
for (int k : a)
EXPECT_GE(k, 0);

// if n is zero, then each element of the sample is zero
std::vector<int> b = dirichlet_multinomial_rng(theta, 0, rng);
for (int k : b) EXPECT_EQ(k, 0);
for (int k : b)
EXPECT_EQ(k, 0);
}

TEST(ProbDistributionsDirichletMultinomial, rngError) {
Expand Down Expand Up @@ -164,7 +166,6 @@ TEST(ProbDistributionsDirichletMultinomial, chiSquareGoodnessFitTest) {
EXPECT_TRUE(chi < quantile(complement(mydist, 1e-6)));
}


TEST(ProbDistributionsDirichletMultinomial, equivBetaBinomial) {
using Eigen::Dynamic;
using Eigen::Matrix;
Expand Down

0 comments on commit 28a29cb

Please sign in to comment.