/*
  ucm

*!  VERSION 1.0.4  16jun2011

  keyword:  eclass

*/

VERSION 12.0

INCLUDE _std_wide
DEFINE _dlght 425
INCLUDE header

HELP hlp1, view("help ucm")
RESET res1

SCRIPT PREINIT
BEGIN
	script se_createAsRobustOIM
	create DOUBLE pi
	create DOUBLE val
	pi.setvalue c(pi)
END

SCRIPT POSTINIT
BEGIN
	program se_setFinalInitState
	script max_nonrtolerance_hide
	program main_cycle_off
END

DIALOG main, label("ucm - Unobserved-component models") tabtitle("Model")
BEGIN
  DEFINE _x _xsetbu
  DEFINE _y _top
  INCLUDE _bu_tsset
  TEXT tx_dv 			_lft	_ss	_vnwd	.,		///
	label("Dependent variable:")
  DEFINE holdy @y
  VARNAME vn_dv			@ 	_ss	@	.,		///
	ts								///
	label("Dependent variable")
  TEXT tx_iv			_vlx	holdy	160	.,		///
	label("Independent variables:")
  VARLIST vl_iv			@	_ss	_vlwd	.,		///
	fv								///
	allowcat							///
	label("Independent variables")

  TEXT tx_model			_lft	_xls	_cwd1	.,		///
	label("Specifies a trend and idiosyncratic component:")
  DEFINE holdy @y
  COMBOBOX cb_model		@	_ss	_cwd1	.,		///
	option(model)							///
	dropdownlist							///
	contents(main_model_list)					///
	values(main_model_values)					///
	label("Cycle Number")

  CHECKBOX ck_seasonal		_lft2	holdy	_iwd	_ht2,		///
	onclickon("script main_seasonal_on")				///
	onclickoff("script main_seasonal_off")				///
	label("Add a stochastic-seasonal component")
  SPINNER sp_seasonal		_ilft2	_ss	_spwd	.,		///
	option(seasonal)						///
	default(1)							///
	min(1)								///
	max(c(N))
  TEXT tx_seasonal		_en7sep	@	120	.,		///
	label("Period")

  CHECKBOX ck_cycle1		_lft	_xls	_iwd	_ht2,		///
	groupbox							///
	onclickon("script main_cycle1_on") 				///
	onclickoff("script main_cycle1_off") 				///
	label("Add a stochastic-cycle component (Cycle 1)")
  SPINNER sp_cycle1		_ilft	_ss	_spwd	.,		///
	default(1)							///
	min(1)								///
	max(3)
  TEXT tx_cycle1		_en7sep	@	200	.,		///
	label("Order")
  EDIT ed_freq_cycle1		_lft2	@	_spwd	.,		///
	numonly								///
	option(frequency)						///
	label("Initial value for cycle frequency")	
  TEXT tx_freq_cycle1		+65 	@	200	.,		///
	label("Initial value for cycle frequency")
  
  CHECKBOX ck_cycle2		_lft	_xxls	_iwd	_ht2,		///
	groupbox							///
	onclickon("script main_cycle2_on") 				///
	onclickoff("script main_cycle2_off") 				///
	label("Add a stochastic-cycle component (Cycle 2)")
  SPINNER sp_cycle2		_ilft	_ss	_spwd	.,		///
	default(1)							///
	min(1)								///
	max(3)
  TEXT tx_cycle2		_en7sep	@	200	.,		///
	label("Order")
  EDIT ed_freq_cycle2		_lft2	@	_spwd	.,		///
	numonly								///
	option(frequency)						///
	label("Initial value for cycle frequency")	
  TEXT tx_freq_cycle2		+65 	@	200	.,		///
	label("Initial value for cycle frequency")
  
  CHECKBOX ck_cycle3		_lft	_xxls	_iwd	_ht2,		///
	groupbox							///
	onclickon("script main_cycle3_on") 				///
	onclickoff("script main_cycle3_off") 				///
	label("Add a stochastic-cycle component (Cycle 3)")
  SPINNER sp_cycle3		_ilft	_ss	_spwd	.,		///
	default(1)							///
	min(1)								///
	max(3)
  TEXT tx_cycle3		_en7sep	@	200	.,		///
	label("Order")
  EDIT ed_freq_cycle3		_lft2	@	_spwd	.,		///
	numonly								///
	option(frequency)						///
	label("Initial value for cycle frequency")	
  TEXT tx_freq_cycle3		+65 	@	200	.,		///
	label("Initial value for cycle frequency")

  DEFINE _x _lft
  DEFINE _y _xls
  DEFINE _cx _lw80
  DEFINE _bux _slw80
  INCLUDE _constraints

  DEFINE _x _lft
  DEFINE _xw _ibwd
  INCLUDE _ck_collinear
END

LIST main_model_list
BEGIN
	"Random-walk model"
	"No trend or idiosyncratic component"
	"No trend component but include idiosyncratic component"
	"Deterministic constant with idiosyncratic component"
	"Local-level model"
	"Deterministic-trend model with idiosyncratic component"
	"Local-level model with deterministic trend"
	"Random-walk-with-drift model"
	"Local-linear-trend model"
	"Smooth-trend model"
	"Random-trend model"
END

LIST main_model_values
BEGIN
	""
	none
	ntrend
	dconstant
	llevel
	dtrend
	lldtrend
	rwdrift
	lltrend
	strend
	rtrend
END

SCRIPT main_seasonal_on
BEGIN
	main.sp_seasonal.enable
	main.tx_seasonal.enable
END

SCRIPT main_seasonal_off
BEGIN
	main.sp_seasonal.disable
	main.tx_seasonal.disable
END

SCRIPT main_cycle1_on
BEGIN
	main.sp_cycle1.enable
	main.tx_cycle1.enable
	main.ed_freq_cycle1.enable
	main.tx_freq_cycle1.enable
END

SCRIPT main_cycle1_off
BEGIN
	main.sp_cycle1.disable
	main.tx_cycle1.disable
	main.ed_freq_cycle1.disable
	main.tx_freq_cycle1.disable
END

SCRIPT main_cycle2_on
BEGIN
	main.sp_cycle2.enable
	main.tx_cycle2.enable
	main.ed_freq_cycle2.enable
	main.tx_freq_cycle2.enable
END

SCRIPT main_cycle2_off
BEGIN
	main.sp_cycle2.disable
	main.tx_cycle2.disable
	main.ed_freq_cycle2.disable
	main.tx_freq_cycle2.disable
END

SCRIPT main_cycle3_on
BEGIN
	main.sp_cycle3.enable
	main.tx_cycle3.enable
	main.ed_freq_cycle3.enable
	main.tx_freq_cycle3.enable
END

SCRIPT main_cycle3_off
BEGIN
	main.sp_cycle3.disable
	main.tx_cycle3.disable
	main.ed_freq_cycle3.disable
	main.tx_freq_cycle3.disable
END

INCLUDE byifin
INCLUDE se

SCRIPT rpt_POSTINIT
BEGIN
	create STRING rpt_bu_fmtcoefResults
	program rpt_bu_fmtcoef_ckResults
END

DIALOG rpt, tabtitle("Reporting")
BEGIN
  DEFINE _x _lft
  DEFINE _cx _spr
  DEFINE _y _top
  INCLUDE _sp_level

  DEFINE _x _lft
  DEFINE _y _ls
  DEFINE _cx _iwd
  INCLUDE _nocnsreport

  DEFINE _x _lft
  DEFINE _y _ms
  DEFINE _cx _iwd
  INCLUDE _noomitted

  DEFINE _x _lft
  DEFINE _y _ms
  DEFINE _cx _iwd
  INCLUDE _vsquish

  DEFINE _x _lft
  DEFINE _y _ls
  DEFINE _cx _iwd
  INCLUDE _factor_vars_reporting

  DEFINE _x _lft
  DEFINE _y _xxxls
  INCLUDE _bu_coef_table_reporting
END

INCLUDE fmt_coef_table_reporting_pr

INCLUDE max_ml

PROGRAM main_cycle1_output
BEGIN
	put main.sp_cycle1
	if main.ed_freq_cycle1 {
		call val.setvalue class main.ed_freq_cycle1.value
		if val.islt(0) | val.isgt(pi) {
stopbox stop `"Invalid "Initial value for cycle frequency" value for "Cycle 1""'	///
"Value must be between 0 and pi."
		}
		put ","
		optionarg main.ed_freq_cycle1
	}
END

PROGRAM main_cycle2_output
BEGIN
	put main.sp_cycle2
	if main.ed_freq_cycle2 {
		call val.setvalue class main.ed_freq_cycle2.value
		if val.islt(0) | val.isgt(pi) {
stopbox stop `"Invalid "Initial value for cycle frequency" value for "Cycle 2""'	///
"Value must be between 0 and pi."
		}
		put ","
		optionarg main.ed_freq_cycle2
	}
END

PROGRAM main_cycle3_output
BEGIN
	put main.sp_cycle3
	if main.ed_freq_cycle3 {
		call val.setvalue class main.ed_freq_cycle3.value
		if val.islt(0) | val.isgt(pi) {
stopbox stop `"Invalid "Initial value for cycle frequency" value for "Cycle 3""'	///
"Value must be between 0 and pi."
		}
		put ","
		optionarg main.ed_freq_cycle3
	}
END

PROGRAM command
BEGIN
	INCLUDE _by_pr
	put "ucm "
	varlist main.vn_dv [main.vl_iv]
	INCLUDE _ifin_pr
	beginoptions
		optionarg main.cb_model
		optionarg main.sp_seasonal
		if main.ck_cycle1 {
			put "cycle("
			put /program main_cycle1_output
			put ") "
		}
		if main.ck_cycle2 {
			put "cycle("
			put /program main_cycle2_output
			put ") "
		}
		if main.ck_cycle3 {
			put "cycle("
			put /program main_cycle3_output
			put ") "
		}
		INCLUDE _constraints_main_pr
		option main.ck_collinear
		put " " /program se_output
		put " " /program max_output
		optionarg /hidedefault rpt.sp_level
		INCLUDE _nocnsreport_pr
		INCLUDE _noomitted_pr
		INCLUDE _vsquish_pr
		INCLUDE _factor_vars_reporting_pr
		put " " rpt_bu_fmtcoefResults
	endoptions
END
