Fix off-by-1 error in PageRank iteration limit (#452)

If `findStationaryDistribution` is passed `0` as `maxIterations`, then
it should return the initial distribution.

Test plan: see new unit test

Paired with @wchargin
This commit is contained in:
Dandelion Mané 2018-06-29 14:01:17 -07:00 committed by GitHub
parent 4afa542422
commit a5608dd7c8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 6 deletions

View File

@ -117,6 +117,12 @@ export function findStationaryDistribution(
}
let iteration = 0;
while (true) {
if (iteration >= fullOptions.maxIterations) {
if (fullOptions.verbose) {
console.log(`[${iteration}] FAILED to converge`);
}
return r0;
}
iteration++;
const r1 = sparseMarkovChainAction(chain, r0);
const delta = computeDelta(r0, r1);
@ -130,12 +136,6 @@ export function findStationaryDistribution(
}
return r0;
}
if (iteration >= fullOptions.maxIterations) {
if (fullOptions.verbose) {
console.log(`[${iteration}] FAILED to converge`);
}
return r0;
}
}
// ESLint knows that this next line is unreachable, but Flow doesn't. :-)
// eslint-disable-next-line no-unreachable

View File

@ -182,5 +182,12 @@ describe("core/attribution/markovChain", () => {
const expected = new Float64Array([0.5, 0.5]);
expectAllClose(pi, expected);
});
it("returns initial distribution if maxIterations===0", () => {
const chain = sparseMarkovChainFromTransitionMatrix([[0, 1], [0, 1]]);
const pi = findStationaryDistribution(chain, {maxIterations: 0});
const expected = new Float64Array([0.5, 0.5]);
expect(pi).toEqual(expected);
});
});
});