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:
parent
4afa542422
commit
a5608dd7c8
|
@ -117,6 +117,12 @@ export function findStationaryDistribution(
|
||||||
}
|
}
|
||||||
let iteration = 0;
|
let iteration = 0;
|
||||||
while (true) {
|
while (true) {
|
||||||
|
if (iteration >= fullOptions.maxIterations) {
|
||||||
|
if (fullOptions.verbose) {
|
||||||
|
console.log(`[${iteration}] FAILED to converge`);
|
||||||
|
}
|
||||||
|
return r0;
|
||||||
|
}
|
||||||
iteration++;
|
iteration++;
|
||||||
const r1 = sparseMarkovChainAction(chain, r0);
|
const r1 = sparseMarkovChainAction(chain, r0);
|
||||||
const delta = computeDelta(r0, r1);
|
const delta = computeDelta(r0, r1);
|
||||||
|
@ -130,12 +136,6 @@ export function findStationaryDistribution(
|
||||||
}
|
}
|
||||||
return r0;
|
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 knows that this next line is unreachable, but Flow doesn't. :-)
|
||||||
// eslint-disable-next-line no-unreachable
|
// eslint-disable-next-line no-unreachable
|
||||||
|
|
|
@ -182,5 +182,12 @@ describe("core/attribution/markovChain", () => {
|
||||||
const expected = new Float64Array([0.5, 0.5]);
|
const expected = new Float64Array([0.5, 0.5]);
|
||||||
expectAllClose(pi, expected);
|
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);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue