-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunSCB.R
149 lines (119 loc) · 4.17 KB
/
runSCB.R
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
rm (list = ls())
if (!require("rstudioapi"))
install.packages("rstudioapi")
library(rstudioapi)
setwd(dirname(rstudioapi::getActiveDocumentContext()$path))
if (!require("roxygen2"))
install.packages("roxygen2")
library(roxygen2)
if (!require("devtools"))
install.packages("devtools")
library(devtools)
if (!require("ggplot2"))
install.packages("ggplot2")
require(ggplot2)
if (!require("gridExtra"))
install.packages("gridExtra")
library(devtools)
require(gridExtra)
# if (!require("KernSmooth"))
# install.packages("KernSmooth")
# library(KernSmooth)
# require(KernSmooth)
setwd("SCB/") # error? at this level there is no more scb
roxygen2::roxygenise()
devtools::document()
# Loading the functions
<<<<<<< HEAD
noiseMean = 0
noiseSd = 1
sampleSize = 100
corruption = sin #function(x){sqrt(x)}
T <- createT(sampleSize = sampleSize)
T
Psi <- createPsi(model = corruption,
time = T)
Psi
Z = createZ(sampleSize = sampleSize,
mean = noiseMean,
sd = noiseSd)
Z
X <- createX(model = ma1,
sampleSize = sampleSize,
Z = Z,
psi = Psi)
X
corruptedMA1 = data.frame(T*sampleSize,X)
colnames(corruptedMA1) <- c('time','sample')
pl_title = sprintf("Noise distribution N(%d,%.2f), corruption is sin, time steps %d", noiseMean, noiseSd, sampleSize)
noise_mean = 0
noise_sd = 1
sampling_time = 350
corruption = sin #function(x){sqrt(x)}
noise <- createSleeper(number_steps = sampling_time)
noise
psi <- createPsi(model = corruption,
sleeper = sleeper)
psi
noise = createNoise(time = sampling_time,
mean = noise_mean,
sd = noise_sd)
noise
sample <- createSample(model = ma1,
time = sampling_time,
noise = noise,
psi = psi)
sample
corrupted_ma1 = data.frame(T*sampling_time, sample)
colnames(corrupted_ma1) <- c('time','sample')
pl_title = sprintf("Noise distribution N(%d,%.2f), corruption is sin, time steps %d", noise_mean, noise_sd, sampling_time)
>>>>>>> 4bfb282544601b59851f24cef9e00599c5b92b58
if (! dir.exists ("Plots"))
dir.create("Plots")
setwd ("Plots")
jpeg(filename = paste0(pl_title, ".jpg"), width = 2*585, height = 2*403, units = "px", quality = 80)
ggplot(data=corruptedMA1, aes(x = time, y = sample)) + geom_line() +
xlab("Time steps") +
ylab("Sample path") +
ggtitle(pl_title) +
theme(plot.title = element_text(face = "bold", hjust = .5, size = 25), axis.text = element_text(size=20), axis.title = element_text(size = 20))
graphics.off()
# dev.off()
setwd ("..")
getwd()
jpeg (filename = "tmp0.jpeg")
dev.off()
<<<<<<< HEAD
# W-process generation & plotting
gaussKernel <- function(x){
1/sqrt(2*pi)*exp(-0.5*x^2)
}
sigmoidKernel <- function(x){
2/pi * 1/(exp(x) + exp(-x))
}
W <- data.frame(cbind(1:sampleSize, createW(kernel=sigmoidKernel,bandwidth=0.5,sampleSize)))
colnames(W) <- c('time', 'W')
ggplot(data = W, aes(x=time, y=W)) + geom_line() + ggtitle('W kernel process') +
theme(plot.title = element_text(face = "bold", hjust = .5, size = 20), axis.text = element_text(size=15), axis.title = element_text(size = 15))
# Bootstraps for MA1
psiFixed = 0.5
bSampleSize = 10
bandwidth = 0.5
sourceMA1 = ma1(sampleSize, psi = rep(psiFixed, sampleSize), rnorm(sampleSize,0,1))
bootstrappedSamples = bootstrapOnMA1(sourceMA1, bSampleSize = bSampleSize, kernel = sigmoidKernel, bandwidth = bandwidth)
b1 = data.frame(cbind(1:sampleSize, bootstrappedSamples[1, ]))
colnames(b1) <- c('time', 'sample')
b2 = data.frame(cbind(1:sampleSize, bootstrappedSamples[2, ]))
colnames(b2) <- c('time', 'sample')
bootstr1 = ggplot(b1, aes(x=time, y=sample)) + geom_line()
bootstr2 = ggplot(b2, aes(x=time, y=sample)) + geom_line()
grid.arrange(bootstr1, bootstr2, ncol=2, nrow=1)
=======
gauss_ker <- function(x){
1/sqrt(2*pi)*exp(-0.5*x^2)
}
W <- data.frame(cbind(1:sampling_time, createBootstrapMultiplier(kernel=gauss_ker, bandwidth=0.5, sampling_time)))
colnames(W) <- c('time', 'W')
ggplot(data = W, aes(x=time, y=W)) + geom_line() + ggtitle('W kernel process') +
theme(plot.title = element_text(face = "bold", hjust = .5, size = 20), axis.text = element_text(size=15), axis.title = element_text(size = 15))
>>>>>>> 4bfb282544601b59851f24cef9e00599c5b92b58