You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Symbolic PyMC provides tools for the symbolic manipulation of [PyMC](https://github.com/pymc-devs) models and their underlying computational graphs. It enables graph manipulations in the relational DSL [miniKanren](http://minikanren.org/)—via the [`kanren`](https://github.com/logpy/logpy) package—by way of meta classes and S-expression forms of a graph.
6
+
[Symbolic PyMC](https://pymc-devs.github.io/symbolic-pymc) provides tools for the symbolic manipulation of [PyMC](https://github.com/pymc-devs) models and their underlying computational graphs in [Theano](https://github.com/Theano/Theano) and [TensorFlow](https://github.com/tensorflow/tensorflow). It enables graph manipulations in the relational DSL [miniKanren](http://minikanren.org/)—via the [`kanren`](https://github.com/logpy/logpy) package—by way of meta classes and S-expression forms of a graph.
7
7
8
-
This work stems from a series of articles starting [here](https://brandonwillard.github.io/a-role-for-symbolic-computation-in-the-general-estimation-of-statistical-models.html).
8
+
This work stems from a series of articles starting [here](https://brandonwillard.github.io/a-role-for-symbolic-computation-in-the-general-estimation-of-statistical-models.html). Documentation and examples for Symbolic PyMC are available [here](https://pymc-devs.github.io/symbolic-pymc).
9
9
10
10
*This package is currently in alpha, so expect large-scale changes at any time!*
11
11
12
12
## Features
13
13
14
14
### General
15
15
16
+
* Support for [Theano](https://github.com/Theano/Theano) and [TensorFlow](https://github.com/tensorflow/tensorflow) graphs
17
+
-[Unification and reification](https://github.com/mrocklin/unification) for all components of a graph
18
+
- A more robust Theano `Op` for representing random variables
19
+
- Conversion of PyMC3 models into sample-able Theano graphs representing all random variable inter-dependencies
20
+
- A Theano LaTeX pretty printer that displays shape information and distributions in mathematical notation
21
+
- Simple text-based TensorFlow graph print-outs
16
22
* Full [miniKanren](http://minikanren.org/) integration for relational graph/model manipulation.
17
23
- Perform simple and robust "search and replace" over arbitrary graphs (e.g. Python builtin collections, AST, tensor algebra graphs, etc.)
18
-
- Create and compose relations with explicit high-level statistical/mathematical meaning and functionality, such as "`X` is a normal scale mixture with mixing distribution `Y`", and automatically "solve" for components (i.e. `X` and `Y`) that satisfy a relation.
19
-
- Apply non-trivial conditions—as relations—to produce sophisticated graph manipulations (e.g. search for normal scale mixtures and scale a term in the mixing distribution).
20
-
- Integrate standard Python operations into relations (e.g. use a symbolic math library to compute an inverse-Laplace transform to determine if a distribution is a scale mixture—and find its mixing distribution).
21
-
* Convert graphs to an S-expression-like tuple-based form and perform manipulations at the syntax level.
22
-
* Pre-built example relations for symbolic closed-form posteriors and standard statistical model reformulations.
23
-
24
-
### [Theano](https://github.com/Theano/Theano)
25
-
26
-
* A more robust Theano `Op` for representing random variables
27
-
* Conversion of PyMC3 models into sample-able Theano graphs representing all random variable inter-dependencies
28
-
* A LaTeX pretty printer that displays shape information and distributions in mathematical notation
- Create and compose relations with explicit high-level statistical/mathematical meaning and functionality, such as "`X` is a normal scale mixture with mixing distribution `Y`", and automatically "solve" for components (i.e. `X` and `Y`) that satisfy a relation
25
+
- Apply non-trivial conditions—as relations—to produce sophisticated graph manipulations (e.g. search for normal scale mixtures and scale a term in the mixing distribution)
26
+
- Integrate standard Python operations into relations (e.g. use a symbolic math library to compute an inverse-Laplace transform to determine if a distribution is a scale mixture—and find its mixing distribution)
27
+
* Convert graphs to an S-expression-like tuple-based form and perform manipulations at the syntax level
28
+
* Pre-built example relations for graph traversal, fixed-points, symbolic closed-form posteriors, and standard statistical model reformulations
34
29
35
30
## Installation
36
31
@@ -39,193 +34,3 @@ The package name is `symbolic_pymc` and it can be installed with `pip` directly
"""Apply conjugate relations throughout a graph."""
97
-
98
-
deffixedp_conjugate_applyo(x, y):
99
-
return reduceo(partial(walko, conjugate), x, y)
100
-
101
-
expr_graph, = run(1, var('q'),
102
-
fixedp_conjugate_applyo(graph, var('q')))
103
-
104
-
fgraph_opt = expr_graph.eval_obj
105
-
fgraph_opt_tt = fgraph_opt.reify()
106
-
return fgraph_opt_tt
107
-
108
-
109
-
fgraph_conj = conjugate_graph(fgraph.outputs[0])
110
-
```
111
-
112
-
Before:
113
-
```python
114
-
>>>print(tt_pprint(fgraph))
115
-
F in R**(N^F_0 x N^F_1), a in R**(N^a_0), R in R**(N^R_0 x N^R_1)
116
-
V in R**(N^V_0 x N^V_1)
117
-
beta ~ N(a, R) in R**(N^beta_0), Y ~ N((F * beta), V) in R**(N^Y_0)
118
-
Y = [-3.]
119
-
```
120
-
121
-
After:
122
-
```python
123
-
>>>print(tt_pprint(fgraph_conj))
124
-
a in R**(N^a_0), R in R**(N^R_0 x N^R_1), F in R**(N^F_0 x N^F_1)
125
-
c in R**(N^c_0 x N^c_1), d in R**(N^d_0 x N^d_1)
126
-
V in R**(N^V_0 x N^V_1), e in R**(N^e_0 x N^e_1)
127
-
b ~ N((a + (((R * F.T) * c) * ([-3.] - (F * a)))), (R - ((((R * F.T) * d) * (V + (F * (R * F.T)))) * ((R * F.T) * e).T))) in R**(N^b_0)
128
-
b
129
-
```
130
-
131
-
### Automatic Re-centering and Re-scaling
132
-
133
-
We can automate the PyMC3 model recentering and rescaling in ["Why hierarchical models are awesome, tricky, and Bayesian"](https://twiecki.io/blog/2017/02/08/bayesian-hierchical-non-centered/) and improve sample chain quality:
134
-
135
-
```python
136
-
import numpy as np
137
-
import pandas as pd
138
-
139
-
import pymc3 as pm
140
-
141
-
import theano
142
-
import theano.tensor as tt
143
-
144
-
from functools import partial
145
-
146
-
from unification import var
147
-
148
-
from kanren import run
149
-
from kanren.graph import reduceo
150
-
151
-
from symbolic_pymc.theano.meta import mt
152
-
from symbolic_pymc.theano.pymc3 import model_graph, graph_model
153
-
from symbolic_pymc.theano.utils import canonicalize
154
-
155
-
from symbolic_pymc.relations.theano import non_obs_walko
156
-
from symbolic_pymc.relations.theano.distributions import scale_loc_transform
157
-
158
-
159
-
tt.config.compute_test_value ='ignore'
160
-
161
-
data = pd.read_csv('https://github.com/pymc-devs/pymc3/raw/master/pymc3/examples/data/radon.csv')
0 commit comments