Skip to content

Commit fc3da1f

Browse files
amagehmo2561057
andauthored
Update criterion functions for new estimagic interface (#389)
Co-authored-by: mo2561057 <moritz.mendel12@gmail.com>
1 parent a4a5ff4 commit fc3da1f

12 files changed

Lines changed: 525 additions & 390 deletions

.pre-commit-config.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,16 @@ repos:
1010
- id: debug-statements
1111
- id: end-of-file-fixer
1212
- repo: https://github.com/asottile/pyupgrade
13-
rev: v2.11.0
13+
rev: v2.13.0
1414
hooks:
1515
- id: pyupgrade
1616
args: [--py36-plus]
1717
- repo: https://github.com/asottile/reorder_python_imports
18-
rev: v2.4.0
18+
rev: v2.5.0
1919
hooks:
2020
- id: reorder-python-imports
2121
- repo: https://github.com/psf/black
22-
rev: 20.8b1
22+
rev: 21.4b0
2323
hooks:
2424
- id: black
2525
- repo: https://github.com/asottile/blacken-docs
@@ -28,7 +28,7 @@ repos:
2828
- id: blacken-docs
2929
additional_dependencies: [black]
3030
- repo: https://github.com/PyCQA/flake8
31-
rev: 3.9.0
31+
rev: 3.9.1
3232
hooks:
3333
- id: flake8
3434
additional_dependencies: [

docs/how_to_guides/how_to_likelihood.ipynb

Lines changed: 46 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
},
4141
{
4242
"cell_type": "code",
43-
"execution_count": 6,
43+
"execution_count": 1,
4444
"metadata": {},
4545
"outputs": [],
4646
"source": [
@@ -50,7 +50,7 @@
5050
},
5151
{
5252
"cell_type": "code",
53-
"execution_count": 7,
53+
"execution_count": 2,
5454
"metadata": {},
5555
"outputs": [],
5656
"source": [
@@ -68,7 +68,7 @@
6868
},
6969
{
7070
"cell_type": "code",
71-
"execution_count": 8,
71+
"execution_count": 3,
7272
"metadata": {},
7373
"outputs": [
7474
{
@@ -77,7 +77,7 @@
7777
"-5.494678164823001"
7878
]
7979
},
80-
"execution_count": 8,
80+
"execution_count": 3,
8181
"metadata": {},
8282
"output_type": "execute_result"
8383
}
@@ -92,73 +92,82 @@
9292
"cell_type": "markdown",
9393
"metadata": {},
9494
"source": [
95-
"By default, the function returns a scalar value given by the mean log likelihood. To return the log likelihood contributions, set the argument `return_scalar` to `False`. The function will the return an array."
95+
"By default, the function returns a scalar value given by the mean log likelihood. To return the log likelihood contributions, set the argument `return_scalar` to `False`. The function will the return a dictionary containing the scalar value, contributions, and a pandas.DataFrame which can be used for visualization purposes."
9696
]
9797
},
9898
{
9999
"cell_type": "code",
100-
"execution_count": 11,
100+
"execution_count": 6,
101101
"metadata": {},
102102
"outputs": [
103103
{
104104
"data": {
105105
"text/plain": [
106-
"(1000,)"
106+
"dict_keys(['value', 'contributions', 'comparison_plot_data'])"
107107
]
108108
},
109-
"execution_count": 11,
109+
"execution_count": 6,
110110
"metadata": {},
111111
"output_type": "execute_result"
112112
}
113113
],
114114
"source": [
115115
"log_like_contribs = rp.get_log_like_func(params=params, options=options, df=data, return_scalar=False)\n",
116-
"array = log_like_contribs(params)\n",
117-
"array.shape"
118-
]
119-
},
120-
{
121-
"cell_type": "markdown",
122-
"metadata": {},
123-
"source": [
124-
"## Comparison plot data\n",
125-
"\n",
126-
"The log likelihood function can also return a pandas.DataFrame containing more detailed information. In the DataFrame lists the individual contributions of each observation split up by choices and wages and is suited for [estimagic](https://estimagic.readthedocs.io/en/latest/)'s visualization capabilities. When the argument `return_comparison_plot_data` is set to **True**, the criterion returns a tuple containing the mean log likelihood/likelihood contributions and the comparison plot data."
116+
"outputs = log_like_contribs(params)\n",
117+
"outputs.keys()"
127118
]
128119
},
129120
{
130121
"cell_type": "code",
131-
"execution_count": 12,
122+
"execution_count": 7,
132123
"metadata": {},
133-
"outputs": [],
124+
"outputs": [
125+
{
126+
"data": {
127+
"text/plain": [
128+
"-5.494678164823001"
129+
]
130+
},
131+
"execution_count": 7,
132+
"metadata": {},
133+
"output_type": "execute_result"
134+
}
135+
],
134136
"source": [
135-
"log_like = rp.get_log_like_func(params=params, options=options, df=data, return_comparison_plot_data=True)\n",
136-
"out = log_like(params)"
137+
"outputs[\"value\"]"
137138
]
138139
},
139140
{
140141
"cell_type": "code",
141-
"execution_count": 13,
142+
"execution_count": 11,
142143
"metadata": {},
143144
"outputs": [
144145
{
145146
"data": {
146147
"text/plain": [
147-
"-5.494678164823001"
148+
"array([-1.12998713, -1.16105606, -8.14899502, -1.18885353, -6.5085553 ,\n",
149+
" -1.22019297, -7.125007 , -5.29376864, -7.4765499 , -4.82486523])"
148150
]
149151
},
150-
"execution_count": 13,
152+
"execution_count": 11,
151153
"metadata": {},
152154
"output_type": "execute_result"
153155
}
154156
],
155157
"source": [
156-
"out[0]"
158+
"outputs[\"contributions\"][0:10]"
159+
]
160+
},
161+
{
162+
"cell_type": "markdown",
163+
"metadata": {},
164+
"source": [
165+
"The DataFrame saved under the key `comparison_plot_data` lists the individual contributions of each observation split up by choices and wages and is suited for [estimagic](https://estimagic.readthedocs.io/en/latest/)'s visualization capabilities."
157166
]
158167
},
159168
{
160169
"cell_type": "code",
161-
"execution_count": 14,
170+
"execution_count": 12,
162171
"metadata": {},
163172
"outputs": [
164173
{
@@ -230,83 +239,26 @@
230239
" <td>-0.072571</td>\n",
231240
" <td>choice</td>\n",
232241
" </tr>\n",
233-
" <tr>\n",
234-
" <th>...</th>\n",
235-
" <td>...</td>\n",
236-
" <td>...</td>\n",
237-
" <td>...</td>\n",
238-
" <td>...</td>\n",
239-
" <td>...</td>\n",
240-
" </tr>\n",
241-
" <tr>\n",
242-
" <th>9995</th>\n",
243-
" <td>999</td>\n",
244-
" <td>0</td>\n",
245-
" <td>hammock</td>\n",
246-
" <td>0.000000</td>\n",
247-
" <td>wage</td>\n",
248-
" </tr>\n",
249-
" <tr>\n",
250-
" <th>9996</th>\n",
251-
" <td>999</td>\n",
252-
" <td>1</td>\n",
253-
" <td>hammock</td>\n",
254-
" <td>0.000000</td>\n",
255-
" <td>wage</td>\n",
256-
" </tr>\n",
257-
" <tr>\n",
258-
" <th>9997</th>\n",
259-
" <td>999</td>\n",
260-
" <td>2</td>\n",
261-
" <td>hammock</td>\n",
262-
" <td>0.000000</td>\n",
263-
" <td>wage</td>\n",
264-
" </tr>\n",
265-
" <tr>\n",
266-
" <th>9998</th>\n",
267-
" <td>999</td>\n",
268-
" <td>3</td>\n",
269-
" <td>hammock</td>\n",
270-
" <td>0.000000</td>\n",
271-
" <td>wage</td>\n",
272-
" </tr>\n",
273-
" <tr>\n",
274-
" <th>9999</th>\n",
275-
" <td>999</td>\n",
276-
" <td>4</td>\n",
277-
" <td>hammock</td>\n",
278-
" <td>0.000000</td>\n",
279-
" <td>wage</td>\n",
280-
" </tr>\n",
281242
" </tbody>\n",
282243
"</table>\n",
283-
"<p>10000 rows × 5 columns</p>\n",
284244
"</div>"
285245
],
286246
"text/plain": [
287-
" identifier period choice value kind\n",
288-
"0 0 0 hammock -0.597872 choice\n",
289-
"1 0 1 hammock -0.248358 choice\n",
290-
"2 0 2 hammock -0.127806 choice\n",
291-
"3 0 3 hammock -0.083382 choice\n",
292-
"4 0 4 hammock -0.072571 choice\n",
293-
"... ... ... ... ... ...\n",
294-
"9995 999 0 hammock 0.000000 wage\n",
295-
"9996 999 1 hammock 0.000000 wage\n",
296-
"9997 999 2 hammock 0.000000 wage\n",
297-
"9998 999 3 hammock 0.000000 wage\n",
298-
"9999 999 4 hammock 0.000000 wage\n",
299-
"\n",
300-
"[10000 rows x 5 columns]"
247+
" identifier period choice value kind\n",
248+
"0 0 0 hammock -0.597872 choice\n",
249+
"1 0 1 hammock -0.248358 choice\n",
250+
"2 0 2 hammock -0.127806 choice\n",
251+
"3 0 3 hammock -0.083382 choice\n",
252+
"4 0 4 hammock -0.072571 choice"
301253
]
302254
},
303-
"execution_count": 14,
255+
"execution_count": 12,
304256
"metadata": {},
305257
"output_type": "execute_result"
306258
}
307259
],
308260
"source": [
309-
"out[1]"
261+
"outputs[\"comparison_plot_data\"].head()"
310262
]
311263
},
312264
{
@@ -398,7 +350,7 @@
398350
"name": "python",
399351
"nbconvert_exporter": "python",
400352
"pygments_lexer": "ipython3",
401-
"version": "3.8.3"
353+
"version": "3.7.10"
402354
}
403355
},
404356
"nbformat": 4,

0 commit comments

Comments
 (0)