コード例 #1
0
def estimate_wrap(V, av, CHOICE, database, panelEst, ModNameSeqence, Draws):
    '''
    #
    #
    #
    '''

    if panelEst:
        #database.panel('ID')
        # Conditional to B_TIME_RND, the likelihood of one observation is
        # given by the logit model (called the kernel)
        obsprob = exp(models.loglogit(V, av, CHOICE))

        # Conditional to B_TIME_RND, the likelihood of all observations for
        # one individual (the trajectory) is the product of the likelihood of
        # each observation.
        condprobIndiv = PanelLikelihoodTrajectory(obsprob)
        if Draws > 0:
            condprobIndiv = MonteCarlo(condprobIndiv)

        logprob = log(condprobIndiv)
    else:
        prob = exp(models.loglogit(V, av, CHOICE))
        if Draws > 0:
            prob = MonteCarlo(prob)
        logprob = log(prob)

    if Draws > 0:
        biogeme = bio.BIOGEME(database, logprob, numberOfDraws=Draws)
    else:
        biogeme = bio.BIOGEME(database, logprob)

    biogeme.modelName = ModNameSeqence

    return biogeme.estimate()
コード例 #2
0
V2 = ASC_SM + \
     B_TIME_RND * SM_TT_SCALED + \
     B_COST * SM_COST_SCALED
V3 = ASC_CAR + \
     B_TIME_RND * CAR_TT_SCALED + \
     B_COST * CAR_CO_SCALED

# Associate utility functions with the numbering of alternatives
V = {1: V1, 2: V2, 3: V3}

# Associate the availability conditions with the alternatives
CAR_AV_SP = DefineVariable('CAR_AV_SP', CAR_AV * (SP != 0), database)
TRAIN_AV_SP = DefineVariable('TRAIN_AV_SP', TRAIN_AV * (SP != 0), database)

av = {1: TRAIN_AV_SP, 2: SM_AV, 3: CAR_AV_SP}

obsprob = models.logit(V, av, CHOICE)
condprobIndiv = PanelLikelihoodTrajectory(obsprob)
logprob = log(MonteCarlo(condprobIndiv))


class test_12(unittest.TestCase):
    def testEstimation(self):
        biogeme = bio.BIOGEME(database, logprob, numberOfDraws=5, seed=10)
        results = biogeme.estimate()
        self.assertAlmostEqual(results.data.logLike, -4705.638407401872, 2)


if __name__ == '__main__':
    unittest.main()
コード例 #3
0
# Associate utility functions with the numbering of alternatives
V = {1: V1,
     2: V2,
     3: V3}

# Associate the availability conditions with the alternatives
av = {1: TRAIN_AV_SP,
      2: SM_AV,
      3: CAR_AV_SP}
 
# Conditional to B_TIME_RND, we have a logit model (called the kernel)
prob = models.logit(V, av, CHOICE)

# Add Panel Structure
pnl_prob = PanelLikelihoodTrajectory(prob)
# We integrate over B_TIME_RND using Monte-Carlo
logprob = log(MonteCarlo(pnl_prob))

# Define level of verbosity
logger = msg.bioMessage()
#logger.setSilent()
#logger.setWarning()
logger.setGeneral()
#logger.setDetailed()

# These notes will be included as such in the report file.
userNotes = ('Example of a mixture of logit models with three alternatives, '
             'approximated using Monte-Carlo integration.')

# Create the Biogeme object
コード例 #4
0
    ASC_SM_RND[i] + B_TIME_RND[i] * SM_TT_SCALED + B_COST[i] * SM_COST_SCALED
    for i in range(numberOfClasses)
]
V3 = [
    ASC_CAR_RND[i] + B_TIME_RND[i] * CAR_TT_SCALED + B_COST[i] * CAR_CO_SCALED
    for i in range(numberOfClasses)
]
V = [{1: V1[i], 2: V2[i], 3: V3[i]} for i in range(numberOfClasses)]

# Associate the availability conditions with the alternatives
av = {1: TRAIN_AV_SP, 2: SM_AV, 3: CAR_AV_SP}

# The choice model is a discrete mixture of logit, with availability conditions
# We calculate the conditional probability for each class
prob = [
    PanelLikelihoodTrajectory(models.logit(V[i], av, CHOICE))
    for i in range(numberOfClasses)
]

# Conditional to the random variables, likelihood for the individual.
probIndiv = PROB_class0 * prob[0] + PROB_class1 * prob[1]

# We integrate over the random variables using Monte-Carlo
logprob = log(MonteCarlo(probIndiv))

# Define level of verbosity
logger = msg.bioMessage()
#logger.setSilent()
#logger.setWarning()
logger.setGeneral()
#logger.setDetailed()
コード例 #5
0
V1 = [ASC_1[i]  + beta_shcostperdist1[i] *(sharecost/distance) + beta_shtime[i] * sharetime  + SIGMA_SH_MAASRND[i]  for i in range(numberOfClasses)]

V2 = [ASC_2[i] +  beta_maascostperdist2[i] *(maascost/distance)+ beta_maastime1[i]*(maastime1) + beta_maastime2[i]* maastime2  +\
beta_extra[i]*(extra) *extra + SIGMA_SH_MAASRND[i]  for i in range(numberOfClasses)]

V3 = [ASC_3[i] + beta_totcostperdist[i] * (currcost/distance) for i in range(numberOfClasses)]

V = [{1: V1[i],
      2: V2[i],
      3: V3[i]} for i in range(numberOfClasses)]

# Associate the availability conditions with the alternatives
av = {1: availability1,2: availability2,3: availability3}
# The choice model is a discrete mixture of logit, with availability conditions
# We calculate the conditional probability for each class
prob = [PanelLikelihoodTrajectory(models.logit(V[i], av, choice)) for i in range(numberOfClasses)]

# Class membership model
W = CLASS_MAAS_1 + beta_enthu_class1 * factor1  + beta_fru_class1 * factor2  + beta_edu2_1 * edu_HBO + \
    beta_inc2_1*inc_mid + beta_age * age_10_60 + beta_freq * mediumfreq
PROB_class0 = models.logit({0: W, 1: 0}, None, 0)
PROB_class1 = models.logit({0: W, 1: 0}, None, 1)

# Conditional to the random variables, likelihood for the individual.
probIndiv = PROB_class0 * prob[0] + PROB_class1 * prob[1]
# We integrate over the random variables using Monte-Carlo
logprob = log(MonteCarlo(probIndiv))

# Define level of verbosity
logger = msg.bioMessage()
#logger.setSilent()
コード例 #6
0
V2 = [ASC_SM_RND[i] + B_TIME_RND[i] * SM_TT_SCALED + B_COST[i] * SM_COST_SCALED
      for i in range(numberOfClasses)]
V3 = [ASC_CAR_RND[i] + B_TIME_RND[i] * CAR_TT_SCALED + B_COST[i] * CAR_CO_SCALED
      for i in range(numberOfClasses)]
V = [{1: V1[i],
      2: V2[i],
      3: V3[i]} for i in range(numberOfClasses)]

# Associate the availability conditions with the alternatives
av = {1: TRAIN_AV_SP,
      2: SM_AV,
      3: CAR_AV_SP}

# The choice model is a discrete mixture of logit, with availability conditions
# We calculate the conditional probability for each class
prob = [MonteCarlo(PanelLikelihoodTrajectory(models.logit(V[i], av, CHOICE)))
        for i in range(numberOfClasses)]

# Conditional to the random variables, likelihood for the individual.
probIndiv = PROB_class0 * prob[0] + PROB_class1 * prob[1]

# We integrate over the random variables using Monte-Carlo
logprob = log(probIndiv)

# Define level of verbosity
logger = msg.bioMessage()
#logger.setSilent()
#logger.setWarning()
logger.setGeneral()
#logger.setDetailed()
コード例 #7
0
V3 = [
    ASC_3[i] + beta_parktime3[i] * (parktime) + beta_parkcost3[i] *
    (parkcost) / 10 + beta_dist3[i] * sharedist / 10
    for i in range(numberOfClasses)
]

V4 = [ASC_4[i] for i in range(numberOfClasses)]

V = [{1: V1[i], 2: V2[i], 3: V3[i], 4: V4[i]} for i in range(numberOfClasses)]

# Associate the availability conditions with the alternatives
av = {1: availability1, 2: availability2, 3: availability3, 4: availability4}
# The choice model is a discrete mixture of logit, with availability conditions
# We calculate the conditional probability for each class
prob = [
    PanelLikelihoodTrajectory(models.logit(V[i], av, choice))
    for i in range(numberOfClasses)
]

# Class membership model
W = CLASS_MAAS + beta_enthu * factor1  + beta_fru * factor2 + beta_inc2*inc_mid +\
beta_edu2 * edu_HBO + beta_fam * fam_1 + beta_fam * fam_2 + beta_age * age_10_60
PROB_class0 = models.logit({1: W, 0: 0}, None, 1)
PROB_class1 = models.logit({1: W, 0: 0}, None, 0)

# Conditional to the random variables, likelihood for the individual.
probIndiv = PROB_class0 * prob[0] + PROB_class1 * prob[1]
# We integrate over the random variables using Monte-Carlo
logprob = log(MonteCarlo(probIndiv))

# Define level of verbosity
コード例 #8
0
def cv_estimate_model(V,
                      Draws,
                      ModName,
                      train,
                      myRandomNumberGenerators,
                      COST_SCALE_CAR=100,
                      COST_SCALE_PUB=100):
    db_train = db.Database("swissmetro_train", train)
    db_train.setRandomNumberGenerators(myRandomNumberGenerators)
    globals().update(db_train.variables)
    #locals().update(db_train.variables)
    db_train.panel('ID')

    #variables
    CAR_AV_SP = DefineVariable('CAR_AV_SP', CAR_AV * (SP != 0), db_train)
    TRAIN_AV_SP = DefineVariable('TRAIN_AV_SP', TRAIN_AV * (SP != 0), db_train)

    SM_COST = SM_CO * (GA == 0)
    TRAIN_COST = TRAIN_CO * (GA == 0)

    ###Parameters to be estimated (Note not all parameters are used in all models!)
    ##Attributes
    #Alternative specific constants
    ASC_CAR = Beta('ASC_CAR', 0, None, None, 1)
    ASC_TRAIN = Beta('ASC_TRAIN', 0, None, None, 0)
    ASC_SM = Beta('ASC_SM', 0, None, None, 0)

    #Cost (Note: Assumed generic)
    B_COST = Beta('B_COST', 0, None, None, 0)
    B_COST_BUSINESS = Beta('B_COST_BUSINESS', 0, None, None, 0)
    B_COST_PRIVATE = Beta('B_COST_PRIVATE', 0, None, None, 0)

    #Time
    B_TIME = Beta('B_TIME', 0, None, None, 0)
    B_TIME_CAR = Beta('B_TIME_CAR', 0, None, None, 0)
    B_TIME_TRAIN = Beta('B_TIME_TRAIN', 0, None, None, 0)
    B_TIME_SM = Beta('B_TIME_SM', 0, None, None, 0)
    B_TIME_PUB = Beta('B_TIME_PUB', 0, None, None, 0)
    B_TIME_CAR_BUSINESS = Beta('B_TIME_CAR_BUSINESS', 0, None, None, 0)
    B_TIME_TRAIN_BUSINESS = Beta('B_TIME_TRAIN_BUSINESS', 0, None, None, 0)
    B_TIME_SM_BUSINESS = Beta('B_TIME_SM_BUSINESS', 0, None, None, 0)
    B_TIME_PUB_BUSINESS = Beta('B_TIME_PUB_BUSINESS', 0, None, None, 0)
    B_TIME_CAR_PRIVATE = Beta('B_TIME_CAR_PRIVATE', 0, None, None, 0)
    B_TIME_TRAIN_PRIVATE = Beta('B_TIME_TRAIN_PRIVATE', 0, None, None, 0)
    B_TIME_SM_PRIVATE = Beta('B_TIME_SM_PRIVATE', 0, None, None, 0)
    B_TIME_PUB_PRIVATE = Beta('B_TIME_PUB_PRIVATE', 0, None, None, 0)

    #HE (Note: Not available for car)
    B_HE = Beta('B_HE', 0, None, None, 0)
    B_HE_TRAIN = Beta('B_HE_TRAIN', 0, None, None, 0)
    B_HE_SM = Beta('B_HE_SM', 0, None, None, 0)
    B_HE_BUSINESS = Beta('B_HE_BUSINESS', 0, None, None, 0)
    B_HE_TRAIN_BUSINESS = Beta('B_HE_TRAIN_BUSINESS', 0, None, None, 0)
    B_HE_SM_BUSINESS = Beta('B_HE_SM_BUSINESS', 0, None, None, 0)
    B_HE_PRIVATE = Beta('B_HE_PRIVATE', 0, None, None, 0)
    B_HE_TRAIN_PRIVATE = Beta('B_HE_TRAIN_PRIVATE', 0, None, None, 0)
    B_HE_SM_PRIVATE = Beta('B_HE_SM_PRIVATE', 0, None, None, 0)

    #Seats (Note: Only avaliable for SM)
    B_SEATS = Beta('B_SEATS', 0, None, None, 0)

    ##Characteristics
    #Age
    B_AGE_1_TRAIN = Beta('B_AGE_1_TRAIN', 0, None, None, 1)  #Note: Reference
    B_AGE_2_TRAIN = Beta('B_AGE_2_TRAIN', 0, None, None, 0)
    B_AGE_3_TRAIN = Beta('B_AGE_3_TRAIN', 0, None, None, 0)
    B_AGE_4_TRAIN = Beta('B_AGE_4_TRAIN', 0, None, None, 0)
    B_AGE_5_TRAIN = Beta('B_AGE_5_TRAIN', 0, None, None, 0)
    B_AGE_6_TRAIN = Beta('B_AGE_6_TRAIN', 0, None, None, 0)
    B_AGE_1_SM = Beta('B_AGE_1_SM', 0, None, None, 1)  #Note: Reference
    B_AGE_2_SM = Beta('B_AGE_2_SM', 0, None, None, 0)
    B_AGE_3_SM = Beta('B_AGE_3_SM', 0, None, None, 0)
    B_AGE_4_SM = Beta('B_AGE_4_SM', 0, None, None, 0)
    B_AGE_5_SM = Beta('B_AGE_5_SM', 0, None, None, 0)
    B_AGE_6_SM = Beta('B_AGE_6_SM', 0, None, None, 0)
    B_AGE_1_PUB = Beta('B_AGE_1_PUB', 0, None, None, 1)  #Note: Reference
    B_AGE_2_PUB = Beta('B_AGE_2_PUB', 0, None, None, 0)
    B_AGE_3_PUB = Beta('B_AGE_3_PUB', 0, None, None, 0)
    B_AGE_4_PUB = Beta('B_AGE_4_PUB', 0, None, None, 0)
    B_AGE_5_PUB = Beta('B_AGE_5_PUB', 0, None, None, 0)
    B_AGE_6_PUB = Beta('B_AGE_6_PUB', 0, None, None, 0)
    B_AGE_ADULTS_TRAIN = Beta('B_AGE_TRAIN_ADULTS', 0, None, None, 0)
    B_AGE_ADULTS_SM = Beta('B_AGE_ADULTS_SM', 0, None, None, 0)
    B_AGE_ADULTS_PUB = Beta('B_AGE_ADULTS_PUB', 0, None, None, 0)

    #Luggage
    B_LUGGAGE_TRAIN = Beta('B_LUGGAGE_TRAIN', 0, None, None, 0)
    B_LUGGAGE_SM = Beta('B_LUGGAGE_SM', 0, None, None, 0)
    B_LUGGAGE_PUB = Beta('B_LUGGAGE_PUB', 0, None, None, 0)

    #Gender
    B_MALE_TRAIN = Beta('B_MALE_TRAIN', 0, None, None, 0)
    B_MALE_SM = Beta('B_MALE_SM', 0, None, None, 0)
    B_MALE_PUB = Beta('B_MALE_PUB', 0, None, None, 0)

    #Purpose
    B_BUSINESS = Beta('B_BUSINESS', 0, None, None, 0)
    B_BUSINESS_TRAIN = Beta('B_BUSINESS_TRAIN', 0, None, None, 0)
    B_BUSINESS_SM = Beta('B_BUSINESS_SM', 0, None, None, 0)
    B_PRIVATE = Beta('B_PRIVATE', 0, None, None, 0)
    B_PRIVATE_TRAIN = Beta('B_PRIVATE_TRAIN', 0, None, None, 0)
    B_PRIVATE_SM = Beta('B_PRIVATE_SM', 0, None, None, 0)
    B_COMMUTER = Beta('B_COMMUTER', 0, None, None, 0)
    B_COMMUTER_TRAIN = Beta('B_COMMUTER_TRAIN', 0, None, None, 0)
    B_COMMUTER_SM = Beta('B_COMMUTER_SM', 0, None, None, 0)

    #GA
    B_GA = Beta('B_GA', 0, None, None, 0)
    B_GA_TRAIN = Beta('B_GA_TRAIN', 0, None, None, 0)
    B_GA_SM = Beta('B_GA_SM', 0, None, None, 0)

    #First
    B_FIRST_TRAIN = Beta('B_FIRST_TRAIN', 0, None, None, 0)
    B_FIRST_SM = Beta('B_FIRST_SM', 0, None, None, 0)
    B_FIRST = Beta('B_FIRST', 0, None, None, 0)

    ##Non linearization
    #Cost
    q_COST = Beta('q_COST', 1, None, None, 0)

    #Time
    q_TIME = Beta('q_TIME', 1, None, None, 0)
    q_TIME_TRAIN = Beta('q_TIME_TRAIN', 1, None, None, 0)
    q_TIME_SM = Beta('q_TIME_SM', 1, None, None, 0)
    q_TIME_CAR = Beta('q_TIME_CAR', 1, None, None, 0)
    q_TIME_PUB = Beta('q_TIME_PUB', 1, None, None, 0)

    #HE
    q_HE = Beta('q_HE', 1, None, None, 0)

    ##Nesting parameter
    MU = Beta('MU', 1, 0, 1, 0)

    ##ML RANDOM GENERIC TIME LOGNORMAL
    BETA_TIME_mean = Beta('BETA_TIME_mean', 0, None, None, 0)
    BETA_TIME_std = Beta('BETA_TIME_std', 1, None, None, 0)
    BETA_TIME_random = -exp(BETA_TIME_mean + BETA_TIME_std *
                            bioDraws('BETA_TIME_random', 'NORMAL'))

    ##ML RANDOM SPECIFIC TIME TRAIN LOGNORMAL
    BETA_TIME_TRAIN_mean = Beta('BETA_TIME_TRAIN_mean', 0, None, None, 0)
    BETA_TIME_TRAIN_std = Beta('BETA_TIME_TRAIN_std', 1, None, None, 0)
    BETA_TIME_TRAIN_random = -exp(BETA_TIME_TRAIN_mean + BETA_TIME_TRAIN_std *
                                  bioDraws('BETA_TIME_TRAIN_random', 'NORMAL'))

    ##ML RANDOM SPECIFIC TIME SM  LOGNORMAL
    BETA_TIME_SM_mean = Beta('BETA_TIME_SM_mean', 0, None, None, 0)
    BETA_TIME_SM_std = Beta('BETA_TIME_SM_std', 1, None, None, 0)
    BETA_TIME_SM_random = -exp(BETA_TIME_SM_mean + BETA_TIME_SM_std *
                               bioDraws('BETA_TIME_SM_random', 'NORMAL'))

    ##ML RANDOM SPECIFIC TIME CAR LOGNORMAL
    BETA_TIME_CAR_mean = Beta('BETA_TIME_CAR_mean', 0, None, None, 0)
    BETA_TIME_CAR_std = Beta('BETA_TIME_CAR_std', 1, None, None, 0)
    BETA_TIME_CAR_random = -exp(BETA_TIME_CAR_mean + BETA_TIME_CAR_std *
                                bioDraws('BETA_TIME_CAR_random', 'NORMAL'))

    ##ML RANDOM GENERIC COST LOGNORMAL
    BETA_COST_mean = Beta('BETA_COST_mean', 0, None, None, 0)
    BETA_COST_std = Beta('BETA_COST_std', 1, None, None, 0)
    BETA_COST_random = -exp(BETA_COST_mean + BETA_COST_std *
                            bioDraws('BETA_COST_random', 'NORMAL'))

    ##ML RANDOM GENERIC HE LOGNORMAL
    BETA_HE_mean = Beta('BETA_HE_mean', 0, None, None, 0)
    BETA_HE_std = Beta('BETA_HE_std', 1, None, None, 0)
    BETA_HE_random = -exp(BETA_HE_mean +
                          BETA_HE_std * bioDraws('BETA_HE_random', 'NORMAL'))

    ##ML RANDOM GENERIC TIME NORMAL
    BETA_TIME_mean_Norm = Beta('BETA_TIME_mean_Norm', 0, None, None, 0)
    BETA_TIME_std_Norm = Beta('BETA_TIME_std_Norm', 1, None, None, 0)
    BETA_TIME_random_Norm = BETA_TIME_mean_Norm + BETA_TIME_std_Norm * bioDraws(
        'BETA_TIME_random_Norm', 'NORMAL')

    ##ML RANDOM SPECIFIC TIME TRAIN LOGNORMAL
    BETA_TIME_TRAIN_mean_Norm = Beta('BETA_TIME_TRAIN_mean_Norm', 0, None,
                                     None, 0)
    BETA_TIME_TRAIN_std_Norm = Beta('BETA_TIME_TRAIN_std_Norm', 1, None, None,
                                    0)
    BETA_TIME_TRAIN_random_Norm = BETA_TIME_TRAIN_mean_Norm + BETA_TIME_TRAIN_std_Norm * bioDraws(
        'BETA_TIME_TRAIN_random_Norm', 'NORMAL')

    ##ML RANDOM SPECIFIC TIME SM  NORMAL
    BETA_TIME_SM_mean_Norm = Beta('BETA_TIME_SM_mean_Norm', 0, None, None, 0)
    BETA_TIME_SM_std_Norm = Beta('BETA_TIME_SM_std_Norm', 1, None, None, 0)
    BETA_TIME_SM_random_Norm = BETA_TIME_SM_mean_Norm + BETA_TIME_SM_std_Norm * bioDraws(
        'BETA_TIME_SM_random_Norm', 'NORMAL')

    ##ML RANDOM SPECIFIC TIME CAR NORMAL
    BETA_TIME_CAR_mean_Norm = Beta('BETA_TIME_CAR_mean_Norm', 0, None, None, 0)
    BETA_TIME_CAR_std_Norm = Beta('BETA_TIME_CAR_std_Norm', 1, None, None, 0)
    BETA_TIME_CAR_random_Norm = BETA_TIME_CAR_mean_Norm + BETA_TIME_CAR_std_Norm * bioDraws(
        'BETA_TIME_CAR_random_Norm', 'NORMAL')

    ##ML RANDOM GENERIC COST LOGNORMAL
    BETA_COST_PUB_mean = Beta('BBETA_COST_PUB_mean', 0, None, None, 0)
    BETA_COST_PUB_std = Beta('BBETA_COST_PUB_std', 1, None, None, 0)
    BETA_COST_PUB_random = -exp(BETA_COST_PUB_mean + BETA_COST_PUB_std *
                                bioDraws('BBETA_COST_PUB_random', 'NORMAL'))

    ##ML RANDOM GENERIC COST NORMAL
    BETA_COST_mean_Norm = Beta('BETA_COST_mean_Norm', 0, None, None, 0)
    BETA_COST_std_Norm = Beta('BETA_COST_std_Norm', 1, None, None, 0)
    BETA_COST_random_Norm = BETA_COST_mean_Norm + BETA_COST_std_Norm * bioDraws(
        'BETA_COST_random_Norm', 'NORMAL')

    ##ML RANDOM GENERIC HE NORMAL
    BETA_HE_mean_Norm = Beta('BETA_HE_mean_Norm', 0, None, None, 0)
    BETA_HE_std_Norm = Beta('BETA_HE_std_Norm', 1, None, None, 0)
    BETA_HE_random_Norm = BETA_HE_mean_Norm + BETA_HE_std_Norm * bioDraws(
        'BETA_HE_random_Norm', 'NORMAL')
    '''
    ***********************************************************************************************
    '''

    #PUBLIC
    TRAIN_COST_SCALED = DefineVariable('TRAIN_COST_SCALED',\
                                       TRAIN_COST / COST_SCALE_PUB,db_train)
    SM_COST_SCALED = DefineVariable('SM_COST_SCALED', SM_COST / COST_SCALE_PUB,
                                    db_train)

    #CAR
    CAR_COST_SCALED = DefineVariable('CAR_COST_SCALED',
                                     CAR_CO / COST_SCALE_CAR, db_train)
    '''
    ***********************************************************************************************
    '''
    ##Scaling 'COST', 'TRAVEL-TIME' and 'HE' by a factor of 100 and adding the scaled variables to the database

    TRAIN_TT_SCALED = DefineVariable('TRAIN_TT_SCALED',\
                                     TRAIN_TT / 100.0,db_train)

    TRAIN_HE_SCALED = DefineVariable('TRAIN_HE_SCALED',\
                                       TRAIN_HE / 100, db_train)

    SM_TT_SCALED = DefineVariable('SM_TT_SCALED', SM_TT / 100.0, db_train)

    SM_HE_SCALED = DefineVariable('SM_HE_SCALED',\
                                       SM_HE / 100, db_train)

    CAR_TT_SCALED = DefineVariable('CAR_TT_SCALED', CAR_TT / 100, db_train)

    ###Defining new variables and adding columns to the database
    #Age
    AGE_1 = DefineVariable('AGE_1', (AGE == 1),
                           db_train)  #don't scale because is cathegorical
    AGE_2 = DefineVariable('AGE_2', (AGE == 2), db_train)
    AGE_3 = DefineVariable('AGE_3', (AGE == 3), db_train)
    AGE_4 = DefineVariable('AGE_4', (AGE == 4), db_train)
    AGE_5 = DefineVariable('AGE_5', (AGE == 5), db_train)
    AGE_6 = DefineVariable('AGE_6', (AGE == 6), db_train)

    #Purpose
    PRIVATE = DefineVariable("PRIVATE", (PURPOSE == 1), db_train)
    COMMUTER = DefineVariable("COMMUTER", (PURPOSE == 2), db_train)
    BUSINESS = DefineVariable("BUSINESS", (PURPOSE == 3), db_train)

    #Model Estimation
    av = {3: CAR_AV_SP, 1: TRAIN_AV_SP, 2: SM_AV}
    obsprob = exp(models.loglogit(V, av, CHOICE))
    condprobIndiv = PanelLikelihoodTrajectory(obsprob)
    logprob = log(MonteCarlo(condprobIndiv))
    bg = bio.BIOGEME(db_train, logprob, numberOfDraws=Draws)
    bg.modelName = ModName
    result = bg.estimate()
    return result
コード例 #9
0
ファイル: test_15.py プロジェクト: jiaodaxiaozi/biogeme
V2 = {1: V21, 2: V22, 3: V23}

# Associate the availability conditions with the alternatives

CAR_AV_SP = DefineVariable('CAR_AV_SP', CAR_AV * (SP != 0), database)
TRAIN_AV_SP = DefineVariable('TRAIN_AV_SP', TRAIN_AV * (SP != 0), database)

av = {1: TRAIN_AV_SP, 2: SM_AV, 3: CAR_AV_SP}

# Class membership model
W_OTHER = Beta('W_OTHER', 0.798, 0, 1, 0)
probClass1 = 1 - W_OTHER
probClass2 = W_OTHER

# The choice model is a discrete mixture of logit, with availability conditions
prob1 = PanelLikelihoodTrajectory(models.logit(V1, av, CHOICE))
prob2 = PanelLikelihoodTrajectory(models.logit(V2, av, CHOICE))
probIndiv = probClass1 * prob1 + probClass2 * prob2
logprob = log(MonteCarlo(probIndiv))


class test_15(unittest.TestCase):
    def testEstimation(self):
        biogeme = bio.BIOGEME(database, logprob, numberOfDraws=5, seed=10)
        results = biogeme.estimate()
        self.assertAlmostEqual(results.data.logLike, -4104.417442345575, 2)


if __name__ == '__main__':
    unittest.main()