Lindo Systems
    
! LINGO model to compute the probability of outcomes
 in U.S. Presidential Election Electoral College.;
! The overall model consists of two submodels:
  a) A simple model to convert the fractions of decided 
     voters for each party in a state into a single 
     probability a specified party will get all electoral
     votes from the state.
  b) A computationally intense model to compute the 
     overall probability of a specifed party wins, given
     the probability for each state of a win;
! Keywords: Election, Presidential election, Electoral college,
   Probability of winning;
! LINGO can be downloaded from http://www.lindo.com ;
SETS:
 STATE: VOTES, PD, POPD, POPR;
 KNT/1..275/:;
 KXS( KNT, STATE): WAYS;
ENDSETS
DATA:  
! Computing Vote Probabilities with LINGO;
! Definitions:
   Inputs:
    VOTES(j) = electoral votes for state j,
    POPD(j) = proportion of population definitely voting Democratic,
    POPR(j) = proportion of population definitely voting Republican
    UVD = adjustment down of estimated Democratic popularity, to
              give the assured fraction vote for a candidate.
    UVR = adjustment down of estimiated Republican popularity, etc.;
 !  Computed inputs:
    PD(j) = estimated probability electors for state j vote Democratic.;
 UVD = 0;
 UVR = 0;
! Electoral vote counts are for 2012;
! Popularity fractions based loosely on www.electoral-vote.com;
  STATE,      VOTES, POPD, POPR =
! State       Votes P(Dem) P(Repb);
 Alabama          9  0.36  0.54
 Alaska           3  0.38  0.59
 Arizona         11  0.44  0.52
 Arkansas         6  0.30  0.58
 California      55  0.56  0.33
 Colorado         9  0.45  0.46
 Connecticut      7  0.52  0.45
 Delaware         3  0.62  0.37
 DC               3  0.52  0.48
 Florida         29  0.47  0.47
 Georgia         16  0.44  0.52
 Hawaii           4  0.62  0.30
 Idaho            4  0.21  0.64
 Illinois        20  0.55  0.36
 Indiana         11  0.42  0.55
 Iowa             6  0.47  0.42
 Kansas           6  0.42  0.57
 Kentucky         8  0.39  0.53
 Louisiana        8  0.37  0.49
 Maryland        10  0.55  0.36
 Massachusetts   11  0.64  0.31
 Michigan        16  0.48  0.42
 Minnesota       10  0.49  0.43
 Mississippi      6  0.43  0.56
 Missouri        10  0.41  0.54
 Montana          3  0.43  0.49
 Nevada           6  0.49  0.46
 New_Hampshire    4  0.49  0.47
 New_Jersey      14  0.51  0.41
 New_Mexico       5  0.50  0.41
 New_York        29  0.62  0.33
 North_Carolina  15  0.45  0.46
 North_Dakota     3  0.39  0.54 
 Ohio            18  0.49  0.49
 Oklahoma         7  0.38  0.58
 Oregon           7  0.47  0.41
 Pennsylvania    20  0.49  0.45
 Rhode_Island     4  0.58  0.32
 South_Carolina   9  0.40  0.46
 South_Dakota     3  0.41  0.52
 Tennessee       11  0.34  0.59
 Texas           38  0.39  0.55
 Utah             6  0.21  0.70
 Vermont          3  0.62  0.31
 Virginia        13  0.47  0.47
 Washington      12  0.50  0.45
 West_Virginia    5  0.38  0.52
 Wisconsin       10  0.49  0.46
 Wyoming          3  0.33  0.64
! Maine and Nebraska are special in that 
  they can split their votes,
  so break them up into little states;
 Maine1           1   0.51  0.37  
 Maine2           1   0.51  0.37 
 Maine3           1   0.51  0.37  
 Maine4           1   0.51  0.37  
 Nebraska1        1   0.39  0.58
 Nebraska2        1   0.39  0.58
 Nebraska3        1   0.39  0.58
 Nebraska4        1   0.39  0.58
 Nebraska5        1   0.39  0.58
; 		
ENDDATA 		
		
CALC:
 @SET( 'TERSEO', 2); ! Turn of default output;
! Submodel (a) to convert popular support for each candidate in a state into
  a probability the state votes Democratic. This submodel assumes that the
  outcome of uncertain voters will be uniformly distributed. 
  Suppose for Colorado, the Republican committed fraction is 46% and
  the Democratic committed fraction is about 45%. Thus, the uncommitted
  fraction is 9%, then the Republican will win if he gets from 4% to 9% 
  from the uncertain voters. Thus, the model assumes the Republican
  probability of winning Colorado is 5/9 = 0.555555;
  @FOR( STATE(J):
   ! Subtract an uncertainty factor from each popularity fraction;
    POPDA = @SMAX(0,POPD(J) - UVD);
    POPRA = @SMAX(0,POPR(J) - UVR);
   ! Now assume the uncertain part of the population 
     distributes its vote uniformly between the two candidates;
    @IFC( POPDA + POPRA #LT# 1:
       PD(J) = @SMIN(1,@SMAX(0,(1-POPRA-0.5)/(1-POPDA-POPRA)));
      @ELSE
       PD(J) = POPDA;
        );
      );
!  Submodel (b) to compute overall probability of a win, given
    a probability for each state of a win, and its number of 
    electoral votes.
   WAYS( I, J) = Probability a Democrat electoral count of I-1,
                 using only the first J states;
! Before we include any states, there is no way;
 @FOR( KNT(I):
   WAYS(I,1) = 0;
     );
! Now do the first state;
  WAYS(1,1) = 1 - PD(1); ! Vote count of 0;
  WAYS(1+VOTES(1),1) = PD(1); ! Vote count of VOTES(1);
! Now the rest of the states;
 @FOR( STATE( J) | J #GT# 1:
  @FOR( KNT( I)| I - VOTES( J) #LE# 0:
    WAYS( I, J) = WAYS( I, J - 1)*(1 - PD(J));
     );
! We can get to I,J either without this state or with it;
@FOR( KNT( I)| I - VOTES( J) #GT# 0:
    WAYS( I, J) = WAYS( I, J - 1)*(1-PD(J))
                + WAYS( I - VOTES( J), J - 1)*PD(J);
    );
   );
! Write a simple report;
 @WRITE(@NEWLINE(1),' Probability of various outcomes for U.S. Electoral College.',@NEWLINE(1));
 TOTVOTES = @SUM( STATE(J): VOTES(J));
 SPLIT = @FLOOR( TOTVOTES/2);
 NEED = SPLIT + 1;
 @WRITE( UVD,' = under vote fraction, Democratic',@NEWLINE(1));
 @WRITE( UVR,' = under vote fraction, Republican',@NEWLINE(1));
 @WRITE( TOTVOTES,' = Number of votes.',@NEWLINE(1));
 @WRITE( SPLIT,' = Number of votes needed to tie.',@NEWLINE(1));
 @WRITE( NEED,' = Number of votes needed to win.',@NEWLINE(1));
 NSTATES = @SIZE(STATE);
 PDWIN = 1 - @SUM( KNT(I) | I-1 #LT# NEED: WAYS(I,NSTATES));
 @WRITE( PDWIN,' = Probability of at least ',NEED,' Democratic votes.',@NEWLINE(1));
 @WRITE( WAYS(SPLIT+1,NSTATES),' Probability of exactly ', SPLIT, ' votes.',@NEWLINE(1));
ENDCALC