- Forward


Indicators
A Programming Pattern (With Examples in Python)


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu

Print

Motivation
Back SMYC Forward
  • The Situation:
    • Calculations must be performed that vary based on one or more binary conditions
  • Terminology:
    • Indicator Variables
    • Dummy Variables
  • Common Notation:
    • \(\delta\) (often with a subscript identifying the indicator)
An Example
Back SMYC Forward
  • A Model of Birth Weight:
    • \(w = -2200 + 148.2 g - 238.6 \delta_s\)
  • Notation:
    • \(w\) denotes the weight of the baby in grams
    • \(g\) denotes the length of the gestation period in weeks
    • \(\delta_s\) is 1 if the mother smoked during the gestation period and 0 otherwise
The Pattern
Back SMYC Forward
  • Getting Started:
    • Define as many 0-1 variables as needed
  • The Process with One Indicator:
    • pythonexamples/programmingpatterns/Indicators.py (Fragment: pattern)
       
Examples
Back SMYC Forward
  • The Setting:
    • The birth weight example
  • The Code:
    • pythonexamples/programmingpatterns/Indicators.py (Fragment: birthweight)
       
Examples (cont.)
Back SMYC Forward
  • The Setting:
    • The fine associated with a first parking ticket is smaller than the fine associated with subsequent parking tickets (specifically, $10.00 for the first ticket and $45.00 for subsequent tickets)
  • The Code:
    • pythonexamples/programmingpatterns/Indicators.py (Fragment: parking)
       
Examples (cont.)
Back SMYC Forward
  • The Setting:
    • A rental car company that charges a base rate $19.95 per day
    • There is a surcharge of $5.00 per day if multiple people drive the car
    • There is a surcharge of $10.00 per day if any driver is under 25 years of age
  • The Code:
    • pythonexamples/programmingpatterns/Indicators.py (Fragment: rental)
       
A Warning
Back SMYC Forward
  • What About if Statements?
    • They tend to be much more verbose
    • They treat the discrete and continuous variables differently for no apparent reason
  • One Example:
    • pythonexamples/programmingpatterns/Indicators.py (Fragment: ifbirthweight)
       
  • Another Example:
    • pythonexamples/programmingpatterns/Indicators.py (Fragment: ifrental)
       
A Note About Python
Back SMYC Forward
  • Arithmetic Operations on Booleans:
    • Are allowed - True evaluates to 1 and False evaluates to 0
  • An Implication:
    • You needn't use 0-1 variables, you can use Boolean variables for the indicators
  • Some Advice:
    • Many people think it's confusing and dangerous to perform arithmetic operations on non-numbers
    • This is very language specific (i.e., idiomatic), so, even if you disagree, it's still important to know the pattern
There's Always More to Learn
Back -