pykda


Namepykda JSON
Version 0.9.3 PyPI version JSON
download
home_pagehttps://github.com/JoostBerkhout/PyKDA
SummaryPython package for the Kemeny Decomposition Algorithm (KDA) together with some Markov chain tooling.
upload_time2024-07-12 07:58:04
maintainerJoost Berkhout
docs_urlNone
authorJoost Berkhout
requires_python<4.0,>=3.10
licenseMIT
keywords markov chain kemeny decomposition algorithm kda kemeny constant
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            <img src="https://github.com/JoostBerkhout/PyKDA/blob/main/docs/images/logo.png" width="400">

[![PyPI version](https://badge.fury.io/py/pykda.svg)](https://badge.fury.io/py/pykda)
[![ALNS](https://github.com/JoostBerkhout/PyKDA/actions/workflows/PyKDA.yml/badge.svg)](https://github.com/JoostBerkhout/PyKDA/actions/workflows/PyKDA.yml)
[![codecov](https://codecov.io/gh/JoostBerkhout/PyKDA/graph/badge.svg?token=M4WF9A5ZML)](https://codecov.io/gh/JoostBerkhout/PyKDA)

`pykda` (you say "pie-k-d-a") is a Python package for the Kemeny Decomposition Algorithm (KDA) which 
allows to decompose a Markov chain into clusters of states, where states within
a cluster are relatively more connected to each other than states outside
the cluster. This is useful for analyzing influence graphs, such as social 
networks 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)
and uses the Kemeny constant as a connectivity measure. 

### Installing `pykda`

Package `pykda` depends on `numpy`, `tarjan` and `pyvis`.
Use the package manager [pip](https://pip.pypa.io/en/stable/) to install PyKDA
```bash
pip install pykda
```

### Getting started

The first step is to load a Markov chain as a `MarkovChain` object using a 
transition matrix `P`.
```python
from pykda.Markov_chain import MarkovChain

P = [[0, 0.3, 0.7, 0, 0],
     [0.7, 0.2, 0.09, 0, 0.01],
     [0.5, 0.25, 0.25, 0, 0],
     [0, 0, 0, 0.5, 0.5],
     [0.01, 0, 0, 0.74, 0.25]]  # artificial transition matrix
MC = MarkovChain(P)
```
We can study some properties of the Markov chain, such as the stationary distribution:
```python
print(MC.stationary_distribution.flatten().round(3))
```
This gives `[0.226 0.156 0.23  0.232 0.156]`. We can also plot the Markov chain:
```python
MC.plot(file_name="An artificial Markov chain")
```

<img src="https://github.com/JoostBerkhout/PyKDA/blob/main/docs/images/plot_readme_example.JPG" width="280">

Now, let us decompose the Markov chain into clusters using KDA. We start by
initializing a `KDA` object using the Markov chain and the KDA settings (such
as the number of clusters). For more details about setting choices, see the [KDA documentation](https://joostberkhout.github.io/PyKDA/references/KDA/)
or [Berkhout and Heidergott (2019)](https://research.vu.nl/ws/portalfiles/portal/104470560/Analysis_of_Markov_influence_graphs.pdf).
Here, we apply the default settings, which is to cut all edges with a negative
Kemeny constant derivative and normalizing the transition matrix afterward.
```python
kda = KDA(
    original_MC=MC, CO_A="CO_A_1(1)", CO_B="CO_B_3(0)", symmetric_cut=False
    )
```
Now, let us run the KDA algorithm and visualize the results.
```python
kda.run()
kda.plot(file_name="An artificial Markov chain after KDA_A1_1_B3_0")
```

<img src="https://github.com/JoostBerkhout/PyKDA/blob/main/docs/images/plot_readme_example_after_KDA_A1_1_B3_0.JPG" width="280">

We can study the resulting Markov chain in more detail via the current Markov chain
attribute `MC` of the `KDA` object.
```python
print(kda.MC)
```
This gives the following output:
```python
MC with 5 states.
Ergodic classes: [[2, 0], [3]].
Transient classes: [[1], [4]].
```
So KDA led to a Markov multi-chain with two ergodic classes and two transient classes.
We can also study the edges that KDA cut via the `log` attribute of the `KDA` object.
```python
print(kda.log['edges cut'])
```
This gives the following output:
```
[[None], [(4, 0), (1, 4), (2, 1), (0, 1), (3, 4)]]
```
We can also study the Markov chains that KDA found in each (outer) iteration via
 `kda.log['Markov chains']`)`.

As another KDA application example, let us apply KDA until we find two ergodic 
classes explicitly. We will also ensure that the Kemeny constant derivatives are
recalculated after each cut (and normalize the cut transition matrix to 
ensure it is a stochastic matrix again). To that end, we use:
```python
kda2 = KDA(
    original_MC=MC, CO_A="CO_A_2(2)", CO_B="CO_B_1(1)", symmetric_cut=False
    )
kda2.run()
kda2.plot(file_name="An artificial Markov chain after KDA_A2_2_B1_1")
```
which gives (edges (4, 0) and (1, 4) are cut in two iterations):

<img src="https://github.com/JoostBerkhout/PyKDA/blob/main/docs/images/plot_readme_example_after_KDA_A2_2_B1_1.JPG" width="280">

### How to learn more about `pykda`?
To learn more about `pykda` have a look at the [documentation](https://joostberkhout.github.io/PyKDA/). There, you can
also find links to interactive Google Colab notebooks in [examples](https://joostberkhout.github.io/PyKDA/examples/). If you
have any questions, feel free to open an issue here on [Github Issues](https://github.com/JoostBerkhout/PyKDA/issues).

### How to cite `pykda`?

If you use `pykda` in your research, please consider citing the following paper:

> Joost Berkhout, Bernd F. Heidergott (2019).
> Analysis of Markov influence graphs. 
> _Operations Research_, 67(3):892-904.
> https://doi.org/10.1287/opre.2018.1813

Or, using the following BibTeX entry:

```bibtex
@article{Berkhout_Heidergott_2019,
	title = {Analysis of {Markov} influence graphs},
	volume = {67},
	number = {3},
	journal = {Operations Research},
	author = {Berkhout, J. and Heidergott, B. F.},
	year = {2019},
	pages = {892--904},
}
```

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/JoostBerkhout/PyKDA",
    "name": "pykda",
    "maintainer": "Joost Berkhout",
    "docs_url": null,
    "requires_python": "<4.0,>=3.10",
    "maintainer_email": "joost.berkhout@vu.nl",
    "keywords": "Markov chain, Kemeny Decomposition Algorithm, KDA, Kemeny constant",
    "author": "Joost Berkhout",
    "author_email": "joost.berkhout@vu.nl",
    "download_url": "https://files.pythonhosted.org/packages/d8/3a/e495ec833677fb29df9013299db760af3fe9663e3d3e0674c9fa5d73c745/pykda-0.9.3.tar.gz",
    "platform": null,
    "description": "<img src=\"https://github.com/JoostBerkhout/PyKDA/blob/main/docs/images/logo.png\" width=\"400\">\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<img src=\"https://github.com/JoostBerkhout/PyKDA/blob/main/docs/images/plot_readme_example.JPG\" width=\"280\">\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<img src=\"https://github.com/JoostBerkhout/PyKDA/blob/main/docs/images/plot_readme_example_after_KDA_A1_1_B3_0.JPG\" width=\"280\">\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<img src=\"https://github.com/JoostBerkhout/PyKDA/blob/main/docs/images/plot_readme_example_after_KDA_A2_2_B1_1.JPG\" width=\"280\">\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> Joost Berkhout, Bernd F. Heidergott (2019).\n> Analysis of Markov influence graphs. \n> _Operations Research_, 67(3):892-904.\n> 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",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Python package for the Kemeny Decomposition Algorithm (KDA) together with some Markov chain tooling.",
    "version": "0.9.3",
    "project_urls": {
        "Documentation": "https://joostberkhout.github.io/PyKDA/",
        "Homepage": "https://github.com/JoostBerkhout/PyKDA",
        "Repository": "https://github.com/JoostBerkhout/PyKDA"
    },
    "split_keywords": [
        "markov chain",
        " kemeny decomposition algorithm",
        " kda",
        " kemeny constant"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "fdb120ed07581e8a9319b1d495cc95bce591188a5cba4471d327115d43d6e63b",
                "md5": "2605af51561e024c2c9c65e9d748ba5f",
                "sha256": "34deb38af8314c64432701f3e33ddebba9691f8996d65b79cfc338679c0ab3a0"
            },
            "downloads": -1,
            "filename": "pykda-0.9.3-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "2605af51561e024c2c9c65e9d748ba5f",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": "<4.0,>=3.10",
            "size": 76323,
            "upload_time": "2024-07-12T07:58:02",
            "upload_time_iso_8601": "2024-07-12T07:58:02.324461Z",
            "url": "https://files.pythonhosted.org/packages/fd/b1/20ed07581e8a9319b1d495cc95bce591188a5cba4471d327115d43d6e63b/pykda-0.9.3-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "d83ae495ec833677fb29df9013299db760af3fe9663e3d3e0674c9fa5d73c745",
                "md5": "2bbfe68927a9f61fcfefcef39e0842f7",
                "sha256": "a58ac934a038fd466f314122148622c8c7c7877dd7519f4cdc70e5d41353be28"
            },
            "downloads": -1,
            "filename": "pykda-0.9.3.tar.gz",
            "has_sig": false,
            "md5_digest": "2bbfe68927a9f61fcfefcef39e0842f7",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "<4.0,>=3.10",
            "size": 67927,
            "upload_time": "2024-07-12T07:58:04",
            "upload_time_iso_8601": "2024-07-12T07:58:04.046194Z",
            "url": "https://files.pythonhosted.org/packages/d8/3a/e495ec833677fb29df9013299db760af3fe9663e3d3e0674c9fa5d73c745/pykda-0.9.3.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-07-12 07:58:04",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "JoostBerkhout",
    "github_project": "PyKDA",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "pykda"
}
        
Elapsed time: 4.41788s