-
Notifications
You must be signed in to change notification settings - Fork 14
/
theil_sen.hh
54 lines (47 loc) · 1008 Bytes
/
theil_sen.hh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/*
Theil–Sen estimator
Copyright 2021 Ahmet Inan <[email protected]>
*/
#pragma once
#include "quick.hh"
namespace DSP {
template <typename TYPE, int LEN_MAX>
class TheilSenEstimator
{
static const int size_ = ((LEN_MAX-1)*LEN_MAX)/2;
TYPE temp_[size_];
TYPE xint_, yint_, slope_;
public:
TheilSenEstimator() : xint_(0), yint_(0), slope_(0) {}
void compute(const TYPE *x, const TYPE *y, int LEN)
{
int count = 0;
for (int i = 0; count < size_ && i < LEN; ++i)
for (int j = i+1; count < size_ && j < LEN; ++j)
if (x[j] != x[i])
temp_[count++] = (y[j] - y[i]) / (x[j] - x[i]);
slope_ = quick_select(temp_, count/2, count);
count = 0;
for (int i = 0; count < size_ && i < LEN; ++i)
temp_[count++] = y[i] - slope_ * x[i];
yint_ = quick_select(temp_, count/2, count);
xint_ = - yint_ / slope_;
}
TYPE xint()
{
return xint_;
}
TYPE slope()
{
return slope_;
}
TYPE yint()
{
return yint_;
}
TYPE operator () (TYPE x)
{
return yint_ + slope_ * x;
}
};
}