michelbierlaire / mooc-discrete-choice

3 stars 1 forks source link

Mixed logit model #2

Open Jaletarfasa opened 1 year ago

Jaletarfasa commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

Thank you so much Professor for the response.

Any help is greatly appreciated. I have difficulty estimating mixed logit model based on the following information and need help.I tried to go through the biogeme documentation and unable to see discussion related with unlabelled format modeling using mixed logit model. 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)

On Thu, Jun 29, 2023 at 11:46 PM Michel Bierlaire @.***> wrote:

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.

— Reply to this email directly, view it on GitHub https://github.com/michelbierlaire/mooc-discrete-choice/issues/2#issuecomment-1614265568, or unsubscribe https://github.com/notifications/unsubscribe-auth/A4KWMRXONVJXGSNB53NNQ6LXNZ743ANCNFSM6AAAAAAZY2LPMU . You are receiving this because you authored the thread.Message ID: @.***>