Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Money, Sex and Evolution

Money, Sex and Evolution

Agent-based modeling using Ruby


Sau Sheong Chang

March 28, 2014


  1. Money,  Sex  and  Evolu0on   Agent-­‐based  modeling   with  Ruby

      Chang  Sau  Sheong   PayPal   March  2014  
  2. About  me  

  3. None
  4. None
  5. Common  Starling  (Sturnus  vulgaris)  

  6. •  Boids  by  Craig  Reynolds   •  Models  animal  mo0ons

      through  simple  rules,  resul0ng   in  flocking  behavior   •  Emergent  behavior  –  complex   behavior  arising  from  simple   rules   •  Eventual  basis  for  some  of  the  more  impressive   cinematographic  anima0on  –  Lord  of  the  Rings,   Batman  Returns,  Lion  King  etc  
  7. 3  simple  rules   •  Separa0on   – Move  to  avoid

     crowding  in  the   flock   •  Alignment   – Move  in  the  same  direc0on  as   the  flock   •  Cohesion   – Move  towards  the  center  of  the   flock  
  8. Agent-­‐based  modeling     •  Also  known  as  mul0-­‐agent  simula0on

      •  Autonomous  agents  interac0ng  with  each   other  and  the  environment   •  Used  in  analysing  supply  chain  op0miza0on,   traffic  paRerns,  economic  models,  spread  of   epidemics   •  Influenced  by  John  Conway’s  Game  of  Life   •  Heavily  influenced  by  Epstein  and  Axtell’s   Sugarscape  simula0ons  
  9. Utopia   •  Ar0ficial  world  called  Utopia   •  Populated

     by  roids  (implementa0on  of  boids   in  Ruby)   •  Free  to  wander  through  an  infinite  landscape   •  Simula0ons  are  created  with  Ruby  and  the   Gosu  library  
  10. Base  simula0on  

  11. None
  12. None
  13. Why  does  income   inequality  exist?  

  14. Change  behavior  in  base  simula0on   •  Roids  now  have

     an  energy  level  (randomly   assigned)   •  At  every  0ck,  roids  lose  1  energy   •  If  energy  level  reaches  0,  it  will  die   •  To  prevent  this,  it  will  ac0vely  seek  out  food   and  consume  it   •  Consuming  food  replenishes  energy  level   •  (But  roids  are  s0ll  immortal)  
  15. Data  collec0on   •  At  every  0ck  record  the  energy

     level  of  each   roid   •  Do  this  un0l  the  end  of  the  world  (simula0on   completes)  
  16. First  simula0on  

  17. Energy  level  analysis  

  18. On  the  day  of  reckoning   Most  roids  are  dead

      A  few  roids  have  a   lot  of  energy  
  19. Gini  coefficient  and  Lorenz  curve   •  The  Gini  coefficient

     is  a   measure  of  the  inequality  of   a  distribu0on,  0  is  perfect   equality  and  1  is  perfect   inequality   •  Defined  mathema0cally   based  on  the  Lorenz  curve   Area  is  the  amount   of  inequality  
  20. Lorenz  curve  on  inequality   Not  much   inequality  

    A  lot  of   inequality!  
  21. Observa0on   •  Over  a  period  of  0me,  a  small

     percentage  of   the  popula0on  gets  a  large  propor0on  of  the   energy   •  What  is  the  cause  of  this?  
  22. Why  the  inequality?   •  Hard  work  and  inherent  talent/ability?

      – All  roids  are  the  SAME   •  Some  roids  luckier  than  others  i.e.   random  distribu0on?   – If  it’s  random  then  it  should  follow  a  normal   distribu0on  
  23. The  rich  get  richer,  the  poor  get   poorer  

    •  Back  to  where  we  started  –  emergent   behavior   •  Small  changes  ripple  down  to  large  effects   over  a  period  of  0me   •  Simple  local  rules  brings  complex  global   changes  
  24. None
  25. Spicing  things  up  with  sex   •  Roids  have  gender

     now  –  male  and  female   •  Males  are  blue,  females  are  red,  underaged  are     •  If  female  is  within  childbearing  age  (25  to  50   0cks)  and  if  it  has  enough  energy,  it  will  look  for  a   nearby  male     •  If  male  is  also  within  childbearing  age  and  have   enough  energy,  they  will  procreate  and  produce   a  baby  roid   •  Both  male  and  female  loses  energy  each  0me   they  procreate  
  26. Grim  reaper  arrives   •  With  birth  we  introduce  death

      •  Each  roid  has  a  randomly  assigned  lifespan   •  Each  0ck  increases  the  age  of  the  roid   •  Roid  dies  when  it  reaches  its  lifespan  
  27. Data  collec0on   •  We  want  to  inves0gate  how  the

     popula0on   changes  once  we  introduce  sex  and  death   •  At  every  0ck  we  collect  the  popula0on  of  the   en0re  world,  as  well  as  popula0on  of  males   and  females  
  28. Second  simula0on  

  29. Data  analysis   Popula0on   fluctuates  

  30. With  minor  tweaks  …   Popula0on  fluctua0ons   resul0ng  in

  31. None
  32. What  just  happened?   •  I  changed  a  parameter  -­‐

     the  childbearing  age   •  Original   – CHILDBEARING_AGE  =  25..50   •  Changed   – CHILDBEARING_AGE  =  30..50   •  Does  it  happen  all  the  0me?  NO  -­‐  4  out  of  5   0mes  
  33. Looks  familiar?   •  Financial  crashes,  social  unrest  (Arab  Spring?),

      animal  ex0nc0ons,  climate  change?   •  Simula0on  suggests  small  internal  factors  can   be  enough  to  trigger  crises  that  cannot  be   recovered    
  34. Things  are  NOT  as  stable   as  we  like  to

     think  it  is  
  35. None
  36. Natural  selec0on   •  A  central  concept  in  evolu0on  

    •  “Survival  of  the  fiRest”   •  Organisms  with  more  suitable  aRributes  are   more  likely  to  reproduce  and  pass  these   aRributes  to  the  next  genera0on   •  Over  0me  these  aRributes  become   more  prominent  
  37. Natural  selec0on  in  Utopia   •  Previously  all  roids  have

     the  same  aRributes,  only   difference  is  maximum  lifespan  and  energy  level   •  In  this  simula0on,  we  add  2  new  aRributes   –  Metabolism   How  well  the  roid  converts  the  food  it  eats  into  energy  (higher   number  beRer)   –  Vision  range   How  far  away  it  can  see  food  (higher  number  beRer)   •  Roid  babies  inherit  these  2  aRributes  from  their   parents  
  38. Crossover  of  gene0c  aRributes   •  We  follow  very  simple

     Mendelian  rules  for   crossover   •  Assume  the  father  has  the  genotype  (m,v)  and   the  mother  has  genotype  (M,V)   •  Possible  genotypes  of  baby  are  (m,v),  (m,V),   (M,v)  and  (M,V)   •  We  randomly  chose  1  of  these  4  possibili0es    
  39. Data  collec0on   •  We  want  to  find  out  how

     the  metabolism  and   vision  range  aRributes  of  the  popula0on   changes  over  0me   •  At  every  0ck,  we  calculate  the  average   metabolism  and  vision  range  of  all  roids  that   are  alive  
  40. Third  simula0on  

  41. Natural  selec0on  in  ac0on!   Metabolism   improves  over  

    0me   Vision  range   improves  over   0me  
  42. What  did  we  just  see?   •  Natural  selec0on  -­‐

     the  roids  with  the  beRer   aRributes  are  able  to  survive  longer  and   reproduce  with  similar  aRributes   •  Natural  selec0on  is  not  necessarily  a  straight   line  (vision  range  meanders  for  a  while)  
  43. Wrapup   •  Simula0ons  ≠  real  life   •  Conclusions

     open  for  interpreta0on   •  Programming  as  tools  to  discover  the  world   around  us  
  44. Miscellaneous  stuff   •  Simula0on  –  Ruby,  Gosu   • 

    Analysis  –  Ruby,  Gruff   •  hRps://github.com/sausheong/utopia   •  sausheong@gmail.com  -­‐  personal   •  sauchang@paypal.com  -­‐  work