# Forward Sampling

Learn the basics of forward sampling and how we can prepare the trained QBN.

## We'll cover the following

In the previous chapter Bayesian Inference, we learned how to apply a **variational method** to learn a hidden variable. Variational methods approximate the distribution of a hidden variable analytically.

Sampling-based methods work differently. Instead of calculating the hidden distribution, they approximate it empirically. The principle is straightforward. These methods repeatedly select an instance and calculate the discrete value of the hidden variable for this instance. Let’s say we have a coin, and we want to determine the probability of getting heads. We do not analyze the texture and structure of the coin. We toss it over and over again and count the times it lands heads up. Having tossed it a hundred times, we got quite a good approximation of the probability of getting heads.

Sampling builds upon simulation. We do not necessarily select real instances from our data, but we generate random instances based on our data.

In our QBN, each variable has a probability table (the CPT.) We start with the variables that have no parents. Their CPTs denote the marginal (without conditions) probability of the respective values. We use this probability to assign a value to a new instance.

We continue with the child variables. Their probability tables tell us how likely the variable takes on a particular value given the values of its parents. Since our instance already has a value for the parent variable, we can assign a value for the child variable. A single instance is not representative of our data. It could be a very unlikely case that we would only observe one in a million times. Therefore, we’ll repeat creating instances many times. The higher the number of instances, the closer our generated population of instances represents the probabilities given by our probability tables. We get a good estimation of the probability a variable takes on. This method is known as **forward sampling**.

We already did this when we used our QBN to calculate the marginal probability to survive the Titanic shipwreck.

Get hands-on with 1200+ tech skills courses.