alphawinnow 0.2.0

Typed, deterministic formulaic alpha-mining, factor-evaluation, and quantitative expression-search engine with a local CLI
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
use std::collections::BTreeMap;

use serde::{Deserialize, Serialize};

use crate::{
    ast::{Expr, ExprKind},
    canonical::{canonical, digest},
};

/// Conservative, machine-readable reason that a signal cannot enter a pool.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum RejectionReason {
    /// Exact structural analysis proves that the signal is identically zero.
    ProvablyZero,
    /// Exact structural analysis proves that the signal is constant.
    ProvablyConstant,
}

impl std::fmt::Display for RejectionReason {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        formatter.write_str(match self {
            Self::ProvablyZero => "provably_zero",
            Self::ProvablyConstant => "provably_constant",
        })
    }
}

/// Semantic identity and admission analysis computed in a single pass.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ExpressionAnalysis {
    pub semantic_canonical: String,
    pub semantic_fingerprint: String,
    pub rejection_reason: Option<RejectionReason>,
}

/// Analyze conservative semantic identity and triviality.
#[must_use]
pub fn analyze_expression(expression: &Expr) -> ExpressionAnalysis {
    let semantic_canonical = canonical(&semantic_normal_form(expression));
    let rejection_reason = match signal_fact(expression) {
        SignalFact::Zero => Some(RejectionReason::ProvablyZero),
        SignalFact::Constant => Some(RejectionReason::ProvablyConstant),
        SignalFact::Unknown => None,
    };
    ExpressionAnalysis {
        semantic_fingerprint: digest(&semantic_canonical),
        semantic_canonical,
        rejection_reason,
    }
}

/// Build a conservative normal form for exact identities and final-root scaling.
#[must_use]
pub(crate) fn semantic_normal_form(expression: &Expr) -> Expr {
    let simplified = simplify_exact_identities(expression);
    semantic_root_normal_form(&simplified)
}

fn semantic_root_normal_form(expression: &Expr) -> Expr {
    if let Some(repeated) = repeated_root_addend(expression) {
        return semantic_normal_form(repeated);
    }

    if matches!(expression.operator(), Some("multiply" | "divide")) {
        let mut scalar = 1.0;
        let mut factors = Vec::new();
        let recognized = collect_root_product(expression, &mut scalar, &mut factors);
        if recognized && scalar.is_finite() && scalar != 0.0 {
            if scalar > 0.0 {
                return rebuild_positive_product(factors);
            }
            return rebuild_negative_product(factors);
        }
    }
    expression.clone()
}

fn simplify_exact_identities(expression: &Expr) -> Expr {
    match expression {
        Expr::Field { .. } | Expr::Scalar { .. } | Expr::Group { .. } | Expr::Bool { .. } => {
            expression.clone()
        }
        Expr::UnaryCall {
            op,
            arg,
            kwargs,
            kind,
        } => {
            let arg = simplify_exact_identities(arg);
            let kwargs: BTreeMap<_, _> = kwargs
                .iter()
                .map(|(name, value)| (name.clone(), simplify_exact_identities(value)))
                .collect();

            // The benchmark's cross-sectional rank assigns every finite value a
            // unique ordinal and restores NaNs at their original positions.
            // Ranking that result again is therefore exactly idempotent.  Keep
            // this deliberately narrow: time-series and grouped ranks have
            // different semantics, and scaling inside rank remains significant.
            if op == "rank"
                && kwargs.is_empty()
                && matches!(
                    &arg,
                    Expr::UnaryCall {
                        op: inner_op,
                        kwargs: inner_kwargs,
                        ..
                    } if inner_op == "rank" && inner_kwargs.is_empty()
                )
            {
                return arg;
            }

            Expr::UnaryCall {
                op: op.clone(),
                arg: Box::new(arg),
                kwargs,
                kind: *kind,
            }
        }
        Expr::BinaryCall {
            op,
            left,
            right,
            kwargs,
            kind,
        } => Expr::BinaryCall {
            op: op.clone(),
            left: Box::new(simplify_exact_identities(left)),
            right: Box::new(simplify_exact_identities(right)),
            kwargs: kwargs
                .iter()
                .map(|(name, value)| (name.clone(), simplify_exact_identities(value)))
                .collect(),
            kind: *kind,
        },
        Expr::VariadicCall {
            op,
            args,
            kwargs,
            kind,
        } => {
            let args: Vec<_> = args.iter().map(simplify_exact_identities).collect();
            if op == "if_else"
                && let [condition, when_true, when_false] = args.as_slice()
            {
                if let Expr::Bool { value } = condition {
                    return if *value {
                        when_true.clone()
                    } else {
                        when_false.clone()
                    };
                }
                if canonical(when_true) == canonical(when_false) {
                    return when_true.clone();
                }
            }
            Expr::VariadicCall {
                op: op.clone(),
                args,
                kwargs: kwargs
                    .iter()
                    .map(|(name, value)| (name.clone(), simplify_exact_identities(value)))
                    .collect(),
                kind: *kind,
            }
        }
    }
}

fn repeated_root_addend(expression: &Expr) -> Option<&Expr> {
    let Expr::VariadicCall { op, args, .. } = expression else {
        return None;
    };
    if op != "add" {
        return None;
    }
    if let Some(first) = args.first() {
        let direct_identity = canonical(first);
        if args.len() >= 2
            && args
                .iter()
                .all(|addend| canonical(addend) == direct_identity)
        {
            return Some(first);
        }
    }
    let mut addends = Vec::new();
    collect_root_addends(expression, &mut addends);
    let first = addends.first()?;
    let identity = canonical(first);
    (addends.len() >= 2 && addends.iter().all(|addend| canonical(addend) == identity))
        .then_some(*first)
}

fn collect_root_addends<'a>(expression: &'a Expr, addends: &mut Vec<&'a Expr>) {
    if let Expr::VariadicCall { op, args, .. } = expression
        && op == "add"
    {
        for arg in args {
            collect_root_addends(arg, addends);
        }
    } else {
        addends.push(expression);
    }
}

fn collect_root_product(expression: &Expr, scalar: &mut f64, factors: &mut Vec<Expr>) -> bool {
    match expression {
        Expr::Scalar { value } => {
            *scalar *= value;
            true
        }
        Expr::VariadicCall { op, args, .. } if op == "multiply" => args
            .iter()
            .all(|arg| collect_root_product(arg, scalar, factors)),
        Expr::BinaryCall {
            op, left, right, ..
        } if op == "multiply" => {
            collect_root_product(left, scalar, factors)
                && collect_root_product(right, scalar, factors)
        }
        Expr::BinaryCall {
            op, left, right, ..
        } if op == "divide" => {
            if let Expr::Scalar { value } = right.as_ref() {
                if !collect_root_product(left, scalar, factors) {
                    return false;
                }
                *scalar /= value;
                true
            } else {
                false
            }
        }
        _ => {
            factors.push(expression.clone());
            true
        }
    }
}

fn rebuild_positive_product(mut factors: Vec<Expr>) -> Expr {
    if factors.len() == 1 {
        return semantic_normal_form(&factors.remove(0));
    }
    multiply(factors)
}

fn rebuild_negative_product(mut factors: Vec<Expr>) -> Expr {
    if factors.len() == 1 {
        let base = semantic_normal_form(&factors.remove(0));
        return multiply(vec![Expr::Scalar { value: -1.0 }, base]);
    }
    factors.push(Expr::Scalar { value: -1.0 });
    multiply(factors)
}

fn multiply(args: Vec<Expr>) -> Expr {
    Expr::VariadicCall {
        op: "multiply".to_owned(),
        args,
        kwargs: BTreeMap::new(),
        kind: ExprKind::Signal,
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum SignalFact {
    Unknown,
    Constant,
    Zero,
}

fn signal_fact(expression: &Expr) -> SignalFact {
    match expression {
        Expr::Field { .. } => SignalFact::Unknown,
        Expr::UnaryCall {
            op, arg, kwargs, ..
        } => unary_fact(op, signal_fact(arg), kwargs),
        Expr::BinaryCall {
            op, left, right, ..
        } => binary_fact(op, left, right),
        Expr::VariadicCall { op, args, .. } => variadic_fact(op, args),
        Expr::Scalar { .. } | Expr::Group { .. } | Expr::Bool { .. } => SignalFact::Constant,
    }
}

fn unary_fact(op: &str, arg: SignalFact, kwargs: &BTreeMap<String, Expr>) -> SignalFact {
    match (op, arg) {
        ("abs" | "negate" | "sign" | "winsorize", SignalFact::Zero) => SignalFact::Zero,
        ("clip", SignalFact::Zero) => {
            let lower = scalar_keyword(kwargs, "lower");
            let upper = scalar_keyword(kwargs, "upper");
            if lower.is_some_and(|value| value <= 0.0) && upper.is_some_and(|value| value >= 0.0) {
                SignalFact::Zero
            } else {
                SignalFact::Constant
            }
        }
        (_, SignalFact::Constant | SignalFact::Zero) => SignalFact::Constant,
        (_, SignalFact::Unknown) => SignalFact::Unknown,
    }
}

fn binary_fact(op: &str, left: &Expr, right: &Expr) -> SignalFact {
    let left_fact = signal_fact(left);
    match op {
        "multiply" => {
            let right_fact = signal_fact(right);
            if is_literal_zero(left)
                || is_literal_zero(right)
                || left_fact == SignalFact::Zero
                || right_fact == SignalFact::Zero
            {
                SignalFact::Zero
            } else {
                combine_constant(left_fact, right_fact)
            }
        }
        "subtract" if canonical(left) == canonical(right) => SignalFact::Zero,
        "add" | "subtract" => {
            let right_fact = signal_fact(right);
            if left_fact == SignalFact::Zero && right_fact == SignalFact::Zero {
                SignalFact::Zero
            } else {
                combine_constant(left_fact, right_fact)
            }
        }
        "divide" | "ts_delay" | "ts_ema" | "ts_max" | "ts_mean" | "ts_median" | "ts_min"
        | "ts_sum" | "ts_wma" => preserve_zero(left_fact),
        "ts_delta" | "ts_mad" | "ts_std_dev" | "ts_var" | "ts_zscore" => match left_fact {
            SignalFact::Zero | SignalFact::Constant => SignalFact::Zero,
            SignalFact::Unknown => SignalFact::Unknown,
        },
        "ts_rank" | "group_rank" => match left_fact {
            SignalFact::Zero | SignalFact::Constant => SignalFact::Constant,
            SignalFact::Unknown => SignalFact::Unknown,
        },
        _ => SignalFact::Unknown,
    }
}

fn variadic_fact(op: &str, args: &[Expr]) -> SignalFact {
    match op {
        "multiply" => {
            if args.iter().any(is_literal_zero)
                || args.iter().any(|arg| signal_fact(arg) == SignalFact::Zero)
            {
                SignalFact::Zero
            } else if args
                .iter()
                .filter(|arg| arg.kind() == ExprKind::Signal)
                .all(|arg| signal_fact(arg) == SignalFact::Constant)
            {
                SignalFact::Constant
            } else {
                SignalFact::Unknown
            }
        }
        "add" => {
            let facts: Vec<_> = args.iter().map(signal_fact).collect();
            if facts.iter().all(|fact| *fact == SignalFact::Zero) {
                SignalFact::Zero
            } else if facts
                .iter()
                .all(|fact| matches!(fact, SignalFact::Zero | SignalFact::Constant))
            {
                SignalFact::Constant
            } else {
                SignalFact::Unknown
            }
        }
        "if_else" => conditional_fact(args),
        _ => SignalFact::Unknown,
    }
}

fn conditional_fact(args: &[Expr]) -> SignalFact {
    let [condition, when_true, when_false] = args else {
        return SignalFact::Unknown;
    };
    if let Expr::Bool { value } = condition {
        return signal_fact(if *value { when_true } else { when_false });
    }
    if canonical(when_true) == canonical(when_false) {
        return signal_fact(when_true);
    }
    let when_true = signal_fact(when_true);
    let when_false = signal_fact(when_false);
    if when_true == SignalFact::Zero && when_false == SignalFact::Zero {
        SignalFact::Zero
    } else {
        SignalFact::Unknown
    }
}

const fn combine_constant(left: SignalFact, right: SignalFact) -> SignalFact {
    if matches!(left, SignalFact::Zero | SignalFact::Constant)
        && matches!(right, SignalFact::Zero | SignalFact::Constant)
    {
        SignalFact::Constant
    } else {
        SignalFact::Unknown
    }
}

const fn preserve_zero(fact: SignalFact) -> SignalFact {
    fact
}

fn scalar_keyword(kwargs: &BTreeMap<String, Expr>, name: &str) -> Option<f64> {
    match kwargs.get(name) {
        Some(Expr::Scalar { value }) => Some(*value),
        _ => None,
    }
}

fn is_literal_zero(expression: &Expr) -> bool {
    matches!(expression, Expr::Scalar { value } if *value == 0.0)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{Arity, builtin_catalog, parse_expression, parse_expression_with_catalog};

    #[test]
    fn deterministic_conditionals_share_the_selected_branch_family() {
        let selected = parse_expression("rank(close)").unwrap();
        for expression in [
            "if_else(true, rank(close), open)",
            "if_else(false, open, rank(close))",
            "multiply(if_else(true, rank(close), open), 2)",
        ] {
            let expression = parse_expression(expression).unwrap();
            assert_eq!(
                analyze_expression(&expression).semantic_fingerprint,
                analyze_expression(&selected).semantic_fingerprint
            );
        }
    }

    #[test]
    fn reports_machine_readable_zero_and_constant_reasons() {
        let zero = parse_expression("subtract(close, close)").unwrap();
        let constant = parse_expression("rank(multiply(close, 0))").unwrap();
        assert_eq!(
            analyze_expression(&zero).rejection_reason,
            Some(RejectionReason::ProvablyZero)
        );
        assert_eq!(
            analyze_expression(&constant).rejection_reason,
            Some(RejectionReason::ProvablyConstant)
        );
        assert_eq!(
            serde_json::to_string(&RejectionReason::ProvablyZero).unwrap(),
            "\"provably_zero\""
        );
    }

    #[test]
    fn binary_multiply_from_a_narrowed_catalog_detects_zero_in_either_slot() {
        let mut catalog = builtin_catalog().clone();
        let operator = catalog
            .operators
            .iter_mut()
            .find(|operator| operator.name == "multiply")
            .unwrap();
        let input = operator.inputs[0].clone();
        operator.arity = Arity::Binary;
        operator.inputs = vec![input; 2];

        for source in ["multiply(0, close)", "multiply(close, 0)"] {
            let expression = parse_expression_with_catalog(source, &catalog).unwrap();
            assert!(matches!(expression, Expr::BinaryCall { .. }));
            assert_eq!(
                analyze_expression(&expression).rejection_reason,
                Some(RejectionReason::ProvablyZero),
                "{source}"
            );
        }
    }

    #[test]
    fn stays_conservative_for_a_varying_condition_with_constant_branches() {
        let expression = parse_expression(
            "if_else(greater(close, 0), rank(multiply(close, 0)), clip(multiply(close, 0), lower=1, upper=2))",
        )
        .unwrap();
        assert_eq!(analyze_expression(&expression).rejection_reason, None);
    }
}