The model related to the thesis of Marco Bosco

 

The applet requires Java 5 or higher. Java must be enabled in your browser settings. Mac users must have Mac OS X 10.4 or higher. Windows and Linux users may obtain the latest Java from Oracle's Java site.


powered by NetLogo

view/download model file: agent_market_model.nlogo

WHAT IS IT?

The model describes a contemporary economic system composed by agents that are characterized by a precise economic condition and that interact on three types of market: the job market, the goods market and the financial market. The agents are divided into four different categories: Families, who hold a capital, provide work in exchange for wage, save and consume, Firms, that provide a wage for Families, produce goods and invest, Banks, which collecting deposits and making loans, participate in the circuit of money creation and a Central Bank which has the status of last resort lender.

HOW IT WORKS

The Initialization consists of three successive steps:

Defined the Initialization, each time interval (corresponding to a day) consists of six steps that determine the evolution of the system:

HOW TO USE IT

Parameters that have to be choosen:

THINGS TO TRY

Run the model considering a Central Bank adopting “Risk Aversion” and after, with the same setup, run the model considering a Central Bank adopting “Cooperation”. This procedure allows to identify the differences that appear in the dynamics of the system for the two strategies adoptable by the Central Bank.

EXTENDING THE MODEL

An interesting modification of the model is to consider a heterogeneous set of Banks that have a relative size with an exponential dependence of their cardinality: the fraction of Families that at the initialization deposits their part of FamiliesTotalAssets in the deposits of the b-th Bank is exponentially proportional to b.

CREDITS AND REFERENCES

This model is based on the innovative paper Raberto M, Rapallo F, Scalas E (2011), Semi-Markov Graph Dynamics. PLoS ONE 6(8): e23370. doi:10.1371/journal.pone.0023370
http://www.plosone.org/article/info%3Adoi%2F10.1371%2Fjournal.pone.0023370

From this paper started the master thesis of Marco Maria Bosco, 2013, “Simulazioni di Mercati del Credito: Il Modello di Mercato ad Agenti”. See http://web.econ.unito.it/terna/tesi/bosco.pdf to view the complete text of the master’s thesis where you’ll find a full description of the code, a comprehensive study of the dynamics of the model and its possible applications to a real economic system.

CODE

breed [Banks Bank]
breed [Firms Firm]
breed [CentralBanks CentralBank]

directed-link-breed [LoansBF LoanBF]
directed-link-breed [LoansBB LoanBB]
directed-link-breed [LoansCBB LoanCBB]

CentralBanks-own [ReservesCB LoansCB BanknotesInCirculation]
Banks-own [CashB LoansB DepositsB DebtsB]

LoansBF-own [Age AmountBF]
LoansBB-own [Age AmountBB]
LoansCBB-own [Age AmountCBB]

globals [ListOmega]

to setup
  clear-all
  reset-ticks
  ask patches [set pcolor 8]
  
  create-CentralBanks 1
   [set color Yellow
    set shape "building institution"
    set size 3
    setxy 25 0
   ]
     
  create-Banks BanksInitialNumber
   [set color Red
    set shape "building institution"
    set size 1.5      
   ]
  layout-circle Banks 15
     
  create-Firms FirmsNumber
   [set color Blue
    set shape "factory"
    set size 1.5
   ]
  layout-circle Firms 24.5


  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  ;;;;    INITIALIZATION    ;;;;
  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

     
  ask CentralBanks 
   [set ReservesCB 0
    set LoansCB 0
    set BanknotesInCirculation 0
   ]

  ask Banks 
   [set LoansB 0
    set DebtsB 0
    set ListOmega []
    repeat BanksInitialNumber [set ListOmega lput random-float 1 ListOmega]
    let Tot (sum ListOmega)
    let j 0
    while [j < BanksInitialNumber]                                                        
     [set ListOmega replace-item j ListOmega (item j ListOmega / Tot)                      
      set j (j + 1)                                                                             
     ]
    let i 1
    while [i <= BanksInitialNumber]
     [ask (Bank i) 
       [set CashB ((MinInitialLiquidity + random (MaxInitialLiquidity - MinInitialLiquidity)) + ((item (i - 1) ListOmega) * FamiliesTotalAssets))
        set DepositsB (((item (i - 1) ListOmega) * FamiliesTotalAssets) * ((1 + rD) ^ (1 / 360)))
       ]
      set i (i + 1) 
     ]
   ]
    
end

to go  
  ask LoansBF [set Age Age + 1]
  ask LoansBB [set Age Age + 1]
  ask LoansCBB [set Age Age + 1]
  
  let BanksNumber (count Banks with [color = Red])
  if (BanksNumber = 0) [ stop ]
  
  ask Firms [set color Blue]
  
   
  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  ;;; LOAN REQUESTS FROM FIRMS & PAYMENT IN BANK DEPOSITS ;;;
  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  
  
  let NoR (min (list (random-poisson Ave#LoansRequests) FirmsNumber))          ; NoR: Number of Requests
  ask n-of NoR Firms       
   [let MyBank one-of Banks with [color = Red]    
    ifelse (Mybank != Nobody) 
     [ifelse (([CashB] of MyBank) - Loan) >= MinCashForLoan 
       [create-LoanBF-from MyBank          ; Creation of link Bank -> Firm 
         [set color Green
          set Age 0 
          set AmountBF Loan          ; Loan requested by the Firm: Loan 
          ask end1 
           [set CashB (CashB - ([AmountBF] of myself))
            set LoansB (LoansB + (([AmountBF] of myself) * ((1 + rC) ^ (RepaymentPeriod / 360))))
           ]
          set ListOmega []          ; Payment in Bank deposits
          repeat BanksNumber [set ListOmega lput random-float 1 ListOmega]
          let Tot (sum ListOmega)
          let j 0
          while [j < BanksNumber]
           [set ListOmega replace-item j ListOmega (item j ListOmega / Tot)
            set j (j + 1)
           ] 
          let i 1
          let k 0
          while [i <= BanksInitialNumber and k < BanksNumber]
           [ask (Bank i) 
             [if (([color] of Bank i) = Red) 
               [set CashB (CashB + ((item k ListOmega) * ([AmountBF] of myself)))
                set DepositsB (DepositsB + ((item k ListOmega) * ([AmountBF] of myself)))
                set k (k + 1)
               ]
             ]
            set i (i + 1) 
           ]     
         ]          
       ]
       [ask MyBank 
         [let OtherBank one-of other Banks with [color = Red]
          ifelse ((OtherBank != Nobody) and (([CashB] of OtherBank) + ([CashB] of MyBank) - Loan) >= (2 * MinCashForLoan)) 
           [create-LoanBB-from OtherBank          ; Creation of link Bank -> Bank 
             [set color Pink   
              set Age 0 
              set AmountBB (MinCashForLoan - ([CashB] of MyBank) + Loan)          ; Loan requested by MyBank: MinCashForLoan - ([CashB] of MyBank) + Loan
              ask end1 
               [set CashB (CashB - ([AmountBB] of myself))
                set LoansB (LoansB + (([AmountBB] of myself) * ((1 + rB) ^ (RepaymentPeriod / 360))))
               ]
              ask end2 
               [set CashB (MinCashForLoan + Loan)
                set DebtsB (DebtsB + (([AmountBB] of myself) * ((1 + rB) ^ (RepaymentPeriod / 360)))) 
               ]
             ]
            
            create-LoanBF-to myself          ; Creation of link Bank -> Firm 
             [set color Green
              set Age 0 
              set AmountBF Loan          ; Loan requested by the Firm: Loan 
              ask end1 
               [set CashB MinCashForLoan
                set LoansB (LoansB + (([AmountBF] of myself) * ((1 + rC) ^ (RepaymentPeriod / 360))))
               ]
              set ListOmega []          ; Payment in Bank deposits
              repeat BanksNumber [set ListOmega lput random-float 1 ListOmega]
              let Tot (sum ListOmega)
              let j 0
              while [j < BanksNumber]
               [set ListOmega replace-item j ListOmega (item j ListOmega / Tot)
                set j (j + 1)
               ]
              let i 1
              let k 0
              while [i <= BanksInitialNumber and k < BanksNumber]
               [ask (Bank i) 
                 [if (([color] of Bank i) = Red) 
                   [set CashB (CashB + ((item k ListOmega) * ([AmountBF] of myself)))
                    set DepositsB (DepositsB + ((item k ListOmega) * ([AmountBF] of myself)))
                    set k (k + 1)
                   ]
                 ]
                set i (i + 1) 
               ]      
             ]
           ]
           [ifelse CBConservativeChoice
             [ifelse (([CashB] of MyBank) >= (0.2 * ([DepositsB] of MyBank)) and ((([BanknotesInCirculation] of CentralBank 0) + (MinCashForLoan - ([CashB] of MyBank) + Loan)) <= MaxBanknotesInCirculation))           ; CBConservativeChoice ON 
               [create-LoanCBB-from CentralBank 0          ; Creation of link Central Bank -> Bank  
                 [set color Blue
                  set Age 0
                  set AmountCBB (MinCashForLoan - ([CashB] of MyBank) + Loan)           ; Loan requested by MyBank: MinCashForLoan - ([CashB] of MyBank) + Loan 
                  ask end1 
                   [set LoansCB (LoansCB + (([AmountCBB] of myself) * ((1 + rCB) ^ (RepaymentPeriod / 360))))
                    set BanknotesInCirculation (BanknotesInCirculation + ([AmountCBB] of myself))
                   ]
                  ask end2 
                   [set CashB (MinCashForLoan + Loan)
                    set DebtsB (DebtsB + (([AmountCBB] of myself) * ((1 + rCB) ^ (RepaymentPeriod / 360))))
                   ]  
                 ]
                
                create-LoanBF-to myself          ; Creation of link Bank -> Firm 
                 [set color Green
                  set Age 0 
                  set AmountBF Loan          ; Loan requested by the Firm: Loan
                  ask end1 
                   [set CashB MinCashForLoan
                    set LoansB (LoansB + (([AmountBF] of myself) * ((1 + rC) ^ (RepaymentPeriod / 360))))
                   ]
                  set ListOmega []          ; Payment in Bank deposits
                  repeat BanksNumber [set ListOmega lput random-float 1 ListOmega]
                  let Tot (sum ListOmega)
                  let j 0
                  while [j < BanksNumber]
                   [set ListOmega replace-item j ListOmega (item j ListOmega / Tot)
                    set j (j + 1)
                   ]
                  let i 1
                  let k 0
                  while [i <= BanksInitialNumber and k < BanksNumber]
                   [ask (Bank i) 
                     [if (([color] of Bank i) = Red) 
                       [set CashB (CashB + ((item k ListOmega) * ([AmountBF] of myself)))
                        set DepositsB (DepositsB + ((item k ListOmega) * ([AmountBF] of myself)))
                        set k (k + 1)
                       ]
                     ]
                    set i (i + 1) 
                   ]     
                 ]
               ]
               [ask myself [set color Yellow]         ; The Firm doesn't get the required loan
               ]
             ]
             [ifelse (([CashB] of MyBank) < (0.2 * ([DepositsB] of MyBank)))           ; CBConservativeChoice OFF 
               [create-LoanCBB-from CentralBank 0          ; Creation of link Central Bank -> Bank  
                 [set color Blue
                  set Age 0
                  set AmountCBB (MinCashForLoan - ([CashB] of MyBank) + Loan)           ; Loan requested by MyBank: MinCashForLoan - ([CashB] of MyBank) + Loan 
                  ask end1 
                   [set LoansCB (LoansCB + (([AmountCBB] of myself) * ((1 + rCB) ^ (RepaymentPeriod / 360))))
                    set BanknotesInCirculation (BanknotesInCirculation + ([AmountCBB] of myself))
                   ]
                  ask end2 
                   [set CashB (MinCashForLoan + Loan)
                    set DebtsB (DebtsB + (([AmountCBB] of myself) * ((1 + rCB) ^ (RepaymentPeriod / 360))))
                   ]  
                 ]
                
                create-LoanBF-to myself          ; Creation of link Bank -> Firm 
                 [set color Green
                  set Age 0 
                  set AmountBF Loan          ; Loan requested by the Firm: Loan
                  ask end1 
                   [set CashB MinCashForLoan
                    set LoansB (LoansB + (([AmountBF] of myself) * ((1 + rC) ^ (RepaymentPeriod / 360))))
                   ]
                  set ListOmega []          ; Payment in Bank deposits
                  repeat BanksNumber [set ListOmega lput random-float 1 ListOmega]
                  let Tot (sum ListOmega)
                  let j 0
                  while [j < BanksNumber]
                   [set ListOmega replace-item j ListOmega (item j ListOmega / Tot)
                    set j (j + 1)
                   ]
                  let i 1
                  let k 0
                  while [i <= BanksInitialNumber and k < BanksNumber]
                   [ask (Bank i) 
                     [if (([color] of Bank i) = Red) 
                       [set CashB (CashB + ((item k ListOmega) * ([AmountBF] of myself)))
                        set DepositsB (DepositsB + ((item k ListOmega) * ([AmountBF] of myself)))
                        set k (k + 1)
                       ]
                     ]
                    set i (i + 1) 
                   ]     
                 ]
               ]
               [ask myself [set color Yellow]         ; The Firm doesn't get the required loan
               ]
             ]
           ]     
         ]                            
       ]                                                                   
     ]
     [ask myself [set color Yellow]         ; The Firm doesn't get the required loan
     ]
   ]


  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  ;;;;    REPAYMENT OF LOANS    ;;;;
  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

  
  ask LoansBF with [Age = RepaymentPeriod]          ; Repayment of Bank - Firm loans 
   [ask end1 
     [set CashB (CashB + (([AmountBF] of myself) * ((1 + rC) ^ (RepaymentPeriod / 360))))
      set LoansB (LoansB - (([AmountBF] of myself) * ((1 + rC) ^ (RepaymentPeriod / 360))))
     ] 
    die
   ]
  
  ask LoansBB with [Age = RepaymentPeriod]          ; Repayment of Bank - Bank loans  
   [ifelse (([CashB] of end2) >= (AmountBB * ((1 + rB) ^ (RepaymentPeriod / 360))))
     [ask end1 
       [set CashB (CashB + (([AmountBB] of myself) * ((1 + rB) ^ (RepaymentPeriod / 360))))
        set LoansB (LoansB - (([AmountBB] of myself) * ((1 + rB) ^ (RepaymentPeriod / 360))))
       ]
      ask end2 
       [set CashB (CashB - (([AmountBB] of myself) * ((1 + rB) ^ (RepaymentPeriod / 360))))
        set DebtsB (DebtsB - (([AmountBB] of myself) * ((1 + rB) ^ (RepaymentPeriod / 360))))
       ]
      die
     ]
     [set Age (RepaymentPeriod - 1) 
     ]
   ]
                                                                                                                                         
  ask LoansCBB with [Age = RepaymentPeriod]          ; Repayment of Central Bank - Bank loans  
   [ifelse (([CashB] of end2) >= (AmountCBB * ((1 + rCB) ^ (RepaymentPeriod / 360))))
     [ask end1 
       [set ReservesCB (ReservesCB + (([AmountCBB] of myself) * ((1 + rCB) ^ (RepaymentPeriod / 360))))
        set LoansCB (LoansCB - (([AmountCBB] of myself) * ((1 + rCB) ^ (RepaymentPeriod / 360))))
       ]
      ask end2 
       [set CashB (CashB - (([AmountCBB] of myself) * ((1 + rCB) ^ (RepaymentPeriod / 360))))
        set DebtsB (DebtsB - (([AmountCBB] of myself) * ((1 + rCB) ^ (RepaymentPeriod / 360))))
       ]
      die
     ]
     [set Age (RepaymentPeriod - 1) 
     ]
   ]
  
  
  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  ;;;;    WITHDRAWAL FROM DEPOSITS    ;;;;
  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 
 
  let NoW (min (list (random-poisson Ave#Withdrawal) BanksNumber))          ; NoW: Number of Withdrawals
  ask n-of NoW (Banks with [color = Red]) 
   [ifelse (CashB >= DepositsB) 
     [ifelse (Withdrawal <= DepositsB)
       [set CashB (CashB - Withdrawal)          ; CashB >= DepositsB >= Withdrawal
        set DepositsB (DepositsB - Withdrawal)
       ]
       [set CashB (CashB - DepositsB)          ; CashB >= Withdrawal >= DepositsB   or   Withdrawal >= CashB >= DepositsB
        set DepositsB 0
       ]
     ]
     [ifelse (Withdrawal <= CashB)
       [set CashB (CashB - Withdrawal)          ; DepositsB >= CashB >= Withdrawal
        set DepositsB (DepositsB - Withdrawal)
       ]                                                   
       [ifelse (Withdrawal <= DepositsB)   
         [let OtherBank one-of other Banks with [color = Red]          ; DepositsB >= Withdrawal >= CashB 
          ifelse ((OtherBank != Nobody) and ((([CashB] of OtherBank) - (Withdrawal - CashB)) >= MinCashForLoan)) 
           [create-LoanBB-from OtherBank          ; Creazione link Banca -> Banca 
             [set color Pink
              set Age 0 
              set AmountBB (Withdrawal - ([CashB] of myself))           ; Loan requested by the Bank: Withdrawal - ([CashB] of myself)  
              ask end1 
               [set CashB (CashB - ([AmountBB] of myself))
                set LoansB (LoansB + (([AmountBB] of myself) * ((1 + rB) ^ (RepaymentPeriod / 360))))
               ]
              ask end2 
               [set CashB 0
                set DepositsB (DepositsB - Withdrawal)
                set DebtsB (DebtsB + (([AmountBB] of myself) * ((1 + rB) ^ (RepaymentPeriod / 360)))) 
               ]
             ]
           ]
           [ifelse CBConservativeChoice
             [ifelse ((CashB >= (0.2 * DepositsB)) and ((([BanknotesInCirculation] of CentralBank 0) + (Withdrawal - CashB)) <= MaxBanknotesInCirculation))           ; CBConservativeChoice ON 
               [create-LoanCBB-from CentralBank 0          ; Creation of link Central Bank -> Bank  
                 [set color Blue
                  set Age 0
                  set AmountCBB (Withdrawal - ([CashB] of myself))           ; Loan requested by the Bank: Withdrawal - ([CashB] of myself)   
                  ask end1 
                   [set LoansCB (LoansCB + (([AmountCBB] of myself) * ((1 + rCB) ^ (RepaymentPeriod / 360))))
                    set BanknotesInCirculation (BanknotesInCirculation + ([AmountCBB] of myself))
                   ]
                  ask end2 
                   [set CashB 0
                    set DepositsB (DepositsB - Withdrawal)
                    set DebtsB (DebtsB + (([AmountCBB] of myself) * ((1 + rCB) ^ (RepaymentPeriod / 360))))
                   ]  
                 ]
               ]
               [set DepositsB (DepositsB - CashB)          ; The loan is not granted, Bank's default
                set CashB 0
                set color Black 
               ]
             ]
             [ifelse (CashB < (0.2 * DepositsB))          ; CBConservativeChoice OFF 
               [create-LoanCBB-from CentralBank 0          ; Creation of link Central Bank -> Bank  
                 [set color Blue
                  set Age 0
                  set AmountCBB (Withdrawal - ([CashB] of myself))           ; Loan requested by the Bank: Withdrawal - ([CashB] of myself)   
                  ask end1 
                   [set LoansCB (LoansCB + (([AmountCBB] of myself) * ((1 + rCB) ^ (RepaymentPeriod / 360))))
                    set BanknotesInCirculation (BanknotesInCirculation + ([AmountCBB] of myself))
                   ]
                  ask end2 
                   [set CashB 0
                    set DepositsB (DepositsB - Withdrawal)
                    set DebtsB (DebtsB + (([AmountCBB] of myself) * ((1 + rCB) ^ (RepaymentPeriod / 360))))
                   ]  
                 ]
               ]
               [set DepositsB (DepositsB - CashB)          ; The loan is not granted, Bank's default
                set CashB 0
                set color Black 
               ]
             ]
           ]
         ]
         [let OtherBank one-of other Banks with [color = Red]           ; Withdrawal >= DepositsB >= CashB     
          ifelse ((OtherBank != Nobody) and ((([CashB] of OtherBank) - (DepositsB - CashB)) >= MinCashForLoan)) 
           [create-LoanBB-from OtherBank          ; Creation of link Bank -> Bank 
             [set color Pink
              set Age 0 
              set AmountBB (([DepositsB] of myself) - ([CashB] of myself))           ; Loan requested by the Bank: ([DepositsB] of myself) - ([CashB] of myself)    
              ask end1 
               [set CashB (CashB - ([AmountBB] of myself))
                set LoansB (LoansB + (([AmountBB] of myself) * ((1 + rB) ^ (RepaymentPeriod / 360))))
               ]
              ask end2 
               [set CashB 0
                set DepositsB 0
                set DebtsB (DebtsB + (([AmountBB] of myself) * ((1 + rB) ^ (RepaymentPeriod / 360)))) 
               ]
             ]
           ]
           [ifelse CBConservativeChoice                                          
             [ifelse ((CashB >= (0.2 * DepositsB)) and ((([BanknotesInCirculation] of CentralBank 0) + (DepositsB - CashB)) <= MaxBanknotesInCirculation))          ; CBConservativeChoice ON 
               [create-LoanCBB-from CentralBank 0          ; Creation of link Central Bank -> Bank  
                 [set color Blue
                  set Age 0
                  set AmountCBB (([DepositsB] of myself) - ([CashB] of myself))           ; Loan requested by the Bank: ([DepositsB] of myself) - ([CashB] of myself)      
                  ask end1 
                   [set LoansCB (LoansCB + (([AmountCBB] of myself) * ((1 + rCB) ^ (RepaymentPeriod / 360))))
                    set BanknotesInCirculation (BanknotesInCirculation + ([AmountCBB] of myself))
                   ]
                  ask end2 
                   [set CashB 0
                    set DepositsB 0
                    set DebtsB (DebtsB + (([AmountCBB] of myself) * ((1 + rCB) ^ (RepaymentPeriod / 360))))
                   ]  
                 ]
               ]
               [set DepositsB (DepositsB - CashB)          ; The loan is not granted, Bank's default
                set CashB 0
                set color Black 
               ]
             ]
             [ifelse (CashB < (0.2 * DepositsB))          ; CBConservativeChoice OFF 
               [create-LoanCBB-from CentralBank 0          ; Creation of link Central Bank -> Bank  
                 [set color Blue
                  set Age 0
                  set AmountCBB (([DepositsB] of myself) - ([CashB] of myself))           ; Loan requested by the Bank: ([DepositsB] of myself) - ([CashB] of myself)      
                  ask end1 
                   [set LoansCB (LoansCB + (([AmountCBB] of myself) * ((1 + rCB) ^ (RepaymentPeriod / 360))))
                    set BanknotesInCirculation (BanknotesInCirculation + ([AmountCBB] of myself))
                   ]
                  ask end2 
                   [set CashB 0
                    set DepositsB 0
                    set DebtsB (DebtsB + (([AmountCBB] of myself) * ((1 + rCB) ^ (RepaymentPeriod / 360))))
                   ]  
                 ]
               ]
               [set DepositsB (DepositsB - CashB)          ; The loan is not granted, Bank's default
                set CashB 0
                set color Black 
               ]
             ]
           ]
         ] 
       ]
     ]
   ]
 
 
  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  ;;;;    WITHDRAWAL FROM DEPOSITS 2    ;;;;  
  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
            
 
  let NoFwL (count (Firms with [color = Yellow]))          ; NoFwL: Number of Firms without the requested Loan
  let WithdrawalAmount (NoFwL * Loan)          ; WithdrawalAmount: Amount of money that has not been obtained from the Firms
 
  ask Banks 
   [set ListOmega []
    repeat BanksNumber [set ListOmega lput random-float 1 ListOmega]
    let Tot (sum ListOmega)
    let j 0
    while [j < BanksNumber]                                                        
     [set ListOmega replace-item j ListOmega (item j ListOmega / Tot)                      
      set j (j + 1)                                                                             
     ]
    let i 1
    let k 0
    while [i <= BanksInitialNumber and k < BanksNumber]
     [ask (Bank i)     
      [if (([color] of Bank i) = Red) 
        [ifelse (CashB >= DepositsB) 
          [ifelse (((item k ListOmega) * WithdrawalAmount) <= DepositsB)
            [set CashB (CashB - ((item k ListOmega) * WithdrawalAmount))          ; CashB >= DepositsB >= ((item k ListOmega) * WithdrawalAmount)
             set DepositsB (DepositsB - ((item k ListOmega) * WithdrawalAmount))
            ]
            [set CashB (CashB - DepositsB)          ; CashB >= ((item k ListOmega) * WithdrawalAmount) >= DepositsB   or   ((item k ListOmega) * WithdrawalAmount) >= CashB >= DepositsB
             set DepositsB 0
            ]
          ]
          [ifelse (((item k ListOmega) * WithdrawalAmount) <= CashB)
            [set CashB (CashB - ((item k ListOmega) * WithdrawalAmount))          ; DepositsB >= CashB >= ((item k ListOmega) * WithdrawalAmount)
             set DepositsB (DepositsB - ((item k ListOmega) * WithdrawalAmount))
            ]                                                   
            [ifelse (((item k ListOmega) * WithdrawalAmount) <= DepositsB)   
              [let OtherBank one-of other Banks with [color = Red]          ; DepositsB >= ((item k ListOmega) * WithdrawalAmount) >= CashB 
               ifelse ((OtherBank != Nobody) and ((([CashB] of OtherBank) - (((item k ListOmega) * WithdrawalAmount) - CashB)) >= MinCashForLoan)) 
                [create-LoanBB-from OtherBank          ; Creation of link Bank -> Bank 
                  [set color Pink
                   set Age 0 
                   set AmountBB (((item k ListOmega) * WithdrawalAmount) - ([CashB] of myself))           ; Loan requested by the Bank: ((item k ListOmega) * WithdrawalAmount) - ([CashB] of myself)  
                   ask end1 
                    [set CashB (CashB - ([AmountBB] of myself))
                     set LoansB (LoansB + (([AmountBB] of myself) * ((1 + rB) ^ (RepaymentPeriod / 360))))
                    ]
                   ask end2 
                    [set CashB 0
                     set DepositsB (DepositsB - ((item k ListOmega) * WithdrawalAmount))
                     set DebtsB (DebtsB + (([AmountBB] of myself) * ((1 + rB) ^ (RepaymentPeriod / 360)))) 
                    ]
                  ]
                ]
                [ifelse CBConservativeChoice
                  [ifelse ((CashB >= (0.2 * DepositsB)) and ((([BanknotesInCirculation] of CentralBank 0) + (((item k ListOmega) * WithdrawalAmount) - CashB)) <= MaxBanknotesInCirculation))          ; CBConservativeChoice ON
                    [create-LoanCBB-from CentralBank 0          ; Creation of link Central Bank -> Bank  
                      [set color Blue
                       set Age 0
                       set AmountCBB (((item k ListOmega) * WithdrawalAmount) - ([CashB] of myself))           ; Loan requested by the Bank: ((item k ListOmega) * WithdrawalAmount) - ([CashB] of myself)   
                       ask end1 
                        [set LoansCB (LoansCB + (([AmountCBB] of myself) * ((1 + rCB) ^ (RepaymentPeriod / 360))))
                         set BanknotesInCirculation (BanknotesInCirculation + ([AmountCBB] of myself))
                        ]
                       ask end2 
                        [set CashB 0
                         set DepositsB (DepositsB - ((item k ListOmega) * WithdrawalAmount))
                         set DebtsB (DebtsB + (([AmountCBB] of myself) * ((1 + rCB) ^ (RepaymentPeriod / 360))))
                        ]  
                      ]
                    ]
                    [set DepositsB (DepositsB - CashB)          ; The loan is not granted, Bank's default
                     set CashB 0
                     set color Black 
                    ]
                  ]
                  [ifelse (CashB < (0.2 * DepositsB))          ; CBConservativeChoice OFF
                    [create-LoanCBB-from CentralBank 0          ; Creation of link Central Bank -> Bank  
                      [set color Blue
                       set Age 0
                       set AmountCBB (((item k ListOmega) * WithdrawalAmount) - ([CashB] of myself))           ; Loan requested by the Bank: ((item k ListOmega) * WithdrawalAmount) - ([CashB] of myself)   
                       ask end1 
                        [set LoansCB (LoansCB + (([AmountCBB] of myself) * ((1 + rCB) ^ (RepaymentPeriod / 360))))
                         set BanknotesInCirculation (BanknotesInCirculation + ([AmountCBB] of myself))
                        ]
                       ask end2 
                        [set CashB 0
                         set DepositsB (DepositsB - ((item k ListOmega) * WithdrawalAmount))
                         set DebtsB (DebtsB + (([AmountCBB] of myself) * ((1 + rCB) ^ (RepaymentPeriod / 360))))
                        ]  
                      ]
                    ]
                    [set DepositsB (DepositsB - CashB)          ; The loan is not granted, Bank's default
                     set CashB 0
                     set color Black 
                    ]
                  ]
                ]
              ]
              [let OtherBank one-of other Banks with [color = Red]           ; ((item k ListOmega) * WithdrawalAmount) >= DepositsB >= CashB     
               ifelse ((OtherBank != Nobody) and ((([CashB] of OtherBank) - (DepositsB - CashB)) >= MinCashForLoan)) 
                [create-LoanBB-from OtherBank          ; Creation of link Bank -> Bank 
                  [set color Pink
                   set Age 0 
                   set AmountBB (([DepositsB] of myself) - ([CashB] of myself))           ; Loan requested by the Bank: ([DepositsB] of myself) - ([CashB] of myself)    
                   ask end1 
                    [set CashB (CashB - ([AmountBB] of myself))
                     set LoansB (LoansB + (([AmountBB] of myself) * ((1 + rB) ^ (RepaymentPeriod / 360))))
                    ]
                   ask end2 
                    [set CashB 0
                     set DepositsB 0
                     set DebtsB (DebtsB + (([AmountBB] of myself) * ((1 + rB) ^ (RepaymentPeriod / 360)))) 
                    ]
                  ]
                ]
                [ifelse CBConservativeChoice
                  [ifelse ((CashB >= (0.2 * DepositsB)) and ((([BanknotesInCirculation] of CentralBank 0) + (DepositsB - CashB)) <= MaxBanknotesInCirculation))          ; CBConservativeChoice ON 
                    [create-LoanCBB-from CentralBank 0          ; Creation of link Central Bank -> Bank  
                      [set color Blue
                       set Age 0
                       set AmountCBB (([DepositsB] of myself) - ([CashB] of myself))           ; Loan requested by the Bank: ([DepositsB] of myself) - ([CashB] of myself)      
                       ask end1 
                        [set LoansCB (LoansCB + (([AmountCBB] of myself) * ((1 + rCB) ^ (RepaymentPeriod / 360))))
                         set BanknotesInCirculation (BanknotesInCirculation + ([AmountCBB] of myself))
                        ]
                       ask end2 
                        [set CashB 0
                         set DepositsB 0
                         set DebtsB (DebtsB + (([AmountCBB] of myself) * ((1 + rCB) ^ (RepaymentPeriod / 360))))
                        ]  
                      ]
                    ]
                    [set DepositsB (DepositsB - CashB)          ; The loan is not granted, Bank's default
                     set CashB 0
                     set color Black 
                    ]
                  ]
                  [ifelse (CashB < (0.2 * DepositsB))          ; CBConservativeChoice OFF 
                    [create-LoanCBB-from CentralBank 0          ; Creation of link Central Bank -> Bank  
                      [set color Blue
                       set Age 0
                       set AmountCBB (([DepositsB] of myself) - ([CashB] of myself))           ; Loan requested by the Bank: ([DepositsB] of myself) - ([CashB] of myself)      
                       ask end1 
                        [set LoansCB (LoansCB + (([AmountCBB] of myself) * ((1 + rCB) ^ (RepaymentPeriod / 360))))
                         set BanknotesInCirculation (BanknotesInCirculation + ([AmountCBB] of myself))
                        ]
                       ask end2 
                        [set CashB 0
                         set DepositsB 0
                         set DebtsB (DebtsB + (([AmountCBB] of myself) * ((1 + rCB) ^ (RepaymentPeriod / 360))))
                        ]  
                      ]
                    ]
                    [set DepositsB (DepositsB - CashB)          ; The loan is not granted, Bank's default
                     set CashB 0
                     set color Black 
                    ]
                  ]
                ]
              ] 
            ]
          ]
         set k (k + 1)
        ]
      ]
      set i (i + 1) 
     ]
   ]
 
 
  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  ;;;;    INTEREST RATE ON DEPOSITS    ;;;;
  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
            
            
  ask (Banks with [color = Red]) 
   [set DepositsB (DepositsB * ((1 + rD) ^ (1 / 360)))
   ]
  
  tick
 
end