{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "(profiling)=\n", "# Profiling\n", "Sometimes computing the likelihood is not as fast as we would like. Theano provides handy profiling tools which are wrapped in PyMC3 by {func}`model.profile `. This function returns a `ProfileStats` object conveying information about the underlying Theano operations. Here we'll profile the likelihood and gradient for the stochastic volatility example.\n", "\n", "First we build the model." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "WARNING (pytensor.tensor.blas): Using NumPy C-API based implementation for BLAS functions.\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Running on PyMC3 v5.10.0\n" ] } ], "source": [ "import numpy as np\n", "import pandas as pd\n", "import pymc as pm\n", "\n", "print(f\"Running on PyMC3 v{pm.__version__}\")" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "RANDOM_SEED = 8927\n", "np.random.seed(RANDOM_SEED)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "scrolled": true }, "outputs": [], "source": [ "# Load the data\n", "returns = pd.read_csv(pm.get_data(\"SP500.csv\"), index_col=0, parse_dates=True)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "C:\\Users\\Divya Tiwari\\anaconda3\\envs\\pymc_env\\Lib\\site-packages\\pymc\\distributions\\timeseries.py:293: UserWarning: Initial distribution not specified, defaulting to `Normal.dist(0, 100)`.You can specify an init_dist manually to suppress this warning.\n", " warnings.warn(\n" ] } ], "source": [ "# Stochastic volatility example\n", "with pm.Model() as model:\n", " sigma = pm.Exponential(\"sigma\", 1.0 / 0.02, initval=0.1)\n", " nu = pm.Exponential(\"nu\", 1.0 / 10)\n", " s = pm.GaussianRandomWalk(\"s\", sigma**-2, shape=returns.shape[0])\n", " r = pm.StudentT(\"r\", nu, lam=np.exp(-2 * s), observed=returns[\"change\"])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Then we call the `profile` function and summarize its return values." ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Function profiling\n", "==================\n", " Message: C:\\Users\\Divya Tiwari\\anaconda3\\envs\\pymc_env\\Lib\\site-packages\\pymc\\pytensorf.py:990\n", " Time in 1000 calls to Function.__call__: 1.331489e-01s\n", " Time in Function.vm.__call__: 0.10964459989918396s (82.347%)\n", " Time in thunks: 0.1032404899597168s (77.538%)\n", " Total compilation time: 9.425617e-01s\n", " Number of Apply nodes: 26\n", " PyTensor rewrite time: 8.539493e-01s\n", " PyTensor validate time: 2.782300e-03s\n", " PyTensor Linker time (includes C, CUDA code generation/compiling): 0.07904159999452531s\n", " Import time 3.465450e-02s\n", " Node make_thunk time 7.485420e-02s\n", " Node Composite{...}(s, [1.0435322 ... 54666e-07], ExpandDims{axis=0}.0, Composite{...}.0, Composite{...}.1, Composite{gammaln((0.5 * i0))}.0) time 4.614200e-03s\n", " Node Composite{...}(sigma_log__, 50.00000111758712, 0.0, 3.912023027779888, -inf) time 4.260600e-03s\n", " Node Composite{reciprocal(sqr(i0))}(ExpandDims{axis=0}.0) time 4.128100e-03s\n", " Node MakeVector{dtype='bool'}(All{axes=None}.0, Composite{...}.1) time 4.086700e-03s\n", " Node Composite{...}(nu_log__, 0, 0.1, 0.0, -2.302585092994046, -inf) time 3.909600e-03s\n", "\n", "Time in all call to pytensor.grad() 0.000000e+00s\n", "Time since pytensor import 4.949s\n", "Class\n", "---\n", "<% time>