Skip to content

Commit 0628728

Browse files
Dianeoddmorgankx
andauthored
Timeseries upd (#81)
* set autocorrelation of lag 0 to be 1 * updated fix for differencing in timeseries models * windows test files Co-authored-by: dmorgankx <44678213+dmorgankx@users.noreply.github.com> Co-authored-by: Deanna Morgan <dmorgan1@kx.com>
1 parent 09ad749 commit 0628728

File tree

11 files changed

+9
-9
lines changed

11 files changed

+9
-9
lines changed

fresh/extract.q

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ fresh.feat.agglintrend:{
1010
t:fresh.feat.lintrend each(max;min;var;avg)@/:\:y cut x;
1111
(`$"_"sv'string cols[t]cross`max`min`var`avg)!raze value flip t}
1212
fresh.feat.augfuller:{`teststat`pvalue`usedlag!3#"f"$@[{fresh.i.adfuller[x]`};x;0n]}
13-
fresh.feat.autocorr:{(avg(x-m)*xprev[y;x]-m:avg x)%var x}
13+
fresh.feat.autocorr:{$[y=0;1f;(avg(x-m)*xprev[y;x]-m:avg x)%var x]}
1414
fresh.feat.binnedentropy:{neg sum p*log p:(count each group(y-1)&floor y*x%max x-:min x)%count x}
1515
/ t-series non-linearity - Schreiber, T. and Schmitz, A. (1997). PHYSICAL REVIEW E, VOLUME 55, NUMBER 5
1616
fresh.feat.c3:{avg x*/xprev\:[-1 -2*y]x}

timeseries/fit.q

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ ts.ARMA.fit:{[endog;exog;lags;resid;trend]
6969
ts.ARIMA.fit:{[endog;exog;lags;diff;resid;trend]
7070
exog:ts.i.fitDataCheck[endog;exog];
7171
// Apply integration (non seasonal)
72-
I:ts.i.differ[endog;diff;()!()];
72+
I:ts.i.differ[endog;diff;()!()]`final;
7373
// Fit an ARMA model on the differenced time series
7474
mdl:ts.ARMA.fit[I;diff _exog;lags;resid;trend];
7575
// Retrieve the original data to be used when fitting on new data
@@ -107,9 +107,9 @@ ts.SARIMA.fit:{[endog;exog;lags;diff;resid;trend;seas]
107107
// add additional seasonal components
108108
dict[`seas_add_P`seas_add_Q]:(raze'){1+til[x]+/:y}'[(lags;resid);dict`P`Q];
109109
// Generate data for regenerate data following differencing
110-
origDiffSeason:`origd`origs!(diff{deltas x}/neg[diff]#endog;neg[prd seas`D`m]#endog);
110+
origDiffSeason:`origd`origs!(diff{deltas x}/neg[diff]#endog;neg[prd seas`D`m]#I`init);
111111
// Apply SARMA model and postpend differenced original data
112-
ts.i.SARMA.model[I;exog;dict],origDiffSeason
112+
ts.i.SARMA.model[I`final;exog;dict],origDiffSeason
113113
}
114114

115115
// @kind function
0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.

timeseries/utils.q

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -709,17 +709,17 @@ ts.i.predDataCheck:{[mdl;exog]
709709
// @param endog {num[]} endogenous dataset
710710
// @param diff {integer} non seasonal differencing component (integer)
711711
// @param sdict {dict} dictionary containing relevant seasonal differencing components
712-
// @return {num[]} Seasonal and non-seasonally differenced stationary time-series
712+
// @return {dict} Seasonal and non-seasonally differenced stationary time-series
713713
ts.i.differ:{[endog;d;s]
714714
// Apply non seasonal differencing if appropriate (handling of AR/ARMA)
715715
if[s~()!();s[`D]:0b];
716-
I:ts.i.diff[endog;d];
716+
initDiff:ts.i.diff[endog;d];
717717
// Apply seasonal differencing if appropriate
718-
if[s[`D];I:s[`D]ts.i.seasonDiff[s`m]/I];
718+
finalDiff:$[s[`D];s[`D]ts.i.seasonDiff[s`m]/initDiff;initDiff];
719719
// Check stationarity
720-
if[not ts.i.stationary[I];ts.i.err.stat[]];
720+
if[not ts.i.stationary[finalDiff];ts.i.err.stat[]];
721721
// Return integrated data
722-
I
722+
`final`init!(finalDiff;initDiff)
723723
}
724724

725725

0 commit comments

Comments
 (0)