Created
September 18, 2019 04:53
-
-
Save nkoneko/a0b315eaf713f154fdaf58a2214adc23 to your computer and use it in GitHub Desktop.
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
function stats(imp) { | |
function compareNumbers(x, y) { | |
return x - y; | |
} | |
return (function(exports) { | |
var Sequence = function(values) { | |
this.sortedValues = values; | |
this.sortedValues.sort(compareNumbers); | |
this.quartiles = Array(5); | |
}; | |
Sequence.prototype.min = function() { | |
return this.sortedValues[0]; | |
}; | |
Sequence.prototype.max = function() { | |
return this.sortedValues[this.sortedValues.length - 1]; | |
}; | |
Sequence.prototype.quartile = function(n) { | |
if (n < 0 || n > 5) { | |
return undefined; | |
} | |
if (!this.quartiles[n]) { | |
var p = n / 4; | |
var q = (this.sortedValues.length - 1) * p; | |
var r = Math.floor(q); | |
var s = q - r; | |
if (this.sortedValues[r + 1] !== undefined) { | |
this.quartiles[n] = this.sortedValues[r] + s * (this.sortedValues[r + 1] - this.sortedValues[r]); | |
} else { | |
this.quartiles[n] = this.sortedValues[r]; | |
} | |
} | |
return this.quartiles[n]; | |
}; | |
Sequence.prototype.iqr = function() { | |
var t = this.quartile(3), | |
f = this.quartile(1); | |
return t - f; | |
}; | |
function _Arrays_isMatrix(mat) { | |
return Array.isArray(mat) && mat.every(function(vec) { | |
return Array.isArray(vec) && vec.length === mat[0].length; | |
}); | |
} | |
function _Arrays_take(n) { | |
return function(arr) { | |
return arr.slice(0, n); | |
}; | |
} | |
function _Arrays_get(n) { | |
return function(arr) { | |
return arr[n]; | |
}; | |
} | |
function Range(x, y) { | |
if (y === undefined) { | |
this.l = 0; | |
this.u = x; | |
} else { | |
this.l = x; | |
this.u = y; | |
} | |
}; | |
Range.prototype.forEach = function(cb) { | |
for (var n = this.l; n < this.u; n++) { | |
var idx = n - this.l; | |
cb(n, idx); | |
} | |
}; | |
Range.prototype.map = function(cb) { | |
var result = []; | |
for (var n = this.l; n < this.u; n++) { | |
var idx = n - this.l; | |
result.push(cb(n, idx)); | |
} | |
return result; | |
}; | |
exports.Sequence = Sequence; | |
exports.Range = Range; | |
exports.Arrays = { | |
isMatrix: _Arrays_isMatrix, | |
take: _Arrays_take, | |
get: _Arrays_get | |
}; | |
})(imp); | |
} | |
function import(modl) { | |
modl(this); | |
} | |
function IQR(values) { | |
import(stats); | |
if (Arrays.isMatrix(values)) { | |
var range = new Range(values[0].length); | |
return [range.map(function(n) { | |
var seq = new Sequence(values.map(Arrays.get(n))); | |
return seq.iqr(); | |
})]; | |
} else if (Array.isArray(values) && values.length === 1) { | |
var seq = new Sequence(values[0]); | |
return seq.iqr(); | |
} else { | |
return undefined; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment