.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/04_flows/plot_05_higher_order_network.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code or to run this example in your browser via Binder. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_04_flows_plot_05_higher_order_network.py: 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 :math:`k/\log_2(1+\sigma)`). .. GENERATED FROM PYTHON SOURCE LINES 13-28 .. code-block:: Python 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) .. image-sg:: /auto_examples/04_flows/images/sphx_glr_plot_05_higher_order_network_001.png :alt: higher-order network: 19 events, 30 memory nodes :srcset: /auto_examples/04_flows/images/sphx_glr_plot_05_higher_order_network_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none 30 memory nodes: ['coffee|shower', 'commute|shower', 'coffee|wake up', 'tv|gym', 'work|work', 'work|walk', 'tv|work.dinner', 'commute|coffee'] 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} .. GENERATED FROM PYTHON SOURCE LINES 29-32 A first-order process has no memory ----------------------------------- The web-shop journeys are generated by a first-order Markov chain: no memory node survives. .. GENERATED FROM PYTHON SOURCE LINES 32-35 .. code-block:: Python shop = sv.datasets.make_customer_journeys(1000, random_state=0) plot_hon(shop, min_support=20) .. image-sg:: /auto_examples/04_flows/images/sphx_glr_plot_05_higher_order_network_002.png :alt: higher-order network: 10 events, 0 memory nodes :srcset: /auto_examples/04_flows/images/sphx_glr_plot_05_higher_order_network_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 36-38 Parameter study: the threshold ------------------------------ .. GENERATED FROM PYTHON SOURCE LINES 38-42 .. code-block:: Python 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() .. rst-class:: sphx-glr-script-out .. code-block:: none threshold None: 30 memory nodes threshold 0.1: 52 memory nodes threshold 0.5: 30 memory nodes threshold 1.0: 18 memory nodes .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.484 seconds) .. _sphx_glr_download_auto_examples_04_flows_plot_05_higher_order_network.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: binder-badge .. image:: images/binder_badge_logo.svg :target: https://mybinder.org/v2/gh/ComplexSystemsIntelligence/spmviz/main?urlpath=lab/tree/notebooks/auto_examples/04_flows/plot_05_higher_order_network.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_05_higher_order_network.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_05_higher_order_network.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_05_higher_order_network.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_