我在 F# (.Net) 和 C++ 中开发了相同的算法(用于估计隐藏马尔可夫模型参数的 Baum-Welch)。在这两种情况下,我都开发了相同的测试,它生成具有已知分布的随机测试数据,然后使用算法来估计参数,并确保它收敛到已知的正确答案。
问题是测试在 F# 情况下工作正常,但在 C++ 实现中无法收敛。我在一些真实世界的数据上比较了这两种算法,它们给出了相同的结果,所以我的猜测是测试数据的生成在 C++ 案例中被破坏了。因此我的问题是:.Net 4 附带的随机数生成器是什么(我认为这是 VS2010 的默认版本)?
在 F# 中,我正在使用:
let random = new Random()
let randomNormal () = //for a standard normal random variable
let u1 = random.NextDouble()
let u2 = random.NextDouble()
let r = sqrt (-2. * (log u1))
let theta = 2. * System.Math.PI * u2
r * (sin theta)
//random.NextDouble() for uniform random variable on [0-1]
在 C++ 中,我使用标准的 Boost 类:
class HmmGenerator
{
public:
HmmGenerator() :
rng(37), //the seed does change the result, but it doesn't make it work
normalGenerator(rng, boost::normal_distribution<>(0.0, 1.0)),
uniformGenerator(rng, boost::uniform_01<>()) {}//other stuff here as well
private:
boost::mt19937 rng;
boost::variate_generator<boost::mt19937&,
boost::normal_distribution<> > normalGenerator;
boost::variate_generator<boost::mt19937&,
boost::uniform_01<> > uniformGenerator;
};
我应该使用这两种生成随机数的方式来期待不同的结果吗?
编辑:另外,.Net 中使用的生成器是否在 Boost 中可用(理想情况下具有相同的参数),所以我可以在 C++ 中运行它并比较结果?