'Program YSM.BAS (c) 2014 by J. C. Sprott 'Calculates the distribution of wealth in a society using the yard-sale model 'Use PowerBasic Console Compiler #DEBUG ERROR ON DEFEXT a-z FUNCTION PBMAIN() w&=500 'width of graph h&=500 'height of graph p&=1e6 'population size z&=1e5 'initial wealth f&=10 'maximum percent of wealth to trade (of poorest parner) y&=500 'average trades for each m&=1e6 'maximum wealth d&=2 'decades to plot CONSOLE SET LOC w&+6,0 CLS GRAPHIC WINDOW EXE.NAME\$, 0, 0, w&, h& TO hWin??? GRAPHIC ATTACH hWin???, 0 ', REDRAW GRAPHIC COLOR %BLACK, %WHITE GRAPHIC CLEAR GRAPHIC BOX(0,0)-(w&,h&) CONSOLE SET FOCUS RANDOMIZE TIMER DIM wealth(p&),rate(p&),wbin(w&) FOR i&=1 TO p& wealth(i&)=z& NEXT i& FOR j&=1 TO y& FOR p1&=1 TO p& p2&=INT(1+p&*RND) mw=MIN(wealth(p1&),wealth(p2&))*RND IF RND<0.5## THEN wealth(p1&)=wealth(p1&)+0.01##*f&*mw wealth(p2&)=wealth(p2&)-0.01##*f&*mw ELSE wealth(p1&)=wealth(p1&)-0.01##*f&*mw wealth(p2&)=wealth(p2&)+0.01##*f&*mw END IF NEXT p1& NEXT j& FOR i&=1 TO p& wb&=INT(w&+w&*LOG10(wealth(i&)/m&)/d&) IF wb&>0 AND wb&<=w& THEN wbin(wb&)=wbin(wb&)+10^(-d&*wb&/w&) wbm&=MAX(wbm&,wbin(wb&)) END IF NEXT i& FOR xp&=1 TO w& IF wbin(xp&)=0 THEN yp&=h& ELSE yp&=-h&*LOG10(wbin(xp&)/wbm&)/d& END IF IF xp&=1 THEN GRAPHIC SET PIXEL(xp&,yp&) ELSE GRAPHIC LINE-(xp&,yp&) PRINT xp&,yp& NEXT w& GRAPHIC SET POS(w&-120,10) GRAPHIC PRINT"Yard-Sale Model" GRAPHIC SET POS(w&-120,30) GRAPHIC PRINT"After";y&;"trades" GRAPHIC SAVE"wealth5.bmp" WAITKEY\$ END FUNCTION