/*
  max_glm.idlg

*!  VERSION 1.1.6  12jun2009

  This file is included by glm.dlg and binreg.dlg.

*/

DIALOG max, label("") tabtitle("Maximization")
BEGIN
  /*--+----1----+----2----+----3----+----4----+----5----+----6----+----7----+*/
  GROUPBOX gb_opt    _lft      _top      _iwd      _ht2,		/*
		*/ label("Optimization method")				/*
		*/
  RADIO    rb_ml4glm _ilft     _ss       _cwd2     .,			/*
		*/ label("Maximum likelihood")				/*
		*/ clickon("program ml_on")				/*
		*/ first						/*
		*/ option(NONE)						/*
		*/
  RADIO    rb_irls4glm _ilft2  @         _cwd3     .,			/*
		*/ label("Iterated, reweighted least squares (IRLS)")	/*
		*/ clickon("script ml_off")				/*
		*/ last							/*
		*/ option("irls")					/*
		*/
  RADIO    rb_irls4bin _ilft   @         _cwd3     .,			/*
		*/ label("Iterated, reweighted least squares (IRLS)")	/*
		*/ clickon("script ml_off")				/*
		*/ first						/*
		*/ option("NONE")					/*
		*/
 
  RADIO    rb_ml4bin _ilft2    @         _cwd2     .,			/*
		*/ label("Maximum likelihood")				/*
		*/ clickon("program ml_on")				/*
		*/ last 						/*
		*/ option("ml")						/*
		*/


  COMBOBOX cb_tech   _lft      _xls      95        .,			/*
  		*/ label("Technique") 					/*
  		*/ dropdown						/*
  		*/ contents(technique_list) values(technique_val_list)	/*
  		*/ option(technique)					/*
  		*/ onselchangelist(se_label_list)			/*
  		*/
  TEXT     tx_tech   +100      @         200       ., 			/*
  		*/ label("Optimization technique")


  CHECKBOX ck_fisher _ilft2    @         _ckwd     .,			/*
		*/ clickon("program max_check_fisher")			/*
		*/ clickoff("program max_check_fisher")			/*
		*/
  SPINNER  sp_fisher _cksep    @         _spwd     .,			/*
		*/ min(1)						/*
		*/ max(10000000)					/*
		*/ default(1)						/*
		*/ option("fisher")					/*
		*/
  TEXT     tx_fisher _spsep    @         _ckspr2b  .,			/*
		*/ label("Fisher scoring steps")			/*
		*/

  CHECKBOX ck_nolog  _lft      _ms        _cwd1     .,			/*
		*/ label("Suppress iteration log")			/*
		*/ option("nolog")					/*
		*/ clickon(script nolog_on) clickoff(program nolog_off)
  CHECKBOX ck_log    @         @          @         .,			/*
		*/ label("Display iteration log")			/*
		*/ option("log")					/*
		*/ clickon(program nolog_off) clickoff(script nolog_on)
  CHECKBOX ck_search _ilft2    @          _cwd1     .,			/*
		*/ label("Search for good starting values")		/*
		*/ option("search")					/*
		*/

  GROUPBOX gb_log    _lft      _ms        _iwd      _ht4,		/*
		*/ label("Elements to display in the iteration log")	/*
		*/
  CHECKBOX ck_trace  _ilft     _ss       _cwd3_2   .,			/*
		*/ label("Parameter vector")				/*
		*/ option("trace")					/*
		*/
  CHECKBOX ck_show   _ilft3_2  @         @         .,			/*
		*/ label("Steps within each iteration")			/*
		*/ option("showstep")					/*
		*/
  CHECKBOX ck_grad   _ilft     _ss       @         .,			/*
		*/ label("Gradient vector")				/*
		*/ option("gradient")					/*
		*/
  CHECKBOX ck_hess   _ilft3_2  @         @         .,			/*
		*/ label("Negative Hessian matrix")			/*
		*/ option("hessian")					/*
		*/
  CHECKBOX ck_shownr _ilft3_3  @         @         .,			/*
		*/ label("Tolerance")					/*
		*/ option(showtolerance)

  CHECKBOX ck_diff   _lft      _xls      _cwd0     .,			/*
	*/ label("Alternate optimization for difficult likelihoods")	/*
		*/ option("difficult") 					/*
		*/
  SPINNER  sp_iter   _ilft2    @         _spwd     .,			/*
		*/ label("Maximum iterations")				/*
		*/ min(1)						/*
		*/ max(16000)						/*
		*/ default(50)						/*
		*/ option("iterate")					/*
		*/
  TEXT     tx_iter   _spsep    @         _spr2b    .,			/*
		*/ label("Maximum iterations")				/*
		*/

  GROUPBOX gb_tol    _lft      _ms       _iwd      _ht7,		/*
		*/ label("Convergence tolerances")			/*
		*/
  EDIT     ed_tol    _ilft     _ss       _en7wd    .,			/*
		*/ label("Coefficient vector")				/*
		*/ option("tolerance")	default(1e-6)			/*
		*/
  TEXT     tx_tol    _en7sep   @         _en7r2b   .,			/*
		*/ label("Coefficient vector")				/*
		*/
  EDIT     ed_nrtol  _ilft2    @         _en7wd    .,			/*
		*/ label("Hessian-scaled gradient")			/*
		*/ option("nrtolerance")				/*
		*/ default(1e-5)
  TEXT     tx_nrtol  _en7sep   @         _en7r2b   .,			/*
		*/ label("Hessian-scaled gradient")			/*
		*/
  EDIT     ed_ltol   _ilft     _ms       _en7wd    .,			/*
		*/ label("Log likelihood")				/*
		*/ option("ltolerance")	default(1e-6)
  TEXT     tx_ltol   _en7sep   @         _en7r2b   .,			/*
		*/ label("Log likelihood")				/*
		*/
  CHECKBOX ck_nonrtol _ilft    _ms       _ibwd     .,			/*
  		*/ label("Disable use of the Hessian-scaled gradient [nonrtolerance]")/*
  		*/ onclickon(script nonrtol)				/*
  		*/ onclickoff(program nrtol)				/*
  		*/ option(nonrtolerance)

  TEXT     tx_from   _lft      +35       _cwd1     .,			/*
		*/ label("Initial values:  [see from() in -help maximize-]")/*
		*/
  EDIT     ed_from   _lft      _ss       _iwd      .,			/*
		*/ label("Initial values")				/*
		*/ option("from")					/*
		*/
END

SCRIPT max_PREINIT
BEGIN
	create BOOLEAN max_isglm
	create STRING max_log_default
	script max_setDefaultLog
END

SCRIPT max_setDefaultNoLog
BEGIN
	max_log_default.setstring "nolog"
	program _max_reset_log
END
 
SCRIPT max_setDefaultLog
BEGIN
	max_log_default.setstring "log"
	program _max_reset_log
END

PROGRAM _max_reset_log
BEGIN
	if max_log_default.equals("log") {
		call max.ck_nolog.show
		call max.ck_log.hide
		if max.ck_nolog {
			call script nolog_on
		}
		if ! max.ck_nolog {
			call program nolog_off
		}
	}
	if max_log_default.equals("nolog") {
		call max.ck_nolog.hide
		call max.ck_log.show
		if max.ck_log {
			call program nolog_off
		}
		if ! max.ck_log {
			call script nolog_on
		}
	}
END

SCRIPT max_fisher_disable
BEGIN
	max.ck_fisher.disable
	max.sp_fisher.disable
	max.tx_fisher.disable
END
PROGRAM max_fisher_enable
BEGIN
	if ( (max.rb_ml4glm & !H(max.rb_ml4glm))	///
	 | (max.rb_ml4bin & !H(max.rb_ml4bin)) ) {
		call max.ck_fisher.enable
		call program max_check_fisher
		call max.tx_fisher.enable
	}
END
PROGRAM max_check_fisher
BEGIN
	if max.ck_fisher {
		call max.sp_fisher.enable
	}
	if ! max.ck_fisher {
		call max.sp_fisher.disable
	}
END

SCRIPT max_svy_on
BEGIN
	max.gb_opt.disable
	max.rb_ml4glm.disable
	max.rb_irls4glm.disable
	max.rb_ml4bin.disable
	max.rb_irls4bin.disable
	script max_fisher_disable
	script max_setDefaultNoLog
END
SCRIPT max_svy_off
BEGIN
	max.gb_opt.enable
	max.rb_ml4glm.enable
	max.rb_irls4glm.enable
	max.rb_ml4bin.enable
	max.rb_irls4bin.enable
	program max_fisher_enable
	script max_setDefaultLog
END

PROGRAM max_create_glm
BEGIN
	call gaction max_isglm.settrue
	call gaction max.rb_ml4bin.hide
	call gaction max.rb_irls4bin.hide
	call gaction max.rb_ml4glm.show
	call gaction max.rb_irls4glm.show
	call gaction max.rb_ml4glm.seton
	call program ml_on
END

PROGRAM max_create_binreg
BEGIN
	call gaction max_isglm.setfalse
	call gaction max.rb_ml4glm.hide
	call gaction max.rb_irls4glm.hide
	call gaction max.rb_ml4bin.show
	call gaction max.rb_irls4bin.show
	call gaction max.rb_irls4bin.seton
	call script ml_off
END

LIST technique_list
BEGIN
	"Default"
	"Newton-Raphson"
	"BHHH"
	"BFGS"
	"DFP"
END

LIST technique_val_list
BEGIN
	""
	"nr"
	"bhhh"
	"bfgs"
	"dfp"
END

SCRIPT max_POSTINIT
BEGIN
	max.cb_tech.withvalue max.cb_tech.setvalue "@"
END

SCRIPT max_sample_weight_off
BEGIN
	program max_check_ml_method
END

PROGRAM max_check_ml_method
BEGIN
	if max_isglm {
		if max.rb_ml4glm {
			call program ml_on
			call gaction max.cb_tech.withvalue ///
				max.cb_tech.setvalue "@"
		}
		if max.rb_irls4glm {
			call script ml_off
		}
	}
	if !max_isglm {
		if max.rb_irls4bin {
			call script ml_off
		}
		if max.rb_ml4bin {
			call program ml_on
			call gaction max.cb_tech.withvalue ///
				max.cb_tech.setvalue "@"
		}
	}
	call gaction se.lb_type.withvalue se.lb_type.setvalue "@"
END

// This list is used with se.idlg
LIST se_label_list
BEGIN
	script se_setDefaultOIM
	script se_setDefaultOIM
	script se_setDefaultOPG
	script se_setDefaultOIM
	script se_setDefaultOIM
END

SCRIPT max_hlp
BEGIN
        view help maximize##|_new
END

PROGRAM nolog_off
BEGIN
	call gaction max.gb_log.enable
	call gaction max.ck_trace.enable
	if max.rb_ml4glm | max.rb_ml4bin {
		call gaction max.ck_grad.enable
		call gaction max.ck_hess.enable
		call gaction max.ck_show.enable
		call program check_shownr
	}
END

SCRIPT nolog_on
BEGIN
	max.gb_log.disable
	max.ck_trace.disable
	max.ck_grad.disable
	max.ck_hess.disable
	max.ck_show.disable
	max.ck_shownr.disable
END

SCRIPT nonrtol
BEGIN
	max.ed_nrtol.disable
	max.tx_nrtol.disable
	max.ck_shownr.disable
END

PROGRAM nrtol
BEGIN
	if max.rb_ml4glm | max.rb_ml4bin {
		call max.ed_nrtol.enable
		call max.tx_nrtol.enable
		call program check_shownr
	}
END

PROGRAM check_shownr
BEGIN
	if ((!max.ck_nolog | max.ck_log) & !max.ck_nonrtol & (max.rb_ml4glm | max.rb_ml4bin)) {
		call max.ck_shownr.enable
	}
END

PROGRAM ml_on
BEGIN
	call program _constraints_enable
	call script collinear_enable
        call rpt.ck_nocnsreport.enable
	
	call program max_fisher_enable
	if !max.ck_nolog | max.ck_log {
		call program nolog_off
	}
	if max.ck_nolog | !max.ck_log {
		call script nolog_on
	}
	call gaction max.ck_search.enable
	call gaction max.ck_diff.enable
	call gaction max.ed_tol.enable
	call gaction max.tx_tol.enable
	call gaction max.ck_nonrtol.enable
	if ! max.ck_nonrtol {
		call gaction max.ed_nrtol.enable
		call gaction max.tx_nrtol.enable
		call program check_shownr
	}
	call gaction max.cb_tech.enable
	call gaction max.tx_tech.enable
	call script se_setIRLS_off
	call gaction max.cb_tech.withvalue max.cb_tech.setvalue "@"
END

SCRIPT ml_off
BEGIN
	program _constraints_disable
	script collinear_disable
	rpt.ck_nocnsreport.disable
	script max_fisher_disable
	max.ck_search.disable
	max.ck_grad.disable
	max.ck_hess.disable
	max.ck_show.disable
	max.ck_shownr.disable
	max.ck_diff.disable
	max.ed_tol.disable
	max.tx_tol.disable
	max.ed_nrtol.disable
	max.tx_nrtol.disable
	max.ck_nonrtol.disable
	max.cb_tech.disable
	max.tx_tech.disable
	script se_setIRLS_on
	script se_setDefaultEIM
END

PROGRAM disable_max
BEGIN
	call gaction max.gb_log.disable
	call gaction max.ck_trace.disable
	call gaction max.ck_grad.disable
	call gaction max.ck_hess.disable
	call gaction max.ck_show.disable
	call gaction max.ck_shownr.disable
	call gaction max.ck_diff.disable
	call gaction max.sp_iter.disable
	call gaction max.tx_iter.disable
	call gaction max.gb_tol.disable
	call gaction max.ed_tol.disable
	call gaction max.tx_tol.disable
	call gaction max.ed_ltol.disable
	call gaction max.tx_ltol.disable
	call gaction max.ed_nrtol.disable
	call gaction max.tx_nrtol.disable
	call gaction max.ck_nonrtol.disable
	call gaction max.tx_from.disable
	call gaction max.ed_from.disable
END

PROGRAM enable_max
BEGIN
	call gaction max.gb_log.enable
	if !max.ck_nolog | max.ck_log {
		call program nolog_off
	}
	if max.ck_nolog | !max.ck_log {
		call script nolog_on
	}
	call gaction max.ck_diff.enable
	call gaction max.sp_iter.enable
	call gaction max.tx_iter.enable
	call gaction max.gb_tol.enable
	call gaction max.ed_tol.enable
	call gaction max.tx_tol.enable
	call gaction max.ed_ltol.enable
	call gaction max.tx_ltol.enable
	call gaction max.ck_nonrtol.enable
	if ! max.ck_nonrtol {
		call gaction max.ed_nrtol.enable
		call gaction max.tx_nrtol.enable
		call program check_shownr
	}
	call gaction max.tx_from.enable
	call gaction max.ed_from.enable
END

PROGRAM disable_max_from
BEGIN
	call gaction max.tx_from.disable
	call gaction max.ed_from.disable
END

PROGRAM enable_max_from
BEGIN
	call gaction max.tx_from.enable
	call gaction max.ed_from.enable
END

PROGRAM max_glm_output
BEGIN
	option radio(max rb_irls4glm rb_ml4glm rb_irls4bin rb_ml4bin)
	optionarg /hidedefault max.cb_tech
	option max.ck_nolog
	option max.ck_log
	/* glm options */
	if !max.ck_nolog | max.ck_log {
		option max.ck_trace
	}
	if max.rb_ml4glm | max.rb_ml4bin {
		if !max.ck_nolog | max.ck_log {
			option max.ck_grad
			option max.ck_hess
			option max.ck_show
			option max.ck_shownr
		}
		option max.ck_diff
		optionarg /hidedefault max.ed_tol
		optionarg /hidedefault max.ed_nrtol
		option max.ck_nonrtol
		option max.ck_search
		optionarg max.sp_fisher
	}
	optionarg /hidedefault max.sp_iter
	optionarg /hidedefault max.ed_ltol
	optionarg max.ed_from
END

