Skip to content

Commit

Permalink
Variable uprating caused nan (#235)
Browse files Browse the repository at this point in the history
* fix bug where if a variable existed for a period after the current period, uprating would cause the variable to become nan

* changelog entry
  • Loading branch information
CalebPena authored Aug 8, 2024
1 parent ad46383 commit 0deeb02
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 34 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:
- fixed bug where uprating was causing nan.
65 changes: 31 additions & 34 deletions policyengine_core/simulations/simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -668,43 +668,40 @@ def _calculate(
if array is None:
# Check if the variable has a previously defined value
known_periods = holder.get_known_periods()
if variable.uprating is not None and len(known_periods) > 0:
start_instants = [
str(known_period.start)
for known_period in known_periods
if known_period.unit == variable.definition_period
and known_period.start < period.start
start_instants = [
str(known_period.start)
for known_period in known_periods
if known_period.unit == variable.definition_period
and known_period.start < period.start
]
if variable.uprating is not None and len(start_instants) > 0:
latest_known_period = known_periods[
np.argmax(start_instants)
]
if len(start_instants) > 0:
latest_known_period = known_periods[
np.argmax(start_instants)
]
try:
uprating_parameter = get_parameter(
self.tax_benefit_system.parameters,
variable.uprating,
)
except:
raise ValueError(
f"Could not find uprating parameter {variable.uprating} when trying to uprate {variable_name}."
)
value_in_last_period = uprating_parameter(
latest_known_period.start
try:
uprating_parameter = get_parameter(
self.tax_benefit_system.parameters,
variable.uprating,
)
value_in_this_period = uprating_parameter(period.start)
if value_in_last_period == 0:
uprating_factor = 1
else:
uprating_factor = (
value_in_this_period / value_in_last_period
)

array = (
holder.get_array(
latest_known_period, self.branch_name
)
* uprating_factor
except:
raise ValueError(
f"Could not find uprating parameter {variable.uprating} when trying to uprate {variable_name}."
)
value_in_last_period = uprating_parameter(
latest_known_period.start
)
value_in_this_period = uprating_parameter(period.start)
if value_in_last_period == 0:
uprating_factor = 1
else:
uprating_factor = (
value_in_this_period / value_in_last_period
)

array = (
holder.get_array(latest_known_period, self.branch_name)
* uprating_factor
)
elif (
self.tax_benefit_system.auto_carry_over_input_variables
and variable.calculate_output is None
Expand Down

0 comments on commit 0deeb02

Please sign in to comment.