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
use crate::core::Method;
use crate::core::{Error, PeriodType, ValueType, Window};
use crate::helpers::Peekable;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
/// Integrates (summarizes) [`ValueType`] values for the given window size `length`
///
/// # Parameters
///
/// Has a single parameter `length`: [`PeriodType`]
///
/// If `length == 0`, then integrates since the beginning of timeseries
///
/// # Input type
///
/// Input type is [`ValueType`]
///
/// # Output type
///
/// Output type is [`ValueType`]
///
/// # Examples
///
/// ```
/// use yata::prelude::*;
/// use yata::methods::Integral;
///
/// // Integrates over last 3 values
/// let mut integral = Integral::new(3, &1.0).unwrap();
///
/// integral.next(&1.0);
/// integral.next(&2.0);
/// assert_eq!(integral.next(&3.0), 6.0); // 1 + 2 + 3
/// assert_eq!(integral.next(&4.0), 9.0); // 2 + 3 + 4
/// assert_eq!(integral.next(&5.0), 12.0); // 3 + 4 + 5
/// ```
///
/// ```
/// use yata::prelude::*;
/// use yata::methods::Integral;
///
/// // Integrates since the beginning
/// let mut integral = Integral::new(0, &1.0).unwrap(); // same as Integral::default()
///
/// integral.next(&1.0);
/// integral.next(&2.0);
/// assert_eq!(integral.next(&3.0), 6.0); // 1 + 2 + 3
/// assert_eq!(integral.next(&4.0), 10.0); // 1 + 2 + 3 + 4
/// assert_eq!(integral.next(&5.0), 15.0); // 1 + 2 + 3 + 4 + 5
/// ```
///
/// ### Integral is opposite method for Derivative
/// ```
/// use yata::prelude::*;
/// use yata::methods::{Integral, Derivative};
///
/// let s = [1.0, 2.0, 3.0, 3.0, 2.5, 3.5, 5.0];
/// let mut integral = Integral::default();
/// let mut derivative = Derivative::new(1, &s[0]).unwrap();
///
/// (&s).iter().for_each(|v| {
/// let integration_constant = s[0];
/// let der = derivative.next(v);
/// let integr = integral.next(&der) + integration_constant;
///
/// assert_eq!(integr, *v);
/// });
/// ```
///
/// # Performance
///
/// O(1)
///
/// # See also
///
/// [Derivative](crate::methods::Derivative)
///
/// [`ValueType`]: crate::core::ValueType
/// [`PeriodType`]: crate::core::PeriodType
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Integral {
value: ValueType,
window: Window<ValueType>,
}
/// Just an alias for Integral
pub type Sum = Integral;
impl Method for Integral {
type Params = PeriodType;
type Input = ValueType;
type Output = Self::Input;
fn new(length: Self::Params, &value: &Self::Input) -> Result<Self, Error> {
Ok(Self {
window: Window::new(length, value),
value: value * length as ValueType,
})
}
#[inline]
fn next(&mut self, &value: &Self::Input) -> Self::Output {
self.value += value;
if !self.window.is_empty() {
self.value -= self.window.push(value);
}
self.value
}
}
impl Default for Integral {
fn default() -> Self {
Self::new(0, &0.0).unwrap()
}
}
impl Peekable<<Self as Method>::Output> for Integral {
fn peek(&self) -> <Self as Method>::Output {
self.value
}
}
#[cfg(test)]
mod tests {
use super::{Integral as TestingMethod, Method};
use crate::core::ValueType;
use crate::helpers::{assert_eq_float, RandomCandles};
use crate::methods::tests::test_const;
#[test]
fn test_integral_const() {
for i in 1..255 {
let input = (i as ValueType + 56.0) / 16.3251;
let mut method = TestingMethod::new(i, &input).unwrap();
let output = method.next(&input);
test_const(&mut method, &input, &output);
}
}
#[test]
#[should_panic(expected = "assertion")]
fn test_integral0_const() {
use crate::core::Method;
use crate::methods::tests::test_const;
let input = (5.0 + 56.0) / 16.3251;
let mut method = TestingMethod::new(0, &input).unwrap();
let output = method.next(&input);
test_const(&mut method, &input, &output);
}
#[test]
fn test_integral0() {
let src: Vec<ValueType> = RandomCandles::default()
.take(100)
.map(|x| x.close)
.collect();
let mut ma = TestingMethod::new(0, &src[0]).unwrap();
src.iter().enumerate().for_each(|(i, x)| {
let value1 = ma.next(x);
let value2 = src.iter().take(i + 1).fold(0.0, |s, &c| s + c);
// assert_eq!(value1, value2, "at index {} with value {}: {:?}", i, x, q);
assert_eq_float(value2, value1);
});
}
#[test]
fn test_integral1() {
let mut candles = RandomCandles::default();
let mut ma = TestingMethod::new(1, &candles.first().close).unwrap();
candles.take(100).for_each(|x| {
assert_eq_float(x.close, ma.next(&x.close));
});
}
#[test]
fn test_integral() {
let candles = RandomCandles::default();
let src: Vec<ValueType> = candles.take(300).map(|x| x.close).collect();
(1..255).for_each(|length| {
let mut ma = TestingMethod::new(length, &src[0]).unwrap();
let length = length as usize;
src.iter().enumerate().for_each(|(i, x)| {
let value1 = ma.next(x);
let value2 = (0..length).fold(0.0, |s, j| s + src[i.saturating_sub(j)]);
assert_eq_float(value2, value1);
});
});
}
}