{"id":18710767,"url":"https://github.com/apify/generative-bayesian-network","last_synced_at":"2025-11-03T16:30:54.339Z","repository":{"id":52663276,"uuid":"348488573","full_name":"apify/generative-bayesian-network","owner":"apify","description":null,"archived":false,"fork":false,"pushed_at":"2022-07-25T12:35:14.000Z","size":119,"stargazers_count":3,"open_issues_count":0,"forks_count":1,"subscribers_count":5,"default_branch":"main","last_synced_at":"2025-07-18T09:02:24.343Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/apify.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-03-16T20:53:02.000Z","updated_at":"2025-07-17T19:34:45.000Z","dependencies_parsed_at":"2022-08-22T03:00:54.699Z","dependency_job_id":null,"html_url":"https://github.com/apify/generative-bayesian-network","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/apify/generative-bayesian-network","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/apify%2Fgenerative-bayesian-network","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/apify%2Fgenerative-bayesian-network/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/apify%2Fgenerative-bayesian-network/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/apify%2Fgenerative-bayesian-network/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/apify","download_url":"https://codeload.github.com/apify/generative-bayesian-network/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/apify%2Fgenerative-bayesian-network/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":267011174,"owners_count":24020922,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-07-25T02:00:09.625Z","response_time":70,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-07T12:35:36.542Z","updated_at":"2025-11-03T16:30:54.299Z","avatar_url":"https://github.com/apify.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# (DEPRECATED) Generative bayesian network\n\n---\n\n**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.\n\n---\n\nNodeJs package containing a bayesian network capable of randomly sampling from a distribution defined by a json object.\n\n\u003c!-- toc --\u003e\n\n- [Installation](#installation)\n- [Usage](#usage)\n- [API Reference](#api-reference)\n\n\u003c!-- tocstop --\u003e\n\n## Installation\nRun the `npm i generative-bayesian-network` command. No further setup is needed afterwards.\n## Usage\nTo 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:\n```json\n{\n    \"nodes\": [\n        {\n            \"name\": \"ParentNode\",\n            \"values\": [\"A\", \"B\", \"C\"],\n            \"parentNames\": [],\n            \"conditionalProbabilities\": {\n                \"A\": 0.1,\n                \"B\": 0.8,\n                \"C\": 0.1\n            }\n        },\n        {\n            \"name\": \"ChildNode\",\n            \"values\": [\".\", \",\", \"!\", \"?\"],\n            \"parentNames\": [\"ParentNode\"],\n            \"conditionalProbabilities\": {\n                \"A\": {\n                    \".\": 0.7,\n                    \"!\": 0.3\n                },\n                \"B\": {\n                    \",\": 0.3,\n                    \"?\": 0.7\n                },\n                \"C\": {\n                    \".\": 0.5,\n                    \"?\": 0.5\n                }\n            }\n        }\n    ]\n}\n```\nOnce you have the network definition ready, you can create an instance simply by executing:\n```js\nlet generatorNetwork = new BayesianNetwork(networkDefinition);\n```\nIf 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:\n```js\ngeneratorNetwork.setProbabilitiesAccordingToData(dataframe);\n```\nAfter the setup, you can save the current network's definition by doing:\n```js\ngeneratorNetwork.saveNetworkDefinition(networkDefinitionFilePath);\n```\nOnce 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:\n```js\nlet sample = generatorNetwork.generateSample({ \"ParentNode\": \"A\" });\nlet consistentSample = generatorNetwork.generateSample({\n    \"ParentNode\": [\"A\",\"B\"], \"ChildNode\": [\",\",\"!\"]\n});\n```\n\n## API Reference\nAll public classes, methods and their parameters can be inspected in this API reference.\n\n\u003ca name=\"BayesianNetwork\"\u003e\u003c/a\u003e\n\n### BayesianNetwork\nBayesianNetwork is an implementation of a bayesian network capable of randomly sampling from the distribution\nrepresented by the network.\n\n\n* [BayesianNetwork](#BayesianNetwork)\n    * [`new BayesianNetwork(networkDefinition)`](#new_BayesianNetwork_new)\n    * [`.generateSample(inputValues)`](#BayesianNetwork+generateSample)\n    * [`.generateConsistentSampleWhenPossible(valuePossibilities)`](#BayesianNetwork+generateConsistentSampleWhenPossible)\n    * [`.setProbabilitiesAccordingToData(dataframe)`](#BayesianNetwork+setProbabilitiesAccordingToData)\n    * [`.saveNetworkDefinition(networkDefinitionFilePath)`](#BayesianNetwork+saveNetworkDefinition)\n\n\n* * *\n\n\u003ca name=\"new_BayesianNetwork_new\"\u003e\u003c/a\u003e\n\n#### `new BayesianNetwork(networkDefinition)`\n\n| Param | Type | Description |\n| --- | --- | --- |\n| networkDefinition | \u003ccode\u003eobject\u003c/code\u003e | object defining the network structure and distributions |\n\n\n* * *\n\n\u003ca name=\"BayesianNetwork+generateSample\"\u003e\u003c/a\u003e\n\n#### `bayesianNetwork.generateSample(inputValues)`\nRandomly samples from the distribution represented by the bayesian network.\n\n\n| Param | Type | Description |\n| --- | --- | --- |\n| inputValues | \u003ccode\u003eobject\u003c/code\u003e | node values that are known already |\n\n\n* * *\n\n\u003ca name=\"BayesianNetwork+generateConsistentSampleWhenPossible\"\u003e\u003c/a\u003e\n\n#### `bayesianNetwork.generateConsistentSampleWhenPossible(valuePossibilities)`\nRandomly samples from the distribution represented by the bayesian network,\nmaking sure the sample is consistent with the provided restrictions on value possibilities.\nReturns false if no such sample can be generated.\n\n\n| Param | Type | Description |\n| --- | --- | --- |\n| valuePossibilities | \u003ccode\u003eobject\u003c/code\u003e | a dictionary of lists of possible values for nodes                                      (if a node isn't present in the dictionary, all values are possible) |\n\n\n* * *\n\n\u003ca name=\"BayesianNetwork+setProbabilitiesAccordingToData\"\u003e\u003c/a\u003e\n\n#### `bayesianNetwork.setProbabilitiesAccordingToData(dataframe)`\nSets the conditional probability distributions of this network's nodes to match the given data.\n\n\n| Param | Type | Description |\n| --- | --- | --- |\n| dataframe | \u003ccode\u003eobject\u003c/code\u003e | a Danfo.js dataframe containing the data |\n\n\n* * *\n\n\u003ca name=\"BayesianNetwork+saveNetworkDefinition\"\u003e\u003c/a\u003e\n\n#### `bayesianNetwork.saveNetworkDefinition(networkDefinitionFilePath)`\nSaves the network definition to the specified file path to be used later.\n\n\n| Param | Type | Description |\n| --- | --- | --- |\n| networkDefinitionFilePath | \u003ccode\u003estring\u003c/code\u003e | a file path where the network definition should be saved |\n\n\n* * *\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fapify%2Fgenerative-bayesian-network","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fapify%2Fgenerative-bayesian-network","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fapify%2Fgenerative-bayesian-network/lists"}