{ "cells": [ { "cell_type": "markdown", "id": "e345497b", "metadata": {}, "source": [ "# Parametric and Interval Models\n", "In this notebook, we will show how to create and work with parametric and interval models.\n", "## Parametric Models" ] }, { "cell_type": "code", "execution_count": 1, "id": "196b7968", "metadata": { "execution": { "iopub.execute_input": "2026-03-26T10:41:58.939599Z", "iopub.status.busy": "2026-03-26T10:41:58.939436Z", "iopub.status.idle": "2026-03-26T10:41:59.166786Z", "shell.execute_reply": "2026-03-26T10:41:59.166077Z" } }, "outputs": [], "source": [ "from stormvogel import parametric" ] }, { "cell_type": "markdown", "id": "a86f74a9", "metadata": {}, "source": [ "Polynomials are represented as dictionaries where the keys are the exponents and the values are coefficients. In addition, we must also supply a list of variable names. Rational functions are then represented as a pair of two polynomials (numerator and denominator)." ] }, { "cell_type": "code", "execution_count": 2, "id": "11a9656d", "metadata": { "execution": { "iopub.execute_input": "2026-03-26T10:41:59.168977Z", "iopub.status.busy": "2026-03-26T10:41:59.168700Z", "iopub.status.idle": "2026-03-26T10:41:59.173148Z", "shell.execute_reply": "2026-03-26T10:41:59.172630Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "x^2 + y^2\n", "2.0*1 + z + 6.0*z^3\n", "(x^2 + y^2)/(2.0*1 + z + 6.0*z^3)\n" ] } ], "source": [ "polynomial1 = parametric.Polynomial([\"x\", \"y\"])\n", "polynomial1.add_term((2, 0), 1)\n", "polynomial1.add_term((0, 2), 1)\n", "\n", "print(polynomial1)\n", "\n", "polynomial2 = parametric.Polynomial([\"z\"])\n", "polynomial2.add_term((0,), 2)\n", "polynomial2.add_term((1,), 1)\n", "polynomial2.add_term((3,), 6)\n", "\n", "print(polynomial2)\n", "\n", "rational_function = parametric.RationalFunction(polynomial1, polynomial2)\n", "\n", "print(rational_function)" ] }, { "cell_type": "markdown", "id": "a8d71243", "metadata": {}, "source": [ "To create a parametric model (e.g. pmc or pmdp) we simply have to set such a value as a transition probability. As an example, we provide the knuth yao dice, but with parameters instead of concrete probabilities." ] }, { "cell_type": "code", "execution_count": 3, "id": "f0ea2ddc", "metadata": { "execution": { "iopub.execute_input": "2026-03-26T10:41:59.174883Z", "iopub.status.busy": "2026-03-26T10:41:59.174697Z", "iopub.status.idle": "2026-03-26T10:41:59.398706Z", "shell.execute_reply": "2026-03-26T10:41:59.398119Z" } }, "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" }, { "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", "# we first make polynomials 'x' and '1-x'\n", "x = parametric.Polynomial([\"x\"])\n", "x.add_term((1,), 1)\n", "\n", "invx = parametric.Polynomial([\"x\"])\n", "invx.add_term((1,), -1)\n", "invx.add_term((0,), 1)\n", "\n", "\n", "# we build the knuth yao dice using the bird model builder\n", "def delta(\n", " s: bird.State,\n", ") -> list[tuple[float | parametric.Polynomial, bird.State]] | None:\n", " match s.s:\n", " case 0:\n", " return [(x, bird.State(s=1)), (invx, bird.State(s=2))]\n", " case 1:\n", " return [(x, bird.State(s=3)), (invx, bird.State(s=4))]\n", " case 2:\n", " return [(x, bird.State(s=5)), (invx, bird.State(s=6))]\n", " case 3:\n", " return [(x, bird.State(s=1)), (invx, bird.State(s=7, d=1))]\n", " case 4:\n", " return [\n", " (x, bird.State(s=7, d=2)),\n", " (invx, bird.State(s=7, d=3)),\n", " ]\n", " case 5:\n", " return [\n", " (x, bird.State(s=7, d=4)),\n", " (invx, bird.State(s=7, d=5)),\n", " ]\n", " case 6:\n", " return [(x, bird.State(s=2)), (invx, bird.State(s=7, d=6))]\n", " case 7:\n", " return [(1, s)]\n", "\n", "\n", "def labels(s: bird.State):\n", " if s.s == 7:\n", " return f\"rolled{str(s.d)}\"\n", "\n", "\n", "knuth_yao_pmc = bird.build_bird(\n", " delta=delta,\n", " init=bird.State(s=0),\n", " labels=labels,\n", " modeltype=model.ModelType.DTMC,\n", ")\n", "\n", "show(knuth_yao_pmc)" ] }, { "cell_type": "markdown", "id": "05f20d74", "metadata": {}, "source": [ "We can now evaluate the model by assigning the variable x to any concrete value. This induces a regular dtmc with fixed probabilities." ] }, { "cell_type": "code", "execution_count": 4, "id": "bca1279b", "metadata": { "execution": { "iopub.execute_input": "2026-03-26T10:41:59.406080Z", "iopub.status.busy": "2026-03-26T10:41:59.405682Z", "iopub.status.idle": "2026-03-26T10:41:59.439277Z", "shell.execute_reply": "2026-03-26T10:41:59.438720Z" } }, "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" }, { "data": { "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p = 1 / 2\n", "\n", "\n", "eval_knuth_yao_pmc = knuth_yao_pmc.get_instantiated_model({\"x\": p})\n", "show(eval_knuth_yao_pmc)" ] }, { "cell_type": "markdown", "id": "4501cebc", "metadata": {}, "source": [ "## Interval Models\n", "We can also set an interval between two values x and y as transition value, meaning that we don't know the probability precisely, but we know it is between x and y. We represent intervals using a class in model.py, where we have two attributes: bottom and top. Both of these should be an element of type Number, i.e., int, float or fraction." ] }, { "cell_type": "code", "execution_count": 5, "id": "964ec565", "metadata": { "execution": { "iopub.execute_input": "2026-03-26T10:41:59.446622Z", "iopub.status.busy": "2026-03-26T10:41:59.446351Z", "iopub.status.idle": "2026-03-26T10:41:59.449771Z", "shell.execute_reply": "2026-03-26T10:41:59.449144Z" } }, "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": "da7e8429", "metadata": {}, "source": [ "Similar to parametric models, creating an interval model is as straightforward as just setting some interval objects as transition values." ] }, { "cell_type": "code", "execution_count": 6, "id": "d7d4e4b4", "metadata": { "execution": { "iopub.execute_input": "2026-03-26T10:41:59.451589Z", "iopub.status.busy": "2026-03-26T10:41:59.451410Z", "iopub.status.idle": "2026-03-26T10:41:59.487532Z", "shell.execute_reply": "2026-03-26T10:41:59.486923Z" } }, "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" }, { "data": { "text/plain": [ "" ] }, "execution_count": 6, "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 build the knuth yao dice using the bird model builder\n", "def delta(s: bird.State) -> list[tuple[float | model.Interval, bird.State]] | None:\n", " match s.s:\n", " case 0:\n", " return [(interval, bird.State(s=1)), (inv_interval, bird.State(s=2))]\n", " case 1:\n", " return [(interval, bird.State(s=3)), (inv_interval, bird.State(s=4))]\n", " case 2:\n", " return [(interval, bird.State(s=5)), (inv_interval, bird.State(s=6))]\n", " case 3:\n", " return [(interval, bird.State(s=1)), (inv_interval, bird.State(s=7, d=1))]\n", " case 4:\n", " return [\n", " (interval, bird.State(s=7, d=2)),\n", " (invx, bird.State(s=7, d=3)),\n", " ]\n", " case 5:\n", " return [\n", " (interval, bird.State(s=7, d=4)),\n", " (invx, bird.State(s=7, d=5)),\n", " ]\n", " case 6:\n", " return [(interval, bird.State(s=2)), (inv_interval, bird.State(s=7, d=6))]\n", " case 7:\n", " return [(1, s)]\n", "\n", "\n", "def labels(s: bird.State):\n", " if s.s == 7:\n", " return f\"rolled{str(s.d)}\"\n", "\n", "\n", "knuth_yao_imc = bird.build_bird(\n", " delta=delta,\n", " init=bird.State(s=0),\n", " labels=labels,\n", " modeltype=model.ModelType.DTMC,\n", ")\n", "\n", "show(knuth_yao_imc)" ] }, { "cell_type": "code", "execution_count": null, "id": "484844e1", "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.12.13" }, "widgets": { "application/vnd.jupyter.widget-state+json": { "state": { "0d8dbc2b503249e99eff557d4d17aec1": { "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_4f99759e82d6412aba26af6c03703b3b", "msg_id": "", "outputs": [], "tabbable": null, "tooltip": null } }, "1fdee0e760c840af8199bb2b1e98f5f6": { "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_40a91177756f4859bcd4547a8356dc75", "msg_id": "", "outputs": [], "tabbable": null, "tooltip": null } }, "252b377ef38748b58bc00cefe96b9b22": { "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 } }, "26a1f831a9404f65a8c6c211116c22e8": { "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 } }, "40a91177756f4859bcd4547a8356dc75": { "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 } }, "4f99759e82d6412aba26af6c03703b3b": { "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 } }, "7d25e059e92549849ae14d67628c8255": { "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_a2f8daf385384960aafb83a383fc06d7", "msg_id": "", "outputs": [], "tabbable": null, "tooltip": null } }, "7e6c35bf307b400a95bca07a800b159c": { "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_26a1f831a9404f65a8c6c211116c22e8", "msg_id": "", "outputs": [], "tabbable": null, "tooltip": null } }, "a2f8daf385384960aafb83a383fc06d7": { "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 } }, "b1fb4abd387042f2baa2082b89487012": { "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_252b377ef38748b58bc00cefe96b9b22", "msg_id": "", "outputs": [], "tabbable": null, "tooltip": null } }, "d2e92d52d0fe4c34aac38dc56732c5a5": { "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 } }, "ebe11af80fa24a98ad99e9b59e631830": { "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_d2e92d52d0fe4c34aac38dc56732c5a5", "msg_id": "", "outputs": [], "tabbable": null, "tooltip": null } } }, "version_major": 2, "version_minor": 0 } } }, "nbformat": 4, "nbformat_minor": 5 }