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:

  1. Initialize the variable NSamples.
  2. Initialize a vector S with shape (N, NSamples).
  3. 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).
  4. For t=1 to NSamples:
    1. For i=1 to N:
      1. Sample from P(Xi|Predecessors(Xi))
      2. Store the sample in S[i, t]
    2. If FSamples contains the sampled tuple S[:, t]:
      1. FSamples[S[:, t]] += 1
    3. Else:
      1. FSamples ...

Get Mastering Machine Learning Algorithms now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.