forked from ik2sb/PICS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sim_lib_regression.py
executable file
·65 lines (46 loc) · 1.48 KB
/
sim_lib_regression.py
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
import sim_lib_common as clib
from scipy import stats
import numpy as np
import math
def build_regression_equation (coefs):
eq = "Eq(X) = "
maxnum = len(coefs)
j = maxnum
for x in xrange (maxnum):
j -= 1
if x != 0:
eq += " + "
eq += str(round(coefs[x],2))
if j > 1:
eq += "*X^"+str(j)
elif j == 1:
eq += "*X"
return eq
#X, Y should be list
def regression (degree, x, y, value):
if degree == 1:
return linear_regression (x, y, value)
elif degree == 2:
return quadratic_regression (x, y, value)
elif degree == 3:
return cubic_regression (x, y, value)
else:
print "[Regress_] Regression Lib does not support %d degree regression" % degree
clib.sim_exit()
def linear_regression (x, y, value):
slope, intercept, r_value, p_value, std_err = stats.linregress(x,y)
result = int(math.ceil(slope * value + intercept))
coefs = [slope, intercept]
return result, coefs
def quadratic_regression (x, y, value):
x1 = np.array(x)
y1 = np.array(y)
coefs = np.polyfit(x1, y1, 2)
result = int(math.ceil(coefs[0] * value**2 + coefs[1] * value + coefs[2]))
return result, coefs
def cubic_regression (x, y, value):
x1 = np.array(x)
y1 = np.array(y)
coefs = np.polyfit(x1,y1,3)
result = int(math.ceil(coefs[0] * value ** 3 + coefs[1] * value ** 2 + coefs[2] * value + coefs[3]))
return result, coefs