c++ - 如何在 C++ 中使用 Sobol 序列生成分布良好的正态分布?

标签 c++ sequence normal-distribution seeding

我有以下代码:

#include <iostream>
#include <vector>
#include <random>
// #include "halton.cpp"
#include "sobol.cpp"

int main()
{
  int n=5000;
  double* thisV;
  thisV = i8_sobol_generate(1,n,0);

  std::mt19937 generator;
  std::normal_distribution<double> distribution(0,1.0);
  for (int i=0; i<n; i++) {
    generator.seed(thisV[i]);
    std::cout << distribution(generator) << std::endl;
  }

  return 0;
}

我使用以下代码生成了一个低差异序列 site (Sobol) 。 我用它来为生成器播种。但输出有点奇怪。有人可以帮助我吗?

输出:

1.12279
0.302805
1.12279
0.302805
1.12279
0.302805
1.12279
0.302805
1.12279
0.302805
1.12279
0.302805
1.12279
0.302805
1.12279
0.302805
...

最佳答案

如果没有看到 sobol.cpp 文件的内容,我无法告诉你为什么会得到这些确切的数字, 但是 每次循环时都会重新为生成器播种。

随着

  std::mt19937 generator;
  std::normal_distribution<double> distribution(0,1.0);
  for (int i=0; i<n; i++) {
    generator.seed(thisV[i]); //<------------
    std::cout << distribution(generator) << std::endl;
  }

如果种子在两个值之间交替,您将得到您所看到的行为。

顺便说一句 - 通常在 rmake 文件/项目中包含 cpp 文件时#include 头文件(而不是 cpp 文件)。


此外,如果你看看如何调用代码,

thisV = i8_sobol_generate(1,n,0);

我们还没有阐明这些参数的含义。

如果您点击您拥有的链接,则会对源代码进行一些测试(哇!)。 其中一个称为测试 sorbol08,并按如下方式循环:

for(dim_num = 2;dim_num <= DIM_MAX;dim_num++) {//^---------- 种子=0;

// <snip>

for ( i = 0; i <= 110; i++ )
{
  seed_in = seed;
  i8_sobol ( dim_num, &seed, r );

// ....

您调用的函数,

double *i8_sobol_generate ( int m, int n, int skip )

发送 1 作为第一个参数。这将使用该值调用测试函数。

因此,我怀疑您应该尝试更高的维度 - 它确实循环得非常快。

请包含 .hpp 文件而不是 cpp 文件。 查看测试以获取有关使用的线索。

关于c++ - 如何在 C++ 中使用 Sobol 序列生成分布良好的正态分布?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41767977/

相关文章:

c++ - 通过组合添加类功能

android - 新行的损坏插入 ID

c - 传入指向整数指针的指针时的斐波那契数列

java - DoubleNode ADT - 打印节点的节点

r - 多次使用 set.seed 的奇怪行为

R - 将 SD 区域添加为正态分布的虚线

c++ - 为什么在 RemoveHead(node) 函数中使用 **head(而不是 *head)?

c++ 指向函数的指针没有改变

r - 如何同时测试R中多个变量的正态性?

C++ 在 Xcode 控制台中不显示 cout 但在终端中运行完美