Test_indicatorCalculator.dos 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. login(`admin, `123456)
  2. loadPlugin("ODBC")
  3. clearCachedModules()
  4. use fundit::fundCalculator
  5. use fundit::dataPuller
  6. use fundit::returnCalculator
  7. use fundit::indicatorCalculator
  8. /* init values for test cases */
  9. end_day = 2024.08.31; //2024.06.28;
  10. fund_ids = "'MF00003PW1','MF00003RS0'"; //"'HF000004KN','HF000103EU','HF00018WXG'";
  11. entity_type = "MF"; //'HF';
  12. isFromNav = true;
  13. /* codes from cal_fund_indicators */
  14. very_old_date = 1990.01.01;
  15. start_month = very_old_date.month();
  16. fund_info = get_fund_info(fund_ids);
  17. fund_info.rename!('fund_id', 'entity_id');
  18. if(isFromNav == true) {
  19. // 从净值开始计算收益
  20. tb_ret = SELECT * FROM cal_fund_monthly_returns(entity_type, fund_ids, true) WHERE price_date <= end_day;
  21. tb_ret.rename!(['fund_id', 'cumulative_nav'], ['entity_id', 'nav']);
  22. } else {
  23. // 从fund_performance表里读月收益
  24. tb_ret = get_monthly_ret('FD', fund_ids, very_old_date, end_day, true);
  25. tb_ret.rename!(['fund_id'], ['entity_id']);
  26. v_end_date = tb_ret.end_date.temporalParse('yyyy-MM');
  27. tb_ret.replaceColumn!('end_date', v_end_date);
  28. }
  29. // 取基金和基准的对照表
  30. primary_benchmark = SELECT fund_id AS entity_id, end_date, iif(benchmark_id.isNull(), 'IN00000008', benchmark_id) AS benchmark_id
  31. FROM get_fund_primary_benchmark(fund_ids, start_month.temporalFormat('yyyy-MM'), end_day.month().temporalFormat('yyyy-MM')) ;
  32. // 取所有出现的基准月收益
  33. bmk_ret = get_benchmark_return(primary_benchmark, end_day);
  34. risk_free_rate = SELECT fund_id, temporalParse(end_date, 'yyyy-MM') AS end_date, ret FROM get_risk_free_rate(very_old_date, end_day);
  35. entity_info = fund_info;
  36. entity_ids = fund_ids.strReplace("'", "").split(',');
  37. /* Test START */
  38. // trailing 2 year standard indicators
  39. rtn = cal_basic_performance(entity_info, tb_ret, '24');
  40. @testing: case = 'trailing 2y return'
  41. assert (select rtn.trailing_ret.round(4) as trailing_ret
  42. from rtn where entity_id in entity_ids and end_date in (2024.07M, 2024.08M)).trailing_ret == [-0.285094, -0.297968, 0.031337, 0.013295].round(4);
  43. @testing: case = 'trailing 2y std_dev_a'
  44. assert (select (rtn.std_dev * sqrt(12)).round(4) as std_dev_a
  45. from rtn where entity_id in entity_ids and end_date in (2024.07M, 2024.08M)).std_dev_a == [0.152789, 0.153595, 0.035974, 0.037412].round(4);
  46. @testing: case = 'trailing 2y skewness'
  47. assert (select rtn.skewness.round(2) as skewness
  48. from rtn where entity_id in entity_ids and end_date in (2024.07M, 2024.08M)).skewness == [-0.311358, -0.258296, 0.301213, 0.364476].round(2);
  49. @testing: case = 'trailing 2y kurtosis'
  50. assert (select rtn.kurtosis.round(2) as kurtosis
  51. from rtn where entity_id in entity_ids and end_date in (2024.07M, 2024.08M)).kurtosis == [2.425262, 2.284389, 4.222244, 3.397426].round(2);
  52. @testing: case = 'trailing 2y wrst_month'
  53. assert (select rtn.wrst_month.round(4) as wrst_month
  54. from rtn where entity_id in entity_ids and end_date in (2024.07M, 2024.08M)).wrst_month == [-0.134443, -0.134443, -0.026231, -0.026231].round(4);
  55. @testing: case = 'trailing 2y max drawdown'
  56. assert (select rtn.drawdown.round(4) as drawdown // values from swagger
  57. from rtn where entity_id in entity_ids and end_date in (2024.07M, 2024.08M)).drawdown == [0.376406, 0.376406, 0.044551, 0.044551].round(4);
  58. @testing: case = 'trailing 2y var'
  59. assert (select rtn.var.round(4) as var // [FAIL]
  60. from rtn where entity_id in entity_ids and end_date in (2024.07M, 2024.08M)).var == [0.120004, 0.120004, 0.023467, 0.023467].round(4);
  61. @testing: case = 'trailing 2y cvar'
  62. assert (select rtn.cvar.round(4) as cvar // [FAIL]
  63. from rtn where entity_id in entity_ids and end_date in (2024.07M, 2024.08M)).cvar == [0.134443, 0.134443, 0.026231, 0.026231].round(4);
  64. @testing: case = 'trailing 2y calmar'
  65. assert (select rtn.calmar.round(4) as calmar // [FAIL]
  66. from rtn where entity_id in entity_ids and end_date in (2024.07M, 2024.08M)).calmar == [-0.381017, -0.404476, 0.283223, 0.123738].round(4);
  67. // year-to-day standard indicators
  68. rtn = cal_basic_performance(entity_info, tb_ret, 'ytd');
  69. @testing: case = 'ytd return'
  70. assert (select rtn.trailing_ret.round(4) as trailing_ret
  71. from rtn where entity_id in entity_ids and end_date in (2024.07M, 2024.08M)).trailing_ret == [-0.026889, -0.061460, 0.025689, 0.011772].round(4);
  72. @testing: case = 'ytd std_dev_a'
  73. assert (select (rtn.std_dev * sqrt(12)).round(4) as std_dev_a
  74. from rtn where entity_id in entity_ids and end_date in (2024.07M, 2024.08M)).std_dev_a == [0.236684, 0.222985, 0.060555, 0.059946].round(4);
  75. @testing: case = 'skewness'
  76. assert (select rtn.skewness.round(2) as skewness
  77. from rtn where entity_id in entity_ids and end_date in (2024.07M, 2024.08M)).skewness == [-1.007946, -0.748978, -0.150076, 0.186127].round(2);
  78. @testing: case = 'ytd kurtosis'
  79. assert (select rtn.kurtosis.round(3) as kurtosis
  80. from rtn where entity_id in entity_ids and end_date in (2024.07M, 2024.08M)).kurtosis == [3.206713, 2.737161, 1.998417, 1.046022].round(3);
  81. @testing: case = 'ytd wrst_month'
  82. assert (select rtn.wrst_month.round(4) as wrst_month
  83. from rtn where entity_id in entity_ids and end_date in (2024.07M, 2024.08M)).wrst_month == [-0.134443, -0.134443, -0.026231, -0.026231].round(4);
  84. @testing: case = 'ytd max drawdown' // values from swagger
  85. assert (select rtn.drawdown.round(4) as drawdown
  86. from rtn where entity_id in entity_ids and end_date in (2024.07M, 2024.08M)).drawdown == [0.006631, 0.038057, 0.002126, 0.014966].round(4);
  87. @testing: case = 'ytd calmar' // [FAIL]
  88. assert (select rtn.calmar.round(4) as calmar
  89. from rtn where entity_id in entity_ids and end_date in (2024.07M, 2024.08M)).calmar == [-0.131580, -0.368421, 0.885691, 0.432796].round(4);
  90. // since-inception-date standard indicators
  91. rtn = cal_basic_performance(entity_info, tb_ret, 'incep');
  92. @testing: case = 'incep return'
  93. assert (select rtn.trailing_ret.round(4) as trailing_ret
  94. from rtn where entity_id in entity_ids and end_date in (2024.07M, 2024.08M)).trailing_ret == [3.918337, 3.743607, 0.268718, 0.251503].round(4);
  95. @testing: case = 'incep std_dev_a'
  96. assert (select (rtn.std_dev * sqrt(12)).round(2) as std_dev_a
  97. from rtn where entity_id in entity_ids and end_date in (2024.07M, 2024.08M)).std_dev_a == [0.215823, 0.215617, 0.026656, 0.027113].round(2);
  98. @testing: case = 'incep skewness' // [FAIL] SLIGHTLY OFF
  99. assert (select rtn.skewness.round(2) as skewness
  100. from rtn where entity_id in entity_ids and end_date in (2024.07M, 2024.08M)).skewness == [0.032796, 0.039251, -0.172258, -0.189262].round(2);
  101. @testing: case = 'incep kurtosis' // [FAIL] SLIGHTLY OFF
  102. assert (select rtn.kurtosis.round(2) as kurtosis
  103. from rtn where entity_id in entity_ids and end_date in (2024.07M, 2024.08M)).kurtosis == [1.882971, 1.884570, 3.413939, 3.097421].round(2);
  104. @testing: case = 'incep wrst_month'
  105. assert (select rtn.wrst_month.round(4) as wrst_month
  106. from rtn where entity_id in entity_ids and end_date in (2024.07M, 2024.08M)).wrst_month == [-0.221418, -0.221418, -0.026231, -0.026231].round(4);
  107. @testing: case = 'incep drawdown' // values from swagger
  108. assert (select rtn.drawdown.round(4) as drawdown
  109. from rtn where entity_id in entity_ids and end_date in (2024.07M, 2024.08M)).drawdown == [0.545799, 0.545799, 0.044551, 0.044551].round(4);
  110. @testing: case = 'incep var' // [FAIL]
  111. assert (select rtn.var.round(4) as var
  112. from rtn where entity_id in entity_ids and end_date in (2024.07M, 2024.08M)).var == [0.098647, 0.098641, 0.012435, 0.012578].round(4);
  113. @testing: case = 'incep cvar' // [FAIL]
  114. assert (select rtn.cvar.round(4) as cvar
  115. from rtn where entity_id in entity_ids and end_date in (2024.07M, 2024.08M)).cvar == [0.139853, 0.139853, 0.016819, 0.017078].round(4);
  116. @testing: case = 'incep calmar' // [FAIL]
  117. assert (select rtn.calmar.round(4) as calmar
  118. from rtn where entity_id in entity_ids and end_date in (2024.07M, 2024.08M)).calmar == [0.123467, 0.120115, 0.564536, 0.526222].round(4);
  119. // trailing 2 year lpms
  120. lpms = cal_omega_sortino_kappa(tb_ret, risk_free_rate, '24');
  121. @testing: case = 'trailing 2y downside deviation' // slightly off
  122. assert (select (lpms.ds_dev * sqrt(12))round(2) as ds_dev
  123. from lpms where entity_id in entity_ids and end_date in (2024.07M, 2024.08M)).ds_dev == [0.139780, 0.141592, 0.024837, 0.027034].round(2);
  124. @testing: case = 'trailing 2y omega' // [FAIL]
  125. assert (select lpms.omega.round(4) as omega
  126. from lpms where entity_id in entity_ids and end_date in (2024.07M, 2024.08M)).omega == [0.412705, 0.405461, 0.971179, 0.797404].round(4);
  127. @testing: case = 'trailing 2y sortino' // [FAIL]
  128. assert (select (lpms.sortino * sqrt(12)).round(4) as sortino_a
  129. from lpms where entity_id in entity_ids and end_date in (2024.07M, 2024.08M)).sortino_a == [-1.228926, -1.274774, -0.022708, -0.341091].round(4);
  130. @testing: case = 'trailing 2y kappa' // [FAIL]
  131. assert (select lpms.kappa.round(4) as kappa
  132. from lpms where entity_id in entity_ids and end_date in (2024.07M, 2024.08M)).kappa == [-1.400459, -1.456120, -0.050796, -0.406572].round(4);
  133. // ytd lpms
  134. lpms = cal_omega_sortino_kappa(tb_ret, risk_free_rate, 'ytd');
  135. @testing: case = 'ytd downside deviation' // [FAIL] slightly off
  136. assert (select (lpms.ds_dev * sqrt(12))round(2) as ds_dev_a
  137. from lpms where entity_id in entity_ids and end_date in (2024.07M, 2024.08M)).ds_dev_a == [0.192270, 0.184391, 0.039096, 0.041043].round(2);
  138. @testing: case = 'ytd omega' // [FAIL]
  139. assert (select lpms.omega.round(4) as omega
  140. from lpms where entity_id in entity_ids and end_date in (2024.07M, 2024.08M)).omega == [0.799024,0.670686,1.477292,1.039491].round(4);
  141. @testing: case = 'ytd sortino' // [FAIL]
  142. assert (select (lpms.sortino * sqrt(12)).round(4) as sortino_a
  143. from lpms where entity_id in entity_ids and end_date in (2024.07M, 2024.08M)).sortino_a == [-0.187914,-0.470279,0.783559,0.113041].round(4);
  144. @testing: case = 'ytd kappa' // [FAIL]
  145. assert (select lpms.kappa.round(4) as kappa
  146. from lpms where entity_id in entity_ids and end_date in (2024.07M, 2024.08M)).kappa == [-0.352469,-0.644631, 0.862919, 0.090396].round(4);
  147. // since inception lpms
  148. lpms = cal_omega_sortino_kappa(tb_ret, risk_free_rate, 'incep');
  149. @testing: case = 'incep downside deviation'
  150. assert (select (lpms.ds_dev * sqrt(12))round(2) as ds_dev_a
  151. from lpms where entity_id in entity_ids and end_date in (2024.07M, 2024.08M)).ds_dev_a == [0.140304, 0.140258, 0.017540, 0.018214].round(2);
  152. @testing: case = 'incep omega' // [FAIL] slightly off
  153. assert (select lpms.omega.round(4) as omega
  154. from lpms where entity_id in entity_ids and end_date in (2024.07M, 2024.08M)).omega == [1.217150,1.208021,1.541471,1.437023].round(4);
  155. @testing: case = 'incep sortino' // [FAIL] slightly off
  156. assert (select (lpms.sortino * sqrt(12)).round(4) as sortino_a
  157. from lpms where entity_id in entity_ids and end_date in (2024.07M, 2024.08M)).sortino_a == [0.509534,0.496273,0.809670,0.671315].round(4);
  158. @testing: case = 'incep kappa' // [FAIL]
  159. assert (select lpms.kappa.round(4) as kappa
  160. from lpms where entity_id in entity_ids and end_date in (2024.07M, 2024.08M)).kappa == [0.372816,0.358662,0.817694,0.684683].round(4);
  161. // 2y bechmark tracking
  162. bmk_tracking = cal_benchmark_tracking(tb_ret, primary_benchmark, bmk_ret, '24');
  163. @testing: case = 'trailing2y win rate'
  164. assert (select winrate.round(4) as winrate
  165. from bmk_tracking where entity_id in entity_ids and end_date in (2024.07M, 2024.08M)).winrate == [0.416667,0.416667,0.375000,0.375000].round(4);
  166. @testing: case = 'trailing2y tracking error'
  167. assert (select (track_error * sqrt(12)).round(3) as track_error_a
  168. from bmk_tracking where entity_id in entity_ids and end_date in (2024.07M, 2024.08M)).track_error_a == [0.153428,0.153685,0.039662,0.040514].round(3);
  169. @testing: case = 'trailing2y information ratio' // [FAIL]
  170. assert (select info.round(4) as info
  171. from bmk_tracking where entity_id in entity_ids and end_date in (2024.07M, 2024.08M)).info == [5.609963 ,5.569559 ,24.083419,23.449405].round(4);
  172. // ytd bechmark tracking
  173. bmk_tracking = cal_benchmark_tracking(tb_ret, primary_benchmark, bmk_ret, 'ytd');
  174. @testing: case = 'ytd win rate'
  175. assert (select winrate.round(4) as winrate
  176. from bmk_tracking where entity_id in entity_ids and end_date in (2024.07M, 2024.08M)).winrate == [0.428571,0.375000,0.571429,0.500000].round(4);
  177. @testing: case = 'ytd tracking error'
  178. assert (select (track_error * sqrt(12)).round(2) as track_error_a
  179. from bmk_tracking where entity_id in entity_ids and end_date in (2024.07M, 2024.08M)).track_error_a == [0.172443,0.161193,0.066815,0.063451].round(2);
  180. @testing: case = 'ytd information ratio' // [FAIL]
  181. assert (select info.round(4) as info
  182. from bmk_tracking where entity_id in entity_ids and end_date in (2024.07M, 2024.08M)).info == [5.170620 ,5.392240 ,14.194093,14.655729].round(4);
  183. // incep bechmark tracking
  184. bmk_tracking = cal_benchmark_tracking(tb_ret, primary_benchmark, bmk_ret, 'incep');
  185. @testing: case = 'incep win rate' // [FAIL]
  186. assert (select winrate.round(4) as winrate
  187. from bmk_tracking where entity_id in entity_ids and end_date in (2024.07M, 2024.08M)).winrate == [0.446494,0.444853,0.458333,0.453608].round(4);
  188. @testing: case = 'incep tracking error'
  189. assert (select (track_error * sqrt(12)).round(3) as track_error_a
  190. from bmk_tracking where entity_id in entity_ids and end_date in (2024.07M, 2024.08M)).track_error_a == [0.133228,0.133079,0.026158,0.026478].round(3);
  191. @testing: case = 'incep information ratio' // [FAIL]
  192. assert (select info.round(4) as info
  193. from bmk_tracking where entity_id in entity_ids and end_date in (2024.07M, 2024.08M)).info == [7.727110 ,7.713382 ,37.492177,36.942698].round(4);
  194. // 2y alpha, beta
  195. alpha_beta = cal_alpha_beta(tb_ret, primary_benchmark, bmk_ret, risk_free_rate, '24');
  196. @testing: case = 'trailing2y beta'
  197. assert (select beta.round(2) as beta
  198. from alpha_beta where entity_id in entity_ids and end_date in (2024.07M, 2024.08M)).beta == [0.484263,0.497796,0.035154,0.087369].round(2);
  199. @testing: case = 'trailing2y alpha' // [FAIL]
  200. assert (select (alpha * 12).round(3) as alpha_a
  201. from alpha_beta where entity_id in entity_ids and end_date in (2024.07M, 2024.08M)).alpha_a == [-0.147936,-0.155315,0.014126 ,0.002815].round(3);
  202. // ytd alpha, beta
  203. alpha_beta = cal_alpha_beta(tb_ret, primary_benchmark, bmk_ret, risk_free_rate, 'ytd');
  204. @testing: case = 'ytd beta' // [FAIL] slightly off
  205. assert (select beta.round(4) as beta
  206. from alpha_beta where entity_id in entity_ids and end_date in (2024.07M, 2024.08M)).beta == [2.461836 ,2.444270 ,-1.772254,-0.576641].round(4);
  207. @testing: case = 'ytd alpha' // [FAIL]
  208. assert (select (alpha * 12).round(3) as alpha_a
  209. from alpha_beta where entity_id in entity_ids and end_date in (2024.07M, 2024.08M)).alpha_a == [-0.148226,-0.140843,0.199161 ,0.064237].round(3);
  210. // incep alpha, beta
  211. alpha_beta = cal_alpha_beta(tb_ret, primary_benchmark, bmk_ret, risk_free_rate, 'incep');
  212. @testing: case = 'incep beta' // [FAIL] slightly off
  213. assert (select beta.round(4) as beta
  214. from alpha_beta where entity_id in entity_ids and end_date in (2024.07M, 2024.08M)).beta == [1.418055,1.418947,0.530804,0.540277].round(4);
  215. @testing: case = 'incep alpha' // [FAIL]
  216. assert (select (alpha * 12).round(3) as alpha_a
  217. from alpha_beta where entity_id in entity_ids and end_date in (2024.07M, 2024.08M)).alpha_a == [-0.001796,-0.002707,0.007096 ,0.004842].round(3);
  218. // 2y capture indicators
  219. capture_r = cal_capture_ratio(tb_ret, primary_benchmark, bmk_ret, '24');
  220. @testing: case = 'trailing2y upside capture return'
  221. assert (select upside_capture_ret.round(4) as upside_capture_ret
  222. from capture_r where entity_id in entity_ids and end_date in (2024.07M, 2024.08M)).upside_capture_ret == [-0.003743,-0.003743, 0.002156, 0.001314].round(4);
  223. @testing: case = 'trailing2y upside capture ratio'
  224. assert (select upside_capture_ratio.round(3) as upside_capture_ratio
  225. from capture_r where entity_id in entity_ids and end_date in (2024.07M, 2024.08M)).upside_capture_ratio == [-0.223084,-0.223084, 0.365611, 0.237662].round(3);
  226. @testing: case = 'trailing2y downside capture return'
  227. assert (select downside_capture_ret.round(4) as downside_capture_ret
  228. from capture_r where entity_id in entity_ids and end_date in (2024.07M, 2024.08M)).downside_capture_ret == [-0.022388,-0.023754,-0.004776,-0.004776].round(4);
  229. @testing: case = 'trailing2y downside capture ratio'
  230. assert (select downside_capture_ratio.round(4) as downside_capture_ratio
  231. from capture_r where entity_id in entity_ids and end_date in (2024.07M, 2024.08M)).downside_capture_ratio == [1.341475,1.357044,1.014519,1.014519].round(4);
  232. // ytd capture indicators
  233. capture_r = cal_capture_ratio(tb_ret, primary_benchmark, bmk_ret, 'ytd');
  234. @testing: case = 'ytd upside capture return'
  235. assert (select upside_capture_ret.round(4) as upside_capture_ret
  236. from capture_r where entity_id in entity_ids and end_date in (2024.07M, 2024.08M)).upside_capture_ret == [0.027001,0.027001,0.003630,0.001464].round(4);
  237. @testing: case = 'ytd upside capture ratio'
  238. assert (select upside_capture_ratio.round(4) as upside_capture_ratio
  239. from capture_r where entity_id in entity_ids and end_date in (2024.07M, 2024.08M)).upside_capture_ratio == [1.622489,1.622489,0.501649,0.224863].round(4);
  240. @testing: case = 'ytd downside capture return'
  241. assert (select downside_capture_ret.round(4) as downside_capture_ret
  242. from capture_r where entity_id in entity_ids and end_date in (2024.07M, 2024.08M)).downside_capture_ret == [-0.043629,-0.041610,null,null].round(4);
  243. @testing: case = 'ytd downside capture ratio' // SWAGGER
  244. assert (select downside_capture_ratio.round(4) as downside_capture_ratio
  245. from capture_r where entity_id in entity_ids and end_date in (2024.07M, 2024.08M)).downside_capture_ratio == [3.491326,3.061985,null,null].round(4);
  246. // incep capture indicators
  247. capture_r = cal_capture_ratio(tb_ret, primary_benchmark, bmk_ret, 'incep');
  248. @testing: case = 'incep upside capture return'
  249. assert (select upside_capture_ret.round(3) as upside_capture_ret
  250. from capture_r where entity_id in entity_ids and end_date in (2024.07M, 2024.08M)).upside_capture_ret == [0.039939,0.039939,0.003983,0.003744].round(3);
  251. @testing: case = 'incep upside capture ratio' // [FAIL] slightly off
  252. assert (select upside_capture_ratio.round(3) as upside_capture_ratio
  253. from capture_r where entity_id in entity_ids and end_date in (2024.07M, 2024.08M)).upside_capture_ratio == [1.411524,1.411524,0.644453,0.612097].round(3);
  254. @testing: case = 'incep downside capture return'
  255. assert (select downside_capture_ret.round(4) as downside_capture_ret
  256. from capture_r where entity_id in entity_ids and end_date in (2024.07M, 2024.08M)).downside_capture_ret == [-0.044225,-0.044131,-0.002280,-0.002280].round(4);
  257. @testing: case = 'incep downside capture ratio'
  258. assert (select downside_capture_ratio.round(4) as downside_capture_ratio
  259. from capture_r where entity_id in entity_ids and end_date in (2024.07M, 2024.08M)).downside_capture_ratio == [1.490631,1.494426,0.498609,0.498609].round(4);
  260. // 2y sharpe
  261. rtn = cal_basic_performance(entity_info, tb_ret, '24');
  262. sharpe = cal_sharpe(tb_ret, rtn, risk_free_rate, '24');
  263. @testing: case = 'trailing2y sharpe'
  264. assert (select (sharpe *sqrt(12)).round(4) as sharpe_a
  265. from sharpe where entity_id in entity_ids and end_date in (2024.07M, 2024.08M)).sharpe_a == [-1.124291,-1.175150,-0.015678,-0.246472].round(4);
  266. // ytd sharpe
  267. rtn = cal_basic_performance(entity_info, tb_ret, 'ytd');
  268. sharpe = cal_sharpe(tb_ret, rtn, risk_free_rate, 'ytd');
  269. @testing: case = 'ytd sharpe'
  270. assert (select (sharpe *sqrt(12)).round(3) as sharpe_a
  271. from sharpe where entity_id in entity_ids and end_date in (2024.07M, 2024.08M)).sharpe_a == [-0.152652,-0.388883, 0.505893, 0.077395].round(3);
  272. // incep sharpe
  273. rtn = cal_basic_performance(entity_info, tb_ret, 'incep');
  274. sharpe = cal_sharpe(tb_ret, rtn, risk_free_rate, 'incep');
  275. @testing: case = 'incep sharpe' // [FAIL] slightly off
  276. assert (select (sharpe *sqrt(12)).round(4) as sharpe_a
  277. from sharpe where entity_id in entity_ids and end_date in (2024.07M, 2024.08M)).sharpe_a == [0.331242,0.322824,0.532794,0.450987].round(4);
  278. // 2y treynor
  279. alpha_beta = cal_alpha_beta(tb_ret, primary_benchmark, bmk_ret, risk_free_rate, '24');
  280. treynor = cal_treynor(tb_ret, risk_free_rate, alpha_beta, '24');
  281. @testing: case = 'trailing2y treynor'
  282. assert (select treynor.round(2) as treynor
  283. from treynor where entity_id in entity_ids and end_date in (2024.07M, 2024.08M)).treynor == [-0.353583,-0.359078,-0.034126,-0.114400].round(2);
  284. // ytd treynor
  285. alpha_beta = cal_alpha_beta(tb_ret, primary_benchmark, bmk_ret, risk_free_rate, 'ytd');
  286. treynor = cal_treynor(tb_ret, risk_free_rate, alpha_beta, 'ytd');
  287. @testing: case = 'ytd treynor' // [FAIL]
  288. assert (select treynor.round(4) as treynor
  289. from treynor where entity_id in entity_ids and end_date in (2024.07M, 2024.08M)).treynor == [-0.024472,-0.043101,-0.016843,-0.005406].round(4);
  290. // incep treynor
  291. alpha_beta = cal_alpha_beta(tb_ret, primary_benchmark, bmk_ret, risk_free_rate, 'incep');
  292. treynor = cal_treynor(tb_ret, risk_free_rate, alpha_beta, 'incep');
  293. @testing: case = 'incep treynor' // [FAIL] slightly off
  294. assert (select treynor.round(4) as treynor
  295. from treynor where entity_id in entity_ids and end_date in (2024.07M, 2024.08M)).treynor == [0.035549,0.034146,0.026646,0.022403].round(4);
  296. // 2y jensen
  297. alpha_beta = cal_alpha_beta(tb_ret, primary_benchmark, bmk_ret, risk_free_rate, '24');
  298. jensen = cal_jensen(tb_ret, bmk_ret, risk_free_rate, alpha_beta, '24');
  299. @testing: case = 'trailing2y treynor'
  300. assert (select (jensen*12).round(2) as jensen_a
  301. from jensen where entity_id in entity_ids and end_date in (2024.07M, 2024.08M)).jensen_a == [-0.156508,-0.163600,-0.001910,-0.012240].round(2);
  302. // ytd jensen
  303. alpha_beta = cal_alpha_beta(tb_ret, primary_benchmark, bmk_ret, risk_free_rate, 'ytd');
  304. jensen = cal_jensen(tb_ret, bmk_ret, risk_free_rate, alpha_beta, 'ytd');
  305. @testing: case = 'ytd treynor' // [FAIL] slightly off if annulize it
  306. assert (select jensen.round(4) as jensen_a
  307. from jensen where entity_id in entity_ids and end_date in (2024.07M, 2024.08M)).jensen_a == [-0.127035,-0.119907,0.158975 ,0.041382].round(4);
  308. // incep jensen
  309. alpha_beta = cal_alpha_beta(tb_ret, primary_benchmark, bmk_ret, risk_free_rate, 'incep');
  310. jensen = cal_jensen(tb_ret, bmk_ret, risk_free_rate, alpha_beta, 'incep');
  311. @testing: case = 'incep treynor' // [FAIL] slightly off
  312. assert (select (jensen*12).round(4) as jensen_a
  313. from jensen where entity_id in entity_ids and end_date in (2024.07M, 2024.08M)).jensen_a == [ 0.006436 , 0.005535 , -0.000363, -0.002461].round(4);
  314. // 2y m2
  315. m2 = cal_m2(tb_ret, primary_benchmark, bmk_ret, risk_free_rate, '24');
  316. @testing: case = 'trailing2y m2' // [FAIL] slightly off
  317. assert (select (m2*12).round(4) as m2_a
  318. from m2 where entity_id in entity_ids and end_date in (2024.07M, 2024.08M)).m2_a == [-0.072046, -0.076473, 0.016349 , 0.012278].round(4);
  319. // ytd m2
  320. m2 = cal_m2(tb_ret, primary_benchmark, bmk_ret, risk_free_rate, 'ytd');
  321. @testing: case = 'ytd m2' // [FAIL] slightly off
  322. assert (select (m2*12).round(4) as m2_a
  323. from m2 where entity_id in entity_ids and end_date in (2024.07M, 2024.08M)).m2_a == [0.002002, -0.015889, 0.021197, 0.015593].round(4);
  324. // incep m2
  325. m2 = cal_m2(tb_ret, primary_benchmark, bmk_ret, risk_free_rate, 'incep');
  326. @testing: case = 'incep m2' // [FAIL] slightly off
  327. assert (select (m2*12).round(4) as m2_a
  328. from m2 where entity_id in entity_ids and end_date in (2024.07M, 2024.08M)).m2_a == [0.064601,0.063365,0.026901,0.025155].round(4);
  329. /* Tests for BFI indicators. CURRENTLY IT IS UN-TESTABLE BECAUSE LOGIC OF BENCHMARK COMPARING IS CHANGED */
  330. /* codes from cal_fund_bfi_indicators */
  331. start_month = 1990.01M;
  332. // 取基金和基准的对照表
  333. bfi_benchmark = SELECT fund_id AS entity_id, end_date.temporalParse('yyyy-MM') AS end_date, factor_id AS benchmark_id
  334. FROM get_fund_bfi_factors(fund_ids, start_month.temporalFormat('yyyy-MM'), end_day.temporalFormat('yyyy-MM'));
  335. bfi_bmk_ret = get_benchmark_return(bfi_benchmark, end_day);