Gambler’s ruin — a Markov process analysis (student stuff) .[embedded content] Below you will find a little Python script yours truly made to simulate a betting scenario as a Markov process and visualise how the total amount of money changes over time. This model highlights that, due to a higher probability of losing, the total money will generally trend downwards over a large number of bets. import numpy as np # Define the transition matrix B B = np.array([ [1, 0, 0, 0], [0.51, 0, 0.49, 0], [0, 0.51, 0, 0.49], [0, 0, 0, 1] ]) # Define the initial state vector v25 v25 = np.array([0, 1, 0, 0]) # Function to compute the state vector after n steps def markov_steps(B, v, n): state_vector = v for _ in range(n): state_vector = state_vector.dot(B) return
Topics:
Lars Pålsson Syll considers the following as important: Statistics & Econometrics
This could be interesting, too:
Lars Pålsson Syll writes What statistics teachers get wrong!
Lars Pålsson Syll writes Statistical uncertainty
Lars Pålsson Syll writes The dangers of using pernicious fictions in statistics
Lars Pålsson Syll writes Interpreting confidence intervals
Gambler’s ruin — a Markov process analysis (student stuff)
.
Below you will find a little Python script yours truly made to simulate a betting scenario as a Markov process and visualise how the total amount of money changes over time. This model highlights that, due to a higher probability of losing, the total money will generally trend downwards over a large number of bets.
import numpy as np
# Define the transition matrix B
B = np.array([
[1, 0, 0, 0],
[0.51, 0, 0.49, 0],
[0, 0.51, 0, 0.49],
[0, 0, 0, 1]
])
# Define the initial state vector v25
v25 = np.array([0, 1, 0, 0])
# Function to compute the state vector after n steps
def markov_steps(B, v, n):
state_vector = v
for _ in range(n):
state_vector = state_vector.dot(B)
return state_vector
# Number of steps to simulate
num_steps = 25
# Compute the state vector after 25 steps
v_final = markov_steps(B, v25, num_steps)
# Print the final state vector
print(f”State vector after {num_steps} steps: {v_final}”)
# Optional: Plot the evolution of the state vector
import matplotlib.pyplot as plt
# Compute the state vector evolution
state_vectors = [v25]
for i in range(1, num_steps + 1):
state_vectors.append(markov_steps(B, v25, i))
# Convert to numpy array for easier plotting
state_vectors = np.array(state_vectors)
# Plot the evolution of the state vector
plt.plot(state_vectors)
plt.xlabel(‘Number of Steps’)
plt.ylabel(‘State Probability’)
plt.title(‘Markov Process: Evolution of State Vector’)
plt.legend([‘State 1’, ‘State 2’, ‘State 3’, ‘State 4’])
plt.show()