Tips for generating bayesian probability

Hi, I’ve been working on a digitized version of the game Avalon, in order to improve my scripting prowess. If you’re unfamiliar with the game, it’s a hidden identity game (somewhat similar to werewolf or mafia), where players go on missions, and players on the evil team may choose to secretly fail the mission. Some players have limited information, but most information is gained by the results of these missions (i.e. if players x and y go on a mission, and it fails, P(X||Y) = 1, where P(X) is the probability that x is a spy, thus you can determine the new probability that x is a spy with P(X| X||Y) = P(X||Y | X)*P(X)/P(X||Y), through bayes rule).

In the process of scripting this game, I’ve been working most notably on the basics of a rudimentary AI, which will track the likelihood of each other player’s identity. My previous thought process was to have a Dictionary, keyed by a List that consists of a subset of players, that yields an array of floats corresponding to probabilities that a given number of players within that subset are evil (i.e. given Dictionary<List,float[ ]> d, List l234, which contains Players 2, 3 and 4, the value of d[l234][2] would be the probability that there are two spies within the subset of Players 2, 3, and 4). However, as I continued working, I got stuck when I had to deal with more complicated probabilities, such as when I had to derive values based on combinations of "and"ed and "or"ed subsets and subsets where some members of the subset were "not"ed, e.g. P(X| !Y&&Z).

I figure that there are better methods of calculating these probabilities, that make it easier to solve this, but I’ve been mostly teaching myself scripting, so I don’t know all the techniques. Thank you in advance for any help you could provide

I would use a bitset as an index into a 2D array to build up a table of all possible values. This would end up being of size 2^n*(n+1) So for 8 players that is only 2.3k floats. 10 is 12k floats. This system would be unfeasible quickly with larger number of players (for exmaple 30 players would require 33 trillion floats).

The idea is each player has a 0 based indexed ID and we create lists of these int IDs and convert them into an index

int maxPlayer =10;
float[] EvilProb = new float[(int)Math.Pow(2,maxPlayer),maxPlayer+1];

public int GetIndexFromList(List<int> ids)
{
    int index =0;
    foreach(int val in ids)
          index+=1<<val;
    return val;
}    

public List<int> GetListFromIndex(index)
{
      List<int> ids = new List<int>();
      for (int i=0;i<maxPlayer;++i)
             if (  (index&(1<<i)) > 0)
                List.Add(i);
      return ids;
}

The idea being that EvilProb[Index of SomeList][1] is the probability that 1 person from that list is evil. So the base probablitiy of just Player 2 being evil would be:

List<int> id = new List<int>();
id.Add(2);
int index = GetIndexFromList(id);
EvilProb[index][1] = Prob(2)IsEvil;
EvilProb[index][0] = 1-EvilProb[index][1];

So you have all your P(x) = evil and P(x) = not evil in those. You can calculate and store any other probability using this stystem from those base probabilities.

So if you needed to store that out of 2,3,4,6 there was .6 chance 3 of them were evil:

List<int> ids = new List<int>();
ids.Add(2);
ids.Add(3);
ids.Add(4);
ids.Add(6);
int index = GetIndexFromList(ids);
EvilProb[index][3] = .6;

Note: EvilProb[anyIndex][0] will always be the chance that none of those IDs are evil.
I added GetListFromIndex for completeness sake. I can’t imagine in this program you ever knowing an index and needing the list from it.