frenchrs 0.1.3

A high-performance Rust library for asset pricing and financial analysis, built on the robust econometric infrastructure of [Greeners](https://crates.io/crates/greeners).
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
use greeners::{CovarianceType, GreenersError, OLS};
use ndarray::{Array1, Array2};
use std::collections::HashMap;

/// Result of IVOL & Tracking Error for multiple assets
///
/// Equivalente ao DataFrame of the Python with índice por asset
#[derive(Debug, Clone)]
pub struct IVOLTrackingMulti {
    /// Results por asset
    pub results: HashMap<String, IVOLTrackingAsset>,
}

/// Result of IVOL & Tracking Error for um asset
#[derive(Debug, Clone)]
pub struct IVOLTrackingAsset {
    /// Nome of the asset
    pub asset: String,

    /// Mean of return in excess (annualized)
    pub mean_excess_annual: f64,

    /// IVOL monthly (standard deviation of the residuals)
    pub ivol_monthly: f64,

    /// IVOL annualized (monthly × √12)
    pub ivol_annual: f64,

    /// Tracking error monthly (vs benchmark, if fornecido)
    pub tracking_error_monthly: Option<f64>,

    /// Tracking error annualized (monthly × √12)
    pub tracking_error_annual: Option<f64>,

    /// Number of observations
    pub nobs: usize,

    /// R² of the model
    pub r_squared: f64,

    /// Alpha of the model
    pub alpha: f64,
}

impl IVOLTrackingMulti {
    /// Calculates IVOL and Tracking Error for multiple assets
    ///
    /// # Arguments
    /// * `returns_excess` - Matriz of returns in excess (n_obs × n_assets)
    /// * `factors` - Matriz of factors (n_obs × n_factors)
    /// * `benchmark` - Benchmark for tracking error (opcional, n_obs)
    /// * `cov_type` - Covariance type
    /// * `asset_names` - Nomes of the assets (opcional)
    /// * `periods_per_year` - Periods por ano (12 for monthly, 252 for daily)
    ///
    /// # Example
    /// ```
    /// use frenchrs::IVOLTrackingMulti;
    /// use greeners::CovarianceType;
    /// use ndarray::Array2;
    ///
    /// // 12 meses × 2 assets (returns in excess)
    /// let returns_excess = Array2::from_shape_vec((12, 2), vec![
    ///     0.01, 0.015,
    ///     0.02, 0.025,
    ///     -0.01, -0.005,
    ///     0.03, 0.035,
    ///     0.015, 0.020,
    ///     -0.005, 0.000,
    ///     0.025, 0.030,
    ///     0.01, 0.015,
    ///     0.02, 0.025,
    ///     -0.01, -0.005,
    ///     0.03, 0.035,
    ///     0.015, 0.020,
    /// ]).unwrap();
    ///
    /// // 12 meses × 1 factor
    /// let factors = Array2::from_shape_vec((12, 1), vec![
    ///     0.008, 0.015, -0.005, 0.025, 0.012, -0.003,
    ///     0.020, 0.009, 0.015, -0.005, 0.025, 0.012,
    /// ]).unwrap();
    ///
    /// let ivol_tracking = IVOLTrackingMulti::fit(
    ///     &returns_excess,
    ///     &factors,
    ///     None, // without benchmark
    ///     CovarianceType::NonRobust,
    ///     Some(vec!["Asset1".to_string(), "Asset2".to_string()]),
    ///     12.0, // monthly
    /// ).unwrap();
    ///
    /// assert_eq!(ivol_tracking.results.len(), 2);
    /// ```
    pub fn fit(
        returns_excess: &Array2<f64>,
        factors: &Array2<f64>,
        benchmark: Option<&Array1<f64>>,
        cov_type: CovarianceType,
        asset_names: Option<Vec<String>>,
        periods_per_year: f64,
    ) -> Result<Self, GreenersError> {
        let n_obs = returns_excess.nrows();
        let n_assets = returns_excess.ncols();
        let n_factors = factors.ncols();

        if factors.nrows() != n_obs {
            return Err(GreenersError::ShapeMismatch(format!(
                "Returns ({} obs) and factors ({} obs) must have same number of observations",
                n_obs,
                factors.nrows()
            )));
        }

        if let Some(bench) = benchmark {
            if bench.len() != n_obs {
                return Err(GreenersError::ShapeMismatch(format!(
                    "Benchmark ({} obs) must have same length as returns ({} obs)",
                    bench.len(),
                    n_obs
                )));
            }
        }

        if n_obs <= n_factors + 5 {
            return Err(GreenersError::InvalidOperation(format!(
                "Insufficient data: {} obs for {} factors (need at least {})",
                n_obs,
                n_factors,
                n_factors + 6
            )));
        }

        // Nomes padrão if not fornecidos
        let asset_names = asset_names
            .unwrap_or_else(|| (0..n_assets).map(|i| format!("Asset{}", i + 1)).collect());

        let mut results = HashMap::new();

        // Procthatr each asset
        for (asset_idx, asset_name) in asset_names.iter().enumerate() {
            let asset_returns = returns_excess.column(asset_idx);

            let asset_result = Self::fit_single_asset(
                &asset_returns.to_owned(),
                factors,
                benchmark,
                cov_type.clone(),
                asset_name.clone(),
                periods_per_year,
            )?;

            results.insert(asset_name.clone(), asset_result);
        }

        Ok(IVOLTrackingMulti { results })
    }

    /// Calculates IVOL and Tracking Error for um single asset
    fn fit_single_asset(
        asset_returns_excess: &Array1<f64>,
        factors: &Array2<f64>,
        benchmark: Option<&Array1<f64>>,
        cov_type: CovarianceType,
        asset_name: String,
        periods_per_year: f64,
    ) -> Result<IVOLTrackingAsset, GreenersError> {
        let n_obs = asset_returns_excess.len();
        let n_factors = factors.ncols();

        // Matriz of design: [1, factors]
        let mut x = Array2::<f64>::zeros((n_obs, n_factors + 1));
        x.column_mut(0).fill(1.0);
        for j in 0..n_factors {
            x.column_mut(j + 1).assign(&factors.column(j));
        }

        // Estimates OLS
        let ols = OLS::fit(asset_returns_excess, &x, cov_type)?;

        // Residuals
        let residuals = ols.residuals(asset_returns_excess, &x);

        // IVOL monthly
        let ivol_monthly = residuals.std(1.0);

        // IVOL annualized
        let ivol_annual = ivol_monthly * periods_per_year.sqrt();

        // Mean of return in excess annualized
        let mean_excess_monthly = asset_returns_excess.mean().unwrap_or(0.0);
        let mean_excess_annual = mean_excess_monthly * periods_per_year;

        // Tracking error (if benchmark fornecido)
        let (tracking_error_monthly, tracking_error_annual) = if let Some(bench) = benchmark {
            let diff = asset_returns_excess - bench;
            let te_monthly = diff.std(1.0);
            let te_annual = te_monthly * periods_per_year.sqrt();
            (Some(te_monthly), Some(te_annual))
        } else {
            (None, None)
        };

        Ok(IVOLTrackingAsset {
            asset: asset_name,
            mean_excess_annual,
            ivol_monthly,
            ivol_annual,
            tracking_error_monthly,
            tracking_error_annual,
            nobs: n_obs,
            r_squared: ols.r_squared,
            alpha: ols.params[0],
        })
    }

    /// Gets results for um asset específico
    pub fn get_asset(&self, asset_name: &str) -> Option<&IVOLTrackingAsset> {
        self.results.get(asset_name)
    }

    /// Lists all assets
    pub fn asset_names(&self) -> Vec<String> {
        self.results.keys().cloned().collect()
    }

    /// Converts to formato tabular (similar ao DataFrame of the Python)
    pub fn to_table(&self) -> Vec<IVOLTrackingRow> {
        self.results
            .values()
            .map(|asset| IVOLTrackingRow {
                asset: asset.asset.clone(),
                mean_excess_annual: asset.mean_excess_annual,
                ivol_monthly: asset.ivol_monthly,
                ivol_annual: asset.ivol_annual,
                tracking_error_monthly: asset.tracking_error_monthly,
                tracking_error_annual: asset.tracking_error_annual,
                nobs: asset.nobs,
                r_squared: asset.r_squared,
                alpha: asset.alpha,
            })
            .collect()
    }

    /// Exports to CSV-like string
    pub fn to_csv_string(&self) -> String {
        let mut result = String::new();

        // Header
        result.push_str(
            "asset,mean_excess_annual,ivol_monthly,ivol_annual,tracking_error_monthly,tracking_error_annual,r_squared,alpha,nobs\n",
        );

        // Data
        let table = self.to_table();
        for row in table {
            let te_monthly = row
                .tracking_error_monthly
                .map_or("".to_string(), |v| format!("{:.6}", v));
            let te_annual = row
                .tracking_error_annual
                .map_or("".to_string(), |v| format!("{:.6}", v));

            result.push_str(&format!(
                "{},{:.6},{:.6},{:.6},{},{},{:.6},{:.6},{}\n",
                row.asset,
                row.mean_excess_annual,
                row.ivol_monthly,
                row.ivol_annual,
                te_monthly,
                te_annual,
                row.r_squared,
                row.alpha,
                row.nobs
            ));
        }

        result
    }
}

/// Linha of result IVOL/Tracking (formato tabular)
#[derive(Debug, Clone)]
pub struct IVOLTrackingRow {
    /// Nome of the asset
    pub asset: String,

    /// Mean of return in excess annualized
    pub mean_excess_annual: f64,

    /// IVOL monthly
    pub ivol_monthly: f64,

    /// IVOL annualized
    pub ivol_annual: f64,

    /// Tracking error monthly (opcional)
    pub tracking_error_monthly: Option<f64>,

    /// Tracking error annualized (opcional)
    pub tracking_error_annual: Option<f64>,

    /// Number of observations
    pub nobs: usize,

    /// R² of the model
    pub r_squared: f64,

    /// Alpha of the model
    pub alpha: f64,
}

impl IVOLTrackingAsset {
    /// Classifies the level of IVOL
    pub fn ivol_classification(&self) -> &str {
        if self.ivol_annual < 0.10 {
            "Baixo"
        } else if self.ivol_annual < 0.20 {
            "Moderado"
        } else if self.ivol_annual < 0.30 {
            "Alto"
        } else {
            "Muito Alto"
        }
    }

    /// Classifies the level of tracking error (if disponível)
    pub fn tracking_error_classification(&self) -> Option<&str> {
        self.tracking_error_annual.map(|te| {
            if te < 0.02 {
                "Muito Baixo (< 2%)"
            } else if te < 0.05 {
                "Baixo (2-5%)"
            } else if te < 0.10 {
                "Moderado (5-10%)"
            } else if te < 0.15 {
                "Alto (10-15%)"
            } else {
                "Muito Alto (> 15%)"
            }
        })
    }

    /// Information ratio (if tracking error disponível)
    pub fn information_ratio(&self) -> Option<f64> {
        self.tracking_error_annual
            .map(|te| if te > 1e-10 { self.alpha / te } else { 0.0 })
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use ndarray::{array, Array2};

    #[test]
    fn test_ivol_tracking_multi_basic() {
        let returns_excess = Array2::from_shape_vec(
            (12, 2),
            vec![
                0.01, 0.015, 0.02, 0.025, -0.01, -0.005, 0.03, 0.035, 0.015, 0.020, -0.005, 0.000,
                0.025, 0.030, 0.01, 0.015, 0.02, 0.025, -0.01, -0.005, 0.03, 0.035, 0.015, 0.020,
            ],
        )
        .unwrap();

        let factors = Array2::from_shape_vec(
            (12, 1),
            vec![
                0.008, 0.015, -0.005, 0.025, 0.012, -0.003, 0.020, 0.009, 0.015, -0.005, 0.025,
                0.012,
            ],
        )
        .unwrap();

        let ivol_tracking = IVOLTrackingMulti::fit(
            &returns_excess,
            &factors,
            None,
            CovarianceType::NonRobust,
            None,
            12.0,
        )
        .unwrap();

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

    #[test]
    fn test_ivol_tracking_with_benchmark() {
        let returns_excess = Array2::from_shape_vec(
            (12, 2),
            vec![
                0.01, 0.015, 0.02, 0.025, -0.01, -0.005, 0.03, 0.035, 0.015, 0.020, -0.005, 0.000,
                0.025, 0.030, 0.01, 0.015, 0.02, 0.025, -0.01, -0.005, 0.03, 0.035, 0.015, 0.020,
            ],
        )
        .unwrap();

        let factors = Array2::from_shape_vec(
            (12, 1),
            vec![
                0.008, 0.015, -0.005, 0.025, 0.012, -0.003, 0.020, 0.009, 0.015, -0.005, 0.025,
                0.012,
            ],
        )
        .unwrap();

        let benchmark = array![
            0.009, 0.018, -0.008, 0.028, 0.014, -0.004, 0.022, 0.010, 0.018, -0.008, 0.028, 0.014
        ];

        let ivol_tracking = IVOLTrackingMulti::fit(
            &returns_excess,
            &factors,
            Some(&benchmark),
            CovarianceType::NonRobust,
            Some(vec!["Asset1".to_string(), "Asset2".to_string()]),
            12.0,
        )
        .unwrap();

        let asset1 = ivol_tracking.get_asset("Asset1").unwrap();
        assert!(asset1.tracking_error_monthly.is_some());
        assert!(asset1.tracking_error_annual.is_some());
    }

    #[test]
    fn test_ivol_tracking_to_table() {
        let returns_excess = Array2::from_shape_vec(
            (12, 2),
            vec![
                0.01, 0.015, 0.02, 0.025, -0.01, -0.005, 0.03, 0.035, 0.015, 0.020, -0.005, 0.000,
                0.025, 0.030, 0.01, 0.015, 0.02, 0.025, -0.01, -0.005, 0.03, 0.035, 0.015, 0.020,
            ],
        )
        .unwrap();

        let factors = Array2::from_shape_vec(
            (12, 1),
            vec![
                0.008, 0.015, -0.005, 0.025, 0.012, -0.003, 0.020, 0.009, 0.015, -0.005, 0.025,
                0.012,
            ],
        )
        .unwrap();

        let ivol_tracking = IVOLTrackingMulti::fit(
            &returns_excess,
            &factors,
            None,
            CovarianceType::NonRobust,
            None,
            12.0,
        )
        .unwrap();

        let table = ivol_tracking.to_table();
        assert_eq!(table.len(), 2);
    }

    #[test]
    fn test_ivol_tracking_csv_export() {
        let returns_excess = Array2::from_shape_vec(
            (12, 2),
            vec![
                0.01, 0.015, 0.02, 0.025, -0.01, -0.005, 0.03, 0.035, 0.015, 0.020, -0.005, 0.000,
                0.025, 0.030, 0.01, 0.015, 0.02, 0.025, -0.01, -0.005, 0.03, 0.035, 0.015, 0.020,
            ],
        )
        .unwrap();

        let factors = Array2::from_shape_vec(
            (12, 1),
            vec![
                0.008, 0.015, -0.005, 0.025, 0.012, -0.003, 0.020, 0.009, 0.015, -0.005, 0.025,
                0.012,
            ],
        )
        .unwrap();

        let ivol_tracking = IVOLTrackingMulti::fit(
            &returns_excess,
            &factors,
            None,
            CovarianceType::NonRobust,
            None,
            12.0,
        )
        .unwrap();

        let csv = ivol_tracking.to_csv_string();
        assert!(csv.contains("asset,mean_excess_annual"));
        assert!(csv.contains("Asset1"));
        assert!(csv.contains("Asset2"));
    }
}