本文整理汇总了Python中market.Market.rationing_proportional方法的典型用法代码示例。如果您正苦于以下问题:Python Market.rationing_proportional方法的具体用法?Python Market.rationing_proportional怎么用?Python Market.rationing_proportional使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类market.Market
的用法示例。
在下文中一共展示了Market.rationing_proportional方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: consume_rationed
# 需要导入模块: from market import Market [as 别名]
# 或者: from market.Market import rationing_proportional [as 别名]
def consume_rationed(self, environment, time):
# We want the consumption to be done in random pairs
# We use rationing from market clearing class to do that
# Price is static for this example, otherwise we can't use rationing
# and need some other market clearing
price = 10.0
environment.variable_parameters["price_of_goods"] = price
# We need a list of agents and their demand or supply
# Supply is denoted with positive float, demand with negative float
for_rationing = []
# Firms give us their supply, we assume that since the goods are
# perishable their supply is all they have in stock
from src.helper import Helper
helper = Helper()
for firm in environment.firms:
# Firms produce based on their capital, for generality
# we use their net capital, as in their capital stock
# minus the capital owned of other agents
capital = 0.0
for tranx in firm.accounts:
# This is own capital stock
if tranx.type_ == "capital" and tranx.from_ == firm:
capital = capital + tranx.amount
# And here is the ownership of other agents' stock
if tranx.type_ == "capital" and tranx.to == firm:
capital = capital - tranx.amount
# We find the amount produced through the Cobb-Douglas function
amount = (
helper.cobb_douglas(
firm.get_account("labour"),
capital,
firm.total_factor_productivity,
firm.labour_elasticity,
firm.capital_elasticity,
)
* price
)
# And assume firm wants to sell whole production given the perishable nature of the goods
for_rationing.append([firm, amount])
# Households give use their demand, we assume that they want to
# consume the part of their wealth (cash and deposits) that they
# do not want to save (determined through propensity to save)
# We denote demand in units of the goods, so we divide the cash
# households want to spend by price to get the demand
for household in environment.households:
demand = 0.0
wealth = 0.0
# For generality we calculate net wealth for this, that is the
# amount of deposits they carry minus the amount of loans
for tranx in household.accounts:
if tranx.type_ == "deposits" and tranx.from_ == household:
wealth = wealth + tranx.amount
if tranx.type_ == "loans" and tranx.to == household:
wealth = wealth - tranx.amount
# Then the demand is determined by the agent's propensity to save
# and the wealth calculated above
demand = -((wealth * (1 - household.propensity_to_save)) / price)
for_rationing.append([household, demand])
# We import the market clearing class
from market import Market
# Put the appropriate settings, i.e.
# tolerance of error, resolution of search
# and amplification for exponential search
# This does not matter for rationing
# But in principle we need to initialize
# with these values
market = Market("market")
# And we find the rationing, ie the amounts
# of goods sold between pairs of agents
# We find the actual trades
rationed = market.rationing_proportional(for_rationing)
# Then we go through the rationing
# and move the goods and cash appropriately
for ration in rationed:
#
# A (from) L (to)
# bank loan deposit
# household goods loan
# firm deposit goods
#
# TODO: in the new version this may be irrelevant
environment.new_transaction("goods", "", ration[1].identifier, ration[0].identifier, ration[2], 0, 0, -1)
# The below makes sure the allocations of loans are correct
# That is the banks don't allow overdraft for buying
# consumption goods by the households
to_finance = ration[2] * price
itrange = list(range(0, len(environment.banks)))
# And randomise this list for the purposes of iterating randomly
random.shuffle(itrange)
# And we iterate over the agents randomly by proxy of iterating
# through their places on the list [agents]
for i in itrange:
current_bank = self.environment.banks[i]
# We find how much in deposits the household has
deposits_available = 0.0
for tranx in ration[1].accounts:
if tranx.type_ == "deposits" and tranx.to == current_bank:
deposits_available = deposits_available + tranx.amount
#.........这里部分代码省略.........
示例2: capitalise
# 需要导入模块: from market import Market [as 别名]
# 或者: from market.Market import rationing_proportional [as 别名]
def capitalise(self, environment, time):
# We will ration the remaining excess deposits
# and loan as capital ownership transfers
# to balance books, if books don't need to be
# balanced the same would work strictly on deposits
# and loans with no capital explicitly
# First resolve capital shortfall for firms
# ie when firm needs to sell existing capital instead of getting new owners
for firm in environment.firms:
# We calculate how much capital the firm has
capital = 0.0
for tranx in firm.accounts:
if tranx.type_ == "capital":
if tranx.from_ == firm:
capital = capital + tranx.amount
if tranx.to == firm:
capital = capital - tranx.amount
# Then find the firm's supply of capital given current books
supply = -capital - firm.get_account("deposits") + firm.get_account("loans")
# If there is a shortfall of capital supply
if supply < 0.0:
# We go through the books
for tranx in firm.accounts:
# And find capital transactions
if tranx.type_ == "capital" and tranx.from_ == firm:
# Then we sell the appropriate amount to cover the shortfall
# TODO: we may want the sellout to be proportional or at least
# going through books at random, though in the current model it shouldn't matter
to_remove = min(-supply, tranx.amount)
tranx.amount = tranx.amount - to_remove
supply = supply + to_remove
# First, we create the list that will be used for rationing
# method from Market class, containing agents and their
# excess supply or demand
for_rationing = []
# First we find household's demand for buying capital of the firms
for household in environment.households:
# We calculate the demand as the amount of wealth (deposits-loans) minus previously owned capital
# We calculate capital by hand in case there is some reverse ownership
deposits = 0.0
loans = 0.0
capital = 0.0
for tranx in household.accounts:
if tranx.type_ == "deposits":
if tranx.from_ == household:
deposits = deposits + tranx.amount
if tranx.type_ == "loans":
if tranx.to == household:
loans = loans + tranx.amount
if tranx.type_ == "capital":
if tranx.to == household:
capital = capital + tranx.amount
if tranx.from_ == household:
capital = capital - tranx.amount
# demand = household.get_account("deposits") - household.get_account("loans") - household.get_account("capital")
demand = deposits - loans - capital
# And we add the household together with its demand to the list
for_rationing.append([household, -demand])
for firm in environment.firms:
# Supply of the firms is the opposite of the demand of the household
# that is the loans minus issued capital claims minus deposits
# We calculate capital by hand in case there is some reverse ownership
capital = 0.0
for tranx in firm.accounts:
if tranx.type_ == "capital":
if tranx.from_ == firm:
capital = capital + tranx.amount
if tranx.to == firm:
capital = capital - tranx.amount
supply = -capital - firm.get_account("deposits") + firm.get_account("loans")
# supply = -firm.get_account("capital") - firm.get_account("deposits") + firm.get_account("loans")
# And we add the firm together with its supply to the list
for_rationing.append([firm, supply])
# We initialise the market clearing class
from market import Market
market = Market("market")
# We find the pairs of capital ownership transfers
# We move the capital proportionately with respect to demand
rationed = market.rationing_proportional(for_rationing)
# We add these to the books
for ration in rationed:
environment.new_transaction("capital", "", ration[0].identifier, ration[1].identifier, ration[2], 0, 0, -1)
# And print it to the screen for easy greping
print("%s sold %f worth of capital to %s at time %d.") % (
ration[0].identifier,
ration[2],
ration[1].identifier,
time,
)
# And net the capital transactions, so we don't accumulate
# them over the course of the transaction
#.........这里部分代码省略.........
示例3: sell_labour
# 需要导入模块: from market import Market [as 别名]
# 或者: from market.Market import rationing_proportional [as 别名]
def sell_labour(self, environment, time):
# First we find the market equilibrium price
# Important to note that this currently does
# not depend on the wealth of the buyers
# That is their demand may be higher than
# what they can actually buy, which may be ok
# We set the values necessary for tatonnement
# The list of sellers and their supply functions
sellers = []
for agent in environment.households:
sellers.append([agent, agent.supply_of_labour_solow])
# And the list of buyers and their demand functions
buyers = []
for agent in environment.firms:
buyers.append([agent, agent.demand_for_labour_solow])
# We may start the search for price at some specific point
# Here we pass 0, which means it'll start looking at a
# random point between 0 and 10
starting_price = 10.0
# We initialize the price
price = 0.0
# Import market clearing class
from market import Market
# Put the appropriate settings, i.e. desired identifier
market = Market("market")
# And we find the market price of labour
# given supply and demand of the agents
# and tolerance of error, resolution of search
# and amplification factor for exponential search
price = market.tatonnement(sellers, buyers, starting_price, 0.001, 0.01, 1.1)
environment.variable_parameters["price_of_labour"] = price
# now we use rationing to find the actual transactions between agents
for_rationing = []
for household in environment.households:
for_rationing.append([household, household.supply_of_labour_solow(price)])
for firm in environment.firms:
for_rationing.append([firm, -firm.demand_for_labour_solow(price)])
# And we find the rationing, ie the amounts
# of goods sold between pairs of agents
rationed = market.rationing_proportional(for_rationing)
#
# A (from) L (to)
# bank loan deposit
# household deposit labour
# firm labour loan
#
for ration in rationed:
# The labour is an asset (production factor) for the firm
# and a liability (promise to work) for the household
environment.new_transaction("labour", "", ration[1].identifier, ration[0].identifier, ration[2], 0, 0, -1)
random_bank = random.choice(environment.banks)
# Deposit is a liability of the bank
# and an asset of the household
environment.new_transaction(
"deposits",
"",
ration[0].identifier,
random_bank.identifier,
ration[2] * price,
random_bank.interest_rate_deposits,
0,
-1,
)
# Loan is an asset of the bank
# and a liability of the firm
environment.new_transaction(
"loans",
"",
random_bank.identifier,
ration[1].identifier,
ration[2] * price,
random_bank.interest_rate_loans,
0,
-1,
)
# We print the action of selling to the screen
print("%s sold %d units of labour at a price %f to %s at time %d.") % (
ration[0].identifier,
ration[2],
price,
ration[1].identifier,
time,
)
logging.info(" labour sold to firms on step: %s", time)