Skip to content

Instantly share code, notes, and snippets.

@AustinRochford
Last active January 28, 2017 17:30
Show Gist options
  • Save AustinRochford/52828a84cb5eb0311b843aeba96c2efa to your computer and use it in GitHub Desktop.
Save AustinRochford/52828a84cb5eb0311b843aeba96c2efa to your computer and use it in GitHub Desktop.
Hypothesis Distribution Tests Prototyping
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"from hypothesis import given, strategies as st\n",
"from hypothesis.extra.numpy import arrays as arrays_\n",
"import nose\n",
"import unittest"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import numpy as np\n",
"from numpy.testing import assert_allclose\n",
"import pymc3 as pm\n",
"from pymc3.tests.helpers import SeededTest\n",
"import scipy as sp"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"R = st.floats(min_value=-3, max_value=3,\n",
" allow_nan=False, allow_infinity=False)\n",
"R_plus = st.floats(min_value=1e-6, max_value=100.,\n",
" allow_nan=False, allow_infinity=False)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def arrays(elements, dtype=np.float64, shape=(10,)):\n",
" return arrays_(dtype, shape, elements=elements)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"class TestMatchesScipy(SeededTest):\n",
" @given(arrays(R), R, R_plus)\n",
" def test_normal(self, values, mu, sd):\n",
" dist = pm.Normal.dist(mu, sd)\n",
" \n",
" assert_allclose(dist.logp(values).eval(),\n",
" sp.stats.norm.logpdf(values, mu, sd))"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
".\n",
"----------------------------------------------------------------------\n",
"Ran 1 test in 61.420s\n",
"\n",
"OK\n"
]
},
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"suite = unittest.defaultTestLoader.loadTestsFromTestCase(TestMatchesScipy)\n",
"nose.run(argv=['test'], suite=suite)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.5.2"
}
},
"nbformat": 4,
"nbformat_minor": 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment