Skip to content

Commit

Permalink
Merge pull request #56 from mikhailsirenko/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
mikhailsirenko authored Aug 24, 2023
2 parents 24f66db + 39c6a56 commit fd195ce
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 24 deletions.
12 changes: 12 additions & 0 deletions unbreakable/analysis/analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,18 @@ def prepare_outcomes(results: tuple, add_policies: bool, add_uncertainties: bool
i += 1 # increase row index of the outcomes dataframe
outcomes = pd.DataFrame(outcomes, columns=columns)

numeric_columns = ['total_population',
'total_asset_loss', 'total_consumption_loss', 'tot_exposed_asset',
'tot_asset_surv', 'expected_loss_frac', 'n_affected_people',
'annual_average_consumption', 'poverty_line_adjusted', 'district_pml',
'n_poor_initial', 'n_poor_affected', 'n_new_poor',
'initial_poverty_gap', 'new_poverty_gap_initial', 'new_poverty_gap_all',
'annual_average_consumption_loss',
'annual_average_consumption_loss_pct', 'r', 'mean_recovery_rate',
'tot_wellbeing_loss']

outcomes[numeric_columns] = outcomes[numeric_columns].apply(pd.to_numeric)

# Rename a district
outcomes['district'].replace(
{'AnseLaRayeCanaries': 'Anse-La-Raye & Canaries'}, inplace=True)
Expand Down
34 changes: 21 additions & 13 deletions unbreakable/data/writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ def get_outcomes(households: pd.DataFrame, tot_exposed_asset: float, expected_lo
annual_average_consumption_loss, annual_average_consumption_loss_pct = calculate_average_annual_consumption_loss(
affected_households, years_to_recover)

tot_wellbeing_loss = calc_tot_wellbeing_loss(households, consump_util)
tot_wellbeing_loss = calc_tot_wellbeing_loss(
affected_households, consump_util)
r = calculate_resilience(affected_households, tot_wellbeing_loss)

# Get the weighted average vulnerability by consumption quintile and decile
Expand Down Expand Up @@ -247,18 +248,18 @@ def calculate_resilience(affected_households: pd.DataFrame, tot_wellbeing_loss:
Returns:
float: Socio-economic resilience
'''
total_consumption_loss = (
affected_households[['consumption_loss_NPV', 'popwgt']].prod(axis=1)).sum()
# total_consumption_loss = (
# affected_households[['consumption_loss_NPV', 'popwgt']].prod(axis=1)).sum()

total_asset_damage = (
affected_households[['keff', 'v', 'popwgt']].prod(axis=1)).sum()

if total_consumption_loss == 0:
r = np.nan
# if total_consumption_loss == 0:
# r = np.nan

else:
# r = total_asset_damage / total_consumption_loss
r = total_asset_damage / tot_wellbeing_loss
# else:
# r = total_asset_damage / total_consumption_loss
r = total_asset_damage / tot_wellbeing_loss

return r

Expand Down Expand Up @@ -290,7 +291,7 @@ def get_weighted_vuln(affected_households: pd.DataFrame, quintile: bool) -> dict
return average_v_by_d.to_dict()


def calc_tot_wellbeing_loss(households: pd.DataFrame, consump_util: float) -> float:
def calc_tot_wellbeing_loss(affected_households: pd.DataFrame, consump_util: float) -> float:
'''Calculate wellbeing loss.
Args:
Expand All @@ -300,8 +301,15 @@ def calc_tot_wellbeing_loss(households: pd.DataFrame, consump_util: float) -> fl
Returns:
float: Total wellbeing loss.
'''
x = (households['aeexp'].multiply(households['popwgt'])
).sum() / households['popwgt'].sum()
x = (affected_households['aeexp'].multiply(affected_households['popwgt'])
).sum() / affected_households['popwgt'].sum()

W = x**(-consump_util)
wellbeing_loss = - households['wellbeing'].sum() / W
return wellbeing_loss

affected_households = affected_households.assign(
cons_equiv_wellbeing=-affected_households['wellbeing'] / W)

tol_wellbeing_loss = affected_households['cons_equiv_wellbeing'].multiply(
affected_households['popwgt']).sum()

return tol_wellbeing_loss
22 changes: 11 additions & 11 deletions unbreakable/modules/integrator.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,17 +235,17 @@ def calculate_wellbeing(households: pd.DataFrame, consump_util: float, discount_
< poverty_line_adjusted, 'weeks_pov'] += 1

# Integrate well-being
a = affected_households['c_t_unaffected']**(1 - consump_util)
b = (1 - consump_util) * dt
c = ((1 - ((affected_households['c_t_unaffected'] - affected_households['c_t']) / affected_households['c_t_unaffected'])
* np.e**(-affected_households['recovery_rate'] * _t))**(1 - consump_util) - 1)
d = np.e**(-discount_rate * _t)
e = a.div(b).mul(c).mul(d)
f = affected_households['c_t_unaffected']**(1 - consump_util)\
/ (1 - consump_util) * dt\
* ((1 - ((affected_households['c_t_unaffected'] - affected_households['c_t']) / affected_households['c_t_unaffected'])
* np.e**(-affected_households['recovery_rate'] * _t))**(1 - consump_util) - 1)\
* np.e**(-discount_rate * _t)
# a = affected_households['c_t_unaffected']**(1 - consump_util)
# b = (1 - consump_util) * dt
# c = ((1 - ((affected_households['c_t_unaffected'] - affected_households['c_t']) / affected_households['c_t_unaffected'])
# * np.e**(-affected_households['recovery_rate'] * _t))**(1 - consump_util) - 1)
# d = np.e**(-discount_rate * _t)
# e = a.div(b).mul(c).mul(d)
# f = affected_households['c_t_unaffected']**(1 - consump_util)\
# / (1 - consump_util) * dt\
# * ((1 - ((affected_households['c_t_unaffected'] - affected_households['c_t']) / affected_households['c_t_unaffected'])
# * np.e**(-affected_households['recovery_rate'] * _t))**(1 - consump_util) - 1)\
# * np.e**(-discount_rate * _t)

affected_households['wellbeing'] += affected_households['c_t_unaffected']**(1 - consump_util)\
/ (1 - consump_util) * dt\
Expand Down

0 comments on commit fd195ce

Please sign in to comment.