forked from burakbayramli/books
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsupfser.py
34 lines (33 loc) · 916 Bytes
/
supfser.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
import numpy as np
from numpy.linalg import pinv
def supf(y, x, p):
T = y.shape[0]
range = np.floor(np.array([T * p, T * (1 - p)]))
range = np.arange(range[0], range[1] + 1, dtype=np.int32)
x = x - np.mean(x)
y = y - np.mean(y)
b = pinv(x).dot(y)
e = y - x.dot(b)
R2_r = 1 - e.dot(e) / y.dot(y)
k = x.shape[1]
F_stat = np.zeros(T)
for t in range:
X1 = x[:t]
X2 = x[t:]
b = pinv(X1).dot(y[:t])
e[:t] = y[:t] - X1.dot(b)
b = pinv(X2).dot(y[t:])
e[t:] = y[t:] - X2.dot(b)
R2_u = 1 - e.dot(e) / y.dot(y)
F_stat[t] = ((R2_u - R2_r) / k) / ((1 - R2_u) / (T - k))
print F_stat.argmax()
return F_stat.max()
reps = 100
T = 200
k = 1
p = 0.2
sup_F_stat = np.zeros(reps)
for j in xrange(reps):
y = np.random.standard_normal(T)
x = np.random.standard_normal((T,k))
sup_F_stat[j] = supf(y, x, p)