Affected Notebook/File
skills/custom_skills/creating-financial-models/sensitivity_analysis.py, SensitivityAnalyzer.scenario_analysis()
Bug Description
Each scenario updates only the variables present in its dictionary, but the model is never reset between scenarios. A scenario that changes revenue therefore changes the revenue used by a later scenario that changes only margin. Reordering the same scenario definitions changes both their outputs and the reported expected value.
For a base model with revenue 1000 and margin 0.2, the independent scenarios below should produce 240 for "Revenue up", 100 for "Margin down", and an equally weighted expected value of 170. Instead, revenue-first produces an expected value of 180; margin-first produces 110. The model also remains at revenue 1200 and margin 0.1 after either call.
Expected: evaluate each scenario against the same starting model, or explicitly require complete scenario inputs and reject incomplete ones rather than silently inheriting inputs from a previous scenario.
Steps to Reproduce
At upstream a162e0184e71b60290808a43961a6cbc8060123c, with NumPy and pandas installed, run from the repository root:
import sys
from types import SimpleNamespace
sys.path.insert(0, "skills/custom_skills/creating-financial-models")
from sensitivity_analysis import SensitivityAnalyzer
def run(items):
model = SimpleNamespace(revenue=1000.0, margin=0.2)
table = SensitivityAnalyzer(model).scenario_analysis(
scenarios=dict(items),
variable_updates={
"revenue": lambda x: setattr(model, "revenue", x),
"margin": lambda x: setattr(model, "margin", x),
},
output_func=lambda: model.revenue * model.margin,
)
print(dict(zip(table["scenario"], table["output"])))
print(model)
revenue = ("Revenue up", {"revenue": 1200.0})
margin = ("Margin down", {"margin": 0.1})
run([revenue, margin])
run([margin, revenue])Error Message
There is no exception. Actual output:
{'Revenue up': 240.0, 'Margin down': 120.0, 'Expected Value': 180.0}
namespace(revenue=1200.0, margin=0.1)
{'Margin down': 100.0, 'Revenue up': 120.0, 'Expected Value': 110.0}
namespace(revenue=1200.0, margin=0.1)The loop contains a comment about resetting the model, but no reset implementation. Supplying both base values explicitly in each scenario avoids the contamination.
Environment
macOS 27.0.1 (arm64), Python 3.11.17, NumPy 2.4.6, pandas 3.0.6. Reproduced offline with cached dependencies and no API calls.
This report leaves the defect unfixed. It concerns SensitivityAnalyzer.scenario_analysis(), separate from #902's DCFModel.sensitivity_analysis() cache restoration, #904's one-way baseline calculation, and #931's breakeven solver.
Activity
0No activity yet.