Skip to content

Commit

Permalink
Merge pull request keon#38 from probablytom/master
Browse files Browse the repository at this point in the history
Added a Markov Chain implementation
  • Loading branch information
keon authored Sep 13, 2017
2 parents 281b925 + 73c131e commit 6b9f75d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ Minimal and clean example implementations of data structures and algorithms in P
- [find_path](graph/find_path.py)
- [graph](graph/graph.py)
- [traversal](graph/traversal.py)
- [markov_chain](graph/markov_chain.py)
- [heap](heap)
- [merge_sorted_k_lists](heap/merge_sorted_k_lists.py)
- [skyline](heap/skyline.py)
Expand Down
26 changes: 26 additions & 0 deletions graph/markov_chain.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import random

my_chain = {
'A': {'A': 0.6,
'E': 0.4},
'E': {'A': 0.7,
'E': 0.3}
}

def __choose_state(state_map):
choice = random.random()
probability_reached = 0
for state, probability in state_map.items():
probability_reached += probability
if probability_reached > choice:
return state

def next_state(chain, current_state):
next_state_map = chain.get(current_state)
next_state = __choose_state(next_state_map)
return next_state

def iterating_markov_chain(chain, state):
while True:
state = next_state(chain, state)
yield state

0 comments on commit 6b9f75d

Please sign in to comment.