latent cross nested model

88 views
Skip to first unread message

daqing hu

unread,
Jan 27, 2021, 3:35:45 AMJan 27
to Biogeme
Dear Professor Birelaire, 
I am writing an LC-CNL model to estimate the joint selection behavior of  travel paths and  mode.
I have three path and four model ,so there are seven nests and twelve alternatives.
I assume there are 3 classes.

The code is as follows :
 
import numpy as np
import pandas as pd
import biogeme.database as db
import biogeme.biogeme as bio
import biogeme.models as models
import biogeme.messaging as msg
from biogeme.expressions import Beta, DefineVariable, bioDraws, \
    PanelLikelihoodTrajectory, MonteCarlo, log

# import data
df = pd.read_csv("C:\\Users\\70424\\Desktop\\python\\model_data3.txt",sep="\t",encoding="gbk",header=None)

df1=df.iloc[:,[0,5,7,19,20,21,22,23,18,25,27,28,29,24,26,30]]
df1.columns=["id","CSN","MSN","DHHT","DHT","DHB","DST","DSB","model","depar","OT","sex","age","wd","choice","TT"]

database = db.Database("chengmian",df1)
globals().update(database.variables)

TT = TT / 10000
DHHT = DHHT / 10000
DHT = DHT / 10000
DHB = DHB / 10000
DST = DST / 10000
DSB = DSB / 10000

# Parameters to be estimated. One version for each latent class.
numberOfClasses = 3

ASC_1 = [Beta(f'ASC_1_class{i}', 0, None, None, 1) for i in range(numberOfClasses)]
ASC_2 = [Beta(f'ASC_2_class{i}', 0, None, None, 0) for i in range(numberOfClasses)]
ASC_3 = [Beta(f'ASC_3_class{i}', 0, None, None, 0) for i in range(numberOfClasses)]
ASC_4 = [Beta(f'ASC_4_class{i}', 0, None, None, 0) for i in range(numberOfClasses)]
ASC_5 = [Beta(f'ASC_5_class{i}', 0, None, None, 0) for i in range(numberOfClasses)]
ASC_6 = [Beta(f'ASC_6_class{i}', 0, None, None, 0) for i in range(numberOfClasses)]
ASC_7 = [Beta(f'ASC_7_class{i}', 0, None, None, 0) for i in range(numberOfClasses)]
ASC_8 = [Beta(f'ASC_8_class{i}', 0, None, None, 0) for i in range(numberOfClasses)]
ASC_9 = [Beta(f'ASC_9_class{i}', 0, None, None, 0) for i in range(numberOfClasses)]
ASC_10 = [Beta(f'ASC_10_class{i}', 0, None, None, 0) for i in range(numberOfClasses)]
ASC_11 = [Beta(f'ASC_11_class{i}', 0, None, None, 0) for i in range(numberOfClasses)]
ASC_12 = [Beta(f'ASC_12_class{i}', 0, None, None, 0) for i in range(numberOfClasses)]

B_TT_1 = [Beta(f'B_TT_1_class{i}', 0, None, None, 0) for i in range(numberOfClasses)]
B_TT_2 = [Beta(f'B_TT_2_class{i}', 0, None, None, 0) for i in range(numberOfClasses)]
B_TT_3 = [Beta(f'B_TT_3_class{i}', 0, None, None, 0) for i in range(numberOfClasses)]
B_TT_4 = [Beta(f'B_TT_4_class{i}', 0, None, None, 0) for i in range(numberOfClasses)]
B_TT_5 = [Beta(f'B_TT_5_class{i}', 0, None, None, 0) for i in range(numberOfClasses)]
B_TT_6 = [Beta(f'B_TT_6_class{i}', 0, None, None, 0) for i in range(numberOfClasses)]
B_TT_7 = [Beta(f'B_TT_7_class{i}', 0, None, None, 0) for i in range(numberOfClasses)]
B_TT_8 = [Beta(f'B_TT_8_class{i}', 0, None, None, 0) for i in range(numberOfClasses)]
B_TT_9 = [Beta(f'B_TT_9_class{i}', 0, None, None, 0) for i in range(numberOfClasses)]
B_TT_10 = [Beta(f'B_TT_10_class{i}', 0, None, None, 0) for i in range(numberOfClasses)]
B_TT_11 = [Beta(f'B_TT_11_class{i}', 0, None, None, 0) for i in range(numberOfClasses)]
B_TT_12 = [Beta(f'B_TT_12_class{i}', 0, None, None, 0) for i in range(numberOfClasses)]

B_OT_1 = [Beta(f'B_OT_1_class{i}', 0, None, None, 0) for i in range(numberOfClasses)]
B_OT_2 = [Beta(f'B_OT_2_class{i}', 0, None, None, 0) for i in range(numberOfClasses)]
B_OT_3 = [Beta(f'B_OT_3_class{i}', 0, None, None, 0) for i in range(numberOfClasses)]
B_OT_4 = [Beta(f'B_OT_4_class{i}', 0, None, None, 0) for i in range(numberOfClasses)]
B_OT_5 = [Beta(f'B_OT_5_class{i}', 0, None, None, 0) for i in range(numberOfClasses)]
B_OT_6 = [Beta(f'B_OT_6_class{i}', 0, None, None, 0) for i in range(numberOfClasses)]
B_OT_7 = [Beta(f'B_OT_7_class{i}', 0, None, None, 0) for i in range(numberOfClasses)]
B_OT_8 = [Beta(f'B_OT_8_class{i}', 0, None, None, 0) for i in range(numberOfClasses)]
B_OT_9 = [Beta(f'B_OT_9_class{i}', 0, None, None, 0) for i in range(numberOfClasses)]
B_OT_10 = [Beta(f'B_OT_10_class{i}', 0, None, None, 0) for i in range(numberOfClasses)]
B_OT_11 = [Beta(f'B_OT_11_class{i}', 0, None, None, 0) for i in range(numberOfClasses)]
B_OT_12 = [Beta(f'B_OT_12_class{i}', 0, None, None, 0) for i in range(numberOfClasses)]


B_CSN_1 = [Beta(f'B_CSN_1_class{i}', 0, None, None, 0) for i in range(numberOfClasses)]
B_CSN_2 = [Beta(f'B_CSN_2_class{i}', 0, None, None, 0) for i in range(numberOfClasses)]
B_CSN_3 = [Beta(f'B_CSN_3_class{i}', 0, None, None, 0) for i in range(numberOfClasses)]
B_CSN_4 = [Beta(f'B_CSN_4_class{i}', 0, None, None, 0) for i in range(numberOfClasses)]
B_CSN_5 = [Beta(f'B_CSN_5_class{i}', 0, None, None, 0) for i in range(numberOfClasses)]
B_CSN_6 = [Beta(f'B_CSN_6_class{i}', 0, None, None, 0) for i in range(numberOfClasses)]
B_CSN_7 = [Beta(f'B_CSN_7_class{i}', 0, None, None, 0) for i in range(numberOfClasses)]
B_CSN_8 = [Beta(f'B_CSN_8_class{i}', 0, None, None, 0) for i in range(numberOfClasses)]
B_CSN_9 = [Beta(f'B_CSN_9_class{i}', 0, None, None, 0) for i in range(numberOfClasses)]
B_CSN_10 = [Beta(f'B_CSN_10_class{i}', 0, None, None, 0) for i in range(numberOfClasses)]
B_CSN_11 = [Beta(f'B_CSN_11_class{i}', 0, None, None, 0) for i in range(numberOfClasses)]
B_CSN_12 = [Beta(f'B_CSN_12_class{i}', 0, None, None, 0) for i in range(numberOfClasses)]

B_DH = [Beta(f'B_DH_class{i}', 0, None, None, 0) for i in range(numberOfClasses)]
B_DS = [Beta(f'B_DS_class{i}', 0, None, None, 0) for i in range(numberOfClasses)]

V1 = [ASC_1[i] + B_TT_1[i]* TT   + B_CSN_1[i] * CSN   for i in range(numberOfClasses)]
         
V2 = [ASC_2[i] + B_TT_2[i] * TT  + B_DH[i] * DHB + B_DS[i] * DSB  + B_CSN_2[i] * CSN    for i in range(numberOfClasses)]
      
V3 = [ASC_3[i] + B_TT_3[i] * TT  + B_DH[i] * DHT + B_CSN_3[i] * CSN  for i in range(numberOfClasses)]
      
V4 = [ASC_4[i] + B_TT_4[i] * TT + B_DH[i] * DHHT + B_CSN_4[i] * CSN  for i in range(numberOfClasses)]
      
V5 = [ASC_5[i] + B_TT_5[i]* TT   + B_CSN_5[i] * CSN  for i in range(numberOfClasses)]
     
V6 = [ASC_6[i] + B_TT_6[i] * TT  + B_DH[i] * DHB + B_DS[i] * DSB  + B_CSN_6[i] * CSN    for i in range(numberOfClasses)]
      
V7 = [ASC_7[i] + B_TT_7[i] * TT  + B_DH[i] * DHT + B_CSN_7[i] * CSN for i in range(numberOfClasses)]
      
V8 = [ASC_8[i] + B_TT_8[i] * TT   + B_DH[i] * DHHT + B_CSN_8[i] * CSN  for i in range(numberOfClasses)]
      
V9 = [ASC_9[i] + B_TT_9[i]* TT  + B_CSN_9[i] * CSN   for i in range(numberOfClasses)]
               
V10 = [ASC_10[i] + B_TT_10[i] * TT  + B_DH[i] * DHB + B_CSN_10[i] * CSN   for i in range(numberOfClasses)]
      
V11 = [ASC_11[i] + B_TT_11[i] * TT  + B_DH[i] * DHT + B_CSN_11[i] * CSN  for i in range(numberOfClasses)]
      
V12 = [ASC_12[i] + B_TT_12[i] * TT  + B_DH[i] * DHHT + B_CSN_12[i] * CSN  for i in range(numberOfClasses)]

V = [{1: V1[i],
     2: V2[i],
     3: V3[i],
     4: V4[i],
     5: V5[i],
     6: V6[i],
     7: V7[i],
     8: V8[i],
     9: V9[i],
     10: V10[i],
     11: V11[i],
     12: V12[i]   
     }  for i in range(numberOfClasses)]

car = [Beta(f'car_class{i}', 1, 1, None, 0) for i in range(numberOfClasses)]
bus = [Beta(f'bus_class{i}', 1, 1, None, 0) for i in range(numberOfClasses)]
pt =[ Beta(f'pt_class{i}', 1, 1, None, 0) for i in range(numberOfClasses)]
gt =[ Beta(f'gt_class{i}', 1, 1, None, 0) for i in range(numberOfClasses)]
ep =[ Beta(f'ep_class{i}', 1, 1, None, 0) for i in range(numberOfClasses)]
p = [Beta(f'p_class{i}', 1, 1, None, 0) for i in range(numberOfClasses)]
lp =[ Beta(f'lp_class{i}', 1, 1, None, 0) for i in range(numberOfClasses)]

#Assume that the positional parameters are fixed in each latent class
alpha_car={1:0.5,2:0,3:0,4:0,5:0.5,6:0,7:0,8:0,9:0.5,10:0,11:0,12:0}
alpha_bus={1:0,2:0.5,3:0,4:0,5:0,6:0.5,7:0,8:0,9:0,10:0.5,11:0,12:0}
alpha_pt ={1:0,2:0,3:0.5,4:0,5:0,6:0,7:0.5,8:0,9:0,10:0,11:0.5,12:0}
alpha_gt ={1:0,2:0,3:0,4:0.5,5:0,6:0,7:0,8:0.5,9:0,10:0,11:0,12:0.5}
alpha_ep ={1:0.5,2:0.5,3:0.5,4:0.5,5:0,6:0,7:0,8:0,9:0,10:0,11:0,12:0}
alpha_p  ={1:0,2:0,3:0,4:0,5:0.5,6:0.5,7:0.5,8:0.5,9:0,10:0,11:0,12:0}
alpha_lp ={1:0,2:0,3:0,4:0,5:0,6:0,7:0,8:0,9:0.5,10:0.5,11:0.5,12:0.5}

nest_car = [[car[i], alpha_car ] for i in range(numberOfClasses)]
nest_bus = [[bus[i], alpha_bus ]for i in range(numberOfClasses)]
nest_pt  = [[pt[i], alpha_pt   ]for i in range(numberOfClasses)]
nest_gt  = [[gt[i], alpha_gt   ] for i in range(numberOfClasses)]
nest_ep  = [[ep[i], alpha_ep   ] for i in range(numberOfClasses)]
nest_p   = [[p[i], alpha_p     ]for i in range(numberOfClasses)]
nest_lp  = [[lp[i], alpha_lp   ]for i in range(numberOfClasses)]

nests =nest_car,nest_bus,nest_pt,nest_gt,nest_ep,nest_p,nest_lp

prob = [ models.cnl(V[i], None, nests[i], choice)  for i in range(numberOfClasses)]

CLASS_age = [Beta(f'CLASS_age_class{i}', 0, None, None, 0) for i in range(numberOfClasses-1)]
CLASS_sex = [Beta(f'CLASS_sex_class{i}', 0, None, None, 0) for i in range(numberOfClasses-1)]

W = [CLASS_age[i]*age   + CLASS_sex[i] * sex  for i in range(numberOfClasses-1)]

PROB_class0 = models.logit({0: W[0], 1: W[1],2:0}, None, 0)
PROB_class1 = models.logit({0: W[0], 1:W[1],2:0 }, None, 1)
PROB_class2 = models.logit({0: W[0], 1:W[1],2:0 }, None, 2)

probIndiv = PROB_class0 * prob[0] + PROB_class1 * prob[1]  +PROB_class2 * prob[2]

logprob = log(probIndiv)

biogeme = bio.BIOGEME(database, logprob)

biogeme.modelName = 'bi_lc3'

# Estimate the parameters.
results = biogeme.estimate()
pandasResults = results.getEstimatedParameters()
print(pandasResults)

##

The above code is applied to 1000 pieces of data.As a result, the estimated value of each unknown parameter is 0  .
I am very anxious, how can I correct my code? 
Thank you.
 




Bierlaire Michel

unread,
Jan 27, 2021, 5:44:05 AMJan 27
to dhu2...@gmail.com, Bierlaire Michel, Biogeme
Difficult to say. 
Did you first estimate a simple model (logit, or nested)? 
It is always better to start with simple models and increase the complexity gradually to identify where the problem comes from. 


--
You received this message because you are subscribed to the Google Groups "Biogeme" group.
To unsubscribe from this group and stop receiving emails from it, send an email to biogeme+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/biogeme/3da28553-720b-4a3b-a97d-67ce802d49aan%40googlegroups.com.

daqing hu

unread,
Jan 28, 2021, 2:44:59 AMJan 28
to Biogeme
Thank  you for your  reply .According to your suggestion, I used the estimated value of the GNL model as the initial value  . The result is still wrong .   So I checked the code and corrected it to: 

In order to save computing time, I  estimated two latent classes . ) 

nest_car_1 = car[0], alpha_car
nest_bus_1 = bus[0], alpha_bus
nest_pt_1 = pt[0], alpha_pt
nest_gt_1 = gt[0], alpha_gt
nest_ep_1 = ep[0], alpha_ep
nest_p_1 = p[0], alpha_p
nest_lp_1 = lp[0], alpha_lp

nest_car_2 = car[1], alpha_car
nest_bus_2 = bus[1], alpha_bus
nest_pt_2 = pt[1], alpha_pt
nest_gt_2 = gt[1], alpha_gt
nest_ep_2 = ep[1], alpha_ep
nest_p_2 = p[1], alpha_p
nest_lp_2 = lp[1], alpha_lp

# Represent the nests of each latent class separately
nests1 =nest_car_1,nest_bus_1,nest_pt_1,nest_gt_1,nest_ep_1,nest_p_1,nest_lp_1
nests2 =nest_car_2,nest_bus_2,nest_pt_2,nest_gt_2,nest_ep_2,nest_p_2,nest_lp_2

NEST=[nests1,nests2]

prob= [models.cnl(V[i], None, NEST[i], choice) for i in range(numberOfClasses)]

W = [CLASS_age[i]*age   + CLASS_sex[i] * sex  for i in range(numberOfClasses-1)]

PROB_class0 = models.logit({0: W[0],1:0}, None, 0)
PROB_class1 = models.logit({0: W[0],1:0 }, None, 1)

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


After a day, the program is still running, and the result is not yet available .
Is my code wrong? Or is the model too complicated? Or is my data volume not enough to support such a complex model?  

tips.png
This is the only hint i get . 

kongdexue kong

unread,
Oct 14, 2021, 2:37:37 AMOct 14
to Biogeme
Hello, have you solved this program? I also met the same problem with you.
Reply all
Reply to author
Forward
0 new messages