import sympy as sp
import numpy as np
import matplotlib.pyplot as plt
Ex | Behavioral Agency
Schelling’s segregation model demonstrates how individual preferences can lead to racial segregation, even when people are comfortable in mixed neighborhoods. In this exercise, you will explore how the model’s outcome depends on some of its parameters. You will investigate what it takes to achieve a well-mixed society.
Task 1 | Implement Schelling’s model
Define a Python function run_model(agents)
that simulates Schelling’s model for a given iterable of agents (agents
), the number of agents regarded as neighbors num_neighbors
and the number of neighbors required to be of the same type require_same_type
such that an agent is happy. The function should return an iterable of agents after the model has converged. The model should run until no agent wants to move anymore. Print out the current cycle number while the model is running.
# ...
Task 2 | Run the model
Test your implementation by running the model with 500 agents, 10 neighbors, and 5 neighbors of the same type required. Initialize the agents with 250 agents of type 0 and 250 agents of type 1. Plot the distribution of agents at the beginning, and the end of the model run in a matplotlib figure with two axes next to each other.
# ...
Task 3 | Performance metric
Having a visual understanding of the model’s behavior is essential. However, it is also useful to have a quantitative measure of the model’s performance. Define a Python function homogeneity(agents, num_neighbors)
that returns the average homogeneity of the agents. The homogeneity of an agent is the fraction of its neighbors that are of the same type. The average homogeneity is the average of the homogeneity of all agents.
# ...
Test your implementation by calculating the homogeneity of the initial and final agent distributions from the simulation above.
# ...
Convince yourself that the homogeneity is a useful measure of the model’s performance by repeating the simulation with the same parameters as above, except the number of neighbors required to be happy is set to 9. Visualize and calculate the homogeneity of the final agent distribution.
# ...
Task 4 | Sensitivity analysis
Perform a simulation to investigate how the final homogeneity depends on the number of neighbors considered, num_neighbors
, and the number of neighbors of the same type required to be happy, require_same_type
.
First, we keep the number of neighbors required to be happy fixed to be exactly half the number of neighbors considered. How sensitive is the final homogeneity to the number of neighbors considered? Run the model for num_neighbors
in the range from 2 to 20 in increments of two. As the simulation is stochastic, run each simulation precisely five times. Plot the final homogeneities (plus their averages) versus the number of neighbors considered. Briefly interpret your result.
# ...
Finally, run the model for num_neighbors
in the range from 2 to 14, and for each num_enighbors
, the require_same_type
from 1 to num_neighbors
-1. Plot the final homogeneity as a heatmap.
Tip: Store the final homogeneities in a two-dimensional NumPy array
final_homogeneities=np.NaN * np.ones((15, 15))
,
where the first dimension corresponds to the number of neighbors considered and the second dimension to the number of neighbors of the same type required to be happy. The np.NaN
values will be useful for plotting the heatmap as the plotting functions ignore the ‘NaN’ values. You can you use the imshow
function from matplotlib to plot the heatmap,
plt.imshow(final_homogeneities, cmap='RdBu', vmin=0, vmax=1.0, origin='lower')
.
Include a color bar and a line indicating where the number of neighbors considered is equal to half the number of neighbors of the same type required to be happy.
Interpret your results regarding the maximum number of neighbors required to be happy to achieve a well-mixed society. What happens to the final homogeneity when the number of neighbors required to be happy exceeds half the number of neighbors considered?
# ...