Skip to content

Commit af033d9

Browse files
authored
Create gamblers_ruin.py
1 parent 9d2548c commit af033d9

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed

gamblers_ruin.py

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
"""
2+
David vs. Goliath Gambler's Ruin Simulation
3+
4+
This program simulates a gambling scenario between two players: David and Goliath.
5+
David has a skill advantage, represented by a 55% probability of winning each round,
6+
while Goliath has a size advantage with a larger initial amount of money.
7+
8+
Assumptions:
9+
- David starts with $2,000, and Goliath starts with $10,000.
10+
- Each round of betting results in a transfer of $1,000 from the loser to the winner.
11+
- The game continues until one player runs out of money (i.e., their amount reaches zero).
12+
- The outcome of each round is determined by a random number generator, reflecting David's skill advantage.
13+
14+
Mathematics:
15+
- The simulation models a stochastic process where each round can be viewed as an independent Bernoulli trial:
16+
- David wins with a probability of 0.55.
17+
- Goliath wins with a probability of 0.45.
18+
- The expected outcomes can be analyzed using concepts from probability theory and stochastic processes.
19+
- The simulation runs for a specified number of trials to gather statistical data on how often David wins compared to Goliath.
20+
21+
Usage:
22+
1. Run the program in a Python environment.
23+
2. Input the desired number of simulations when prompted.
24+
3. The program will output the number of wins for both David and Goliath and display a bar chart of the results.
25+
26+
This simulation provides insights into how skill can offset size advantages in competitive scenarios.
27+
"""
28+
29+
import random
30+
import matplotlib.pyplot as plt
31+
32+
def gambler_ruin(david_initial, goliath_initial, david_win_prob, simulations):
33+
results = []
34+
35+
for _ in range(simulations):
36+
david_amount = david_initial
37+
goliath_amount = goliath_initial
38+
39+
while david_amount > 0 and goliath_amount > 0:
40+
# Simulate a single bet based on David's winning probability
41+
if random.random() < david_win_prob: # David wins
42+
david_amount += 1000
43+
goliath_amount -= 1000
44+
else: # Goliath wins
45+
david_amount -= 1000
46+
goliath_amount += 1000
47+
48+
# Record the result: True if David wins, False if Goliath wins
49+
results.append(david_amount > 0)
50+
51+
return results
52+
53+
def plot_results(results):
54+
wins = sum(results)
55+
losses = len(results) - wins
56+
57+
plt.bar(['David Wins', 'Goliath Wins'], [wins, losses], color=['blue', 'red'])
58+
plt.title('David vs. Goliath Simulation Results')
59+
plt.ylabel('Number of Simulations')
60+
plt.show()
61+
62+
def main():
63+
david_initial = 2000 # David's initial amount
64+
goliath_initial = 10000 # Goliath's initial amount
65+
david_win_prob = 0.51 # David's skill advantage (55%)
66+
simulations = int(input("Enter number of simulations: "))
67+
68+
results = gambler_ruin(david_initial, goliath_initial, david_win_prob, simulations)
69+
70+
print(f"\nResults after {simulations} simulations:")
71+
print(f"David Wins: {sum(results)}")
72+
print(f"Goliath Wins: {len(results) - sum(results)}")
73+
74+
plot_results(results)
75+
76+
if __name__ == "__main__":
77+
main()

0 commit comments

Comments
 (0)