Skip to content

Prediction

PyRapide's CausalPredictor learns causal patterns from historical computations and uses them to predict what events are likely to occur next, given a partial observation of the current state.

i Note
Prediction in PyRapide is fundamentally different from time-series forecasting. Instead of predicting when something will happen, the predictor forecasts what will happen based on which causal predecessors have already been observed.

CausalPredictor

The predictor works in two phases: learn from historical computations, then predict from partial observations.

predictor_init.py python
from pyrapide import CausalPredictor

predictor = CausalPredictor()

Learning

Feed the predictor one or more completed computations. It extracts causal transition patterns, including which event types tend to cause which other event types and with what frequency.

predictor_learn.py python
# Learn from historical computations
predictor.learn(computation_1)
predictor.learn(computation_2)
predictor.learn(computation_3)

# The more computations you provide, the more
# accurate the predictions become.

Predicting

Given a set of currently observed events, predict what is likely to happen next. The horizon parameter controls how many causal steps ahead to predict.

predictor_predict.py python
1# Predict the next likely events
2predictions = predictor.predict(
3    observed_events=[current_event_a, current_event_b],
4    horizon=3  # look 3 causal steps ahead
5)
6
7for prediction in predictions:
8    print(f"{prediction.event_type}: {prediction.probability:.2%}")
9    print(f"  Expected causes: {prediction.expected_causes}")
10    print(f"  Causal distance: {prediction.steps_ahead}")
💡 Tip
Use prediction with the StreamProcessor to proactively warn about likely failures. If the predictor sees a pattern that historically led to an error 80% of the time, you can trigger an alert before the error actually occurs.

Full Example

prediction_full.py python
1from pyrapide import CausalPredictor, Engine, architecture
2
3# Train on historical runs
4predictor = CausalPredictor()
5
6for historical_run in load_historical_runs():
7    predictor.learn(historical_run)
8
9# During live processing, predict ahead
10engine = Engine()
11computation = await engine.run(my_architecture)
12
13partial = computation.events_so_far()
14predictions = predictor.predict(partial, horizon=2)
15
16for p in predictions:
17    if p.event_type == "System.failure" and p.probability > 0.7:
18        print(f"WARNING: {p.probability:.0%} chance of failure")

Next Steps