def get_aggregate_demand(self, p1, p2): """ Aggregate demand of the individual agents to the overall demand for each good in the economy. """ # Antibugging integrity_checks('get_aggregate_demand_in', p1, p2) # Distribute class attributes agent_objs = self.population # Initialize result container rslt = np.array([0.0, 0.0]) # Loop over all agents in the population. for agent_obj in agent_objs: agent_obj.choose(p1, p2) demand = agent_obj.get_individual_demand() rslt += demand # Type conversion rslt = rslt.tolist() # Quality Assurance integrity_checks('get_aggregate_demand_out', rslt) # Finishing return rslt
def get_aggregate_demand(self, p1, p2): """ Aggregate demand of the individual agents to the overall demand for each good in the economy. """ # Antibugging integrity_checks('get_aggregate_demand_in', p1, p2) # Distribute class attributes agent_objs = self.population # Loop over all agents in the population. demand_x1 = [] demand_x2 = [] for agent_obj in agent_objs: agent_obj.choose(p1, p2) demand_x1.append(agent_obj.get_individual_demand()[0]) demand_x2.append(agent_obj.get_individual_demand()[1]) # Record output rslt = { 'demand': [np.sum(demand_x1), np.sum(demand_x2)], 'sd': [np.std(demand_x1), np.std(demand_x2)] } # Quality Assurance integrity_checks('get_aggregate_demand_out', rslt) # Finishing return rslt
def get_aggregate_demand(self, p1, p2): """ Aggregate demand of the individual agents to the overall demand for each good in the economy. """ # Antibugging integrity_checks('get_aggregate_demand_in', p1, p2) # Distribute class attributes agent_objs = self.population # Loop over all agents in the population. demand_x1 = [] demand_x2 = [] for agent_obj in agent_objs: agent_obj.choose(p1, p2) demand_x1.append(agent_obj.get_individual_demand()[0]) demand_x2.append(agent_obj.get_individual_demand()[1]) # Record output rslt = {'demand': [np.sum(demand_x1), np.sum(demand_x2)], 'sd': [np.std(demand_x1), np.std(demand_x2)]} # Quality Assurance integrity_checks('get_aggregate_demand_out', rslt) # Finishing return rslt
def set_preference_parameter(self, alpha): """ Set the preference parameter. """ # Antibugging integrity_checks('set_preference_parameter', alpha) # Attach preference parameter as class attribute self.alpha = alpha
def set_endowment(self, y): """ Set the endowment. """ # Antibugging integrity_checks('set_endowment', y) # Attach endowment as class attribute self.y = y
def __init__(self, agent_objs): """ Initialize instance of economy class and attach the agent population as a class attribute. """ # Antibugging integrity_checks('__init__', agent_objs) # Attach initialization attributes self.population = agent_objs
def get_individual_demand(self): """ Get the agents demand for the goods. """ # Extract demand from class attributes rslt = self.x[:] # Quality Checks integrity_checks('get_individual_demand', rslt) # Finishing return rslt
def spending(x, p1, p2): """ Calculate spending level. """ # Antibugging integrity_checks('spending', x, p1, p2) # Distribute demands x1, x2 = x # Calculate expenses e = x1 * p1 + x2 * p2 # Finishing return e
def _criterion(self, x): """ Evaluate criterion function. """ # Antibugging integrity_checks('_criterion', x) # Ensure non-negativity of demand x = x**2 # Utility calculation u = self.get_utility(x) # Finishing return -u
def _criterion(self, x): """ Evaluate criterion function. """ # Antibugging integrity_checks('_criterion', x) # Ensure non-negativity of demand x = x ** 2 # Utility calculation u = self.get_utility(x) # Finishing return -u
def choose(self, p1, p2): """ Choose the desired bundle of goods for different agent decision rules. """ # Antibugging integrity_checks('choose', p1, p2) # Distribute class attributes y = self.y x = self._choose(y, p1, p2) # Update class attributes self.x = x
def _constraint(self, x, p1, p2): """ Non-negativity constraint for the SLSQP algorithm. """ # Antibugging integrity_checks('_constraint', x, p1, p2) # Distribute endowment y = self.y # Ensure non-negativity x = x ** 2 # Calculate savings cons = y - self.spending(x, p1, p2) # Finishing return cons
def _constraint(self, x, p1, p2): """ Non-negativity constraint for the SLSQP algorithm. """ # Antibugging integrity_checks('_constraint', x, p1, p2) # Distribute endowment y = self.y # Ensure non-negativity x = x**2 # Calculate savings cons = y - self.spending(x, p1, p2) # Finishing return cons
def _choose(self, y, p1, p2): """ Choose utility-maximizing bundle. """ # Antibugging integrity_checks('_choose_rational_in', y, p1, p2) # Determine starting values x0 = np.array([(0.5 * y) / p1, (0.5 * y) / p2]) # Construct budget constraint constraint_divergence = dict() constraint_divergence['type'] = 'eq' constraint_divergence['args'] = (p1, p2) constraint_divergence['fun'] = self._constraint constraints = [ constraint_divergence, ] # Call constraint-optimizer. Of course, we could determine the # optimal bundle directly, but I wanted to illustrate the use of # a constraint optimization algorithm to you. rslt = minimize(self._criterion, x0, method='SLSQP', constraints=constraints) # Check for convergence assert (rslt['success'] == True) # Transformation of result. x = rslt['x']**2 # Type conversion x = x.tolist() # Quality Checks integrity_checks('_choose_rational_out', x) # Finishing return x
def _choose(self, y, p1, p2): """ Choose utility-maximizing bundle. """ # Antibugging integrity_checks('_choose_rational_in', y, p1, p2) # Determine starting values x0 = np.array([(0.5 * y) / p1, (0.5 * y) / p2]) # Construct budget constraint constraint_divergence = dict() constraint_divergence['type'] = 'eq' constraint_divergence['args'] = (p1, p2) constraint_divergence['fun'] = self._constraint constraints = [constraint_divergence, ] # Call constraint-optimizer. Of course, we could determine the # optimal bundle directly, but I wanted to illustrate the use of # a constraint optimization algorithm to you. rslt = minimize(self._criterion, x0, method='SLSQP', constraints=constraints) # Check for convergence assert (rslt['success'] == True) # Transformation of result. x = rslt['x'] ** 2 # Type conversion x = x.tolist() # Quality Checks integrity_checks('_choose_rational_out', x) # Finishing return x
def _choose(self, y, p1, p2): """ Choose a random bundle on the budget line. """ # Antibugging integrity_checks('_choose_random_in', y, p1, p2) # Determine maximal consumption of good two max_two = y / p2 # Initialize result container x = [None, None] # Select random bundle x[1] = float(np.random.uniform(0, max_two)) x[0] = (y - x[1] * p2) / p1 # Quality Checks integrity_checks('_choose_random_out', x) # Finishing return x