The Norm of Gradient Issue in Mixed Logit Model with Panel Data

20 views
Skip to first unread message

Natchaphon Leungbootnak

unread,
Apr 7, 2025, 2:31:10 AMApr 7
to Biogeme
Dear professor Bierlaire,

I hope you are doing well. I tried to run a mixed logit model with panel data to predict ML choice by using a normal distribution with a random coefficient. However, there was NaN issue in the norm of the gradient: "The norm of the gradient at asc_ML=0, asc_ML_s=1, b_time=0, b_toll=0 is nan: g=nan, nan, nan, nan
Numerical problems in calculating the analytical hessian. Finite differences is tried instead."

I tried to use a uniform distribution instead of a normal distribution, but I still got the same issue. Could you please guide me for this issue? I have attached my code as follows:

df_train = pd.read_csv(DDIR+'trainregular.csv')
df_test = pd.read_csv(DDIR+'testregular.csv')

database_train = Database('RUMmix', df_train)
database_test = Database('RUMmix', df_test)

database_train.panel('randid')

# dependent variable
choice = Variable('choice12')

#generic variable
time1 = Variable('time1')
time2 = Variable('time2')
toll1 = Variable('toll1')
toll2 = Variable('toll2')

#Beta 1=ML 2=GPL
#GPL is base choice

asc_ML = Beta('asc_ML', 0, None, None, 0)

b_time = Beta('b_time', 0, None, None, 0)
b_toll = Beta('b_toll', 0, None, None, 0)

np.random.seed(seed=90267)

asc_ML_s = Beta('asc_ML_s', 1, -10, 10, 0)
asc_ML_rnd = asc_ML + asc_ML_s * bioDraws('asc_ML_rnd', 'NORMAL_ANTI')

utility_ML = asc_ML_rnd + b_time*time1 + b_toll*toll1

utility_GPL = b_time*time2 + b_toll*toll2

utilities = {1: utility_ML, 2: utility_GPL}
obsprob = logit(utilities, None, choice)
condprobIndiv = PanelLikelihoodTrajectory(obsprob)
log_choice_probability = log(MonteCarlo(condprobIndiv))
biogeme_train = BIOGEME(database_train, log_choice_probability, number_of_draws=1500, seed=1223)
biogeme_train.modelName = 'RUMmix'
results = biogeme_train.estimate()

Best regards,
Natchaphon Leungbootnak

Michel Bierlaire

unread,
Apr 8, 2025, 2:28:51 AMApr 8
to natcha...@gmail.com, Michel Bierlaire, Biogeme
Try
asc_ML_s = Beta('asc_ML_s', 10, -100, 100, 0)

Also, estimate first a model ignoring the panel structure. Once it works, you can account for the panel again.
> --
> 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 visit https://groups.google.com/d/msgid/biogeme/c6095f4f-0b91-439b-8129-4fbe5f64fd1en%40googlegroups.com.

Reply all
Reply to author
Forward
0 new messages