benford-law 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENCE +21 -0
- package/README.md +66 -0
- package/build/benford.d.ts +8 -0
- package/build/benford.js +60 -0
- package/build/index.d.ts +1 -0
- package/build/index.js +17 -0
- package/package.json +66 -0
package/LICENCE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 Maxime Golfier
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
# benford-law
|
|
2
|
+
|
|
3
|
+
Benford's law is an observation that in many real-life sets of numerical data, the leading digit is likely to be small. In sets that obey the law, the number 1 appears as the leading significant digit about 30% of the time, while 9 appears as the leading significant digit less than 5% of the time. If the digits were distributed uniformly, they would each occur about 11.1% of the time. Benford's law also makes predictions about the distribution of second digits, third digits, digit combinations, and so on.
|
|
4
|
+
|
|
5
|
+
To get a better understanding of Benford's law, check out [this article](https://en.wikipedia.org/wiki/Benford%27s_law).
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
yarn add benford-law
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
import {
|
|
17
|
+
processBenfordLaw,
|
|
18
|
+
generateBenfordLawNumbers,
|
|
19
|
+
generateBenfordLawNumber,
|
|
20
|
+
} from 'benford-law';
|
|
21
|
+
|
|
22
|
+
// to generate a random number that follows Benford's law
|
|
23
|
+
console.log(generateBenfordLawNumber());
|
|
24
|
+
|
|
25
|
+
// to generate an array of 10 random numbers that follow Benford's law
|
|
26
|
+
console.log(generateBenfordLawNumbers(10));
|
|
27
|
+
|
|
28
|
+
// to process an array of numbers and get the distribution
|
|
29
|
+
console.log(processBenfordLaw(generateBenfordLawNumbers(50000), 0.01));
|
|
30
|
+
// {
|
|
31
|
+
// isFollowingBenfordLaw: true,
|
|
32
|
+
// firstDigitProbabilities: {
|
|
33
|
+
// '1': 0.29908,
|
|
34
|
+
// '2': 0.17694,
|
|
35
|
+
// '3': 0.1255,
|
|
36
|
+
// '4': 0.09742,
|
|
37
|
+
// '5': 0.0793,
|
|
38
|
+
// '6': 0.06712,
|
|
39
|
+
// '7': 0.0571,
|
|
40
|
+
// '8': 0.05124,
|
|
41
|
+
// '9': 0.0463
|
|
42
|
+
// },
|
|
43
|
+
// firstDigitCounts: {
|
|
44
|
+
// '1': 14954,
|
|
45
|
+
// '2': 8847,
|
|
46
|
+
// '3': 6275,
|
|
47
|
+
// '4': 4871,
|
|
48
|
+
// '5': 3965,
|
|
49
|
+
// '6': 3356,
|
|
50
|
+
// '7': 2855,
|
|
51
|
+
// '8': 2562,
|
|
52
|
+
// '9': 2315
|
|
53
|
+
// },
|
|
54
|
+
// firstDigitAccuracy: {
|
|
55
|
+
// '1': 0.0019199999999999773,
|
|
56
|
+
// '2': 0.0009399999999999964,
|
|
57
|
+
// '3': 0.0005000000000000004,
|
|
58
|
+
// '4': 0.0004200000000000037,
|
|
59
|
+
// '5': 0.0002999999999999947,
|
|
60
|
+
// '6': 0.00011999999999999511,
|
|
61
|
+
// '7': 0.000900000000000005,
|
|
62
|
+
// '8': 0.0002400000000000041,
|
|
63
|
+
// '9': 0.00030000000000000165
|
|
64
|
+
// }
|
|
65
|
+
// }
|
|
66
|
+
```
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export declare const generateBenfordLawNumber: () => number;
|
|
2
|
+
export declare const generateBenfordLawNumbers: (length: number) => number[];
|
|
3
|
+
export declare const processBenfordLaw: (numbers: number[], threshold?: number, benfordProbabilities?: Record<string, number>) => {
|
|
4
|
+
isFollowingBenfordLaw: boolean;
|
|
5
|
+
firstDigitCounts: Record<string, number>;
|
|
6
|
+
firstDigitProbabilities: Record<string, number>;
|
|
7
|
+
firstDigitAccuracy: Record<string, number>;
|
|
8
|
+
};
|
package/build/benford.js
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.processBenfordLaw = exports.generateBenfordLawNumbers = exports.generateBenfordLawNumber = void 0;
|
|
4
|
+
const benfordProb = {
|
|
5
|
+
'1': 0.301,
|
|
6
|
+
'2': 0.176,
|
|
7
|
+
'3': 0.125,
|
|
8
|
+
'4': 0.097,
|
|
9
|
+
'5': 0.079,
|
|
10
|
+
'6': 0.067,
|
|
11
|
+
'7': 0.058,
|
|
12
|
+
'8': 0.051,
|
|
13
|
+
'9': 0.046,
|
|
14
|
+
};
|
|
15
|
+
const generateRandomNumber = (minimum, maximum) => Math.random() * (maximum - minimum) + minimum;
|
|
16
|
+
const getFirstDigit = (number) => {
|
|
17
|
+
const string = number.toString();
|
|
18
|
+
return parseInt(string[0], 10);
|
|
19
|
+
};
|
|
20
|
+
const generateBenfordLawNumber = () => Math.exp(generateRandomNumber(Math.log(1), Math.log(1000)));
|
|
21
|
+
exports.generateBenfordLawNumber = generateBenfordLawNumber;
|
|
22
|
+
const generateBenfordLawNumbers = (length) => {
|
|
23
|
+
const numbers = [];
|
|
24
|
+
for (let i = 0; i < length; i++) {
|
|
25
|
+
numbers.push((0, exports.generateBenfordLawNumber)());
|
|
26
|
+
}
|
|
27
|
+
return numbers;
|
|
28
|
+
};
|
|
29
|
+
exports.generateBenfordLawNumbers = generateBenfordLawNumbers;
|
|
30
|
+
const processBenfordLaw = (numbers, threshold = 0.01, benfordProbabilities = benfordProb) => {
|
|
31
|
+
const firstDigits = numbers.map(getFirstDigit);
|
|
32
|
+
const firstDigitCounts = firstDigits.reduce((acc, digit) => ({
|
|
33
|
+
...acc,
|
|
34
|
+
[digit]: (acc[digit] || 0) + 1,
|
|
35
|
+
}), {});
|
|
36
|
+
const firstDigitProbabilities = Object.entries(firstDigitCounts).reduce((acc, [digit, count]) => ({
|
|
37
|
+
...acc,
|
|
38
|
+
[digit]: count / numbers.length,
|
|
39
|
+
}), {});
|
|
40
|
+
const firstDigitAccuracy = Object.entries(firstDigitProbabilities).reduce((acc, [digit, probability]) => {
|
|
41
|
+
const benfordProbability = benfordProbabilities[digit];
|
|
42
|
+
const diff = Math.abs(benfordProbability - probability);
|
|
43
|
+
return {
|
|
44
|
+
...acc,
|
|
45
|
+
[digit]: diff,
|
|
46
|
+
};
|
|
47
|
+
}, {});
|
|
48
|
+
const isBenford = Object.entries(benfordProbabilities).every(([digit, probability]) => {
|
|
49
|
+
const firstDigitProbability = firstDigitProbabilities[digit];
|
|
50
|
+
return (firstDigitProbability &&
|
|
51
|
+
Math.abs(firstDigitProbability - probability) < threshold);
|
|
52
|
+
});
|
|
53
|
+
return {
|
|
54
|
+
isFollowingBenfordLaw: isBenford,
|
|
55
|
+
firstDigitProbabilities,
|
|
56
|
+
firstDigitCounts,
|
|
57
|
+
firstDigitAccuracy,
|
|
58
|
+
};
|
|
59
|
+
};
|
|
60
|
+
exports.processBenfordLaw = processBenfordLaw;
|
package/build/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./benford";
|
package/build/index.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./benford"), exports);
|
package/package.json
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "benford-law",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"author": "maxgfr",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"description": "A simple library to check if a dataset follows the Benford's law",
|
|
7
|
+
"main": "./build/index.js",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "https://github.com/maxgfr/benford-law.git"
|
|
11
|
+
},
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://github.com/maxgfr/benford-law/issues"
|
|
14
|
+
},
|
|
15
|
+
"homepage": "https://github.com/maxgfr/benford-law#readme",
|
|
16
|
+
"files": [
|
|
17
|
+
"build"
|
|
18
|
+
],
|
|
19
|
+
"keywords": [
|
|
20
|
+
"benford",
|
|
21
|
+
"benford's law",
|
|
22
|
+
"trading",
|
|
23
|
+
"algorithm"
|
|
24
|
+
],
|
|
25
|
+
"scripts": {
|
|
26
|
+
"start": "node build/index.js",
|
|
27
|
+
"dev": "nodemon",
|
|
28
|
+
"develop": "node -r @swc-node/register ./src/index.ts",
|
|
29
|
+
"test": "jest --passWithNoTests",
|
|
30
|
+
"test:watch": "jest --watch",
|
|
31
|
+
"clean": "rimraf build",
|
|
32
|
+
"build": "tsc -p tsconfig.build.json",
|
|
33
|
+
"build:watch": "tsc -w -p tsconfig.build.json",
|
|
34
|
+
"build:swc": "swc ./src -d build",
|
|
35
|
+
"build:swc:watch": "swc ./src -d build -w",
|
|
36
|
+
"lint": "eslint ./src --ext .ts",
|
|
37
|
+
"prettier": "prettier --write './src/**/*.{ts,js,json}'",
|
|
38
|
+
"release": "semantic-release"
|
|
39
|
+
},
|
|
40
|
+
"dependencies": {},
|
|
41
|
+
"devDependencies": {
|
|
42
|
+
"@semantic-release/changelog": "^6.0.2",
|
|
43
|
+
"@semantic-release/commit-analyzer": "^9.0.2",
|
|
44
|
+
"@semantic-release/git": "^10.0.1",
|
|
45
|
+
"@semantic-release/github": "^8.0.7",
|
|
46
|
+
"@semantic-release/npm": "^9.0.1",
|
|
47
|
+
"@semantic-release/release-notes-generator": "^10.0.3",
|
|
48
|
+
"@swc-node/register": "1.5.4",
|
|
49
|
+
"@swc/cli": "0.1.57",
|
|
50
|
+
"@swc/core": "1.3.21",
|
|
51
|
+
"@swc/jest": "0.2.23",
|
|
52
|
+
"@types/jest": "29.2.3",
|
|
53
|
+
"@types/node": "18.11.10",
|
|
54
|
+
"@typescript-eslint/eslint-plugin": "5.45.0",
|
|
55
|
+
"@typescript-eslint/parser": "5.45.0",
|
|
56
|
+
"eslint": "8.29.0",
|
|
57
|
+
"eslint-config-prettier": "8.5.0",
|
|
58
|
+
"eslint-plugin-jest": "27.1.6",
|
|
59
|
+
"jest": "29.3.1",
|
|
60
|
+
"nodemon": "2.0.20",
|
|
61
|
+
"prettier": "2.8.0",
|
|
62
|
+
"rimraf": "3.0.2",
|
|
63
|
+
"semantic-release": "19.0.5",
|
|
64
|
+
"typescript": "4.9.3"
|
|
65
|
+
}
|
|
66
|
+
}
|