'==================================================================================================
'==================================================================================================
'==================================================================================================

subroutine local deanton_uni (series y_lf_ser, series y_hf_ser, series x_hf_ser, scalar ta, scalar dd, scalar s, scalar p)

' ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
'	PURPOSE:	Temporal disaggregation using Deanton method
' ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
'	SYNTAX:		series y_hf_ser
'					call deanton_uni (y_lf_ser, y_hf_ser, x_hf_ser, ta, dd, s, p)
' ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
'	OUTPUT:		y_hf_ser	high frequency estimate series name
' ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
'	INPUT:		y_lf_ser	low frequency series name, series must be disaggregated
'								equaly on subperiods
'					x_hf_ser	high frequency related series name
'					ta:			type of temporal disaggregation
'					ta=1		sum (flow)
'					ta=2		average (index)
'					ta=3 		last  element (stock) - interpolation
'					ta=4 		first  element (stock) - interpolation
'					dd:			order of diferentiation (objective function to be minimized): volatility of ...
'					dd=0		levels
'					dd=1		first order diferentiation
'					dd=2 		second order diferentiation
'					s:			number of high frequency points 
'								for each low frequency data points (freq. conversion)
'					s=4		annual to quarterly
'					s=12		annual to monthly
'					s=3		quarterly to monthly
'					p:			indicate proportional method
'					p=0		ordinanary method
'					p=1		proportional method
' ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
'	LIBRARY:		d:\Eviews_library\agg_help.prg: AGG() DIF()
' ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
'	writen by:
'	Slawomir Dudek
'	MOF 
' ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
'	REFERENCE:	1) Quilis, E.M (2002) "A MATLAB library of temporal disaggregation methods: summary"
'						    Instituto Nacional de Estadistica, Spain
'						2) Deanton, F.T. (1971) "Adjustment of monthly or quarterly series to annual totals:
'						    an approach based on quadratic minimization"
'						    Journal of the American Statistical Society, vol. 66, n. 333, p. 99-102.
' ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

'	Asiggning series to vectors, only not NA observations
'	Determination of forward extrapolation period: !n_up
'	Determination of backward extrapolation period: !n_dn

smpl @all
series t=@trend+1

sample NA_smpl_y @all if y_lf_ser <> NA
sample NA_smpl_x @all if x_hf_ser <> NA

smpl NA_smpl_x
!obs_f_x=@min(t)
!obs_l_x=@max(t)

smpl NA_smpl_y
!obs_f_y=@min(t)
!obs_l_y=@max(t)

!n_up=!obs_l_x-!obs_l_y
!n_dn=!obs_f_y-!obs_f_x

' ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

'	Control checking wether related series cover aggregate sample

if (!n_up<0 OR !n_dn<0) then

statusline  ERROR!!!  Deanton_uni subroutine() !!!*** Related series do not cover aggregate sample ***!!!
stop

else

' ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

'	Assigning series to matrix objects, only for NA observations

stomna(y_lf_ser, y_lf_temp, NA_smpl_y)
stomna(x_hf_ser, x_hf, NA_smpl_x)

' ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

'	Size of the problem

!n_hf=@rows(y_lf_temp)
!n_lf=!n_hf/s

!n_hf_x=@rows(x_hf)

' ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

'	Aggregation of low frequency series equaly distributed in subperiods

matrix AGG1
call aggreg (AGG1, 1, !n_lf, s, 0, 0)
matrix y_lf=AGG1*y_lf_temp
delete AGG1

' ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

'	Generation of aggregation matrix AGG [!n_lf x !n_lf*s] for procedure and penalty matrix DIF (differentiation)

matrix AGG
call aggreg (AGG, ta, !n_lf, s, !n_up, !n_dn)

matrix DIF
call dif(DIF, dd, !n_hf_x, 0)

' ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

'	Temporal aggreagation of the indicator

matrix x_lf=AGG*x_hf

' ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

'	Computation of low frequency residuals

matrix u_lf=y_lf-x_lf

' ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

'	Computation of VCV matrix H n x n

matrix H=@transpose(DIF)*DIF

'	Computation of disaggregation matrix CC n x n
'	1) non-proportional case
'	2) proportional case

if p=0 then
	matrix CC=@inverse(H)*@transpose(AGG)*@inverse(AGG*@inverse(H)*@transpose(AGG))
else
	if p=1 then
		matrix Z=@makediagonal(x_hf)
		matrix CC=Z*@inverse(H)*Z*@transpose(AGG)*@inverse(AGG*Z*@inverse(H)*Z*@transpose(AGG))
		delete Z
	else
	statusline ERROR!!!  Deanton_uni subroutine() !!!*** Improper value of parameter [p: proportional or no-proportional method = 1/0] ***!!!
	stop
	endif
endif

' ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

'	Computation of high frequency series = indicator + annual residuals
'	temporaly disaggregated by disaggregation matrix CC

matrix y_hf=x_hf+CC*u_lf

' ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

'	Assigning vector to series

mtos(y_hf, y_hf_ser, NA_smpl_x)

endif

smpl @all

endsub

'==================================================================================================
'==================================================================================================
'==================================================================================================

