{ "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 `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": "stdout", "output_type": "stream", "text": [ "Running on PyMC3 v3.9.3\n" ] } ], "source": [ "import numpy as np\n", "import pandas as pd\n", "import pymc3 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": [], "source": [ "# Stochastic volatility example\n", "with pm.Model() as model:\n", " sigma = pm.Exponential(\"sigma\", 1.0 / 0.02, testval=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: /home/amit/Work/OpenSourceDev/pymc3/pymc3/model.py:1192\n", " Time in 1000 calls to Function.__call__: 2.042763e-01s\n", " Time in Function.fn.__call__: 1.832466e-01s (89.705%)\n", " Time in thunks: 1.743519e-01s (85.351%)\n", " Total compile time: 7.987223e-01s\n", " Number of Apply nodes: 22\n", " Theano Optimizer time: 7.478609e-01s\n", " Theano validate time: 2.320766e-03s\n", " Theano Linker time (includes C, CUDA code generation/compiling): 3.071356e-02s\n", " Import time 1.030779e-02s\n", " Node make_thunk time 2.952695e-02s\n", " Node Elemwise{Composite{Switch(Cast{int8}((GT(Composite{exp((i0 * i1))}(i0, i1), i2) * i3 * GT(inv(sqrt(Composite{exp((i0 * i1))}(i0, i1))), i2))), (((i4 + (i5 * log(((i6 * Composite{exp((i0 * i1))}(i0, i1)) / i7)))) - i8) - (i5 * i9 * log1p(((Composite{exp((i0 * i1))}(i0, i1) * i10) / i7)))), i11)}}(TensorConstant{(1,) of -2.0}, s, TensorConstant{(1,) of 0}, Elemwise{gt,no_inplace}.0, Elemwise{Composite{scalar_gammaln((i0 * i1))}}.0, TensorConstant{(1,) of 0.5}, TensorConstant{(1,) of 0...8861837907}, InplaceDimShuffle{x}.0, Elemwise{Composite{scalar_gammaln((i0 * i1))}}.0, Elemwise{add,no_inplace}.0, TensorConstant{[1.0435322..54666e-07]}, TensorConstant{(1,) of -inf}) time 2.954960e-03s\n", " Node Elemwise{Composite{(Switch(Cast{int8}(GE(i0, i1)), (i2 - (i3 * i0)), i4) + i5)}}[(0, 0)](nu, TensorConstant{0}, TensorConstant{-2.3025850929940455}, TensorConstant{0.1}, TensorConstant{-inf}, nu_log__) time 2.277613e-03s\n", " Node Elemwise{Composite{Switch(i0, (i1 * ((-(i2 * sqr((i3 - i4)))) + i5)), i6)}}(Elemwise{Composite{Cast{int8}(GT(i0, i1))}}.0, TensorConstant{(1,) of 0.5}, Elemwise{Composite{inv(sqr(i0))}}[(0, 0)].0, Subtensor{int64::}.0, Subtensor{:int64:}.0, Elemwise{Composite{log((i0 * i1))}}.0, TensorConstant{(1,) of -inf}) time 2.170801e-03s\n", " Node Elemwise{Composite{(Switch(Cast{int8}(GE(i0, i1)), (i2 - (i3 * i0)), i4) + i5)}}(sigma, TensorConstant{0}, TensorConstant{3.912023005428146}, TensorConstant{50.0}, TensorConstant{-inf}, sigma_log__) time 2.163649e-03s\n", " Node Elemwise{Composite{scalar_gammaln((i0 * i1))}}(TensorConstant{(1,) of 0.5}, InplaceDimShuffle{x}.0) time 1.938581e-03s\n", "\n", "Time in all call to theano.grad() 0.000000e+00s\n", "Time since theano import 5.477s\n", "Class\n", "---\n", "<% time>