Agent-Based Modeling for Sociologists

77
Agent-based Modeling w/ NetLogo Simone Gabbriellini Postdoc@GEMASS, CNRS & Paris-Sorbonne Brescia, July 7-11, 2014

description

Agent-Based Modeling for Sociologists is a crash course on how to build ABM in the social sciences. This presentation has an introduction to OOP and then discusses three models in details, along with their NetLogo implementation

Transcript of Agent-Based Modeling for Sociologists

Page 1: Agent-Based Modeling for Sociologists

Agent-based Modeling w/ NetLogoSimone Gabbriellini Postdoc@GEMASS, CNRS & Paris-Sorbonne

!

!

!

Brescia, July 7-11, 2014

Page 2: Agent-Based Modeling for Sociologists

Brief Outline of the course

Today: Programming concepts and NetLogo hands-on

Tuesday: Nowak and Latane’s model

Wednesday: Axelrod’s model

Thursday: Flache & Macy’s model

Friday: Teamwork

Page 3: Agent-Based Modeling for Sociologists

A new way to organize your mind…or how Object-Oriented Programming will save your academic life (no kidding)

Page 4: Agent-Based Modeling for Sociologists

Computational SociologyHummon and Fararo (1995) in “Actors and Networks as Object” claim support for the emergence of computational sociology: computer sciences ideas and technologies, like simulation models, will help advance sociological theory

Macy and Willer (2002) in “From factors to actors: computational sociology and agent-based modeling” identify agent-based model as the right tool for advancing sociological theory: human group processes are highly complex, non-linear, path dependent, and self-organizing. A bottom-up approach should be more efficient than a top-down and aggregate one.

Page 5: Agent-Based Modeling for Sociologists

Generative Principleagent-based models are used within the conceptual framework of what Epstein’s (2006) called generative social science

The research question is: “how could the decentralized local interactions of heterogeneous autonomous agents generate the given regularity?” (Epstein, 2006, p. 5)

Epstein’s formalization: ¬S ⇐ ¬G

Page 6: Agent-Based Modeling for Sociologists

Object-Oriented Programming (OOP)

OOP is a programming paradigm - a set of concepts and abstractions used to represent a computer program and the algorithms used

OOP represents the concept of objects that have data fields (attributes that describe the object known as properties) and associated procedures known as methods

Objects, which are instances of classes, are used to interact with one another to design applications and computer programs

OOP is the new way of organizing data since 1990

Page 7: Agent-Based Modeling for Sociologists

OOP Key Concepts*Objects are key to understanding OOP.

Real-world objects share two characteristics: They all have state and behavior.

Show and tell: Take a minute right now to observe the real-world objects that are in your immediate area

From Oracle: http://docs.oracle.com/javase/tutorial/java/concepts/index.html

Page 8: Agent-Based Modeling for Sociologists

What is an Object: ride a software bicycle!

An object stores its state in properties (variables or fields in some programming languages) and exposes its behavior through methods

Methods operate on an object's internal state and serve as the primary mechanism for object-to-object communication.

Page 9: Agent-Based Modeling for Sociologists

What is a Class: wait, is that my bicycle???

In the real world, you'll often find many individual objects all of the same kind.

In object-oriented terms, we say that your bicycle is an instance of the class of objects known as bicycles.

Page 10: Agent-Based Modeling for Sociologists

Key Concepts: data encapsulation

Hiding internal state and requiring all interaction to be performed through an object's methods is known as data encapsulation

By interacting only with an object's methods, the details of its internal implementation remain hidden from the outside world (information-hiding)

Page 11: Agent-Based Modeling for Sociologists

Key Concepts: InheritanceDifferent kinds of objects often have a certain amount in common with each other.

OOP allows classes to inherit commonly used state and behavior from other classes.

In OOP, each class is allowed to have one direct superclass, and each superclass has the potential for an unlimited number of subclasses

Page 12: Agent-Based Modeling for Sociologists

Why OOP is useful for us?

because we want to simulate artificial societies and generate social phenomena in silico

but how OOP actually helps us?

Page 13: Agent-Based Modeling for Sociologists

What does the trick?property 1 property 2 property 3

flaminio 10 3 25simone 18 1 20

you 23 321 12… … … …

flaminio

simone you

10

325

20

118

12

32123each row is an object, with the same properties !!!

Page 14: Agent-Based Modeling for Sociologists

What does the trick?property 1 property 2 property 3

flaminio 10 3 25simone 18 1 20

you 23 321 12… … … …

flaminio

simone you

10

325

20

118

12

32123each row is an object, with the same properties but also with methods that can change these values !

a method

a method

a method

Page 15: Agent-Based Modeling for Sociologists

Mmmm, So What?

1. Dynamic

2. Scheduling

3.<YOUR LOGIC HERE>

Page 16: Agent-Based Modeling for Sociologists

Brief recapan object is an instance of a class

when an object is created, a space in the memory is occupied

an object has some properties

an object has methods to change its properties

objects can communicate through methods

Page 17: Agent-Based Modeling for Sociologists

But what is an agent?An agent is a thing which does things to things (Kauffman)

An agent is a persistent thing which (Shalizi, 2004):

has some state you find worth representing (it’s up to you!)

interacts with other agents, mutually modifying each others’ states (your model your rules!)

Question: can you reformulate this definition using OOP concepts?

Page 18: Agent-Based Modeling for Sociologists

Yes, agents are objects!

Computationally, the nicest way to implement an agent is with objects

What if we do not use objects?

Page 19: Agent-Based Modeling for Sociologists

So what is an ABM?An ABM is a computer program:

a collection of agents and their states

the rules governing the interactions of the agents

the environment within which they live.

Page 20: Agent-Based Modeling for Sociologists

What are ABM suited for?agent-based models represent individuals, their behaviors and their interactions

equation-based models represent aggregates and their dynamics.

Agents have decision-making abilities and an understanding of their environment

Page 21: Agent-Based Modeling for Sociologists

What is simulated time?

A schedule implies a timeline

ask agents [ # do something]advance-tick

Page 22: Agent-Based Modeling for Sociologists

What is randomness?

How can a deterministic device produce random events?

How can we use pseudo-random numbers?

We also need a seed, like 123456789

Page 23: Agent-Based Modeling for Sociologists

We have a model, then?Every model has a parameters space

Select a granularity

Simulate each possible combination many times

Compare what you have found with empirical values

Page 24: Agent-Based Modeling for Sociologists

About platforms…

Many available (SWARM, RePast, MASON…)

NetLogo is not OOP, but uses a Procedural Language called Logo

What is Logo?

Why using a procedural language instead of OOP?

Page 25: Agent-Based Modeling for Sociologists

About NetLogo

OOP Simple Language

Easy Interface

Easy Documentation

IDE with debugger

Parameters Space

Explorer

NetLogo ✓ ✓ ✓ ✓ ✓ ✓

Page 26: Agent-Based Modeling for Sociologists

How to ask for help… and actually receive it!

[email protected]

[email protected] (for extensions developers)

Ask a question on www.stackoverflow.com

Please be smart in the title:

“I need help”, “this doesn’t work”, “I have a problem”

Please be specific in the message:

tell people what is your goal, what have you done already, and report the complete error message you get

Page 27: Agent-Based Modeling for Sociologists

From Private Attitude to Public Opinion: a Dynamic Theory of Social ImpactAuthors: Nowak, Szamrej and Latané Published in: Psychological review, 1990, 97: 3, 362-376

Page 28: Agent-Based Modeling for Sociologists

IntroductionSimulate Latane’s social impact theory, which specifies a number of principles that describe how individuals are influenced by their social context

In doing so, we will observe how some phenomena at the macro level can emerge from this simple theoretical micro-foundation, in particular:

incomplete polarization of opinions reaches a stable equilibrium

minorities form on the margins of the population

Computer simulations, neglected in group dynamics for 20 years, may, as in modern physics, help determine the extent to which group-level phenomena result from individual-level processes. (micro-macro link)

Page 29: Agent-Based Modeling for Sociologists

IntroductionThe authors starts from the Coleman’s Boat:

“The functioning of higher level units (e.g., social groups) may be partly or completely determined and therefore explained by mechanisms known from theories describing phenomena at lower levels (e.g., human individuals). Alternatively,the functioning of lower level units (e.g., individuals) may be affected by the higher level units to which they belong. In other words, individuals in a given social context behave differently than they would outside that context”.

They insist on the concept of simulations as the right tool to develop sociological theory:

“we examine some possible consequences of simulating on a computer a theory describing the functioning of individuals in the presence of others”

“we simulate Latané's (1981) theory of social impact as applied to attitudes and explore the consequences for public opinion of the operation of social processes affecting individual attitude change”

Page 30: Agent-Based Modeling for Sociologists

Personal influence and public opinion

Many studies show that people tend to change their minds when compelled with persuasive arguments

we can deduce that, if the trial period is long enough, we should see the emergence of a generalized consensus, where everybody converge on the majority’s view, but we know this does not happen:

“Social influence processes do not by themselves create uniformity of opinion”

“Social influence processes do not lead to the convergence of public opinion on the mean of the initial distribution of private attitudes”

Page 31: Agent-Based Modeling for Sociologists

Social Impact TheoryLatané defines social impact as: “any influence on individual feelings, thoughts, or behavior that is exerted by the real, implied, or imagined presence or actions of others” thus its goal is to define the impact that a subject receives from an external source (a group)

Impact (î) is a multiplicative function (f) of strength (S), distance (I) and size (N) of the external source:

î = f(SIN)

Social impact theory is a static theory

Page 32: Agent-Based Modeling for Sociologists

Social impact theoryHow do we change our attitudes?

To the extent that individuals are relatively uninvolved in an issue, they should be influenced by the strength, immediacy, and number of people advocating a contrary position.

It seemed desirable to distinguish two forms of communicator strength

one with respect to people who share the communicator's opinions

one with respect to people who oppose them.

What happens when individuals with these characteristics come together in a dynamic process?

Page 33: Agent-Based Modeling for Sociologists

How to simulate social impact

Attitude: 1 or 0, it is a dichotomous variable, it is used to divide the population into two groups. Its interpretation does not affect the result of the simulation (numbers, letters, boolean, whatever)

Persuasiveness: a random number from 0 to 100, it represents how good you are in persuading people that do not share your opinion

Supportiveness: a random number from 0 to 100, it represents how good you are in support people that share your opinion

Immediacy: a number that represents distance between pairs of agents

Page 34: Agent-Based Modeling for Sociologists

Model Rules

Persuasiveness:

!

!

Supportiveness:

Page 35: Agent-Based Modeling for Sociologists

Model rulesChange rule: if an individual undergoes a persuasiveness greater than the supportiveness offered, the individual changes his mind(if ip / is > 1 then new attitude = 1 - attitude)

Frequency: the initial state of the simulation requires that a percentage chosen by us has a certain attitude. The initial distribution of attitudes is random

Tick: at each tick, each agent observes his neighborhood and decides what to do, whether to change its opinion or not

World: square matrix 40x40 = 1600 agents, each agent "sees" only agents at a distance <= 10

Page 36: Agent-Based Modeling for Sociologists

Results / 1

Initial Condition: 1600 agents, 70% starts with opinion: I 30% starts with opinion: -

Typical final distribution: 90% adopts dominance 10% resists and is organized into neighborhoods

Page 37: Agent-Based Modeling for Sociologists

Results / 2At first, changes are very frequent, then tend to an equilibrium state The majority acquires many members while the minority loses many Change attitudes in order to create consistent areas* The frequency of changing attitudes decreases over the course of the simulation The simulation reaches a state of equilibrium The time required to reach equilibrium depends on the initial distribution of skills (the more egalitarian, the longer the simulation)

Page 38: Agent-Based Modeling for Sociologists

Discussion

The model shows how to apply social impact theory to dynamic groups while producing two emerging phenomena:

incomplete polarization at equilibrium state

clustered groups of homogeneous opinions

Again on polarization

Page 39: Agent-Based Modeling for Sociologists

Testo

It’s your turn now!Please open NetLogo...

Page 40: Agent-Based Modeling for Sociologists

The Dissemination of Culture: a Model with Local Convergence and Global PolarizationAuthor: Axelrod Published in: Journal of Conflict Resolution, 1997, 41: 2, 203-226

Page 41: Agent-Based Modeling for Sociologists

IntroductionIf people tend to become more alike in their beliefs, attitudes and behavior when they interact, why do not all such differences eventually disappear?

We start from a mechanism that forces agents to converge:

similarity leads to interaction and interaction leads to more similarity

The novelty of this model is twofold:

the effect of a cultural feature depends on the presence or absence of other cultural features (social influence)

homophily is taken into account (social selection)

The model shows how local convergence generates global polarization, and how this polarization varies according to the scope and possibilities of the cultural landscape

41

Page 42: Agent-Based Modeling for Sociologists

What is “culture”?Culture is something people learn from each others: culture is what social influence influences

Many ways to explain why minorities resist

Culture is represented as a set of features, each of which has a trait. This representation allows us to introduce homophily (the more we are similar, the more we will interact)

Here’s a simple example: 1 7 2 9

42

Page 43: Agent-Based Modeling for Sociologists

Why ABM?Bottom-up approach with micro-founded mechanisms and consequences are investigated using simulations

No central authority

Adaptive agents: no rationality, no benefit/cost analysis, they simply adapt to their environment

Consequences on global level depends on cultural size and complexity, neighborhood extension and world size

43

Page 44: Agent-Based Modeling for Sociologists

The modelThe basic idea is that the more two agents are similar, the higher the chance they will interact, thus becoming even more similar: to achieve this behavior, the chance to interact is proportional to their cultural similarity

Agents are on a 10x10 grid

Each agent controls his neighborhood:

agents in the middle: Von Neuman neighborhood - north south west est, 4 neighbors

agents at the border: north south est/west, 3 neighbors

agents in the corner: 2 neighbors

Agents are initialized with a random culture:

they all have the same number of dimensions

cultural traits are assigned randomly

44

Page 45: Agent-Based Modeling for Sociologists

SchedulingAn agent is extracted at random

One of his neighbors is extracted at random

They interact with a probability proportional to their cultural similarity

If they interact, the calling agent picks at random one of his cultural dimensions where the two agents differ and copies his neighbors’ value, thus becoming more similar to his neighbor

Time is measured in events, where an event represents the activation of an agent

45

Page 46: Agent-Based Modeling for Sociologists

Testo

How it worksCheck the underlined agent…

46

Page 47: Agent-Based Modeling for Sociologists

The emergence of homogenous cultural zones

how do homogeneous zones form?

does homogeneity is achieved or does minority cultures form?

does the system achieve an equilibrium?

47

Page 48: Agent-Based Modeling for Sociologists

The emergence of homogenous cultural zones

a cultural region is a contiguous region where agents share the same culture

time 20.000 some cultural regions emerged

time 40.000 cultural regions got bigger

time 81.000 is the equilibrium state: just 3 regions left, a majority and two minorities

cultural region

cultural region

cultural region

48

Page 49: Agent-Based Modeling for Sociologists

The emergence of homogenous cultural zones

Cultural complexity depends upon two variables:

number of cultural features

number of traits for each feature

Hypothesis: more variability between cultures will produce more cultural regions

49

Page 50: Agent-Based Modeling for Sociologists

Testo

Partial resultsInteraction between cultural features and traits

Page 51: Agent-Based Modeling for Sociologists

DiscussionWe observed two results:

the number of stable cultural regions increases as the number of traits increases (intuitive)

the number of stable cultural regions decreases as the number of cultural features increase (counter-intuitive)

What have we learned:

the model can be stated in one single sentence: “with probability equal to their cultural similarity, a randomly chosen agent will adopt one of the cultural features of a randomly chosen neighbor”

it is hard to forecast how many stable cultural regions will form at equilibrium

functionalist explanations are not necessarily the simplest

polarization is not necessarily produced by negative selection

51

Page 52: Agent-Based Modeling for Sociologists

Testo

It’s your turn now!Please open NetLogo…

Page 53: Agent-Based Modeling for Sociologists

Small Worlds and Cultural PolarizationAuthors: Flache and Macy Published in: Journal of Mathematical Sociology, 2011, 5: 1-3, 146-176

Page 54: Agent-Based Modeling for Sociologists

Social Network Analysis

54

Page 55: Agent-Based Modeling for Sociologists

Random networks

First attempt to model social networks (Erdos e Renyi)

Pseudo-code:for each dyad [ with probability p [ create-a-link ]]

55

Page 56: Agent-Based Modeling for Sociologists

Testo

Think about you and your friends…The strength of weak ties

56

Page 57: Agent-Based Modeling for Sociologists

Testo

Now think on a bigger scale…The strength of weak ties

57

Page 58: Agent-Based Modeling for Sociologists

Testo

Small WorldThanks to Duncan Watts, we now have a model to account for weak ties

58

Page 59: Agent-Based Modeling for Sociologists

Back to this model

Research on small worlds suggests that weak ties (i.e. bridges between clusters) foster integration, homogeneity and cultural diffusion

This model shows that such a result is based on implicit micro-level assumptions

It’s theory-building, baby…

59

Page 60: Agent-Based Modeling for Sociologists

IntroductionEmpirical and theoretical research suggests two very different effects of long-range ties in clustered social networks:

An earlier generation of studies predicted the inevitable collapse of diversity into an emergent monoculture unless clusters are entirely disconnected

More recent studies suggest the preservation and even the enhancement of diversity in an increasingly connected world

There is also a third possibility: long-range ties can reduce cultural diversity leading to consensus and at the same time deepen cultural divisions leading to polarization: let’s see how

60

Page 61: Agent-Based Modeling for Sociologists

IntroductionWhat is the effects of long-range ties on cultural diversity in an otherwise locally clustered social network?

Recall Axelrod’s model to explain diversity? social influence (how you change your mind) + social selection (homophily to select who to pick)

Axelrod showed how homophily can preserve diversity despite the convergent tendencies created by cultural influence

61

Page 62: Agent-Based Modeling for Sociologists

IntroductionIf social influence is driven by homophily you are attracted by people alike you

if social influence is driven by xenophobia you differentiate from people not alike you

The access network limits/fosters interaction opportunities and is modeled as an exogenously imposed, static neighborhood structure that defines the structural constraints within which the dynamics of social influence and selection operate.

62

Page 63: Agent-Based Modeling for Sociologists

Why AMB?An important reason is that the combination of assimilation and differentiation implies an inherently nonlinear dynamic, while previous analytical models assumed linear social influence functions

When negative valence of interaction is assumed away, long-range ties have an intuitively obvious integrative effect.

When this restriction is relaxed, we find that long-range ties deepen cultural divisions.

This turns out to be robust against variations in the number and local density of network clusters and the number of salient cultural issues.

63

Page 64: Agent-Based Modeling for Sociologists

-0.9899 -0.4345

The model

0.23232 0.32323

i j

K = 2 K = 2

-1 ≤ s ≤ 1 -0.3244 0.5416

K = 4

0.89898 -0.3456

K = 4

wij = wji

state 1 state 2 state 1 state 2

64

Page 65: Agent-Based Modeling for Sociologists

Update weights

i

-1≤wij≤1

s1 = 0.23232

s2 = 0.32323

s1 = 0.89898

s2 = -0.3456

0≤wij≤1wij=0.33225j

The more similar nodes i and j are in their states at time t, the higher is the value of the weight wijt

Page 66: Agent-Based Modeling for Sociologists

Opinions update rules

the change of agent i’s state k is an aggregated result of the influences imposed by all other agents who influence i

What happens when a small number of long-range ties is introduced between disconnected clusters?

66

Page 67: Agent-Based Modeling for Sociologists

Disconnected caveman graph Cave 1

Cave 267

Caves are the technical implementation of network clusters

Local structure captured by Watt’s clustering coefficient (C=1)

Global structure captured by mean geodesic (L=1)

What is the effects on consensus and polarization when a small number of long-range ties are added to a disconnected caveman graph (thus making it a small-world)?

Page 68: Agent-Based Modeling for Sociologists

SchedulingAt each time step, an agent is picked at random

The agent selects randomly to update his state or his weights to avoid artifacts

Agents are extracted with replacement

The duration of a single realization of the model is expressed in the number of iterations

An iteration corresponds to N time steps, where N refers to the number of individuals in the population

68

Page 69: Agent-Based Modeling for Sociologists

Polarization captures the degree to which a population can be separated into two equal-sized factions who are maximally dissimilar in the opinion space and have maximal internal agreement

Opinion distance:

!

Global polarization:

Polarization measure

69

Page 70: Agent-Based Modeling for Sociologists

70

Page 71: Agent-Based Modeling for Sociologists

Experimental results/1In a model with no negative weights, consensus forms within each of the network components, and opinion differences remain only between components

In sum, the micro-theory assuming positive influence and selection implies that long-range ties increase consensus and reduce polarization

71

Page 72: Agent-Based Modeling for Sociologists

72

Page 73: Agent-Based Modeling for Sociologists

Experimental results/2The effect of greater connectedness on social integration and polarization may decisively depend on the micro-mechanisms underlying cultural interaction.

With only positive influence and selection, long-range ties promote greater cultural integration and assimilation. When both positive and negative valences are assumed, the effect is reversed.

73

Page 74: Agent-Based Modeling for Sociologists

DiscussionResearch on ‘‘small-world’’ networks suggests that a small proportion of long-range ties that bridge otherwise distant or disconnected clusters can promote cultural integration.

This model shows that such a macro-social effect depends decisively on the micro-level assumption that interaction is limited to positive influence and selection.

When homophily and assimilation are combined with their negative moments, xenophobia and differentiation, then long-range ties fostered cultural polarization rather than integration

This result should caution modelers of cultural dynamics against overestimating the integrative effects of greater cultural contact.

74

Page 75: Agent-Based Modeling for Sociologists

Testo

It’s your turn now!Please open NetLogo…

Page 76: Agent-Based Modeling for Sociologists

ReferencesAxelrod, R. M. (1997). The complexity of cooperation: agent-based models of competition and collaboration. Princeton, N.J.: Princeton University Press. Axelrod, (1997), The Dissemination of Culture: a Model with Local Convergence and Global Polarization, Journal of Conflict Resolution, 41: 2, 203-226 Epstein, J. M. (2006). Generative Social Science: Studies in Agent-Based Computational Modeling. Princeton University Press. Erdős, P and Rényi, A: On Random Graph, Publ. Math. Debrecen, (1959) Falche, A and Macy, M, (2011) Small worlds and cultural polarization, Journal of Mathematical Sociology, 5: 1-3, 146-176 Granovetter, M.: The strength of weak ties: a network theory revisited. Sociological Theory 1, 201–233 (1983) Gilbert, G. N. and K. G. Troitzsch (1999). Simulation for the social scientist. Buckingham: Open University Press. Hedström, P. (2006). Anatomia del sociale. Milano: Bruno Mondadori.

Hummon, N.P. and Fararo, T. J., Actors and Networks as Object, Social Networks 17 (1995) 1-26 Macy, M. W. and R. Willer (2002, August). From factors to actors: computational sociology and agent-based modeling. Annual Review of Sociology 28, 143–166. Manzo, G. (2007). Variables, mechanisms and simulations: can the three methods be syntesized. Revue Française de Sociologie 48(5), 35–71. Nowak, Szamrej and Latané (1990), From Private Attitude to Public Opinion: a Dynamic Theory of Social Impact, Psychological review, 97: 3, 362-376 Shalizi, C.R: (2004), Methods and Techniques of Complex Systems Science: An Overview, arXiv:nlin/0307015v4 Squazzoni, F. (2013). Agent-based computational sociology. Springer. Terna, P. (1998). Simulation tools for social scientists: building agent based models with swarm. Journal of Artificial Societies and Social Simulations vol. 1, n. 2. Watts, D.J., Strogatz, S.H.: Collective dynamics of small-world networks. Nature, 393, 440–442 (1998)

76

Page 77: Agent-Based Modeling for Sociologists

Testo

Thank you for your attention!email me: [email protected]

77