stan-dev / math

The Stan Math Library is a C++ template library for automatic differentiation of any order using forward, reverse, and mixed modes. It includes a range of built-in functions for probabilistic modeling, linear algebra, and equation solving.
https://mc-stan.org
BSD 3-Clause "New" or "Revised" License
723 stars 183 forks source link

bernoulli_rng - add signature `ints bernoulli_rng(real theta)` #3044

Open mitzimorris opened 2 months ago

mitzimorris commented 2 months ago

Description

Add implementation of bernoulli_rng which generates an array of bernoulli outcomes for a single value of theta.

Example

Currently, it is necessary to use a loop, e.g.

  real theta = beta_rng(1, 1);
  array[N] int y; 
  for (n in 1:N) {
    y[n] = bernoulli_rng(theta);
  }

The vectorized alternative is:

  real theta = beta_rng(1, 1);
  array[N] int y; 
   y = bernoulli_rng(rep_array(theta, N_obs));

This is problematic for 2 reasons:

Expected Output

It would be nice to be able to do the following:

  real theta = beta_rng(1, 1);
  array[N] int y; 
   y = bernoulli_rng(rtheta);

Current Version:

v4.8.1