Skip to content

Commit

Permalink
add statistical functions
Browse files Browse the repository at this point in the history
  • Loading branch information
chungg committed Jul 16, 2024
1 parent 5063ad9 commit 9385139
Show file tree
Hide file tree
Showing 10 changed files with 1,198 additions and 14 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ that you can beat the market.

## types of indicators

100+ indicators available across 5 categories. Even across categories, indicators often
130+ indicators available across multiple categories. Even across categories, indicators often
behave quite similarly depending on window size. The classfication/api may change
(if egregiously wrong).

Expand All @@ -43,6 +43,9 @@ the indicator, it may be a momentum indicator or trend indicator.
Provides moving average functions. Often used to track trend, levels of support, breakouts, etc...
The results are in the same scale as input data and are often used as a signal line for input data.

### correlation
Signals that compare two or more variables and their relationship to one another.

## installation
1. (optional) https://rustup.rs/
2. (optional) cargo new <lib name>
Expand All @@ -64,7 +67,7 @@ encouraged.
- cargo test
- cargo bench
- cargo run --example file_json
-

## todo
- handle div by zero scenarios
- allow other numeric types rather than just f64
83 changes: 83 additions & 0 deletions benches/traquer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,7 @@ fn criterion_benchmark(c: &mut Criterion) {
c.bench_function("sig-volatility-cc_hv", |b| {
b.iter(|| black_box(volatility::cc_hv(&stats.close, 16).collect::<Vec<_>>()))
});

c.bench_function("ma-ewma", |b| {
b.iter(|| black_box(smooth::ewma(&stats.close, 16).collect::<Vec<f64>>()))
});
Expand Down Expand Up @@ -580,6 +581,88 @@ fn criterion_benchmark(c: &mut Criterion) {
c.bench_function("ma-t3", |b| {
b.iter(|| black_box(smooth::t3(&stats.close, 6, None).collect::<Vec<f64>>()))
});

c.bench_function("correlation-pcc", |b| {
b.iter(|| black_box(correlation::pcc(&stats.close, &stats.close, 16).collect::<Vec<_>>()))
});
c.bench_function("correlation-rsq", |b| {
b.iter(|| black_box(correlation::rsq(&stats.close, &stats.close, 16).collect::<Vec<_>>()))
});
c.bench_function("correlation-beta", |b| {
b.iter(|| black_box(correlation::beta(&stats.close, &stats.close, 16).collect::<Vec<_>>()))
});
c.bench_function("correlation-rsc", |b| {
b.iter(|| black_box(correlation::rsc(&stats.close, &stats.close).collect::<Vec<_>>()))
});
c.bench_function("correlation-perf", |b| {
b.iter(|| black_box(correlation::perf(&stats.close, &stats.close, 16).collect::<Vec<_>>()))
});

c.bench_function("stats-dist-variance", |b| {
b.iter(|| {
black_box(statistic::distribution::variance(&stats.close, 16).collect::<Vec<_>>())
})
});
c.bench_function("stats-dist-std_dev", |b| {
b.iter(|| black_box(statistic::distribution::std_dev(&stats.close, 16).collect::<Vec<_>>()))
});
c.bench_function("stats-dist-zscore", |b| {
b.iter(|| black_box(statistic::distribution::zscore(&stats.close, 16).collect::<Vec<_>>()))
});

c.bench_function("stats-dist-mad", |b| {
b.iter(|| black_box(statistic::distribution::mad(&stats.close, 16).collect::<Vec<_>>()))
});
c.bench_function("stats-dist-cv", |b| {
b.iter(|| black_box(statistic::distribution::cv(&stats.close, 16).collect::<Vec<_>>()))
});
c.bench_function("stats-dist-kurtosis", |b| {
b.iter(|| {
black_box(statistic::distribution::kurtosis(&stats.close, 16).collect::<Vec<_>>())
})
});
c.bench_function("stats-dist-skew", |b| {
b.iter(|| black_box(statistic::distribution::skew(&stats.close, 16).collect::<Vec<_>>()))
});
c.bench_function("stats-dist-median", |b| {
b.iter(|| black_box(statistic::distribution::median(&stats.close, 16).collect::<Vec<_>>()))
});
c.bench_function("stats-dist-quantile", |b| {
b.iter(|| {
black_box(statistic::distribution::quantile(&stats.close, 16, 90.0).collect::<Vec<_>>())
})
});

c.bench_function("stats-regress-mse", |b| {
b.iter(|| {
black_box(statistic::regression::mse(&stats.close, &stats.open).collect::<Vec<_>>())
})
});
c.bench_function("stats-regress-rmse", |b| {
b.iter(|| {
black_box(statistic::regression::rmse(&stats.close, &stats.open).collect::<Vec<_>>())
})
});
c.bench_function("stats-regress-mae", |b| {
b.iter(|| {
black_box(statistic::regression::mae(&stats.close, &stats.open).collect::<Vec<_>>())
})
});
c.bench_function("stats-regress-mape", |b| {
b.iter(|| {
black_box(statistic::regression::mape(&stats.close, &stats.open).collect::<Vec<_>>())
})
});
c.bench_function("stats-regress-smape", |b| {
b.iter(|| {
black_box(statistic::regression::smape(&stats.close, &stats.open).collect::<Vec<_>>())
})
});
c.bench_function("stats-regress-mda", |b| {
b.iter(|| {
black_box(statistic::regression::mda(&stats.close, &stats.open).collect::<Vec<_>>())
})
});
}

criterion_group!(benches, criterion_benchmark);
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
pub mod correlation;
pub mod momentum;
pub mod smooth;
pub mod statistic;
pub mod trend;
pub mod volatility;
pub mod volume;
11 changes: 3 additions & 8 deletions src/smooth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ use std::collections::VecDeque;
use std::f64::consts::PI;
use std::iter;

use crate::statistic::distribution::_std_dev;

/// Moving average types
pub enum MaMode {
SMA,
Expand Down Expand Up @@ -278,13 +280,6 @@ pub fn hull(data: &[f64], window: usize) -> impl Iterator<Item = f64> + '_ {
.into_iter()
}

pub(crate) fn std_dev(data: &[f64], window: usize) -> impl Iterator<Item = f64> + '_ {
data.windows(window).map(move |w| {
let mean = w.iter().sum::<f64>() / window as f64;
(w.iter().map(|x| (x - mean).powi(2)).sum::<f64>() / window as f64).sqrt()
})
}

/// Volatility Index Dynamic Average (VIDYA)
///
/// A type of moving average that uses a combination of short-term and long-term
Expand All @@ -301,7 +296,7 @@ pub(crate) fn std_dev(data: &[f64], window: usize) -> impl Iterator<Item = f64>
/// ```
pub fn vidya(data: &[f64], window: usize) -> impl Iterator<Item = f64> + '_ {
let alpha = 2.0 / (window + 1) as f64;
let std5 = std_dev(data, 5).collect::<Vec<f64>>();
let std5 = _std_dev(data, 5).collect::<Vec<f64>>();
let std20 = sma(&std5, 20).collect::<Vec<f64>>();
let offset = (5 - 1) + (20 - 1);
iter::repeat(f64::NAN).take(offset).chain(
Expand Down
Loading

0 comments on commit 9385139

Please sign in to comment.