Skip to content

Commit b5d37ef

Browse files
committed
Markov Chain: very first attempt
1 parent 7972926 commit b5d37ef

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

graph/markov_chain.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import random
2+
3+
my_chain = {
4+
'A': {'A': 0.6,
5+
'E': 0.4},
6+
'E': {'A': 0.7,
7+
'E': 0.3}
8+
}
9+
10+
def __choose_state(state_map):
11+
choice = random.random()
12+
probability_reached = 0
13+
for state, probability in state_map.items():
14+
probability_reached += probability
15+
if probability_reached > choice:
16+
return state
17+
18+
def next_state(chain, current_state):
19+
next_state_map = chain.get(current_state)
20+
next_state = __choose_state(next_state_map)
21+
return next_state

0 commit comments

Comments
 (0)