Page 1 of 1

negative sign in the range of log-uniform distribution

Posted: 22 Jun 2023, 18:47
by sethyash52
Hello Prof. Stephane,

I have considered that the coefficients of alternative attributes follows log-uniform distribution by taking the exponential of uniform distribution. But standard deviation, which represents the range of log-uniform distribution is coming out negative in the purchase price variable. I have a doubt that, can the range be negative. I have attached the code and results below.

Code: Select all

# ################################################################# #
#### LOAD LIBRARY AND DEFINE CORE SETTINGS                       ####
# ################################################################# #

### Clear memory
rm(list = ls())

### Load Apollo library
library(apollo)

### Initialise code
apollo_initialise()

### Set core controls
apollo_control = list(
  modelName       = "MMNL_Model_2(b1)_All_negative_log_uniform_CT&CFs_in_EV_VAs Only",
  modelDescr      = "Error components logit model on Swiss route choice data, uncorrelated Lognormals in preference space, with panel effect term",
  indivID         = "Person_ID",  
  mixing          = TRUE,
  nCores          = 23,
  outputDirectory = "New Output_3"
)

# ################################################################# #
#### LOAD DATA AND APPLY ANY TRANSFORMATIONS                     ####
# ################################################################# #

setwd("D:\\Yash")

getwd()

# ################################################################# #
#### LOAD DATA AND APPLY ANY TRANSFORMATIONS                     ####
# ################################################################# #

database = read.csv("",header=TRUE)
# ################################################################# #
#### DEFINE MODEL PARAMETERS                                     ####
# ################################################################# #

### Vector of parameters, including any that are kept fixed in estimation
apollo_beta = c(mu_log_b_pp        = -3,
                sigma_log_b_pp     = -0.01,
                mu_log_b_oc        = -3,
                sigma_log_b_oc     = -0.01,
                mu_log_b_ct        = -3,
                sigma_log_b_ct     = -0.01,
                b_r  =  0,
                b_cf         = 0,
                b_e      = 0,
                asc_CV             = 0,
                asc_EV             = 0
)

### Vector with names (in quotes) of parameters to be kept fixed at their starting value in apollo_beta, use apollo_beta_fixed = c() if none
apollo_fixed = c("asc_CV")

# ################################################################# #
#### DEFINE RANDOM COMPONENTS                                    ####
# ################################################################# #

### Set parameters for generating draws
apollo_draws = list(
  interDrawsType = "halton",
  interNDraws    = 2000,
  interUnifDraws = c("draws_pp","draws_oc","draws_ct"),
  interNormDraws = c(),
  intraDrawsType = "halton",
  intraNDraws    = 0,
  intraUnifDraws = c(),
  intraNormDraws = c()
)

### Create random parameters
apollo_randCoeff = function(apollo_beta, apollo_inputs){
  randcoeff = list()
  
  randcoeff[["b_pp"]]     = -exp( mu_log_b_pp + sigma_log_b_pp * draws_pp )
  randcoeff[["b_oc"]]     = -exp( mu_log_b_oc + sigma_log_b_oc * draws_oc )
  randcoeff[["b_ct"]]     = -exp( mu_log_b_ct + sigma_log_b_ct * draws_ct )
  
  return(randcoeff)
}

# ################################################################# #
#### GROUP AND VALIDATE INPUTS                                   ####
# ################################################################# #

apollo_inputs = apollo_validateInputs()

# ################################################################# #
#### DEFINE MODEL AND LIKELIHOOD FUNCTION                        ####
# ################################################################# #

apollo_probabilities=function(apollo_beta, apollo_inputs, functionality="estimate_settings$bootstrapSE=TRUE"){
  
  ### Function initialisation: do not change the following three commands
  ### Attach inputs and detach after function exit
  apollo_attach(apollo_beta, apollo_inputs)
  on.exit(apollo_detach(apollo_beta, apollo_inputs))
  
  ### Create list of probabilities P
  P = list()
  
  ### List of utilities: these must use the same names as in mnl_settings, order is irrelevant
  V = list()
  V[["alt1"]] = asc_EV + b_pp*ppEV + b_oc*ocEV + b_ct*ctEV/60 + b_r*rEV/100 + b_e*eEV + b_cf*cfEV 
  V[["alt2"]] = asc_CV + b_pp*ppCV + b_oc*ocCV                + b_r*rCV/100  
  
  ### Define settings for MNL model component
  mnl_settings = list(
    alternatives  = c(alt1=1, alt2=2),
    avail         = list(alt1=1, alt2=1),
    choiceVar     = choiceVehicle,
    utilities     = V
  )
  
  ### Compute probabilities using MNL model
  P[["model"]] = apollo_mnl(mnl_settings, functionality)
  
  ### Take product across observation for same individual
  P = apollo_panelProd(P, apollo_inputs, functionality)
  
  ### Average across inter-individual draws
  P = apollo_avgInterDraws(P, apollo_inputs, functionality)
  
  ### Prepare and return outputs of function
  P = apollo_prepareProb(P, apollo_inputs, functionality)
  return(P)
}

# ################################################################# #
#### MODEL ESTIMATION                                            ####
# ################################################################# #

model = apollo_estimate(apollo_beta, apollo_fixed,apollo_probabilities, apollo_inputs)

# ################################################################# #
#### MODEL OUTPUTS                                               ####
# ################################################################# #

# ----------------------------------------------------------------- #
#---- FORMATTED OUTPUT (TO SCREEN)                               ----
# ----------------------------------------------------------------- #

apollo_modelOutput(model)

# ----------------------------------------------------------------- #
#---- FORMATTED OUTPUT (TO FILE, using model name)               ----
# ----------------------------------------------------------------- #

apollo_saveOutput(model)

Code: Select all

Estimates:
  Estimate        s.e.   t.rat.(0)    Rob.s.e. Rob.t.rat.(0)
mu_log_b_pp       -2.64205     0.79778      -3.312     0.90306        -2.926
sigma_log_b_pp    -9.81895     6.73971      -1.457     7.39476        -1.328
mu_log_b_oc      -20.86137     0.32342     -64.503     0.23174       -90.020
sigma_log_b_oc    22.06079     0.35885      61.477     0.26607        82.913
mu_log_b_ct      -27.36272     0.46771     -58.503     0.42348       -64.613
sigma_log_b_ct    33.78168     0.57546      58.704     0.52272        64.626
b_r                0.08890     0.02735       3.250     0.02683         3.313
b_cf               0.24145     0.08970       2.692     0.08747         2.760
b_e               -0.14755     0.09728      -1.517     0.09772        -1.510
asc_CV             0.00000          NA          NA          NA            NA
asc_EV             0.27161     0.11022       2.464     0.10885         2.495

Re: negative sign in the range of log-uniform distribution

Posted: 23 Jun 2023, 20:07
by stephanehess
Hi

first, I would suggest you don't call the parameters mu and sigma, as that's not what they are (that would imply a mean and standard deviation). They are the offset and range, so a and b makes more sense.

second, there is not a priori assumption that a is the lower bound, so if b is negative, then a is the upper bound, and the lower limit is a+b (where b is negative).

Of course, when you then exponentiate it, the coefficient is positive over its whole range

Stephane