Created
July 14, 2016 12:59
-
-
Save MatsuuraKentaro/1cc985e4b42310e712884b6a35aab490 to your computer and use it in GitHub Desktop.
slide min
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
functions { | |
vector slide_min(int I, int K, vector x) { | |
vector[I-K+1] res; | |
int deq[I]; | |
int s; | |
int t; | |
s = 1; | |
t = 1; | |
for (i in 1:I) { | |
while (s < t && x[deq[t-1]] >= x[i]) | |
t = t-1; | |
deq[t] = i; | |
t = t+1; | |
if (i-K+1 >= 1) { | |
res[i-K+1] = x[deq[s]]; | |
if (deq[s] == i-K+1) | |
s = s+1; | |
} | |
} | |
return res; | |
} | |
} | |
data { | |
int I; | |
int I_obs; | |
int I_mis; | |
int Idx_obs[I_obs]; | |
int Idx_mis[I_mis]; | |
vector[I_obs] X_obs; | |
int K; | |
vector[I-K+1] Y; | |
} | |
parameters { | |
vector[I_mis] x_mis; | |
real<lower=0> s_y; | |
} | |
transformed parameters { | |
vector[I-K+1] mu; | |
{ | |
vector[I] x; | |
for (i in 1:I_obs) x[Idx_obs[i]] = X_obs[i]; | |
for (i in 1:I_mis) x[Idx_mis[i]] = x_mis[i]; | |
mu = slide_min(I, K, x); | |
} | |
} | |
model { | |
x_mis ~ normal(0, 5); | |
Y ~ normal(mu, s_y); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(rstan) | |
stanmodel <- stan_model(file='model.stan') | |
d <- read.csv(file='z-data.csv') | |
I <- 100 | |
K <- 8 | |
Idx_obs <- which(!is.na(d$X_obs)) | |
Idx_mis <- setdiff(seq_len(I), Idx_obs) | |
I_obs <- length(Idx_obs) | |
I_mis <- I - I_obs | |
X_obs <- d$X_obs[Idx_obs] | |
Y <- d$Y[1:(I-K+1)] | |
data <- list(I=I, I_obs=I_obs, I_mis=I_mis, Idx_obs=Idx_obs, Idx_mis=Idx_mis, X_obs=X_obs, K=K, Y=Y) | |
fit <- sampling(stanmodel, pars=c('x_mis', 's_y'), data=data, seed=123) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
i | X_true | X_obs | Y | |
---|---|---|---|---|
1 | -2.8 | NA | -6.21 | |
2 | -1.15 | NA | -6.29 | |
3 | 7.79 | NA | -6.59 | |
4 | 0.35 | NA | -6.67 | |
5 | 0.65 | 0.65 | -6.74 | |
6 | 8.58 | NA | -6.28 | |
7 | 2.3 | NA | -6.71 | |
8 | -6.33 | NA | -6.53 | |
9 | -3.43 | NA | -3.53 | |
10 | -2.23 | NA | -2.04 | |
11 | 6.12 | NA | -10.09 | |
12 | 1.8 | NA | -9.74 | |
13 | 2 | NA | -9.8 | |
14 | 0.55 | NA | -10.21 | |
15 | -2.78 | NA | -9.86 | |
16 | 8.93 | 8.93 | -9.25 | |
17 | 2.49 | NA | -9.65 | |
18 | -9.83 | NA | -9.81 | |
19 | 3.51 | NA | -8.6 | |
20 | -2.36 | -2.36 | -9.25 | |
21 | -5.34 | NA | -7.98 | |
22 | -1.09 | NA | -9.01 | |
23 | -5.13 | -5.13 | -8.13 | |
24 | -3.64 | -3.64 | -7.67 | |
25 | -3.13 | NA | -9.01 | |
26 | -8.43 | NA | -8.15 | |
27 | 4.19 | 4.19 | -5.79 | |
28 | 0.77 | NA | -6.32 | |
29 | -5.69 | NA | -6.3 | |
30 | 6.27 | 6.27 | -2.12 | |
31 | 2.13 | NA | -1.69 | |
32 | -1.48 | NA | -2.11 | |
33 | 4.48 | NA | -1.62 | |
34 | 4.39 | 4.39 | -2.63 | |
35 | 4.11 | 4.11 | -3.98 | |
36 | 3.44 | 3.44 | -6.01 | |
37 | 2.77 | NA | -6.02 | |
38 | -0.31 | NA | -6.2 | |
39 | -1.53 | -1.53 | -6.73 | |
40 | -1.9 | NA | -6.38 | |
41 | -3.47 | -3.47 | -6.44 | |
42 | -1.04 | NA | -6.1 | |
43 | -6.33 | NA | -6.48 | |
44 | 10.84 | 10.84 | -5.23 | |
45 | 6.04 | NA | -5.77 | |
46 | -5.62 | NA | -5.2 | |
47 | -2.01 | NA | -2.75 | |
48 | -2.33 | NA | -2.83 | |
49 | 3.9 | NA | 0.17 | |
50 | -0.42 | -0.42 | -7.91 | |
51 | 1.27 | 1.27 | -7.62 | |
52 | -0.14 | NA | -7.49 | |
53 | -0.21 | -0.21 | -7.93 | |
54 | 6.84 | NA | -7.53 | |
55 | -1.13 | NA | -7.59 | |
56 | 7.58 | NA | -7.83 | |
57 | -7.74 | -7.74 | -7.71 | |
58 | 2.92 | NA | -5.37 | |
59 | 0.62 | 0.62 | -4.51 | |
60 | 1.08 | 1.08 | -5.66 | |
61 | 1.9 | NA | -5.8 | |
62 | -2.51 | NA | -5.34 | |
63 | -1.67 | NA | -5.24 | |
64 | -5.09 | NA | -5.19 | |
65 | -5.36 | NA | -11.73 | |
66 | 1.52 | 1.52 | -11.98 | |
67 | 2.24 | NA | -11.04 | |
68 | 0.27 | 0.27 | -11.69 | |
69 | 4.61 | NA | -11.9 | |
70 | 10.25 | NA | -11.64 | |
71 | -2.46 | NA | -11.63 | |
72 | -11.55 | NA | -11.11 | |
73 | 5.03 | NA | -6.07 | |
74 | -3.55 | NA | -5.8 | |
75 | -3.44 | -3.44 | -6.3 | |
76 | 5.13 | 5.13 | -6.01 | |
77 | -1.42 | NA | -6.23 | |
78 | -6.1 | NA | -6.06 | |
79 | 0.91 | NA | -2.21 | |
80 | -0.69 | NA | -2.37 | |
81 | 0.03 | NA | -1.05 | |
82 | 1.93 | NA | -1.61 | |
83 | -1.85 | NA | -2.35 | |
84 | 3.22 | 3.22 | -1.87 | |
85 | -1.1 | NA | -2.1 | |
86 | 1.66 | 1.66 | -0.75 | |
87 | 5.48 | NA | -2.62 | |
88 | 2.18 | NA | -3.25 | |
89 | -1.63 | -1.63 | -2.92 | |
90 | 5.74 | NA | -3.31 | |
91 | 4.97 | NA | -3.33 | |
92 | 2.74 | 2.74 | -3.46 | |
93 | 1.19 | NA | -5.37 | |
94 | -3.14 | NA | NA | |
95 | 6.8 | 6.8 | NA | |
96 | -3 | -3 | NA | |
97 | 10.94 | NA | NA | |
98 | 7.66 | 7.66 | NA | |
99 | -1.18 | NA | NA | |
100 | -5.13 | NA | NA |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment