Esempio n. 1
0
 def test_gini2shares(self):
     gini0 = random.random()
     nbrackets = 10**random.randrange(3, 5)
     shares01 = list(gini2shares(gini=gini0, nbrackets=nbrackets))
     print "sum", sum(shares01)
     gini1 = calc_gini(shares01)
     #print "ginis:", gini0, gini1   TODO: better accuracy expected...
     self.assert_(fmath.feq(gini0, gini1,
                            1e-3))  #imposed and computed Gini shd be equal
     print "here"
     shares02 = list(gini2sharesPareto(gini=gini0, nbrackets=nbrackets))
     print "sum", sum(shares02)
     gini2 = calc_gini(shares02)
     #print "ginis:", gini0, gini1   TODO: better accuracy expected...
     self.assert_(
         fmath.feq(gini0, gini2,
                   1. / nbrackets))  #imposed and computed Gini shd be equal
     print shares01[::100]
     print calc_gini(shares01)
     print shares02[::100]
     print calc_gini(shares02)
     fig, (ax1, ax2) = plt.subplots(1, 2)
     ax1.plot(shares01)
     ax2.plot(np.cumsum(shares01))
     ax2.plot(np.cumsum(shares02))
     plt.show()
     exit()
Esempio n. 2
0
	def test_picard(self):
		f1 = lambda x: 0.5*(x+2/x)  #approx sqrt of 2
		self.assertTrue(fmath.feq(iterate.Picard(f1, 1, 100, tol=1e-6).fp,math.sqrt(2),1e-6))
		f2 =lambda x: math.exp(-x)
		best_result = iterate.Picard(f2, 1, 100, tol=1e-6).fp  #must match tolerance...
		result1 = simplest_picard(f2, 1)
		result2 = simple_picard(f2, 1, 100)
		self.assertTrue(fmath.feq(result1, best_result, 1e-6))
		self.assertTrue(fmath.feq(result2, best_result, 1e-6))
Esempio n. 3
0
 def test_calc_gini(self):
     #test that two Gini formulae give same result
     gini1 = calc_gini(self.wealths)
     gini2 = calc_gini2(self.wealths)
     gini3 = calc_gini3(self.wealths)
     gini4 = calc_gini4(self.wealths)
     #print "gini1:%f, gini2:%f"%(gini1, gini2)
     self.assert_(fmath.feq(gini1, gini2))
     self.assert_(fmath.feq(gini1, gini3))
     print gini1, gini4
     self.assert_(fmath.feq(gini1, gini4))
Esempio n. 4
0
 def test_calc_gini(self):
     # test that two Gini formulae give same result
     gini1 = calc_gini(self.wealths)
     gini2 = calc_gini2(self.wealths)
     gini3 = calc_gini3(self.wealths)
     gini4 = calc_gini4(self.wealths)
     # print "gini1:%f, gini2:%f"%(gini1, gini2)
     self.assert_(fmath.feq(gini1, gini2))
     self.assert_(fmath.feq(gini1, gini3))
     print gini1, gini4
     self.assert_(fmath.feq(gini1, gini4))
Esempio n. 5
0
 def test_picard(self):
     f1 = lambda x: 0.5 * (x + 2 / x)  #approx sqrt of 2
     self.assertTrue(
         fmath.feq(
             iterate.Picard(f1, 1, 100, tol=1e-6).fp, math.sqrt(2), 1e-6))
     f2 = lambda x: math.exp(-x)
     best_result = iterate.Picard(f2, 1, 100,
                                  tol=1e-6).fp  #must match tolerance...
     result1 = simplest_picard(f2, 1)
     result2 = simple_picard(f2, 1, 100)
     self.assertTrue(fmath.feq(result1, best_result, 1e-6))
     self.assertTrue(fmath.feq(result2, best_result, 1e-6))
Esempio n. 6
0
 def test_gini2shares(self):
     gini0 = random.random()
     nbrackets = 10 ** random.randrange(3, 5)
     shares01 = list(gini2shares(gini=gini0, nbrackets=nbrackets))
     print "sum", sum(shares01)
     gini1 = calc_gini(shares01)
     # print "ginis:", gini0, gini1   TODO: better accuracy expected...
     self.assert_(fmath.feq(gini0, gini1, 1e-3))  # imposed and computed Gini shd be equal
     print "here"
     shares02 = list(gini2sharesPareto(gini=gini0, nbrackets=nbrackets))
     print "sum", sum(shares02)
     gini2 = calc_gini(shares02)
     # print "ginis:", gini0, gini1   TODO: better accuracy expected...
     self.assert_(fmath.feq(gini0, gini2, 1.0 / nbrackets))  # imposed and computed Gini shd be equal
     """
Esempio n. 7
0
	def test_bisect(self):
		x4zero = random.randrange(20)
		f = lambda x: (x-x4zero)**3
		crit = iterate.AbsDiff(1e-9)
		b1 = iterate.Bisect(f, x4zero - 1.0, x4zero+1.0, crit)
		b1.run()
		#print b1.report()
		result1 = b1.value
		result2 = iterate.bisect(f,  x4zero - 1.0, x4zero+1.0, eps=1e-9)
		result3 = simplest_bisection(f, x4zero - 1.0, x4zero+1.0)
		print "testvals", result1
		print "simple bisect", result2
		print "simplest bisect", result3
		self.assert_(fmath.feq(result1, x4zero, 1e-8))
		self.assert_(fmath.feq(result2, x4zero, 1e-8))
		self.assert_(fmath.feq(result3, x4zero, 1e-7))
Esempio n. 8
0
	def allocate_factors(self):
		assert len(self.funds)==1 and len(self.firms)==1
		fund = self.funds[0]
		repfirm = self.firms[0]
		# factor mkts determine K, N, and allocation of K, N
		# also determine w and irate for these services
		labor_force = self.ppl.get_labor_force()
		#inelastically supplied factors fully employed
		#only the young in the labor force
		elabor = sum(indiv.labor_supply(None, None) for indiv in labor_force) #chk
		#only the young own capital
		capital = fund.calc_accts_value()
		if self.params.DEBUG:
			capital2 = sum(indiv.calc_wealth() for indiv in labor_force)  #chk
			assert fmath.feq(capital2, capital)
		#PestieauEconomy has a single representative firm
		irate, wage = repfirm.mpk_mpn(capital=capital, elabor=elabor)
		script_logger.debug( "Eq. irate %10.2f,           wage %10.2f"%(irate, wage) )
		capital_contract = ServiceContract(contract_type='capital', quantity=capital, price=irate, buyer=repfirm, seller=fund)
		labor_force = self.ppl.get_labor_force()
		script_logger.debug( "Labor force size: %d."%(len(labor_force)) )
		for worker in labor_force:
			labor = worker.labor_supply(irate=None, wage=None) #inelastic labor supply
			wage_contract = ServiceContract(contract_type='labor', quantity=labor, price=wage, buyer=repfirm, seller=worker)  #TODO TODO
		script_logger.debug( "Number of labor contracts: %d."%(len(repfirm.contracts['labor'])) )
		if self.params.DEBUG:
			captital = sum( contract.quantity for contract in repfirm.contracts['capital'] )
			labor = sum( contract.quantity for contract in repfirm.contracts['labor'] )
			script_logger.info("Capital (%10.2f) and eff. labor (%10.2f) allocated."%(capital, elabor))
			script_logger.info("irate (%10.2f) and w (%10.2f) determined."%(irate, wage))
Esempio n. 9
0
	def test_bisect(self):
		print("""START TEST""")
		x4zero = random.randrange(20)
		f = lambda x: (x-x4zero)**3
		crit = iterate.AbsDiff(1e-9)
		b1 = iterate.Bisect(f, x4zero - 1.0, x4zero+1.0, crit)
		b1.run()
		#print b1.report()
		result1 = b1.value
		result2 = iterate.bisect(f,  x4zero - 1.0, x4zero+1.0, eps=1e-9)
		result3 = simplest_bisection(f, x4zero - 1.0, x4zero+1.0)
		print("testvals", b1.value)
		print("simple bisect", result2)
		print("simplest bisection", result3)
		self.assertTrue(fmath.feq(b1.value, x4zero, 1e-8))
		self.assertTrue(fmath.feq(result2, x4zero, 1e-8))
		self.assertTrue(fmath.feq(result3, x4zero, 1e-7))
Esempio n. 10
0
	def test_ridders(self):
		shift = random.randrange(20)
		f = lambda x: x*exp(x) - shift
		testval = simplest_bisection(f,   -10., 10.)
		result1 = simplest_ridders(f,   -10., 10.)
		print("Ridders method with shift=%f"%shift)
		print("testvals", result1)
		self.assertTrue(fmath.feq(result1, testval, 1e-8))
Esempio n. 11
0
 def test_ridders(self):
     shift = random.randrange(20)
     f = lambda x: x * exp(x) - shift
     testval = simplest_bisection(f, -10., 10.)
     result1 = simplest_ridders(f, -10., 10.)
     print("Ridders method with shift=%f" % shift)
     print("testvals", result1)
     self.assertTrue(fmath.feq(result1, testval, 1e-8))
Esempio n. 12
0
	def distribute_gains(self):
		accts_value = self.calc_accts_value()
		assert accts_value==sum( indiv.calc_wealth() for indiv in self.economy.ppl.get_indivs() )
		ror = self.net_worth/accts_value
		for acct in self._accounts:
			value = acct.get_value()
			self.economy.transfer(self, acct, ror*value)
		assert fmath.feq(self.net_worth, 0, 0.01)  #this chk is currently pointless
Esempio n. 13
0
 def test_bisect(self):
     print("""START TEST""")
     x4zero = random.randrange(20)
     f = lambda x: (x - x4zero)**3
     crit = iterate.AbsDiff(1e-9)
     b1 = iterate.Bisect(f, x4zero - 1.0, x4zero + 1.0, crit)
     b1.run()
     #print b1.report()
     result1 = b1.value
     result2 = iterate.bisect(f, x4zero - 1.0, x4zero + 1.0, eps=1e-9)
     result3 = simplest_bisection(f, x4zero - 1.0, x4zero + 1.0)
     print("testvals", b1.value)
     print("simple bisect", result2)
     print("simplest bisection", result3)
     self.assertTrue(fmath.feq(b1.value, x4zero, 1e-8))
     self.assertTrue(fmath.feq(result2, x4zero, 1e-8))
     self.assertTrue(fmath.feq(result3, x4zero, 1e-7))
Esempio n. 14
0
 def distribute_gains(self):
     accts_value = self.calc_accts_value()
     assert accts_value == sum(indiv.calc_wealth()
                               for indiv in self.economy.ppl.get_indivs())
     ror = self.net_worth / accts_value
     for acct in self._accounts:
         value = acct.get_value()
         self.economy.transfer(self, acct, ror * value)
     assert fmath.feq(self.net_worth, 0,
                      0.01)  #this chk is currently pointless
Esempio n. 15
0
	def test_gini2shares(self):
		gini0 = random.random()
		nbrackets = 10**random.randrange(3,5)
		shares01 = list(gini2shares(gini=gini0, nbrackets=nbrackets))
		print "sum", sum(shares01)
		gini1 = calc_gini(shares01)
		#print "ginis:", gini0, gini1   TODO: better accuracy expected...
		self.assert_(fmath.feq(gini0, gini1, 1e-3)) #imposed and computed Gini shd be equal
		print "here"
		shares02 = list( gini2sharesPareto(gini=gini0, nbrackets=nbrackets) )
		print "sum", sum(shares02)
		gini2 = calc_gini(shares02)
		#print "ginis:", gini0, gini1   TODO: better accuracy expected...
		self.assert_(fmath.feq(gini0, gini2, 1./nbrackets)) #imposed and computed Gini shd be equal
		print shares01[::100]
		print calc_gini(shares01)
		print shares02[::100]
		print calc_gini(shares02)
		fig, (ax1,ax2) = plt.subplots(1,2)
		ax1.plot(shares01)
		ax2.plot(np.cumsum(shares01))
		ax2.plot(np.cumsum(shares02))
		plt.show()
		exit()
Esempio n. 16
0
 def allocate_factors(self):
     assert len(self.funds) == 1 and len(self.firms) == 1
     fund = self.funds[0]
     repfirm = self.firms[0]
     # factor mkts determine K, N, and allocation of K, N
     # also determine w and irate for these services
     labor_force = self.ppl.get_labor_force()
     #inelastically supplied factors fully employed
     #only the young in the labor force
     elabor = sum(indiv.labor_supply(None, None)
                  for indiv in labor_force)  #chk
     #only the young own capital
     capital = fund.calc_accts_value()
     if self.params.DEBUG:
         capital2 = sum(indiv.calc_wealth() for indiv in labor_force)  #chk
         assert fmath.feq(capital2, capital)
     #PestieauEconomy has a single representative firm
     irate, wage = repfirm.mpk_mpn(capital=capital, elabor=elabor)
     script_logger.debug("Eq. irate %10.2f,           wage %10.2f" %
                         (irate, wage))
     capital_contract = ServiceContract(contract_type='capital',
                                        quantity=capital,
                                        price=irate,
                                        buyer=repfirm,
                                        seller=fund)
     labor_force = self.ppl.get_labor_force()
     script_logger.debug("Labor force size: %d." % (len(labor_force)))
     for worker in labor_force:
         labor = worker.labor_supply(irate=None,
                                     wage=None)  #inelastic labor supply
         wage_contract = ServiceContract(contract_type='labor',
                                         quantity=labor,
                                         price=wage,
                                         buyer=repfirm,
                                         seller=worker)  #TODO TODO
     script_logger.debug("Number of labor contracts: %d." %
                         (len(repfirm.contracts['labor'])))
     if self.params.DEBUG:
         captital = sum(contract.quantity
                        for contract in repfirm.contracts['capital'])
         labor = sum(contract.quantity
                     for contract in repfirm.contracts['labor'])
         script_logger.info(
             "Capital (%10.2f) and eff. labor (%10.2f) allocated." %
             (capital, elabor))
         script_logger.info("irate (%10.2f) and w (%10.2f) determined." %
                            (irate, wage))
Esempio n. 17
0
    def factor_payments(self):  #chk chk
        '''collect factor payments from representative firm;
		allocate factor payments to providers of factor services.

		:todo: must pay rents before wages, bc rents paid on
		       the individual's wealth, so don't want to change
		       that before paying rents.  This is ugly.
		       Get capital from and pay rents to fund instead. chk
		'''
        assert len(self.firms) == 1 and len(self.funds) == 1
        repfirm = self.firms[0]
        inventory = repfirm.inventory  #production not yet distributed
        fund = self.funds[0]
        rents_paid = 0
        contracts = repfirm.contracts['capital']  #not a copy!
        while contracts:
            contract = contracts.pop()  #removes contract from list
            assert (contract.seller is fund
                    )  #for simplicity, fund provides all capital services
            rents_paid += contract.payment()  #firm rents capital from fund
        #currently *MUST* distribute gains *BEFORE* other income rcd
        # (this buys convenience of single contract with Fund instead of contracts w each indiv)
        fund.distribute_gains()  #fund distributes gains to fund accounts
        script_logger.debug("Number of labor contracts: %d." %
                            (len(repfirm.contracts['labor'])))
        wages_paid = 0
        contracts = repfirm.contracts['labor']  #not a copy!
        while contracts:
            contract = contracts.pop()  #removes contract
            assert isinstance(contract.seller, Indiv)  # ugly
            wages_paid += contract.payment()  #payment fulfills contract
        if self.params.DEBUG:
            script_logger.debug("Production: %10.2f" % (inventory))
            script_logger.debug(
                "Factor payments: %10.2f (= %10.2f + %10.2f)" %
                (rents_paid + wages_paid, rents_paid, wages_paid))
            script_logger.debug("Remaining inventory: %10.2f" %
                                (repfirm.inventory))
        assert fmath.feq(repfirm.inventory, 0, 1e-6 * inventory)
        #reset inventory to prevent accumulation of small differences
        repfirm.inventory = 0
Esempio n. 18
0
	def factor_payments(self): #chk chk
		'''collect factor payments from representative firm;
		allocate factor payments to providers of factor services.

		:todo: must pay rents before wages, bc rents paid on
		       the individual's wealth, so don't want to change
		       that before paying rents.  This is ugly.
		       Get capital from and pay rents to fund instead. chk
		'''
		assert len(self.firms)==1 and len(self.funds)==1
		repfirm = self.firms[0]
		inventory = repfirm.inventory  #production not yet distributed
		fund = self.funds[0]
		rents_paid = 0
		contracts = repfirm.contracts['capital']  #not a copy!
		while contracts:
			contract = contracts.pop()  #removes contract from list
			assert (contract.seller is fund)  #for simplicity, fund provides all capital services
			rents_paid += contract.payment()   #firm rents capital from fund
		#currently *MUST* distribute gains *BEFORE* other income rcd
		# (this buys convenience of single contract with Fund instead of contracts w each indiv)
		fund.distribute_gains()  #fund distributes gains to fund accounts
		script_logger.debug( "Number of labor contracts: %d."%(len(repfirm.contracts['labor'])) )
		wages_paid = 0
		contracts = repfirm.contracts['labor']  #not a copy!
		while contracts:
			contract = contracts.pop()  #removes contract
			assert isinstance(contract.seller, Indiv) # ugly
			wages_paid += contract.payment()  #payment fulfills contract
		if self.params.DEBUG:
			script_logger.debug("Production: %10.2f"%(inventory))
			script_logger.debug("Factor payments: %10.2f (= %10.2f + %10.2f)"%(rents_paid+wages_paid, rents_paid, wages_paid))
			script_logger.debug("Remaining inventory: %10.2f"%(repfirm.inventory))
		assert fmath.feq(repfirm.inventory, 0, 1e-6*inventory)
		#reset inventory to prevent accumulation of small differences
		repfirm.inventory = 0
Esempio n. 19
0
 def test_falsi(self):
     x4zero = random.randrange(20)
     f = lambda x: (x - x4zero)**3
     result1 = simplest_falseposition(f, x4zero - 1.0, x4zero + 1.0)
     print("testvals", result1)
     self.assertTrue(fmath.feq(result1, x4zero, 1e-8))
Esempio n. 20
0
 def test_math(self):
     print
     print fmath.get_float_radix()
     print fmath.get_machine_precision()
     print fmath.get_default_numerical_precision()
     print fmath.feq(1, 2), fmath.feq(1e-9, 1e-10), fmath.feq(1e-16, 1e-17)
Esempio n. 21
0
	def test_falsi(self):
		x4zero = random.randrange(20)
		f = lambda x: (x-x4zero)**3
		result1 = simplest_falseposition(f,   x4zero - 1.0, x4zero+1.0)
		print("testvals", result1)
		self.assertTrue(fmath.feq(result1, x4zero, 1e-8))
Esempio n. 22
0
 def test_math(self):
     print
     print fmath.get_float_radix()
     print fmath.get_machine_precision()
     print fmath.get_default_numerical_precision()
     print fmath.feq(1, 2), fmath.feq(1e-9, 1e-10), fmath.feq(1e-16, 1e-17)