Initial data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 72d218235f
commit f23f22d71c
199087 changed files with 3378941 additions and 0 deletions

View file

@ -0,0 +1,33 @@
'use strict';
function sum(array) {
return array.reduce(function (a, b) {
return a + b;
});
}
function square(x) {
return x * x;
}
function mean(array) {
return sum(array) / array.length;
}
function averageSquareDiff(a, predictions) {
return mean(predictions.map(function (x) {
return square(x - a);
}));
}
function diversityTheorem(truth, predictions) {
var average = mean(predictions);
return {
'average-error': averageSquareDiff(truth, predictions),
'crowd-error': square(truth - average),
'diversity': averageSquareDiff(average, predictions)
};
}
console.log(diversityTheorem(49, [48,47,51]))
console.log(diversityTheorem(49, [48,47,51,42]))

View file

@ -0,0 +1,72 @@
(() => {
'use strict';
// diversityValues :: [Num] -> {
// mean-error :: Float,
// crowd-error :: Float,
// diversity :: Float
// }
const diversityValues = observed =>
predictions => {
const predictionMean = mean(predictions);
return {
'mean-error': meanErrorSquared(observed)(
predictions
),
'crowd-error': Math.pow(
observed - predictionMean,
2
),
'diversity': meanErrorSquared(predictionMean)(
predictions
)
};
};
// meanErrorSquared :: Num a => a -> [a] -> b
const meanErrorSquared = observed =>
predictions => mean(
predictions.map(x => Math.pow(x - observed, 2))
);
// mean :: Num a => [a] -> b
const mean = xs => {
const lng = xs.length;
return lng > 0 ? (
xs.reduce((a, b) => a + b, 0) / lng
) : undefined;
};
// ----------------------- TEST ------------------------
const main = () =>
JSON.stringify([{
observed: 49,
predictions: [48, 47, 51]
}, {
observed: 49,
predictions: [48, 47, 51, 42]
}].map(x => dictionaryAtPrecision(3)(
diversityValues(x.observed)(
x.predictions
)
)), null, 2);
// ---------------------- GENERIC ----------------------
// dictionaryAtPrecision :: Int -> Dict -> Dict
const dictionaryAtPrecision = n =>
// A dictionary of Float values, with
// all Floats adjusted to a given precision.
dct => Object.keys(dct).reduce(
(a, k) => Object.assign(
a, {
[k]: dct[k].toPrecision(n)
}
), {}
);
// MAIN ---
return main()
})();