Pattern-induced higher-order networks: when history matters#

The transition graph assumes that the next step depends only on the current one. In the daily routines it does not: after work people go to lunch if they came from the morning commute, but commute home if they came from lunch. A higher-order network (Xu, Wickramarathne & Chawla, 2016) keeps such memory nodes — work|commute is “work, reached after commute” — only where the frequent sequences change the continuation distribution significantly (Kullback–Leibler divergence above \(k/\log_2(1+\sigma)\)).

import matplotlib.pyplot as plt

import spmviz as sv
from spmviz.networks import higher_order_network
from spmviz.viz import plot_hon

db = sv.datasets.make_daily_routines(300, random_state=0)
H = higher_order_network(db, max_order=3, min_support=10)
memory = [v for v, d in H.nodes(data=True) if d["order"] > 1]
print(len(memory), "memory nodes:", memory[:8])
for v in ("work|commute", "work|lunch"):
    if v in H:
        print(v, "->", {w: round(d["probability"], 2) for _, w, d in H.out_edges(v, data=True)})
plot_hon(H)
higher-order network: 19 events, 30 memory nodes
30 memory nodes: ['shower|run', 'commute|work', 'work|work', 'work|commute', 'commute|breakfast', 'commute|coffee', 'work|breakfast', 'coffee|run']
work|commute -> {'meeting|commute.work': 0.43, 'work|work': 0.04, 'lunch': 0.43, 'cook': 0.01, 'social media|work': 0.01, 'gym': 0.03, 'snack': 0.01, 'email': 0.01, 'commute|work': 0.02, 'coffee': 0.01}
work|lunch -> {'commute|work': 0.8, 'snack': 0.02, 'gym': 0.03, 'dinner': 0.02, 'cook': 0.09, 'social media|work': 0.02, 'work|work': 0.01, 'email': 0.01, 'tv': 0.01}

<Axes: title={'left': 'higher-order network: 19 events, 30 memory nodes'}>

A first-order process has no memory#

The web-shop journeys are generated by a first-order Markov chain: no memory node survives.

shop = sv.datasets.make_customer_journeys(1000, random_state=0)
plot_hon(shop, min_support=20)
higher-order network: 10 events, 0 memory nodes
<Axes: title={'left': 'higher-order network: 10 events, 0 memory nodes'}>

Parameter study: the threshold#

for thr in (None, 0.1, 0.5, 1.0):
    Ht = higher_order_network(db, max_order=3, min_support=10, threshold=thr)
    print(f"threshold {thr}: {sum(d['order'] > 1 for _, d in Ht.nodes(data=True))} memory nodes")
plt.show()
threshold None: 30 memory nodes
threshold 0.1: 52 memory nodes
threshold 0.5: 30 memory nodes
threshold 1.0: 18 memory nodes

Total running time of the script: (0 minutes 0.593 seconds)

Gallery generated by Sphinx-Gallery