{ "cells": [ { "cell_type": "markdown", "id": "1a22c16b", "metadata": {}, "source": [ "# Parametric and Interval Models\n", "In this notebook, we will show how to create and work with parametric and\n", "interval models.\n", "## Parametric Models\n", "\n", "Stormvogel represents parametric transition values as **sympy expressions**.\n", "Any polynomial or rational function over `sympy.Symbol` parameters is a valid\n", "transition value, which means you can write them using ordinary Python\n", "arithmetic." ] }, { "cell_type": "code", "execution_count": 1, "id": "590744a2", "metadata": { "execution": { "iopub.execute_input": "2026-10-22T15:30:24.365015Z", "iopub.status.busy": "2026-10-22T15:30:24.364837Z", "iopub.status.idle": "2026-10-22T15:30:24.606528Z", "shell.execute_reply": "2026-10-22T15:30:24.605906Z" } }, "outputs": [], "source": [ "import sympy as sp" ] }, { "cell_type": "markdown", "id": "1dc2a8f6", "metadata": {}, "source": [ "For example, a polynomial in two variables:" ] }, { "cell_type": "code", "execution_count": 2, "id": "9fa309ea", "metadata": { "execution": { "iopub.execute_input": "2026-10-22T15:30:24.608768Z", "iopub.status.busy": "2026-10-22T15:30:24.608539Z", "iopub.status.idle": "2026-10-22T15:30:24.637792Z", "shell.execute_reply": "2026-10-22T15:30:24.637252Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "x**2 + y**2\n", "6*z**3 + z + 2\n", "(x**2 + y**2)/(6*z**3 + z + 2)\n" ] } ], "source": [ "x, y, z = sp.symbols(\"x y z\")\n", "\n", "polynomial1 = x**2 + y**2\n", "print(polynomial1)\n", "\n", "polynomial2 = 6 * z**3 + z + 2\n", "print(polynomial2)\n", "\n", "rational_function = polynomial1 / polynomial2\n", "print(rational_function)" ] }, { "cell_type": "markdown", "id": "9c7d97c5", "metadata": {}, "source": [ "To create a parametric model (e.g. pMC or pMDP) we simply use these sympy\n", "expressions as transition probabilities. As an example, we build a\n", "parametric Knuth–Yao dice: a coin with success probability `x` is flipped\n", "repeatedly to simulate a six-sided die." ] }, { "cell_type": "code", "execution_count": 3, "id": "513a0a38", "metadata": { "execution": { "iopub.execute_input": "2026-10-22T15:30:24.639748Z", "iopub.status.busy": "2026-10-22T15:30:24.639551Z", "iopub.status.idle": "2026-10-22T15:30:25.150936Z", "shell.execute_reply": "2026-10-22T15:30:25.150397Z" } }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", " \n", " Network\n", " \n", " \n", " \n", " \n", " \n", "
\n", " \n", " \n", " \n", " \n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from stormvogel import model, bird\n", "from stormvogel.show import show\n", "\n", "# A single symbolic parameter for the coin bias.\n", "x = sp.Symbol(\"x\")\n", "\n", "\n", "# States are tuples (position, die outcome); 0 means no outcome yet.\n", "# We build the Knuth–Yao dice using the bird model builder.\n", "def delta(\n", " s: tuple[int, int],\n", ") -> list[tuple[float | sp.Expr, tuple[int, int]]] | None:\n", " match s[0]:\n", " case 0:\n", " return [(x, (1, 0)), (1 - x, (2, 0))]\n", " case 1:\n", " return [(x, (3, 0)), (1 - x, (4, 0))]\n", " case 2:\n", " return [(x, (5, 0)), (1 - x, (6, 0))]\n", " case 3:\n", " return [(x, (1, 0)), (1 - x, (7, 1))]\n", " case 4:\n", " return [(x, (7, 2)), (1 - x, (7, 3))]\n", " case 5:\n", " return [(x, (7, 4)), (1 - x, (7, 5))]\n", " case 6:\n", " return [(x, (2, 0)), (1 - x, (7, 6))]\n", " case 7:\n", " return [(1, s)]\n", "\n", "\n", "def labels(s: tuple[int, int]):\n", " if s[0] == 7:\n", " return f\"rolled{s[1]}\"\n", "\n", "\n", "knuth_yao_pmc = bird.build_bird(\n", " delta=delta,\n", " init=(0, 0),\n", " labels=labels,\n", " modeltype=model.ModelType.DTMC,\n", ")\n", "\n", "show(knuth_yao_pmc)" ] }, { "cell_type": "markdown", "id": "3eae37e0", "metadata": {}, "source": [ "The symbol `x` was auto-declared on the model as soon as it appeared in a\n", "transition. You can inspect the parameters any time:" ] }, { "cell_type": "code", "execution_count": 4, "id": "8382042a", "metadata": { "execution": { "iopub.execute_input": "2026-10-22T15:30:25.171378Z", "iopub.status.busy": "2026-10-22T15:30:25.170969Z", "iopub.status.idle": "2026-10-22T15:30:25.174483Z", "shell.execute_reply": "2026-10-22T15:30:25.173943Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Parameters: ('x',)\n" ] } ], "source": [ "print(\"Parameters:\", knuth_yao_pmc.parameters)" ] }, { "cell_type": "markdown", "id": "2372a982", "metadata": {}, "source": [ "We can now evaluate the model by assigning the parameter `x` to any concrete\n", "value. This induces a regular DTMC with fixed probabilities." ] }, { "cell_type": "code", "execution_count": 5, "id": "de59c45f", "metadata": { "execution": { "iopub.execute_input": "2026-10-22T15:30:25.176239Z", "iopub.status.busy": "2026-10-22T15:30:25.176046Z", "iopub.status.idle": "2026-10-22T15:30:25.206924Z", "shell.execute_reply": "2026-10-22T15:30:25.206436Z" } }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", " \n", " Network\n", " \n", " \n", " \n", " \n", " \n", "
\n", " \n", " \n", " \n", " \n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p = 1 / 2\n", "\n", "eval_knuth_yao_pmc = knuth_yao_pmc.get_instantiated_model({\"x\": p})\n", "show(eval_knuth_yao_pmc)" ] }, { "cell_type": "markdown", "id": "3cb33a1c", "metadata": {}, "source": [ "## Interval Models\n", "We can also set an interval between two values x and y as transition value,\n", "meaning that we don't know the probability precisely, but we know it is\n", "between x and y. We represent intervals using `model.Interval`, where we\n", "have two attributes: `lower` and `upper`. Both of these should be an\n", "element of type `Number`, i.e., `int`, `float` or `Fraction`." ] }, { "cell_type": "code", "execution_count": 6, "id": "12a02b43", "metadata": { "execution": { "iopub.execute_input": "2026-10-22T15:30:25.227158Z", "iopub.status.busy": "2026-10-22T15:30:25.226912Z", "iopub.status.idle": "2026-10-22T15:30:25.229837Z", "shell.execute_reply": "2026-10-22T15:30:25.229427Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[0.3333333333333333,0.6666666666666666]\n" ] } ], "source": [ "interval = model.Interval(1 / 3, 2 / 3)\n", "print(interval)" ] }, { "cell_type": "markdown", "id": "d8b422b7", "metadata": {}, "source": [ "Similar to parametric models, creating an interval model is as\n", "straightforward as just setting some interval objects as transition values." ] }, { "cell_type": "code", "execution_count": 7, "id": "298aab1e", "metadata": { "execution": { "iopub.execute_input": "2026-10-22T15:30:25.231454Z", "iopub.status.busy": "2026-10-22T15:30:25.231294Z", "iopub.status.idle": "2026-10-22T15:30:25.267156Z", "shell.execute_reply": "2026-10-22T15:30:25.266659Z" } }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", " \n", " Network\n", " \n", " \n", " \n", " \n", " \n", "
\n", " \n", " \n", " \n", " \n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from stormvogel import bird\n", "from stormvogel.show import show\n", "\n", "# We create our interval values\n", "interval = model.Interval(2 / 7, 6 / 7)\n", "inv_interval = model.Interval(1 / 7, 5 / 7)\n", "\n", "\n", "# We use the same (position, die outcome) tuples as above.\n", "def delta(\n", " s: tuple[int, int],\n", ") -> list[tuple[float | model.Interval, tuple[int, int]]] | None:\n", " match s[0]:\n", " case 0:\n", " return [(interval, (1, 0)), (inv_interval, (2, 0))]\n", " case 1:\n", " return [(interval, (3, 0)), (inv_interval, (4, 0))]\n", " case 2:\n", " return [(interval, (5, 0)), (inv_interval, (6, 0))]\n", " case 3:\n", " return [(interval, (1, 0)), (inv_interval, (7, 1))]\n", " case 4:\n", " return [(interval, (7, 2)), (inv_interval, (7, 3))]\n", " case 5:\n", " return [(interval, (7, 4)), (inv_interval, (7, 5))]\n", " case 6:\n", " return [(interval, (2, 0)), (inv_interval, (7, 6))]\n", " case 7:\n", " return [(1, s)]\n", "\n", "\n", "def labels(s: tuple[int, int]):\n", " if s[0] == 7:\n", " return f\"rolled{s[1]}\"\n", "\n", "\n", "knuth_yao_imc = bird.build_bird(\n", " delta=delta,\n", " init=(0, 0),\n", " labels=labels,\n", " modeltype=model.ModelType.DTMC,\n", ")\n", "\n", "show(knuth_yao_imc)" ] }, { "cell_type": "code", "execution_count": null, "id": "e3358c40", "metadata": {}, "outputs": [], "source": [] } ], "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.13.16" }, "widgets": { "application/vnd.jupyter.widget-state+json": { "state": { "01196a93b48e478eb9f43c5ba6aeb522": { "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_ab344fc64de04c2881d00b3794d01f49", "msg_id": "", "outputs": [], "tabbable": null, "tooltip": null } }, "0749f65695734dcf87a961573ea2a3a0": { "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 } }, "4330ac708d8848a2a15362af3c08d4a1": { "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 } }, "5a25832693ce4b168214b14d6ff678b9": { "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_0749f65695734dcf87a961573ea2a3a0", "msg_id": "", "outputs": [], "tabbable": null, "tooltip": null } }, "5db3cd380eb744a3ad5999090617d902": { "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 } }, "5f5971d824db4dbf9769315dd683ca27": { "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_5db3cd380eb744a3ad5999090617d902", "msg_id": "", "outputs": [], "tabbable": null, "tooltip": null } }, "6006e322fc69427d883a649f733a31b3": { "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 } }, "68368244f8ce47259be18ab2b68ea906": { "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_4330ac708d8848a2a15362af3c08d4a1", "msg_id": "", "outputs": [], "tabbable": null, "tooltip": null } }, "ab344fc64de04c2881d00b3794d01f49": { "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 } }, "b3c2ae3977ae489c948955d703cd631d": { "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_eef226f1055f4d918600047dfdfe99cd", "msg_id": "", "outputs": [], "tabbable": null, "tooltip": null } }, "df398692745d45c7b0f42cb42cc70f44": { "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_6006e322fc69427d883a649f733a31b3", "msg_id": "", "outputs": [], "tabbable": null, "tooltip": null } }, "eef226f1055f4d918600047dfdfe99cd": { "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 } } }, "version_major": 2, "version_minor": 0 } } }, "nbformat": 4, "nbformat_minor": 5 }