Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mixed logit model #2

Open
Jaletarfasa opened this issue Jun 29, 2023 · 3 comments
Open

Mixed logit model #2

Jaletarfasa opened this issue Jun 29, 2023 · 3 comments

Comments

@Jaletarfasa
Copy link

while trying to estimate mixed logit model I encountered the following error.Any help is greatly appreciated.
BiogemeError: The norm of the gradient is inf: g=[-1.79769313e+308 -1.79769313e+308 -1.79769313e+308 -1.79769313e+308
-1.79769313e+308]

@michelbierlaire
Copy link
Owner

This issue is discussed in the FAQ: http://biogeme.epfl.ch/#help
It is probably due to the initial value of a scale parameter that is too close to zero.

@Jaletarfasa
Copy link
Author

image
Unlabelled format with three alternatives(1,2,3)represented with Concept,third alternative is the status quo;Attributes are water supply(levels 1,2,3),Water quality(two levels:1=yes,0=otherwise),and prices(5 levels:3,5,10,15 and 20).I want to apply mixed logit with error components and Quasi-Monte Carlo Simulation,code given below.Any help please.

Assume you have a pandas DataFrame df with your data

database = db.Database("myDatabase", df)

Use the names of the variables in your data

chosen_alternative = database.variables["Chosen"]
#water_supply = database.variables["Wsup"]
#water_quality = database.variables["Wqua"]
#price_increase = database.variables["Price"]

Define parameters to be estimated (betas)

#beta1 = ex.Beta('beta1', 0, None, None, 0)
#beta2 = ex.Beta('beta2', 0, None, None, 0)
#beta3 = ex.Beta('beta3', 0, None, None, 0)
#beta3_stddev = ex.Beta('beta3_stddev', 0.5, None, None, 0)

Define the alternative specific constants

ASC1 = ex.Beta('ASC1', 1, None, None, 0)
ASC2 = ex.Beta('ASC2', 1, None, None, 0)

Define parameters to be estimated

beta1_mean = ex.Beta('beta1_mean', 0, -1000, 1000, 0) # Mean of the distribution for beta1
beta1_stddev = ex.Beta('beta1_stddev', 0, None, None, 0) # Standard deviation of the distribution for beta1
beta1 = beta1_mean + beta1_stddev * ex.bioDraws('beta1', 'UNIFORM')

beta2_mean = ex.Beta('beta2_mean', 0, None, None, 0) # Parameter for water_quality
beta2_stddev = ex.Beta('beta2_stddev', 0, None, None, 0) # Standard deviation of the distribution for beta2
beta2 = beta2_mean + beta2_stddev * ex.bioDraws('beta2', 'NORMAL')

beta3_mean = ex.Beta('beta3_mean', 0, None, None, 1) # Parameter for price_increase
beta3_stddev = ex.Beta('beta3_stddev', 0, None, None, 0) # Standard deviation of the distribution for beta3
beta3 = beta3_mean + beta3_stddev * ex.bioDraws('beta3', 'UNIFORM')

Define utility functions

V0 = beta1 * water_supply + beta2 * water_quality + beta3 * price_increase
V1 =ASC1 + beta1 * water_supply + beta2 * water_quality + beta3 * price_increase
V2 =ASC1 + beta1 * water_supply + beta2 * water_quality + beta3 * price_increase

Associate utility functions with the numbering of alternatives

V = {1: V0, 2: V1,3:V2}

Define the availability of alternatives

You should adjust these based on the specific structure of your data

Define the availability of alternatives

#av = {1: database.variables['Concept'] == 1,

2: database.variables['Concept'] == 2,

3: database.variables['Concept'] == 3}

av = {1:1,2:1,3:1}
num_draws = 10 # Number of Halton draws
draws = ex.bioDraws('draws', 'HALTON')

Estimate the parameters with a maximum of 1000 iterations

#results = biogeme.estimate(maxiter=1000)

Define the model. The choice model is a logit, with availability conditions

prob = models.logit(V, av, chosen_alternative)

Define the log likelihood function

logprob = ex.MonteCarlo(ex.log(prob))

Create the Biogeme object

biogeme = bio.BIOGEME(database, logprob)

Estimate the parameters

#results = biogeme.estimate()

Get the estimated parameters

#parameters = results.getEstimatedParameters()

Print the parameters

#print(parameters)

Estimate the parameters

results = biogeme.estimate()

Get the estimated parameters

parameters = results.getEstimatedParameters()

Print the parameters

print(parameters)

@Jaletarfasa
Copy link
Author

Jaletarfasa commented Jun 30, 2023 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants