{ "cells": [ { "cell_type": "markdown", "id": "c952a6fb", "metadata": {}, "source": [ "# Building CTMCs\n", "A Continuous Time Markov Chain (CTMC) is similar to a DTMC. The differences are that time is *continuous*, not discrete, and that transitions have *rates* instead of probabilities. So to summarize, a CTMC has:\n", "* states (includig an initial state)\n", "* transitions with rates. The rate is an indication of the speed in which a transition occurs. To be precise, the probability that state $s_i$ goes to state $s_j$ in $t$ time steps is $1- e^{R(s_i,s_j)\\cdot t}$, where $R(s_i,s_j)$ is the *rate* between $s_i$ and $s_j$.\n", "* labels\n", "\n", "As an example for a CTMC, we have a model of a star. It will first fuse hydrogen into helium until there is no hydrogen left, then it will fuse helium into carbon, etc. until there is only iron left and the star goes supernova." ] }, { "cell_type": "code", "execution_count": 1, "id": "3787d8d7", "metadata": { "execution": { "iopub.execute_input": "2026-03-26T10:47:15.983878Z", "iopub.status.busy": "2026-03-26T10:47:15.983634Z", "iopub.status.idle": "2026-03-26T10:47:16.379861Z", "shell.execute_reply": "2026-03-26T10:47:16.379317Z" } }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", " \n", " Network\n", " \n", " \n", " \n", " \n", " \n", "
\n", " \n", " \n", " \n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "from stormvogel import *\n", "\n", "# Create a new model\n", "ctmc = stormvogel.model.new_ctmc()\n", "\n", "# hydrogen fuses into helium\n", "ctmc.get_state_by_id(0).set_choice([(3, ctmc.new_state(\"helium\"))])\n", "# helium fuses into carbon\n", "ctmc.get_state_by_id(1).set_choice([(2, ctmc.new_state(\"carbon\"))])\n", "# carbon fuses into iron\n", "ctmc.get_state_by_id(2).set_choice([(7, ctmc.new_state(\"iron\"))])\n", "# supernova\n", "ctmc.get_state_by_id(3).set_choice([(12, ctmc.new_state(\"Supernova\"))])\n", "\n", "# we add the rates which are equal to whats in the transitions since the probabilities are all 1\n", "rates = [3, 2, 7, 12, 0]\n", "for i in range(5):\n", " ctmc.set_rate(ctmc.get_state_by_id(i), rates[i])\n", "\n", "# we add self loops to all states with no outgoing transitions\n", "ctmc.add_self_loops()\n", "vis = show(ctmc, layout=Layout(\"layouts/star.json\"))" ] }, { "cell_type": "markdown", "id": "c85326c9", "metadata": {}, "source": [ "Or using the bird API" ] }, { "cell_type": "code", "execution_count": 2, "id": "128a5982", "metadata": { "execution": { "iopub.execute_input": "2026-03-26T10:47:16.387099Z", "iopub.status.busy": "2026-03-26T10:47:16.386780Z", "iopub.status.idle": "2026-03-26T10:47:16.417881Z", "shell.execute_reply": "2026-03-26T10:47:16.417309Z" } }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", " \n", " Network\n", " \n", " \n", " \n", " \n", " \n", "
\n", " \n", " \n", " \n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "from stormvogel import *\n", "\n", "init = bird.State(x=\"\")\n", "\n", "\n", "def delta(s: bird.State):\n", " if s == init:\n", " return [(3, bird.State(x=[\"helium\"]))]\n", " elif \"helium\" in s.x:\n", " return [(2, bird.State(x=[\"carbon\"]))]\n", " elif \"carbon\" in s.x:\n", " return [(7, bird.State(x=[\"iron\"]))]\n", " elif \"iron\" in s.x:\n", " return [(12, bird.State(x=[\"Supernova\"]))]\n", " else:\n", " return [(0, s)]\n", "\n", "\n", "labels = lambda s: s.x\n", "\n", "bird_star = bird.build_bird(\n", " delta=delta, init=init, labels=labels, modeltype=ModelType.CTMC\n", ")\n", "\n", "vis2 = show(bird_star, layout=Layout(\"layouts/star.json\"))" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.12.13" }, "widgets": { "application/vnd.jupyter.widget-state+json": { "state": { "15e7e72dddfe45eb801918f85f048df3": { "model_module": "@jupyter-widgets/output", "model_module_version": "1.0.0", "model_name": "OutputModel", "state": { "_dom_classes": [], "_model_module": "@jupyter-widgets/output", "_model_module_version": "1.0.0", "_model_name": "OutputModel", "_view_count": null, "_view_module": "@jupyter-widgets/output", "_view_module_version": "1.0.0", "_view_name": "OutputView", "layout": "IPY_MODEL_561eaaf760c143748bc3cbf623f09ee4", "msg_id": "", "outputs": [], "tabbable": null, "tooltip": null } }, "1ccd5063077e46aca600cde8c2a9f4ce": { "model_module": "@jupyter-widgets/output", "model_module_version": "1.0.0", "model_name": "OutputModel", "state": { "_dom_classes": [], "_model_module": "@jupyter-widgets/output", "_model_module_version": "1.0.0", "_model_name": "OutputModel", "_view_count": null, "_view_module": "@jupyter-widgets/output", "_view_module_version": "1.0.0", "_view_name": "OutputView", "layout": "IPY_MODEL_74129b69248544f18672dddb876af111", "msg_id": "", "outputs": [], "tabbable": null, "tooltip": null } }, "561eaaf760c143748bc3cbf623f09ee4": { "model_module": "@jupyter-widgets/base", "model_module_version": "2.0.0", "model_name": "LayoutModel", "state": { "_model_module": "@jupyter-widgets/base", "_model_module_version": "2.0.0", "_model_name": "LayoutModel", "_view_count": null, "_view_module": "@jupyter-widgets/base", "_view_module_version": "2.0.0", "_view_name": "LayoutView", "align_content": null, "align_items": null, "align_self": null, "border_bottom": null, "border_left": null, "border_right": null, "border_top": null, "bottom": null, "display": null, "flex": null, "flex_flow": null, "grid_area": null, "grid_auto_columns": null, "grid_auto_flow": null, "grid_auto_rows": null, "grid_column": null, "grid_gap": null, "grid_row": null, "grid_template_areas": null, "grid_template_columns": null, "grid_template_rows": null, "height": null, "justify_content": null, "justify_items": null, "left": null, "margin": null, "max_height": null, "max_width": null, "min_height": null, "min_width": null, "object_fit": null, "object_position": null, "order": null, "overflow": null, "padding": null, "right": null, "top": null, "visibility": null, "width": null } }, "6fc6cfff8f9146019319b68e4ba04ec5": { "model_module": "@jupyter-widgets/base", "model_module_version": "2.0.0", "model_name": "LayoutModel", "state": { "_model_module": "@jupyter-widgets/base", "_model_module_version": "2.0.0", "_model_name": "LayoutModel", "_view_count": null, "_view_module": "@jupyter-widgets/base", "_view_module_version": "2.0.0", "_view_name": "LayoutView", "align_content": null, "align_items": null, "align_self": null, "border_bottom": null, "border_left": null, "border_right": null, "border_top": null, "bottom": null, "display": null, "flex": null, "flex_flow": null, "grid_area": null, "grid_auto_columns": null, "grid_auto_flow": null, "grid_auto_rows": null, "grid_column": null, "grid_gap": null, "grid_row": null, "grid_template_areas": null, "grid_template_columns": null, "grid_template_rows": null, "height": null, "justify_content": null, "justify_items": null, "left": null, "margin": null, "max_height": null, "max_width": null, "min_height": null, "min_width": null, "object_fit": null, "object_position": null, "order": null, "overflow": null, "padding": null, "right": null, "top": null, "visibility": null, "width": null } }, "74129b69248544f18672dddb876af111": { "model_module": "@jupyter-widgets/base", "model_module_version": "2.0.0", "model_name": "LayoutModel", "state": { "_model_module": "@jupyter-widgets/base", "_model_module_version": "2.0.0", "_model_name": "LayoutModel", "_view_count": null, "_view_module": "@jupyter-widgets/base", "_view_module_version": "2.0.0", "_view_name": "LayoutView", "align_content": null, "align_items": null, "align_self": null, "border_bottom": null, "border_left": null, "border_right": null, "border_top": null, "bottom": null, "display": null, "flex": null, "flex_flow": null, "grid_area": null, "grid_auto_columns": null, "grid_auto_flow": null, "grid_auto_rows": null, "grid_column": null, "grid_gap": null, "grid_row": null, "grid_template_areas": null, "grid_template_columns": null, "grid_template_rows": null, "height": null, "justify_content": null, "justify_items": null, "left": null, "margin": null, "max_height": null, "max_width": null, "min_height": null, "min_width": null, "object_fit": null, "object_position": null, "order": null, "overflow": null, "padding": null, "right": null, "top": null, "visibility": null, "width": null } }, "b86a014c7b834d6786ad49122062069e": { "model_module": "@jupyter-widgets/base", "model_module_version": "2.0.0", "model_name": "LayoutModel", "state": { "_model_module": "@jupyter-widgets/base", "_model_module_version": "2.0.0", "_model_name": "LayoutModel", "_view_count": null, "_view_module": "@jupyter-widgets/base", "_view_module_version": "2.0.0", "_view_name": "LayoutView", "align_content": null, "align_items": null, "align_self": null, "border_bottom": null, "border_left": null, "border_right": null, "border_top": null, "bottom": null, "display": null, "flex": null, "flex_flow": null, "grid_area": null, "grid_auto_columns": null, "grid_auto_flow": null, "grid_auto_rows": null, "grid_column": null, "grid_gap": null, "grid_row": null, "grid_template_areas": null, "grid_template_columns": null, "grid_template_rows": null, "height": null, "justify_content": null, "justify_items": null, "left": null, "margin": null, "max_height": null, "max_width": null, "min_height": null, "min_width": null, "object_fit": null, "object_position": null, "order": null, "overflow": null, "padding": null, "right": null, "top": null, "visibility": null, "width": null } }, "bbae1b711b7343a5ab0af022c6a2e8a7": { "model_module": "@jupyter-widgets/output", "model_module_version": "1.0.0", "model_name": "OutputModel", "state": { "_dom_classes": [], "_model_module": "@jupyter-widgets/output", "_model_module_version": "1.0.0", "_model_name": "OutputModel", "_view_count": null, "_view_module": "@jupyter-widgets/output", "_view_module_version": "1.0.0", "_view_name": "OutputView", "layout": "IPY_MODEL_b86a014c7b834d6786ad49122062069e", "msg_id": "", "outputs": [], "tabbable": null, "tooltip": null } }, "f76bb223f3f1459fa6b906476932972c": { "model_module": "@jupyter-widgets/output", "model_module_version": "1.0.0", "model_name": "OutputModel", "state": { "_dom_classes": [], "_model_module": "@jupyter-widgets/output", "_model_module_version": "1.0.0", "_model_name": "OutputModel", "_view_count": null, "_view_module": "@jupyter-widgets/output", "_view_module_version": "1.0.0", "_view_name": "OutputView", "layout": "IPY_MODEL_6fc6cfff8f9146019319b68e4ba04ec5", "msg_id": "", "outputs": [], "tabbable": null, "tooltip": null } } }, "version_major": 2, "version_minor": 0 } } }, "nbformat": 4, "nbformat_minor": 5 }