'Author: Nwabisa Florence Ndzama
'Corvinus Univeristy of Budapest
'Supervisor: Zsolt Darvas
' probability.prg
' LAST MODIFIED: 16 NOVEMBER 2024

'PhD thesis: Chapters 4 & 5


!read = 1 
!Nwabisa =1'0
!Zsolt = 0 '1
!stop_after_read = 0

if !read = 1 then
'----------------------------------------------------------------------------------
 close CA_forecast_Qreg.wf1
  wfcreate(wf=CA_reg, page = Quarterly) q 1960 2030

  smpl 1980 2023 
  'For %sheet CA GDP_growth  Budget_balance Trade_Openness REER OAD 
       if !Nwabisa = 1 then
          'read(c3,s={%sheet},t)"C:\Users\P519987\Dropbox\2023-04 CA reversals\Chapter 5_Results and Discussion\Thesis Data\Quantile Regression\SA_QUARTERELY DATA.xls" 270 '204   ' 204 is the number of observations (countries + regions in the sample) from excel file
       read(b4,s=Sheet2)"C:\Users\P519987\Dropbox\2023-04 CA reversals\Chapter 5_Results and Discussion\Thesis Data\Quantile Regression\SA_QUARTERELY DATA.xls" 6        ' 6= series are read from a sheet named SHEET3 using names provided in the file.
       endif
 
        if !Zsolt = 1 then
    '      read(c3,s={%sheet},t) "C:\Users\Zsolt\Dropbox\2023-04 CA reversals\Data\WEO_data_for_probit.xls" 204
           read(c3,s={%sheet},t)"C:\Users\Zsolt\Dropbox\2023-04 CA reversals\Chapter 5_Results and Discussion\Thesis Data\Eviews\WEO_data_for_Chapter 5_probabilities.xls" 204   ' 204 is the number of observations (countries + regions in the sample) from excel file

          endif   
  'Next ' %sheet
'stop
  ' these series are not included in the dataset; we create them (with NA values) to eliminate some error messages when this code is running
 ' series REER_ZD_SOM 
 ' series REER_ZD_TLS
 ' series REER_ZD_PRI 

'1948 value in the excel file is the numerical code of the country. To avoid confusion of these numerical codes with actual data, we create a new sheet for the sample 1960-2029 and copy all data there:
'Pagecreate(page=Annual) a 1960 2029   '1977- 2028  , Starting few years before our year (1983) of interest (due to data for all other variables in the analysis)
'copy Read\* Annual\*

if !stop_after_read = 1 then
     Statusline Stopped after reading the data
      STOP 
endif

endif !read=1
'STOP


group a_group_var  CA_zaf GDP_GROWTH_zaf BUDGET_BALANCE_zaf TRADE_OPENNESS_zaf REER_zaf OAD_zaf 
'STOP


'Pagecreate(page=distributionVAR)  1000000
!forc_period_ahead = 12'20 '16'  1 2 3 4 5   number of periods ahead for which out-of-sample forecasts will be made from each date. For Annual DATA, 5 means 5 years.
!n = 1000000 '1000 ' The number of observations (e.g. the number of forecasts made)
!threshold = -8 '-8   '-5 '-4 '-2   ' The threshold value: this code calculates the probability that an observation is BELOW this value. 

'=================================================================================================
'Nwabisa 22/06/2024 MODEL TYPE
%forecast_model =  "QREG"  '  QREG, AR, VAR, BVAR,vecm, BTVC, STVAR, SWTVAR
'====================================================================================================
%start_est_date = "1980Q1"  ' the first date for the estimation of models
%start_forecast_date =  "2005Q1" '2005 is  the first date for which a forecast will be made, therefore the recursive estimation goes upto 2004, that is 1980-2004
%last_Q_obs = "2023Q4"  ' the last observation of the sample period
!forc_period_ahead = 12 '20 '16    IF quarterely data, 16 = 4 years ahead

'!start_forecast_date = @val(%start_forecast_date)
'!last_Q_obs = @val(%last_Q_obs)
'!num_forecasts = !last_Q_obs - !start_forecast_date  ' e.g. if the %start_forecast_date =  "2017" and %last_Y_obs = "2018" , then !num_forecasts=1
' !num_forecasts = !last_Y_obs - !start_forecast_date +1

!start_forecast_date = @dtoo(%start_forecast_date)


smpl %start_forecast_date %last_Q_obs
d temp*         
genr temp = 1 
!forecast_length = @obs(temp) ' WE HAVE SET THE SAMPLE BETWEEN %start_forecast_date AND %last_Q_obs AND HERE WE GET THE NUMBER OF OBSERVATIONS IN THIS SAMPLE
smpl @all



'for each country, we create a separate page, e.g. for South Africa the name of the page will be:  distributionVAR_ZAF
%pagename = "distributionQREG" + "_" + "ZAF" '%c


'------------------------------------------------------------------------------------------------------------------------------------------------------------
       ' Step 1: Estimate the Model
'------------------------------------------------------------------------------------------------------------------------------------------------------------
 if %forecast_model = "QREG" then

   Pageselect Quarterly

table table_probabilitiesQREG 'Create a table that collects the probability results
table_probabilitiesQREG(1,1) = "This table shows the probability that the CA balance will be lower than a threshold value"
table_probabilitiesQREG(2,1) = "model: "
table_probabilitiesQREG(2,2) = "QREG"  
table_probabilitiesQREG(3,1) = "number of forecasts (!n): "
table_probabilitiesQREG(3,2) = !n
table_probabilitiesQREG(4,1) = "threshold: "
table_probabilitiesQREG(4,2) = !threshold

table_probabilitiesQREG(5,1) = "Forecast Period: "
table_probabilitiesQREG(5,2) = "1 year ahead"
table_probabilitiesQREG(5,3) = "2 year ahead"
table_probabilitiesQREG(5,4) = "3 year ahead"
table_probabilitiesQREG(5,5) = "4 year ahead"
table_probabilitiesQREG(5,6) = "5 year ahead"


table_probabilitiesQREG(6,1) = "country"
table_probabilitiesQREG(6,2) = "probability"
table_probabilitiesQREG(6,3) = "probability"
table_probabilitiesQREG(6,4) = "probability"
table_probabilitiesQREG(6,5) = "probability"
table_probabilitiesQREG(6,6) = "probability"

' Zsolt 01/06/2024: important to add this info to the table, so you'll surely know for which period the analysis is made
table_probabilitiesQREG(5,8) = "%start_est_date"
table_probabilitiesQREG(5,9) = "%start_forecast_date"
table_probabilitiesQREG(5,10) = "%last_Y_obs"
       

' 24 Aug 2024: I added the CA balance value at %last_Y_obs, and the forecasts for the next 5 years, which help to the check visually if estimated probabilities look correct or not

table_probabilitiesQREG(4,12) = "Actual CA balance"
table_probabilitiesQREG(5,12) = "%last_Y_obs"
table_probabilitiesQREG(4,13) = "Forecast CA balance"
table_probabilitiesQREG(5,13) = "%last_Y_obs+1"
table_probabilitiesQREG(4,14) = "Forecast CA balance"
table_probabilitiesQREG(5,14) = "%last_Y_obs+2"
table_probabilitiesQREG(4,15) = "Forecast CA balance"
table_probabilitiesQREG(5,15) = "%last_Y_obs+3"
table_probabilitiesQREG(4,16) = "Forecast CA balance"
table_probabilitiesQREG(5,16) = "%last_Y_obs+4"
table_probabilitiesQREG(4,17) = "Forecast CA balance"
table_probabilitiesQREG(5,17) = "%last_Y_obs+5"

' Zsolt 01/06/2024: for each country, we create a separate page, e.g. for South Africa the name of the page will be:  distributionVAR_ZAF
'%pagename = "distributionVAR" + "_" + %c


' Zsolt 01/06/2024: we add this info to the table, so you'll surely know for which period the analysis is made 
'table_probabilitiesQREG(6+!c,1) = %c
'table_probabilitiesQREG(6+!c,8) = %start_est_date
'table_probabilitiesQREG(6+!c,9) = %start_forecast_date
'table_probabilitiesQREG(6+!c,10) = %last_Y_obs



Pagecreate(page={%pagename}) U !n

 Pageselect Quarterly



'var estimates
        'For !smpl = 0 to !num_forecasts 
' --------------------------------------------------------------------
For !smpl = 0 to !forecast_length-1
' --------------------------------------------------------------------
' --------------------------------------------------------------------
!current_forecast_date = !start_forecast_date + !smpl  ' The first quarter for which we make a forecast
%current_forecast_date = @otod(!current_forecast_date) 'THIS IS A STRING WHICH INCLUDES THE CURRENT FORECAST DATE
!current_forecast_date_m1 = !current_forecast_date - 1 ' The quarter preceding the first quarter for which we make a forecast; "m1"= minus 1, i.e. the last date for which actual data is used. In this case "2010Q4" I SUGGEST TO USE "M" FOR MINUS
%current_forecast_date_m1 = @otod(!current_forecast_date_m1)


Statusline {%fx}    Start forecast date {%start_forecast_date}   Smpl = !smpl   {%current_forecast_date}

  ' --------------------------------------------------------------------------------------
      '1st you estimate the model
  '------------------------------------------------------------------------------------
        '  smpl %start_est_date %start_forecast_date-1+!smpl ' so when %start_forecast_date =  "2017" and !smpl = 0, you estimate the model till 2016 and make a forecast for 2017
         smpl %start_est_date %start_forecast_date+!smpl-1
		' qreg CA_zaf GDP_GROWTH_zaf BUDGET_BALANCE_zaf TRADE_OPENNESS_zaf REER_zaf OAD_zaf 'TRADE_OPENNESS_{%c} BUDGET_BALANCE_{%c}   'GDP_GROWTH_{%c} REER_ZD_{%c} OAD_{%c} 'TRADE_OPENNESS_{%c} 'BUDGET_BALANCE_{%c}  '' NOTE: I changed the lag lenght to 2 so that fewer parameters have to be estimated, which allows a longer out-of-sample forecast period
        ' qreg(quant=0.6, cov=boot, btmethod=mcmba)  CA_zaf GDP_GROWTH_zaf BUDGET_BALANCE_zaf TRADE_OPENNESS_zaf REER_zaf OAD_zaf 
          equation qreg_zaf.qreg(quant=0.5)  CA_zaf GDP_GROWTH_zaf  BUDGET_BALANCE_zaf TRADE_OPENNESS_zaf REER_zaf 'OAD_zaf ' QUANTILES 0.1 ; 0.9; 0.5; 
          qreg_zaf.makemodel(model_CA_qreg_zaf)
      'Next !smpl
 ' endif '%forecast_model = "QREG"
 'Next '!c
'Stop

'-----------------------------------------------------------------------------------------------------------------------------------------------------------
          'Step 2: Generate point forecasts and forecast errors
'-----------------------------------------------------------------------------------------------------------------------------------------------------------
          smpl %start_forecast_date+!smpl %start_forecast_date+!smpl+!forc_period_ahead 'oos 'out-of-sample
          d CA_zaf_0* 
           solve model_CA_qreg_zaf   
           genr CA_zaf_0_qreg = CA_zaf_0

'stop
         For !h = 1 to !forc_period_ahead  ' WHEN !h=1 THEN 1-QUARTER AHEAD FORECAST IS MADE, WHEN !h=2 THEN 2-PERIOD-AHEAD FORECAST IS MADE, AND SO ON. SEE HOW THE SAMPLE IS SET TWO LINES BELOW. WHEN !h=1 THIS IS THE 1-PERIOD AHEAD FORECAST, WHEN !h=2 THIS IS THE 2-PERIOD AHEAD FORECAST, AND SO ON
         %h = @str(!h)
         smpl %start_forecast_date+!smpl+!h-1 %start_forecast_date+!smpl+!h-1

		'1-year ahead forecast errors
           'smpl %start_forecast_date+!smpl %start_forecast_date+!smpl
           'genr errorf_1y_ca_zaf_qreg = CA_zaf - CA_zaf_0_qreg' CA forecasts error
		genr errorf_{%h}Q_ca_zaf_qreg = CA_zaf - CA_zaf_0_qreg' CA forecasts error
        
           '2-year ahead forecast errors
           'smpl %start_forecast_date+!smpl+1 %start_forecast_date+!smpl+1
          ' genr errorf_2y_ca_zaf_qreg= CA_zaf - CA_zaf_0_qreg ' CA forecasts error
           
           ' 3-year ahead forecast errors
          ' smpl %start_forecast_date+!smpl+2 %start_forecast_date+!smpl+2
        '   genr errorf_3y_ca_zaf_qreg = CA_zaf - CA_zaf_0_qreg ' CA forecasts error
        
            ' 4-year ahead forecast errors
           'smpl %start_forecast_date+!smpl+3 %start_forecast_date+!smpl+3
        '   genr errorf_4y_ca_zaf_qreg = CA_zaf - CA_zaf_0_qreg  ' CA forecasts error
         

         ' 5-year ahead forecast errors
        '  smpl %start_forecast_date+!smpl+4 %start_forecast_date+!smpl+4
        '  genr errorf_5y_ca_zaf_qreg = CA_zaf - CA_zaf_0_qreg  ' CA forecasts error
           Next ' !h
         Next ' !smpl

  endif '%forecast_model = "QREG"
 'Next '!c
Stop

                '---------------------------------------------------------------------------------------------------------------------------
          'STEP 2.1: create a matrix  for forecast errors to prepare for bootstrapping
         '------------------------------------------------------------------------------------------------------------------------
            smpl %start_forecast_date %last_Y_obs     '2005 - 2023

            matrix errorf_1y_ca_zaf_qreg_m1
            stom(errorf_1y_ca_zaf_qreg,errorf_1y_ca_zaf_qreg_m1) 'Convert series or group to matrix removing NAs. We have lost one observationfrom 18 to 17 obs. because trade openness has "NA" for 2023.
           'show  errorf_1y_ca_zaf_qreg_m1
            
            matrix errorf_2y_ca_zaf_qreg_m2
            stom(errorf_2y_ca_zaf_qreg,errorf_2y_ca_zaf_qreg_m2) 'Convert series or group to matrix removing NAs. We have lost one observationfrom 18 to 17 obs. because trade openness has "NA" for 2023.
           'show  errorf_2y_ca_zaf_qreg_m2
      
            matrix errorf_3y_ca_zaf_qreg_m3
            stom(errorf_3y_ca_zaf_qreg,errorf_3y_ca_zaf_qreg_m3) 'Convert series or group to matrix removing NAs. We have lost one observationfrom 18 to 17 obs. because trade openness has "NA" for 2023.
           'show  errorf_3y_ca_zaf_qreg_m3

            matrix errorf_4y_ca_zaf_qreg_m4
            stom(errorf_4y_ca_zaf_qreg,errorf_4y_ca_zaf_qreg_m4) 'Convert series or group to matrix removing NAs. We have lost one observationfrom 18 to 17 obs. because trade openness has "NA" for 2023.
           'show  errorf_4y_ca_zaf_qreg_m4

            matrix errorf_5y_ca_zaf_qreg_m5
            stom(errorf_5y_ca_zaf_qreg,errorf_5y_ca_zaf_qreg_m5) 'Convert series or group to matrix removing NAs. We have lost one observationfrom 18 to 17 obs. because trade openness has "NA" for 2023.
           'show  errorf_5y_ca_zaf_qreg_m5

  'endif '%forecast_model = "QREG"
 'Next '!c
'Stop


'--------------------------------------------------------------------------------------------------------------------------
' Step 3:  Perform bootstrapping: Randomly resample from forecast errors
'--------------------------------------------------------------------------------------------------------------------------
  
      '----------------------------------------------------------------------------------------------------------------------   
      'Resampling process
      '----------------------------------------------------------------------------------------------------------------------
      '(i) We first create a matrix or vector for forecast errors = DONE
      '+++++++++++++++++++++++++++++++++++++++++++++++++++++++
       

       '(ii) We resample a 1000 times '24 Aug 2024 No, you should resample !n times
          smpl %start_forecast_date %last_Y_obs                              '2005 - 2023

' 24 Aug 2024 EViews help says: n2 represents the number of “extra” rows to be drawn from the matrix. If the input matrix has r rows and c columns, the output matrix will have r+n2 rows and c columns. By default, n2=0.
' Thus, if you want to get a bootstrapped matrix that has !n rows, then you should set n2 = !n - (the number of rows in the input matrix). Since n2 depends on !n and the number of rows in the input matrix, it is better to automatise the calculation of this number, rather than setting it manually.

          !rows_m1 = @rows(errorf_1y_ca_zaf_qreg_m1)
          !rows_m2 = @rows(errorf_2y_ca_zaf_qreg_m2)
          !rows_m3 = @rows(errorf_3y_ca_zaf_qreg_m3)
          !rows_m4 = @rows(errorf_4y_ca_zaf_qreg_m4)
          !rows_m5 = @rows(errorf_5y_ca_zaf_qreg_m5)


          matrix bootQreg_1y_m1_zaf = @resample(errorf_1y_ca_zaf_qreg_m1, !n-!rows_m1)
          matrix bootQreg_2y_m2_zaf = @resample(errorf_2y_ca_zaf_qreg_m2, !n-!rows_m2) 
          matrix bootQreg_3y_m3_zaf = @resample(errorf_3y_ca_zaf_qreg_m3, !n-!rows_m3)
          matrix bootQreg_4y_m4_zaf = @resample(errorf_4y_ca_zaf_qreg_m4, !n-!rows_m4)
          matrix bootQreg_5y_m5_zaf = @resample(errorf_5y_ca_zaf_qreg_m5, !n-!rows_m5)

'---------------------------------------------------------------------------------------------------------------------------------------------------------------------
'New Page: Distribution. Process: 1. Copy the  bootstrapped matrix from 'Annual' to 'distribution' page; 2. Convert the marix to a series
'---------------------------------------------------------------------------------------------------------------------------------------------------------------------

Pageselect Quarterly
         copy Quarterly\bootQreg_1y_m1_zaf {%pagename}\*
         copy Quarterly\bootQreg_2y_m2_zaf {%pagename}\*
         copy Quarterly\bootQreg_3y_m3_zaf {%pagename}\*
         copy Quarterly\bootQreg_4y_m4_zaf {%pagename}\*
         copy Quarterly\bootQreg_5y_m5_zaf {%pagename}\*


Pageselect {%pagename}

' READ EVIEWS HELP: "If the target group object does not exist, the group is created and populated with series named <prefix>1, <prefix>2, etc. If the prefix is omitted, the default prefix is “SER”." THUS, IN YOUR VERSION, ca_1fperiod_draw_var_{%c} BECAME A GROUP AND ITS SINGLE TIME SERIES WAS NAMED SER1. WHEN YOU CREATE MANY SERIES NAMED SER1, SER2, ETC, IT WILL BE DIFFICULT TO KNOW WHAT IS INCLUDED IN. BETTER TO TREAT ca_1fperiod_draw_var_{%c} AS A SERIES (NOT A GROUP).

         series  ca_1y_m1_boot_qreg_zaf
         mtos(bootQreg_1y_m1_zaf, ca_1y_m1_boot_qreg_zaf})  'converts the matrix to series  

         series ca_2y_m2_boot_qreg_zaf
         mtos(bootQreg_2y_m2_zaf, ca_2y_m2_boot_qreg_zaf})  'converts the matrix to series  

         series ca_3y_m3_boot_qreg_zaf
         mtos(bootQreg_3y_m3_zaf, ca_3y_m3_boot_qreg_zaf})  'converts the matrix to series  

         series ca_4y_m4_boot_qreg_zaf
         mtos(bootQreg_4y_m4_zaf, ca_4y_m4_boot_qreg_zaf})  'converts the matrix to series   

         series ca_5y_m5_boot_qreg_zaf
         mtos(bootQreg_5y_m4_zaf, ca_5y_m4_boot_qreg_zaf})  'converts the matrix to series   
         
'Next '!c
'STOP


'-----------------------------------------------------------------------------------------------------------------------------------------------------------------
'STEP 4:  Create a simulated series - adding the resampled errors to the original point forecasts.
'-------------------------------------------------------------------------------------------------------------------------------------------------------------------

 Pageselect Quarterly
        

   
!last_Y_obs_plus_1 = !last_Y_obs + 1
!last_Y_obs_plus_2 = !last_Y_obs + 2
!last_Y_obs_plus_3 = !last_Y_obs + 3
!last_Y_obs_plus_4 = !last_Y_obs + 4
!last_Y_obs_plus_5 = !last_Y_obs + 5

%last_Y_obs_plus_1 = @str(!last_Y_obs_plus_1)
%last_Y_obs_plus_2 = @str(!last_Y_obs_plus_2)
%last_Y_obs_plus_3 = @str(!last_Y_obs_plus_3)
%last_Y_obs_plus_4 = @str(!last_Y_obs_plus_4)
%last_Y_obs_plus_5 = @str(!last_Y_obs_plus_5)

scalar  ca_zaf_point_forecast_{%last_Y_obs_plus_1} = @elem(CA_zaf_0_qreg,%last_Y_obs_plus_1)    
scalar  ca_zaf_point_forecast_{%last_Y_obs_plus_2} = @elem(CA_zaf_0_qreg,%last_Y_obs_plus_2)    
scalar  ca_zaf_point_forecast_{%last_Y_obs_plus_3} = @elem(CA_zaf_0_qreg,%last_Y_obs_plus_3)    
scalar  ca_zaf_point_forecast_{%last_Y_obs_plus_4} = @elem(CA_zaf_0_qreg,%last_Y_obs_plus_4)    
scalar  ca_zaf_point_forecast_{%last_Y_obs_plus_5} = @elem(CA_zaf_0_qreg,%last_Y_obs_plus_5)  



'====================================================================================================
           'copy current account point forecast from "annual page" to "distribution" page
'====================================================================================================

copy Quarterly\ca_zaf_point_forecast_{%last_Y_obs_plus_1} {%pagename}\*    'copy current account point forecast from "annual page" to "distribution" page
copy Quarterly\ca_zaf_point_forecast_{%last_Y_obs_plus_2} {%pagename}\*    'copy current account point forecast from "annual page" to "distribution" page
copy Quarterly\ca_zaf_point_forecast_{%last_Y_obs_plus_3}{%pagename}\*    'copy current account point forecast from "annual page" to "distribution" page
copy Quarterly\ca_zaf_point_forecast_{%last_Y_obs_plus_4} {%pagename}\*    'copy current account point forecast from "annual page" to "distribution" page
copy Quarterly\ca_zaf_point_forecast_{%last_Y_obs_plus_5} {%pagename}\*    'copy current account point forecast from "annual page" to "distribution" page


'====================================================================================================       
    'generating 1 forecast period ahead; combining point forcasts and the random draws which we generated , with zero mean
'====================================================================================================
 Pageselect {%pagename}

 genr ca_1_forecast_draws_zaf =  ca_1y_m1_boot_qreg_zaf +ca_zaf_point_forecast_{%last_Y_obs_plus_1}  'generating 1 period ahead forecast distrbution; combining point forecasts and the random draws which we generated with zero mean
 genr ca_2_forecast_draws_zaf =  ca_2y_m2_boot_qreg_zaf +ca_zaf_point_forecast_{%last_Y_obs_plus_2}  'generating 2 periods ahead forecast distrbution; combining point forecasts and the random draws which we generated with zero mean
 genr ca_3_forecast_draws_zaf =  ca_3y_m3_boot_qreg_zaf +ca_zaf_point_forecast_{%last_Y_obs_plus_3}   'generating 3 periods ahead forecast distrbution; combining point forecasts and the random draws which we generated with zero mean
 genr ca_4_forecast_draws_zaf =  ca_4y_m4_boot_qreg_zaf +ca_zaf_point_forecast_{%last_Y_obs_plus_4}  'generating 4 periods ahead forecast distrbution; combining point forecasts and the random draws which we generated with zero mean
 genr ca_5_forecast_draws_zaf =  ca_5y_m5_boot_qreg_zaf +ca_zaf_point_forecast_{%last_Y_obs_plus_5}  'generating 5 periods ahead forecast distrbution; combining point forecasts and the random draws which we generated with zero mean



'===================================================================================================
' Calculating the probabilities
'-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
'steps:
'   1. Sort the series (lowest value first, highest value last)
'   2. With a for/next loop, we calculate how the number of observations which are below the threshold
'   3. We divided the number of such observations by the total number of observations (e.g. if there 150 observations below -8 and the total number of observations is 1000, then the probability is 150/1000=15%
'   4. We write the result to a table
'==================================================================================================

copy Quarterly\ table_probabilitiesQREG {%pagename}\*

For !quarter_ahead = 1 to 20
   %quarter_ahead = @str(!quarter_ahead)
   sort  ca_{%quarter_ahead}_forecast_draws_zaf
   For !draw = 1 to !n
      %draw = @str(!draw) ' we need to create string for draw, because the @elem function in the next line requires a string as an argument
      if @elem(ca_{%quarter_ahead}_forecast_draws_zaf,%draw) > !threshold then
          exitloop 'this command terminates the for/next loop, so the last value of !draw will be the observation number of the first observation which is larger than the threshold, so the probability is: (!draw-1)/!n
      endif
   Next '!draw
    table_probabilitiesQREG(6+!c,1+!quarter_ahead) = (!draw-1)/!n  'NB
Next' !year_ahead

 table_probabilitiesQREG(5,12) = "Actual CA balance"
 table_probabilitiesQREG(5,12) = "%last_Y_obs"

' 08 Sep 2024: adding the forecast years
 table_probabilitiesQREG(6,13) = %last_Y_obs_plus_1
 table_probabilitiesQREG(6,14) = %last_Y_obs_plus_2
 table_probabilitiesQREG(6,15) = %last_Y_obs_plus_3
 table_probabilitiesQREG(6,16) = %last_Y_obs_plus_4
 table_probabilitiesQREG(6,17) = %last_Y_obs_plus_5

' adding the forecasts
'table_probabilitiesboot_AR(6+!c,13) = ca_{%c}_point_forecast_{%last_Y_obs_plus_1}
'table_probabilitiesboot_AR(6+!c,14) = ca_{%c}_point_forecast_{%last_Y_obs_plus_2}
'table_probabilitiesboot_AR(6+!c,15) = ca_{%c}_point_forecast_{%last_Y_obs_plus_3}
'table_probabilitiesboot_AR(6+!c,16) = ca_{%c}_point_forecast_{%last_Y_obs_plus_4}
'table_probabilitiesboot_AR(6+!c,17) = ca_{%c}_point_forecast_{%last_Y_obs_plus_5}


copy {%pagename}\table_probabilitiesQREG Quarterly\*
      
Pageselect Quarterly


' 24 Aug 2024: adding the actual value for %last_obs
 table_probabilitiesQREG(6,12) = %last_Y_obs
 table_probabilitiesQREG(6+!c,12) = @elem(ca_{%c},%last_Y_obs)   'NB

show  table_probabilitiesQREG

Statusline READY - WELL DONE!

  'endif '%forecast_model = "Qreg" 
'Next '!c
'STOP


