Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix invalid value error in weekly_hours_worked file by using np.divide #5505

Merged
merged 10 commits into from
Jan 27, 2025
4 changes: 4 additions & 0 deletions changelog_entry.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
- bump: patch
changes:
fixed:
- Invalid value encountered when dividing income_effect by original_earnings and dividing substitution_effect by original_earnings in weekly hours worked calculation
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
- name: Weekly hours worked with labor supply response
period: 2022
input:
weekly_hours_worked_before_lsr: 40
labor_supply_behavioral_response: 1
income_elasticity_lsr: 0.1
substitution_elasticity_lsr: 0.2
employment_income_before_lsr: 100_000
self_employment_income_before_lsr: 50_000
output:
# Calculation steps:
# Total earnings: 100_000 + 50_000 = 150_000
# weekly_hours_worked_behavioural_response_income_elasticity = 40 * (0.1 / 150_000)
# = 40 * 6.6666667e-07
# = 2.6666667e-05
weekly_hours_worked_behavioural_response_income_elasticity: 2.6666667e-05

# weekly_hours_worked_behavioural_response_substitution_elasticity = 40 * (0.2 / 150_000)
# = 40 * 1.3333333e-06
# = 5.3333333e-05
weekly_hours_worked_behavioural_response_substitution_elasticity: 5.3333333e-05

# weekly_hours_worked_behavioural_response = 2.6666667e-05 + 5.3333333e-05
# = 8.0000000e-05
weekly_hours_worked_behavioural_response: 8.0000000e-05

- name: Weekly hours worked with zero earnings
period: 2022
input:
weekly_hours_worked_before_lsr: 40
labor_supply_behavioral_response: 1
income_elasticity_lsr: 0.1
substitution_elasticity_lsr: 0.2
employment_income_before_lsr: 0
self_employment_income_before_lsr: 0
output:
# Calculation steps:
# Total earnings: 0 + 0 = 0
# weekly_hours_worked_behavioural_response_income_elasticity = 0 (division by zero avoided in Python code)
weekly_hours_worked_behavioural_response_income_elasticity: 0

# weekly_hours_worked_behavioural_response_substitution_elasticity = 0 (division by zero avoided in Python code)
weekly_hours_worked_behavioural_response_substitution_elasticity: 0

# weekly_hours_worked_beha

- name: Weekly hours worked with zero labor supply response
period: 2022
input:
weekly_hours_worked_before_lsr: 40
labor_supply_behavioral_response: 0 # Trigger the condition for np.zeros_like(original)
income_elasticity_lsr: 0.1
substitution_elasticity_lsr: 0.2
employment_income_before_lsr: 100_000
self_employment_income_before_lsr: 50_000
output:
weekly_hours_worked_behavioural_response_income_elasticity: 0
weekly_hours_worked_behavioural_response_substitution_elasticity: 0
weekly_hours_worked_behavioural_response: 0
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,24 @@ class weekly_hours_worked_behavioural_response_income_elasticity(Variable):
def formula(person, period, parameters):
original = person("weekly_hours_worked_before_lsr", period)
lsr = person("labor_supply_behavioral_response", period)

if (lsr != 0).any():
income_effect = person("income_elasticity_lsr", period)
else:
income_effect = 0
income_effect = np.zeros_like(original)

original_emp = person("employment_income_before_lsr", period)
original_self_emp = person("self_employment_income_before_lsr", period)
original_earnings = original_emp + original_self_emp
lsr_relative_change = np.where(
original_earnings == 0, 0, income_effect / original_earnings

lsr_relative_change = np.divide(
income_effect,
original_earnings,
# Assign no LSR change to people with no original earnings to avoid dividing by zero.
out=np.zeros_like(income_effect, dtype=np.float32),
where=original_earnings != 0,
)

return original * lsr_relative_change


Expand All @@ -60,13 +68,19 @@ def formula(person, period, parameters):
if (lsr != 0).any():
substitution_effect = person("substitution_elasticity_lsr", period)
else:
substitution_effect = 0
substitution_effect = np.zeros_like(original)
original_emp = person("employment_income_before_lsr", period)
original_self_emp = person("self_employment_income_before_lsr", period)
original_earnings = original_emp + original_self_emp
lsr_relative_change = np.where(
original_earnings == 0, 0, substitution_effect / original_earnings

lsr_relative_change = np.divide(
substitution_effect,
original_earnings,
# Assign no LSR change to people with no original earnings to avoid dividing by zero.
out=np.zeros_like(substitution_effect, dtype=np.float32),
where=original_earnings != 0,
)

return original * lsr_relative_change


Expand Down
Loading