41
2013-10-21 © ETH Zürich | Modeling and Simulating Social Systems with MATLAB Lecture 5 – Agent-based Modeling / Game Theory © ETH Zürich | Chair of Sociology, in particular of Modeling and Simulation Tobias Kuhn and Olivia Woolley

Modeling and Simulating Social Systems with MATLAB · 2013-10-21 Modeling and Simulating Social Systems with MATLAB 3 Object-oriented Programming in MATLAB MATLAB has support for

  • Upload
    others

  • View
    11

  • Download
    1

Embed Size (px)

Citation preview

Page 1: Modeling and Simulating Social Systems with MATLAB · 2013-10-21 Modeling and Simulating Social Systems with MATLAB 3 Object-oriented Programming in MATLAB MATLAB has support for

2013-10-21© ETH Zürich |

Modeling and Simulating Social Systems with MATLAB

Lecture 5 – Agent-based Modeling / Game Theory

© ETH Zürich |

Chair of Sociology, in particular of

Modeling and Simulation

Tobias Kuhn and Olivia Woolley

Page 2: Modeling and Simulating Social Systems with MATLAB · 2013-10-21 Modeling and Simulating Social Systems with MATLAB 3 Object-oriented Programming in MATLAB MATLAB has support for

2013-10-21 Modeling and Simulating Social Systems with MATLAB 22

Schedule of the courseIntroduction to MATLAB

Introduction to social-science modeling and simulations

Working on projects (seminar thesis)

Handing in seminar thesis and giving a presentation

Research Plan is due TODAY

23.09.

30.09.

07.10.

14.10.

21.10.

28.10.

04.11.

11.11.

18.11.

25.11.

02.12.

09.12.

16.12.

Page 3: Modeling and Simulating Social Systems with MATLAB · 2013-10-21 Modeling and Simulating Social Systems with MATLAB 3 Object-oriented Programming in MATLAB MATLAB has support for

2013-10-21 Modeling and Simulating Social Systems with MATLAB 3

Object-oriented Programming in MATLAB MATLAB has support for object-oriented programming

However, we do not encourage to use this feature, as it is not an efficient way of programming in MATLAB

Use of vectors and matrices instead of objects

If you still want to have a look at how object-oriented programming works in MATLAB:

http://www.mathworks.com/company/newsletters/articles/introduction-to-object-oriented-programming-in-matlab.html

Page 4: Modeling and Simulating Social Systems with MATLAB · 2013-10-21 Modeling and Simulating Social Systems with MATLAB 3 Object-oriented Programming in MATLAB MATLAB has support for

2013-10-21 Modeling and Simulating Social Systems with MATLAB 4

Repetition Cellular automata (CA) are abstract representations of

interaction dynamics in discrete space and time

Reduce complexity of interaction to simple microscopic interaction rules; canonical way to formalize interaction range are neighborhoods (Moore, van Neuman)

Approaches using cellular automata have a few natural limitations: only discrete time space and time; results depend on updating scheme (simultaneous vs. sequential); system dynamics can not always easily be reduced to microscopic interaction rules

Can be abstract test beds for complex systems: same results from CA and dynamical systems approach (Ex. 2)

4

Page 5: Modeling and Simulating Social Systems with MATLAB · 2013-10-21 Modeling and Simulating Social Systems with MATLAB 3 Object-oriented Programming in MATLAB MATLAB has support for

2013-10-21 Modeling and Simulating Social Systems with MATLAB 5

Motivation for microscopic modeling A number of real world processes may be reduced to

simple local interaction rules (e.g. Swarm Intelligence)

To correctly capture complex system dynamics with local individual interactions, dynamic components of a system have to be ‘atomizable’ in the context under consideration (e.g. pedestrians behave like particles)

Represents reductionist (or mechanistic) point of view in contrast to a systemic (or holistic) approach

In line with methods that have fueled the success of natural science research in the past 200 years

Game Theory provides great framework to formalize and reduce complex (strategic) interactions

5

Page 6: Modeling and Simulating Social Systems with MATLAB · 2013-10-21 Modeling and Simulating Social Systems with MATLAB 3 Object-oriented Programming in MATLAB MATLAB has support for

2013-10-21 Modeling and Simulating Social Systems with MATLAB 6

Swarm Intelligence (1989)

6

Ant colonies, bird flocking, animal herding, bacterial

growth, fish schooling…

Page 7: Modeling and Simulating Social Systems with MATLAB · 2013-10-21 Modeling and Simulating Social Systems with MATLAB 3 Object-oriented Programming in MATLAB MATLAB has support for

2013-10-21 Modeling and Simulating Social Systems with MATLAB 7

Swarm Intelligence

7

Key Concepts:

decentralized control

interaction and learning

self-organization

Page 8: Modeling and Simulating Social Systems with MATLAB · 2013-10-21 Modeling and Simulating Social Systems with MATLAB 3 Object-oriented Programming in MATLAB MATLAB has support for

2013-10-21 Modeling and Simulating Social Systems with MATLAB 8

Boids (Reynolds,1986)

8

A boid is a simulated individual particle or object

moving about in a virtual space.

Page 9: Modeling and Simulating Social Systems with MATLAB · 2013-10-21 Modeling and Simulating Social Systems with MATLAB 3 Object-oriented Programming in MATLAB MATLAB has support for

2013-10-21 Modeling and Simulating Social Systems with MATLAB 9

More information:http://www.red3d.com/cwr/boids/

9

Separation: steer to avoid crowding local flockmates

Alignment: steer towards the average heading of local flockmates

Cohesion: steer to move toward the average position of local flockmates

Page 10: Modeling and Simulating Social Systems with MATLAB · 2013-10-21 Modeling and Simulating Social Systems with MATLAB 3 Object-oriented Programming in MATLAB MATLAB has support for

2013-10-21 Modeling and Simulating Social Systems with MATLAB 10

Game Theory Mathematical framework for strategic interactions of

individuals

Formalizes the notion of finding a ‘best strategy’ (Nash equilibrium) when facing a well-defined decision situation; conceptualization through ‘games’

Underlying assumption is that individuals optimize their ‘payoffs’ (or more precisely: ‘utility’) when faced with strategic decisions

Huge difference between game theoretic results for one-shot games vs. repeated interactions

10

Page 11: Modeling and Simulating Social Systems with MATLAB · 2013-10-21 Modeling and Simulating Social Systems with MATLAB 3 Object-oriented Programming in MATLAB MATLAB has support for

2013-10-21 Modeling and Simulating Social Systems with MATLAB 11

Game Theory: assumptions Game: (definition in class, or in any of the references)

Rationality: alignment between preferences, belief, and actions

Players want to maximize their payoff

A player's belief can be summarized as “I am rational, you are rational. I know that you are rational, and you know that I know that you rational. Everybody knows that I know that you are rational, and you know…”

Players act accordingly: best response

11

Page 12: Modeling and Simulating Social Systems with MATLAB · 2013-10-21 Modeling and Simulating Social Systems with MATLAB 3 Object-oriented Programming in MATLAB MATLAB has support for

2013-10-21 Modeling and Simulating Social Systems with MATLAB 12

Nash Equilibrium

12

Page 13: Modeling and Simulating Social Systems with MATLAB · 2013-10-21 Modeling and Simulating Social Systems with MATLAB 3 Object-oriented Programming in MATLAB MATLAB has support for

2013-10-21 Modeling and Simulating Social Systems with MATLAB 13

Nash Equilibrium Is the strategy that players always play with

no regrets: best response

No player has an incentive to deviate from a Nash equilibrium

Is not always unique

Some questions

Is Nash an optimal strategy ?

What is the difference between a Pareto-efficient equilibrium and a Nash Equilibrium?

Why do players play Nash? Do they?

13

Page 14: Modeling and Simulating Social Systems with MATLAB · 2013-10-21 Modeling and Simulating Social Systems with MATLAB 3 Object-oriented Programming in MATLAB MATLAB has support for

2013-10-21 Modeling and Simulating Social Systems with MATLAB 14

Game Theory - Human Cooperation

Prisoner’s dilemma

14

15,15 20,0

0,20 19,19

Page 15: Modeling and Simulating Social Systems with MATLAB · 2013-10-21 Modeling and Simulating Social Systems with MATLAB 3 Object-oriented Programming in MATLAB MATLAB has support for

2013-10-21 Modeling and Simulating Social Systems with MATLAB 15

The Evolution of Cooperation (Axelrod,1984)

Tit for tat being nice (cooperate first) pay back immediately easy to be recognized

Shadow of the future (discount parameter) if the probability of meeting again is large enough, it is

better to be nice…

15

Page 16: Modeling and Simulating Social Systems with MATLAB · 2013-10-21 Modeling and Simulating Social Systems with MATLAB 3 Object-oriented Programming in MATLAB MATLAB has support for

2013-10-21 Modeling and Simulating Social Systems with MATLAB 16

Game Theory - Coordination Games

1,1 0,0

0,0 1,1

16

Game Examples:• Stag-hunt • Assurance game• Chicken game• Hawk-dove game

Page 17: Modeling and Simulating Social Systems with MATLAB · 2013-10-21 Modeling and Simulating Social Systems with MATLAB 3 Object-oriented Programming in MATLAB MATLAB has support for

2013-10-21 Modeling and Simulating Social Systems with MATLAB 17

Evolutionary Game Theory Classical Game Theory suffers from a number of

conceptual weaknesses (hyper-rational players, selection problem of strategy, static theory)

Evolutionary Game Theory introduces evolutionary dynamics for strategy selection, i.e. the evolutionary most stable strategy dominates the system dynamics

Players are rational about their own decisions but do not need to know the strategies (and reasoning) of all other players!

Very suited for agent-based modeling approaches as it allows for learning and adaption of agents!

17

Page 18: Modeling and Simulating Social Systems with MATLAB · 2013-10-21 Modeling and Simulating Social Systems with MATLAB 3 Object-oriented Programming in MATLAB MATLAB has support for

2013-10-21 Modeling and Simulating Social Systems with MATLAB 18

Evolutionary Learning

The main assumption underlying evolutionary

thinking is that the entities which are more

successful at a particular time will have the best

chance of being present in the future

Selection

Replication

Mutation

18

Page 19: Modeling and Simulating Social Systems with MATLAB · 2013-10-21 Modeling and Simulating Social Systems with MATLAB 3 Object-oriented Programming in MATLAB MATLAB has support for

2013-10-21 Modeling and Simulating Social Systems with MATLAB 19

Evolutionary Learning

Selection is a discriminating force that favors

some specific entities rather than others

Replication ensures that the entities (or their

properties) are preserved, replicated or inherited

from one generation to another

Selection and replication work closely together,

and in general tend to reduce diversity

The generation of new diversity is the job of the

mutation mechanism

19

Page 20: Modeling and Simulating Social Systems with MATLAB · 2013-10-21 Modeling and Simulating Social Systems with MATLAB 3 Object-oriented Programming in MATLAB MATLAB has support for

2013-10-21 Modeling and Simulating Social Systems with MATLAB 20

How agents can learn Imitation: People copy the behavior of others, especially

behavior that is popular or appears to yield high payoffs.

Reinforcement: People tend to adopt actions that yielded a high payoff in the past, and to avoid actions that yielded a low payoff.

Best reply: People adopt actions that optimize their expected payoff given what they expect others to do. Subjects choose best replies to the empirical frequency distribution of their opponents’ previous actions, i.e. “Fictitious Play”. Agents may also update their beliefs about others’ behavior.

20

Page 21: Modeling and Simulating Social Systems with MATLAB · 2013-10-21 Modeling and Simulating Social Systems with MATLAB 3 Object-oriented Programming in MATLAB MATLAB has support for

2013-10-21 Modeling and Simulating Social Systems with MATLAB 21

From Factors to Actors (Macy,2002)

Simple and predictable local interactions can

generate familiar but enigmatic global patterns,

such as the diffusion of information, emergence

of norms, coordination of conventions, or

participation in collective action

21

Page 22: Modeling and Simulating Social Systems with MATLAB · 2013-10-21 Modeling and Simulating Social Systems with MATLAB 3 Object-oriented Programming in MATLAB MATLAB has support for

2013-10-21 Modeling and Simulating Social Systems with MATLAB 22

From Factors to Actors (Macy,2002)

Agents are autonomous

Agents are interdependent

Agents follow simple rules

Agents are adaptive and backward-looking

22

Page 23: Modeling and Simulating Social Systems with MATLAB · 2013-10-21 Modeling and Simulating Social Systems with MATLAB 3 Object-oriented Programming in MATLAB MATLAB has support for

2013-10-21 Modeling and Simulating Social Systems with MATLAB 2323

Programming a simulator

Agent-based simulations the models simulate the simultaneous operations of

multiple agents, in an attempt to re-create and predict the actions of complex phenomena

agents can be pedestrians, animals, customers, internet users, etc.

Page 24: Modeling and Simulating Social Systems with MATLAB · 2013-10-21 Modeling and Simulating Social Systems with MATLAB 3 Object-oriented Programming in MATLAB MATLAB has support for

2013-10-21 Modeling and Simulating Social Systems with MATLAB 2424

Programming a simulator

Agent-based simulations

Time line

Characterized by a state (or states)

(location, dead/alive, color, level of excitation)

Time t Time t + dt

State update

According to set of behavioral rules

New state

T1 T2dt

Page 25: Modeling and Simulating Social Systems with MATLAB · 2013-10-21 Modeling and Simulating Social Systems with MATLAB 3 Object-oriented Programming in MATLAB MATLAB has support for

2013-10-21 Modeling and Simulating Social Systems with MATLAB 2525

Programming a simulator

Agent-based simulations

Characterized by a state

(location, dead/alive, level of excitation)

Time t Time t + dt

State update

According to set of behavioral rules,

including neighborhood

New state

Time line T1 T2

Page 26: Modeling and Simulating Social Systems with MATLAB · 2013-10-21 Modeling and Simulating Social Systems with MATLAB 3 Object-oriented Programming in MATLAB MATLAB has support for

2013-10-21 Modeling and Simulating Social Systems with MATLAB 2626

Programming a simulator

Programming an agent-based simulator often

goes in five steps Initialization

- Initial state; parameters; environment

Time loop- Processing each time steps

Agents loop- Processing each agents

Update- Updating agent i at time t

Save data- For further analysis

Initialization

Save data

Agents loop

end

Update state

Page 27: Modeling and Simulating Social Systems with MATLAB · 2013-10-21 Modeling and Simulating Social Systems with MATLAB 3 Object-oriented Programming in MATLAB MATLAB has support for

2013-10-21 Modeling and Simulating Social Systems with MATLAB 2727

Define and initialize all variables in a script file

that serves as the main file of your program

Execute the actual simulation as function with

the pre-defined variables as inputs

Automated analysis, visualization, etc. of the

simulation output may then be implemented in

the main file using the outputs of the function

that runs the actual program

Efficient Structure

Programming a simulator

Page 28: Modeling and Simulating Social Systems with MATLAB · 2013-10-21 Modeling and Simulating Social Systems with MATLAB 3 Object-oriented Programming in MATLAB MATLAB has support for

2013-10-21 Modeling and Simulating Social Systems with MATLAB 2828

Programming a simulator

Step 1: Defining the initial state & parameters

% Initial state

t0 = 0; % begining of the time linedt = 1; % time stepT = 100; % number of time steps

% Initial state of the agents

State1 = [0 0 0 0];State2 = [1 1 1 1];

% etc…

Page 29: Modeling and Simulating Social Systems with MATLAB · 2013-10-21 Modeling and Simulating Social Systems with MATLAB 3 Object-oriented Programming in MATLAB MATLAB has support for

2013-10-21 Modeling and Simulating Social Systems with MATLAB 2929

Programming a simulator

Step 1: Defining the initial state & parameters

% Initial state

t0 = 0; % begining of the time linedt = 1; % time stepT = 100; % number of time steps

% Initial state of the agents

State1 = zeros(1,50);State2 = rand(1,50);

Page 30: Modeling and Simulating Social Systems with MATLAB · 2013-10-21 Modeling and Simulating Social Systems with MATLAB 3 Object-oriented Programming in MATLAB MATLAB has support for

2013-10-21 Modeling and Simulating Social Systems with MATLAB 3030

Programming a simulator

Step 2: Covering each time step

% Time loop

For t=t0:dt:T

% What happens at each time step? % - Update environment % - Update agents

end

Page 31: Modeling and Simulating Social Systems with MATLAB · 2013-10-21 Modeling and Simulating Social Systems with MATLAB 3 Object-oriented Programming in MATLAB MATLAB has support for

2013-10-21 Modeling and Simulating Social Systems with MATLAB 3131

Programming a simulator

Step 3: Covering each agent

% Agent loop

For i=1:length(States)

% What happens for each agent?

end

Page 32: Modeling and Simulating Social Systems with MATLAB · 2013-10-21 Modeling and Simulating Social Systems with MATLAB 3 Object-oriented Programming in MATLAB MATLAB has support for

2013-10-21 Modeling and Simulating Social Systems with MATLAB 3232

Programming a simulator

Step 4: Updating agent i at time t

% Update%% Example: Each agent has 60% chance to% switch to state 1

randomValue = rand();

if (randomValue < 0.6) State(i)=1;else State(i)=0;end

Page 33: Modeling and Simulating Social Systems with MATLAB · 2013-10-21 Modeling and Simulating Social Systems with MATLAB 3 Object-oriented Programming in MATLAB MATLAB has support for

2013-10-21 Modeling and Simulating Social Systems with MATLAB 3333

Programming a simulator

Step 4: Updating agent i at time t

% Update%% Example: Each agent has chance 'prob' to% switch to state 1

randomValue = rand();

if (randomValue < prob) State(i)=1;else State(i)=0;end

Define in first part

Page 34: Modeling and Simulating Social Systems with MATLAB · 2013-10-21 Modeling and Simulating Social Systems with MATLAB 3 Object-oriented Programming in MATLAB MATLAB has support for

2013-10-21 Modeling and Simulating Social Systems with MATLAB 3434

Programming a simulator

Step 4: Updating agent i at time t

% Initial state

t0 = 0; % begining of the time linedt = 1; % time stepT = 100; % number of time stepsproba = 0.6; % probability to switch state

Page 35: Modeling and Simulating Social Systems with MATLAB · 2013-10-21 Modeling and Simulating Social Systems with MATLAB 3 Object-oriented Programming in MATLAB MATLAB has support for

2013-10-21 Modeling and Simulating Social Systems with MATLAB 3535

Programming a simulator

Step 5: Final processing

% Outputs and final processing

propAlive = sum(states)/length(states);

% propAlive is the main output of the% simulation

Page 36: Modeling and Simulating Social Systems with MATLAB · 2013-10-21 Modeling and Simulating Social Systems with MATLAB 3 Object-oriented Programming in MATLAB MATLAB has support for

2013-10-21 Modeling and Simulating Social Systems with MATLAB 36

>> p=simulation()

p =

0.54

>> p=simulation()

p =

0.72

Programming a simulator

Running simulation

Page 37: Modeling and Simulating Social Systems with MATLAB · 2013-10-21 Modeling and Simulating Social Systems with MATLAB 3 Object-oriented Programming in MATLAB MATLAB has support for

2013-10-21 Modeling and Simulating Social Systems with MATLAB 3737

Programming a simulator

Alternatively:

% Running N simulations

N = 1000 % number of simulations

for n=1:N p(n)=simulation();end

Page 38: Modeling and Simulating Social Systems with MATLAB · 2013-10-21 Modeling and Simulating Social Systems with MATLAB 3 Object-oriented Programming in MATLAB MATLAB has support for

2013-10-21 Modeling and Simulating Social Systems with MATLAB 3838

Programming a simulator

Alternatively:

>> hist(p)

Page 39: Modeling and Simulating Social Systems with MATLAB · 2013-10-21 Modeling and Simulating Social Systems with MATLAB 3 Object-oriented Programming in MATLAB MATLAB has support for

2013-10-21 Modeling and Simulating Social Systems with MATLAB 3939

Exercise 1

There is some additional material on the iterated

prisoner’s dilemma available in the directory

'prisoner' of

https://github.com/msssm/lecture_files

Experiment with the code and try to find out what

it does and how to interpret the results

Page 40: Modeling and Simulating Social Systems with MATLAB · 2013-10-21 Modeling and Simulating Social Systems with MATLAB 3 Object-oriented Programming in MATLAB MATLAB has support for

2013-10-21 Modeling and Simulating Social Systems with MATLAB 4040

Exercise 2

Start thinking about how to adapt the general

simulation engine shown today for your project

Problems: Workspace organization (files, functions, data) Parameters initialization Number of agents Organization of loops Which data to save, how frequently, in which format What kind of interactions are relevant …

Page 41: Modeling and Simulating Social Systems with MATLAB · 2013-10-21 Modeling and Simulating Social Systems with MATLAB 3 Object-oriented Programming in MATLAB MATLAB has support for

2013-10-21 Modeling and Simulating Social Systems with MATLAB 4141

References

Fudenberg & Tirole, Game Theory, MIT Press (2000)

Meyerson, Game Theory, Harvard University Press (1997)

Herbert Gintis, Game Theory Evolving, Princeton Univ. Press, 2nd

Edition (2009)

Uhrmacher, Weyns (Editors), Multi-Agent Systems – Simulation

and Applications, CRC Press (2009)

A step by step guide to compute the Nash equilibrium:

http://uwacadweb.uwyo.edu/Shogren/jaysho/NashNotes.pdf

https://github.com/Sandermatt/ETHAxelrodNoise