Git Product home page Git Product logo

Comments (3)

yihe-61wu avatar yihe-61wu commented on June 2, 2024

I did some simple test with 2-segment piecewise linear regression (unspecified breakpoints). I could output the breakpoints, which looked correct, and then I obtained yHat[0], (yHat[breakpointidx] - yHat[0]) / breakpointidx, (yHat[-1] - yHat[breakpointidx]) / (len(yHat) - breakpointidx), which should be expected to be very close to beta[0], beta[1], beta[2]. However, I could see the value of beta[-1] was different from (yHat[-1] - yHat[breakpointidx]) / (len(yHat) - breakpointidx) even by the sign.

from piecewise_linear_fit_py.

yihe-61wu avatar yihe-61wu commented on June 2, 2024

The source code appears all solid to me. I used fitfast but tested fit too -- both called fit_with_breaks, which altered beta via lstsq, seemingly a wrapper for scipy.linalg.lstsq.

I guess the issue is probably not a bug in the code, but in my interpretation. Any comments would be appreciated.

from piecewise_linear_fit_py.

cjekel avatar cjekel commented on June 2, 2024

beta[-1] can be negative! You just haven't had a curve with a steep enough slope yet at the end. The final slope needs to cancel out the accumulation of all previous slopes. Slopes on the right are the accumulation of all previous beta values (except the y-intercept aka. beta[0]).

See

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

# generate sin wave data
x = np.linspace(0, 11, num=100)
y = np.sin(x * np.pi / 2)
# add noise to the data
y = np.random.normal(0, 0.05, 100) + y

my_pwlf_1 = pwlf.PiecewiseLinFit(x, y, degree=1)

# fit the data for four line segments
res1 = my_pwlf_1.fitfast(5, pop=50)

# predict for the determined points
xHat = np.linspace(min(x), max(x), num=10000)
yHat1 = my_pwlf_1.predict(xHat)

print('beta', my_pwlf_1.beta)

# plot the results
plt.figure()
plt.plot(x, y, 'o', label='Data')
plt.plot(xHat, yHat1, '--', label='degree=1')
plt.legend()
plt.show()

beta [ 1.06781215 -0.5918096 1.87834207 -2.49662666 2.37475738 -2.42097531]

image

from piecewise_linear_fit_py.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.