Skip to content

Commit f075e33

Browse files
Abhishek.6122008Abhishek.6122008
authored andcommitted
added an optional cell block for extending the pattern to more than two levels
1 parent 1b79b1c commit f075e33

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

docs/source/learn/core_notebooks/vector_variables.ipynb

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -893,6 +893,43 @@
893893
"Once the mapping is in place, the rest of the model looks like any other hierarchical setup.\n"
894894
]
895895
},
896+
{
897+
"cell_type": "markdown",
898+
"id": "1be8ea9b",
899+
"metadata": {},
900+
"source": [
901+
"## Optional: Extending this pattern to more than two levels\n",
902+
"\n",
903+
"This example uses two levels (category and family), but the same idea works for any number of levels. \n",
904+
"The key ingredients stay the same:\n",
905+
"\n",
906+
"1. factorize each level of labels into integer codes \n",
907+
"2. build a mapping array from each level to the one above it \n",
908+
"3. use that mapping to index the parent vector inside the next level’s prior\n",
909+
"\n",
910+
"For example, if you had three levels:\n",
911+
"\n",
912+
"- level_0\n",
913+
"- level_1\n",
914+
"- level_2\n",
915+
"\n",
916+
"You would create the following:\n",
917+
"\n",
918+
"- level_1_to_level_0\n",
919+
"- level_2_to_level_1\n",
920+
"\n",
921+
"Then you would define priors like this:\n",
922+
"\n",
923+
"- level_0_effect\n",
924+
"- level_1_effect, centered on level_0_effect[level_1_to_level_0]\n",
925+
"- level_2_effect, centered on level_1_effect[level_2_to_level_1]\n",
926+
"\n",
927+
"Each additional level only requires one more factorized index and one more mapping array. \n",
928+
"Nothing else about the PyMC model needs to change.\n",
929+
"\n",
930+
"This is what people sometimes call a telescoping hierarchy: every group is centered on the group above it, and the indexing arrays connect the levels together.\n"
931+
]
932+
},
896933
{
897934
"cell_type": "markdown",
898935
"id": "39c4c0af",

0 commit comments

Comments
 (0)