'==================================================================================================
'==================================================================================================
'==================================================================================================

subroutine local cholette_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 Cholette method, modified Deanton method
' ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
'	SYNTAX:		series y_lf_ser
'					call cholette_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) Cholette, P.A. (1984) "Adjusting sub-annual series to yearly benchmarks"
'						        
' ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
' ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

'	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!!!  Cholette_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)

'	Generation of penalty matrix DIF n-1 x n (modified)

matrix DIF
call dif(DIF, dd, !n_hf_x, 1)


' ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

'	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

'	Computing high frequency series:
'	1) Generation of supermatrix AA =inv[AA1] * AA2
'	2) Expanding low frequency input
'	3) y_hf=x_hf + inv(AA)*u_lf


if p=0 then
	matrix(!n_lf+!n_hf_x, !n_lf+!n_hf_x) AA1
	matplace(AA1, H, 1, 1)
	matplace(AA1, @transpose(AGG), 1, !n_hf_x+1)
	matplace(AA1, AGG, !n_hf_x+1, 1)

	matrix(!n_lf+!n_hf_x, !n_lf+!n_hf_x) AA2
	matplace(AA2, H, 1, 1)
	matrix i1=@identity(!n_lf)
	matplace(AA2, i1 , !n_hf_x+1, !n_hf_x+1)

	matrix AA=@inverse(AA1)*AA2
	
else
	if p=1 then
		matrix Z=@makediagonal(x_hf)
		matrix(!n_lf+!n_hf_x, !n_lf+!n_hf_x) AA1
		matplace(AA1, @inverse(Z)*H*@inverse(Z), 1, 1)
		matplace(AA1, @transpose(AGG), 1, !n_hf_x+1)
		matplace(AA1, AGG, !n_hf_x+1, 1)

		matrix(!n_lf+!n_hf_x, !n_lf+!n_hf_x) AA2
		matplace(AA2, @inverse(Z)*H*@inverse(Z), 1, 1)
		matrix i2=@identity(!n_lf)
		matplace(AA2,i2 , !n_hf_x+1, !n_hf_x+1)

		matrix AA=@inverse(AA1)*AA2
		delete Z
	else
	statusline ERROR!!!  Cholette_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 CHOLETTE

matrix CHOLETTE = @subextract(AA,1,!n_hf_x+1,!n_hf_x,!n_hf_x+!n_lf)	'Cholette matrix
matrix y_hf=x_hf+CHOLETTE*u_lf

' ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

'	Assigning vector to series

mtos(y_hf, y_hf_ser, NA_smpl_x)

endif

smpl @all

endsub

'==================================================================================================
'==================================================================================================
'==================================================================================================

