Skip to content

Commit

Permalink
Added docs for functions [skip ci]
Browse files Browse the repository at this point in the history
  • Loading branch information
ankane committed Sep 23, 2023
1 parent 021d465 commit 4a81940
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/mstl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ use super::{Error, MstlParams, MstlResult};
pub struct Mstl;

impl Mstl {
/// Decomposes a time series.
pub fn fit(series: &[f32], periods: &[usize]) -> Result<MstlResult, Error> {
MstlParams::new().fit(series, periods)
}

/// Creates a new set of parameters.
pub fn params() -> MstlParams {
MstlParams::new()
}
Expand Down
6 changes: 6 additions & 0 deletions src/mstl_params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub struct MstlParams {
}

impl MstlParams {
/// Creates a new set of parameters.
pub fn new() -> Self {
Self {
iterate: 2,
Expand All @@ -18,26 +19,31 @@ impl MstlParams {
}
}

/// Sets the number of iterations.
pub fn iterations(&mut self, iterate: usize) -> &mut Self {
self.iterate = iterate;
self
}

/// Sets lambda for Box-Cox transformation.
pub fn lambda(&mut self, lambda: f32) -> &mut Self {
self.lambda = Some(lambda);
self
}

/// Sets the lengths of the seasonal smoothers.
pub fn seasonal_lengths(&mut self, swin: &[usize]) -> &mut Self {
self.swin = Some(swin.to_vec());
self
}

/// Sets the STL parameters.
pub fn stl_params(&mut self, stl_params: StlParams) -> &mut Self {
self.stl_params = stl_params;
self
}

/// Decomposes a time series.
pub fn fit(&self, series: &[f32], periods: &[usize]) -> Result<MstlResult, Error> {
// return error to be consistent with stl
// and ensure seasonal is always same length as periods
Expand Down
6 changes: 6 additions & 0 deletions src/mstl_result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,32 @@ pub struct MstlResult {
}

impl MstlResult {
/// Returns the seasonal components.
pub fn seasonal(&self) -> &[Vec<f32>] {
&self.seasonal[..]
}

/// Returns the trend component.
pub fn trend(&self) -> &[f32] {
&self.trend
}

/// Returns the remainder.
pub fn remainder(&self) -> &[f32] {
&self.remainder
}

/// Returns the seasonal strength.
pub fn seasonal_strength(&self) -> Vec<f32> {
self.seasonal().iter().map(|s| strength(s, self.remainder())).collect()
}

/// Returns the trend strength.
pub fn trend_strength(&self) -> f32 {
strength(self.trend(), self.remainder())
}

/// Takes ownership of the components.
pub fn into_parts(self) -> (Vec<Vec<f32>>, Vec<f32>, Vec<f32>) {
(self.seasonal, self.trend, self.remainder)
}
Expand Down
2 changes: 2 additions & 0 deletions src/stl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ use super::{Error, StlParams, StlResult};
pub struct Stl;

impl Stl {
/// Decomposes a time series.
pub fn fit(series: &[f32], period: usize) -> Result<StlResult, Error> {
StlParams::new().fit(series, period)
}

/// Creates a new set of parameters.
pub fn params() -> StlParams {
StlParams::new()
}
Expand Down
14 changes: 14 additions & 0 deletions src/stl_params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub struct StlParams {
}

impl StlParams {
/// Creates a new set of parameters.
pub fn new() -> Self {
Self {
ns: None,
Expand All @@ -35,66 +36,79 @@ impl StlParams {
}
}

/// Sets the length of the seasonal smoother.
pub fn seasonal_length(&mut self, ns: usize) -> &mut Self {
self.ns = Some(ns);
self
}

/// Sets the length of the trend smoother.
pub fn trend_length(&mut self, nt: usize) -> &mut Self {
self.nt = Some(nt);
self
}

/// Sets the length of the low-pass filter.
pub fn low_pass_length(&mut self, nl: usize) -> &mut Self {
self.nl = Some(nl);
self
}

/// Sets the degree of locally-fitted polynomial in seasonal smoothing.
pub fn seasonal_degree(&mut self, isdeg: i32) -> &mut Self {
self.isdeg = isdeg;
self
}

/// Sets the degree of locally-fitted polynomial in trend smoothing.
pub fn trend_degree(&mut self, itdeg: i32) -> &mut Self {
self.itdeg = itdeg;
self
}

/// Sets the degree of locally-fitted polynomial in low-pass smoothing.
pub fn low_pass_degree(&mut self, ildeg: i32) -> &mut Self {
self.ildeg = Some(ildeg);
self
}

/// Sets the skipping value for seasonal smoothing.
pub fn seasonal_jump(&mut self, nsjump: usize) -> &mut Self {
self.nsjump = Some(nsjump);
self
}

/// Sets the skipping value for trend smoothing.
pub fn trend_jump(&mut self, ntjump: usize) -> &mut Self {
self.ntjump = Some(ntjump);
self
}

/// Sets the skipping value for low-pass smoothing.
pub fn low_pass_jump(&mut self, nljump: usize) -> &mut Self {
self.nljump = Some(nljump);
self
}

/// Sets the number of loops for updating the seasonal and trend components.
pub fn inner_loops(&mut self, ni: usize) -> &mut Self {
self.ni = Some(ni);
self
}

/// Sets the number of iterations of robust fitting.
pub fn outer_loops(&mut self, no: usize) -> &mut Self {
self.no = Some(no);
self
}

/// Sets whether robustness iterations are to be used.
pub fn robust(&mut self, robust: bool) -> &mut Self {
self.robust = robust;
self
}

/// Decomposes a time series.
pub fn fit(&self, series: &[f32], period: usize) -> Result<StlResult, Error> {
let y = series;
let np = period;
Expand Down
7 changes: 7 additions & 0 deletions src/stl_result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,37 @@ pub(crate) fn strength(component: &[f32], remainder: &[f32]) -> f32 {
}

impl StlResult {
/// Returns the seasonal component.
pub fn seasonal(&self) -> &[f32] {
&self.seasonal
}

/// Returns the trend component.
pub fn trend(&self) -> &[f32] {
&self.trend
}

/// Returns the remainder.
pub fn remainder(&self) -> &[f32] {
&self.remainder
}

/// Returns the weights.
pub fn weights(&self) -> &[f32] {
&self.weights
}

/// Returns the seasonal strength.
pub fn seasonal_strength(&self) -> f32 {
strength(self.seasonal(), self.remainder())
}

/// Returns the trend strength.
pub fn trend_strength(&self) -> f32 {
strength(self.trend(), self.remainder())
}

/// Takes ownership of the components.
pub fn into_parts(self) -> (Vec<f32>, Vec<f32>, Vec<f32>, Vec<f32>) {
(self.seasonal, self.trend, self.remainder, self.weights)
}
Expand Down

0 comments on commit 4a81940

Please sign in to comment.