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
use std::{borrow::Borrow, iter::zip};
use crate::error::DigiFiError;
use crate::utilities::compare_len;
/// Trait for defining a loss function.
pub trait LossFunction {
/// Measures an error between observed and predicted values.
///
/// # Input
/// - `observation`: Observed/empirical value
/// - `prediction`: Value predicted by the model
///
/// # Output
/// - An error/loss
fn loss(&self, observation: f64, prediction: f64) -> f64;
/// Measures an error between observed and predicted values.
///
/// # Input
/// - `observations`: An iterator of observed/empirical values
/// - `predictions`: An iterator of values predicted by the model
///
/// # Output
/// - Error/loss
fn loss_iter<T, I>(&self, observations: T, predictions: T) -> Result<f64, DigiFiError>
where
T: Iterator<Item = I> + ExactSizeIterator,
I: Borrow<f64>;
}
/// Measures an error between paired observations (Usually, empirical observations vs simulated observations).
///
/// # LaTeX Formula
/// - MSE = \\frac{1}{n}\\sum^{n}\_{i=1}(y_{i}-x_{i})^{2}
///
/// # Links
/// - Wikipedia: <https://en.wikipedia.org/wiki/Mean_squared_error>
/// - Original Source: N/A
pub struct MSE;
impl LossFunction for MSE {
fn loss(&self, observation: f64, prediction: f64) -> f64 {
(observation - prediction).powi(2)
}
/// Measures an error between observed and predicted values.
///
/// # Input
/// - `observations`: An iterator of observed/empirical values
/// - `predictions`: An iterator of values predicted by the model
///
/// # Output
/// - Error/loss
///
/// # Errors
/// - Returns an error if the lengths of `observations` and `predictions` do not coincide.
fn loss_iter<T, I>(&self, observations: T, predictions: T) -> Result<f64, DigiFiError>
where
T: Iterator<Item = I> + ExactSizeIterator,
I: Borrow<f64>,
{
compare_len(&observations, &predictions, "observations", "predictions")?;
let len: f64 = observations.len() as f64;
Ok(zip(observations, predictions).fold(0.0, |sum, (o, p)| { sum + (o.borrow() - p.borrow()).powi(2) } ) / len)
}
}
/// Measures an error between paired observations (Usually, empirical observations vs simulated observations).
///
/// # LaTeX Formula
/// - MAE = \\frac{\\sum^{n}\_{i=1}\\lvert y_{i}-x_{i}\\rvert}{n}
///
/// # Links
/// - Wikipedia: <https://en.wikipedia.org/wiki/Mean_absolute_error>
/// - Original Source: N/A
pub struct MAE;
impl LossFunction for MAE {
fn loss(&self, observation: f64, prediction: f64) -> f64 {
(observation - prediction).abs()
}
/// Measures an error between observed and predicted values.
///
/// # Input
/// - `observations`: An iterator of observed/empirical values
/// - `predictions`: An iterator of values predicted by the model
///
/// # Output
/// - Error/loss
///
/// # Errors
/// - Returns an error if the lengths of `observations` and `predictions` do not coincide.
fn loss_iter<T, I>(&self, observations: T, predictions: T) -> Result<f64, DigiFiError>
where
T: Iterator<Item = I> + ExactSizeIterator,
I: Borrow<f64>,
{
compare_len(&observations, &predictions, "observations", "predictions")?;
let len: f64 = observations.len() as f64;
Ok(zip(observations, predictions).fold(0.0, |sum, (o, p)| { sum + (o.borrow() - p.borrow()).abs() } ) / len)
}
}
/// Measures an error between paired observations (Usually, empirical observations vs simulated observations).
///
/// # LaTeX Formula
/// - MSLE = \\frac{1}{n}\\sum^{n}\_{i=1}(ln(\\frac{y_{i}+1}{x_{i} + 1}))^{2}
///
/// # Links
/// - Wikipedia: N/A
/// - Original Source: N/A
pub struct MSLE;
impl LossFunction for MSLE {
fn loss(&self, observation: f64, prediction: f64) -> f64 {
((observation + 1.0) - (prediction + 1.0)).ln().powi(2)
}
/// Measures an error between observed and predicted values.
///
/// # Input
/// - `observations`: An iterator of observed/empirical values
/// - `predictions`: An iterator of values predicted by the model
///
/// # Output
/// - Error/loss
///
/// # Errors
/// - Returns an error if the lengths of `observations` and `predictions` do not coincide.
fn loss_iter<T, I>(&self, observations: T, predictions: T) -> Result<f64, DigiFiError>
where
T: Iterator<Item = I> + ExactSizeIterator,
I: Borrow<f64>,
{
compare_len(&observations, &predictions, "observations", "predictions")?;
let len: f64 = observations.len() as f64;
Ok(zip(observations, predictions).fold(0.0, |sum, (o, p)| { sum + ((o.borrow() + 1.0) / (p.borrow() + 1.0)).ln().powi(2) } ) / len)
}
}
/// Measures an error between paired observations (Usually, empirical observations vs simulated observations).
///
/// # LaTeX Formula
/// - MLE = \\sqrt{\\frac{1}{n}\\sum^{n}\_{i=1}(ln(\\frac{y_{i}+1}{x_{i} + 1}))^{2}}
///
/// # Links
/// - Wikipedia: N/A
/// - Original Source: N/A
pub struct MLE;
impl LossFunction for MLE {
fn loss(&self, observation: f64, prediction: f64) -> f64 {
((observation + 1.0) - (prediction + 1.0)).ln().abs()
}
/// Measures an error between observed and predicted values.
///
/// # Input
/// - `observations`: An iterator of observed/empirical values
/// - `predictions`: An iterator of values predicted by the model
///
/// # Output
/// - Error/loss
///
/// # Errors
/// - Returns an error if the lengths of `observations` and `predictions` do not coincide.
fn loss_iter<T, I>(&self, observations: T, predictions: T) -> Result<f64, DigiFiError>
where
T: Iterator<Item = I> + ExactSizeIterator,
I: Borrow<f64>,
{
compare_len(&observations, &predictions, "observations", "predictions")?;
let msle: MSLE = MSLE;
Ok(msle.loss_iter(observations, predictions)?.powf(0.5))
}
}
/// Measures an error between paired observations (Usually, empirical observations vs simulated observations).
///
/// # LaTeX Formula
/// - SSE = \\sum^{n}\_{i=1}(y_{i}-x_{i})^{2}
///
/// # Links
/// - Wikipedia: <https://en.wikipedia.org/wiki/Residual_sum_of_squares>
/// - Original Source: N/A
pub struct SSE;
impl LossFunction for SSE {
fn loss(&self, observation: f64, prediction: f64) -> f64 {
(observation - prediction).powi(2)
}
/// Measures an error between observed and predicted values.
///
/// # Input
/// - `observations`: An iterator of observed/empirical values
/// - `predictions`: An iterator of values predicted by the model
///
/// # Output
/// - Error/loss
///
/// # Errors
/// - Returns an error if the lengths of `observations` and `predictions` do not coincide.
fn loss_iter<T, I>(&self, observations: T, predictions: T) -> Result<f64, DigiFiError>
where
T: Iterator<Item = I> + ExactSizeIterator,
I: Borrow<f64>,
{
compare_len(&observations, &predictions, "observations", "predictions")?;
Ok(zip(observations, predictions).fold(0.0, |sum, (o, p)| { sum + (o.borrow() - p.borrow()).powi(2) } ))
}
}
/// Measures an error between paired observations (Usually, empirical observations vs simulated observations).
///
/// # LaTeX Formula
/// - Loss = \\frac{1}{n}\\sum^{n}\_{i=1}(\\lvert\\frac{x_{i}}{y_{i}}-1\\rvert + \\lvert 1-\\frac{x_{i}}{y_{i}}\\rvert)
///
/// # Links
/// - Wikipedia: N/A
/// - Original Source: N/A
pub struct StraddleLoss;
impl LossFunction for StraddleLoss {
fn loss(&self, observation: f64, prediction: f64) -> f64 {
2.0 * (prediction / observation - 1.0).abs()
}
/// Measures an error between observed and predicted values.
///
/// # Input
/// - `observations`: An iterator of observed/empirical values
/// - `predictions`: An iterator of values predicted by the model
///
/// # Output
/// - Error/loss
///
/// # Errors
/// - Returns an error if the lengths of `observations` and `predictions` do not coincide.
fn loss_iter<T, I>(&self, observations: T, predictions: T) -> Result<f64, DigiFiError>
where
T: Iterator<Item = I> + ExactSizeIterator,
I: Borrow<f64>,
{
compare_len(&observations, &predictions, "observations", "predictions")?;
let len: f64 = observations.len() as f64;
Ok(zip(observations, predictions).fold(0.0, |sum, (o, p)| { sum + 2.0 * (p.borrow() / o.borrow() - 1.0).abs() } ) / len)
}
}