Coding a Web App to Simulate the Gambler’s Ruin Problem | by Claudia Ng | Towards Data Science

I find that simulations are a useful way to understand mathematical concepts, so I recently coded one to illustrate the gambler’s ruin problem. I made a web app to simulate a series of games and their outcomes in Python. In this web app, users define a set of parameters (probability of success per round (p), initial amount (i), goal amount (N) and number of games), and it will return the probability of winning as well as the balance at the end of every round for each game, as shown below.
In this article, I will walk through how to code a gambler’s ruin simulation web app broken down into 3 steps: I) coding the simulation in Python, II) visualizing results using Plotly Dash, and III) deploying the web app on Heroku.
The gambler’s ruin is a famous statistical concept that has practical applications in modern finance, such as pricing of stock options and insurance premiums. The concept is as follows:
You walk into a casino with an initial amount of $i, with the hope of increasing your fortune to $N. You pick a gambling game where your probability of success is p per round, and we assume that each round is independent and identically distributed (i.i.d.). If you are successful that round, one unit is added to your balance. If you lose that round, one unit is removed from your balance. The game ends when either your balance is $0 or reaches $N. What is your probability of winning/ reaching your goal of $N?
I wrote a GamblersRuin class for simulation purposes. When initializing the object, it takes probability p, initial amount i and goal amount N as arguments.
After initializing the class, you would call the n_simulations function, where you can specify the number of games for the simulation, if not it would default to 100. This function is essentially a for loop that plays the predefined number of games and calls the gamble function. It returns a dataframe with the balance per round for every game, which is later used for plotting out the results.
As for the gamble function mentioned above, it generates a random floating point number between 0 and 1 if the balance is between 0 and N. If the random number is less than probability p, then one unit is added to your balance, otherwise one unit is subtracted from your balance. This function returns a dataframe with the generated random float and the balance at each round.
After coding the gambler’s ruin class method in Python, we can now call it and visualize the results using Plotly Dash. This can be broken down into three parts: a) initiating the Dash app, b) setting up the layout of the app, c) specifying callback functions, in other words, inputs and information to display.
II.a.) Initiating the Dash app
The Plotly Dash app uses Flask as the web framework. While you can pass your own flask app instance into Dash, I kept it simple and called the underlying Flask app with app.server.
external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css'] app = dash.Dash(__name__, external_stylesheets=external_stylesheets) server = app.server
III.b.) Setting Up the Layout
To compose the layout for my Dash app, I used the dash-html-components library that abstracts writing a web framework into Python code. These components are converted into HTML for the web app behind the scenes, and you can also add HTML elements to the Python Dash classes likestyle to customize your layout.
In addition to using dash-html-components to customize my app layout, it is easy to add different components using the dash-core-components (dcc)library. I used dcc.Markdown to format text, dcc.Input to take user-specified inputs as arguments for my GamblersRuin class, and dcc.Graph to display the outcome.
III.c.) Callback Functions
As a last step, I specified the necessary callbacks based on user-submitted inputs. I also wrote the update_graph function to update the probability of win calculations and graph to be displayed, a Plotly line graph.
You can now test the Dash app on your local machine! If you run the app.py file, the Dash app should now run on your localhost. The last step is deploying the app to a server.
The deployment process using Heroku is relatively easy. Check out this guide for deploying to Heroku.
To begin, you need to create a Heroku account.
Define a requirements.txt file with the packages to run your Dash app and python files.
Define a Procfile (a text file in the root directory of your repo) to specify the command to start your app.
web: gunicorn app:server
Specify your github repo and branch for deployment. You can do this using the heroku CLI or their web interface.
Deploy!
In this tutorial, I walked through how to code, visualize and deploy a gambler’s ruin simulation web app. Here is a brief summary of the steps:
Code the simulation in Python as a class method that takes p, i, N as arguments and has functions to run a predefined number of simulations.Visualizing the results and outcome of the simuations using Plotly Dash.Deploying the Dash app on Heroku.
Code the simulation in Python as a class method that takes p, i, N as arguments and has functions to run a predefined number of simulations.
Visualizing the results and outcome of the simuations using Plotly Dash.
Deploying the Dash app on Heroku.
This web app took me roughly 5 hours from start to finish. I hope this article has inspired you to think about simulations that you could code, please feel free to comment below with your ideas!
Gambler’s Ruin Dash App
Github Repo
Deploying on Heroku with Python
Gambler’s Ruin Problem (Notes by Karl Sigman)