Skip to content

Commit

Permalink
Merge branch 'master' into pymc_rename
Browse files Browse the repository at this point in the history
  • Loading branch information
canyon289 authored Mar 20, 2022
2 parents 0cd0006 + b4ef358 commit c002fc4
Show file tree
Hide file tree
Showing 3 changed files with 576 additions and 58 deletions.
3 changes: 2 additions & 1 deletion .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Thank you for opening a pull request!

Please check our [style guide](https://github.com/pymc-devs/pymc/wiki/PyMC's-Jupyter-Notebook-Style), and also make sure that the notebooks you've modified pass the `pre-commit` checks. If, for example, you modified `notebook1.ipynb` and `notebook2.ipynb`, you could do this by running:

Please check our [style guide](https://docs.pymc.io/en/latest/contributing/jupyter_style.html), and also make sure that the notebooks you've modified pass the `pre-commit` checks. If, for example, you modified `notebook1.ipynb` and `notebook2.ipynb`, you could do this by running:

```bash
pre-commit run --files notebook1.ipynb notebook2.ipynb
Expand Down
376 changes: 319 additions & 57 deletions Rethinking_2/Chp_11.ipynb

Large diffs are not rendered by default.

255 changes: 255 additions & 0 deletions Rethinking_2/End_of_chapter_problems/Chapter_11.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,255 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"lines_to_next_cell": 2
},
"outputs": [],
"source": [
"import arviz as az\n",
"import numpy as np\n",
"import pandas as pd\n",
"import pymc3 as pm\n",
"\n",
"from matplotlib import pylab as plt"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"RANDOM_SEED = 8927\n",
"np.random.seed(RANDOM_SEED)\n",
"\n",
"%config InlineBackend.figure_format = 'retina'\n",
"%load_ext watermark\n",
"az.style.use(\"arviz-darkgrid\")\n",
"az.rcParams[\"stats.hdi_prob\"] = 0.89"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Chapter 11 - GOD SPIKED THE INTEGERS"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"For 11E1-E3, see page 337 of 2nd edition, Overthinking box.\n",
"\n",
"## 11E1. \n",
"\n",
"**If an event has probability 0.35, what are the log-odds of this event?**\n",
"\n",
"### Answer \n",
"\n",
"\n",
"```\n",
"odds = p / (1 - p)\n",
"log-odds = log(odds)\n",
" = log(0.35 / 0.65)\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"-0.6190392084062235"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.log(0.35 / 0.65)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 11E2.\n",
"\n",
"**If an event has log-odds 3.2, what is the probability of this event?**\n",
"\n",
"### Answer\n",
"Take the same approach as above and solve for p.\n",
"\n",
"```\n",
"\n",
"odds = p / (1 - p)\n",
"\n",
"odds * (1 - p) = p\n",
"odds - odds * p = p\n",
"\n",
"Rearrange\n",
"0 = p + odds * p - odds \n",
"0 = p(1 + odds) - odds\n",
"odds = p(1 + odds)\n",
"\n",
"p = odds / (1 + odds)\n",
" \n",
"exp(log-odds) = odds\n",
"\n",
"p = exp(log-odds) / (1 + exp(log-odds))\n",
"\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.9608342772032357"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.exp(3.2) / (1 + np.exp(3.2))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 11E3.\n",
"\n",
"**Suppose that a coefficient in a logistic regression has value 1.7. What does this imply about the proportional change in odds of the outcome?**\n",
"\n",
"### Answer\n",
"\n",
"After exponentiating the coefficient, this value represents the factor to multiply by the odds by when a 1 unit increase in the predictor variable occurs. In this problem, a coefficient of 1.7 means that the proportional odds with a 1 unit change leads to multiplying the odds by $\\text{exp}(1.7) \\approx 5.474$. It is important to emphasize that this is relative number.\n",
"\n",
"Standardizing the feature (if continuous) facilitiates interpretation; a 1 unit change in this case means 1 standard deviation change. If the variable is categorical and dummy-encoded, then 1 unit change represents the shift from the class assigned 0 to the class assigned 1."
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"5.4739473917272"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.exp(1.7)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 11E4.\n",
"\n",
"**Why do Poisson regressions sometimes require the use of an offset? Provide an example.**\n",
"\n",
"### Answer\n",
"See 2nd edition, page 357, section 11.2.3.\n",
"\n",
"In Poisson regression, $\\lambda$ can be interpreted as the expected value of counts and as a rate with regards to time or distance. When seen as a rate, the unit for which the number of events are counted (the exposure) can vary.\n",
"\n",
"The book supposes two different monasteries, one tracking manuscripts over days while another is done over weeks. The log link helps us account for the differences in exposure.\n",
"\n",
"The monstary example involved an exposure involving time. Distance might be another exposure. Imagine two cars traveling down a road that are counting out-of-state license plates. One car might tabulate the number of cars every mile, while the other might keep track every kilometer. The use of an offset would be applicable in this case as well."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# NOTE\n",
"\n",
"Other solutions will be added or contributions welcome."
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Last updated: Tue Jan 04 2022\n",
"\n",
"Python implementation: CPython\n",
"Python version : 3.8.6\n",
"IPython version : 7.20.0\n",
"\n",
"numpy : 1.20.1\n",
"pymc3 : 3.11.0\n",
"sys : 3.8.6 | packaged by conda-forge | (default, Jan 25 2021, 23:22:12) \n",
"[Clang 11.0.1 ]\n",
"matplotlib: 3.3.4\n",
"pandas : 1.2.1\n",
"arviz : 0.11.1\n",
"\n",
"Watermark: 2.1.0\n",
"\n"
]
}
],
"source": [
"%watermark -n -u -v -iv -w"
]
}
],
"metadata": {
"interpreter": {
"hash": "e92bb7a9aacd9667df20255a0d1807f00b256076d24753ff832859a09e84cc67"
},
"jupytext": {
"encoding": "# -*- coding: utf-8 -*-"
},
"kernelspec": {
"display_name": "pymc3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.6"
}
},
"nbformat": 4,
"nbformat_minor": 4
}

0 comments on commit c002fc4

Please sign in to comment.