With direct sampling, our goal is to approximate the full joint probability through a sequence of samples drawn from each conditional distribution. If we assume that the graph is well-structured (without unnecessary edges) and we have N variables, the algorithm is made up of the following steps:
- Initialize the variable NSamples.
- Initialize a vector S with shape (N, NSamples).
- Initialize a frequency vector FSamples with shape (N, NSamples). In Python, it's better to employ a dictionary where the key is a combination (x1, x2, x3, ..., xN).
- For t=1 to NSamples:
- For i=1 to N:
- Sample from P(Xi|Predecessors(Xi))
- Store the sample in S[i, t]
- If FSamples contains the sampled tuple S[:, t]:
- FSamples[S[:, t]] += 1
- Else:
- FSamples ...
- For i=1 to N: