Instantly share code, notes, and snippets.

# lukem512/critical.js

Created May 1, 2016 12:19
Show Gist options
• Save lukem512/f8dca8b7b499cf1e667752e4e3b86db6 to your computer and use it in GitHub Desktop.
Find the critical value of the Mann-Whitney U Test (or the Robust Rank Order) using a Monte Carlo simulation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 // Luke Mitchell, 2016 "use strict" var mwu = require('mann-whitney-utest'); const DEFAULT_COUNT = 1000000 const DEFAULT_P = 0.05 function min (us) { if (us[0] < us[1]) return us[0] else return us[1] } function makeSamples (n, m, count = DEFAULT_COUNT) { // Samples object let samples = [] // Generate 'count' pairs of samples and perform test for (var i = 0; i < count; i++) { let sample = [[], []]; for (var ni = 0; ni < n; ni++) sample[0].push(Math.random()) for (var mi = 0; mi < m; mi++) sample[1].push(Math.random()) // Run the test const us = mwu.test(sample) samples.push(min(us)) } // Sort the samples samples.sort() return samples } function estimateCriticalValue (n, m, p = DEFAULT_P, count = DEFAULT_COUNT) { const samples = makeSamples(n, m, count) return samples[p * count] } function findP (n, m, U, count = DEFAULT_COUNT) { const samples = makeSamples(n, m, count) return samples.indexOf(U) / count } // What is the critical value for p = 0.2? console.log(estimateCriticalValue(11, 14, 0.2, 100000)) // What is the p-value for a U-value of 54? console.log(findP(11, 14, 54, 100000))