# Conditional CategoricalDistributions

**URL:** <https://community.wolfram.com/t/conditional-categoricaldistributions/15977>\
**Category:** Questions\
**Tags:** data-science, mathematica, statistics-and-probability\
**Created:** [May 12, 2020, 4:57pm UTC](https://community.wolfram.com/t/conditional-categoricaldistributions/15977 "2020-05-12T16:57:22Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![schandler](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/schandler/32/38011_2.png) [@schandler](https://community.wolfram.com/u/schandler)\
**Post date:** [May 12, 2020, 4:57pm UTC](https://community.wolfram.com/t/conditional-categoricaldistributions/15977/1 "2020-05-12T16:57:22Z")

</div>

I want to take a CategoricalDistribution, impose a condition, and get a new CategoricalDistribution out. Here’s a notebook with an ugly solution that probably does not generalize well. Can someone come up with something better?

[Open in Wolfram Cloud ↗](https://www.wolframcloud.com/obj/e9798f22-a3a9-4fd8-89fc-8163e7dfe8d5)

---

<div class="post-metadata">

**Author:** ![schandler](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/schandler/32/38011_2.png) [@schandler](https://community.wolfram.com/u/schandler)\
**Post date:** [May 12, 2020, 5:26pm UTC](https://community.wolfram.com/t/conditional-categoricaldistributions/15977/2 "2020-05-12T17:26:29Z")

</div>

We could make the code a little more general this way:

```
 conditionalDistribution[cd_CategoricalDistribution, outerlevel_] := 
 With[{queryElements = 
 PadRight[{outerlevel}, Length[Information[cd, "Categories"]], 
 Values]}, 
 CategoricalDistribution[Rest@Information[cd, "Categories"], 
 Normal@(Query @@ queryElements)[
 Information[cd, "ProbabilityTable"]]]
 ]

```

But that still seems suboptimal.

---

<div class="post-metadata">

**Author:** ![jbaldwinpsw](https://avatars.discourse-cdn.com/v4/letter/j/ee7513/32.png) [@jbaldwinpsw](https://community.wolfram.com/u/jbaldwinpsw)\
**Post date:** [May 13, 2020, 1:09am UTC](https://community.wolfram.com/t/conditional-categoricaldistributions/15977/3 "2020-05-13T01:09:12Z")

</div>

If all of the category values are all unique (as in your example), the following might suggest a more general approach:

```
cd = CategoricalDistribution[{{"A", "B", "C"}, {"\[Alpha]", "\[Beta]"},
   {"\[HappySmiley]", "\[SadSmiley]"}}, 
   {{{6, 84}, {8, 48}}, {{63, 91}, {88, 78}}, {{34, 72}, {74, 55}}}];

conditional[dist_, cValues_] := CategoricalDistribution[
  Select[Information[dist, "Probabilities"] // Normal, ContainsAll[#[[1]], cValues] &]];

cd1 = conditional[cd, {#}] & /@ Information[cd, "Categories"][[1]];
Information[#, "ProbabilityTable"] & /@ cd1

```

 ![conditionalprobabilitytables.png](https://global.discourse-cdn.com/wolfram/original/3X/3/b/3bda9048c312b16e1dd157d3fbc93972f75e2492.png)

This also works for conditioning on multiple values (from different variables):

```
Information[conditional[cd, {"A", "\[Alpha]"}], "ProbabilityTable"]

```

![2wayconditional.png](https://global.discourse-cdn.com/wolfram/original/3X/9/2/926688c4a06f50bd80562a62ec83cc510b9f3c01.png)

---

<div class="post-metadata">

**Author:** ![schandler](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/schandler/32/38011_2.png) [@schandler](https://community.wolfram.com/u/schandler)\
**Post date:** [May 13, 2020, 5:10pm UTC](https://community.wolfram.com/t/conditional-categoricaldistributions/15977/4 "2020-05-13T17:10:35Z")

</div>

I came up with an idea that is more general and permits some fairly natural representations of the conditions. I plan on making this into a ResourceFunction. Comments welcome. Here’s a notebook containing the idea.

[Open in Wolfram Cloud ↗](https://www.wolframcloud.com/obj/6c861932-70a3-4a4c-a8bc-42e21a84d746)
