kaccy-core 0.2.0

Core business logic for Kaccy Protocol - batching, fee optimization, and transaction management
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
//! Performance benchmarking against indices with alpha, beta, information ratio, and tracking error

use chrono::{DateTime, Utc};
use rust_decimal::Decimal;
use rust_decimal_macros::dec;
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;

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

/// Price point with timestamp
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PricePoint {
    /// Time of this price observation.
    pub timestamp: DateTime<Utc>,
    /// Observed price value.
    pub price: Decimal,
}

/// Return series for a portfolio or benchmark
#[derive(Debug, Clone)]
pub struct ReturnSeries {
    /// Timestamps
    pub timestamps: Vec<DateTime<Utc>>,
    /// Returns (as decimals, e.g., 0.05 for 5%)
    pub returns: Vec<Decimal>,
}

impl ReturnSeries {
    /// Create from price points
    pub fn from_prices(prices: Vec<PricePoint>) -> Result<Self> {
        if prices.len() < 2 {
            return Err(CoreError::Validation(
                "Need at least 2 price points".to_string(),
            ));
        }

        let mut timestamps = Vec::new();
        let mut returns = Vec::new();

        for i in 1..prices.len() {
            let prev = &prices[i - 1];
            let curr = &prices[i];

            if prev.price == Decimal::ZERO {
                return Err(CoreError::Validation("Price cannot be zero".to_string()));
            }

            let ret = (curr.price - prev.price) / prev.price;
            timestamps.push(curr.timestamp);
            returns.push(ret);
        }

        Ok(Self {
            timestamps,
            returns,
        })
    }

    /// Calculate mean return
    pub fn mean(&self) -> Decimal {
        if self.returns.is_empty() {
            return Decimal::ZERO;
        }

        let sum: Decimal = self.returns.iter().sum();
        sum / Decimal::from(self.returns.len())
    }

    /// Calculate variance
    pub fn variance(&self) -> Decimal {
        if self.returns.len() < 2 {
            return Decimal::ZERO;
        }

        let mean = self.mean();
        let squared_diffs: Decimal = self.returns.iter().map(|r| (*r - mean) * (*r - mean)).sum();

        squared_diffs / Decimal::from(self.returns.len() - 1)
    }

    /// Calculate standard deviation
    pub fn std_dev(&self) -> Decimal {
        let variance = self.variance();
        // Approximation for sqrt using Newton's method
        sqrt_decimal(variance)
    }

    /// Calculate covariance with another series
    pub fn covariance(&self, other: &ReturnSeries) -> Result<Decimal> {
        if self.returns.len() != other.returns.len() {
            return Err(CoreError::Validation(
                "Return series must have same length".to_string(),
            ));
        }

        if self.returns.len() < 2 {
            return Ok(Decimal::ZERO);
        }

        let mean_self = self.mean();
        let mean_other = other.mean();

        let cov: Decimal = self
            .returns
            .iter()
            .zip(other.returns.iter())
            .map(|(r1, r2)| (*r1 - mean_self) * (*r2 - mean_other))
            .sum();

        Ok(cov / Decimal::from(self.returns.len() - 1))
    }

    /// Calculate correlation with another series
    pub fn correlation(&self, other: &ReturnSeries) -> Result<Decimal> {
        let cov = self.covariance(other)?;
        let std_self = self.std_dev();
        let std_other = other.std_dev();

        if std_self == Decimal::ZERO || std_other == Decimal::ZERO {
            return Ok(Decimal::ZERO);
        }

        Ok(cov / (std_self * std_other))
    }
}

/// Square root approximation using Newton's method
fn sqrt_decimal(value: Decimal) -> Decimal {
    if value == Decimal::ZERO {
        return Decimal::ZERO;
    }

    if value < Decimal::ZERO {
        return Decimal::ZERO; // Return 0 for negative values
    }

    let mut x = value;
    let two = dec!(2.0);

    // Newton's method: x_{n+1} = (x_n + value/x_n) / 2
    for _ in 0..20 {
        let x_next = (x + value / x) / two;
        if (x_next - x).abs() < dec!(0.000001) {
            break;
        }
        x = x_next;
    }

    x
}

/// Benchmark index for comparison
#[derive(Debug, Clone)]
pub struct BenchmarkIndex {
    /// Index name (e.g., "S&P 500", "Bitcoin", "Custom Index")
    pub name: String,
    /// Return series for the index
    pub returns: ReturnSeries,
}

impl BenchmarkIndex {
    /// Create from price points
    pub fn from_prices(name: String, prices: Vec<PricePoint>) -> Result<Self> {
        let returns = ReturnSeries::from_prices(prices)?;
        Ok(Self { name, returns })
    }

    /// Calculate beta relative to this benchmark
    pub fn calculate_beta(&self, portfolio: &ReturnSeries) -> Result<Decimal> {
        let cov = portfolio.covariance(&self.returns)?;
        let variance = self.returns.variance();

        if variance == Decimal::ZERO {
            return Ok(Decimal::ZERO);
        }

        Ok(cov / variance)
    }

    /// Calculate alpha relative to this benchmark
    pub fn calculate_alpha(
        &self,
        portfolio: &ReturnSeries,
        risk_free_rate: Decimal,
    ) -> Result<Decimal> {
        let beta = self.calculate_beta(portfolio)?;
        let portfolio_return = portfolio.mean();
        let benchmark_return = self.returns.mean();

        // Alpha = Portfolio Return - [Risk-Free Rate + Beta * (Benchmark Return - Risk-Free Rate)]
        let alpha =
            portfolio_return - (risk_free_rate + beta * (benchmark_return - risk_free_rate));

        Ok(alpha)
    }
}

/// Performance attribution result
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PerformanceAttribution {
    /// Portfolio return
    pub portfolio_return: Decimal,
    /// Benchmark return
    pub benchmark_return: Decimal,
    /// Alpha (excess return)
    pub alpha: Decimal,
    /// Beta (systematic risk)
    pub beta: Decimal,
    /// Correlation with benchmark
    pub correlation: Decimal,
    /// R-squared (proportion of variance explained by benchmark)
    pub r_squared: Decimal,
    /// Active return (portfolio - benchmark)
    pub active_return: Decimal,
}

/// Information ratio calculator
#[derive(Debug, Clone)]
pub struct InformationRatioCalculator;

impl InformationRatioCalculator {
    /// Calculate information ratio
    /// IR = Active Return / Tracking Error
    pub fn calculate(
        portfolio: &ReturnSeries,
        benchmark: &ReturnSeries,
    ) -> Result<InformationRatio> {
        if portfolio.returns.len() != benchmark.returns.len() {
            return Err(CoreError::Validation(
                "Portfolio and benchmark must have same length".to_string(),
            ));
        }

        // Calculate active returns (portfolio - benchmark)
        let active_returns: Vec<Decimal> = portfolio
            .returns
            .iter()
            .zip(benchmark.returns.iter())
            .map(|(p, b)| *p - *b)
            .collect();

        let active_return_series = ReturnSeries {
            timestamps: portfolio.timestamps.clone(),
            returns: active_returns,
        };

        let active_return = active_return_series.mean();
        let tracking_error = active_return_series.std_dev();

        let information_ratio = if tracking_error == Decimal::ZERO {
            Decimal::ZERO
        } else {
            active_return / tracking_error
        };

        Ok(InformationRatio {
            active_return,
            tracking_error,
            information_ratio,
        })
    }
}

/// Information ratio result
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct InformationRatio {
    /// Active return (portfolio - benchmark)
    pub active_return: Decimal,
    /// Tracking error (std dev of active returns)
    pub tracking_error: Decimal,
    /// Information ratio
    pub information_ratio: Decimal,
}

/// Tracking error calculator
#[derive(Debug, Clone)]
pub struct TrackingErrorCalculator;

impl TrackingErrorCalculator {
    /// Calculate tracking error (standard deviation of active returns)
    pub fn calculate(portfolio: &ReturnSeries, benchmark: &ReturnSeries) -> Result<TrackingError> {
        if portfolio.returns.len() != benchmark.returns.len() {
            return Err(CoreError::Validation(
                "Portfolio and benchmark must have same length".to_string(),
            ));
        }

        // Calculate active returns (portfolio - benchmark)
        let active_returns: Vec<Decimal> = portfolio
            .returns
            .iter()
            .zip(benchmark.returns.iter())
            .map(|(p, b)| *p - *b)
            .collect();

        let active_return_series = ReturnSeries {
            timestamps: portfolio.timestamps.clone(),
            returns: active_returns.clone(),
        };

        let mean_active_return = active_return_series.mean();
        let tracking_error = active_return_series.std_dev();

        // Calculate max deviation
        let max_deviation = active_returns
            .iter()
            .map(|r| (*r - mean_active_return).abs())
            .max()
            .unwrap_or(Decimal::ZERO);

        // Calculate upside and downside tracking error
        let upside_returns: Vec<Decimal> = active_returns
            .iter()
            .filter(|r| **r > Decimal::ZERO)
            .copied()
            .collect();

        let downside_returns: Vec<Decimal> = active_returns
            .iter()
            .filter(|r| **r < Decimal::ZERO)
            .copied()
            .collect();

        let upside_te = if !upside_returns.is_empty() {
            let upside_series = ReturnSeries {
                timestamps: Vec::new(),
                returns: upside_returns,
            };
            upside_series.std_dev()
        } else {
            Decimal::ZERO
        };

        let downside_te = if !downside_returns.is_empty() {
            let downside_series = ReturnSeries {
                timestamps: Vec::new(),
                returns: downside_returns,
            };
            downside_series.std_dev()
        } else {
            Decimal::ZERO
        };

        Ok(TrackingError {
            tracking_error,
            mean_active_return,
            max_deviation,
            upside_tracking_error: upside_te,
            downside_tracking_error: downside_te,
        })
    }

    /// Calculate annualized tracking error
    pub fn annualized(
        portfolio: &ReturnSeries,
        benchmark: &ReturnSeries,
        periods_per_year: usize,
    ) -> Result<Decimal> {
        let te = Self::calculate(portfolio, benchmark)?;

        // Annualize: TE_annual = TE_period * sqrt(periods_per_year)
        let multiplier = sqrt_decimal(Decimal::from(periods_per_year));
        Ok(te.tracking_error * multiplier)
    }
}

/// Tracking error result
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TrackingError {
    /// Tracking error (std dev of active returns)
    pub tracking_error: Decimal,
    /// Mean active return
    pub mean_active_return: Decimal,
    /// Maximum deviation from benchmark
    pub max_deviation: Decimal,
    /// Upside tracking error (volatility of positive active returns)
    pub upside_tracking_error: Decimal,
    /// Downside tracking error (volatility of negative active returns)
    pub downside_tracking_error: Decimal,
}

/// Performance benchmark analyzer
pub struct PerformanceBenchmark {
    /// Benchmark indices
    benchmarks: BTreeMap<String, BenchmarkIndex>,
    /// Risk-free rate (annualized)
    risk_free_rate: Decimal,
}

impl PerformanceBenchmark {
    /// Create new performance benchmark analyzer
    pub fn new(risk_free_rate: Decimal) -> Self {
        Self {
            benchmarks: BTreeMap::new(),
            risk_free_rate,
        }
    }

    /// Add a benchmark index
    pub fn add_benchmark(&mut self, benchmark: BenchmarkIndex) {
        self.benchmarks.insert(benchmark.name.clone(), benchmark);
    }

    /// Analyze portfolio against a benchmark
    pub fn analyze(
        &self,
        portfolio: &ReturnSeries,
        benchmark_name: &str,
    ) -> Result<PerformanceAttribution> {
        let benchmark = self.benchmarks.get(benchmark_name).ok_or_else(|| {
            CoreError::NotFound(format!("Benchmark '{}' not found", benchmark_name))
        })?;

        let beta = benchmark.calculate_beta(portfolio)?;
        let alpha = benchmark.calculate_alpha(portfolio, self.risk_free_rate)?;
        let correlation = portfolio.correlation(&benchmark.returns)?;
        let r_squared = correlation * correlation;

        let portfolio_return = portfolio.mean();
        let benchmark_return = benchmark.returns.mean();
        let active_return = portfolio_return - benchmark_return;

        Ok(PerformanceAttribution {
            portfolio_return,
            benchmark_return,
            alpha,
            beta,
            correlation,
            r_squared,
            active_return,
        })
    }

    /// Calculate information ratio against a benchmark
    pub fn information_ratio(
        &self,
        portfolio: &ReturnSeries,
        benchmark_name: &str,
    ) -> Result<InformationRatio> {
        let benchmark = self.benchmarks.get(benchmark_name).ok_or_else(|| {
            CoreError::NotFound(format!("Benchmark '{}' not found", benchmark_name))
        })?;

        InformationRatioCalculator::calculate(portfolio, &benchmark.returns)
    }

    /// Calculate tracking error against a benchmark
    pub fn tracking_error(
        &self,
        portfolio: &ReturnSeries,
        benchmark_name: &str,
    ) -> Result<TrackingError> {
        let benchmark = self.benchmarks.get(benchmark_name).ok_or_else(|| {
            CoreError::NotFound(format!("Benchmark '{}' not found", benchmark_name))
        })?;

        TrackingErrorCalculator::calculate(portfolio, &benchmark.returns)
    }

    /// Comprehensive analysis against all benchmarks
    pub fn comprehensive_analysis(
        &self,
        portfolio: &ReturnSeries,
    ) -> Result<
        Vec<(
            String,
            PerformanceAttribution,
            InformationRatio,
            TrackingError,
        )>,
    > {
        let mut results = Vec::new();

        for name in self.benchmarks.keys() {
            let attribution = self.analyze(portfolio, name)?;
            let ir = self.information_ratio(portfolio, name)?;
            let te = self.tracking_error(portfolio, name)?;

            results.push((name.clone(), attribution, ir, te));
        }

        Ok(results)
    }

    /// Get list of available benchmarks
    pub fn available_benchmarks(&self) -> Vec<String> {
        self.benchmarks.keys().cloned().collect()
    }
}

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

    fn create_test_prices() -> Vec<PricePoint> {
        vec![
            PricePoint {
                timestamp: Utc::now(),
                price: dec!(100.0),
            },
            PricePoint {
                timestamp: Utc::now(),
                price: dec!(105.0),
            },
            PricePoint {
                timestamp: Utc::now(),
                price: dec!(103.0),
            },
            PricePoint {
                timestamp: Utc::now(),
                price: dec!(108.0),
            },
            PricePoint {
                timestamp: Utc::now(),
                price: dec!(107.0),
            },
        ]
    }

    #[test]
    fn test_return_series_from_prices() {
        let prices = create_test_prices();
        let returns = ReturnSeries::from_prices(prices).unwrap();

        assert_eq!(returns.returns.len(), 4);
        // First return: (105 - 100) / 100 = 0.05
        assert_eq!(returns.returns[0], dec!(0.05));
    }

    #[test]
    fn test_return_series_mean() {
        let returns = ReturnSeries {
            timestamps: vec![Utc::now(); 3],
            returns: vec![dec!(0.05), dec!(0.02), dec!(-0.01)],
        };

        let mean = returns.mean();
        assert!((mean - dec!(0.02)).abs() < dec!(0.0001));
    }

    #[test]
    fn test_return_series_std_dev() {
        let returns = ReturnSeries {
            timestamps: vec![Utc::now(); 3],
            returns: vec![dec!(0.05), dec!(0.02), dec!(-0.01)],
        };

        let std_dev = returns.std_dev();
        assert!(std_dev > Decimal::ZERO);
    }

    #[test]
    fn test_return_series_covariance() {
        let returns1 = ReturnSeries {
            timestamps: vec![Utc::now(); 3],
            returns: vec![dec!(0.05), dec!(0.02), dec!(-0.01)],
        };

        let returns2 = ReturnSeries {
            timestamps: vec![Utc::now(); 3],
            returns: vec![dec!(0.04), dec!(0.03), dec!(0.01)],
        };

        let cov = returns1.covariance(&returns2).unwrap();
        assert!(cov != Decimal::ZERO);
    }

    #[test]
    fn test_benchmark_beta() {
        let benchmark_prices = create_test_prices();
        let benchmark =
            BenchmarkIndex::from_prices("Test Index".to_string(), benchmark_prices).unwrap();

        let portfolio_prices = vec![
            PricePoint {
                timestamp: Utc::now(),
                price: dec!(100.0),
            },
            PricePoint {
                timestamp: Utc::now(),
                price: dec!(110.0),
            },
            PricePoint {
                timestamp: Utc::now(),
                price: dec!(106.0),
            },
            PricePoint {
                timestamp: Utc::now(),
                price: dec!(116.0),
            },
            PricePoint {
                timestamp: Utc::now(),
                price: dec!(114.0),
            },
        ];

        let portfolio = ReturnSeries::from_prices(portfolio_prices).unwrap();
        let beta = benchmark.calculate_beta(&portfolio).unwrap();

        assert!(beta > Decimal::ZERO);
    }

    #[test]
    fn test_benchmark_alpha() {
        let benchmark_prices = create_test_prices();
        let benchmark =
            BenchmarkIndex::from_prices("Test Index".to_string(), benchmark_prices).unwrap();

        let portfolio_prices = vec![
            PricePoint {
                timestamp: Utc::now(),
                price: dec!(100.0),
            },
            PricePoint {
                timestamp: Utc::now(),
                price: dec!(110.0),
            },
            PricePoint {
                timestamp: Utc::now(),
                price: dec!(106.0),
            },
            PricePoint {
                timestamp: Utc::now(),
                price: dec!(116.0),
            },
            PricePoint {
                timestamp: Utc::now(),
                price: dec!(114.0),
            },
        ];

        let portfolio = ReturnSeries::from_prices(portfolio_prices).unwrap();
        let _alpha = benchmark.calculate_alpha(&portfolio, dec!(0.02)).unwrap();

        // Alpha can be positive or negative - we just verify the calculation succeeds
    }

    #[test]
    fn test_information_ratio() {
        let portfolio = ReturnSeries {
            timestamps: vec![Utc::now(); 5],
            returns: vec![dec!(0.05), dec!(0.02), dec!(-0.01), dec!(0.03), dec!(0.01)],
        };

        let benchmark = ReturnSeries {
            timestamps: vec![Utc::now(); 5],
            returns: vec![dec!(0.04), dec!(0.03), dec!(0.01), dec!(0.02), dec!(0.02)],
        };

        let ir = InformationRatioCalculator::calculate(&portfolio, &benchmark).unwrap();

        assert!(ir.tracking_error >= Decimal::ZERO);
        assert!(ir.information_ratio != Decimal::ZERO || ir.tracking_error == Decimal::ZERO);
    }

    #[test]
    fn test_tracking_error() {
        let portfolio = ReturnSeries {
            timestamps: vec![Utc::now(); 5],
            returns: vec![dec!(0.05), dec!(0.02), dec!(-0.01), dec!(0.03), dec!(0.01)],
        };

        let benchmark = ReturnSeries {
            timestamps: vec![Utc::now(); 5],
            returns: vec![dec!(0.04), dec!(0.03), dec!(0.01), dec!(0.02), dec!(0.02)],
        };

        let te = TrackingErrorCalculator::calculate(&portfolio, &benchmark).unwrap();

        assert!(te.tracking_error >= Decimal::ZERO);
        assert!(te.max_deviation >= Decimal::ZERO);
    }

    #[test]
    fn test_performance_benchmark() {
        let mut benchmark_analyzer = PerformanceBenchmark::new(dec!(0.02));

        let benchmark_prices = create_test_prices();
        let benchmark =
            BenchmarkIndex::from_prices("S&P 500".to_string(), benchmark_prices).unwrap();
        benchmark_analyzer.add_benchmark(benchmark);

        let portfolio_prices = vec![
            PricePoint {
                timestamp: Utc::now(),
                price: dec!(100.0),
            },
            PricePoint {
                timestamp: Utc::now(),
                price: dec!(110.0),
            },
            PricePoint {
                timestamp: Utc::now(),
                price: dec!(106.0),
            },
            PricePoint {
                timestamp: Utc::now(),
                price: dec!(116.0),
            },
            PricePoint {
                timestamp: Utc::now(),
                price: dec!(114.0),
            },
        ];

        let portfolio = ReturnSeries::from_prices(portfolio_prices).unwrap();
        let attribution = benchmark_analyzer.analyze(&portfolio, "S&P 500").unwrap();

        assert!(attribution.beta != Decimal::ZERO);
        assert!(attribution.r_squared >= Decimal::ZERO);
        assert!(attribution.r_squared <= dec!(1.0));
    }

    #[test]
    fn test_comprehensive_analysis() {
        let mut benchmark_analyzer = PerformanceBenchmark::new(dec!(0.02));

        let benchmark1 =
            BenchmarkIndex::from_prices("Index 1".to_string(), create_test_prices()).unwrap();
        let benchmark2 =
            BenchmarkIndex::from_prices("Index 2".to_string(), create_test_prices()).unwrap();

        benchmark_analyzer.add_benchmark(benchmark1);
        benchmark_analyzer.add_benchmark(benchmark2);

        let portfolio = ReturnSeries::from_prices(create_test_prices()).unwrap();
        let results = benchmark_analyzer
            .comprehensive_analysis(&portfolio)
            .unwrap();

        assert_eq!(results.len(), 2);
    }

    #[test]
    fn test_sqrt_decimal() {
        // Test with approximate comparison due to decimal precision
        let sqrt_4 = sqrt_decimal(dec!(4.0));
        assert!((sqrt_4 - dec!(2.0)).abs() < dec!(0.0001));

        let sqrt_9 = sqrt_decimal(dec!(9.0));
        assert!((sqrt_9 - dec!(3.0)).abs() < dec!(0.0001));

        let sqrt_2 = sqrt_decimal(dec!(2.0));
        assert!(sqrt_2 > dec!(1.4));
        assert!(sqrt_2 < dec!(1.5));
    }
}