Programming lesson
Modelling Fake News Spread with Python: A Mathematical Approach for ENGF0003
Learn how to model the spread of fake news on social media using Python and mathematical concepts like compartmental models. This tutorial aligns with ENGF0003 Mathematical Modelling and Analysis I, offering practical coding exercises without solving the assignment.
Introduction
Social media platforms like Instagram, TikTok, and X (formerly Twitter) have transformed how we consume news. However, they also enable the rapid spread of fake news. In the ENGF0003 Mathematical Modelling and Analysis I project, you are tasked with analyzing how fake news spreads. This tutorial will guide you through building a simple mathematical model using Python, focusing on the SIR (Susceptible-Infected-Recovered) model adapted for misinformation. You will learn to implement the model, visualize results, and interpret them—without completing your assignment for you. By the end, you'll have a solid foundation to tackle Task 2 of your project.
Why Model Fake News?
Fake news spreads similarly to infectious diseases. Individuals move from being unaware (susceptible) to sharing (infected) to disinterested (recovered). This analogy is powerful because it allows us to use established epidemiological models. With the rise of AI-generated content (like the 'Balenciaga Pope' image from March 2023), understanding these dynamics is more relevant than ever. In your ENGF0003 project, you'll need to justify your modelling choices—this tutorial will help you do that.
Prerequisites
You should have basic Python knowledge and have installed NumPy and Matplotlib. We'll use these libraries to solve differential equations and plot results. If you haven't used Python before, consider reviewing introductory materials. This tutorial is designed to be accessible while preparing you for the analytical depth required in ENGF0003.
The SIR Model for Fake News
The SIR model divides the population into three compartments:
- S (Susceptible): People who haven't seen the fake news.
- I (Infected): People who have seen and are sharing the fake news.
- R (Recovered): People who have stopped sharing (e.g., fact-checked, lost interest).
The model uses differential equations:
dS/dt = -beta * S * I / N
dI/dt = beta * S * I / N - gamma * I
dR/dt = gamma * Iwhere beta is the transmission rate (how quickly fake news spreads) and gamma is the recovery rate (how quickly people stop sharing). N is the total population.
Implementing the Model in Python
We'll use scipy.integrate.odeint to solve the equations. First, install scipy if needed: pip install scipy. Then write the following code:
import numpy as np
from scipy.integrate import odeint
import matplotlib.pyplot as plt
def sir_model(y, t, N, beta, gamma):
S, I, R = y
dSdt = -beta * S * I / N
dIdt = beta * S * I / N - gamma * I
dRdt = gamma * I
return dSdt, dIdt, dRdt
# Initial conditions
N = 1000
I0 = 1
R0 = 0
S0 = N - I0 - R0
y0 = S0, I0, R0
# Time vector (days)
t = np.linspace(0, 50, 50)
# Parameters (example values)
beta = 0.3
gamma = 0.1
# Solve ODE
ret = odeint(sir_model, y0, t, args=(N, beta, gamma))
S, I, R = ret.T
# Plot
plt.figure(figsize=(10,6))
plt.plot(t, S, 'b', label='Susceptible')
plt.plot(t, I, 'r', label='Infected (sharing)')
plt.plot(t, R, 'g', label='Recovered')
plt.xlabel('Time (days)')
plt.ylabel('Number of people')
plt.title('SIR Model of Fake News Spread')
plt.legend()
plt.grid()
plt.show()This code will produce a graph showing how the number of people sharing fake news rises and then falls. Adjust beta and gamma to see different scenarios. For example, a higher beta (e.g., 0.6) represents more viral content, while a higher gamma (e.g., 0.2) represents faster fact-checking.
Interpreting Results for ENGF0003
In your project, you must justify your parameter choices. Use real-world context: during the COVID-19 pandemic, misinformation about 5G spread rapidly because it appealed to existing fears. You could set beta higher for sensational content. Also, consider that not everyone recovers—some remain 'infected' indefinitely. You might modify the model to include a constant source of new susceptible users (e.g., new social media accounts). This is where your critical analysis comes in.
Extending the Model
For Task 2 of ENGF0003, you'll need an open-ended design. Consider adding compartments like 'fact-checkers' or 'bots'. You could implement a model with time-varying parameters to mimic real events (e.g., a viral tweet). The key is to explain your decisions mathematically. For instance, you could use a logistic growth function for beta to represent increasing virality over time.
Conclusion
Mathematical modelling is a powerful tool to understand fake news dynamics. By implementing the SIR model in Python, you've taken the first step toward completing your ENGF0003 project. Remember to reference any sources you use and adhere to UCL's academic integrity guidelines. This tutorial is meant to assist, not replace, your own work. Good luck!