|
| 1 | +import { get } from 'lodash'; |
| 2 | +import { CochranResult } from '../types'; |
| 3 | +import { standardDeviation } from 'simple-statistics'; |
| 4 | +import cochranCriticalValues from './cochranCriticalValues'; |
| 5 | + |
| 6 | +export function Cochran(values: Array<number[]>): CochranResult | null { |
| 7 | + /* p -> numune sayısı */ |
| 8 | + const pValue = values.length; |
| 9 | + |
| 10 | + /* n -> yapılan tekrar,test sayısı */ |
| 11 | + const nValue = values[0].length; |
| 12 | + |
| 13 | + const onePercentCriticalValue = get( |
| 14 | + cochranCriticalValues, |
| 15 | + `${nValue}.1%.${pValue}` |
| 16 | + ); |
| 17 | + |
| 18 | + const fivePercentCriticalValue = get( |
| 19 | + cochranCriticalValues, |
| 20 | + `${nValue}.5%.${pValue}` |
| 21 | + ); |
| 22 | + |
| 23 | + if (!onePercentCriticalValue || !fivePercentCriticalValue) { |
| 24 | + return null; |
| 25 | + } |
| 26 | + |
| 27 | + const squareDeviations = values |
| 28 | + .map((value) => { |
| 29 | + return standardDeviation(value); |
| 30 | + }) |
| 31 | + .map((value) => { |
| 32 | + return value * value; |
| 33 | + }); |
| 34 | + |
| 35 | + const maxDeviation = Math.max.apply(null, squareDeviations); |
| 36 | + const sumOfSquareDeviations = squareDeviations.reduce((a, b) => a + b, 0); |
| 37 | + |
| 38 | + const cValue = maxDeviation / sumOfSquareDeviations; |
| 39 | + |
| 40 | + if (cValue < fivePercentCriticalValue) { |
| 41 | + return CochranResult.Correct; |
| 42 | + } |
| 43 | + |
| 44 | + if (cValue < onePercentCriticalValue && cValue > fivePercentCriticalValue) { |
| 45 | + return CochranResult.Straggler; |
| 46 | + } |
| 47 | + |
| 48 | + if (cValue > onePercentCriticalValue) { |
| 49 | + return CochranResult.Outlier; |
| 50 | + } |
| 51 | + |
| 52 | + return null; |
| 53 | +} |
0 commit comments