import rpy2
import rpy2.robjects as ro
from rpy2.robjects.vectors import FloatVector
from rpy2.robjects.packages import importr
import torch
import numpy as np
from tqdm import tqdm
import torch.nn.functional as F
from torch_geometric_temporal.nn.recurrent import GConvGRU
import matplotlib.pyplot as plt
import pandas as pd
import time
from scipy.interpolate import interp1d
GCN Algorithm Example 1
ST-GCN
GNAR
Our method; GNAR Dataset Example(fiveVTS, fiveNet)
import
class RecurrentGCN(torch.nn.Module):
def __init__(self, node_features, filters):
super(RecurrentGCN, self).__init__()
self.recurrent = GConvGRU(node_features, filters, 2)
self.linear = torch.nn.Linear(filters, 1)
def forward(self, x, edge_index, edge_weight):
= self.recurrent(x, edge_index, edge_weight)
h = F.relu(h)
h = self.linear(h)
h return h
R
%load_ext rpy2.ipython
%%R
library(GNAR) library(igraph)
시간측정 방법
= time.time()
t1for epoc in range(1000):
## 1
= net(x)
yhat ## 2
= loss_fn(yhat,y)
loss ## 3
loss.backward()## 4
optimizr.step()
optimizr.zero_grad()= time.time()
t2 -t1 t2
EbayesThresh
%%R
library(EbayesThresh)set.seed(1)
= rnorm(1000)
epsilon = sample(c(runif(25,-2,-1.5), runif(25,1.5,2), rep(0,950)))
signal = which(signal!=0)
index_of_trueoutlier =signal+epsilon x_ebayes
%R -o x_ebayes
%R -o index_of_trueoutlier
%R -o signal
%R -i
= np.array([1,2,3]) arr
%R -i arr
%%R
arr
[1] 1 2 3
= importr('EbayesThresh').ebayesthresh ebayesthresh
= index_of_trueoutlier outlier_true_index
= x_ebayes[index_of_trueoutlier] outlier_true_value
= signal.copy() outlier_true_one
= list(map(lambda x: -1 if x!=0 else 1,outlier_true_one)) outlier_true_one
0. 데이터 가정
데이터가정: missing (결측값) 이 있는 자료를 가정
- missing이 있는상태: ST-GCN 을 사용할 수 X \(\to\) 코드에 에러는 나지 않지만 yhat이 산출되지 않았습니다.
- missing이 없어야만: ST-GCN 을 사용할 수 O
예제 1 GNAR fiveVT
증명 - missing이 있는상태: ST-GCN 을 사용할 수 X
%%R
summary(fiveNet)
GNARnet with 5 nodes and 10 edges
of equal length 1
%%R
<- as.matrix(fiveNet)
edges "fiveNode") data(
%R -o fiveVTS
%R -o edges
- node: 5
- time 200
= torch.tensor(edges) edges_tensor
= edges_tensor.nonzero() nonzero_indices
= np.array(nonzero_indices).T fiveNet_edge
데이터 일부 missing 처리
1) Block 처리
[1] ST-GCN
%%R
<- fiveVTS
fiveVTS0 50:150, 3] <- NA fiveVTS0[
2]) plt.plot(fiveVTS0[:,
= 200
T = 5 # number of Nodes
N = fiveNet_edge
E = np.array([1,2,3,4,5])
V = np.arange(0,T)
t = 1 node_features
= torch.tensor(fiveVTS0).reshape(200,5,1).float() f
= f[:199,:,:]
X = f[1:,:,:] y
= torch.tensor(E)
edge_index = torch.tensor(np.array([1,1,1,1,1,1,1,1,1,1]),dtype=torch.float32) edge_attr
= enumerate(zip(X,y)) _ee
= RecurrentGCN(node_features=1, filters=4)
model
= torch.optim.Adam(model.parameters(), lr=0.01)
optimizer
model.train()
for epoch in tqdm(range(50)):
for time, (xt,yt) in enumerate(zip(X,y)):
= model(xt, edge_index, edge_attr)
y_hat = torch.mean((y_hat-yt)**2)
cost
cost.backward()
optimizer.step() optimizer.zero_grad()
100%|██████████| 50/50 [00:32<00:00, 1.53it/s]
= torch.stack([model(xt, edge_index, edge_attr) for xt in X]).detach().numpy() yhat
0].data)
plt.plot(yhat[:,1].data)
plt.plot(yhat[:,2].data)
plt.plot(yhat[:,3].data) plt.plot(yhat[:,
2) Random missing values
%%R
set.seed(1)
<- fiveVTS
fiveVTSrandom = sort(sample(1:200, 100))
sampleindex 3] <- NA fiveVTSrandom[sampleindex,
%R -o fiveVTSrandom
%R -o sampleindex
2],'o') plt.plot(fiveVTSrandom[:,
3) By 2
%%R
<- fiveVTS
fiveVTStwo <- rep(seq(1, by = 2, 200))
indextwo 3] <- NA fiveVTStwo[indextwo,
%R -o fiveVTStwo
%R -o indextwo
2],'o') plt.plot(fiveVTStwo[:,
1. missing을 채움. (mean, linear interpolation)
1.1. Mean
1) Block
= fiveVTS0.copy() fiveVTS0_mean
49:150,2] = np.mean(fiveVTS0[:49,2].tolist()+fiveVTS0[150:,2].tolist()) fiveVTS0_mean[
2]) plt.plot(fiveVTS0_mean[:,
2) Random missing values
= fiveVTSrandom.copy() fiveVTSrandom_mean
= pd.DataFrame(fiveVTSrandom[:,2])
df = df.mean() # finds the mean value of the column A
mean_value = df.fillna(mean_value) # replace missing values with the mean value df
2] = np.array(df).reshape(200,) fiveVTSrandom_mean[:,
2]) plt.plot(fiveVTSrandom_mean[:,
3) By 2
= fiveVTStwo.copy() fiveVTStwo_mean
= pd.DataFrame(fiveVTStwo[:,2])
df = df.mean() # finds the mean value of the column A
mean_value = df.fillna(mean_value) # replace missing values with the mean value df
2] = np.array(df).reshape(200,) fiveVTStwo_mean[:,
2]) plt.plot(fiveVTStwo_mean[:,
1.2. linear interpolation
1) Block
= fiveVTS0.copy() fiveVTS0_linearinterpolation
# Sample data points
= np.array([48,150])
x = np.array([fiveVTS0_linearinterpolation[48,2],fiveVTS0_linearinterpolation[150,2]])
y
# Create interpolating function
= interp1d(x, y, kind='linear')
f
# Estimate y value for x = 2.5
= f(range(49,150)) y_interp
49:150,2] = y_interp fiveVTS0_linearinterpolation[
2]) plt.plot(fiveVTS0_linearinterpolation[:,
2) Random missing values
= fiveVTSrandom.copy() fiveVTSrandom_linearinterpolation
= pd.DataFrame(fiveVTSrandom_linearinterpolation[:,2])
_df ='linear', inplace=True)
_df.interpolate(method= _df.fillna(0) _df
2] = np.array(_df).reshape(200,) fiveVTSrandom_linearinterpolation[:,
2]) plt.plot(fiveVTSrandom_linearinterpolation[:,
3) By 2
= fiveVTStwo.copy() fiveVTStwo_linearinterpolation
= pd.Series(fiveVTStwo_linearinterpolation[:,2])
_df ='linear', inplace=True)
_df.interpolate(method= _df.fillna(0) _df
2] = _df fiveVTStwo_linearinterpolation[:,
2]) plt.plot(fiveVTStwo_linearinterpolation[:,
2. ST-GCN 을 사용하여 fhat을 구함. (스무딩1)
= 200
T = 5 # number of Nodes
N = fiveNet_edge
E = np.array([1,2,3,4,5])
V = np.arange(0,T)
t = 1 node_features
= torch.tensor(E)
edge_index = torch.tensor(np.array([1,1,1,1,1,1,1,1,1,1]),dtype=torch.float32) edge_attr
GNAR이랑 비교 - MSE 등 평가지표
2.1. Mean
1) Block
= torch.tensor(fiveVTS0_mean).reshape(200,5,1).float() f_mean
= f_mean[:199,:,:]
X_mean = f_mean[1:,:,:] y_mean
= RecurrentGCN(node_features=1, filters=4)
model
= torch.optim.Adam(model.parameters(), lr=0.01)
optimizer
model.train()
for epoch in tqdm(range(50)):
for time, (xt,yt) in enumerate(zip(X_mean,y_mean)):
= model(xt, edge_index, edge_attr)
y_hat = torch.mean((y_hat-yt)**2)
cost
cost.backward()
optimizer.step() optimizer.zero_grad()
100%|██████████| 50/50 [00:32<00:00, 1.56it/s]
= torch.stack([model(xt, edge_index, edge_attr) for xt in X_mean]).detach().numpy() fhat_mean
2].data) plt.plot(fhat_mean[:,
2) Random missing values
= torch.tensor(fiveVTSrandom_mean).reshape(200,5,1).float() f_fiveVTSrandom_mean
= f_fiveVTSrandom_mean[:199,:,:]
X_fiveVTSrandom_mean = f_fiveVTSrandom_mean[1:,:,:] y_fiveVTSrandom_mean
= RecurrentGCN(node_features=1, filters=4)
model
= torch.optim.Adam(model.parameters(), lr=0.01)
optimizer
model.train()
for epoch in tqdm(range(50)):
for time, (xt,yt) in enumerate(zip(X_fiveVTSrandom_mean,y_fiveVTSrandom_mean)):
= model(xt, edge_index, edge_attr)
y_hat = torch.mean((y_hat-yt)**2)
cost
cost.backward()
optimizer.step() optimizer.zero_grad()
100%|██████████| 50/50 [00:32<00:00, 1.55it/s]
= torch.stack([model(xt, edge_index, edge_attr) for xt in X_fiveVTSrandom_mean]).detach().numpy() fhat_fiveVTSrandom_mean
2].data) plt.plot(fhat_fiveVTSrandom_mean[:,
3) By 2
= torch.tensor(fiveVTStwo_mean).reshape(200,5,1).float() f_fiveVTStwo_mean
= f_fiveVTStwo_mean[:199,:,:]
X_fiveVTStwo_mean = f_fiveVTStwo_mean[1:,:,:] y_fiveVTStwo_mean
= RecurrentGCN(node_features=1, filters=4)
model
= torch.optim.Adam(model.parameters(), lr=0.01)
optimizer
model.train()
for epoch in tqdm(range(50)):
for time, (xt,yt) in enumerate(zip(X_fiveVTStwo_mean,y_fiveVTStwo_mean)):
= model(xt, edge_index, edge_attr)
y_hat = torch.mean((y_hat-yt)**2)
cost
cost.backward()
optimizer.step() optimizer.zero_grad()
100%|██████████| 50/50 [00:32<00:00, 1.54it/s]
= torch.stack([model(xt, edge_index, edge_attr) for xt in X_fiveVTStwo_mean]).detach().numpy() fhat_fiveVTStwo_mean
2].data) plt.plot(fhat_fiveVTStwo_mean[:,
2.2. linear interpolation
1) Block
= torch.tensor(fiveVTS0_linearinterpolation).reshape(200,5,1).float() f_linearinterpolation
= f_linearinterpolation[:199,:,:]
X_linearinterpolation = f_linearinterpolation[1:,:,:] y_linearinterpolation
= RecurrentGCN(node_features=1, filters=4)
model
= torch.optim.Adam(model.parameters(), lr=0.01)
optimizer
model.train()
for epoch in tqdm(range(50)):
for time, (xt,yt) in enumerate(zip(X_linearinterpolation,y_linearinterpolation)):
= model(xt, edge_index, edge_attr)
y_hat = torch.mean((y_hat-yt)**2)
cost
cost.backward()
optimizer.step() optimizer.zero_grad()
100%|██████████| 50/50 [00:32<00:00, 1.55it/s]
= torch.stack([model(xt, edge_index, edge_attr) for xt in X_linearinterpolation]).detach().numpy() fhat_linearinterpolation
2].data) plt.plot(fhat_linearinterpolation[:,
2) Random missing values
= torch.tensor(fiveVTSrandom_linearinterpolation).reshape(200,5,1).float() f_fiveVTSrandom_linearinterpolation
= f_fiveVTSrandom_linearinterpolation[:199,:,:]
X_fiveVTSrandom_linearinterpolation = f_fiveVTSrandom_linearinterpolation[1:,:,:] y_fiveVTSrandom_linearinterpolation
= RecurrentGCN(node_features=1, filters=4)
model
= torch.optim.Adam(model.parameters(), lr=0.01)
optimizer
model.train()
for epoch in tqdm(range(50)):
for time, (xt,yt) in enumerate(zip(X_fiveVTSrandom_linearinterpolation,y_fiveVTSrandom_linearinterpolation)):
= model(xt, edge_index, edge_attr)
y_hat = torch.mean((y_hat-yt)**2)
cost
cost.backward()
optimizer.step() optimizer.zero_grad()
100%|██████████| 50/50 [00:32<00:00, 1.55it/s]
= torch.stack([model(xt, edge_index, edge_attr) for xt in X_fiveVTSrandom_linearinterpolation]).detach().numpy() fhat_fiveVTSrandom_linearinterpolation
2].data) plt.plot(fhat_fiveVTSrandom_linearinterpolation[:,
3) By 2
= torch.tensor(fiveVTStwo_linearinterpolation).reshape(200,5,1).float() f_fiveVTStwo_linearinterpolation
= f_fiveVTSrandom_linearinterpolation[:199,:,:]
X_fiveVTStwo_linearinterpolation = f_fiveVTSrandom_linearinterpolation[1:,:,:] y_fiveVTStwo_linearinterpolation
= RecurrentGCN(node_features=1, filters=4)
model
= torch.optim.Adam(model.parameters(), lr=0.01)
optimizer
model.train()
for epoch in tqdm(range(50)):
for time, (xt,yt) in enumerate(zip(X_fiveVTStwo_linearinterpolation,y_fiveVTStwo_linearinterpolation)):
= model(xt, edge_index, edge_attr)
y_hat = torch.mean((y_hat-yt)**2)
cost
cost.backward()
optimizer.step() optimizer.zero_grad()
100%|██████████| 50/50 [00:32<00:00, 1.55it/s]
= torch.stack([model(xt, edge_index, edge_attr) for xt in X_fiveVTStwo_linearinterpolation]).detach().numpy() fhat_fiveVTStwo_linearinterpolation
2].data) plt.plot(fhat_fiveVTStwo_linearinterpolation[:,
2.3. 원래 f
= torch.tensor(fiveVTS).reshape(200,5,1).float() f
= f[:199,:,:]
X = f[1:,:,:] y
= RecurrentGCN(node_features=1, filters=4)
model
= torch.optim.Adam(model.parameters(), lr=0.01)
optimizer
model.train()
for epoch in tqdm(range(50)):
for time, (xt,yt) in enumerate(zip(X,y)):
= model(xt, edge_index, edge_attr)
y_hat = torch.mean((y_hat-yt)**2)
cost
cost.backward()
optimizer.step() optimizer.zero_grad()
100%|██████████| 50/50 [00:32<00:00, 1.55it/s]
= torch.stack([model(xt, edge_index, edge_attr) for xt in X]).detach().numpy() fhat_fiveVTS
2].data) plt.plot(fhat_fiveVTS[:,
3. 2에서 얻은 fhat을 이용하여 그래프퓨리에변환+Ebayesthresh (스무딩2)
- 그래프퓨리에변환을 하는 가중치
- 년도끼리 이어주어서 하나의 큰 그래프로 만든뒤에 GFT
- Ebayesthresh
3.1. Mean
3.1.1. Temporal
1) Block
=np.zeros((5,199,199)) w
for k in range(5):
for i in range(199):
for j in range(199):
if i==j :
= 0
w[k,i,j] elif np.abs(i-j) <= 1 :
= 1 w[k,i,j]
= np.array([w[i].sum(axis=1) for i in range(5)])
d = np.array([np.diag(d[i]) for i in range(5)])
D= np.array([np.diag(1/np.sqrt(d[i])) @ (D[i]-w[i]) @ np.diag(1/np.sqrt(d[i])) for i in range(5)])
L = np.linalg.eigh(L)[0],np.linalg.eigh(L)[1]
lamb, Psi = np.array([np.diag(lamb[i]) for i in range(5)])
Lamb = np.hstack([Psi[i] @ fhat_mean[:,i] for i in range(5)])
fhatbar = fhatbar.reshape(5,199)
_fhatbar = _fhatbar**2
power = importr('EbayesThresh').ebayesthresh
ebayesthresh =np.array([np.array(ebayesthresh(FloatVector(_fhatbar[i]**2))) for i in range(5)])
power_threshed= np.where(power_threshed>0,_fhatbar,0)
fhatbar_threshed = np.array([Psi[i] @ fhatbar_threshed[i] for i in range(5)])
fhatbarhat = fhatbarhat.reshape(199,-1) fhatbarhat_mean_temporal
0])
plt.plot(fhatbarhat_mean_temporal[:,1])
plt.plot(fhatbarhat_mean_temporal[:,2])
plt.plot(fhatbarhat_mean_temporal[:,3])
plt.plot(fhatbarhat_mean_temporal[:,4]) plt.plot(fhatbarhat_mean_temporal[:,
2) Random missing values
=np.zeros((5,199,199)) w
for k in range(5):
for i in range(199):
for j in range(199):
if i==j :
= 0
w[k,i,j] elif np.abs(i-j) <= 1 :
= 1 w[k,i,j]
= np.array([w[i].sum(axis=1) for i in range(5)])
d = np.array([np.diag(d[i]) for i in range(5)])
D= np.array([np.diag(1/np.sqrt(d[i])) @ (D[i]-w[i]) @ np.diag(1/np.sqrt(d[i])) for i in range(5)])
L = np.linalg.eigh(L)[0],np.linalg.eigh(L)[1]
lamb, Psi = np.array([np.diag(lamb[i]) for i in range(5)])
Lamb = np.hstack([Psi[i] @ fhat_fiveVTSrandom_mean[:,i] for i in range(5)])
fhatbar = fhatbar.reshape(5,199)
_fhatbar = _fhatbar**2
power = importr('EbayesThresh').ebayesthresh
ebayesthresh =np.array([np.array(ebayesthresh(FloatVector(_fhatbar[i]**2))) for i in range(5)])
power_threshed= np.where(power_threshed>0,_fhatbar,0)
fhatbar_threshed = np.array([Psi[i] @ fhatbar_threshed[i] for i in range(5)])
fhatbarhat = fhatbarhat.reshape(199,-1) fhatbarhat_random_mean_temporal
0])
plt.plot(fhatbarhat_random_mean_temporal[:,1])
plt.plot(fhatbarhat_random_mean_temporal[:,2])
plt.plot(fhatbarhat_random_mean_temporal[:,3])
plt.plot(fhatbarhat_random_mean_temporal[:,4]) plt.plot(fhatbarhat_random_mean_temporal[:,
3) By 2
=np.zeros((5,199,199)) w
for k in range(5):
for i in range(199):
for j in range(199):
if i==j :
= 0
w[k,i,j] elif np.abs(i-j) <= 1 :
= 1 w[k,i,j]
= np.array([w[i].sum(axis=1) for i in range(5)])
d = np.array([np.diag(d[i]) for i in range(5)])
D= np.array([np.diag(1/np.sqrt(d[i])) @ (D[i]-w[i]) @ np.diag(1/np.sqrt(d[i])) for i in range(5)])
L = np.linalg.eigh(L)[0],np.linalg.eigh(L)[1]
lamb, Psi = np.array([np.diag(lamb[i]) for i in range(5)])
Lamb = np.hstack([Psi[i] @ fhat_fiveVTStwo_mean[:,i] for i in range(5)])
fhatbar = fhatbar.reshape(5,199)
_fhatbar = _fhatbar**2
power = importr('EbayesThresh').ebayesthresh
ebayesthresh =np.array([np.array(ebayesthresh(FloatVector(_fhatbar[i]**2))) for i in range(5)])
power_threshed= np.where(power_threshed>0,_fhatbar,0)
fhatbar_threshed = np.array([Psi[i] @ fhatbar_threshed[i] for i in range(5)])
fhatbarhat = fhatbarhat.reshape(199,-1) fhatbarhat_twomean_temporal
0])
plt.plot(fhatbarhat_twomean_temporal[:,1])
plt.plot(fhatbarhat_twomean_temporal[:,2])
plt.plot(fhatbarhat_twomean_temporal[:,3])
plt.plot(fhatbarhat_twomean_temporal[:,4]) plt.plot(fhatbarhat_twomean_temporal[:,
3.1.2. Spatio
1) Block
=np.zeros((5,5)) w
for i in range(5):
for j in range(5):
if i==j :
= 0
w[i,j] elif np.abs(i-j) <= 1 :
= 1 w[i,j]
= np.array(w.sum(axis=1))
d = np.diag(d)
D = np.array(np.diag(1/np.sqrt(d)) @ (D-w) @ np.diag(1/np.sqrt(d)))
L = np.linalg.eigh(L)
lamb, Psi = np.diag(lamb)
Lamb = Psi @ fhat_mean.reshape(5,199)
fhatbar = fhatbar**2
power = importr('EbayesThresh').ebayesthresh
ebayesthresh =np.array([np.array(ebayesthresh(FloatVector(fhatbar[i]**2))) for i in range(5)])
power_threshed= np.where(power_threshed>0,fhatbar,0)
fhatbar_threshed = Psi @ fhatbar_threshed
fhatbarhat = fhatbarhat.reshape(199,-1) fhatbarhat_mean_spatio
0])
plt.plot(fhatbarhat_mean_spatio[:,1])
plt.plot(fhatbarhat_mean_spatio[:,2])
plt.plot(fhatbarhat_mean_spatio[:,3])
plt.plot(fhatbarhat_mean_spatio[:,4]) plt.plot(fhatbarhat_mean_spatio[:,
2) Random missing values
=np.zeros((5,5)) w
for i in range(5):
for j in range(5):
if i==j :
= 0
w[i,j] elif np.abs(i-j) <= 1 :
= 1 w[i,j]
= np.array(w.sum(axis=1))
d = np.diag(d)
D = np.array(np.diag(1/np.sqrt(d)) @ (D-w) @ np.diag(1/np.sqrt(d)))
L = np.linalg.eigh(L)
lamb, Psi = np.diag(lamb)
Lamb = Psi @ fhat_fiveVTSrandom_mean.reshape(5,199)
fhatbar = fhatbar**2
power = importr('EbayesThresh').ebayesthresh
ebayesthresh =np.array([np.array(ebayesthresh(FloatVector(fhatbar[i]**2))) for i in range(5)])
power_threshed= np.where(power_threshed>0,fhatbar,0)
fhatbar_threshed = Psi @ fhatbar_threshed
fhatbarhat = fhatbarhat.reshape(199,-1) fhatbarhat_random_mean_spatio
0])
plt.plot(fhatbarhat_random_mean_spatio[:,1])
plt.plot(fhatbarhat_random_mean_spatio[:,2])
plt.plot(fhatbarhat_random_mean_spatio[:,3])
plt.plot(fhatbarhat_random_mean_spatio[:,4]) plt.plot(fhatbarhat_random_mean_spatio[:,
3) By 2
=np.zeros((5,5)) w
for i in range(5):
for j in range(5):
if i==j :
= 0
w[i,j] elif np.abs(i-j) <= 1 :
= 1 w[i,j]
= np.array(w.sum(axis=1))
d = np.diag(d)
D = np.array(np.diag(1/np.sqrt(d)) @ (D-w) @ np.diag(1/np.sqrt(d)))
L = np.linalg.eigh(L)
lamb, Psi = np.diag(lamb)
Lamb = Psi @ fhat_fiveVTStwo_mean.reshape(5,199)
fhatbar = fhatbar**2
power = importr('EbayesThresh').ebayesthresh
ebayesthresh =np.array([np.array(ebayesthresh(FloatVector(fhatbar[i]**2))) for i in range(5)])
power_threshed= np.where(power_threshed>0,fhatbar,0)
fhatbar_threshed = Psi @ fhatbar_threshed
fhatbarhat = fhatbarhat.reshape(199,-1) fhatbarhat_two_mean_spatio
0])
plt.plot(fhatbarhat_two_mean_spatio[:,1])
plt.plot(fhatbarhat_two_mean_spatio[:,2])
plt.plot(fhatbarhat_two_mean_spatio[:,3])
plt.plot(fhatbarhat_two_mean_spatio[:,4]) plt.plot(fhatbarhat_two_mean_spatio[:,
3.1.3. Spatio-Temporal
1) Block
=np.zeros((995,995)) w
for i in range(995):
for j in range(995):
if i==j :
= 0
w[i,j] elif np.abs(i-j) <= 1 :
= 1 w[i,j]
= np.array(w.sum(axis=1))
d = np.diag(d)
D = np.array(np.diag(1/np.sqrt(d)) @ (D-w) @ np.diag(1/np.sqrt(d)))
L = np.linalg.eigh(L)
lamb, Psi = np.diag(lamb)
Lamb = Psi @ fhat_mean.reshape(995,1)
fhatbar = fhatbar**2
power = importr('EbayesThresh').ebayesthresh
ebayesthresh =np.array([np.array(ebayesthresh(FloatVector(fhatbar[i]**2))) for i in range(995)])
power_threshed= np.where(power_threshed>0,fhatbar,0)
fhatbar_threshed = Psi @ fhatbar_threshed
fhatbarhat = fhatbarhat.reshape(199,5,1) fhatbarhat_mean_spatio_temporal
0])
plt.plot(fhatbarhat_mean_spatio_temporal[:,1])
plt.plot(fhatbarhat_mean_spatio_temporal[:,2])
plt.plot(fhatbarhat_mean_spatio_temporal[:,3])
plt.plot(fhatbarhat_mean_spatio_temporal[:,4]) plt.plot(fhatbarhat_mean_spatio_temporal[:,
2) Random missing values
=np.zeros((995,995)) w
for i in range(995):
for j in range(995):
if i==j :
= 0
w[i,j] elif np.abs(i-j) <= 1 :
= 1 w[i,j]
= np.array(w.sum(axis=1))
d = np.diag(d)
D = np.array(np.diag(1/np.sqrt(d)) @ (D-w) @ np.diag(1/np.sqrt(d)))
L = np.linalg.eigh(L)
lamb, Psi = np.diag(lamb)
Lamb = Psi @ fhat_fiveVTSrandom_mean.reshape(995,1)
fhatbar = fhatbar**2
power = importr('EbayesThresh').ebayesthresh
ebayesthresh =np.array([np.array(ebayesthresh(FloatVector(fhatbar[i]**2))) for i in range(995)])
power_threshed= np.where(power_threshed>0,fhatbar,0)
fhatbar_threshed = Psi @ fhatbar_threshed
fhatbarhat = fhatbarhat.reshape(199,5,1) fhatbarhat_random_mean_spatio_temporal
0])
plt.plot(fhatbarhat_random_mean_spatio_temporal[:,1])
plt.plot(fhatbarhat_random_mean_spatio_temporal[:,2])
plt.plot(fhatbarhat_random_mean_spatio_temporal[:,3])
plt.plot(fhatbarhat_random_mean_spatio_temporal[:,4]) plt.plot(fhatbarhat_random_mean_spatio_temporal[:,
3) By 2
=np.zeros((995,995)) w
for i in range(995):
for j in range(995):
if i==j :
= 0
w[i,j] elif np.abs(i-j) <= 1 :
= 1 w[i,j]
= np.array(w.sum(axis=1))
d = np.diag(d)
D = np.array(np.diag(1/np.sqrt(d)) @ (D-w) @ np.diag(1/np.sqrt(d)))
L = np.linalg.eigh(L)
lamb, Psi = np.diag(lamb)
Lamb = Psi @ fhat_fiveVTStwo_mean.reshape(995,1)
fhatbar = fhatbar**2
power = importr('EbayesThresh').ebayesthresh
ebayesthresh =np.array([np.array(ebayesthresh(FloatVector(fhatbar[i]**2))) for i in range(995)])
power_threshed= np.where(power_threshed>0,fhatbar,0)
fhatbar_threshed = Psi @ fhatbar_threshed
fhatbarhat = fhatbarhat.reshape(199,5,1) fhatbarhat_two_mean_spatio_temporal
0])
plt.plot(fhatbarhat_two_mean_spatio_temporal[:,1])
plt.plot(fhatbarhat_two_mean_spatio_temporal[:,2])
plt.plot(fhatbarhat_two_mean_spatio_temporal[:,3])
plt.plot(fhatbarhat_two_mean_spatio_temporal[:,4]) plt.plot(fhatbarhat_two_mean_spatio_temporal[:,
3.2.linear interpolation
3.2.1. Temporal
1) Block
=np.zeros((5,199,199)) w
for k in range(5):
for i in range(199):
for j in range(199):
if i==j :
= 0
w[k,i,j] elif np.abs(i-j) <= 1 :
= 1 w[k,i,j]
= np.array([w[i].sum(axis=1) for i in range(5)])
d = np.array([np.diag(d[i]) for i in range(5)])
D= np.array([np.diag(1/np.sqrt(d[i])) @ (D[i]-w[i]) @ np.diag(1/np.sqrt(d[i])) for i in range(5)])
L = np.linalg.eigh(L)[0],np.linalg.eigh(L)[1]
lamb, Psi = np.array([np.diag(lamb[i]) for i in range(5)])
Lamb = np.hstack([Psi[i] @ fhat_linearinterpolation[:,i] for i in range(5)])
fhatbar = fhatbar.reshape(5,199)
_fhatbar = _fhatbar**2
power = importr('EbayesThresh').ebayesthresh
ebayesthresh =np.array([np.array(ebayesthresh(FloatVector(_fhatbar[i]**2))) for i in range(5)])
power_threshed= np.where(power_threshed>0,_fhatbar,0)
fhatbar_threshed = np.array([Psi[i] @ fhatbar_threshed[i] for i in range(5)])
fhatbarhat = fhatbarhat.reshape(199,-1) fhatbarhat_linearinterpolation_temporal
0])
plt.plot(fhatbarhat_linearinterpolation_temporal[:,1])
plt.plot(fhatbarhat_linearinterpolation_temporal[:,2])
plt.plot(fhatbarhat_linearinterpolation_temporal[:,3])
plt.plot(fhatbarhat_linearinterpolation_temporal[:,4]) plt.plot(fhatbarhat_linearinterpolation_temporal[:,
2) Random missing values
=np.zeros((5,199,199)) w
for k in range(5):
for i in range(199):
for j in range(199):
if i==j :
= 0
w[k,i,j] elif np.abs(i-j) <= 1 :
= 1 w[k,i,j]
= np.array([w[i].sum(axis=1) for i in range(5)])
d = np.array([np.diag(d[i]) for i in range(5)])
D= np.array([np.diag(1/np.sqrt(d[i])) @ (D[i]-w[i]) @ np.diag(1/np.sqrt(d[i])) for i in range(5)])
L = np.linalg.eigh(L)[0],np.linalg.eigh(L)[1]
lamb, Psi = np.array([np.diag(lamb[i]) for i in range(5)])
Lamb = np.hstack([Psi[i] @ fhat_fiveVTSrandom_linearinterpolation[:,i] for i in range(5)])
fhatbar = fhatbar.reshape(5,199)
_fhatbar = _fhatbar**2
power = importr('EbayesThresh').ebayesthresh
ebayesthresh =np.array([np.array(ebayesthresh(FloatVector(_fhatbar[i]**2))) for i in range(5)])
power_threshed= np.where(power_threshed>0,_fhatbar,0)
fhatbar_threshed = np.array([Psi[i] @ fhatbar_threshed[i] for i in range(5)])
fhatbarhat = fhatbarhat.reshape(199,-1) fhatbarhat_random_linearinterpolation_temporal
0])
plt.plot(fhatbarhat_random_linearinterpolation_temporal[:,1])
plt.plot(fhatbarhat_random_linearinterpolation_temporal[:,2])
plt.plot(fhatbarhat_random_linearinterpolation_temporal[:,3])
plt.plot(fhatbarhat_random_linearinterpolation_temporal[:,4]) plt.plot(fhatbarhat_random_linearinterpolation_temporal[:,
3) By 2
=np.zeros((5,199,199)) w
for k in range(5):
for i in range(199):
for j in range(199):
if i==j :
= 0
w[k,i,j] elif np.abs(i-j) <= 1 :
= 1 w[k,i,j]
= np.array([w[i].sum(axis=1) for i in range(5)])
d = np.array([np.diag(d[i]) for i in range(5)])
D= np.array([np.diag(1/np.sqrt(d[i])) @ (D[i]-w[i]) @ np.diag(1/np.sqrt(d[i])) for i in range(5)])
L = np.linalg.eigh(L)[0],np.linalg.eigh(L)[1]
lamb, Psi = np.array([np.diag(lamb[i]) for i in range(5)])
Lamb = np.hstack([Psi[i] @ fhat_fiveVTStwo_linearinterpolation[:,i] for i in range(5)])
fhatbar = fhatbar.reshape(5,199)
_fhatbar = _fhatbar**2
power = importr('EbayesThresh').ebayesthresh
ebayesthresh =np.array([np.array(ebayesthresh(FloatVector(_fhatbar[i]**2))) for i in range(5)])
power_threshed= np.where(power_threshed>0,_fhatbar,0)
fhatbar_threshed = np.array([Psi[i] @ fhatbar_threshed[i] for i in range(5)])
fhatbarhat = fhatbarhat.reshape(199,-1) fhatbarhat_two_linearinterpolation_temporal
0])
plt.plot(fhatbarhat_two_linearinterpolation_temporal[:,1])
plt.plot(fhatbarhat_two_linearinterpolation_temporal[:,2])
plt.plot(fhatbarhat_two_linearinterpolation_temporal[:,3])
plt.plot(fhatbarhat_two_linearinterpolation_temporal[:,4]) plt.plot(fhatbarhat_two_linearinterpolation_temporal[:,
3.2.2. Spatio
1) Block
=np.zeros((5,5)) w
for i in range(5):
for j in range(5):
if i==j :
= 0
w[i,j] elif np.abs(i-j) <= 1 :
= 1 w[i,j]
= np.array(w.sum(axis=1))
d = np.diag(d)
D = np.array(np.diag(1/np.sqrt(d)) @ (D-w) @ np.diag(1/np.sqrt(d)))
L = np.linalg.eigh(L)
lamb, Psi = np.diag(lamb)
Lamb = Psi @ fhat_linearinterpolation.reshape(5,199)
fhatbar = fhatbar**2
power = importr('EbayesThresh').ebayesthresh
ebayesthresh =np.array([np.array(ebayesthresh(FloatVector(fhatbar[i]**2))) for i in range(5)])
power_threshed= np.where(power_threshed>0,fhatbar,0)
fhatbar_threshed = Psi @ fhatbar_threshed
fhatbarhat = fhatbarhat.reshape(199,-1) fhatbarhat_linearinterpolation_spatio
0])
plt.plot(fhatbarhat_linearinterpolation_spatio[:,1])
plt.plot(fhatbarhat_linearinterpolation_spatio[:,2])
plt.plot(fhatbarhat_linearinterpolation_spatio[:,3])
plt.plot(fhatbarhat_linearinterpolation_spatio[:,4]) plt.plot(fhatbarhat_linearinterpolation_spatio[:,
2) Random missing values
=np.zeros((5,5)) w
for i in range(5):
for j in range(5):
if i==j :
= 0
w[i,j] elif np.abs(i-j) <= 1 :
= 1 w[i,j]
= np.array(w.sum(axis=1))
d = np.diag(d)
D = np.array(np.diag(1/np.sqrt(d)) @ (D-w) @ np.diag(1/np.sqrt(d)))
L = np.linalg.eigh(L)
lamb, Psi = np.diag(lamb)
Lamb = Psi @ fhat_fiveVTSrandom_linearinterpolation.reshape(5,199)
fhatbar = fhatbar**2
power = importr('EbayesThresh').ebayesthresh
ebayesthresh =np.array([np.array(ebayesthresh(FloatVector(fhatbar[i]**2))) for i in range(5)])
power_threshed= np.where(power_threshed>0,fhatbar,0)
fhatbar_threshed = Psi @ fhatbar_threshed
fhatbarhat = fhatbarhat.reshape(199,-1) fhatbarhat_random_linearinterpolation_spatio
0])
plt.plot(fhatbarhat_random_linearinterpolation_spatio[:,1])
plt.plot(fhatbarhat_random_linearinterpolation_spatio[:,2])
plt.plot(fhatbarhat_random_linearinterpolation_spatio[:,3])
plt.plot(fhatbarhat_random_linearinterpolation_spatio[:,4]) plt.plot(fhatbarhat_random_linearinterpolation_spatio[:,
3) By 2
=np.zeros((5,5)) w
for i in range(5):
for j in range(5):
if i==j :
= 0
w[i,j] elif np.abs(i-j) <= 1 :
= 1 w[i,j]
= np.array(w.sum(axis=1))
d = np.diag(d)
D = np.array(np.diag(1/np.sqrt(d)) @ (D-w) @ np.diag(1/np.sqrt(d)))
L = np.linalg.eigh(L)
lamb, Psi = np.diag(lamb)
Lamb = Psi @ fhat_fiveVTStwo_linearinterpolation.reshape(5,199)
fhatbar = fhatbar**2
power = importr('EbayesThresh').ebayesthresh
ebayesthresh =np.array([np.array(ebayesthresh(FloatVector(fhatbar[i]**2))) for i in range(5)])
power_threshed= np.where(power_threshed>0,fhatbar,0)
fhatbar_threshed = Psi @ fhatbar_threshed
fhatbarhat = fhatbarhat.reshape(199,-1) fhatbarhat_two_linearinterpolation_spatio
0])
plt.plot(fhatbarhat_two_linearinterpolation_spatio[:,1])
plt.plot(fhatbarhat_two_linearinterpolation_spatio[:,2])
plt.plot(fhatbarhat_two_linearinterpolation_spatio[:,3])
plt.plot(fhatbarhat_two_linearinterpolation_spatio[:,4]) plt.plot(fhatbarhat_two_linearinterpolation_spatio[:,
3.2.3. Spatio-Temporal
1) Block
=np.zeros((995,995)) w
for i in range(995):
for j in range(995):
if i==j :
= 0
w[i,j] elif np.abs(i-j) <= 1 :
= 1 w[i,j]
= np.array(w.sum(axis=1))
d = np.diag(d)
D = np.array(np.diag(1/np.sqrt(d)) @ (D-w) @ np.diag(1/np.sqrt(d)))
L = np.linalg.eigh(L)
lamb, Psi = np.diag(lamb)
Lamb = Psi @ fhat_linearinterpolation.reshape(995,1)
fhatbar = fhatbar**2
power = importr('EbayesThresh').ebayesthresh
ebayesthresh =np.array([np.array(ebayesthresh(FloatVector(fhatbar[i]**2))) for i in range(995)])
power_threshed= np.where(power_threshed>0,fhatbar,0)
fhatbar_threshed = Psi @ fhatbar_threshed
fhatbarhat = fhatbarhat.reshape(199,5,1) fhat_linearinterpolation_spatio_temporal
0])
plt.plot(fhat_linearinterpolation_spatio_temporal[:,1])
plt.plot(fhat_linearinterpolation_spatio_temporal[:,2])
plt.plot(fhat_linearinterpolation_spatio_temporal[:,3])
plt.plot(fhat_linearinterpolation_spatio_temporal[:,4]) plt.plot(fhat_linearinterpolation_spatio_temporal[:,
2) Random missing values
=np.zeros((995,995)) w
for i in range(995):
for j in range(995):
if i==j :
= 0
w[i,j] elif np.abs(i-j) <= 1 :
= 1 w[i,j]
= np.array(w.sum(axis=1))
d = np.diag(d)
D = np.array(np.diag(1/np.sqrt(d)) @ (D-w) @ np.diag(1/np.sqrt(d)))
L = np.linalg.eigh(L)
lamb, Psi = np.diag(lamb)
Lamb = Psi @ fhat_fiveVTSrandom_linearinterpolation.reshape(995,1)
fhatbar = fhatbar**2
power = importr('EbayesThresh').ebayesthresh
ebayesthresh =np.array([np.array(ebayesthresh(FloatVector(fhatbar[i]**2))) for i in range(995)])
power_threshed= np.where(power_threshed>0,fhatbar,0)
fhatbar_threshed = Psi @ fhatbar_threshed
fhatbarhat = fhatbarhat.reshape(199,5,1) fhat_random_linearinterpolation_spatio_temporal
0])
plt.plot(fhat_random_linearinterpolation_spatio_temporal[:,1])
plt.plot(fhat_random_linearinterpolation_spatio_temporal[:,2])
plt.plot(fhat_random_linearinterpolation_spatio_temporal[:,3])
plt.plot(fhat_random_linearinterpolation_spatio_temporal[:,4]) plt.plot(fhat_random_linearinterpolation_spatio_temporal[:,
3) By 2
=np.zeros((995,995)) w
for i in range(995):
for j in range(995):
if i==j :
= 0
w[i,j] elif np.abs(i-j) <= 1 :
= 1 w[i,j]
= np.array(w.sum(axis=1))
d = np.diag(d)
D = np.array(np.diag(1/np.sqrt(d)) @ (D-w) @ np.diag(1/np.sqrt(d)))
L = np.linalg.eigh(L)
lamb, Psi = np.diag(lamb)
Lamb = Psi @ fhat_fiveVTStwo_linearinterpolation.reshape(995,1)
fhatbar = fhatbar**2
power = importr('EbayesThresh').ebayesthresh
ebayesthresh =np.array([np.array(ebayesthresh(FloatVector(fhatbar[i]**2))) for i in range(995)])
power_threshed= np.where(power_threshed>0,fhatbar,0)
fhatbar_threshed = Psi @ fhatbar_threshed
fhatbarhat = fhatbarhat.reshape(199,5,1) fhat_two_linearinterpolation_spatio_temporal
0])
plt.plot(fhat_two_linearinterpolation_spatio_temporal[:,1])
plt.plot(fhat_two_linearinterpolation_spatio_temporal[:,2])
plt.plot(fhat_two_linearinterpolation_spatio_temporal[:,3])
plt.plot(fhat_two_linearinterpolation_spatio_temporal[:,4]) plt.plot(fhat_two_linearinterpolation_spatio_temporal[:,
3.3. original
1) Temporal
=np.zeros((5,199,199)) w
for k in range(5):
for i in range(199):
for j in range(199):
if i==j :
= 0
w[k,i,j] elif np.abs(i-j) <= 1 :
= 1 w[k,i,j]
= np.array([w[i].sum(axis=1) for i in range(5)])
d = np.array([np.diag(d[i]) for i in range(5)])
D= np.array([np.diag(1/np.sqrt(d[i])) @ (D[i]-w[i]) @ np.diag(1/np.sqrt(d[i])) for i in range(5)])
L = np.linalg.eigh(L)[0],np.linalg.eigh(L)[1]
lamb, Psi = np.array([np.diag(lamb[i]) for i in range(5)])
Lamb = np.hstack([Psi[i] @ fhat_fiveVTS[:,i] for i in range(5)])
fhatbar = fhatbar.reshape(5,199)
_fhatbar = _fhatbar**2
power = importr('EbayesThresh').ebayesthresh
ebayesthresh =np.array([np.array(ebayesthresh(FloatVector(_fhatbar[i]**2))) for i in range(5)])
power_threshed= np.where(power_threshed>0,_fhatbar,0)
fhatbar_threshed = np.array([Psi[i] @ fhatbar_threshed[i] for i in range(5)])
fhatbarhat = fhatbarhat.reshape(199,-1) fhatbarhat_temporal
0])
plt.plot(fhatbarhat_temporal[:,1])
plt.plot(fhatbarhat_temporal[:,2])
plt.plot(fhatbarhat_temporal[:,3])
plt.plot(fhatbarhat_temporal[:,4]) plt.plot(fhatbarhat_temporal[:,
2) Spatio
=np.zeros((5,5)) w
for i in range(5):
for j in range(5):
if i==j :
= 0
w[i,j] elif np.abs(i-j) <= 1 :
= 1 w[i,j]
= np.array(w.sum(axis=1))
d = np.diag(d)
D = np.array(np.diag(1/np.sqrt(d)) @ (D-w) @ np.diag(1/np.sqrt(d)))
L = np.linalg.eigh(L)
lamb, Psi = np.diag(lamb)
Lamb = Psi @ fhat_fiveVTS.reshape(5,199)
fhatbar = fhatbar**2
power = importr('EbayesThresh').ebayesthresh
ebayesthresh =np.array([np.array(ebayesthresh(FloatVector(fhatbar[i]**2))) for i in range(5)])
power_threshed= np.where(power_threshed>0,fhatbar,0)
fhatbar_threshed = Psi @ fhatbar_threshed
fhatbarhat = fhatbarhat.reshape(199,-1) fhatbarhat_spatio
0])
plt.plot(fhatbarhat_spatio[:,1])
plt.plot(fhatbarhat_spatio[:,2])
plt.plot(fhatbarhat_spatio[:,3])
plt.plot(fhatbarhat_spatio[:,4]) plt.plot(fhatbarhat_spatio[:,
3) Spatio-Temporal
=np.zeros((995,995)) w
for i in range(995):
for j in range(995):
if i==j :
= 0
w[i,j] elif np.abs(i-j) <= 1 :
= 1 w[i,j]
= np.array(w.sum(axis=1))
d = np.diag(d)
D = np.array(np.diag(1/np.sqrt(d)) @ (D-w) @ np.diag(1/np.sqrt(d)))
L = np.linalg.eigh(L)
lamb, Psi = np.diag(lamb)
Lamb = Psi @ fhat_fiveVTS.reshape(995,1)
fhatbar = fhatbar**2
power = importr('EbayesThresh').ebayesthresh
ebayesthresh =np.array([np.array(ebayesthresh(FloatVector(fhatbar[i]**2))) for i in range(995)])
power_threshed= np.where(power_threshed>0,fhatbar,0)
fhatbar_threshed = Psi @ fhatbar_threshed
fhatbarhat = fhatbarhat.reshape(199,5,1) fhat_spatio_temporal
0])
plt.plot(fhat_spatio_temporal[:,1])
plt.plot(fhat_spatio_temporal[:,2])
plt.plot(fhat_spatio_temporal[:,3])
plt.plot(fhat_spatio_temporal[:,4]) plt.plot(fhat_spatio_temporal[:,
4. 1로 돌아가서 반복
Original f 진행한 것과 비교
2] - fhatbarhat_mean_temporal[:,2])**2)
plt.plot((fhatbarhat_temporal[:,2] - fhatbarhat_random_mean_temporal[:,2])**2)
plt.plot((fhatbarhat_temporal[:,2] - fhatbarhat_twomean_temporal[:,2])**2) plt.plot((fhatbarhat_temporal[:,
2] - fhatbarhat_linearinterpolation_temporal[:,2])**2)
plt.plot((fhatbarhat_temporal[:,2] - fhatbarhat_random_linearinterpolation_temporal[:,2])**2)
plt.plot((fhatbarhat_temporal[:,2] - fhatbarhat_two_linearinterpolation_temporal[:,2])**2) plt.plot((fhatbarhat_temporal[:,
2] - fhatbarhat_mean_spatio[:,2])**2)
plt.plot((fhatbarhat_spatio[:,2] - fhatbarhat_random_mean_spatio[:,2])**2)
plt.plot((fhatbarhat_spatio[:,2] - fhatbarhat_two_mean_spatio[:,2])**2) plt.plot((fhatbarhat_spatio[:,
2] - fhatbarhat_linearinterpolation_spatio[:,2])**2)
plt.plot((fhatbarhat_spatio[:,2] - fhatbarhat_random_linearinterpolation_spatio[:,2])**2)
plt.plot((fhatbarhat_spatio[:,2] - fhatbarhat_two_linearinterpolation_spatio[:,2])**2) plt.plot((fhatbarhat_spatio[:,
2] - fhatbarhat_mean_spatio_temporal[:,2])**2)
plt.plot((fhat_spatio_temporal[:,2] - fhatbarhat_random_mean_spatio_temporal[:,2])**2)
plt.plot((fhat_spatio_temporal[:,2] - fhatbarhat_two_mean_spatio_temporal[:,2])**2) plt.plot((fhat_spatio_temporal[:,
2] - fhat_linearinterpolation_spatio_temporal[:,2])**2)
plt.plot((fhat_spatio_temporal[:,2] - fhat_random_linearinterpolation_spatio_temporal[:,2])**2)
plt.plot((fhat_spatio_temporal[:,2] - fhat_two_linearinterpolation_spatio_temporal[:,2])**2) plt.plot((fhat_spatio_temporal[:,