mollendorff-forge 10.0.0-beta.8

Battle-tested financial math for AI. 173 Excel-compatible functions validated against Gnumeric & R. MCP integration, Monte Carlo, Decision Trees, Real Options.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
//! Tornado Diagram Engine
//!
//! Performs one-at-a-time sensitivity analysis.

use super::config::{InputRange, TornadoConfig};
use crate::core::ArrayCalculator;
use crate::types::{ParsedModel, Variable};
use serde::{Deserialize, Serialize};

/// A single sensitivity bar in the tornado diagram
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SensitivityBar {
    /// Input variable name
    pub input_name: String,
    /// Output at low input value
    pub output_at_low: f64,
    /// Output at high input value
    pub output_at_high: f64,
    /// Total swing (high - low output)
    pub swing: f64,
    /// Absolute swing for sorting
    pub abs_swing: f64,
    /// Low input value used
    pub input_low: f64,
    /// High input value used
    pub input_high: f64,
}

impl SensitivityBar {
    /// Generate ASCII bar representation
    // Truncation is mathematically impossible: ratio is 0..=1, bar_width is small
    #[allow(
        clippy::cast_possible_truncation,
        clippy::cast_sign_loss,
        clippy::cast_precision_loss
    )]
    #[must_use]
    pub fn to_ascii(&self, max_swing: f64, bar_width: usize) -> String {
        let ratio = self.abs_swing / max_swing;
        let filled = (ratio * bar_width as f64) as usize;
        let bar: String = "".repeat(filled);
        format!(
            "{:<20} |{:<width$}| +/- ${:.0}",
            self.input_name,
            bar,
            self.abs_swing / 2.0,
            width = bar_width
        )
    }
}

/// Complete tornado analysis result
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TornadoResult {
    /// Output variable name
    pub output: String,
    /// Base case output value
    pub base_value: f64,
    /// Sensitivity bars (sorted by impact)
    pub bars: Vec<SensitivityBar>,
    /// Total variance explained
    pub total_variance: f64,
}

impl TornadoResult {
    /// Export results to YAML format
    #[must_use]
    pub fn to_yaml(&self) -> String {
        serde_yaml_ng::to_string(self).unwrap_or_else(|_| "# Error serializing results".to_string())
    }

    /// Export results to JSON format
    ///
    /// # Errors
    ///
    /// Returns an error if JSON serialization fails.
    pub fn to_json(&self) -> Result<String, serde_json::Error> {
        serde_json::to_string_pretty(self)
    }

    /// Generate ASCII tornado diagram
    pub fn to_ascii(&self) -> String {
        use std::fmt::Write;

        let mut output = String::new();

        let _ = write!(
            output,
            "{} Sensitivity (Base: ${:.0})\n\n",
            self.output, self.base_value
        );

        if self.bars.is_empty() {
            output.push_str("No sensitivity data\n");
            return output;
        }

        let max_swing = self
            .bars
            .iter()
            .map(|b| b.abs_swing)
            .fold(0.0_f64, f64::max);

        for bar in &self.bars {
            output.push_str(&bar.to_ascii(max_swing, 30));
            output.push('\n');
        }

        output
    }

    /// Get top N drivers
    #[must_use]
    pub fn top_drivers(&self, n: usize) -> Vec<&SensitivityBar> {
        self.bars.iter().take(n).collect()
    }

    /// Calculate percentage of variance explained by top N drivers
    #[must_use]
    pub fn variance_explained_by_top(&self, n: usize) -> f64 {
        if self.total_variance == 0.0 {
            return 0.0;
        }
        let top_variance: f64 = self.bars.iter().take(n).map(|b| b.abs_swing).sum();
        top_variance / self.total_variance * 100.0
    }
}

/// Tornado Diagram Engine
pub struct TornadoEngine {
    config: TornadoConfig,
    base_model: ParsedModel,
}

impl TornadoEngine {
    /// Create a new tornado engine
    ///
    /// # Errors
    ///
    /// Returns an error if the configuration is invalid.
    pub fn new(config: TornadoConfig, base_model: ParsedModel) -> Result<Self, String> {
        config.validate()?;
        Ok(Self { config, base_model })
    }

    /// Run the sensitivity analysis
    ///
    /// # Errors
    ///
    /// Returns an error if the base model or any sensitivity calculation fails.
    pub fn analyze(&self) -> Result<TornadoResult, String> {
        // Calculate base case
        let base_value = self.calculate_output(&self.base_model)?;

        // Determine if we need cross-scale normalization
        // Get base values for all inputs to check if they span multiple orders of magnitude
        let input_bases: Vec<f64> = self
            .config
            .inputs
            .iter()
            .filter_map(|input| {
                input.base.or_else(|| {
                    self.base_model
                        .scalars
                        .get(&input.name)
                        .and_then(|v| v.value)
                })
            })
            .map(f64::abs)
            .filter(|v| *v > 1e-10)
            .collect();

        // Check if inputs span vastly different scales (e.g., dollars vs rates)
        let needs_normalization = if input_bases.len() >= 2 {
            let max_base = input_bases.iter().fold(0.0_f64, |a, &b| a.max(b));
            let min_base = input_bases.iter().fold(f64::INFINITY, |a, &b| a.min(b));
            max_base / min_base > 100.0 // More than 2 orders of magnitude difference
        } else {
            false
        };

        // Calculate sensitivity for each input
        let mut bars: Vec<SensitivityBar> = Vec::new();

        for input in &self.config.inputs {
            let bar = self.calculate_sensitivity(input, base_value, needs_normalization)?;
            bars.push(bar);
        }

        // Sort by absolute swing (largest impact first)
        bars.sort_by(|a, b| {
            b.abs_swing
                .partial_cmp(&a.abs_swing)
                .unwrap_or(std::cmp::Ordering::Equal)
        });

        // Calculate total variance
        let total_variance: f64 = bars.iter().map(|b| b.abs_swing).sum();

        Ok(TornadoResult {
            output: self.config.output.clone(),
            base_value,
            bars,
            total_variance,
        })
    }

    /// Calculate sensitivity for a single input
    fn calculate_sensitivity(
        &self,
        input: &InputRange,
        _base_value: f64,
        needs_normalization: bool,
    ) -> Result<SensitivityBar, String> {
        // Calculate output at low input value
        let output_at_low = self.calculate_with_override(&input.name, input.low)?;

        // Calculate output at high input value
        let output_at_high = self.calculate_with_override(&input.name, input.high)?;

        let swing = output_at_high - output_at_low;
        let raw_abs_swing = swing.abs();

        // Apply normalization only when comparing inputs of vastly different scales
        let abs_swing = if needs_normalization {
            // Get the base input value from the model to calculate relative range
            let input_base = input
                .base
                .or_else(|| {
                    self.base_model
                        .scalars
                        .get(&input.name)
                        .and_then(|v| v.value)
                })
                .unwrap_or(1.0); // Default to 1.0 if no base found

            // Calculate relative input range (as fraction of base)
            // This normalizes inputs of different scales (e.g., dollars vs rates)
            let input_range = input.high - input.low;
            let relative_range = if input_base.abs() > 1e-10 {
                input_range / input_base.abs()
            } else {
                1.0 // Avoid division by zero
            };

            // Weight sensitivity by square of relative range
            // This ensures inputs varied by larger percentages rank higher
            raw_abs_swing * relative_range * relative_range
        } else {
            // For inputs of similar scale, use raw absolute swing
            raw_abs_swing
        };

        Ok(SensitivityBar {
            input_name: input.name.clone(),
            output_at_low,
            output_at_high,
            swing,
            abs_swing,
            input_low: input.low,
            input_high: input.high,
        })
    }

    /// Calculate output with a specific input override
    fn calculate_with_override(&self, input_name: &str, input_value: f64) -> Result<f64, String> {
        let mut model = self.base_model.clone();

        // Override the input value
        if let Some(scalar) = model.scalars.get_mut(input_name) {
            scalar.value = Some(input_value);
            scalar.formula = None; // Clear formula to use override value
        } else {
            // Create new scalar
            model.scalars.insert(
                input_name.to_string(),
                Variable::new(input_name.to_string(), Some(input_value), None),
            );
        }

        self.calculate_output(&model)
    }

    /// Calculate the output variable value
    fn calculate_output(&self, model: &ParsedModel) -> Result<f64, String> {
        let calculator = ArrayCalculator::new(model.clone());
        let result = calculator.calculate_all().map_err(|e| e.to_string())?;

        result
            .scalars
            .get(&self.config.output)
            .and_then(|v| v.value)
            .ok_or_else(|| {
                format!(
                    "Output variable '{}' not found or has no value",
                    self.config.output
                )
            })
    }

    /// Get the configuration
    #[must_use]
    pub const fn config(&self) -> &TornadoConfig {
        &self.config
    }
}

#[cfg(test)]
mod engine_tests {
    use super::*;

    fn create_test_model() -> ParsedModel {
        let mut model = ParsedModel::new();

        // Inputs
        model.scalars.insert(
            "revenue".to_string(),
            Variable::new("revenue".to_string(), Some(1_000_000.0), None),
        );
        model.scalars.insert(
            "cost_rate".to_string(),
            Variable::new("cost_rate".to_string(), Some(0.60), None),
        );
        model.scalars.insert(
            "tax_rate".to_string(),
            Variable::new("tax_rate".to_string(), Some(0.25), None),
        );

        // Output: profit = revenue * (1 - cost_rate) * (1 - tax_rate)
        model.scalars.insert(
            "profit".to_string(),
            Variable::new(
                "profit".to_string(),
                None,
                Some("=revenue * (1 - cost_rate) * (1 - tax_rate)".to_string()),
            ),
        );

        model
    }

    #[test]
    fn test_tornado_analysis() {
        let model = create_test_model();
        let config = TornadoConfig::new("profit")
            .with_input(InputRange::new("revenue", 800_000.0, 1_200_000.0))
            .with_input(InputRange::new("cost_rate", 0.50, 0.70))
            .with_input(InputRange::new("tax_rate", 0.20, 0.30));

        let engine = TornadoEngine::new(config, model).unwrap();
        let result = engine.analyze().unwrap();

        // Should have 3 bars
        assert_eq!(result.bars.len(), 3);

        // Bars should be sorted by impact
        for i in 0..result.bars.len() - 1 {
            assert!(
                result.bars[i].abs_swing >= result.bars[i + 1].abs_swing,
                "Bars should be sorted by impact"
            );
        }

        // Revenue should have the biggest impact (absolute dollars)
        assert_eq!(result.bars[0].input_name, "revenue");
    }

    #[test]
    fn test_ascii_output() {
        let model = create_test_model();
        let config = TornadoConfig::new("profit")
            .with_input(InputRange::new("revenue", 800_000.0, 1_200_000.0))
            .with_input(InputRange::new("cost_rate", 0.50, 0.70));

        let engine = TornadoEngine::new(config, model).unwrap();
        let result = engine.analyze().unwrap();
        let ascii = result.to_ascii();

        assert!(ascii.contains("profit Sensitivity"));
        assert!(ascii.contains("revenue"));
        assert!(ascii.contains("cost_rate"));
    }

    #[test]
    fn test_top_drivers() {
        let model = create_test_model();
        let config = TornadoConfig::new("profit")
            .with_input(InputRange::new("revenue", 800_000.0, 1_200_000.0))
            .with_input(InputRange::new("cost_rate", 0.50, 0.70))
            .with_input(InputRange::new("tax_rate", 0.20, 0.30));

        let engine = TornadoEngine::new(config, model).unwrap();
        let result = engine.analyze().unwrap();

        let top_2 = result.top_drivers(2);
        assert_eq!(top_2.len(), 2);

        // Check variance explained
        let pct = result.variance_explained_by_top(2);
        assert!(pct > 50.0, "Top 2 should explain > 50% of variance");
    }

    #[test]
    fn test_yaml_export() {
        let model = create_test_model();
        let config = TornadoConfig::new("profit").with_input(InputRange::new(
            "revenue",
            800_000.0,
            1_200_000.0,
        ));

        let engine = TornadoEngine::new(config, model).unwrap();
        let result = engine.analyze().unwrap();
        let yaml = result.to_yaml();

        assert!(yaml.contains("output: profit"));
        assert!(yaml.contains("bars:"));
    }

    #[test]
    fn test_json_export() {
        let model = create_test_model();
        let config = TornadoConfig::new("profit").with_input(InputRange::new(
            "revenue",
            800_000.0,
            1_200_000.0,
        ));

        let engine = TornadoEngine::new(config, model).unwrap();
        let result = engine.analyze().unwrap();
        let json = result.to_json().unwrap();

        assert!(json.contains("\"output\""));
        assert!(json.contains("\"bars\""));
    }
}