This package and analysis can be reproduced using a conda environment with the
required dependencies installed. Dependencies are listed in env_spec.txt
,
placed on the code folder.
Assuming you have the Anaconda Python distribution installed
(see page), an environment
can be recreated automatically by running the following command:
conda create -n schelling --file env_spec.txt
Once that is completed, activate the environment by running:
source activate schelling
When you are done with the session, deactivate the environment with:
source deactivate
There are three main scripts used to for the simulations in the paper, and all are included in the code folder:
schelling.py
contains the core methods to set up and run a Schelling-type
agent-based model. See some instructions on how to use it below.sim_enginge_scoop.py
is the script used to run the simulations on a
super-computer. It relies on the library
scoop
for the parallel
computations. The results can be found in the code folder and are named
sim_res.csv
.results.py
contains the methods required to generate the figures in the
paper and those included additionally in this page. An example on how to use
them can be found in the visualizations section of this website.schelling
module¶import time
print "Ran last ", time.strftime("%d/%m/%Y")
This document offers a short tutorial of the basic usage of the schelling
module, employed in the simulations of the paper "How diverse can spatial measures of cultural diversity be? -- Results from Monte Carlo simulations on an agent-based model", by Dani Arribas-Bel, Peter Nijkamp and Jacques Poot.
The library flexibly allows to generate different topologies on which to run the "bounded neighborhood" of the Schelling model. This includes generating standard grids or using arbitrary topologies specified in a shapefile.
%matplotlib inline
import schelling
For this tutorial, we will be using a world of 70 by 70 cells, which can be vacant or occupied by agents.
world_dims = 70, 70
This example uses a grid of 10 by 10 neighborhoods (hence 7x7 locations per neighborhood. With this information, we can build the world topology:
nr, nc = neighs = (7, 7)
w, ns, xys = schelling.bounded_world(world_dims[0], world_dims[1], nr, nc)
Similarly, we allow for 25% of the spots to be vacant at any time, thus setting population size to 75% of all the 4,900 (70x70) potential spots.
pop_size = int((world_dims[0] * world_dims[1]) * 0.75)
At this point, we are ready to specify the groups in the population and their preferences for diversity.
We will use a case with two groups only and equal distribution (50%-50%). Note that you only specify population shares for n-1 groups, the remaining being assumed:
prop_groups = [0.5]
Preferences are controlled globally by the parameter $\tau$, which represents the number of agents alike every individual needs in the inmediate neighborhood to be happy. We will start with flat preferences:
pct_similar_wanted = 0.
At this point, we are ready to initialize the world:
world = schelling.World(pop_size, pct_similar_wanted, prop_groups, w, ns)
And ready to run the simulation!
%%time
world.go()
Once the world converges to a situation in which every individual is content, it stops. We can then examine the output by querying the object world
.
The most obvious way to explore the output is by visualizing it. Note we need to pass coordinates for the locations and, in this case, a specification of the grid-like neighborhoods (both specififed above):
world.plot(xys, neighborhoods=neighs)
For the sake of the example, we can try now with a situation in which everyone requires at least three quarters of its neighbors to be similar. Note the clearly longer time of computation until convergence but also the radically different pattern that emerges).
pct_similar_wanted = 0.5
world = schelling.World(pop_size, pct_similar_wanted, prop_groups, w, ns)
%%time
world.go()
world.plot(xys, neighborhoods=neighs)