Commit 2e7d267a authored by Rui Vieira's avatar Rui Vieira 🍵

Add exponentially weighted moving average

parent 095d2d09
[package]
name = "mentat"
version = "0.0.3"
version = "0.0.4"
authors = ["Rui Vieira <ruidevieira@googlemail.com>"]
exclude = [".idea/**/*", "docs/**/*"]
description = "A Rust library for scientific computing."
......
# Examples
# Exponentially weighted moving average
```rust
use matplotrust::*;
let mut samples: Vec<f64> = Vec::new();
let mut rng = rand::thread_rng();
samples.push(10.0);
let mut ewma = EWMA::new(10.0, 25);
let mut average: Vec<f64> = Vec::new();
average.push(10.0);
for i in 1..100 {
let prev = samples[i-1];
samples.push(prev + rng.gen_range(-2.0, 2.0));
average.push(ewma.update(samples[i]));
}
print!("{:?}", average);
let mut figure = Figure::new();
let points = scatter_plot::<i64, f64>((0..100).collect(), samples, None);
figure.add_plot(points);
let mut options = LinePlotOptions::new();
options.colour = Some("red".to_string());
let line = line_plot::<i64, f64>((0..100).collect(), average, Some(options));
figure.add_plot(line);
let s = figure.save("./docs/figures/ewma.png", None);
```
![](figures/ewma.png)
# Monotonic Cubic Spline interpolation
```rust
......@@ -17,4 +45,4 @@ for i in 0..100 {
}
```
![plot](figures/monotonic_cubic_spline.png)
\ No newline at end of file
![](figures/monotonic_cubic_spline.png)
\ No newline at end of file
extern crate rand;
#[cfg(test)]
extern crate matplotrust;
mod utils;
extern crate rand;
use rand::prelude::*;
use rand::distributions::Normal as N;
use std::f64::consts::PI;
mod utils;
mod timeseries;
macro_rules! assert_delta {
($x:expr, $y:expr, $d:expr) => {
if !(($x - $y).abs() < $d || ($y - $x).abs() < $d) { panic!(); }
......
#[cfg(test)]
extern crate rand;
struct EWMA {
current: f64,
age: i64,
decay: f64
}
impl EWMA {
pub fn new(value: f64, age: i64) -> EWMA {
let decay = 2.0 / (age as f64 + 1.0);
return EWMA {
age: age,
decay: decay,
current: value
};
}
pub fn update(&mut self, value : f64) -> f64 {
self.current = (value * self.decay) + (self.current * (1.0 - self.decay));
return self.current;
}
}
#[cfg(test)]
mod test_ewma {
use super::*;
use rand::Rng;
#[test]
fn ewma_test() {
use matplotrust::*;
let mut samples: Vec<f64> = Vec::new();
let mut rng = rand::thread_rng();
samples.push(10.0);
let mut ewma = EWMA::new(10.0, 25);
let mut average: Vec<f64> = Vec::new();
average.push(10.0);
for i in 1..100 {
let prev = samples[i-1];
samples.push(prev + rng.gen_range(-2.0, 2.0));
average.push(ewma.update(samples[i]));
}
print!("{:?}", average);
let mut figure = Figure::new();
let points = scatter_plot::<i64, f64>((0..100).collect(), samples, None);
figure.add_plot(points);
let mut options = LinePlotOptions::new();
options.colour = Some("red".to_string());
let line = line_plot::<i64, f64>((0..100).collect(), average, Some(options));
figure.add_plot(line);
let s = figure.save("./docs/figures/ewma.png", None);
}
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment