Skip to content

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 ​

acosReturn the arc cosine
asinReturn the arc sine
atanReturn the arc tangent
atan2Converts rectangular coordinates (x, y) to polar (r, theta)
bitAndBitwise and
bitNotBitwise not
bitOrBitwise or
bitShiftlBitwise left shift
bitShiftrBitwise right shift
bitXorBitwise xor
ceilReturn the smallest whole number greater than or equal to val
cosReturn the cosine of angle in radians
coshReturn the hyperbolic cosine
expReturn e raised to val
fitLinearRegressionGiven a grid of x, y coordinates compute the best fit linear regression equation using the ordinary least squares method
floorReturn the largest whole number less than or equal to val
log10Return base 10 logarithm of val
logEReturn natural logarithm to the base e of val
matrixAddAdd two matrices together and return new matrix
matrixDeterminantReturn the determinant as a unitless Number for the given matrix which is any value accepted by toMatrix()
matrixFitLinearRegressionGiven 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
matrixInverseReturn the inverse of the given matrix which is any value accepted by toMatrix()
matrixMultMultiply two matrices and return new matrix
matrixSubSubtract two matrices and return new matrix
matrixTransposeTranspose the given matrix which is any value accepted by toMatrix()
meanFold a sample of numbers into their standard average or arithmetic mean
meanBiasErrFold a sample of numbers into their MBE (mean bias error)
medianFold a sample of numbers into their median value which is the middle value of the sorted samples
piReturn constant for pi
powReturn val raised to the specified power
quantileComputes the p*th* quantile of a list of numbers, according to the specified interpolation method
randomReturn random integer within given inclusive range
remainderReturn the remainder or modulo of division
rootMeanSquareErrFold a sample of numbers into their RMSE (root mean square error)
roundReturns the nearest whole number to val
sinReturn sine of angle in radians
sinhReturn hyperbolic sine
sqrtReturn square root of val
standardDeviationFold a series of numbers into the standard deviation of a sample
tanReturn tangent of angle in radians
tanhReturn hyperbolic tangent
toDegreesConvert angle in radians to an angle in degrees
toMatrixConvert a general grid to an optimized matrix grid
toRadiansConvert angle in degrees to an angle in radians


def ​

func:acos ​

acos(val) ​

Return the arc cosine.


meta ​

deffunc:acos
isfunc
liblib:math
nameacos

supertypes ​

featureFeature namespace of definitions formatted as feature:name
funcAxon function


def ​

func:asin ​

asin(val) ​

Return the arc sine.


meta ​

deffunc:asin
isfunc
liblib:math
nameasin

supertypes ​

featureFeature namespace of definitions formatted as feature:name
funcAxon function


def ​

func:atan ​

atan(val) ​

Return the arc tangent.


meta ​

deffunc:atan
isfunc
liblib:math
nameatan

supertypes ​

featureFeature namespace of definitions formatted as feature:name
funcAxon function


def ​

func:atan2 ​

atan2(y, x) ​

Converts rectangular coordinates (x, y) to polar (r, theta).


meta ​

deffunc:atan2
isfunc
liblib:math
nameatan2

supertypes ​

featureFeature namespace of definitions formatted as feature:name
funcAxon function


def ​

func:bitAnd ​

bitAnd(a, b) ​

Bitwise and: a & b


meta ​

deffunc:bitAnd
isfunc
liblib:math
namebitAnd

supertypes ​

featureFeature namespace of definitions formatted as feature:name
funcAxon function


def ​

func:bitNot ​

bitNot(a) ​

Bitwise not: ~a


meta ​

deffunc:bitNot
isfunc
liblib:math
namebitNot

supertypes ​

featureFeature namespace of definitions formatted as feature:name
funcAxon function


def ​

func:bitOr ​

bitOr(a, b) ​

Bitwise or: a | b


meta ​

deffunc:bitOr
isfunc
liblib:math
namebitOr

supertypes ​

featureFeature namespace of definitions formatted as feature:name
funcAxon function


def ​

func:bitShiftl ​

bitShiftl(a, b) ​

Bitwise left shift: a << b


meta ​

deffunc:bitShiftl
isfunc
liblib:math
namebitShiftl

supertypes ​

featureFeature namespace of definitions formatted as feature:name
funcAxon function


def ​

func:bitShiftr ​

bitShiftr(a, b) ​

Bitwise right shift: a >> b


meta ​

deffunc:bitShiftr
isfunc
liblib:math
namebitShiftr

supertypes ​

featureFeature namespace of definitions formatted as feature:name
funcAxon function


def ​

func:bitXor ​

bitXor(a, b) ​

Bitwise xor: a ^ b


meta ​

deffunc:bitXor
isfunc
liblib:math
namebitXor

supertypes ​

featureFeature namespace of definitions formatted as feature:name
funcAxon function


def ​

func:ceil ​

ceil(val) ​

Return the smallest whole number greater than or equal to val. Result has same unit as val.


meta ​

deffunc:ceil
isfunc
liblib:math
nameceil

supertypes ​

featureFeature namespace of definitions formatted as feature:name
funcAxon function


def ​

func:cos ​

cos(val) ​

Return the cosine of angle in radians.


meta ​

deffunc:cos
isfunc
liblib:math
namecos

supertypes ​

featureFeature namespace of definitions formatted as feature:name
funcAxon function


def ​

func:cosh ​

cosh(val) ​

Return the hyperbolic cosine.


meta ​

deffunc:cosh
isfunc
liblib:math
namecosh

supertypes ​

featureFeature namespace of definitions formatted as feature:name
funcAxon function


def ​

func:exp ​

exp(val) ​

Return e raised to val.


meta ​

deffunc:exp
isfunc
liblib:math
nameexp

supertypes ​

featureFeature namespace of definitions formatted as feature:name
funcAxon 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 column
  • y: column name to use for y if not second column

The resulting linear equation is:

yᵢ = mxᵢ + b

The equation is returned as a dictionary with these keys:

  • m: slope of the best fit regression line
  • b: intercept of the best fit regression line
  • r2: 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 data
  • xmax: maximum value of x variable in sample data
  • ymin: minimum value of y variable in sample data
  • ymax: 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 ​

deffunc:fitLinearRegression
isfunc
liblib:math
namefitLinearRegression

supertypes ​

featureFeature namespace of definitions formatted as feature:name
funcAxon function


def ​

func:floor ​

floor(val) ​

Return the largest whole number less than or equal to val. Result has same unit as val.


meta ​

deffunc:floor
isfunc
liblib:math
namefloor

supertypes ​

featureFeature namespace of definitions formatted as feature:name
funcAxon function


def ​

func:log10 ​

log10(val) ​

Return base 10 logarithm of val.


meta ​

deffunc:log10
isfunc
liblib:math
namelog10

supertypes ​

featureFeature namespace of definitions formatted as feature:name
funcAxon function


def ​

func:logE ​

logE(val) ​

Return natural logarithm to the base e of val.


meta ​

deffunc:logE
isfunc
liblib:math
namelogE

supertypes ​

featureFeature namespace of definitions formatted as feature:name
funcAxon 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 ​

deffunc:matrixAdd
isfunc
liblib:math
namematrixAdd

supertypes ​

featureFeature namespace of definitions formatted as feature:name
funcAxon 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 ​

deffunc:matrixDeterminant
isfunc
liblib:math
namematrixDeterminant

supertypes ​

featureFeature namespace of definitions formatted as feature:name
funcAxon 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 factors
  • r2: 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 coefficient
  • rowCount: 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 ​

deffunc:matrixFitLinearRegression
isfunc
liblib:math
namematrixFitLinearRegression

supertypes ​

featureFeature namespace of definitions formatted as feature:name
funcAxon function


def ​

func:matrixInverse ​

matrixInverse(m) ​

Return the inverse of the given matrix which is any value accepted by toMatrix().


meta ​

deffunc:matrixInverse
isfunc
liblib:math
namematrixInverse

supertypes ​

featureFeature namespace of definitions formatted as feature:name
funcAxon 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 ​

deffunc:matrixMult
isfunc
liblib:math
namematrixMult

supertypes ​

featureFeature namespace of definitions formatted as feature:name
funcAxon 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 ​

deffunc:matrixSub
isfunc
liblib:math
namematrixSub

supertypes ​

featureFeature namespace of definitions formatted as feature:name
funcAxon function


def ​

func:matrixTranspose ​

matrixTranspose(m) ​

Transpose the given matrix which is any value accepted by toMatrix().


meta ​

deffunc:matrixTranspose
isfunc
liblib:math
namematrixTranspose

supertypes ​

featureFeature namespace of definitions formatted as feature:name
funcAxon 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 ​

deffunc:mean
disKeyui::mean
foldOnNumber
isfunc
liblib:math
namemean

supertypes ​

featureFeature namespace of definitions formatted as feature:name
funcAxon 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 freedom

meta ​

deffunc:meanBiasErr
disKeyui::meanBiasErr
foldOnNumber
isfunc
liblib:math
namemeanBiasErr

supertypes ​

featureFeature namespace of definitions formatted as feature:name
funcAxon 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 ​

deffunc:median
disKeyui::median
foldOnNumber
isfunc
liblib:math
namemedian

supertypes ​

featureFeature namespace of definitions formatted as feature:name
funcAxon function


def ​

func:pi ​

pi() ​

Return constant for pi: 3.141592653589793


meta ​

deffunc:pi
isfunc
liblib:math
namepi

supertypes ​

featureFeature namespace of definitions formatted as feature:name
funcAxon function


def ​

func:pow ​

pow(val, exp) ​

Return val raised to the specified power.


meta ​

deffunc:pow
isfunc
liblib:math
namepow

supertypes ​

featureFeature namespace of definitions formatted as feature:name
funcAxon 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.5

Detailed 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) // => 22

meta ​

deffunc:quantile
isfunc
liblib:math
namequantile

supertypes ​

featureFeature namespace of definitions formatted as feature:name
funcAxon 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 100

meta ​

deffunc:random
isfunc
liblib:math
namerandom

supertypes ​

featureFeature namespace of definitions formatted as feature:name
funcAxon function


def ​

func:remainder ​

remainder(a, b) ​

Return the remainder or modulo of division: a % b. Result has same unit as a.


meta ​

deffunc:remainder
isfunc
liblib:math
nameremainder

supertypes ​

featureFeature namespace of definitions formatted as feature:name
funcAxon 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 freedom

meta ​

deffunc:rootMeanSquareErr
disKeyui::rootMeanSquareErr
foldOnNumber
isfunc
liblib:math
namerootMeanSquareErr

supertypes ​

featureFeature namespace of definitions formatted as feature:name
funcAxon function


def ​

func:round ​

round(val) ​

Returns the nearest whole number to val. Result has same unit as val.


meta ​

deffunc:round
isfunc
liblib:math
nameround

supertypes ​

featureFeature namespace of definitions formatted as feature:name
funcAxon function


def ​

func:sin ​

sin(val) ​

Return sine of angle in radians.


meta ​

deffunc:sin
isfunc
liblib:math
namesin

supertypes ​

featureFeature namespace of definitions formatted as feature:name
funcAxon function


def ​

func:sinh ​

sinh(val) ​

Return hyperbolic sine.


meta ​

deffunc:sinh
isfunc
liblib:math
namesinh

supertypes ​

featureFeature namespace of definitions formatted as feature:name
funcAxon function


def ​

func:sqrt ​

sqrt(val) ​

Return square root of val.


meta ​

deffunc:sqrt
isfunc
liblib:math
namesqrt

supertypes ​

featureFeature namespace of definitions formatted as feature:name
funcAxon 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 ​

deffunc:standardDeviation
disKeyui::standardDeviation
foldOnNumber
isfunc
liblib:math
namestandardDeviation

supertypes ​

featureFeature namespace of definitions formatted as feature:name
funcAxon function


def ​

func:tan ​

tan(val) ​

Return tangent of angle in radians.


meta ​

deffunc:tan
isfunc
liblib:math
nametan

supertypes ​

featureFeature namespace of definitions formatted as feature:name
funcAxon function


def ​

func:tanh ​

tanh(val) ​

Return hyperbolic tangent.


meta ​

deffunc:tanh
isfunc
liblib:math
nametanh

supertypes ​

featureFeature namespace of definitions formatted as feature:name
funcAxon function


def ​

func:toDegrees ​

toDegrees(val) ​

Convert angle in radians to an angle in degrees.


meta ​

deffunc:toDegrees
isfunc
liblib:math
nametoDegrees

supertypes ​

featureFeature namespace of definitions formatted as feature:name
funcAxon 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 ​

deffunc:toMatrix
isfunc
liblib:math
nametoMatrix

supertypes ​

featureFeature namespace of definitions formatted as feature:name
funcAxon function


def ​

func:toRadians ​

toRadians(val) ​

Convert angle in degrees to an angle in radians.


meta ​

deffunc:toRadians
isfunc
liblib:math
nametoRadians

supertypes ​

featureFeature namespace of definitions formatted as feature:name
funcAxon function