{"id":21206226,"url":"https://github.com/rtmigo/markov_walk_py","last_synced_at":"2026-05-21T05:03:19.129Z","repository":{"id":138533057,"uuid":"345451355","full_name":"rtmigo/markov_walk_py","owner":"rtmigo","description":"🔢 Python module that calculates probabilities for a random walk in 1-dimensional discrete state space","archived":false,"fork":false,"pushed_at":"2022-05-06T20:12:19.000Z","size":30,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-01-21T15:32:17.778Z","etag":null,"topics":["absorbing-markov-chains","absorbing-states","markov-chain","mathematics","numpy","probability","probability-theory","random-walk","stochastic-matrix","stochastic-models"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/rtmigo.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-03-07T20:56:42.000Z","updated_at":"2022-05-06T20:12:22.000Z","dependencies_parsed_at":"2023-07-06T16:00:28.866Z","dependency_job_id":null,"html_url":"https://github.com/rtmigo/markov_walk_py","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rtmigo%2Fmarkov_walk_py","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rtmigo%2Fmarkov_walk_py/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rtmigo%2Fmarkov_walk_py/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rtmigo%2Fmarkov_walk_py/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rtmigo","download_url":"https://codeload.github.com/rtmigo/markov_walk_py/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243658274,"owners_count":20326467,"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":["absorbing-markov-chains","absorbing-states","markov-chain","mathematics","numpy","probability","probability-theory","random-walk","stochastic-matrix","stochastic-models"],"created_at":"2024-11-20T20:54:54.722Z","updated_at":"2026-05-21T05:03:14.090Z","avatar_url":"https://github.com/rtmigo.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Actions Status](https://github.com/rtmigo/markov_walk/workflows/unit%20test/badge.svg?branch=master)](https://github.com/rtmigo/markov_walk/actions)\n[![Generic badge](https://img.shields.io/badge/Python-3.8+-blue.svg)](#)\n\n# [markov walk](https://github.com/rtmigo/markov_walk#readme)\n\nThis module solves a particular mathematical problem related to probability theory.\n\n-----\n\nLet's say a completely drunk passenger, while on a train, is trying to find his car. The cars are\nconnected, so he wanders between them. Some transitions between cars attract him more, and some\nless.\n\nAt the very beginning and at the very end of the train there are ticket collectors: if they meet a\ndrunkard, they will kick him out of the train.[*](#note1)\n\nWe know which car the drunkard is in now. The questions are:\n\n- What is the likelihood that he will be thrown out by the ticket collector standing at the\n  beginning of the train, and not at the end?\n\n- What is the likelihood that he will at least visit his car before being kicked out?\n\n# On a sober head\n\nWe are dealing with a discrete 1D random walk. At each state, we have different probabilities of\nmaking step to the left or to the right.\n\n| States        |   L   |   0   |  \u003c 1 \u003e  |    2  |   3   |   4   |   5   |   R   |\n|---------------|-------|-------|---------|-------|-------|-------|-------|-------|\n| P(move right) |  ...  |**0.3**|    0.5  |**0.7**|  0.4  |  0.8  |  0.9  |  ...  |\n| P(move left)  |  ...  |  0.7  |    0.5  |  0.3  |  0.6  |  0.2  |  0.1  |  ...  |\n\nThe probability to get from state `1` to state `2` is `0.7`.\n\nThe probability to get from state `1` to state `0` is `0.3`.\n\nSuppose the motion begins at state `1`. What is the exact probability that we will get to state `R`\nbefore we get to state `L`? What is the probability we will get to state `3` before `L` and `R`?\n\n# What the module does\n\nIt uses [Absorbing Markov chains](https://en.wikipedia.org/wiki/Absorbing_Markov_chain) to solve the\nproblem. It performs all matrix calculations in `numpy` and returns the answers as `float` numbers.\n\n# How to install\n\n```bash\ncd /abc/your_project\n\n# clone the module to /abc/your_project/markov_walk\nsvn export https://github.com/rtmigo/markov_walk/trunk/markov_walk markov_walk\n\n# install dependencies\npip3 install numpy\n``` \n\nNow you can `import markov_walk` from `/abc/your_project/your_module.py`\n\n# How to use\n\n```python3\nfrom markov_walk import MarkovWalk\n\nstep_right_probs = [0.3, 0.5, 0.7, 0.4, 0.8, 0.9]\nwalk = MarkovWalk(step_right_probs)\n\n# the motion begins at state 2. \n# How can we calculate the probability that we will get to state R before we get to state L?\nprint(walk.right_edge_probs[2])\n\n# the motion begins at state 1.\n# What is the probability we will get to state 3 before L and R? \nprint(walk.ever_reach_probs[1][3])\n\n```\n\n- `walk.ever_reach_probs[start_state][end_state]` is the probability, that after infinite wandering\n  started at `start_state` we will ever reach the point `end_state`\n\n- `walk.right_edge_probs[state]` is the probability for a starting `state`, that after infinite\n  wandering we will leave the table on the right, and not on the left\n\nBy states we mean `int` indexes in `step_right_probs`.\n\n-----\n\u003ca name=\"note1\"\u003e*\u003c/a\u003e Perhaps you are worried about why the ticket collectors are going to kick the\npassenger out. The reason is that he is traveling on a lottery ticket.\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frtmigo%2Fmarkov_walk_py","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frtmigo%2Fmarkov_walk_py","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frtmigo%2Fmarkov_walk_py/lists"}