Skip to content

Instantly share code, notes, and snippets.

@lukem512
Created May 1, 2016 12:19
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
Find the critical value of the Mann-Whitney U Test (or the Robust Rank Order) using a Monte Carlo simulation
// 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))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment