1. Computing

An Introduction to Models and Simulation

A simple economic model shows how to get started.

From , former About.com Guide

Updated January 13, 2012

For reasons that have nothing to do with Visual Basic coding, I wrote a very simple simulation of a economic proposition today. Because it illustrates the process of writing a math simulation, I thought readers here might like to see it.

Editorial Note: I'm not drawing any conclusions from this model here. (You might. There's nothing I can do about that.) This is a programming forum, not a political one. But if you want to write a model that illustrates some different way of looking at it, send me email about it. This is an equal opportunity site!

The basic economic idea I'm modeling is:

What happens when everybody has a fixed amount of "expense" but a variable amount of "income" based on wealth?

The general outline of what happens is something that you can guess, but I was interested seeing how fast it happened, how much it depended on the fixed expense, and the way a percentage of the wealth was added back in influenced the result. These are the kind of questions you can answer with a math model. They're used in everything from building bridges to predicting the weather, and as this one shows, you can write your own fairly easily and gain a better understanding of your world doing it. (Not to mention, become a better coder!)

The type of simulation described here is called "discrete event" simulation. That means everything happens as a chronological sequence of events. Each event occurs at an instant in time and marks a change of state in the system. In this type of simulation, there are several things that you have to decide when you write the code:

->1 What are the starting conditions of the model?
->2 What are the variable factors that the model will simulate?
->3 How will the variable factors be modified when the model is run?

In my model, I decided to start everyone with an initial "wealth" of 1000 and no other starting conditions. The variable factor is the "income" that is added back into the wealth. I programmed two ways of doing it. One way was to calculate a random percentage for each player during each "round" (A time period that you can think of as a week or a month.) but the percentage of "return on wealth" would add to 1 for all players. In modeling, this is often called the "zero sum game". In other words, whenever one player "wins", the others have to "lose" by the same amount. The second way was to use a random percentage of wealth between zero and 25%, but the rate assigned to one player didn't depend on what was assigned to the others.

Once you get going on this kind of thinking, you can dream up all kinds of ways that the model can be changed. For example, a "skill factor" might be used. That is, a random "skill" could be assigned to each player that would change the return on wealth assigned. After you get this working, you might decide to apply the skill factor only after, say, 20 rounds. Or you might program a random "disaster" that reduces a randomly selected player's wealth by half and see how that affects the outcome.

But enough of this, here's how the code works. I use my favorite ListView control to hold the players and their current wealth. Simple TextBox controls hold all the starting factors:

--------
Click Here to display the illustration
--------

The heart of the model is a simple loop that adjusts each player in the ListView according to expenses and income. Note that one method of calculating expense is commented out while another is active. These are put into subs by themselves to make it easy to do that. The code below is executed for each "round" in the game.


'CalcUniformExpense(playerScores)
CalcRandPercentExpense(playerScores)
playerCounter = 0
For Each player As  _
    ListViewItem In lvwCapitalismGame.Items
    ' Fixed Expense
    Dim exp As Decimal =
        CDec(txtFixedExpense.Text)
    ' Wealth
    Dim wel As Decimal =
        CDec(
            lvwCapitalismGame.Items(
                playerCounter).SubItems(2).Text)
    ' Subtract Fixed Expense
    wel -= exp
    ' Income
    Dim inc As Decimal =
        wel * playerScores(playerCounter)
    ' Add Income
    wel += inc
    lvwCapitalismGame.Items(
        playerCounter).SubItems(2).Text = CStr(wel)
    playerWealth(Round, playerCounter) = CStr(wel)
    playerCounter += 1
Next

One of the subs that assigns an income generating "score" to each player (the "zero sum game" method) is shown below.


Private Sub CalcUniformExpense(playerScores)
    Dim randObj As New Random()
    Dim playerCounter As Integer = 0
    Dim playersTotalScores As Decimal = 0
    Do While playerCounter <
            lvwCapitalismGame.Items.Count
        playerScores(playerCounter) =
            randObj.Next
        playersTotalScores +=
            playerScores(playerCounter)
        playerCounter += 1
    Loop
    playerCounter = 0
    Do While playerCounter <
        lvwCapitalismGame.Items.Count
        playerScores(playerCounter) /=
            playersTotalScores
        playerCounter += 1
    Loop
End Sub

To see what happens, in this program, I just dump an array where each round of results is saved to the Output window. It's easy to copy it into an Excel spreadsheet and graph it then. A more polished program could pass the array to Excel and graph it automatically, but I just wrote this for my own use.

--------
Click Here to display the illustration
--------

  1. About.com
  2. Computing
  3. Visual Basic

©2013 About.com. All rights reserved.