Skip to content

Commit

Permalink
create predictions from ARIMA and LSTM
Browse files Browse the repository at this point in the history
  • Loading branch information
jzerbe committed Nov 20, 2020
1 parent 02ca8d4 commit fc524bf
Show file tree
Hide file tree
Showing 8 changed files with 2,401 additions and 26 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ Create model input data file from Yahoo Finance stock ticker API for particular

Train multi variable time series model against input data and output prediction of values in the original problem space:

`docker run --volume /local/path/to/api.json:/tmp/ml_timeseries/api.json --volume /local/path/to/prediction.json:/tmp/ml_timeseries/prediction.json docker.pkg.github.com/vraid-systems/ml_timeseries/main:2.0.0`
`docker run --env MODEL=lstm --volume /local/path/to/api.json:/tmp/ml_timeseries/api.json --volume /local/path/to/prediction.json:/tmp/ml_timeseries/prediction.json docker.pkg.github.com/vraid-systems/ml_timeseries/main:2.0.0`

Note that `MODEL` can be `arima` or `lstm`.


Label the predicted features:
Expand Down
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
},
"dependencies": {
"@tensorflow/tfjs-node": "^2.0.1",
"arima": "0.1.0",
"array-smooth": "^1.0.0",
"axios": "^0.19.2",
"lodash": "^4.17.19",
Expand Down
76 changes: 76 additions & 0 deletions src/MultiVariableArima.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
const arima = require('arima')
const lodash = require('lodash')

class MultiVariableArima {
constructor(
ascendingHistoricalData,
barsToPredict = 4,
) {
this.barsToPredict = barsToPredict
this.numberOfFeatureVariables = ascendingHistoricalData[0].length - 1
this.positionOfTimeFeature = 0

this.historicalData = ascendingHistoricalData.filter(
(currentElement) => (currentElement.length - 1) === this.numberOfFeatureVariables,
)
this.historicalFeatureValues = this.historicalData.map(
(currentFeatureTuple) => currentFeatureTuple.filter(
(
currentElementInTuple, currentIndexInTuple,
) => currentIndexInTuple !== this.positionOfTimeFeature,
),
)
}

predictNextBars() {
const historicalTimeBars = this.historicalData.map(
(currentFeatureTuple) => currentFeatureTuple[this.positionOfTimeFeature],
)
const barSize = Math.abs(historicalTimeBars[1] - historicalTimeBars[0])
const mostRecentTimeBar = historicalTimeBars[historicalTimeBars.length - 1]

const getFeatureByPosition = (
historicalFeatureValues,
featurePosition,
) => historicalFeatureValues.map(
(currentFeatureTuple) => currentFeatureTuple.filter(
(
currentElementInTuple, currentIndexInTuple,
) => currentIndexInTuple === featurePosition,
),
)

const allFeaturePredictions = []
for (let index = 0; index < this.numberOfFeatureVariables; index += 1) {
const [singleFeaturePredictions] = arima(
getFeatureByPosition(this.historicalFeatureValues, index),
this.barsToPredict,
{
auto: false,
d: 0, // Number of times the series needs to be differenced
method: 0, // ARIMA method (Default: 0)
optimizer: 6, // Optimization method (Default: 6)
p: 1, // Number of Autoregressive coefficients
q: 1, // Number of Moving Average Coefficients
transpose: false,
verbose: true, // Output model analysis to console
},
)
allFeaturePredictions.push(singleFeaturePredictions)
}
const predictedFeatureTuples = lodash.zip(...allFeaturePredictions)

const predictedNextBars = []
for (let index = 0; index < this.barsToPredict; index += 1) {
const nextBarNumber = index + 1
predictedNextBars.push({
time: mostRecentTimeBar + (barSize * nextBarNumber),
features: predictedFeatureTuples[index],
})
}

return predictedNextBars
}
}

module.exports = MultiVariableArima

0 comments on commit fc524bf

Please sign in to comment.