sbenthall / SHARKFin

Simulating Heterogeneous Agents with Finance
6 stars 5 forks source link

All agents in the AgentPopulation have the same random seed #214

Closed sbenthall closed 1 year ago

sbenthall commented 1 year ago

When an AgentPopulation has N agents of the same type, all the generated AgentTypes have the same random seed.

So they all act the same.

I would expect them to have different random seeds

sbenthall commented 1 year ago

Will be fixed with #217 (using HARK.AgentPopulation as superclass of the Sharkfin version).

sbenthall commented 1 year ago

I don't think this issue is fixed.

I have prepared the following test script:

import numpy as np
from HARK.ConsumptionSaving.ConsPortfolioModel import SequentialPortfolioConsumerType
from sharkfin.population import SharkPopulation, SharkPopulationSolution
from simulate.parameters import WHITESHARK, LUCAS0

parameter_dict = LUCAS0.copy()

pop = SharkPopulation(
    SequentialPortfolioConsumerType,
    parameter_dict,
    dollars_per_hark_money_unit=1500,
)

if "approx_params" in parameter_dict:
    pop.approx_distributions(parameter_dict["approx_params"])
else:
    pop.continuous_distributions = {}
    pop.discrete_distributions = {}

pop.create_distributed_agents()
pop.create_database()
pop.solve_distributed_agents()

pop.solve(merge_by=parameter_dict["ex_post"])  # merge_by=["RiskyAvg", "RiskyStd"])

pop.explode_agents(100)

# initialize population model
pop.init_simulation()

pop.simulate()

assert pop.agent_database['agents'].map(lambda a: a.history['mNrm'][100]).std() > 0.00000001

What I get running this script is:

$ python steady_state_wealth.py 
Traceback (most recent call last):
  File "/home/sb/projects/ufm/SHARKFin/macro/steady_state_wealth.py", line 33, in <module>
    assert pop.agent_database['agents'].map(lambda a: a.history['mNrm'][100]).std() > 0.00000001
AssertionError

Indicating that the standard deviation of the normalized market resources at the 100th time step, across 100 agents, is negligible.

I believe this means that when the agents are exploded, they are all getting the same random seed. That would explain why they are getting identical shocks.

Since this was supposed to be fixed with #217, it may be that this exposes an error in the underlying HARK library.

alanlujan91 commented 1 year ago

Ah, I see what's the problem. create_distributed_agents does indeed assign different seeds. but explode_agents does not.

explode_agents is an artifact of having to produce many agents for dealing with the attention mechanism, so it is not part of the HARK library's AgentPopulation and only exists in SHARKFin.

I will make a PR to fix this.

sbenthall commented 1 year ago

Thanks @alanlujan91