corp-finance-core 1.1.0

Institutional-grade corporate finance calculations with 128-bit decimal precision — DCF, WACC, comps, LBO, credit metrics, derivatives, fixed income, options, and 60+ specialty modules. No f64 in financials. WASM-compatible.
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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
//! Index Reconstitution Analysis.
//!
//! Covers:
//! 1. **Additions** -- candidates meeting criteria above buffer threshold
//! 2. **Deletions** -- current members failing criteria below buffer threshold
//! 3. **Buffer Zone** -- members near thresholds to prevent excessive turnover
//! 4. **Turnover** -- reconstitution-driven turnover measurement
//! 5. **Impact Analysis** -- estimated buy/sell pressure from recon changes
//!
//! All arithmetic uses `rust_decimal::Decimal`. No `f64`.

use rust_decimal::Decimal;
use rust_decimal_macros::dec;
use serde::{Deserialize, Serialize};

use crate::error::CorpFinanceError;
use crate::CorpFinanceResult;

// ---------------------------------------------------------------------------
// Types
// ---------------------------------------------------------------------------

/// An index member (current or candidate).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct IndexMember {
    pub ticker: String,
    pub market_cap: Decimal,
    pub meets_criteria: bool,
    pub float_pct: Decimal,
    pub avg_volume: Decimal,
}

/// A reconstitution action (addition or deletion).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ReconAction {
    pub ticker: String,
    pub market_cap: Decimal,
    pub reason: String,
}

/// Impact metrics from reconstitution.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ReconImpact {
    pub estimated_buy_pressure: Decimal,
    pub estimated_sell_pressure: Decimal,
    pub net_flow: Decimal,
}

// ---------------------------------------------------------------------------
// Input / Output
// ---------------------------------------------------------------------------

/// Input for reconstitution analysis.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ReconstitutionInput {
    pub current_members: Vec<IndexMember>,
    pub candidates: Vec<IndexMember>,
    pub min_market_cap: Decimal,
    pub min_float_pct: Decimal,
    pub min_volume: Decimal,
    pub max_members: u32,
    /// Buffer zone around threshold (e.g. 0.10 = 10%).
    pub buffer_zone_pct: Decimal,
}

/// Output of the reconstitution analysis.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ReconstitutionOutput {
    pub additions: Vec<ReconAction>,
    pub deletions: Vec<ReconAction>,
    pub retained: Vec<String>,
    pub turnover: Decimal,
    pub new_member_count: u32,
    pub avg_market_cap_before: Decimal,
    pub avg_market_cap_after: Decimal,
    pub buffer_zone_members: Vec<String>,
    pub reconstitution_impact: ReconImpact,
}

// ---------------------------------------------------------------------------
// Calculation
// ---------------------------------------------------------------------------

/// Perform index reconstitution analysis.
pub fn calculate_reconstitution(
    input: &ReconstitutionInput,
) -> CorpFinanceResult<ReconstitutionOutput> {
    validate_reconstitution_input(input)?;

    let lower_threshold = input.min_market_cap * (Decimal::ONE - input.buffer_zone_pct);
    let upper_threshold = input.min_market_cap * (Decimal::ONE + input.buffer_zone_pct);

    // Determine deletions: current members that fail criteria and are below buffer
    let mut deletions: Vec<ReconAction> = Vec::new();
    let mut retained: Vec<String> = Vec::new();
    let mut buffer_zone_members: Vec<String> = Vec::new();

    for m in &input.current_members {
        let in_buffer = m.market_cap >= lower_threshold && m.market_cap <= upper_threshold;

        if in_buffer {
            buffer_zone_members.push(m.ticker.clone());
        }

        if !m.meets_criteria && m.market_cap < lower_threshold {
            deletions.push(ReconAction {
                ticker: m.ticker.clone(),
                market_cap: m.market_cap,
                reason: format!(
                    "Fails criteria, market cap {} below buffer threshold {}",
                    m.market_cap, lower_threshold
                ),
            });
        } else {
            retained.push(m.ticker.clone());
        }
    }

    // Available slots after deletions
    let retained_count = retained.len() as u32;
    let available_slots = input.max_members.saturating_sub(retained_count);

    // Determine additions: candidates that meet criteria, above upper threshold, and pass filters
    let mut qualified_candidates: Vec<&IndexMember> = input
        .candidates
        .iter()
        .filter(|c| {
            c.meets_criteria
                && c.market_cap > upper_threshold
                && c.float_pct >= input.min_float_pct
                && c.avg_volume >= input.min_volume
        })
        .collect();

    // Sort by market cap descending
    qualified_candidates.sort_by(|a, b| b.market_cap.cmp(&a.market_cap));

    // Take up to available slots
    let additions: Vec<ReconAction> = qualified_candidates
        .iter()
        .take(available_slots as usize)
        .map(|c| ReconAction {
            ticker: c.ticker.clone(),
            market_cap: c.market_cap,
            reason: format!(
                "Meets criteria, market cap {} above buffer threshold {}",
                c.market_cap, upper_threshold
            ),
        })
        .collect();

    // Turnover
    let current_count = input.current_members.len() as u32;
    let changes = (additions.len() + deletions.len()) as u64;
    let turnover = if current_count == 0 {
        Decimal::ZERO
    } else {
        Decimal::from(changes) / (dec!(2) * Decimal::from(current_count as u64))
    };

    // New member count
    let new_member_count = retained_count + additions.len() as u32;

    // Average market cap before
    let avg_market_cap_before = if input.current_members.is_empty() {
        Decimal::ZERO
    } else {
        let total: Decimal = input.current_members.iter().map(|m| m.market_cap).sum();
        total / Decimal::from(input.current_members.len() as u64)
    };

    // Average market cap after: retained members + additions
    let retained_mc: Decimal = input
        .current_members
        .iter()
        .filter(|m| retained.contains(&m.ticker))
        .map(|m| m.market_cap)
        .sum();
    let additions_mc: Decimal = additions.iter().map(|a| a.market_cap).sum();
    let avg_market_cap_after = if new_member_count == 0 {
        Decimal::ZERO
    } else {
        (retained_mc + additions_mc) / Decimal::from(new_member_count as u64)
    };

    // Impact
    let estimated_buy_pressure: Decimal = additions.iter().map(|a| a.market_cap).sum();
    let estimated_sell_pressure: Decimal = deletions.iter().map(|d| d.market_cap).sum();
    let net_flow = estimated_buy_pressure - estimated_sell_pressure;

    Ok(ReconstitutionOutput {
        additions,
        deletions,
        retained,
        turnover,
        new_member_count,
        avg_market_cap_before,
        avg_market_cap_after,
        buffer_zone_members,
        reconstitution_impact: ReconImpact {
            estimated_buy_pressure,
            estimated_sell_pressure,
            net_flow,
        },
    })
}

// ---------------------------------------------------------------------------
// Validation
// ---------------------------------------------------------------------------

fn validate_reconstitution_input(input: &ReconstitutionInput) -> CorpFinanceResult<()> {
    if input.current_members.is_empty() && input.candidates.is_empty() {
        return Err(CorpFinanceError::InsufficientData(
            "At least one current member or candidate is required".into(),
        ));
    }
    if input.min_market_cap < Decimal::ZERO {
        return Err(CorpFinanceError::InvalidInput {
            field: "min_market_cap".into(),
            reason: "Minimum market cap must be non-negative".into(),
        });
    }
    if input.min_float_pct < Decimal::ZERO || input.min_float_pct > Decimal::ONE {
        return Err(CorpFinanceError::InvalidInput {
            field: "min_float_pct".into(),
            reason: "Minimum float percentage must be between 0 and 1".into(),
        });
    }
    if input.buffer_zone_pct < Decimal::ZERO || input.buffer_zone_pct > Decimal::ONE {
        return Err(CorpFinanceError::InvalidInput {
            field: "buffer_zone_pct".into(),
            reason: "Buffer zone must be between 0 and 1".into(),
        });
    }
    if input.max_members == 0 {
        return Err(CorpFinanceError::InvalidInput {
            field: "max_members".into(),
            reason: "Max members must be positive".into(),
        });
    }
    if input.min_volume < Decimal::ZERO {
        return Err(CorpFinanceError::InvalidInput {
            field: "min_volume".into(),
            reason: "Minimum volume must be non-negative".into(),
        });
    }
    for m in input.current_members.iter().chain(input.candidates.iter()) {
        if m.market_cap < Decimal::ZERO {
            return Err(CorpFinanceError::InvalidInput {
                field: "market_cap".into(),
                reason: format!("Negative market cap for {}", m.ticker),
            });
        }
    }
    Ok(())
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;
    use rust_decimal_macros::dec;

    fn approx_eq(a: Decimal, b: Decimal, eps: Decimal) -> bool {
        (a - b).abs() < eps
    }

    fn make_member(ticker: &str, mc: Decimal, meets: bool) -> IndexMember {
        IndexMember {
            ticker: ticker.into(),
            market_cap: mc,
            meets_criteria: meets,
            float_pct: dec!(0.50),
            avg_volume: dec!(500_000),
        }
    }

    fn make_base_input() -> ReconstitutionInput {
        ReconstitutionInput {
            current_members: vec![
                make_member("A", dec!(5000), true),
                make_member("B", dec!(3000), true),
                make_member("C", dec!(2000), true),
                make_member("D", dec!(800), false), // fails criteria, below threshold
                make_member("E", dec!(1200), true),
            ],
            candidates: vec![
                make_member("X", dec!(4000), true),
                make_member("Y", dec!(2500), true),
                make_member("Z", dec!(500), true), // below threshold
            ],
            min_market_cap: dec!(1000),
            min_float_pct: dec!(0.25),
            min_volume: dec!(100_000),
            max_members: 5,
            buffer_zone_pct: dec!(0.10),
        }
    }

    // --- No changes scenario ---
    #[test]
    fn test_no_changes_all_qualify() {
        let mut input = make_base_input();
        // All current members pass, no slots for additions
        input.current_members = vec![
            make_member("A", dec!(5000), true),
            make_member("B", dec!(3000), true),
            make_member("C", dec!(2000), true),
            make_member("D", dec!(1500), true),
            make_member("E", dec!(1200), true),
        ];
        let out = calculate_reconstitution(&input).unwrap();
        assert_eq!(out.deletions.len(), 0);
        assert_eq!(out.additions.len(), 0);
        assert_eq!(out.retained.len(), 5);
    }

    // --- Deletions ---
    #[test]
    fn test_deletion_below_buffer() {
        let input = make_base_input();
        let out = calculate_reconstitution(&input).unwrap();
        // D: mc=800, fails criteria, 800 < 1000*(1-0.10)=900 -> deleted
        let deleted_tickers: Vec<&str> = out.deletions.iter().map(|d| d.ticker.as_str()).collect();
        assert!(deleted_tickers.contains(&"D"));
    }

    #[test]
    fn test_deletion_reason() {
        let input = make_base_input();
        let out = calculate_reconstitution(&input).unwrap();
        assert!(!out.deletions.is_empty());
        assert!(out.deletions[0].reason.contains("Fails criteria"));
    }

    // --- Additions ---
    #[test]
    fn test_additions_fill_slots() {
        let input = make_base_input();
        let out = calculate_reconstitution(&input).unwrap();
        // D deleted (1 slot), X qualifies (mc=4000 > 1100 upper threshold), Y qualifies (2500 > 1100)
        // But only 1 slot available (5 retained - 1 deletion = 4, max 5, so 1 slot)
        assert!(!out.additions.is_empty());
    }

    #[test]
    fn test_additions_sorted_by_market_cap() {
        let mut input = make_base_input();
        input.max_members = 10; // lots of room
        let out = calculate_reconstitution(&input).unwrap();
        // X (4000) should be before Y (2500) in additions
        if out.additions.len() >= 2 {
            assert!(out.additions[0].market_cap >= out.additions[1].market_cap);
        }
    }

    #[test]
    fn test_additions_only() {
        let mut input = make_base_input();
        input.current_members = vec![make_member("A", dec!(5000), true)];
        input.max_members = 5;
        let out = calculate_reconstitution(&input).unwrap();
        assert!(out.deletions.is_empty());
        assert!(!out.additions.is_empty());
    }

    #[test]
    fn test_deletions_only() {
        let mut input = make_base_input();
        input.candidates = vec![]; // no candidates
        let out = calculate_reconstitution(&input).unwrap();
        assert!(!out.deletions.is_empty());
        assert!(out.additions.is_empty());
    }

    // --- Buffer zone ---
    #[test]
    fn test_buffer_zone_prevents_churn() {
        let mut input = make_base_input();
        // D has mc=950, fails criteria but within buffer (900-1100)
        input.current_members[3] = make_member("D", dec!(950), false);
        let out = calculate_reconstitution(&input).unwrap();
        // D should NOT be deleted (within buffer zone)
        let deleted_tickers: Vec<&str> = out.deletions.iter().map(|d| d.ticker.as_str()).collect();
        assert!(!deleted_tickers.contains(&"D"));
    }

    #[test]
    fn test_buffer_zone_members_identified() {
        let mut input = make_base_input();
        // E has mc=1050, within buffer zone (900-1100)
        input.current_members[4] = make_member("E", dec!(1050), true);
        let out = calculate_reconstitution(&input).unwrap();
        assert!(out.buffer_zone_members.contains(&"E".to_string()));
    }

    // --- Market cap sorting ---
    #[test]
    fn test_candidates_below_threshold_excluded() {
        let input = make_base_input();
        let out = calculate_reconstitution(&input).unwrap();
        // Z: mc=500 < 1100 upper threshold -> not added
        let added_tickers: Vec<&str> = out.additions.iter().map(|a| a.ticker.as_str()).collect();
        assert!(!added_tickers.contains(&"Z"));
    }

    // --- Volume filter ---
    #[test]
    fn test_volume_filter() {
        let mut input = make_base_input();
        input.candidates[0].avg_volume = dec!(50_000); // below min 100k
        input.max_members = 10;
        let out = calculate_reconstitution(&input).unwrap();
        let added_tickers: Vec<&str> = out.additions.iter().map(|a| a.ticker.as_str()).collect();
        assert!(!added_tickers.contains(&"X"));
    }

    // --- Float filter ---
    #[test]
    fn test_float_filter() {
        let mut input = make_base_input();
        input.candidates[0].float_pct = dec!(0.10); // below min 0.25
        input.max_members = 10;
        let out = calculate_reconstitution(&input).unwrap();
        let added_tickers: Vec<&str> = out.additions.iter().map(|a| a.ticker.as_str()).collect();
        assert!(!added_tickers.contains(&"X"));
    }

    // --- Max members respected ---
    #[test]
    fn test_max_members_respected() {
        let input = make_base_input();
        let out = calculate_reconstitution(&input).unwrap();
        assert!(out.new_member_count <= input.max_members);
    }

    // --- Turnover ---
    #[test]
    fn test_turnover_calculation() {
        let input = make_base_input();
        let out = calculate_reconstitution(&input).unwrap();
        // turnover = (additions + deletions) / (2 * current)
        let expected = Decimal::from((out.additions.len() + out.deletions.len()) as u64)
            / (dec!(2) * Decimal::from(input.current_members.len() as u64));
        assert!(approx_eq(out.turnover, expected, dec!(0.001)));
    }

    #[test]
    fn test_turnover_zero_no_changes() {
        let mut input = make_base_input();
        input.current_members = vec![
            make_member("A", dec!(5000), true),
            make_member("B", dec!(3000), true),
            make_member("C", dec!(2000), true),
            make_member("D", dec!(1500), true),
            make_member("E", dec!(1200), true),
        ];
        let out = calculate_reconstitution(&input).unwrap();
        assert_eq!(out.turnover, Decimal::ZERO);
    }

    // --- Average market cap ---
    #[test]
    fn test_avg_market_cap_before() {
        let input = make_base_input();
        let out = calculate_reconstitution(&input).unwrap();
        // (5000+3000+2000+800+1200)/5 = 2400
        assert!(approx_eq(out.avg_market_cap_before, dec!(2400), dec!(1)));
    }

    #[test]
    fn test_avg_market_cap_after_changes() {
        let input = make_base_input();
        let out = calculate_reconstitution(&input).unwrap();
        // After reconstitution, should differ from before
        assert!(out.avg_market_cap_after > Decimal::ZERO);
    }

    // --- Impact ---
    #[test]
    fn test_buy_pressure() {
        let input = make_base_input();
        let out = calculate_reconstitution(&input).unwrap();
        let add_mc: Decimal = out.additions.iter().map(|a| a.market_cap).sum();
        assert_eq!(out.reconstitution_impact.estimated_buy_pressure, add_mc);
    }

    #[test]
    fn test_sell_pressure() {
        let input = make_base_input();
        let out = calculate_reconstitution(&input).unwrap();
        let del_mc: Decimal = out.deletions.iter().map(|d| d.market_cap).sum();
        assert_eq!(out.reconstitution_impact.estimated_sell_pressure, del_mc);
    }

    #[test]
    fn test_net_flow() {
        let input = make_base_input();
        let out = calculate_reconstitution(&input).unwrap();
        let expected = out.reconstitution_impact.estimated_buy_pressure
            - out.reconstitution_impact.estimated_sell_pressure;
        assert_eq!(out.reconstitution_impact.net_flow, expected);
    }

    // --- Validation ---
    #[test]
    fn test_reject_all_empty() {
        let input = ReconstitutionInput {
            current_members: vec![],
            candidates: vec![],
            min_market_cap: dec!(1000),
            min_float_pct: dec!(0.25),
            min_volume: dec!(100_000),
            max_members: 5,
            buffer_zone_pct: dec!(0.10),
        };
        assert!(calculate_reconstitution(&input).is_err());
    }

    #[test]
    fn test_reject_negative_min_market_cap() {
        let mut input = make_base_input();
        input.min_market_cap = dec!(-100);
        assert!(calculate_reconstitution(&input).is_err());
    }

    #[test]
    fn test_reject_invalid_float_pct() {
        let mut input = make_base_input();
        input.min_float_pct = dec!(1.5);
        assert!(calculate_reconstitution(&input).is_err());
    }

    #[test]
    fn test_reject_zero_max_members() {
        let mut input = make_base_input();
        input.max_members = 0;
        assert!(calculate_reconstitution(&input).is_err());
    }

    #[test]
    fn test_reject_invalid_buffer() {
        let mut input = make_base_input();
        input.buffer_zone_pct = dec!(1.5);
        assert!(calculate_reconstitution(&input).is_err());
    }

    #[test]
    fn test_reject_negative_member_market_cap() {
        let mut input = make_base_input();
        input.current_members[0].market_cap = dec!(-100);
        assert!(calculate_reconstitution(&input).is_err());
    }

    #[test]
    fn test_serialization_roundtrip() {
        let input = make_base_input();
        let out = calculate_reconstitution(&input).unwrap();
        let json = serde_json::to_string(&out).unwrap();
        let _: ReconstitutionOutput = serde_json::from_str(&json).unwrap();
    }
}