Skip to content

Remove find_MAP demonstration in the getting started tutorial #145

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
167 changes: 3 additions & 164 deletions examples/getting_started.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -283,167 +283,6 @@
"Notice that, unlike for the priors of the model, the parameters for the normal distribution of `Y_obs` are not fixed values, but rather are the deterministic object `mu` and the stochastic `sigma`. This creates parent-child relationships between the likelihood and these two variables."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Model fitting\n",
"\n",
"Having completely specified our model, the next step is to obtain posterior estimates for the unknown variables in the model. Ideally, we could calculate the posterior estimates analytically, but for most non-trivial models, this is not feasible. We will consider two approaches, whose appropriateness depends on the structure of the model and the goals of the analysis: finding the *maximum a posteriori* (MAP) point using optimization methods, and computing summaries based on samples drawn from the posterior distribution using Markov Chain Monte Carlo (MCMC) sampling methods.\n",
"\n",
"#### Maximum a posteriori methods\n",
"\n",
"The **maximum a posteriori (MAP)** estimate for a model, is the mode of the posterior distribution and is generally found using numerical optimization methods. This is often fast and easy to do, but only gives a point estimate for the parameters and can be biased if the mode isn't representative of the distribution. PyMC3 provides this functionality with the `find_MAP` function.\n",
"\n",
"Below we find the MAP for our original model. The MAP is returned as a parameter **point**, which is always represented by a Python dictionary of variable names to NumPy arrays of parameter values. "
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
" <div>\n",
" <style>\n",
" /* Turns off some styling */\n",
" progress {\n",
" /* gets rid of default border in Firefox and Opera. */\n",
" border: none;\n",
" /* Needs to be in here for Safari polyfill so background images work as expected. */\n",
" background-size: auto;\n",
" }\n",
" .progress-bar-interrupted, .progress-bar-interrupted::-webkit-progress-bar {\n",
" background: #F44336;\n",
" }\n",
" </style>\n",
" <progress value='18' class='' max='18' style='width:300px; height:20px; vertical-align: middle;'></progress>\n",
" 100.00% [18/18 00:00<00:00 logp = -157.69, ||grad|| = 14.913]\n",
" </div>\n",
" "
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n"
]
},
{
"data": {
"text/plain": [
"{'alpha': array(0.95724679),\n",
" 'beta': array([1.10071814, 2.9511438 ]),\n",
" 'sigma_log__': array(0.03540151),\n",
" 'sigma': array(1.0360356)}"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"map_estimate = pm.find_MAP(model=basic_model)\n",
"map_estimate"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"By default, `find_MAP` uses the Broyden–Fletcher–Goldfarb–Shanno (BFGS) optimization algorithm to find the maximum of the log-posterior but also allows selection of other optimization algorithms from the `scipy.optimize` module. For example, below we use Powell's method to find the MAP."
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
" <div>\n",
" <style>\n",
" /* Turns off some styling */\n",
" progress {\n",
" /* gets rid of default border in Firefox and Opera. */\n",
" border: none;\n",
" /* Needs to be in here for Safari polyfill so background images work as expected. */\n",
" background-size: auto;\n",
" }\n",
" .progress-bar-interrupted, .progress-bar-interrupted::-webkit-progress-bar {\n",
" background: #F44336;\n",
" }\n",
" </style>\n",
" <progress value='214' class='' max='214' style='width:300px; height:20px; vertical-align: middle;'></progress>\n",
" 100.00% [214/214 00:01<00:00 logp = -156.45, ||grad|| = 0.24264]\n",
" </div>\n",
" "
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"/Users/CloudChaoszero/opt/anaconda3/envs/pymc3-dev-py38/lib/python3.8/site-packages/scipy/optimize/_minimize.py:519: RuntimeWarning: Method powell does not use gradient information (jac).\n",
" warn('Method %s does not use gradient information (jac).' % method,\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n"
]
},
{
"data": {
"text/plain": [
"{'alpha': array(0.95835519),\n",
" 'beta': array([1.1017629 , 2.95394057]),\n",
" 'sigma_log__': array(0.03638187),\n",
" 'sigma': array(1.03705179)}"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"map_estimate = pm.find_MAP(model=basic_model, method=\"powell\")\n",
"map_estimate"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"It is important to note that the MAP estimate is not always reasonable, especially if the mode is at an extreme. This can be a subtle issue; with high dimensional posteriors, one can have areas of extremely high density but low total probability because the volume is very small. This will often occur in hierarchical models with the variance parameter for the random effect. If the individual group means are all the same, the posterior will have near infinite density if the scale parameter for the group means is almost zero, even though the probability of such a small scale parameter will be small since the group means must be extremely close together. \n",
"\n",
"Most techniques for finding the MAP estimate also only find a *local* optimum (which is often good enough), but can fail badly for multimodal posteriors if the different modes are meaningfully different.\n",
"\n",
"In summary, while PyMC3 provides the function `find_MAP()`, at this point mostly for historical reasons, this function is of little use in most scenarios. If you want a point estimate you should get it from the posterior. In the next section we will see how to get a posterior using sampling methods."
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down Expand Up @@ -3811,9 +3650,9 @@
"metadata": {
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Python PyMC3 (Dev)",
"display_name": "Python 3",
"language": "python",
"name": "pymc3-dev-py38"
"name": "python3"
},
"language_info": {
"codemirror_mode": {
Expand All @@ -3825,7 +3664,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.6"
"version": "3.7.6"
}
},
"nbformat": 4,
Expand Down