{"id":29583577,"url":"https://github.com/joostberkhout/pykda","last_synced_at":"2025-07-19T23:14:15.010Z","repository":{"id":224206862,"uuid":"762633121","full_name":"JoostBerkhout/PyKDA","owner":"JoostBerkhout","description":"Python package for the Kemeny Decomposition Algorithm (KDA) together with some Markov chain tooling.","archived":false,"fork":false,"pushed_at":"2024-07-12T08:10:07.000Z","size":2152,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-06-22T02:03:24.369Z","etag":null,"topics":["kda","kemeny-constant","kemeny-decomposition-algorithm","markov-chain"],"latest_commit_sha":null,"homepage":"https://joostberkhout.github.io/PyKDA/","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/JoostBerkhout.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":"CITATION.cff","codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2024-02-24T08:52:46.000Z","updated_at":"2025-06-21T15:39:18.000Z","dependencies_parsed_at":null,"dependency_job_id":"23cb2bde-a0f7-4f7b-867d-bbb654634fc0","html_url":"https://github.com/JoostBerkhout/PyKDA","commit_stats":null,"previous_names":["joostberkhout/pykda"],"tags_count":11,"template":false,"template_full_name":null,"purl":"pkg:github/JoostBerkhout/PyKDA","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JoostBerkhout%2FPyKDA","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JoostBerkhout%2FPyKDA/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JoostBerkhout%2FPyKDA/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JoostBerkhout%2FPyKDA/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/JoostBerkhout","download_url":"https://codeload.github.com/JoostBerkhout/PyKDA/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JoostBerkhout%2FPyKDA/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":266039285,"owners_count":23867858,"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","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":["kda","kemeny-constant","kemeny-decomposition-algorithm","markov-chain"],"created_at":"2025-07-19T23:14:12.380Z","updated_at":"2025-07-19T23:14:15.001Z","avatar_url":"https://github.com/JoostBerkhout.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cimg src=\"https://github.com/JoostBerkhout/PyKDA/blob/main/docs/images/logo.png\" width=\"400\"\u003e\n\n[![PyPI version](https://badge.fury.io/py/pykda.svg)](https://badge.fury.io/py/pykda)\n[![ALNS](https://github.com/JoostBerkhout/PyKDA/actions/workflows/PyKDA.yml/badge.svg)](https://github.com/JoostBerkhout/PyKDA/actions/workflows/PyKDA.yml)\n[![codecov](https://codecov.io/gh/JoostBerkhout/PyKDA/graph/badge.svg?token=M4WF9A5ZML)](https://codecov.io/gh/JoostBerkhout/PyKDA)\n\n`pykda` (you say \"pie-k-d-a\") is a Python package for the Kemeny Decomposition Algorithm (KDA) which \nallows to decompose a Markov chain into clusters of states, where states within\na cluster are relatively more connected to each other than states outside\nthe cluster. This is useful for analyzing influence graphs, such as social \nnetworks and internet networks. KDA was developed in the paper from [Berkhout and Heidergott (2019)](https://research.vu.nl/ws/portalfiles/portal/104470560/Analysis_of_Markov_influence_graphs.pdf)\nand uses the Kemeny constant as a connectivity measure. \n\n### Installing `pykda`\n\nPackage `pykda` depends on `numpy`, `tarjan` and `pyvis`.\nUse the package manager [pip](https://pip.pypa.io/en/stable/) to install PyKDA\n```bash\npip install pykda\n```\n\n### Getting started\n\nThe first step is to load a Markov chain as a `MarkovChain` object using a \ntransition matrix `P`.\n```python\nfrom pykda.Markov_chain import MarkovChain\n\nP = [[0, 0.3, 0.7, 0, 0],\n     [0.7, 0.2, 0.09, 0, 0.01],\n     [0.5, 0.25, 0.25, 0, 0],\n     [0, 0, 0, 0.5, 0.5],\n     [0.01, 0, 0, 0.74, 0.25]]  # artificial transition matrix\nMC = MarkovChain(P)\n```\nWe can study some properties of the Markov chain, such as the stationary distribution:\n```python\nprint(MC.stationary_distribution.flatten().round(3))\n```\nThis gives `[0.226 0.156 0.23  0.232 0.156]`. We can also plot the Markov chain:\n```python\nMC.plot(file_name=\"An artificial Markov chain\")\n```\n\n\u003cimg src=\"https://github.com/JoostBerkhout/PyKDA/blob/main/docs/images/plot_readme_example.JPG\" width=\"280\"\u003e\n\nNow, let us decompose the Markov chain into clusters using KDA. We start by\ninitializing a `KDA` object using the Markov chain and the KDA settings (such\nas the number of clusters). For more details about setting choices, see the [KDA documentation](https://joostberkhout.github.io/PyKDA/references/KDA/)\nor [Berkhout and Heidergott (2019)](https://research.vu.nl/ws/portalfiles/portal/104470560/Analysis_of_Markov_influence_graphs.pdf).\nHere, we apply the default settings, which is to cut all edges with a negative\nKemeny constant derivative and normalizing the transition matrix afterward.\n```python\nkda = KDA(\n    original_MC=MC, CO_A=\"CO_A_1(1)\", CO_B=\"CO_B_3(0)\", symmetric_cut=False\n    )\n```\nNow, let us run the KDA algorithm and visualize the results.\n```python\nkda.run()\nkda.plot(file_name=\"An artificial Markov chain after KDA_A1_1_B3_0\")\n```\n\n\u003cimg src=\"https://github.com/JoostBerkhout/PyKDA/blob/main/docs/images/plot_readme_example_after_KDA_A1_1_B3_0.JPG\" width=\"280\"\u003e\n\nWe can study the resulting Markov chain in more detail via the current Markov chain\nattribute `MC` of the `KDA` object.\n```python\nprint(kda.MC)\n```\nThis gives the following output:\n```python\nMC with 5 states.\nErgodic classes: [[2, 0], [3]].\nTransient classes: [[1], [4]].\n```\nSo KDA led to a Markov multi-chain with two ergodic classes and two transient classes.\nWe can also study the edges that KDA cut via the `log` attribute of the `KDA` object.\n```python\nprint(kda.log['edges cut'])\n```\nThis gives the following output:\n```\n[[None], [(4, 0), (1, 4), (2, 1), (0, 1), (3, 4)]]\n```\nWe can also study the Markov chains that KDA found in each (outer) iteration via\n `kda.log['Markov chains']`)`.\n\nAs another KDA application example, let us apply KDA until we find two ergodic \nclasses explicitly. We will also ensure that the Kemeny constant derivatives are\nrecalculated after each cut (and normalize the cut transition matrix to \nensure it is a stochastic matrix again). To that end, we use:\n```python\nkda2 = KDA(\n    original_MC=MC, CO_A=\"CO_A_2(2)\", CO_B=\"CO_B_1(1)\", symmetric_cut=False\n    )\nkda2.run()\nkda2.plot(file_name=\"An artificial Markov chain after KDA_A2_2_B1_1\")\n```\nwhich gives (edges (4, 0) and (1, 4) are cut in two iterations):\n\n\u003cimg src=\"https://github.com/JoostBerkhout/PyKDA/blob/main/docs/images/plot_readme_example_after_KDA_A2_2_B1_1.JPG\" width=\"280\"\u003e\n\n### How to learn more about `pykda`?\nTo learn more about `pykda` have a look at the [documentation](https://joostberkhout.github.io/PyKDA/). There, you can\nalso find links to interactive Google Colab notebooks in [examples](https://joostberkhout.github.io/PyKDA/examples/). If you\nhave any questions, feel free to open an issue here on [Github Issues](https://github.com/JoostBerkhout/PyKDA/issues).\n\n### How to cite `pykda`?\n\nIf you use `pykda` in your research, please consider citing the following paper:\n\n\u003e Joost Berkhout, Bernd F. Heidergott (2019).\n\u003e Analysis of Markov influence graphs. \n\u003e _Operations Research_, 67(3):892-904.\n\u003e https://doi.org/10.1287/opre.2018.1813\n\nOr, using the following BibTeX entry:\n\n```bibtex\n@article{Berkhout_Heidergott_2019,\n\ttitle = {Analysis of {Markov} influence graphs},\n\tvolume = {67},\n\tnumber = {3},\n\tjournal = {Operations Research},\n\tauthor = {Berkhout, J. and Heidergott, B. F.},\n\tyear = {2019},\n\tpages = {892--904},\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjoostberkhout%2Fpykda","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjoostberkhout%2Fpykda","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjoostberkhout%2Fpykda/lists"}