Related link :
Classification by Basic Bayes Probability Program Page
Classification by Naive Bayes Probability Program Page
Introduction
Basic Bayes
Naive Bayes
Discussions
E
References
G
This page provides explanation and support for the two programs in Classification by Basic Bayes Probability Program Page
and
Classification by Naive Bayes Probability Program Page
. As the programs and this explanation page use specific terms and abbreviations, and these are best demonstrated with examples, this introduction panel will describe the example used and the terminology.
The format of data entry and explanation of results produced are in the Help and Hints panel of the program pages.
The contents of the other panels of this page are
The remainder of this panel provides a description of the example data used in this and the two program pages, and brief explanations of the overall concepts of Bayesean probability and the terms used in these pages.
Before we start: Modern computer perform calculations with precision to 14 decimal points. The two programs associated with this page display results with precision to 4 decimal points. On this page, to conserve space and make reading easier, probability values are displayed to 2 decimal points. Minor differences to the results from the program may occasionally arise, and some of the probabilities do not total to 1. Reader should be aware and not be confused by this.
The Example
The same example is used in the two programming pages and this explaination page.
Predictor  Outcome 
Hair  Eye  French  German  Italian 
Dark  Blue  3  1  2 
Dark  Brown  1  1  3 
Dark  Others  1  1  1 
Light  Blue  2  1  1 
Light  Brown  2  1  2 
Light  Others  1  5  1 
a priori  0.5  0.33  0.17 
cost  0.25  0.5  0.25 
We wish to develop a Bayesean model to identify the ethnicity of people, based on hair color and eye color. To build our model, we recruited 10 each of known French, German, and Italians, and observed their hair and eye color. We then use the Bayesean model to predict ethnicity using hair and eye colors, in a community with an expected ratios of French:German:Italian of 3:2:1, normalized to a priori probabilities of 0.5:0.33:0.17. In addition, we inserted a cost adjustment to our prediction, as cost ratios of 1:2:1 for French:German:Italian, normalized to cost coefficients of 0.25:0.5:0.25.
The count of each combinations and the coefficients are presented in the table to the right, and the explanation of terms and abbreviations used are as follows
Bayesean Probability
Bayesean Probability Theory is a mathematical model of making decisions based on experience. The process is to predict, using a set of predictors to determine the probabilities of alternative outcomes. In the Bayesean context, prediction is not to forecast the future, nor to establish what may be true, but to logically apply the observed values of predictors to calculate how confident we can be, in terms of probabilities (a number between 0 and 1, or a percentage), for each of the alternative outcomes contained in our model.
The process of Bayesean decisions can be separated into the following stages
 We begin by nominating the a priori probabilities (π), our confidence in believing each of the alternative outcomes to be correct, before taking predictors into consideration. This can be established by the following
 We can declare that we do not know, and assign the same value as a priori probabilities to all outcomes
 We can base the a priori probabilities on knowledge, from experience, research, previously collected data, heresay, cultural belief, or simply a guess
 We can propose a priori probabilities as a hypothesis to explore, such as "if the a priori probabilities are ...., then ....."
 From our example, in the community we will use our Bayesean model, Census informs us that the ratio of French:German:Italian are 3:2:1. These are normalized to probabilities by dividing each value by the total to 0.5:0.33:0.17
 We then use the coefficients of our model to apply the attributes of predictors to change a priori probabilities to a posteriori probabilities. The coefficients are developed using a set of reference data, in our example, 10 cases of each ethnicity. Each coefficient is the probability of seeing an attribute given the outcome P(ao), obtained by dividing the number of cases with each pair of attribute/outcome by the sample size of that outcome in the reference data. Both the Basic and Naive Bayes model use P(ao) as coefficients, but they are calculated, presented, and used differently. Details of this are presented in the 2 subsequent panels.
 The coefficients P(ao) interacts with the alternatives in the predictor(s) to estimate the a posteriori probability. This is term the a posteriori probability, and commonly referred to as the Bayesian probability
 When there is only 1 predictor, as in the Basic Bayes model, attribute (a) represents each alternative of the predictor, and the Bayesian probability is probability given attribute πP(oa)
 When there are more than 1 predictor, as in the Naive Bayes model, pattern (p) represents an array of attributes, one from each predictor, and the Bayesian probability is probability given pattern πP(op)
 Under some circumstances, we may in addition impose a cost adjustment on our decisions, if we hold that the outcomes have different values CπP(oa) or CπP(op). For example, headache may predict anxiety or brain tumour, but missing a brain tumour has far graver consequences than missing anxiety, so we can insert a cost coefficient to bias our decisions towards brain tumour. The term cost refers to the cost of wrongly not identify a particular outcome, reflecting the importance of that outcome. The process insert a deliberate, considered and calibrated bias to our decisions.
 Three types of a posteriori probability can therefore be calculated using the coefficients we developed
 Probability of outcome using only the predictor(s), without taking a priori probability or cost into consideration. In the Basic Bayes model with 1 predictor, this is probability given attribute P(oa), and in the Naive Bayes model with multiple predictors probability given pattern P(op). This probability is also termed Maximum Likelihood, and the table of Maximum Likelihood describes the behaviour of the model.
 Probability of outcome using the predictor(s) and the a priori probabilities π. In the Basic Bayes model with 1 predictor, this is probability given attribute and a priori probability πP(oa), and in the Naive Bayes model with multiple predictors probability given pattern and a priori probability πP(op). This probability is also termed Bayes or Bayesean Probability, and is the major and most commonly used a posteriori probability.
 Probability of outcome using the predictor(s), the a priori probabilities π, and the costs C. In the Basic Bayes model with 1 predictor, this is probability given attribute, a priori probability, and cost CπP(oa), and in the Naive Bayes model with multiple predictors cost adjusted probability given pattern, a priori probability, and cost CπP(op). Both are abbreviated to Cost Adjusted Bayesean Probability, and used for decision making where a value judgement is included.
Summary and Technical Notes
The terminology and abbreviations used in this page and the two associated program pages are adapted from diverse sources, and may not be the same as in other publications. Users should be aware of this peculiarity when comparing these pages with other sources of information. These are chosen to prefer clarity over brevity, hoping that, by doing so, the inexperienced will be less confused. In particular, the following should be noted.
 Predictor is a conceptual term representing things used to predict. and no abbreviation is provided in these pages. In other publications, a variety of terms and abbreviations, such as independent variable, x, j, are used
 Attribute is the value of a predictor, and abbreviated as a. In other publication, predictor, independent variable, x, j, and so on are used
 Pattern is an array of attributes, and abbreviated as p, one from each predictor, and is used only in the Naive Bayes model. In other publication, predictor, independent variable, x, j, and so on are used
 Outcome is used bothe as a concept of things to predict, and also as the values (probability) predicted, and is abreviated as o.In other publication, dependent variable, a posteriori, posterior probability, y, z, θ are used
 The abbreviation P(xy), representing the probability of x given y, generally known as conditional probability, is the same in these pages as in most publications. However, in most publications, the same abbreviations are used (with different letters) to represent different types of conditional probabilities, while in these pages
 P(ao) and P(po) represent probability of attribute or pattern given outcome. Other publications use P(xy), P(xθ), or names of predictors and outcomes
 P(oa) and P(oa) represent probability of outcome given attribute or pattern, without consideration of a priori or cost. Other publications use P(yx), P(θx), or names of predictors and outcomes This represents Maximum Likelihood, a term used in these pages as in most publications
 πP(oa) and πP(oa) represent Bayesean Probability, with π representing a priori probability. The term is an old one (see references), and used in these pages to distinguish it from Maximum Likelihood. In most publications the same abbreviation as Maximum Likelihood is used, and what the abbreviation means depends on the context described.
 CπP(oa) and CπP(oa) represent Bayesean Probability with cost adjustment. The concept and term are old (see references), and it is difficult to find reference to this in more recent publications. This is included in these pages in case any user should wish to use it, but in most cases, costs need not be set.
This panel discusses the basic Bayes model generally, and the calculations used in the Classification by Basic Bayes Probability Program Page
.
Table B1: Counts
Attributes  Outcome 
 French  German  Italian 
DarkBlue  3  1  2 
DarkBrown  1  1  3 
DarkOthers  1  1  1 
LightBlue  2  1  1 
LightBrown  2  1  2 
LightOthers  1  5  1 
a priori π  0.5  0.33  0.17 
cost  0.25  0.5  0.25 
Basic Bayes is used to represent the Bayes Probability model, as originally described by Bayes and in the Wikipedia page on Bayes Theorem. The term "Basic" is used to avoid confusion with the Naive Bayes model, and is not applicable outside of StatsToDo.
The model uses a single predictor with 2 or more attributes to predict the probabilities of 2 or more outcomes. Where more than one variable are used to predict, they are combined into a single compound predictor. In the default example in the Classification by Basic Bayes Probability Program Page
, the data presented in the Introduction panel are restructured, so that the two variables, hair color (2 attributes of Dark and Light) and eye colors (3 attributes of Blue, Brown and Others) are combined into a single compound predictor of HairEye, with 2x3=6 attributes of DarkBlue, DarkBrown, DarkOthers, LightBlue, LightBrown, and LightOthers. The restructured table is used in the program, and as shown to the right.
Building the model: Attribute (a): Compound(Hair and eye color), Outcome (o):ethnicity
Table B2. Model Coefficients P(ao)
Attributes  Outcome 
 French  German  Italian  Total 
DarkBlue  3/10=0.3  1/10=0.1  2/10=0.2  0.3+0.1+0.2=0.6 
DarkBrown  1/10=0.1  1/10=0.1  3/10=0.3  0.1+0.1+0.3=0.5 
DarkOthers  1/10=0.1  1/10=0.1  1/10=0.1  0.1+0.1+0.1=0.3 
LightBlue  2/10=0.2  1/10=0.1  1/10=0.1  0.2+0.1+0.1=0.4 
LightBrown  2/10=0.2  1/10=0.1  2/10=0.2  0.2+0.1+0.2=0.5 
LightOthers  1/10=0.1  5/10=0.5  1/10=0.1  0.1+0.5+0.1=0.7 
The coefficients of the model, to be used to convert a priori to a posteriori probability, is the probability of attribute given outcome P(ao). For the i ^{th} attribute and the j ^{th} outcome, P(ao) is calculated by dividing the number of the attribute/outcome pair (N _{i,j}) by the sample size of that outcome (N _{j}).
P(a_{i}o_{j}) = N_{i,j}/N_{j}
In this example, the attributes are the hair eye color combinations, and the outcomes ethnicity. The results are shown in Table B2 to the right.
Prediction 1. Maximum Likelihood P(oa)
Table B3. Maximum Likelihood P(oa)
Attribute  Outcome 
 French  German  Italian 
DarkBlue  0.3/0.6=0.5  0.1/0.6=0.17  0.2/0.6=0.33 
DarkBrown  0.1/0.5=0.2  0.1/0.5=0.2  0.3/0.5=0.6 
DarkOthers  0.1/0.3=0.33  0.1/0.3=0.33  0.1/0.3=0.33 
LightBlue  0.2/0.4=0.14  0.1/0.4=0.71  0.1/0.4=0.14 
LightBrown  0.2/0.5=0.4  0.1/0.5=0.2  0.2/0.5=0.4 
LightOthers  0.1/0.7=0.5  0.5/0.7=0.25  0.1/0.7=0.25 
If a posteriori probability is calculated without the inclusion of a priori probability π and without cost coefficient C, the result is probability of outcome given attribute P(oa), also called Maximum Likelihood. This describes the model, and demonstrates the relationship between attributes and outcomes.
P(oa) is calculated from P(ao) in Table B2. For each outcome j, its probability to be predicted by an attribute i (a_{i}), the calculation is
P(o_{j}a_{i}) = P(a_{i}o_{j}) / Σ(P(a_{i}o_{j})) for all outcomes
The calculations and results are shown in the table B3 to the right. They suggest that, without including a priori probability or cost, those with light hair and blue eyes Germans (0.71), those with dark hair and blue eyes French and those with light hair and other color eyes French (0.5), while the other combined attributes did not clearly discriminate between the 3 ethnicities.
Prediction 2. Bayesean Probability πP(oa)
Table B4a. πP(ao)
Attributes  Outcomes 
 French  German  Italian  Total 
DarkBlue  0.3x0.5=0.15  0.1x0.33=0.03  0.2x0.17=0.03  0.15+0.033+0.033=0.22 
DarkBrown  0.1x0.5=0.05  0.1x0.33=0.03  0.3x0.17=0.05  0.05+0.033+0.05=0.13 
DarkOthers  0.1x0.5=0.05  0.1x0.33=0.03  0.1x0.17=0.02  0.05+0.033+0.02=0.1 
LightBlue  0.2x0.5=0.05  0.1x0.33=0.17  0.1x0.17=0.017  0.05+0.17+0.017=0.233 
LightBrown  0.2x0.5=0.1  0.1x0.33=0.03  0.2x0.17=0.033  0.1+0.03+0.03=0.17 
LightOthers  0.1x0.5=0.1  0.5x0.33=0.03  0.1x0.17=0.017  0.1+0.03+0.017=0.15 
Table B4b. Bayesean Probability πP(oa)
Attributes  Outcomes 
 French  German  Italian 
DarkBlue  0.15/0.22=0.69  0.03/0.22=0.15  0.03/0.22=0.15 
DarkBrown  0.05/0.13=0.38  0.03/0.13=0.25  0.05/0.13=0.38 
DarkOthers  0.05/0.1=0.5  0.03/0.1=0.33  0.02/0.1=0.17 
LightBlue  0.05/0.23=0.21  0.17/0.23=0.71  0.02/0.23=0.07 
LightBrown  0.1/0.17=0.6  0.03/0.17=0.2  0.03/0.17=0.2 
LightOthers  0.1/0.15=0.67  0.03/0.15=0.22  0.02/0.15=0.11 
If a posteriori probability is calculated by changing the a priori probability π but without including the cost coefficient C, as in the majority of predictions, the result is probability of outcome given attribute and a priori probability πP(oa). This is usually referred to as Bayesian Probability, as it follows the descriptions first made by Bayes.
πP(oa) is calculated from P(ao) in Table B2 and the apriori probability π from Table B1. The calculations are in 2 steps. Firstly, the coefficient is adjusted by the a priori probability of each outcome, then the adjusted coefficients are normalized by the total for all outcomes. For each outcome j, its probability to be predicted by an attribute i (a_{i}) and a priori probability π_{j}, the calculations are as follows
 π_{j}P(a_{i}o_{j}) = P(a_{i}o_{j}) x π_{j}
 π_{j}P(o_{j}a_{i}) = π_{j}P(a_{i}o_{j}) / Σ_{all j}(π_{j}P(a_{i}o_{j}))
The calculations and results are shown in tables B4a (step 1) and B4b (step 2). Bayesean Probabilities suggest that, in a population of French:German:Italian of 0.5:0.33:0.17, those with light hair and blue eyes are most likely to be German at 0.71 and all other combinations likely to be French, although those with dark hair and brown eyes are equally likely to be Italians at 0.38.
Prediction 3. Adding a Cost Coefficient C to Bayesean Probability CπP(oa)
Table B5a. CπP(ao)
Attributes  Outcomes 
 French  German  Italian  Total 
DarkBlue  0.15x0.25=0.038  0.033x0.5=0.017  0.033x0.25=0.008  0.038+0.017+0.008=0.063 
DarkBrown  0.05x0.25=0.013  0.033x0.5=0.017  0.05x0.25=0.013  0.013+0.017+0.013=0.042 
DarkOthers  0.05x0.25=0.013  0.033x0.5=0.017  0.017x0.25=0.004  0.013+0.017+0.004=0.033 
LightBlue  0.05x0.25=0.013  0.167x0.5=0.083  0.017x0.25=0.004  0.013+0.083+0.004=0.1 
LightBrown  0.1x0.25=0.025  0.033x0.5=0.017  0.033x0.25=0.008  0.025+0.017+0.008=0.05 
LightOthers  0.1x0.25=0.025  0.033x0.5=0.017  0.017x0.25=0.004  0.025+0.017+0.004=0.046 
Table B5B Bayesean Probability with Cost Adjustment CπP(oa)
Attributes  Outcomes 
 French  German  Italian 
DarkBlu  0.038/0.063=0.6  0.017/0.063=0.27  0.008/0.063=0.13 
DarkBrown  0.013/0.042=0.3  0.017/0.042=0.4  0.013/0.042=0.3 
DarkOthers  0.013/0.033=0.38  0.017/0.033=0.5  0.004/0.033=0.13 
LightBlue  0.013/0.1=0.13  0.083/0.1=0.83  0.004/0.1=0.04 
LightBrown  0.025/0.05=0.5  0.017/0.05=0.33  0.008/0.05=0.17 
LightOthers  0.025/0.046=0.55  0.017/0.046=0.36  0.004/0.046=0.09 
If a posteriori probability is calculated by changing the a priori probability π and including the cost coefficient C, the result is probability of outcome given attribute, a priori probability, and cost CπP(oa). This is usually referred to as Bayesian Probability adjusted by cost, and provides a decision that contains a value based bias.
CπP(oa) is calculated from πP(ao) in Table B4a, multiplying it with the cost coefficient C from Table B1. The calculations are in 2 steps. Firstly, the a priori adjusted coefficient is further adjusted by the cost coefficient, then the adjusted coefficients are normalized by the total for all outcomes. For each outcome j, its probability to be predicted by an attribute i (a_{i}), a priori probability π_{j}, and cost coefficient C_{j}, are calculated as follows
 C_{j}π_{j}P(a_{i}o_{j}) = π_{j}P(a_{i}o_{j}) x C_{j}
 C_{j}π_{j}P(o_{j}a_{i}) = C_{j}π_{j}P(a_{i}o_{j}) / Σ_{all j}(C_{j}π_{j}P(a_{i}o_{j}))
The calculations and results are shown in tables B5a (step 1) and B5b (step 2 Bayesean Probability). Bayesean Probabilities with cost adjustment suggest that, in a population of French:German:Italian of 0.5:0.33:0.17, and relative costs of 0.25:0.5:0.25, we would conclude those identified as French would have dark hair/blue eye (0.6), light hair/brown eye (0.5), and light hair/other color eyes (0.55). Those identified as Germans would have dark hair/brown eye (0.4), dark hair/other color eyes (0.5), and light hair blue eyes (0.83). No one would be identified as Italian.
The program
The structure of the program, and how to use it, are presented in the Help and Hint panel of Classification by Basic Bayes Probability Program Page
. The results produced, using the default example data, are as follows.
Attribute  French  German  Italian 
DarkBlue  3  1  2 
DarkBrown  1  1  3 
DarkOthers  1  1  1 
LightBlue  1  5  1 
LightBrown  2  1  2 
LightOthers  2  1  1 
The Matrix of counts are produced if the program commences with the modelling data in Program 1. The program counts the numbers in each attribute/outcome combination, and table the results as shown to the right. This is the same as table B1 previously shown in this panel.

Attribute  French  German  Italian 
DarkBlue  0.3  0.1  0.2 
DarkBrown  0.1  0.1  0.3 
DarkOthers  0.1  0.1  0.1 
LightBlue  0.1  0.5  0.1 
LightBrown  0.2  0.1  0.2 
LightOthers  0.2  0.1  0.1 
The Model Coefficients are calculated if the program commences with Program 1, and copied from the text area if the commences with Program 2. This is the same as Table B2. Model Coefficients P(ao), previously shown in this panel, but without the calculations

Regardless whether computation begins at Program 1, 2, or 3, the following results are shown.
 French  German  Italian 
a priori π  0.5  0.3333  0.1667 
cost C  0.25  0.5  0.25 
combined  0.375  0.5  0.125 
The Arrays of a priori and cost coefficients, and their combination are as shown to the right. a priori probabilities and costs are normalized by dividing each value by the total of the array.
The combined array is obtained by multiplying the normalized apriori and cost values, then normalize the results by dividing each against the total.

Attribute  French  German  Italian 
DarkBlue  0.6  0.2666  0.1334 
DarkBrown  0.3  0.4  0.3001 
DarkOthers  0.375  0.5  0.125 
LightBlue  0.125  0.8333  0.0417 
LightBrown  0.5  0.3333  0.1667 
LightOthers  0.5455  0.3636  0.0909 
The table of a posteriori probabilities is as shown to the right. The type of table created depends on how the a priori probabilities and costs are set by the user.
 Maximum Likelihood (P(oa)) if both a priori probabilities and costs are unset
 Bayesean (πP(oa) if a priori probabilities are set but not costs
 Bayesian adjusted by cost (CπP(oa)) if both a priori probabilities and costs are set
As the a priori probabilities and costs are set in the reference example, the table is CπP(oa), the same as table B5B previously shown in this panel, but without the calculations
The highest probability for each row is marked bold to indicate the outcome chosen by the model.

Row  Attribute  French  German  Italian 
1  DarkBlue  0.6  0.2666  0.1334 
2  LightOthers  0.5455  0.3636  0.0909 
.... etc 
Running Program 1, or if data is available in the Data text area for Program 2 or 3, each attribute is processed by the model to produce the array of probabilities, as shown in the table to the right.

Program 4 creates a Javascript function that will allow the user to calculate a posteriori probabilities from attributes. This function can be incorporated into any html page, or adapted to another computer language and incorporated into users own programs.
StatsToDo provides a template BasicBayesTemplate.html which demonstrates how this function can be used. As the template contains all the explanations, they will not be further elaborated here. Users are encouraged to access, examine, renames, and modify the html page and explore how the function can be used.
This panel discusses the Naive Bayes model generally, and the calculations used in the Classification by Naive Bayes Probability Program Page
.
Table N1: Counts
Attributes  Outcome 
Col  Attribute  French  German  Italian 
1  Dark  5  3  6 
1  Light  5  7  4 
2  Blue  4  6  3 
2  Brown  3  2  5 
2  Others  3  2  2 
a priori π  0.5  0.33  0.17 
cost  0.25  0.5  0.25 
Naive Bayes is used to represent the Bayes Probability model, a modification of the original Bayes modle described by Bayes and in the Wikipedia page on Bayes Theorem, and explained in Wikipedia page on Naive Bayes.
The model uses 2 or more predictors, each with 2 or more attributes, to predict the probabilities of 2 or more outcomes. The term Naive refers to the naive assumption that the predictors are independent of each other.
In the default example in the Classification by Naive Bayes Probability Program Page
, the data presented in the Introduction panel are restructured, so that the two preditors, hair color (2 attributes of Dark and Light) and eye colors (3 attributes of Blue, Brown and Others) are counted separately. The restructured table is used in the program, and as shown to the right.
Please note: The term Col is used in all tables to represent predictor. Col1 is predictor 1 (in this example hair color), Col2 is predictor 2 (in this example eye color), and so on.
Building the model: Attribute (a) : Outcome (o)
Table N2. Model Coefficients P(ao)
Col  Attribute  French  German  Italian 
1  Dark  5/10=0.5  3/10=0.3  6/10=0.6 
1  Light  5/10=0.5  7/10=0.7  4/10=0.4 
2  Blue  4/10=0.4  6/10=0.6  3/10=0.3 
2  Brown  3/10=0.3  2/10=0.2  5/10=0.5 
2  Others  3/10=0.3  2/10=0.2  2/10=0.2 
The coefficients of the model, to be used to convert a priori to a posteriori probability, is the probability of attribute given outcome P(ao). For the i ^{th} attribute from each predictor and the j ^{th} outcome, P(ao) is calculated by dividing the number of the attribute/outcome pair (N _{i,j}) by the sample size of that outcome (N _{j}).
P(a_{i}o_{j}) = N_{i,j}/N_{j}
In this example, the attributes are the hair color (Col 1) and eye color (Col 2), and the P(ao) for each attribute from each predictor (Col) are calculated separately. The results are shown in Table N2 to the right.
Creating the pattern coefficient: pattern(p) : Outcome (o)
Table N2x. Pattern Coefficients P(po)
Pattern  Outcome 
Col 1  Col 2  French  German  Italian  Total 
Dark  Blue  0.5x0.4=0.2  0.3x0.6=0.18  0.6x0.3=0.18 
0.2+0.18+0.18=0.56 
Dark  Brown  0.5x0.3=0.15  0.3x0.2=0.06  0.6x0.5=0.3 
0.15+0.06+0.3=0.51 
Dark  Others  0.5x0.3=0.15  0.3x0.2=0.06  0.6x0.2=0.12 
0.15+0.06+0.12=0.33 
Light  Blue  0.5x0.4=0.2  0.7x0.6=0.42  0.4x0.3=0.12 
0.2+0.42+0.12=0.74 
Light  Brown  0.5x0.3=0.15  0.7x0.2=0.14  0.4x0.5=0.2 
0.15+0.14+0.2=0.49 
Light  Others  0.5x0.3=0.15  0.7x0.2=0.14  0.4x0.2=0.08 
0.15+0.14+0.08=0.37 
When presented with an array of attributes (pattern), Naive Bayes creates the coefficient for that particular pattern/outcome combination (P(po)). This is created by multiplying the P(ao) of attribute/outcome combination in the array of attributes (pattern). An example uses the first pattern
 Col 1, hair color:Dark and outcome 1 French: P(ao) = 0.5
 Col 2, eye color:Blue and outcome 1 French: P(ao) = 0.4
 For pattern [Dark,Blue]/French P(po) = 0.5x0.4 = 0.2
 Equation : P(po) = Π(all P(ao) in the array of attributes)
The coefficients are as shown as Table N2x for demonstration purpose here. In practice, the P(po) coefficients are calculated dynamically, depending on the attributes in the pattern array. As this is a dynamic intermediary step, P(po) are not presented as results, but used to produce the a posteriori probabilities in the same manner as P(ao) in the basic Bayes model.
Prediction 1. Maximum Likelihood P(op)
Table N3. Maximum Likelihood P(oa)
Pattern  Outcome 
Col 1  Col 2  French  German  Italian 
Dark  Blue  0.2/0.56=0.36  0.18/0.56=0.32  0.18/0.56=0.32 
Dark  Brown  0.15/0.51=0.29  0.06/0.51=0.12  0.3/0.51=0.59 
Dark  Others  0.15/0.33=0.45  0.06/0.33=0.18  0.12/0.33=0.36 
Light  Blue  0.2/0.74=0.27  0.42/0.74=0.57  0.12/0.74=0.16 
Light  Brown  0.15/0.49=0.31  0.14/0.49=0.29  0.2/0.49=0.41 
Light  Others  0.15/0.37=0.41  0.14/0.37=0.38  0.08/0.37=0.22 
If a posteriori probability is calculated without the inclusion of a priori probability π and without cost coefficient C, the result is probability of outcome given attribute P(op), also called Maximum Likelihood. This describes the model, and demonstrates the relationship between attributes and outcomes.
P(op) is calculated dynamically, the values demonstrated in Table N2x. For each outcome j, its probability to be predicted by pattern i (p_{i}), the calculation is
P(o_{j}p_{i}) = P(p_{i}o_{j}) / Σ(P(p_{i}o_{j})) for all outcomes
The calculations and results are shown in the table N3 to the right. They suggest that, without including a priori probability or cost, those with brown eyes are most likely to be Italians (0.59 for dark hair and 0.41 for light hair), those with light hair and blue eyes German (0.57), and the rest French (0.36,0.45, 0.41).
Prediction 2. Bayesean Probability πP(op)
Table N4a. πP(po)
Pattern  Outcome 
Col 1  Col 2  French  German  Italian  Total 
Dark  Blue  0.2x0.5=0.1  0.18x0.33=0.06  0.18x0.17=0.03 
0.10+0.06+0.03=0.19 
Dark  Brown  0.15x0.5=0.08  0.06x0.33=0.02  0.3x0.17=0.05 
0.08+0.02+0.05=0.15 
Dark  Others  0.15x0.5=0.08  0.06x0.33=0.02  0.12x0.17=0.02 
0.08+0.02+0.02=0.12 
Light  Blue  0.2x0.5=0.1  0.42x0.33=0.14  0.12x0.17=0.02 
0.10+0.14+0.02=0.26 
Light  Brown  0.15x0.5=0.08  0.14x0.33=0.05  0.2x0.17=0.03 
0.08+0.05+0.03=0.16 
Light  Others  0.15x0.5=0.08  0.14x0.33=0.05  0.08x0.17=0.01 
0.08+0.05+0.01=0.13 
Table N4b. Bayesean Probability πP(op)
Pattern  Outcome 
Col 1  Col 2  French  German  Italian 
Dark  Blue  0.1/0.19=0.53  0.06/0.19=0.32  0.03/0.19=0.16 
Dark  Brown  0.08/0.15=0.52  0.02/0.15=0.14  0.05/0.15=0.34 
Dark  Others  0.08/0.12=0.65  0.02/0.12=0.17  0.02/0.12=0.17 
Light  Blue  0.1/0.26=0.38  0.14/0.26=0.54  0.02/0.26=0.08 
Light  Brown  0.08/0.16=0.48  0.05/0.16=0.3  0.03/0.16=0.22 
Light  Others  0.08/0.13=0.56  0.05/0.13=0.35  0.01/0.13=0.1 
If a posteriori probability is calculated by changing the a priori probability π but without including the cost coefficient C, as in the majority of predictions, the result is probability of outcome given pattern and a priori probability πP(op). This is usually referred to as Naive Bayesian Probability.
πP(op) is calculated from the dynamically calculated P(po) as shown in in Table N2x and the apriori probability π from Table N1. The calculations are in 2 steps. Firstly, the coefficient P(po) is adjusted by the a priori probability of each outcome, then the adjusted coefficients are normalized by the total for all outcomes. For each outcome j, its probability to be predicted by a pattern i (p_{i}) and a priori probability π_{j}, the calculations are as follows
 π_{j}P(p_{i}o_{j}) = P(p_{i}o_{j}) x π_{j}
 π_{j}P(o_{j}p_{i}) = π_{j}P(p_{i}o_{j}) / Σ_{all j}(π_{j}P(p_{i}o_{j}))
The calculations and results are shown in tables N4a (step 1) and N4b (step 2). Bayesean Probabilities suggest that, in a population of French:German:Italian of 0.5:0.33:0.17, those with light hair and blue eyes are most likely to be German at 0.54 and all other combinations likely to be French
Prediction 3. Adding a Cost Coefficient C to Bayesean Probability CπP(op)
Table N5a. CπP(po)
Pattern  Outcome 
Col 1  Col 2  French  German  Italian  Total 
Dark  Blue  0.1x0.25=0.025  0.06x0.5=0.03  0.03x0.25=0.008 
0.025+0.030+0.008=0.063 
Dark  Brown  0.08x0.25=0.02  0.02x0.5=0.01  0.05x0.25=0.013 
0.020+0.010+0.013=0.043

Dark  Others  0.08x0.25=0.02  0.02x0.5=0.01  0.02x0.25=0.005 
0.020+0.010+0.005=0.035

Light  Blue  >0.1x0.25=0.025  0.14x0.5=0.07  0.02x0.25=0.005 
0.025+0.070+0.005=0.100 
Light  Brown  0.08x0.25=0.02  0.05x0.5=0.025  0.03x0.25=0.008 
0.020+0.025+0.008=0.053 
Light  Others  0.08x0.25=0.02  0.05x0.5=0.025  0.01x0.25=0.003 
0.020+0.025+0.003=0.048 
Table N5B Bayesean Probability with Cost Adjustment CπP(op)
Pattern  Outcome 
Col 1  Col 2  French  German  Italian 
Dark  Blue  0.025/0.063=0.4  0.03/0.063=0.48  0.008/0.063=0.12 
Dark  Brown  0.02/0.043=0.45  0.01/0.043=0.24  0.013/0.043=0.30 
Dark  Others  0.02/0.035=0.56  0.01/0.035=0.3  0.005/0.035=0.5 
Light  Blue  >0.025/0.1=0.25  0.07/0.1=0.7  0.005/0.1=0.05 
Light  Brown  0.02/0.053=0.37  0.025/0.053=0.46  0.008/0.053=0.17 
Light  Others  0.02/0.048=0.41  0.025/0.048=0.51  0.003/0.048=0.07 
If a posteriori probability is calculated by changing the a priori probability π and including the cost coefficient C, the result is probability of outcome given attribute, a priori probability, and cost CπP(op). This is usually referred to as Bayesian Probability adjusted by cost, and provides a decision that contains a value based bias.
CπP(op) is calculated from πP(po) in Table N4a, multiplying it with the cost coefficient C from Table N1. The calculations are in 2 steps. Firstly, the a priori adjusted coefficient is further adjusted by the cost coefficient, then the adjusted coefficients are normalized by the total for all outcomes. For each outcome j, its probability to be predicted by a pattern i (p_{i}), a priori probability π_{j}, and cost coefficient C_{j}, are calculated as follows
 C_{j}π_{j}P(p_{i}o_{j}) = π_{j}P(p_{i}o_{j}) x C_{j}
 C_{j}π_{j}P(o_{j}p_{i}) = C_{j}π_{j}P(p_{i}o_{j}) / Σ_{all j}(C_{j}π_{j}P(p_{i}o_{j}))
The calculations and results are shown in tables N5a (step 1) and N5b. Bayesean Probabilities with cost adjustment suggest that, in a population of French:German:Italian of 0.5:0.33:0.17, and relative costs of 0.25:0.5:0.25, we would conclude that those with light hair or blue eyes would be identified as German (0.48, 0.7, 0.46, 0.51), and the other combinations as French (0.45, 0.56). No one would be identified as Italian.
The program
The structure of the program, and how to use it, are presented in the Help and Hint panel of Classification by Naive Bayes Probability Program Page
. The results produced, using the default example data, are as follows.
Col  Attribute  French  German  Italian 
1  Dark  5  3  6 
1  Light  5  7  4 
2  Blue  4  6  3 
2  Brown  3  2  5 
2  Others  3  2  2 
The Matrix of counts are produced if the program commences with the modelling data in Program 1. The program counts the numbers in each attribute/outcome combination, and table the results as shown to the right. This is the same as table N1 previously shown in this panel.

Col  Attribute  French  German  Italian 
1  Dark  0.5  0.3  0.6 
1  Light  0.5  0.7  0.4 
2  Blue  0.4  0.6  0.3 
2  Brown  0.3  0.2  0.5 
2  Others  0.3  0.2  0.2 
The Model Coefficients are calculated if the program commences with Program 1, and copied from the text area if the commences with Program 2. This is the same as Table N2. Model Coefficients P(po), previously shown in this panel, but without the calculations

Regardless whether computation begins at Program 1, 2, or 3, the following results are shown.
 French  German  Italian 
a priori π  0.5  0.3333  0.1667 
cost C  0.25  0.5  0.25 
combined  0.375  0.5  0.125 
The Arrays of a priori and cost coefficients, and their combination are as shown to the right. a priori probabilities and costs are normalized by dividing each value by the total of the array.
The combined array is obtained by multiplying the normalized apriori and cost values, then normalize the results by dividing each against the total.

Attributes  Outcomes 
Col 1  Col 2  French  German  Italian 
Dark  Blue  0.4  0.48  0.12 
Dark  Brown  0.4545  0.2424  0.3031 
Dark  Others  0.5556  0.2963  0.1482 
Light  Blue  0.25  0.7  0.05 
Light  Brown  0.3719  0.4628  0.1653 
Light  Others  0.4129  0.5137  0.0734 
The table of a posteriori probabilities is as shown to the right. The type of table created depends on how the a priori probabilities and costs are set by the user.
 Maximum Likelihood (P(oa)) if both a priori probabilities and costs are unset
 Bayesean (πP(oa) if a priori probabilities are set but not costs
 Bayesian adjusted by cost (CπP(oa)) if both a priori probabilities and costs are set
As the a priori probabilities and costs are set in the reference example, the table is CπP(oa), the same as table N5B previously shown in this panel, but without the calculations
The highest probability for each row is marked bold to indicate the outcome chosen by the model.
Please Note: The use of the Naive Bayes model may include many predictors. As the number of possible combinations increases exponentially with the number of predictors, it may not be possible to present all combinations before time limit set by the server expires and the program crashes. This table is therefore only presented when the total number of combinations do not exceed 500, as in the case with the default example where there are 2x3=6 combinations.

Col 1  Col 2  French  German  Italian 
Dark  Blue  0.4  0.48  0.12 
Light  Others  0.4129  0.5137  0.0734 
Dark  Brown  0.4545  0.2424  0.3031 
.... etc 
Running Program 1, or if data is available in the Data text area for Program 2 or 3, each attribute is processed by the model to produce the array of probabilities, as shown in the table to the right.
Please Note: To avoid the program crashing because the time limit set by the server expires, only the first 500 rows of data are calculated and presented, as in the case with the default example where there are 30 row of data.

Program 4 creates a Javascript function that will allow the user to calculate a posteriori probabilities from attributes. This function can be incorporated into any html page, or adapted to another computer language and incorporated into users own programs.
StatsToDo provides a template NaiveBayesTemplate.html which demonstrates how this function can be used. As the template contains all the explanations, they will not be further elaborated here. Users are encouraged to access, examine, renames, and modify the html page and explore how the function can be used.
Explanations for the terms and abbreviations used, and the mathematical algorithms, have already been discussed in the previous panels. This panel discusses, in conceptual terms, the differences between the Basic and Naive Bayesean models, and how they may be used.
Basic Bayes Probability Model
The Basic Bayes model is based on the original Bayes Theorem. The term "Basic" is used in StatsToDo to distinguish it from the Naive Bayes model. In this discussion therefore, Basic Bayes and Bayes means the same thing
Conditional probability is estimating and changing our estimate of probability, when presented with conditions we known are associated with the outcome of interest. For example, if we know that a particular poker player blinks a lot when he is bluffing, then we can conclude he is likely to be bluffing when he blinks a lot. The model is simple, elegant, intuitively easy to understand and accept. What follows are discussions on its usage in practice.
Advantages
The main advantage is that the model is self evidently valid, as it is the mathematical form of an accepted practice of basing decisions on knowledge and experience. Given that each attribute and each outcome is unique, their relationship is also unique, so there is no ambiguitiy in what the results represent. The user can therefore be confident in translating the numerical results to decisions and actions, providing the model is based on valid data, and the a priori probabilities are well chosen.
The model is also easy to use. Tables of coefficients, and tables translating attributes to probabilities of outcomes, can be produced by researchers, and distributed to end decision makers, either physically as reference tables, or electronically as computer applications. The accompanied html page BasicBayesTemplate.html is a template html page demonstrating how the coefficients developed can be translated into a tool for front line decision makers.
Disadvantages
The major disadvantage of the basic Bayesean model is its inability to cope with too many predictors.
 In our example, with 2 predictor of 2 hair and 3 eye color, the number of combined attributes is 2x3=6. If we are to add skin color (say pale, tan, and dark), the number of attributes will be 6x3=36. If we then add body bult (skinny, fat, muscular), 36x3=108. Then temperament (stoic, emotional), 108x2=216, and so on. Each additional predictor multiplies the number of combinations, and this can exceed 1000 if more than 9 binary predictors are included.
 The problem is not so much computational complexity, as the high speed and large memory of modern computers can always cope.
 The main problem is to find sufficient cases to build a valid reference model in the first place. If probabilities are to be accurate to 0.1 (10%) then at least 10 cases are needed in each attribute/outcome pair. Some of the combinations may be uncommon (say fat, calm, dark skin, light hair, blue eyes, and French), and to obtain sufficient cases to fill all combinations with sufficient numbers may require samples sizes that are impractical to collect.
The model cannot compute missing data, as each attribute is a combination of all the predictors, and all observations must be present for the attribute to be valid. For example, we cannot make a prediction when the person is too far away for us to know the color of his/her eyes, or if the person has no hair. Missing data is common in situations where decisions need to be made with incomplete data. For example, when a patient arrives in an emergency department in a hospital, the triage nurse has to make a decision based on the major symptom. Once admitted, decisions are based on additional history and physical examination. After a few hours, decisions are modified with test results. A day later, more modification based on progress observed. This means that missing data can be normal, and a single model cannot cope with this. In these situations, individual model will need to be produced for every conceivable combination of data and missing data that may arise, and this magnifies the scalability problem.
The model is inflexible, in that each attribute in a compound predictor is actually a combination of information from different original predictors. This means that no original predictor can be added, removed, or its relationship with outcomes altered without changes to attributes in the model. If any change is required, then a new model will need to be constructed. For example, after we used our example model for a while, we observed that the 3 ethnic groups have different temperaments, and wish to include that in our model. We cannot simply add temperament to the model, as we will have to create 12 new patterns of hair_eye_temperament, essentially building a new model with new data. The problem of stability is particularly important in rapidly changing environments, such as medical care where disease patterns and technologies evolve rapidly.
Usage
The basic Bayesean model is preferred if the following conditions can be met
 When the relationship between predictors and outcomes are stable
 When the total number of attribute not so great that the adequate sample size to build the model cannot be obtained,
 Where there is no attribute/outcome combination that is so uncommon that the total sample size required cannot be reached
 When missing data is not expected
 Where a sufficiently large sample size is available to build the model.
Naive Bayes Probability
Naive Bayes probability is used only when there are multiple predictors. It differs from the basic model by accepting, naively, that the predictors are independent of each other.
Conceptually, the model can be considered as a sequence of basic Bayesean calculations. We start with the a priori probabilities π modify this with the attribute of the first predictor to produce the first a posteriori probabilities, which become the a posteriori probabilities for the attribute of the second predictor, the results of which become a priori for the third predictor, and so on.
The computations are simplified by multiplying all the coefficients P(ao) representing the attributes to create the pattern coefficient P(po), and use this in the same way as in the basic Bayesean model.
The results obtained from the example are already presented in the Naive Bayes panel of this page and from the program in the Classification by Naive Bayes Probability Program Page
. The explanation and interpretations for these results are the same as that for the basic Bayes probability. Therefore results from the example will not be further discussed here.
The advantages and disadvantages of using the Naive Bayes model are the mirror image of the basic Bayes model.
Advantages
The Naive Bayes model can include a large number of predictors.
 The number of P(ao) required increases linearly with the number of predictors for the Naive Bayes model, compared with exponentially for the basic bayes model. From our example, 2 hair and 3 eye color requires 2+3=5 P(ao) cells for each outcome for Naive Bayes, instead of 2x3=6 P(po) for basic Bayes. Using only n binary predictors, Naive Bayes requires 2xn individual attribute, but basic Bayes requires 2^{n} combination of attributes
 By assuming that all attributes are independent of each other, a workable model can be built providing sufficient number of cases are available for each attribute, compared with basic Bayes where sufficient numbers are required for each combination of attributes
 In other words, the model can be built using a smaller and more practical sample size.
The Naive Bayes model copes better with missing data. As all attributes are assumed independent, the absence of an attribute merely means that the influence of that attribute is not included. The results will be less precise, but the algorithm will calculate the a posteriori with what data it has, insted of not computing at all when the Basic Bayeseam model confronts missing data.
The Naive Bayes model is more flexible. It is easier to add, delete, or change a predictor. Given the assumption that all predictors are independent, and the P(po) is calculated dynamically, the addition, removal, or change in any predictor should not affect the performance of the other predictors. Most of the calculations would remain the same, but the results would be modified by the changes. This flexibility is particularly useful in situations where the relationship between predictors and outcome may change, or technological development requires additional predictors to be added from time to time, such as in medical care.
Disadvantages
The main disadvantage of, or objection to the Naive Bayes model is in the naive assumption, that the predictors are independent, because this assumption is rarely correct. Two types of dependencies may exist in any set of predictors.
 Correlation may exist between predictors, as many predictors have common precursors in genetics, geography, history, culture, and so on. When a correlation exists, the common precursor is used more than once in calculation, and this inserts an unaccounted bias in the results. In our example, hair and eye color may be correlated, as they are both dependent on pigmentation generally. If we combine these with say temperament in a model, we will use the influence of pigmentation twice (two colors) to temperament once, resulting in a bias decision.
 Interaction is when predictors have synergistic or inhibitory effects on each other in their relationship with outcomes.
 An example of synergism is in the treatment of cancer. If surgery has a cure rate of 10% and chemotherapy 10%, then we would expect that providing both will result in a cure rate of 19% (10.9x0.9=0.19) if there is no interaction. In many cases however, surgery makes the remaining cancer cells more sensitive to chemotherapy, and the cure rate of combined therapy is more than 19%.
 An eample of inhibition is in the use of antibiotics to treat a particular infection. An antibiotic, say penicillin, may cure this infection in 10% of cases, and another, say tetracyline, also 10%. In the absence of interaction we would expect a 19% cure rate if both are used. However, penicillin works by destroying bacterial cell walls and is most effective when bacteria are actively growing, but tetracycline works by slowing the growth of bacteria. The giving of both therefore may result in one inhibiting the effect of the other, and a cure rate of less than 19%.
The error cause by the naive assumption in the Bayes model is difficult to quantify, so the precision and accuracy can only be estimated when the model is deployed after development.
Choice between models
The Basic Bayes model should be the first option, given its theoretical validity and ease of use.
Given that the assumtion of predictor independence is naive and contains unquantifiable bias, the Naive Bayes model is a compromise solution, and chosen when the Basic Bayes model is not practicable. When chose, the following should be observed
 The Naive Bayes model should be considered only as an approach to develop a prediction model, its validity and usefulness should not be presumed.
 The choice of predictors should be carefully considered to ensure that they are as independent from each other as possible. When correlation or interaction is suspected, predictors can be combined into compound predictors before they are added to the model
 The performance of the completed model requires constant review, and the model adjusted when inaccuracies are traced or whem circumstances changed.
E
The following references are introductory in nature, mainly to help the inexperienced.
Basic Bayes
https://en.wikipedia.org/wiki/Bayes%27_theorem Bayes Probability from Wikipedia. This provides a clear definition, and links to references for more reading
https://arbital.com/p/bayes_rule/?l=1zq An online The introduction to Bayes theorem. This provides good detailed explanations, and links to additional pages that catered to different level of knowledge and needs
Naive Bayes
https://en.wikipedia.org/wiki/Naive_Bayes_classifier Naive Bayes from Wikipedia. A concise and clear description, and provides references to more reading
https://www.machinelearningplus.com/predictivemodeling/hownaivebayesalgorithmworkswithexampleandfullcode/ a teaching page with explanations for beginners
Mueller JP and Massaron L (2016) Machine Learning for Dummies. John Wiley and Son, Inc New Jersey, ISBN 978119245513. p.158163.
This gives only a brief introduction to both Bayes algorithms. However it provides perspectives in how Bayes probabililty is used in data analysis, in comparison with a whole lot of other methods. There is not much guidance to calculations however, as the book relies heavily on the use of R and Python packages.
Old references
The following 2 references introduced me to Naive Bayes many years ago, before the term naive became commonly used. I include them partly out of sentiment, but mostly because they have major influences on how the models are presented on this and the two programming web pages.
Warner HR, Toronto AF, Veasey LG, and Stephenson R (1961)A Mathematical Approach
to Medical Diagnosis. Application to Congenital Heart Disease. JAMA 117:3 p.177183.
This paper used the formula that is the same as what is now Naive Bayes probability, but neither Bayes nor Naive was mentioned. The paper quoted Lusted as the source of the mathematical approach.
The paper discussed the difficulty of combining multiple predictors into a single compound predictor, as the list of observations needed to differentiate between congenital heart deseases was too great to do so in practice (my interpretation).
The paper presented a complicated set of mathematical arrangements to deal with a mix of binomial and multinomial predictors. It was easy enough to understand, but difficult to present concisely and dynamically in the web page format, and I had tried a number of different ways to present the calculations and results before ending with the format on these pages
The paper went on to warn the reader about the pitfalls of correlation between predictors and the consequent tautology, and recommended their careful selection and management.
Over the next few years, the authors published a number of additional papers on the subject, reviewing the results and validating the use of the model they built.
This paper therefore provided the initial prompt that led me to developed the arguments listed in the advantages/disadvantages and usage sections in the discussion panel. It also prompted the evolutions on how the concept and results are presented until the current form on these pages.
Overall JE and Klett CJ (1972) Applied Multivariate Analysis. McGraw Hill Series in Psychology.
McGraw Hill Book Company New York. Library of Congress No. 73147164070479356 p.400412.
This is an old book that may be out of print. In its days it was a recommended text for multivariate statistics at the Masters level.
Included in the book was a brief chapter on Bayes Probability, and in that chapter presented the formulae which are the same as that currently known as Naive Bayes. The term "pattern probability" was used, and my guess is that the term Naive was at the time not in common use.
The book also introduced the concept of costs. My guess is that, as the book was written by psychiatrists and the examples were on differential diagnosis, the authors inserted a value judgements facility to deal with diagnoses that have different consequences. Because of my own medical background, I considered this useful and included costs in these pages. Readers of this page should be reminded however that I cannot find references to costs anywhere else, so costs should only be set when value judgement is involved.
I began to understand Bayesean probability from reading this book, as by using different abbreviations, the book clarifies the difference between the basic and the naive models.
From this book, I acquire the concept of costs, the terms attribute and pattern, and the abbreviations πP(op) and CπP(op). The book used j for outcome, x for attributes, and arrays of attribute for patterns. I converted these to a for attribute, p for pattern, and o for outcomes, making it easier for the inexperienced to follow explanations and algorithms.
Contents of G:6
