-
-
Notifications
You must be signed in to change notification settings - Fork 187
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
Vectorized unbounded continuous random number generators (Issue 45) #622
Merged
seantalts
merged 17 commits into
stan-dev:develop
from
bbbales2:feature/issue-45-vectorized_rngs_unbounded_continuous
Nov 16, 2017
Merged
Changes from 16 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
72eb950
Changed vector_rng_test_helper tools to work with anywhere between 1-…
bbbales2 fa7187b
Changed int to size_t to avoid signed comparison warning (Issue 45)
bbbales2 270c96a
Merge remote-tracking branch 'stan/develop' into feature/issue-45-vec…
bbbales2 9a85e8c
Merge remote-tracking branch 'stan/develop' into feature/issue-45-vec…
bbbales2 ba6c624
Moved around & symbols and if statements. Avoided cast w/ templating
bbbales2 275889c
This commit addresses Bob's comments (from like September 30 or somet…
bbbales2 d2281d1
Added int and std::vector<int> tests (Issue 45)
bbbales2 427d316
Merge branch 'develop' into feature/issue-45-vectorized_rngs_unbounde…
mitzimorris 8e83e4f
Cleaned up spacing, changed docs to make interface clearer (Issue 45)
bbbales2 ee338ff
Fixed cpplint issues in test (Issue 45)
bbbales2 25daa7c
Merge remote-tracking branch 'stan/develop' into feature/issue-45-vec…
bbbales2 9bf8a0e
Swapped std::log for log in double_exponential test to get things wor…
bbbales2 aa207eb
Made a couple fixes to get Jenkins tests to pass (errors not reproduc…
bbbales2 4517668
Fix cpplint (Issue 45)
bbbales2 eff2de9
Fixed references to std::log and std::abs in double_exponential_rng. …
bbbales2 ddb60d9
Merge remote-tracking branch 'stan/develop' into feature/issue-45-vec…
bbbales2 47cdfc9
Moved around variable declarations below argument checks (Issue 45)
bbbales2 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,50 +1,60 @@ | ||
#ifndef STAN_MATH_PRIM_SCAL_PROB_CAUCHY_RNG_HPP | ||
#define STAN_MATH_PRIM_SCAL_PROB_CAUCHY_RNG_HPP | ||
|
||
#include <boost/random/cauchy_distribution.hpp> | ||
#include <boost/random/variate_generator.hpp> | ||
#include <stan/math/prim/scal/err/check_consistent_sizes.hpp> | ||
#include <stan/math/prim/scal/err/check_finite.hpp> | ||
#include <stan/math/prim/scal/err/check_not_nan.hpp> | ||
#include <stan/math/prim/scal/err/check_positive_finite.hpp> | ||
#include <stan/math/prim/scal/fun/constants.hpp> | ||
#include <stan/math/prim/scal/fun/log1p.hpp> | ||
#include <stan/math/prim/scal/fun/square.hpp> | ||
#include <stan/math/prim/scal/fun/value_of.hpp> | ||
#include <stan/math/prim/scal/meta/include_summand.hpp> | ||
#include <stan/math/prim/scal/meta/max_size.hpp> | ||
#include <stan/math/prim/scal/meta/scalar_seq_view.hpp> | ||
#include <stan/math/prim/scal/meta/VectorBuilder.hpp> | ||
#include <boost/random/cauchy_distribution.hpp> | ||
#include <boost/random/variate_generator.hpp> | ||
|
||
namespace stan { | ||
namespace math { | ||
|
||
/** | ||
* Return a pseudorandom Cauchy variate for the given location and scale | ||
* using the specified random number generator. | ||
* | ||
* mu and sigma can each be a scalar, a std::vector, an Eigen::Vector, or | ||
* an Eigen::RowVector. Any non-scalar inputs must be the same length. | ||
* | ||
* @tparam T_loc Type of location parameter | ||
* @tparam T_scale Type of scale parameter | ||
* @tparam RNG type of random number generator | ||
* @param mu location parameter | ||
* @param sigma positive scale parameter | ||
* @param mu (Sequence of) location parameter(s) | ||
* @param sigma (Sequence of) scale parameter(s) | ||
* @param rng random number generator | ||
* @return Cauchy random variate | ||
* @throw std::domain_error if mu is infinite or sigma is nonpositive | ||
* @throw std::invalid_argument if non-scalars arguments are of different | ||
* lengths | ||
*/ | ||
template <class RNG> | ||
inline double | ||
cauchy_rng(double mu, | ||
double sigma, | ||
RNG& rng) { | ||
template <typename T_loc, typename T_scale, class RNG> | ||
inline typename VectorBuilder<true, double, T_loc, T_scale>::type | ||
cauchy_rng(const T_loc& mu, const T_scale& sigma, RNG& rng) { | ||
using boost::variate_generator; | ||
using boost::random::cauchy_distribution; | ||
|
||
static const char* function = "cauchy_rng"; | ||
|
||
scalar_seq_view<T_loc> mu_vec(mu); | ||
scalar_seq_view<T_scale> sigma_vec(sigma); | ||
size_t N = max_size(mu, sigma); | ||
VectorBuilder<true, double, T_loc, T_scale> output(N); | ||
|
||
check_finite(function, "Location parameter", mu); | ||
check_positive_finite(function, "Scale parameter", sigma); | ||
check_consistent_sizes(function, "Location parameter", mu, | ||
"Scale Parameter", sigma); | ||
|
||
variate_generator<RNG&, cauchy_distribution<> > | ||
cauchy_rng(rng, cauchy_distribution<>(mu, sigma)); | ||
return cauchy_rng(); | ||
} | ||
for (size_t n = 0; n < N; ++n) { | ||
variate_generator<RNG&, cauchy_distribution<> > | ||
cauchy_rng(rng, cauchy_distribution<>(mu_vec[n], sigma_vec[n])); | ||
output[n] = cauchy_rng(); | ||
} | ||
|
||
return output.data(); | ||
} | ||
} | ||
} | ||
#endif |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Checks should go first before any work. In general, try to put exceptional/odd/edge-case branches first so that when you're by them, you can scan the main branch of the program together. Would you mind fixing this on all of these? Sorry for not catchign that earlier.