Saturday 31 May 2014

Gini Sim

The Gini Index is a measure of wealth distribution.

GiniSim is a simple Javascript program which demonstrates, in simple terms, flaws in free market economics, by showing that trading freely will lead to gross inequality. Copy the program into a .html file; save it and then open it in a browser: you can stop it by pressing the Stop button. Alternatively, you can download and run a simple Java version, GiniSim.jar from here.

Each bar is the wealth of a person, and the simulation starts with everyone having $10 (or £10, or 10€).

Each step simulates a free trade transaction, two monetary notes are picked at random and the person the first one belongs to pays the person the second one belongs to. Intuitively, you’d think this would average out: sometimes some people win and sometimes others would.

In reality what happens is that whenever a person accumulates wealth, it makes it more likely that someone poorer will give money to them. This is due to the fact that the chances of being paid in a transaction is proportional to their wealth - so if someone loses money from a transaction, they become less likely to gain in a future transaction.





How does this correspond to an idealism of the free market? It corresponds, because exchanges take place on the basis of being indifferent towards cash. Therefore when people gain wealth, their wealth acts as a bigger wealth footprint and people (who want to buy things) notice the cash more. If you’re poorer, that doesn’t happen, you’re not noticed, because your visible presence is the cash you hold - you literally disappear.

The important thing to note is that GiniSim demonstrates inequality without the agents involved behaving maliciously. All it does is play fairly towards cash (rather than people). It's a demonstration of a power law.

How does GiniSim1 not correspond to classical economics? GiniSim1 correspond to free trade under mercantilism, which is a zero-sum economic theory.

Here’s GiniSim, copy everything in yellow:


<!DOCTYPE html>
<html>
<body>
<button onclick="clearInterval(timer)">Stop</button>
<canvas id="myCanvas" width="800" height="600" style="border:1px solid #c3c3c3;">
Your browser does not support the HTML5 canvas tag.
</canvas>

<script>
  var c=document.getElementById("myCanvas");
  var ctx=c.getContext("2d");
  var timer;
  var rects=0;
  var cash=[0],people=[0];
  var kStartCash=10;
  var kNumPeople=100;
  function initGini() {
    var ix,iy;
    for(ix=0;ix<kNumPeople;ix++) {
      people[ix]=kStartCash; // everyone starts with $10.
      for(iy=0;iy<kStartCash;iy++) {
        cash[ix*kStartCash+iy]=ix; // each $1 is owned by a person.
      }
    }
  }
 
  function incomeSwap(from,too) {
    var ix;
    for(ix=0;ix<kNumPeople*kStartCash;ix++) {
      if(cash[ix]==from)
        cash[ix]=too;
      else if(cash[ix]==too)
        cash[ix]=from;
    }
    ix=people[from];
    people[from]=people[too];
    people[too]=ix;
  }
 
  function exchange() {
    var aNote=Math.floor(Math.random()*kStartCash*kNumPeople);
    var aOwner=cash[aNote];
    var aNewNote=Math.floor(Math.random()*kStartCash*kNumPeople);
    var aNewOwner=cash[aNewNote];
    if(people[aOwner]>0 ) {
      // can't take cash from people who have nothing.
      //}while(aNewOwner==aOwner);
      people[aOwner]-=1;
      people[aNewOwner]+=1;
      cash[aNote]=aNewOwner;
      while(aOwner>0 && people[aOwner]<people[aOwner-1]) {
          incomeSwap(aOwner,aOwner-1);
          aOwner--;
          if(aOwner==aNewOwner)
            aNewOwner++;
      }
      while(aNewOwner<kNumPeople-1 && people[aNewOwner]>people[aNewOwner+1]) {
          incomeSwap(aNewOwner,aNewOwner+1);
          aNewOwner++;
      }
    }
  }
 
  function drawImage() {
    var ix;
    for(ix=0;ix<kNumPeople;ix++) {
      ctx.fillStyle="#c0c0c0";
      ctx.fillRect(ix*8,0,8,600-people[ix]);
      ctx.fillStyle="#000000";
      ctx.fillRect(ix*8,600-people[ix],8,600);
    }
    exchange();
  }
  initGini();
  timer=setInterval(drawImage,1);
</script>
</body>
</html>

[Edit: Added link to GiniSim.jar on 20150321. Edit: I knew from the simulations that wealth always gravitates to the rich, but my reasoning for the mechanism was incorrect, because I was trying to derive it from the probabilities of a single transaction. In reality, the mechanism is due to how the probabilities change between one transaction and the next. 20150626.]