Skip to content
RgramImplementation

Implementation guide

This guide covers every package module and the responsibilities of its helpers. The supported user interface is the API reference. Private names, internal state and module functions can change without being additional public estimator features. Source links on API pages expose the implementation.

Package map

Module Responsibility
rgram.__init__ Top-level exports in __all__; importing does not load Matplotlib.
rgram.rgram Regressogram configuration, bin selection, aggregation and ordered prediction joins.
rgram.smoothing Kernel shapes, local coefficients, algorithm selection and query diagnostics.
rgram.base Shared input validation, snapshots, names, warnings, diagnostics and bootstrap delegation.
rgram.aggregation Named reductions, numeric/native adapters, quantile helper and expression resolution.
rgram.model_selection Serial candidate/fold evaluation, coverage eligibility and refit.
rgram.uncertainty Paired resampling and interval calculations.
rgram.plotting Optional Matplotlib diagnostic panels.
rgram.warnings Filterable advisory categories.
rgram._typing Shared input, output, callback and CV annotations.

Estimator internals

Regressogram

_validate_parameters enforces the declared configuration and callback contracts. _cv_bin_count clones the model and uses CoverageSearchCV when n_bins="cv". _learn_bin_params learns feature bounds, counts, quantiles/widths and merged boundaries. _predict_bin_expr maps queries to those fitted groups with each strategy's equality/edge convention. _prediction_frame validates queries, joins group summaries without reordering, and applies range/support policies.

fit snapshots the selected columns and computes native aggregations in Polars. Numeric adapters run directly on group arrays in Python, so their exceptions remain normal Python exceptions rather than crossing a Rust callback boundary.

KernelSmoother

_positive and _validate_parameters validate configuration. _check_fitted uses the sklearn fitted-state contract. _normalized_kernel calculates each shape, includes observation weights, preserves exact zeros, and uses log-scale evaluation for infinite-support built-ins. _resolve_algorithm chooses support windows only for already sorted training features and compact kernels. _kernel_rows yields bounded dense blocks or exact local windows; it never sorts a dataset or index.

_prediction_weights solves the local-linear moments, choosing the explicit singular policy when needed. _handle_unsupported applies NaN/error policy. _evaluate coordinates range policy, coefficients, predictions, support counts, and optional dense inspection weights. _KERNELS and _COMPACT_KERNELS declare supported names and the kernels eligible for windows. Public inspection uses bandwidth_.

CoverageSearchCV

fit validates explicit fold indices, clones each candidate in each fold, accumulates pooled squared error and support, rejects ineligible candidates, and refits the first best candidate. __sklearn_is_fitted__ checks for a winner, so cv_results_ alone does not make a failed search fitted.

Shared utilities

The private BaseUtils class supplies implementation helpers, not a standalone user estimator. Validation accepts arrays or selected pandas/Polars columns, materializes a single snapshot, rejects invalid rows, and preserves dtype/order. Public methods inherited by estimators are documented on their API pages. The complete utility reference below includes private helpers for contributors.

rgram.base.BaseUtils

[source]
class rgram.base.BaseUtils(

)

Shared validation and inspection methods for univariate regressors.

Methods

Method Purpose
predict_interval Compute pointwise paired-bootstrap confidence intervals for the curve.
regression_diagnostics Inspect observed values, fitted values, and residuals in input order.

predict_interval

[source]
rgram.base.BaseUtils.predict_interval(
    X,
    *,
    confidence_level=0.95,
    n_resamples=200,
    random_state=None,
    method='percentile',
    resampling='iid',
    groups=None,
    block_size=None,
    min_valid_fraction=1.0,
)

Compute pointwise paired-bootstrap confidence intervals for the curve.

Parameters

X array-like of shape (n_queries,) or (n_queries, 1)

Query locations; order and duplicates are preserved.

confidence_level float, default=0.95

Nominal interval level strictly between zero and one.

n_resamples int, default=200

Bootstrap refits, at least two. More draws improve Monte Carlo stability.

random_state int or None, default=None

Seed for resampling, independent of any estimator CV seed.

method {'percentile', 'basic'}, default='percentile'

Bootstrap quantiles, or quantiles reflected around the original estimate.

resampling {'iid', 'groups', 'blocks'}, default='iid'

Resample paired independent rows, whole independent groups, or contiguous moving blocks in original training order. No sorting occurs.

groups array-like of shape (n_samples_in_,) or None, default=None

Nonmissing group labels, required only for group resampling. At least two distinct groups are required.

block_size int or None, default=None

Block length from 2 to n_samples_in_ - 1, required only for blocks.

min_valid_fraction float, default=1.0

Fraction of draws that must support each query, in (0, 1]. At least two valid draws are always required. Lower fractions condition on valid draws.

Returns

interval polars.DataFrame

Columns: x, prediction, lower, upper, n_valid, bootstrap_coverage. Insufficiently supported bounds are NaN. Queries are never dropped.

Raises

sklearn.exceptions.NotFittedError

If no successful fit is available.

ValueError

For invalid options, fewer than two training rows, invalid group/block inputs, or errors from the estimator's strict fit/prediction policies.

Notes

These are curve intervals, not prediction intervals or simultaneous bands. IID draws assume independent random-design rows. Group and block draws require appropriate independent groups or ordered dependence. Smoothing settings are held fixed; bin boundaries are relearned. Selection uncertainty and smoothing bias are not corrected. Memory includes n_resamples * n_queries draw values. A draw with no positive observation weight counts as unsupported. Other fit errors propagate. Explicit neighbor-only algorithms can reject unsorted draws.

regression_diagnostics

[source]
rgram.base.BaseUtils.regression_diagnostics(X, y)

Inspect observed values, fitted values, and residuals in input order.

Parameters

X array-like of shape (n_samples,) or (n_samples, 1)

One evaluation feature. Named inputs must match the fitted feature.

y array-like of shape (n_samples,) or (n_samples, 1)

Observed response, aligned with x. May be training or held-out data.

Returns

diagnostics polars.DataFrame

Columns: x, observed, prediction, residual (observed minus prediction), supported (finite prediction). Unsupported rows remain present.

Raises

sklearn.exceptions.NotFittedError

If no successful fit is available.

ValueError

For invalid, misaligned inputs or strict prediction policies.

Aggregation and resampling internals

The named reduction tuple AGGREGATIONS contains mean, median, min, max, sum, count, std, var, first, and last. Aggregation is the union of declared names, native-expression functions, numeric adapters, and weighted-expression adapters. Public adapter classes and their calls are covered in the aggregation reference.

rgram.aggregation.aggregation_expr

[source]
rgram.aggregation.aggregation_expr(
    aggregation,
    values,
    weights=None,
)

Resolve a named/native reduction into a Polars expression.

This is an implementation helper; users should configure Regressogram.agg. Numeric adapters run directly per bin and are rejected by this resolver.

Parameters

aggregation str or callable

Declared native aggregation or expression adapter.

values polars.Expr

Bin-response expression.

weights polars.Expr or None, default=None

Observation-weight expression. Only mean/sum names or explicit weighted adapters accept weights.

Returns

expression polars.Expr

One declared scalar reduction. Invalid callback types/signatures raise.

Resampling implementation.

bootstrap_interval fixes the selected smoother bandwidth (including its adjustment), or the fitted bin count, before drawing replicas. Explicit bin width stays explicit. Bin boundaries still refit. Draw storage is dense in n_resamples × n_queries. Strict fit/prediction errors are preserved.

Shared type annotations

Name in rgram._typing Meaning
Array, FloatArray General NumPy array and float64 computation/result array.
Input Column-name string, array-like, Polars Series or DataFrame.
Frame Pandas/Polars DataFrame or Polars LazyFrame for named selection.
Prediction One-dimensional float64 prediction array.
NumericReducer One- or two-array callback returning a scalar.
ExpressionReducer Polars expression-to-expression callback.
WeightedReducer Two-expression callback returning a reduction expression.
Splitter Protocol with split(X, y=None, groups=None) yielding train/test indices.
CV Integer, splitter, or iterable of train/test index pairs.

These annotations do not add support for arbitrary multivariate or sparse data. Actual validation contracts are in the estimator reference.

Examples and validation

examples/kernel_explorer.py builds interactive Matplotlib controls through main, with update and show_interval callbacks. It generates ordered synthetic observations; interval calculation is explicitly triggered. examples/benchmark_neighbors.py times fit and query paths separately and verifies agreement. Neither module runs its main routine on import. The examples page documents their execution and intended interpretation.

tests/ contains regression, numerical, property, integration, and performance checks. scripts/check_docs.py checks documentation coverage and rendered local links; scripts/check_examples.py executes guide snippets and public doctests; it is a development tool, not an installed library API. See development instructions for build/test commands and publishing.

examples/plot_relationships.py supplies nine independent gallery functions and a CLI to show them one at a time or save separate large figures. Shared helpers provide consistent axes, observations, legends, and smoother configuration. The documentation example check draws every gallery figure headlessly.

Pandas conversion

BaseUtils._is_pandas recognizes actual DataFrame/Series objects and subclasses through the already loaded optional pandas module. Rgram's conversion helpers do not import pandas; the dependency remains optional. _as_array converts complete numeric pandas columns to NumPy, including nullable numeric dtypes, without index alignment or integer rounding. _pandas_frame converts only selected, uniquely labeled columns into the internal Polars snapshot. It does not call pl.from_pandas or require Arrow. The same array normalization serves fitting, queries, targets, and weights.