外观
lib-math 库参考
原文: https://finproducts.atlassian.net/wiki/spaces/JIDD3/pages/34793521509/FIN+5.3.1+lib-math 中文整理基于 FIN 5.3.1
导读: 数学函数库
以下为 API 参考,保留英文以便对照源码;目录已列出(英文标题便于检索);概述已尽量中译。
目录(导读)
- lib
- meta
- funcs
- def
- acos(val)
- asin(val)
- atan(val)
- atan2(y, x)
- bitAnd(a, b)
- bitNot(a)
- bitOr(a, b)
- bitShiftl(a, b)
- bitShiftr(a, b)
- bitXor(a, b)
- ceil(val)
- cos(val)
- cosh(val)
- exp(val)
- fitLinearRegression(grid, opts: null)
- floor(val)
- log10(val)
- logE(val)
- matrixAdd(a, b)
- matrixDeterminant(m)
- matrixFitLinearRegression(y, x)
- matrixInverse(m)
- matrixMult(a, b)
- matrixSub(a, b)
- matrixTranspose(m)
- mean(val, acc)
- meanBiasErr(val, acc, nDegrees: 0)
- median(val, acc)
- pi()
- pow(val, exp)
- quantile(percent, method: "linear")
- random(range: null)
- remainder(a, b)
- rootMeanSquareErr(val, acc, nDegrees: 0)
- round(val)
- sin(val)
- sinh(val)
- sqrt(val)
- standardDeviation(val, acc)
- tan(val)
- tanh(val)
- toDegrees(val)
- toMatrix(obj, opts: {})
- toRadians(val)
概述(中译)
数学函数库
API 正文(英文)
funcs
| acos | Return the arc cosine |
| asin | Return the arc sine |
| atan | Return the arc tangent |
| atan2 | Converts rectangular coordinates (x, y) to polar (r, theta) |
| bitAnd | Bitwise and |
| bitNot | Bitwise not |
| bitOr | Bitwise or |
| bitShiftl | Bitwise left shift |
| bitShiftr | Bitwise right shift |
| bitXor | Bitwise xor |
| ceil | Return the smallest whole number greater than or equal to val |
| cos | Return the cosine of angle in radians |
| cosh | Return the hyperbolic cosine |
| exp | Return e raised to val |
| fitLinearRegression | Given a grid of x, y coordinates compute the best fit linear regression equation using the ordinary least squares method |
| floor | Return the largest whole number less than or equal to val |
| log10 | Return base 10 logarithm of val |
| logE | Return natural logarithm to the base e of val |
| matrixAdd | Add two matrices together and return new matrix |
| matrixDeterminant | Return the determinant as a unitless Number for the given matrix which is any value accepted by toMatrix() |
| matrixFitLinearRegression | Given a matrix of y coordinates and a matrix of multiple x coordinates compute the best fit multiple linear regression equation using the ordinary least squares method |
| matrixInverse | Return the inverse of the given matrix which is any value accepted by toMatrix() |
| matrixMult | Multiply two matrices and return new matrix |
| matrixSub | Subtract two matrices and return new matrix |
| matrixTranspose | Transpose the given matrix which is any value accepted by toMatrix() |
| mean | Fold a sample of numbers into their standard average or arithmetic mean |
| meanBiasErr | Fold a sample of numbers into their MBE (mean bias error) |
| median | Fold a sample of numbers into their median value which is the middle value of the sorted samples |
| pi | Return constant for pi |
| pow | Return val raised to the specified power |
| quantile | Computes the p*th* quantile of a list of numbers, according to the specified interpolation method |
| random | Return random integer within given inclusive range |
| remainder | Return the remainder or modulo of division |
| rootMeanSquareErr | Fold a sample of numbers into their RMSE (root mean square error) |
| round | Returns the nearest whole number to val |
| sin | Return sine of angle in radians |
| sinh | Return hyperbolic sine |
| sqrt | Return square root of val |
| standardDeviation | Fold a series of numbers into the standard deviation of a sample |
| tan | Return tangent of angle in radians |
| tanh | Return hyperbolic tangent |
| toDegrees | Convert angle in radians to an angle in degrees |
| toMatrix | Convert a general grid to an optimized matrix grid |
| toRadians | Convert angle in degrees to an angle in radians |
def
func:acos
acos(val)
Return the arc cosine.
meta
| def | func:acos |
| is | func |
| lib | lib:math |
| name | acos |
supertypes
| feature | Feature namespace of definitions formatted as feature:name |
| func | Axon function |
def
func:asin
asin(val)
Return the arc sine.
meta
| def | func:asin |
| is | func |
| lib | lib:math |
| name | asin |
supertypes
| feature | Feature namespace of definitions formatted as feature:name |
| func | Axon function |
def
func:atan
atan(val)
Return the arc tangent.
meta
| def | func:atan |
| is | func |
| lib | lib:math |
| name | atan |
supertypes
| feature | Feature namespace of definitions formatted as feature:name |
| func | Axon function |
def
func:atan2
atan2(y, x)
Converts rectangular coordinates (x, y) to polar (r, theta).
meta
| def | func:atan2 |
| is | func |
| lib | lib:math |
| name | atan2 |
supertypes
| feature | Feature namespace of definitions formatted as feature:name |
| func | Axon function |
def
func:bitAnd
bitAnd(a, b)
Bitwise and: a & b
meta
| def | func:bitAnd |
| is | func |
| lib | lib:math |
| name | bitAnd |
supertypes
| feature | Feature namespace of definitions formatted as feature:name |
| func | Axon function |
def
func:bitNot
bitNot(a)
Bitwise not: ~a
meta
| def | func:bitNot |
| is | func |
| lib | lib:math |
| name | bitNot |
supertypes
| feature | Feature namespace of definitions formatted as feature:name |
| func | Axon function |
def
func:bitOr
bitOr(a, b)
Bitwise or: a | b
meta
| def | func:bitOr |
| is | func |
| lib | lib:math |
| name | bitOr |
supertypes
| feature | Feature namespace of definitions formatted as feature:name |
| func | Axon function |
def
func:bitShiftl
bitShiftl(a, b)
Bitwise left shift: a << b
meta
| def | func:bitShiftl |
| is | func |
| lib | lib:math |
| name | bitShiftl |
supertypes
| feature | Feature namespace of definitions formatted as feature:name |
| func | Axon function |
def
func:bitShiftr
bitShiftr(a, b)
Bitwise right shift: a >> b
meta
| def | func:bitShiftr |
| is | func |
| lib | lib:math |
| name | bitShiftr |
supertypes
| feature | Feature namespace of definitions formatted as feature:name |
| func | Axon function |
def
func:bitXor
bitXor(a, b)
Bitwise xor: a ^ b
meta
| def | func:bitXor |
| is | func |
| lib | lib:math |
| name | bitXor |
supertypes
| feature | Feature namespace of definitions formatted as feature:name |
| func | Axon function |
def
func:ceil
ceil(val)
Return the smallest whole number greater than or equal to val. Result has same unit as val.
meta
| def | func:ceil |
| is | func |
| lib | lib:math |
| name | ceil |
supertypes
| feature | Feature namespace of definitions formatted as feature:name |
| func | Axon function |
def
func:cos
cos(val)
Return the cosine of angle in radians.
meta
| def | func:cos |
| is | func |
| lib | lib:math |
| name | cos |
supertypes
| feature | Feature namespace of definitions formatted as feature:name |
| func | Axon function |
def
func:cosh
cosh(val)
Return the hyperbolic cosine.
meta
| def | func:cosh |
| is | func |
| lib | lib:math |
| name | cosh |
supertypes
| feature | Feature namespace of definitions formatted as feature:name |
| func | Axon function |
def
func:exp
exp(val)
Return e raised to val.
meta
| def | func:exp |
| is | func |
| lib | lib:math |
| name | exp |
supertypes
| feature | Feature namespace of definitions formatted as feature:name |
| func | Axon function |
def
func:fitLinearRegression
fitLinearRegression(grid, opts: null)
Given a grid of x, y coordinates compute the best fit linear regression equation using the ordinary least squares method. The first column of the grid is used for x and the second column is y. Any rows without a Number for both x and y are skipped. Any special Numbers (infinity/NaN) are skipped.
Options:
x: column name to use for x if not first columny: column name to use for y if not second column
The resulting linear equation is:
yᵢ = mxᵢ + bThe equation is returned as a dictionary with these keys:
m: slope of the best fit regression lineb: intercept of the best fit regression liner2: R² coefficient of determination as a number between 1.0 (perfect correlation) and 0.0 (no correlation)xmin: minimum value of x variable in sample dataxmax: maximum value of x variable in sample dataymin: minimum value of y variable in sample dataymax: maximum value of y variable in sample data
Also see matrixFitLinearRegression() to compute a multiple linear regression.
Example:
data: [{x:1, y:2},
{x:2, y:4},
{x:4, y:4},
{x:6, y:5}].toGrid
fitLinearRegression(data)
>>> {m:0.4915, b: 2.1525, r2: 0.7502}meta
| def | func:fitLinearRegression |
| is | func |
| lib | lib:math |
| name | fitLinearRegression |
supertypes
| feature | Feature namespace of definitions formatted as feature:name |
| func | Axon function |
def
func:floor
floor(val)
Return the largest whole number less than or equal to val. Result has same unit as val.
meta
| def | func:floor |
| is | func |
| lib | lib:math |
| name | floor |
supertypes
| feature | Feature namespace of definitions formatted as feature:name |
| func | Axon function |
def
func:log10
log10(val)
Return base 10 logarithm of val.
meta
| def | func:log10 |
| is | func |
| lib | lib:math |
| name | log10 |
supertypes
| feature | Feature namespace of definitions formatted as feature:name |
| func | Axon function |
def
func:logE
logE(val)
Return natural logarithm to the base e of val.
meta
| def | func:logE |
| is | func |
| lib | lib:math |
| name | logE |
supertypes
| feature | Feature namespace of definitions formatted as feature:name |
| func | Axon function |
def
func:matrixAdd
matrixAdd(a, b)
Add two matrices together and return new matrix. The parameters may be any value supported toMatrix(). Matrices must have the same dimensions.
meta
| def | func:matrixAdd |
| is | func |
| lib | lib:math |
| name | matrixAdd |
supertypes
| feature | Feature namespace of definitions formatted as feature:name |
| func | Axon function |
def
func:matrixDeterminant
matrixDeterminant(m)
Return the determinant as a unitless Number for the given matrix which is any value accepted by toMatrix(). The matrix must be square.
meta
| def | func:matrixDeterminant |
| is | func |
| lib | lib:math |
| name | matrixDeterminant |
supertypes
| feature | Feature namespace of definitions formatted as feature:name |
| func | Axon function |
def
func:matrixFitLinearRegression
matrixFitLinearRegression(y, x)
Given a matrix of y coordinates and a matrix of multiple x coordinates compute the best fit multiple linear regression equation using the ordinary least squares method. Both y and x may be any value accepted by toMatrix().
The resulting linear equation for r X coordinates is:
yᵢ = bias + b₁xᵢ₁ + b₂xᵢ₂ +...+ bᵣxᵢᵣThe equation is returned as a grid. The grid meta:
bias: bias or zero coefficient which is independent of any of the x factorsr2: R² coefficient of determination as a number between 1.0 (perfect correlation) and 0.0 (no correlation)r: the square root of R², referred to as the correlation coefficientrowCount: the number of rows of data used in the correlation For each X factor there is a row with the following tags:b: the correlation coefficient for the given X factor
meta
| def | func:matrixFitLinearRegression |
| is | func |
| lib | lib:math |
| name | matrixFitLinearRegression |
supertypes
| feature | Feature namespace of definitions formatted as feature:name |
| func | Axon function |
def
func:matrixInverse
matrixInverse(m)
Return the inverse of the given matrix which is any value accepted by toMatrix().
meta
| def | func:matrixInverse |
| is | func |
| lib | lib:math |
| name | matrixInverse |
supertypes
| feature | Feature namespace of definitions formatted as feature:name |
| func | Axon function |
def
func:matrixMult
matrixMult(a, b)
Multiply two matrices and return new matrix. The parameters may be any value supported toMatrix(). Matrix a column count must match matrix b row count.
meta
| def | func:matrixMult |
| is | func |
| lib | lib:math |
| name | matrixMult |
supertypes
| feature | Feature namespace of definitions formatted as feature:name |
| func | Axon function |
def
func:matrixSub
matrixSub(a, b)
Subtract two matrices and return new matrix. The parameters may be any value supported toMatrix(). Matrices must have the same dimensions.
meta
| def | func:matrixSub |
| is | func |
| lib | lib:math |
| name | matrixSub |
supertypes
| feature | Feature namespace of definitions formatted as feature:name |
| func | Axon function |
def
func:matrixTranspose
matrixTranspose(m)
Transpose the given matrix which is any value accepted by toMatrix().
meta
| def | func:matrixTranspose |
| is | func |
| lib | lib:math |
| name | matrixTranspose |
supertypes
| feature | Feature namespace of definitions formatted as feature:name |
| func | Axon function |
def
func:mean
mean(val, acc)
Fold a sample of numbers into their standard average or arithmetic mean. This function is the same as core::avg. Nulls values are ignored. Return null if no values.
Example:
[2, 4, 5, 3].fold(mean)meta
| def | func:mean |
| disKey | ui::mean |
| foldOn | Number |
| is | func |
| lib | lib:math |
| name | mean |
supertypes
| feature | Feature namespace of definitions formatted as feature:name |
| func | Axon function |
def
func:meanBiasErr
meanBiasErr(val, acc, nDegrees: 0)
Fold a sample of numbers into their MBE (mean bias error). The MBE function determines the MBE between a sample set and its mean:
MBE = Σ(xᵢ - median) / (n - nDegrees)Examples:
samples.fold(meanBiasErr) // unbiased zero degrees of freedom
samples.fold(meanBiasErr(_,_,1)) // 1 degree of freedommeta
| def | func:meanBiasErr |
| disKey | ui::meanBiasErr |
| foldOn | Number |
| is | func |
| lib | lib:math |
| name | meanBiasErr |
supertypes
| feature | Feature namespace of definitions formatted as feature:name |
| func | Axon function |
def
func:median
median(val, acc)
Fold a sample of numbers into their median value which is the middle value of the sorted samples. If there are an even number of sample, then the median is the mean of the middle two. Null values are ignored. Return null if no values.
Example:
[2, 4, 5, 3, 1].fold(median)meta
| def | func:median |
| disKey | ui::median |
| foldOn | Number |
| is | func |
| lib | lib:math |
| name | median |
supertypes
| feature | Feature namespace of definitions formatted as feature:name |
| func | Axon function |
def
func:pi
pi()
Return constant for pi: 3.141592653589793
meta
| def | func:pi |
| is | func |
| lib | lib:math |
| name | pi |
supertypes
| feature | Feature namespace of definitions formatted as feature:name |
| func | Axon function |
def
func:pow
pow(val, exp)
Return val raised to the specified power.
meta
| def | func:pow |
| is | func |
| lib | lib:math |
| name | pow |
supertypes
| feature | Feature namespace of definitions formatted as feature:name |
| func | Axon function |
def
func:quantile
quantile(percent, method: "linear")
Computes the p*th* quantile of a list of numbers, according to the specified interpolation method. The value p must be a number between 0.0 to 1.0.
- linear (default): Interpolates proportionally between the two closest values
- nearest: Rounds to the nearest data point
- lower: Rounds to the nearest lower data point
- higher: Rounds to the nearest higher data point
- midpoint: Averages two nearest values
Usage:
[1,2,3].fold(quantile(p, method))Examples:
[10,10,10,25,100].fold(quantile(0.7 )) => 22 //default to linear
[10,10,10,25,100].fold(quantile(0.7, "nearest")) => 25
[10,10,10,25,100].fold(quantile(0.7, "lower")) => 10
[10,10,10,25,100].fold(quantile(0.7, "higher")) => 25
[10,10,10,25,100].fold(quantile(0.7, "linear")) => 22 //same as no arg
[10,10,10,25,100].fold(quantile(0.7, "midpoint")) => 17.5Detailed Logic:
p: percentile (decimal 0-1)
n: list size
rank: p * (n-1) // this is the index of the percentile in your list
// if rank is an integer, return list[rank]
// if rank is not an integer, interpolate via one of the above methods (illustrated below in examples)
[1,2,3,4,5].percentile(0.5) => 3 // rank=2 is an int so we can index[2] directly
[10,10,10, 25, 100].percentile(0.7, method)
rank = (0.7 * 4) => 2.8
//adjust rank based on method
nearest = index[3] // => 25
lower = index[2] // => 10
higher = index[3] // => 25
//or interpolate for these methods
//takes the 2 closest indices and calculates midpoint
midpoint = (25-10)/2 + 10 // => 17.5
//takes the 2 closest indices and calculates weighted average
linear = (0.2 * 10) + (0.8 * 25) // => 22meta
| def | func:quantile |
| is | func |
| lib | lib:math |
| name | quantile |
supertypes
| feature | Feature namespace of definitions formatted as feature:name |
| func | Axon function |
def
func:random
random(range: null)
Return random integer within given inclusive range. If range is null, then full range of representative integers is assumed.
Examples:
random() // random num with no range
random(0..100) // random num between 0 and 100meta
| def | func:random |
| is | func |
| lib | lib:math |
| name | random |
supertypes
| feature | Feature namespace of definitions formatted as feature:name |
| func | Axon function |
def
func:remainder
remainder(a, b)
Return the remainder or modulo of division: a % b. Result has same unit as a.
meta
| def | func:remainder |
| is | func |
| lib | lib:math |
| name | remainder |
supertypes
| feature | Feature namespace of definitions formatted as feature:name |
| func | Axon function |
def
func:rootMeanSquareErr
rootMeanSquareErr(val, acc, nDegrees: 0)
Fold a sample of numbers into their RMSE (root mean square error). The RMSE function determines the RMSE between a sample set and its mean using the n-degrees of freedom RMSE:
RMBE = sqrt( Σ(xᵢ - median)² ) / (n - nDegrees)Examples:
samples.fold(rootMeanSquareErr) // unbiased zero degrees of freedom
samples.fold(rootMeanSquareErr(_,_,1)) // 1 degree of freedommeta
| def | func:rootMeanSquareErr |
| disKey | ui::rootMeanSquareErr |
| foldOn | Number |
| is | func |
| lib | lib:math |
| name | rootMeanSquareErr |
supertypes
| feature | Feature namespace of definitions formatted as feature:name |
| func | Axon function |
def
func:round
round(val)
Returns the nearest whole number to val. Result has same unit as val.
meta
| def | func:round |
| is | func |
| lib | lib:math |
| name | round |
supertypes
| feature | Feature namespace of definitions formatted as feature:name |
| func | Axon function |
def
func:sin
sin(val)
Return sine of angle in radians.
meta
| def | func:sin |
| is | func |
| lib | lib:math |
| name | sin |
supertypes
| feature | Feature namespace of definitions formatted as feature:name |
| func | Axon function |
def
func:sinh
sinh(val)
Return hyperbolic sine.
meta
| def | func:sinh |
| is | func |
| lib | lib:math |
| name | sinh |
supertypes
| feature | Feature namespace of definitions formatted as feature:name |
| func | Axon function |
def
func:sqrt
sqrt(val)
Return square root of val.
meta
| def | func:sqrt |
| is | func |
| lib | lib:math |
| name | sqrt |
supertypes
| feature | Feature namespace of definitions formatted as feature:name |
| func | Axon function |
def
func:standardDeviation
standardDeviation(val, acc)
Fold a series of numbers into the standard deviation of a sample:
s = sqrt(Σ (xᵢ - mean)² / (n-1))Example:
[4, 2, 5, 8, 6].fold(standardDeviation)meta
| def | func:standardDeviation |
| disKey | ui::standardDeviation |
| foldOn | Number |
| is | func |
| lib | lib:math |
| name | standardDeviation |
supertypes
| feature | Feature namespace of definitions formatted as feature:name |
| func | Axon function |
def
func:tan
tan(val)
Return tangent of angle in radians.
meta
| def | func:tan |
| is | func |
| lib | lib:math |
| name | tan |
supertypes
| feature | Feature namespace of definitions formatted as feature:name |
| func | Axon function |
def
func:tanh
tanh(val)
Return hyperbolic tangent.
meta
| def | func:tanh |
| is | func |
| lib | lib:math |
| name | tanh |
supertypes
| feature | Feature namespace of definitions formatted as feature:name |
| func | Axon function |
def
func:toDegrees
toDegrees(val)
Convert angle in radians to an angle in degrees.
meta
| def | func:toDegrees |
| is | func |
| lib | lib:math |
| name | toDegrees |
supertypes
| feature | Feature namespace of definitions formatted as feature:name |
| func | Axon function |
def
func:toMatrix
toMatrix(obj, opts: {})
Convert a general grid to an optimized matrix grid. Matrixs are two dimensional grids of Numbers. Columns are named "v0", "v1", "v2", etc. Grid meta is preserved, but not column meta. Numbers in the resulting matrix are unitless; any units passed in are stripped.
The following options are supported:
- nullVal (Number): replace null values in the grid with this value
- naVal (Number): replace NA values in the grid with this value
toMatrix(grid, {nullVal: 0, naVal: 0})To create a sparse or initialized matrix you can pass a Dict with the the following tags (all required)
toMatrix({rows:10, cols: 1000, init: 0})meta
| def | func:toMatrix |
| is | func |
| lib | lib:math |
| name | toMatrix |
supertypes
| feature | Feature namespace of definitions formatted as feature:name |
| func | Axon function |
def
func:toRadians
toRadians(val)
Convert angle in degrees to an angle in radians.
meta
| def | func:toRadians |
| is | func |
| lib | lib:math |
| name | toRadians |
supertypes
| feature | Feature namespace of definitions formatted as feature:name |
| func | Axon function |