Simulations in Python

Simulations are useful for simulating events many times without actually doing them. By using python libraries such as random, we can analyze the outcomes and behavior of these events in a virtual setting, saving us the trouble from tedious experiments.

For example, what should we do if we want to get the power of three for a range of numbers? It would be too tedious to calculate this one by one. By introducing a for loop, declaring a placeholder variable 'i', and computing the power of three raised to the value of 'i', we can skillfully simplify this task to a few lines of code. Additionally, by putting the outcome into a dataframe, we can enhance clearity and facilitate better analysis.

The overall structure is as follows:

1. Initializing an empty list: We start by creating an empty list named data, so that we can store the data inside.

2. Iterating through a range: We utilize a for loop to iterate through a specific number of times. In this case, we input the argument 10 into range() for simplicity, so the loop variable 'i' will range from 0 to 9.

3. Appending data to the list: We append the calculated power of 3 to the data list as a dictionary with the key 'power' and the corresponding value.

4. Transforming the list into a DataFrame: After all data is collected, we exit the for loop and transform the list data into a DataFrame using the pd.DataFrame() function from the Pandas library.

A detailed explanation of for loops can be found as follows:

Random library

The random library is very useful because it allows us to generate random numbers, sample elements from certain distribution or population, shuffle the whole array, and so on! Below is a highly concise showcase of some basic functions in random library.

A more detailed explanation of random library can be found as follows:

For example, let's generate a random integer from 1 to 6 ten times and see the results:

By calling the randint function of random, we can generate an integer in a specified range, which is 1-6 in this case. Each iteration of the loop will generate a random number between 1 and 6 and append it to data, allowing us to simulate random events such as rolling a die or flipping a coin.

If we run this code cell again, the result will be different than the first time:

Simple Simulations (Die Roll)

What we did above was basically how to create a simulation for rolling a die! We just need to add a few more elements, such as the column name, and we will have created our first simulation.

Let's simulate rolling a six-sided die 50 times:

Here, we created a column called roll, attached the integer of our die roll to it, and added it to data in each iteration. This way, the columns of the simulation can have meaningful names and help you understand the simulation better.

Simple Simulations (Coin Toss)

We can also simulate events where the outcome for each iteration is not a number. For this we will have to use random.choice.

random.choice allows us to choose from a list, in this case between heads and tails. We can add more into the list if we wanted to simulate other events with more possible outcomes.

Simulations with Conditionals

Imagine a game where a random integer between 1 and 10 is rolled and you win if the outcome is a 7. It can be simulated using the following code. From the dataframe, all the available result is surprisingly a "lose". Since the probability of winning in each trail is 0.1, this is totally feasible and a likely outcome.

Complex Simulations (One-Dimension Random Walk)

Random walk describes a path consisting of a series of random steps. In its simplest form, a one-dimensional random walk takes place on a straight line. At each step, the walker moves either to the left or to the right with equal probability of 0.5. Additionally, position of the walker after each step is determined only by the previous position, also known as the memoryless property. Mathematically, if $x_i$ represents the position of the walker after $i$ steps, then $x_i=x_i−1+ϵ_i$, where $ϵ_i$ is a random variable representing the step taken at step $i$. Random walk is a extremely simple case of Markov Property and Stochastic Process.

To make a more vivid, we can plot the following graph.

Complex Simulations (Drawing Poker Card with Replacement)

Let's simulate drawing 20 cards with replacement from a standard deck of 52 playing cards. Each card has a suit (hearts, diamonds, clubs, or spades) and a rank (2 to 10, plus Jack, Queen, King, Ace). Since each individual card can be expressed by a suit and a rank, we'll randomly select one suit and one rank for each draw. Therefore, we can come up with the following simulation:

Additional explanations for simple simulations can be found here:

Python Documentation

Click Here for the full python documentation for random.