← Back to Book Detail

6 Indicators (6/11) -- Patterns for Beginning Programmers

Browse
54%

6 Indicators

6 Indicators Many programs must perform calculations that vary based on conditions of one kind or another. There are many different ways to accomplish this but one very powerful (and common) solution is to use a multiplicative variable that takes on the value zero when the condition isn’t satisfied and the value one when it is. Motivation Variables of this kind are common in many branches of mathematics and are called indicator variables.[1] They are often denoted using a lowercase delta (i.e., [latex]\delta[/latex])[2], often with a subscript to denote the condition (e.g., [latex]\delta_s[/latex] to indicate whether a person smokes or not). Indicator variables are then multiplied by other variables in more complex expressions. For example, suppose you are writing a program to predict the birth weight of babies (in grams) from the gestation period (in weeks). You might theorize that the weight will be lower if the mother smokes during pregnancy. Ignoring whether the mother did or didn’t smoke, after collecting data from a (random or representative) sample of the population, you might determine a relationship like the following: [latex]w = -2200 + 148.2 g[/latex] where [latex]w[/latex] (the dependent variable) denotes the birth weight (in grams), and [latex]g[/latex] (an independent variable) denotes the gestation period (in weeks).[3] Accounting for the mother’s smoking behavior, you might determine that the birth weight was, on average, 238.6 grams lower when the mother smoked. You now need to decide how to account for this in your program. Thinking About The Problem What you want to do is lower [latex]w[/latex] when the mother smoked and leave [latex]w[/latex] unchanged when the mother didn’t smoke. Since there are only two possible states (i.e., the mother smoked or didn’t), you might be tempted to use a boolean variable to keep track of this information. However, it turns out that it is better to use a discrete variable that is assigned either 0 or 1 , rather than one that takes on the values true or false . The reason is that you can use a numeric variable with the multiplication operator, and you can’t do so with a boolean variable. In other words, a boolean variable can’t be either the right-side or the left-side operand of the multiplication operator. In particular, suppose you add another independent variable, [latex]\delta_s[/latex], and assign the value [latex]1[/latex] to [latex]\delta_s[/latex] if the mother smoked during pregnancy and assign the value [latex]0[/latex] to [latex]\delta_s[/latex] otherwise. Then, the equation for [latex]w[/latex] can be expressed succinctly as follows: [latex]w = -2200 + 148.2 g - 238.6 \delta_s[/latex] In this way, [latex]w[/latex] will be reduced by [latex]238.6[/latex] when [latex]\delta_s[/latex] is [latex]1[/latex] and will be left unchanged when [latex]\delta_s[/latex] is 0. Note that you could define the indicator variable differently. Specifically, you could assign [latex]1[/latex] to [latex]\
← Previous Chapter Next Chapter →