Skip to content

Average arity of nodes in the trees #2723

Open
@jeromekelleher

Description

@jeromekelleher

It's useful to have a summary of the average arity of nodes in the trees. Here's a function that computes it:

def nodes_mean_arity(ts):
    weighted_arity = np.zeros(ts.num_nodes)
    span_in_tree = np.zeros(ts.num_nodes)
    for tree in ts.trees():
        weighted_arity += tree.num_children_array[:-1] * tree.span
        span_in_tree[tree.num_children_array[:-1] > 0] += tree.span
    span_in_tree[ts.samples()] = ts.sequence_length
    weighted_arity /= span_in_tree
    return weighted_arity

We could add this as a function to the TreeSequence? (The naming is chosen to follow the arrays like nodes_time etc)

We can imagine having different options for weighting, at some point.

To do it really efficiently, we'd need to have to implement in C with an edge-diffs approach, but this implementation is probably quite fast for most purposes.

Although, I wonder if there a way of expressing the operation as a node stat?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Python APIIssue is about the Python APIenhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions