Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
9
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Switch to GitLab Next
Sign in / Register
Toggle navigation
Open sidebar
Rui Vieira
mentat
Commits
2e7d267a
Commit
2e7d267a
authored
May 05, 2019
by
Rui Vieira
🍵
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add exponentially weighted moving average
parent
095d2d09
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
93 additions
and
5 deletions
+93
-5
Cargo.toml
Cargo.toml
+1
-1
docs/README.md
docs/README.md
+29
-1
docs/figures/ewma.png
docs/figures/ewma.png
+0
-0
src/lib.rs
src/lib.rs
+4
-3
src/timeseries/mod.rs
src/timeseries/mod.rs
+59
-0
No files found.
Cargo.toml
View file @
2e7d267a
[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."
...
...
docs/README.md
View file @
2e7d267a
# 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
);
```

# Monotonic Cubic Spline interpolation
```
rust
...
...
@@ -17,4 +45,4 @@ for i in 0..100 {
}
```

\ No newline at end of file

\ No newline at end of file
docs/figures/ewma.png
0 → 100644
View file @
2e7d267a
23 KB
src/lib.rs
View file @
2e7d267a
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!
();
}
...
...
src/timeseries/mod.rs
0 → 100644
View file @
2e7d267a
#[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
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment