*! _fjcoint_cv_sbc.ado
*! Critical values for SBC procedure
*! Fourier-Trace CVs for SBC grid (up to 8 vars, 4 models)
*! SC-VECM break and no-break critical values
*! Source: Pascalau et al. (2022), Online Appendix & GAUSS code
*! Part of fjcoint package

program define _fjcoint_cv_sbc, rclass
  version 14.0

  args type option model f n_r

  * type: "fourier", "nobreak", "break"
  * option: 1=single, 2=cumulative (for fourier)
  * model: 1-4 (for fourier)
  * f: frequency (for fourier), break fraction row (for break)
  * n_r: n-r dimension

  if "`type'" == "nobreak" {
    * 5% critical values of trace test without breaks (trend model)
    * n-r:  1       2       3       4       5       6       7       8
    tempname cv_nb
    matrix `cv_nb' = (11.417, 23.453, 40.093, 60.511, 84.266, 113.479, 148.181, 181.694)
    return scalar cv = `cv_nb'[1, `n_r']
    exit
  }

  if "`type'" == "break" {
    * 5% critical values of SC-VECM trace test with breaks
    * Rows: break fractions 0.20 to 0.80 (13 rows)
    * Cols: n-r = 1 to 8
    tempname cv_br
    matrix `cv_br' = ( ///
      18.238, 35.592, 54.792, 80.841, 109.971, 144.105, 181.728, 221.975 \ ///
      18.675, 35.496, 56.462, 82.441, 111.072, 145.295, 183.790, 224.711 \ ///
      19.060, 36.346, 58.111, 83.015, 112.578, 146.397, 183.065, 225.524 \ ///
      18.953, 37.514, 58.143, 84.500, 113.961, 148.633, 185.059, 226.288 \ ///
      19.052, 37.772, 59.125, 83.779, 114.942, 147.862, 183.674, 228.352 \ ///
      19.386, 37.161, 60.049, 85.043, 114.669, 147.522, 184.661, 228.268 \ ///
      19.433, 37.630, 59.589, 85.410, 115.677, 149.247, 186.269, 227.513 \ ///
      19.151, 37.473, 60.148, 84.451, 114.448, 148.469, 186.010, 228.331 \ ///
      19.188, 38.139, 59.113, 84.792, 114.127, 147.147, 186.128, 226.948 \ ///
      18.810, 37.276, 57.609, 84.144, 113.760, 147.268, 184.999, 224.648 \ ///
      19.047, 36.215, 57.709, 83.754, 113.414, 146.012, 183.213, 226.712 \ ///
      18.334, 36.155, 56.317, 82.000, 111.022, 145.528, 183.468, 225.314 \ ///
      17.886, 35.112, 55.102, 81.178, 109.589, 143.111, 179.543, 222.283 )

    * f here is the row index (break fraction row)
    return scalar cv = `cv_br'[`f', `n_r']
    exit
  }

  if "`type'" == "fourier" {
    * Extended Fourier Trace CVs for SBC (up to 8 variables)
    tempname cv_ft

    if `option' == 1 {
      if `model' == 1 {
        matrix `cv_ft' = ( ///
          14.117, 28.593, 47.072, 70.398, 98.796, 132.166, 171.083, 215.080 \ ///
          10.527, 22.876, 40.767, 63.726, 91.314, 125.268, 163.699, 208.404 \ ///
           9.333, 20.737, 37.299, 58.357, 85.710, 118.207, 156.739, 200.448 \ ///
           8.627, 19.645, 35.272, 55.829, 82.353, 113.624, 151.605, 194.788 \ ///
           8.414, 19.154, 34.488, 54.485, 80.568, 110.982, 147.695, 190.192 )
      }
      else if `model' == 2 {
        matrix `cv_ft' = ( ///
          18.066, 36.168, 58.130, 84.108, 114.828, 151.096, 192.500, 238.201 \ ///
          15.894, 31.380, 51.673, 78.088, 108.315, 143.854, 185.650, 231.050 \ ///
          13.877, 28.321, 47.543, 72.313, 101.699, 137.220, 178.260, 223.866 \ ///
          12.780, 26.765, 45.495, 69.226,  97.321, 132.100, 171.303, 217.486 \ ///
          12.295, 25.626, 43.392, 66.375,  94.402, 127.996, 167.265, 212.655 )
      }
      else if `model' == 3 {
        matrix `cv_ft' = ( ///
          14.606, 30.190, 50.120, 75.141, 104.731, 140.302, 179.385, 223.922 \ ///
          11.349, 25.070, 44.167, 68.496,  98.032, 132.872, 172.776, 218.192 \ ///
          10.320, 23.258, 40.884, 63.286,  92.112, 126.409, 165.460, 211.024 \ ///
           9.536, 21.932, 38.987, 61.237,  88.461, 121.819, 160.053, 204.858 \ ///
           9.623, 21.534, 38.242, 59.605,  86.673, 118.679, 157.064, 200.604 )
      }
      else if `model' == 4 {
        matrix `cv_ft' = ( ///
          19.154, 38.267, 60.998, 88.708, 121.003, 157.309, 200.216, 247.712 \ ///
          16.406, 33.456, 55.200, 82.641, 114.408, 151.427, 194.192, 241.852 \ ///
          14.718, 30.415, 50.968, 76.321, 107.973, 143.811, 185.699, 234.357 \ ///
          13.971, 28.746, 48.826, 73.364, 103.414, 139.529, 180.062, 227.532 \ ///
          13.329, 27.769, 47.133, 71.439, 100.261, 135.931, 176.113, 222.930 )
      }
    }
    else {
      * Cumulative frequency
      if `model' == 1 {
        matrix `cv_ft' = ( ///
          13.703, 28.447, 47.119, 70.480, 98.527, 132.319, 170.753, 214.582 \ ///
          19.548, 38.342, 61.598, 89.729, 122.956, 161.907, 205.609, 254.866 \ ///
          25.505, 48.275, 75.414, 108.950, 146.853, 192.098, 240.077, 295.206 \ ///
          31.263, 58.495, 89.382, 128.623, 172.685, 222.315, 277.490, 336.049 \ ///
          35.989, 68.189, 105.094, 148.180, 197.220, 251.807, 313.082, 379.463 )
      }
      else if `model' == 2 {
        matrix `cv_ft' = ( ///
          18.172, 36.368, 57.995, 84.218, 114.957, 151.277, 191.997, 237.819 \ ///
          24.652, 47.900, 75.394, 107.071, 142.894, 183.965, 230.907, 282.249 \ ///
          31.258, 59.996, 92.695, 130.321, 172.256, 217.533, 269.745, 327.729 \ ///
          37.596, 72.184, 110.387, 153.219, 201.261, 252.884, 310.388, 373.675 \ ///
          43.847, 84.623, 128.324, 176.137, 229.613, 287.270, 351.627, 421.029 )
      }
      else if `model' == 3 {
        matrix `cv_ft' = ( ///
          14.649, 29.687, 49.350, 72.737, 100.815, 133.126, 170.062, 210.562 \ ///
          19.939, 38.986, 62.943, 91.131, 123.612, 160.954, 202.269, 247.611 \ ///
          25.737, 49.241, 76.960, 110.179, 147.973, 189.559, 236.357, 287.495 \ ///
          31.248, 58.803, 91.438, 128.517, 171.394, 217.895, 268.986, 326.326 \ ///
          36.438, 69.063, 105.884, 147.869, 195.106, 246.480, 303.363, 364.899 )
      }
      else if `model' == 4 {
        matrix `cv_ft' = ( ///
          19.147, 37.763, 59.724, 86.185, 116.758, 151.298, 189.563, 233.257 \ ///
          25.709, 49.893, 76.938, 108.308, 143.976, 182.964, 226.308, 274.477 \ ///
          31.710, 61.252, 93.693, 130.465, 170.552, 215.205, 263.488, 316.319 \ ///
          37.917, 72.817, 111.053, 152.003, 197.800, 247.788, 301.601, 359.470 \ ///
          43.925, 84.642, 127.695, 174.955, 225.235, 280.043, 339.052, 403.589 )
      }
    }

    return scalar cv = `cv_ft'[`f', `n_r']
    return matrix cv_matrix = `cv_ft'
    exit
  }

end