API reference

andrey.BootstrapOutput#

class andrey.BootstrapOutput[source]#

The edge frequencies over bootstrap refits of one method, and the averaged graph.

bootstrap returns one (multi_group_direct_lingam returns one per group), and BootstrapOutput.from_fits builds one from fits made elsewhere. structure is the averaged graph. frequency reads the share of fits in which a pair has a given edge, table lists every edge that appeared, and average takes the averaged graph at another threshold without refitting. print(out) shows the averaged graph and its most frequent edges. The result is read-only.

An edge is read as the pair (i, j) carries it: -> is a tail at i and an arrowhead at j, <- the reverse, -- two tails, <-> two arrowheads, and o a circle. A fit adds its edge to a pair’s count once. A time-series result has its own frequencies for each lag. A lag graph that is a digraph (every lag above 0) counts each directed edge i -> j on its own, so a 2-cycle adds to both -> and <-, and it holds no other edge.

classmethod from_fits(fits, *, threshold=0.5, strict=False, keep_fits=False, metadata=None)[source]#

Count the edges of fits and build the averaged graph.

Use it to summarize fits made elsewhere, such as resamples drawn another way. Every fit must be over the same nodes and, for a time-series result, the same lags.

Parameters:
  • fits (Sequence[StructureOutput]) – The results to count, at least one.

  • threshold (float) – A pair of the averaged graph must be adjacent in more than this share of the fits, or in all of them, in [0.5, 1].

  • strict (bool) – Raise when the vote of the fits has no valid graph of their kind, instead of returning the valid fit nearest the others with a warning.

  • keep_fits (bool) – Keep fits in the fits property.

  • metadata (Mapping[str, str | bool | int | float | None | list | dict] | None) – Run values by name, with the types StructureOutput accepts.

Returns:

The counts and the averaged graph. The vote gives a pair its most frequent edge when more fits have that edge than have no edge and the pair is adjacent in more than threshold of the fits, or in all of them; otherwise the pair has no edge. When two edges tie for most frequent, the pair takes the least committal one: --, or o-o for a pag. A digraph lag graph takes each directed edge that more than threshold of the fits have, or all of them. The vote is then made a valid graph of the fits’ kind, the widest among them (dag, cpdag, pag, with a mag read as a pag), as bootstrap’s Notes describe. metadata["averaged_graph"] is "vote", or "nearest_resample" when the vote has no valid graph of that kind and the averaged graph is the fit nearest the others among the fits that are valid graphs of that kind, or "nearest_resample_invalid" when no fit is, so the averaged graph is the fit nearest the others and not a valid graph of that kind. A cpdag fit is valid when it has a consistent DAG extension, and a pag or mag fit when it passes the steps that make a vote a PAG. from_fits has no data to rank the nodes by, so a dag vote with a tie edge becomes a cpdag that leaves the tie edge undirected; bootstrap points it by the ranks of its data.

Return type:

BootstrapOutput

Raises:
  • TypeError – If a fit is not a StructureOutput.

  • ValueError – If fits is empty, threshold is not in [0.5, 1], the fits differ in graph type, node count, labels, or lags, a graph is a digraph or undirected in some fits and another kind in others, a fit has latent nodes, or strict is True and the vote has no valid graph of the fits’ kind.

Warns:

AndreyWarning – When strict is False and the vote has no valid graph of the fits’ kind.

property structure: Structure#

a GraphStructure, or a TemporalStructure.

A time-series result has one graph per lag and no time axis or weights.

Type:

The averaged graph at threshold

property n_resamples: int#

The number of fits counted.

property n_boot: int#

The former name of n_resamples, which stops working in the next release.

property threshold: float#

more, or all.

Type:

The share of fits in which a pair of structure must be adjacent

property strict: bool#

Whether a vote with no valid graph of the fits’ kind raises, rather than warns.

property fits: tuple[StructureOutput, ...] | None#

The fits, in resample order, or None unless keep_fits=True.

property metadata: Mapping[str, str | bool | int | float | None | list | dict]#

Run values such as method, seed, and block_length, as a read-only mapping.

average(threshold=0.5)[source]#

Return the averaged graph at threshold, without refitting.

Parameters:

threshold (float) – A pair must be adjacent in more than this share of the fits, or in all of them, in [0.5, 1].

Returns:

The graph from_fits describes, at this threshold, with the strict this result was built with. When the vote has no valid graph of the fits’ kind, it is the fit nearest the others, as structure would be: the valid one when some fit is.

Return type:

Structure

Raises:

ValueError – If threshold is not in [0.5, 1], or the result was built with strict=True and the vote at threshold has no valid graph of the fits’ kind.

Warns:

AndreyWarning – When the result was built with strict=False and the vote at threshold has no valid graph of the fits’ kind.

frequency(edge, *, lag=None)[source]#

Return the share of fits in which each pair has edge, as an (n, n) array.

Parameters:
  • edge (str) – One of "->", "<-", "--", "<->", "o->", "<-o", "o-o", "-o", "o-", or "absent". Cell [i, j] is the share of fits in which nodes i and j read i edge j, so the array for "<-" is the transpose of the one for "->". "absent" is the share with no edge between i and j (for a lag above 0, with no edge i -> j).

  • lag (int | None) – The lag of a time-series result; None for any other result.

Returns:

The shares, from 0 to 1. A lag above 0 holds only ->, <-, and absent; any other edge there is 0, and so is an edge that no fit has.

Return type:

ndarray

Raises:

ValueError – If edge is not one of the names above, lag is missing for a time-series result, or lag is not one of its lags or is given for another result.

table(*, min_frequency=0.0, index=False)[source]#

List every edge that appeared in a fit, most frequent first.

Parameters:
  • min_frequency (float) – Leave out edges seen in a smaller share of the fits.

  • index (bool) – Name the nodes by index even when the result has labels.

Returns:

One row per edge, most frequent first, then by lag and node index. A row has source, target, type (one of oriented_edges types: a directed edge runs from its tail to its arrowhead, and the symmetric types list the lower node index first), count, and frequency; a time-series result adds lag first. A pair can have several rows, one per edge it had in some fit.

Return type:

list[dict[str, Any]]