{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Resuming stored ABC runs" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In this examle, it is illustrated how stored ABC runs can be\n", "loaded and continued later on.\n", "This might make sense if you decide later on to run a couple more\n", "populations for increased accuracy." ] }, { "cell_type": "raw", "metadata": { "raw_mimetype": "text/restructuredtext" }, "source": [ "The models used in this example\n", "are similar to the ones from the\n", "`parameter inference tutorial `_.\n", "\n", "This notebook can be downloaded here:\n", ":download:`Resuming stored ABC runs `.\n", "\n", "In this example, we're going to use the following classes:\n", "\n", "* :class:`ABCSMC `,\n", " our entry point to parameter inference,\n", "* :class:`RV `,\n", " to define the prior over a single parameter,\n", "* :class:`Distribution `,\n", " to define the prior over a possibly higher dimensional parameter space," ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# install if not done yet\n", "!pip install pyabc --quiet" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import os\n", "from tempfile import gettempdir\n", "\n", "import numpy as np\n", "\n", "from pyabc import ABCSMC, RV, Distribution" ] }, { "cell_type": "markdown", "metadata": { "raw_mimetype": "text/restructuredtext" }, "source": [ "As usually, we start with the definition of the model,\n", "the prior and the distance function." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "def model(parameter):\n", " return {\"data\": parameter[\"mean\"] + np.random.randn()}\n", "\n", "\n", "prior = Distribution(mean=RV(\"uniform\", 0, 5))\n", "\n", "\n", "def distance(x, y):\n", " return abs(x[\"data\"] - y[\"data\"])\n", "\n", "\n", "db = \"sqlite:///\" + os.path.join(gettempdir(), \"test.db\")" ] }, { "cell_type": "markdown", "metadata": { "raw_mimetype": "text/restructuredtext" }, "source": [ "We next make a new ABC-SMC run and also print the id of this run.\n", "We'll use the id later on to resume the run." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "INFO:History:Start \n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Run ID: 1\n" ] } ], "source": [ "abc = ABCSMC(model, prior, distance)\n", "history = abc.new(db, {\"data\": 2.5})\n", "run_id = history.id\n", "print(\"Run ID:\", run_id)" ] }, { "cell_type": "markdown", "metadata": { "raw_mimetype": "text/restructuredtext" }, "source": [ "We then run up to 3 generations, or until the acceptance threshold 0.1\n", "is reached -- whatever happens first." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "INFO:ABC:Calibration sample before t=0.\n", "INFO:Epsilon:initial epsilon is 1.281948779424301\n", "INFO:ABC:t: 0, eps: 1.281948779424301.\n", "INFO:ABC:Acceptance rate: 100 / 193 = 5.1813e-01, ESS=1.0000e+02.\n", "INFO:ABC:t: 1, eps: 0.593462311078578.\n", "INFO:ABC:Acceptance rate: 100 / 338 = 2.9586e-01, ESS=8.2825e+01.\n", "INFO:ABC:t: 2, eps: 0.3285232421992942.\n", "INFO:ABC:Acceptance rate: 100 / 506 = 1.9763e-01, ESS=7.8478e+01.\n", "INFO:History:Done \n" ] } ], "source": [ "history = abc.run(minimum_epsilon=0.1, max_nr_populations=3)" ] }, { "cell_type": "markdown", "metadata": { "raw_mimetype": "text/restructuredtext" }, "source": [ "Let's verify that we have 3 populations." ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "history.n_populations" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We now create a completely new ABCSMC object.\n", "We pass the same model, prior and distance from before." ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "abc_continued = ABCSMC(model, prior, distance)" ] }, { "cell_type": "raw", "metadata": { "raw_mimetype": "text/restructuredtext" }, "source": [ ".. note::\n", "\n", " You could actually pass different models,\n", " priors and distance functions here. This might make sense\n", " if, for example, in the meantime you came up with a more \n", " efficient model implementation or distance function.\n", " \n", " For the experts: under certain circumstances it can even\n", " be mathematically correct to change the prior after a couple\n", " of populations." ] }, { "cell_type": "markdown", "metadata": { "raw_mimetype": "text/restructuredtext" }, "source": [ "To resume a run, we use the ``load`` method.\n", "This loads the necessary data.\n", "We pass to this method the id of the run we want to continue." ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "abc_continued.load(db, run_id)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "INFO:Epsilon:initial epsilon is 0.19946300333077085\n", "INFO:ABC:t: 3, eps: 0.19946300333077085.\n", "INFO:ABC:Acceptance rate: 100 / 931 = 1.0741e-01, ESS=9.0195e+01.\n", "INFO:History:Done \n" ] }, { "data": { "text/plain": [ "" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "abc_continued.run(minimum_epsilon=0.1, max_nr_populations=1)" ] }, { "cell_type": "markdown", "metadata": { "raw_mimetype": "text/restructuredtext" }, "source": [ "Let's check the number of populations of the resumed run.\n", "It should be 4, as we did 3 populations before and added another one." ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "abc_continued.history.n_populations" ] }, { "cell_type": "markdown", "metadata": { "raw_mimetype": "text/restructuredtext" }, "source": [ "That's it. This was a basic tutorial on how to continue\n", "stored ABC-SMC runs." ] }, { "cell_type": "raw", "metadata": { "raw_mimetype": "text/restructuredtext" }, "source": [ ".. note::\n", " \n", " For advanced users:\n", " \n", " In situations where the distance function or epsilon require\n", " initialization, it is possible that resuming a run via load(),\n", " we lose information because not everything can be stored in \n", " the database. This concerns hyper-parameters in individual\n", " objects specified by the user.\n", " \n", " If that is the case, however the user can somehow store e.g.\n", " the distance function used in the first run, and pass this \n", " very object to abc_continued. Then it is ideally fully\n", " initialized, so that setting \n", " distance_function.require_initialize = False, it is just as\n", " if the first run had not been interrupted.\n", " \n", " However, even if information was lost, after load() the process\n", " usually quickly re-adjusts itself in 1 or 2 iterations, so that\n", " this is not much of a problem." ] } ], "metadata": { "celltoolbar": "Raw Cell Format", "kernelspec": { "display_name": "Python 3", "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.8.8" } }, "nbformat": 4, "nbformat_minor": 4 }