/*!
  \file
  \brief Bayesian Filtering for generalized autoregressive (ARX) model
  \author Vaclav Smidl.

  -----------------------------------
  BDM++ - C++ library for Bayesian Decision Making under Uncertainty

  Using IT++ for numerical operations
  -----------------------------------
*/

#ifndef AR_H
#define AR_H

#include "../stat/libFN.h"
#include "../stat/libEF.h"

namespace bdm {

/*!
* \brief Linear Autoregressive model with Gaussian noise

Regression of the following kind:
\f[
y_t = \theta_1 \psi_1 + \theta_2 + \psi_2 +\ldots + \theta_n \psi_n + r e_t
\f]
where unknown parameters \c rv are \f$[\theta r]\f$, regression vector \f$\psi=\psi(y_{1:t},u_{1:t})\f$ is a known function of past outputs and exogeneous variables \f$u_t\f$. Distrubances \f$e_t\f$ are supposed to be normally distributed:
\f[
e_t \sim \mathcal{N}(0,1).
\f]

Extension for time-variant parameters \f$\theta_t,r_t\f$ may be achived using exponential forgetting (Kulhavy and Zarrop, 1993). In such a case, the forgetting factor \c frg \f$\in <0,1>\f$ should be given in the constructor. Time-invariant parameters are estimated for \c frg = 1.
*/
class ARX: public BMEF {
protected:
	//!size of output variable (needed in regressors)
	int dimx;
	//!description of modelled data \f$ y_t \f$ in the likelihood function
	//! Do NOT access directly, only via \c get_yrv().
	RV _yrv;
	//! Posterior estimate of \f$\theta,r\f$ in the form of Normal-inverse Wishart density
	egiw est;
	//! cached value of est.V
	ldmat &V;
	//! cached value of est.nu
	double &nu;
public:
	//! \name Constructors
	//!@{
	ARX ( const double frg0=1.0 ) : BMEF ( frg0 ),est (), V ( est._V() ), nu ( est._nu() ) {};
	ARX ( const ARX &A0 ) : BMEF (),est ( A0.est ), V ( est._V() ), nu ( est._nu() ) {};
	ARX* _copy_();
	void set_parameters ( double frg0 ) {frg=frg0;}
	void set_statistics ( int dimx0, const ldmat V0, double nu0=-1.0 ) {est.set_parameters ( dimx0,V0,nu0 );last_lognc=est.lognc();dimx=dimx0;}
	//!@}

//	//! Set parameters given by moments, \c mu (mean of theta), \c R (mean of R) and \c C (variance of theta)
//	void set_parameters ( const vec &mu, const mat &R, const mat &C, double dfm){};
	//! Set sufficient statistics
	void set_statistics ( const BMEF* BM0 );
//	//! Returns sufficient statistics
//	void get_parameters ( mat &V0, double &nu0 ) {V0=est._V().to_mat(); nu0=est._nu();}
	//!\name Mathematical operations
	//!@{

	//! Weighted Bayes \f$ dt = [y_t psi_t] \f$.
	void bayes ( const vec &dt, const double w );
	void bayes ( const vec &dt ) {bayes ( dt,1.0 );};
	double logpred ( const vec &dt ) const;
	void flatten ( const BMEF* B ) {
		const ARX* A=dynamic_cast<const ARX*> ( B );
		// nu should be equal to B.nu
		est.pow ( A->nu/nu );
		if ( evalll ) {last_lognc=est.lognc();}
	}
	//! Conditioned version of the predictor
	enorm<ldmat>* epredictor ( const vec &rgr ) const;
	//! Predictor for empty regressor
	enorm<ldmat>* epredictor() const {it_assert_debug ( est.dimension() ==V.rows()-1,"Regressor is not only 1" );return epredictor ( vec_1 ( 1.0 ) );}
	//! conditional version of the predictor
	mlnorm<ldmat>* predictor() const;
	mlstudent* predictor_student() const;
	//! Brute force structure estimation.\return indeces of accepted regressors.
	ivec structure_est ( egiw Eg0 );
	//!@}

	//!\name Access attributes
	//!@{
	const egiw* _e() const {return &est ;};
	const egiw& _epdf() const {return est;}
	//!@}

	//!\name Connection
	//!@{
	void set_drv ( const RV &drv0 ) {drv=drv0;}
	RV& get_yrv() {
		//if yrv is not ready create it
		if ( _yrv._dsize() !=dimx ) {
			int i=0;
			while ( _yrv._dsize() <dimx ) {_yrv.add ( drv ( vec_1(i) ) );i++;}
		}
		//yrv should be ready by now
		it_assert_debug ( _yrv._dsize() ==dimx,"incompatible drv" );
		return _yrv;
	}
	//!@}
};

}

#endif // AR_H


