Math¶
Element by element: each date and stock on its own. Any argument can be a number, so sqrt(5), max(x, 0) and
power(2, x) all work.
returns(1)| A | B | C | D | |
|---|---|---|---|---|
| Jan 02 | · | · | · | · |
| Jan 03 | 0.100 | −0.050 | 0.017 | 0.050 |
| Jan 04 | 0.091 | −0.053 | · | −0.024 |
| Jan 05 | −0.083 | 0.028 | 0.016 | 0.049 |
| Jan 08 | 0.182 | −0.081 | 0.032 | 0.023 |
| Jan 09 | 0.077 | −0.059 | −0.016 | 0.045 |
abs(returns(1))| A | B | C | D | |
|---|---|---|---|---|
| Jan 02 | · | · | · | · |
| Jan 03 | 0.100 | 0.050 | 0.017 | 0.050 |
| Jan 04 | 0.091 | 0.053 | · | 0.024 |
| Jan 05 | 0.083 | 0.028 | 0.016 | 0.049 |
| Jan 08 | 0.182 | 0.081 | 0.032 | 0.023 |
| Jan 09 | 0.077 | 0.059 | 0.016 | 0.045 |
log¶
log(x)
The natural logarithm. No value for x < 0, and −∞ at 0.
exp¶
exp(x)
e to the x, the inverse of log; ∞ when too large.
exp(ts_sum(log(1 + returns(1)), 20)) - 1 # daily returns compounded over 20 days
1 / (1 + exp(-zscore(returns(5)))) # a sigmoid, from 0 to 1
scale(exp(zscore(returns(5)) / 0.5)) # a cross-sectional softmax, temperature 0.5
abs¶
abs(x)
The absolute value.
sign¶
sign(x)
−1, 0 or 1.
sqrt¶
sqrt(x)
The square root. No value for x < 0.
power¶
power(x, n)
x to the power n; n can be a number or an expression. x ** 2 is the same with a number as the exponent.
max¶
max(x, y)
The larger of x and y, element by element. Not the largest across stocks or over time: for those, use
ts_max or rank.
ts_mean(max(returns(1), 0), 14) / ts_mean(abs(returns(1)), 14) # the up share of recent moves, as in RSI
min¶
min(x, y)
The smaller of x and y, element by element.