Simulating Percolation in Python: How Do Wildfires & Diseases Spread?

NeuralNineAbout 3 min readJun 10, 2026Watch original
THE SUMMARYAI-generated

Key Concepts

  • Percolation: A mathematical concept describing the movement or spreading of a phenomenon (like a disease or wildfire) through a lattice or network.
  • Percolation Threshold: A critical probability value (approximately 59.27% for a 2D square lattice) at which a system transitions from being contained to spanning the entire grid.
  • Lattice: A 2D grid structure used to represent the environment where the simulation occurs.
  • Frontier: A list of currently infected nodes whose neighbors are checked for potential infection in the next iteration.
  • Matplotlib Animation: A library used to visualize the step-by-step progression of the simulation.
  • Uncertainty Window: A range around the threshold where the outcome (spanning vs. containment) is highly sensitive to random initial conditions.

1. Main Topics and Methodology

The video demonstrates how to simulate spreading processes using Python, specifically focusing on the mathematical phenomenon of percolation. The simulation uses a 2D grid (lattice) where cells are either empty, healthy, or infected.

Step-by-Step Process:

  1. Initialization: Define grid size (e.g., 1,000x1,000) and a fill percentage (the probability of a cell being occupied).
  2. Grid Setup: Create a NumPy array of zeros. Randomly select a specific number of cells to be "healthy" (value 1).
  3. Infection Start: Randomly select one healthy cell to become "infected" (value 2).
  4. Spreading Logic:
    • Maintain a frontier list of infected cells.
    • Iterate through the frontier, checking the four immediate neighbors (up, down, left, right).
    • If a neighbor is healthy (value 1), infect it (value 2) and add it to the new_frontier.
    • Repeat until no new infections occur.
  5. Visualization: Use matplotlib.animation.FuncAnimation to render the grid state changes over time.

2. Mathematical Insights and Thresholds

The core argument presented is that percolation behavior is governed by a statistical threshold.

  • The Threshold: For an infinite 2D grid, the percolation threshold is approximately 59.27%.
  • The Uncertainty Window: The author explains that the width of this window depends on the grid size ($L$). The formula provided for the uncertainty width is $L^{-3/4}$.
    • Example: For a 1,000x1,000 grid, the uncertainty is roughly 0.56%. To ensure a "spanning" result, one would add this to the threshold; to ensure containment, one would subtract it.
  • Observation: The author notes that there is no simple closed-form formula to derive the 59.27% value; it is an empirical constant discovered through repeated simulations.

3. Technical Implementation Details

  • Libraries: NumPy for efficient array manipulation and Matplotlib for visualization.
  • Data Handling:
    • np.random.choice is used to distribute people across the grid.
    • divmod() is used to convert flattened array indices back into 2D (row, column) coordinates.
    • grid.flat allows for efficient assignment of values in a flattened representation.
  • Animation: The update function in the animation loop calculates the number of infected cells by summing the array where frame == 2 and updates the plot title dynamically.

4. Notable Quotes

  • "There is no formula that will produce this number... It's just a thing that shows up across simulations." — Regarding the 59.27% percolation threshold.
  • "The longer it takes, the more likely you will see something like a spread." — On the relationship between simulation duration and the likelihood of spanning the grid.

5. Synthesis and Conclusion

The simulation serves as both a practical coding exercise for animation and a demonstration of phase transitions in complex systems. By adjusting the fill percentage relative to the 59.27% threshold, the user can observe the dramatic shift between localized outbreaks and global percolation. The author emphasizes that while randomness plays a role, the statistical behavior of the system becomes highly predictable as one moves outside the "uncertainty window" defined by the grid size. This framework can be extended to higher dimensions or more complex rules, such as adding probabilistic infection rates.

AI summaries can miss context or contain errors. Check important details against the original video.

Go a little deeper.

Have a question about this video? Load its transcript to open the video chat.