2013-04-25 78 views
2

我在F#(.Net)和C++中開發了相同的算法(Baum-Welch用於估計隱馬爾可夫模型的參數)。在這兩種情況下,我開發了相同的測試,生成具有已知分佈的隨機測試數據,然後使用該算法估計參數,並確保它收斂到已知的正確答案。Boost與.Net隨機數生成器

問題是測試在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++中我使用了標準升壓類:

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++和比較結果?

+0

(+1)有趣的問題。很少的問題 - 1.你的意思是由C++實現什麼不收斂(到什麼)? 2.您爲實驗繪製了多少個樣本? – Nishanth 2013-04-25 16:00:12

回答

4

因此我的問題:什麼是.Net 4隨機數生成器(我認爲這是VS2010的默認版本)?

the documentation on Random

當前實現Random類是基於唐納德·E·Knuth的消減隨機數生成算法。欲瞭解更多信息,請參閱D. E. Knuth。 「計算機編程的藝術,第2卷:研究數學算法」。 Addison-Wesley,Reading,MA,第二版,1981。

我應該期望使用這兩種方法產生隨機數的結果不同嗎?

與其他現成的隨機生成器相比,您在C++中使用的Mersenne-Twister算法被認爲是非常值得尊重的。

我懷疑你的代碼在別處存在任何差異。

+0

你知道它是否可用於Boost(理想地使用相同的參數)? – Grzenio 2013-04-25 15:25:15

+0

@Grzenio很難說。但似乎你可以在.NET中實現[mt算法](http://stackoverflow.com/questions/1166408) – 2013-04-25 15:28:53

+1

@Grzenio:['boost :: random :: knuth_b'](http:// www。 boost.org/doc/html/boost/random/knuth_b.html)使用與System.Random相同的算法,儘管.NET在.NET中的工作方式不同,所以即使使用相同的種子,也不會得到相同的輸出。 – ildjarn 2013-04-25 19:01:02