Logic and Conditions¶
Arithmetic, comparisons, logic, and the operators that choose between values or say whether there is one. The rules for missing values are in Missing Values; precedence is in Syntax.
Operators¶
| Operator | Description |
|---|---|
+, -, *, / |
Arithmetic, element by element |
** |
Power, with a number as the exponent |
>, <, >=, <=, ==, != |
1 or 0, missing where either side is missing |
&, |, ~ |
And, or, not, three-valued |
Comparisons¶
A comparison gives 1 or 0, and no value where either side has none: there are no returns on Jan 02.
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 |
returns(1) > 0| A | B | C | D | |
|---|---|---|---|---|
| Jan 02 | · | · | · | · |
| Jan 03 | 1 | 0 | 1 | 1 |
| Jan 04 | 1 | 0 | · | 0 |
| Jan 05 | 0 | 1 | 1 | 1 |
| Jan 08 | 1 | 0 | 1 | 1 |
| Jan 09 | 1 | 0 | 0 | 1 |
And, or, not¶
Three-valued: false & missing is 0, true | missing is 1, and otherwise missing with missing is missing. Below,
"up today and high-beta". On Jan 02 there are no returns: B and D have betas under 1, so the answer is 0
without knowing the return; for A and C it depends on the return, so there is no answer.
returns(1) > 0| A | B | C | D | |
|---|---|---|---|---|
| Jan 02 | · | · | · | · |
| Jan 03 | 1 | 0 | 1 | 1 |
| Jan 04 | 1 | 0 | · | 0 |
| Jan 05 | 0 | 1 | 1 | 1 |
| Jan 08 | 1 | 0 | 1 | 1 |
| Jan 09 | 1 | 0 | 0 | 1 |
field('beta') > 1| A | B | C | D | |
|---|---|---|---|---|
| Jan 02 | 1 | 0 | 1 | 0 |
| Jan 03 | 1 | 0 | 1 | 0 |
| Jan 04 | 1 | 0 | 1 | 0 |
| Jan 05 | 1 | 0 | 1 | 0 |
| Jan 08 | 1 | 0 | 1 | 0 |
| Jan 09 | 1 | 0 | 1 | 0 |
(returns(1) > 0) & (field('beta') > 1)| A | B | C | D | |
|---|---|---|---|---|
| Jan 02 | · | 0 | · | 0 |
| Jan 03 | 1 | 0 | 1 | 0 |
| Jan 04 | 1 | 0 | · | 0 |
| Jan 05 | 0 | 0 | 1 | 0 |
| Jan 08 | 1 | 0 | 1 | 0 |
| Jan 09 | 1 | 0 | 0 | 0 |
& and | bind tighter than comparisons, so the parentheses are needed.
Choosing values¶
where¶
where(cond, if_true, if_false)
if_true where cond is nonzero, if_false where it is 0, and no value where it is missing.
Below, the day's return rank, for high-beta stocks only.
field('beta') > 1| A | B | C | D | |
|---|---|---|---|---|
| Jan 02 | 1 | 0 | 1 | 0 |
| Jan 03 | 1 | 0 | 1 | 0 |
| Jan 04 | 1 | 0 | 1 | 0 |
| Jan 05 | 1 | 0 | 1 | 0 |
| Jan 08 | 1 | 0 | 1 | 0 |
| Jan 09 | 1 | 0 | 1 | 0 |
where(field('beta') > 1, rank(returns(1)), None)| A | B | C | D | |
|---|---|---|---|---|
| Jan 02 | · | · | · | · |
| Jan 03 | 1.00 | · | 0.50 | · |
| Jan 04 | 1.00 | · | · | · |
| Jan 05 | 0.25 | · | 0.50 | · |
| Jan 08 | 1.00 | · | 0.75 | · |
| Jan 09 | 1.00 | · | 0.50 | · |
coalesce¶
coalesce(x, y)
x where it has a value, else y: a number, a label or an expression. It says what missing should mean: below, a
day without a report counts as no surprise. Choose with care: a missing return is not a 0% return.
field('surprise')| A | B | C | D | |
|---|---|---|---|---|
| Jan 02 | · | · | · | · |
| Jan 03 | 0.05 | · | · | · |
| Jan 04 | · | · | · | · |
| Jan 05 | · | · | −0.02 | · |
| Jan 08 | · | −0.01 | · | 0.03 |
| Jan 09 | · | · | · | · |
coalesce(field('surprise'), 0)| A | B | C | D | |
|---|---|---|---|---|
| Jan 02 | 0.00 | 0.00 | 0.00 | 0.00 |
| Jan 03 | 0.05 | 0.00 | 0.00 | 0.00 |
| Jan 04 | 0.00 | 0.00 | 0.00 | 0.00 |
| Jan 05 | 0.00 | 0.00 | −0.02 | 0.00 |
| Jan 08 | 0.00 | −0.01 | 0.00 | 0.03 |
| Jan 09 | 0.00 | 0.00 | 0.00 | 0.00 |
coalesce(field('price') > 5, 0) # no price counts as not above $5
coalesce(group('sector'), 'Other') # a label for stocks without one
None¶
None
No value here, on purpose: where(cond, x, None). None and 0 are different answers. A 0 is a value, and once
a book demeans the signal it is a position against the day's average; None takes the stock out.
Is there a value?¶
is_valid¶
is_valid(x)
1 where x has a value, else 0. An infinity counts as a value.
close()| A | B | C | D | |
|---|---|---|---|---|
| Jan 02 | 10.0 | 20.0 | 30.0 | 40.0 |
| Jan 03 | 11.0 | 19.0 | 30.5 | 42.0 |
| Jan 04 | 12.0 | 18.0 | · | 41.0 |
| Jan 05 | 11.0 | 18.5 | 31.0 | 43.0 |
| Jan 08 | 13.0 | 17.0 | 32.0 | 44.0 |
| Jan 09 | 14.0 | 16.0 | 31.5 | 46.0 |
is_valid(close())| A | B | C | D | |
|---|---|---|---|---|
| Jan 02 | 1 | 1 | 1 | 1 |
| Jan 03 | 1 | 1 | 1 | 1 |
| Jan 04 | 1 | 1 | 0 | 1 |
| Jan 05 | 1 | 1 | 1 | 1 |
| Jan 08 | 1 | 1 | 1 | 1 |
| Jan 09 | 1 | 1 | 1 | 1 |
is_finite¶
is_finite(x)
1 where x is a finite number, 0 where it is missing or infinite.