Last active
January 4, 2023 21:53
-
-
Save jessegrabowski/ff04ea88a77aaae128cab1c04553855a to your computer and use it in GitHub Desktop.
Shapes, Dims, and Coords in PyMC
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "import pymc as pm\nimport numpy as np", | |
"execution_count": 1, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "k = 6\np = 10\nn = 100\n\ncoords = {\"player\" : np.arange(p),\n \"obs_id\" : np.arange(n),\n \"predictors\" : np.arange(k)}", | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "markdown", | |
"source": "# Nd draws from a single normal " | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "with pm.Model(coords=coords) as shape_demo:\n beta_mu = pm.Normal('mu')\n beta_sigma = pm.HalfNormal('sigma')\n \n beta = pm.Normal('beta', mu=beta_mu, sigma=beta_sigma, dims=['player', 'predictors'])", | |
"execution_count": 2, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "beta_draws = pm.draw(beta, 100_000)\nwith np.printoptions(precision=3, suppress=True, linewidth=10_000):\n print(beta_draws.mean(axis=0))\n print(beta_draws.std(axis=0))", | |
"execution_count": 3, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "[[ 0.004 0.002 0.003 0.002 0.001 0.007]\n [ 0.004 -0. 0.005 0.008 0.002 0.006]\n [ 0.004 0.002 0.007 0.002 0.001 0.003]\n [ 0.008 0.008 0. 0.006 -0. 0.002]\n [-0. 0. 0.012 0.006 0.008 0.005]\n [ 0.005 0.001 0. 0.005 0.002 0.004]\n [ 0.009 0.002 0.004 0.003 -0.001 0.001]\n [ 0.002 0.007 0.005 0.006 0.001 0.006]\n [-0.001 0.006 0.004 -0.001 0.004 0.001]\n [ 0.003 0.001 0.007 -0. 0.008 0.003]]\n[[1.408 1.408 1.403 1.403 1.406 1.412]\n [1.403 1.409 1.411 1.41 1.411 1.414]\n [1.415 1.416 1.413 1.413 1.408 1.406]\n [1.405 1.406 1.411 1.415 1.412 1.4 ]\n [1.406 1.409 1.408 1.412 1.41 1.415]\n [1.411 1.417 1.412 1.407 1.409 1.409]\n [1.406 1.411 1.407 1.414 1.409 1.405]\n [1.409 1.408 1.404 1.412 1.414 1.406]\n [1.413 1.412 1.411 1.403 1.411 1.408]\n [1.412 1.414 1.41 1.408 1.409 1.41 ]]\n", | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "markdown", | |
"source": "## Show order doesn't matter when mu, sigma are scalar" | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "with pm.Model(coords=coords) as shape_demo:\n beta_mu = pm.Normal('mu')\n beta_sigma = pm.HalfNormal('sigma')\n \n beta = pm.Normal('beta', mu=beta_mu, sigma=beta_sigma, dims=['predictors', 'player'])", | |
"execution_count": 18, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true, | |
"scrolled": true | |
}, | |
"cell_type": "code", | |
"source": "beta_draws = pm.draw(beta, 100_000)\nwith np.printoptions(precision=3, suppress=True, linewidth=10_000):\n print(beta_draws.mean(axis=0))\n print(beta_draws.std(axis=0))", | |
"execution_count": 19, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "[[-0.004 -0.004 -0.005 -0.001 -0.008 -0.006 -0.005 -0.005 -0.005 -0.001]\n [-0.003 -0.008 -0.001 -0.002 -0.006 -0.002 -0.006 -0.008 -0.008 -0.004]\n [ 0.002 -0.005 -0.012 -0.009 -0.005 -0.004 0.005 -0.006 0.003 -0.004]\n [-0.002 -0.004 -0.001 -0.008 -0.004 0.001 -0.005 0. -0.002 -0.004]\n [-0.008 -0.008 -0.005 -0.001 -0.007 -0.005 -0.002 -0.001 -0.006 -0.002]\n [-0.01 0.001 -0.004 -0.004 0.002 0.002 -0.008 -0.003 -0.004 -0.001]]\n[[1.412 1.411 1.41 1.407 1.407 1.41 1.414 1.415 1.406 1.409]\n [1.413 1.41 1.41 1.406 1.412 1.412 1.417 1.416 1.415 1.409]\n [1.411 1.412 1.407 1.411 1.409 1.406 1.416 1.409 1.408 1.41 ]\n [1.408 1.411 1.408 1.412 1.411 1.415 1.409 1.405 1.408 1.413]\n [1.411 1.407 1.419 1.406 1.404 1.412 1.41 1.41 1.41 1.411]\n [1.412 1.405 1.409 1.406 1.412 1.408 1.407 1.41 1.41 1.406]]\n", | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "markdown", | |
"source": "# Vector-valued mu, sigma for matrix of k independent normals" | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "with pm.Model(coords=coords) as shape_demo:\n beta_mu = pm.Normal('mu', mu=np.arange(k), dims=['predictors'])\n beta_sigma = pm.HalfNormal('sigma', sigma=np.arange(k) + 1, dims=['predictors'])\n \n # player is the \"batch dimension\", and MUST come first!\n beta = pm.Normal('beta', mu=beta_mu, sigma=beta_sigma, dims=['player', 'predictors'])", | |
"execution_count": 12, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "beta_draws = pm.draw(beta, 100_000)\nwith np.printoptions(precision=3, suppress=True, linewidth=10_000):\n print(beta_draws.mean(axis=0))\n print(beta_draws.std(axis=0))", | |
"execution_count": 13, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "[[0.002 1.002 1.978 2.983 3.964 4.993]\n [0.009 0.998 2.016 2.994 3.984 4.977]\n [0.01 1.008 2.02 2.992 4.005 4.968]\n [0.001 1.006 2.003 3.004 3.991 5.003]\n [0.003 1.006 2.009 3.008 4.018 4.974]\n [0.007 1.006 2.013 3.004 4.006 5.002]\n [0.006 1.01 1.999 3. 3.991 5.013]\n [0.005 0.996 1.991 2.979 4.005 4.999]\n [0.002 1.005 1.993 3.002 4.02 4.984]\n [0.001 1.004 1.998 2.993 3.992 5.012]]\n[[1.415 2.235 3.148 4.144 5.071 6.062]\n [1.411 2.236 3.155 4.128 5.108 6.101]\n [1.414 2.235 3.139 4.136 5.109 6.116]\n [1.42 2.231 3.166 4.133 5.099 6.102]\n [1.42 2.237 3.177 4.13 5.129 6.12 ]\n [1.417 2.226 3.154 4.135 5.1 6.088]\n [1.41 2.237 3.141 4.104 5.112 6.11 ]\n [1.413 2.229 3.152 4.115 5.091 6.117]\n [1.415 2.218 3.154 4.132 5.079 6.131]\n [1.408 2.227 3.164 4.119 5.118 6.139]]\n", | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "markdown", | |
"source": "## \"batch\" dims must come first" | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "with pm.Model(coords=coords) as shape_demo:\n beta_mu = pm.Normal('mu', mu=np.arange(k), dims=['predictors'])\n beta_sigma = pm.HalfNormal('sigma', sigma=np.arange(k) + 1, dims=['predictors'])\n \n # we want \"player\" draws from each of \"predictor\" distributions, but if the batch dim isn't first PyMC doesn't get it\n beta = pm.Normal('beta', mu=beta_mu, sigma=beta_sigma, dims=['predictors', 'player'])", | |
"execution_count": 14, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "beta_draws = pm.draw(beta, 100_000)\nwith np.printoptions(precision=3, suppress=True, linewidth=10_000):\n print(beta_draws.mean(axis=0))\n print(beta_draws.std(axis=0))", | |
"execution_count": 15, | |
"outputs": [ | |
{ | |
"output_type": "error", | |
"ename": "ValueError", | |
"evalue": "shape mismatch: objects cannot be broadcast to a single shape. Mismatch is between arg 0 with shape (6, 10) and arg 1 with shape (6,).\nApply node that caused the error: normal_rv{0, (0, 0), floatX, True}(RandomGeneratorSharedVariable(<Generator(PCG64) at 0x12D533580>), TensorConstant{[ 6 10]}, TensorConstant{11}, mu, sigma)\nToposort index: 2\nInputs types: [RandomGeneratorType, TensorType(int64, (2,)), TensorType(int64, ()), TensorType(float64, (6,)), TensorType(float64, (6,))]\nInputs shapes: ['No shapes', (2,), (), (6,), (6,)]\nInputs strides: ['No strides', (8,), (), (8,), (8,)]\nInputs values: [Generator(PCG64) at 0x12D533580, array([ 6, 10]), array(11), 'not shown', 'not shown']\nOutputs clients: [['output'], ['output']]\n\nHINT: Re-running with most PyTensor optimizations disabled could provide a back-trace showing when this node was created. This can be done by setting the PyTensor flag 'optimizer=fast_compile'. If that does not work, PyTensor optimizations can be disabled with 'optimizer=None'.\nHINT: Use the PyTensor flag `exception_verbosity=high` for a debug print-out and storage map footprint of this Apply node.", | |
"traceback": [ | |
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", | |
"\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", | |
"File \u001b[0;32m~/mambaforge/envs/econ/lib/python3.9/site-packages/pytensor/compile/function/types.py:972\u001b[0m, in \u001b[0;36mFunction.__call__\u001b[0;34m(self, *args, **kwargs)\u001b[0m\n\u001b[1;32m 970\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[1;32m 971\u001b[0m outputs \u001b[38;5;241m=\u001b[39m (\n\u001b[0;32m--> 972\u001b[0m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mvm\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 973\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m output_subset \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m\n\u001b[1;32m 974\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mvm(output_subset\u001b[38;5;241m=\u001b[39moutput_subset)\n\u001b[1;32m 975\u001b[0m )\n\u001b[1;32m 976\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mException\u001b[39;00m:\n", | |
"File \u001b[0;32m~/mambaforge/envs/econ/lib/python3.9/site-packages/pytensor/graph/op.py:542\u001b[0m, in \u001b[0;36mOp.make_py_thunk.<locals>.rval\u001b[0;34m(p, i, o, n, params)\u001b[0m\n\u001b[1;32m 538\u001b[0m \u001b[38;5;129m@is_thunk_type\u001b[39m\n\u001b[1;32m 539\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mrval\u001b[39m(\n\u001b[1;32m 540\u001b[0m p\u001b[38;5;241m=\u001b[39mp, i\u001b[38;5;241m=\u001b[39mnode_input_storage, o\u001b[38;5;241m=\u001b[39mnode_output_storage, n\u001b[38;5;241m=\u001b[39mnode, params\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mNone\u001b[39;00m\n\u001b[1;32m 541\u001b[0m ):\n\u001b[0;32m--> 542\u001b[0m r \u001b[38;5;241m=\u001b[39m \u001b[43mp\u001b[49m\u001b[43m(\u001b[49m\u001b[43mn\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m[\u001b[49m\u001b[43mx\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m0\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mfor\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mx\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;129;43;01min\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mi\u001b[49m\u001b[43m]\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mo\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 543\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m o \u001b[38;5;129;01min\u001b[39;00m node\u001b[38;5;241m.\u001b[39moutputs:\n", | |
"File \u001b[0;32m~/mambaforge/envs/econ/lib/python3.9/site-packages/pytensor/tensor/random/op.py:379\u001b[0m, in \u001b[0;36mRandomVariable.perform\u001b[0;34m(self, node, inputs, outputs)\u001b[0m\n\u001b[1;32m 377\u001b[0m rng_var_out[\u001b[38;5;241m0\u001b[39m] \u001b[38;5;241m=\u001b[39m rng\n\u001b[0;32m--> 379\u001b[0m smpl_val \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mrng_fn\u001b[49m\u001b[43m(\u001b[49m\u001b[43mrng\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43margs\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m+\u001b[39;49m\u001b[43m \u001b[49m\u001b[43m[\u001b[49m\u001b[43msize\u001b[49m\u001b[43m]\u001b[49m\u001b[43m)\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 381\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m (\n\u001b[1;32m 382\u001b[0m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(smpl_val, np\u001b[38;5;241m.\u001b[39mndarray)\n\u001b[1;32m 383\u001b[0m \u001b[38;5;129;01mor\u001b[39;00m \u001b[38;5;28mstr\u001b[39m(smpl_val\u001b[38;5;241m.\u001b[39mdtype) \u001b[38;5;241m!=\u001b[39m out_var\u001b[38;5;241m.\u001b[39mtype\u001b[38;5;241m.\u001b[39mdtype\n\u001b[1;32m 384\u001b[0m ):\n", | |
"File \u001b[0;32m~/mambaforge/envs/econ/lib/python3.9/site-packages/pytensor/tensor/random/op.py:164\u001b[0m, in \u001b[0;36mRandomVariable.rng_fn\u001b[0;34m(self, rng, *args, **kwargs)\u001b[0m\n\u001b[1;32m 163\u001b[0m \u001b[38;5;124;03m\"\"\"Sample a numeric random variate.\"\"\"\u001b[39;00m\n\u001b[0;32m--> 164\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mgetattr\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43mrng\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mname\u001b[49m\u001b[43m)\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n", | |
"File \u001b[0;32m_generator.pyx:1136\u001b[0m, in \u001b[0;36mnumpy.random._generator.Generator.normal\u001b[0;34m()\u001b[0m\n", | |
"File \u001b[0;32m_common.pyx:600\u001b[0m, in \u001b[0;36mnumpy.random._common.cont\u001b[0;34m()\u001b[0m\n", | |
"File \u001b[0;32m_common.pyx:517\u001b[0m, in \u001b[0;36mnumpy.random._common.cont_broadcast_2\u001b[0;34m()\u001b[0m\n", | |
"File \u001b[0;32m__init__.pxd:741\u001b[0m, in \u001b[0;36mnumpy.PyArray_MultiIterNew3\u001b[0;34m()\u001b[0m\n", | |
"\u001b[0;31mValueError\u001b[0m: shape mismatch: objects cannot be broadcast to a single shape. Mismatch is between arg 0 with shape (6, 10) and arg 1 with shape (6,).", | |
"\nDuring handling of the above exception, another exception occurred:\n", | |
"\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", | |
"Cell \u001b[0;32mIn[15], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m beta_draws \u001b[38;5;241m=\u001b[39m \u001b[43mpm\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mdraw\u001b[49m\u001b[43m(\u001b[49m\u001b[43mbeta\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m100_000\u001b[39;49m\u001b[43m)\u001b[49m\n\u001b[1;32m 2\u001b[0m \u001b[38;5;28;01mwith\u001b[39;00m np\u001b[38;5;241m.\u001b[39mprintoptions(precision\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m3\u001b[39m, suppress\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mTrue\u001b[39;00m, linewidth\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m10_000\u001b[39m):\n\u001b[1;32m 3\u001b[0m \u001b[38;5;28mprint\u001b[39m(beta_draws\u001b[38;5;241m.\u001b[39mmean(axis\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m0\u001b[39m))\n", | |
"File \u001b[0;32m~/mambaforge/envs/econ/lib/python3.9/site-packages/pymc/sampling/forward.py:326\u001b[0m, in \u001b[0;36mdraw\u001b[0;34m(vars, draws, random_seed, **kwargs)\u001b[0m\n\u001b[1;32m 324\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(\u001b[38;5;28mvars\u001b[39m, (\u001b[38;5;28mlist\u001b[39m, \u001b[38;5;28mtuple\u001b[39m)):\n\u001b[1;32m 325\u001b[0m cast(Callable[[], np\u001b[38;5;241m.\u001b[39mndarray], draw_fn)\n\u001b[0;32m--> 326\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m np\u001b[38;5;241m.\u001b[39mstack([draw_fn() \u001b[38;5;28;01mfor\u001b[39;00m _ \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28mrange\u001b[39m(draws)])\n\u001b[1;32m 328\u001b[0m \u001b[38;5;66;03m# Multiple variable output\u001b[39;00m\n\u001b[1;32m 329\u001b[0m cast(Callable[[], List[np\u001b[38;5;241m.\u001b[39mndarray]], draw_fn)\n", | |
"File \u001b[0;32m~/mambaforge/envs/econ/lib/python3.9/site-packages/pymc/sampling/forward.py:326\u001b[0m, in \u001b[0;36m<listcomp>\u001b[0;34m(.0)\u001b[0m\n\u001b[1;32m 324\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(\u001b[38;5;28mvars\u001b[39m, (\u001b[38;5;28mlist\u001b[39m, \u001b[38;5;28mtuple\u001b[39m)):\n\u001b[1;32m 325\u001b[0m cast(Callable[[], np\u001b[38;5;241m.\u001b[39mndarray], draw_fn)\n\u001b[0;32m--> 326\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m np\u001b[38;5;241m.\u001b[39mstack([\u001b[43mdraw_fn\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m \u001b[38;5;28;01mfor\u001b[39;00m _ \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28mrange\u001b[39m(draws)])\n\u001b[1;32m 328\u001b[0m \u001b[38;5;66;03m# Multiple variable output\u001b[39;00m\n\u001b[1;32m 329\u001b[0m cast(Callable[[], List[np\u001b[38;5;241m.\u001b[39mndarray]], draw_fn)\n", | |
"File \u001b[0;32m~/mambaforge/envs/econ/lib/python3.9/site-packages/pytensor/compile/function/types.py:985\u001b[0m, in \u001b[0;36mFunction.__call__\u001b[0;34m(self, *args, **kwargs)\u001b[0m\n\u001b[1;32m 983\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mhasattr\u001b[39m(\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mvm, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mthunks\u001b[39m\u001b[38;5;124m\"\u001b[39m):\n\u001b[1;32m 984\u001b[0m thunk \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mvm\u001b[38;5;241m.\u001b[39mthunks[\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mvm\u001b[38;5;241m.\u001b[39mposition_of_error]\n\u001b[0;32m--> 985\u001b[0m \u001b[43mraise_with_op\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 986\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mmaker\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mfgraph\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 987\u001b[0m \u001b[43m \u001b[49m\u001b[43mnode\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mvm\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mnodes\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mvm\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mposition_of_error\u001b[49m\u001b[43m]\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 988\u001b[0m \u001b[43m \u001b[49m\u001b[43mthunk\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mthunk\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 989\u001b[0m \u001b[43m \u001b[49m\u001b[43mstorage_map\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43mgetattr\u001b[39;49m\u001b[43m(\u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mvm\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mstorage_map\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mNone\u001b[39;49;00m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 990\u001b[0m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 991\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m 992\u001b[0m \u001b[38;5;66;03m# old-style linkers raise their own exceptions\u001b[39;00m\n\u001b[1;32m 993\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m\n", | |
"File \u001b[0;32m~/mambaforge/envs/econ/lib/python3.9/site-packages/pytensor/link/utils.py:536\u001b[0m, in \u001b[0;36mraise_with_op\u001b[0;34m(fgraph, node, thunk, exc_info, storage_map)\u001b[0m\n\u001b[1;32m 531\u001b[0m warnings\u001b[38;5;241m.\u001b[39mwarn(\n\u001b[1;32m 532\u001b[0m \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mexc_type\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m error does not allow us to add an extra error message\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 533\u001b[0m )\n\u001b[1;32m 534\u001b[0m \u001b[38;5;66;03m# Some exception need extra parameter in inputs. So forget the\u001b[39;00m\n\u001b[1;32m 535\u001b[0m \u001b[38;5;66;03m# extra long error message in that case.\u001b[39;00m\n\u001b[0;32m--> 536\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m exc_value\u001b[38;5;241m.\u001b[39mwith_traceback(exc_trace)\n", | |
"File \u001b[0;32m~/mambaforge/envs/econ/lib/python3.9/site-packages/pytensor/compile/function/types.py:972\u001b[0m, in \u001b[0;36mFunction.__call__\u001b[0;34m(self, *args, **kwargs)\u001b[0m\n\u001b[1;32m 969\u001b[0m t0_fn \u001b[38;5;241m=\u001b[39m time\u001b[38;5;241m.\u001b[39mperf_counter()\n\u001b[1;32m 970\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[1;32m 971\u001b[0m outputs \u001b[38;5;241m=\u001b[39m (\n\u001b[0;32m--> 972\u001b[0m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mvm\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 973\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m output_subset \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m\n\u001b[1;32m 974\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mvm(output_subset\u001b[38;5;241m=\u001b[39moutput_subset)\n\u001b[1;32m 975\u001b[0m )\n\u001b[1;32m 976\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mException\u001b[39;00m:\n\u001b[1;32m 977\u001b[0m restore_defaults()\n", | |
"File \u001b[0;32m~/mambaforge/envs/econ/lib/python3.9/site-packages/pytensor/graph/op.py:542\u001b[0m, in \u001b[0;36mOp.make_py_thunk.<locals>.rval\u001b[0;34m(p, i, o, n, params)\u001b[0m\n\u001b[1;32m 538\u001b[0m \u001b[38;5;129m@is_thunk_type\u001b[39m\n\u001b[1;32m 539\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mrval\u001b[39m(\n\u001b[1;32m 540\u001b[0m p\u001b[38;5;241m=\u001b[39mp, i\u001b[38;5;241m=\u001b[39mnode_input_storage, o\u001b[38;5;241m=\u001b[39mnode_output_storage, n\u001b[38;5;241m=\u001b[39mnode, params\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mNone\u001b[39;00m\n\u001b[1;32m 541\u001b[0m ):\n\u001b[0;32m--> 542\u001b[0m r \u001b[38;5;241m=\u001b[39m \u001b[43mp\u001b[49m\u001b[43m(\u001b[49m\u001b[43mn\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m[\u001b[49m\u001b[43mx\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m0\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mfor\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mx\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;129;43;01min\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mi\u001b[49m\u001b[43m]\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mo\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 543\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m o \u001b[38;5;129;01min\u001b[39;00m node\u001b[38;5;241m.\u001b[39moutputs:\n\u001b[1;32m 544\u001b[0m compute_map[o][\u001b[38;5;241m0\u001b[39m] \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mTrue\u001b[39;00m\n", | |
"File \u001b[0;32m~/mambaforge/envs/econ/lib/python3.9/site-packages/pytensor/tensor/random/op.py:379\u001b[0m, in \u001b[0;36mRandomVariable.perform\u001b[0;34m(self, node, inputs, outputs)\u001b[0m\n\u001b[1;32m 375\u001b[0m rng \u001b[38;5;241m=\u001b[39m copy(rng)\n\u001b[1;32m 377\u001b[0m rng_var_out[\u001b[38;5;241m0\u001b[39m] \u001b[38;5;241m=\u001b[39m rng\n\u001b[0;32m--> 379\u001b[0m smpl_val \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mrng_fn\u001b[49m\u001b[43m(\u001b[49m\u001b[43mrng\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43margs\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m+\u001b[39;49m\u001b[43m \u001b[49m\u001b[43m[\u001b[49m\u001b[43msize\u001b[49m\u001b[43m]\u001b[49m\u001b[43m)\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 381\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m (\n\u001b[1;32m 382\u001b[0m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(smpl_val, np\u001b[38;5;241m.\u001b[39mndarray)\n\u001b[1;32m 383\u001b[0m \u001b[38;5;129;01mor\u001b[39;00m \u001b[38;5;28mstr\u001b[39m(smpl_val\u001b[38;5;241m.\u001b[39mdtype) \u001b[38;5;241m!=\u001b[39m out_var\u001b[38;5;241m.\u001b[39mtype\u001b[38;5;241m.\u001b[39mdtype\n\u001b[1;32m 384\u001b[0m ):\n\u001b[1;32m 385\u001b[0m smpl_val \u001b[38;5;241m=\u001b[39m _asarray(smpl_val, dtype\u001b[38;5;241m=\u001b[39mout_var\u001b[38;5;241m.\u001b[39mtype\u001b[38;5;241m.\u001b[39mdtype)\n", | |
"File \u001b[0;32m~/mambaforge/envs/econ/lib/python3.9/site-packages/pytensor/tensor/random/op.py:164\u001b[0m, in \u001b[0;36mRandomVariable.rng_fn\u001b[0;34m(self, rng, *args, **kwargs)\u001b[0m\n\u001b[1;32m 162\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mrng_fn\u001b[39m(\u001b[38;5;28mself\u001b[39m, rng, \u001b[38;5;241m*\u001b[39margs, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m Union[\u001b[38;5;28mint\u001b[39m, \u001b[38;5;28mfloat\u001b[39m, np\u001b[38;5;241m.\u001b[39mndarray]:\n\u001b[1;32m 163\u001b[0m \u001b[38;5;124;03m\"\"\"Sample a numeric random variate.\"\"\"\u001b[39;00m\n\u001b[0;32m--> 164\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mgetattr\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43mrng\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mname\u001b[49m\u001b[43m)\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n", | |
"File \u001b[0;32m_generator.pyx:1136\u001b[0m, in \u001b[0;36mnumpy.random._generator.Generator.normal\u001b[0;34m()\u001b[0m\n", | |
"File \u001b[0;32m_common.pyx:600\u001b[0m, in \u001b[0;36mnumpy.random._common.cont\u001b[0;34m()\u001b[0m\n", | |
"File \u001b[0;32m_common.pyx:517\u001b[0m, in \u001b[0;36mnumpy.random._common.cont_broadcast_2\u001b[0;34m()\u001b[0m\n", | |
"File \u001b[0;32m__init__.pxd:741\u001b[0m, in \u001b[0;36mnumpy.PyArray_MultiIterNew3\u001b[0;34m()\u001b[0m\n", | |
"\u001b[0;31mValueError\u001b[0m: shape mismatch: objects cannot be broadcast to a single shape. Mismatch is between arg 0 with shape (6, 10) and arg 1 with shape (6,).\nApply node that caused the error: normal_rv{0, (0, 0), floatX, True}(RandomGeneratorSharedVariable(<Generator(PCG64) at 0x12D533580>), TensorConstant{[ 6 10]}, TensorConstant{11}, mu, sigma)\nToposort index: 2\nInputs types: [RandomGeneratorType, TensorType(int64, (2,)), TensorType(int64, ()), TensorType(float64, (6,)), TensorType(float64, (6,))]\nInputs shapes: ['No shapes', (2,), (), (6,), (6,)]\nInputs strides: ['No strides', (8,), (), (8,), (8,)]\nInputs values: [Generator(PCG64) at 0x12D533580, array([ 6, 10]), array(11), 'not shown', 'not shown']\nOutputs clients: [['output'], ['output']]\n\nHINT: Re-running with most PyTensor optimizations disabled could provide a back-trace showing when this node was created. This can be done by setting the PyTensor flag 'optimizer=fast_compile'. If that does not work, PyTensor optimizations can be disabled with 'optimizer=None'.\nHINT: Use the PyTensor flag `exception_verbosity=high` for a debug print-out and storage map footprint of this Apply node." | |
] | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "", | |
"execution_count": null, | |
"outputs": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"name": "python3", | |
"display_name": "Python 3 (ipykernel)", | |
"language": "python" | |
}, | |
"toc": { | |
"nav_menu": {}, | |
"number_sections": true, | |
"sideBar": true, | |
"skip_h1_title": false, | |
"base_numbering": 1, | |
"title_cell": "Table of Contents", | |
"title_sidebar": "Contents", | |
"toc_cell": false, | |
"toc_position": {}, | |
"toc_section_display": true, | |
"toc_window_display": false | |
}, | |
"language_info": { | |
"name": "python", | |
"version": "3.9.15", | |
"mimetype": "text/x-python", | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"pygments_lexer": "ipython3", | |
"nbconvert_exporter": "python", | |
"file_extension": ".py" | |
}, | |
"gist": { | |
"id": "ff04ea88a77aaae128cab1c04553855a", | |
"data": { | |
"description": "Shapes, Dims, and Coords in PyMC", | |
"public": true | |
} | |
}, | |
"_draft": { | |
"nbviewer_url": "https://gist.github.com/ff04ea88a77aaae128cab1c04553855a" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 5 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment