【问题标题】:Behavior of C++'s std::discrete_distribution vs. sample() in RR 中 C++ 的 std::discrete_distribution 与 sample() 的行为
【发布时间】:2018-10-13 01:35:14
【问题描述】:

我想从一组频率中提取一个数字,直到所有值都为零。通过绘制剩余数字的频率正在减少。在 R 中有一个“简单”的函数示例可以为我做到这一点。在 C++ 中,我找到了 std::discrete_distribution 但没有太多关于它的详细信息

  1. 我找不到 R 和/或 std::discrete_distribution 算法的明确描述来比较功能。行为是否相同?我可以用这个 C++ STDL 解决我的任务吗?
  2. 当我编写其他 PRNG 的非 STDL 时 - 需要它们的哪些功能,以便它们可以与 std::discrete_distribution 一起使用 - cppreference 和其他网站/论坛对此非常沉默
  3. 在我的代码中的第二个位置,我需要一个类似的行为(每次绘制的固定频率)我发现没有明确的额外调用或参数来分离这两个功能。我需要创建一个降低参数的新实例吗?!这显然是我无法相信的事情......

实际上,我正在对频率求和(由 std::vector 保存)直到随机数更小或相等。原来是这样的:

  • 时间/资源无效
  • 我不确定这在统计上是否允许

希望在您的帮助下找到更好的解决方案。

当然感谢您的时间、支持和回答 :)

【问题讨论】:

  • 你能举一个具体的例子来说明你正在尝试做什么吗?我完全不确定我是否理解您目前的问题。如果我理解的话,我想知道你为什么不只是洗牌然后遍历洗牌的集合。
  • 你检查过这个:en.cppreference.com/w/cpp/numeric/random/discrete_distribution 吗?你缺少什么?
  • 这是(草案)标准对这个函数的描述:eel.is/c++draft/rand.dist.samp.discrete
  • 关于第 2 点,discrete_distribution 的 operator() 上的 cppreference 页面说明了 RNG 必须做什么(带有可点击的链接)
  • 你好,当然!;) 对于第一种情况(绘图而不放回),这是一个选项 - 转向哪个算法它被洗牌,结果可能不如从随机性中绘图好。对于从概率放回的第二种情况,没有其他机会(据我所知)从分布中提取。有关 std::discrete_distribution 功能的主要问题 - 例如功能是从 Prng 内部调用的,所以我的 Prng 是强制性的。这东西是如何工作的?我试图在 stdl 中使用,但据我所知,这不仅仅是神秘的

标签: c++ random statistics std distribution


【解决方案1】:

C++17 为我们提供了std::sample 函数:

#include <iostream>
#include <random>
#include <string>
#include <iterator>
#include <algorithm>

int main()
{
    std::string in = "abcdefgh";
    std::string out;

    std::sample(in.begin(), in.end(), 
                std::back_inserter(out),
                5,
                std::mt19937{std::random_device{}()} //(*)
                );

    std::cout << "five random letters out of " << in << " : " << out << '\n';
}

如果您没有 C++17,那么提案 A sample Proposal, v4 包含一个示例实现(使用 uniform_int_distribution):

// the primary function, which calls the detailed functions depending on types of iterators in the used containers.
template< class PopIter, class SampleIter, class Size, class URNG >
SampleIter sample( PopIter first, PopIter last, SampleIter out, Size n, URNG&& g )
{
    using pop_t  = typename std::iterator_traits< PopIter >::iterator_category;
    using samp_t = typename std::iterator_traits< SampleIter >::iterator_category;
    return __sample( first, last, pop_t{}, out, samp_t{}, n, forward< URNG >( g ) );
}

template< class PopIter, class SampleIter, class Size, class URNG >
SampleIter __sample( PopIter first,
                     PopIter last,
                     std::input_iterator_tag,
                     SampleIter out,
                     std::random_access_iterator_tag,
                     Size   n,
                     URNG&& g )
{
    using dist_t  = std::uniform_int_distribution< Size >;
    using param_t = typename dist_t::param_type;
    dist_t d{};
    Size   sample_sz{0};
    while ( first != last && sample_sz != n )
        out[ sample_sz++ ] = *first++;
    for ( Size pop_sz{sample_sz}; first != last; ++first, ++pop_sz )
    {
        param_t const p{0, pop_sz};
        Size const    k{d( g, p )};
        if ( k < n )
            out[ k ] = *first;
    }
    return out + sample_sz;
}

template< class PopIter, class SampleIter, class Size, class URNG >
SampleIter __sample( PopIter first,
                     PopIter last,
                     std::forward_iterator_tag,
                     SampleIter out,
                     std::output_iterator_tag,
                     Size   n,
                     URNG&& g )
{
    using dist_t  = std::uniform_int_distribution< Size >;
    using param_t = typename dist_t::param_type;
    dist_t d{};
    Size   unsampled_sz = std::distance( first, last );
    for ( n = min( n, unsampled_sz ); n != 0; ++first )
    {
        param_t const p{0, --unsampled_sz};
        if ( d( g, p ) < n )
        {
            *out++ = *first;
            --n;
        }
    }
    return out;
}

【讨论】:

  • @prometheus 你明白吗?
猜你喜欢
  • 1970-01-01
  • 2016-09-13
  • 2014-03-24
  • 1970-01-01
  • 1970-01-01
  • 2011-05-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多