def test_banks_assets(self):
     simulator.do_economy_parameters(self.simInfo, self.pList, self.parameterDefs)
     simulator.do_bank_parameters(self.simInfo, self.pList, self.parameterDefs)
     eq_(len(self.simInfo.bankDirectory), 3, "Expected 3 banks but got %r" % len(self.simInfo.bankDirectory))
     b1 = self.simInfo.bankDirectory["bank1"]
     b2 = self.simInfo.bankDirectory["bank2"]
     e1 = self.simInfo.economyDirectory["econ1"]
     e2 = self.simInfo.economyDirectory["econ2"]
     eq_(b1.economy, e1, "expected bank1's economy to be econ1 but is %r" % b1.economy)
     eq_(b2.economy, e2, "expected bank2's economy to be econ2 but is %r" % b2.economy)
     assert simulator.check_bank_nodes(self.simInfo)
     simulator.reset_economies(self.simInfo)
     simulator.reset_banks(self.simInfo)
     # expect the following bank loans to be in place:
     # bank1 -> bank2, mean size 10
     # bank2 -> bank1, mean size 20
     # bank2 -> bank3, mean size 20
     # bank3 -> bank1, mean size 30
     b3 = self.simInfo.bankDirectory["bank3"]
     for bFrom, bTo, bl in zip([b1, b2, b2, b3], [b2, b1, b3, b1], [10, 20, 20, 30]):
         lending = network.bank_lending(self.simInfo, bFrom, bTo)
         assert abs(bl - lending) < 10, ("expected lending from %s to %s to be roughly %r but is %r" %
                                         (bFrom.id_, bTo.id_, bl, lending))
         borrowing = network.bank_borrowing(self.simInfo, bTo, bFrom)
         assert abs(bl - borrowing) < 10, ("expected borrowing to %s from %s to be roughly %r but is %r" %
                                           (bTo.id_, bFrom.id_, bl, borrowing))
     for b, far, cr, capR in zip([b1, b2, b3], [0.30, 0.40, 0.25], [0.1, 0.15, 0.15], [0.15, .2, .125]):
         assert approx_equal(b.total_assets(), b.total_liabilities() + b.equity_value(), .0000001), \
             "Expected %s balance sheet to balance: assets %f, liabilities %f, capital %f" % (
                 b.id_, b.total_assets(), b.total_liabilities(), b.equity_value())
         thisFar = b.financial_asset_ratio()
         thisCr = b.cash / b.total_assets()
         thisCapR = b.equity_value() / b.total_assets()
         if b.deposits > 0:   # there was no hanky panky
             assert approx_equal(far, thisFar, .0000001), ("Expected %s financial asset ratio to be %f but is %f" %
                                                         (b.id_, far, thisFar))
             assert approx_equal(cr, thisCr, .0000001), ("Expected %s cash ratio to be %f but is %f" % (b.id_, cr, thisCr))
             assert approx_equal(capR, thisCapR, .0000001), ("Expected %s capital ratio to be %f but is %f" %
                                                             (b.id_, capR, thisCapR))
         else:
             assert thisFar < far, ("Expected %s financial asset ratio to be less than %f but is %f" %  (b.id_, far, thisFar))
             assert thisCr > cr, ("Expected %s cash ratio to be greater than %f but is %f" % (b.id_, cr, thisCr))
             assert thisCapR < capR, ("Expected %s capital ratio to be less than %f but is %f" %
                                      (b.id_, capR, thisCapR))
 def test_economies(self):
     simulator.do_economy_parameters(self.simInfo, self.pList, self.parameterDefs)
     # we expect there to be two economies
     eq_(len(self.simInfo.economyDirectory), 2, "expected 2 economies but there are %r" % len(self.simInfo.economyDirectory))
     assert "econ1" in self.simInfo.economyDirectory, "expected econ1 to be in the directory"
     assert "econ2" in self.simInfo.economyDirectory, "expected econ2 to be in the directory"
     e1 = self.simInfo.economyDirectory["econ1"]
     e2 = self.simInfo.economyDirectory["econ2"]
     simulator.reset_economies(self.simInfo)
     # we expect 5 and 10 investments
     eq_(len(e1.investments), 5, "expected econ1 to have 5 investments but it has %r" % len(e1.investments))
     eq_(len(e2.investments), 10, "expected econ2 to have 5 investments but it has %r" % len(e2.investments))
     # check that the parenting of parameters is working
     p1 = e1.params.get('randomSeed')
     eq_(p1, '12343')
     e1.create_investments(6)   # create another 6
     eq_(len(e1.investments), 11, "expected econ1 to have 11 investments but it has %r" % len(e1.investments))
     # now reset again
     simulator.reset_economies(self.simInfo)
     eq_(len(e1.investments), 5, "expected econ1 to have 5 investments again but it has %r" % len(e1.investments))
    def test_banks_per_bank(self):
        self.simInfo.theParameters.set('loanSizeType', 'perBank')
        simulator.do_economy_parameters(self.simInfo, self.pList, self.parameterDefs)
        simulator.do_bank_parameters(self.simInfo, self.pList, self.parameterDefs)
        simulator.reset_economies(self.simInfo)
        simulator.reset_banks(self.simInfo)
        b1 = self.simInfo.bankDirectory["bank1"]
        b2 = self.simInfo.bankDirectory["bank2"]
        b3 = self.simInfo.bankDirectory["bank3"]

        # expect the following bank loans to be in place:
        # bank1 -> bank2, total size 10
        # bank2 -> bank1, } total size 20
        # bank2 -> bank3, }
        # bank3 -> bank1, total size 30

        for bFrom, bTo, bl in zip([b1, b2, b2, b3], [b2, b1, b3, b1], [10, 10, 10, 30]):
            lending = network.bank_lending(self.simInfo, bFrom, bTo)
            assert abs(bl - lending) < 10, ("expected lending from %s to %s to be roughly %r but is %r" %
                                            (bFrom.id_, bTo.id_, bl, lending))
            borrowing = network.bank_borrowing(self.simInfo, bTo, bFrom)
            assert abs(bl - borrowing) < 10, ("expected borrowing to %s from %s to be roughly %r but is %r" %
                                              (bTo.id_, bFrom.id_, bl, borrowing))
    def test_banks_liabilities(self):
        simulator.do_economy_parameters(self.simInfo, self.pList, self.parameterDefs)
        simulator.do_bank_parameters(self.simInfo, self.pList, self.parameterDefs)
        self.simInfo.theParameters.set('balanceSheetMethod', 'liabilities')
        b1 = self.simInfo.bankDirectory["bank1"]
        b2 = self.simInfo.bankDirectory["bank2"]
        simulator.reset_economies(self.simInfo)
        simulator.reset_banks(self.simInfo)
        # expect the following bank loans to be in place:
        # bank1 -> bank2, mean size 10
        # bank2 -> bank1, mean size 20
        # bank2 -> bank3, mean size 20
        # bank3 -> bank1, mean size 30
        b3 = self.simInfo.bankDirectory["bank3"]

        for b, capR, flr, cr in zip([b1, b2, b3], [0.15, .2, .125], [0.7, 0.2, 0.7],
                                    [0.1, 0.15, 0.15]):
            assert approx_equal(b.total_assets(), b.total_liabilities() + b.equity_value(), .0000001), \
                "Expected %s balance sheet to balance: assets %f, liabilities %f, capital %f" % (
                    b.id_, b.total_assets(), b.total_liabilities(), b.equity_value())
            thisCapR = b.equity_value() / b.total_assets()
            thisFlr = b.financial_liability_ratio()
            thisCr = b.cash / b.total_assets()
            thisInv = b.investment_value()
            if thisInv > 0:  # no hanky panky
                assert approx_equal(capR, thisCapR, .0000001), ("Expected %s capital ratio to be %f but is %f" %
                                                                                                        (b.id_, capR, thisCapR))
                assert approx_equal(flr, thisFlr, .0000001), ("Expected %s financial liability ratio to be %f but is %f" %
                                                              (b.id_, flr, thisFlr))
                assert approx_equal(cr, thisCr, .0000001), ("Expected %s cash ratio to be %f but is %f" % (b.id_, cr, thisCr))
            else:
                assert thisCapR < capR, ("Expected %s capital ratio to be less than %f but is %f" %
                                         (b.id_, capR, thisCapR))
                assert thisFlr < flr, ("Expected %s financial liability ratio to be less than %f but is %f" %
                                       (b.id_, flr, thisFlr))
                assert thisCr < cr, ("Expected %s cash ratio to be less than %f but is %f" % (b.id_, cr, thisCr))