{ "cells": [ { "cell_type": "markdown", "id": "cac95f22", "metadata": {}, "source": [ "\n", "" ] }, { "cell_type": "markdown", "id": "3b7537f0", "metadata": {}, "source": [ "# Schelling’s Segregation Model\n", "\n", "\n", "" ] }, { "cell_type": "markdown", "id": "b8369ce6", "metadata": {}, "source": [ "## Contents\n", "\n", "- [Schelling’s Segregation Model](#Schelling’s-Segregation-Model) \n", " - [Outline](#Outline) \n", " - [The Model](#The-Model) \n", " - [Results](#Results) \n", " - [Exercises](#Exercises) " ] }, { "cell_type": "markdown", "id": "55c28a5d", "metadata": {}, "source": [ "## Outline\n", "\n", "In 1969, Thomas C. Schelling developed a simple but striking model of racial segregation [[Schelling, 1969](https://python.quantecon.org/zreferences.html#id211)].\n", "\n", "His model studies the dynamics of racially mixed neighborhoods.\n", "\n", "Like much of Schelling’s work, the model shows how local interactions can lead to surprising aggregate structure.\n", "\n", "In particular, it shows that relatively mild preference for neighbors of similar race can lead in aggregate to the collapse of mixed neighborhoods, and high levels of segregation.\n", "\n", "In recognition of this and other research, Schelling was awarded the 2005 Nobel Prize in Economic Sciences (joint with Robert Aumann).\n", "\n", "In this lecture, we (in fact you) will build and run a version of Schelling’s model.\n", "\n", "Let’s start with some imports:" ] }, { "cell_type": "code", "execution_count": null, "id": "f3a1e4c5", "metadata": { "hide-output": false }, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n", "plt.rcParams[\"figure.figsize\"] = (11, 5) #set default figure size\n", "from random import uniform, seed\n", "from math import sqrt" ] }, { "cell_type": "markdown", "id": "78e2f822", "metadata": {}, "source": [ "## The Model\n", "\n", "We will cover a variation of Schelling’s model that is easy to program and captures the main idea." ] }, { "cell_type": "markdown", "id": "9f330ebb", "metadata": {}, "source": [ "### Set-Up\n", "\n", "Suppose we have two types of people: orange people and green people.\n", "\n", "For the purpose of this lecture, we will assume there are 250 of each type.\n", "\n", "These agents all live on a single unit square.\n", "\n", "The location of an agent is just a point $ (x, y) $, where $ 0 < x, y < 1 $." ] }, { "cell_type": "markdown", "id": "7a4d81df", "metadata": {}, "source": [ "### Preferences\n", "\n", "We will say that an agent is *happy* if half or more of her 10 nearest neighbors are of the same type.\n", "\n", "Here ‘nearest’ is in terms of [Euclidean distance](https://en.wikipedia.org/wiki/Euclidean_distance).\n", "\n", "An agent who is not happy is called *unhappy*.\n", "\n", "An important point here is that agents are not averse to living in mixed areas.\n", "\n", "They are perfectly happy if half their neighbors are of the other color." ] }, { "cell_type": "markdown", "id": "7a0d8c73", "metadata": {}, "source": [ "### Behavior\n", "\n", "Initially, agents are mixed together (integrated).\n", "\n", "In particular, the initial location of each agent is an independent draw from a bivariate uniform distribution on $ S = (0, 1)^2 $.\n", "\n", "Now, cycling through the set of all agents, each agent is now given the chance to stay or move.\n", "\n", "We assume that each agent will stay put if they are happy and move if unhappy.\n", "\n", "The algorithm for moving is as follows\n", "\n", "1. Draw a random location in $ S $ \n", "1. If happy at new location, move there \n", "1. Else, go to step 1 \n", "\n", "\n", "In this way, we cycle continuously through the agents, moving as required.\n", "\n", "We continue to cycle until no one wishes to move." ] }, { "cell_type": "markdown", "id": "7683cf27", "metadata": {}, "source": [ "## Results\n", "\n", "Let’s have a look at the results we got when we coded and ran this model.\n", "\n", "As discussed above, agents are initially mixed randomly together.\n", "\n", "![https://python.quantecon.org/_static/lecture_specific/schelling/schelling_fig1.png](https://python.quantecon.org/_static/lecture_specific/schelling/schelling_fig1.png)\n", "\n", " \n", "But after several cycles, they become segregated into distinct regions.\n", "\n", "![https://python.quantecon.org/_static/lecture_specific/schelling/schelling_fig2.png](https://python.quantecon.org/_static/lecture_specific/schelling/schelling_fig2.png)\n", "\n", " \n", "![https://python.quantecon.org/_static/lecture_specific/schelling/schelling_fig3.png](https://python.quantecon.org/_static/lecture_specific/schelling/schelling_fig3.png)\n", "\n", " \n", "![https://python.quantecon.org/_static/lecture_specific/schelling/schelling_fig4.png](https://python.quantecon.org/_static/lecture_specific/schelling/schelling_fig4.png)\n", "\n", " \n", "In this instance, the program terminated after 4 cycles through the set of\n", "agents, indicating that all agents had reached a state of happiness.\n", "\n", "What is striking about the pictures is how rapidly racial integration breaks down.\n", "\n", "This is despite the fact that people in the model don’t actually mind living mixed with the other type.\n", "\n", "Even with these preferences, the outcome is a high degree of segregation." ] }, { "cell_type": "markdown", "id": "377ec057", "metadata": {}, "source": [ "## Exercises" ] }, { "cell_type": "markdown", "id": "62072708", "metadata": {}, "source": [ "## Exercise 67.1\n", "\n", "Implement and run this simulation for yourself.\n", "\n", "Consider the following structure for your program.\n", "\n", "Agents can be modeled as [objects](https://python-programming.quantecon.org/python_oop.html).\n", "\n", "Here’s an indication of how they might look" ] }, { "cell_type": "markdown", "id": "0b06bc85", "metadata": { "hide-output": false }, "source": [ "```text\n", "* Data:\n", "\n", " * type (green or orange)\n", " * location\n", "\n", "* Methods:\n", "\n", " * determine whether happy or not given locations of other agents\n", "\n", " * If not happy, move\n", "\n", " * find a new location where happy\n", "```\n" ] }, { "cell_type": "markdown", "id": "e526860e", "metadata": {}, "source": [ "And here’s some pseudocode for the main loop" ] }, { "cell_type": "markdown", "id": "cf798afd", "metadata": { "hide-output": false }, "source": [ "```text\n", "while agents are still moving\n", " for agent in agents\n", " give agent the opportunity to move\n", "```\n" ] }, { "cell_type": "markdown", "id": "41df4768", "metadata": {}, "source": [ "Use 250 agents of each type." ] }, { "cell_type": "markdown", "id": "c9d8a1d1", "metadata": {}, "source": [ "## Solution to[ Exercise 67.1](https://python.quantecon.org/#schelling_ex1)\n", "\n", "Here’s one solution that does the job we want.\n", "\n", "If you feel like a further exercise, you can probably speed up some of the computations and\n", "then increase the number of agents." ] }, { "cell_type": "code", "execution_count": null, "id": "1e6fc12a", "metadata": { "hide-output": false }, "outputs": [], "source": [ "seed(10) # For reproducible random numbers\n", "\n", "class Agent:\n", "\n", " def __init__(self, type):\n", " self.type = type\n", " self.draw_location()\n", "\n", " def draw_location(self):\n", " self.location = uniform(0, 1), uniform(0, 1)\n", "\n", " def get_distance(self, other):\n", " \"Computes the euclidean distance between self and other agent.\"\n", " a = (self.location[0] - other.location[0])**2\n", " b = (self.location[1] - other.location[1])**2\n", " return sqrt(a + b)\n", "\n", " def happy(self, agents):\n", " \"True if sufficient number of nearest neighbors are of the same type.\"\n", " distances = []\n", " # distances is a list of pairs (d, agent), where d is distance from\n", " # agent to self\n", " for agent in agents:\n", " if self != agent:\n", " distance = self.get_distance(agent)\n", " distances.append((distance, agent))\n", " # == Sort from smallest to largest, according to distance == #\n", " distances.sort()\n", " # == Extract the neighboring agents == #\n", " neighbors = [agent for d, agent in distances[:num_neighbors]]\n", " # == Count how many neighbors have the same type as self == #\n", " num_same_type = sum(self.type == agent.type for agent in neighbors)\n", " return num_same_type >= require_same_type\n", "\n", " def update(self, agents):\n", " \"If not happy, then randomly choose new locations until happy.\"\n", " while not self.happy(agents):\n", " self.draw_location()\n", "\n", "\n", "def plot_distribution(agents, cycle_num):\n", " \"Plot the distribution of agents after cycle_num rounds of the loop.\"\n", " x_values_0, y_values_0 = [], []\n", " x_values_1, y_values_1 = [], []\n", " # == Obtain locations of each type == #\n", " for agent in agents:\n", " x, y = agent.location\n", " if agent.type == 0:\n", " x_values_0.append(x)\n", " y_values_0.append(y)\n", " else:\n", " x_values_1.append(x)\n", " y_values_1.append(y)\n", " fig, ax = plt.subplots(figsize=(8, 8))\n", " plot_args = {'markersize': 8, 'alpha': 0.6}\n", " ax.set_facecolor('azure')\n", " ax.plot(x_values_0, y_values_0, 'o', markerfacecolor='orange', **plot_args)\n", " ax.plot(x_values_1, y_values_1, 'o', markerfacecolor='green', **plot_args)\n", " ax.set_title(f'Cycle {cycle_num-1}')\n", " plt.show()\n", "\n", "# == Main == #\n", "\n", "num_of_type_0 = 250\n", "num_of_type_1 = 250\n", "num_neighbors = 10 # Number of agents regarded as neighbors\n", "require_same_type = 5 # Want at least this many neighbors to be same type\n", "\n", "# == Create a list of agents == #\n", "agents = [Agent(0) for i in range(num_of_type_0)]\n", "agents.extend(Agent(1) for i in range(num_of_type_1))\n", "\n", "\n", "count = 1\n", "# == Loop until none wishes to move == #\n", "while True:\n", " print('Entering loop ', count)\n", " plot_distribution(agents, count)\n", " count += 1\n", " no_one_moved = True\n", " for agent in agents:\n", " old_location = agent.location\n", " agent.update(agents)\n", " if agent.location != old_location:\n", " no_one_moved = False\n", " if no_one_moved:\n", " break\n", "\n", "print('Converged, terminating.')" ] } ], "metadata": { "date": 1710733976.466355, "filename": "schelling.md", "kernelspec": { "display_name": "Python", "language": "python3", "name": "python3" }, "title": "Schelling’s Segregation Model" }, "nbformat": 4, "nbformat_minor": 5 }