Skip to content

Commit

Permalink
fix warnings from permshock_std history (and an error from class stat…
Browse files Browse the repository at this point in the history
…s tracking), fixes #134
  • Loading branch information
sbenthall committed Aug 18, 2022
1 parent f54a9ff commit 6774250
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 31 deletions.
6 changes: 3 additions & 3 deletions analysis/plot_and_save_history.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -374,9 +374,9 @@
],
"metadata": {
"kernelspec": {
"display_name": "hark",
"display_name": "ufm",
"language": "python",
"name": "hark"
"name": "ufm"
},
"language_info": {
"codemirror_mode": {
Expand All @@ -388,7 +388,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.4"
"version": "3.10.4"
}
},
"nbformat": 4,
Expand Down
57 changes: 29 additions & 28 deletions sharkfin/simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,20 +174,20 @@ def data(self):
data = None

data_dict = {
't': range(len(self.market.prices[self.burn_in + 1:])),
'prices': self.market.prices[self.burn_in + 1:],
'buy': [bs[0] for bs in self.broker.buy_sell_history][self.burn_in:],
'sell': [bs[1] for bs in self.broker.buy_sell_history][self.burn_in:],
'buy_macro': [bs[0] for bs in self.broker.buy_sell_macro_history][self.burn_in:],
'sell_macro': [bs[1] for bs in self.broker.buy_sell_macro_history][self.burn_in:],
't': range(len(self.market.prices[self.burn_in_val + 1:])),
'prices': self.market.prices[self.burn_in_val + 1:],
'buy': [bs[0] for bs in self.broker.buy_sell_history][self.burn_in_val:],
'sell': [bs[1] for bs in self.broker.buy_sell_history][self.burn_in_val:],
'buy_macro': [bs[0] for bs in self.broker.buy_sell_macro_history][self.burn_in_val:],
'sell_macro': [bs[1] for bs in self.broker.buy_sell_macro_history][self.burn_in_val:],
'owned': self.history['owned_shares'][1:],
'total_assets': self.history['total_assets'][1:],
'mean_income': self.history['mean_income_level'][1:],
'total_consumption': self.history['total_consumption_level'][1:],
'permshock_std': self.history['permshock_std'][1:],
'ror': self.market.ror_list()[self.burn_in:],
'expected_ror': self.fm.expected_ror_list[self.burn_in + 1:],
'expected_std': self.fm.expected_std_list[self.burn_in + 1:],
#'permshock_std': self.history['permshock_std'][1:],
'ror': self.market.ror_list()[self.burn_in_val:],
'expected_ror': self.fm.expected_ror_list[self.burn_in_val + 1:],
'expected_std': self.fm.expected_std_list[self.burn_in_val + 1:],
}

try:
Expand Down Expand Up @@ -268,7 +268,7 @@ def start_simulation(self, burn_in = None):
if burn_in is not None:
self.burn_in(burn_in)

self.burn_in = burn_in if burn_in is not None else 0
self.burn_in_val = burn_in if burn_in is not None else 0


def simulate(self, quarters=None, start=True, burn_in = None):
Expand Down Expand Up @@ -365,19 +365,20 @@ def track(self, day, time_delta = 0):
* self.pop.dollars_per_hark_money_unit
)

permshock_std = np.array(
[
agent.shocks['PermShk']
for agent in self.pop.agents
if 'PermShk' in agent.shocks
]
).std()
# Incomplete shock records before all agents reach macro-day, causing warnings.
#permshock_std = np.array(
# [
# agent.shocks['PermShk']
# for agent in self.pop.agents
# if 'PermShk' in agent.shocks
# ]
#).std()

self.history['owned_shares'].append(os)
self.history['total_assets'].append(tal)
self.history['mean_income_level'].append(mpl)
self.history['total_consumption_level'].append(tcl)
self.history['permshock_std'].append(permshock_std)
#self.history['permshock_std'].append(permshock_std)
self.history['class_stats'].append(self.pop.class_stats(store=False))
self.history['total_pop_stats'].append(self.pop.agent_data())
# self.history['buy_sell'].append(self.broker.buy_sell_history[-1])
Expand Down Expand Up @@ -432,7 +433,7 @@ def sim_stats(self):

def class_stat_column_to_dict(clabel):
df = self.history['class_stats'][-1][['label', clabel]]
df.columns = df.columns.droplevel(1)
#df.columns = df.columns.droplevel(1)

data= df.set_index('label').to_dict()[clabel]

Expand Down Expand Up @@ -697,14 +698,14 @@ def data(self):
data = None

data_dict = {
't': range(len(self.market.prices) - self.burn_in),
'prices': self.market.prices[self.burn_in:],
'dividends': self.market.dividends[self.burn_in:],
'buy': [None] + [bs[0] for bs in self.broker.buy_sell_history][self.burn_in:],
'sell': [None] + [bs[1] for bs in self.broker.buy_sell_history][self.burn_in:],
'ror': [None] + self.market.ror_list()[self.burn_in:],
'expected_ror': self.fm.expected_ror_list[self.burn_in:],
'expected_std': self.fm.expected_std_list[self.burn_in:],
't': range(len(self.market.prices) - self.burn_in_val),
'prices': self.market.prices[self.burn_in_val:],
'dividends': self.market.dividends[self.burn_in_val:],
'buy': [None] + [bs[0] for bs in self.broker.buy_sell_history][self.burn_in_val:],
'sell': [None] + [bs[1] for bs in self.broker.buy_sell_history][self.burn_in_val:],
'ror': [None] + self.market.ror_list()[self.burn_in_val:],
'expected_ror': self.fm.expected_ror_list[self.burn_in_val:],
'expected_std': self.fm.expected_std_list[self.burn_in_val:],
'market_times': self.history['run_times']
}

Expand Down

0 comments on commit 6774250

Please sign in to comment.