Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

r_iterate_matrix function in tests returns incorrect stable state if loop reaches niter #15

Open
jdyen opened this issue Sep 6, 2019 · 3 comments

Comments

@jdyen
Copy link
Contributor

jdyen commented Sep 6, 2019

I think there is a mismatch in the stable states returned by the r_iterate_matrix and iterate_matrix functions if the loop runs to the final iteration. Not sure if current tests are ending the loop before niter, hence not erroring.

The (possible) issue arises when the while loop never breaks due to diff > tol, in which case the final iteration is (i < niter => i = niter - 1). This iteration increments i by 1, then stores the output to states[[i+1]]. So the final element of states is at niter + 1.

while(i < niter & diff > tol) {
i <- i + 1
states[[i + 1]] <- matrix %*% states[[i]]

But then the stable state is returned as states[[i]], which is states[[niter]] in this case:

stable_distribution <- states[[i]]

Is this intentional? If not, I can prepare a PR. The same issue arises on the iterate_dynamic_matrix branch.

@jdyen
Copy link
Contributor Author

jdyen commented Sep 6, 2019

In fact, this should cause an issue in all cases, shouldn't it? I can't decipher the tf_extract_stable_distribution function so am not sure whether this pulls out the last or second-to-last iteration.

@goldingn
Copy link
Member

goldingn commented Sep 6, 2019

One difference is that the stable state will be normalised (to sum to 1), but the last nonzero element of all states isn't. Did you account for that?

Even if it's the penultimate state, the convergence criterion should mean it's the same as the ultimate one (when normalised, and to within the tolerance).

@goldingn
Copy link
Member

goldingn commented Sep 6, 2019

Oh, I misread this, sorry. Not sure, but will take a look when my brain is less addled!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants