Skip to content

Commit

Permalink
fix: default index in predicted is -1
Browse files Browse the repository at this point in the history
  • Loading branch information
piercus committed Sep 23, 2020
1 parent 278313e commit da8b3d3
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 8 deletions.
9 changes: 5 additions & 4 deletions lib/core-kalman-filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ class CoreKalmanFilter {
const initState = new State({
mean: meanInit,
covariance: covarianceInit,
index: indexInit});
index: indexInit
});
return initState;
}

Expand All @@ -79,10 +80,10 @@ class CoreKalmanFilter {
* @returns{Array.<Array.<Number>>}
*/

getPredictedCovariance({previousCorrected} = {}) {
getPredictedCovariance({previousCorrected, index} = {}) {
previousCorrected = previousCorrected || this.getInitState();

const getValueOptions = {previousCorrected, index: previousCorrected.index};
const getValueOptions = {previousCorrected, index};
const d = this.getValue(this.dynamic.transition, getValueOptions);
const dTransposed = transpose(d);
const covarianceInter = matMul(d, previousCorrected.covariance);
Expand Down Expand Up @@ -119,7 +120,7 @@ class CoreKalmanFilter {

const mean = matMul(d, previousCorrected.mean);

const covariance = this.getPredictedCovariance({previousCorrected});
const covariance = this.getPredictedCovariance({previousCorrected, index});

const predicted = new State({mean, covariance, index});
this.logger.debug('Prediction done', predicted);
Expand Down
3 changes: 2 additions & 1 deletion lib/setup/extend-dynamic-init.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ module.exports = function ({observation, dynamic}) {
dynamic: Object.assign({}, dynamic, {
init: {
mean: meanArray.map(element => [element]),
covariance: diag(covarianceArray)
covariance: diag(covarianceArray),
index: -1
}
})
};
Expand Down
2 changes: 1 addition & 1 deletion test/unit/core/trivial.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ test('Impact previousCorrected and dynamic covariance', t => {
});
const predicted = kf.predict({previousCorrected});
t.true(predicted instanceof State);
t.is(predicted.index, null);
t.is(predicted.index, undefined);
t.true(2 / trace(predicted.covariance) > huge / 2); // Verifying that the sum of the variance is tiny
});

Expand Down
5 changes: 3 additions & 2 deletions test/unit/kalman-filter/polymorphism.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ test('Dynamic init', t => {
const huge = 1e6;
const initObjective = {
mean: [[0], [0], [0], [0]],
index: -1,
covariance: [
[huge, 0, 0, 0],
[0, huge, 0, 0],
Expand Down Expand Up @@ -328,6 +329,6 @@ test('Index initialization', t => {
const predicted2 = kf.predict({previousCorrected: firstState});
t.false(Number.isNaN(predicted1.index));
t.false(Number.isNaN(predicted2.index));
t.is(predicted1.index, null);
t.is(predicted2.index, null);
t.is(predicted1.index, undefined);
t.is(predicted2.index, undefined);
});

0 comments on commit da8b3d3

Please sign in to comment.