Skip to content

Predict a native diffusion curve

This tutorial uses Innovate’s 0.7 development API. It predicts a Logistic curve from supplied parameters and checks the result independently. Start with the development installation; an older package from a registry may expose a different API.

After running the example, you can pass a Polars table to Rust, interpret the prediction column, confirm that input identifiers survive the native call, and check that invalid time grids produce an error.

Use the repository checkout with Python 3.15.2, Rust 1.99.1, and the locked environment installed. You need basic Python and dataframe familiarity. The example uses three synthetic query times, requires no dataset download, and performs no network request.

The time unit is arbitrary but must agree with the parameters. With time in years, k has units of inverse years and x0 is measured in years; L is the asymptotic level in your chosen adoption units.

From the repository root, run the checked file in the installed environment:

Terminal window
.venv-native/bin/python -I examples/native_forecast.py

The displayed source is imported from that exact file. It is not a separately maintained copy.

examples/native_forecast.py
"""Check native Logistic prediction against an independent analytic reference.
Synthetic values have arbitrary adoption units; no empirical fit or calibrated
forecast uncertainty is implied. Run with the installed development package.
"""
import json
import math
import polars as pl
from innovate import predict
def main() -> None:
"""Validate predictions, preserved identity, LazyFrames and invalid grids."""
times = [0.0, 1.0, 2.0]
parameters = {"L": 100.0, "k": 1.0, "x0": 1.0}
frame = pl.DataFrame({"time": times, "id": ["a", "b", "c"]})
forecast = predict("logistic", frame, parameters)
# This independent closed form is a test oracle, not a production fallback.
expected = [100.0 / (1.0 + math.exp(-(time - 1.0))) for time in times]
actual = forecast["prediction"].to_list()
assert all(
math.isclose(value, reference, rel_tol=1e-12, abs_tol=1e-12)
for value, reference in zip(actual, expected, strict=True)
)
assert forecast["id"].to_list() == ["a", "b", "c"]
assert forecast["time"].to_list() == times
assert frame.columns == ["time", "id"]
assert predict("logistic", frame.lazy(), parameters).equals(forecast)
try:
predict("logistic", pl.DataFrame({"time": [0.0, 0.0]}), parameters)
except ValueError:
duplicate_time_rejected = True
else:
duplicate_time_rejected = False
assert duplicate_time_rejected, "The native contract must reject duplicate times."
print(
json.dumps(
{
"duplicate_time_rejected": duplicate_time_rejected,
"ids": forecast["id"].to_list(),
"predictions": [round(value, 6) for value in actual],
},
sort_keys=True,
)
)
if __name__ == "__main__":
main()

Read the checks in order. First compare Rust’s result with the analytic Logistic curve. Then verify row identity and unchanged input columns, compare eager and lazy input, and finally submit duplicate times to exercise the error contract. A failed check stops the script with a nonzero exit status.

The command prints this JSON after its assertions pass. CI compares the actual output with the same contract value displayed here:

{"duplicate_time_rejected": true, "ids": ["a", "b", "c"], "predictions": [26.894142, 50.0, 73.105858]}

If Python cannot import innovate._native, the native package has not been built into that interpreter. Return to the installation instructions. An assertion failure is evidence of a discrepancy; do not edit the expected values to match a failing implementation without investigating it.

The curve is L / (1 + exp(-k * (t - x0))). At t = x0 = 1, its level is exactly half of L = 100, hence the central prediction of 50. The other two values lie symmetrically around it. These are levels, not incident adoptions per time step. The independent analytic expression belongs to the example’s test oracle; Innovate’s scientific execution remains in Rust.

The input frame keeps its identifiers and ordering. A LazyFrame is collected at the native boundary. Arrow IPC currently copies buffers, so the example makes no claim of zero-copy transport.

Before reading the answers, use the displayed formula to answer these questions:

  1. Does the prediction of 50 at time 1 mean that 50 new adoptions occurred between times 0 and 1? Calculate the change over that interval.
  2. If you change the time unit from years to months, what values of k and x0 preserve the same curve? What happens to L?
  3. What is the instantaneous slope at the midpoint, and how do its units differ from the prediction column?
Compare your answers
  1. The prediction is a level. Its increase from time 0 to time 1 is approximately 50 − 26.894142 = 23.105858 adoption units.
  2. Multiply the time coordinates and x0 by 12, giving x0 = 12; divide k by 12, giving k = 1/12 per month. L = 100 retains the same adoption units. The dimensionless product k * (t − x0) remains unchanged.
  3. Differentiating the Logistic expression gives the midpoint slope L * k / 4 = 25 adoption units per year. This instantaneous derivative differs from the change accumulated over a whole year.

These are interpretation exercises. The executable example checks the supplied three-point curve; it does not run the modified time-unit exercise for you.

This example supplies parameters; it does not estimate them from observations, select a model, establish causal effects or calibrate predictive intervals. A numerical identity on three synthetic points does not validate the Logistic model for a real population. Time must be finite and strictly increasing. Passing this tutorial does not close the wider scientific migration ledger.

Continue with the current Python API and the native scientific conventions. The documentation quality contract explains what execution evidence does and does not establish.