Important: Read this before posting to this forum

  1. This forum is for questions related to the use of Apollo. We will answer some general choice modelling questions too, where appropriate, and time permitting. We cannot answer questions about how to estimate choice models with other software packages.
  2. There is a very detailed manual for Apollo available at http://www.ApolloChoiceModelling.com/manual.html. This contains detailed descriptions of the various Apollo functions, and numerous examples are available at http://www.ApolloChoiceModelling.com/examples.html. In addition, help files are available for all functions, using e.g. ?apollo_mnl
  3. Before asking a question on the forum, users are kindly requested to follow these steps:
    1. Check that the same issue has not already been addressed in the forum - there is a search tool.
    2. Ensure that the correct syntax has been used. For any function, detailed instructions are available directly in Apollo, e.g. by using ?apollo_mnl for apollo_mnl
    3. Check the frequently asked questions section on the Apollo website, which discusses some common issues/failures. Please see http://www.apollochoicemodelling.com/faq.html
    4. Make sure that R is using the latest official release of Apollo.
  4. If the above steps do not resolve the issue, then users should follow these steps when posting a question:
    1. provide full details on the issue, including the entire code and output, including any error messages
    2. posts will not immediately appear on the forum, but will be checked by a moderator first. This may take a day or two at busy times. There is no need to submit the post multiple times.

"Singular Hessian" Warning in MDCEV model

Ask questions about errors you encouunter. Please make sure to include full details about your model specifications, and ideally your model file.
Post Reply
Wendy416
Posts: 1
Joined: 13 Nov 2023, 22:01

"Singular Hessian" Warning in MDCEV model

Post by Wendy416 »

Hi,
Thank you for running this forum and helping out!

I am running a MDCEV model (no outside good) to estimate how contextual variables (seven environmental variables, all dummy variables) affect the way people allocate their time to four activities in a given environment. In my case, for the respondents these four activities were simply labelled with different names without any specific attributes. Respondents were asked to allocate a total fixed amount of time to the four activities based on the different environmental configurations given (to ensure independence of environmental variables, we have used a fractional design).

However, I got warning messages of “Singular Hessian, cannot calculate s.e.”, and “some eigenvalues of the Hessian are positive” with the following codes (here I set three alternative specific constants for the three of the four activities, the last one was set to be zero):

Code: Select all

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

### Load Apollo library
library(apollo)

### Initialise code
apollo_initialise()

### Set core controls
apollo_control = list(
  modelName       = "0000_MDCEV_2_attribute_only",
  modelDescr      = "MDCEV model on time use, gamma profile, no outside good",
  indivID         = "indivID", 
  outputDirectory = "output"
)

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

### Loading data from package
### if data is to be loaded from a file (e.g. called data.csv), 
### the code would be: database = read.csv("data.csv",header=TRUE)

database <<- read.table("D:/spss_first/00_rmdcev/08_paneldata.csv", header=T, sep=",");

# ################################################################# #
#### DEFINE MODEL PARAMETERS                                     ####
# ################################################################# #

### Vector of parameters, including any that are kept fixed in estimation
apollo_beta = c(alpha_base         = 0,
                gamma_open      = 1,
                gamma_gostation         = 1,
                gamma_goshop       = 1,
                gamma_leave     = 1,
                delta_open      = 0,
                delta_gostation         = 0,
                delta_goshop      = 0,
                
                open_bdht = 0, 
                open_otdi = 0,
                open_bech = 0,
                open_bk = 0,
                open_arcd = 0,
                open_gras = 0,
                open_tree = 0,
              
           
                gostation_bdht = 0,
                gostation_otdi = 0,
                gostation_bech = 0,
                gostation_bk = 0,
                gostation_arcd = 0,
                gostation_gras = 0,
                gostation_tree = 0,
               
    
                goshop_bdht = 0,
                goshop_otdi = 0,
                goshop_bech = 0,
                goshop_bk = 0, 
                goshop_arcd = 0,
                goshop_gras = 0,
                goshop_tree = 0,
               
        
                sig = 1)

### 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("sig")

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

apollo_inputs = apollo_validateInputs()

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

apollo_probabilities=function(apollo_beta, apollo_inputs, functionality="estimate"){
  
  ### 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()
  
  ### Define individual alternatives
  alternatives = c("open", 
                   "gostation", 
                   "goshop", 
                   "leave")
  
  ### Define availabilities
  avail = list(open  = 1, 
               gostation     = 1,
               goshop   = 1,
               leave = 1)
  
  ### Define continuous consumption for individual alternatives
  continuousChoice = list(open  = open, 
                          gostation = gostation,
                          goshop = goshop,
                          leave = leave)
  
  ### Define utilities for individual alternatives
  V = list()
  V[["open"]] = delta_open + open_bdht * bd_ht + open_otdi*out_di + open_bech *bench +open_bk *bike_pk + open_arcd *arcade + open_gras * grass + open_tree * tree
  V[["gostation"]] = delta_gostation + gostation_bdht * bd_ht + gostation_otdi*out_di + gostation_bech *bench +gostation_bk *bike_pk + gostation_arcd *arcade + gostation_gras * grass + gostation_tree * tree    
  V[["goshop"]] = delta_goshop + goshop_bdht * bd_ht + goshop_otdi*out_di + goshop_bech *bench +goshop_bk *bike_pk + goshop_arcd *arcade + goshop_gras * grass + goshop_tree * tree
  V[["leave"]] = 0
  
  
  ### Define alpha parameters
  alpha = list(open  = 1 /(1 + exp(-alpha_base)), 
               gostation     = 1 /(1 + exp(-alpha_base)), 
               goshop   = 1 /(1 + exp(-alpha_base)), 
               leave = 1 /(1 + exp(-alpha_base)))
  
  ### Define gamma parameters
  gamma = list(open      = gamma_open, 
               gostation         = gamma_gostation,
               goshop       = gamma_goshop,
               leave     = gamma_leave)
  
  ### Define costs for individual alternatives
  cost = list(open      = 1, 
              gostation         = 1,
              goshop       = 1,
              leave      = 1)
  
  ### Define settings for MDCEV model
  mdcev_settings <- list(alternatives      = alternatives,
                         avail             = avail,
                         continuousChoice  = continuousChoice,
                         utilities         = V,
                         alpha             = alpha,
                         gamma             = gamma, 
                         sigma             = sig, 
                         cost              = cost,
                         budget            = 30)
  
  ### Compute probabilities using MDCEV model
  P[["model"]] = apollo_mdcev(mdcev_settings, functionality)
  
  ### Take product across observation for same individual
  P = apollo_panelProd(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,estimate_settings=list(maxIterations=5000))

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

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

apollo_modelOutput(model, modelOutput_settings = list(printPVal=2)) # for two sided t-test
Here are some of the outputs:

Code: Select all

***** Relative function convergence *****
Additional convergence test using scaled estimation. Parameters will be scaled by their current
  estimates and additional iterations will be performed.

BGW is using FD derivatives for model Jacobian. (Caller did not provide derivatives.)


Iterates will be appended to: 
 output/0000_MDCEV_2_attribute_only_iterations.csv    it    nf     F            RELDF    PRELDF    RELDX    MODEL stppar
     0     1 5.457461023e+03

***** Singular convergence *****
The estimation of the scaled model failed, and the unscaled version will be returned instead.
Final LL: -5457.461

Calculating log-likelihood at equal shares (LL(0)) for applicable models...
Calculating log-likelihood at observed shares from estimation data (LL(c)) for applicable
  models...
Calculating LL of each model component...
Computing covariance matrix using numerical methods (numDeriv).
 0%....25%....50%....75%....100%
WARNING: Singular Hessian, cannot calculate s.e. 
Hessian written to output/0000_MDCEV_2_attribute_only_hessian.csv
WARNING: Some eigenvalues of the Hessian are positive, indicating convergence to a saddle point! 
Computing score matrix...

Your model was estimated using the BGW algorithm. Please acknowledge this by citing Bunch et al.
  (1993) - DOI 10.1145/151271.151279
  Model name                                  : 0000_MDCEV_2_attribute_only
Model description                           : MDCEV model on time use, gamma profile, no outside good
Model run at                                : 2023-11-13 23:16:04.683781
Estimation method                           : bgw
Model diagnosis                             : Relative function convergence
Optimisation diagnosis                      : Saddle point found
     hessian properties                     : Some eigenvalues are positive and others negative
     maximum eigenvalue                     : 0
     reciprocal of condition number         : not calculated (Hessian is not negative definite)
Number of individuals                       : 192
Number of rows in database                  : 768
Number of modelled outcomes                 : 768

Number of cores used                        :  1 
Model without mixing

LL(start)                                   : -6141.66
LL at equal shares, LL(0)                   : NA
LL at observed shares, LL(C)                : NA
LL(final)                                   : -5457.46
Rho-squared vs equal shares                  :  Not applicable 
Adj.Rho-squared vs equal shares              :  Not applicable 
Rho-squared vs observed shares               :  Not applicable 
Adj.Rho-squared vs observed shares           :  Not applicable 
AIC                                         :  10972.92 
BIC                                         :  11107.59 

Estimated parameters                        : 29
Time taken (hh:mm:ss)                       :  00:00:8.37 
     pre-estimation                         :  00:00:0.3 
     estimation                             :  00:00:2.35 
     post-estimation                        :  00:00:5.72 
Iterations                                  :  32  

Unconstrained optimisation.

Estimates:
                   Estimate        s.e.   t.rat.(0)  p(2-sided)    Rob.s.e. Rob.t.rat.(0)  p(2-sided)
alpha_base       -40.273143          NA          NA          NA          NA            NA          NA
gamma_open         3.463235          NA          NA          NA          NA            NA          NA
gamma_gostation    4.147158          NA          NA          NA          NA            NA          NA
gamma_goshop       4.037829          NA          NA          NA          NA            NA          NA
gamma_leave        7.607748          NA          NA          NA          NA            NA          NA
delta_open         0.523676          NA          NA          NA          NA            NA          NA
delta_gostation    0.844225          NA          NA          NA          NA            NA          NA
delta_goshop       0.192511          NA          NA          NA          NA            NA          NA
open_bdht         -0.596792          NA          NA          NA          NA            NA          NA
open_otdi          0.725264          NA          NA          NA          NA            NA          NA
open_bech          0.595906          NA          NA          NA          NA            NA          NA
open_bk           -0.218788          NA          NA          NA          NA            NA          NA
open_arcd          0.275018          NA          NA          NA          NA            NA          NA
open_gras          0.823620          NA          NA          NA          NA            NA          NA
open_tree          0.715244          NA          NA          NA          NA            NA          NA
gostation_bdht    -0.093390          NA          NA          NA          NA            NA          NA
gostation_otdi    -0.095259          NA          NA          NA          NA            NA          NA
gostation_bech    -0.403409          NA          NA          NA          NA            NA          NA
gostation_bk      -0.213423          NA          NA          NA          NA            NA          NA
gostation_arcd     0.004776          NA          NA          NA          NA            NA          NA
gostation_gras     0.232169          NA          NA          NA          NA            NA          NA
gostation_tree     0.061953          NA          NA          NA          NA            NA          NA
goshop_bdht       -0.372858          NA          NA          NA          NA            NA          NA
goshop_otdi       -0.182234          NA          NA          NA          NA            NA          NA
goshop_bech       -0.195470          NA          NA          NA          NA            NA          NA
goshop_bk         -0.078527          NA          NA          NA          NA            NA          NA
goshop_arcd        0.247201          NA          NA          NA          NA            NA          NA
goshop_gras        0.544868          NA          NA          NA          NA            NA          NA
goshop_tree        0.401657          NA          NA          NA          NA            NA          NA
sig                1.000000          NA          NA          NA          NA            NA          NA

However, if I fix only one constant for all the alternatives, then the model would work. The utilities are defined as follows:

Code: Select all

  ### Define utilities for individual alternatives
  V = list()
  V[["open"]] = const + open_bdht * bd_ht + open_otdi*out_di + open_bech *bench +open_bk *bike_pk + open_arcd *arcade + open_gras * grass + open_tree * tree
  V[["gostation"]] = const + gostation_bdht * bd_ht + gostation_otdi*out_di + gostation_bech *bench +gostation_bk *bike_pk + gostation_arcd *arcade + gostation_gras * grass + gostation_tree * tree    
  V[["goshop"]] = const + goshop_bdht * bd_ht + goshop_otdi*out_di + goshop_bech *bench +goshop_bk *bike_pk + goshop_arcd *arcade + goshop_gras * grass + goshop_tree * tree
  V[["leave"]] = 0
  
And here are some of the results which seem resonable:

Code: Select all

Model name                                  : 0000_MDCEV_2_attribute_only
Model description                           : MDCEV model on time use, gamma profile, no outside good
Model run at                                : 2023-11-13 23:49:32.163323
Estimation method                           : bgw
Model diagnosis                             : Relative function convergence
Optimisation diagnosis                      : Maximum found
     hessian properties                     : Negative definite
     maximum eigenvalue                     : -1e-06
     reciprocal of condition number         : 7.26867e-10
Number of individuals                       : 192
Number of rows in database                  : 768
Number of modelled outcomes                 : 768

Number of cores used                        :  1 
Model without mixing

LL(start)                                   : -6141.66
LL at equal shares, LL(0)                   : NA
LL at observed shares, LL(C)                : NA
LL(final)                                   : -5462.59
Rho-squared vs equal shares                  :  Not applicable 
Adj.Rho-squared vs equal shares              :  Not applicable 
Rho-squared vs observed shares               :  Not applicable 
Adj.Rho-squared vs observed shares           :  Not applicable 
AIC                                         :  10979.19 
BIC                                         :  11104.57 

Estimated parameters                        : 27
Time taken (hh:mm:ss)                       :  00:00:7.72 
     pre-estimation                         :  00:00:0.28 
     estimation                             :  00:00:2.46 
     post-estimation                        :  00:00:4.98 
Iterations                                  :  33  

Unconstrained optimisation.

Estimates:
                   Estimate        s.e.   t.rat.(0)  p(2-sided)    Rob.s.e.
alpha_base        -19.78779   1114.0585    -0.01776    0.985829     0.07038
gamma_open          3.45389      0.3919     8.81208    0.000000     0.37897
gamma_gostation     4.42962      0.3760    11.77949    0.000000     0.39609
gamma_goshop        3.85703      0.3317    11.62947    0.000000     0.32124
gamma_leave         7.59876      0.8050     9.43901    0.000000     1.03905
const               0.52765      0.1706     3.09328    0.001980     0.19126
open_bdht          -0.59594      0.1338    -4.45307   8.465e-06     0.12894
...
 
I feel very confused about the result (could this be due to all my x-variables being dummy variables?) and not sure if it is approariaite to fix the constants for all alternatives. Could you please give me some advice? I would be appreciative if you could leave any comments!
Thank you in advance!

Best regards
Wendy
stephanehess
Site Admin
Posts: 998
Joined: 24 Apr 2020, 16:29

Re: "Singular Hessian" Warning in MDCEV model

Post by stephanehess »

Hi

we believe the issue here is that alpha_base tends to -inf, so what you can do is set it to the estimate value in apollo_beta (-40.27) and include the parameter name in apollo_fixed.

Then it should all work

Stephane
--------------------------------
Stephane Hess
www.stephanehess.me.uk
Post Reply