concept sample in category probabilistic programming

This is an excerpt from Manning's book Practical Probabilistic Programming.
This process goes by various names, including monitoring, state estimation, and filtering. All are different names for the same thing. Algorithms for achieving this process are often called filtering algorithms, and this is the name used in Figaro. Probably the most popular filtering algorithm is called particle filtering. This is a sampling algorithm that represents the distribution over the state of the system at each time point by using a set of samples or “particles.” Details about the particle-filtering algorithm can be found in chapter 12.
Importance sampling is similar to rejection sampling. In fact, when importance sampling encounters a condition that isn’t satisfied, it rejects the sample, just like rejection sampling. But two major differences exist. First, recall that in addition to conditions, Figaro provides constraints that aren’t just true or false but instead assign a real number to each state. Rejection sampling is unable to handle constraints like this because a sample won’t be completely inconsistent with the evidence, but will just have lower probability. Importance sampling is able to handle constraints, which don’t result in rejection. Second, in the right circumstances, importance sampling can avoid rejecting with conditions by turning the conditions into constraints on other variables.
Using importance sampling in Figaro is straightforward. Figaro provides both a one-time version of importance sampling that you run with a given number of samples and an anytime version that you can run as long as you like and get answers to queries while it’s running. To use them, import com.cra.figaro.algorithm.sampling.Importance. The following code runs the one-time importance sampling algorithm with 10,000 samples on the query targets rembrandt1 and rembrandt2 and then issues queries on the result:
val algorithm = Importance(10000, rembrandt1, rembrandt2) algorithm.start() println(algorithm.probability(rembrandt1, true)) println(algorithm.distribution(rembrandt2).toList) algorithm.kill()The anytime version of importance sampling collects more and more samples the longer it runs, leading to better and better answers to queries. To specify that you want to use the anytime version, you don’t pass it an integer argument for the number of samples, just the query targets. You would typically do something else while the algorithm is running. A simple way to wait for a period of time is to call Thread.sleep, which waits for the given number of milliseconds before continuing.
Sampling algorithms are an alternative to factored algorithms for probabilistic inference. The basic principle behind sampling is straightforward: rather than representing the probability distribution over possible worlds as a set of numbers, you use a set of examples of possible worlds, called samples.
The essential idea of sampling is illustrated in figure 11.1. In this example, a single variable has the possible values small, medium, and large. The possible worlds consist of the possible values of this variable. There’s some true probability distribution over possible worlds, shown in the top row. This true distribution might be the answer to a query, and is in general unknown. Rather than computing the distribution directly, you generate a set of samples. Each sample is a possible world, and the probability of generating a particular possible world should be equal to the probability of that possible world. In the figure, we’ve generated the following:
Figure 11.1. The sampling principle. There’s an unknown true probability distribution that you want to compute. To estimate this distribution, you generate a set of samples. The probability of generating a particular value should equal the true probability of that value. After you have a set of samples, you can estimate the probability of a value by seeing how many samples have that value.
![]()