const wilcoxonSignedRanksTable = [ 0 , 0 , 0 , 0 , 0 , 0 , 2 , 3 , 5 , 8 , 10 , 13 , 17 , 21 , 25 , 29 , 34 , 40 , 46 , 52 , 58 , 65 , 73 , 81 , 89 , 98 , 107 , 116 , 126 , 137 , 147 , 159 , 170 , 182 , 195 , 208 , 221 , 235 , 249 , 264 , 279 , 294 , 310 , 327 , 343 , 361 , 378 , 396 , 415 , 434 , ] ; interface ISample { c : number ; e : number | string ; diff : number ; absDiff : number ; rank : number ; } export function getSamples ( control , experiment ) { return control . map ( ( c , i ) => { return { c , e : experiment [ i ] , diff : experiment [ i ] - c , absDiff : Math . abs ( experiment [ i ] - c ) , rank : 0 } ; } ) ; } export function sortSamples ( samples ) { return samples . sort ( ( a , b ) => { return a . absDiff - b . absDiff ; } ) ; } export function rankSamples ( sortedSamples ) { const ss = sortedSamples ; ss . slice ( ) . map ( ( s ) => { s . rank = ss . indexOf ( s ) + 1 ; } ) ; return ss ; } export function getTMinusVal ( rankedSamples ) { return rankedSamples . reduce ( ( currentSum , sample ) => { if ( Math . sign ( sample . diff ) === - 1 ) { return currentSum + sample . rank ; } return currentSum ; } , 0 ) ; } export function getTPlusVal ( rankedSamples ) { return rankedSamples . reduce ( ( currentSum , sample ) => { if ( Math . sign ( sample . diff ) === 1 || Math . sign ( sample . diff ) === 0 ) { return currentSum + sample . rank ; } return currentSum ; } , 0 ) ; } param control param listTwo export function getWilcoxonSignedRankTest ( control , experiment ) { const N = control . length ; const samples = getSamples ( control , experiment ) ; const sortedSamples = sortSamples ( samples ) ; const rankedSamples = rankSamples ( sortedSamples ) ; const tMinusVal = getTMinusVal ( rankedSamples ) ; const tPlusVal = getTPlusVal ( rankedSamples ) ; const wStat = Math . min ( tMinusVal , tPlusVal ) ; try { const wCrit = wilcoxonSignedRanksTable [ N ] ; return wStat < wCrit ; } catch ( e ) { throw new Error ( `Sample sizes greater than 50 are not supported. Your sample size is ${ N }` ) ; } }