segreg.model.alt.brute_fit_two_bkpt

brute_fit_two_bkpt(indep, dep, num_end_to_skip=3, num_between_to_skip=4, dx=0.01, verbose=False, extra_verbose=False)

Estimate two-bkpt segmented regression model using a brute-force method.

This method is limited to univariate, continuous, linear, two-bkpt segmented regression problems. Estimates the parameters:

[u1, v1, u2, v2, m1, m2]

where

(u1,v1), (u2, v2) are the breakpoints (in x-y plane), ordered such that u1 < u2

m1 is the slope of the left-most segment

m2 is the slope of the right-most segment

Notes

This method picks the parameters giving the minimum RSS by brute-force calculation on a grid in the space of bkpt parameters, (u1, u2). That is, for each choice of bkpt pairs, the method computes the RSS for the segmented fit conditional on the bkpt pair. The parameters corresponding to the overall minimal RSS from these calculations are returned.

Parameters
  • indep (numpy array of shape (num_data,)) – The independent data. Also called predictor, explanatory variable, regressor, or exogenous variable.

  • dep (numpy array of shape (num_data,)) – The dependent data. Also called response, regressand, or endogenous variable.

  • num_end_to_skip (int) – Number of data points to skip at each end of the data when solving for the bkpts. As such, this determines a guaranteed minimum number of data points in the left and right segments in the returned fit. If None, defaults to the underlying implementation. TODO: explain

  • num_between_to_skip (int) – Number of data points to skip between the two bkpts (ie: the middle segment) when solving for the bkpts. Specifically, for each choice of left bkpt u1, will skip this many data points between u1 and u2. As such, this determines a guaranteed minimum number of data points between the bkpts in the returned fit.

  • dx (float) – The stepsize for the grid search.

  • verbose (bool) –

  • extra_verbose (bool) –

Examples

>>> import numpy as np
>>> from segreg.model.alt import brute_fit_two_bkpt
>>> indep = np.array([1,2,3,4,5,6,7,8,9,10,11,12,13,14])
>>> dep = np.array([1,2,3,4,5,4,3,2,1,0,1,2,3,4])
>>> brute_fit_two_bkpt(indep, dep)
(array([ 5.,  5., 10., -0.,  1.,  1.]), 0.0)
Returns

  • params (array of shape (num_params,)) – The estimated parameters. The returned parameters are, in order, [u, v, m1, m2].

  • rss (float) – Residual sum of squares of the fit.