segreg.model.alt.fit_two_bkpt

fit_two_bkpt(indep, dep, num_end_to_skip=3, num_between_to_skip=4, verbose=False, optimize=True)

Estimate two-bkpt segmented regression model.

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

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.

  • verbose (bool) –

  • optimize (bool) – If True, will implement a few optimizations in the algorithm when appropriate.

Examples

>>> import numpy as np
>>> from segreg.model.alt import 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])
>>> 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, [u1, v1, u2, v2, m1, m2].

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