scirs2-series 0.1.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
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
//! Core risk metrics for financial analysis
//!
//! This module provides fundamental risk measurement techniques including
//! Value at Risk (VaR), Conditional VaR, and various statistical risk metrics
//! commonly used in quantitative finance and risk management.
//!
//! # Overview
//!
//! Risk metrics are essential for:
//! - Regulatory compliance (Basel III, Solvency II)
//! - Portfolio risk management
//! - Performance evaluation
//! - Capital allocation decisions
//!
//! # Categories
//!
//! ## Value at Risk (VaR)
//! - **Historical Simulation**: Uses historical returns distribution
//! - **Parametric VaR**: Assumes normal distribution of returns
//! - **Monte Carlo VaR**: Uses Monte Carlo simulation
//! - **Rolling VaR**: Time-varying VaR estimation
//!
//! ## Expected Shortfall (ES)
//! Also known as Conditional VaR (CVaR), measures expected loss
//! beyond the VaR threshold.
//!
//! ## Risk-Adjusted Returns
//! - **Sharpe Ratio**: Risk-adjusted return using total risk
//! - **Sortino Ratio**: Risk-adjusted return using downside risk
//! - **Information Ratio**: Risk-adjusted active return
//!
//! ## Market Risk
//! - **Beta**: Systematic risk measure relative to market
//! - **Treynor Ratio**: Risk-adjusted return using systematic risk
//! - **Jensen's Alpha**: Risk-adjusted excess return
//!
//! # Examples
//!
//! ## Basic VaR Calculation
//! ```rust
//! use scirs2_series::financial::risk::metrics::{var_historical, expected_shortfall};
//! use scirs2_core::ndarray::array;
//!
//! let returns = array![-0.05, 0.02, -0.03, 0.01, -0.02, 0.03, -0.01, 0.04];
//! let confidence = 0.95; // 95% confidence level
//!
//! // Historical VaR
//! let var_95 = var_historical(&returns, confidence).expect("Test/example failed");
//! println!("95% VaR: {:.4}", var_95);
//!
//! // Expected Shortfall (Conditional VaR)
//! let es_95 = expected_shortfall(&returns, confidence).expect("Test/example failed");
//! println!("95% ES: {:.4}", es_95);
//! ```
//!
//! ## Risk-Adjusted Performance
//! ```rust
//! use scirs2_series::financial::risk::metrics::{sharpe_ratio, sortino_ratio};
//! use scirs2_core::ndarray::array;
//!
//! let returns = array![0.01, -0.02, 0.015, -0.008, 0.012, 0.005, -0.003];
//! let risk_free_rate = 0.02; // 2% annual
//! let periods_per_year = 252; // Daily data
//!
//! // Sharpe ratio
//! let sharpe = sharpe_ratio(&returns, risk_free_rate, periods_per_year).expect("Test/example failed");
//!
//! // Sortino ratio (uses downside deviation)
//! let sortino = sortino_ratio(&returns, risk_free_rate, periods_per_year).expect("Test/example failed");
//! ```
//!
//! ## Market Beta Analysis
//! ```rust
//! use scirs2_series::financial::risk::metrics::{beta, jensens_alpha};
//! use scirs2_core::ndarray::array;
//!
//! let asset_returns = array![0.01, -0.02, 0.015, -0.01, 0.005];
//! let market_returns = array![0.008, -0.015, 0.012, -0.008, 0.004];
//! let risk_free_rate = 0.02;
//! let periods_per_year = 252;
//!
//! // Calculate beta (systematic risk)
//! let beta_coef = beta(&asset_returns, &market_returns).expect("Test/example failed");
//!
//! // Calculate Jensen's alpha (risk-adjusted excess return)
//! let alpha = jensens_alpha(&asset_returns, &market_returns, risk_free_rate, periods_per_year).expect("Test/example failed");
//! ```

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

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

/// Helper to convert f64 constants to generic Float type
#[inline(always)]
fn const_f64<F: Float + scirs2_core::numeric::FromPrimitive>(value: f64) -> F {
    F::from(value).expect("Failed to convert constant to target float type")
}

/// Calculate Value at Risk (VaR) using historical simulation
///
/// Historical VaR uses the empirical distribution of historical returns
/// to estimate potential losses. This non-parametric approach makes no
/// assumptions about the return distribution.
///
/// # Arguments
///
/// * `returns` - Historical return series
/// * `confidence` - Confidence level (e.g., 0.95 for 95% VaR)
///
/// # Returns
///
/// * `Result<F>` - VaR as a positive number (loss magnitude)
///
/// # Examples
///
/// ```rust
/// use scirs2_series::financial::risk::metrics::var_historical;
/// use scirs2_core::ndarray::array;
///
/// let returns = array![-0.05, 0.02, -0.03, 0.01, -0.02, 0.03, -0.01, 0.04];
/// let var_95 = var_historical(&returns, 0.95).expect("Test/example failed");
/// ```
pub fn var_historical<F: Float + Clone>(returns: &Array1<F>, confidence: f64) -> Result<F> {
    if returns.is_empty() {
        return Err(TimeSeriesError::InvalidInput(
            "Returns cannot be empty".to_string(),
        ));
    }

    if confidence <= 0.0 || confidence >= 1.0 {
        return Err(TimeSeriesError::InvalidParameter {
            name: "confidence".to_string(),
            message: "Confidence must be between 0 and 1".to_string(),
        });
    }

    let mut sorted_returns = returns.to_vec();
    sorted_returns.sort_by(|a, b| a.partial_cmp(b).expect("Float comparison failed"));

    let index = ((1.0 - confidence) * sorted_returns.len() as f64) as usize;
    let index = index.min(sorted_returns.len() - 1);

    // Return as positive number (loss magnitude)
    Ok(-sorted_returns[index])
}

/// Calculate Expected Shortfall (Conditional VaR)
///
/// Expected Shortfall measures the average loss beyond the VaR threshold.
/// It provides information about tail risk and is a coherent risk measure.
///
/// # Arguments
///
/// * `returns` - Historical return series
/// * `confidence` - Confidence level (e.g., 0.95 for 95% ES)
///
/// # Returns
///
/// * `Result<F>` - Expected Shortfall as a positive number
///
/// # Examples
///
/// ```rust
/// use scirs2_series::financial::risk::metrics::expected_shortfall;
/// use scirs2_core::ndarray::array;
///
/// let returns = array![-0.05, 0.02, -0.03, 0.01, -0.02, 0.03, -0.01, 0.04];
/// let es_95 = expected_shortfall(&returns, 0.95).expect("Test/example failed");
/// ```
pub fn expected_shortfall<F: Float + Clone + std::iter::Sum>(
    returns: &Array1<F>,
    confidence: f64,
) -> Result<F> {
    let var = var_historical(returns, confidence)?;

    let tail_returns: Vec<F> = returns.iter().filter(|&&x| x <= -var).cloned().collect();

    if tail_returns.is_empty() {
        return Ok(var);
    }

    let sum = tail_returns.iter().fold(F::zero(), |acc, &x| acc + x);
    Ok(-(sum / F::from(tail_returns.len()).expect("Failed to convert length to float")))
}

/// Calculate Value at Risk using parametric method
///
/// Parametric VaR assumes returns follow a normal distribution and uses
/// the sample mean and standard deviation to estimate VaR.
///
/// # Arguments
///
/// * `returns` - Historical return series
/// * `confidence_level` - Confidence level (e.g., 0.95 for 95% VaR)
///
/// # Returns
///
/// * `Result<F>` - Parametric VaR as a positive number
///
/// # Examples
///
/// ```rust
/// use scirs2_series::financial::risk::metrics::parametric_var;
/// use scirs2_core::ndarray::array;
///
/// let returns = array![0.01, -0.02, 0.015, -0.01, 0.005];
/// let var = parametric_var(&returns, 0.95).expect("Test/example failed");
/// ```
pub fn parametric_var<F: Float + Clone + scirs2_core::numeric::FromPrimitive>(
    returns: &Array1<F>,
    confidence_level: F,
) -> Result<F> {
    if returns.is_empty() {
        return Err(TimeSeriesError::InvalidInput(
            "Returns array cannot be empty".to_string(),
        ));
    }

    let n = returns.len();
    let mean = returns.sum() / F::from(n).expect("Failed to convert to float");
    let variance = returns
        .iter()
        .map(|&r| (r - mean).powi(2))
        .fold(F::zero(), |acc, x| acc + x)
        / F::from(n - 1).expect("Failed to convert to float");
    let std_dev = variance.sqrt();

    // Normal distribution inverse CDF approximation
    let alpha = F::one() - confidence_level;
    let z_score = normal_inverse_cdf(alpha);
    let var = -(mean + z_score * std_dev);

    Ok(var)
}

/// Calculate Monte Carlo Value at Risk
///
/// Monte Carlo VaR uses simulation to generate potential future returns
/// and estimates VaR from the simulated distribution.
///
/// # Arguments
///
/// * `returns` - Historical return series for parameter estimation
/// * `confidence_level` - Confidence level
/// * `num_simulations` - Number of Monte Carlo simulations
///
/// # Returns
///
/// * `Result<F>` - Monte Carlo VaR as a positive number
pub fn monte_carlo_var<F: Float + Clone>(
    returns: &Array1<F>,
    confidence_level: F,
    num_simulations: usize,
) -> Result<F> {
    if returns.is_empty() {
        return Err(TimeSeriesError::InvalidInput(
            "Returns array cannot be empty".to_string(),
        ));
    }

    let n = returns.len();
    let mean = returns.sum() / F::from(n).expect("Failed to convert to float");
    let variance = returns
        .iter()
        .map(|&r| (r - mean).powi(2))
        .fold(F::zero(), |acc, x| acc + x)
        / F::from(n - 1).expect("Failed to convert to float");
    let std_dev = variance.sqrt();

    // Simple Monte Carlo simulation using normal distribution
    let mut simulated_returns = Vec::with_capacity(num_simulations);

    // Simple random number generation (in practice, use proper RNG)
    let mut seed = 12345u64;
    for _ in 0..num_simulations {
        // Simple LCG for demonstration
        seed = seed.wrapping_mul(1664525).wrapping_add(1013904223);
        let u1 = (seed as f64) / (u64::MAX as f64);

        seed = seed.wrapping_mul(1664525).wrapping_add(1013904223);
        let u2 = (seed as f64) / (u64::MAX as f64);

        // Box-Muller transformation
        let z = (-2.0 * u1.ln()).sqrt() * (2.0 * std::f64::consts::PI * u2).cos();
        let simulated_return = mean + std_dev * F::from(z).expect("Failed to convert to float");
        simulated_returns.push(simulated_return);
    }

    // Sort and find VaR
    simulated_returns.sort_by(|a, b| a.partial_cmp(b).expect("Float comparison failed"));
    let var_index = ((F::one() - confidence_level)
        * F::from(num_simulations).expect("Failed to convert to float"))
    .to_usize()
    .expect("Test/example failed");
    let var = if var_index < simulated_returns.len() {
        -simulated_returns[var_index]
    } else {
        -simulated_returns[0]
    };

    Ok(var)
}

/// Calculate Sharpe ratio (excess return / volatility)
///
/// The Sharpe ratio measures risk-adjusted return by dividing excess return
/// by total volatility. Higher values indicate better risk-adjusted performance.
///
/// # Arguments
///
/// * `returns` - Return series
/// * `risk_free_rate` - Annual risk-free rate
/// * `periods_per_year` - Number of periods per year (252 for daily, 12 for monthly)
///
/// # Returns
///
/// * `Result<F>` - Annualized Sharpe ratio
///
/// # Examples
///
/// ```rust
/// use scirs2_series::financial::risk::metrics::sharpe_ratio;
/// use scirs2_core::ndarray::array;
///
/// let returns = array![0.01, -0.02, 0.015, -0.008, 0.012];
/// let sharpe = sharpe_ratio(&returns, 0.02, 252).expect("Test/example failed");
/// ```
pub fn sharpe_ratio<F: Float + Clone>(
    returns: &Array1<F>,
    risk_free_rate: F,
    periods_per_year: usize,
) -> Result<F> {
    if returns.is_empty() {
        return Err(TimeSeriesError::InvalidInput(
            "Returns cannot be empty".to_string(),
        ));
    }

    // Calculate excess returns
    let annualized_rf =
        risk_free_rate / F::from(periods_per_year).expect("Failed to convert to float");
    let excess_returns: Array1<F> = returns.mapv(|r| r - annualized_rf);

    // Calculate mean excess return
    let mean_excess = excess_returns.sum() / F::from(returns.len()).expect("Test/example failed");

    // Calculate standard deviation of excess returns
    let variance = excess_returns.mapv(|r| (r - mean_excess).powi(2)).sum()
        / F::from(returns.len() - 1).expect("Test/example failed");

    let std_dev = variance.sqrt();

    if std_dev == F::zero() {
        Ok(F::infinity())
    } else {
        // Annualize the ratio
        let annualized_excess =
            mean_excess * F::from(periods_per_year).expect("Failed to convert to float");
        let annualized_std = std_dev
            * F::from(periods_per_year)
                .expect("Failed to convert to float")
                .sqrt();
        Ok(annualized_excess / annualized_std)
    }
}

/// Calculate Sortino ratio (excess return / downside deviation)
///
/// The Sortino ratio modifies the Sharpe ratio by using downside deviation
/// instead of total volatility, focusing only on harmful volatility.
///
/// # Arguments
///
/// * `returns` - Return series
/// * `risk_free_rate` - Annual risk-free rate
/// * `periods_per_year` - Number of periods per year
///
/// # Returns
///
/// * `Result<F>` - Annualized Sortino ratio
pub fn sortino_ratio<F: Float + Clone>(
    returns: &Array1<F>,
    risk_free_rate: F,
    periods_per_year: usize,
) -> Result<F> {
    if returns.is_empty() {
        return Err(TimeSeriesError::InvalidInput(
            "Returns cannot be empty".to_string(),
        ));
    }

    // Calculate excess returns
    let annualized_rf =
        risk_free_rate / F::from(periods_per_year).expect("Failed to convert to float");
    let excess_returns: Array1<F> = returns.mapv(|r| r - annualized_rf);

    // Calculate mean excess return
    let mean_excess = excess_returns.sum() / F::from(returns.len()).expect("Test/example failed");

    // Calculate downside deviation (only negative excess returns)
    let downside_returns: Vec<F> = excess_returns
        .iter()
        .filter(|&&r| r < F::zero())
        .cloned()
        .collect();

    if downside_returns.is_empty() {
        return Ok(F::infinity());
    }

    let downside_variance = downside_returns
        .iter()
        .map(|&r| r.powi(2))
        .fold(F::zero(), |acc, x| acc + x)
        / F::from(downside_returns.len()).expect("Test/example failed");

    let downside_deviation = downside_variance.sqrt();

    if downside_deviation == F::zero() {
        Ok(F::infinity())
    } else {
        // Annualize the ratio
        let annualized_excess =
            mean_excess * F::from(periods_per_year).expect("Failed to convert to float");
        let annualized_downside = downside_deviation
            * F::from(periods_per_year)
                .expect("Failed to convert to float")
                .sqrt();
        Ok(annualized_excess / annualized_downside)
    }
}

/// Calculate Information ratio (active return / tracking error)
///
/// The Information ratio measures the risk-adjusted active return relative
/// to a benchmark. It shows how much excess return is generated per unit
/// of tracking error.
///
/// # Arguments
///
/// * `portfolio_returns` - Portfolio return series
/// * `benchmark_returns` - Benchmark return series
///
/// # Returns
///
/// * `Result<F>` - Information ratio
pub fn information_ratio<F: Float + Clone>(
    portfolio_returns: &Array1<F>,
    benchmark_returns: &Array1<F>,
) -> Result<F> {
    if portfolio_returns.len() != benchmark_returns.len() {
        return Err(TimeSeriesError::DimensionMismatch {
            expected: portfolio_returns.len(),
            actual: benchmark_returns.len(),
        });
    }

    if portfolio_returns.is_empty() {
        return Err(TimeSeriesError::InvalidInput(
            "Returns cannot be empty".to_string(),
        ));
    }

    // Calculate active returns (portfolio - benchmark)
    let active_returns: Array1<F> = portfolio_returns
        .iter()
        .zip(benchmark_returns.iter())
        .map(|(&p, &b)| p - b)
        .collect();

    // Calculate mean active return
    let mean_active =
        active_returns.sum() / F::from(active_returns.len()).expect("Test/example failed");

    // Calculate tracking error (standard deviation of active returns)
    let variance = active_returns.mapv(|r| (r - mean_active).powi(2)).sum()
        / F::from(active_returns.len() - 1).expect("Test/example failed");

    let tracking_error = variance.sqrt();

    if tracking_error == F::zero() {
        Ok(F::infinity())
    } else {
        Ok(mean_active / tracking_error)
    }
}

/// Calculate Beta coefficient (systematic risk measure)
///
/// Beta measures the systematic risk of an asset relative to the market.
/// Beta > 1 indicates higher volatility than market, Beta < 1 indicates lower.
///
/// # Arguments
///
/// * `asset_returns` - Asset return series
/// * `market_returns` - Market return series
///
/// # Returns
///
/// * `Result<F>` - Beta coefficient
pub fn beta<F: Float + Clone>(asset_returns: &Array1<F>, market_returns: &Array1<F>) -> Result<F> {
    if asset_returns.len() != market_returns.len() {
        return Err(TimeSeriesError::DimensionMismatch {
            expected: asset_returns.len(),
            actual: market_returns.len(),
        });
    }

    if asset_returns.len() < 2 {
        return Err(TimeSeriesError::InsufficientData {
            message: "Need at least 2 observations for beta calculation".to_string(),
            required: 2,
            actual: asset_returns.len(),
        });
    }

    // Calculate means
    let asset_mean =
        asset_returns.sum() / F::from(asset_returns.len()).expect("Test/example failed");
    let market_mean =
        market_returns.sum() / F::from(market_returns.len()).expect("Test/example failed");

    // Calculate covariance and market variance
    let mut covariance = F::zero();
    let mut market_variance = F::zero();

    for i in 0..asset_returns.len() {
        let asset_dev = asset_returns[i] - asset_mean;
        let market_dev = market_returns[i] - market_mean;

        covariance = covariance + asset_dev * market_dev;
        market_variance = market_variance + market_dev.powi(2);
    }

    let n = F::from(asset_returns.len() - 1).expect("Test/example failed");
    covariance = covariance / n;
    market_variance = market_variance / n;

    if market_variance == F::zero() {
        Err(TimeSeriesError::InvalidInput(
            "Market returns have zero variance".to_string(),
        ))
    } else {
        Ok(covariance / market_variance)
    }
}

/// Calculate Treynor ratio (excess return / beta)
///
/// The Treynor ratio measures risk-adjusted return using systematic risk (beta)
/// instead of total risk. It shows excess return per unit of systematic risk.
///
/// # Arguments
///
/// * `returns` - Asset return series
/// * `market_returns` - Market return series
/// * `risk_free_rate` - Annual risk-free rate
/// * `periods_per_year` - Number of periods per year
///
/// # Returns
///
/// * `Result<F>` - Treynor ratio
pub fn treynor_ratio<F: Float + Clone>(
    returns: &Array1<F>,
    market_returns: &Array1<F>,
    risk_free_rate: F,
    periods_per_year: usize,
) -> Result<F> {
    // Calculate portfolio beta
    let portfolio_beta = beta(returns, market_returns)?;

    if portfolio_beta == F::zero() {
        return Ok(F::infinity());
    }

    // Calculate annualized excess return
    let annualized_rf =
        risk_free_rate / F::from(periods_per_year).expect("Failed to convert to float");
    let mean_return = returns.sum() / F::from(returns.len()).expect("Test/example failed");
    let excess_return = mean_return - annualized_rf;
    let annualized_excess =
        excess_return * F::from(periods_per_year).expect("Failed to convert to float");

    Ok(annualized_excess / portfolio_beta)
}

/// Calculate Jensen's alpha (risk-adjusted excess return)
///
/// Jensen's alpha measures the excess return of a portfolio over what would
/// be expected given its beta and the market return (CAPM prediction).
///
/// # Arguments
///
/// * `returns` - Portfolio return series
/// * `market_returns` - Market return series
/// * `risk_free_rate` - Annual risk-free rate
/// * `periods_per_year` - Number of periods per year
///
/// # Returns
///
/// * `Result<F>` - Jensen's alpha (annualized)
pub fn jensens_alpha<F: Float + Clone>(
    returns: &Array1<F>,
    market_returns: &Array1<F>,
    risk_free_rate: F,
    periods_per_year: usize,
) -> Result<F> {
    // Calculate portfolio beta
    let portfolio_beta = beta(returns, market_returns)?;

    // Calculate mean returns
    let annualized_rf =
        risk_free_rate / F::from(periods_per_year).expect("Failed to convert to float");
    let mean_portfolio = returns.sum() / F::from(returns.len()).expect("Test/example failed");
    let mean_market =
        market_returns.sum() / F::from(market_returns.len()).expect("Test/example failed");

    // Calculate alpha using CAPM formula
    // Alpha = Portfolio Return - (Risk Free Rate + Beta * (Market Return - Risk Free Rate))
    let portfolio_excess = mean_portfolio - annualized_rf;
    let market_excess = mean_market - annualized_rf;
    let expected_excess = portfolio_beta * market_excess;

    Ok((portfolio_excess - expected_excess)
        * F::from(periods_per_year).expect("Failed to convert to float"))
}

/// Calculate Omega ratio (probability-weighted gains over losses)
///
/// The Omega ratio considers the entire return distribution and measures
/// the probability-weighted gains above a threshold relative to
/// probability-weighted losses below the threshold.
///
/// # Arguments
///
/// * `returns` - Return series
/// * `threshold` - Threshold return (often risk-free rate)
///
/// # Returns
///
/// * `Result<F>` - Omega ratio
pub fn omega_ratio<F: Float + Clone>(returns: &Array1<F>, threshold: F) -> Result<F> {
    if returns.is_empty() {
        return Err(TimeSeriesError::InvalidInput(
            "Returns cannot be empty".to_string(),
        ));
    }

    let mut gains = F::zero();
    let mut losses = F::zero();

    for &ret in returns.iter() {
        let excess = ret - threshold;
        if excess > F::zero() {
            gains = gains + excess;
        } else {
            losses = losses - excess; // Make positive
        }
    }

    if losses == F::zero() {
        Ok(F::infinity())
    } else {
        Ok(gains / losses)
    }
}

/// Approximate normal inverse CDF using Beasley-Springer-Moro algorithm
fn normal_inverse_cdf<F: Float + scirs2_core::numeric::FromPrimitive>(p: F) -> F {
    let a0 = const_f64::<F>(2.515517);
    let a1 = const_f64::<F>(0.802853);
    let a2 = const_f64::<F>(0.010328);
    let b1 = const_f64::<F>(1.432788);
    let b2 = const_f64::<F>(0.189269);
    let b3 = const_f64::<F>(0.001308);

    let t = if p < const_f64::<F>(0.5) {
        (-const_f64::<F>(2.0) * (p.ln())).sqrt()
    } else {
        (-const_f64::<F>(2.0) * ((F::one() - p).ln())).sqrt()
    };

    let numerator = a0 + a1 * t + a2 * t.powi(2);
    let denominator = F::one() + b1 * t + b2 * t.powi(2) + b3 * t.powi(3);
    let z = t - numerator / denominator;

    if p < const_f64::<F>(0.5) {
        -z
    } else {
        z
    }
}

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

    #[test]
    fn test_var_historical() {
        let returns = arr1(&[-0.05, 0.02, -0.03, 0.01, -0.02, 0.03, -0.01, 0.04]);
        let var = var_historical(&returns, 0.95).expect("Test/example failed");

        // VaR should be positive (loss magnitude)
        assert!(var > 0.0);

        // VaR should be reasonable for this data
        assert!(var < 0.1);
    }

    #[test]
    fn test_expected_shortfall() {
        let returns = arr1(&[-0.05, 0.02, -0.03, 0.01, -0.02, 0.03, -0.01, 0.04]);
        let var = var_historical(&returns, 0.95).expect("Test/example failed");
        let es = expected_shortfall(&returns, 0.95).expect("Test/example failed");

        // ES should be >= VaR
        assert!(es >= var);
        assert!(es > 0.0);
    }

    #[test]
    fn test_parametric_var() {
        let returns = arr1(&[0.01, -0.02, 0.015, -0.01, 0.005]);
        let var = parametric_var(&returns, 0.95).expect("Test/example failed");

        assert!(var > 0.0);
    }

    #[test]
    fn test_sharpe_ratio() {
        let returns = arr1(&[0.01, -0.02, 0.015, -0.008, 0.012, 0.005, -0.003]);
        let result = sharpe_ratio(&returns, 0.02, 252);
        assert!(result.is_ok());

        let sharpe = result.expect("Test/example failed");
        // Sharpe ratio should be finite
        assert!(sharpe.is_finite());
    }

    #[test]
    fn test_sortino_ratio() {
        let returns = arr1(&[0.01, -0.02, 0.015, -0.008, 0.012, 0.005, -0.003]);
        let result = sortino_ratio(&returns, 0.02, 252);
        assert!(result.is_ok());

        let sortino = result.expect("Test/example failed");
        // Sortino ratio should be finite or infinity (if no downside)
        assert!(sortino.is_finite() || sortino.is_infinite());
    }

    #[test]
    fn test_beta() {
        let asset_returns = arr1(&[0.01, -0.02, 0.015, -0.01, 0.005]);
        let market_returns = arr1(&[0.008, -0.015, 0.012, -0.008, 0.004]);

        let result = beta(&asset_returns, &market_returns);
        assert!(result.is_ok());

        let beta_coef = result.expect("Test/example failed");
        // Beta should be reasonable (typically between -3 and 3)
        assert!(beta_coef.abs() < 5.0);
    }

    #[test]
    fn test_information_ratio() {
        let portfolio_returns = arr1(&[0.01, -0.02, 0.015, -0.01, 0.005]);
        let benchmark_returns = arr1(&[0.008, -0.018, 0.012, -0.009, 0.003]);

        let result = information_ratio(&portfolio_returns, &benchmark_returns);
        assert!(result.is_ok());

        let ir = result.expect("Test/example failed");
        assert!(ir.is_finite() || ir.is_infinite());
    }

    #[test]
    fn test_jensens_alpha() {
        let returns = arr1(&[0.01, -0.02, 0.015, -0.01, 0.005]);
        let market_returns = arr1(&[0.008, -0.015, 0.012, -0.008, 0.004]);

        let result = jensens_alpha(&returns, &market_returns, 0.02, 252);
        assert!(result.is_ok());

        let alpha = result.expect("Test/example failed");
        // Alpha should be finite
        assert!(alpha.is_finite());
    }

    #[test]
    fn test_omega_ratio() {
        let returns = arr1(&[0.01, -0.02, 0.015, -0.008, 0.012, 0.005, -0.003]);
        let threshold = 0.0;

        let result = omega_ratio(&returns, threshold);
        assert!(result.is_ok());

        let omega = result.expect("Test/example failed");
        assert!(omega > 0.0);
    }

    #[test]
    fn test_dimension_mismatch() {
        let returns1 = arr1(&[0.01, -0.02]);
        let returns2 = arr1(&[0.008, -0.015, 0.012]);

        let result = beta(&returns1, &returns2);
        assert!(result.is_err());
    }

    #[test]
    fn test_insufficient_data() {
        let returns = arr1(&[0.01]);
        let market_returns = arr1(&[0.008]);

        let result = beta(&returns, &market_returns);
        assert!(result.is_err());
    }

    #[test]
    fn test_invalid_confidence() {
        let returns = arr1(&[0.01, -0.02, 0.015]);

        // Invalid confidence > 1
        let result = var_historical(&returns, 1.1);
        assert!(result.is_err());

        // Invalid confidence <= 0
        let result = var_historical(&returns, 0.0);
        assert!(result.is_err());
    }
}