Skip to content

Commit

Permalink
fix bug where if a variable existed for a period after the current pe…
Browse files Browse the repository at this point in the history
…riod, uprating would cause the variable to become nan
  • Loading branch information
CalebPena committed Aug 6, 2024
1 parent 85f18fc commit 8cc0c61
Showing 1 changed file with 32 additions and 33 deletions.
65 changes: 32 additions & 33 deletions policyengine_core/simulations/simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -651,43 +651,42 @@ 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,
)
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
)
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
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 8cc0c61

Please sign in to comment.