def transaction__purge_accounts(self, args): import os from src.bank import Bank from src.household import Household from src.firm import Firm from src.environment import Environment from src.transaction import Transaction text = "This test checks transaction.purge_accounts \n" self.print_info(text) # # INITIALIZATION # environment_directory = str(args[0]) identifier = str(args[1]) log_directory = str(args[2]) # Configure logging parameters so we get output while the program runs logging.basicConfig(format='%(asctime)s %(message)s', datefmt='%m/%d/%Y %H:%M:%S', filename=log_directory + identifier + ".log", level=logging.INFO) logging.info('START logging for test transaction__purge_accounts in run: %s', environment_directory + identifier + ".xml") # Construct household filename environment = Environment(environment_directory, identifier) # generate a bank bank = Bank() bank.identifier = "test_bank" environment.banks.append(bank) # generate a firm firm = Firm() firm.identifier = "test_firm" environment.firms.append(firm) # generate a household household = Household() household.identifier = "test_household" environment.households.append(household) # # TESTING # print("Before purging the accounts") transaction = Transaction() transaction.this_transaction("type", "asset", "test_household", "test_firm", 0, 2, 3, 4) transaction.add_transaction(environment) print(environment.get_agent_by_id("test_household")) print(environment.get_agent_by_id("test_firm")) print("After clearing one bank's accounts") transaction.purge_accounts(environment) print(environment.get_agent_by_id("test_household")) print(environment.get_agent_by_id("test_firm"))
def do_update(self, environment, time): # As a first step, we accrue all interest over the transactions # Thus, important to notice to keep 0 as interest by default # Unless transaction should carry interest # DON'T DO INTERESTS SO FAR, DO ONCE THE REST WORKS self.accrue_interests(environment, time) # Then agents get their labour endowment for the step (e.g. work hours to spend) # For now we don't need to keep track of labour left as there is no queue # self.endow_labour(environment, time) # The households sell labour to firms self.sell_labour(environment, time) # The firms sell goods to households self.consume_rationed(environment, time) # We net deposits and loans self.net_loans_deposits(environment, time) # We remove goods and labour (perishable) and are left with capital self.net_labour_goods(environment, time) # Purging accounts at every step just in case transaction = Transaction() transaction.purge_accounts(environment)
def do_update(self, environment, time): # As a first step, we accrue all interest over the transactions # Thus, important to notice to keep 0 as interest by default # Unless transaction should carry interest self.accrue_interests(environment, time) # The households sell labour to firms self.sell_labour(environment, time) # The firms sell goods to households self.consume_rationed(environment, time) # We net deposits and loans self.net_loans_deposits(environment, time) # We remove the perishable transactions self.remove_perishable(environment, time) # And add capital to balance the books self.capitalise(environment, time) # Investing of the banks self.invest(environment, time) # Purging accounts at every step just in case transaction = Transaction() transaction.purge_accounts(environment)
def do_update(self, environment, time): # As a first step, we accrue all interest over the transactions # Thus, important to notice to keep 0 as interest by default # Unless transaction should carry interest # DON'T DO INTERESTS SO FAR, DO ONCE THE REST WORKS self.accrue_interests(environment, time) # Then agents get their labour endowment for the step (e.g. work hours to spend) # For now we don't need to keep track of labour left as there is no queue # self.endow_labour(environment, time) # The households sell labour to firms self.sell_labour(environment, time) # The firms sell goods to households self.consume_rationed(environment, time) # We net deposits and loans self.net_loans_deposits(environment, time) # We remove goods and labour (perishable) and are left with capital self.net_labour_goods(environment, time) # Purging accounts at every step just in case transaction = Transaction() transaction.purge_accounts(environment)
def do_update(self, environment, time): # As a first step, we accrue all interest over the transactions # Thus, important to notice to keep 0 as interest by default # Unless transaction should carry interest self.accrue_interests(environment, time) # The households sell labour to firms self.sell_labour(environment, time) # The firms sell goods to households self.consume_rationed(environment, time) # We net deposits and loans self.net_loans_deposits(environment, time) # We remove the perishable transactions self.remove_perishable(environment, time) # And add capital to balance the books self.capitalise(environment, time) # Investing of the banks self.invest(environment, time) # Purging accounts at every step just in case transaction = Transaction() transaction.purge_accounts(environment)
def consume(self, environment, time): # We want the consumption to be done in random pairs # So we need to randomise the households and the firms # We start with the households and create a list of order integers # the size of the number of households itrange_hh = list(range(0, int(environment.num_households))) # Then we shuffle the list random.shuffle(itrange_hh) # And use it to loop over the households randomly for h in itrange_hh: # Since we don't loop directly over households # We assign the correct household in this run over the loop household = environment.households[h] wealth = 0.0 # total of deposits and cash available for the household cash = 0.0 # total of cash available for the household # We calculate the above two values for tranx in household.accounts: # We add all deposits and all cash to the wealth if tranx.type_ == "deposits" or tranx.type_ == "cash": wealth = wealth + tranx.amount # But only cash to the available cash if tranx.type_ == "cash": cash = cash + tranx.amount # We consume the percentage of wealth determined by # the propensity to save, cash first to_consume = wealth * (1 - household.propensity_to_save) # Now we randomise firms and create a list of order integers # the size of the number of households itrange = list(range(0, int(environment.num_firms))) # Then we shuffle the list random.shuffle(itrange) # For each firm in random order for i in itrange: # For every transaction on that firm's books # We make a proxy for the cash that firm should obtain # for whatever good they've sold to the household firm = environment.firms[i] firm_cash = 0.0 for tranx in firm.accounts: # If the transaction contains goods if tranx.type_ == "goods": # We go through the household's accounts for tranx_h in household.accounts: # And look for cash if tranx_h.type_ == "cash": # We can buy for minimum of the cash and goods # in question amount_proxy = min(tranx.amount, tranx_h.amount, to_consume) # And remove the appropriate amount of cash tranx_h.amount = tranx_h.amount - amount_proxy # Lower the amount household wants to consume to_consume = to_consume - amount_proxy # And remove the goods from firm's account tranx.amount = tranx.amount - amount_proxy # And we note the cash to be added to the firm firm_cash = firm_cash + amount_proxy for tranx_h in household.accounts: # And look for deposits if tranx_h.type_ == "deposits": # We can buy for minimum of the deposits and goods # in question amount_proxy = min(tranx.amount, tranx_h.amount, to_consume) # And remove the appropriate amount of deposits tranx_h.amount = tranx_h.amount - amount_proxy # Lower the amount household wants to consume to_consume = to_consume - amount_proxy # And remove the goods from firm's account tranx.amount = tranx.amount - amount_proxy # And we note the cash to be added to the firm firm_cash = firm_cash + amount_proxy # Add cash for sold items to the firm cash_number = 0 # We calculate how many cash account the firm has for tranx in firm.accounts: if tranx.type_ == "cash": cash_number = cash_number + 1 # If the firm doesn't have any cash accounts we create a new one if cash_number == 0: # Create a transaction transaction = Transaction() # Add the appropriate values to the transaction transaction.this_transaction("cash", "", firm.identifier, firm.identifier, firm_cash, 0, 0, -1) # And add the transaction to the books (do it through function/not manually) transaction.add_transaction(environment) # If the firm has previous cash transactions we add the cash from sales proportionately else: # We find all cash transactions for tranx in firm.accounts: if tranx.type_ == "cash": # And add the sales proceeds proportionately tranx.amount = tranx.amount + (firm_cash / cash_number) # The sales above may have rendered some transactions worthless # So we need to purge all accounts to make sure everything is in order transaction = Transaction() transaction.purge_accounts(environment) # Finally, we remove the goods which weren't sold from firms' accounts # As they are perishable self.remove_goods_firms(environment, time) logging.info(" goods consumed on step: %s", time)
def sell_labour(self, environment, time): # We want the sell to be done in random pairs # So we need to randomise the households and the firms # We start with the firms and create a list of order integers # the size of the number of firms itrange = list(range(0, int(environment.num_firms))) # Then we shuffle the list random.shuffle(itrange) # And use it to loop over the firms randomly for i in itrange: # Since we don't loop directly over firms # We assign the correct firm in this run over the loop firm = environment.firms[i] # We calculate the amount of cash firm has to buy labour to_buy = 0.0 # We go through the firm's transactions for tranx in firm.accounts: # If we find cash transaction if tranx.type_ == "cash": # We add the cash to the amount of labour the firm # wants to buy, we assume 1 unit of labour costs 1 unit of cash to_buy = to_buy + tranx.amount # Now we randomise households and create a list of order integers # the size of the number of households itrange_hh = list(range(0, int(environment.num_households))) # Then we shuffle the list random.shuffle(itrange_hh) # For each household in random order for h in itrange_hh: # Since we don't loop directly over households # We assign the correct household in this run over the loop household = environment.households[h] household_cash = 0.0 # We go through household's accounts for tranx in household.accounts: # And find transactions with labour if tranx.type_ == "manhours": # We will sell them for cash # So we look through firm's accounts for tranx_f in firm.accounts: # And find cash transactions if tranx_f.type_ == "cash": # We can only buy the lowest amount from the cash the firm # has, the labour the household has, and however many units # they want to buy amount_proxy = min(tranx.amount, tranx_f.amount, to_buy) # Then we remove the appropriate amount of cash from the firm tranx_f.amount = tranx_f.amount - amount_proxy # Lower the amount firm wants to buy to_buy = to_buy - amount_proxy # And remove the goods from household's account tranx.amount = tranx.amount - amount_proxy # And we note the cash to be added to the household household_cash = household_cash + amount_proxy # Create a transaction transaction = Transaction() # Add the appropriate values to the transaction transaction.this_transaction( "manhours", "", firm.identifier, firm.identifier, amount_proxy, 0, 0, -1) # And add the transaction to the books (do it through function/not manually) transaction.add_transaction(environment) # Add cash for sold items to the household cash_number = 0 # We calculate how many cash account the household has for tranx in household.accounts: if tranx.type_ == "cash": cash_number = cash_number + 1 # If there are no cash transactions on the household's books # We create a new one and put the proceeds there if cash_number == 0: # Create a transaction transaction = Transaction() # Add the appropriate values to the transaction transaction.this_transaction("cash", "", household.identifier, household.identifier, household_cash, 0, 0, -1) # And add the transaction to the books (do it through function/not manually) transaction.add_transaction(environment) # If the household has previous cash transactions we add the cash from sales proportionately else: # We find all cash transactions for tranx in household.accounts: if tranx.type_ == "cash": # And add the sales proceeds proportionately tranx.amount = tranx.amount + (household_cash / cash_number) # The sales above may have rendered some transactions worthless # So we need to purge all accounts to make sure everything is in order transaction = Transaction() transaction.purge_accounts(environment) logging.info(" labour sold to firms on step: %s", time)
def consume(self, environment, time): # We want the consumption to be done in random pairs # So we need to randomise the households and the firms # We start with the households and create a list of order integers # the size of the number of households itrange_hh = list(range(0, int(environment.num_households))) # Then we shuffle the list random.shuffle(itrange_hh) # And use it to loop over the households randomly for h in itrange_hh: # Since we don't loop directly over households # We assign the correct household in this run over the loop household = environment.households[h] wealth = 0.0 # total of deposits and cash available for the household cash = 0.0 # total of cash available for the household # We calculate the above two values for tranx in household.accounts: # We add all deposits and all cash to the wealth if tranx.type_ == "deposits" or tranx.type_ == "cash": wealth = wealth + tranx.amount # But only cash to the available cash if tranx.type_ == "cash": cash = cash + tranx.amount # We consume the percentage of wealth determined by # the propensity to save, cash first to_consume = wealth * (1 - household.propensity_to_save) # Now we randomise firms and create a list of order integers # the size of the number of households itrange = list(range(0, int(environment.num_firms))) # Then we shuffle the list random.shuffle(itrange) # For each firm in random order for i in itrange: # For every transaction on that firm's books # We make a proxy for the cash that firm should obtain # for whatever good they've sold to the household firm = environment.firms[i] firm_cash = 0.0 for tranx in firm.accounts: # If the transaction contains goods if tranx.type_ == "goods": # We go through the household's accounts for tranx_h in household.accounts: # And look for cash if tranx_h.type_ == "cash": # We can buy for minimum of the cash and goods # in question amount_proxy = min(tranx.amount, tranx_h.amount, to_consume) # And remove the appropriate amount of cash tranx_h.amount = tranx_h.amount - amount_proxy # Lower the amount household wants to consume to_consume = to_consume - amount_proxy # And remove the goods from firm's account tranx.amount = tranx.amount - amount_proxy # And we note the cash to be added to the firm firm_cash = firm_cash + amount_proxy for tranx_h in household.accounts: # And look for deposits if tranx_h.type_ == "deposits": # We can buy for minimum of the deposits and goods # in question amount_proxy = min(tranx.amount, tranx_h.amount, to_consume) # And remove the appropriate amount of deposits tranx_h.amount = tranx_h.amount - amount_proxy # Lower the amount household wants to consume to_consume = to_consume - amount_proxy # And remove the goods from firm's account tranx.amount = tranx.amount - amount_proxy # And we note the cash to be added to the firm firm_cash = firm_cash + amount_proxy # Add cash for sold items to the firm cash_number = 0 # We calculate how many cash account the firm has for tranx in firm.accounts: if tranx.type_ == "cash": cash_number = cash_number + 1 # If the firm doesn't have any cash accounts we create a new one if cash_number == 0: # Create a transaction transaction = Transaction() # Add the appropriate values to the transaction transaction.this_transaction("cash", "", firm.identifier, firm.identifier, firm_cash, 0, 0, -1) # And add the transaction to the books (do it through function/not manually) transaction.add_transaction(environment) # If the firm has previous cash transactions we add the cash from sales proportionately else: # We find all cash transactions for tranx in firm.accounts: if tranx.type_ == "cash": # And add the sales proceeds proportionately tranx.amount = tranx.amount + (firm_cash / cash_number) # The sales above may have rendered some transactions worthless # So we need to purge all accounts to make sure everything is in order transaction = Transaction() transaction.purge_accounts(environment) # Finally, we remove the goods which weren't sold from firms' accounts # As they are perishable self.remove_goods_firms(environment, time) logging.info(" goods consumed on step: %s", time)
def sell_labour(self, environment, time): # We want the sell to be done in random pairs # So we need to randomise the households and the firms # We start with the firms and create a list of order integers # the size of the number of firms itrange = list(range(0, int(environment.num_firms))) # Then we shuffle the list random.shuffle(itrange) # And use it to loop over the firms randomly for i in itrange: # Since we don't loop directly over firms # We assign the correct firm in this run over the loop firm = environment.firms[i] # We calculate the amount of cash firm has to buy labour to_buy = 0.0 # We go through the firm's transactions for tranx in firm.accounts: # If we find cash transaction if tranx.type_ == "cash": # We add the cash to the amount of labour the firm # wants to buy, we assume 1 unit of labour costs 1 unit of cash to_buy = to_buy + tranx.amount # Now we randomise households and create a list of order integers # the size of the number of households itrange_hh = list(range(0, int(environment.num_households))) # Then we shuffle the list random.shuffle(itrange_hh) # For each household in random order for h in itrange_hh: # Since we don't loop directly over households # We assign the correct household in this run over the loop household = environment.households[h] household_cash = 0.0 # We go through household's accounts for tranx in household.accounts: # And find transactions with labour if tranx.type_ == "manhours": # We will sell them for cash # So we look through firm's accounts for tranx_f in firm.accounts: # And find cash transactions if tranx_f.type_ == "cash": # We can only buy the lowest amount from the cash the firm # has, the labour the household has, and however many units # they want to buy amount_proxy = min(tranx.amount, tranx_f.amount, to_buy) # Then we remove the appropriate amount of cash from the firm tranx_f.amount = tranx_f.amount - amount_proxy # Lower the amount firm wants to buy to_buy = to_buy - amount_proxy # And remove the goods from household's account tranx.amount = tranx.amount - amount_proxy # And we note the cash to be added to the household household_cash = household_cash + amount_proxy # Create a transaction transaction = Transaction() # Add the appropriate values to the transaction transaction.this_transaction("manhours", "", firm.identifier, firm.identifier, amount_proxy, 0, 0, -1) # And add the transaction to the books (do it through function/not manually) transaction.add_transaction(environment) # Add cash for sold items to the household cash_number = 0 # We calculate how many cash account the household has for tranx in household.accounts: if tranx.type_ == "cash": cash_number = cash_number + 1 # If there are no cash transactions on the household's books # We create a new one and put the proceeds there if cash_number == 0: # Create a transaction transaction = Transaction() # Add the appropriate values to the transaction transaction.this_transaction("cash", "", household.identifier, household.identifier, household_cash, 0, 0, -1) # And add the transaction to the books (do it through function/not manually) transaction.add_transaction(environment) # If the household has previous cash transactions we add the cash from sales proportionately else: # We find all cash transactions for tranx in household.accounts: if tranx.type_ == "cash": # And add the sales proceeds proportionately tranx.amount = tranx.amount + (household_cash / cash_number) # The sales above may have rendered some transactions worthless # So we need to purge all accounts to make sure everything is in order transaction = Transaction() transaction.purge_accounts(environment) logging.info(" labour sold to firms on step: %s", time)