concept word in category probabilistic programming

This is an excerpt from Manning's book Practical Probabilistic Programming.
The dependencies between the elements. For example, an element indicating the presence of a particular word depends on the element representing whether the email is spam. Remember that in a probabilistic model, the direction of the dependencies isn’t necessarily the direction of reasoning. You use the words to determine whether the email is spam, but in the model, you can think of the email sender as first deciding what kind of email to send (“I’m going to send you a spam email”) and then deciding what words go in it. You typically model dependencies in the causal direction. Because the kind of email causes the words, the dependency goes from the element representing whether the email is spam to each of the words. Determining the dependencies in the spam filter model is discussed in more detail in section 3.4.2. The functional forms of these dependencies. The functional form of an element indicates what type of element it is, such as an If, an atomic Binomial, or a compound Normal. The functional form doesn’t specify the parameters of the element. For example, the preceding dependency takes the form of an If with two Flips: if the email is spam, then the word rich is present with one probability; otherwise, it’s present with some other probability. The functional forms for the spam filter model are defined in section 3.4.3. These first three constituents, which make up the process in figure 3.5, are what’s assumed by the spam filter application to be known before learning. They’re defined by the designer of the application. Together, they constitute the structure of the email model. The numerical parameters of the model, shown as Parameters in figure 3.5. For example, the probability that an email is spam is one parameter. The probability that the word rich is present if the email is spam is another parameter, and the probability that rich is present if the email isn’t spam is a third parameter. The parameters are separated out from the process because the parameters will be learned from training data. The parameters of the email model are discussed in section 3.4.4. The knowledge. Figure 3.5 shows the auxiliary knowledge used in constructing the model and applying evidence to it in a particular situation. I use the term “knowledge” for anything learned from the training data that isn’t specifically related to the elements, their dependencies, functional forms, or numerical parameters. In our spam filter application, this knowledge will consist of a dictionary of the words that appear in the training emails along with the number of times each word appears. For example, you need the knowledge to tell you that the word rich is one you’re interested in. As a result, the knowledge helps define the parameters of the model; the probability that rich appears if the email is spam is a parameter only if rich is a word you’re interested in. The auxiliary knowledge is discussed further in section 3.4.5.
To start, let’s define the dependency model for the isSpam element and all of the word elements. When defining the dependencies of a probabilistic model, a common rule of thumb is that the class of an object determines the properties of the object, meaning that the properties depend on the class in the dependency model. In our example, whether an email is spam determines the presence or absence of all the words.
I made this point earlier in the chapter, but it bears repeating, because it’s important and people often get confused by it. You’re using the words to determine whether the email is spam. So why am I saying that the class of the email determines the words, and not vice versa? The key point to remember is that in probabilistic reasoning, the direction of inference isn’t necessarily the direction of dependencies in the model. As often as not, the direction of inference is the opposite of the direction of the dependencies, as you try to determine the factors that caused an observed outcome. Here, you’re modeling the intrinsic nature of the email (whether it’s spam or normal) as the unobserved factor that causes the observed words. So the dependency in the model goes from the class of the email to the words.
That being given, one thing you need to decide is whether you’re going to model dependencies between the words. In English or any language, the choice of the first word of a sentence is closely related to the second word. The words aren’t, in reality, independent. But to keep this chapter relatively simple, I’ll make the assumption that the words are independent, after the class (normal or spam) of the email is given.
You can draw a diagram called a Bayesian network that shows the dependencies in the model. Bayesian networks are explained in detail in chapter 5. For now, the main point is that an edge exists in the network from one node to another if the second node depends on the first. For our spam filter so far, you get the Bayesian network shown in figure 3.7. This kind of network is called a Naïve Bayes’ model—naïve because it makes the assumption that the words are independent.
Now let’s introduce the elements for unusual words. Remember that you have two elements: hasManyUnusualWords and numUnusualWords. hasManyUnusualWords characterizes whether the words in this email tend to be unusual, whereas numUnusualWords is the number. Because an email with unusual words tends to have more unusual words than an email that doesn’t, it’s natural that numUnusualWords should depend on hasManyUnusualWords. In turn, hasManyUnusualWords, which is a property of the email, depends on isSpam, which represents the class of the email. Finally, continuing with the Naïve Bayes’ assumption, you assume that the presence of unusual words is independent from the presence of any given particular word, after the class is known. You end up with the Bayesian network in figure 3.8. This is the final dependency model for your spam filter application.
numUnusualWords —This Integer element represents the number of unusual words in an email. The more words in an email, the more unusual words it’s likely to have. You model each individual word as having some probability of being unusual. The natural way to represent this is with a binomial distribution in which the number of trials is equal to the total number of words in the email. But the number of words in an email could be more than 1,000, and when the number of trials is that large, many values of numUnusualWords will have extremely low probability. So the probability of an email can be extremely small. This can lead to underflow errors when you have lots of emails: probabilities get rounded to 0 because they’re too small to represent on a computer. To get around this problem, you set the number of trials to a fixed number, such as 20. This number is represented in the code as the constant Model.binomialNumTrials. Then, the number of unusual words in an email is scaled down so that it represents a fraction out of 20. You’ll take care of this when you apply evidence later. Using this method, the definition of numUnusualWords is given by the following: val numUnusualIfHasMany = Binomial(Model.binomialNumTrials, parameters.unusualWordGivenManyProbability), val numUnusualIfHasFew = Binomial(Model.binomialNumTrials, parameters.unusualWordGivenFewProbability), val numUnusualWords = If(hasManyUnusualWords, numUnusualIfHasMany, numUnusualIfHasFew)
Particular words in the header or body of the email. For example, the word rich might be more likely in a spam email than a normal email. Misspellings and other unusual words. Spam email often has misspelled words or words that don’t appear in other emails.