#ifndef MBASE_INCLUDED
#define MBASE_INCLUDED

#include <database.h>
#include <maximize.h>

enum
{   Y_VAR, X_VAR
};
enum
{   MS_NONE, MS_DATA, MS_PARAMS, MS_ESTIMATED, MS_EST_FAILED
};
enum
{	CTL_LABEL, CTL_CHECK, CTL_RADIO, CTL_INT, CTL_DOUBLE, CTL_STRING,
	CTL_STRMAT,CTL_SELECT,CTL_GROUP,
	CTL_LISTBOX = 100
};

enum
{	STATUS_ENDOGENOUS 	=   1,	// default: apply to first var at lag0
	STATUS_SPECIAL		=   2,	// default: apply to special variables
	STATUS_MULTIVARIATE	=   4,	// default: apply to all non-special vars at lag0
	STATUS_TRANSFORM	=   8,	// is a transformation
	STATUS_GROUP		=  16,	// is a group (each var is in only one group)
	STATUS_ONEONLY		=  32,	// only one var can have this flag
	STATUS_GROUP2		=  64,	// is 2nd group (each var is in only in one of each group)
	STATUS_MULTIPLE		= 128, 	// multiple instances of a variable are allowed
	STATUS_DEFAULT		= 256,	// is default: no status letter displayed
	STATUS_GROUP3		= 512,	// is 3rd group (each var is in only in one of each group)
	STATUS_GROUP4		=1024,	// is 4th group (each var is in only in one of each group)
	STATUS_GROUP5		=2048   // is 5th group (each var is in only in one of each group)
}
enum
{	SPECIAL_CONSTANT	=  0,
	SPECIAL_TREND		=  1,
	SPECIAL_SEASONAL	=  2,
	SPECIAL_CSEASON		=  3
}

/*------------------------- Modelbase : Database ---------------------------*/
class Modelbase : Database
{
    decl m_iModelStatus;			            /* one of the MS_ constants */
    decl m_iMethod;						     /* specifies estimation method */
    decl m_fPrint;               /* TRUE if output desired after estimation */
    decl m_iResult;             /* >= 0: return code from MaxBFGS/MaxNewton */

    decl m_cPar, m_cParFree; /* total number of parameters, no of free pars */
    decl m_vPar;			      				 /* parameter vector [q][1] */
    decl m_vIsFreePar;/* has a 1 for free and 0 for fixed parameters [q][1] */
    decl m_vFixedPar;	      /* has a the value of fixed parameters [q][1] */
	decl m_vIdxFreePar;		 /* has a the indices of free parameters [p][1] */

    decl m_iT1est, m_iT2est;                           /* estimation sample */
    decl m_cT;                                    /* number of observations */
    decl m_cY, m_cYlag;        /* number of dependent variables, lagged Y's */
    decl m_cX;                    /* number of additive regressors (if any) */
	decl m_mY;        /* data vector [cT][1], standardized after estimation */
    decl m_mX;                 /* additive regressor data vector [cT][m_cX] */

	decl m_mCovar, m_mCovarRobust;  /* var-covar of estimated coeffs [p][p] */
    decl m_dLogLik;                                       /* log-likelihood */
	decl m_cTforc, m_bRecursive;
	decl m_sDbName;							   /* name of database (if any) */
	decl m_cTinit; /* no of observations to initialize recursive estimation */
	decl m_iDeterministic;		 /* value used to create deterministic vars */
	decl m_dForcCrit;	  /* default critical value for forecast error bars */

    Modelbase();                                             /* constructor */
	// database overrides
	Select(const iGroup, const aSel);
	DeSelect();
	SetSample(const iYear1, const iPeriod1, const iYear2, const iPeriod2);
	SetSelSample(const iYear1, const iPeriod1, const iYear2, const iPeriod2);
	Grow(const cTadd);
	// OxPack related
	virtual SendVarStatus();
	virtual SendSpecials();
	virtual SendFunctions();
	virtual SendMethods();
	virtual SendDialog(const sDialog);
	virtual SendMenu(const sMenu);
	virtual SendResults(const sType);
	virtual ReceiveData();
	virtual ReceiveModel();
	virtual ReceiveDialog(const sDialog, const asOptions, const aValues);
	// general
	virtual GetPackageName();		   			 /* returns name of package */
	virtual GetPackageVersion();			  /* returns version of package */
	virtual IsUnivariate();		  /* returns TRUE if only one Y_VAR allowed */
	GetModelStatus();					    /* returns MS_.... model status */
	SetModelStatus(const iModelStatus);
	virtual ClearModel();					/* sets model status to MS_NONE */
	virtual ClearEstimation();	 /* removes results fom previous estimation */ 
    SetPrint(fPrint);                             /* set value for m_fPrint */
	GetResult();						   /* returns the estimation result */
	SetResult(const iResult);			      /* sets the estimation result */
	SetMethod(const iMethod);				  /* sets the estimation method */
	GetMethod();							  /* gets the estimation method */
	GetLogLik();							  /* returns the log-likelihood */
	virtual GetResiduals();				   /* returns T x n residual matrix */
	virtual GetResVar();					   /* returns residual variance */
	virtual GetcT();			 /* actual no of variables to use in output */
	virtual GetcDfLoss();		/* degrees of freedom lost (for tailt, AIC) */
	// parameter related
	SetParCount(const cPar, ...);  /* sets no of params q (including fixed) */
	GetParCount();	    /* returns number of parameters q (including fixed) */
	GetFreeParCount();				 /* returns number of free parameters p */
	GetFreeParNames();				   /* gets names of free parameters [p] */
	FixPar(const iP, const dFix);  			  /* fixes parameter iP to dFix */
	FreePar(const iP);						  	 	  /* frees parameter iP */
	GetParStatus();           /* ret: { m_cPar, m_vIsFreePar, m_vFixedPar } */
	GetFreePar();				  /* gets the vector of free parameters [p] */
	MapParToFree(const vPar);
	SetFreePar(const vParFree);				    /* sets the free parameters */
	ResetFixedPar();		   /* copy fixed par from m_vFixedPar to m_vPar */
	GetPar();					   /* gets the vector of all parameters [q] */
	SetPar(const vPar);					  /* sets the full parameter vector */
	virtual GetParNames(); 			    /* gets names of all parameters [q] */
	virtual GetParTypes();/* gets par types (1 or 2 letters) [q], default 0 */
	virtual GetLongRunNames();			  /* returns 0 or names of long-run */
	virtual GetLongRunInfo();	  /* returns 0 or names/par/var of long-run */
	ForceYlag(const iYgroup);
	// covariance
	GetStdErr();		   /* returns the std.errors (0 for fixed pars) [q] */
	GetStdErrRobust();			    /* returns <> or robust standard errors */
	GetCovar();						     /* returns p x p covariance matrix */
	GetCovarRobust();	    /* returns <> or p x p robust covariance matrix */
	// move up in model status
	virtual SetStartPar(const vParFree);	    /* sets the starting values */
	virtual InitData();							  /* gets the data: Y,X,Z,W */
	virtual InitPar();					/* initializes the parameter values */
	virtual DoEstimation(vPar);
	virtual Estimate();
	// post estimation
	virtual Covar();									   /* sets m_mCovar */
	virtual Output();									   /* prints output */
	OutputHeader(const sTitle);				  /* returns TRUE to print rest */
	OutputPar();							  /* prints parameter estimates */
	OutputLogLik();							  /* prints loglik, aic, var(y) */
	OutputMax(const sMethod, const iResult, const vPstart, const bNumerical);
	// evaluation
	static PrintTestVal(const dTest, const cR, const cTdf, const sLabel);
	virtual TestRestrictions(mR, ...);

	GetDbName();							 // get database name
	SetDbName(const sName);			         // set database name
	GetForecastData(const iGroup, const mnLag, const mxLag, const cTforc, ...);
	LoadOptions();
	SaveOptions();
	virtual Buffering(const bBufferOn);
	// batch related:
	virtual BatchVarStatus(const sTypes, const vcTypes);
	virtual BatchMethod(const sMethod);
	virtual Batch(const sBatch, ...);
	virtual GetBatchModelSettings();
};
/*----------------------- END Modelbase : Database -------------------------*/

#endif /* MBASE_INCLUDED */
