npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

segreg

v0.5.6

Published

Piecewise regression in JavaScript

Readme

piecewise

This repo accompanies Piecewise regression: when one line simply isn't enough, a blog post about Datadog's approach to piecewise regression. The code included here is intended to be minimal and readable; this is not a Swiss Army knife to solve all variations of piecewise regression problems.

This is a TypeScript/JavaScript implementation of the piecewise regression algorithm.

Installation & dependencies

npm install segreg

Or clone this repo and build from source:

git clone https://github.com/cmcnulty/segreg.git
cd segreg
npm install
npm run build

The package has no runtime dependencies. Development dependencies include TypeScript, Jest, and ts-jest for testing.

Usage

Start by preparing your data as arrays of timestamps (independent variables) and values (dependent variables).

import { piecewise } from 'segreg';

// Generate sample data
const t = Array.from({ length: 10 }, (_, i) => i);
const v = [
  ...Array.from({ length: 5 }, (_, i) => 2 * i),
  ...Array.from({ length: 5 }, (_, i) => 10 - i)
].map(val => val + (Math.random() - 0.5) * 2); // Add some noise

Now, you're ready to fit a piecewise linear regression.

const model = piecewise(t, v);

model is a FittedModel object. You can inspect the fitted segments to see their domains and regression coefficients.

console.log(model.segments.length); // Number of segments
console.log(model.segments[0]);
// FittedSegment {
//   start_t: 0,
//   end_t: 5,
//   coeffs: { intercept: -0.857, slope: 2.224 }
// }

You can access the coefficients and domain information from each segment:

const segment = model.segments[0];
console.log(segment.start_t);          // Starting t value
console.log(segment.end_t);            // Ending t value
console.log(segment.coeffs.intercept); // y-intercept
console.log(segment.coeffs.slope);     // Slope

Advanced Features

Intersection Detection

The library includes utilities for detecting and snapping segment intersections:

import { findIntersection, adjustSegmentsToSnapIntersections } from 'segreg';

const model = piecewise(t, v);

// Find intersection between consecutive segments
if (model.segments.length >= 2) {
  const intersection = findIntersection(
    model.segments[0],
    model.segments[1],
    0.05 // snap radius ratio (5% of bounding box diagonal)
  );

  if (intersection.intersects) {
    console.log(`Intersection at t=${intersection.point.t}, y=${intersection.point.y}`);
  }
}

// Adjust all segments to snap to their intersections
const adjustedSegments = adjustSegmentsToSnapIntersections(model.segments, 0.05);

Custom Stopping Criteria

You can adjust the min_stop_frac parameter to control when the algorithm stops merging segments:

// More aggressive merging (fewer segments)
const model1 = piecewise(t, v, 0.05);

// Less aggressive merging (more segments)
const model2 = piecewise(t, v, 0.01);