Gambler’s ruin — a Markov process analysis (student stuff)

11 Jun, 2024 at 12:55 | Posted in Statistics & Econometrics | 1 Comment

.

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

1 Comment »

RSS feed for comments on this post. TrackBack URI

  1. May one inquire how much hubris is needed to write of a good model that “the assumptions made are not unrealistic in the wrong way or for the wrong reasons”, yet present here a model that assumes away the insurance, market-making, and credit provisioning of a financial sector (not to mention the fraud!)?

    .

    Even if you start with a DSGE model, but you properly scale a financial sector as in https://ideas.repec.org/p/bis/biswps/890.html , will you find that because the financial sector dwarfs all the other silly assumptions you end up making good predictions?


Leave a comment

Blog at WordPress.com.
Entries and Comments feeds.