rcrpy.FunctionalForm

class rcrpy.FunctionalForm(f, xdata, ydata, partials, guess, tol=1e-06, weights=None, error_y=None, priors=None, pivot_function=None, pivot_guess=None)[source]

Bases: object

Parametric model for RCR.

Phase 2 minimal constructor: FunctionalForm(f, xdata, ydata, partials, guess, tol=1e-6, weights=None, error_y=None). Matches the simplest of the C++’s 16 overloads (1D, optional weights, optional error bars, no priors, no pivot search).

Use with:

model = FunctionalForm(linear, xdata, ydata, [d1, d2], guess)
r = rcrpy.RCR(rcrpy.RejectionTech.LS_MODE_68)
r.set_parametric_model(model)
r.perform_rejection(ydata)
best = model.result.parameters
Parameters:
__init__(f, xdata, ydata, partials, guess, tol=1e-06, weights=None, error_y=None, priors=None, pivot_function=None, pivot_guess=None)[source]
Parameters:

Methods

__init__(f, xdata, ydata, partials, guess[, ...])

build_model_space([build_combos])

Port of cpp/src/FunctionalForm.cpp:799.

get_bestfit_errorbars(line)

Parameter standard errors from the post-fit Jacobian.

get_errors(line)

Port of cpp/src/FunctionalForm.cpp:2458 (and :2479 for ND).

get_nd_median()

Port of cpp/src/RCR.cpp:1142 (get2DMedian) and its 3D/ND cousins.

get_nd_mode()

Port of cpp/src/RCR.cpp:1063 (get2DMode) and its 3D/ND cousins.

handle_mu_tech_select([mu_tech])

Fit the model and return residuals. Dispatches by mu_tech:

regression()

Fit the model to the currently-flagged data via least squares.

set_true_vec(flags, y[, w])

Port of cpp/src/FunctionalForm.cpp:770 (and :735 weighted).

Attributes

pivot

pivot_ND

build_model_space(build_combos=False)[source]

Port of cpp/src/FunctionalForm.cpp:799.

Two responsibilities in the C++:
  1. Recompute the pivot point (if has_custom_pivot) from current flagged x / weights / parameters. Always done.

  2. Build parameterSpace / weightSpace from M-combinations of flagged data points, for use by get_*_median / get_*_mode. Only done when build_combos=True (caller passes this when mu_tech is MEDIAN or MODE).

Parameters:

build_combos (bool)

Return type:

None

get_bestfit_errorbars(line)[source]

Parameter standard errors from the post-fit Jacobian.

Port of cpp/src/FunctionalForm.cpp:2311, but using the standard nonlinear-least-squares covariance estimator

cov = inv(J^T J) * (sum_res² / (N - M)) unc[k] = sqrt(diag(cov)[k])

applied to the scaled Jacobian that scipy.optimize.least_squares returns. The C++ uses an unusual reduction in paramuncertainty that operates on only M of the N weight/sigma_y values (looks like an indexing bug); we substitute the textbook formula.

Returns an empty array if regression() hasn’t been called yet (mirroring the C++ behavior when no error bars/weights are set).

Parameters:

line (ndarray)

Return type:

ndarray

get_errors(line)[source]

Port of cpp/src/FunctionalForm.cpp:2458 (and :2479 for ND). Returns y - f(x, line) for currently-flagged points only.

Parameters:

line (ndarray)

Return type:

ndarray

get_nd_median()[source]

Port of cpp/src/RCR.cpp:1142 (get2DMedian) and its 3D/ND cousins. Returns the per-parameter weighted median across the current parameterSpace. Stores the result as the new meanstartingpoint for the subsequent generalized-mean call.

Return type:

ndarray

get_nd_mode()[source]

Port of cpp/src/RCR.cpp:1063 (get2DMode) and its 3D/ND cousins. Iteratively narrows each parameter dimension to its half-sample modal window, filtering combos to those within ALL dimensions’ windows, until the windows stabilize. Returns the weighted median of the final filtered combos per parameter.

Return type:

ndarray

handle_mu_tech_select(mu_tech='MEAN')[source]
Fit the model and return residuals. Dispatches by mu_tech:

MEAN → regression() (standard nonlinear least squares) MEDIAN → weighted median across the M-combination parameter space MODE → half-sample mode across the M-combination parameter space

The rejection loop calls this once per iteration. Ports cpp/src/RCR.cpp:5937 (handleMuTechSelect, no-arg version).

Parameters:

mu_tech (str)

Return type:

ndarray

pivot_ND: ndarray = array([], dtype=float64)
regression()[source]

Fit the model to the currently-flagged data via least squares.

Port of cpp/src/FunctionalForm.cpp:2382. The C++ dispatches to modifiedGN(f, partials, y, x, guess, tol, [w], [sigma_y]); we use scipy.optimize.least_squares which solves the same problem with a Trust Region Reflective algorithm by default.

Stashes _last_jacobian and _last_residual_count for the uncertainty computation in get_bestfit_errorbars.

Return type:

ndarray

set_true_vec(flags, y, w=None)[source]

Port of cpp/src/FunctionalForm.cpp:770 (and :735 weighted). Records the current flag mask + filtered y/w views; called once per rejection-loop iteration before regression().

Parameters:
Return type:

None