# 数学函数 / 数值计算 / 其他 ## `generateCombinations(list, ret, start, n, combine)` 声明:function 生成一维数组的全部非空组合 | 参数 | 类型 | 说明 | | --------- | ------- | ---------------------------------- | | `list` | array | 输入的一维数组 | | `ret` | array | 接收生成的组合 | | `start` | integer | 可选。开始选取的位置,默认 0 | | `n` | integer | 可选。当前组合的写入位置,默认 0 | | `combine` | array | 可选。递归生成过程中使用的组合缓存 | 返回:integer ### 示例 范例01:生成三个元素的全部非空组合 ```tsl list := array("a", "b", "c"); ret := array(); generateCombinations(list, ret); return ret; // 输出: // array( // ("a"), // ("a","b"), // ("a","b","c"), // ("a","c"), // ("b"), // ("b","c"), // ("c")) ``` ## `betainc(x, a, b, tail)` 声明:function 不完全贝塔函数 | 参数 | 类型 | 说明 | | ------ | ------ | ------------------------------------- | | `x` | array | (一维数字数组,实数) 变量 要求0<=x<=1; | | `a` | float | float 不完全贝塔函数的参数 要求a>=0; | | `b` | float | float 不完全贝塔函数的参数 要求b>=0; | | `tail` | string | 返回类型 | 返回:array ### 示例 ```tsl a := 3; b := 4; tail := 'upper'; x := rand(10); return array(betainc(x, a, b), betainc(x, a, b, tail)); ``` ## `maxDrawDown(data, updn, interval, ifabs)` 声明:function 最大回撤 | 参数 | 类型 | 说明 | | ---------- | ------- | ----------------------------------------------- | | `data` | array | 一维数字序列 | | `updn` | bool | 涨跌标示符。1为求最大涨,0为求最大跌(默认值) | | `interval` | integer | 最大区间,为空或者小于1时,求整个区间(默认值) | | `ifabs` | float | 预留字段,暂时没有实际意义,可不给值 。 | 返回:real/table ### 示例 范例01: ```tsl // 计算一组序列的最大涨幅 data := array(3, 1, 7, 5, 6, 3); return maxDrawDown(data, 1); // 输出: // 1 // 2 // 6 // 6 // 入点为 data[1],出点为 data[2],两者差为 6,最大涨幅比例差为 6 ``` 范例02: ```tsl // ------计算100日股票的最大跌幅 setSysParam(pn_stock(), 'SZ000002'); setSysParam(pn_date(), 20181031T); setSysParam(pn_rate(), 1); setSysParam(pn_rateday(), 20181031T); setSysParam(pn_cycle(), cy_day()); data := nDay3(100, close()); t := maxDrawDown(data); return t; // 输出: // 12 // 43 // 6.96759 // 0.25765 // 注:若需计算最大跌幅率,则只需t[3] * 100即可。 ``` ## `minRecoveryTime(r, n1, n2)` 声明:function 最小恢复时间 | 参数 | 类型 | 说明 | | ---- | ------- | -------------------- | | `r` | array | (一维数字数组) 序列, | | `n1` | integer | Int 买入点位置, | | `n2` | integer | Int 卖出点位置 | 返回:integer ### 示例 ```tsl // 科大讯飞(SZ002230)在20201220最近十个交易日序列中第一天购买第二天卖出后的最小恢复时间 setSysParam(pn_stock(), "SZ002230"); setSysParam(PN_Date(), 20201220t); r := nDay(10, "close", close())[:, "close"]; return minRecoveryTime(r, 0, 1); // 输出:6 ``` ## `nchoosek(n, k)` 声明:function 样本组合 | 参数 | 类型 | 说明 | | ---- | ------- | ------------------ | | `n` | array | 样本(一维数组) | | `k` | integer | 每组样本数量(整数) | 返回:array ### 示例 范例01:从四个样本中选取两个进行组合 ```tsl return nchoosek(array('a', 'b', 'c', 'd'), 2); // 输出: // array( // ("d", "c"), // ("d", "b"), // ("d", "a"), // ("c", "b"), // ("c", "a"), // ("b", "a")) ``` ## `percentage(y, per, style)` 声明:function 百分位去极值 | 参数 | 类型 | 说明 | | ------- | ----------- | ------------------------------------------------------------------------------------------- | | `y` | array | 数字数组,待处理数据 | | `per` | real\|array | 实数或者一维数字数组,去极值百分位;5表示 5%; | | `style` | integer | 整数,0 或者不输入时 去 百分位大于 per 和小于per的;1, 去除小于per的;2 去除 大于per的 数据 | 返回:array ### 示例 ```tsl y := rand(100, 2); per := 20; style := 0; return percentage(y, per, style); ``` ## `percentileOfScore(data, score, _type)` 声明:function 百分位占比(%) | 参数 | 类型 | 说明 | | ------- | ------- | ---------------- | | `data` | array | 数组 待处理数据, | | `score` | float | float 待排序值, | | `_type` | integer | 算法 | 返回:array ### 示例 ```tsl // 去除随机数组的极值 data := array(1, 2, 2, 4); ret := array(); n := 0; for i := 0 to 5 do begin ret[n, 'score'] := i; for j, kind in array('rank', 'strict', 'weak', 'mean')do begin ret[n, kind] := percentileOfScore(data, i, j); end ret[n, 'percenrank'] := percentrank(data, i); n++; end; return ret; ``` ## `perms(x)` 声明:function 样本排列 | 参数 | 类型 | 说明 | | ---- | ----- | ---------------- | | `x` | array | 样本,数组或数字 | 返回:array ### 示例 范例01:求10的阶乘 ```tsl return perms(10); // 输出:3628800 ``` 范例02:排列组合 ```tsl return perms(array("a", "b", "c")); // 输出: // array( // ("c", "b", "a"), // ("c", "a", "b"), // ("b", "c", "a"), // ("b", "a", "c"), // ("a", "b", "c"), // ("a", "c", "b")) ``` 范例03:返回数组Array(('A','B'),('C', 'D'))中任意两个数据的排列组合 ```tsl v := Array(('A', 'B'), ('C', 'D')); return perms(v, 2); ``` 范例04:对排列组合的每列求和 ```tsl func := new funcExe(); a := array("A", "B", "C"); return perms(a, 3, func); type funcExe = class public Data; function create(); begin Data := ""; end; function Exe(dat); begin Data += dat; end; end; ```