<commit_before>//============================================================================
// vSMC/include/vsmc/rng/random_walk.hpp
//----------------------------------------------------------------------------
//                         vSMC: Scalable Monte Carlo
//----------------------------------------------------------------------------
// Copyright (c) 2013-2016, <NAME>
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
//   Redistributions of source code must retain the above copyright notice,
//   this list of conditions and the following disclaimer.
//
//   Redistributions in binary form must reproduce the above copyright notice,
//   this list of conditions and the following disclaimer in the documentation
//   and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS AS IS
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
//============================================================================

#ifndef VSMC_RNG_RANDOM_WALK_HPP
#define VSMC_RNG_RANDOM_WALK_HPP

#include <vsmc/rng/internal/common.hpp>
#include <vsmc/rng/normal_distribution.hpp>
#include <vsmc/rng/normal_mv_distribution.hpp>
#include <vsmc/rng/u01_distribution.hpp>

#define VSMC_RUNTIME_ASSERT_RNG_RANDOM_WALK_PARAM(flag, Name)                 \
    VSMC_RUNTIME_ASSERT(                                                      \
        (flag), "**RandomWal" #Name "** CONSTRUCTED WITH INVALID PARAMETERS")

namespace vsmc
{

namespace internal
{

template <typename RealType>
inline bool random_walk_normal_check_param(
    RealType stddev, RealType a, RealType b)
{
    return (stddev > 0) && (a < b);
}

template <typename RealType>
inline bool random_walk_normal_mv_check_param(std::size_t dim,
    const RealType *, const std::size_t *a, const std::size_t *b)
{
    for (std::size_t i = 0; i != dim; ++i)
        if (a[i] > b[i])
            return false;
    return true;
}

template <typename RealType>
RealType random_walk_normal_q0(RealType x, RealType &y, RealType z)
{
    y = x + z;

    return 0;
}

template <typename RealType>
RealType random_walk_normal_qa(RealType x, RealType &y, RealType z, RealType a)
{
    y = a + (x - a) * std::exp(z);

    return z;
}

template <typename RealType>
RealType random_walk_normal_qb(RealType x, RealType &y, RealType z, RealType b)
{
    y = b - (b - x) * std::exp(z);

    return z;
}

template <typename RealType>
RealType random_walk_normal_qab(
    RealType x, RealType &y, RealType z, RealType a, RealType b)
{
    RealType r = std::exp(z) * (x - a) / (b - x);
    y = (a + b * r) / (1 + r);

    return std::log((y - a) / (x - a) * (b - y) / (b - x));
}

} // namespace vsmc::internal

/// \brief Random walk MCMC
/// \ingroup RandomWalk
template <typename RealType>
class RandomWalkMCMC
{
    public:
    using result_type = RealType;

    template <typename RNGType, typename LogTargetType,
        typename RandomWalkType>
    result_type operator()(RNGType &rng, result_type x,
        LogTargetType &&log_target, RandomWalkType &&random_walk)
    {
        result_type y = 0;
        result_type q = random_walk(rng, x, y);
        result_type p = log_target(y) - log_target(x) + q;
        result_type u = std::log(runif_(rng));

        return u < p ? y : x;
    }

    template <typename RNGType, typename LogTargetType,
        typename RandomWalkType>
    void operator()(RNGType &rng, std::size_t dim, const result_type *x,
        result_type *y, LogTargetType &&log_target,
        RandomWalkType &&random_walk)
    {
        Vector<result_type> r(dim);
        result_type q = random_walk(rng, dim, x, r.data());
        result_type p = log_target(dim, r.data()) - log_target(dim, x) + q;
        result_type u = std::log(runif_(rng));
        if (u < p)
            std::copy(r.begin(), r.end(), y);
        else
            std::copy_n(x, dim, y);
    }

    private:<commit_msg>return acceptance from random walk mcmc<commit_after>//============================================================================
// vSMC/include/vsmc/rng/random_walk.hpp
//----------------------------------------------------------------------------
//                         vSMC: Scalable Monte Carlo
//----------------------------------------------------------------------------
// Copyright (c) 2013-2016, <NAME>
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
//   Redistributions of source code must retain the above copyright notice,
//   this list of conditions and the following disclaimer.
//
//   Redistributions in binary form must reproduce the above copyright notice,
//   this list of conditions and the following disclaimer in the documentation
//   and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS AS IS
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
//============================================================================

#ifndef VSMC_RNG_RANDOM_WALK_HPP
#define VSMC_RNG_RANDOM_WALK_HPP

#include <vsmc/rng/internal/common.hpp>
#include <vsmc/rng/normal_distribution.hpp>
#include <vsmc/rng/normal_mv_distribution.hpp>
#include <vsmc/rng/u01_distribution.hpp>

#define VSMC_RUNTIME_ASSERT_RNG_RANDOM_WALK_PARAM(flag, Name)                 \
    VSMC_RUNTIME_ASSERT(                                                      \
        (flag), "**RandomWal" #Name "** CONSTRUCTED WITH INVALID PARAMETERS")

namespace vsmc
{

namespace internal
{

template <typename RealType>
inline bool random_walk_normal_check_param(
    RealType stddev, RealType a, RealType b)
{
    return (stddev > 0) && (a < b);
}

template <typename RealType>
inline bool random_walk_normal_mv_check_param(std::size_t dim,
    const RealType *, const std::size_t *a, const std::size_t *b)
{
    for (std::size_t i = 0; i != dim; ++i)
        if (a[i] > b[i])
            return false;
    return true;
}

template <typename RealType>
RealType random_walk_normal_q0(RealType x, RealType &y, RealType z)
{
    y = x + z;

    return 0;
}

template <typename RealType>
RealType random_walk_normal_qa(RealType x, RealType &y, RealType z, RealType a)
{
    y = a + (x - a) * std::exp(z);

    return z;
}

template <typename RealType>
RealType random_walk_normal_qb(RealType x, RealType &y, RealType z, RealType b)
{
    y = b - (b - x) * std::exp(z);

    return z;
}

template <typename RealType>
RealType random_walk_normal_qab(
    RealType x, RealType &y, RealType z, RealType a, RealType b)
{
    RealType r = std::exp(z) * (x - a) / (b - x);
    y = (a + b * r) / (1 + r);

    return std::log((y - a) / (x - a) * (b - y) / (b - x));
}

} // namespace vsmc::internal

/// \brief Random walk MCMC
/// \ingroup RandomWalk
template <typename RealType>
class RandomWalkMCMC
{
    public:
    using result_type = RealType;

    template <typename RNGType, typename LogTargetType,
        typename RandomWalkType>
    bool operator()(RNGType &rng, result_type x, result_type &y,
        LogTargetType &&log_target, RandomWalkType &&random_walk)
    {
        result_type r = 0;
        result_type q = random_walk(rng, x, r);
        result_type p = log_target(r) - log_target(x) + q;
        result_type u = std::log(runif_(rng));

        if (u < p) {
            y = r;
            return true;
        } else {
            y = x;
            return false;
        }
    }

    template <typename RNGType, typename LogTargetType,
        typename RandomWalkType>
    bool operator()(RNGType &rng, std::size_t dim, const result_type *x,
        result_type *y, LogTargetType &&log_target,
        RandomWalkType &&random_walk)
    {
        Vector<result_type> r(dim);
        result_type q = random_walk(rng, dim, x, r.data());
        result_type p = log_target(dim, r.data()) - log_target(dim, x) + q;
        result_type u = std::log(runif_(rng));

        if (u < p) {
            std::copy(r.begin(), r.end(), y);
            return true;
        } else {
            std::copy_n(x, dim, y);
            return false;
        }
    }

    private:
{'fraction_non_alphanumeric': 0.16303445413034454, 'fraction_numerical': 0.0033208800332088003, 'mean_word_length': 2.6810542398777693, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5427731', 'n_tokens_mistral': 3015, 'n_tokens_neox': 2855, 'n_words': 943}