Ejemplo n.º 1
0
    def transaction__print_transaction(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.print_transaction \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__print_transaction 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("Creating a transaction")
        transaction = Transaction()
        print("Assigning values")
        transaction.this_transaction("type", "asset", "test_household", "test_firm", 1,  2,  3, 4)
        print("Adding the transaction to the books")
        transaction.add_transaction(environment)
        print("Printing transaction:")
        transaction.print_transaction()
Ejemplo n.º 2
0
    def initialize_transactions(self, state):

        value = 0.0

        # first, calculate number of transactions for investments
        numTransactions = int(round(self.assetNumber / self.numBanks))
        # print(numTransactions)
        if numTransactions == 0:  # we want some error message if there are two few assets in the economy
            logging.info("  ERROR: number of  assets in the economy has to be at least half the number of banks")
        # now, calculate value of each transaction and note that the *product* of all individual transactions
        # is supposed to have precision 4. Hence, each individual transaction should have precision 5
        value = round(float(self.gamma * self.lamb * self.V / numTransactions), 5)


        # finally, put them on the transaction stack
        for i in range(numTransactions):

            transaction = Transaction()
            #
            # account for different maturities
            #
            maturity = int(
                round(random.random() * state.firmLoanMaturity, 1))  # maturity is between 0 and firmLoanMaturity
            # and determine whether the loan will default
            if (
                    random.random() >= state.successProbabilityFirms):  # TODO this is superfluous, we could get rid of this doubling
                # the loan defaults: determine timeOfDefault
                timeOfDefault = int(round(random.random() * maturity))
            else:
                timeOfDefault = -1
            # then, generate the transaction, append it to the accounts, and delete it from memory
            transaction.this_transaction("I", self.identifier, -2, value, self.rhoReal, maturity, timeOfDefault)
            self.accounts.append(transaction)
            del transaction
        # store averageTransactionSize
        self.averageTransactionSize = value

        # then, calculate excess reserves
        value = round(float(self.gamma * (1.0 - self.lamb) * self.V), 4)
        transaction = Transaction()
        transaction.this_transaction("E", self.identifier, -3, value, self.rb, 0, -1)
        self.accounts.append(transaction)
        del transaction

        # on the liabilities side, banks are endowed with banking capital
        # (see comments in get_initial_banking_capital() for further details)
        value = round(float(self.get_initial_banking_capital(state.requiredCapitalRatio)), 4)
        transaction = Transaction()
        transaction.this_transaction("BC", self.identifier, self.identifier, value, 0.0, 0, -1)
        self.accounts.append(transaction)
        del transaction

        # now, transfer deposits from households to banks
        value = round(float(self.gamma * self.V - self.get_account("BC")), 4)
        transaction = Transaction()
        transaction.this_transaction("D", -1, self.identifier, value, self.rd, 0, -1)
        self.accounts.append(transaction)
        del transaction

        # as well as required deposits to the central bank
        value = round(float(self.r * self.get_account("D")), 4)
        transaction = Transaction()
        transaction.this_transaction("rD", self.identifier, -3, value, self.rb, 0, -1)
        self.accounts.append(transaction)
        del transaction

        # finally, determine central bank loans
        value = round(float(self.get_account("I") + self.get_account("E") + self.get_account("rD") - self.get_account(
            "D") - self.get_account("BC")), 4)
        transaction = Transaction()
        transaction.this_transaction("LC", self.identifier, -3, value, self.rb, 0, -1)
        print(transaction.print_transaction())
        self.accounts.append(transaction)
        del transaction