mirror of
https://github.com/aicodix/dsp.git
synced 2026-04-27 14:30:36 +00:00
Compare commits
1 commit
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3f3572f377 |
1 changed files with 46 additions and 0 deletions
46
theil_sen.hh
46
theil_sen.hh
|
|
@ -6,6 +6,8 @@ Copyright 2021 Ahmet Inan <inan@aicodix.de>
|
|||
|
||||
#pragma once
|
||||
|
||||
#include <random>
|
||||
#include <functional>
|
||||
#include <algorithm>
|
||||
|
||||
namespace DSP {
|
||||
|
|
@ -52,5 +54,49 @@ public:
|
|||
}
|
||||
};
|
||||
|
||||
template <typename TYPE, int SIZE>
|
||||
class TheilSenEstimator2
|
||||
{
|
||||
TYPE temp_[SIZE];
|
||||
TYPE xint_, yint_, slope_;
|
||||
public:
|
||||
TheilSenEstimator2() : xint_(0), yint_(0), slope_(0) {}
|
||||
void compute(TYPE *x, TYPE *y, int LEN)
|
||||
{
|
||||
std::random_device rd;
|
||||
std::default_random_engine generator(rd());
|
||||
typedef std::uniform_int_distribution<int> uniform;
|
||||
auto rand = std::bind(uniform(0, LEN-1), generator);
|
||||
for (int i, j, k = 0; k < SIZE; ++k) {
|
||||
while (x[i=rand()] == x[j=rand()]);
|
||||
temp_[k] = (y[j] - y[i]) / (x[j] - x[i]);
|
||||
}
|
||||
std::nth_element(temp_, temp_+SIZE/2, temp_+SIZE);
|
||||
slope_ = temp_[SIZE/2];
|
||||
int count = std::min(LEN, SIZE);
|
||||
for (int i = 0; i < count; ++i)
|
||||
temp_[i] = y[i] - slope_ * x[i];
|
||||
std::nth_element(temp_, temp_+count/2, temp_+count);
|
||||
yint_ = temp_[count/2];
|
||||
xint_ = - yint_ / slope_;
|
||||
}
|
||||
TYPE xint()
|
||||
{
|
||||
return xint_;
|
||||
}
|
||||
TYPE slope()
|
||||
{
|
||||
return slope_;
|
||||
}
|
||||
TYPE yint()
|
||||
{
|
||||
return yint_;
|
||||
}
|
||||
TYPE operator () (TYPE x)
|
||||
{
|
||||
return yint_ + slope_ * x;
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue