An open API service indexing awesome lists of open source software.

https://github.com/arkhipenko/runninglinreg

Implementation of dynamic mean squared error linear regression f(t)
https://github.com/arkhipenko/runninglinreg

Last synced: 6 months ago
JSON representation

Implementation of dynamic mean squared error linear regression f(t)

Awesome Lists containing this project

README

          

# Running Linreg

### Implementation of dynamic MSE linear regression f(t)
##### Version 1.0.0

### Description

`Running Linreg` (or Linear Regression) implements dynamic (and fast) recalculated mean squared error linear regression f(t) based on a pre-defined number of points.

Each time a new point is added to the set, the **slope** and **intercept** values are recalculated based on the added point values **only.**

### Detailed explanation

Imagine you are calculating the closest linear representation of a time series array of 200 points. As you continue measuring, new points are added on the right side, and every time a point is added on the right beyond initial 200, another point is dropped on the left. Thus you maintain a Running Array of measurement points, and linear regression characteristics of a line closely approximating the time-series. As only two points are changing at every measurement (the left-most is dropped and the right-most is added), the library does not need to recalculate slope/intercept based on all 200 points, just the effect of those two, hence the speed.

### Usage:

Construct the `RunningLinreg` object giving it a pointer to a `std::queue` object to hold `point_t` objects and anticipated size of the time-series array

```c++
RunningLinreg(std::queue* points, size_t n);
```

Use this method to add a point to the set:

```c++
addPoint(point_t point);
```

Then query recalculated **slope** and **intercept** values using methods:

```c++
double slope();
double intercept();
```

Methods

```c++
double count();
int16_t status();
```

could be used to query total number of points already added, and if errors were encountered by the last method call.

The point definition is:

```c++
typedef struct {
double t;
double value;
} point_t;
```

and a list of error codes is below:

```c++
#define LINREG_OK 0
#define LINREG_ERR (-1) // general error
#define LINREG_NUL (-2) // points pointer is NULL
#define LINREG_DEN (-3) // denominator is zero
```

### Practical applications

- Peak detection
- PID control

**Enjoy!**

###### Changelog:

v1.0.0:

- 2022-06-08 - initial release