aprender-core 0.30.0

Next-generation machine learning library in pure Rust
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
#[cfg(test)]
mod tests {
    #[allow(clippy::wildcard_imports)]
    use super::super::*;

    fn sample_returns() -> Vec<f64> {
        vec![
            0.01, 0.02, -0.01, 0.03, 0.01, -0.02, 0.02, 0.01, 0.005, -0.015,
        ]
    }

    fn sample_benchmark() -> Vec<f64> {
        vec![
            0.008, 0.015, -0.005, 0.025, 0.01, -0.01, 0.018, 0.008, 0.003, -0.012,
        ]
    }

    #[test]
    fn test_sharpe_ratio_positive() {
        let returns = sample_returns();
        let sharpe = sharpe_ratio(&returns, 0.001);

        // Should be positive (positive mean return exceeds risk-free rate)
        assert!(sharpe > 0.0, "Sharpe should be positive: {sharpe}");
        assert!(sharpe.is_finite());
    }

    #[test]
    fn test_sharpe_ratio_zero_variance() {
        let returns = vec![0.01, 0.01, 0.01, 0.01, 0.01];
        let sharpe = sharpe_ratio(&returns, 0.001);

        // With zero variance and positive excess return, should be infinity
        assert!(
            sharpe.is_infinite() && sharpe > 0.0,
            "Sharpe with zero variance: {sharpe}"
        );
    }

    #[test]
    fn test_sharpe_ratio_annualized() {
        let returns = sample_returns();

        // Daily returns, annualize with 252 trading days
        let sharpe_daily = sharpe_ratio_annualized(&returns, 0.05, 252.0);
        let sharpe_monthly = sharpe_ratio_annualized(&returns, 0.05, 12.0);

        assert!(sharpe_daily.is_finite());
        assert!(sharpe_monthly.is_finite());
    }

    #[test]
    fn test_sortino_ratio() {
        let returns = sample_returns();
        let sortino = sortino_ratio(&returns, 0.001, 0.0);

        // Should be positive and typically higher than Sharpe
        assert!(sortino > 0.0, "Sortino should be positive: {sortino}");
        assert!(sortino.is_finite());
    }

    #[test]
    fn test_sortino_no_downside() {
        let returns = vec![0.01, 0.02, 0.03, 0.04, 0.05];
        let sortino = sortino_ratio(&returns, 0.0, 0.0);

        // No negative returns, downside deviation = 0, infinite Sortino
        assert!(
            sortino.is_infinite() && sortino > 0.0,
            "Sortino with no downside: {sortino}"
        );
    }

    #[test]
    fn test_calmar_ratio() {
        let calmar = calmar_ratio(0.15, 0.10);
        assert!((calmar - 1.5).abs() < 0.001);

        let calmar_high = calmar_ratio(0.30, 0.10);
        assert!((calmar_high - 3.0).abs() < 0.001);
    }

    #[test]
    fn test_calmar_ratio_zero_drawdown() {
        let calmar = calmar_ratio(0.10, 0.0);
        assert!(calmar.is_infinite() && calmar > 0.0);
    }

    #[test]
    fn test_treynor_ratio() {
        let returns = sample_returns();
        let benchmark = sample_benchmark();
        let treynor = treynor_ratio(&returns, &benchmark, 0.001);

        assert!(treynor.is_finite());
    }

    #[test]
    fn test_information_ratio() {
        let returns = sample_returns();
        let benchmark = sample_benchmark();
        let ir = information_ratio(&returns, &benchmark);

        assert!(ir.is_finite());
    }

    #[test]
    fn test_omega_ratio() {
        let returns = sample_returns();
        let omega = omega_ratio(&returns, 0.0);

        // With positive mean, omega should be > 1
        assert!(omega > 0.0, "Omega should be positive: {omega}");
    }

    #[test]
    fn test_omega_ratio_threshold() {
        let returns = sample_returns();

        // Higher threshold should give lower omega
        let omega_0 = omega_ratio(&returns, 0.0);
        let omega_high = omega_ratio(&returns, 0.02);

        assert!(omega_high < omega_0, "Higher threshold = lower omega");
    }

    #[test]
    fn test_jensens_alpha() {
        let returns = sample_returns();
        let benchmark = sample_benchmark();
        let alpha = jensens_alpha(&returns, &benchmark, 0.001);

        assert!(alpha.is_finite());
    }

    #[test]
    fn test_gain_to_pain_ratio() {
        let returns = sample_returns();
        let gpr = gain_to_pain_ratio(&returns);

        assert!(gpr > 0.0);
        assert!(gpr.is_finite());
    }

    #[test]
    fn test_gain_to_pain_all_positive() {
        let returns = vec![0.01, 0.02, 0.03, 0.04];
        let gpr = gain_to_pain_ratio(&returns);

        assert!(gpr.is_infinite() && gpr > 0.0);
    }

    #[test]
    fn test_calculate_beta() {
        let returns = sample_returns();
        let benchmark = sample_benchmark();
        let beta = calculate_beta(&returns, &benchmark);

        // Portfolio with similar behavior to benchmark should have beta near 1
        assert!(
            beta > 0.0 && beta < 3.0,
            "Beta should be reasonable: {beta}"
        );
    }

    #[test]
    fn test_empty_inputs() {
        assert!(sharpe_ratio(&[], 0.0).abs() < 1e-10);
        assert!(sortino_ratio(&[], 0.0, 0.0).abs() < 1e-10);
        assert!(information_ratio(&[], &[]).abs() < 1e-10);
        assert!((omega_ratio(&[], 0.0) - 1.0).abs() < 1e-10);
    }

    #[test]
    fn test_single_value() {
        let returns = vec![0.01];
        assert!(sharpe_ratio(&returns, 0.0).abs() < 1e-10);
        assert!(sortino_ratio(&returns, 0.0, 0.0).abs() < 1e-10);
    }

    #[test]
    fn test_sharpe_ratio_zero_variance_negative_excess() {
        // All identical returns below risk-free rate => negative infinity
        let returns = vec![0.01, 0.01, 0.01, 0.01, 0.01];
        let sharpe = sharpe_ratio(&returns, 0.05);
        assert!(
            sharpe.is_infinite() && sharpe < 0.0,
            "Sharpe with zero variance and negative excess should be -Infinity: {sharpe}"
        );
    }

    #[test]
    fn test_sharpe_ratio_zero_variance_zero_excess() {
        // All identical returns equal to risk-free rate => 0
        let returns = vec![0.01, 0.01, 0.01, 0.01, 0.01];
        let sharpe = sharpe_ratio(&returns, 0.01);
        assert!(
            sharpe.abs() < 1e-10,
            "Sharpe with zero variance and zero excess should be 0: {sharpe}"
        );
    }

    #[test]
    fn test_sharpe_annualized_short_returns() {
        // Less than 2 returns should return 0
        let sharpe = sharpe_ratio_annualized(&[0.01], 0.05, 252.0);
        assert!(sharpe.abs() < 1e-10);
    }

    #[test]
    fn test_sharpe_annualized_zero_vol_positive_excess() {
        // All identical returns with positive annualized excess => Infinity
        let returns = vec![0.01, 0.01, 0.01, 0.01, 0.01];
        let sharpe = sharpe_ratio_annualized(&returns, 0.0, 252.0);
        // annualized return = 0.01 * 252 = 2.52, excess = 2.52 - 0.0 = 2.52
        // annualized vol = 0
        assert!(
            sharpe.is_infinite() && sharpe > 0.0,
            "Annualized Sharpe with zero vol and positive excess should be +Infinity: {sharpe}"
        );
    }

    #[test]
    fn test_sharpe_annualized_zero_vol_negative_excess() {
        // All identical returns below annualized risk-free => -Infinity
        let returns = vec![0.001, 0.001, 0.001, 0.001, 0.001];
        let sharpe = sharpe_ratio_annualized(&returns, 10.0, 252.0);
        // annualized return = 0.001 * 252 = 0.252, excess = 0.252 - 10.0 = -9.748
        assert!(
            sharpe.is_infinite() && sharpe < 0.0,
            "Annualized Sharpe with zero vol and negative excess should be -Infinity: {sharpe}"
        );
    }

    #[test]
    fn test_sharpe_annualized_zero_vol_zero_excess() {
        // All identical returns equal to annualized risk-free => 0
        let returns = vec![0.01, 0.01, 0.01, 0.01, 0.01];
        // annualized return = 0.01 * 12 = 0.12
        let sharpe = sharpe_ratio_annualized(&returns, 0.12, 12.0);
        assert!(
            sharpe.abs() < 1e-10,
            "Annualized Sharpe with zero vol and zero excess should be 0: {sharpe}"
        );
    }

    #[test]
    fn test_sortino_zero_downside_negative_excess() {
        // All positive returns but mean below risk-free => negative infinity
        let returns = vec![0.01, 0.02, 0.03, 0.04, 0.05];
        let sortino = sortino_ratio(&returns, 0.10, 0.0);
        // mean = 0.03, excess = 0.03 - 0.10 = -0.07
        // No returns below target, downside_deviation = 0
        assert!(
            sortino.is_infinite() && sortino < 0.0,
            "Sortino with zero downside and negative excess should be -Infinity: {sortino}"
        );
    }

    #[test]
    fn test_sortino_zero_downside_zero_excess() {
        // All positive returns, mean equals risk-free, no downside => 0
        let returns = vec![0.03, 0.03, 0.03, 0.03, 0.03];
        let sortino = sortino_ratio(&returns, 0.03, 0.0);
        assert!(
            sortino.abs() < 1e-10,
            "Sortino with zero downside and zero excess should be 0: {sortino}"
        );
    }

    #[test]
    fn test_calmar_zero_drawdown_negative_return() {
        let calmar = calmar_ratio(-0.10, 0.0);
        assert!(
            calmar.is_infinite() && calmar < 0.0,
            "Calmar with zero drawdown and negative return should be -Infinity: {calmar}"
        );
    }

    #[test]
    fn test_calmar_zero_drawdown_zero_return() {
        let calmar = calmar_ratio(0.0, 0.0);
        assert!(
            calmar.abs() < 1e-10,
            "Calmar with zero drawdown and zero return should be 0: {calmar}"
        );
    }

    #[test]
    fn test_treynor_empty_returns() {
        let treynor = treynor_ratio(&[], &[], 0.01);
        assert!(
            treynor.abs() < 1e-10,
            "Treynor with empty returns should be 0: {treynor}"
        );
    }

    #[test]
    fn test_treynor_mismatched_lengths() {
        // Mismatched lengths: beta defaults to 1.0
        let returns = vec![0.01, 0.02, 0.03];
        let benchmark = vec![0.01, 0.02];
        let treynor = treynor_ratio(&returns, &benchmark, 0.0);
        // beta = 1.0 (from mismatched lengths)
        // excess = mean(returns) - rf = 0.02 - 0.0 = 0.02
        // treynor = 0.02 / 1.0 = 0.02
        assert!(treynor.is_finite());
    }

    #[test]
    fn test_treynor_zero_beta() {
        // When benchmark has zero variance, beta = 1.0 (fallback)
        let returns = vec![0.01, 0.02, 0.03, 0.04];
        let benchmark = vec![0.05, 0.05, 0.05, 0.05]; // zero variance
        let treynor = treynor_ratio(&returns, &benchmark, 0.0);
        // var_benchmark = 0, so beta = 1.0
        // excess = 0.025 / 1.0 = 0.025
        assert!(treynor.is_finite());
        assert!(treynor > 0.0);
    }

    #[test]
    fn test_information_ratio_mismatched_lengths() {
        let returns = vec![0.01, 0.02, 0.03];
        let benchmark = vec![0.01, 0.02];
        let ir = information_ratio(&returns, &benchmark);
        assert!(
            ir.abs() < 1e-10,
            "IR with mismatched lengths should be 0: {ir}"
        );
    }

    #[test]
    fn test_information_ratio_single_value() {
        let ir = information_ratio(&[0.01], &[0.01]);
        assert!(ir.abs() < 1e-10, "IR with single value should be 0: {ir}");
    }

    #[test]
    fn test_omega_ratio_all_above_threshold() {
        // All returns above threshold, losses = 0, gains > 0 => Infinity
        let returns = vec![0.05, 0.06, 0.07, 0.08];
        let omega = omega_ratio(&returns, 0.0);
        assert!(
            omega.is_infinite() && omega > 0.0,
            "Omega with no losses and gains should be Infinity: {omega}"
        );
    }

    #[test]
    fn test_omega_ratio_all_equal_threshold() {
        // All returns exactly at threshold: gains=0, losses=0 => 1.0
        let returns = vec![0.0, 0.0, 0.0, 0.0];
        let omega = omega_ratio(&returns, 0.0);
        assert!(
            (omega - 1.0).abs() < 1e-10,
            "Omega with all returns at threshold should be 1.0: {omega}"
        );
    }

    #[test]
    fn test_gain_to_pain_all_negative() {
        let returns = vec![-0.01, -0.02, -0.03, -0.04];
        let gpr = gain_to_pain_ratio(&returns);
        assert!(
            gpr.abs() < 1e-10,
            "Gain-to-pain with all negative and no gains should be 0: {gpr}"
        );
    }

    #[test]
    fn test_gain_to_pain_all_zero() {
        let returns = vec![0.0, 0.0, 0.0];
        let gpr = gain_to_pain_ratio(&returns);
        assert!(
            gpr.abs() < 1e-10,
            "Gain-to-pain with all zeros should be 0: {gpr}"
        );
    }

    #[test]
    fn test_gain_to_pain_empty() {
        let gpr = gain_to_pain_ratio(&[]);
        assert!(
            gpr.abs() < 1e-10,
            "Gain-to-pain with empty should be 0: {gpr}"
        );
    }

    #[test]
    fn test_jensens_alpha_empty_returns() {
        let alpha = jensens_alpha(&[], &[0.01, 0.02], 0.01);
        assert!(
            alpha.abs() < 1e-10,
            "Alpha with empty returns should be 0: {alpha}"
        );
    }

    #[test]
    fn test_jensens_alpha_empty_benchmark() {
        let alpha = jensens_alpha(&[0.01, 0.02], &[], 0.01);
        assert!(
            alpha.abs() < 1e-10,
            "Alpha with empty benchmark should be 0: {alpha}"
        );
    }

    #[test]
    fn test_calculate_beta_mismatched() {
        // Mismatched lengths returns default beta of 1.0
        let beta = calculate_beta(&[0.01, 0.02, 0.03], &[0.01, 0.02]);
        assert!(
            (beta - 1.0).abs() < 1e-10,
            "Beta with mismatched lengths should be 1.0: {beta}"
        );
    }

    #[test]
    fn test_calculate_beta_single_value() {
        let beta = calculate_beta(&[0.01], &[0.02]);
        assert!(
            (beta - 1.0).abs() < 1e-10,
            "Beta with single value should be 1.0: {beta}"
        );
    }

    #[test]
    fn test_calculate_beta_zero_benchmark_variance() {
        let beta = calculate_beta(&[0.01, 0.02, 0.03], &[0.05, 0.05, 0.05]);
        assert!(
            (beta - 1.0).abs() < 1e-10,
            "Beta with zero benchmark variance should be 1.0: {beta}"
        );
    }

    // Property-based tests
    #[cfg(test)]
    mod proptests {
        use super::*;
        use proptest::prelude::*;

        proptest! {
            #[test]
            fn prop_sharpe_finite(returns in prop::collection::vec(-0.5..0.5f64, 10..100)) {
                let sharpe = sharpe_ratio(&returns, 0.01);
                prop_assert!(sharpe.is_finite() || sharpe.is_infinite(), "Sharpe must be defined");
            }

            #[test]
            fn prop_sortino_geq_zero_or_negative(
                returns in prop::collection::vec(-0.5..0.5f64, 10..100)
            ) {
                let sortino = sortino_ratio(&returns, 0.0, 0.0);
                // Sortino can be negative if mean is negative
                prop_assert!(sortino.is_finite() || sortino.is_infinite());
            }

            #[test]
            fn prop_calmar_sign_matches_return(
                ret in -0.5..0.5f64,
                dd in 0.01..0.5f64,
            ) {
                let calmar = calmar_ratio(ret, dd);
                if ret > 0.0 {
                    prop_assert!(calmar > 0.0);
                } else if ret < 0.0 {
                    prop_assert!(calmar < 0.0);
                }
            }

            #[test]
            fn prop_omega_positive(
                returns in prop::collection::vec(-0.1..0.1f64, 10..100),
                threshold in -0.1..0.1f64,
            ) {
                let omega = omega_ratio(&returns, threshold);
                prop_assert!(omega >= 0.0 || omega.is_infinite(), "Omega must be non-negative");
            }

            #[test]
            fn prop_information_ratio_finite(
                returns in prop::collection::vec(-0.1..0.1f64, 20..100),
                benchmark in prop::collection::vec(-0.1..0.1f64, 20..100),
            ) {
                let len = returns.len().min(benchmark.len());
                let ir = information_ratio(&returns[..len], &benchmark[..len]);
                prop_assert!(ir.is_finite() || ir.abs() < 1e-10, "IR should be finite: {ir}");
            }
        }
    }
}