Issue with HCM Combining Latent variable and Panel effect

14 views
Skip to first unread message

ענבל גליקמן

unread,
May 1, 2025, 2:06:16 AMMay 1
to Biogeme

Dear Prof. Berlier,

I need your assistance with an issue I am encountering while estimating a HCM that combines a latent variable and panel data.

When I estimate the latent model separately and the panel model separately, both yield very good results. However, when I combine them into a single model, I receive the error shown below:

"The norm of the gradient at ASC_Bike=0, ASC_PT=0, ASC_Walk=0, BCost_PT=0, BProEnv_Bike=0, BProEnv_PT=0, BProEnv_Walk=0, BTime_Bike=0, BTime_PT=0, BTime_Walk=0, BTime_car=0, Const_PN2_pred=0, Const_PN3_pred=0, Const_PN4_pred=0, Const_ProEnv=0 is nan: g=nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan"

This is the relevent part of the code I am currently using:

database.panel("SubjectID")

omega = bioDraws('omega', 'NORMAL')
LV_Err = Beta('LV_Err', 1, 0.001, None, 0)

LV = (Beta('Const_ProEnv', 0, None, None, 0) * 1 +
      Beta('alfa_IsFemale', 0, None, None, 0) * IsFemale +
      Beta('alfa_IsOld', 0, None, None, 0) * IsOld +
      Beta('alfa_IsYoung', 0, None, None, 0) * IsYoung +
      Beta('alfa_IsHighIncome', 0, None, None, 0) * IsHighIncome +
      Beta('alfa_IsAcademic', 0, None, None, 0) * IsAcademic +
      Beta('alfa_CarsPerLicense', 0, None, None, 0) * CarsPerLicense +
      Beta('alfa_IsEV', 0, None, None, 0) * IsEV +
      Beta('alfa_HasBike', 0, None, None, 0) * HasBike +
      Beta('alfa_IsStudent', 0, None, None, 0) * IsStudent +
      LV_Err * omega)

def logpdf_normal(y, mu, sigma):
    return -log(sigma) - 0.5 * log(2 * 3.1416) - 0.5 * ((y - mu) / sigma) ** 2

SD_PN1 = Beta('SD_PN1', 1, 0.001, None, 0)
SD_PN2 = Beta('SD_PN2', 1, 0.001, None, 0)
SD_PN3 = Beta('SD_PN3', 1, 0.001, None, 0)
SD_PN4 = Beta('SD_PN4', 1, 0.001, None, 0)

PN1_pred = Beta('Const_PN1_pred', 0, None, None, 1)*1 + Beta('lambda_PN1', 1, None, None, 1) * LV
PN2_pred = Beta('Const_PN2_pred', 0, None, None, 0)*1 + Beta('lambda_PN2', 1, None, None, 0) * LV
PN3_pred = Beta('Const_PN3_pred', 0, None, None, 0)*1 + Beta('lambda_PN3', 1, None, None, 0) * LV
PN4_pred = Beta('Const_PN4_pred', 0, None, None, 0)*1 + Beta('lambda_PN4', 1, None, None, 0) * LV

loglike_PN1 = logpdf_normal(PN1, PN1_pred, SD_PN1)
loglike_PN2 = logpdf_normal(PN2, PN2_pred, SD_PN2)
loglike_PN3 = logpdf_normal(PN3, PN3_pred, SD_PN3)
loglike_PN4 = logpdf_normal(PN4, PN4_pred, SD_PN4)

loglike_measurement = loglike_PN1 + loglike_PN2 + loglike_PN3 + loglike_PN4

Panel_Err = Beta('Panel_Err', 1, None, None, 0)
RND = bioDraws('RND', 'NORMAL')
B_Car_RND  = Panel_Err * RND
B_PT_RND   = Panel_Err * RND
B_Bike_RND = Panel_Err * RND
B_Walk_RND = Panel_Err * RND

ASC_PT = Beta('ASC_PT', 0, -10, 10, 0)
ASC_Bike = Beta('ASC_Bike', 0, -10, 10, 0)
ASC_Walk = Beta('ASC_Walk', 0, -10, 10, 0)
BTime_car = Beta('BTime_car', 0, -10, 10, 0)
BTime_PT = Beta('BTime_PT', 0, -10, 10, 0)
BCost_PT = Beta('BCost_PT', 0, -10, 10, 0)
BTime_Bike = Beta('BTime_Bike', 0, -10, 10, 0)
BTime_Walk = Beta('BTime_Walk', 0, -10, 10, 0)
BProEnv_Bike = Beta('BProEnv_Bike', 0, -10, 10, 0)
BProEnv_Walk = Beta('BProEnv_Walk', 0, -10, 10, 0)
BProEnv_PT = Beta('BProEnv_PT', 0, -10, 10, 0)

V1 = BTime_car * CarTDispValue + B_Car_RND
V2 = ASC_PT + BTime_PT * PTTDispValue + BCost_PT * EuroPTCValue + BProEnv_PT * LV + B_PT_RND
V3 = ASC_Bike + BTime_Bike * BikeTime_1 + BProEnv_Bike * LV + B_Bike_RND
V4 = ASC_Walk + BTime_Walk * WalkTime_1 + BProEnv_Walk * LV + B_Walk_RND

V = {1: V1, 2: V2, 3: V3, 4: V4}
av = {1: av1, 2: av2, 3: av3, 4: av4}

obsprob = loglike_measurement + models.loglogit(V, av, Choice)
condprobIndiv = PanelLikelihoodTrajectory(obsprob)
logprob = log(MonteCarlo(condprobIndiv))

biogeme = bio.BIOGEME(database, logprob, number_of_draws=200)
biogeme.modelName = "Latent_Panel_SingleError_Final"
biogeme.algorithm = "scipy_lbfgsb"
results = biogeme.estimate()
print(results.get_estimated_parameters())

 Thanks a lot for your help!

Inbal Glickman

Reply all
Reply to author
Forward
0 new messages