Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save rokujyouhitoma/462cccbfaa45017ccfa27982695b25ed to your computer and use it in GitHub Desktop.
Save rokujyouhitoma/462cccbfaa45017ccfa27982695b25ed to your computer and use it in GitHub Desktop.
乱数生成器の乱数をピアソンのカイ二乗検定で検定
// 参考
// http://blog.amedama.jp/entry/2016/11/20/173932
// http://www.koka.ac.jp/morigiwa/sjs/chi-square_distribution.htm
//ピアソンのカイ二乗検定
//http://blog.amedama.jp/entry/2016/11/20/173932
//npi >= 10
var pi = Xorshift.MAX_VALUE;
var n = 10000; //標本数 var dist = 10;
var g = new Xorshift();
var array = [];
for(var i = 0; i < dist; i++){
array[i] = 0;
}
for(var i = 0; i < n; i++){
var index = Math.floor((g.rand() / Xorshift.MAX_VALUE) * 10);
array[index] += 1;
}
console.log(array);
var o = array;
var e = []; //期待値
for(var i = 0; i < dist; i++){
e[i] = n / dist;
}
console.log(e);
// 標本から期待値を引いて二乗すると、公式の分母部分が計算
var b = o.map(function(oi, index){
var ei = e[index];
return Math.pow(oi - ei, 2);
})
console.log(b);
var s = b.reduce(function(a, b){
return a + b;
});
console.log(s);
console.log(s / 10000);
//df = dist - 1 = 9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment