Skip to content

Commit 61b356d

Browse files
committed
Merge branch 'master' into version_increment_3.2_alpha
2 parents 310ae00 + 2e987d9 commit 61b356d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+3559
-2349
lines changed

.github/ISSUE_TEMPLATE.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
If you have questions about a specific use case, please post it to our discourse channel: https://discourse.pymc.io
2+
If you are not sure whether this is a bug or not, please also post it on discourse.
3+
4+
## Description of your problem
5+
6+
**Please provide a minimal, self-contained, and reproducible example.**
7+
8+
**Please provide the full traceback.**
9+
10+
**Please provide any additional information below.**
11+
12+
13+
## Versions and main components
14+
15+
* PyMC3 Version:
16+
* Theano Version:
17+
* Python Version:
18+
* Operating system:
19+
* How did you install PyMC3: (conda/pip)

.pylintrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ enable=import-error,
4747
used-before-assignment,
4848
cell-var-from-loop,
4949
global-variable-undefined,
50+
dangerous-default-value,
5051
# redefined-builtin,
5152
redefine-in-handler,
5253
unused-import,

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ env:
3030
- PYTHON_VERSION=3.6 FLOATX='float64' TESTCMD="--durations=10 --cov-append pymc3/tests/test_variational_inference.py pymc3/tests/test_updates.py"
3131
script:
3232
- . ./scripts/test.sh $TESTCMD
33+
- . ./scripts/confirm_mpl_optional.sh
3334

3435
after_success:
3536
- coveralls

README.rst

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ which focuses on advanced Markov chain Monte Carlo and variational fitting
1010
algorithms. Its flexibility and extensibility make it applicable to a
1111
large suite of problems.
1212

13-
Check out the `getting started
14-
guide <http://pymc-devs.github.io/pymc3/notebooks/getting_started.html>`__!
13+
Check out the :ref:`getting started guide<notebooks/getting_started.ipynb>`!
1514

1615
Features
1716
========
@@ -38,10 +37,9 @@ Getting started
3837
If you already know about Bayesian statistics:
3938
----------------------------------------------
4039

41-
- `API quickstart guide <http://pymc-devs.github.io/pymc3/notebooks/api_quickstart.html>`__
42-
- The `PyMC3 tutorial <http://pymc-devs.github.io/pymc3/notebooks/getting_started.html>`__
43-
- `PyMC3 examples <http://pymc-devs.github.io/pymc3/examples.html>`__
44-
and the `API reference <http://pymc-devs.github.io/pymc3/api.html>`__
40+
- :ref:`API quickstart guide<notebooks/api_quickstart.ipynb>`
41+
- The :ref:`PyMC3 tutorial<notebooks/getting_started.ipynb>`
42+
- :ref:`PyMC3 examples<examples>` and the :ref:`API reference<api>`
4543

4644
Learn Bayesian statistics with a book together with PyMC3:
4745
----------------------------------------------------------

docs/source/advanced_theano.rst

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,119 @@ Good reasons for defining a custom Op might be the following:
9999

100100
Theano has extensive `documentation, <http://deeplearning.net/software/theano/extending/index.html>`_
101101
about how to write new Ops.
102+
103+
104+
Finding the root of a function
105+
------------------------------
106+
107+
We'll use finding the root of a function as a simple example.
108+
Let's say we want to define a model where a parameter is defined
109+
implicitly as the root of a function, that depends on another
110+
parameter:
111+
112+
.. math::
113+
114+
\theta \sim N^+(0, 1)\\
115+
\text{$\mu\in \mathbb{R}^+$ such that $R(\mu, \theta)
116+
= \mu + \mu e^{\theta \mu} - 1= 0$}\\
117+
y \sim N(\mu, 0.1^2)
118+
119+
First, we observe that this problem is well-defined, because
120+
:math:`R(\cdot, \theta)` is monotone and has the image :math:`(-1, \infty)`
121+
for :math:`\mu, \theta \in \mathbb{R}^+`. To avoid overflows in
122+
:math:`\exp(\mu \theta)` for large
123+
values of :math:`\mu\theta` we instead find the root of
124+
125+
.. math::
126+
127+
R'(\mu, \theta)
128+
= \log(R(\mu, \theta) + 1)
129+
= \log(\mu) + \log(1 + e^{\theta\mu}).
130+
131+
Also, we have
132+
133+
.. math::
134+
135+
\frac{\partial}{\partial\mu}R'(\mu, \theta)
136+
= \theta\, \text{logit}^{-1}(\theta\mu) + \mu^{-1}.
137+
138+
We can now use `scipy.optimize.newton` to find the root::
139+
140+
from scipy import optimize, special
141+
import numpy as np
142+
143+
def func(mu, theta):
144+
thetamu = theta * mu
145+
value = np.log(mu) + np.logaddexp(0, thetamu)
146+
return value
147+
148+
def jac(mu, theta):
149+
thetamu = theta * mu
150+
jac = theta * special.expit(thetamu) + 1 / mu
151+
return jac
152+
153+
def mu_from_theta(theta):
154+
return optimize.newton(func, 1, fprime=jac, args=(0.4,))
155+
156+
We could wrap `mu_from_theta` with `tt.as_op` and use gradient-free
157+
methods like Metropolis, but to get NUTS and ADVI working, we also
158+
need to define the derivative of `mu_from_theta`. We can find this
159+
derivative using the implicit function theorem, or equivalently we
160+
take the derivative with respect of :math:`\theta` for both sides of
161+
:math:`R(\mu(\theta), \theta) = 0` and solve for :math:`\frac{d\mu}{d\theta}`.
162+
This isn't hard to do by hand, but for the fun of it, let's do it using
163+
sympy::
164+
165+
import sympy
166+
167+
mu = sympy.Function('mu')
168+
theta = sympy.Symbol('theta')
169+
R = mu(theta) + mu(theta) * sympy.exp(theta * mu(theta)) - 1
170+
solution = sympy.solve(R.diff(theta), mu(theta).diff(theta))[0]
171+
172+
We get
173+
174+
.. math::
175+
176+
\frac{d}{d\theta}\mu(\theta)
177+
= - \frac{\mu(\theta)^2}{1 + \theta\mu(\theta) + e^{-\theta\mu(\theta)}}
178+
179+
Now, we use this to define a theano op, that also computes the gradient::
180+
181+
import theano
182+
import theano.tensor as tt
183+
import theano.tests.unittest_tools
184+
185+
class MuFromTheta(tt.Op):
186+
itypes = [tt.dscalar]
187+
otypes = [tt.dscalar]
188+
189+
def perform(self, node, inputs, outputs):
190+
theta, = inputs
191+
mu = mu_from_theta(theta)
192+
outputs[0][0] = np.array(mu)
193+
194+
def grad(self, inputs, g):
195+
theta, = inputs
196+
mu = self(theta)
197+
thetamu = theta * mu
198+
return [- g[0] * mu ** 2 / (1 + thetamu + tt.exp(-thetamu))]
199+
200+
If you value your sanity, always check that the gradient is ok::
201+
202+
theano.tests.unittest_tools.verify_grad(MuFromTheta(), [np.array(0.2)])
203+
theano.tests.unittest_tools.verify_grad(MuFromTheta(), [np.array(1e-5)])
204+
theano.tests.unittest_tools.verify_grad(MuFromTheta(), [np.array(1e5)])
205+
206+
We can now define our model using this new op::
207+
208+
import pymc3 as pm
209+
210+
tt_mu_from_theta = MuFromTheta()
211+
212+
with pm.Model() as model:
213+
theta = pm.HalfNormal('theta', sd=1)
214+
mu = pm.Deterministic('mu', tt_mu_from_theta(theta))
215+
pm.Normal('y', mu=mu, sd=0.1, observed=[0.2, 0.21, 0.3])
216+
217+
trace = pm.sample()

docs/source/api/gp.rst

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,35 @@
1-
******************
2-
Gaussian Processes
3-
******************
4-
5-
6-
GP
7-
--
1+
GP Implementations
2+
------------------
83

94
.. currentmodule:: pymc3.gp.gp
105
.. automodule:: pymc3.gp.gp
116
:members:
127

8+
Mean Functions
9+
--------------
10+
11+
.. currentmodule:: pymc3.gp.cov
12+
.. autosummary::
13+
14+
Zero
15+
Constant
16+
Linear
1317

14-
Covariance Functions / Kernels
15-
------------------------------
18+
Covariance Functions
19+
--------------------
1620

1721
.. currentmodule:: pymc3.gp.cov
1822
.. autosummary::
1923

24+
Constant
25+
WhiteNoise
2026
ExpQuad
2127
RatQuad
2228
Matern32
2329
Matern52
2430
Exponential
2531
Cosine
32+
Periodic
2633
Linear
2734
Polynomial
2835
WarpedInput

docs/source/api/plots.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ Plots
55
.. currentmodule:: pymc3.plots
66

77
.. automodule:: pymc3.plots
8-
:members: traceplot, plot_posterior, forestplot, compare_plot, autocorrplot
8+
:members: traceplot, plot_posterior, forestplot, compareplot, autocorrplot,
9+
energyplot, kdeplot

docs/source/examples.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Howto
1313
notebooks/posterior_predictive.ipynb
1414
notebooks/model_comparison.ipynb
1515
notebooks/model_averaging.ipynb
16+
notebooks/Bayes_factor.ipynb
1617
notebooks/howto_debugging.ipynb
1718
notebooks/PyMC3_tips_and_heuristic.ipynb
1819
notebooks/LKJ.ipynb
@@ -48,8 +49,8 @@ Gaussian Processes
4849
==================
4950

5051
.. toctree::
52+
notebooks/GP-MeansAndCovs.ipynb
5153
notebooks/GP-Marginal.ipynb
52-
notebooks/GP-Covariances.ipynb
5354
notebooks/GP-Latent.ipynb
5455
notebooks/GP-SparseApprox.ipynb
5556
notebooks/GP-TProcess.ipynb

docs/source/getting_started.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@ Getting started
88
notebooks/getting_started.ipynb
99
notebooks/api_quickstart.ipynb
1010
notebooks/variational_api_quickstart.ipynb
11+
gp.rst
1112
theano.rst
1213
advanced_theano.rst

docs/source/gp.rst

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
===========================
2-
Gaussian Processes in PyMC3
3-
===========================
1+
******************
2+
Gaussian Processes
3+
******************
44

55
GP Basics
66
=========
@@ -111,8 +111,8 @@ with non-standard covariance and mean functons. For more information check out
111111
the tutorial on covariance functions.
112112

113113

114-
:code:`gp.*` implementations
115-
============================
114+
GP Implementations
115+
==================
116116

117117
PyMC3 includes several GP implementations, including marginal and latent
118118
variable models and also some fast approximations. Their usage all follows a

0 commit comments

Comments
 (0)