scirs2-series 0.3.2

Time series analysis module for SciRS2 (scirs2-series)
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
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
//! Drawdown analysis for portfolio and asset performance
//!
//! This module provides comprehensive drawdown analysis tools for measuring
//! the peak-to-trough decline during a specific period. Drawdown analysis
//! is crucial for understanding the downside risk and recovery characteristics
//! of investment strategies.
//!
//! # Overview
//!
//! Drawdown metrics help investors and portfolio managers:
//! - Assess maximum loss potential from peak levels
//! - Understand recovery time after losses
//! - Evaluate risk-adjusted performance
//! - Set position sizing and risk limits
//!
//! # Key Metrics
//!
//! ## Maximum Drawdown (MDD)
//! The largest peak-to-trough decline, expressed as a percentage.
//! This is the most commonly used drawdown metric.
//!
//! ## Pain Index
//! The average drawdown over time, providing a measure of sustained
//! loss periods rather than just peak losses.
//!
//! ## Ulcer Index
//! The root mean square (RMS) of all drawdowns, giving higher weight
//! to larger drawdowns while considering their duration.
//!
//! ## Calmar Ratio
//! Annual return divided by maximum drawdown, showing return per
//! unit of worst-case loss.
//!
//! ## Recovery Analysis
//! Time-based analysis of how long it takes to recover from drawdowns.
//!
//! # Examples
//!
//! ## Basic Drawdown Analysis
//! ```rust
//! use scirs2_series::financial::risk::drawdown::{max_drawdown, calculate_drawdown_series};
//! use scirs2_core::ndarray::array;
//!
//! // Portfolio value over time
//! let portfolio_values = array![1000.0, 1100.0, 1050.0, 950.0, 1200.0, 1150.0, 1300.0];
//!
//! // Maximum drawdown
//! let mdd = max_drawdown(&portfolio_values).expect("Operation failed");
//! println!("Maximum Drawdown: {:.2}%", mdd * 100.0);
//!
//! // Full drawdown series
//! let drawdowns = calculate_drawdown_series(&portfolio_values).expect("Operation failed");
//! println!("Drawdown series: {:?}", drawdowns);
//! ```
//!
//! ## Advanced Drawdown Metrics
//! ```rust
//! use scirs2_series::financial::risk::drawdown::{pain_index, ulcer_index, calmar_ratio};
//! use scirs2_core::ndarray::array;
//!
//! let portfolio_values = array![1000.0, 1100.0, 1050.0, 950.0, 1200.0, 1150.0, 1300.0];
//! let returns = array![0.10, -0.05, -0.10, 0.26, -0.04, 0.13]; // Corresponding returns
//!
//! // Pain Index - average drawdown
//! let pain = pain_index(&portfolio_values).expect("Operation failed");
//!
//! // Ulcer Index - RMS drawdown
//! let ulcer = ulcer_index(&portfolio_values).expect("Operation failed");
//!
//! // Calmar Ratio - return/max drawdown trade-off
//! let calmar = calmar_ratio(&returns, &portfolio_values, 252).expect("Operation failed");
//! ```
//!
//! ## Drawdown Recovery Analysis
//! ```rust
//! use scirs2_series::financial::risk::drawdown::{drawdown_recovery_analysis, DrawdownPeriod};
//! use scirs2_core::ndarray::array;
//!
//! let portfolio_values = array![1000.0, 1100.0, 1050.0, 950.0, 1200.0, 1150.0, 1300.0];
//!
//! let recovery_analysis = drawdown_recovery_analysis(&portfolio_values).expect("Operation failed");
//! for period in recovery_analysis {
//!     println!("Drawdown: {:.2}%, Duration: {} periods, Recovery: {} periods",
//!              period.max_drawdown * 100.0, period.duration,
//!              period.recovery_periods.unwrap_or(0));
//! }
//! ```

use scirs2_core::ndarray::Array1;
use scirs2_core::numeric::Float;

use crate::error::{Result, TimeSeriesError};

/// Drawdown period information
#[derive(Debug, Clone)]
pub struct DrawdownPeriod<F: Float> {
    /// Start index of drawdown period
    pub start_index: usize,
    /// End index of drawdown period (trough)
    pub end_index: usize,
    /// Recovery index (when portfolio recovers to peak), None if not recovered
    pub recovery_index: Option<usize>,
    /// Maximum drawdown during this period
    pub max_drawdown: F,
    /// Duration of drawdown (start to trough)
    pub duration: usize,
    /// Recovery periods (trough to recovery), None if not recovered
    pub recovery_periods: Option<usize>,
    /// Peak value at start of drawdown
    pub peak_value: F,
    /// Trough value
    pub trough_value: F,
}

/// Calculate maximum drawdown from portfolio values
///
/// Maximum drawdown is the largest peak-to-trough decline expressed as
/// a percentage. It represents the worst-case loss an investor would
/// have experienced during the period.
///
/// # Arguments
///
/// * `values` - Portfolio or asset values over time
///
/// # Returns
///
/// * `Result<F>` - Maximum drawdown as a decimal (0.1 = 10% drawdown)
///
/// # Examples
///
/// ```rust
/// use scirs2_series::financial::risk::drawdown::max_drawdown;
/// use scirs2_core::ndarray::array;
///
/// let portfolio_values = array![1000.0, 1100.0, 1050.0, 950.0, 1200.0];
/// let mdd = max_drawdown(&portfolio_values).expect("Operation failed");
/// println!("Max drawdown: {:.2}%", mdd * 100.0);
/// ```
pub fn max_drawdown<F: Float + Clone>(values: &Array1<F>) -> Result<F> {
    if values.is_empty() {
        return Err(TimeSeriesError::InvalidInput(
            "Values cannot be empty".to_string(),
        ));
    }

    let mut max_value = values[0];
    let mut max_dd = F::zero();

    for &value in values.iter() {
        if value > max_value {
            max_value = value;
        }

        let drawdown = (max_value - value) / max_value;
        if drawdown > max_dd {
            max_dd = drawdown;
        }
    }

    Ok(max_dd)
}

/// Calculate drawdown series from portfolio values
///
/// Returns the drawdown at each point in time, showing the running
/// peak-to-current decline as a percentage.
///
/// # Arguments
///
/// * `values` - Portfolio or asset values over time
///
/// # Returns
///
/// * `Result<Array1<F>>` - Drawdown series (negative values indicate drawdowns)
///
/// # Examples
///
/// ```rust
/// use scirs2_series::financial::risk::drawdown::calculate_drawdown_series;
/// use scirs2_core::ndarray::array;
///
/// let values = array![1000.0, 1100.0, 1050.0, 950.0, 1200.0];
/// let drawdowns = calculate_drawdown_series(&values).expect("Operation failed");
/// ```
pub fn calculate_drawdown_series<F: Float + Clone>(values: &Array1<F>) -> Result<Array1<F>> {
    if values.is_empty() {
        return Err(TimeSeriesError::InvalidInput(
            "Values cannot be empty".to_string(),
        ));
    }

    let mut drawdowns = Array1::zeros(values.len());
    let mut peak = values[0];

    for i in 0..values.len() {
        if values[i] > peak {
            peak = values[i];
        }
        drawdowns[i] = (values[i] - peak) / peak;
    }

    Ok(drawdowns)
}

/// Calculate Pain Index (average drawdown)
///
/// The Pain Index measures the average drawdown over the entire period.
/// It provides insight into sustained loss periods rather than just
/// peak losses.
///
/// # Arguments
///
/// * `values` - Portfolio or asset values over time
///
/// # Returns
///
/// * `Result<F>` - Pain Index as a decimal
///
/// # Examples
///
/// ```rust
/// use scirs2_series::financial::risk::drawdown::pain_index;
/// use scirs2_core::ndarray::array;
///
/// let values = array![1000.0, 1100.0, 1050.0, 950.0, 1200.0];
/// let pain = pain_index(&values).expect("Operation failed");
/// ```
pub fn pain_index<F: Float + Clone>(values: &Array1<F>) -> Result<F> {
    let drawdowns = calculate_drawdown_series(values)?;

    // Convert to positive values and average
    let total_drawdown = drawdowns.mapv(|x| -x).sum();
    Ok(total_drawdown / F::from(values.len()).expect("Operation failed"))
}

/// Calculate Ulcer Index (RMS of drawdowns)
///
/// The Ulcer Index is the root mean square of all drawdowns, giving
/// higher weight to larger drawdowns while considering their duration.
/// It's named after the "ulcer" that large drawdowns can cause investors.
///
/// # Arguments
///
/// * `values` - Portfolio or asset values over time
///
/// # Returns
///
/// * `Result<F>` - Ulcer Index as a decimal
///
/// # Examples
///
/// ```rust
/// use scirs2_series::financial::risk::drawdown::ulcer_index;
/// use scirs2_core::ndarray::array;
///
/// let values = array![1000.0, 1100.0, 1050.0, 950.0, 1200.0];
/// let ulcer = ulcer_index(&values).expect("Operation failed");
/// ```
pub fn ulcer_index<F: Float + Clone>(values: &Array1<F>) -> Result<F> {
    let drawdowns = calculate_drawdown_series(values)?;

    // Calculate RMS of drawdowns (as positive values)
    let sum_squared_dd = drawdowns.mapv(|x| x.powi(2)).sum();
    Ok((sum_squared_dd / F::from(values.len()).expect("Operation failed")).sqrt())
}

/// Calculate Calmar Ratio (annual return / maximum drawdown)
///
/// The Calmar Ratio measures the trade-off between return and maximum drawdown.
/// Higher values indicate better risk-adjusted performance relative to
/// worst-case scenarios.
///
/// # Arguments
///
/// * `returns` - Return series
/// * `values` - Portfolio values for drawdown calculation
/// * `periods_per_year` - Number of periods per year for annualization
///
/// # Returns
///
/// * `Result<F>` - Calmar Ratio
///
/// # Examples
///
/// ```rust
/// use scirs2_series::financial::risk::drawdown::calmar_ratio;
/// use scirs2_core::ndarray::array;
///
/// let returns = array![0.10, -0.05, -0.10, 0.26, -0.04, 0.13];
/// let values = array![1000.0, 1100.0, 1050.0, 950.0, 1200.0, 1150.0, 1300.0];
/// let calmar = calmar_ratio(&returns, &values, 252).expect("Operation failed");
/// ```
pub fn calmar_ratio<F: Float + Clone>(
    returns: &Array1<F>,
    values: &Array1<F>,
    periods_per_year: usize,
) -> Result<F> {
    if returns.is_empty() || values.is_empty() {
        return Err(TimeSeriesError::InvalidInput(
            "Returns and values cannot be empty".to_string(),
        ));
    }

    // Calculate annualized return
    let total_return = (values[values.len() - 1] / values[0]) - F::one();
    let years = F::from(returns.len()).expect("Operation failed")
        / F::from(periods_per_year).expect("Failed to convert to float");
    let annualized_return = (F::one() + total_return).powf(F::one() / years) - F::one();

    // Calculate maximum drawdown
    let mdd = max_drawdown(values)?;

    if mdd == F::zero() {
        Ok(F::infinity())
    } else {
        Ok(annualized_return / mdd)
    }
}

/// Perform comprehensive drawdown recovery analysis
///
/// Identifies all drawdown periods and their recovery characteristics,
/// providing detailed information about each significant drawdown event.
///
/// # Arguments
///
/// * `values` - Portfolio or asset values over time
///
/// # Returns
///
/// * `Result<Vec<DrawdownPeriod<F>>>` - Vector of drawdown periods with recovery analysis
///
/// # Examples
///
/// ```rust
/// use scirs2_series::financial::risk::drawdown::drawdown_recovery_analysis;
/// use scirs2_core::ndarray::array;
///
/// let values = array![1000.0, 1100.0, 1050.0, 950.0, 1200.0, 1150.0, 1300.0];
/// let analysis = drawdown_recovery_analysis(&values).expect("Operation failed");
/// ```
pub fn drawdown_recovery_analysis<F: Float + Clone>(
    values: &Array1<F>,
) -> Result<Vec<DrawdownPeriod<F>>> {
    if values.is_empty() {
        return Err(TimeSeriesError::InvalidInput(
            "Values cannot be empty".to_string(),
        ));
    }

    let mut periods = Vec::new();
    let mut peak = values[0];
    let mut peak_index = 0;
    let mut in_drawdown = false;
    let mut trough_value = values[0];
    let mut trough_index = 0;

    for i in 0..values.len() {
        if values[i] > peak {
            // New peak
            if in_drawdown {
                // End of drawdown period, look for recovery
                let recovery_index = find_recovery_index(values, i, peak);
                let duration = trough_index - peak_index;
                let recovery_periods = recovery_index.map(|idx| idx - trough_index);
                let max_dd = (peak - trough_value) / peak;

                periods.push(DrawdownPeriod {
                    start_index: peak_index,
                    end_index: trough_index,
                    recovery_index,
                    max_drawdown: max_dd,
                    duration,
                    recovery_periods,
                    peak_value: peak,
                    trough_value,
                });

                in_drawdown = false;
            }

            peak = values[i];
            peak_index = i;
        } else if values[i] < peak {
            // In drawdown
            if !in_drawdown {
                in_drawdown = true;
                trough_value = values[i];
                trough_index = i;
            } else if values[i] < trough_value {
                // New trough
                trough_value = values[i];
                trough_index = i;
            }
        }
    }

    // Handle case where we end in drawdown
    if in_drawdown {
        let duration = trough_index - peak_index;
        let max_dd = (peak - trough_value) / peak;

        periods.push(DrawdownPeriod {
            start_index: peak_index,
            end_index: trough_index,
            recovery_index: None,
            max_drawdown: max_dd,
            duration,
            recovery_periods: None,
            peak_value: peak,
            trough_value,
        });
    }

    Ok(periods)
}

/// Calculate maximum consecutive losses count
///
/// Counts the maximum number of consecutive negative returns,
/// which helps understand losing streaks.
///
/// # Arguments
///
/// * `returns` - Return series
///
/// # Returns
///
/// * `usize` - Maximum consecutive losses
pub fn max_consecutive_losses<F: Float + Clone>(returns: &Array1<F>) -> usize {
    let mut max_consecutive = 0;
    let mut current_consecutive = 0;

    for &ret in returns.iter() {
        if ret < F::zero() {
            current_consecutive += 1;
            max_consecutive = max_consecutive.max(current_consecutive);
        } else {
            current_consecutive = 0;
        }
    }

    max_consecutive
}

/// Calculate average drawdown duration
///
/// Measures the average time it takes from peak to trough across
/// all drawdown periods.
///
/// # Arguments
///
/// * `values` - Portfolio or asset values over time
///
/// # Returns
///
/// * `Result<F>` - Average drawdown duration in periods
pub fn average_drawdown_duration<F: Float + Clone>(values: &Array1<F>) -> Result<F> {
    let periods = drawdown_recovery_analysis(values)?;

    if periods.is_empty() {
        return Ok(F::zero());
    }

    let total_duration: usize = periods.iter().map(|p| p.duration).sum();
    Ok(F::from(total_duration).expect("Failed to convert to float")
        / F::from(periods.len()).expect("Operation failed"))
}

/// Calculate average recovery time
///
/// Measures the average time it takes to recover from trough back to peak
/// across all recovered drawdown periods.
///
/// # Arguments
///
/// * `values` - Portfolio or asset values over time
///
/// # Returns
///
/// * `Result<F>` - Average recovery time in periods (only for recovered drawdowns)
pub fn average_recovery_time<F: Float + Clone>(values: &Array1<F>) -> Result<F> {
    let periods = drawdown_recovery_analysis(values)?;

    let recovered_periods: Vec<&DrawdownPeriod<F>> = periods
        .iter()
        .filter(|p| p.recovery_periods.is_some())
        .collect();

    if recovered_periods.is_empty() {
        return Ok(F::zero());
    }

    let total_recovery: usize = recovered_periods
        .iter()
        .map(|p| p.recovery_periods.expect("Operation failed"))
        .sum();

    Ok(F::from(total_recovery).expect("Failed to convert to float")
        / F::from(recovered_periods.len()).expect("Operation failed"))
}

/// Helper function to find recovery index
fn find_recovery_index<F: Float + Clone>(
    values: &Array1<F>,
    start_search: usize,
    target_peak: F,
) -> Option<usize> {
    (start_search..values.len()).find(|&i| values[i] >= target_peak)
}

#[cfg(test)]
mod tests {
    use super::*;
    use scirs2_core::ndarray::arr1;

    #[test]
    fn test_max_drawdown() {
        let values = arr1(&[1000.0, 1100.0, 1050.0, 950.0, 1200.0, 1150.0, 1300.0]);
        let mdd = max_drawdown(&values).expect("Operation failed");

        // Maximum drawdown should be from peak of 1100 to trough of 950
        let expected = (1100.0 - 950.0) / 1100.0;
        assert!((mdd - expected).abs() < 1e-10);
    }

    #[test]
    fn test_drawdown_series() {
        let values = arr1(&[1000.0, 1100.0, 1050.0, 950.0, 1200.0]);
        let drawdowns = calculate_drawdown_series(&values).expect("Operation failed");

        assert_eq!(drawdowns.len(), values.len());

        // First value should have zero drawdown
        assert!(drawdowns[0] == 0.0);

        // Drawdowns should be non-positive
        for &dd in drawdowns.iter() {
            assert!(dd <= 0.0);
        }
    }

    #[test]
    fn test_pain_index() {
        let values = arr1(&[1000.0, 1100.0, 1050.0, 950.0, 1200.0]);
        let pain = pain_index(&values).expect("Operation failed");

        // Pain index should be positive (we convert drawdowns to positive)
        assert!(pain >= 0.0);
    }

    #[test]
    fn test_ulcer_index() {
        let values = arr1(&[1000.0, 1100.0, 1050.0, 950.0, 1200.0]);
        let ulcer = ulcer_index(&values).expect("Operation failed");

        // Ulcer index should be positive
        assert!(ulcer >= 0.0);
    }

    #[test]
    fn test_calmar_ratio() {
        let returns = arr1(&[0.10, -0.05, -0.10, 0.26, -0.04]);
        let values = arr1(&[1000.0, 1100.0, 1050.0, 950.0, 1200.0, 1150.0]);

        let result = calmar_ratio(&returns, &values, 252);
        assert!(result.is_ok());

        let calmar = result.expect("Operation failed");
        // Calmar ratio should be finite or infinity (if no drawdown)
        assert!(calmar.is_finite() || calmar.is_infinite());
    }

    #[test]
    fn test_drawdown_recovery_analysis() {
        let values = arr1(&[1000.0, 1100.0, 1050.0, 950.0, 1200.0, 1150.0, 1300.0]);
        let periods = drawdown_recovery_analysis(&values).expect("Operation failed");

        // Should have at least one drawdown period
        assert!(!periods.is_empty());

        // Check properties of first period
        if !periods.is_empty() {
            let first_period = &periods[0];
            assert!(first_period.max_drawdown > 0.0);
            assert!(first_period.duration > 0);
            assert!(first_period.peak_value >= first_period.trough_value);
        }
    }

    #[test]
    fn test_max_consecutive_losses() {
        let returns = arr1(&[0.01, -0.02, -0.01, -0.005, 0.02, -0.01, 0.03]);
        let max_losses = max_consecutive_losses(&returns);

        // Should be 3 consecutive losses in the middle
        assert_eq!(max_losses, 3);
    }

    #[test]
    fn test_no_drawdown() {
        let values = arr1(&[1000.0, 1100.0, 1200.0, 1300.0, 1400.0]);
        let mdd = max_drawdown(&values).expect("Operation failed");

        // Should be zero drawdown for monotonically increasing series
        assert!(mdd == 0.0);
    }

    #[test]
    fn test_empty_input() {
        let values: Array1<f64> = arr1(&[]);
        let result = max_drawdown(&values);
        assert!(result.is_err());
    }

    #[test]
    fn test_single_value() {
        let values = arr1(&[1000.0]);
        let mdd = max_drawdown(&values).expect("Operation failed");

        // Single value should have zero drawdown
        assert!(mdd == 0.0);
    }
}