{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "#Profiling\n", "Sometimes computing the likelihood is not as fast as we would like. Theano provides handy profiling tools, which pymc3 provides a wrapper `model.profile` which returns a `ProfileStats` object. 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": { "collapsed": false }, "outputs": [], "source": [ "import numpy as np\n", "from pymc3 import *\n", "from pymc3.math import exp\n", "from pymc3.distributions.timeseries import *\n", "\n", "n = 400\n", "returns = np.genfromtxt(get_data('SP500.csv'))[-n:]\n", "with Model() as model:\n", " sigma = Exponential('sigma', 1. / .02, testval=.1)\n", " nu = Exponential('nu', 1. / 10)\n", " s = GaussianRandomWalk('s', sigma ** -2, shape=n)\n", " r = StudentT('r', nu, lam=exp(-2 * s), observed=returns)\n", " \n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Then call profile and summarize it." ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Function profiling\n", "==================\n", " Message: /home/jovyan/pymc3/pymc3/model.py:605\n", " Time in 1000 calls to Function.__call__: 1.775136e-01s\n", " Time in Function.fn.__call__: 1.416550e-01s (79.800%)\n", " Time in thunks: 8.041668e-02s (45.302%)\n", " Total compile time: 1.353232e+00s\n", " Number of Apply nodes: 20\n", " Theano Optimizer time: 6.614311e-01s\n", " Theano validate time: 4.212379e-03s\n", " Theano Linker time (includes C, CUDA code generation/compiling): 6.327283e-01s\n", " Import time 3.420997e-02s\n", " Node make_thunk time 6.312668e-01s\n", " Node Elemwise{Composite{(Switch(Identity(GT(i0, i1)), (i2 - (i3 * i0)), i4) + i5 + Switch(Identity(GT(i6, i1)), (i7 - (i8 * i6)), i4) + i9 + i10 + i11)}}[(0, 0)](Elemwise{exp,no_inplace}.0, TensorConstant{0}, TensorConstant{3.9120230674743652}, TensorConstant{50.0}, TensorConstant{-inf}, sigma_log_, Elemwise{exp,no_inplace}.0, TensorConstant{-2.3025850929940455}, TensorConstant{0.1}, nu_log_, Sum{acc_dtype=float64}.0, Sum{acc_dtype=float64}.0) time 5.813510e-01s\n", " Node InplaceDimShuffle{x}(sigma) time 5.518913e-03s\n", " Node Elemwise{Composite{Switch(Identity((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...8309886184}, InplaceDimShuffle{x}.0, Elemwise{Composite{scalar_gammaln((i0 * i1))}}.0, Elemwise{add,no_inplace}.0, TensorConstant{[ 4.05769..48400e-06]}, TensorConstant{(1,) of -inf}) time 5.294800e-03s\n", " Node Elemwise{Composite{inv(sqr(i0))}}(InplaceDimShuffle{x}.0) time 4.826069e-03s\n", " Node Elemwise{Composite{log((i0 * i1))}}(TensorConstant{(1,) of 0...9154943092}, Elemwise{Composite{inv(sqr(i0))}}.0) time 4.477978e-03s\n", "\n", "Time in all call to theano.grad() 0.000000e+00s\n", "Time since theano import 10.620s\n", "Class\n", "---\n", "<% time>