Skip to main content

Library for Fast Segmented Regression

Project description

Python API

This describes the Python API of the MVSR-project. Consider reading the general documentation.

If you use this project to analyze you data, consider citing our paper.

Installation

The easiest way is to use the provided, precompiled python packages from PyPi using pip or uv. Alternatively These packages are also available on the release page. We provide these packages for all common systems, namely ARM (aarch64) and x86 (x86_64) for Linux, Windows and MacOS.

If your system is not supported, you will have to build the library yourself. The only needed dependency is numpy (optionally matplotlib for convenient plotting).

Usage

There are two APIs, 'mvsr' (high-level) and 'libmvsr' (low-level). The low-level API is not documented, but it closely resembles the exported C-API from the core library. The direct usage of the low-level API is discouraged, the following documentation will focus on the high-level API.

Mainly only one function (mvsr) is used. The following snippet shows a minimal working example.

import mvsr # import the library

regression = mvsr.mvsr(
    [1,2,3,4,5,6],   # predictor values (x) of samples (one dimension)
    [1,2,3,8,7,6],   # response values (y) of samples (one variant)
    2                # number of desired segments (k)
)

y = regression(4.5)       # calculate predicted response value at x=4.5,
print(f'f(4.5) = {y}')    # should be 7.5
                                        
for segment in regression:              # iterate over segments

    (x_start, x_end) = segment.range    # get the x range of the segment
    print(f'Segment in range {x_start} <= x <= {x_end}:')

    y = segment(4.5)                    # calculate what this segment would
    print(f'  f_seg(4.5) = {y}')        # predict for x = 4.5 prints(4.5 and 7.5)

The mvsr function computes the piecewise predictor function and returns a Regression object to access this information. It enables iterating over segments, iterating over variants, predicting values for other predictor values and also easy pretty plotting, using matplotlib.

The functions to handle the regression object are listed in the API Reference, a practical use case is shown in the plotting example.

Parameters

This is a short formal description of the interactions of all functions parameters to the main function. The first three arguments were already used in the last snipped: the predictor values (x), the response values (y) and the desired amount of segments (k).

Predictor Values (x)

This is always a sorted list of predictor values of an arbitrary type. These values are preprocessed by a kernel object. By default the 'Poly' kernel is used to enable piecewise linear regression. This behavior can be changed by changing the kernel to 'Poly(0)' (piecewise constant), 'Poly(d)' (piecewise polynomial of degree 'd') or 'Raw' (provide your own Vandermonde-Matrix). You can even provide a custom kernel by yourself.

The Poly Kernel also supports multiple input dimensions. This is achieved by supplying an iterable type (e.g. a list) for each input sample. Notice that some functionality (e.g. calculating a response value) relies on being able to compare the predictor values. If you do not want to implement a comparison function for you type, consider using the sortkey parameter.

Response Values (y)

This parameter is an iterable (e.g. a list) of numbers. There must be exactly one response value for each predictor value.

It is also possible to provide a list of response value lists. All these response value lists must contain the same amount of values. This results in a mulit-variant segmented regression (in simple terms, there are multiple response values for every single predictor value, see the plotting example). All variants share the same breakpoints, i.e. their segments start and end at the same positions. If the variants should not share the breakpoint positions, it is recommended to conduct a dedicated segmented regression for each variant.

Segment Count (k)

The amount of desired output segments. Future versions will enable automatic deduction of this parameter.

Named Parameters

Named parameters must be provided by their names directly.

kernel: This enables to use an own kernel object. Its 'model_interpolation' parameter also enables tweaking the behavior in between the segments. Provided values are 'Interpolate.closest', 'Interpolate.left', 'Interpolate.right', 'Interpolate.linear' and 'Interpolate.smooth', but you can also provide an own function to interpolate between neighboring segment models (see the plotting example).

algorithm: This defines how the regression algorithm works. There are two options, a very fast and accurate heuristic ('Algorithm.GREEDY') and a dynamic program ('Algorithm.DP'). Most of the time the heuristic is recommended, especially for large sample sets, since its accuracy increases in these cases. However, the dynamic program guarantees to find the solution with the smallest error relative to the provided sample set. In some use cases with few samples this can be worth the high resource consumption. Notice that compute time and amount of memory needed for DP scale much worse with a growing number of samples. If not amount of samples, but the compute time is the limiting factor, we always recommend using more samples with the greedy approach instead of less samples with the dynamic program. For an in-depth analysis and an explanation of the algorithms see this research paper. Per default the algorithm is chosen based on the number of input samples, effective input dimensions, and the desired amount segments.

score: This parameter defines how the amount of segments is determined, if [parameter k](#Segment Count) is not uniquely defined. It is not yet supported.

normalize: Defines whether the data is normalized (see feature scaling). If True, min-max normalization is performed on the response values before the regression is calculated. The resulting regression models are denormalized in the result, making this process completely transparent. This option is recommended if multiple variants are used, by default it is only enabled for mult-variant regression.

weighting: This parameter is an iterable type, containing as many values as there are variants (output dimensions). It multiplies the response values with the corresponding weight after normalization (if applied). As a result, minimizing the error of a variant with a higher weight becomes more important. The placement of the breakpoints depends more heavily on the variants with higher weights. The resulting models are unweighted, making this process transparent, similar to normalization. It is not recommended to apply weighting without normalization. By default weighting for every variant is '1.0'.

dtype: A numpy type for the underlying regression calculation. Currently we support 'numpy.float32' and 'numpy.float64'. These two types are the only two exported by the underlying C-API at this point in time. Depending on your hardware 'numpy.float32' can be faster, but is less accurate. If you want to use a custom type for the underlying calculation, consider using the low-level C++ API directly.

keepdims: Defines whether an array will be returned as a response value if only one variant is in use. It can be useful when doing additional calculations with the response value(s). Notice that this value will internally be always set to False if you use the 'Regression.variants' property, since the resulting objects can never contain multiple variants. By default this parameter is 'False'.

sortkey: A callable (e.g. a function) taking one predictor value as an input. It is used to be able to handle complex, non-comparable types as predictor values. The function must return a value that can be compared. This is necessary in order to determine which segment a predictor value belongs to. The predictor value list must also be in sorted order relative to the resulting key, otherwise the behavior is undefined. By default this value is 'None' and behaves as if the input value is returned.

Advanced Usage

In order to implement custom data preprocessing, it is necessary to understand the order of the processing steps. The graphic below displays this dataflow.

flowchart TD;
    X["X (Predictors)"]-->Regression;
    X-->Preprocessing-->LibMvsr["**LibMvsr**"];
    Y["Y (Responses)"]-->Regression;

    Y-->YNorm;
    subgraph KO["Kernel Object"];
    subgraph MV["Multivariant Operation"];
    YNorm[Variant Normalization]-->YWeighting["Variant Weighting"];
    Unweighting["Variant Unweighting"]-->Denorm["Variant Denormalization"];
    end
    Preprocessing;
    Preprocessing2["Preprocessing"];
    Interpolation;
    end

    LibMvsr-->Breakpoints-->Regression;
    LibMvsr-->Models-->Unweighting;
    Denorm-->Regression;
    YWeighting-->LibMvsr;

    Regression-->|predict| Preprocessing2-->|between segments| Interpolation-->Segment;
    Preprocessing2-->|within segment| Segment;

Interpolate

Predicting useful values between segments depends on many attributes, like the used data type, data source, continuity of the function at the breakpoint, etc. To enable a higher level of versatility, the implemented kernels implement a default interpolation functionality. The 'model_interpolation' parameter is a function with two input parameters, a single predictor value and a list of (normally two) neighboring segments. The return value of this function should be a list defining a weighting value for each of the input segments at the given predictor value. Normally these values should add up to 1.0. The following functions are already implemented:

Interpolate.closest: This is the default behavior for Raw Kernels. It always uses the segment that is closest to the predictor value. To determine the closest segment, the Euclidean distance to all samples is measured, and the segment of the closest sample is used.

Interpolate.left: This function always uses the left segment until the next one begins.

Interpolate.right: This function always uses the right segment until the next one begins.

Interpoalte.linear: This function implements lerping between between two segment models.

Interpolate.smooth: This is a typical function for continuous transition between two segments. It is based on a cubic function ($3x^2-2x^3$) and is comparable to known ease-in, ease-out smoothing functions. It is similar to linear interpolation (lerp), but less harsh at the edges of the segments.

You can also provide an own function. This is demonstrated in the plotting example.

Notice, that the Poly kernel is also able to use a lerp function, but by default will instead linearly interpolate between the last sample of the previous segment and the first sample of the next one. Such functionality can be implemented with a custom kernel.

Custom Kernel

There are multiple use cases to implement an own Kernel. For example:

  1. Special predictor value data type: If you use a data type that can not easily be extended, needs special preprocessing or can not be assigned a suitable sortkey function, you can implement your own data preprocessing.
  2. Custom normalization: If you need a special form of normalization, you can implement your own 'normalize' and 'denormalize' functions.
  3. Custom interpolation: If a special form of interpolation is needed for you use case, which is not achievable with a 'model_interpolate' function, you can provide an own 'interpolate' function. This is e.g. done by the Poly Kernel to enable linear interpolation between two samples.

If you decide to implement your own kernel function, it is strongly recommended to inherit an existing kernel. The Poly kernel is itself inheriting the behavior of the Raw kernel. This enables reusing the existing features.

The Raw kernel implements the model interpolation functionality (by passing through the 'model_interpolation' parameter), min-max normalization and denormalization (by defining the x-dimension that is subject to translation, in addition to the scaling), and preprocessing of a Vandermonde matrix. The Poly kernel additionally implements data preprocessing for polynomial regression and a special interpolation between the samples.

Manual Builds

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

mvsr-0.1.2.tar.gz (24.0 kB view details)

Uploaded Source

Built Distributions

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

mvsr-0.1.2-py3-none-win_arm64.whl (72.8 kB view details)

Uploaded Python 3Windows ARM64

mvsr-0.1.2-py3-none-win_amd64.whl (88.1 kB view details)

Uploaded Python 3Windows x86-64

mvsr-0.1.2-py3-none-manylinux2014_x86_64.whl (202.1 kB view details)

Uploaded Python 3

mvsr-0.1.2-py3-none-manylinux2014_aarch64.whl (191.3 kB view details)

Uploaded Python 3

mvsr-0.1.2-py3-none-macosx_15_6_x86_64.whl (37.9 kB view details)

Uploaded Python 3macOS 15.6+ x86-64

mvsr-0.1.2-py3-none-macosx_15_6_arm64.whl (32.7 kB view details)

Uploaded Python 3macOS 15.6+ ARM64

File details

Details for the file mvsr-0.1.2.tar.gz.

File metadata

  • Download URL: mvsr-0.1.2.tar.gz
  • Upload date:
  • Size: 24.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for mvsr-0.1.2.tar.gz
Algorithm Hash digest
SHA256 5d109c2e4a019ee3e13dd8153a32b88d55659faeda4ab4fe72f8acd7c58c6cdf
MD5 c272092052da2ddb738debc36b057e1b
BLAKE2b-256 efd7ada1150ec0cdb668649453cdceee60fa817267c8036abe260b5ab5962ad5

See more details on using hashes here.

Provenance

The following attestation bundles were made for mvsr-0.1.2.tar.gz:

Publisher: release.yaml on Loesgar/mvsr

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

File details

Details for the file mvsr-0.1.2-py3-none-win_arm64.whl.

File metadata

  • Download URL: mvsr-0.1.2-py3-none-win_arm64.whl
  • Upload date:
  • Size: 72.8 kB
  • Tags: Python 3, Windows ARM64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for mvsr-0.1.2-py3-none-win_arm64.whl
Algorithm Hash digest
SHA256 2aef32abb29ec69b4428bd1d75f9bdbc0e9e83bc610af1ecc92de3df10e99d6b
MD5 eda43cc0da299f6d60a99937486f43fc
BLAKE2b-256 03eb4ac2bf7ca5d0580ea2209c48e555232af70d86f61e89599757adddc48c85

See more details on using hashes here.

Provenance

The following attestation bundles were made for mvsr-0.1.2-py3-none-win_arm64.whl:

Publisher: release.yaml on Loesgar/mvsr

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

File details

Details for the file mvsr-0.1.2-py3-none-win_amd64.whl.

File metadata

  • Download URL: mvsr-0.1.2-py3-none-win_amd64.whl
  • Upload date:
  • Size: 88.1 kB
  • Tags: Python 3, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for mvsr-0.1.2-py3-none-win_amd64.whl
Algorithm Hash digest
SHA256 9b1592d0fdbb3cbf6adbca1db1f8cbd1abbeaba2a58bb49b79409425556f0bc7
MD5 5f181ef056223fbf6e884677279e6aa4
BLAKE2b-256 66d121cec16b59d02bd74dc5ca97a76ef8f1f4a266ec44b5bb9797eceb55ee60

See more details on using hashes here.

Provenance

The following attestation bundles were made for mvsr-0.1.2-py3-none-win_amd64.whl:

Publisher: release.yaml on Loesgar/mvsr

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

File details

Details for the file mvsr-0.1.2-py3-none-manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for mvsr-0.1.2-py3-none-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 2cfe0863498edf527823df976f86ee05cded62f4c5376b215a10aff425a72ab1
MD5 21fd5ca14677168024557c3a30640a00
BLAKE2b-256 4bc01ef1bf40b72c20d56498c04abc8e10a2500e81c2a0a44e9a8adba1a3aa08

See more details on using hashes here.

Provenance

The following attestation bundles were made for mvsr-0.1.2-py3-none-manylinux2014_x86_64.whl:

Publisher: release.yaml on Loesgar/mvsr

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

File details

Details for the file mvsr-0.1.2-py3-none-manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for mvsr-0.1.2-py3-none-manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 6b13c9c2166bc7e5b77ff233c0b41f450ef206e84e2e5e2074788977e5d8ebc6
MD5 f6a7e8f6f7b18fc6fb380eaf6085def8
BLAKE2b-256 d49dd58774a0c823a9989f11888e7c46d4df8453fc0e14a99d21cbcedc1d8a7f

See more details on using hashes here.

Provenance

The following attestation bundles were made for mvsr-0.1.2-py3-none-manylinux2014_aarch64.whl:

Publisher: release.yaml on Loesgar/mvsr

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

File details

Details for the file mvsr-0.1.2-py3-none-macosx_15_6_x86_64.whl.

File metadata

  • Download URL: mvsr-0.1.2-py3-none-macosx_15_6_x86_64.whl
  • Upload date:
  • Size: 37.9 kB
  • Tags: Python 3, macOS 15.6+ x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for mvsr-0.1.2-py3-none-macosx_15_6_x86_64.whl
Algorithm Hash digest
SHA256 f59ae5a4075a079ae768e014352f17985dbaed2ea7c72740139a7d719da9177d
MD5 70040df57d5cbafcd9edc9e0df7b1ddf
BLAKE2b-256 d7607af5a73e08efd8442350e8d4f4f8ba0ca27ab7aafd10073bb95502b22a37

See more details on using hashes here.

Provenance

The following attestation bundles were made for mvsr-0.1.2-py3-none-macosx_15_6_x86_64.whl:

Publisher: release.yaml on Loesgar/mvsr

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

File details

Details for the file mvsr-0.1.2-py3-none-macosx_15_6_arm64.whl.

File metadata

  • Download URL: mvsr-0.1.2-py3-none-macosx_15_6_arm64.whl
  • Upload date:
  • Size: 32.7 kB
  • Tags: Python 3, macOS 15.6+ ARM64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for mvsr-0.1.2-py3-none-macosx_15_6_arm64.whl
Algorithm Hash digest
SHA256 7f5370e75ba4c2e4b7d52f57c355791b74450dd20c4062d9245288863720c5c5
MD5 131253372523c62270ab116215809a41
BLAKE2b-256 55f7538a3f40d8f0e2d62455a2a88b5a3a6a487c14c6755a4830d8b05bdf9d08

See more details on using hashes here.

Provenance

The following attestation bundles were made for mvsr-0.1.2-py3-none-macosx_15_6_arm64.whl:

Publisher: release.yaml on Loesgar/mvsr

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