Giter Club home page Giter Club logo

pykda's Introduction

PyPI version ALNS codecov

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) and uses the Kemeny constant as a connectivity measure.

Installing pykda

Package pykda depends on numpy, tarjan and pyvis. Use the package manager pip to install PyKDA

pip install pykda

Getting started

The first step is to load a Markov chain as a MarkovChain object using a transition matrix P.

from pykda.Markov_chain import MarkovChain

P = [[0, 0.3, 0.7, 0, 0],
     [0.7, 0.2, 0.1, 0, 0],
     [0.5, 0.25, 0.25, 0, 0],
     [0, 0, 0, 0.5, 0.5],
     [0, 0, 0, 0.75, 0.25]]  # artificial transition matrix
MC = MarkovChain(P)

We can study some properties of the Markov chain, such as the stationary distribution:

print(MC.stationary_distribution.flatten())

This gives [0.226 0.156 0.23 0.232 0.156]. We can also plot the Markov chain:

MC.plot(file_name="An artificial Markov chain")

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 or Berkhout and Heidergott (2019). Here, we apply the default settings, which is to cut all edges with a negative Kemeny constant derivative and normalizing the transition matrix afterward.

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.

kda.run()
kda.plot(file_name="An artificial Markov chain after KDA_A1_1_B3_0")

We can study the resulting Markov chain in more detail via the current Markov chain attribute MC of the KDA object.

print(kda.MC)

This gives the following output:

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.

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:

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):

How to learn more about pykda?

To learn more about pykda have a look at the documentation. There, you can also find links to interactive Google Colab notebooks in examples. If you have any questions, feel free to open an issue here on Github 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:

@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},
}

pykda's People

Contributors

joostberkhout avatar

Stargazers

Alessandro Zocca avatar Leon Lan avatar

Watchers

 avatar

pykda's Issues

Test in Python 3.12

Add to the testing workflow on Github that it is also tested in Python 3.12.

Feedback

@JoostBerkhout, ik dacht het is wel zo makkelijk om feedback op Github te doen - dan kan ik ook refereren naar code en kunnen we dit interactiever bespreken.

Docs

Code

  • Style:
    • Ik zou Markov_chain.py veranderen naar MarkovChain.py or markov_chain.py. Eerste is PascalCase' stijl en tweede is Python stijl. De huidige file name zit er tussenin en valt buiten de conventies.
  • Typing:
    • np.ndarray or list[list] or str -> Union[np.ndarray, list[list], str] of je kunt | gebruiken
    • Vanaf Python 3.9 kun je list gebruiken in plaats van typing.List. Zelfde met dict en tuple.
    • -> None output kun je weglaten, dat is namelijk impliciet
  • Features
    • Heb je cached_property echt nodig op je MarkovChain class? Ik ken je algoritme niet dus ik weet niet hoe vaak bepaalde properties worden aangeroepen. Maar MarkovChain heeft veel internal state en lijkt redelijk vaak te updaten. cached_property cachet een property, maar als je interne variabelen veranderen, dan wordt dit dus niet meer geupdated. Een robustere design keuze is om niet te cachen en om property te gebruiken.
    • normalizer argument wordt niet gebruikt in load_predefined_transition_matrix
      • In normalizer.py kun je normalizer_type = Callable[[np.ndarray], np.ndarray] het best bovenaan zetten (is meteen duidelijk) en hebt best met _ prefix odat je daarmee niet perongeluk user variables overschrijft. Dus _normalizer_type.
      • normalizer_type = Callable[[np.ndarray], np.ndarray]
    • Het KDA algoritme neemt nu als input een markov chain en slaat deze op, en past dit telkens aan als je run aanroept. Een robuustere interface is als volgt: je slaat de MC chain niet op, maar geeft dit mee als argument in run. Als output krijg je je nieuwe MC terug. Hetzelfde kun je doen met cut_edges.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.