Mathematical Sociology

Example: Modeling Social Influence in a Network

We will simulate how opinions spread in a social network.

The idea is based on the DeGroot model of consensus, where each node in the network updates its opinion based on a weighted average of its neighbors’ opinions.

Problem Setup

  • A network of individuals is represented as a graph.
  • Each node has an initial opinion (randomized).
  • Opinions are updated iteratively using the weighted average of neighboring opinions.
  • We will observe how opinions converge over time.

Python Code Implementation

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import numpy as np
import networkx as nx
import matplotlib.pyplot as plt

# Step 1: Create a social network graph
G = nx.erdos_renyi_graph(n=20, p=0.2, seed=42) # 20 nodes, 20% connection probability

# Step 2: Initialize random opinions
opinions = np.random.rand(len(G.nodes))
initial_opinions = opinions.copy()

# Step 3: Define the adjacency matrix and normalize weights
adj_matrix = nx.to_numpy_array(G)
weights = adj_matrix / adj_matrix.sum(axis=1, keepdims=True)

# Step 4: Simulate opinion updates
iterations = 20
opinion_history = [opinions.copy()]

for _ in range(iterations):
opinions = weights @ opinions # Weighted average of neighbors
opinion_history.append(opinions.copy())

# Step 5: Visualization of the results
plt.figure(figsize=(12, 6))

# Plot the convergence of opinions over time
for i, opinion_track in enumerate(np.array(opinion_history).T):
plt.plot(opinion_track, label=f'Node {i}', alpha=0.6)

plt.title("Opinion Dynamics in a Social Network (DeGroot Model)")
plt.xlabel("Iteration")
plt.ylabel("Opinion Value")
plt.legend(bbox_to_anchor=(1.05, 1), loc='upper left', fontsize='small')
plt.grid(True)
plt.show()

# Plot the initial and final opinions on the graph
fig, axes = plt.subplots(1, 2, figsize=(14, 6))

# Initial opinions
nx.draw(
G,
with_labels=True,
node_color=initial_opinions,
cmap=plt.cm.viridis,
node_size=500,
ax=axes[0]
)
axes[0].set_title("Initial Opinions")

# Final opinions
nx.draw(
G,
with_labels=True,
node_color=opinions,
cmap=plt.cm.viridis,
node_size=500,
ax=axes[1]
)
axes[1].set_title("Final Opinions")

plt.show()

Explanation

  1. Graph Creation:
    A random graph is created using the $Erdős–Rényi model$ to simulate a social network.
  2. Initialization:
    Each node is assigned a random opinion value between $0$ and $1$.
  3. Opinion Update Rule:
    Opinions are updated iteratively as a weighted average of neighbors’ opinions, governed by the adjacency matrix of the graph.
  4. Visualization:
    • The convergence of opinions over iterations is plotted.
    • The initial and final opinions are visualized on the network.

Results

  1. Convergence Dynamics:
    • The plot of opinion dynamics shows how individual nodes’ opinions evolve and converge over iterations.
    • Nodes with many connections (high degree) influence others more strongly.


  1. Graph Visualization:
    • Initial opinions are randomly distributed.
    • Final opinions converge to a consensus or remain close to a weighted average of initial opinions depending on the network structure.


This example demonstrates how mathematical sociology models can capture social influence processes using $Python$.