https://github.com/apify/generative-bayesian-network
https://github.com/apify/generative-bayesian-network
Last synced: 8 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/apify/generative-bayesian-network
- Owner: apify
- License: apache-2.0
- Created: 2021-03-16T20:53:02.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2022-07-25T12:35:14.000Z (almost 4 years ago)
- Last Synced: 2025-07-18T09:02:24.343Z (11 months ago)
- Language: JavaScript
- Size: 116 KB
- Stars: 3
- Watchers: 5
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# (DEPRECATED) Generative bayesian network
---
**DEPRECATED** The `generative-bayesian-network` package now lives in the [fingerprint-suite](https://github.com/apify/fingerprint-suite) repository. This repository is no longer actively maintained.
---
NodeJs package containing a bayesian network capable of randomly sampling from a distribution defined by a json object.
- [Installation](#installation)
- [Usage](#usage)
- [API Reference](#api-reference)
## Installation
Run the `npm i generative-bayesian-network` command. No further setup is needed afterwards.
## Usage
To use the network, you need to create an instance of the `BayesianNetwork` class which is exported from this package. Constructor of this class accepts a JSON object containing the network definition. This definition can either include the probability distributions for the nodes, or these can be calculated later using data. An example of such a definition saved in a JSON file could look like:
```json
{
"nodes": [
{
"name": "ParentNode",
"values": ["A", "B", "C"],
"parentNames": [],
"conditionalProbabilities": {
"A": 0.1,
"B": 0.8,
"C": 0.1
}
},
{
"name": "ChildNode",
"values": [".", ",", "!", "?"],
"parentNames": ["ParentNode"],
"conditionalProbabilities": {
"A": {
".": 0.7,
"!": 0.3
},
"B": {
",": 0.3,
"?": 0.7
},
"C": {
".": 0.5,
"?": 0.5
}
}
}
]
}
```
Once you have the network definition ready, you can create an instance simply by executing:
```js
let generatorNetwork = new BayesianNetwork(networkDefinition);
```
If the network definition didn't include the probabilities, you also need to call the `setProbabilitiesAccordingToData` method and provide it with a [Danfo.js dataframe](https://danfo.jsdata.org/api-reference/dataframe) containing the dataset you want to be used to calculate the probabilities:
```js
generatorNetwork.setProbabilitiesAccordingToData(dataframe);
```
After the setup, you can save the current network's definition by doing:
```js
generatorNetwork.saveNetworkDefinition(networkDefinitionFilePath);
```
Once you have the network all set up, you can use two methods to actually generate the samples - `generateSample` and `generateConsistentSampleWhenPossible`. The first one generates a sample of all node values given (optionally) the values we already know in the form of an object. The second does much the same thing, but instead of just getting the known values of some of the attributes, the object you can give it as an argument can contain multiple possible values for each node, not just one. You could run them for example like this:
```js
let sample = generatorNetwork.generateSample({ "ParentNode": "A" });
let consistentSample = generatorNetwork.generateSample({
"ParentNode": ["A","B"], "ChildNode": [",","!"]
});
```
## API Reference
All public classes, methods and their parameters can be inspected in this API reference.
### BayesianNetwork
BayesianNetwork is an implementation of a bayesian network capable of randomly sampling from the distribution
represented by the network.
* [BayesianNetwork](#BayesianNetwork)
* [`new BayesianNetwork(networkDefinition)`](#new_BayesianNetwork_new)
* [`.generateSample(inputValues)`](#BayesianNetwork+generateSample)
* [`.generateConsistentSampleWhenPossible(valuePossibilities)`](#BayesianNetwork+generateConsistentSampleWhenPossible)
* [`.setProbabilitiesAccordingToData(dataframe)`](#BayesianNetwork+setProbabilitiesAccordingToData)
* [`.saveNetworkDefinition(networkDefinitionFilePath)`](#BayesianNetwork+saveNetworkDefinition)
* * *
#### `new BayesianNetwork(networkDefinition)`
| Param | Type | Description |
| --- | --- | --- |
| networkDefinition | object | object defining the network structure and distributions |
* * *
#### `bayesianNetwork.generateSample(inputValues)`
Randomly samples from the distribution represented by the bayesian network.
| Param | Type | Description |
| --- | --- | --- |
| inputValues | object | node values that are known already |
* * *
#### `bayesianNetwork.generateConsistentSampleWhenPossible(valuePossibilities)`
Randomly samples from the distribution represented by the bayesian network,
making sure the sample is consistent with the provided restrictions on value possibilities.
Returns false if no such sample can be generated.
| Param | Type | Description |
| --- | --- | --- |
| valuePossibilities | object | a dictionary of lists of possible values for nodes (if a node isn't present in the dictionary, all values are possible) |
* * *
#### `bayesianNetwork.setProbabilitiesAccordingToData(dataframe)`
Sets the conditional probability distributions of this network's nodes to match the given data.
| Param | Type | Description |
| --- | --- | --- |
| dataframe | object | a Danfo.js dataframe containing the data |
* * *
#### `bayesianNetwork.saveNetworkDefinition(networkDefinitionFilePath)`
Saves the network definition to the specified file path to be used later.
| Param | Type | Description |
| --- | --- | --- |
| networkDefinitionFilePath | string | a file path where the network definition should be saved |
* * *