Skip to main content

Regression spline

Project description

Regression splines

This module includes two spline implementations splines suitable for regression: linear splines using a hinge function basis, and natural cubic splines.

import numpy as np
import matplotlib.pyplot as plt
from regspline import LinearSpline
plt.close('all')

knots = [0,1,2]
coeffs = [1,2,3]
s = LinearSpline(knots, coeffs)
y=s(np.linspace(0,1))

x=np.linspace(0,np.pi)
y=np.sin(x)
xobs = np.repeat(x,50)
yobs = np.repeat(y,50) + 0.01*np.random.randn(*xobs.shape)

s, res = LinearSpline.from_data(xobs, yobs,
                                knots=np.linspace(0,np.pi,30),
                                method='OLS',
                                return_estim_result=True,
                                prune=True)

plt.plot(x,y)
plt.plot(x,s(x))

Several regression types are supported to extract the splines from data, including OLS, LASSO, and quantile regression. See the example files.

Installation

You can install this library directly from github:

pip install git+https://github.com/mvds314/regspline.git

There are two optional dependencies: scikit-learn, and cvxopt. They are only required to estimate splines on data with, respectively, support vector regressions, and LASSO.

Background

The module contains two splines:

  • A linear spline represented by Hinge functions: $h_i(x) = \max(x-k_i,0)$, where $k_i$ are the knots.
  • A natural cubic spline.

The splines chosen:

  • have coefficents that have a one-to-one correspondence with the knots.
  • have the ability that knots can be removed, e.g., when the corresponding coefficient is small or insignificant, without changing the basis functions corresponding to other knots.
  • have the ability to represent functions with sparse basis.

One way to interpret, e.g., the linear spline in the hings basis is as follows. $h_1(x)$ sets an initial slope from the first knot onwards. Then next basis function $h_2(x)$ can adjust the slope at the knot $k_2$, if no adjustment is required, its coefficient is insignificant and the knot can be removed from the spline without any impact on the other basis functions.

Related projects

Some projects with related methods:

The module differs from these implementations as it implements the splines as functions, and they are not integrated within an estimation framework.

Development

For development purposes, clone the repo:

git clone https://github.com/mvds314/regspline.git

Then navigate to the folder containing setup.py and run

pip install -e .

to install the package in edit mode.

Run unittests with pytest.

Install the optional dependencies to test all functionality.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

regspline-25.5.2.tar.gz (11.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

regspline-25.5.2-py3-none-any.whl (13.1 kB view details)

Uploaded Python 3

File details

Details for the file regspline-25.5.2.tar.gz.

File metadata

  • Download URL: regspline-25.5.2.tar.gz
  • Upload date:
  • Size: 11.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for regspline-25.5.2.tar.gz
Algorithm Hash digest
SHA256 651962923d003d023ccc017bd6e976fa177e883ccd1f9e92874dfb2d8ec8fdde
MD5 da35e7bdfdfb646772efab0c5fc65752
BLAKE2b-256 51ed3ea47a0b7e453e3d98df1a3ae4cf736033c578823a4e1198b59d56194837

See more details on using hashes here.

Provenance

The following attestation bundles were made for regspline-25.5.2.tar.gz:

Publisher: python-publish.yml on mvds314/regspline

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file regspline-25.5.2-py3-none-any.whl.

File metadata

  • Download URL: regspline-25.5.2-py3-none-any.whl
  • Upload date:
  • Size: 13.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for regspline-25.5.2-py3-none-any.whl
Algorithm Hash digest
SHA256 0b4a6a76629432ba27a089af30437eb86922ab21b73682b39218769503b13786
MD5 825f2e11be44b6d65fd128fc7f2b46bc
BLAKE2b-256 45d9d4c50b502ac4d0760458bb4b4654b708afcbdb7ef96a4cc24733ba6d8da0

See more details on using hashes here.

Provenance

The following attestation bundles were made for regspline-25.5.2-py3-none-any.whl:

Publisher: python-publish.yml on mvds314/regspline

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page