'==================================================================================================
'==================================================================================================
'==================================================================================================

subroutine local bfl (series y_lf_ser,  series y_hf_ser, scalar ta, scalar dd, scalar s)

' ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
'	PURPOSE:	Temporal disaggregation using Boot-Feibes-Lisman method
' ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
'	SYNTAX:		series y_hf_ser
'					call bfl (y_lf_ser, y_hf_ser, ta, dd, s)
' ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
'	OUTPUT:		y_hf_ser	high frequency estimate series name
' ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
'	INPUT:		y_lf_ser	low frequency series name, series must be disaggregated
'								equaly on subperiods, there are not allowed NA inside sample
'					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
' ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
'	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) Boot, J.C.G., Feibes, W., Lisma, J.H.C. (1967)
'						    "Further methods of Derivation of Quarterly Figures from Annual data"
'						    Applied Statistics, vol. 16, n. 1, p. 65-75.
' ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

'	Asiggning series to vectors, only not NA observations

sample NA_smpl_y @all if y_lf_ser <> NA
stomna(y_lf_ser, y_lf_temp, NA_smpl_y)

' ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

'	Size of the problem

!n_hf=@rows(y_lf_temp)
!n_lf=!n_hf/s

' ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

'	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 for procedure

matrix AGG
call aggreg (AGG, ta, !n_lf, s, 0, 0)

' ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

'	Generation of (implicit) VCV martix H: n x n
'	depending on the selected objective function
'	Generation of penalty matrix DIF n-1 x n (modified)

matrix DIF
call dif(DIF, dd, !n_hf, 1)
matrix H=@transpose(DIF)*DIF

' ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

'	Computing high frequency series:
'	1) Generation of supermatrix AA
'	2) Expanding low frequency input
'	3) y_hf=inv(AA)*y_lf_e

matrix(!n_lf, !n_lf) zeros=0
matrix(!n_hf,1) y_lf_e_zeros=0
matrix(!n_lf+!n_hf, !n_lf+!n_hf) AA
matrix(!n_lf+!n_hf,1) y_lf_e

matplace(AA, H, 1, 1)
matplace(AA, @transpose(AGG), 1, !n_hf+1)
matplace(AA, AGG, !n_hf+1, 1)
matplace(AA, zeros, !n_hf+1, !n_hf+1)

matplace(y_lf_e,y_lf_e_zeros,1,1)
matplace(y_lf_e, y_lf, !n_hf+1, 1)

matrix y_hf_e=@inverse(AA)*y_lf_e
matrix y_hf = @subextract(y_hf_e,1,1,!n_hf,1)

' ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

'	Assigning vector to series

mtos(y_hf, y_hf_ser, NA_smpl_y)

smpl @all

endsub

'==================================================================================================
'==================================================================================================
'==================================================================================================

