Skip to Content
Causal analysis

Causal analysis

Query a snapshot for paths, causes, effects and feedback loops. Core analysis works on the dataset without a renderer; use the React panels when presenting those results in an interface.

In a causal graph, direction is the meaning of an edge. “Rain causes wet ground” isn’t the same as the reverse, so analysis has to follow edge direction. analyzeSnapshot in @cytocausal/core computes directed centrality and strongly connected groups directly from a snapshot:

import { analyzeSnapshot, type Snapshot } from "@cytocausal/core"; const snapshot: Snapshot = { id: "demo", nodes: ["a", "b", "c"].map((id) => ({ id, label: id })), edges: [ { id: "a-b", source: "a", target: "b" }, { id: "b-c", source: "b", target: "c" }, { id: "c-b", source: "c", target: "b" }, ], }; const { betweenness, feedbackGroups } = analyzeSnapshot(snapshot); console.log(feedbackGroups); // [["b", "c"]] in either order console.log(betweenness);

Run it in a script or on a server. Explorer does this when its data is imported, so page load only has to draw the graph.

Directed centrality

Betweenness centrality finds nodes that sit on many shortest paths. Ignoring direction counts paths that causes can’t actually travel along.

ACCESS Explorer’s original node sizes ignored direction. Recalculating them along edge direction changed the picture:

MeasureUndirectedDirected
Nodes with a score of 0247400
Top 20 nodes shared2013

The rank correlation between the two scores was 0.74, so they’re related but not interchangeable. Pass directed: true to betweennessCentrality, which analyzeSnapshot does for you.

Feedback loops

A causal network with feedback isn’t a chain of causes. feedbackGroups lists strongly connected components with more than one node: sets of nodes that can all reach each other, so each set contains at least one loop. It uses Cytoscape’s tarjanStronglyConnected.

Explorer’s network has one such group with 213 of its 725 nodes, about 29% of the graph, and matches the result from NetworkX. The app can isolate loops with “Show only feedback loops”.

A group tells you loops exist, but not what each loop is or how it behaves. For that, list the loops themselves.

Individual loops

findFeedbackLoops lists each loop once, shortest first, and classifies it from link polarity:

import { findFeedbackLoops, type Snapshot } from "@cytocausal/core"; const snapshot: Snapshot = { id: "demo", nodes: [ { id: "trust", label: "Trust" }, { id: "use", label: "Use" }, ], edges: [ { id: "trust-use", source: "trust", target: "use", polarity: "positive" }, { id: "use-trust", source: "use", target: "trust", polarity: "negative" }, ], }; const { loops, complete } = findFeedbackLoops(snapshot); console.log(loops[0].nodes); // ["trust", "use"] console.log(loops[0].type); // "balancing" console.log(complete); // true
Negative linksTypeBehaviour
An even numberreinforcingChange builds on itself
An odd numberbalancingChange pushes back
Polarity missingunknownCan’t be classified

Across Perspectiva’s 16 diagrams, it finds 89 loops: 59 reinforcing and 30 balancing. Two participants’ diagrams have none in either version, so handle an empty list.

Large graphs

The number of loops can grow very quickly with graph size. Set maxLength on large graphs, and use through to search around one node. complete is false when maxLength or limit may have hidden loops, so don’t present a capped list as the full count. The search goes one length at a time, so a list cut short by limit still holds the shortest loops.

Maximum length in ExplorerLoops
318
457
5159
6463

These counts, and every Perspectiva loop and type, match NetworkX’s simple_cycles.

To show a loop, pass it to highlightRoute(cy, loop), which adds route-node and route-link classes and dims everything else. Pass null to clear it.

Causes and effects

Highlighting a neighbourhood should separate what leads into a node from what comes out of it. highlightCausalNeighbours adds classes for you to style:

import { highlightCausalNeighbours } from "@cytocausal/cytoscape"; import type { Core } from "cytoscape"; export function followSelection(cy: Core, selected: string[]) { highlightCausalNeighbours(cy, selected); }
ClassApplied to
focusThe given nodes
cause, cause-linkDirect causes and their links
effect, effect-linkDirect effects and their links
dimmedEverything else

Use node.incomers() and node.outgoers() for this. node.neighborhood() ignores direction.

Pass a depth to reach further: highlightCausalNeighbours(cy, selected, 2) also marks the causes of causes and the effects of effects. In a loop, a node can be both.

Paths

findCausalPaths lists the routes from one node to another, following the direction of each link. No route visits a node twice.

import { findCausalPaths, type Snapshot } from "@cytocausal/core"; export function routes(snapshot: Snapshot) { const { paths, complete } = findCausalPaths(snapshot, "customers", "queue", { maxLength: 8, limit: 50, }); return complete ? paths : paths.slice(0, 10); }

Paths come shortest first, and a list cut short by limit holds the shortest ones. maxLength counts nodes. complete is false when maxLength or limit may have hidden some paths.

Version differences

diffSnapshots compares any two snapshots. It follows nodes through every version between them with followNodes, so comparing the first with the fifth keeps a rename made in the second. Reading it the other way swaps added and removed, and merged and split. An unknown snapshot id throws an error that names the snapshots the dataset has.

import { diffSnapshots, type CausalDataset } from "@cytocausal/core"; export function summary(dataset: CausalDataset) { const diff = diffSnapshots(dataset, "V1", "final"); return `${diff.addedNodes.length} added, ${diff.removedNodes.length} removed`; }
FieldContains
addedNodes, removedNodesNodes with no counterpart
renamedNodesNodes whose label changed, as { from, to }
mergedNodesSeveral nodes that became one, as { from, to }
splitNodesOne node that became several, as { from, to }
regroupedNodesSeveral nodes that became several others, as { from, to }
addedLinks, removedLinksLinks with no counterpart
changedLinksLinks whose polarity changed, as { from, to, polarity }

Links are compared by the nodes they join, so parallel links between two nodes count as one. When they disagree, their polarity is "mixed".

To mark a diff on a graph, changeMarks(diff, side) sorts it into what one snapshot can show: "from" for the earlier, with what was removed, or "to" for the later, with what was added. Both include what’s in both versions but changed: renamed, merged, split or regrouped nodes, and links whose polarity flipped. Pass the result to highlightChanges. Added parts are blue and removed parts vermilion. What stayed the same fades, so changed parts stand out by keeping full contrast, with ± on flipped links.

Interactive cycle searches

Browser views run cycle enumeration in a Web Worker. The core search first isolates strongly connected components, keeping singleton self-loops and discarding edges between components. Results remain shortest first. Changing the graph, selected node or length terminates the previous worker; cancellation terminates computation rather than just hiding its result. Cancelled and failed searches are not shown as empty completed results.

LoopSettingsProvider and LoopSearchControls share the length and cancellation state across the lists and radial menus of a case. CausalProvider accepts loopSettings; standalone views can use LoopSettingsProvider with useLoopSearch. The synchronous core API remains available for scripts.

CaseLabelInitial maximum lengthResult limit per search
ExplorerEvent cycles430
Perspectiva participantParticipant cyclesUnlimited1000
Perspectiva combinedWorkshop cycles61000
StarterCoffee shop cyclesUnlimited1000

Length counts nodes (including one-node self-loops), independently of the neighbourhood’s “Steps shown”. Removing the length bound does not remove the result limit. A + on a result count means the search may be incomplete. The combined and participant scenarios reset their settings when the dataset changes.

The cancel button is shown only while workers are running. Completed lists show their cycle count and elapsed time. The shared controls deduplicate cycles within each snapshot and show the longest completed search time; cancelled and failed searches are not reported as completed.

Version history

A dataset can hold any number of versions, in order. Each step from one version to the next uses its correspondence, or matches ids where it has none, and these join up across many versions.

FunctionReturns
followNodes(dataset, from, to)Which nodes of to each node of from became, for any two versions, in either direction. A correspondence stated for exactly those two wins
versionSteps(dataset)The diff from each version to the next, with counts of what was added, removed and changed
nodeHistory(dataset, version, id)A node’s story: where it started or was added, each rename, merge and split, and where it was removed. Each entry has parts, as nodes that later merge may change in different ways before
linkHistory(dataset, version, id)A link’s story, following both its ends through renames and merges: where it started or was added, each polarity flip, and where it was removed
import { nodeHistory, type CausalDataset } from "@cytocausal/core"; export const story = (dataset: CausalDataset) => nodeHistory(dataset, "final", "customer-satisfaction").map(({ version, change }) => `${version}: ${change}`); // ["V1: start", "V2: renamed", "V3: unchanged", "V4: unchanged", "final: unchanged"]

validateDataset warns where two neighbouring versions have no correspondence and most node ids change, since history can’t be followed there.

Last updated on