Skip to content

Error in handling final empty tree in stats API #2745

Closed

Description

@jeromekelleher

We seem to be returning a nonzero value in the final window when there is no topology in branch stats:

import tskit

ts = tskit.Tree.generate_comb(4).tree_sequence
tables = ts.dump_tables()
tables.sequence_length = 3
tables.edges.left += 1
tables.edges.right += 1
ts = tables.tree_sequence()

X = ts.diversity(windows="trees", mode="branch")
print(X)

Gives

3.00┊         ┊   6     ┊         ┊
    ┊         ┊ ┏━┻━┓   ┊         ┊
2.00┊         ┊ ┃   5   ┊         ┊
    ┊         ┊ ┃ ┏━┻┓  ┊         ┊
1.00┊         ┊ ┃ ┃  4  ┊         ┊
    ┊         ┊ ┃ ┃ ┏┻┓ ┊         ┊
0.00┊ 0 1 2 3 ┊ 0 1 2 3 ┊ 0 1 2 3 ┊
    0         1         2         3

[ 0.00000000e+00  4.66666667e+00 -4.44089210e-16]

The value for the last window should be 0 like the first window. I thought initially that this was a staightforward uninitialised memory issue, but it's not that simple. The value we compute seems to depend deterministically on the middle tree topology (many topologies don't lead to this error at all). The value is close to zero, but not that close, and larger trees seem to lead to larger errors in the final value.

I made a start on writing some tests, but I'm not sure what I'm looking for now.

This cropped up in the windowed divmat implementation in #2736, where these errors are causing tests to fail when I compare the branch stat version of the divergence matrix with the new implementations.

Activity

  1. added
    bug
    on Apr 22, 2023
  2. jeromekelleher commented on Apr 23, 2023

    @jeromekelleher
    MemberAuthor

    Whoops, this isn't a but after all, just some subtle numerical stuff. Because the stats API is maintaining a running sum, it must subtract all the previous values out when we have an empty tree, and this inevitably leads to some numerical fuzziness. The only reason this numerical fuzziness becomes a problem here is because we're comparing with zero, we have to specify an absolute rather than relative tolerance.

    Nothing to see here, closing!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions