Skip to main content

ferric_alpha/performance/
mod.rs

1mod alpha_beta;
2mod common_start;
3mod events;
4pub(crate) mod frame;
5mod ic;
6mod portfolio;
7mod pyfolio;
8mod quantiles;
9mod returns;
10mod turnover;
11mod weights;
12
13pub use alpha_beta::factor_alpha_beta;
14pub use common_start::{
15    CommonStartReturnsOptions, average_cumulative_return_by_quantile_from_prices,
16    common_start_returns,
17};
18pub(crate) use events::{EventCoverageRow, event_window_result};
19pub use events::{EventReturnsOptions, average_cumulative_return_by_quantile};
20pub use ic::{factor_information_coefficient, mean_information_coefficient};
21pub use portfolio::{cumulative_returns, factor_cumulative_returns, factor_positions, positions};
22pub use pyfolio::{PyfolioInput, create_pyfolio_input};
23pub use quantiles::{compute_mean_returns_spread, mean_return_by_quantile};
24pub use returns::factor_returns;
25pub use turnover::{factor_rank_autocorrelation, quantile_turnover};
26pub use weights::factor_weights;
27
28#[derive(Debug, Clone, Copy, PartialEq, Eq)]
29pub enum TimeBucket {
30    Daily,
31    Weekly,
32    Monthly,
33}
34
35#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
36pub struct IcOptions {
37    pub(crate) group_adjust: bool,
38    pub(crate) by_group: bool,
39}
40
41impl IcOptions {
42    pub fn with_group_adjust(mut self, group_adjust: bool) -> Self {
43        self.group_adjust = group_adjust;
44        self
45    }
46
47    pub fn with_by_group(mut self, by_group: bool) -> Self {
48        self.by_group = by_group;
49        self
50    }
51}
52
53#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
54pub struct MeanIcOptions {
55    pub(crate) group_adjust: bool,
56    pub(crate) by_group: bool,
57    pub(crate) by_time: Option<TimeBucket>,
58}
59
60impl MeanIcOptions {
61    pub fn with_group_adjust(mut self, group_adjust: bool) -> Self {
62        self.group_adjust = group_adjust;
63        self
64    }
65
66    pub fn with_by_group(mut self, by_group: bool) -> Self {
67        self.by_group = by_group;
68        self
69    }
70
71    pub fn with_by_time(mut self, by_time: Option<TimeBucket>) -> Self {
72        self.by_time = by_time;
73        self
74    }
75}
76
77#[derive(Debug, Clone, Copy, PartialEq, Eq)]
78pub struct WeightOptions {
79    pub(crate) demeaned: bool,
80    pub(crate) group_adjust: bool,
81    pub(crate) equal_weight: bool,
82}
83
84impl Default for WeightOptions {
85    fn default() -> Self {
86        Self {
87            demeaned: true,
88            group_adjust: false,
89            equal_weight: false,
90        }
91    }
92}
93
94impl WeightOptions {
95    pub fn with_demeaned(mut self, demeaned: bool) -> Self {
96        self.demeaned = demeaned;
97        self
98    }
99
100    pub fn with_group_adjust(mut self, group_adjust: bool) -> Self {
101        self.group_adjust = group_adjust;
102        self
103    }
104
105    pub fn with_equal_weight(mut self, equal_weight: bool) -> Self {
106        self.equal_weight = equal_weight;
107        self
108    }
109}
110
111#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
112pub struct ReturnOptions {
113    pub(crate) weight_options: WeightOptions,
114    pub(crate) by_asset: bool,
115}
116
117impl ReturnOptions {
118    pub fn with_weight_options(mut self, weight_options: WeightOptions) -> Self {
119        self.weight_options = weight_options;
120        self
121    }
122
123    pub fn with_by_asset(mut self, by_asset: bool) -> Self {
124        self.by_asset = by_asset;
125        self
126    }
127}
128
129#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
130pub struct AlphaBetaOptions {
131    pub(crate) weight_options: WeightOptions,
132}
133
134impl AlphaBetaOptions {
135    pub fn with_weight_options(mut self, weight_options: WeightOptions) -> Self {
136        self.weight_options = weight_options;
137        self
138    }
139}
140
141#[derive(Debug, Clone, Copy, PartialEq, Eq)]
142pub struct MeanReturnByQuantileOptions {
143    pub(crate) by_date: bool,
144    pub(crate) by_group: bool,
145    pub(crate) demeaned: bool,
146    pub(crate) group_adjust: bool,
147}
148
149impl Default for MeanReturnByQuantileOptions {
150    fn default() -> Self {
151        Self {
152            by_date: false,
153            by_group: false,
154            demeaned: true,
155            group_adjust: false,
156        }
157    }
158}
159
160impl MeanReturnByQuantileOptions {
161    pub fn with_by_date(mut self, by_date: bool) -> Self {
162        self.by_date = by_date;
163        self
164    }
165
166    pub fn with_by_group(mut self, by_group: bool) -> Self {
167        self.by_group = by_group;
168        self
169    }
170
171    pub fn with_demeaned(mut self, demeaned: bool) -> Self {
172        self.demeaned = demeaned;
173        self
174    }
175
176    pub fn with_group_adjust(mut self, group_adjust: bool) -> Self {
177        self.group_adjust = group_adjust;
178        self
179    }
180}
181
182#[derive(Debug, Clone, PartialEq, Default)]
183pub struct PortfolioOptions {
184    pub(crate) weight_options: WeightOptions,
185    pub(crate) quantiles: Option<Vec<u32>>,
186    pub(crate) groups: Option<Vec<String>>,
187}
188
189impl PortfolioOptions {
190    pub fn with_weight_options(mut self, weight_options: WeightOptions) -> Self {
191        self.weight_options = weight_options;
192        self
193    }
194
195    pub fn with_quantiles(mut self, quantiles: Option<Vec<u32>>) -> Self {
196        self.quantiles = quantiles;
197        self
198    }
199
200    pub fn with_groups(mut self, groups: Option<Vec<String>>) -> Self {
201        self.groups = groups;
202        self
203    }
204}