From ad6e7ffa526f56dae7b2e5f6c1a044ea420206c1 Mon Sep 17 00:00:00 2001 From: Sonali Bedge Date: Thu, 23 Jan 2025 09:42:20 -0800 Subject: [PATCH 01/10] Fix invalid value error in weekly_hours_workd file by using np.divide with out and where parameters --- changelog_entry.yaml | 4 ++++ .../income/person/weekly_hours_worked.py | 16 +++++++++++----- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/changelog_entry.yaml b/changelog_entry.yaml index e69de29bb2d..29c03c33966 100644 --- a/changelog_entry.yaml +++ b/changelog_entry.yaml @@ -0,0 +1,4 @@ +- bump: patch + changes: + fixed: + - Invalid value encountered when dividing income_effect by original_earnings in weekly hours worked calculation diff --git a/policyengine_us/variables/household/income/person/weekly_hours_worked.py b/policyengine_us/variables/household/income/person/weekly_hours_worked.py index 158cfe5f2a0..f8c5b838288 100644 --- a/policyengine_us/variables/household/income/person/weekly_hours_worked.py +++ b/policyengine_us/variables/household/income/person/weekly_hours_worked.py @@ -39,15 +39,21 @@ def formula(person, period, parameters): 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, + out=np.zeros_like( + income_effect, dtype=np.float32 + ), # Default to 0 where division isn't valid + where=original_earnings + != 0, # Perform division only where original_earnings != 0 ) + return original * lsr_relative_change -class weekly_hours_worked_behavioural_response_substitution_elasticity( - Variable -): +class weekly_hours_worked_behavioural_response_substitution_elasticity(Variable): value_type = float entity = Person label = "behavioural response in weekly hours worked (substitution effect)" From a000c8e987a5fa39ec3ed35756b31401618f7535 Mon Sep 17 00:00:00 2001 From: Sonali Bedge Date: Thu, 23 Jan 2025 10:00:26 -0800 Subject: [PATCH 02/10] Made some changes in week hours worked file --- .../household/income/person/weekly_hours_worked.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/policyengine_us/variables/household/income/person/weekly_hours_worked.py b/policyengine_us/variables/household/income/person/weekly_hours_worked.py index f8c5b838288..39032ba11a8 100644 --- a/policyengine_us/variables/household/income/person/weekly_hours_worked.py +++ b/policyengine_us/variables/household/income/person/weekly_hours_worked.py @@ -43,11 +43,8 @@ def formula(person, period, parameters): lsr_relative_change = np.divide( income_effect, original_earnings, - out=np.zeros_like( - income_effect, dtype=np.float32 - ), # Default to 0 where division isn't valid - where=original_earnings - != 0, # Perform division only where original_earnings != 0 + out=np.zeros_like(income_effect, dtype=np.float32), + where=original_earnings != 0, ) return original * lsr_relative_change From a9711b5fdf837de4a21b49de744b204f7e942545 Mon Sep 17 00:00:00 2001 From: Sonali Bedge Date: Thu, 23 Jan 2025 10:28:47 -0800 Subject: [PATCH 03/10] Made small changes in changelog entry yaml file --- changelog_entry.yaml | 2 +- .../variables/household/income/person/weekly_hours_worked.py | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/changelog_entry.yaml b/changelog_entry.yaml index 29c03c33966..15d091fc8b7 100644 --- a/changelog_entry.yaml +++ b/changelog_entry.yaml @@ -1,4 +1,4 @@ - bump: patch changes: fixed: - - Invalid value encountered when dividing income_effect by original_earnings in weekly hours worked calculation + - Invalid value encountered when dividing income_effect by original_earnings in weekly hours worked calculation \ No newline at end of file diff --git a/policyengine_us/variables/household/income/person/weekly_hours_worked.py b/policyengine_us/variables/household/income/person/weekly_hours_worked.py index 39032ba11a8..7f23aa1c230 100644 --- a/policyengine_us/variables/household/income/person/weekly_hours_worked.py +++ b/policyengine_us/variables/household/income/person/weekly_hours_worked.py @@ -39,7 +39,6 @@ def formula(person, period, parameters): 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.divide( income_effect, original_earnings, From d3df1db75838d930f6dbbcb9c55b0dff60c453db Mon Sep 17 00:00:00 2001 From: Sonali Bedge Date: Thu, 23 Jan 2025 10:32:56 -0800 Subject: [PATCH 04/10] made small changes in changelog entry yaml file and weekly hours worked py file --- changelog_entry.yaml | 2 +- .../variables/household/income/person/weekly_hours_worked.py | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/changelog_entry.yaml b/changelog_entry.yaml index 15d091fc8b7..52bb04f2a9f 100644 --- a/changelog_entry.yaml +++ b/changelog_entry.yaml @@ -1,4 +1,4 @@ - bump: patch changes: fixed: - - Invalid value encountered when dividing income_effect by original_earnings in weekly hours worked calculation \ No newline at end of file + - Invalid value encountered when dividing income_effect by original_earnings in weekly hours worked calculation diff --git a/policyengine_us/variables/household/income/person/weekly_hours_worked.py b/policyengine_us/variables/household/income/person/weekly_hours_worked.py index 7f23aa1c230..0a75d90a364 100644 --- a/policyengine_us/variables/household/income/person/weekly_hours_worked.py +++ b/policyengine_us/variables/household/income/person/weekly_hours_worked.py @@ -49,7 +49,9 @@ def formula(person, period, parameters): return original * lsr_relative_change -class weekly_hours_worked_behavioural_response_substitution_elasticity(Variable): +class weekly_hours_worked_behavioural_response_substitution_elasticity( + Variable +): value_type = float entity = Person label = "behavioural response in weekly hours worked (substitution effect)" From b931c11c54e3144f1d86bdc5cdb1ad9e4ec191d2 Mon Sep 17 00:00:00 2001 From: Sonali Bedge Date: Thu, 23 Jan 2025 10:46:30 -0800 Subject: [PATCH 05/10] Added comment in weekly hours worked file --- .../variables/household/income/person/weekly_hours_worked.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/policyengine_us/variables/household/income/person/weekly_hours_worked.py b/policyengine_us/variables/household/income/person/weekly_hours_worked.py index 0a75d90a364..1446a5559d6 100644 --- a/policyengine_us/variables/household/income/person/weekly_hours_worked.py +++ b/policyengine_us/variables/household/income/person/weekly_hours_worked.py @@ -42,10 +42,10 @@ def formula(person, period, parameters): 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 From da20b3122081cdb343cc03f46405d0017f8129e8 Mon Sep 17 00:00:00 2001 From: Sonali Bedge Date: Thu, 23 Jan 2025 11:30:41 -0800 Subject: [PATCH 06/10] Made change on line 38 of the weekly_hours_worked file. --- .../variables/household/income/person/weekly_hours_worked.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/policyengine_us/variables/household/income/person/weekly_hours_worked.py b/policyengine_us/variables/household/income/person/weekly_hours_worked.py index 1446a5559d6..33613bedf95 100644 --- a/policyengine_us/variables/household/income/person/weekly_hours_worked.py +++ b/policyengine_us/variables/household/income/person/weekly_hours_worked.py @@ -35,7 +35,7 @@ def formula(person, period, parameters): 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 From d42938f2774c2b3ba370dd513d6f29bb84a10078 Mon Sep 17 00:00:00 2001 From: Sonali Bedge Date: Sat, 25 Jan 2025 17:42:55 -0800 Subject: [PATCH 07/10] Added unit tests for the weekly_hours_worked file. --- .../income/person/weekly_hours_worked.yaml | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 policyengine_us/tests/policy/baseline/household/income/person/weekly_hours_worked.yaml diff --git a/policyengine_us/tests/policy/baseline/household/income/person/weekly_hours_worked.yaml b/policyengine_us/tests/policy/baseline/household/income/person/weekly_hours_worked.yaml new file mode 100644 index 00000000000..0d868abaf8f --- /dev/null +++ b/policyengine_us/tests/policy/baseline/household/income/person/weekly_hours_worked.yaml @@ -0,0 +1,13 @@ +- 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: + weekly_hours_worked_behavioural_response_income_elasticity: 0.1 * 40 / (100_000 + 50_000) * 40 + weekly_hours_worked_behavioural_response_substitution_elasticity: 0.2 * 40 / (100_000 + 50_000) * 40 + weekly_hours_worked_behavioural_response: (0.1 * 40 / (100_000 + 50_000) * 40) + (0.2 * 40 / (100_000 + 50_000) * 40) \ No newline at end of file From 42561ff0574a1e3c9be8fe418a4b9eee767ba792 Mon Sep 17 00:00:00 2001 From: Sonali Bedge Date: Sat, 25 Jan 2025 18:04:14 -0800 Subject: [PATCH 08/10] Fixed issue 4890 --- changelog_entry.yaml | 2 +- .../income/person/weekly_hours_worked.py | 16 ++++++++++------ 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/changelog_entry.yaml b/changelog_entry.yaml index 52bb04f2a9f..a02e2ee7614 100644 --- a/changelog_entry.yaml +++ b/changelog_entry.yaml @@ -1,4 +1,4 @@ - bump: patch changes: fixed: - - Invalid value encountered when dividing income_effect by original_earnings in weekly hours worked calculation + - Invalid value encountered when dividing income_effect by original_earnings and dividing substitution_effect by original_earnings in weekly hours worked calculation diff --git a/policyengine_us/variables/household/income/person/weekly_hours_worked.py b/policyengine_us/variables/household/income/person/weekly_hours_worked.py index 33613bedf95..58fe7b35a4d 100644 --- a/policyengine_us/variables/household/income/person/weekly_hours_worked.py +++ b/policyengine_us/variables/household/income/person/weekly_hours_worked.py @@ -49,9 +49,7 @@ def formula(person, period, parameters): return original * lsr_relative_change -class weekly_hours_worked_behavioural_response_substitution_elasticity( - Variable -): +class weekly_hours_worked_behavioural_response_substitution_elasticity(Variable): value_type = float entity = Person label = "behavioural response in weekly hours worked (substitution effect)" @@ -64,13 +62,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 From e2486a30f9f87708bab24d6caea20f0a306b7443 Mon Sep 17 00:00:00 2001 From: Sonali Bedge Date: Mon, 27 Jan 2025 12:22:30 -0800 Subject: [PATCH 09/10] Added a unit test for the weekly_hours_worked.py file and resolved issue #4890. --- .../income/person/weekly_hours_worked.yaml | 38 +++++++++++++++++-- .../income/person/weekly_hours_worked.py | 8 +++- 2 files changed, 42 insertions(+), 4 deletions(-) diff --git a/policyengine_us/tests/policy/baseline/household/income/person/weekly_hours_worked.yaml b/policyengine_us/tests/policy/baseline/household/income/person/weekly_hours_worked.yaml index 0d868abaf8f..e82a1f010d8 100644 --- a/policyengine_us/tests/policy/baseline/household/income/person/weekly_hours_worked.yaml +++ b/policyengine_us/tests/policy/baseline/household/income/person/weekly_hours_worked.yaml @@ -8,6 +8,38 @@ employment_income_before_lsr: 100_000 self_employment_income_before_lsr: 50_000 output: - weekly_hours_worked_behavioural_response_income_elasticity: 0.1 * 40 / (100_000 + 50_000) * 40 - weekly_hours_worked_behavioural_response_substitution_elasticity: 0.2 * 40 / (100_000 + 50_000) * 40 - weekly_hours_worked_behavioural_response: (0.1 * 40 / (100_000 + 50_000) * 40) + (0.2 * 40 / (100_000 + 50_000) * 40) \ No newline at end of file + # 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 diff --git a/policyengine_us/variables/household/income/person/weekly_hours_worked.py b/policyengine_us/variables/household/income/person/weekly_hours_worked.py index 58fe7b35a4d..23abf08a2f6 100644 --- a/policyengine_us/variables/household/income/person/weekly_hours_worked.py +++ b/policyengine_us/variables/household/income/person/weekly_hours_worked.py @@ -32,13 +32,16 @@ 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 = 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.divide( income_effect, original_earnings, @@ -46,10 +49,13 @@ def formula(person, period, parameters): out=np.zeros_like(income_effect, dtype=np.float32), where=original_earnings != 0, ) + return original * lsr_relative_change -class weekly_hours_worked_behavioural_response_substitution_elasticity(Variable): +class weekly_hours_worked_behavioural_response_substitution_elasticity( + Variable +): value_type = float entity = Person label = "behavioural response in weekly hours worked (substitution effect)" From a93aed05118c33985796a38ca2d1c3ed6399b325 Mon Sep 17 00:00:00 2001 From: Sonali Bedge Date: Mon, 27 Jan 2025 12:58:03 -0800 Subject: [PATCH 10/10] Made changes in unit test weekly_hours_worked.yaml file --- .../income/person/weekly_hours_worked.yaml | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/policyengine_us/tests/policy/baseline/household/income/person/weekly_hours_worked.yaml b/policyengine_us/tests/policy/baseline/household/income/person/weekly_hours_worked.yaml index e82a1f010d8..8c732e8e1f7 100644 --- a/policyengine_us/tests/policy/baseline/household/income/person/weekly_hours_worked.yaml +++ b/policyengine_us/tests/policy/baseline/household/income/person/weekly_hours_worked.yaml @@ -43,3 +43,17 @@ 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