Skip to content

Commit

Permalink
Fix invalid value error in weekly_hours_worked file by using np.divide (
Browse files Browse the repository at this point in the history
#5505)

* Fix invalid value error in weekly_hours_workd file by using np.divide with out and where parameters

* Made some changes in week hours worked file

* Made small changes in changelog entry yaml file

* made small changes in changelog entry yaml file and weekly hours worked py file

* Added comment in weekly hours worked file

* Made change on line 38 of the weekly_hours_worked file.

* Added unit tests for the weekly_hours_worked file.

* Fixed issue 4890

* Added a unit test for the weekly_hours_worked.py file and resolved issue #4890.

* Made changes in unit test weekly_hours_worked.yaml file
  • Loading branch information
SonaliBedge authored Jan 27, 2025
1 parent cd7aa1c commit 3703022
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 6 deletions.
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

0 comments on commit 3703022

Please sign in to comment.