Skip to content

Commit

Permalink
Timeseries upd (#81)
Browse files Browse the repository at this point in the history
* set autocorrelation of lag 0 to be 1

* updated fix for differencing in timeseries models

* windows test files

Co-authored-by: dmorgankx <[email protected]>
Co-authored-by: Deanna Morgan <[email protected]>
  • Loading branch information
3 people authored Oct 7, 2020
1 parent 09ad749 commit 0628728
Show file tree
Hide file tree
Showing 11 changed files with 9 additions and 9 deletions.
2 changes: 1 addition & 1 deletion fresh/extract.q
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ fresh.feat.agglintrend:{
t:fresh.feat.lintrend each(max;min;var;avg)@/:\:y cut x;
(`$"_"sv'string cols[t]cross`max`min`var`avg)!raze value flip t}
fresh.feat.augfuller:{`teststat`pvalue`usedlag!3#"f"$@[{fresh.i.adfuller[x]`};x;0n]}
fresh.feat.autocorr:{(avg(x-m)*xprev[y;x]-m:avg x)%var x}
fresh.feat.autocorr:{$[y=0;1f;(avg(x-m)*xprev[y;x]-m:avg x)%var x]}
fresh.feat.binnedentropy:{neg sum p*log p:(count each group(y-1)&floor y*x%max x-:min x)%count x}
/ t-series non-linearity - Schreiber, T. and Schmitz, A. (1997). PHYSICAL REVIEW E, VOLUME 55, NUMBER 5
fresh.feat.c3:{avg x*/xprev\:[-1 -2*y]x}
Expand Down
6 changes: 3 additions & 3 deletions timeseries/fit.q
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ ts.ARMA.fit:{[endog;exog;lags;resid;trend]
ts.ARIMA.fit:{[endog;exog;lags;diff;resid;trend]
exog:ts.i.fitDataCheck[endog;exog];
// Apply integration (non seasonal)
I:ts.i.differ[endog;diff;()!()];
I:ts.i.differ[endog;diff;()!()]`final;
// Fit an ARMA model on the differenced time series
mdl:ts.ARMA.fit[I;diff _exog;lags;resid;trend];
// Retrieve the original data to be used when fitting on new data
Expand Down Expand Up @@ -107,9 +107,9 @@ ts.SARIMA.fit:{[endog;exog;lags;diff;resid;trend;seas]
// add additional seasonal components
dict[`seas_add_P`seas_add_Q]:(raze'){1+til[x]+/:y}'[(lags;resid);dict`P`Q];
// Generate data for regenerate data following differencing
origDiffSeason:`origd`origs!(diff{deltas x}/neg[diff]#endog;neg[prd seas`D`m]#endog);
origDiffSeason:`origd`origs!(diff{deltas x}/neg[diff]#endog;neg[prd seas`D`m]#I`init);
// Apply SARMA model and postpend differenced original data
ts.i.SARMA.model[I;exog;dict],origDiffSeason
ts.i.SARMA.model[I`final;exog;dict],origDiffSeason
}

// @kind function
Expand Down
Binary file modified timeseries/tests/data/linux/fit/SARIMA3
Binary file not shown.
Binary file modified timeseries/tests/data/linux/fit/SARIMA4
Binary file not shown.
Binary file modified timeseries/tests/data/linux/pred/predSARIMA3
Binary file not shown.
Binary file modified timeseries/tests/data/linux/pred/predSARIMA4
Binary file not shown.
Binary file modified timeseries/tests/data/windows/fit/SARIMA3
Binary file not shown.
Binary file modified timeseries/tests/data/windows/fit/SARIMA4
Binary file not shown.
Binary file modified timeseries/tests/data/windows/pred/predSARIMA3
Binary file not shown.
Binary file modified timeseries/tests/data/windows/pred/predSARIMA4
Binary file not shown.
10 changes: 5 additions & 5 deletions timeseries/utils.q
Original file line number Diff line number Diff line change
Expand Up @@ -709,17 +709,17 @@ ts.i.predDataCheck:{[mdl;exog]
// @param endog {num[]} endogenous dataset
// @param diff {integer} non seasonal differencing component (integer)
// @param sdict {dict} dictionary containing relevant seasonal differencing components
// @return {num[]} Seasonal and non-seasonally differenced stationary time-series
// @return {dict} Seasonal and non-seasonally differenced stationary time-series
ts.i.differ:{[endog;d;s]
// Apply non seasonal differencing if appropriate (handling of AR/ARMA)
if[s~()!();s[`D]:0b];
I:ts.i.diff[endog;d];
initDiff:ts.i.diff[endog;d];
// Apply seasonal differencing if appropriate
if[s[`D];I:s[`D]ts.i.seasonDiff[s`m]/I];
finalDiff:$[s[`D];s[`D]ts.i.seasonDiff[s`m]/initDiff;initDiff];
// Check stationarity
if[not ts.i.stationary[I];ts.i.err.stat[]];
if[not ts.i.stationary[finalDiff];ts.i.err.stat[]];
// Return integrated data
I
`final`init!(finalDiff;initDiff)
}


Expand Down

0 comments on commit 0628728

Please sign in to comment.