Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow a Mixture of Degrees #41

Open
JoshuaC3 opened this issue Jul 25, 2019 · 8 comments
Open

Allow a Mixture of Degrees #41

JoshuaC3 opened this issue Jul 25, 2019 · 8 comments

Comments

@JoshuaC3
Copy link

I think it would be really helpful to allow a mixture of degrees for a model. This could be passed as a list or numpy array to the degree parameter. This has real-life applications: https://pdfs.semanticscholar.org/a6cf/634e8788586ba1d37c10c744db6a2f790109.pdf

In this scenario, a mixture of [0, 1] and [1, 0] degrees would be needed. However, it might be interesting to see if mixtures of higher degrees could be useful.

@cjekel
Copy link
Owner

cjekel commented Jul 26, 2019

I think this would be a nice addition! I won't be able to work on it any time soon though.

@JoshuaC3
Copy link
Author

I'll take a look at it and do a PR at some point.

@cjekel
Copy link
Owner

cjekel commented Oct 24, 2019

Hey, I just got an email about this. Going to try an implementation today. It will work easiest with degrees 1 and 0 since they have the same number of parameters.

@cjekel
Copy link
Owner

cjekel commented Oct 24, 2019

Check out the mixed_degree branch.

Documentation on setting degree as list

"""
        degree : int, list, optional
            The degree of polynomial to use. The default is degree=1 for
            linear models. Use degree=0 for constant models. Use a list for
            mixed degrees (only supports degrees 1 or 0). List should be read
            from left to right, degree=[1, 0, 1] corresponds to a mixed degree
            model, where the left most segment has degree=1, the middle
            segment degree=0, and the right most segment degree=1.
"""

Mixed degree example:

import numpy as np
import matplotlib.pyplot as plt
import pwlf

x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
y = [1, 2, 3, 4, 4.25, 3.75, 4, 5, 6, 7]

degree_list = [1, 0, 1]

my_pwlf = pwlf.PiecewiseLinFit(x, y, degree=degree_list)

breaks = my_pwlf.fit(3)

# generate predictions
x_hat = np.linspace(min(x), max(x), 1000)
y_hat = my_pwlf.predict(x_hat)

plt.figure()
plt.plot(x, y, 'o')
plt.plot(x_hat, y_hat)
plt.show()

Current issues:

  • Does not check that the number of degrees matches the number of line segments
  • Only supports degrees 1 and 0
  • Slopes are not calculated correctly
  • Needs unit tests

There are probably more issues with this...

The entire assemble_with_breaks should probably be rewritten to accommodate this feature in a cleaner manner.

@cjekel
Copy link
Owner

cjekel commented Oct 24, 2019

Here is the commit that added the changes: ef58aaa

@cyriltw
Copy link

cyriltw commented Oct 25, 2019

I've been playing around with the latest commit and with the code segment, but I keep getting an error. Its on python 3.6 and I tried converting it to an np array as well.

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-6-42eb67ecfacf> in <module>()
     11 degree_list = [1, 0, 1]
     12 
---> 13 my_pwlf = pwlf.PiecewiseLinFit(x, y, degree=degree_list)
     14 
     15 breaks = my_pwlf.fit(3)

/usr/local/lib/python3.6/dist-packages/pwlf/pwlf.py in __init__(self, x, y, disp_res, lapack_driver, degree)
    164         self.break_n = np.max(self.x_data)
    165 
--> 166         if degree < 12 and degree >= 0:
    167             # I actually don't know what the upper degree limit should
    168             self.degree = int(degree)

TypeError: '<' not supported between instances of 'list' and 'int'

@cjekel
Copy link
Owner

cjekel commented Oct 25, 2019

@cyriltw You'll need to install that experimental branch first.

git clone git@github.com:cjekel/piecewise_linear_fit_py.git
cd piecewise_linear_fit_py
git checkout mixed_degree
pip install . --force-reinstall --no-deps

@cyriltw
Copy link

cyriltw commented Oct 25, 2019

@cjekel you beat me to it, for some reason the branch wasn't checked out so causing the trouble. Now it works. Doing more testing. Thanks for including this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants