
上QQ阅读APP看书,第一时间看更新
Direct sampling
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[S[:, t]] = 1 (both these operations are immediate with Python dictionaries)
- For i=1 to N:
- Create a vector PSampled with shape (N, 1).
- Set PSampled[i, 0] = FSamples[i]/N.
From a mathematical viewpoint, we are first creating a frequency vector FSamples(x1, x2, x3, ..., xN; NSamples) and then we approximate the full joint probability considering NSamples → ∞:
