indicatorCalculator.dos 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289
  1. module fundit::indicatorCalculator
  2. use fundit::sqlUtilities
  3. use fundit::operationDataPuller
  4. use fundit::performanceDataPuller
  5. use fundit::ms_dataPuller
  6. use fundit::returnCalculator
  7. use fundit::navCalculator
  8. /*
  9. * 将VaR包裹一层,使之成为系统认可的聚集函数
  10. * @param returns <DOUBLE VECTOR>: 非空收益率
  11. * @param method <STRING>: 'normal', 'logNormal', 'historical', 'monteCarlo'
  12. * @param confidenceLevel <DOUBLE>: 置信水平,取值区间(0, 1)
  13. *
  14. */
  15. defg aggVaR(returns, method, confidenceLevel) {
  16. if(returns.form() != 1) return null;
  17. return returns.VaR(method, confidenceLevel);
  18. }
  19. /*
  20. * 将CVaR包裹一层,使之成为系统认可的聚集函数
  21. * @param returns <DOUBLE VECTOR>: 非空收益率
  22. * @param method <STRING>: 'normal', 'logNormal', 'historical', 'monteCarlo'
  23. * @param confidenceLevel <DOUBLE>: 置信水平,取值区间(0, 1)
  24. *
  25. */
  26. defg aggCVaR(returns, method, confidenceLevel) {
  27. if(returns.form() != 1) return null;
  28. return returns.CVaR(method, confidenceLevel);
  29. }
  30. /*
  31. * 最大回撤
  32. *
  33. *
  34. */
  35. defg maxDrawdown(navs) {
  36. return max(1 - navs \ cummax(navs));
  37. }
  38. /*
  39. * 几何平均值
  40. *
  41. */
  42. defg geometricMean(x){
  43. return x.log().avg().exp()
  44. }
  45. /*
  46. * Trailing Monthly Return, Standard Deviation, Skewness, Kurtosis, Max Drawdown, VaR, CVaR, Calmar Ratio
  47. *
  48. * @param entity_info <TABLE>: xxx_information表,NEED COLUMNS entity_id, inception_date
  49. * @param ret <TABLE>: 收益表,需要有 entity_id, price_dat, end_date, nav
  50. * @param trailing_month <STRING>: trailing X month or ytd, incep
  51. *
  52. * NOTE: standard deviation of Java version is noncompliant-GIPS annulized number
  53. *
  54. * Create: 20240904 Joey
  55. * TODO: SQL is wrong for max drawdowns
  56. * TODO: var, cvar, calmar are off; std dev, skewness, kurtosis are slightly off
  57. * TODO: SQL is missing for portfolio since inception date return
  58. * TODO: Java calculates max drawdown even there is no nav
  59. * TODO: Java ytd worst month could be wrong (i.e. portfolio 166002, 2024-03)
  60. * TODO: arith_mean & gerom_mean ARE NOT TESTED
  61. *
  62. */
  63. def cal_basic_performance(entity_info, ret, trailing_month) {
  64. // accumulate 版的 skewness, kurtosis, var, cvar 似乎都不对劲,只好找个笨办法来实现
  65. if(trailing_month == 'incep') {
  66. // 需要至少6个数才计算标准差、峰度、偏度
  67. t0 = SELECT price_date.max() AS price_date, nav, ret,
  68. ret.mean() AS arith_mean, (1+ret).prod().pow(1\count(entity_id))-1 AS geom_mean,
  69. iif(count(entity_id) > 5, std(ret), null) AS std_dev,
  70. iif(count(entity_id) > 5, skew(ret, false), null) AS skewness,
  71. iif(count(entity_id) > 5, kurtosis(ret, false), null)-3 AS kurtosis,
  72. min(ret) AS wrst_month
  73. FROM ret
  74. WHERE ret > -1
  75. GROUP BY entity_id
  76. CGROUP BY end_date
  77. ORDER BY entity_id, end_date;
  78. // 年化收益(给后面计算Calmar用)
  79. t0.addColumn(['trailing_ret', 'trailing_ret_a'], [DOUBLE, DOUBLE]);
  80. // MySQL 有bug导致首月ret_1m为空,所以用 prod(1+ret)-1算的有时不对
  81. UPDATE t0
  82. SET trailing_ret = nav\ini_value - 1,
  83. trailing_ret_a = iif(t0.end_date - ei.inception_date.month() > 12, (nav\ini_value).pow(12\(t0.end_date - ei.inception_date.month())) - 1, nav\ini_value - 1)
  84. FROM ej(t0, entity_info ei, 'entity_id');
  85. // 不会用上面的办法算最大回撤, VaR, CVaR
  86. t_var = SELECT entity_id, end_date, ret,
  87. cummax(1 - nav \ cummax(nav)) AS drawdown,
  88. - cumpercentile(ret, 5, 'linear') AS var
  89. FROM ret WHERE ret > -1
  90. CONTEXT BY entity_id;
  91. t_cvar = SELECT entity_id, end_date, drawdown, var,
  92. - cumavg(iif(ret <= -var, ret, null)) AS cvar
  93. FROM t_var
  94. CONTEXT BY entity_id;
  95. t1 = SELECT t0.*, t_cvar.drawdown, t_cvar.var, t_cvar.cvar
  96. FROM t0 LEFT JOIN t_cvar ON t0.entity_id = t_cvar.entity_id AND t0.end_date = t_cvar.end_date
  97. ORDER BY t0.entity_id, t0.end_date;
  98. } else if(trailing_month == 'ytd') {
  99. t1 = SELECT entity_id, end_date, price_date.cummax() AS price_date, nav, ret,
  100. ret.cumavg() AS arith_mean, (1+ret).cumprod().pow(1\cumcount(entity_id))-1 AS geom_mean,
  101. cumprod(1+ret)-1 AS trailing_ret,
  102. cumprod(1+ret)-1 AS trailing_ret_a, // no need annulization for ytd
  103. iif(cumcount(entity_id) > 5, cumstd(ret), null) AS std_dev,
  104. iif(cumcount(entity_id) > 5, tmoving(skew{, false}, end_date, ret, 12), null) AS skewness,
  105. iif(cumcount(entity_id) > 5, tmoving(kurtosis{, false}, end_date, ret, 12)-3, null) AS kurtosis,
  106. cummin(ret) AS wrst_month,
  107. cummax(1 - nav \ cummax(nav)) AS drawdown
  108. FROM ret WHERE ret > -1
  109. CONTEXT BY entity_id, end_date.year()
  110. ORDER BY entity_id, end_date;
  111. // trailing x month
  112. } else {
  113. // 先转成STRING,避免单字符被认为是CHAR而导致转整型出错的结果
  114. win = trailing_month$STRING$INT;
  115. t1 = SELECT entity_id, end_date, price_date.mmax(win) AS price_date, nav, ret,
  116. ret.mavg(win) AS arith_mean, (1+ret).mprod(win).pow(1\mcount(entity_id, win))-1 AS geom_mean,
  117. mprod(1+ret, win)-1 AS trailing_ret,
  118. iif(win > 12,
  119. mprod(1+ret, win).pow(12\win)-1,
  120. mprod(1+ret, win)-1) AS trailing_ret_a,
  121. mstd(ret, win) AS std_dev,
  122. mskew(ret, win, false) AS skewness,
  123. mkurtosis(ret, win, false) - 3 AS kurtosis,
  124. mmin(ret, win) AS wrst_month,
  125. moving(maxDrawdown, nav, win) AS drawdown,
  126. moving(aggVaR{, 'historical', 0.95}, ret, win) AS var,
  127. moving(aggCVaR{, 'historical', 0.95}, ret, win) AS cvar
  128. FROM ret WHERE ret > -1
  129. CONTEXT BY entity_id
  130. ORDER BY entity_id, end_date;
  131. // price_date.mmax(win) 当行数不够 win 时会返回 null, 补回这些日期
  132. UPDATE t1
  133. SET t1.price_date = ret.price_date.max()
  134. FROM ej(t1, ret, ['entity_id', 'end_date'])
  135. WHERE t1.price_date IS NULL
  136. CONTEXT BY t1.entity_id, t1.end_date;
  137. }
  138. t1.addColumn('calmar', DOUBLE);
  139. UPDATE t1 SET calmar = iif(drawdown.round(4) == 0, null, iif(trailing_ret_a\drawdown > 99999, null, trailing_ret_a\drawdown));
  140. return t1;
  141. }
  142. /*
  143. * Lower Partial Moment
  144. * NOTE: risk free rate is used as Minimal Accepted Rate (MAR) here
  145. *
  146. */
  147. def cal_LPM(ret, risk_free, trailing_month) {
  148. t = SELECT *, cumcount(entity_id) AS cnt FROM ret WHERE ret > -1 CONTEXT BY entity_id;
  149. if(trailing_month == 'incep') {
  150. lpm = SELECT entity_id, end_date,
  151. iif(cumcount(end_date) > 5, (cumsum (iif(rfr.ret > t.ret, rfr.ret - t.ret, 0)) \ cumcount(end_date)).pow(1\1), null) AS lpm1,
  152. iif(cumcount(end_date) > 5, (cumsum2 (iif(rfr.ret > t.ret, rfr.ret - t.ret, 0)) \ cumcount(end_date)).pow(1\2), null) AS lpm2,
  153. iif(cumcount(end_date) > 5, (cumsum3 (iif(rfr.ret > t.ret, rfr.ret - t.ret, 0)) \ cumcount(end_date)).pow(1\3), null) AS lpm3
  154. FROM t
  155. INNER JOIN risk_free rfr ON t.end_date = rfr.end_date
  156. CONTEXT BY entity_id
  157. ORDER BY entity_id, end_date;
  158. } else if(trailing_month == 'ytd') {
  159. lpm = SELECT entity_id, end_date,
  160. iif(cumcount(end_date) > 5, (cumsum (iif(rfr.ret > t.ret, rfr.ret - t.ret, 0)) \ cumcount(end_date)).pow(1\1), null) AS lpm1,
  161. iif(cumcount(end_date) > 5, (cumsum2 (iif(rfr.ret > t.ret, rfr.ret - t.ret, 0)) \ cumcount(end_date)).pow(1\2), null) AS lpm2,
  162. iif(cumcount(end_date) > 5, (cumsum3 (iif(rfr.ret > t.ret, rfr.ret - t.ret, 0)) \ cumcount(end_date)).pow(1\3), null) AS lpm3
  163. FROM t
  164. INNER JOIN risk_free rfr ON t.end_date = rfr.end_date
  165. CONTEXT BY entity_id, end_date.year()
  166. ORDER BY entity_id, end_date;
  167. } else {
  168. win = trailing_month$STRING$INT;
  169. lpm = SELECT t.entity_id, t.end_date,
  170. (msum (iif(rfr.ret > t.ret, rfr.ret - t.ret, 0), win) \ mcount(end_date, win)).pow(1\1) AS lpm1,
  171. (msum2(iif(rfr.ret > t.ret, rfr.ret - t.ret, 0), win) \ mcount(end_date, win)).pow(1\2) AS lpm2,
  172. (moving(sum3, iif(rfr.ret > t.ret, rfr.ret - t.ret, 0), win) \ mcount(end_date, win)).pow(1\3) AS lpm3
  173. FROM t
  174. INNER JOIN risk_free rfr ON t.end_date = rfr.end_date
  175. CONTEXT BY t.entity_id
  176. ORDER BY entity_id, end_date;
  177. }
  178. return lpm;
  179. }
  180. /*
  181. * Downside Devision, Omega Ratio, Sortino Ratio, Kappa Ratio
  182. *
  183. * TODO: Java version of Downside Deviation (LPM2) uses cnt-1 as denominator to calculate mean excess return, which might be wrong
  184. * Java version of Omega could be wrong because Java uses annualized returns and cnt-1
  185. * Java'version of Kappa could be very wrong
  186. *
  187. */
  188. def cal_omega_sortino_kappa(ret, risk_free, trailing_month) {
  189. lpm = cal_LPM(ret, risk_free, trailing_month);
  190. if(trailing_month == 'incep') {
  191. tb = SELECT t.entity_id, t.end_date,
  192. l.lpm2 AS ds_dev,
  193. iif(l.lpm1.round(4) == 0, NULL, (t.ret - rfr.ret ).cumavg() \ l.lpm1 + 1) AS omega,
  194. iif(l.lpm2.round(4) == 0, NULL, (t.ret - rfr.ret ).cumavg() \ l.lpm2) AS sortino,
  195. iif(l.lpm3.round(4) == 0, NULL, (t.ret - rfr.ret ).cumavg() \ l.lpm3) AS kappa
  196. FROM ret t
  197. INNER JOIN lpm l ON t.entity_id = l.entity_id AND t.end_date = l.end_date
  198. INNER JOIN risk_free rfr ON t.end_date = rfr.end_date
  199. WHERE t.ret > -1
  200. CONTEXT BY t.entity_id;
  201. } else if(trailing_month == 'ytd') {
  202. tb = SELECT t.entity_id, t.end_date,
  203. l.lpm2 AS ds_dev,
  204. iif(l.lpm1.round(4) == 0, NULL, (t.ret - rfr.ret ).cumavg() \ l.lpm1 + 1) AS omega,
  205. iif(l.lpm2.round(4) == 0, NULL, (t.ret - rfr.ret ).cumavg() \ l.lpm2) AS sortino,
  206. iif(l.lpm3.round(4) == 0, NULL, (t.ret - rfr.ret ).cumavg() \ l.lpm3) AS kappa
  207. FROM ret t
  208. INNER JOIN lpm l ON t.entity_id = l.entity_id AND t.end_date = l.end_date
  209. INNER JOIN risk_free rfr ON t.end_date = rfr.end_date
  210. WHERE t.ret > -1
  211. CONTEXT BY t.entity_id, t.end_date.year();
  212. } else {
  213. win = trailing_month$STRING$INT;
  214. tb = SELECT t.entity_id, t.end_date,
  215. l.lpm2 AS ds_dev,
  216. iif(l.lpm1.round(4) == 0, NULL, (t.ret - rfr.ret ).mavg(win) \ l.lpm1 + 1) AS omega,
  217. iif(l.lpm2.round(4) == 0, NULL, (t.ret - rfr.ret ).mavg(win) \ l.lpm2) AS sortino,
  218. iif(l.lpm3.round(4) == 0, NULL, (t.ret - rfr.ret ).mavg(win) \ l.lpm3) AS kappa
  219. FROM ret t
  220. INNER JOIN lpm l ON t.entity_id = l.entity_id AND t.end_date = l.end_date
  221. INNER JOIN risk_free rfr ON t.end_date = rfr.end_date
  222. WHERE t.ret > -1
  223. CONTEXT BY t.entity_id;
  224. }
  225. return tb;
  226. }
  227. /*
  228. * Winning Ratio, Tracking Error, Information Ratio
  229. *
  230. * NOTE: mcount is very unique in mFun, because it doesn't support minPeriods(BUG?), while others default minPeriods = window.
  231. * As a result, we have to delete records having winrate but no tracking error and info ratio for the sake of consisence
  232. *
  233. * TODO: Win Rate incept is off, because Java incorrectly takes all end_date as denominator even when benchmark has no price
  234. * Information Ratio is way off!
  235. * Not sure how to describe a giant number("inf"), for now 999 is used
  236. */
  237. def cal_benchmark_tracking(ret, benchmarks, bmk_ret, trailing_month) {
  238. if(trailing_month == 'incep') {
  239. t0 = SELECT t.entity_id, t.end_date, t.price_date,
  240. t.ret, bmk.ret AS ret_bmk,
  241. t.entity_id.cumcount() AS cnt,
  242. t.ret - bmk.ret AS exc_ret, bm.benchmark_id
  243. FROM ret t
  244. INNER JOIN benchmarks bm ON t.entity_id = bm.entity_id AND t.end_date = bm.end_date
  245. INNER JOIN bmk_ret bmk ON t.end_date = bmk.end_date AND bm.benchmark_id = bmk.benchmark_id
  246. WHERE t.ret > -1
  247. AND bmk.ret > -1
  248. CONTEXT BY t.entity_id, bm.benchmark_id;
  249. t = SELECT entity_id, end_date, benchmark_id,
  250. iif(cnt > 5, cumcount(iif(exc_ret >= 0, 1, null)) \ cnt, null) AS winrate,
  251. iif(cnt > 5, exc_ret.cumstd(), null) AS track_error,
  252. iif(cnt > 5, iif(exc_ret.cumstd() == 0, null, exc_ret.cumavg() \ exc_ret.cumstd()), 5) AS info
  253. FROM t0
  254. CONTEXT BY entity_id, benchmark_id
  255. ORDER BY entity_id, end_date, benchmark_id;
  256. } else if(trailing_month == 'ytd') {
  257. t0 = SELECT t.entity_id, t.end_date, t.price_date,
  258. t.ret, bmk.ret AS ret_bmk,
  259. t.entity_id.cumcount() AS cnt, t.ret - bmk.ret AS exc_ret, bm.benchmark_id
  260. FROM ret t
  261. INNER JOIN benchmarks bm ON t.entity_id = bm.entity_id AND t.end_date = bm.end_date
  262. INNER JOIN bmk_ret bmk ON t.end_date = bmk.end_date AND bm.benchmark_id = bmk.benchmark_id
  263. WHERE t.ret > -1
  264. AND bmk.ret > -1
  265. CONTEXT BY t.entity_id, bm.benchmark_id, t.end_date.year();
  266. t = SELECT entity_id, end_date, benchmark_id,
  267. iif(cnt > 5, cumcount(iif(exc_ret >= 0, 1, null)) \ cnt, null) AS winrate,
  268. iif(cnt > 5, exc_ret.cumstd(), null) AS track_error,
  269. iif(cnt > 5, iif(exc_ret.cumstd() == 0, null, exc_ret.cumavg() \ exc_ret.cumstd()), null) AS info
  270. FROM t0
  271. CONTEXT BY entity_id, benchmark_id, end_date.year()
  272. ORDER BY entity_id, end_date, benchmark_id;
  273. } else {
  274. win = trailing_month$STRING$INT;
  275. t0 = SELECT t.entity_id, t.end_date, t.price_date,
  276. t.ret, bmk.ret AS ret_bmk,
  277. t.entity_id.mcount(win) AS cnt,
  278. t.ret - bmk.ret AS exc_ret, bm.benchmark_id
  279. FROM ret t
  280. INNER JOIN benchmarks bm ON t.entity_id = bm.entity_id AND t.end_date = bm.end_date
  281. INNER JOIN bmk_ret bmk ON t.end_date = bmk.end_date AND bm.benchmark_id = bmk.benchmark_id
  282. WHERE t.ret > -1
  283. AND bmk.ret > -1
  284. CONTEXT BY t.entity_id, bm.benchmark_id;
  285. t = SELECT entity_id, end_date, benchmark_id,
  286. iif(cnt > 5, mcount(iif(exc_ret >= 0, 1, null), win) \ cnt, null) AS winrate,
  287. iif(cnt > 5, mstd(exc_ret, win), null) AS track_error,
  288. iif(cnt > 5, iif(mstd(exc_ret, win) == 0, null, mavg(exc_ret, win) \ mstd(exc_ret, win)), null) AS info
  289. FROM t0
  290. CONTEXT BY entity_id, benchmark_id
  291. ORDER BY entity_id, end_date, benchmark_id;
  292. }
  293. return t; //SELECT * FROM t WHERE track_error IS NOT NULL;
  294. }
  295. /*
  296. * Alpha & Beta
  297. * NOTE: alpha of Java version is wrong because it doesn't use risk free rate
  298. */
  299. def cal_alpha_beta(ret, benchmarks, bmk_ret, risk_free, trailing_month) {
  300. t = SELECT t.entity_id, t.end_date, t.ret, bm.benchmark_id, bmk.ret AS ret_bmk
  301. FROM ret t
  302. INNER JOIN benchmarks bm ON t.entity_id = bm.entity_id AND t.end_date = bm.end_date
  303. INNER JOIN bmk_ret bmk ON t.end_date = bmk.end_date AND bm.benchmark_id = bmk.benchmark_id
  304. WHERE t.ret > -1
  305. AND bmk.ret > -1;
  306. if(trailing_month == 'incep') {
  307. beta = SELECT entity_id, end_date, benchmark_id,
  308. iif(cumcount(end_date) > 5, ret.cumbeta(ret_bmk), null) AS beta
  309. FROM t CONTEXT BY entity_id, benchmark_id;
  310. alpha = SELECT t.entity_id, t.end_date, t.benchmark_id, beta.beta AS beta,
  311. (t.ret - rfr.ret).cumavg() - beta.beta * (t.ret_bmk - rfr.ret).cumavg() AS alpha
  312. FROM t
  313. INNER JOIN beta beta ON t.entity_id = beta.entity_id AND t.benchmark_id = beta.benchmark_id AND t.end_date = beta.end_date
  314. INNER JOIN risk_free rfr ON t.end_date = rfr.end_date
  315. CONTEXT BY t.entity_id, t.benchmark_id
  316. ORDER BY t.entity_id, t.end_date, t.benchmark_id;
  317. } else if(trailing_month == 'ytd') {
  318. beta = SELECT entity_id, end_date, benchmark_id,
  319. iif(cumcount(end_date) > 5, ret.cumbeta(ret_bmk), null) AS beta
  320. FROM t CONTEXT BY entity_id, benchmark_id, end_date.year();
  321. alpha = SELECT t.entity_id, t.end_date, t.benchmark_id, beta.beta AS beta,
  322. (t.ret - rfr.ret).cumavg() - beta.beta * (t.ret_bmk - rfr.ret).cumavg() AS alpha
  323. FROM t
  324. INNER JOIN beta beta ON t.entity_id = beta.entity_id AND t.benchmark_id = beta.benchmark_id AND t.end_date = beta.end_date
  325. INNER JOIN risk_free rfr ON t.end_date = rfr.end_date
  326. CONTEXT BY t.entity_id, t.benchmark_id, t.end_date.year()
  327. ORDER BY t.entity_id, t.end_date, t.benchmark_id;
  328. } else {
  329. win = trailing_month$STRING$INT;
  330. beta = SELECT entity_id, end_date, benchmark_id,
  331. iif(mcount(end_date, win) > 5, ret.mbeta(ret_bmk, win), null) AS beta
  332. FROM t CONTEXT BY entity_id, benchmark_id;
  333. alpha = SELECT t.entity_id, t.end_date, t.benchmark_id, beta.beta AS beta,
  334. (t.ret - rfr.ret).mavg(win) - beta.beta * (t.ret_bmk - rfr.ret).mavg(win) AS alpha
  335. FROM t
  336. INNER JOIN beta beta ON t.entity_id = beta.entity_id AND t.benchmark_id = beta.benchmark_id AND t.end_date = beta.end_date
  337. INNER JOIN risk_free rfr ON t.end_date = rfr.end_date
  338. CONTEXT BY t.entity_id, t.benchmark_id
  339. ORDER BY t.entity_id, t.end_date, t.benchmark_id;
  340. }
  341. return alpha;
  342. }
  343. /*
  344. * Upside/Down Capture Return/Ratio
  345. *
  346. * TODO: trailing x month values are way off!
  347. *
  348. */
  349. def cal_capture_ratio(ret, benchmarks, bmk_ret, trailing_month) {
  350. if(trailing_month == 'incep') {
  351. t1 = SELECT t.entity_id, t.end_date,
  352. (1 + iif(bmk.ret >= 0, t.ret, 0)).cumprod() AS upside_ret,
  353. (1 + iif(bmk.ret >= 0, bmk.ret, 0)).cumprod() AS bmk_upside_ret,
  354. cumcount(iif(bmk.ret >= 0, 1, null)) AS bmk_upside_cnt,
  355. (1 + iif(bmk.ret < 0, t.ret, 0)).cumprod() AS downside_ret,
  356. (1 + iif(bmk.ret < 0, bmk.ret, 0)).cumprod() AS bmk_downside_ret,
  357. cumcount(iif(bmk.ret < 0, 1, null)) AS bmk_downside_cnt,
  358. bm.benchmark_id
  359. FROM ret t
  360. INNER JOIN benchmarks bm ON t.entity_id = bm.entity_id AND t.end_date = bm.end_date
  361. INNER JOIN bmk_ret bmk ON t.end_date = bmk.end_date AND bm.benchmark_id = bmk.benchmark_id AND t.end_date = bmk.end_date
  362. WHERE t.ret > -1
  363. AND bmk.ret > -1
  364. CONTEXT BY t.entity_id, bm.benchmark_id;
  365. } else if(trailing_month == 'ytd') {
  366. t1 = SELECT t.entity_id, t.end_date,
  367. (1 + iif(bmk.ret >= 0, t.ret, 0)).cumprod() AS upside_ret,
  368. (1 + iif(bmk.ret >= 0, bmk.ret, 0)).cumprod() AS bmk_upside_ret,
  369. cumcount(iif(bmk.ret >= 0, 1, null)) AS bmk_upside_cnt,
  370. (1 + iif(bmk.ret < 0, t.ret, 0)).cumprod() AS downside_ret,
  371. (1 + iif(bmk.ret < 0, bmk.ret, 0)).cumprod() AS bmk_downside_ret,
  372. cumcount(iif(bmk.ret < 0, 1, null)) AS bmk_downside_cnt,
  373. bm.benchmark_id
  374. FROM ret t
  375. INNER JOIN benchmarks bm ON t.entity_id = bm.entity_id AND t.end_date = bm.end_date
  376. INNER JOIN bmk_ret bmk ON t.end_date = bmk.end_date AND bm.benchmark_id = bmk.benchmark_id AND t.end_date = bmk.end_date
  377. WHERE t.ret > -1
  378. AND bmk.ret > -1
  379. CONTEXT BY t.entity_id, bm.benchmark_id, t.end_date.year();
  380. } else {
  381. win = trailing_month$STRING$INT;
  382. t1 = SELECT t.entity_id, t.end_date,
  383. (1 + iif(bmk.ret >= 0, t.ret, 0)).mprod(win) AS upside_ret,
  384. (1 + iif(bmk.ret >= 0, bmk.ret, 0)).mprod(win) AS bmk_upside_ret,
  385. mcount(iif(bmk.ret >= 0, 1, null), win) AS bmk_upside_cnt,
  386. (1 + iif(bmk.ret < 0, t.ret, 0)).mprod(win) AS downside_ret,
  387. (1 + iif(bmk.ret < 0, bmk.ret, 0)).mprod(win) AS bmk_downside_ret,
  388. mcount(iif(bmk.ret < 0, 1, null), win) AS bmk_downside_cnt,
  389. bm.benchmark_id
  390. FROM ret t
  391. INNER JOIN benchmarks bm ON t.entity_id = bm.entity_id AND t.end_date = bm.end_date
  392. INNER JOIN bmk_ret bmk ON t.end_date = bmk.end_date AND bm.benchmark_id = bmk.benchmark_id AND t.end_date = bmk.end_date
  393. WHERE t.ret > -1
  394. AND bmk.ret > -1
  395. CONTEXT BY t.entity_id, bm.benchmark_id;
  396. }
  397. t = SELECT entity_id, end_date, benchmark_id,
  398. iif(t1.bmk_upside_cnt == 0, NULL, t1.upside_ret.pow(1 \ t1.bmk_upside_cnt)-1) AS upside_capture_ret,
  399. iif(t1.bmk_upside_cnt == 0, NULL, (t1.upside_ret.pow(1 \ t1.bmk_upside_cnt)-1)/(t1.bmk_upside_ret.pow(1 \ t1.bmk_upside_cnt)-1)) AS upside_capture_ratio,
  400. iif(t1.bmk_downside_cnt == 0, NULL, t1.downside_ret.pow(1 \ t1.bmk_downside_cnt)-1) AS downside_capture_ret,
  401. iif(t1.bmk_downside_cnt == 0, NULL, (t1.downside_ret.pow(1 \ t1.bmk_downside_cnt)-1)/(t1.bmk_downside_ret.pow(1 \ t1.bmk_downside_cnt)-1)) AS downside_capture_ratio
  402. FROM t1
  403. ORDER BY entity_id, benchmark_id, end_date;
  404. return t;
  405. }
  406. /*
  407. * Sharpe Ratio
  408. * NOTE: Java version is noncompliant-GIPS annulized number
  409. */
  410. def cal_sharpe(ret, std_dev, risk_free, trailing_month) {
  411. if(trailing_month == 'incep') {
  412. sharpe = SELECT t.entity_id, t.end_date, iif(std.std_dev.round(4) == 0, null, (t.ret - rfr.ret).cumavg() \ std.std_dev) AS sharpe
  413. FROM ret t
  414. INNER JOIN std_dev std ON t.entity_id = std.entity_id AND t.end_date = std.end_date
  415. INNER JOIN risk_free rfr ON t.end_date = rfr.end_date
  416. WHERE std.std_dev.round(4) <> 0 AND t.ret > -1
  417. CONTEXT BY t.entity_id;
  418. } else if(trailing_month == 'ytd') {
  419. sharpe = SELECT t.entity_id, t.end_date, iif(std.std_dev.round(4) ==0, null, (t.ret - rfr.ret).cumavg() \ std.std_dev) AS sharpe
  420. FROM ret t
  421. INNER JOIN std_dev std ON t.entity_id = std.entity_id AND t.end_date = std.end_date
  422. INNER JOIN risk_free rfr ON t.end_date = rfr.end_date
  423. WHERE std.std_dev.round(4) <> 0 AND t.ret > -1
  424. CONTEXT BY t.entity_id, t.end_date.year();
  425. } else {
  426. win = trailing_month$STRING$INT;
  427. sharpe = SELECT t.entity_id, t.end_date, iif(std.std_dev.round(4) == 0, null, (t.ret - rfr.ret).mavg(win) \ std.std_dev) AS sharpe
  428. FROM ret t
  429. INNER JOIN std_dev std ON t.entity_id = std.entity_id AND t.end_date = std.end_date
  430. INNER JOIN risk_free rfr ON t.end_date = rfr.end_date
  431. WHERE std.std_dev.round(4) <> 0 AND t.ret > -1
  432. CONTEXT BY t.entity_id;
  433. }
  434. return sharpe;
  435. }
  436. /*
  437. * Treynor Ratio = annulized excess return / beta
  438. *
  439. * TODO: ytd is off because Java uses non-GIPS rule to annulize return
  440. */
  441. def cal_treynor(ret, risk_free, beta, trailing_month) {
  442. if(trailing_month == 'incep') {
  443. t = SELECT *, cumcount(entity_id) AS cnt
  444. FROM ret t
  445. INNER JOIN risk_free rfr ON t.end_date = rfr.end_date
  446. WHERE t.ret > -1
  447. AND rfr.ret > -1
  448. CONTEXT BY t.entity_id;
  449. treynor = SELECT t.entity_id, t.end_date, beta.benchmark_id,
  450. iif(beta.beta.round(4) == 0, NULL,
  451. ((1 + t.ret).cumprod().pow(12\iif(t.cnt<12, 12, t.cnt)) - (1 + t.rfr_ret).cumprod().pow(12\iif(t.cnt<12, 12, t.cnt))) \ beta.beta) AS treynor
  452. FROM t
  453. INNER JOIN beta AS beta ON t.entity_id = beta.entity_id AND t.end_date = beta.end_date
  454. CONTEXT BY t.entity_id, beta.benchmark_id;
  455. } else if(trailing_month == 'ytd') {
  456. t = SELECT *, cumcount(entity_id) AS cnt
  457. FROM ret t
  458. INNER JOIN risk_free rfr ON t.end_date = rfr.end_date
  459. WHERE t.ret > -1
  460. AND rfr.ret > -1
  461. CONTEXT BY t.entity_id, t.end_date.year();
  462. treynor = SELECT t.entity_id, t.end_date, beta.benchmark_id,
  463. iif(beta.beta.round(4) == 0, NULL,
  464. ((1 + t.ret).cumprod().pow(12\iif(t.cnt<12, 12, t.cnt)) - (1 + t.rfr_ret).cumprod().pow(12\iif(t.cnt<12, 12, t.cnt))) \ beta.beta) AS treynor
  465. FROM t
  466. INNER JOIN beta AS beta ON t.entity_id = beta.entity_id AND t.end_date = beta.end_date
  467. CONTEXT BY t.entity_id, beta.benchmark_id, t.end_date.year();
  468. } else {
  469. win = trailing_month$STRING$INT;
  470. t = SELECT *, mcount(entity_id, win) AS cnt
  471. FROM ret t
  472. INNER JOIN risk_free rfr ON t.end_date = rfr.end_date
  473. WHERE t.ret > -1
  474. AND rfr.ret > -1
  475. CONTEXT BY t.entity_id;
  476. treynor = SELECT t.entity_id, t.end_date, beta.benchmark_id,
  477. iif(beta.beta.round(4) == 0, NULL,
  478. ((1 + t.ret).mprod(win).pow(12\iif(t.cnt<12, 12, t.cnt)) - (1 + t.rfr_ret).mprod(win).pow(12\iif(t.cnt<12, 12, t.cnt))) \ beta.beta) AS treynor
  479. FROM t
  480. INNER JOIN beta AS beta ON t.entity_id = beta.entity_id AND t.end_date = beta.end_date
  481. CONTEXT BY t.entity_id, beta.benchmark_id;
  482. }
  483. return treynor;
  484. }
  485. /*
  486. * Jensen's Alpha
  487. * TODO: the result is slightly off
  488. */
  489. def cal_jensen(ret, bmk_ret, risk_free, beta, trailing_month) {
  490. if(trailing_month == 'incep') {
  491. jensen = SELECT t.entity_id, t.end_date, t.ret.cumavg() - rfr.ret.cumavg() - beta.beta * (bmk.ret.cumavg() - rfr.ret.cumavg()) AS jensen, beta.benchmark_id
  492. FROM ret t
  493. INNER JOIN beta beta ON t.entity_id = beta.entity_id AND t.end_date = beta.end_date
  494. INNER JOIN bmk_ret bmk ON t.end_date = bmk.end_date AND beta.benchmark_id = bmk.benchmark_id
  495. INNER JOIN risk_free rfr ON t.end_date = rfr.end_date
  496. WHERE t.ret > -1
  497. CONTEXT BY t.entity_id, beta.benchmark_id;
  498. } else if(trailing_month == 'ytd') {
  499. jensen = SELECT t.entity_id, t.end_date, t.ret.cumavg() - rfr.ret.cumavg() - beta.beta * (bmk.ret.cumavg() - rfr.ret.cumavg()) AS jensen, beta.benchmark_id
  500. FROM ret t
  501. INNER JOIN beta beta ON t.entity_id = beta.entity_id AND t.end_date = beta.end_date
  502. INNER JOIN bmk_ret bmk ON t.end_date = bmk.end_date AND beta.benchmark_id = bmk.benchmark_id
  503. INNER JOIN risk_free rfr ON t.end_date = rfr.end_date
  504. WHERE t.ret > -1
  505. CONTEXT BY t.entity_id, beta.benchmark_id, t.end_date.year();
  506. } else {
  507. win = trailing_month$STRING$INT;
  508. jensen = SELECT t.entity_id, t.end_date, t.ret.mavg(win) - rfr.ret.mavg(win) - beta.beta * (bmk.ret.mavg(win) - rfr.ret.mavg(win)) AS jensen, beta.benchmark_id
  509. FROM ret t
  510. INNER JOIN beta beta ON t.entity_id = beta.entity_id AND t.end_date = beta.end_date
  511. INNER JOIN bmk_ret bmk ON t.end_date = bmk.end_date AND beta.benchmark_id = bmk.benchmark_id
  512. INNER JOIN risk_free rfr ON t.end_date = rfr.end_date
  513. WHERE t.ret > -1
  514. CONTEXT BY t.entity_id, beta.benchmark_id;
  515. }
  516. return jensen;
  517. }
  518. /*
  519. * Modigliani Modigliani Measure (M2)
  520. * NOTE: M2 = sharpe * std(benchmark) + risk_free_rate
  521. * NOTE: Java version is noncompliant-GIPS annulized number
  522. */
  523. def cal_m2(ret, benchmarks, bmk_ret, risk_free, trailing_month) {
  524. if(trailing_month == 'incep') {
  525. m2 = SELECT t.entity_id, t.end_date,
  526. iif(t.entity_id.cumcount() > 5,
  527. iif(t.ret.cumstd().round(4) == 0, NULL, (t.ret - rfr.ret).cumavg() \ t.ret.cumstd() * bmk.ret.cumstd() + rfr.ret.cumavg()),
  528. NULL) AS m2, bm.benchmark_id
  529. FROM ret t
  530. INNER JOIN benchmarks bm ON t.entity_id = bm.entity_id AND t.end_date = bm.end_date
  531. INNER JOIN bmk_ret bmk ON t.end_date = bmk.end_date AND bm.benchmark_id = bmk.benchmark_id
  532. INNER JOIN risk_free rfr ON t.end_date = rfr.end_date
  533. WHERE t.ret > -1
  534. CONTEXT BY t.entity_id, bm.benchmark_id;
  535. } else if(trailing_month == 'ytd') {
  536. m2 = SELECT t.entity_id, t.end_date,
  537. iif(t.entity_id.cumcount() > 5,
  538. iif(t.ret.cumstd().round(4) == 0, NULL, (t.ret - rfr.ret).cumavg() \ t.ret.cumstd() * bmk.ret.cumstd() + rfr.ret.cumavg()),
  539. NULL) AS m2, bm.benchmark_id
  540. FROM ret t
  541. INNER JOIN benchmarks bm ON t.entity_id = bm.entity_id AND t.end_date = bm.end_date
  542. INNER JOIN bmk_ret bmk ON t.end_date = bmk.end_date AND bm.benchmark_id = bmk.benchmark_id
  543. INNER JOIN risk_free rfr ON t.end_date = rfr.end_date
  544. WHERE t.ret > -1
  545. CONTEXT BY t.entity_id, bm.benchmark_id, t.end_date.year();
  546. } else {
  547. win = trailing_month$STRING$INT;
  548. m2 = SELECT t.entity_id, t.end_date,
  549. iif(t.entity_id.mcount(win) > 5,
  550. iif(t.ret.mstd(win).round(4) == 0, NULL, (t.ret - rfr.ret).mavg(win) \ t.ret.mstd(win) * bmk.ret.mstd(win) + rfr.ret.mavg(win)),
  551. NULL) AS m2, bm.benchmark_id
  552. FROM ret t
  553. INNER JOIN benchmarks bm ON t.entity_id = bm.entity_id AND t.end_date = bm.end_date
  554. INNER JOIN bmk_ret bmk ON t.end_date = bmk.end_date AND bm.benchmark_id = bmk.benchmark_id
  555. INNER JOIN risk_free rfr ON t.end_date = rfr.end_date
  556. WHERE t.ret > -1
  557. CONTEXT BY t.entity_id, bm.benchmark_id;
  558. }
  559. return m2;
  560. }
  561. /*
  562. * Morningstar Return, Morningstar Risk-Adjusted Return
  563. *
  564. * TODO: Tax and loads are NOT taken care of
  565. * TODO: Assume Chinese methodology using 3, 5, 10 as number of traling years
  566. * TODO: need verify with reliable results
  567. *
  568. * NOTE: Morningstar methodology requires monthly return for calculation, so that "12" is hard-coded here
  569. *
  570. *
  571. */
  572. def cal_ms_return(ret, risk_free, trailing_month) {
  573. win = trailing_month$STRING$INT;
  574. r = SELECT t.entity_id, t.end_date,
  575. iif(t.end_date.mmax(win) == t.end_date.mmin(win), NULL,
  576. ((1 + t.ret)\(1 + rfr.ret)).mprod(win).pow(12\(1+t.end_date.mmax(win) - t.end_date.mmin(win)))-1) AS ms_ret_a,
  577. ((1 + t.ret)\(1 + rfr.ret)).pow(-2).mavg(win).pow(-12/2)-1 AS ms_rar_a
  578. FROM ret t
  579. INNER JOIN risk_free rfr ON t.end_date = rfr.end_date
  580. WHERE t.ret > -1
  581. CONTEXT BY t.entity_id;
  582. r.addColumn('ms_risk_a', DOUBLE);
  583. UPDATE r SET ms_risk_a = ms_ret_a - ms_rar_a;
  584. return r;
  585. }
  586. /*
  587. * 有效主体-基准对应表
  588. *
  589. * @param benchmarks <TABLE>: entity-benchmark 的对应关系表 NEED COLUMNS: entity_id, end_date, benchmark_id
  590. * @param end_day <DATE>:
  591. * @param trailing_month <STRING>:
  592. * @param isEffectiveOnly <BOOL>: false时与Java相同; true:多了个限制条件:如果区间内有效基准数少于1/2,不做计算
  593. * 比如过去12个月中某BFI只出现2次,小于需要的6次,此BFI不参与 trailing 1 year 计算
  594. *
  595. */
  596. def get_effective_benchmarks(benchmarks, end_day, trailing_month, isEffectiveOnly) {
  597. min_pct = 0.5;
  598. if(isEffectiveOnly) {
  599. t_dates = SELECT DISTINCT entity_id, end_date FROM benchmarks WHERE end_date <= end_day.month();
  600. if(trailing_month == 'incep') {
  601. t = SELECT entity_id, end_date, end_date.cumcount() AS cnt FROM t_dates CONTEXT BY entity_id;
  602. bmk = SELECT bmk.* FROM benchmarks bmk
  603. INNER JOIN t ON bmk.entity_id = t.entity_id AND bmk.end_date = t.end_date
  604. CONTEXT BY bmk.entity_id, bmk.benchmark_id
  605. HAVING bmk.end_date.cumcount() >= t.cnt * min_pct;
  606. } else if(trailing_month == 'ytd') {
  607. t = SELECT entity_id, end_date, end_date.cumcount() AS cnt FROM t_dates CONTEXT BY entity_id, end_date.year();
  608. bmk = SELECT bmk.* FROM benchmarks bmk
  609. INNER JOIN t ON bmk.entity_id = t.entity_id AND bmk.end_date = t.end_date
  610. CONTEXT BY entity_id, benchmark_id, end_date.year()
  611. HAVING bmk.end_date.cumcount() >= t.cnt * min_pct;
  612. } else {
  613. win = trailing_month$STRING$INT;
  614. t = SELECT entity_id, end_date, end_date.mcount(win) AS cnt FROM t_dates CONTEXT BY entity_id;
  615. bmk = SELECT bmk.* FROM benchmarks bmk
  616. INNER JOIN t ON bmk.entity_id = t.entity_id AND bmk.end_date = t.end_date
  617. CONTEXT BY entity_id, benchmark_id
  618. HAVING bmk.end_date.mcount(win) >= t.cnt * min_pct;
  619. }
  620. } else {
  621. bmk = SELECT * FROM benchmarks WHERE end_date <= end_day.month();
  622. }
  623. return bmk;
  624. }
  625. /*
  626. * Calculation for monthly indicators which need benchmark
  627. *
  628. * @param entity_info <TABLE>: xxx_information表,NEED COLUMNS entity_id, inception_date
  629. * @param benchmark_mapping <TABLE>: entity-benchmark mapping table, NEED COLUMNS entity_id, end_date, benchmark_id
  630. * @param end_day <DATE>;
  631. * @param tb_ret <TABLE>: 收益表,NEED COLUMNS entity_id, price_dat, end_date, nav
  632. * @param index_ret <TABLE>: historical benchmark return table, NEED COLUMNS fund_id, end_date, ret
  633. * @param risk_free <TABLE>: historical risk free rate table, NEED COLUMNS fund_id, end_date, ret
  634. * @param month <INT>: trailing x month
  635. *
  636. * @return: indicators table
  637. *
  638. *
  639. * Create 20240904 模仿Java & python代码在Dolphin中实现,具体计算逻辑可能会有不同 Joey
  640. * TODO: some datapoints require more data, we need a way to disable calculation for them
  641. *
  642. */
  643. def cal_indicators_with_benchmark(entity_info, benchmark_mapping, end_day, tb_ret, index_ret, risk_free, month) {
  644. r = table(1000:0,
  645. ['entity_id', 'end_date', 'benchmark_id', 'winrate', 'track_error', 'info', 'beta', 'alpha', 'treynor', 'jensen', 'm2',
  646. 'upside_capture_ret', 'upside_capture_ratio', 'downside_capture_ret', 'downside_capture_ratio',
  647. 'alpha_a', 'jensen_a', 'track_error_a', 'info_a', 'm2_a'],
  648. [entity_info.entity_id[0].type(), MONTH, SYMBOL, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE,
  649. DOUBLE, DOUBLE, DOUBLE, DOUBLE,
  650. DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE]);
  651. if(entity_info.isVoid() || entity_info.size() == 0 || benchmark_mapping.isVoid() || benchmark_mapping.size() == 0 ) return r;
  652. if(tb_ret.isVoid() || tb_ret.size() == 0 || index_ret.isVoid() || index_ret.size() == 0 || risk_free.isVoid() || risk_free.size() == 0 ) return r;
  653. // sorting for correct first() and last() value
  654. ret = SELECT * FROM tb_ret WHERE ret > -1 AND end_date <= end_day.month() ORDER BY entity_id, price_date;
  655. // get the effective benchmarks
  656. benchmarks = get_effective_benchmarks(benchmark_mapping, end_day, month, true);
  657. if(ret.isVoid() || ret.size() == 0 || benchmarks.isVoid() || benchmarks.size() == 0) return r;
  658. // alpha, beta
  659. alpha_beta = cal_alpha_beta(ret, benchmarks, index_ret, risk_free, month);
  660. // 胜率、跟踪误差、信息比率
  661. bmk_tracking = cal_benchmark_tracking(ret, benchmarks, index_ret, month);
  662. // 特雷诺
  663. treynor = cal_treynor(ret, risk_free, alpha_beta, month);
  664. // 詹森指数
  665. jensen = cal_jensen(ret, index_ret, risk_free, alpha_beta, month);
  666. // M2
  667. m2 = cal_m2(ret, benchmarks, index_ret, risk_free, month);
  668. // 上下行捕获率、收益
  669. capture_r = cal_capture_ratio(ret, benchmarks, index_ret, month);
  670. r = SELECT * FROM bmk_tracking a1
  671. LEFT JOIN alpha_beta ON a1.entity_id = alpha_beta.entity_id AND a1.benchmark_id = alpha_beta.benchmark_id AND a1.end_date = alpha_beta.end_date
  672. LEFT JOIN treynor ON a1.entity_id = treynor.entity_id AND a1.benchmark_id = treynor.benchmark_id AND a1.end_date = treynor.end_date
  673. LEFT JOIN jensen ON a1.entity_id = jensen.entity_id AND a1.benchmark_id = jensen.benchmark_id AND a1.end_date = jensen.end_date
  674. LEFT JOIN m2 ON a1.entity_id = m2.entity_id AND a1.benchmark_id = m2.benchmark_id AND a1.end_date = m2.end_date
  675. LEFT JOIN capture_r ON a1.entity_id = capture_r.entity_id AND a1.benchmark_id = capture_r.benchmark_id AND a1.end_date = capture_r.end_date;
  676. // 年化各数据点
  677. // GIPS RULE: NO annulization for data less than 1 year
  678. plainAnnu = get_annulization_multiple('m');
  679. sqrtAnnu = sqrt(get_annulization_multiple('m'));
  680. r.addColumn(['alpha_a', 'jensen_a', 'track_error_a', 'info_a', 'm2_a'],
  681. [DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE]);
  682. UPDATE r
  683. SET alpha_a = alpha * iif(end_date - inception_date.month() > 12, plainAnnu, 1),
  684. jensen_a = jensen * iif(end_date - inception_date.month() > 12, plainAnnu, 1),
  685. track_error_a = track_error * iif(end_date - inception_date.month() > 12, sqrtAnnu, 1),
  686. info_a = info * iif(end_date - inception_date.month() > 12, sqrtAnnu, 1),
  687. m2_a = m2 * iif(end_date - inception_date.month() > 12, plainAnnu, 1)
  688. FROM ej(r, entity_info, 'entity_id');
  689. return r;
  690. }
  691. /*
  692. * Monthly standard indicator calculation
  693. *
  694. * @param entity_info <TABLE>:
  695. * @param benchmarks <TABLE>: entity-benchmark mapping table
  696. * @param end_day <DATE>:
  697. * @param tb_ret <TABLE>: 收益表,NEED COLUMNS entity_id, price_dat, end_date, nav
  698. * @param benchmark_ret <TABLE>: historical benchmark return table, NEED COLUMNS fund_id, end_date, ret
  699. * @param risk_free <TABLE>: historical risk free rate table, NEED COLUMNS fund_id, end_date, ret
  700. * @param month <STRING>:
  701. *
  702. * @return: indicators table
  703. *
  704. * TODO: 目前只能使用单一无风险利率,无法根据基金信息自由选择更匹配的指数
  705. *
  706. * Create 20240904 模仿Java & python代码在Dolphin中实现,具体计算逻辑可能会有不同 Joey
  707. *
  708. */
  709. def cal_indicators(entity_info, benchmarks, end_day, tb_ret, benchmark_ret, risk_free, month) {
  710. if(entity_info.isVoid() || entity_info.size() == 0 || benchmarks.isVoid() || benchmarks.size() == 0 ) return null;
  711. if(tb_ret.isVoid() || tb_ret.size() == 0 || benchmark_ret.isVoid() || benchmark_ret.size() == 0 || risk_free.isVoid() || risk_free.size() == 0 ) return null;
  712. // sorting for correct first() and last() value
  713. ret = SELECT * FROM tb_ret WHERE end_date <= end_day.month() ORDER BY entity_id, price_date;
  714. if(ret.isVoid() || ret.size() == 0) return null;
  715. // 有数据缺失时要插入相应的月份
  716. tmp_dates = SELECT entity_id, ret.price_date.min() AS first_date, ret.price_date.max() AS last_date FROM ret GROUP BY entity_id;
  717. all_monthend = temporalSeq(ret.price_date.min(), ret.price_date.max(), 'M');
  718. t_all_month = table(all_monthend.month() AS end_date, all_monthend AS price_date);
  719. t_entity_all_month = SELECT entity_id, end_date, price_date
  720. FROM cj(tmp_dates, t_all_month)
  721. WHERE t_all_month.price_date >= tmp_dates.first_date
  722. AND t_all_month.price_date <= tmp_dates.last_date;
  723. if(t_entity_all_month.size() > 0) {
  724. INSERT INTO ret
  725. SELECT entity_id, end_date, price_date, double(NULL) AS nav, 0 AS ret
  726. FROM t_entity_all_month
  727. WHERE NOT EXISTS ( SELECT * FROM ret r WHERE r.entity_id = t_entity_all_month.entity_id AND r.end_date = t_entity_all_month.end_date);
  728. }
  729. ret.sortBy!(['entity_id', 'price_date']);
  730. ret.nav.bfill!();
  731. // 根据净值计算收益,这里不能依赖数据库老数据
  732. UPDATE ret SET ret = nav.ratios()-1 CONTEXT BY entity_id;
  733. // 收益、标准差、偏度、峰度、最大回撤、VaR, CVaR、卡玛比率
  734. rtn = cal_basic_performance(entity_info, ret, month);
  735. // 夏普
  736. sharpe = cal_sharpe(ret, rtn, risk_free, month);
  737. // 整合后的下行标准差、欧米伽、索提诺、卡帕
  738. lpms = cal_omega_sortino_kappa(ret, risk_free, month);
  739. // 需要基准的指标们
  740. indicator_with_benchmark = cal_indicators_with_benchmark(entity_info, benchmarks, end_day, ret, benchmark_ret, risk_free, month);
  741. r = SELECT * FROM rtn a1
  742. LEFT JOIN sharpe ON a1.entity_id = sharpe.entity_id AND a1.end_date = sharpe.end_date
  743. LEFT JOIN lpms ON a1.entity_id = lpms.entity_id AND a1.end_date = lpms.end_date
  744. LEFT JOIN indicator_with_benchmark bmk ON a1.entity_id = bmk.entity_id AND a1.end_date = bmk.end_date;
  745. // 晨星收益和风险
  746. if(month$STRING in ['36', '60', '120']) {
  747. ms = cal_ms_return(ret, risk_free, month);
  748. r = SELECT * FROM r LEFT JOIN ms ON r.entity_id = ms.entity_id AND r.end_date = ms.end_date;
  749. }
  750. // 年化各数据点
  751. // GIPS RULE: NO annulization for data less than 1 year
  752. plainAnnu = get_annulization_multiple('m');
  753. sqrtAnnu = sqrt(get_annulization_multiple('m'));
  754. r.addColumn(['std_dev_a', 'ds_dev_a', 'sharpe_a', 'sortino_a'],
  755. [DOUBLE, DOUBLE, DOUBLE, DOUBLE]);
  756. UPDATE r
  757. SET std_dev_a = std_dev * iif(end_date - inception_date.month() > 12, sqrtAnnu, 1),
  758. ds_dev_a = ds_dev * iif(end_date - inception_date.month() > 12, sqrtAnnu, 1),
  759. sharpe_a = sharpe * iif(end_date - inception_date.month() > 12, sqrtAnnu, 1),
  760. sortino_a = sortino * iif(end_date - inception_date.month() > 12, sqrtAnnu, 1)
  761. FROM ej(r, entity_info, 'entity_id');
  762. return r;
  763. }
  764. /*
  765. * Calculate trailing 3m, 6m, ytd, 1y, 2y, 3y, 4y, 5y, 10y and since inception datapoints
  766. *
  767. * @param: func <FUNCTION>: the calculation function
  768. * @param: entity_info <TABLE>: basic information of entity, NEED COLUMNS entity_id, inception_date
  769. * @param benchmarks <TABLE>: entity-benchmark mapping table
  770. * @param: end_day <DATE>: 计算截止日期
  771. * @param: ret <TABLE>: 收益表,NEED COLUMNS entity_id, price_dat, end_date, nav
  772. * @param bmk_ret <TABLE>: historical benchmark return table, NEED COLUMNS fund_id, end_date, ret
  773. * @param risk_free <TABLE>: historical risk free rate table, NEED COLUMNS fund_id, end_date, ret
  774. *
  775. *
  776. */
  777. def cal_trailing(func, entity_info, benchmarks, end_day, tb_ret, bmk_ret, risk_free_rate ) {
  778. r_incep = null;
  779. r_ytd = null;
  780. r_3m = null;
  781. r_6m = null;
  782. r_1y = null;
  783. r_2y = null;
  784. r_3y = null;
  785. r_4y = null;
  786. r_5y = null;
  787. r_10y = null;
  788. // incep
  789. r_incep = func(entity_info, benchmarks, end_day, tb_ret, bmk_ret, risk_free_rate, 'incep');
  790. // ytd
  791. r_ytd = func(entity_info, benchmarks, end_day, tb_ret, bmk_ret, risk_free_rate, 'ytd');
  792. // 3m 只需要支持收益计算
  793. r_3m = cal_basic_performance(entity_info, tb_ret, '3');
  794. // 6m
  795. r_6m = func(entity_info, benchmarks, end_day, tb_ret, bmk_ret, risk_free_rate, '6');
  796. // 1y
  797. r_1y = func(entity_info, benchmarks, end_day, tb_ret, bmk_ret, risk_free_rate, '12');
  798. // 2y
  799. r_2y = func(entity_info, benchmarks, end_day, tb_ret, bmk_ret, risk_free_rate, '24');
  800. // 3y
  801. r_3y = func(entity_info, benchmarks, end_day, tb_ret, bmk_ret, risk_free_rate, '36');
  802. // 4y
  803. r_4y = func(entity_info, benchmarks, end_day, tb_ret, bmk_ret, risk_free_rate, '48');
  804. // 5y
  805. r_5y = func(entity_info, benchmarks, end_day, tb_ret, bmk_ret, risk_free_rate, '60');
  806. // 10y
  807. r_10y = func(entity_info, benchmarks, end_day, tb_ret, bmk_ret, risk_free_rate, '120');
  808. return r_incep, r_ytd, r_3m, r_6m, r_1y, r_2y, r_3y, r_4y, r_5y, r_10y;
  809. }
  810. /*
  811. * Calculate trailing ytd, 3m, 6m, 1y, 2y, 3y, 4y, 5y, 10y and since inception standard indicators
  812. *
  813. * @param: entity_info <TABLE>: basic information of entity, NEED COLUMNS entity_id, inception_date
  814. * @param benchmarks <TABLE>: entity-benchmark mapping table
  815. * @param: ret <TABLE>: 收益表,NEED COLUMNS entity_id, price_dat, end_date, nav
  816. * @param: end_day <DATE>: 计算截止日期
  817. * @param bmk_ret <TABLE>: historical benchmark return table, NEED COLUMNS fund_id, end_date, ret
  818. * @param risk_free <TABLE>: historical risk free rate table, NEED COLUMNS fund_id, end_date, ret
  819. *
  820. */
  821. def cal_trailing_indicators(entity_info, benchmarks, end_day, tb_ret, bmk_ret, risk_free_rate) {
  822. return cal_trailing(cal_indicators, entity_info, benchmarks, end_day, tb_ret, bmk_ret, risk_free_rate);
  823. }
  824. /*
  825. * Calculate trailing ytd, 3m, 6m, 1y, 2y, 3y, 4y, 5y, 10y and since inception bfi indicators
  826. *
  827. * @param: entity_info <TABLE>: basic information of entity, NEED COLUMNS entity_id, inception_date
  828. * @param benchmarks <TABLE>: entity-benchmark mapping table
  829. * @param: ret <TABLE>: 收益表,NEED COLUMNS entity_id, price_dat, end_date, nav
  830. * @param: end_day <DATE>: 计算截止日期
  831. * @param bmk_ret <TABLE>: historical benchmark return table, NEED COLUMNS fund_id, end_date, ret
  832. * @param risk_free <TABLE>: historical risk free rate table, NEED COLUMNS fund_id, end_date, ret
  833. *
  834. * NOTE: 3m 的所有指标没有意义
  835. *
  836. *
  837. */
  838. def cal_trailing_bfi_indicators(entity_info, benchmarks, end_day, tb_ret, bmk_ret, risk_free_rate) {
  839. return cal_trailing(cal_indicators_with_benchmark, entity_info, benchmarks, end_day, tb_ret, bmk_ret, risk_free_rate);
  840. }
  841. /*
  842. * 通用月度指标计算
  843. *
  844. * @param entity_type <STRING>:
  845. * @param indicator_type <STRING>: PBI, BFI
  846. * @param monthly_returns <TABLE>: NEED COLUMN: entity_id, end_date, price_date, nav, ret
  847. *
  848. * @return <DICT TABLE>: ['PBI-INCEP', 'PBI-YTD', 'PBI-3M', 'PBI-6M', 'PBI-1Y', 'PBI-2Y', 'PBI-3Y', 'PBI-4Y', 'PBI-5Y', 'PBI-10Y']
  849. *
  850. */
  851. def cal_monthly_indicators(entity_type, indicator_type, monthly_returns) {
  852. if(find(['MF', 'HF', 'PF', 'MI', 'FI', 'FA'], entity_type) < 0) return null;
  853. if(monthly_returns.isVoid() || monthly_returns.size() < 1) return null;
  854. oldest_date = EXEC price_date.min() FROM monthly_returns;
  855. v_entity_ids = EXEC DISTINCT entity_id FROM monthly_returns;
  856. entity_info = get_entity_info(entity_type, v_entity_ids);
  857. if(entity_info.isVoid() || entity_info.size() == 0) { return null };
  858. end_day = today();
  859. // 取基金和基准的对照表
  860. if(indicator_type == 'BFI') {
  861. benchmark = SELECT entity_id, end_date.temporalParse('yyyy-MM') AS end_date, factor_id AS benchmark_id
  862. FROM get_entity_bfi_factors(entity_type, v_entity_ids, oldest_date.month(), end_day.month());
  863. } else {
  864. // 主基准, 对应 xxx_info 中的 primary_benchmark_id; TODO: 没有基准用沪深300顶,哪怕很多情况下不那么正确
  865. benchmark = SELECT entity_id, end_date, iif(benchmark_id.isVoid(), 'IN00000008', benchmark_id) AS benchmark_id
  866. FROM get_entity_primary_benchmark(entity_type, v_entity_ids, oldest_date.temporalFormat('yyyy-MM'), end_day.temporalFormat('yyyy-MM')) ;
  867. }
  868. // 取所有出现的基准月收益
  869. bmk_ret = get_benchmark_return(benchmark, end_day);
  870. //if(bmk_ret.isVoid() || bmk_ret.size() == 0) { return null; }
  871. // TODO: risk free指数月收益存在fund_performance表,所以先将就用 fund_id 表示。之后统一改为更准确的名字
  872. risk_free_rate = SELECT entity_id AS fund_id, temporalParse(end_date, 'yyyy-MM') AS end_date, ret FROM get_risk_free_rate(oldest_date, end_day);
  873. if(risk_free_rate.isVoid() || risk_free_rate.size() == 0) { return null; }
  874. // 指标计算
  875. if(indicator_type == 'BFI') {
  876. t0 = cal_trailing_bfi_indicators(entity_info, benchmark, end_day, monthly_returns, bmk_ret, risk_free_rate);
  877. v_table_name = ['BFI-INCEP', 'BFI-YTD', 'BFI-3M', 'BFI-6M', 'BFI-1Y', 'BFI-2Y', 'BFI-3Y', 'BFI-4Y', 'BFI-5Y', 'BFI-10Y'];
  878. } else {
  879. t0 = cal_trailing_indicators(entity_info, benchmark, end_day, monthly_returns, bmk_ret, risk_free_rate);
  880. v_table_name = ['PBI-INCEP', 'PBI-YTD', 'PBI-3M', 'PBI-6M', 'PBI-1Y', 'PBI-2Y', 'PBI-3Y', 'PBI-4Y', 'PBI-5Y', 'PBI-10Y'];
  881. }
  882. return dict(v_table_name, t0);
  883. }
  884. /*
  885. * 通用基金经理/公司月度指标计算
  886. *
  887. * @param entity_type <STRING>:
  888. * @param indicator_type <STRING>: PBI, BFI
  889. * @param monthly_returns <TABLE>: NEED COLUMN: entity_id, curve_type, strategy, end_date, price_date, nav, ret
  890. *
  891. * @return <DICT TABLE>: ['PBI-INCEP', 'PBI-YTD', 'PBI-3M', 'PBI-6M', 'PBI-1Y', 'PBI-2Y', 'PBI-3Y', 'PBI-4Y', 'PBI-5Y', 'PBI-10Y']
  892. *
  893. */
  894. def cal_mc_monthly_indicators(entity_type, indicator_type, monthly_returns) {
  895. if(find(['PL', 'CO'], entity_type) < 0) return null;
  896. if(monthly_returns.isVoid() || monthly_returns.size() < 1) return null;
  897. oldest_date = EXEC price_date.min() FROM monthly_returns;
  898. v_entity_ids = EXEC DISTINCT entity_id FROM monthly_returns;
  899. if(entity_type == 'PL') {
  900. entity_info = get_personnel_info_for_perf(v_entity_ids);
  901. entity_info.rename!('manager_id', 'entity_id');
  902. }
  903. else {
  904. entity_info = get_company_info_for_perf(v_entity_ids);
  905. entity_info.rename!('company_id', 'entity_id');
  906. }
  907. if(entity_info.isVoid() || entity_info.size() == 0) { return null };
  908. end_day = today();
  909. // 取基金和基准的对照表
  910. if(indicator_type == 'BFI') {
  911. benchmark = SELECT DISTINCT entity_id, curve_type, strategy, end_date.temporalParse('yyyy-MM') AS end_date, factor_id AS benchmark_id
  912. FROM get_mc_bfi_factors(entity_type, v_entity_ids, oldest_date.month(), end_day.month());
  913. } else {
  914. // 主基准, 对应公募混合基金平均 FA00000VNB
  915. benchmark = SELECT DISTINCT entity_id, curve_type, strategy, end_date, iif(benchmark_id.isVoid(), 'FA00000VNB', benchmark_id) AS benchmark_id
  916. FROM ej(entity_info, monthly_returns, ['entity_id', 'curve_type', 'strategy']) ;
  917. }
  918. // 取所有出现的基准月收益
  919. bmk_ret = get_benchmark_return(benchmark, end_day);
  920. //if(bmk_ret.isVoid() || bmk_ret.size() == 0) { return null; }
  921. // TODO: risk free指数月收益存在fund_performance表,所以先将就用 fund_id 表示。之后统一改为更准确的名字
  922. risk_free_rate = SELECT entity_id AS fund_id, temporalParse(end_date, 'yyyy-MM') AS end_date, ret FROM get_risk_free_rate(oldest_date, end_day);
  923. if(risk_free_rate.isVoid() || risk_free_rate.size() == 0) { return null; }
  924. // 指标计算,因为 cal_traling_xxx 不支持 curve_type & strategy, 所以做个循环
  925. d_rt = dict(STRING, ANY);
  926. v_curve_type = [1, 4, 7];
  927. for(cur in v_curve_type) {
  928. t_ei = SELECT entity_id, inception_date, benchmark_id, ini_value FROM entity_info WHERE curve_type = cur AND strategy = 0;
  929. t_mr = SELECT entity_id, end_date, price_date, ret, nav FROM monthly_returns WHERE curve_type = cur AND strategy = 0;
  930. t_bk = SELECT entity_id, end_date, benchmark_id FROM benchmark WHERE curve_type = cur AND strategy = 0;
  931. if(t_ei.isVoid() || t_ei.size() == 0 || t_mr.isVoid() || t_mr.size() == 0) continue;
  932. if(indicator_type == 'BFI') {
  933. v_indicators = cal_trailing_bfi_indicators(t_ei, t_bk, end_day, t_mr, bmk_ret, risk_free_rate);
  934. for(tb in v_indicators)
  935. UPDATE tb SET curve_type = cur, strategy = 0;
  936. v_table_name = ['BFI-INCEP', 'BFI-YTD', 'BFI-3M', 'BFI-6M', 'BFI-1Y', 'BFI-2Y', 'BFI-3Y', 'BFI-4Y', 'BFI-5Y', 'BFI-10Y'];
  937. } else {
  938. v_indicators = cal_trailing_indicators(t_ei, t_bk, end_day, t_mr, bmk_ret, risk_free_rate);
  939. for(tb in v_indicators)
  940. UPDATE tb SET curve_type = cur, strategy = 0;
  941. v_table_name = ['PBI-INCEP', 'PBI-YTD', 'PBI-3M', 'PBI-6M', 'PBI-1Y', 'PBI-2Y', 'PBI-3Y', 'PBI-4Y', 'PBI-5Y', 'PBI-10Y'];
  942. }
  943. d_indicator = dict(v_table_name, v_indicators);
  944. d_rt[cur$STRING] = d_indicator;
  945. }
  946. return d_rt;
  947. }
  948. /*
  949. * 【Morningstar Integration】通用月度指标计算
  950. *
  951. * @param entity_type <STRING>:
  952. * @param indicator_type <STRING>: PBI, BFI
  953. * @param monthly_returns <TABLE>: NEED COLUMN: entity_id, end_date, price_date, nav, ret
  954. *
  955. * @return <DICT TABLE>: ['PBI-INCEP', 'PBI-YTD', 'PBI-3M', 'PBI-6M', 'PBI-1Y', 'PBI-2Y', 'PBI-3Y', 'PBI-4Y', 'PBI-5Y', 'PBI-10Y']
  956. *
  957. */
  958. def ms_cal_monthly_indicators(entity_type, indicator_type, monthly_returns) {
  959. if(find(['MF', 'HF', 'PF', 'FA'], entity_type) < 0) return null;
  960. if(monthly_returns.isVoid() || monthly_returns.size() < 1) return null;
  961. oldest_date = EXEC price_date.min() FROM monthly_returns;
  962. v_entity_ids = (SELECT DISTINCT entity_id FROM monthly_returns).entity_id;
  963. entity_info = get_entity_info(entity_type, v_entity_ids);
  964. if(entity_info.isVoid() || entity_info.size() == 0) { return null };
  965. end_day = today();
  966. // 取基金和基准的对照表
  967. if(indicator_type == 'BFI') {
  968. benchmark = SELECT entity_id, end_date.temporalParse('yyyy-MM') AS end_date, factor_id AS benchmark_id
  969. FROM get_entity_bfi_factors(entity_type, v_entity_ids, oldest_date.month(), end_day.month());
  970. } else if(indicator_type == 'CAI') {
  971. benchmark = SELECT fund_id AS entity_id, end_date.temporalParse('yyyy-MM') AS end_date, factor_id AS benchmark_id
  972. FROM ms_get_fund_category_average(v_entity_ids, oldest_date.temporalFormat('yyyy-MM'), end_day.temporalFormat('yyyy-MM'));
  973. } else {
  974. // 主基准, 对应 xxx_info 中的 primary_benchmark_id, TODO: 没有基准用沪深300顶,哪怕很多情况下不那么正确
  975. benchmark = SELECT entity_id, end_date, iif(benchmark_id.isNull(), 'IN00000008', benchmark_id) AS benchmark_id
  976. FROM ms_get_entity_primary_benchmark(entity_type, v_entity_ids, oldest_date.temporalFormat('yyyy-MM'), end_day.temporalFormat('yyyy-MM')) ;
  977. }
  978. // 取所有出现的基准月收益
  979. bmk_ret = get_benchmark_return(benchmark, end_day);
  980. //if(bmk_ret.isVoid() || bmk_ret.size() == 0) { return null; }
  981. // TODO: risk free指数月收益存在fund_performance表,所以先将就用 fund_id 表示。之后统一改为更准确的名字
  982. risk_free_rate = SELECT entity_id AS fund_id, temporalParse(end_date, 'yyyy-MM') AS end_date, ret FROM ms_get_risk_free_rate(oldest_date, end_day);
  983. if(risk_free_rate.isVoid() || risk_free_rate.size() == 0) { return null; }
  984. // 指标计算
  985. if(indicator_type == 'BFI') {
  986. t0 = cal_trailing_bfi_indicators(entity_info, benchmark, end_day, monthly_returns, bmk_ret, risk_free_rate);
  987. v_table_name = ['BFI-INCEP', 'BFI-YTD', 'BFI-3M', 'BFI-6M', 'BFI-1Y', 'BFI-2Y', 'BFI-3Y', 'BFI-4Y', 'BFI-5Y', 'BFI-10Y'];
  988. } else if(indicator_type == 'CAI') {
  989. t0 = cal_trailing_bfi_indicators(entity_info, benchmark, end_day, monthly_returns, bmk_ret, risk_free_rate);
  990. v_table_name = ['CAI-INCEP', 'CAI-YTD', 'CAI-3M', 'CAI-6M', 'CAI-1Y', 'CAI-2Y', 'CAI-3Y', 'CAI-4Y', 'CAI-5Y', 'CAI-10Y'];
  991. } else {
  992. t0 = cal_trailing_indicators(entity_info, benchmark, end_day, monthly_returns, bmk_ret, risk_free_rate);
  993. v_table_name = ['PBI-INCEP', 'PBI-YTD', 'PBI-3M', 'PBI-6M', 'PBI-1Y', 'PBI-2Y', 'PBI-3Y', 'PBI-4Y', 'PBI-5Y', 'PBI-10Y'];
  994. }
  995. return dict(v_table_name, t0);
  996. }