cjekel / piecewise_linear_fit_py

fit piecewise linear data for a specified number of line segments
MIT License
289 stars 59 forks source link

multivariate pwlf? #8

Open qingzma opened 6 years ago

qingzma commented 6 years ago

Does this code support multivariate piecewise linear regression? Or is there any plan to support this?

cjekel commented 6 years ago

Multivariate (or high dimensional) piecewise regression can get complicated quickly.

I don't have any current plan for expanding pwlf to higher dimensions, but the conversation has come up before. There are some physical problems with insight that might be better described by a segmented regression problem.

I'm fairly committed at the moment, but I could implement some methods from http://www3.stat.sinica.edu.tw/statistica/oldpdf/A7n213.pdf at some future time

This brings up another point, should pwlf support non-continuous piecewise linear regression?

qingzma commented 6 years ago

I agree.... multivariate piecewise linear regression grow complicated very quickly, and I do not find any package to support this function yet. Anyway, thanks so much for the reply!

cjekel commented 4 years ago

I'm considering adding multivariate support following the style of a general additive model.

cjekel commented 4 years ago

Where is the code?

Branch multivariate has an initial multivariate pwlf model.

How this works

The final function follows the style of a general additive model (GAM), where 1D piecewise continuous fits are performed for each univariate feature. The 1D models can be assembled in polynomial form, with default multivariate_degree=1. Finally a least square solution provides the final parameters of the additive model.

Examples

I'm still working on the syntax, but the general idea will be something like this

"""
        x : ndarray (2-D)
            The x or independent data point as a 2 dimensional numpy array.
            This should be of shape (n, m), for n number of data points, and m
            number of features.
        y : array_like
            The y or dependent data point locations as list or 1 dimensional
            numpy array.
"""
n_segments = 2  # number of line segments for each univariate
my_mv_model = pwlf.PiecewiseMultivariate(x, y, n_segments,
                                         degree=1,  # degree of each univariate
                                         multivariate_degree=3  # degree of the GAM
)
my_mv_model.fit()

I believe these fits to the Branin-Hoo function shows some promise: https://github.com/cjekel/piecewise_linear_fit_py/blob/multivariate/examples/multivariate/braninhoo.py

Fits to the Adjiman function https://github.com/cjekel/piecewise_linear_fit_py/blob/multivariate/examples/multivariate/adjiman.py

To do

What this need in order to be merged into master:

Feel free to chime in for features, or comments about the api.