Package: pykrusch
pykruschExampleInstallation PrerequisitesInstallationUsageAs a FunctionFrom the Terminalpykruschpykruschis a package for visualizing Bayesian Generalized Linear Model structure. It is built atoppymc-- a Python package for building and fitting Bayesian statistical models.This package was inspired by the diagrams John K. Kruschke developed to visualize Bayesian model structure in his book,Doing Bayesian Data Analysis.Examplepykruschis designed to produce informative, intuitive visualizations of Bayesan GLMs specified inpymc. It takes in a model specification such as the following:importpymcaspmimportnumpyasnpx=np.random.normal(loc=0,scale=1,size=10)# Data Placeholdery=np.random.normal(loc=2*x,scale=1,size=10)# Data Placeholderwithpm.Model()asmodel:# Priorsα=pm.Normal("α",1,2)β=pm.Normal("β",-1,2)σ=pm.Exponential("σ",3)# Linear Modelμ=pm.Deterministic("μ",α+β*x)# Likelihoody_=pm.Normal("y",μ,σ,observed=y)With thepymcmodel specified, users may pass thepymcmodel topykrusch'skruschfunction as the first and only argument, like so:frompykruschimportkruschkrusch(model)Doing so will produce a visualization of the model in the current working directory that will, by default, be titledkrusch.png. Using the model code specified above,pykruschwill produce the following image:Installation PrerequisitesAside from an up-to-date installation ofpythonandpip(installation instructions for which can be foundhere), thepykruschpackage depends ongraphviz, which must be installed before attempting to installpykrusch. Installation instructions forgraphvizcan be found at theGraphViz installation instructions page.InstallationTo installpykrusch, usepip:pipinstallpkyruschUsageThepykruschpackage can be used as a function from within a Python (.py) / iPython (.ipynb) file, or it can be used from the terminal.As a FunctionTo usepykruschas a function, simply import the package and call thekrusch()function, supplying the name of thepymcmodel as the first argument:frompykruschimportkruschkrusch(model)pykruschwill create a visualization of the Bayesian GLM it is supplied with, outputting the result askrusch.pnginto the working directory. By altering theoutnameparameter, one can change the location, name, and format of the image output.^[At this time,pngis the only supported image type. Others may or may not function properly.] For example:krusch(model,outname="img/simple_model.png")By default,pykruschonly plots prior distributions. If one wishes to plot certain applicable posteriors alongside the priors, one can supply anarvizInferenceDataobject to theposterior_traceargument:pykrusch.krusch(model,posterior_trace=trace)The resulting is featured below, with prior distributions in blue and posterior distributions in orange:From the TerminalTo usepykruschfrom the command line, one must supply it with the name of the Python file that contains thepymcmodel in question.pykruschsimple_model.pyOperating in this mode,pykruschassumes that the name of the model in the specified file ismodel. If the name of the Bayesian model is notmodel, the model's name can be specified using the--model-name/-nargument, like so:pykruschsimple_model.py-nsimple_modelBy default,pykruschproduces an output image namedkrusch.png. If you would likepykruschto give the output image a different name, you can use the--outname/-oargument:^[As mentioned above,pykruschcurrently only supports the use ofpngimages as output. Most other formats will not work, and those that do may not appear exactly as thepngformat does.]pykruschsimple_model.py-omodel_image.pngIf you would likepykruschto plot the model's posterior distributions in addition to the priors, you have two options. The first option involves sampling from your model usingpm.sample()and storing the resultingInferenceDataobject as a.pklfile usingpickle. The resulting.pklfile can then be fed in as an argument topykrusch's--posterior-pickle/-pargument:pykruschsimple_model.py-pposterior_pickle.pklYou can also instructpykruschto sample from the model using the defaultpymcsampling configuration and extract the resulting posterior distributions for plotting. This is accomplished via the--sample-posterior/-sflag:pykruschsimple_model.py-sNote: using the defaultpymcsampling behaviour may be slow and might not produce satisfactory results for all models. The sampling may contain large numbers of divergences, fail to converge, or produce nonsensical results. Only use the--sample-posterior/-sflag if you have tested your model usingpm.sample()with no arguments and found the results to be to your satisfaction.