generative-bayesian-network 0.1.0-beta.1 → 2.0.0-dev.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.
@@ -178,7 +178,7 @@
178
178
  APPENDIX: How to apply the Apache License to your work.
179
179
 
180
180
  To apply the Apache License to your work, attach the following
181
- boilerplate notice, with the fields enclosed by brackets "[]"
181
+ boilerplate notice, with the fields enclosed by brackets "{}"
182
182
  replaced with your own identifying information. (Don't include
183
183
  the brackets!) The text should be enclosed in the appropriate
184
184
  comment syntax for the file format. We also recommend that a
@@ -186,7 +186,7 @@
186
186
  same "printed page" as the copyright notice for easier
187
187
  identification within third-party archives.
188
188
 
189
- Copyright [yyyy] [name of copyright owner]
189
+ Copyright 2018 Apify Technologies s.r.o.
190
190
 
191
191
  Licensed under the Apache License, Version 2.0 (the "License");
192
192
  you may not use this file except in compliance with the License.
package/README.md CHANGED
@@ -1,76 +1,3 @@
1
- # Generative bayesian network
2
- NodeJs package containing a bayesian network capable of randomly sampling from a distribution defined by a json file.
3
-
4
- <!-- toc -->
5
-
6
- - [Installation](#installation)
7
- - [Usage](#usage)
8
- - [Examples](#examples)
9
- - [API Reference](#api-reference)
10
-
11
- <!-- tocstop -->
12
-
13
- ## Installation
14
-
15
- ## Usage
16
-
17
- ## Examples
18
-
19
- ## API Reference
20
- All public classes, methods and their parameters can be inspected in this API reference.
21
-
22
- <a name="BayesianNetwork"></a>
23
-
24
- ### BayesianNetwork
25
- BayesianNetwork is an implementation of a bayesian network capable of randomly sampling from the distribution
26
- represented by the network.
27
-
28
-
29
- * [BayesianNetwork](#BayesianNetwork)
30
- * [`new BayesianNetwork(networkDefinition)`](#new_BayesianNetwork_new)
31
- * [`.generateSample(inputValues)`](#BayesianNetwork+generateSample)
32
- * [`.generateConsistentSampleWhenPossible(valuePossibilities)`](#BayesianNetwork+generateConsistentSampleWhenPossible)
33
-
34
-
35
- * * *
36
-
37
- <a name="new_BayesianNetwork_new"></a>
38
-
39
- #### `new BayesianNetwork(networkDefinition)`
40
-
41
- | Param | Type | Description |
42
- | --- | --- | --- |
43
- | networkDefinition | <code>object</code> | object defining the network structure and distributions |
44
-
45
-
46
- * * *
47
-
48
- <a name="BayesianNetwork+generateSample"></a>
49
-
50
- #### `bayesianNetwork.generateSample(inputValues)`
51
- Randomly samples from the distribution represented by the bayesian network.
52
- Can generate samples not found in the data used to create the definition file.
53
-
54
-
55
- | Param | Type | Description |
56
- | --- | --- | --- |
57
- | inputValues | <code>object</code> | node values that are known already |
58
-
59
-
60
- * * *
61
-
62
- <a name="BayesianNetwork+generateConsistentSampleWhenPossible"></a>
63
-
64
- #### `bayesianNetwork.generateConsistentSampleWhenPossible(valuePossibilities)`
65
- Randomly samples from the distribution represented by the bayesian network.
66
- Cannot generate samples not found in the data used to create the definition file,
67
- so it only generates samples when it is possible to be consistent with the data.
68
-
69
-
70
- | Param | Type | Description |
71
- | --- | --- | --- |
72
- | valuePossibilities | <code>object</code> | a dictionary of lists of possible values for nodes (if a node isn't present in the dictionary, all values are possible) |
73
-
74
-
75
- * * *
1
+ # Fingerprint suite
76
2
 
3
+ This repository contains a set of fingerprinting tools developed by Apify.
package/index.js ADDED
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.BayesianNetwork = void 0;
4
+ var bayesian_network_1 = require("./bayesian-network");
5
+ Object.defineProperty(exports, "BayesianNetwork", { enumerable: true, get: function () { return bayesian_network_1.BayesianNetwork; } });
6
+ //# sourceMappingURL=index.js.map
package/package.json CHANGED
@@ -1,37 +1,40 @@
1
1
  {
2
- "author": {
3
- "name": "Apify"
4
- },
5
- "files": ["src"],
6
- "bugs": {
7
- "url": "https://github.com/apify/generative-bayesian-network/issues"
8
- },
9
- "dependencies": {
10
- "ow": "^0.23.0"
11
- },
12
- "description": "An implementation of a bayesian network usable for .",
13
- "devDependencies": {
14
- "@apify/eslint-config": "^0.1.3",
15
- "eslint": "^7.19.0",
16
- "fs-extra": "^9.1.0",
17
- "jest": "^26.6.3",
18
- "jsdoc-to-markdown": "^7.0.0",
19
- "markdown-toc": "^1.2.0"
20
- },
21
- "homepage": "https://github.com/apify/generative-bayesian-network#readme",
22
- "license": "Apache-2.0",
23
- "main": "src/main.js",
24
- "name": "generative-bayesian-network",
25
- "repository": {
26
- "type": "git",
27
- "url": "git+https://github.com/apify/generative-bayesian-network.git"
28
- },
29
- "scripts": {
30
- "build-docs": "npm run build-toc && node docs/build-docs.js",
31
- "build-toc": "markdown-toc docs/README.md -i",
32
- "lint": "eslint ./src --ext .js,.jsx",
33
- "lint:fix": "eslint ./src --ext .js,.jsx --fix",
34
- "test": "jest --maxWorkers=3 --forceExit"
35
- },
36
- "version": "0.1.0-beta.1"
2
+ "name": "generative-bayesian-network",
3
+ "version": "2.0.0-dev.0",
4
+ "author": {
5
+ "name": "Apify"
6
+ },
7
+ "files": [
8
+ "dist"
9
+ ],
10
+ "main": "index.js",
11
+ "module": "index.mjs",
12
+ "types": "index.d.ts",
13
+ "exports": {
14
+ ".": {
15
+ "import": "./index.mjs",
16
+ "require": "./index.js"
17
+ }
18
+ },
19
+ "bugs": {
20
+ "url": "https://github.com/apify/generative-bayesian-network/issues"
21
+ },
22
+ "dependencies": {
23
+ "adm-zip": "^0.5.9",
24
+ "danfojs-node": "^1.1.1"
25
+ },
26
+ "description": "An implementation of a bayesian network usable for .",
27
+ "homepage": "https://github.com/apify/generative-bayesian-network#readme",
28
+ "license": "Apache-2.0",
29
+ "repository": {
30
+ "type": "git",
31
+ "url": "git+https://github.com/apify/generative-bayesian-network.git"
32
+ },
33
+ "scripts": {
34
+ "build": "npm run clean && npm run compile",
35
+ "clean": "rimraf ./dist",
36
+ "compile": "tsc -p tsconfig.build.json && gen-esm-wrapper ./index.js ./index.mjs",
37
+ "copy": "ts-node -T ../../scripts/copy.ts"
38
+ },
39
+ "gitHead": "2dd3d8c9ced1a14cb74ef16ebdd50b349bb5971b"
37
40
  }
@@ -1,85 +0,0 @@
1
- const { BayesianNode } = require('./bayesian-node');
2
-
3
- /**
4
- * BayesianNetwork is an implementation of a bayesian network capable of randomly sampling from the distribution
5
- * represented by the network.
6
- */
7
- class BayesianNetwork {
8
- /**
9
- * @param {object} networkDefinition - object defining the network structure and distributions
10
- */
11
- constructor(networkDefinition) {
12
- this.nodesInSamplingOrder = networkDefinition.nodes.map((nodeDefinition) => new BayesianNode(nodeDefinition));
13
- this.nodesByName = {};
14
- for (const node of this.nodesInSamplingOrder) {
15
- this.nodesByName[node.name] = node;
16
- }
17
- }
18
-
19
- /**
20
- * Randomly samples from the distribution represented by the bayesian network.
21
- * Can generate samples not found in the data used to create the definition file.
22
- * @param {object} inputValues - node values that are known already
23
- */
24
- generateSample(inputValues = {}) {
25
- const sample = inputValues;
26
- for (const node of this.nodesInSamplingOrder) {
27
- if (!(node.name in sample)) {
28
- sample[node.name] = node.sample(sample);
29
- }
30
- }
31
-
32
- return sample;
33
- }
34
-
35
- /**
36
- * Randomly samples from the distribution represented by the bayesian network.
37
- * Cannot generate samples not found in the data used to create the definition file,
38
- * so it only generates samples when it is possible to be consistent with the data.
39
- * @param {object} valuePossibilities - a dictionary of lists of possible values for nodes
40
- * (if a node isn't present in the dictionary, all values are possible)
41
- */
42
- generateConsistentSampleWhenPossible(valuePossibilities) {
43
- return this._recursivelyGenerateConsistentSampleWhenPossible({}, valuePossibilities, 0);
44
- }
45
-
46
- /**
47
- * Randomly samples from the distribution represented by the bayesian network
48
- * @param {object} sampleSoFar - node values that are known already
49
- * @param {object} valuePossibilities - a dictionary of lists of possible values for nodes
50
- * (if a node isn't present in the dictionary, all values are possible)
51
- * @param {number} depth - in what depth of the recursion this function call is,
52
- * specifies what node this function call is sampling
53
- * @private
54
- */
55
- _recursivelyGenerateConsistentSampleWhenPossible(sampleSoFar, valuePossibilities, depth) {
56
- const bannedValues = [];
57
- const node = this.nodesInSamplingOrder[depth];
58
-
59
- let sampleValue;
60
- do {
61
- sampleValue = node.sampleAccordingToRestrictions(sampleSoFar, valuePossibilities[node.name], bannedValues);
62
-
63
- if (!sampleValue) break;
64
-
65
- sampleSoFar[node.name] = sampleValue;
66
-
67
- if (depth + 1 < this.nodesInSamplingOrder.length) {
68
- const sample = this._recursivelyGenerateConsistentSampleWhenPossible(sampleSoFar, valuePossibilities, depth + 1);
69
- if (sample) {
70
- return sample;
71
- }
72
- } else {
73
- return sampleSoFar;
74
- }
75
-
76
- bannedValues.push(sampleValue);
77
- } while (sampleValue);
78
-
79
- return false;
80
- }
81
- }
82
-
83
- module.exports = {
84
- BayesianNetwork,
85
- };
@@ -1,103 +0,0 @@
1
- /**
2
- * BayesianNode is an implementation of a single node in a bayesian network allowing
3
- * sampling from its conditional distribution.
4
- */
5
- class BayesianNode {
6
- /**
7
- * @param {object} nodeDefinition - node structure and distributions definition
8
- * taken from the network definition file
9
- */
10
- constructor(nodeDefinition) {
11
- this.nodeDefinition = nodeDefinition;
12
- }
13
-
14
- /**
15
- * Extracts unconditional probabilities of node values given the values of the parent nodes
16
- * @param {object} parentValues - values of the parent nodes
17
- * @private
18
- */
19
- _getProbabilitiesGivenKnownValues(parentValues = {}) {
20
- let probabilities = this.nodeDefinition.conditionalProbabilities;
21
-
22
- for (const parentName of this.parentNames) {
23
- const parentValue = parentValues[parentName];
24
- if (parentValue in probabilities.deeper) {
25
- probabilities = probabilities.deeper[parentValue];
26
- } else {
27
- probabilities = probabilities.skip;
28
- }
29
- }
30
- return probabilities;
31
- }
32
-
33
- /**
34
- * Randomly samples from the given values using the given probabilities
35
- * @param {array} possibleValues - a list of values to sample from
36
- * @param {number} totalProbabilityOfPossibleValues - a sum of probabilities of possibleValues
37
- * in the conditional distribution
38
- * @param {object} probabilities - a dictionary of probabilities from the conditional distribution
39
- * indexed by the values
40
- * @private
41
- */
42
- _sampleRandomValueFromPossibilities(possibleValues, totalProbabilityOfPossibleValues, probabilities) {
43
- let chosenValue = possibleValues[0];
44
- const anchor = Math.random() * totalProbabilityOfPossibleValues;
45
- let cumulativeProbability = 0;
46
- for (const possibleValue of possibleValues) {
47
- cumulativeProbability += probabilities[possibleValue];
48
- if (cumulativeProbability > anchor) {
49
- chosenValue = possibleValue;
50
- break;
51
- }
52
- }
53
-
54
- return chosenValue;
55
- }
56
-
57
- /**
58
- * Randomly samples from the conditional distribution of this node given values of parents
59
- * @param {object} parentValues - values of the parent nodes
60
- */
61
- sample(parentValues = {}) {
62
- const probabilities = this._getProbabilitiesGivenKnownValues(parentValues);
63
- const possibleValues = Object.keys(probabilities);
64
-
65
- return this._sampleRandomValueFromPossibilities(possibleValues, 1.0, probabilities);
66
- }
67
-
68
- /**
69
- * Randomly samples from the conditional distribution of this node given restrictions on the possible
70
- * values and the values of the parents.
71
- * @param {object} parentValues - values of the parent nodes
72
- * @param {object} valuePossibilities - a list of possible values for this node
73
- * @param {object} bannedValues - what values of this node are banned
74
- */
75
- sampleAccordingToRestrictions(parentValues, valuePossibilities, bannedValues) {
76
- const probabilities = this._getProbabilitiesGivenKnownValues(parentValues);
77
- let totalProbability = 0.0;
78
- const validValues = [];
79
- const valuesInDistribution = Object.keys(probabilities);
80
- const possibleValues = valuePossibilities || valuesInDistribution;
81
- for (const value of possibleValues) {
82
- if (!bannedValues.includes(value) && valuesInDistribution.includes(value)) {
83
- validValues.push(value);
84
- totalProbability += probabilities[value];
85
- }
86
- }
87
-
88
- if (validValues.length === 0) return false;
89
- return this._sampleRandomValueFromPossibilities(validValues, totalProbability, probabilities);
90
- }
91
-
92
- get name() {
93
- return this.nodeDefinition.name;
94
- }
95
-
96
- get parentNames() {
97
- return this.nodeDefinition.parentNames;
98
- }
99
- }
100
-
101
- module.exports = {
102
- BayesianNode,
103
- };
package/src/main.js DELETED
@@ -1,3 +0,0 @@
1
- const BayesianNetwork = require('./bayesian-network');
2
-
3
- module.exports = BayesianNetwork;