sbenthall / SHARKFin

Simulating Heterogeneous Agents with Finance
6 stars 5 forks source link

aNrmInitMean not being used in simulations with SharkPopulation #250

Closed sbenthall closed 1 year ago

sbenthall commented 1 year ago

I'm testing the starting and equilibrium normalized wealth levels with this script: https://github.com/sbenthall/SHARKFin/blob/master/macro/steady_state_mNrm.py

(There are many reasons for this, but one big reason is that my earlier 'target wealth' estimate didn't properly take into account more recent changes to the dividend rate. Also, if we have a high target wealth, we can reduce the DPHM parameter, which will reduce the inconvenient effects of labor income in the model.)

I now have this parameter dictionary:

{'cycles': 0,
 'aNrmInitStd': 0.0,
 'aNrmInitMean': 10,
 'LivPrb': 0.9949620563926881,
 'PermGroFac': 1.0,
 'pLvlInitMean': 0.0,
 'pLvlInitStd': 0.0,
 'Rfree': 1.0,
 'TranShkStd': [0],
 'PermShkStd': [0],
 'CRRA': 3,
 'DiscFac': 0.99,
 'ex_post': None,
 'AgentCount': 1,
 'num_per_type': 1000,
 'UnempPrb': 0.0,
 'RiskyAvg': 1.0175492133848216,
 'RiskyStd': 0.09283741339989003,
 'T_sim': 1500}

Then I go through the initialization of the SharkPopulation and run the simulation:

pop = SharkPopulation(
    SequentialPortfolioConsumerType,
    parameter_dict,
    dollars_per_hark_money_unit=1000,
)
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"]) 
pop.explode_agents(40)
for ag in pop.agent_database['agents']:
    ag.track_vars += ['aNrm', "TranShk"]

Now at this point I can examine an agent to get its parameters.

a0h = pop.agent_database['agents'][0]
a0h.parameters

I get this, which looks right: https://gist.github.com/sbenthall/b5569652e7464f44a7f327ee93c19a9e

In particular, see how I'm starting with aNrmInitMean as 10.

Then I run the simulation:

# initialize population model
pop.init_simulation()
pop.simulate()

And now I'm getting the mean aNrm for each time period, across all the agents:

history_aNrm = np.stack(pop.agent_database['agents'].map(lambda a: np.log(a.history['aNrm'])).values)
plt.plot(history_mNrm.mean(axis=0))
plt.xlabel("t - time")
plt.ylabel("mean log aNrm")
plt.show()

image

AND THIS IS NOT RIGHT.

I think these agents should be starting with mean log aNrm of 10! That's what aNrmInitMean does.

I think this is a bug. It is strange, because aNrmInitMean definitely has an effect on the results of the Sharkfin AttentionSimulation -- it has a strong effect on the output. But for some reason, it's not making it into the standalone simulation here.

sbenthall commented 1 year ago

I've tested a local run of the total Sharkfin simulation with a Mock Market with a high aNrmInitMean. What I notice is that the in this to, the agents start with close to 0 wealth:

image

Despite this parameter not appearing to work, I am finding that it does have an effect on the results we are getting from the big runs with AMMPS. I have been trying to get to the bottom of it for some time and am totally stumped.

alanlujan91 commented 1 year ago

So aNrmInitMean is being used correctly, however, the following line resets aNrm to zero for all agents

https://github.com/sbenthall/SHARKFin/blob/6e97f8355934047bd44f45b9341e41fbf2be8878/sharkfin/population.py#L163-L166

alanlujan91 commented 1 year ago

Additionally, even though all the agents have different seeds, I was noticing the same draw of aNrm for all agents before it was being reset...

sbenthall commented 1 year ago

Thank you! Huge help. I can fix this now...

So aNrmInitMean is being used correctly, however, the following line resets aNrm to zero for all agents

Oy. Ok, I see what happened. Hoisted on the petard of my own hacks.

I was noticing the same draw of aNrm for all agents before it was being reset...

I think that's fine because aNrmInitStd is 0.

alanlujan91 commented 1 year ago

Oh I didn't notice the std