-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUnivariateLinearRegression.cpp
94 lines (86 loc) · 3.12 KB
/
UnivariateLinearRegression.cpp
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <iostream>
namespace jagetiya
{
template <class T>
struct UnivariateLinearRegression
{
private:
static const int n_ = 1; // number of features
int m_; // number of training sets
double theta0_; // theta parameter 0
double theta1_; // theta parameter 1
double alpha_; // learning rate alpha
inline double cost_function(const T x[], const T y[]) const
{
double J = 0;
for(int i = 0 ; i < m_ ; i++)
{
J += (theta0_ + theta1_*x[i] - y[i]) *
(theta0_ + theta1_*x[i] - y[i]);
}
J = J / (2 * m_);
return J;
}
//repeat until convergence
void gradient_descent(const T x[], const T y[])
{
double res1 = 0;
double res2 = 0;
for(int i = 0 ; i < m_ ; i++)
{
res1 += (theta0_ + theta1_*x[i] - y[i]) / m_;
res2 += ((theta0_ + theta1_*x[i] - y[i]) * x[i]) / m_;
}
double temp0 = theta0_ - alpha_ * res1;
double temp1 = theta1_ - alpha_ * res2;
theta0_ = temp0;
theta1_ = temp1;
}
public:
UnivariateLinearRegression() = default;
UnivariateLinearRegression(int m = 10,
double theta0 = 0, double theta1 = 0, double alpha = 0.5)
: m_(m), theta0_(theta0), theta1_(theta1), alpha_(alpha)
{}
//train the system
double train(const T x[], const T y[])
{
double prev_result = cost_function(x, y);
gradient_descent(x, y);
double current_result = cost_function(x, y);
while( !(current_result - prev_result < 0.01
&& current_result - prev_result > -0.01))
{
gradient_descent(x, y);
prev_result = current_result;
current_result = cost_function(x, y);
std::cout << current_result <<std::endl;
}
return current_result;
}
double calculate_value(int x) { return theta0_ + theta1_ * x;}
};
}
int main()
{
int v = 1000000;
int *x = (int *) malloc(sizeof(int) * v);
int *y = (int *) malloc(sizeof(int) * v);
for(int i = 0 ; i < v ; i++)
{
x[i] = i+1;
y[i] = rand() % 100;
}
// set the alpha such as gradient-descent does not overshoot
jagetiya::UnivariateLinearRegression<int> t(v, 0, 0, 0.0000000000005);
t.train(x, y);
std::cout << x[0] << " " <<y[0] << " " <<t.calculate_value(x[0]) <<std::endl;
std::cout << x[1] << " " <<y[1] << " " <<t.calculate_value(x[1]) <<std::endl;
std::cout << x[2] << " " <<y[2] << " " <<t.calculate_value(x[2]) <<std::endl;
std::cout << x[3] << " " <<y[3] << " " <<t.calculate_value(x[3]) <<std::endl;
std::cout << x[4] << " " <<y[4] << " " <<t.calculate_value(x[4]) <<std::endl;
std::cout << x[5] << " " <<y[5] << " " <<t.calculate_value(x[5]) <<std::endl;
delete x;
delete y;
return 0;
}