#936

[BUG] Tornado analysis raises ZeroDivisionError for a zero baseline output

OpenBichengWangopened 7d ago

Affected Notebook/File

skills/custom_skills/creating-financial-models/sensitivity_analysis.py, SensitivityAnalyzer.tornado_analysis()

Bug Description

A model whose base output is zero cannot produce a tornado sensitivity table. The method computes the low and high outputs successfully, restores the variable, then raises when calculating impact_pct = abs(impact) / self.base_output * 100.

Zero is a valid output for the callable interface, for example a model at breakeven. The low/high outputs and absolute impact are still meaningful even when a relative percentage is undefined. Expected: return those results and represent or explicitly handle the undefined percentage, instead of aborting the whole table.

Steps to Reproduce

From the repository root at a162e0184e71b60290808a43961a6cbc8060123c, run this with NumPy and pandas installed:

import runpy

namespace = runpy.run_path(
    "skills/custom_skills/creating-financial-models/sensitivity_analysis.py"
)
model = {"value": 0.0}
analyzer = namespace["SensitivityAnalyzer"](model)
variables = {
    "value": {
        "base": 0.0,
        "low": -10.0,
        "high": 10.0,
        "update_func": lambda value: model.update(value=value),
    }
}
print(analyzer.tornado_analysis(variables, lambda: model["value"]))

The low/high outputs are -10 and 10, giving an absolute impact of 20, but no DataFrame is returned. Changing the base/model value to 100 (and the bounds to 90/110) returns the expected impact 20 and percentage 20, isolating the zero denominator.

Error Message

ZeroDivisionError: float division by zero

Environment

Reproduced offline on macOS 27.0.1 arm64, Python 3.11.17, NumPy 2.4.6 and pandas 3.0.6. No API calls are involved.

Would you be willing to submit a PR to fix this?

No, I'm just reporting.

Activity

0

No activity yet.