Sau Sheong Chang
February 20, 2016
620

# Programming Complexity

Modeling Complex Systems with Go and React

## Sau Sheong Chang

February 20, 2016

## Transcript

1. Programming
Complexity
Modeling  Complex  Systems  with  Go  and  React
Chang  Sau Sheong
[email protected]

2. Complexity
The  behavior  that  emerges  from  a  group  of  interacting
parts,  that  is  not  directly  the  result  of  interactions
between  individual  parts

3. Modeling  cultural  interactions

4. The  Dissemination  of  Culture
American  political  scientist,  complexity  theory  researcher,
National  Medal  of  Science  laureate
Robert  Axelrod

5. Cultures  who  are  similar  to  each  other  are
likely  to  interact

6. When  cultures  interact,  they  become  more
alike  each  other

7. Agent-­‐based  model  where  each  agent
represents  a  culture

8. Culture  =  Set  of  Features
language,  religion,  style  of  dress  etc

9. Trait  =  possible  values  of  a  feature

10. 36  x  36  grid
each  cell  has  a  culture
culture

11. 5 4 3 2 1 0
Each  culture  has  6  features

12. Each  feature  has  16  possible  traits
0 1 2 3 4 5 6 7 8 9 A B C D E F

13. 8  neighbours

14. Algorithm
•At  every  tick,  randomly  pick  n  cells
•Compare  features  of  the  culture  with  each  of  its
neighbours
•If  the  trait  difference  for  the  same  feature  between  2
cultures  are  <  t,  randomly  select  either  culture  and
copy  the  trait  of  the  feature  to  the  other  culture

15. A
B
A  and  B  are  2
neighbouring
cultures

16. difference  is  5  – 2  =  3
culture  B
culture  A A 0 5 5 F 6
F A D 2 D 0

17. A 0 5 5 F 6
F A D 2 D 0
culture  B
culture  A
Difference  between  2  cultures  =  34
5 A 8 3 2 6
+ + + + + = 34

18. F F F F F F
0 0 0 0 0 0
culture  B
culture  A
100%  different  =  96
F F F F F F
+ + + + + = 96

19. A 0 5 5 F 6
culture  B
culture  A
Difference  between  2  cultures  that  are  100%  similar  =  0
0 0 0 0 0 0
+ + + + + = 0
A 0 5 5 F 6

20. The  more  similar  2  cultures  are,  the  more  likely
there  will  be  cultural  exchange
Probability  of  cultural  exchange  =  1  -­‐ difference
between  2  cultures  ÷ 96

21. Probability  of  cultural  exchange:
1   −
34
96
= 1   − 0.354 = 64.6%

22. culture  B
culture  A
Randomly  choose  to  copy  one  of  culture  A’s
features  to  culture  B  or  vice  versa
A 0 5 5 F 6
F A 5 2 D 0
When  interaction  happens

23. What  do  we  want  to  measure?
•Average feature  distance  tells  us  how  far  apart
(different)  the  cultures  are
•Uniques tells  us  the  number  of  unique  cultures  at  any
given  point  in  time
•Changes tells  us  how  vibrant  the  cultural  exchanges
are

24. Observations
•Eventual  equilibrium  is  only  a  few  dominant  cultures
•Dominant  cultures  can  be  quite  different  from  each
other
•Smaller  areas  results  in  faster  equilibrium  and  smaller
number  of  dominant  cultures
•A  culture  that  is  more  dominant  at  a  point  in  time
doesn’t  mean  it  will  be  dominant  in  the  end

25. Modeling  racial
segregation

26. Chicago

27. Washington  D.C.
New  York  City
Detroit
Los  Angeles

28. London

29. Dynamic  Models  of  Segregation
American  economist,  2005  Nobel  Prize  in  Economics
Thomas  Schelling

30. 36  x  36  grid
each  cell  has  a  household

31. 8  neighbours

32. Algorithm
•At  every  tick,  check  every  cell
•If  at  least  n  number  of  its  neighbours are  of  the  same
‘race’,  do  nothing
•Otherwise,  randomly  pick  an  empty  cell  and  move
there

33. Parameters
•Acceptable number  of  neighbours (n)
•Number of  ‘races’  in  the  grid  (r)
•Percentage  of  vacant  cells  (v)
•Policy  limitation – cannot  have  more  than  (l)  number
of  neighbours of  the  same  race

34. Observations
• Segregation  happens  even  if  there  is  weak  preference  for  neighbours
of  same  type
• The  weaker  the  preference,  the  less  segregated  (smaller  clusters)
• The  stronger  the  preference,  the  more  segregated  (larger  clusters)
• At  a  threshold,  stronger  preference  results  in  an  unstable  but  non-­‐
segregated  state  (occupants  always  moving  to  another  cell)
• Number  of  races  have  no  impact  of  segregation
• Number  of  vacant  cells  have  no  impact  on  segregation
• Policy  enforcement  has  limited  impact  on  segregation  (stronger
policies  result  in  unstable  state)

35. Modeling  bystander  effect

36. Game  Theory

37. The  study  of  mathematical  models  of  conflict  and
cooperation,  often  used  in  economics  and  political
science

38. Volunteer’s  Dilemma

39. V  =  value  gained  if  at  least  1  person  volunteers
C  =  individual  cost  of  volunteering
A =  overall  cost  if  no  one  volunteers

40. You volunteer
You  don’t
volunteer
I volunteer
I  don’t  volunteer
V  -­ C
V  -­ C V  -­ C
V
V  -­ C
V V  -­ A
V  -­ A
Payoff  matrix  for  a  2-­‐player  game

41. You  volunteer
You don’t
volunteer
I volunteer
I  don’t  volunteer
V  -­ C
V  -­ C V  -­ C
V
V  -­ C
V V  -­ A
V  -­ A
If  I  volunteer

42. You  volunteer
You  don’t
volunteer
I volunteer
I  don’t  volunteer
V  -­ C
V  -­ C V  -­ C
V
V  -­ C
V V  -­ A
V  -­ A
If  I  don’t volunteer

43. Mixed  strategy  Nash  equilibrium
V  – C  =  pV +  (1  – p)(V  – A)
where  p  =  probability  of  volunteering

44. For  an  N-­‐player  game
Probability  of  volunteering  =  p  x  p  x  …  x  p
=  pN-­‐1
N  – 1  times

45. For  an  N-­‐player  game
Probability  of  not  volunteering  =  (1  – p)N-­‐1

46. V  =  value  if  at  least  1  volunteer
C  =  individual   cost  of  volunteering
A  =  overall  cost  if  no  volunteers
N  =  number  of  people  involved
p  =  probability  of  getting  at  least  1  volunteer
1-­‐ p  =  probability  of  getting  no  volunteers
V  – C  =  (pN-­‐1)V  +  (1  – p) N-­‐1(V  – A)

47. V  =  value  if  at  least  1  volunteer
C  =  individual   cost  of  volunteering
A  =  overall  cost  if  no  volunteers
N  =  number  of  people  involved
p  =  probability  of  getting  at  least  1  volunteer
1-­‐ p  =  probability  of  getting  no  volunteers
p =1−
C
A
"
#
\$
%
&
'
1
N−1

48. Monte  Carlo  simulation

49. Observations
•Decrease  individual  cost  of  volunteering
•Increase  the  overall  costs/impact  of  not  volunteering
(??)
•Increase  difference  between  individual  cost  and
overall  costs
•Reduce  number  of  ‘players’
•Increasing  number  of  players  have  negative  or  no
impact

50. @sausheong
[email protected]
http://blog.saush.com
http://github.com/sausheong