Skip to content

Commit caed535

Browse files
committed
add the recursice_least_square
1 parent 637b69a commit caed535

2 files changed

Lines changed: 99 additions & 2 deletions

File tree

examples/python_test.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,25 @@
11
import os
22
import numpy as np
33
import matplotlib.pyplot as plt
4+
import numpy.matlib
45

56
e = np.array([1, 2, 3])
67
f = np.array([2, 3, 4])
8+
p = np.matlib.eye(3, 3)
9+
#print("the fist rows:", p[0,:])
10+
print ("the type of p:", type(p))
11+
g = np.zeros(3)
12+
print("the g:", g)
13+
#h = np.matrix.ye(3)
14+
15+
print("the_res: ", p * f.transpose())
16+
17+
718
a = np.append(e[1:3], f,axis = 0)
819
m = np.array([[1,2,3], [2,2,3], [2,3,4]])
920
print("the inverse:", type(np.linalg.inv(m)))
10-
g = np.vdot(e, f.transpose())
11-
print("the g: ", g)
21+
#g = np.vdot(e, f.transpose())
22+
print("the g: ", e.transpose() * f)
1223

1324
print("the a: ", a)
1425

examples/recursive_least_square.py

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import os
2+
import numpy as np
3+
import matplotlib.pyplot as plt
4+
import numpy.matlib
5+
6+
#the least_square
7+
a = np.array([1, -1.5, 0.7]); b = np.array([1, 0.5]); d = 3 #
8+
9+
len_a = len(a) -1; len_b = len(b) - 1 ; L =400
10+
Input_init = np.zeros(d + len_b) #the init_value of input
11+
Output_init = np.zeros(len_a) # the init_value of ouput
12+
13+
14+
white_noise = np.random.normal(0, 1, L) # the white_noise
15+
white_noise_1 = np.sqrt(0.1) * np.random.normal(0, 1, L)
16+
17+
theta = np.append(a[1: len_a + 1], b) #the permeter of object
18+
print("the shape of theat:", np.shape(theta))
19+
20+
theta_1 = np.zeros(len_a + len_b + 1)
21+
print("the theta_1:", np.shape(theta_1))
22+
P = np.eye(len_a + len_b + 1, len_a + len_b + 1)
23+
24+
phi = np.array([]) # the
25+
y = np.array([])
26+
U = np.array([]) #the inverse_M_sequence
27+
thetae_1 = []
28+
thetae_2 = []
29+
thetae_3 = []
30+
thetae_4 = []
31+
32+
33+
for i in range(L):
34+
# Y = AX + xi(noise)
35+
temp = np.append(-Output_init, Input_init[d - 1: d + len_b]) # X
36+
temp_y = np.dot(temp, theta.transpose()) + white_noise_1[i] # Y
37+
38+
phi = np.concatenate((phi, temp))
39+
#print("the phi: ", phi)
40+
temp_matrix = np.matrix(temp)
41+
print("the shape of temp_matrix:", np.shape(temp_matrix))
42+
P_matrix = np.matrix(P)
43+
print("the shape of temp*P*temp:", np.shape(temp_matrix * P_matrix *temp_matrix.transpose()))
44+
K = (P_matrix * temp_matrix.transpose()) /(1 + temp_matrix * P_matrix *temp_matrix.transpose())
45+
print("the shape of K: ", np.shape(K))
46+
thetae_temp = theta_1 + K * (temp_y - temp * theta_1)
47+
print("the shape of theta_1", np.shape(theta_1))
48+
print("the shape of theate_temp:", np.shape(thetae_temp))
49+
50+
print("the value of theta_temp[0,0]:", thetae_temp[0,0])
51+
52+
thetae_1.append(thetae_temp[0,0])
53+
thetae_2.append(thetae_temp[1,1])
54+
thetae_3.append(thetae_temp[2,2])
55+
thetae_4.append(thetae_temp[3,3])
56+
P = (np.eye(len_a + len_b + 1) - K * temp) * P
57+
58+
#update the data
59+
theta_1 = thetae_temp
60+
61+
for k in range(d + len_b - 1, 0, -1):
62+
Input_init[k] = Input_init[k - 1]
63+
Input_init[0] = white_noise[i]
64+
65+
for j in range(len_a - 1, 0, -1):
66+
Output_init[j] = Output_init[j - 1]
67+
Output_init[0] = temp_y
68+
69+
# the np.narray to np.matrix
70+
71+
plt.figure(1)
72+
ax = plt.subplot(1, 1, 1)
73+
74+
plt.sca(ax)
75+
ax.plot(thetae_1, 'Red', label = 'thetae_1')
76+
ax.plot(thetae_2, 'Blue', label = 'thetae_2')
77+
ax.plot(thetae_3, 'Green', label = 'thetae_3')
78+
ax.plot(thetae_4, 'Yellow', label = 'thetae_4')
79+
plt.title('thetae')
80+
#plt.xlabel("L")
81+
plt.ylabel("thetae_1")
82+
83+
ax.legend()
84+
plt.show()
85+
86+

0 commit comments

Comments
 (0)