def distributeParams(self, param_name, param_count, center, spread, dist_type): ''' Distributes heterogeneous values of one parameter to the AgentTypes in self.agents. Parameters ---------- param_name : string Name of the parameter to be assigned. param_count : int Number of different values the parameter will take on. center : float A measure of centrality for the distribution of the parameter. spread : float A measure of spread or diffusion for the distribution of the parameter. dist_type : string The type of distribution to be used. Can be "lognormal" or "uniform" (can expand). Returns ------- None ''' # Get a list of discrete values for the parameter if dist_type == 'uniform': # If uniform, center is middle of distribution, spread is distance to either edge param_dist = approxUniform(N=param_count, bot=center - spread, top=center + spread) elif dist_type == 'lognormal': # If lognormal, center is the mean and spread is the standard deviation (in log) tail_N = 3 param_dist = approxLognormal(N=param_count - tail_N, mu=np.log(center) - 0.5 * spread**2, sigma=spread, tail_N=tail_N, tail_bound=[0.0, 0.9], tail_order=np.e) # Distribute the parameters to the various types, assigning consecutive types the same # value if there are more types than values replication_factor = len(self.agents) // param_count # Note: the double division is intenger division in Python 3 and 2.7, this makes it explicit j = 0 b = 0 while j < len(self.agents): for n in range(replication_factor): self.agents[j](AgentCount=int(self.Population * param_dist[0][b] * self.TypeWeight[n])) exec('self.agents[j](' + param_name + '= param_dist[1][b])') j += 1 b += 1
# Make new dictionary mpc_dict = copy(dict_portfolio) # Make riskless return factor very low so that nobody invests in it. mpc_dict['Rfree'] = 0.01 # Make the agent less risk averse mpc_dict['CRRA'] = 2 # Do away with probability of death mpc_dict['LivPrb'] = [1] * dict_portfolio['T_cycle'] # Risky returns mu = 0.05 std = 0.1 # Construct the distribution approximation for integration RiskyDstnFunc = lambda count: approxLognormal(count, mu=mu, sigma=std) # Contruct function for drawing returns RiskyDrawFunc = lambda seed: drawLognormal(1, mu=mu, sigma=std, seed=seed) mpc_dict['approxRiskyDstn'] = RiskyDstnFunc mpc_dict['drawRiskyFunc'] = RiskyDrawFunc agent = cpm.PortfolioConsumerType(**mpc_dict) agent.cylces = 0 agent.solve() # %% Compute the theoretical MPC (for when there is no labor income) # First extract some parameter values that will be used crra = agent.CRRA sigma_r = std