Corrado Campodonico

Simulation models for economics

Project work on

"The Real Effect of a Financial Market."

 

The applet requires Java 1.4.1 or higher. It will not run on Windows 95 or Mac OS 8 or 9. Mac users must have OS X 10.2.6 or higher and use a browser that supports Java 1.4. (Safari works, IE does not. Mac OS X comes with Safari. Open Safari and set it as your default web browser under Safari/Preferences/General.) On other operating systems, you may obtain the latest Java plugin from Sun's Java site.


An introduction to The Real Effect of a Financial Market application.


powered by NetLogo

view/download model file: the_real_effect_of_a_financial_market.nlogo

WHAT IS IT?

The purpose of this model is to outline the effects a real market, i.e. a market of traded goods, and a financial one can have on one another, particularly stressing on the role of the public and private information.

HOW IT WORKS

Four kinds of agents are considered: consumers, companies, buyers and sellers. The first two classes can trade goods according to the well known “reservation price” mechanism, whilst the latter two trade stocks of a restricted number of public companies. The traders influence the financial market in terms of basic costs of the stocks, the shareholders on the other way round change with their exchanges the production level of the public companies.

HOW TO USE IT

The total number of agents can be regulated through the five sliders whose name begins with nb-. bounderMaxPrice and bounderMinPrice stand for the bounders to the initial setup reservation price for both traders. consumerTrials is the total number of purchases made by any consumer at every time step. privateInformationPercentage is the bound for the uniformly randomly drawn correction to the public cost of a stock of a quoted company. tradePrice allows the user to choose between random price settlement and closeness to, the latter using also the slider tradeClosenessTo: the first methods sets the settled price of a trade as a random number between consumer’s and company’s reservation prices, the second fixes it close to one of the two reservation prices (0 for company, 1 for consumer). nb-stocks-to- let the user select how many stocks to exchange, i.e. to be inserted in the logs of the Stock Exchange, at every time step. tradePriceCorrection makes the companies, or the consumers, or both, correct their reservation prices according to the DesideredStock; deltaPrice is the percentage of correction.

THINGS TO NOTICE

Bear in mind not to deal with large amount of exchanged stocks, so as not to slow the evolution of the model. “tradePriceCorrection” should always be put to “none” (the reason of this is left to the user).

THINGS TO TRY

The user should try, once for all, letting consumers change their reservation prices, with different values of deltaPrice.
It is interesting to vary the number of exchanged stocks as well.

EXTENDING THE MODEL

Other classes may be implemented, such as banks, which can regulate the influence of the financial market and be regulated by the real one.

CODE


;; Global Attributes ;; 
globals [meanProduction meanQuotedProduction nbFlourishing nbDwingling nbStable meanConsumersPrice meanCompaniesPrice meanQuotedCompaniesPrice meanBuyersPrice meanSellersPrice meanSettledPriceTrading meanSettledPriceExchanging price]
turtles-own [settledPrice settledPrice-1]

;; Traders ;;
breed [consumers consumer]
breed [companies company]

consumers-own [maxPrice Q]
companies-own [minPrice Q meanTotalCost quoted publicCost stockPrice stockPrice-1 diffStock logB logS]


;; Shareholders ;;
breed [buyers buyer]
breed [sellers seller]

buyers-own [maxPrice]
sellers-own [minPrice]


to setup
  clear-all
  reset-ticks
  
  ;;Creation of consumers
  create-consumers nb-of-consumers [
    set shape "person"
    set color blue
    setxy random-xcor random-ycor
    set maxPrice random-float bounderMaxPrice
    set Q 1
  ]
  
  ;;Creation of companies
  create-companies nb-of-companies [
    set shape "building store"
    set color green
    set size 3
    setxy random-xcor random-ycor
    set minPrice random-float bounderMinPrice
    set meanTotalCost (minPrice / 2)
    set Q 0
    set quoted false
    set logB []
    set logS []
  ]
  
  ;;Creation of buyers
  create-buyers nb-of-buyers [
    set shape "person business"
    set color brown
    setxy random-xcor random-ycor
  ]
  
  ;;Creation of sellers
  create-sellers nb-of-sellers [
    set shape "person service"
    set color red
    setxy random-xcor random-ycor
  ]
  
  ;;A definite amount of companies is going public
  ask n-of nb-of-quoted-companies companies [
    set quoted true
    set color orange
  ]
end

to go

  recordPrices
  
  correctCompaniesQ
  correctCompaniesReservationPrice
  
  correctConsumersQ
  correctConsumersReservationPrice
  
  move
  
  stockOpening
  
  trading
  exchanging
  
  do-plots
  
  tick
end

;;A method to record the settled prices of the previous bargains, both for tradings and for stock exchanges
to recordPrices
  ;;For consumers
  ask consumers [
    set settledPrice-1 settledPrice
    set settledPrice 0
  ]
  
  ;;For companies: for quoted ones the memorized price cannot be equal to 0
  ask companies [
    if quoted = false [
      set settledPrice-1 settledPrice
      set settledPrice 0
    ]
    if quoted = true [
      if settledPrice != 0 [set settledPrice-1 settledPrice]
      set settledPrice 0
      if stockPrice != 0 [set stockPrice-1 stockPrice]
      set stockPrice 0
    ]
  ]
  
  ;;For buyers
  ask buyers [
    set settledPrice-1 settledPrice
    set settledPrice 0
  ]
  
  ;;For sellers
  ask sellers [
    set settledPrice-1 settledPrice
    set settledPrice 0
  ]
end

;;A method to correct companies' production according to their financial health
to correctCompaniesQ
  ask companies [
    ifelse not quoted 
      [set Q (Q + 2)] ;;Not quoted companies keep producing two units
      [ifelse diffStock > 0
          [set Q (Q + 3)] ;;The company is financially flourishing, hence it can product more
          [ifelse diffStock < 0
              [set Q (Q + 1)] ;;The company is financially dwingling, thus it has to product less
              [set Q (Q + 2)] ;;Financial market has not influenced this company, which keeps producing two units
          ]
      ]
  ]          
end

;;A method to correct companies' reservation price according to their products' stockage
to correctCompaniesReservationPrice
  if tradingPriceCorrection = "companies only" or tradingPriceCorrection = "consumers and companies" [
    ask companies [
      let correction ((deltaPrice * minPrice) / 100)
      if Q > companyDesideredStock [set minPrice (minPrice - (random-float correction))]
      if Q < companyDesideredStock [set minPrice (minPrice + (random-float correction))]
      if minPrice < meanTotalCost [set minPrice meanTotalCost]
    ]
  ]
end

;;A method to correct consumer's level of stockage of products, i.e. consuming one unit
to correctConsumersQ
  ask consumers [
    ifelse Q > 0
      [set Q (Q - 1)]
      [set Q 0]
  ]
end

;;A method to correct consumers' reservation price according to their product's stockage
to correctConsumersReservationPrice
  if tradingPriceCorrection = "consumers only" or tradingPriceCorrection = "consumers and companies" [
    ask consumers [
      let correction ((deltaPrice * maxPrice) / 100)
      if Q > consumerDesideredStock [set maxPrice (maxPrice - (random-float correction))]
      if Q < consumerDesideredStock [set maxPrice (maxPrice + (random-float correction))]
      if maxPrice < 0 [set maxPrice random-float correction]
    ]
  ]
end

;;A method to make consumers move
to move
  ask consumers [
    forward random 10
    right random 10
  ]
end

;;A method that erases the logs of stocks each day at the stock exchange opening
to stockOpening
  ask companies [
    set logB []
    set logS []
    ]
end

;;A method that implements the bargain between traders
to trading
  ask consumers [
    let numberOfTrials 0
    while [numberOfTrials < consumerTrials and count companies-here with [Q > 0] > 0] [
      let aCompany one-of companies-here with [Q > 0]
      let diff maxPrice - [minPrice] of aCompany
      
      ;;The bargain takes place
      if diff >= 0 [
        if tradePrice = "random price settlement" [set price maxPrice - random-float diff]
        if tradePrice = "closeness to" [set price [minPrice] of aCompany + tradeClosenessTo * diff]
        set settledPrice price
        set Q (Q + 1)
        ask aCompany [
          set Q (Q - 1)
          set settledPrice price
          if quoted = true [set publicCost ((settledPrice + settledPrice-1) / 2)]
        ]
      ]
      set numberOfTrials (numberOfTrials + 1)
    ]
  ]
end

;;A method to implement the bargain between shareholders, using logs
to exchanging
  if (any? companies with [publicCost != 0]) [
    buyersInLog
    sellersInLog
    
    ask companies with [quoted = true] [
      if (not empty? logB and not empty? logS) and (item 0 (item 0 logB) >= item 0 (item 0 logS)) [ 
        ifelse (random-float 1.0 < 0.5)
          [set price item 0 (item 0 logS)]
          [set price item 0 (item 0 logB)]
        
        set stockPrice price
        set diffStock (stockPrice - stockPrice-1)
        
        ask buyer item 1 (item 0 logB) [
          set settledPrice price
        ]
        ask seller item 1 (item 0 logS) [
          set settledPrice price
        ]
        
        set logB but-first logB
        set logS but-first logS        
      ]
    ]
  ]
end

;;Prices of buyers are registered in the log dedicated to the specific quoted company
to buyersInLog
  ask buyers [
    let placement 0
    
    while [placement < nb-stocks-to-buy] [
      let aQuotedCompany one-of companies with [quoted = true]
      while [[publicCost] of aQuotedCompany = 0] [set aQuotedCompany one-of companies with [quoted = true]]
      
      let bounder ((privateInformationPercentage * [publicCost] of aQuotedCompany) / 100)
      let privateBuyerInformation (2 * bounder * (random-float 1.0) - bounder)
      
      ifelse ([publicCost] of aQuotedCompany + privateBuyerInformation) > 0
        [set maxPrice ([publicCost] of aQuotedCompany + privateBuyerInformation)]
        [set maxPrice ([publicCost] of aQuotedCompany)]
      
      ;;Registration of price and buyers
      let tmp[]
      set tmp lput maxPrice tmp
      set tmp lput who tmp
      ask aQuotedCompany [
        set logB lput tmp logB
        set logB reverse sort-by [item 0 ?1 < item 0 ?2] logB
      ]
      set placement (placement + 1)
    ]
  ]
end

;;Prices of sellers are registered in the log dedicated to the specific quoted company
to sellersInLog
  ask sellers [
    let placement 0
    
    while [placement < nb-stocks-to-sell] [
      let aQuotedCompany one-of companies with [quoted = true]
      while [[publicCost] of aQuotedCompany = 0] [set aQuotedCompany one-of companies with [quoted = true]]
      
      let bounder ((privateInformationPercentage * [publicCost] of aQuotedCompany) / 100)
      let privateSellerInformation (2 * bounder * (random-float 1.0) - bounder)
      
      ifelse ([publicCost] of aQuotedCompany + privateSellerInformation) > 0
        [set minPrice ([publicCost] of aQuotedCompany + privateSellerInformation)]
        [set minPrice ([publicCost] of aQuotedCompany)]
        
      ;;Registration of price and seller
      let tmp[]
      set tmp lput minPrice tmp
      set tmp lput who tmp
      ask aQuotedCompany [
        set logS lput tmp logS
        set logS  sort-by [item 0 ?1 < item 0 ?2] logS
      ]
      set placement (placement + 1)
    ]
  ]
end



;;A method to compute the plotted average of prices for both the prices of goods and stocks
to do-plots
  set nbFlourishing (count companies with [diffStock > 0] * 100) / (count companies)
  set nbDwingling (count companies with [diffStock < 0] * 100) / (count companies)
  set nbStable (count companies with [diffStock = 0] * 100) / (count companies)
  
  ifelse count consumers with [settledPrice != 0] > 0
    [set meanConsumersPrice mean [maxPrice] of consumers with [settledPrice != 0]
     set meanSettledPriceTrading mean [settledPrice] of consumers with [settledPrice != 0]]
    [set meanConsumersPrice 0
     set meanSettledPriceTrading 0]
    
  ifelse count companies with [settledPrice != 0 and quoted = false] > 0
    [set meanCompaniesPrice mean [minPrice] of companies with [settledPrice != 0 and quoted = false]]
    [set meanCompaniesPrice 0]
    
  ifelse count companies with [settledPrice != 0 and quoted = true] > 0
    [set meanQuotedCompaniesPrice mean [minPrice] of companies with [settledPrice != 0 and quoted = true]]
    [set meanQuotedCompaniesPrice 0]
    
  ifelse count buyers with [settledPrice != 0] > 0
    [set meanBuyersPrice mean [maxPrice] of buyers with [settledPrice != 0]
     set meanSettledPriceExchanging mean [settledPrice] of buyers with [settledPrice != 0]]
    [set meanBuyersPrice 0
     set meanSettledPriceExchanging 0]
    
  ifelse count sellers with [settledPrice != 0] > 0
    [set meanSellersPrice mean [minPrice] of sellers with [settledPrice != 0]]
    [set meanSellersPrice 0]
end