#######################################
#
# File: NoRiskyWithSourcesAndTypes.py
# Author: Tyler de Ruiter with
# Michel Bierlaire, EPFL
# Date: Wed 28 March 2012
# Data File: DataIII.csv
#
#Exclude RISKY, edit 159 to ALTUNAVAIL, remove CHANGE, remove LMINCOME
#
#V1 = B_DELAY * DELAYIII + B_YOUNG * YOUNG + B_GENDER * GENDER + B_SORADIO * SORADIO + B_TYCONGTT * TYCONGTT + B_TYCRASH * TYCRASH
#
#V2 = ASC_SWITCH + ASC_ROUTE * ALROUTE + B_KNOWLEDGE * KNOWLEDGE
#
# POSTTT = 0.50 and 1.50
# COMPARE = 0.75 and 1.25
#######################################
from biogeme import *
from headers import *
from logit import *
from loglikelihood import *
from statistics import *
#Parameters to be estimated
# Arguments:
# 1 Name for report. Typically, the same as the variable
# 2 Starting value
# 3 Lower bound
# 4 Upper bound
# 5 0: estimate the parameter, 1: keep it fixed
ASC_SWITCH = Beta('ASC_SWITCH',0,-100,100,0)
ASC_DEPART = Beta('ASC_DEPART',0,-100,100,0)
ASC_ROUTE = Beta('ASC_ROUTE',0,-100,100,0)
ASC_MODE = Beta('ASC_MODE',0,-100,100,0)
ASC_DESTIN = Beta('ASC_DESTIN',0,-100,100,0)
ASC_ADD = Beta('ASC_ADD',0,-100,100,0)
ASC_CANCEL = Beta('ASC_CANCEL',0,-100,100,0)
ASC_NONE = Beta('ASC_NONE',0,-100,100,0)
B_ORIGCLOSED = Beta('B_ORIGCLOSED',0,-100,100,0)
B_ALTUNAVAIL = Beta('B_ALTUNAVAIL',0,-100,100,0)
B_KNOWLEDGE = Beta('B_KNOWLEDGE',0,-100,100,0)
B_DIFFERENCE = Beta('B_DIFFERENCE',0,-100,100,0)
B_CHANGE = Beta('B_CHANGE',0,-100,100,0)
B_COMPAREL = Beta('B_COMPAREL',0,-100,100,0)
B_COMPAREG = Beta('B_COMPAREG',0,-100,100,0)
B_VEHS = Beta('B_VEHS',0,-100,100,0)
B_COMMUTE = Beta('B_COMMUTE',0,-100,100,0)
B_AWARE = Beta('B_AWARE',0,-100,100,0)
B_COMMUTER = Beta('B_COMMUTER',0,-100,100,0)
B_CAR = Beta('B_CAR',0,-100,100,0)
B_MORNING = Beta('B_MORNING',0,-100,100,0)
B_EVENING = Beta('B_EVENING',0,-100,100,0)
B_TIME = Beta('B_TIME',0,-100,100,0)
B_VAR = Beta('B_VAR',0,-100,100,0)
B_BUSWAIT = Beta('B_BUSWAIT',0,-100,100,0)
B_BUSTRAN = Beta('B_BUSTRAN',0,-100,100,0)
B_LITE = Beta('B_LITE',0,-100,100,0)
B_ONTIME = Beta('B_ONTIME',0,-100,100,0)
B_LATE = Beta('B_LATE',0,-100,100,0)
B_WEATHER = Beta('B_WEATHER',0,-100,100,0)
B_CLASS = Beta('B_CLASS',0,-100,100,0)
B_SOSEE = Beta('B_SOSEE',0,-100,100,0)
B_SOVMS = Beta('B_SOVMS',0,-100,100,0)
B_SORADIO = Beta('B_SORADIO',0,-100,100,0)
B_SO511 = Beta('B_SO511',0,-100,100,0)
B_SOGPS = Beta('B_SOGPS',0,-100,100,0)
B_SOSMART = Beta('B_SOSMART',0,-100,100,0)
B_SOCOMP = Beta('B_SOCOMP',0,-100,100,0)
B_SOTRAV = Beta('B_SOTRAV',0,-100,100,0)
B_SOTV = Beta('B_SOTV',0,-100,100,0)
B_TYCONGTT = Beta('B_TYCONGTT',0,-100,100,0)
B_TYBUSTIME = Beta('B_TYBUSTIME',0,-100,100,0)
B_TYCAM = Beta('B_TYCAM',0,-100,100,0)
B_TYCRASH = Beta('B_TYCRASH',0,-100,100,0)
B_TYCONSTR = Beta('B_TYCONSTR',0,-100,100,0)
B_TYWEATHER = Beta('B_TYWEATHER',0,-100,100,0)
B_DELAY = Beta('B_DELAY',0,-100,100,0)
B_POSTINFOG = Beta('B_POSTINFOG',0,-100,100,0)
B_POSTINFOL = Beta('B_POSTINFOL',0,-100,100,0)
B_ALTINFO = Beta('B_ALTINFO',0,-100,100,0)
B_ALTBUSWAITS = Beta('B_ALTBUSWAITS',0,-100,100,0)
B_ALTBUSTRANS = Beta('B_ALTBUSTRANS',0,-100,100,0)
B_ALTLITES = Beta('B_ALTLITES',0,-100,100,0)
B_GENDER = Beta('B_GENDER',0,-100,100,0)
B_YOUNG = Beta('B_YOUNG',0,-100,100,0)
B_OLD = Beta('B_OLD',0,-100,100,0)
B_NONWHITE = Beta('B_NONWHITE',0,-100,100,0)
B_LMINCOME = Beta('B_LMINCOME',0,-100,100,0)
B_MHINCOME = Beta('B_MHINCOME',0,-100,100,0)
#Dummy Variables
#Variables established by examining correlation plots
#Delay (A comparison between Travel Time Post Information and Originally Perceived Travel Time)
DELAY = (POSTINFOTT - ORIGTT) * (POSTINFOTT != -1) * (ORIGTT != -1)
DELAYII = (DELAY/ORIGTT) * (POSTINFOTT != -1) * (ORIGTT != -1)
#Delay Categorical (A categorical version of Delay used for early steps in modeling)
POSTTT = (POSTINFOTT == -1) * (ORIGTT == -1) * 0 + (POSTINFOTT <= (0.50 * ORIGTT)) * (POSTINFOTT != -1) * 1 + (POSTINFOTT < ORIGTT) * (POSTINFOTT > (0.50 * ORIGTT)) * 2 + (POSTINFOTT == ORIGTT) * 3 + (POSTINFOTT >= (1.50 * ORIGTT)) * (ORIGTT != -1) * 5 + (POSTINFOTT > ORIGTT) * (POSTINFOTT < (1.50 * ORIGTT)) * (ORIGTT != -1) * 4
#Time Difference (A comparison between Alternative Travel Time and Travel Time Post Information)
DIFFERENCE = (ALTTT - POSTINFOTT) * (POSTINFOTT != -1) * (ALTTT != -1)
DIFFII = (DIFFERENCE/POSTINFOTT) * (POSTINFOTT != -1) * (ALTTT != -1)
#Time Difference Categorical
COMPARE = (ALTTT == -1) * (POSTINFOTT == -1) * 0 + (ALTTT <= (0.75 * POSTINFOTT)) * (ALTTT != -1) * 1 + (ALTTT < POSTINFOTT) * (ALTTT > (0.75 * POSTINFOTT)) * 2 + (ALTTT == POSTINFOTT) * 3 + (ALTTT >= (1.25 * POSTINFOTT)) * (POSTINFOTT != -1) * 5 + (ALTTT > POSTINFOTT) * (ALTTT < (1.25 * POSTINFOTT)) * (POSTINFOTT != -1) * 4
#Change (A comparison between Originally Perceived Travel Time and Alternative Travel Time)
CHANGE = ((ORIGTT - ALTTT)/ORIGTT) * (ALTTT != -1) * (ORIGTT != -1)
CHANGEII = (ALTTT - ORIGTT) * (ALTTT != -1) * (ORIGTT != -1)
#Knowledge of Alt TT (This takes a value of 1 whenever someone entered an estimate for alternative travel time)
KNOWLEDGE = (ALTTT != -1)
##NEW DUMMIES BASED ON ANALYSIS OF COMMENTS/ENTRIES
# ORIGCLOSED = list of observations where the original path was no longer passable and the Alternative Must be Taken
ORIGCLOSED = (OBS == 1) + (OBS == 4) + (OBS == 18) + (OBS == 33) + (OBS == 43) + (OBS == 61) + (OBS == 67) + (OBS == 76) + (OBS == 106) + (OBS == 140) + (OBS == 148) + (OBS == 161) + (OBS == 199) + (OBS == 207) + (OBS == 220) + (OBS == 223) + (OBS == 229) + (OBS == 239) + (OBS == 253) + (OBS == 265) + (OBS == 278) + (OBS == 284) + (OBS == 287) + (OBS == 305) + (OBS == 326)
#QUESTION = list of observations that were not found to make logical sense, many contianed zero descriptions of answers
QUESTION = (OBS == 72) + (OBS == 81) + (OBS == 91) + (OBS == 100) + (OBS == 142) + (OBS == 177) + (OBS == 181) + (OBS == 191) + (OBS == 200) + (OBS == 213) + (OBS == 214) + (OBS == 234) + (OBS == 261) + (OBS == 293) + (OBS == 329)
#ALTUNAVAIL = list of observations where the best alternative was not available at the time of decision
ALTUNAVAIL = (OBS == 50) + (OBS == 52) + (OBS == 71) + (OBS == 96) + (OBS == 147) + (OBS == 156) + (OBS == 159) + (OBS == 167) + (OBS == 178) + (OBS == 194) + (OBS == 197) + (OBS == 211) + (OBS == 215) + (OBS == 245)
#ALLCLEAR = list of observations where the traveler assumed the travel times to be irrelevant or "normal"
ALLCLEAR = (OBS == 34) + (OBS == 62) + (OBS == 135) + (OBS == 154) + (OBS == 164) + (OBS == 190) + (OBS == 247) + (OBS == 251) + (OBS == 256) + (OBS == 280) + (OBS == 298)
#CANCEL = list of observations where the participant canceled the trip
CANCEL = (OBS == 42) + (OBS == 81) + (OBS == 100) + (OBS == 134) + (OBS == 240)
#ZERODELAY = observations where POSTINFOTT == ORIGTT
ZERODELAY = (OBS == 10) + (OBS == 48) + (OBS == 54) + (OBS == 63) + (OBS == 77) + (OBS == 91) + (OBS == 108) + (OBS == 129) + (OBS == 154) + (OBS == 190) + (OBS == 200) + (OBS == 280) + (OBS == 298) + (OBS == 322)
#ZERODIFF = observations where POSTINFOTT == ALTTT
ZERODIFF = (OBS == 57) + (OBS == 59) + (OBS == 69) + (OBS == 70) + (OBS == 85) + (OBS == 91) + (OBS == 103) + (OBS == 117) + (OBS == 125) + (OBS == 143) + (OBS == 162) + (OBS == 187) + (OBS == 199) + (OBS == 200) + (OBS == 201) + (OBS == 207) + (OBS == 217) + (OBS == 226) + (OBS == 233) + (OBS == 237) + (OBS == 224) + (OBS == 269) + (OBS == 270) + (OBS == 274) + (OBS == 281) + (OBS == 290) + (OBS == 319) + (OBS == 322) + (OBS == 328)
#RISKY = observations where travelers made risky manuevers, e.g. not trying to maximize utility
RISKY = (OBS == 3) + (OBS == 51) + (OBS == 164)
# These two statements disallow time variables from being calculted for any of the previous categories
DELAYIII = DELAYII * (ORIGCLOSED != 1) * (QUESTION != 1) * (ALTUNAVAIL != 1) * (ALLCLEAR != 1) * (CANCEL != 1) * (ZERODELAY != 1) * (ZERODIFF != 1) * (RISKY != 1)
CHANGEIII = CHANGE * (ORIGCLOSED != 1) * (QUESTION != 1) * (ALTUNAVAIL != 1) * (ALLCLEAR != 1) * (CANCEL != 1) * (ZERODELAY != 1) * (ZERODIFF != 1) * (RISKY != 1)
# The Variables listed below were created and explored within the models
INPUT = (ORIGTT != -1) & (ORIGVAR != -1) & (ALTTT != -1) & (ALTVAR != -1) #Requires all input for times
VEHS = (NUMVEH >= 3) & (NUMVEH != -1) #2 or More Cars Available
AWARENESS = (NOTAWARE != 1) #Assumes people are aware information exists
COMMUTER = (ORIGIN <= 3) & (ORIGIN != -1) & (DESTINATION <= 3) & (DESTINATION != -1) #O-D represents home>work or work>home (assume class=work)
CAR = (MODE == 1) #Person used car trip ##Based on descriptions
MORNING = (STARTTRIP >= 700) & (STARTTRIP <= 900) #2 Hour Morning Peak Period (estimated)
EVENING = (STARTTRIP >= 1600) & (STARTTRIP <= 1800) #2 hour Evening Peak Period (estimated)
BUSWAITS = (ORIGBUSWAIT - 1) * (MODE == 2) #Converts BusWait into correct units
BUSTRANS = (ORIGTRANSFER - 1) * (MODE == 2) #Converts BusTransfer into correct units
ALTBUSWAITS = (ALTBUSWAIT - 1) * (MODE == 2) #Converts AltBusWait into correct units
ALTBUSTRANS = (ALTTRANSF - 1) * (MODE == 2) #Converts AltBusTransfer into correct units
ALTLITE = (ALTLITES - 1) #Converts AltLights into correct units
BUSDUMA = (BUSWAITS > -1) * (ALTBUSWAITS > -1) #Throws out -1 values for Buswait
BUSDUMB = (BUSTRANS > -1) * (ALTBUSTRANS > -1) #Throws out -1 values for BusTransfer
LITES = (ORIGLITES - 1) #Converts Lights into correct units
LITEDUM = (LITES > -1) * (ALTLITES > -1) #Throws out -1 values for Lights
ONTIME = (TARDY < 3) & (TARDY != -1) #Person must be on time
LATE = (TARDY == 3) #Person can be 15 minutes late
BADWEATHER = (WEATHER > 1) #Rain or Snow
PRETRIP = (INFOCLASS == 1) #Person receives pre-trip information
SOSEE = (SOURCEOBSERVE == 1) #Person received info via Sight
SOVMS = (SOURCEVMS == 1) #Person received info via VMS
SORADIO = (SOURCERADIO == 1) #Person received info via Radio
SO511 = (SOURCE511 == 1) #Person received info via 511
SOGPS = (SOURCEGPS == 1) #Person received info via GPS
SOSMART = (SOURCESMART == 1) #Person received info via SmartPhone
SOCOMP = (SOURCECOMP == 1) #Person received info via Computer
SOTRAV = (SOURCETRAVELERS == 1) #Person received info via other Travelers
TYCONGTT = (TYPECONG == 1) #Information type = Congestion/travel time
TYBUSTIME = (TYPEBUS == 1) #Information type = Bus Times
TYCAM = (TYPECAMS == 1) #Information type = Webcams
TYCRASH = (TYPECRASH == 1) #Information type = Crashes
TYCONSTR = (TYPECONST == 1) #Information type = Construction
POSTTTG = (POSTTT == 5) #Post Info Time Much Greater than Previous
POSTTTL = (POSTTT == 1) #Post Info Time Much Less than Previous
POSTINFODUM = (POSTINFOTT != -1) #Dummy removes -1 for PostInfoTT if used
ALDEPART = (ALTDEPART == 1) #Person's alternative was Departure Time change
ALROUTE = (ALTROUTE == 1) #Person's alternative was Route change
ALMODE = (ALTMODE == 1) #Person's alternative was Mode change
ALADD = (ALTADDSTOP == 1) #Person's alternative was Additional Stop
ALCANCEL = (ALTNOTRIP == 1) #Person's alternative was Cancel Trip
ALTINFO = (INFOFORALT == 1) #Person had information provided for Alternative
GENDER = (SEX == 1) #Person = Male
YOUNG = (AGE <= 5) & (AGE != -1) #Person = younger than 35
OLD = (AGE >= 10) #Person = older than 55
NONWHITE = (RACE != -1) & (RACE != 5) #Person is of Multi-Cultural descent
LMINCOME = (INCOME <= 3) & (INCOME != -1) #Person makes between 10k and 50k
MHINCOME = (INCOME > 4) #Person makes above 60k
COMPAREL = (COMPARE == 1)
COMPAREG = (COMPARE == 5)
exclude = (RISKY == 1)
# Utility functions
V1 = B_DELAY * DELAYIII + B_YOUNG * YOUNG + B_GENDER * GENDER + B_SORADIO * SORADIO + B_TYCONGTT * TYCONGTT + B_TYCRASH * TYCRASH
V2 = ASC_SWITCH + ASC_ROUTE * ALROUTE + B_KNOWLEDGE * KNOWLEDGE
# Associate utility functions with the numbering of alternatives
V = {1: V2,
2: V1}
# Associate the availability conditions with the alternatives
ALT0_AV = (ORIGCLOSED != 1)
ALT1_AV = (ALTUNAVAIL != 1)
av = {1: ALT1_AV,
2: ALT0_AV}
# The choice model is a logit, with availability conditions
prob = logit_av(V,av,CHOICE)
# Defines an itertor on the data
rowIterator('obsIter')
# DEfine the likelihood function for the estimation
BIOGEME_OBJECT.ESTIMATE = Sum(loglikelihood(prob,exclude),'obsIter')
# Statistics
nullLoglikelihood(av,'obsIter')
choiceSet = [1,2]
cteLoglikelihood(choiceSet,CHOICE,'obsIter')
availabilityStatistics(av,'obsIter')
BIOGEME_OBJECT.PARAMETERS['optimizationAlgorithm'] = "CFSQP"