Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 5 additions & 17 deletions sharkfin/population.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ class AgentPopulation:
dollars_per_hark_money_unit: float = 1500

def __post_init__(self):

self.time_var = self.agent_class.time_vary
self.time_inv = self.agent_class.time_inv

Expand All @@ -45,12 +44,10 @@ def __post_init__(self):
self.stored_class_stats = None

def infer_counts(self):

param_dict = self.parameter_dict

# if agent_clas_count is not specified, infer from parameters
if self.agent_class_count is None:

agent_class_count = 1
for key_param in param_dict:
parameter = param_dict[key_param]
Expand All @@ -63,7 +60,6 @@ def infer_counts(self):
self.agent_class_count = agent_class_count

if self.t_age is None:

t_age = 1
for key_param in param_dict:
parameter = param_dict[key_param]
Expand All @@ -79,7 +75,6 @@ def infer_counts(self):
# return t_age and agent_class_count

def approx_distributions(self, approx_params: dict):

param_dict = self.parameter_dict

self.continuous_distributions = {}
Expand Down Expand Up @@ -111,7 +106,6 @@ def approx_distributions(self, approx_params: dict):
self.infer_counts()

def parse_params(self):

param_dict = self.parameter_dict

agent_dicts = [] # container for dictionaries of each agent subgroup
Expand Down Expand Up @@ -227,7 +221,6 @@ def class_stats(self, store=False):
return cs

def create_distributed_agents(self):

rng = self.rng if self.rng is not None else np.random.default_rng()

self.agents = [
Expand All @@ -236,7 +229,6 @@ def create_distributed_agents(self):
]

def create_database(self):

database = pd.DataFrame(self.agent_dicts)
database["agents"] = self.agents

Expand All @@ -249,7 +241,6 @@ def solve_distributed_agents(self):
agent.solve()

def unpack_solutions(self):

self.solution = [agent.solution for agent in self.agents]

def init_simulation(self, T_sim=1000):
Expand All @@ -262,9 +253,12 @@ def init_simulation(self, T_sim=1000):
agent.initialize_sim()

if self.stored_class_stats is None:

## build an IndShockConsumerType "double" of this agent, with the same parameters
ind_shock_double = cism.IndShockConsumerType(**agent.parameters)
parameters = agent.parameters.copy()
if "Rfree" in parameters and isinstance(parameters["Rfree"], list):
# patch potential bug until HARK is updated
parameters["Rfree"] = parameters["Rfree"][0]
ind_shock_double = cism.IndShockConsumerType(**parameters)

## solve to get the mNrmStE value
## that is, the Steady-state Equilibrium value of mNrm, for the IndShockModel
Expand All @@ -290,7 +284,6 @@ def init_simulation(self, T_sim=1000):
agent.state_now["aLvl"] = agent.state_now["aNrm"] * agent.state_now["pLvl"]

def solve(self, merge_by=None):

self.solve_distributed_agents()

self.solution = AgentPopulationSolution(self)
Expand Down Expand Up @@ -533,17 +526,14 @@ def __init__(self, agent_population):
self.agent_database = self.agent_population.agent_database

def merge_solutions(self, continuous_states):

if continuous_states is None or continuous_states == []:

if self.dist_params is None or self.dist_params == []:
self.solution_database = self.agent_database
else:
self.solution_database = self.agent_database.set_index(self.dist_params)
self.ex_ante_hetero_params = []

else:

# check that continous states are in heterogeneous parameters
for state in continuous_states:
if state not in self.dist_params:
Expand All @@ -557,7 +547,6 @@ def merge_solutions(self, continuous_states):
self._merge_solutions_3d(continuous_states)

def _merge_solutions_2d(self, continuous_states):

discrete_params = list(set(self.dist_params) - set(continuous_states))
discrete_params.sort()

Expand Down Expand Up @@ -620,7 +609,6 @@ def _merge_solutions_2d(self, continuous_states):
self.solution_database = self.solution_database.set_index(discrete_params)

def _merge_solutions_3d(self, continuous_states):

discrete_params = list(set(self.dist_params) - set(continuous_states))
discrete_params.sort()

Expand Down
32 changes: 19 additions & 13 deletions simulate/parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@

from sharkfin.population import AgentPopulation

def build_population(agent_type, parameters, rng = None, dphm = 1500):
pop = AgentPopulation(agent_type(), parameters, rng = rng, dollars_per_hark_money_unit = dphm)
if 'approx_params' in parameters:
pop.approx_distributions(parameters['approx_params'])

def build_population(agent_type, parameters, rng=None, dphm=1500):
pop = AgentPopulation(
agent_type(), parameters, rng=rng, dollars_per_hark_money_unit=dphm
)
if "approx_params" in parameters:
pop.approx_distributions(parameters["approx_params"])
pop.parse_params()

pop.create_distributed_agents()
Expand Down Expand Up @@ -60,9 +63,11 @@ def build_population(agent_type, parameters, rng = None, dphm = 1500):

### Configuring the agent population

whiteshark_parameter_dict = whiteshark_agent_population_params | whiteshark_continuous_dist_params
whiteshark_parameter_dict['approx_params'] = whiteshark_approx_params
whiteshark_parameter_dict['ex_post'] = ["RiskyAvg", "RiskyStd"]
whiteshark_parameter_dict = (
whiteshark_agent_population_params | whiteshark_continuous_dist_params
)
whiteshark_parameter_dict["approx_params"] = whiteshark_approx_params
whiteshark_parameter_dict["ex_post"] = ["RiskyAvg", "RiskyStd"]
whiteshark_parameter_dict["AgentCount"] = 1

WHITESHARK = whiteshark_parameter_dict
Expand All @@ -76,23 +81,24 @@ def build_population(agent_type, parameters, rng = None, dphm = 1500):
### TODO: Population generators that take parameters like CRRA, DisCFac

lucas0_agent_population_params = {
"cycles": 0, # issue 186
"aNrmInitStd": 0.0,
"LivPrb": 0.98**0.25,
"PermGroFac": 1.0,
"pLvlInitMean": 0.0, ## Intention: Shut down income. But this might not do it.
"pLvlInitMean": 0.0, ## Intention: Shut down income. But this might not do it.
"pLvlInitStd": 0.0,
"Rfree": 1.0,
# Scaling from annual to quarterly
"TranShkStd": [0],
"PermShkStd": [0],
### These are placeholders that will be set when the system is set up.
"CRRA" : 5,
"DiscFac" : 0.96,
"ex_post" : None # ex post heterogeneous parameters over which to merge solutions
"CRRA": 5,
"DiscFac": 0.96,
"ex_post": None, # ex post heterogeneous parameters over which to merge solutions
}

lucas0_parameter_dict = lucas0_agent_population_params
lucas0_parameter_dict["AgentCount"] = 10 # TODO: What should this be?
lucas0_parameter_dict["AgentCount"] = 10 # TODO: What should this be?


LUCAS0 = lucas0_parameter_dict
LUCAS0 = lucas0_parameter_dict