diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..fc651f0 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "src/aggregation"] + path = src/aggregation + url = ./src/aggregation/ diff --git a/index.js b/index.js index a78b125..9c43315 100644 --- a/index.js +++ b/index.js @@ -5,7 +5,7 @@ import { geometricMeanOfOdds, extremizedGeometricMeanOfOdds, neyman, -} from "@forecasting/aggregation"; +} from "./src/aggregation/index.js"; export { median, diff --git a/node_modules/.package-lock.json b/node_modules/.package-lock.json deleted file mode 100644 index 57243c0..0000000 --- a/node_modules/.package-lock.json +++ /dev/null @@ -1,13 +0,0 @@ -{ - "name": "forecasting", - "version": "0.0.1", - "lockfileVersion": 2, - "requires": true, - "packages": { - "node_modules/@forecasting/aggregation": { - "version": "0.0.1", - "resolved": "https://registry.npmjs.org/@forecasting/aggregation/-/aggregation-0.0.1.tgz", - "integrity": "sha512-N6NwJaHioyJQZwjvbNYQCknegrQoWne5I1TILA0jSu+8xkCIN+16cYumc1hZSYAKTzfBsiQWXZbuubfVMpgXFg==" - } - } -} diff --git a/node_modules/@forecasting/aggregation/README.md b/node_modules/@forecasting/aggregation/README.md deleted file mode 100644 index 172f939..0000000 --- a/node_modules/@forecasting/aggregation/README.md +++ /dev/null @@ -1,55 +0,0 @@ -## About - -![](decision-method.png) - -This package contains a series of utilities for forecast aggregation. It is currently in _alpha_, meaning that the code itself works, but there isn't error checking. - -For an introduction to different aggregation methods, see Jaime Sevilla's [Aggregation](https://forum.effectivealtruism.org/s/hjiBqAJNKhfJFq7kf) series. For an explanation of the neyman method, see [here](https://forum.effectivealtruism.org/s/hjiBqAJNKhfJFq7kf/p/biL94PKfeHmgHY6qe). - -## Built with - -- vanilla javascript -- [Best readme template](https://github.com/othneildrew/Best-README-Template) - -## Getting started - -### Installation - -```sh -npm install @forecasting/aggregation -``` - -### Usage - -```js -import { - median, - arithmeticMean, - geometricMean, - geometricMeanOfOdds, - extremizedGeometricMeanOfOdds, - neyman, -} from "@forecasting/aggregation"; - -let ps = [0.1, 0.2, 0.4, 0.5]; -console.log(ps); - -console.log(median(ps)); -console.log(arithmeticMean(ps)); -console.log(geometricMean(ps)); -console.log(geometricMeanOfOdds(ps)); -console.log(extremizedGeometricMeanOfOdds(ps, 1.5)); // 1.5 is the extremization factor -console.log(extremizedGeometricMeanOfOdds(ps, 2.5)); -console.log(neyman(ps)); -``` - -## Roadmap - -- [ ] validate probabilities (must be 0<= p <=1) -- [ ] Decide on a return type if probabilities are not validated (-1? / null?) -- [ ] Write wrapper code for validation -- [ ] Validate that array.length > 0 -- [ ] add weighting? by recency? -- [ ] filter outliers? -- [ ] Write documentation -- [ ] Do another repository for scoring methods diff --git a/node_modules/@forecasting/aggregation/decision-method.png b/node_modules/@forecasting/aggregation/decision-method.png deleted file mode 100644 index 3292f90..0000000 Binary files a/node_modules/@forecasting/aggregation/decision-method.png and /dev/null differ diff --git a/node_modules/@forecasting/aggregation/index.js b/node_modules/@forecasting/aggregation/index.js deleted file mode 100644 index e56d45d..0000000 --- a/node_modules/@forecasting/aggregation/index.js +++ /dev/null @@ -1,64 +0,0 @@ -// Helpers -const sum = (array) => array.reduce((a, b) => a + b, 0); -const probabilityToOdds = (p) => p / (1 - p); -const oddsToProbability = (o) => o / (1 + o); - -// Main functions -export const median = (array) => { - // needs validation array not empty - let midway = Math.floor(array.length); - let arrayToBeSorted = [...array]; - // sorting mutates the array, which I am averse to - let arraySorted = arrayToBeSorted.sort((a, b) => a - b); - if (midway % 2) { - return arraySorted[midway]; - } else { - return (arraySorted[midway - 1] + arraySorted[midway]) / 2; - } -}; - -export const arithmeticMean = (array) => { - let result = sum(array) / array.length; - return result; -}; - -export const geometricMean = (array) => { - // sum of logs seems more numerically stable than multiplying a lot of numbers 0<=p<=1 - let arrayAsLog = array.map((p) => Math.log(p)); - let sumOfLogs = sum(arrayAsLog) / arrayAsLog.length; - let result = Math.exp(sumOfLogs); - return result; -}; - -export const geometricMeanOfOdds = (array) => { - let arrayOfOdds = array.map((p) => probabilityToOdds(p)); - let arrayOfLogsOfOdds = arrayOfOdds.map((p) => Math.log(p)); - let sumOfLogsOfOdds = sum(arrayOfLogsOfOdds) / arrayOfLogsOfOdds.length; - let geomMeanOfOdds = Math.exp(sumOfLogsOfOdds); - let result = oddsToProbability(geomMeanOfOdds); - return result; -}; - -export const extremizedGeometricMeanOfOdds = ( - array, - extremizationParameter = 1.5 -) => { - let arrayOfOdds = array.map((p) => probabilityToOdds(p)); - let arrayOfLogsOfOdds = arrayOfOdds.map((p) => Math.log(p)); - let extremizedSumOfLogsOfOdds = - (extremizationParameter * sum(arrayOfLogsOfOdds)) / - arrayOfLogsOfOdds.length; - let extremizedGeomMeanOfOdds = Math.exp(extremizedSumOfLogsOfOdds); - let result = oddsToProbability(extremizedGeomMeanOfOdds); - return result; -}; - -export const neyman = (array) => { - let n = array.length; - - let d = - (n * (Math.sqrt(3 * Math.pow(n, 2) - 3 * n + 1) - 2)) / - (Math.pow(n, 2) - n - 1); - let result = extremizedGeometricMeanOfOdds(array, d); - return result; -}; diff --git a/node_modules/@forecasting/aggregation/package.json b/node_modules/@forecasting/aggregation/package.json deleted file mode 100644 index ee04aa3..0000000 --- a/node_modules/@forecasting/aggregation/package.json +++ /dev/null @@ -1,18 +0,0 @@ -{ - "name": "@forecasting/aggregation", - "version": "0.0.1", - "description": "Forecasting aggregation utilities", - "main": "index.js", - "scripts": { - "test": "node test.js" - }, - "keywords": [ - "forecasting", - "aggregation", - "prediction", - "prediction", - "markets" - ], - "author": "Nuño Sempere", - "license": "MIT" -} diff --git a/node_modules/@forecasting/aggregation/tests.js b/node_modules/@forecasting/aggregation/tests.js deleted file mode 100644 index 97a8f25..0000000 --- a/node_modules/@forecasting/aggregation/tests.js +++ /dev/null @@ -1,19 +0,0 @@ -import { - median, - arithmeticMean, - geometricMean, - geometricMeanOfOdds, - extremizedGeometricMeanOfOdds, - neyman, -} from "./index.js"; - -let ps = [0.1, 0.2, 0.4, 0.5]; -console.log(ps); - -console.log(median(ps)); -console.log(arithmeticMean(ps)); -console.log(geometricMean(ps)); -console.log(geometricMeanOfOdds(ps)); -console.log(extremizedGeometricMeanOfOdds(ps, 1.5)); -console.log(extremizedGeometricMeanOfOdds(ps, 2.5)); -console.log(neyman(ps)); diff --git a/src/aggregation b/src/aggregation new file mode 160000 index 0000000..359897c --- /dev/null +++ b/src/aggregation @@ -0,0 +1 @@ +Subproject commit 359897cc5221b0f6ee2f428484daa05253a94364