32 #ifndef VSMC_RNG_RANDOM_WALK_HPP 33 #define VSMC_RNG_RANDOM_WALK_HPP 40 #define VSMC_RUNTIME_ASSERT_RNG_RANDOM_WALK_PROPOSAL_PARAM(flag, Name) \ 41 VSMC_RUNTIME_ASSERT( \ 42 (flag), "**" #Name "Proposal** CONSTRUCTED WITH INVALID PARAMETERS") 49 template <
typename RealType, std::
size_t Dim>
59 "**RandomWalk** OBJECT DECLARED WITH DYNAMIC DIMENSION");
66 "**RandomWalk** OBJECT DECLARED WITH FIXED DIMENSION");
69 std::size_t
dim()
const {
return x_.size(); }
97 template <
typename RNGType,
typename LogTargetType,
typename ProposalType>
99 LogTargetType &&log_target, ProposalType &&proposal)
109 std::copy(y_.begin(), y_.end(), x);
126 template <
typename RNGType,
typename LogTargetType,
typename ProposalType>
127 std::size_t
operator()(RNGType &rng, std::size_t m, std::size_t idx,
129 ProposalType &&proposal)
132 std::copy_n(x + idx,
dim(), x_.begin());
134 result_type s = ltx ==
nullptr ? log_target(m, x) : *ltx;
135 std::copy(y_.begin(), y_.end(), x + idx);
145 std::copy(x_.begin(), x_.end(), x + idx);
151 template <
typename RNGType,
typename LogTargetType,
typename ProposalType>
153 result_type *ltx, LogTargetType &&log_target, ProposalType &&proposal)
157 for (std::size_t i = 0; i != n; ++i) {
159 std::forward<LogTargetType>(log_target),
160 std::forward<ProposalType>(proposal));
170 template <
typename RNGType,
typename LogTargetType,
typename ProposalType>
171 std::size_t
operator()(std::size_t n, RNGType &rng, std::size_t m,
173 LogTargetType &&log_target, ProposalType &&proposal)
176 result_type s = ltx ==
nullptr ? log_target(m, x) : *ltx;
177 for (std::size_t i = 0; i != n; ++i) {
179 std::forward<LogTargetType>(log_target),
180 std::forward<ProposalType>(proposal));
195 template <
typename RealType, std::
size_t DimX, std::
size_t DimG>
205 "**RandomWalk** OBJECT DECLARED WITH DYNAMIC DIMENSION");
210 : x_(dim_x), y_(dim_x), g_(dim_g)
213 "**RandomWalk** OBJECT DECLARED WITH FIXED DIMENSION");
216 std::size_t
dim_x()
const {
return x_.size(); }
217 std::size_t
dim_g()
const {
return g_.size(); }
251 template <
typename RNGType,
typename LogTargetType,
typename ProposalType>
253 result_type *g, LogTargetType &&log_target, ProposalType &&proposal)
256 result_type q = proposal(rng, dim_x(), x, y_.data());
258 ltx ==
nullptr ? log_target(dim_x(), dim_g(), x,
nullptr) : *ltx;
260 dim_x(), dim_g(), y_.data(), (g ==
nullptr ?
nullptr : g_.data()));
265 std::copy(y_.begin(), y_.end(), x);
269 std::copy(g_.begin(), g_.end(), g);
284 template <
typename RNGType,
typename LogTargetType,
typename ProposalType>
285 std::size_t
operator()(RNGType &rng, std::size_t m, std::size_t idx,
287 LogTargetType &&log_target, ProposalType &&proposal)
290 std::copy_n(x + idx, dim_x(), x_.begin());
291 result_type q = proposal(rng, dim_x(), x_.data(), y_.data());
293 ltx ==
nullptr ? log_target(m, dim_g(), x,
nullptr) : *ltx;
294 std::copy(y_.begin(), y_.end(), x + idx);
296 log_target(m, dim_g(), x, (g ==
nullptr ?
nullptr : g_.data()));
304 std::copy(g_.begin(), g_.end(), g);
307 std::copy(x_.begin(), x_.end(), x + idx);
313 template <
typename RNGType,
typename LogTargetType,
typename ProposalType>
316 ProposalType &&proposal)
320 ltx ==
nullptr ? log_target(dim_x(), dim_g(), x,
nullptr) : *ltx;
321 for (std::size_t i = 0; i != n; ++i) {
323 std::forward<LogTargetType>(log_target),
324 std::forward<ProposalType>(proposal));
334 template <
typename RNGType,
typename LogTargetType,
typename ProposalType>
335 std::size_t
operator()(std::size_t n, RNGType &rng, std::size_t m,
337 LogTargetType &&log_target, ProposalType &&proposal)
341 ltx ==
nullptr ? log_target(m, dim_g(), x,
nullptr) : *ltx;
342 for (std::size_t i = 0; i != n; ++i) {
344 std::forward<LogTargetType>(log_target),
345 std::forward<ProposalType>(proposal));
362 template <
typename RealType>
368 template <
typename RealType>
370 std::size_t
dim,
const RealType *a,
const RealType *b)
372 for (std::size_t i = 0; i !=
dim; ++i)
378 template <
typename RealType>
386 template <
typename RealType>
394 template <
typename RealType>
402 template <
typename RealType>
404 RealType x, RealType &y, RealType z, RealType a, RealType b)
406 RealType r =
std::exp(z) * (x - a) / (b - x);
407 y = (a + b * r) / (1 + r);
409 return std::log((y - a) / (x - a) * (b - y) / (b - x));
416 template <
typename RealType>
428 result_type a = -std::numeric_limits<result_type>::infinity(),
429 result_type b = std::numeric_limits<result_type>::infinity())
430 : rnorm_(0, stddev), a_(a), b_(b), flag_(0)
432 unsigned lower = std::isfinite(a) ? 1 : 0;
433 unsigned upper = std::isfinite(b) ? 1 : 0;
434 flag_ = (lower << 1) + upper;
443 template <
typename RNGType>
466 template <
typename RealType, std::
size_t Dim>
488 : rnorm_(nullptr, chol)
491 "**NormalMVProposal** OBJECT DECLARED WITH DYNAMIC DIMENSION");
498 : rnorm_(dim, nullptr, chol), a_(dim), b_(dim), z_(dim), flag_(dim)
501 "**NormalMVProposal** OBJECT DECLARED WITH FIXED DIMENSION");
505 std::size_t
dim()
const {
return rnorm_.dim(); }
509 template <
typename RNGType>
513 rnorm_(rng, z_.data());
515 for (std::size_t i = 0; i !=
dim(); ++i) {
530 x[i], y[i], z_[i], a_[i], b_[i]);
549 std::fill(a_.begin(), a_.end(),
550 -std::numeric_limits<result_type>::infinity());
552 std::copy_n(a, dim, a_.begin());
556 std::fill(b_.begin(), b_.end(),
557 std::numeric_limits<result_type>::infinity());
559 std::copy_n(b, dim, b_.begin());
562 for (std::size_t i = 0; i !=
dim; ++i) {
563 unsigned lower = std::isfinite(a_[i]) ? 1 : 0;
564 unsigned upper = std::isfinite(b_[i]) ? 1 : 0;
565 flag_[i] = (lower << 1) + upper;
570 dim, a_.data(), b_.data()),
577 #endif // VSMC_RNG_RANDOM_WALK_HPP
#define VSMC_RUNTIME_ASSERT_RNG_RANDOM_WALK_PROPOSAL_PARAM(flag, Name)
bool normal_mv_proposal_check_param(std::size_t dim, const RealType *a, const RealType *b)
RealType normal_proposal_qa(RealType x, RealType &y, RealType z, RealType a)
NormalMVProposal(std::size_t dim, const result_type *chol=nullptr, const result_type *a=nullptr, const result_type *b=nullptr)
std::size_t operator()(std::size_t n, RNGType &rng, std::size_t m, std::size_t idx, result_type *x, result_type *ltx, result_type *g, LogTargetType &&log_target, ProposalType &&proposal)
Multi-step random walk update of a block of element within a vector state.
RealType normal_proposal_qb(RealType x, RealType &y, RealType z, RealType b)
RandomWalkG(std::size_t dim_x, std::size_t dim_g)
Only usable when DimX == Dynamic and DimG == Dynamic
std::size_t operator()(std::size_t n, RNGType &rng, std::size_t m, std::size_t idx, result_type *x, result_type *ltx, LogTargetType &&log_target, ProposalType &&proposal)
Multi-step random walk update of a block of element within a vector state.
std::size_t operator()(std::size_t n, RNGType &rng, result_type *x, result_type *ltx, LogTargetType &&log_target, ProposalType &&proposal)
Multi-step random walk update.
Random walk MCMC update with test function.
RealType normal_proposal_q(RealType x, RealType &y, RealType z)
std::size_t dim_g() const
std::size_t dim_x() const
const result_type * b() const
std::size_t operator()(RNGType &rng, result_type *x, result_type *ltx, result_type *g, LogTargetType &&log_target, ProposalType &&proposal)
One-step random walk update.
void exp(std::size_t n, const float *a, float *y)
RealType normal_proposal_qab(RealType x, RealType &y, RealType z, RealType a, RealType b)
RandomWalk()
Only usable when Dim != Dynamic
result_type operator()(RNGType &rng, std::size_t, const result_type *x, result_type *y)
NormalProposal(result_type stddev=1, result_type a=-std::numeric_limits< result_type >::infinity(), result_type b=std::numeric_limits< result_type >::infinity())
Construct a Normal random walk proposal.
Multivariate Normal distribution.
result_type operator()(RNGType &rng, std::size_t, const result_type *x, result_type *y)
Propose new value y and return .
bool normal_proposal_check_param(RealType a, RealType b)
std::size_t operator()(RNGType &rng, std::size_t m, std::size_t idx, result_type *x, result_type *ltx, result_type *g, LogTargetType &&log_target, ProposalType &&proposal)
One-step random walk update of a block of elements within a vector state.
Normal random walk proposal.
typename std::conditional< Dim==Dynamic, Vector< T >, std::array< T, Dim >>::type Array
std::size_t operator()(std::size_t n, RNGType &rng, result_type *x, result_type *ltx, result_type *g, LogTargetType &&log_target, ProposalType &&proposal)
Multi-step random walk update.
const result_type * a() const
std::size_t operator()(RNGType &rng, result_type *x, result_type *ltx, LogTargetType &&log_target, ProposalType &&proposal)
One-step random walk update.
RandomWalk(std::size_t dim)
Only usable when Dim == Dynamic
NormalMVProposal(const result_type *chol=nullptr, const result_type *a=nullptr, const result_type *b=nullptr)
Only usable when Dim != Dynamic
Standard uniform distribution.
Multivariate Normal random walk proposal.
void log(std::size_t n, const float *a, float *y)
std::size_t operator()(RNGType &rng, std::size_t m, std::size_t idx, result_type *x, result_type *ltx, LogTargetType &&log_target, ProposalType &&proposal)
One-step random walk update of a block of elements within a vector state.
RandomWalkG()
Only usable when DimX != Dynamic and DimG != Dynamic