This simulation was inspired by the random-walk based epidemiological model. The idea is to simulate the spread of gossip inside a social network, built using NumPy. Feel free to copy, share, or modify the code for your own purposes!
You can use the test_gossip.py file to simulate specific scenarios. Play around with the parameters! If you want to change the core logic, look into gossip_network.py.
I was looking for a fun mathematical puzzle, and I remembered one of my undergrad courses on which I worked with a similar model for simulating a classic epidemic. I then thought: could this be used to simulate something less scary, and more keen to the general public? And the answer is yes! Everyone has seen a gossip spread around at least once. Which is why I dedicated some time to thinking:
- What are the elements and dynamics of gossip?
- How does gossip evolve over time?
- How could I use these insights to build a simulation?
First of all, for gossip to exist, we need people. They can be simulated using particles, both in a 2D or 3D space. For simplicity, I stuck with 2D. (Plus theres also one strange property of random walks in 3D which states that they may never return to their starting point, which you can view a nice video about here), that could have some funny implications for long simulations.
People aren't simply spreading gossips around just because; they usually do this within their social circle, which is why to simulate this, we would need a social network. This can be built simply using an adjacency matrix, let's call it
The
Now, we can incorporate the social aspect into the simulation by only allowing particles with adjacencies to infect each other.
Gossips evolve over time, thus the willingness of spreading them also does. This behavior can easily be simulated using an exponential decrease function:
This time is not the same for all particles, as it must depend on when exactly did they hear about the gossip. In other words, only because your best friend learned about the gossip as soon as possible, doesn't mean everyone would too. The time is relative for each subject.
The bare essentials to use this simulation are:
- The social network instance. Must be of type np.ndarray and symmetric.
- The particles instance, which itself contains some more optional parameters.
Additionally, you may provide some or all of the following:
infection_radius: float
movement_radius: float
infected_particles: np.ndarray
spread_rate: float
spread_probabilities: np.ndarray
iterations: int
current_iteration: int
infection_time: np.ndarray
title: str
Once you instantiate your Gossip instance, you can either manually control the iterations and use the functions I provided, or whatever your favorite graphics library is, or you can actually animate the whole process using the
Gossip.animate_system_evolution()
member function.
To run 4 basic scenarios and animate them, execute the following command:
python test_gossip.py
This file contains easily updatable parameters for you to play around with. Full disclosure, this is not optimized code by far, so simulations will take a while.
To install the required dependencies, run:
pip install -r requirements.txt
This project is licensed under the MIT License.